DP_CSQC_BOXPARTICLES use :P for fading out particle trails
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / projectile.qc
1 .vector iorigin1, iorigin2;
2 .float spawntime;
3 .vector trail_oldorigin;
4 .float trail_oldtime;
5 .float fade_time, fade_rate;
6
7 void SUB_Null()
8 {
9 }
10
11 void SUB_Stop()
12 {
13         self.move_velocity = self.move_avelocity = '0 0 0';
14         self.move_movetype = MOVETYPE_NONE;
15 }
16
17 .float alphamod;
18 .float count; // set if clientside projectile
19 .float cnt; // sound index
20 .float gravity;
21 .float snd_looping;
22 .float silent;
23 .float traileffect;
24
25 void Projectile_DrawTrail(vector to)
26 {
27         vector from;
28         float t0;
29         from = self.trail_oldorigin;
30         t0 = self.trail_oldtime;
31         self.trail_oldorigin = to;
32         self.trail_oldtime = time;
33
34         // force the effect even for stationary firemine
35         if(self.cnt == PROJECTILE_FIREMINE)
36                 if(from == to)
37                         from_z += 1;
38
39         if (self.traileffect)
40         {
41                 if(checkextension("DP_CSQC_BOXPARTICLES"))
42                 {
43                         /* looks good, but we can do better with particle count
44                         particles_alphamin = particles_alphamax = self.alpha;
45                         boxparticles(self.traileffect, self, from, to, self.velocity, self.velocity, 1, PARTICLES_USEALPHA);
46                         */
47                         /* looks bad
48                         boxparticles(self.traileffect, self, from, to, self.velocity, self.velocity, self.alpha, 0);
49                         */
50                         particles_alphamin = particles_alphamax = sqrt(self.alpha);
51                         boxparticles(self.traileffect, self, from, to, self.velocity, self.velocity, sqrt(self.alpha), PARTICLES_USEALPHA);
52                 }
53                 else
54                         trailparticles(self, self.traileffect, from, to);
55         }
56 }
57
58 void Projectile_Draw()
59 {
60         vector rot;
61         vector trailorigin;
62         float f;
63         float drawn;
64         float t;
65         float a;
66
67         f = self.move_flags;
68
69         if(self.count & 0x80)
70         {
71                 //self.move_flags &~= FL_ONGROUND;
72                 Movetype_Physics_MatchServer(autocvar_cl_projectiles_sloppy);
73                 if(!(self.move_flags & FL_ONGROUND))
74                         self.angles = vectoangles(self.velocity);
75         }
76         else
77         {
78                 InterpolateOrigin_Do();
79         }
80
81         if(self.count & 0x80)
82         {
83                 drawn = (time >= self.spawntime - 0.02);
84                 t = max(time, self.spawntime);
85         }
86         else
87         {
88                 drawn = (self.iflags & IFLAG_VALID);
89                 t = time;
90         }
91
92         if(!(f & FL_ONGROUND))
93         {
94                 rot = '0 0 0';
95                 switch(self.cnt)
96                 {
97                         /*
98                         case PROJECTILE_GRENADE:
99                                 rot = '-2000 0 0'; // forward
100                                 break;
101                         */
102                         case PROJECTILE_GRENADE_BOUNCING:
103                                 rot = '0 -1000 0'; // sideways
104                                 break;
105                         case PROJECTILE_HOOKBOMB:
106                                 rot = '1000 0 0'; // forward
107                                 break;
108                         default:
109                                 break;
110                 }
111                 self.angles = AnglesTransform_ToAngles(AnglesTransform_Multiply(AnglesTransform_FromAngles(self.angles), rot * (t - self.spawntime)));
112         }
113
114         vector ang;
115         ang = self.angles;
116         ang_x = -ang_x;
117         makevectors(ang);
118
119         a = 1 - (time - self.fade_time) * self.fade_rate;
120         self.alpha = bound(0, self.alphamod * a, 1);
121         if(self.alpha <= 0)
122                 drawn = 0;
123         self.renderflags = 0;
124
125         trailorigin = self.origin;
126         switch(self.cnt)
127         {
128                 case PROJECTILE_GRENADE:
129                 case PROJECTILE_GRENADE_BOUNCING:
130                         trailorigin += v_right * 1 + v_forward * -10;
131                         break;
132                 default:
133                         break;
134         }
135         if(drawn)
136                 Projectile_DrawTrail(trailorigin);
137         else
138         {
139                 self.trail_oldorigin = trailorigin;
140                 self.trail_oldtime = time;
141         }
142
143         if(!drawn)
144                 return;
145
146         switch(self.cnt)
147         {
148                 case PROJECTILE_BULLET_GLOWING:
149                 case PROJECTILE_BULLET_GLOWING_TRACER:
150                         R_AddDynamicLight(self.origin, 50 * a, '1 1 0');
151                         break;
152                 default:
153                         break;
154         }
155
156         R_AddEntity(self);
157 }
158
159 void loopsound(entity e, float ch, string samp, float vol, float attn)
160 {
161         if(self.silent)
162                 return;
163
164         sound(e, ch, samp, vol, attn);
165         e.snd_looping = 1;
166 }
167
168 void Ent_RemoveProjectile()
169 {
170         if(self.snd_looping)
171                 sound(self, CHAN_PROJECTILE, "misc/null.wav", VOL_BASE, ATTN_NORM);
172
173         if(self.count & 0x80)
174         {
175                 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.05, MOVE_NORMAL, self);
176                 Projectile_DrawTrail(trace_endpos);
177         }
178 }
179
180 void Ent_Projectile()
181 {
182         float f;
183
184         // projectile properties:
185         //   kind (interpolated, or clientside)
186         //
187         //   modelindex
188         //   origin
189         //   scale
190         //   if clientside:
191         //     velocity
192         //     gravity
193         //   soundindex (hardcoded list)
194         //   effects
195         //
196         // projectiles don't send angles, because they always follow the velocity
197         
198         f = ReadByte();
199         self.count = (f & 0x80);
200         self.iflags = (self.iflags & IFLAG_INTERNALMASK) | IFLAG_AUTOANGLES | IFLAG_ANGLES;
201         self.solid = SOLID_TRIGGER;
202         //self.effects = EF_NOMODELFLAGS;
203
204         // this should make collisions with bmodels more exact, but it leads to
205         // projectiles no longer being able to lie on a bmodel
206         self.move_nomonsters = MOVE_WORLDONLY;
207         if(f & 0x40)
208                 self.move_flags |= FL_ONGROUND;
209         else
210                 self.move_flags &~= FL_ONGROUND;
211
212         if(!self.move_time)
213         {
214                 // for some unknown reason, we don't need to care for
215                 // sv_gameplayfix_delayprojectiles here.
216                 self.move_time = time;
217                 self.spawntime = time;
218         }
219         else
220                 self.move_time = max(self.move_time, time);
221
222         if(!(self.count & 0x80))
223                 InterpolateOrigin_Undo();
224
225         if(f & 1)
226         {
227                 self.origin_x = ReadCoord();
228                 self.origin_y = ReadCoord();
229                 self.origin_z = ReadCoord();
230                 if(self.count & 0x80)
231                 {
232                         self.velocity_x = ReadCoord();
233                         self.velocity_y = ReadCoord();
234                         self.velocity_z = ReadCoord();
235                         if(f & 0x10)
236                                 self.gravity = ReadCoord();
237                         else
238                                 self.gravity = 0; // none
239                         self.move_origin = self.origin;
240                         self.move_velocity = self.velocity;
241                 }
242
243                 if(time == self.spawntime || (self.count & 0x80) || (f & 0x08))
244                 {
245                         self.trail_oldorigin = self.origin;
246                         if(!(self.count & 0x80))
247                                 InterpolateOrigin_Reset();
248                 }
249
250                 if(f & 0x20)
251                 {
252                         self.fade_time = time + ReadByte() * ticrate;
253                         self.fade_rate = 1 / (ReadByte() * ticrate);
254                 }
255                 else
256                 {
257                         self.fade_time = 0;
258                         self.fade_rate = 0;
259                 }
260         }
261
262         if(f & 2)
263         {
264                 self.cnt = ReadByte();
265
266                 self.silent = (self.cnt & 0x80);
267                 self.cnt = (self.cnt & 0x7F);
268
269                 self.scale = 1;
270                 self.traileffect = 0;
271                 switch(self.cnt)
272                 {
273                         case PROJECTILE_ELECTRO: setmodel(self, "models/ebomb.mdl");self.traileffect = particleeffectnum("TR_NEXUIZPLASMA"); break;
274                         case PROJECTILE_ROCKET: setmodel(self, "models/rocket.md3");self.traileffect = particleeffectnum("TR_ROCKET"); self.scale = 2; break;
275                         case PROJECTILE_BULLET: setmodel(self, "models/tracer.mdl");self.traileffect = particleeffectnum("tr_bullet"); break;
276                         case PROJECTILE_BULLET_GLOWING: setmodel(self, "models/tracer.mdl");self.traileffect = particleeffectnum("tr_bullet"); break;
277                         case PROJECTILE_BULLET_GLOWING_TRACER: setmodel(self, "models/tracer.mdl");self.traileffect = particleeffectnum("tr_rifle"); break;
278                         case PROJECTILE_CRYLINK: setmodel(self, "models/plasmatrail.mdl");self.traileffect = particleeffectnum("TR_CRYLINKPLASMA"); break;
279                         case PROJECTILE_CRYLINK_BOUNCING: setmodel(self, "models/plasmatrail.mdl");self.traileffect = particleeffectnum("TR_CRYLINKPLASMA"); break;
280                         case PROJECTILE_ELECTRO_BEAM: setmodel(self, "models/elaser.mdl");self.traileffect = particleeffectnum("TR_NEXUIZPLASMA"); break;
281                         case PROJECTILE_GRENADE: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_GRENADE"); break;
282                         case PROJECTILE_GRENADE_BOUNCING: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_GRENADE"); break;
283                         case PROJECTILE_LASER: setmodel(self, "models/laser.mdl");self.traileffect = particleeffectnum(""); break;
284                         case PROJECTILE_HLAC: setmodel(self, "models/hlac_bullet.md3");self.traileffect = particleeffectnum(""); break;
285                         case PROJECTILE_PORTO_RED: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_WIZSPIKE"); self.scale = 4; break;
286                         case PROJECTILE_PORTO_BLUE: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_WIZSPIKE"); self.scale = 4; break;
287                         case PROJECTILE_HOOKBOMB: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_KNIGHTSPIKE"); break;
288                         case PROJECTILE_HAGAR: setmodel(self, "models/hagarmissile.mdl");self.traileffect = particleeffectnum("TR_GRENADE"); self.scale = 0.4; break;
289                         case PROJECTILE_HAGAR_BOUNCING: setmodel(self, "models/hagarmissile.mdl");self.traileffect = particleeffectnum("TR_GRENADE"); self.scale = 0.4; break;
290                         case PROJECTILE_FIREBALL: self.model = ""; self.modelindex = 0; self.traileffect = particleeffectnum("fireball"); break; // particle effect is good enough
291                         case PROJECTILE_FIREMINE: self.model = ""; self.modelindex = 0; self.traileffect = particleeffectnum("firemine"); break; // particle effect is good enough
292                         case PROJECTILE_TAG: setmodel(self, "models/laser.mdl"); self.traileffect = particleeffectnum("TR_ROCKET"); break;
293                         case PROJECTILE_FLAC: setmodel(self, "models/hagarmissile.mdl"); self.scale = 0.4; self.traileffect = particleeffectnum("TR_ROCKET"); break;
294                         case PROJECTILE_SEEKER: setmodel(self, "models/tagrocket.md3"); self.scale = 2; self.traileffect = particleeffectnum("TR_ROCKET"); break;
295                         default:
296                                 error("Received invalid CSQC projectile, can't work with this!");
297                                 break;
298                 }
299
300                 self.mins = '0 0 0';
301                 self.maxs = '0 0 0';
302                 self.colormod = '0 0 0';
303                 self.move_touch = SUB_Stop;
304                 self.move_movetype = MOVETYPE_TOSS;
305                 self.alphamod = 1;
306
307                 switch(self.cnt)
308                 {
309                         case PROJECTILE_ELECTRO:
310                                 // only new engines support sound moving with object
311                                 loopsound(self, CHAN_PROJECTILE, "weapons/electro_fly.wav", VOL_BASE, ATTN_NORM);
312                                 self.mins = '0 0 -3';
313                                 self.maxs = '0 0 -3';
314                                 self.move_movetype = MOVETYPE_BOUNCE;
315                                 self.move_touch = SUB_Null;
316                                 break;
317                         case PROJECTILE_ROCKET:
318                                 loopsound(self, CHAN_PROJECTILE, "weapons/rocket_fly.wav", VOL_BASE, ATTN_NORM);
319                                 self.mins = '-3 -3 -3';
320                                 self.maxs = '3 3 3';
321                                 break;
322                         case PROJECTILE_GRENADE:
323                                 self.mins = '0 0 -3';
324                                 self.maxs = '0 0 -3';
325                                 break;
326                         case PROJECTILE_GRENADE_BOUNCING:
327                                 self.mins = '0 0 -3';
328                                 self.maxs = '0 0 -3';
329                                 self.move_movetype = MOVETYPE_BOUNCE;
330                                 self.move_touch = SUB_Null;
331                                 self.move_bounce_factor = g_balance_grenadelauncher_secondary_bouncefactor;
332                                 self.move_bounce_stopspeed = g_balance_grenadelauncher_secondary_bouncestop;
333                                 break;
334                         case PROJECTILE_PORTO_RED:
335                                 self.colormod = '2 1 1';
336                                 self.alphamod = 0.5;
337                                 self.move_movetype = MOVETYPE_BOUNCE;
338                                 self.move_touch = SUB_Null;
339                                 break;
340                         case PROJECTILE_PORTO_BLUE:
341                                 self.colormod = '1 1 2';
342                                 self.alphamod = 0.5;
343                                 self.move_movetype = MOVETYPE_BOUNCE;
344                                 self.move_touch = SUB_Null;
345                                 break;
346                         case PROJECTILE_HAGAR_BOUNCING:
347                                 self.move_movetype = MOVETYPE_BOUNCE;
348                                 self.move_touch = SUB_Null;
349                                 break;
350                         case PROJECTILE_CRYLINK_BOUNCING:
351                                 self.move_movetype = MOVETYPE_BOUNCE;
352                                 self.move_touch = SUB_Null;
353                                 break;
354                         case PROJECTILE_FIREBALL:
355                                 loopsound(self, CHAN_PROJECTILE, "weapons/fireball_fly2.wav", VOL_BASE, ATTN_NORM);
356                                 self.mins = '-16 -16 -16';
357                                 self.maxs = '16 16 16';
358                                 break;
359                         case PROJECTILE_FIREMINE:
360                                 loopsound(self, CHAN_PROJECTILE, "weapons/fireball_fly.wav", VOL_BASE, ATTN_NORM);
361                                 self.move_movetype = MOVETYPE_BOUNCE;
362                                 self.move_touch = SUB_Null;
363                                 self.mins = '-4 -4 -4';
364                                 self.maxs = '4 4 4';
365                                 break;
366                         case PROJECTILE_TAG:
367                                 loopsound(self, CHAN_PROJECTILE, "weapons/tag_rocket_fly.wav", VOL_BASE, ATTN_NORM);
368                                 self.mins = '-2 -2 -2';
369                                 self.maxs = '2 2 2';
370                                 break;
371                         case PROJECTILE_FLAC:
372                                 self.mins = '-2 -2 -2';
373                                 self.maxs = '2 2 2';
374                                 break;
375                         case PROJECTILE_SEEKER:
376                                 self.mins = '-4 -4 -4';
377                                 self.maxs = '4 4 4';
378                                 break;
379                         default:
380                                 break;
381                 }
382         }
383
384         if(self.gravity)
385         {
386                 if(self.move_movetype == MOVETYPE_FLY)
387                         self.move_movetype = MOVETYPE_TOSS;
388                 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
389                         self.move_movetype = MOVETYPE_BOUNCE;
390         }
391         else
392         {
393                 if(self.move_movetype == MOVETYPE_TOSS)
394                         self.move_movetype = MOVETYPE_FLY;
395                 if(self.move_movetype == MOVETYPE_BOUNCE)
396                         self.move_movetype = MOVETYPE_BOUNCEMISSILE;
397         }
398
399         if(!(self.count & 0x80))
400                 InterpolateOrigin_Note();
401         
402         self.draw = Projectile_Draw;
403         self.entremove = Ent_RemoveProjectile;
404 }
405
406 void Projectile_Precache()
407 {
408         precache_model("models/ebomb.mdl");
409         precache_model("models/elaser.mdl");
410         precache_model("models/grenademodel.md3");
411         precache_model("models/hagarmissile.mdl");
412         precache_model("models/hlac_bullet.md3");
413         precache_model("models/laser.mdl");
414         precache_model("models/plasmatrail.mdl");
415         precache_model("models/rocket.md3");
416         precache_model("models/tagrocket.md3");
417         precache_model("models/tracer.mdl");
418         precache_sound("weapons/electro_fly.wav");
419         precache_sound("weapons/rocket_fly.wav");
420         precache_sound("weapons/fireball_fly.wav");
421         precache_sound("weapons/fireball_fly2.wav");
422         precache_sound("weapons/tag_rocket_fly.wav");
423 }