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