Merge remote branch 'remotes/origin/fruitiex/newpanelhud' into terencehill/newpanelhud
[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                 if(self.move_movetype == MOVETYPE_NONE || self.move_movetype == MOVETYPE_FLY)
73                         Movetype_Physics_NoMatchServer();
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(autocvar_cl_projectiles_sloppy);
81                 if(!(self.move_flags & FL_ONGROUND))
82                         self.angles = vectoangles(self.velocity);
83         }
84         else
85         {
86                 InterpolateOrigin_Do();
87         }
88
89         if(self.count & 0x80)
90         {
91                 drawn = (time >= self.spawntime - 0.02);
92                 t = max(time, self.spawntime);
93         }
94         else
95         {
96                 drawn = (self.iflags & IFLAG_VALID);
97                 t = time;
98         }
99
100         if(!(f & FL_ONGROUND))
101         {
102                 rot = '0 0 0';
103                 switch(self.cnt)
104                 {
105                         /*
106                         case PROJECTILE_GRENADE:
107                                 rot = '-2000 0 0'; // forward
108                                 break;
109                         */
110                         case PROJECTILE_GRENADE_BOUNCING:
111                                 rot = '0 -1000 0'; // sideways
112                                 break;
113                         case PROJECTILE_HOOKBOMB:
114                                 rot = '1000 0 0'; // forward
115                                 break;
116                         default:
117                                 break;
118                 }
119                 self.angles = AnglesTransform_ToAngles(AnglesTransform_Multiply(AnglesTransform_FromAngles(self.angles), rot * (t - self.spawntime)));
120         }
121
122         vector ang;
123         ang = self.angles;
124         ang_x = -ang_x;
125         makevectors(ang);
126
127         a = 1 - (time - self.fade_time) * self.fade_rate;
128         self.alpha = bound(0, self.alphamod * a, 1);
129         if(self.alpha <= 0)
130                 drawn = 0;
131         self.renderflags = 0;
132
133         trailorigin = self.origin;
134         switch(self.cnt)
135         {
136                 case PROJECTILE_GRENADE:
137                 case PROJECTILE_GRENADE_BOUNCING:
138                         trailorigin += v_right * 1 + v_forward * -10;
139                         break;
140                 default:
141                         break;
142         }
143         if(drawn)
144                 Projectile_DrawTrail(trailorigin);
145         else
146         {
147                 self.trail_oldorigin = trailorigin;
148                 self.trail_oldtime = time;
149         }
150
151         if(!drawn)
152                 return;
153
154         switch(self.cnt)
155         {
156                 case PROJECTILE_BULLET_GLOWING:
157                 case PROJECTILE_BULLET_GLOWING_TRACER:
158                         R_AddDynamicLight(self.origin, 50 * a, '1 1 0');
159                         break;
160                 default:
161                         break;
162         }
163
164         R_AddEntity(self);
165 }
166
167 void loopsound(entity e, float ch, string samp, float vol, float attn)
168 {
169         if(self.silent)
170                 return;
171
172         sound(e, ch, samp, vol, attn);
173         e.snd_looping = 1;
174 }
175
176 void Ent_RemoveProjectile()
177 {
178         if(self.snd_looping)
179                 sound(self, CHAN_PROJECTILE, "misc/null.wav", VOL_BASE, ATTN_NORM);
180
181         if(self.count & 0x80)
182         {
183                 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.05, MOVE_NORMAL, self);
184                 Projectile_DrawTrail(trace_endpos);
185         }
186 }
187
188 void Ent_Projectile()
189 {
190         float f;
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         f = ReadByte();
207         self.count = (f & 0x80);
208         self.iflags = (self.iflags & IFLAG_INTERNALMASK) | IFLAG_AUTOANGLES | IFLAG_ANGLES;
209         self.solid = SOLID_TRIGGER;
210         //self.effects = EF_NOMODELFLAGS;
211
212         // this should make collisions with bmodels more exact, but it leads to
213         // projectiles no longer being able to lie on a bmodel
214         self.move_nomonsters = MOVE_WORLDONLY;
215         if(f & 0x40)
216                 self.move_flags |= FL_ONGROUND;
217         else
218                 self.move_flags &~= FL_ONGROUND;
219
220         if(!self.move_time)
221         {
222                 // for some unknown reason, we don't need to care for
223                 // sv_gameplayfix_delayprojectiles here.
224                 self.move_time = time;
225                 self.spawntime = time;
226         }
227         else
228                 self.move_time = max(self.move_time, time);
229
230         if(!(self.count & 0x80))
231                 InterpolateOrigin_Undo();
232
233         if(f & 1)
234         {
235                 self.origin_x = ReadCoord();
236                 self.origin_y = ReadCoord();
237                 self.origin_z = ReadCoord();
238                 if(self.count & 0x80)
239                 {
240                         self.velocity_x = ReadCoord();
241                         self.velocity_y = ReadCoord();
242                         self.velocity_z = ReadCoord();
243                         if(f & 0x10)
244                                 self.gravity = ReadCoord();
245                         else
246                                 self.gravity = 0; // none
247                         self.move_origin = self.origin;
248                         self.move_velocity = self.velocity;
249                 }
250
251                 if(time == self.spawntime || (self.count & 0x80) || (f & 0x08))
252                 {
253                         self.trail_oldorigin = self.origin;
254                         if(!(self.count & 0x80))
255                                 InterpolateOrigin_Reset();
256                 }
257
258                 if(f & 0x20)
259                 {
260                         self.fade_time = time + ReadByte() * ticrate;
261                         self.fade_rate = 1 / (ReadByte() * ticrate);
262                 }
263                 else
264                 {
265                         self.fade_time = 0;
266                         self.fade_rate = 0;
267                 }
268         }
269
270         if(f & 2)
271         {
272                 self.cnt = ReadByte();
273
274                 self.silent = (self.cnt & 0x80);
275                 self.cnt = (self.cnt & 0x7F);
276
277                 self.scale = 1;
278                 self.traileffect = 0;
279                 switch(self.cnt)
280                 {
281                         case PROJECTILE_ELECTRO: setmodel(self, "models/ebomb.mdl");self.traileffect = particleeffectnum("TR_NEXUIZPLASMA"); break;
282                         case PROJECTILE_ROCKET: setmodel(self, "models/rocket.md3");self.traileffect = particleeffectnum("TR_ROCKET"); self.scale = 2; break;
283                         case PROJECTILE_BULLET: setmodel(self, "models/tracer.mdl");self.traileffect = particleeffectnum("tr_bullet"); break;
284                         case PROJECTILE_BULLET_GLOWING: setmodel(self, "models/tracer.mdl");self.traileffect = particleeffectnum("tr_bullet"); break;
285                         case PROJECTILE_BULLET_GLOWING_TRACER: setmodel(self, "models/tracer.mdl");self.traileffect = particleeffectnum("tr_rifle"); break;
286                         case PROJECTILE_CRYLINK: setmodel(self, "models/plasmatrail.mdl");self.traileffect = particleeffectnum("TR_CRYLINKPLASMA"); break;
287                         case PROJECTILE_CRYLINK_BOUNCING: setmodel(self, "models/plasmatrail.mdl");self.traileffect = particleeffectnum("TR_CRYLINKPLASMA"); break;
288                         case PROJECTILE_ELECTRO_BEAM: setmodel(self, "models/elaser.mdl");self.traileffect = particleeffectnum("TR_NEXUIZPLASMA"); break;
289                         case PROJECTILE_GRENADE: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_GRENADE"); break;
290                         case PROJECTILE_GRENADE_BOUNCING: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_GRENADE"); break;
291                         case PROJECTILE_LASER: setmodel(self, "models/laser.mdl");self.traileffect = particleeffectnum(""); break;
292                         case PROJECTILE_HLAC: setmodel(self, "models/hlac_bullet.md3");self.traileffect = particleeffectnum(""); break;
293                         case PROJECTILE_PORTO_RED: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_WIZSPIKE"); self.scale = 4; break;
294                         case PROJECTILE_PORTO_BLUE: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_WIZSPIKE"); self.scale = 4; break;
295                         case PROJECTILE_HOOKBOMB: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_KNIGHTSPIKE"); break;
296                         case PROJECTILE_HAGAR: setmodel(self, "models/hagarmissile.mdl");self.traileffect = particleeffectnum("TR_GRENADE"); self.scale = 0.4; break;
297                         case PROJECTILE_HAGAR_BOUNCING: setmodel(self, "models/hagarmissile.mdl");self.traileffect = particleeffectnum("TR_GRENADE"); self.scale = 0.4; break;
298                         case PROJECTILE_FIREBALL: self.model = ""; self.modelindex = 0; self.traileffect = particleeffectnum("fireball"); break; // particle effect is good enough
299                         case PROJECTILE_FIREMINE: self.model = ""; self.modelindex = 0; self.traileffect = particleeffectnum("firemine"); break; // particle effect is good enough
300                         case PROJECTILE_TAG: setmodel(self, "models/laser.mdl"); self.traileffect = particleeffectnum("TR_ROCKET"); break;
301                         case PROJECTILE_FLAC: setmodel(self, "models/hagarmissile.mdl"); self.scale = 0.4; self.traileffect = particleeffectnum("TR_ROCKET"); break;
302                         case PROJECTILE_SEEKER: setmodel(self, "models/tagrocket.md3"); self.scale = 2; self.traileffect = particleeffectnum("TR_ROCKET"); break;
303                         default:
304                                 error("Received invalid CSQC projectile, can't work with this!");
305                                 break;
306                 }
307
308                 self.mins = '0 0 0';
309                 self.maxs = '0 0 0';
310                 self.colormod = '0 0 0';
311                 self.move_touch = SUB_Stop;
312                 self.move_movetype = MOVETYPE_TOSS;
313                 self.alphamod = 1;
314
315                 switch(self.cnt)
316                 {
317                         case PROJECTILE_ELECTRO:
318                                 // only new engines support sound moving with object
319                                 loopsound(self, CHAN_PROJECTILE, "weapons/electro_fly.wav", VOL_BASE, ATTN_NORM);
320                                 self.mins = '0 0 -3';
321                                 self.maxs = '0 0 -3';
322                                 self.move_movetype = MOVETYPE_BOUNCE;
323                                 self.move_touch = SUB_Null;
324                                 break;
325                         case PROJECTILE_ROCKET:
326                                 loopsound(self, CHAN_PROJECTILE, "weapons/rocket_fly.wav", VOL_BASE, ATTN_NORM);
327                                 self.mins = '-3 -3 -3';
328                                 self.maxs = '3 3 3';
329                                 break;
330                         case PROJECTILE_GRENADE:
331                                 self.mins = '0 0 -3';
332                                 self.maxs = '0 0 -3';
333                                 break;
334                         case PROJECTILE_GRENADE_BOUNCING:
335                                 self.mins = '0 0 -3';
336                                 self.maxs = '0 0 -3';
337                                 self.move_movetype = MOVETYPE_BOUNCE;
338                                 self.move_touch = SUB_Null;
339                                 self.move_bounce_factor = g_balance_grenadelauncher_secondary_bouncefactor;
340                                 self.move_bounce_stopspeed = g_balance_grenadelauncher_secondary_bouncestop;
341                                 break;
342                         case PROJECTILE_PORTO_RED:
343                                 self.colormod = '2 1 1';
344                                 self.alphamod = 0.5;
345                                 self.move_movetype = MOVETYPE_BOUNCE;
346                                 self.move_touch = SUB_Null;
347                                 break;
348                         case PROJECTILE_PORTO_BLUE:
349                                 self.colormod = '1 1 2';
350                                 self.alphamod = 0.5;
351                                 self.move_movetype = MOVETYPE_BOUNCE;
352                                 self.move_touch = SUB_Null;
353                                 break;
354                         case PROJECTILE_HAGAR_BOUNCING:
355                                 self.move_movetype = MOVETYPE_BOUNCE;
356                                 self.move_touch = SUB_Null;
357                                 break;
358                         case PROJECTILE_CRYLINK_BOUNCING:
359                                 self.move_movetype = MOVETYPE_BOUNCE;
360                                 self.move_touch = SUB_Null;
361                                 break;
362                         case PROJECTILE_FIREBALL:
363                                 loopsound(self, CHAN_PROJECTILE, "weapons/fireball_fly2.wav", VOL_BASE, ATTN_NORM);
364                                 self.mins = '-16 -16 -16';
365                                 self.maxs = '16 16 16';
366                                 break;
367                         case PROJECTILE_FIREMINE:
368                                 loopsound(self, CHAN_PROJECTILE, "weapons/fireball_fly.wav", VOL_BASE, ATTN_NORM);
369                                 self.move_movetype = MOVETYPE_BOUNCE;
370                                 self.move_touch = SUB_Null;
371                                 self.mins = '-4 -4 -4';
372                                 self.maxs = '4 4 4';
373                                 break;
374                         case PROJECTILE_TAG:
375                                 loopsound(self, CHAN_PROJECTILE, "weapons/tag_rocket_fly.wav", VOL_BASE, ATTN_NORM);
376                                 self.mins = '-2 -2 -2';
377                                 self.maxs = '2 2 2';
378                                 break;
379                         case PROJECTILE_FLAC:
380                                 self.mins = '-2 -2 -2';
381                                 self.maxs = '2 2 2';
382                                 break;
383                         case PROJECTILE_SEEKER:
384                                 self.mins = '-4 -4 -4';
385                                 self.maxs = '4 4 4';
386                                 break;
387                         default:
388                                 break;
389                 }
390         }
391
392         if(self.gravity)
393         {
394                 if(self.move_movetype == MOVETYPE_FLY)
395                         self.move_movetype = MOVETYPE_TOSS;
396                 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
397                         self.move_movetype = MOVETYPE_BOUNCE;
398         }
399         else
400         {
401                 if(self.move_movetype == MOVETYPE_TOSS)
402                         self.move_movetype = MOVETYPE_FLY;
403                 if(self.move_movetype == MOVETYPE_BOUNCE)
404                         self.move_movetype = MOVETYPE_BOUNCEMISSILE;
405         }
406
407         if(!(self.count & 0x80))
408                 InterpolateOrigin_Note();
409         
410         self.draw = Projectile_Draw;
411         self.entremove = Ent_RemoveProjectile;
412 }
413
414 void Projectile_Precache()
415 {
416         precache_model("models/ebomb.mdl");
417         precache_model("models/elaser.mdl");
418         precache_model("models/grenademodel.md3");
419         precache_model("models/hagarmissile.mdl");
420         precache_model("models/hlac_bullet.md3");
421         precache_model("models/laser.mdl");
422         precache_model("models/plasmatrail.mdl");
423         precache_model("models/rocket.md3");
424         precache_model("models/tagrocket.md3");
425         precache_model("models/tracer.mdl");
426         precache_sound("weapons/electro_fly.wav");
427         precache_sound("weapons/rocket_fly.wav");
428         precache_sound("weapons/fireball_fly.wav");
429         precache_sound("weapons/fireball_fly2.wav");
430         precache_sound("weapons/tag_rocket_fly.wav");
431 }