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