Add cvars for bouncefactor/bouncestop of GL secondary
[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 .float bouncefactor;
162 .float bouncestop;
163 void Ent_Projectile()
164 {
165         float f, s;
166
167         // projectile properties:
168         //   kind (interpolated, or clientside)
169         //
170         //   modelindex
171         //   origin
172         //   scale
173         //   if clientside:
174         //     velocity
175         //     gravity
176         //   soundindex (hardcoded list)
177         //   effects
178         //
179         // projectiles don't send angles, because they always follow the velocity
180         
181         f = ReadByte();
182         self.count = (f & 0x80);
183         self.iflags = (self.iflags & IFLAG_INTERNALMASK) | IFLAG_AUTOANGLES | IFLAG_ANGLES;
184         self.solid = SOLID_TRIGGER;
185         //self.effects = EF_NOMODELFLAGS;
186
187         // this should make collisions with bmodels more exact, but it leads to
188         // projectiles no longer being able to lie on a bmodel
189         self.move_nomonsters = MOVE_WORLDONLY;
190         if(f & 0x40)
191                 self.move_flags |= FL_ONGROUND;
192         else
193                 self.move_flags &~= FL_ONGROUND;
194
195         if(!self.move_time)
196         {
197                 // for some unknown reason, we don't need to care for
198                 // sv_gameplayfix_delayprojectiles here.
199                 self.move_time = time;
200                 self.spawntime = time;
201         }
202         else
203                 self.move_time = max(self.move_time, time);
204
205         if(!(self.count & 0x80))
206                 InterpolateOrigin_Undo();
207
208         if(f & 1)
209         {
210                 self.origin_x = ReadCoord();
211                 self.origin_y = ReadCoord();
212                 self.origin_z = ReadCoord();
213                 if(self.count & 0x80)
214                 {
215                         self.velocity_x = ReadCoord();
216                         self.velocity_y = ReadCoord();
217                         self.velocity_z = ReadCoord();
218                         self.gravity = ReadCoord();
219
220                         s = ReadByte();
221
222                         if (s & 1)
223                         {
224                                 self.bouncefactor = ReadCoord();
225                                 self.bouncestop = ReadCoord();
226                         }
227
228                         self.move_origin = self.origin;
229                         self.move_velocity = self.velocity;
230                         self.move_bounce_factor = self.bouncefactor;
231                         self.move_bounce_stopspeed = self.bouncestop;
232                 }
233
234                 if(time == self.spawntime || (self.count & 0x80) || (f & 0x10))
235                 {
236                         self.trail_oldorigin = self.origin;
237                         if(!(self.count & 0x80))
238                                 InterpolateOrigin_Reset();
239                 }
240
241                 if(f & 0x20)
242                 {
243                         self.fade_time = time + ReadByte() * ticrate;
244                         self.fade_rate = 1 / (ReadByte() * ticrate);
245                 }
246                 else
247                 {
248                         self.fade_time = 0;
249                         self.fade_rate = 0;
250                 }
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                 self.traileffect = 0;
262                 switch(self.cnt)
263                 {
264                         case PROJECTILE_ELECTRO: setmodel(self, "models/ebomb.mdl");self.traileffect = particleeffectnum("TR_NEXUIZPLASMA"); break;
265                         case PROJECTILE_ROCKET: setmodel(self, "models/rocket.md3");self.traileffect = particleeffectnum("TR_ROCKET"); self.scale = 2; break;
266                         case PROJECTILE_BULLET: setmodel(self, "models/tracer.mdl");self.traileffect = particleeffectnum("tr_bullet"); break;
267                         case PROJECTILE_BULLET_GLOWING: setmodel(self, "models/tracer.mdl");self.traileffect = particleeffectnum("tr_bullet"); break;
268                         case PROJECTILE_BULLET_GLOWING_TRACER: setmodel(self, "models/tracer.mdl");self.traileffect = particleeffectnum("tr_rifle"); break;
269                         case PROJECTILE_CRYLINK: setmodel(self, "models/plasmatrail.mdl");self.traileffect = particleeffectnum("TR_CRYLINKPLASMA"); break;
270                         case PROJECTILE_CRYLINK_BOUNCING: setmodel(self, "models/plasmatrail.mdl");self.traileffect = particleeffectnum("TR_CRYLINKPLASMA"); break;
271                         case PROJECTILE_ELECTRO_BEAM: setmodel(self, "models/elaser.mdl");self.traileffect = particleeffectnum("TR_NEXUIZPLASMA"); break;
272                         case PROJECTILE_GRENADE: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_GRENADE"); break;
273                         case PROJECTILE_GRENADE_BOUNCING: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_GRENADE"); break;
274                         case PROJECTILE_LASER: setmodel(self, "models/laser.mdl");self.traileffect = particleeffectnum(""); break;
275                         case PROJECTILE_HLAC: setmodel(self, "models/hlac_bullet.md3");self.traileffect = particleeffectnum(""); break;
276                         case PROJECTILE_PORTO_RED: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_WIZSPIKE"); self.scale = 4; break;
277                         case PROJECTILE_PORTO_BLUE: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_WIZSPIKE"); self.scale = 4; break;
278                         case PROJECTILE_HOOKBOMB: setmodel(self, "models/grenademodel.md3");self.traileffect = particleeffectnum("TR_KNIGHTSPIKE"); break;
279                         case PROJECTILE_HAGAR: setmodel(self, "models/hagarmissile.mdl");self.traileffect = particleeffectnum("TR_GRENADE"); self.scale = 0.4; break;
280                         case PROJECTILE_HAGAR_BOUNCING: setmodel(self, "models/hagarmissile.mdl");self.traileffect = particleeffectnum("TR_GRENADE"); self.scale = 0.4; break;
281                         case PROJECTILE_FIREBALL: self.model = ""; self.modelindex = 0; self.traileffect = particleeffectnum("fireball"); break; // particle effect is good enough
282                         case PROJECTILE_FIREMINE: self.model = ""; self.modelindex = 0; self.traileffect = particleeffectnum("firemine"); break; // particle effect is good enough
283                         default:
284                                 error("Received invalid CSQC projectile, can't work with this!");
285                                 break;
286                 }
287
288                 self.mins = '0 0 0';
289                 self.maxs = '0 0 0';
290                 self.colormod = '0 0 0';
291                 self.move_touch = SUB_Stop;
292                 self.move_movetype = MOVETYPE_TOSS;
293                 self.alphamod = 1;
294
295                 switch(self.cnt)
296                 {
297                         case PROJECTILE_ELECTRO:
298                                 // only new engines support sound moving with object
299                                 loopsound(self, CHAN_PROJECTILE, "weapons/electro_fly.wav", VOL_BASE, ATTN_NORM);
300                                 self.mins = '0 0 -3';
301                                 self.maxs = '0 0 -3';
302                                 self.move_movetype = MOVETYPE_BOUNCE;
303                                 self.move_touch = SUB_Null;
304                                 break;
305                         case PROJECTILE_ROCKET:
306                                 loopsound(self, CHAN_PROJECTILE, "weapons/rocket_fly.wav", VOL_BASE, ATTN_NORM);
307                                 self.mins = '-3 -3 -3';
308                                 self.maxs = '3 3 3';
309                                 break;
310                         case PROJECTILE_GRENADE:
311                                 self.mins = '0 0 -3';
312                                 self.maxs = '0 0 -3';
313                                 break;
314                         case PROJECTILE_GRENADE_BOUNCING:
315                                 self.mins = '0 0 -3';
316                                 self.maxs = '0 0 -3';
317                                 self.move_movetype = MOVETYPE_BOUNCE;
318                                 self.move_touch = SUB_Null;
319                                 break;
320                         case PROJECTILE_PORTO_RED:
321                                 self.colormod = '2 1 1';
322                                 self.alphamod = 0.5;
323                                 self.move_movetype = MOVETYPE_BOUNCE;
324                                 self.move_touch = SUB_Null;
325                                 break;
326                         case PROJECTILE_PORTO_BLUE:
327                                 self.colormod = '1 1 2';
328                                 self.alphamod = 0.5;
329                                 self.move_movetype = MOVETYPE_BOUNCE;
330                                 self.move_touch = SUB_Null;
331                                 break;
332                         case PROJECTILE_HAGAR_BOUNCING:
333                                 self.move_movetype = MOVETYPE_BOUNCE;
334                                 self.move_touch = SUB_Null;
335                                 break;
336                         case PROJECTILE_CRYLINK_BOUNCING:
337                                 self.move_movetype = MOVETYPE_BOUNCE;
338                                 self.move_touch = SUB_Null;
339                                 break;
340                         case PROJECTILE_FIREBALL:
341                                 loopsound(self, CHAN_PROJECTILE, "weapons/fireball_fly2.wav", VOL_BASE, ATTN_NORM);
342                                 self.mins = '-16 -16 -16';
343                                 self.maxs = '16 16 16';
344                                 break;
345                         case PROJECTILE_FIREMINE:
346                                 loopsound(self, CHAN_PROJECTILE, "weapons/fireball_fly.wav", VOL_BASE, ATTN_NORM);
347                                 self.move_movetype = MOVETYPE_BOUNCE;
348                                 self.move_touch = SUB_Null;
349                                 self.mins = '-4 -4 -4';
350                                 self.maxs = '4 4 4';
351                                 break;
352                         default:
353                                 break;
354                 }
355         }
356
357         if(self.gravity)
358         {
359                 if(self.move_movetype == MOVETYPE_FLY)
360                         self.move_movetype = MOVETYPE_TOSS;
361                 if(self.move_movetype == MOVETYPE_BOUNCEMISSILE)
362                         self.move_movetype = MOVETYPE_BOUNCE;
363         }
364         else
365         {
366                 if(self.move_movetype == MOVETYPE_TOSS)
367                         self.move_movetype = MOVETYPE_FLY;
368                 if(self.move_movetype == MOVETYPE_BOUNCE)
369                         self.move_movetype = MOVETYPE_BOUNCEMISSILE;
370         }
371
372         if(!(self.count & 0x80))
373                 InterpolateOrigin_Note();
374         
375         self.draw = Projectile_Draw;
376         self.entremove = Ent_RemoveProjectile;
377 }
378
379 void Projectile_Precache()
380 {
381         precache_model("models/ebomb.mdl");
382         precache_model("models/elaser.mdl");
383         precache_model("models/grenademodel.md3");
384         precache_model("models/hagarmissile.mdl");
385         precache_model("models/hlac_bullet.md3");
386         precache_model("models/laser.mdl");
387         precache_model("models/plasmatrail.mdl");
388         precache_model("models/rocket.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/fireball_fly.wav");
393         precache_sound("weapons/fireball_fly2.wav");
394 }