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