]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/weapons/projectile.qc
CSQC: use touch accessors
[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     TC(int, ch);
175         if (e.silent)
176                 return;
177
178         _sound(e, ch, samp, vol, attn);
179         e.snd_looping = ch;
180 }
181
182 void Ent_RemoveProjectile(entity this)
183 {
184         if (this.count & 0x80)
185         {
186                 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.05, MOVE_NORMAL, this);
187                 Projectile_DrawTrail(this, trace_endpos);
188         }
189 }
190
191 NET_HANDLE(ENT_CLIENT_PROJECTILE, bool isnew)
192 {
193         // projectile properties:
194         //   kind (interpolated, or clientside)
195         //
196         //   modelindex
197         //   origin
198         //   scale
199         //   if clientside:
200         //     velocity
201         //     gravity
202         //   soundindex (hardcoded list)
203         //   effects
204         //
205         // projectiles don't send angles, because they always follow the velocity
206
207         int f = ReadByte();
208         this.count = (f & 0x80);
209         this.flags |= FL_PROJECTILE;
210         this.iflags = (this.iflags & IFLAG_INTERNALMASK) | IFLAG_AUTOANGLES | IFLAG_ANGLES | IFLAG_ORIGIN;
211         this.solid = SOLID_TRIGGER;
212         // this.effects = EF_NOMODELFLAGS;
213
214         // this should make collisions with bmodels more exact, but it leads to
215         // projectiles no longer being able to lie on a bmodel
216         this.move_nomonsters = MOVE_WORLDONLY;
217         if (f & 0x40)
218                 this.move_flags |= FL_ONGROUND;
219         else
220                 this.move_flags &= ~FL_ONGROUND;
221
222         if (!this.move_time)
223         {
224                 // for some unknown reason, we don't need to care for
225                 // sv_gameplayfix_delayprojectiles here.
226                 this.move_time = time;
227                 this.spawntime = time;
228         }
229         else
230         {
231                 this.move_time = max(this.move_time, time);
232         }
233
234         if (!(this.count & 0x80))
235                 InterpolateOrigin_Undo(this);
236
237         if (f & 1)
238         {
239                 this.origin_x = ReadCoord();
240                 this.origin_y = ReadCoord();
241                 this.origin_z = ReadCoord();
242                 setorigin(this, this.origin);
243                 if (this.count & 0x80)
244                 {
245                         this.velocity_x = ReadCoord();
246                         this.velocity_y = ReadCoord();
247                         this.velocity_z = ReadCoord();
248                         if (f & 0x10)
249                                 this.gravity = ReadCoord();
250                         else
251                                 this.gravity = 0;  // none
252                         this.move_origin = this.origin;
253                         this.move_velocity = this.velocity;
254                 }
255
256                 if (time == this.spawntime || (this.count & 0x80) || (f & 0x08))
257                 {
258                         this.trail_oldorigin = this.origin;
259                         if (!(this.count & 0x80))
260                                 InterpolateOrigin_Reset(this);
261                 }
262
263                 if (f & 0x20)
264                 {
265                         this.fade_time = time + ReadByte() * ticrate;
266                         this.fade_rate = 1 / (ReadByte() * ticrate);
267                 }
268                 else
269                 {
270                         this.fade_time = 0;
271                         this.fade_rate = 0;
272                 }
273
274                 this.team = ReadByte() - 1;
275         }
276
277         if (f & 2)
278         {
279                 this.cnt = ReadByte();
280
281                 this.silent = (this.cnt & 0x80);
282                 this.cnt = (this.cnt & 0x7F);
283
284                 this.scale = 1;
285                 this.traileffect = 0;
286                 switch (this.cnt)
287                 {
288 #define HANDLE(id) case PROJECTILE_##id: setmodel(this, MDL_PROJECTILE_##id);
289                         HANDLE(ELECTRO)            this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
290                         HANDLE(ROCKET)             this.traileffect = EFFECT_TR_ROCKET.m_id; this.scale = 2; break;
291                         HANDLE(CRYLINK)            this.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
292                         HANDLE(CRYLINK_BOUNCING)   this.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
293                         HANDLE(ELECTRO_BEAM)       this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
294                         HANDLE(GRENADE)            this.traileffect = EFFECT_TR_GRENADE.m_id; break;
295                         HANDLE(GRENADE_BOUNCING)   this.traileffect = EFFECT_TR_GRENADE.m_id; break;
296                         HANDLE(MINE)               this.traileffect = EFFECT_TR_GRENADE.m_id; break;
297                         HANDLE(BLASTER)            this.traileffect = EFFECT_Null.m_id; break;
298                         HANDLE(ARC_BOLT)           this.traileffect = EFFECT_Null.m_id; break;
299                         HANDLE(HLAC)               this.traileffect = EFFECT_Null.m_id; break;
300                         HANDLE(PORTO_RED)          this.traileffect = EFFECT_TR_WIZSPIKE.m_id; this.scale = 4; break;
301                         HANDLE(PORTO_BLUE)         this.traileffect = EFFECT_TR_WIZSPIKE.m_id; this.scale = 4; break;
302                         HANDLE(HOOKBOMB)           this.traileffect = EFFECT_TR_KNIGHTSPIKE.m_id; break;
303                         HANDLE(HAGAR)              this.traileffect = EFFECT_HAGAR_ROCKET.m_id; this.scale = 0.75; break;
304                         HANDLE(HAGAR_BOUNCING)     this.traileffect = EFFECT_HAGAR_ROCKET.m_id; this.scale = 0.75; break;
305                         HANDLE(FIREBALL)           this.modelindex = 0; this.traileffect = EFFECT_FIREBALL.m_id; break; // particle effect is good enough
306                         HANDLE(FIREMINE)           this.modelindex = 0; this.traileffect = EFFECT_FIREMINE.m_id; break; // particle effect is good enough
307                         HANDLE(TAG)                this.traileffect = EFFECT_TR_ROCKET.m_id; break;
308                         HANDLE(FLAC)               this.scale = 0.4; this.traileffect = EFFECT_FLAC_TRAIL.m_id; break;
309                         HANDLE(SEEKER)             this.traileffect = EFFECT_SEEKER_TRAIL.m_id; break;
310
311                         HANDLE(MAGE_SPIKE)         this.traileffect = EFFECT_TR_VORESPIKE.m_id; break;
312                         HANDLE(SHAMBLER_LIGHTNING) this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
313
314                         HANDLE(RAPTORBOMB)         this.gravity = 1; this.avelocity = '0 0 180'; this.traileffect = EFFECT_Null.m_id; break;
315                         HANDLE(RAPTORBOMBLET)      this.gravity = 1; this.avelocity = '0 0 180'; this.traileffect = EFFECT_Null.m_id; break;
316                         HANDLE(RAPTORCANNON)       this.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
317
318                         HANDLE(SPIDERROCKET)       this.traileffect = EFFECT_SPIDERBOT_ROCKET_TRAIL.m_id; break;
319                         HANDLE(WAKIROCKET)         this.traileffect = EFFECT_RACER_ROCKET_TRAIL.m_id; break;
320                         HANDLE(WAKICANNON)         this.traileffect = EFFECT_Null.m_id; break;
321
322                         HANDLE(BUMBLE_GUN)         this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
323                         HANDLE(BUMBLE_BEAM)        this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
324
325                         HANDLE(RPC)                this.traileffect = EFFECT_TR_ROCKET.m_id; break;
326
327                         HANDLE(ROCKETMINSTA_LASER) this.traileffect = EFFECT_ROCKETMINSTA_LASER(this.team).m_id; break;
328 #undef HANDLE
329                         default:
330                                 if (MUTATOR_CALLHOOK(Ent_Projectile, this))
331                                         break;
332
333                                 error("Received invalid CSQC projectile, can't work with this!");
334                                 break;
335                 }
336
337                 this.mins = '0 0 0';
338                 this.maxs = '0 0 0';
339                 this.colormod = '0 0 0';
340                 settouch(this, SUB_Stop_self);
341                 this.move_movetype = MOVETYPE_TOSS;
342                 this.alphamod = 1;
343
344                 switch (this.cnt)
345                 {
346                         case PROJECTILE_ELECTRO:
347                                 // only new engines support sound moving with object
348                                 loopsound(this, CH_SHOTS_SINGLE, SND(ELECTRO_FLY), VOL_BASE, ATTEN_NORM);
349                                 this.mins = '0 0 -4';
350                                 this.maxs = '0 0 -4';
351                                 this.move_movetype = MOVETYPE_BOUNCE;
352                                 settouch(this, func_null);
353                                 this.move_bounce_factor = WEP_CVAR_SEC(electro, bouncefactor);
354                                 this.move_bounce_stopspeed = WEP_CVAR_SEC(electro, bouncestop);
355                                 break;
356                         case PROJECTILE_RPC:
357                         case PROJECTILE_ROCKET:
358                                 loopsound(this, CH_SHOTS_SINGLE, SND(ROCKET_FLY), VOL_BASE, ATTEN_NORM);
359                                 this.mins = '-3 -3 -3';
360                                 this.maxs = '3 3 3';
361                                 break;
362                         case PROJECTILE_GRENADE:
363                                 this.mins = '-3 -3 -3';
364                                 this.maxs = '3 3 3';
365                                 break;
366                         case PROJECTILE_GRENADE_BOUNCING:
367                                 this.mins = '-3 -3 -3';
368                                 this.maxs = '3 3 3';
369                                 this.move_movetype = MOVETYPE_BOUNCE;
370                                 settouch(this, func_null);
371                                 this.move_bounce_factor = WEP_CVAR(mortar, bouncefactor);
372                                 this.move_bounce_stopspeed = WEP_CVAR(mortar, bouncestop);
373                                 break;
374                         case PROJECTILE_SHAMBLER_LIGHTNING:
375                                 this.mins = '-8 -8 -8';
376                                 this.maxs = '8 8 8';
377                                 this.scale = 2.5;
378                                 this.avelocity = randomvec() * 720;
379                                 break;
380                         case PROJECTILE_MINE:
381                                 this.mins = '-4 -4 -4';
382                                 this.maxs = '4 4 4';
383                                 break;
384                         case PROJECTILE_PORTO_RED:
385                                 this.colormod = '2 1 1';
386                                 this.alphamod = 0.5;
387                                 this.move_movetype = MOVETYPE_BOUNCE;
388                                 settouch(this, func_null);
389                                 break;
390                         case PROJECTILE_PORTO_BLUE:
391                                 this.colormod = '1 1 2';
392                                 this.alphamod = 0.5;
393                                 this.move_movetype = MOVETYPE_BOUNCE;
394                                 settouch(this, func_null);
395                                 break;
396                         case PROJECTILE_HAGAR_BOUNCING:
397                                 this.move_movetype = MOVETYPE_BOUNCE;
398                                 settouch(this, func_null);
399                                 break;
400                         case PROJECTILE_CRYLINK_BOUNCING:
401                                 this.move_movetype = MOVETYPE_BOUNCE;
402                                 settouch(this, func_null);
403                                 break;
404                         case PROJECTILE_FIREBALL:
405                                 loopsound(this, CH_SHOTS_SINGLE, SND(FIREBALL_FLY2), VOL_BASE, ATTEN_NORM);
406                                 this.mins = '-16 -16 -16';
407                                 this.maxs = '16 16 16';
408                                 break;
409                         case PROJECTILE_FIREMINE:
410                                 loopsound(this, CH_SHOTS_SINGLE, SND(FIREBALL_FLY), VOL_BASE, ATTEN_NORM);
411                                 this.move_movetype = MOVETYPE_BOUNCE;
412                                 settouch(this, func_null);
413                                 this.mins = '-4 -4 -4';
414                                 this.maxs = '4 4 4';
415                                 break;
416                         case PROJECTILE_TAG:
417                                 this.mins = '-2 -2 -2';
418                                 this.maxs = '2 2 2';
419                                 break;
420                         case PROJECTILE_FLAC:
421                                 this.mins = '-2 -2 -2';
422                                 this.maxs = '2 2 2';
423                                 break;
424                         case PROJECTILE_SEEKER:
425                                 loopsound(this, CH_SHOTS_SINGLE, SND(TAG_ROCKET_FLY), VOL_BASE, ATTEN_NORM);
426                                 this.mins = '-4 -4 -4';
427                                 this.maxs = '4 4 4';
428                                 break;
429                         case PROJECTILE_RAPTORBOMB:
430                                 this.mins = '-3 -3 -3';
431                                 this.maxs = '3 3 3';
432                                 break;
433                         case PROJECTILE_RAPTORBOMBLET:
434                                 break;
435                         case PROJECTILE_RAPTORCANNON:
436                                 break;
437                         case PROJECTILE_SPIDERROCKET:
438                                 loopsound(this, CH_SHOTS_SINGLE, SND(TAG_ROCKET_FLY), VOL_BASE, ATTEN_NORM);
439                                 break;
440                         case PROJECTILE_WAKIROCKET:
441                                 loopsound(this, CH_SHOTS_SINGLE, SND(TAG_ROCKET_FLY), VOL_BASE, ATTEN_NORM);
442                                 break;
443                         /*
444                         case PROJECTILE_WAKICANNON:
445                             break;
446                         case PROJECTILE_BUMBLE_GUN:
447                             // only new engines support sound moving with object
448                             loopsound(this, CH_SHOTS_SINGLE, SND(ELECTRO_FLY), VOL_BASE, ATTEN_NORM);
449                             this.mins = '0 0 -4';
450                             this.maxs = '0 0 -4';
451                             this.move_movetype = MOVETYPE_BOUNCE;
452                             this.move_touch = func_null;
453                             this.move_bounce_factor = WEP_CVAR_SEC(electro, bouncefactor);
454                             this.move_bounce_stopspeed = WEP_CVAR_SEC(electro, bouncestop);
455                             break;
456                         */
457                         default:
458                                 break;
459                 }
460
461                 MUTATOR_CALLHOOK(EditProjectile, this);
462
463                 setsize(this, this.mins, this.maxs);
464         }
465
466         return = true;
467
468         if (this.gravity)
469         {
470                 if (this.move_movetype == MOVETYPE_FLY)
471                         this.move_movetype = MOVETYPE_TOSS;
472                 if (this.move_movetype == MOVETYPE_BOUNCEMISSILE)
473                         this.move_movetype = MOVETYPE_BOUNCE;
474         }
475         else
476         {
477                 if (this.move_movetype == MOVETYPE_TOSS)
478                         this.move_movetype = MOVETYPE_FLY;
479                 if (this.move_movetype == MOVETYPE_BOUNCE)
480                         this.move_movetype = MOVETYPE_BOUNCEMISSILE;
481         }
482
483         if (!(this.count & 0x80))
484                 InterpolateOrigin_Note(this);
485
486         this.classname = "csqcprojectile";
487         this.draw = Projectile_Draw;
488         this.entremove = Ent_RemoveProjectile;
489 }
490
491 PRECACHE(Projectiles)
492 {
493         MUTATOR_CALLHOOK(PrecacheProjectiles);
494 }