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