]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/weapons/projectile.qc
Add a new attack mode to Arc (disabled by default)
[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(ARC_BOLT)           this.traileffect = EFFECT_Null.m_id; break;
298                         HANDLE(HLAC)               this.traileffect = EFFECT_Null.m_id; break;
299                         HANDLE(PORTO_RED)          this.traileffect = EFFECT_TR_WIZSPIKE.m_id; this.scale = 4; break;
300                         HANDLE(PORTO_BLUE)         this.traileffect = EFFECT_TR_WIZSPIKE.m_id; this.scale = 4; break;
301                         HANDLE(HOOKBOMB)           this.traileffect = EFFECT_TR_KNIGHTSPIKE.m_id; break;
302                         HANDLE(HAGAR)              this.traileffect = EFFECT_HAGAR_ROCKET.m_id; this.scale = 0.75; break;
303                         HANDLE(HAGAR_BOUNCING)     this.traileffect = EFFECT_HAGAR_ROCKET.m_id; this.scale = 0.75; break;
304                         HANDLE(FIREBALL)           this.modelindex = 0; this.traileffect = EFFECT_FIREBALL.m_id; break; // particle effect is good enough
305                         HANDLE(FIREMINE)           this.modelindex = 0; this.traileffect = EFFECT_FIREMINE.m_id; break; // particle effect is good enough
306                         HANDLE(TAG)                this.traileffect = EFFECT_TR_ROCKET.m_id; break;
307                         HANDLE(FLAC)               this.scale = 0.4; this.traileffect = EFFECT_FLAC_TRAIL.m_id; break;
308                         HANDLE(SEEKER)             this.traileffect = EFFECT_SEEKER_TRAIL.m_id; break;
309
310                         HANDLE(MAGE_SPIKE)         this.traileffect = EFFECT_TR_VORESPIKE.m_id; break;
311                         HANDLE(SHAMBLER_LIGHTNING) this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
312
313                         HANDLE(RAPTORBOMB)         this.gravity = 1; this.avelocity = '0 0 180'; this.traileffect = EFFECT_Null.m_id; break;
314                         HANDLE(RAPTORBOMBLET)      this.gravity = 1; this.avelocity = '0 0 180'; this.traileffect = EFFECT_Null.m_id; break;
315                         HANDLE(RAPTORCANNON)       this.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
316
317                         HANDLE(SPIDERROCKET)       this.traileffect = EFFECT_SPIDERBOT_ROCKET_TRAIL.m_id; break;
318                         HANDLE(WAKIROCKET)         this.traileffect = EFFECT_RACER_ROCKET_TRAIL.m_id; break;
319                         HANDLE(WAKICANNON)         this.traileffect = EFFECT_Null.m_id; break;
320
321                         HANDLE(BUMBLE_GUN)         this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
322                         HANDLE(BUMBLE_BEAM)        this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
323
324                         HANDLE(RPC)                this.traileffect = EFFECT_TR_ROCKET.m_id; break;
325
326                         HANDLE(ROCKETMINSTA_LASER) this.traileffect = EFFECT_ROCKETMINSTA_LASER(this.team).m_id; break;
327 #undef HANDLE
328                         default:
329                                 if (MUTATOR_CALLHOOK(Ent_Projectile, this))
330                                         break;
331
332                                 error("Received invalid CSQC projectile, can't work with this!");
333                                 break;
334                 }
335
336                 this.mins = '0 0 0';
337                 this.maxs = '0 0 0';
338                 this.colormod = '0 0 0';
339                 this.move_touch = SUB_Stop_self;
340                 this.move_movetype = MOVETYPE_TOSS;
341                 this.alphamod = 1;
342
343                 switch (this.cnt)
344                 {
345                         case PROJECTILE_ELECTRO:
346                                 // only new engines support sound moving with object
347                                 loopsound(this, CH_SHOTS_SINGLE, SND(ELECTRO_FLY), VOL_BASE, ATTEN_NORM);
348                                 this.mins = '0 0 -4';
349                                 this.maxs = '0 0 -4';
350                                 this.move_movetype = MOVETYPE_BOUNCE;
351                                 this.move_touch = func_null;
352                                 this.move_bounce_factor = WEP_CVAR_SEC(electro, bouncefactor);
353                                 this.move_bounce_stopspeed = WEP_CVAR_SEC(electro, bouncestop);
354                                 break;
355                         case PROJECTILE_RPC:
356                         case PROJECTILE_ROCKET:
357                                 loopsound(this, CH_SHOTS_SINGLE, SND(ROCKET_FLY), VOL_BASE, ATTEN_NORM);
358                                 this.mins = '-3 -3 -3';
359                                 this.maxs = '3 3 3';
360                                 break;
361                         case PROJECTILE_GRENADE:
362                                 this.mins = '-3 -3 -3';
363                                 this.maxs = '3 3 3';
364                                 break;
365                         case PROJECTILE_GRENADE_BOUNCING:
366                                 this.mins = '-3 -3 -3';
367                                 this.maxs = '3 3 3';
368                                 this.move_movetype = MOVETYPE_BOUNCE;
369                                 this.move_touch = func_null;
370                                 this.move_bounce_factor = WEP_CVAR(mortar, bouncefactor);
371                                 this.move_bounce_stopspeed = WEP_CVAR(mortar, bouncestop);
372                                 break;
373                         case PROJECTILE_SHAMBLER_LIGHTNING:
374                                 this.mins = '-8 -8 -8';
375                                 this.maxs = '8 8 8';
376                                 this.scale = 2.5;
377                                 this.avelocity = randomvec() * 720;
378                                 break;
379                         case PROJECTILE_MINE:
380                                 this.mins = '-4 -4 -4';
381                                 this.maxs = '4 4 4';
382                                 break;
383                         case PROJECTILE_PORTO_RED:
384                                 this.colormod = '2 1 1';
385                                 this.alphamod = 0.5;
386                                 this.move_movetype = MOVETYPE_BOUNCE;
387                                 this.move_touch = func_null;
388                                 break;
389                         case PROJECTILE_PORTO_BLUE:
390                                 this.colormod = '1 1 2';
391                                 this.alphamod = 0.5;
392                                 this.move_movetype = MOVETYPE_BOUNCE;
393                                 this.move_touch = func_null;
394                                 break;
395                         case PROJECTILE_HAGAR_BOUNCING:
396                                 this.move_movetype = MOVETYPE_BOUNCE;
397                                 this.move_touch = func_null;
398                                 break;
399                         case PROJECTILE_CRYLINK_BOUNCING:
400                                 this.move_movetype = MOVETYPE_BOUNCE;
401                                 this.move_touch = func_null;
402                                 break;
403                         case PROJECTILE_FIREBALL:
404                                 loopsound(this, CH_SHOTS_SINGLE, SND(FIREBALL_FLY2), VOL_BASE, ATTEN_NORM);
405                                 this.mins = '-16 -16 -16';
406                                 this.maxs = '16 16 16';
407                                 break;
408                         case PROJECTILE_FIREMINE:
409                                 loopsound(this, CH_SHOTS_SINGLE, SND(FIREBALL_FLY), VOL_BASE, ATTEN_NORM);
410                                 this.move_movetype = MOVETYPE_BOUNCE;
411                                 this.move_touch = func_null;
412                                 this.mins = '-4 -4 -4';
413                                 this.maxs = '4 4 4';
414                                 break;
415                         case PROJECTILE_TAG:
416                                 this.mins = '-2 -2 -2';
417                                 this.maxs = '2 2 2';
418                                 break;
419                         case PROJECTILE_FLAC:
420                                 this.mins = '-2 -2 -2';
421                                 this.maxs = '2 2 2';
422                                 break;
423                         case PROJECTILE_SEEKER:
424                                 loopsound(this, CH_SHOTS_SINGLE, SND(TAG_ROCKET_FLY), VOL_BASE, ATTEN_NORM);
425                                 this.mins = '-4 -4 -4';
426                                 this.maxs = '4 4 4';
427                                 break;
428                         case PROJECTILE_RAPTORBOMB:
429                                 this.mins = '-3 -3 -3';
430                                 this.maxs = '3 3 3';
431                                 break;
432                         case PROJECTILE_RAPTORBOMBLET:
433                                 break;
434                         case PROJECTILE_RAPTORCANNON:
435                                 break;
436                         case PROJECTILE_SPIDERROCKET:
437                                 loopsound(this, CH_SHOTS_SINGLE, SND(TAG_ROCKET_FLY), VOL_BASE, ATTEN_NORM);
438                                 break;
439                         case PROJECTILE_WAKIROCKET:
440                                 loopsound(this, CH_SHOTS_SINGLE, SND(TAG_ROCKET_FLY), VOL_BASE, ATTEN_NORM);
441                                 break;
442                         /*
443                         case PROJECTILE_WAKICANNON:
444                             break;
445                         case PROJECTILE_BUMBLE_GUN:
446                             // only new engines support sound moving with object
447                             loopsound(this, CH_SHOTS_SINGLE, SND(ELECTRO_FLY), VOL_BASE, ATTEN_NORM);
448                             this.mins = '0 0 -4';
449                             this.maxs = '0 0 -4';
450                             this.move_movetype = MOVETYPE_BOUNCE;
451                             this.move_touch = func_null;
452                             this.move_bounce_factor = WEP_CVAR_SEC(electro, bouncefactor);
453                             this.move_bounce_stopspeed = WEP_CVAR_SEC(electro, bouncestop);
454                             break;
455                         */
456                         default:
457                                 break;
458                 }
459
460                 MUTATOR_CALLHOOK(EditProjectile, this);
461
462                 setsize(this, this.mins, this.maxs);
463         }
464
465         return = true;
466
467         if (this.gravity)
468         {
469                 if (this.move_movetype == MOVETYPE_FLY)
470                         this.move_movetype = MOVETYPE_TOSS;
471                 if (this.move_movetype == MOVETYPE_BOUNCEMISSILE)
472                         this.move_movetype = MOVETYPE_BOUNCE;
473         }
474         else
475         {
476                 if (this.move_movetype == MOVETYPE_TOSS)
477                         this.move_movetype = MOVETYPE_FLY;
478                 if (this.move_movetype == MOVETYPE_BOUNCE)
479                         this.move_movetype = MOVETYPE_BOUNCEMISSILE;
480         }
481
482         if (!(this.count & 0x80))
483                 InterpolateOrigin_Note(this);
484
485         this.classname = "csqcprojectile";
486         this.draw = Projectile_Draw;
487         this.entremove = Ent_RemoveProjectile;
488 }
489
490 PRECACHE(Projectiles)
491 {
492         MUTATOR_CALLHOOK(PrecacheProjectiles);
493 }