308a7b219422cef29c6a43c9080d92ba11fff6a9
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / effects / qc / casings.qc
1 #ifdef SVQC
2 void SpawnCasing(vector vel, float randomvel, vector ang, vector avel, float randomavel, int casingtype, entity casingowner);
3 #endif
4
5 #ifdef IMPLEMENTATION
6
7 #include <common/util.qh>
8
9 #ifdef CSQC
10 #include <common/physics/movetypes/movetypes.qh>
11 #include "rubble.qh"
12 #endif
13
14 REGISTER_NET_TEMP(casings)
15
16 #ifdef SVQC
17 void SpawnCasing(vector vel, float randomvel, vector ang, vector avel, float randomavel, int casingtype, entity casingowner)
18 {
19     .entity weaponentity = weaponentities[0]; // TODO: parameter
20     entity wep = casingowner.(weaponentity);
21     vector org = casingowner.origin + casingowner.view_ofs + wep.spawnorigin.x * v_forward - wep.spawnorigin.y * v_right + wep.spawnorigin.z * v_up;
22
23     if (!sound_allowed(MSG_BROADCAST, casingowner))
24         casingtype |= 0x80;
25
26     WriteHeader(MSG_ALL, casings);
27     WriteByte(MSG_ALL, casingtype);
28     WriteCoord(MSG_ALL, org.x);
29     WriteCoord(MSG_ALL, org.y);
30     WriteCoord(MSG_ALL, org.z);
31     WriteShort(MSG_ALL, compressShortVector(vel)); // actually compressed velocity
32     WriteByte(MSG_ALL, ang.x * 256 / 360);
33     WriteByte(MSG_ALL, ang.y * 256 / 360);
34     WriteByte(MSG_ALL, ang.z * 256 / 360);
35 }
36 #endif
37
38 #ifdef CSQC
39 entityclass(Casing);
40 class(Casing) .float alpha;
41 class(Casing) .bool silent;
42 class(Casing) .int state;
43 class(Casing) .float cnt;
44
45 void Casing_Delete(entity this)
46 {
47     remove(this);
48 }
49
50 void Casing_Draw(entity this)
51 {
52     if (this.move_flags & FL_ONGROUND)
53     {
54         this.move_angles_x = 0;
55         this.move_angles_z = 0;
56         UNSET_ONGROUND(this);
57     }
58
59     Movetype_Physics_MatchTicrate(this, autocvar_cl_casings_ticrate, autocvar_cl_casings_sloppy);
60     if (wasfreed(this))
61         return; // deleted by touch function
62
63     this.renderflags = 0;
64     this.alpha = bound(0, this.cnt - time, 1);
65
66     if (this.alpha < ALPHA_MIN_VISIBLE)
67     {
68         Casing_Delete(this);
69         this.drawmask = 0;
70     }
71 }
72
73 SOUND(BRASS1, W_Sound("brass1"));
74 SOUND(BRASS2, W_Sound("brass2"));
75 SOUND(BRASS3, W_Sound("brass3"));
76 Sound SND_BRASS_RANDOM() {
77     return Sounds_from(SND_BRASS1.m_id + floor(prandom() * 3));
78 }
79 SOUND(CASINGS1, W_Sound("casings1"));
80 SOUND(CASINGS2, W_Sound("casings2"));
81 SOUND(CASINGS3, W_Sound("casings3"));
82 Sound SND_CASINGS_RANDOM() {
83     return Sounds_from(SND_CASINGS1.m_id + floor(prandom() * 3));
84 }
85
86 void Casing_Touch(entity this, entity toucher)
87 {
88     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
89     {
90         Casing_Delete(this);
91         return;
92     }
93
94     if (!this.silent)
95     if (!trace_ent || trace_ent.solid == SOLID_BSP)
96     {
97         if(vdist(this.velocity, >, 50))
98         {
99             if (time >= this.nextthink)
100             {
101                 Sound s;
102                 switch (this.state)
103                 {
104                     case 1:
105                         s = SND_CASINGS_RANDOM();
106                         break;
107                     default:
108                         s = SND_BRASS_RANDOM();
109                         break;
110                 }
111
112                 sound (this, CH_SHOTS, s, VOL_BASE, ATTEN_LARGE);
113             }
114         }
115     }
116
117     this.nextthink = time + 0.2;
118 }
119
120 void Casing_Damage(entity this, float thisdmg, int hittype, vector org, vector thisforce)
121 {
122     if (thisforce.z < 0)
123         thisforce.z = 0;
124     this.move_velocity = this.move_velocity + thisforce + '0 0 100';
125     this.move_flags &= ~FL_ONGROUND;
126 }
127
128 NET_HANDLE(casings, bool isNew)
129 {
130     int _state = ReadByte();
131     vector org;
132     org_x = ReadCoord();
133     org_y = ReadCoord();
134     org_z = ReadCoord();
135     vector vel = decompressShortVector(ReadShort());
136     vector ang;
137     ang_x = ReadByte() * 360 / 256;
138     ang_y = ReadByte() * 360 / 256;
139     ang_z = ReadByte() * 360 / 256;
140     return = true;
141
142     if (!autocvar_cl_casings) return;
143
144     Casing casing = RubbleNew("casing");
145     casing.silent = (_state & 0x80);
146     casing.state = (_state & 0x7F);
147     casing.origin = org;
148     setorigin(casing, casing.origin);
149     casing.velocity = vel;
150     casing.angles = ang;
151     casing.drawmask = MASK_NORMAL;
152
153     casing.draw = Casing_Draw;
154     casing.move_origin = casing.origin;
155     casing.move_velocity = casing.velocity + 2 * prandomvec();
156     casing.move_angles = casing.angles;
157     casing.move_avelocity = '0 250 0' + 100 * prandomvec();
158     casing.move_movetype = MOVETYPE_BOUNCE;
159     settouch(casing, Casing_Touch);
160     casing.move_time = time;
161     casing.event_damage = Casing_Damage;
162     casing.solid = SOLID_TRIGGER;
163
164     switch (casing.state)
165     {
166         case 1:
167             setmodel(casing, MDL_CASING_SHELL);
168             casing.cnt = time + autocvar_cl_casings_shell_time;
169             break;
170         default:
171             setmodel(casing, MDL_CASING_BULLET);
172             casing.cnt = time + autocvar_cl_casings_bronze_time;
173             break;
174     }
175
176     setsize(casing, '0 0 -1', '0 0 -1');
177
178     RubbleLimit("casing", autocvar_cl_casings_maxcount, Casing_Delete);
179 }
180
181 #endif
182 #endif