Merge remote branch 'origin/mirceakitsune/per_character_sounds'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / projectile.qc
1 .vector iorigin1, iorigin2;
2 .float spawntime;
3 .vector trail_oldorigin;
4 .float trail_oldtime;
5 .float fade_time, fade_rate;
6
7 void SUB_Null()
8 {
9 }
10
11 void SUB_Stop()
12 {
13         self.move_velocity = self.move_avelocity = '0 0 0';
14         self.move_movetype = MOVETYPE_NONE;
15 }
16
17 .float alphamod;
18 .float count; // set if clientside projectile
19 .float cnt; // sound index
20 .float gravity;
21 .float snd_looping;
22 .float silent;
23 .float traileffect;
24
25 void Projectile_DrawTrail(vector to)
26 {
27         vector from;
28         float t0;
29         from = self.trail_oldorigin;
30         t0 = self.trail_oldtime;
31         self.trail_oldorigin = to;
32         self.trail_oldtime = time;
33
34         // force the effect even for stationary firemine
35         if(self.cnt == PROJECTILE_FIREMINE)
36                 if(from == to)
37                         from_z += 1;
38
39         if (self.traileffect)
40                 trailparticles(self, self.traileffect, from, to);
41 }
42
43 void Projectile_Draw()
44 {
45         vector rot;
46         vector trailorigin;
47         float f;
48         float drawn;
49         float t;
50         float a;
51
52         f = self.move_flags;
53
54         if(self.count & 0x80)
55         {
56                 //self.move_flags &~= FL_ONGROUND;
57                 Movetype_Physics_MatchServer(autocvar_cl_projectiles_sloppy);
58                 if(!(self.move_flags & FL_ONGROUND))
59                         self.angles = vectoangles(self.velocity);
60         }
61         else
62         {
63                 InterpolateOrigin_Do();
64         }
65
66         if(self.count & 0x80)
67         {
68                 drawn = (time >= self.spawntime - 0.02);
69                 t = max(time, self.spawntime);
70         }
71         else
72         {
73                 drawn = (self.iflags & IFLAG_VALID);
74                 t = time;
75         }
76
77         if(!(f & FL_ONGROUND))
78         {
79                 rot = '0 0 0';
80                 switch(self.cnt)
81                 {
82                         /*
83                         case PROJECTILE_GRENADE:
84                                 rot = '-2000 0 0'; // forward
85                                 break;
86                         */
87                         case PROJECTILE_GRENADE_BOUNCING:
88                                 rot = '0 -1000 0'; // sideways
89                                 break;
90                         case PROJECTILE_HOOKBOMB:
91                                 rot = '1000 0 0'; // forward
92                                 break;
93                         default:
94                                 break;
95                 }
96                 self.angles = AnglesTransform_ToAngles(AnglesTransform_Multiply(AnglesTransform_FromAngles(self.angles), rot * (t - self.spawntime)));
97         }
98
99         vector ang;
100         ang = self.angles;
101         ang_x = -ang_x;
102         makevectors(ang);
103
104         a = 1 - (time - self.fade_time) * self.fade_rate;
105         if(a <= 0)
106                 drawn = 0;
107
108         trailorigin = self.origin;
109         switch(self.cnt)
110         {
111                 case PROJECTILE_GRENADE:
112                 case PROJECTILE_GRENADE_BOUNCING:
113                         trailorigin += v_right * 1 + v_forward * -10;
114                         break;
115                 default:
116                         break;
117         }
118         if(drawn)
119                 Projectile_DrawTrail(trailorigin);
120         else
121         {
122                 self.trail_oldorigin = trailorigin;
123                 self.trail_oldtime = time;
124         }
125
126         if(!drawn)
127                 return;
128
129         switch(self.cnt)
130         {
131                 case PROJECTILE_BULLET_GLOWING:
132                 case PROJECTILE_BULLET_GLOWING_TRACER:
133                         R_AddDynamicLight(self.origin, 50 * a, '1 1 0');
134                         break;
135                 default:
136                         break;
137         }
138
139         self.alpha = self.alphamod * a;
140         self.renderflags = 0;
141
142         R_AddEntity(self);
143 }
144
145 void loopsound(entity e, float ch, string samp, float vol, float attn)
146 {
147         if(self.silent)
148                 return;
149
150         sound(e, ch, samp, vol, attn);
151         e.snd_looping = 1;
152 }
153
154 void Ent_RemoveProjectile()
155 {
156         if(self.snd_looping)
157                 sound(self, CHAN_PROJECTILE, "misc/null.wav", VOL_BASE, ATTN_NORM);
158
159         if(self.count & 0x80)
160         {
161                 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.05, MOVE_NORMAL, self);
162                 Projectile_DrawTrail(trace_endpos);
163         }
164 }
165
166 void Ent_Projectile()
167 {
168         float f;
169
170         // projectile properties:
171         //   kind (interpolated, or clientside)
172         //
173         //   modelindex
174         //   origin
175         //   scale
176         //   if clientside:
177         //     velocity
178         //     gravity
179         //   soundindex (hardcoded list)
180         //   effects
181         //
182         // projectiles don't send angles, because they always follow the velocity
183         
184         f = ReadByte();
185         self.count = (f & 0x80);
186         self.iflags = (self.iflags & IFLAG_INTERNALMASK) | IFLAG_AUTOANGLES | IFLAG_ANGLES;
187         self.solid = SOLID_TRIGGER;
188         //self.effects = EF_NOMODELFLAGS;
189
190         // this should make collisions with bmodels more exact, but it leads to
191         // projectiles no longer being able to lie on a bmodel
192         self.move_nomonsters = MOVE_WORLDONLY;
193         if(f & 0x40)
194                 self.move_flags |= FL_ONGROUND;
195         else
196                 self.move_flags &~= FL_ONGROUND;
197
198         if(!self.move_time)
199         {
200                 // for some unknown reason, we don't need to care for
201                 // sv_gameplayfix_delayprojectiles here.
202                 self.move_time = time;
203                 self.spawntime = time;
204         }
205         else
206                 self.move_time = max(self.move_time, time);
207
208         if(!(self.count & 0x80))
209                 InterpolateOrigin_Undo();
210
211         if(f & 1)
212         {
213                 self.origin_x = ReadCoord();
214                 self.origin_y = ReadCoord();
215                 self.origin_z = ReadCoord();
216                 if(self.count & 0x80)
217                 {
218                         self.velocity_x = ReadCoord();
219                         self.velocity_y = ReadCoord();
220                         self.velocity_z = ReadCoord();
221                         if(f & 0x10)
222                                 self.gravity = ReadCoord();
223                         else
224                                 self.gravity = 0; // none
225                         self.move_origin = self.origin;
226                         self.move_velocity = self.velocity;
227                 }
228
229                 if(time == self.spawntime || (self.count & 0x80) || (f & 0x08))
230                 {
231                         self.trail_oldorigin = self.origin;
232                         if(!(self.count & 0x80))
233                                 InterpolateOrigin_Reset();
234                 }
235
236                 if(f & 0x20)
237                 {
238                         self.fade_time = time + ReadByte() * ticrate;
239                         self.fade_rate = 1 / (ReadByte() * ticrate);
240                 }
241                 else
242                 {
243                         self.fade_time = 0;
244                         self.fade_rate = 0;
245                 }
246         }
247
248         if(f & 2)
249         {
250                 self.cnt = ReadByte();
251
252                 self.silent = (self.cnt & 0x80);
253                 self.cnt = (self.cnt & 0x7F);
254
255                 self.scale = 1;
256                 self.traileffect = 0;
257                 switch(self.cnt)
258                 {
259                         case PROJECTILE_ELECTRO: setmodel(self, "models/ebomb.mdl");self.traileffect = particleeffectnum("TR_NEXUIZPLASMA"); break;
260                         case PROJECTILE_ROCKET: setmodel(self, "models/rocket.md3");self.traileffect = particleeffectnum("TR_ROCKET"); self.scale = 2; break;
261                         case PROJECTILE_BULLET: setmodel(self, "models/tracer.mdl");self.traileffect = particleeffectnum("tr_bullet"); break;
262                         case PROJECTILE_BULLET_GLOWING: setmodel(self, "models/tracer.mdl");self.traileffect = particleeffectnum("tr_bullet"); break;
263                         case PROJECTILE_BULLET_GLOWING_TRACER: setmodel(self, "models/tracer.mdl");self.traileffect = particleeffectnum("tr_rifle"); break;
264                         case PROJECTILE_CRYLINK: setmodel(self, "models/plasmatrail.mdl");self.traileffect = particleeffectnum("TR_CRYLINKPLASMA"); break;
265                         case PROJECTILE_CRYLINK_BOUNCING: setmodel(self, "models/plasmatrail.mdl");self.traileffect = particleeffectnum("TR_CRYLINKPLASMA"); break;
266                         case PROJECTILE_ELECTRO_BEAM: setmodel(self, "models/elaser.mdl");self.traileffect = particleeffectnum("TR_NEXUIZPLASMA"); break;
267                         case PROJECTILE_GRENADE: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_GRENADE"); break;
268                         case PROJECTILE_GRENADE_BOUNCING: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_GRENADE"); break;
269                         case PROJECTILE_LASER: setmodel(self, "models/laser.mdl");self.traileffect = particleeffectnum(""); break;
270                         case PROJECTILE_HLAC: setmodel(self, "models/hlac_bullet.md3");self.traileffect = particleeffectnum(""); break;
271                         case PROJECTILE_PORTO_RED: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_WIZSPIKE"); self.scale = 4; break;
272                         case PROJECTILE_PORTO_BLUE: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_WIZSPIKE"); self.scale = 4; break;
273                         case PROJECTILE_HOOKBOMB: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_KNIGHTSPIKE"); break;
274                         case PROJECTILE_HAGAR: setmodel(self, "models/hagarmissile.mdl");self.traileffect = particleeffectnum("TR_GRENADE"); self.scale = 0.4; break;
275                         case PROJECTILE_HAGAR_BOUNCING: setmodel(self, "models/hagarmissile.mdl");self.traileffect = particleeffectnum("TR_GRENADE"); self.scale = 0.4; break;
276                         case PROJECTILE_FIREBALL: self.model = ""; self.modelindex = 0; self.traileffect = particleeffectnum("fireball"); break; // particle effect is good enough
277                         case PROJECTILE_FIREMINE: self.model = ""; self.modelindex = 0; self.traileffect = particleeffectnum("firemine"); break; // particle effect is good enough
278                         case PROJECTILE_TAG: setmodel(self, "models/laser.mdl"); self.traileffect = particleeffectnum("TR_ROCKET"); break;
279                         case PROJECTILE_FLAC: setmodel(self, "models/hagarmissile.mdl"); self.scale = 0.4; self.traileffect = particleeffectnum("TR_ROCKET"); break;
280                         case PROJECTILE_SEEKER: setmodel(self, "models/tagrocket.md3"); self.scale = 2; self.traileffect = particleeffectnum("TR_ROCKET"); break;
281                         default:
282                                 error("Received invalid CSQC projectile, can't work with this!");
283                                 break;
284                 }
285
286                 self.mins = '0 0 0';
287                 self.maxs = '0 0 0';
288                 self.colormod = '0 0 0';
289                 self.move_touch = SUB_Stop;
290                 self.move_movetype = MOVETYPE_TOSS;
291                 self.alphamod = 1;
292
293                 switch(self.cnt)
294                 {
295                         case PROJECTILE_ELECTRO:
296                                 // only new engines support sound moving with object
297                                 loopsound(self, CHAN_PROJECTILE, "weapons/electro_fly.wav", VOL_BASE, ATTN_NORM);
298                                 self.mins = '0 0 -3';
299                                 self.maxs = '0 0 -3';
300                                 self.move_movetype = MOVETYPE_BOUNCE;
301                                 self.move_touch = SUB_Null;
302                                 break;
303                         case PROJECTILE_ROCKET:
304                                 loopsound(self, CHAN_PROJECTILE, "weapons/rocket_fly.wav", VOL_BASE, ATTN_NORM);
305                                 self.mins = '-3 -3 -3';
306                                 self.maxs = '3 3 3';
307                                 break;
308                         case PROJECTILE_GRENADE:
309                                 self.mins = '0 0 -3';
310                                 self.maxs = '0 0 -3';
311                                 break;
312                         case PROJECTILE_GRENADE_BOUNCING:
313                                 self.mins = '0 0 -3';
314                                 self.maxs = '0 0 -3';
315                                 self.move_movetype = MOVETYPE_BOUNCE;
316                                 self.move_touch = SUB_Null;
317                                 self.move_bounce_factor = g_balance_grenadelauncher_secondary_bouncefactor;
318                                 self.move_bounce_stopspeed = g_balance_grenadelauncher_secondary_bouncestop;
319                                 break;
320                         case PROJECTILE_PORTO_RED:
321                                 self.colormod = '2 1 1';
322                                 self.alphamod = 0.5;
323                                 self.move_movetype = MOVETYPE_BOUNCE;
324                                 self.move_touch = SUB_Null;
325                                 break;
326                         case PROJECTILE_PORTO_BLUE:
327                                 self.colormod = '1 1 2';
328                                 self.alphamod = 0.5;
329                                 self.move_movetype = MOVETYPE_BOUNCE;
330                                 self.move_touch = SUB_Null;
331                                 break;
332                         case PROJECTILE_HAGAR_BOUNCING:
333                                 self.move_movetype = MOVETYPE_BOUNCE;
334                                 self.move_touch = SUB_Null;
335                                 break;
336                         case PROJECTILE_CRYLINK_BOUNCING:
337                                 self.move_movetype = MOVETYPE_BOUNCE;
338                                 self.move_touch = SUB_Null;
339                                 break;
340                         case PROJECTILE_FIREBALL:
341                                 loopsound(self, CHAN_PROJECTILE, "weapons/fireball_fly2.wav", VOL_BASE, ATTN_NORM);
342                                 self.mins = '-16 -16 -16';
343                                 self.maxs = '16 16 16';
344                                 break;
345                         case PROJECTILE_FIREMINE:
346                                 loopsound(self, CHAN_PROJECTILE, "weapons/fireball_fly.wav", VOL_BASE, ATTN_NORM);
347                                 self.move_movetype = MOVETYPE_BOUNCE;
348                                 self.move_touch = SUB_Null;
349                                 self.mins = '-4 -4 -4';
350                                 self.maxs = '4 4 4';
351                                 break;
352                         case PROJECTILE_TAG:
353                                 loopsound(self, CHAN_PROJECTILE, "weapons/tag_rocket_fly.wav", VOL_BASE, ATTN_NORM);
354                                 self.mins = '-2 -2 -2';
355                                 self.maxs = '2 2 2';
356                                 break;
357                         case PROJECTILE_FLAC:
358                                 self.mins = '-2 -2 -2';
359                                 self.maxs = '2 2 2';
360                                 break;
361                         case PROJECTILE_SEEKER:
362                                 self.mins = '-4 -4 -4';
363                                 self.maxs = '4 4 4';
364                                 break;
365                         default:
366                                 break;
367                 }
368         }
369
370         if(self.gravity)
371         {
372                 if(self.move_movetype == MOVETYPE_FLY)
373                         self.move_movetype = MOVETYPE_TOSS;
374                 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
375                         self.move_movetype = MOVETYPE_BOUNCE;
376         }
377         else
378         {
379                 if(self.move_movetype == MOVETYPE_TOSS)
380                         self.move_movetype = MOVETYPE_FLY;
381                 if(self.move_movetype == MOVETYPE_BOUNCE)
382                         self.move_movetype = MOVETYPE_BOUNCEMISSILE;
383         }
384
385         if(!(self.count & 0x80))
386                 InterpolateOrigin_Note();
387         
388         self.draw = Projectile_Draw;
389         self.entremove = Ent_RemoveProjectile;
390 }
391
392 void Projectile_Precache()
393 {
394         precache_model("models/ebomb.mdl");
395         precache_model("models/elaser.mdl");
396         precache_model("models/grenademodel.md3");
397         precache_model("models/hagarmissile.mdl");
398         precache_model("models/hlac_bullet.md3");
399         precache_model("models/laser.mdl");
400         precache_model("models/plasmatrail.mdl");
401         precache_model("models/rocket.md3");
402         precache_model("models/tagrocket.md3");
403         precache_model("models/tracer.mdl");
404         precache_sound("weapons/electro_fly.wav");
405         precache_sound("weapons/rocket_fly.wav");
406         precache_sound("weapons/fireball_fly.wav");
407         precache_sound("weapons/fireball_fly2.wav");
408         precache_sound("weapons/tag_rocket_fly.wav");
409 }