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