Viewmodels: cl_bobmodel: check onground for other players too
[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 NET_HANDLE(ENT_CLIENT_PROJECTILE, bool isnew)
187 {
188         // projectile properties:
189         //   kind (interpolated, or clientside)
190         //
191         //   modelindex
192         //   origin
193         //   scale
194         //   if clientside:
195         //     velocity
196         //     gravity
197         //   soundindex (hardcoded list)
198         //   effects
199         //
200         // projectiles don't send angles, because they always follow the velocity
201
202         int f = ReadByte();
203         self.count = (f & 0x80);
204         self.iflags = (self.iflags & IFLAG_INTERNALMASK) | IFLAG_AUTOANGLES | IFLAG_ANGLES | IFLAG_ORIGIN;
205         self.solid = SOLID_TRIGGER;
206         // self.effects = EF_NOMODELFLAGS;
207
208         // this should make collisions with bmodels more exact, but it leads to
209         // projectiles no longer being able to lie on a bmodel
210         self.move_nomonsters = MOVE_WORLDONLY;
211         if (f & 0x40)
212                 self.move_flags |= FL_ONGROUND;
213         else
214                 self.move_flags &= ~FL_ONGROUND;
215
216         if (!self.move_time)
217         {
218                 // for some unknown reason, we don't need to care for
219                 // sv_gameplayfix_delayprojectiles here.
220                 self.move_time = time;
221                 self.spawntime = time;
222         }
223         else
224         {
225                 self.move_time = max(self.move_time, time);
226         }
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                 {
282                         #define HANDLE(id) case PROJECTILE_##id: setmodel(self, MDL_PROJECTILE_##id);
283                         HANDLE(ELECTRO)            self.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
284                         HANDLE(ROCKET)             self.traileffect = EFFECT_TR_ROCKET.m_id; self.scale = 2; break;
285                         HANDLE(CRYLINK)            self.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
286                         HANDLE(CRYLINK_BOUNCING)   self.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
287                         HANDLE(ELECTRO_BEAM)       self.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
288                         HANDLE(GRENADE)            self.traileffect = EFFECT_TR_GRENADE.m_id; break;
289                         HANDLE(GRENADE_BOUNCING)   self.traileffect = EFFECT_TR_GRENADE.m_id; break;
290                         HANDLE(MINE)               self.traileffect = EFFECT_TR_GRENADE.m_id; break;
291                         HANDLE(BLASTER)            self.traileffect = EFFECT_Null.m_id; break;
292                         HANDLE(HLAC)               self.traileffect = EFFECT_Null.m_id; break;
293                         HANDLE(PORTO_RED)          self.traileffect = EFFECT_TR_WIZSPIKE.m_id; self.scale = 4; break;
294                         HANDLE(PORTO_BLUE)         self.traileffect = EFFECT_TR_WIZSPIKE.m_id; self.scale = 4; break;
295                         HANDLE(HOOKBOMB)           self.traileffect = EFFECT_TR_KNIGHTSPIKE.m_id; break;
296                         HANDLE(HAGAR)              self.traileffect = EFFECT_HAGAR_ROCKET.m_id; self.scale = 0.75; break;
297                         HANDLE(HAGAR_BOUNCING)     self.traileffect = EFFECT_HAGAR_ROCKET.m_id; self.scale = 0.75; break;
298                         HANDLE(NAPALM_FOUNTAIN)                                                                         // fallthrough // sself.modelindex = 0; self.traileffect = _particleeffectnum("torch_small"); break;
299                         HANDLE(FIREBALL)           self.modelindex = 0; self.traileffect = EFFECT_FIREBALL.m_id; break; // particle effect is good enough
300                         HANDLE(FIREMINE)           self.modelindex = 0; self.traileffect = EFFECT_FIREMINE.m_id; break; // particle effect is good enough
301                         HANDLE(TAG)                self.traileffect = EFFECT_TR_ROCKET.m_id; break;
302                         HANDLE(FLAC)               self.scale = 0.4; self.traileffect = EFFECT_FLAC_TRAIL.m_id; break;
303                         HANDLE(SEEKER)             self.traileffect = EFFECT_SEEKER_TRAIL.m_id; break;
304
305                         HANDLE(MAGE_SPIKE)         self.traileffect = EFFECT_TR_VORESPIKE.m_id; break;
306                         HANDLE(SHAMBLER_LIGHTNING) self.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
307
308                         HANDLE(RAPTORBOMB)         self.gravity = 1; self.avelocity = '0 0 180'; self.traileffect = EFFECT_Null.m_id; break;
309                         HANDLE(RAPTORBOMBLET)      self.gravity = 1; self.avelocity = '0 0 180'; self.traileffect = EFFECT_Null.m_id; break;
310                         HANDLE(RAPTORCANNON)       self.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
311
312                         HANDLE(SPIDERROCKET)       self.traileffect = EFFECT_SPIDERBOT_ROCKET_TRAIL.m_id; break;
313                         HANDLE(WAKIROCKET)         self.traileffect = EFFECT_RACER_ROCKET_TRAIL.m_id; break;
314                         HANDLE(WAKICANNON)         self.traileffect = EFFECT_Null.m_id; break;
315
316                         HANDLE(BUMBLE_GUN)         self.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
317                         HANDLE(BUMBLE_BEAM)        self.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
318
319                         HANDLE(RPC)                self.traileffect = EFFECT_TR_ROCKET.m_id; break;
320
321                         HANDLE(ROCKETMINSTA_LASER) self.traileffect = EFFECT_ROCKETMINSTA_LASER(self.team).m_id; break;
322 #undef HANDLE
323                         default:
324                                 if (MUTATOR_CALLHOOK(Ent_Projectile, self))
325                                         break;
326
327                                 if (Nade_FromProjectile(self.cnt) != NADE_TYPE_Null)
328                                 {
329                                         setmodel(self, MDL_PROJECTILE_NADE);
330                                         entity trail = Nade_TrailEffect(self.cnt, self.team);
331                                         if (trail.eent_eff_name) self.traileffect = trail.m_id;
332                                         break;
333                                 }
334                                 error("Received invalid CSQC projectile, can't work with this!");
335                                 break;
336                 }
337
338                 self.mins = '0 0 0';
339                 self.maxs = '0 0 0';
340                 self.colormod = '0 0 0';
341                 self.move_touch = SUB_Stop;
342                 self.move_movetype = MOVETYPE_TOSS;
343                 self.alphamod = 1;
344
345                 switch (self.cnt)
346                 {
347                         case PROJECTILE_ELECTRO:
348                                 // only new engines support sound moving with object
349                                 loopsound(self, CH_SHOTS_SINGLE, SND(ELECTRO_FLY), VOL_BASE, ATTEN_NORM);
350                                 self.mins = '0 0 -4';
351                                 self.maxs = '0 0 -4';
352                                 self.move_movetype = MOVETYPE_BOUNCE;
353                                 self.move_touch = func_null;
354                                 self.move_bounce_factor = g_balance_electro_secondary_bouncefactor;
355                                 self.move_bounce_stopspeed = g_balance_electro_secondary_bouncestop;
356                                 break;
357                         case PROJECTILE_RPC:
358                         case PROJECTILE_ROCKET:
359                                 loopsound(self, CH_SHOTS_SINGLE, SND(ROCKET_FLY), VOL_BASE, ATTEN_NORM);
360                                 self.mins = '-3 -3 -3';
361                                 self.maxs = '3 3 3';
362                                 break;
363                         case PROJECTILE_GRENADE:
364                                 self.mins = '-3 -3 -3';
365                                 self.maxs = '3 3 3';
366                                 break;
367                         case PROJECTILE_GRENADE_BOUNCING:
368                                 self.mins = '-3 -3 -3';
369                                 self.maxs = '3 3 3';
370                                 self.move_movetype = MOVETYPE_BOUNCE;
371                                 self.move_touch = func_null;
372                                 self.move_bounce_factor = g_balance_mortar_bouncefactor;
373                                 self.move_bounce_stopspeed = g_balance_mortar_bouncestop;
374                                 break;
375                         case PROJECTILE_SHAMBLER_LIGHTNING:
376                                 self.mins = '-8 -8 -8';
377                                 self.maxs = '8 8 8';
378                                 self.scale = 2.5;
379                                 self.avelocity = randomvec() * 720;
380                                 break;
381                         case PROJECTILE_MINE:
382                                 self.mins = '-4 -4 -4';
383                                 self.maxs = '4 4 4';
384                                 break;
385                         case PROJECTILE_PORTO_RED:
386                                 self.colormod = '2 1 1';
387                                 self.alphamod = 0.5;
388                                 self.move_movetype = MOVETYPE_BOUNCE;
389                                 self.move_touch = func_null;
390                                 break;
391                         case PROJECTILE_PORTO_BLUE:
392                                 self.colormod = '1 1 2';
393                                 self.alphamod = 0.5;
394                                 self.move_movetype = MOVETYPE_BOUNCE;
395                                 self.move_touch = func_null;
396                                 break;
397                         case PROJECTILE_HAGAR_BOUNCING:
398                                 self.move_movetype = MOVETYPE_BOUNCE;
399                                 self.move_touch = func_null;
400                                 break;
401                         case PROJECTILE_CRYLINK_BOUNCING:
402                                 self.move_movetype = MOVETYPE_BOUNCE;
403                                 self.move_touch = func_null;
404                                 break;
405                         case PROJECTILE_NAPALM_FOUNTAIN:
406                         case PROJECTILE_FIREBALL:
407                                 loopsound(self, CH_SHOTS_SINGLE, SND(FIREBALL_FLY2), VOL_BASE, ATTEN_NORM);
408                                 self.mins = '-16 -16 -16';
409                                 self.maxs = '16 16 16';
410                                 break;
411                         case PROJECTILE_FIREMINE:
412                                 loopsound(self, CH_SHOTS_SINGLE, SND(FIREBALL_FLY), VOL_BASE, ATTEN_NORM);
413                                 self.move_movetype = MOVETYPE_BOUNCE;
414                                 self.move_touch = func_null;
415                                 self.mins = '-4 -4 -4';
416                                 self.maxs = '4 4 4';
417                                 break;
418                         case PROJECTILE_TAG:
419                                 self.mins = '-2 -2 -2';
420                                 self.maxs = '2 2 2';
421                                 break;
422                         case PROJECTILE_FLAC:
423                                 self.mins = '-2 -2 -2';
424                                 self.maxs = '2 2 2';
425                                 break;
426                         case PROJECTILE_SEEKER:
427                                 loopsound(self, CH_SHOTS_SINGLE, SND(TAG_ROCKET_FLY), VOL_BASE, ATTEN_NORM);
428                                 self.mins = '-4 -4 -4';
429                                 self.maxs = '4 4 4';
430                                 break;
431                         case PROJECTILE_RAPTORBOMB:
432                                 self.mins = '-3 -3 -3';
433                                 self.maxs = '3 3 3';
434                                 break;
435                         case PROJECTILE_RAPTORBOMBLET:
436                                 break;
437                         case PROJECTILE_RAPTORCANNON:
438                                 break;
439                         case PROJECTILE_SPIDERROCKET:
440                                 loopsound(self, CH_SHOTS_SINGLE, SND(TAG_ROCKET_FLY), VOL_BASE, ATTEN_NORM);
441                                 break;
442                         case PROJECTILE_WAKIROCKET:
443                                 loopsound(self, CH_SHOTS_SINGLE, SND(TAG_ROCKET_FLY), VOL_BASE, ATTEN_NORM);
444                                 break;
445                         /*
446                         case PROJECTILE_WAKICANNON:
447                             break;
448                         case PROJECTILE_BUMBLE_GUN:
449                             // only new engines support sound moving with object
450                             loopsound(self, CH_SHOTS_SINGLE, SND(ELECTRO_FLY), VOL_BASE, ATTEN_NORM);
451                             self.mins = '0 0 -4';
452                             self.maxs = '0 0 -4';
453                             self.move_movetype = MOVETYPE_BOUNCE;
454                             self.move_touch = func_null;
455                             self.move_bounce_factor = g_balance_electro_secondary_bouncefactor;
456                             self.move_bounce_stopspeed = g_balance_electro_secondary_bouncestop;
457                             break;
458                         */
459                         default:
460                                 break;
461                 }
462
463                 if (Nade_FromProjectile(self.cnt) != NADE_TYPE_Null)
464                 {
465                         entity nade_type = Nade_FromProjectile(self.cnt);
466                         self.mins = '-16 -16 -16';
467                         self.maxs = '16 16 16';
468                         self.colormod = nade_type.m_color;
469                         self.move_movetype = MOVETYPE_BOUNCE;
470                         self.move_touch = func_null;
471                         self.scale = 1.5;
472                         self.avelocity = randomvec() * 720;
473
474                         if (nade_type == NADE_TYPE_TRANSLOCATE || nade_type == NADE_TYPE_SPAWN)
475                                 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
476                         else
477                                 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
478                 }
479
480                 MUTATOR_CALLHOOK(EditProjectile, self);
481
482                 setsize(self, self.mins, self.maxs);
483         }
484
485         return = true;
486
487         if (self.gravity)
488         {
489                 if (self.move_movetype == MOVETYPE_FLY)
490                         self.move_movetype = MOVETYPE_TOSS;
491                 if (self.move_movetype == MOVETYPE_BOUNCEMISSILE)
492                         self.move_movetype = MOVETYPE_BOUNCE;
493         }
494         else
495         {
496                 if (self.move_movetype == MOVETYPE_TOSS)
497                         self.move_movetype = MOVETYPE_FLY;
498                 if (self.move_movetype == MOVETYPE_BOUNCE)
499                         self.move_movetype = MOVETYPE_BOUNCEMISSILE;
500         }
501
502         if (!(self.count & 0x80))
503                 InterpolateOrigin_Note();
504
505         self.classname = "csqcprojectile";
506         self.draw = Projectile_Draw;
507         self.entremove = Ent_RemoveProjectile;
508 }
509
510 PRECACHE(Projectiles)
511 {
512         MUTATOR_CALLHOOK(PrecacheProjectiles);
513 }