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