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