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