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