Fix typo
[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/net_linked.qh>
10 #include <common/physics/movetypes/movetypes.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(entity this, entity toucher)
21 {
22         this.velocity = this.avelocity = '0 0 0';
23         set_movetype(this, 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 = this.flags;
63
64         if (this.count & 0x80)
65         {
66                 // UNSET_ONGROUND(this);
67                 if (this.move_movetype == MOVETYPE_NONE || this.move_movetype == MOVETYPE_FLY)
68                         Movetype_Physics_NoMatchServer(this);
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(this, autocvar_cl_projectiles_sloppy);
76                 if (!IS_ONGROUND(this))
77                         if (this.velocity != '0 0 0')
78                                 this.angles = vectoangles(this.velocity);
79         }
80         else
81         {
82                 InterpolateOrigin_Do(this);
83         }
84
85         if (this.count & 0x80)
86         {
87                 drawn = (time >= this.spawntime - 0.02);
88                 t = max(time, this.spawntime);
89         }
90         else
91         {
92                 drawn = (this.iflags & IFLAG_VALID);
93                 t = time;
94         }
95
96         if (!(f & FL_ONGROUND))
97         {
98                 rot = '0 0 0';
99                 switch (this.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                         case PROJECTILE_ROCKET:
113                                 rot = '0 0 720'; // spinning
114                                 break;
115                         default:
116                                 break;
117                 }
118
119                 if (Projectile_isnade(this.cnt))
120                         rot = this.avelocity;
121
122                 this.angles = AnglesTransform_ToAngles(AnglesTransform_Multiply(AnglesTransform_FromAngles(this.angles), rot * (t - this.spawntime)));
123         }
124
125         vector ang;
126         ang = this.angles;
127         ang.x = -ang.x;
128         makevectors(ang);
129
130         a = 1 - (time - this.fade_time) * this.fade_rate;
131         this.alpha = bound(0, this.alphamod * a, 1);
132         if (this.alpha <= 0)
133                 drawn = 0;
134         this.renderflags = 0;
135
136         trailorigin = this.origin;
137         switch (this.cnt)
138         {
139                 case PROJECTILE_GRENADE:
140                 case PROJECTILE_GRENADE_BOUNCING:
141                         trailorigin += v_right * 1 + v_forward * -10;
142                         break;
143                 default:
144                         break;
145         }
146
147         if (Projectile_isnade(this.cnt))
148                 trailorigin += v_up * 4;
149
150         if (drawn)
151                 Projectile_DrawTrail(this, trailorigin);
152         else
153                 Projectile_ResetTrail(this, trailorigin);
154
155         this.drawmask = 0;
156
157         if (!drawn)
158                 return;
159
160         switch (this.cnt)
161         {
162                 // Possibly add dlights here.
163                 default:
164                         break;
165         }
166
167         this.drawmask = MASK_NORMAL;
168 }
169
170 void loopsound(entity e, int ch, Sound samp, float vol, float attn)
171 {
172     TC(int, ch);
173         if (e.silent)
174                 return;
175
176         sound(e, ch, samp, vol, attn);
177         e.snd_looping = ch;
178 }
179
180 void Ent_RemoveProjectile(entity this)
181 {
182         if (this.count & 0x80)
183         {
184                 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.05, MOVE_NORMAL, this);
185                 Projectile_DrawTrail(this, trace_endpos);
186         }
187 }
188
189 NET_HANDLE(ENT_CLIENT_PROJECTILE, bool isnew)
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         int f = ReadByte();
206         this.count = (f & 0x80);
207         this.flags |= FL_PROJECTILE;
208         this.iflags = (this.iflags & IFLAG_INTERNALMASK) | IFLAG_AUTOANGLES | IFLAG_ANGLES | IFLAG_ORIGIN;
209         this.solid = SOLID_TRIGGER;
210         // this.effects = EF_NOMODELFLAGS;
211
212         // this should make collisions with bmodels more exact, but it leads to
213         // projectiles no longer being able to lie on a bmodel
214         this.move_nomonsters = MOVE_WORLDONLY;
215         if (f & 0x40)
216                 SET_ONGROUND(this);
217         else
218                 UNSET_ONGROUND(this);
219
220         if (!this.move_time)
221         {
222                 // for some unknown reason, we don't need to care for
223                 // sv_gameplayfix_delayprojectiles here.
224                 this.move_time = time;
225                 this.spawntime = time;
226         }
227         else
228         {
229                 this.move_time = max(this.move_time, time);
230         }
231
232         if (!(this.count & 0x80))
233                 InterpolateOrigin_Undo(this);
234
235         if (f & 1)
236         {
237                 this.origin_x = ReadCoord();
238                 this.origin_y = ReadCoord();
239                 this.origin_z = ReadCoord();
240                 setorigin(this, this.origin);
241                 if (this.count & 0x80)
242                 {
243                         this.velocity_x = ReadCoord();
244                         this.velocity_y = ReadCoord();
245                         this.velocity_z = ReadCoord();
246                         if (f & 0x10)
247                                 this.gravity = ReadCoord();
248                         else
249                                 this.gravity = 0;  // none
250                 }
251
252                 if (time == this.spawntime || (this.count & 0x80) || (f & 0x08))
253                 {
254                         this.trail_oldorigin = this.origin;
255                         if (!(this.count & 0x80))
256                                 InterpolateOrigin_Reset(this);
257                 }
258
259                 if (f & 0x20)
260                 {
261                         this.fade_time = time + ReadByte() * ticrate;
262                         this.fade_rate = 1 / (ReadByte() * ticrate);
263                 }
264                 else
265                 {
266                         this.fade_time = 0;
267                         this.fade_rate = 0;
268                 }
269
270                 int myteam = ReadByte();
271                 this.team = myteam - 1;
272
273                 if(teamplay)
274                 {
275                         if(myteam)
276                                 this.colormap = (this.team) * 0x11; // note: team - 1 on server (client uses different numbers)
277                         else
278                                 this.colormap = 0x00;
279                         this.colormap |= BIT(10); // RENDER_COLORMAPPED
280                 }
281                 else
282                         this.colormap = myteam;
283                 // TODO: projectiles use glowmaps for their color, not teams
284                 #if 0
285                 if(this.colormap > 0)
286                         this.glowmod = colormapPaletteColor(this.colormap & 0x0F, true) * 2;
287                 else
288                         this.glowmod = '1 1 1';
289                 #endif
290         }
291
292         if (f & 2)
293         {
294                 this.cnt = ReadByte();
295
296                 this.silent = (this.cnt & 0x80);
297                 this.cnt = (this.cnt & 0x7F);
298
299                 this.scale = 1;
300                 this.traileffect = 0;
301                 switch (this.cnt)
302                 {
303 #define HANDLE(id) case PROJECTILE_##id: setmodel(this, MDL_PROJECTILE_##id);
304                         HANDLE(ELECTRO)            this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
305                         HANDLE(ROCKET)             this.traileffect = EFFECT_TR_ROCKET.m_id; this.scale = 2; break;
306                         HANDLE(CRYLINK)            this.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
307                         HANDLE(CRYLINK_BOUNCING)   this.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
308                         HANDLE(ELECTRO_BEAM)       this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
309                         HANDLE(GRENADE)            this.traileffect = EFFECT_TR_GRENADE.m_id; break;
310                         HANDLE(GRENADE_BOUNCING)   this.traileffect = EFFECT_TR_GRENADE.m_id; break;
311                         HANDLE(MINE)               this.traileffect = EFFECT_TR_GRENADE.m_id; break;
312                         HANDLE(BLASTER)            this.traileffect = EFFECT_Null.m_id; break;
313                         HANDLE(ARC_BOLT)           this.traileffect = EFFECT_Null.m_id; break;
314                         HANDLE(HLAC)               this.traileffect = EFFECT_Null.m_id; break;
315                         HANDLE(PORTO_RED)          this.traileffect = EFFECT_TR_WIZSPIKE.m_id; this.scale = 4; break;
316                         HANDLE(PORTO_BLUE)         this.traileffect = EFFECT_TR_WIZSPIKE.m_id; this.scale = 4; break;
317                         HANDLE(HOOKBOMB)           this.traileffect = EFFECT_TR_KNIGHTSPIKE.m_id; break;
318                         HANDLE(HAGAR)              this.traileffect = EFFECT_HAGAR_ROCKET.m_id; this.scale = 0.75; break;
319                         HANDLE(HAGAR_BOUNCING)     this.traileffect = EFFECT_HAGAR_ROCKET.m_id; this.scale = 0.75; break;
320                         HANDLE(FIREBALL)           this.modelindex = 0; this.traileffect = EFFECT_FIREBALL.m_id; break; // particle effect is good enough
321                         HANDLE(FIREMINE)           this.modelindex = 0; this.traileffect = EFFECT_FIREMINE.m_id; break; // particle effect is good enough
322                         HANDLE(TAG)                this.traileffect = EFFECT_TR_ROCKET.m_id; break;
323                         HANDLE(FLAC)               this.scale = 0.4; this.traileffect = EFFECT_FLAC_TRAIL.m_id; break;
324                         HANDLE(SEEKER)             this.traileffect = EFFECT_SEEKER_TRAIL.m_id; break;
325
326                         HANDLE(MAGE_SPIKE)         this.traileffect = EFFECT_TR_VORESPIKE.m_id; break;
327                         HANDLE(SHAMBLER_LIGHTNING) this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
328
329                         HANDLE(RAPTORBOMB)         this.gravity = 1; this.avelocity = '0 0 180'; this.traileffect = EFFECT_Null.m_id; break;
330                         HANDLE(RAPTORBOMBLET)      this.gravity = 1; this.avelocity = '0 0 180'; this.traileffect = EFFECT_Null.m_id; break;
331                         HANDLE(RAPTORCANNON)       this.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
332
333                         HANDLE(SPIDERROCKET)       this.traileffect = EFFECT_SPIDERBOT_ROCKET_TRAIL.m_id; break;
334                         HANDLE(WAKIROCKET)         this.traileffect = EFFECT_RACER_ROCKET_TRAIL.m_id; break;
335                         HANDLE(WAKICANNON)         this.traileffect = EFFECT_Null.m_id; break;
336
337                         HANDLE(BUMBLE_GUN)         this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
338                         HANDLE(BUMBLE_BEAM)        this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
339
340                         HANDLE(RPC)                this.traileffect = EFFECT_TR_ROCKET.m_id; break;
341
342                         HANDLE(ROCKETMINSTA_LASER) this.traileffect = EFFECT_ROCKETMINSTA_LASER(this.team).m_id; break;
343 #undef HANDLE
344                         default:
345                                 if (MUTATOR_CALLHOOK(Ent_Projectile, this))
346                                         break;
347
348                                 error("Received invalid CSQC projectile, can't work with this!");
349                                 break;
350                 }
351
352                 this.mins = '0 0 0';
353                 this.maxs = '0 0 0';
354                 this.colormod = '0 0 0';
355                 settouch(this, SUB_Stop);
356                 set_movetype(this, MOVETYPE_TOSS);
357                 this.alphamod = 1;
358
359                 switch (this.cnt)
360                 {
361                         case PROJECTILE_ELECTRO:
362                                 // only new engines support sound moving with object
363                                 loopsound(this, CH_SHOTS_SINGLE, SND_ELECTRO_FLY, VOL_BASE, ATTEN_NORM);
364                                 this.mins = '-4 -4 -4';
365                                 this.maxs = '4 4 4';
366                                 set_movetype(this, MOVETYPE_BOUNCE);
367                                 settouch(this, func_null);
368                                 this.bouncefactor = WEP_CVAR_SEC(electro, bouncefactor);
369                                 this.bouncestop = WEP_CVAR_SEC(electro, bouncestop);
370                                 break;
371                         case PROJECTILE_RPC:
372                         case PROJECTILE_ROCKET:
373                                 loopsound(this, CH_SHOTS_SINGLE, SND_ROCKET_FLY, VOL_BASE, ATTEN_NORM);
374                                 this.mins = '-3 -3 -3';
375                                 this.maxs = '3 3 3';
376                                 break;
377                         case PROJECTILE_GRENADE:
378                                 this.mins = '-3 -3 -3';
379                                 this.maxs = '3 3 3';
380                                 break;
381                         case PROJECTILE_GRENADE_BOUNCING:
382                                 this.mins = '-3 -3 -3';
383                                 this.maxs = '3 3 3';
384                                 set_movetype(this, MOVETYPE_BOUNCE);
385                                 settouch(this, func_null);
386                                 this.bouncefactor = WEP_CVAR(mortar, bouncefactor);
387                                 this.bouncestop = WEP_CVAR(mortar, bouncestop);
388                                 break;
389                         case PROJECTILE_SHAMBLER_LIGHTNING:
390                                 this.mins = '-8 -8 -8';
391                                 this.maxs = '8 8 8';
392                                 this.scale = 2.5;
393                                 this.avelocity = randomvec() * 720;
394                                 break;
395                         case PROJECTILE_MINE:
396                                 this.mins = '-4 -4 -4';
397                                 this.maxs = '4 4 4';
398                                 break;
399                         case PROJECTILE_PORTO_RED:
400                                 this.colormod = '2 1 1';
401                                 this.alphamod = 0.5;
402                                 set_movetype(this, MOVETYPE_BOUNCE);
403                                 settouch(this, func_null);
404                                 break;
405                         case PROJECTILE_PORTO_BLUE:
406                                 this.colormod = '1 1 2';
407                                 this.alphamod = 0.5;
408                                 set_movetype(this, MOVETYPE_BOUNCE);
409                                 settouch(this, func_null);
410                                 break;
411                         case PROJECTILE_HAGAR_BOUNCING:
412                                 set_movetype(this, MOVETYPE_BOUNCE);
413                                 settouch(this, func_null);
414                                 break;
415                         case PROJECTILE_CRYLINK_BOUNCING:
416                                 set_movetype(this, MOVETYPE_BOUNCE);
417                                 settouch(this, func_null);
418                                 break;
419                         case PROJECTILE_FIREBALL:
420                                 loopsound(this, CH_SHOTS_SINGLE, SND_FIREBALL_FLY2, VOL_BASE, ATTEN_NORM);
421                                 this.mins = '-16 -16 -16';
422                                 this.maxs = '16 16 16';
423                                 break;
424                         case PROJECTILE_FIREMINE:
425                                 loopsound(this, CH_SHOTS_SINGLE, SND_FIREBALL_FLY, VOL_BASE, ATTEN_NORM);
426                                 set_movetype(this, MOVETYPE_BOUNCE);
427                                 settouch(this, func_null);
428                                 this.mins = '-4 -4 -4';
429                                 this.maxs = '4 4 4';
430                                 break;
431                         case PROJECTILE_TAG:
432                                 this.mins = '-2 -2 -2';
433                                 this.maxs = '2 2 2';
434                                 break;
435                         case PROJECTILE_FLAC:
436                                 this.mins = '-2 -2 -2';
437                                 this.maxs = '2 2 2';
438                                 break;
439                         case PROJECTILE_SEEKER:
440                                 loopsound(this, CH_SHOTS_SINGLE, SND_TAG_ROCKET_FLY, VOL_BASE, ATTEN_NORM);
441                                 this.mins = '-4 -4 -4';
442                                 this.maxs = '4 4 4';
443                                 break;
444                         case PROJECTILE_RAPTORBOMB:
445                                 this.mins = '-3 -3 -3';
446                                 this.maxs = '3 3 3';
447                                 break;
448                         case PROJECTILE_RAPTORBOMBLET:
449                                 break;
450                         case PROJECTILE_RAPTORCANNON:
451                                 break;
452                         case PROJECTILE_SPIDERROCKET:
453                                 loopsound(this, CH_SHOTS_SINGLE, SND_TAG_ROCKET_FLY, VOL_BASE, ATTEN_NORM);
454                                 break;
455                         case PROJECTILE_WAKIROCKET:
456                                 loopsound(this, CH_SHOTS_SINGLE, SND_TAG_ROCKET_FLY, VOL_BASE, ATTEN_NORM);
457                                 break;
458                         /*
459                         case PROJECTILE_WAKICANNON:
460                             break;
461                         case PROJECTILE_BUMBLE_GUN:
462                             // only new engines support sound moving with object
463                             loopsound(this, CH_SHOTS_SINGLE, SND_ELECTRO_FLY, VOL_BASE, ATTEN_NORM);
464                             this.mins = '0 0 -4';
465                             this.maxs = '0 0 -4';
466                             this.move_movetype = MOVETYPE_BOUNCE;
467                             settouch(this, func_null);
468                             this.bouncefactor = WEP_CVAR_SEC(electro, bouncefactor);
469                             this.bouncestop = WEP_CVAR_SEC(electro, bouncestop);
470                             break;
471                         */
472                         default:
473                                 break;
474                 }
475
476                 MUTATOR_CALLHOOK(EditProjectile, this);
477
478                 setsize(this, this.mins, this.maxs);
479         }
480
481         return = true;
482
483         if (this.gravity)
484         {
485                 if (this.move_movetype == MOVETYPE_FLY)
486                         set_movetype(this, MOVETYPE_TOSS);
487                 if (this.move_movetype == MOVETYPE_BOUNCEMISSILE)
488                         set_movetype(this, MOVETYPE_BOUNCE);
489         }
490         else
491         {
492                 if (this.move_movetype == MOVETYPE_TOSS)
493                         set_movetype(this, MOVETYPE_FLY);
494                 if (this.move_movetype == MOVETYPE_BOUNCE)
495                         set_movetype(this, MOVETYPE_BOUNCEMISSILE);
496         }
497
498         if (!(this.count & 0x80))
499                 InterpolateOrigin_Note(this);
500
501         this.classname = "csqcprojectile";
502         this.draw = Projectile_Draw;
503         if (isnew) IL_PUSH(g_drawables, this);
504         this.entremove = Ent_RemoveProjectile;
505 }
506
507 PRECACHE(Projectiles)
508 {
509         MUTATOR_CALLHOOK(PrecacheProjectiles);
510 }