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