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