]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/weapons/projectile.qc
Merged master into Lyberta/TeamplayFixes_.
[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/effects/effect.qh>
10 #include <common/effects/all.qh>
11 #include <common/net_linked.qh>
12 #include <common/physics/movetypes/movetypes.qh>
13
14 #include <lib/csqcmodel/interpolate.qh>
15
16 #include <lib/warpzone/anglestransform.qh>
17
18 .float alpha;
19 .float scale;
20 .vector colormod;
21
22 void SUB_Stop(entity this, entity toucher)
23 {
24         this.velocity = this.avelocity = '0 0 0';
25         set_movetype(this, MOVETYPE_NONE);
26 }
27
28 void Projectile_ResetTrail(entity this, vector to)
29 {
30         this.trail_oldorigin = to;
31         this.trail_oldtime = time;
32 }
33
34 void Projectile_DrawTrail(entity this, vector to)
35 {
36         vector from = this.trail_oldorigin;
37         // float t0 = this.trail_oldtime;
38         this.trail_oldorigin = to;
39         this.trail_oldtime = time;
40
41         // force the effect even for stationary firemine
42         if (this.cnt == PROJECTILE_FIREMINE)
43                 if (from == to)
44                         from.z += 1;
45
46         if (this.traileffect)
47         {
48                 particles_alphamin = particles_alphamax = particles_fade = sqrt(this.alpha);
49                 boxparticles(particleeffectnum(Effects_from(this.traileffect)), this, from, to, this.velocity, this.velocity, 1, PARTICLES_USEALPHA | PARTICLES_USEFADE | PARTICLES_DRAWASTRAIL);
50         }
51 }
52
53 bool Projectile_isnade(int proj); // TODO: remove
54
55 void Projectile_Draw(entity this)
56 {
57         vector rot;
58         vector trailorigin;
59         int f;
60         bool drawn;
61         float t;
62         float a;
63
64         f = this.flags;
65
66         if (this.count & 0x80)
67         {
68                 // UNSET_ONGROUND(this);
69                 if (this.move_movetype == MOVETYPE_NONE || this.move_movetype == MOVETYPE_FLY)
70                         Movetype_Physics_NoMatchServer(this);
71                 // the trivial movetypes do not have to match the
72                 // server's ticrate as they are ticrate independent
73                 // NOTE: this assumption is only true if MOVETYPE_FLY
74                 // projectiles detonate on impact. If they continue
75                 // moving, we might still be ticrate dependent.
76                 else
77                         Movetype_Physics_MatchServer(this, autocvar_cl_projectiles_sloppy);
78                 if (!IS_ONGROUND(this))
79                         if (this.velocity != '0 0 0')
80                                 this.angles = vectoangles(this.velocity);
81         }
82         else
83         {
84                 InterpolateOrigin_Do(this);
85         }
86
87         if (this.count & 0x80)
88         {
89                 drawn = (time >= this.spawntime - 0.02);
90                 t = max(time, this.spawntime);
91         }
92         else
93         {
94                 drawn = (this.iflags & IFLAG_VALID);
95                 t = time;
96         }
97
98         if (!(f & FL_ONGROUND))
99         {
100                 rot = '0 0 0';
101                 switch (this.cnt)
102                 {
103                         /*
104                         case PROJECTILE_GRENADE:
105                             rot = '-2000 0 0'; // forward
106                             break;
107                         */
108                         case PROJECTILE_GRENADE_BOUNCING:
109                                 rot = '0 -1000 0'; // sideways
110                                 break;
111                         case PROJECTILE_HOOKBOMB:
112                                 rot = '1000 0 0';  // forward
113                                 break;
114                         case PROJECTILE_ROCKET:
115                                 rot = '0 0 720'; // spinning
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, Sound 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                 SET_ONGROUND(this);
219         else
220                 UNSET_ONGROUND(this);
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                 }
253
254                 if (time == this.spawntime || (this.count & 0x80) || (f & 0x08))
255                 {
256                         this.trail_oldorigin = this.origin;
257                         if (!(this.count & 0x80))
258                                 InterpolateOrigin_Reset(this);
259                 }
260
261                 if (f & 0x20)
262                 {
263                         this.fade_time = time + ReadByte() * ticrate;
264                         this.fade_rate = 1 / (ReadByte() * ticrate);
265                 }
266                 else
267                 {
268                         this.fade_time = 0;
269                         this.fade_rate = 0;
270                 }
271
272                 int myteam = ReadByte();
273                 this.team = myteam - 1;
274
275                 if(teamplay)
276                 {
277                         if(myteam)
278                                 this.colormap = (this.team) * 0x11; // note: team - 1 on server (client uses different numbers)
279                         else
280                                 this.colormap = 0x00;
281                         this.colormap |= BIT(10); // RENDER_COLORMAPPED
282                 }
283                 else
284                         this.colormap = myteam;
285                 // TODO: projectiles use glowmaps for their color, not teams
286                 #if 0
287                 if(this.colormap > 0)
288                         this.glowmod = colormapPaletteColor(this.colormap & 0x0F, true) * 2;
289                 else
290                         this.glowmod = '1 1 1';
291                 #endif
292         }
293
294         if (f & 2)
295         {
296                 this.cnt = ReadByte();
297
298                 this.silent = (this.cnt & 0x80);
299                 this.cnt = (this.cnt & 0x7F);
300
301                 this.scale = 1;
302                 this.traileffect = 0;
303                 switch (this.cnt)
304                 {
305 #define HANDLE(id) case PROJECTILE_##id: setmodel(this, MDL_PROJECTILE_##id);
306                         HANDLE(ELECTRO)            this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
307                         HANDLE(ROCKET)             this.traileffect = EFFECT_TR_ROCKET.m_id; this.scale = 2; break;
308                         HANDLE(CRYLINK)            this.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
309                         HANDLE(CRYLINK_BOUNCING)   this.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
310                         HANDLE(ELECTRO_BEAM)       this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
311                         HANDLE(GRENADE)            this.traileffect = EFFECT_TR_GRENADE.m_id; break;
312                         HANDLE(GRENADE_BOUNCING)   this.traileffect = EFFECT_TR_GRENADE.m_id; break;
313                         HANDLE(MINE)               this.traileffect = EFFECT_TR_GRENADE.m_id; break;
314                         HANDLE(BLASTER)            this.traileffect = EFFECT_Null.m_id; break;
315                         HANDLE(ARC_BOLT)           this.traileffect = EFFECT_Null.m_id; break;
316                         HANDLE(HLAC)               this.traileffect = EFFECT_Null.m_id; break;
317                         HANDLE(PORTO_RED)          this.traileffect = EFFECT_TR_WIZSPIKE.m_id; this.scale = 4; break;
318                         HANDLE(PORTO_BLUE)         this.traileffect = EFFECT_TR_WIZSPIKE.m_id; this.scale = 4; break;
319                         HANDLE(HOOKBOMB)           this.traileffect = EFFECT_TR_KNIGHTSPIKE.m_id; break;
320                         HANDLE(HAGAR)              this.traileffect = EFFECT_HAGAR_ROCKET.m_id; this.scale = 0.75; break;
321                         HANDLE(HAGAR_BOUNCING)     this.traileffect = EFFECT_HAGAR_ROCKET.m_id; this.scale = 0.75; break;
322                         HANDLE(FIREBALL)           this.modelindex = 0; this.traileffect = EFFECT_FIREBALL.m_id; break; // particle effect is good enough
323                         HANDLE(FIREMINE)           this.modelindex = 0; this.traileffect = EFFECT_FIREMINE.m_id; break; // particle effect is good enough
324                         HANDLE(TAG)                this.traileffect = EFFECT_TR_ROCKET.m_id; break;
325                         HANDLE(FLAC)               this.scale = 0.4; this.traileffect = EFFECT_FLAC_TRAIL.m_id; break;
326                         HANDLE(SEEKER)             this.traileffect = EFFECT_SEEKER_TRAIL.m_id; break;
327
328                         HANDLE(MAGE_SPIKE)         this.traileffect = EFFECT_TR_VORESPIKE.m_id; break;
329                         HANDLE(SHAMBLER_LIGHTNING) this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
330
331                         HANDLE(RAPTORBOMB)         this.gravity = 1; this.avelocity = '0 0 180'; this.traileffect = EFFECT_Null.m_id; break;
332                         HANDLE(RAPTORBOMBLET)      this.gravity = 1; this.avelocity = '0 0 180'; this.traileffect = EFFECT_Null.m_id; break;
333                         HANDLE(RAPTORCANNON)       this.traileffect = EFFECT_TR_CRYLINKPLASMA.m_id; break;
334
335                         HANDLE(SPIDERROCKET)       this.traileffect = EFFECT_SPIDERBOT_ROCKET_TRAIL.m_id; break;
336                         HANDLE(WAKIROCKET)         this.traileffect = EFFECT_RACER_ROCKET_TRAIL.m_id; break;
337                         HANDLE(WAKICANNON)         this.traileffect = EFFECT_Null.m_id; break;
338
339                         HANDLE(BUMBLE_GUN)         this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
340                         HANDLE(BUMBLE_BEAM)        this.traileffect = EFFECT_TR_NEXUIZPLASMA.m_id; break;
341
342                         HANDLE(RPC)                this.traileffect = EFFECT_TR_ROCKET.m_id; break;
343
344                         HANDLE(ROCKETMINSTA_LASER) this.traileffect = EFFECT_ROCKETMINSTA_LASER(this.team).m_id; break;
345 #undef HANDLE
346                         default:
347                                 if (MUTATOR_CALLHOOK(Ent_Projectile, this))
348                                         break;
349
350                                 error("Received invalid CSQC projectile, can't work with this!");
351                                 break;
352                 }
353
354                 this.mins = '0 0 0';
355                 this.maxs = '0 0 0';
356                 this.colormod = '0 0 0';
357                 settouch(this, SUB_Stop);
358                 set_movetype(this, MOVETYPE_TOSS);
359                 this.alphamod = 1;
360
361                 switch (this.cnt)
362                 {
363                         case PROJECTILE_ELECTRO:
364                                 // only new engines support sound moving with object
365                                 loopsound(this, CH_SHOTS_SINGLE, SND_ELECTRO_FLY, VOL_BASE, ATTEN_NORM);
366                                 this.mins = '-4 -4 -4';
367                                 this.maxs = '4 4 4';
368                                 set_movetype(this, MOVETYPE_BOUNCE);
369                                 settouch(this, func_null);
370                                 this.bouncefactor = WEP_CVAR_SEC(electro, bouncefactor);
371                                 this.bouncestop = WEP_CVAR_SEC(electro, bouncestop);
372                                 break;
373                         case PROJECTILE_RPC:
374                         case PROJECTILE_ROCKET:
375                                 loopsound(this, CH_SHOTS_SINGLE, SND_ROCKET_FLY, VOL_BASE, ATTEN_NORM);
376                                 this.mins = '-3 -3 -3';
377                                 this.maxs = '3 3 3';
378                                 break;
379                         case PROJECTILE_GRENADE:
380                                 this.mins = '-3 -3 -3';
381                                 this.maxs = '3 3 3';
382                                 break;
383                         case PROJECTILE_GRENADE_BOUNCING:
384                                 this.mins = '-3 -3 -3';
385                                 this.maxs = '3 3 3';
386                                 set_movetype(this, MOVETYPE_BOUNCE);
387                                 settouch(this, func_null);
388                                 this.bouncefactor = WEP_CVAR(mortar, bouncefactor);
389                                 this.bouncestop = WEP_CVAR(mortar, bouncestop);
390                                 break;
391                         case PROJECTILE_SHAMBLER_LIGHTNING:
392                                 this.mins = '-8 -8 -8';
393                                 this.maxs = '8 8 8';
394                                 this.scale = 2.5;
395                                 this.avelocity = randomvec() * 720;
396                                 break;
397                         case PROJECTILE_MINE:
398                                 this.mins = '-4 -4 -4';
399                                 this.maxs = '4 4 4';
400                                 break;
401                         case PROJECTILE_PORTO_RED:
402                                 this.colormod = '2 1 1';
403                                 this.alphamod = 0.5;
404                                 set_movetype(this, MOVETYPE_BOUNCE);
405                                 settouch(this, func_null);
406                                 break;
407                         case PROJECTILE_PORTO_BLUE:
408                                 this.colormod = '1 1 2';
409                                 this.alphamod = 0.5;
410                                 set_movetype(this, MOVETYPE_BOUNCE);
411                                 settouch(this, func_null);
412                                 break;
413                         case PROJECTILE_HAGAR_BOUNCING:
414                                 set_movetype(this, MOVETYPE_BOUNCE);
415                                 settouch(this, func_null);
416                                 break;
417                         case PROJECTILE_CRYLINK_BOUNCING:
418                                 set_movetype(this, MOVETYPE_BOUNCE);
419                                 settouch(this, func_null);
420                                 break;
421                         case PROJECTILE_FIREBALL:
422                                 loopsound(this, CH_SHOTS_SINGLE, SND_FIREBALL_FLY2, VOL_BASE, ATTEN_NORM);
423                                 this.mins = '-16 -16 -16';
424                                 this.maxs = '16 16 16';
425                                 break;
426                         case PROJECTILE_FIREMINE:
427                                 loopsound(this, CH_SHOTS_SINGLE, SND_FIREBALL_FLY, VOL_BASE, ATTEN_NORM);
428                                 set_movetype(this, MOVETYPE_BOUNCE);
429                                 settouch(this, func_null);
430                                 this.mins = '-4 -4 -4';
431                                 this.maxs = '4 4 4';
432                                 break;
433                         case PROJECTILE_TAG:
434                                 this.mins = '-2 -2 -2';
435                                 this.maxs = '2 2 2';
436                                 break;
437                         case PROJECTILE_FLAC:
438                                 this.mins = '-2 -2 -2';
439                                 this.maxs = '2 2 2';
440                                 break;
441                         case PROJECTILE_SEEKER:
442                                 loopsound(this, CH_SHOTS_SINGLE, SND_TAG_ROCKET_FLY, VOL_BASE, ATTEN_NORM);
443                                 this.mins = '-4 -4 -4';
444                                 this.maxs = '4 4 4';
445                                 break;
446                         case PROJECTILE_RAPTORBOMB:
447                                 this.mins = '-3 -3 -3';
448                                 this.maxs = '3 3 3';
449                                 break;
450                         case PROJECTILE_RAPTORBOMBLET:
451                                 break;
452                         case PROJECTILE_RAPTORCANNON:
453                                 break;
454                         case PROJECTILE_SPIDERROCKET:
455                                 loopsound(this, CH_SHOTS_SINGLE, SND_TAG_ROCKET_FLY, VOL_BASE, ATTEN_NORM);
456                                 break;
457                         case PROJECTILE_WAKIROCKET:
458                                 loopsound(this, CH_SHOTS_SINGLE, SND_TAG_ROCKET_FLY, VOL_BASE, ATTEN_NORM);
459                                 break;
460                         /*
461                         case PROJECTILE_WAKICANNON:
462                             break;
463                         case PROJECTILE_BUMBLE_GUN:
464                             // only new engines support sound moving with object
465                             loopsound(this, CH_SHOTS_SINGLE, SND_ELECTRO_FLY, VOL_BASE, ATTEN_NORM);
466                             this.mins = '0 0 -4';
467                             this.maxs = '0 0 -4';
468                             this.move_movetype = MOVETYPE_BOUNCE;
469                             settouch(this, func_null);
470                             this.bouncefactor = WEP_CVAR_SEC(electro, bouncefactor);
471                             this.bouncestop = WEP_CVAR_SEC(electro, bouncestop);
472                             break;
473                         */
474                         default:
475                                 break;
476                 }
477
478                 MUTATOR_CALLHOOK(EditProjectile, this);
479
480                 setsize(this, this.mins, this.maxs);
481         }
482
483         return = true;
484
485         if (this.gravity)
486         {
487                 if (this.move_movetype == MOVETYPE_FLY)
488                         set_movetype(this, MOVETYPE_TOSS);
489                 if (this.move_movetype == MOVETYPE_BOUNCEMISSILE)
490                         set_movetype(this, MOVETYPE_BOUNCE);
491         }
492         else
493         {
494                 if (this.move_movetype == MOVETYPE_TOSS)
495                         set_movetype(this, MOVETYPE_FLY);
496                 if (this.move_movetype == MOVETYPE_BOUNCE)
497                         set_movetype(this, MOVETYPE_BOUNCEMISSILE);
498         }
499
500         if (!(this.count & 0x80))
501                 InterpolateOrigin_Note(this);
502
503         this.classname = "csqcprojectile";
504         this.draw = Projectile_Draw;
505         if (isnew) IL_PUSH(g_drawables, this);
506         this.entremove = Ent_RemoveProjectile;
507 }
508
509 PRECACHE(Projectiles)
510 {
511         MUTATOR_CALLHOOK(PrecacheProjectiles);
512 }