Nades: rename registry globals
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / weapons / projectile.qc
1 #include "projectile.qh"
2
3 #include "../autocvars.qh"
4 #include "../defs.qh"
5 #include "../main.qh"
6
7 #include "../../common/constants.qh"
8 #include "../../common/nades/all.qh"
9 #include "../../common/movetypes/movetypes.qh"
10 #include "../../common/util.qh"
11
12 #include "../../csqcmodellib/interpolate.qh"
13
14 #include "../../warpzonelib/anglestransform.qh"
15
16 .float alpha;
17 .float scale;
18 .vector colormod;
19
20 void SUB_Stop()
21 {SELFPARAM();
22         self.move_velocity = self.move_avelocity = '0 0 0';
23         self.move_movetype = MOVETYPE_NONE;
24 }
25
26 void Projectile_ResetTrail(vector to)
27 {SELFPARAM();
28         self.trail_oldorigin = to;
29         self.trail_oldtime = time;
30 }
31
32 void Projectile_DrawTrail(vector to)
33 {SELFPARAM();
34         vector from;
35         float t0;
36
37         from = self.trail_oldorigin;
38         t0 = self.trail_oldtime;
39         self.trail_oldorigin = to;
40         self.trail_oldtime = time;
41
42         // force the effect even for stationary firemine
43         if(self.cnt == PROJECTILE_FIREMINE)
44                 if(from == to)
45                         from.z += 1;
46
47         if (self.traileffect)
48         {
49                 particles_alphamin = particles_alphamax = particles_fade = sqrt(self.alpha);
50                 boxparticles(particleeffectnum(effects_ent[self.traileffect]), self, from, to, self.velocity, self.velocity, 1, PARTICLES_USEALPHA | PARTICLES_USEFADE | PARTICLES_DRAWASTRAIL);
51         }
52 }
53
54 void Projectile_Draw()
55 {SELFPARAM();
56         vector rot;
57         vector trailorigin;
58         int f;
59         bool drawn;
60         float t;
61         float a;
62
63         f = self.move_flags;
64
65         if(self.count & 0x80)
66         {
67                 //self.move_flags &= ~FL_ONGROUND;
68                 if(self.move_movetype == MOVETYPE_NONE || self.move_movetype == MOVETYPE_FLY)
69                         Movetype_Physics_NoMatchServer();
70                         // the trivial movetypes do not have to match the
71                         // server's ticrate as they are ticrate independent
72                         // NOTE: this assumption is only true if MOVETYPE_FLY
73                         // projectiles detonate on impact. If they continue
74                         // moving, we might still be ticrate dependent.
75                 else
76                         Movetype_Physics_MatchServer(autocvar_cl_projectiles_sloppy);
77                 if(!(self.move_flags & FL_ONGROUND))
78                         if(self.velocity != '0 0 0')
79                                 self.move_angles = self.angles = vectoangles(self.velocity);
80         }
81         else
82         {
83                 InterpolateOrigin_Do();
84         }
85
86         if(self.count & 0x80)
87         {
88                 drawn = (time >= self.spawntime - 0.02);
89                 t = max(time, self.spawntime);
90         }
91         else
92         {
93                 drawn = (self.iflags & IFLAG_VALID);
94                 t = time;
95         }
96
97         if(!(f & FL_ONGROUND))
98         {
99                 rot = '0 0 0';
100                 switch(self.cnt)
101                 {
102                         /*
103                         case PROJECTILE_GRENADE:
104                                 rot = '-2000 0 0'; // forward
105                                 break;
106                         */
107                         case PROJECTILE_GRENADE_BOUNCING:
108                                 rot = '0 -1000 0'; // sideways
109                                 break;
110                         case PROJECTILE_HOOKBOMB:
111                                 rot = '1000 0 0'; // forward
112                                 break;
113                         default:
114                                 break;
115                 }
116
117                 if(Nade_FromProjectile(self.cnt) != NADE_TYPE_Null)
118                         rot = self.avelocity;
119
120                 self.angles = AnglesTransform_ToAngles(AnglesTransform_Multiply(AnglesTransform_FromAngles(self.angles), rot * (t - self.spawntime)));
121         }
122
123         vector ang;
124         ang = self.angles;
125         ang.x = -ang.x;
126         makevectors(ang);
127
128         a = 1 - (time - self.fade_time) * self.fade_rate;
129         self.alpha = bound(0, self.alphamod * a, 1);
130         if(self.alpha <= 0)
131                 drawn = 0;
132         self.renderflags = 0;
133
134         trailorigin = self.origin;
135         switch(self.cnt)
136         {
137                 case PROJECTILE_GRENADE:
138                 case PROJECTILE_GRENADE_BOUNCING:
139                         trailorigin += v_right * 1 + v_forward * -10;
140                         break;
141                 default:
142                         break;
143         }
144
145         if(Nade_FromProjectile(self.cnt) != NADE_TYPE_Null)
146                 trailorigin += v_up * 4;
147
148         if(drawn)
149                 Projectile_DrawTrail(trailorigin);
150         else
151                 Projectile_ResetTrail(trailorigin);
152
153         self.drawmask = 0;
154
155         if(!drawn)
156                 return;
157
158         switch(self.cnt)
159         {
160                 // Possibly add dlights here.
161                 default:
162                         break;
163         }
164
165         self.drawmask = MASK_NORMAL;
166 }
167
168 void loopsound(entity e, int ch, string samp, float vol, float attn)
169 {SELFPARAM();
170         if(self.silent)
171                 return;
172
173         _sound(e, ch, samp, vol, attn);
174         e.snd_looping = ch;
175 }
176
177 void Ent_RemoveProjectile()
178 {SELFPARAM();
179         if(self.count & 0x80)
180         {
181                 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.05, MOVE_NORMAL, self);
182                 Projectile_DrawTrail(trace_endpos);
183         }
184 }
185
186 void Ent_Projectile()
187 {SELFPARAM();
188         int f;
189
190         // projectile properties:
191         //   kind (interpolated, or clientside)
192         //
193         //   modelindex
194         //   origin
195         //   scale
196         //   if clientside:
197         //     velocity
198         //     gravity
199         //   soundindex (hardcoded list)
200         //   effects
201         //
202         // projectiles don't send angles, because they always follow the velocity
203
204         f = ReadByte();
205         self.count = (f & 0x80);
206         self.iflags = (self.iflags & IFLAG_INTERNALMASK) | IFLAG_AUTOANGLES | IFLAG_ANGLES | IFLAG_ORIGIN;
207         self.solid = SOLID_TRIGGER;
208         //self.effects = EF_NOMODELFLAGS;
209
210         // this should make collisions with bmodels more exact, but it leads to
211         // projectiles no longer being able to lie on a bmodel
212         self.move_nomonsters = MOVE_WORLDONLY;
213         if(f & 0x40)
214                 self.move_flags |= FL_ONGROUND;
215         else
216                 self.move_flags &= ~FL_ONGROUND;
217
218         if(!self.move_time)
219         {
220                 // for some unknown reason, we don't need to care for
221                 // sv_gameplayfix_delayprojectiles here.
222                 self.move_time = time;
223                 self.spawntime = time;
224         }
225         else
226                 self.move_time = max(self.move_time, time);
227
228         if(!(self.count & 0x80))
229                 InterpolateOrigin_Undo();
230
231         if(f & 1)
232         {
233                 self.origin_x = ReadCoord();
234                 self.origin_y = ReadCoord();
235                 self.origin_z = ReadCoord();
236                 setorigin(self, self.origin);
237                 if(self.count & 0x80)
238                 {
239                         self.velocity_x = ReadCoord();
240                         self.velocity_y = ReadCoord();
241                         self.velocity_z = ReadCoord();
242                         if(f & 0x10)
243                                 self.gravity = ReadCoord();
244                         else
245                                 self.gravity = 0; // none
246                         self.move_origin = self.origin;
247                         self.move_velocity = self.velocity;
248                 }
249
250                 if(time == self.spawntime || (self.count & 0x80) || (f & 0x08))
251                 {
252                         self.trail_oldorigin = self.origin;
253                         if(!(self.count & 0x80))
254                                 InterpolateOrigin_Reset();
255                 }
256
257                 if(f & 0x20)
258                 {
259                         self.fade_time = time + ReadByte() * ticrate;
260                         self.fade_rate = 1 / (ReadByte() * ticrate);
261                 }
262                 else
263                 {
264                         self.fade_time = 0;
265                         self.fade_rate = 0;
266                 }
267
268                 self.team = ReadByte() - 1;
269         }
270
271         if(f & 2)
272         {
273                 self.cnt = ReadByte();
274
275                 self.silent = (self.cnt & 0x80);
276                 self.cnt = (self.cnt & 0x7F);
277
278                 self.scale = 1;
279                 self.traileffect = 0;
280                 switch (self.cnt) {
281 #define CASE(id) case PROJECTILE_##id: setmodel(self, MDL_PROJECTILE_##id);
282                         CASE(ELECTRO)            self.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
283                         CASE(ROCKET)             self.traileffect = EFFECT_TR_ROCKET.m_id; self.scale = 2; break;
284                         CASE(CRYLINK)            self.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
285                         CASE(CRYLINK_BOUNCING)   self.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
286                         CASE(ELECTRO_BEAM)       self.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
287                         CASE(GRENADE)            self.traileffect = EFFECT_TR_GRENADE.m_id; break;
288                         CASE(GRENADE_BOUNCING)   self.traileffect = EFFECT_TR_GRENADE.m_id; break;
289                         CASE(MINE)               self.traileffect = EFFECT_TR_GRENADE.m_id; break;
290                         CASE(BLASTER)            self.traileffect = EFFECT_Null.m_id; break;
291                         CASE(HLAC)               self.traileffect = EFFECT_Null.m_id; break;
292                         CASE(PORTO_RED)          self.traileffect = EFFECT_TR_WIZSPIKE.m_id; self.scale = 4; break;
293                         CASE(PORTO_BLUE)         self.traileffect = EFFECT_TR_WIZSPIKE.m_id; self.scale = 4; break;
294                         CASE(HOOKBOMB)           self.traileffect = EFFECT_TR_KNIGHTSPIKE.m_id; break;
295                         CASE(HAGAR)              self.traileffect = EFFECT_HAGAR_ROCKET.m_id; self.scale = 0.75; break;
296                         CASE(HAGAR_BOUNCING)     self.traileffect = EFFECT_HAGAR_ROCKET.m_id; self.scale = 0.75; break;
297                         CASE(NAPALM_FOUNTAIN)    // fallthrough // sself.modelindex = 0; self.traileffect = _particleeffectnum("torch_small"); break;
298                         CASE(FIREBALL)           self.modelindex = 0; self.traileffect = EFFECT_FIREBALL.m_id; break; // particle effect is good enough
299                         CASE(FIREMINE)           self.modelindex = 0; self.traileffect = EFFECT_FIREMINE.m_id; break; // particle effect is good enough
300                         CASE(TAG)                self.traileffect = EFFECT_TR_ROCKET.m_id; break;
301                         CASE(FLAC)               self.scale = 0.4; self.traileffect = EFFECT_FLAC_TRAIL.m_id; break;
302                         CASE(SEEKER)             self.traileffect = EFFECT_SEEKER_TRAIL.m_id; break;
303
304                         CASE(MAGE_SPIKE)         self.traileffect = EFFECT_TR_VORESPIKE.m_id; break;
305                         CASE(SHAMBLER_LIGHTNING) self.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
306
307                         CASE(RAPTORBOMB)         self.gravity = 1; self.avelocity = '0 0 180'; self.traileffect = EFFECT_Null.m_id; break;
308                         CASE(RAPTORBOMBLET)      self.gravity = 1; self.avelocity = '0 0 180'; self.traileffect = EFFECT_Null.m_id; break;
309                         CASE(RAPTORCANNON)       self.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
310
311                         CASE(SPIDERROCKET)       self.traileffect = EFFECT_SPIDERBOT_ROCKET_TRAIL.m_id; break;
312                         CASE(WAKIROCKET)         self.traileffect = EFFECT_RACER_ROCKET_TRAIL.m_id; break;
313                         CASE(WAKICANNON)         self.traileffect = EFFECT_Null.m_id; break;
314
315                         CASE(BUMBLE_GUN)         self.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
316                         CASE(BUMBLE_BEAM)        self.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
317
318                         CASE(RPC)                self.traileffect = EFFECT_TR_ROCKET.m_id; break;
319
320                         CASE(ROCKETMINSTA_LASER) self.traileffect = EFFECT_ROCKETMINSTA_LASER(self.team).m_id; break;
321 #undef CASE
322                         default:
323                                 if(MUTATOR_CALLHOOK(Ent_Projectile, self))
324                                         break;
325
326                                 if (Nade_FromProjectile(self.cnt) != NADE_TYPE_Null)
327                                 {
328                                         setmodel(self, MDL_PROJECTILE_NADE);
329                                         entity trail = Nade_TrailEffect(self.cnt, self.team);
330                                         if (trail.eent_eff_name) self.traileffect = trail.m_id;
331                                         break;
332                                 }
333                                 error("Received invalid CSQC projectile, can't work with this!");
334                                 break;
335                 }
336
337                 self.mins = '0 0 0';
338                 self.maxs = '0 0 0';
339                 self.colormod = '0 0 0';
340                 self.move_touch = SUB_Stop;
341                 self.move_movetype = MOVETYPE_TOSS;
342                 self.alphamod = 1;
343
344                 switch(self.cnt)
345                 {
346                         case PROJECTILE_ELECTRO:
347                                 // only new engines support sound moving with object
348                                 loopsound(self, CH_SHOTS_SINGLE, SND(ELECTRO_FLY), VOL_BASE, ATTEN_NORM);
349                                 self.mins = '0 0 -4';
350                                 self.maxs = '0 0 -4';
351                                 self.move_movetype = MOVETYPE_BOUNCE;
352                                 self.move_touch = func_null;
353                                 self.move_bounce_factor = g_balance_electro_secondary_bouncefactor;
354                                 self.move_bounce_stopspeed = g_balance_electro_secondary_bouncestop;
355                                 break;
356                         case PROJECTILE_RPC:
357                         case PROJECTILE_ROCKET:
358                                 loopsound(self, CH_SHOTS_SINGLE, SND(ROCKET_FLY), VOL_BASE, ATTEN_NORM);
359                                 self.mins = '-3 -3 -3';
360                                 self.maxs = '3 3 3';
361                                 break;
362                         case PROJECTILE_GRENADE:
363                                 self.mins = '-3 -3 -3';
364                                 self.maxs = '3 3 3';
365                                 break;
366                         case PROJECTILE_GRENADE_BOUNCING:
367                                 self.mins = '-3 -3 -3';
368                                 self.maxs = '3 3 3';
369                                 self.move_movetype = MOVETYPE_BOUNCE;
370                                 self.move_touch = func_null;
371                                 self.move_bounce_factor = g_balance_mortar_bouncefactor;
372                                 self.move_bounce_stopspeed = g_balance_mortar_bouncestop;
373                                 break;
374                         case PROJECTILE_SHAMBLER_LIGHTNING:
375                                 self.mins = '-8 -8 -8';
376                                 self.maxs = '8 8 8';
377                                 self.scale = 2.5;
378                                 self.avelocity = randomvec() * 720;
379                                 break;
380                         case PROJECTILE_MINE:
381                                 self.mins = '-4 -4 -4';
382                                 self.maxs = '4 4 4';
383                                 break;
384                         case PROJECTILE_PORTO_RED:
385                                 self.colormod = '2 1 1';
386                                 self.alphamod = 0.5;
387                                 self.move_movetype = MOVETYPE_BOUNCE;
388                                 self.move_touch = func_null;
389                                 break;
390                         case PROJECTILE_PORTO_BLUE:
391                                 self.colormod = '1 1 2';
392                                 self.alphamod = 0.5;
393                                 self.move_movetype = MOVETYPE_BOUNCE;
394                                 self.move_touch = func_null;
395                                 break;
396                         case PROJECTILE_HAGAR_BOUNCING:
397                                 self.move_movetype = MOVETYPE_BOUNCE;
398                                 self.move_touch = func_null;
399                                 break;
400                         case PROJECTILE_CRYLINK_BOUNCING:
401                                 self.move_movetype = MOVETYPE_BOUNCE;
402                                 self.move_touch = func_null;
403                                 break;
404                         case PROJECTILE_NAPALM_FOUNTAIN:
405                         case PROJECTILE_FIREBALL:
406                                 loopsound(self, CH_SHOTS_SINGLE, SND(FIREBALL_FLY2), VOL_BASE, ATTEN_NORM);
407                                 self.mins = '-16 -16 -16';
408                                 self.maxs = '16 16 16';
409                                 break;
410                         case PROJECTILE_FIREMINE:
411                                 loopsound(self, CH_SHOTS_SINGLE, SND(FIREBALL_FLY), VOL_BASE, ATTEN_NORM);
412                                 self.move_movetype = MOVETYPE_BOUNCE;
413                                 self.move_touch = func_null;
414                                 self.mins = '-4 -4 -4';
415                                 self.maxs = '4 4 4';
416                                 break;
417                         case PROJECTILE_TAG:
418                                 self.mins = '-2 -2 -2';
419                                 self.maxs = '2 2 2';
420                                 break;
421                         case PROJECTILE_FLAC:
422                                 self.mins = '-2 -2 -2';
423                                 self.maxs = '2 2 2';
424                                 break;
425                         case PROJECTILE_SEEKER:
426                                 loopsound(self, CH_SHOTS_SINGLE, SND(TAG_ROCKET_FLY), VOL_BASE, ATTEN_NORM);
427                                 self.mins = '-4 -4 -4';
428                                 self.maxs = '4 4 4';
429                                 break;
430             case PROJECTILE_RAPTORBOMB:
431                                 self.mins = '-3 -3 -3';
432                                 self.maxs = '3 3 3';
433                                 break;
434             case PROJECTILE_RAPTORBOMBLET:
435                                 break;
436             case PROJECTILE_RAPTORCANNON:
437                                 break;
438             case PROJECTILE_SPIDERROCKET:
439                 loopsound(self, CH_SHOTS_SINGLE, SND(TAG_ROCKET_FLY), VOL_BASE, ATTEN_NORM);
440                                 break;
441             case PROJECTILE_WAKIROCKET:
442                 loopsound(self, CH_SHOTS_SINGLE, SND(TAG_ROCKET_FLY), VOL_BASE, ATTEN_NORM);
443                                 break;
444             /*
445             case PROJECTILE_WAKICANNON:
446                                 break;
447                         case PROJECTILE_BUMBLE_GUN:
448                                 // only new engines support sound moving with object
449                                 loopsound(self, CH_SHOTS_SINGLE, SND(ELECTRO_FLY), VOL_BASE, ATTEN_NORM);
450                                 self.mins = '0 0 -4';
451                                 self.maxs = '0 0 -4';
452                                 self.move_movetype = MOVETYPE_BOUNCE;
453                                 self.move_touch = func_null;
454                                 self.move_bounce_factor = g_balance_electro_secondary_bouncefactor;
455                                 self.move_bounce_stopspeed = g_balance_electro_secondary_bouncestop;
456                                 break;
457                         */
458                         default:
459                                 break;
460                 }
461
462                 if(Nade_FromProjectile(self.cnt) != NADE_TYPE_Null)
463                 {
464                         entity nade_type = Nade_FromProjectile(self.cnt);
465                         self.mins = '-16 -16 -16';
466                         self.maxs = '16 16 16';
467                         self.colormod = nade_type.m_color;
468                         self.move_movetype = MOVETYPE_BOUNCE;
469                         self.move_touch = func_null;
470                         self.scale = 1.5;
471                         self.avelocity = randomvec() * 720;
472
473                         if(nade_type == NADE_TYPE_TRANSLOCATE || nade_type == NADE_TYPE_SPAWN)
474                                 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
475                         else
476                                 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
477                 }
478
479                 MUTATOR_CALLHOOK(EditProjectile, self);
480
481                 setsize(self, self.mins, self.maxs);
482         }
483
484         if(self.gravity)
485         {
486                 if(self.move_movetype == MOVETYPE_FLY)
487                         self.move_movetype = MOVETYPE_TOSS;
488                 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
489                         self.move_movetype = MOVETYPE_BOUNCE;
490         }
491         else
492         {
493                 if(self.move_movetype == MOVETYPE_TOSS)
494                         self.move_movetype = MOVETYPE_FLY;
495                 if(self.move_movetype == MOVETYPE_BOUNCE)
496                         self.move_movetype = MOVETYPE_BOUNCEMISSILE;
497         }
498
499         if(!(self.count & 0x80))
500                 InterpolateOrigin_Note();
501
502         self.classname = "csqcprojectile";
503         self.draw = Projectile_Draw;
504         self.entremove = Ent_RemoveProjectile;
505 }
506
507 void Projectile_Precache()
508 {
509         MUTATOR_CALLHOOK(PrecacheProjectiles);
510 }