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