Vehicles: make racer depend on weapon system for ammo and refire checks
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / vehicles / vehicle / racer.qc
1 #ifndef VEHICLE_RACER
2 #define VEHICLE_RACER
3
4 CLASS(Racer, Vehicle)
5 /* spawnflags */ ATTRIB(Racer, spawnflags, int, VHF_DMGSHAKE | VHF_DMGROLL);
6 /* mins       */ ATTRIB(Racer, mins, vector, '-120 -120 -40' * 0.5);
7 /* maxs       */ ATTRIB(Racer, maxs, vector, '120 120 40' * 0.5);
8 /* model          */ ATTRIB(Racer, mdl, string, "models/vehicles/wakizashi.dpm");
9 /* model          */ ATTRIB(Racer, model, string, "models/vehicles/wakizashi.dpm");
10 /* head_model */ ATTRIB(Racer, head_model, string, "null");
11 /* hud_model  */ ATTRIB(Racer, hud_model, string, "models/vehicles/wakizashi_cockpit.dpm");
12 /* tags       */ ATTRIB(Racer, tag_head, string, "");
13 /* tags       */ ATTRIB(Racer, tag_hud, string, "");
14 /* tags       */ ATTRIB(Racer, tag_hview, string, "tag_viewport");
15 /* netname    */ ATTRIB(Racer, netname, string, "racer");
16 /* fullname   */ ATTRIB(Racer, vehicle_name, string, _("Racer"));
17 /* icon       */ ATTRIB(Racer, m_icon, string, "vehicle_racer");
18 ENDCLASS(Racer)
19
20 REGISTER_VEHICLE(RACER, NEW(Racer));
21
22 #include "../../weapons/all.qh"
23
24 CLASS(RacerAttack, PortoLaunch)
25 /* flags     */ ATTRIB(RacerAttack, spawnflags, int, WEP_TYPE_OTHER);
26 /* impulse   */ ATTRIB(RacerAttack, impulse, int, 3);
27 /* refname   */ ATTRIB(RacerAttack, netname, string, "racercannon");
28 /* wepname   */ ATTRIB(RacerAttack, message, string, _("Racer cannon"));
29 ENDCLASS(RacerAttack)
30 REGISTER_WEAPON(RACER, NEW(RacerAttack));
31
32 #endif
33
34 #ifdef IMPLEMENTATION
35 #ifdef SVQC
36 #include "../../effects/effects.qh"
37 #include "../../triggers/trigger/impulse.qh"
38
39 float autocvar_g_vehicle_racer_cannon_cost;
40 float autocvar_g_vehicle_racer_cannon_damage;
41 float autocvar_g_vehicle_racer_cannon_radius;
42 float autocvar_g_vehicle_racer_cannon_refire;
43 float autocvar_g_vehicle_racer_cannon_speed;
44 float autocvar_g_vehicle_racer_cannon_spread;
45 float autocvar_g_vehicle_racer_cannon_force;
46
47 void racer_fire_rocket(vector org, vector dir, entity trg);
48 METHOD(RacerAttack, wr_think, bool(entity thiswep, bool fire1, bool fire2)) {
49         SELFPARAM();
50         bool isPlayer = IS_PLAYER(self);
51         entity player = isPlayer ? self : self.owner;
52         entity veh = player.vehicle;
53         setself(player);
54         if (fire1)
55         if (weapon_prepareattack(false, autocvar_g_vehicle_racer_cannon_refire)) {
56                 if (veh) {
57                         veh.vehicle_energy -= autocvar_g_vehicle_racer_cannon_cost;
58                         veh.wait = time;
59                 }
60                 if (isPlayer) W_SetupShot_Dir(self, v_forward, false, 0, SND(Null), CH_WEAPON_B, 0);
61                 vector org = w_shotorg;
62                 vector dir = w_shotdir;
63                 entity bolt = vehicles_projectile(EFFECT_RACER_MUZZLEFLASH.eent_eff_name, SND(LASERGUN_FIRE),
64                                                            org, normalize(v_forward + randomvec() * autocvar_g_vehicle_racer_cannon_spread) * autocvar_g_vehicle_racer_cannon_speed,
65                                                            autocvar_g_vehicle_racer_cannon_damage, autocvar_g_vehicle_racer_cannon_radius, autocvar_g_vehicle_racer_cannon_force,  0,
66                                                            DEATH_VH_WAKI_GUN, PROJECTILE_WAKICANNON, 0, true, true, self.owner);
67                 bolt.velocity = normalize(dir) * autocvar_g_vehicle_racer_cannon_speed;
68                 weapon_thinkf(WFRAME_FIRE1, 0, w_ready);
69         }
70         if (fire2)
71         if (!isPlayer || weapon_prepareattack(false, 0.2)) {
72                 if (isPlayer) W_SetupShot_Dir(self, v_forward, false, 0, SND(Null), CH_WEAPON_B, 0);
73                 racer_fire_rocket(w_shotorg, w_shotdir, NULL);
74                 weapon_thinkf(WFRAME_FIRE2, 0, w_ready);
75         }
76         setself(this);
77         return true;
78 }
79 METHOD(RacerAttack, wr_checkammo1, bool(RacerAttack thiswep)) {
80         SELFPARAM();
81         bool isPlayer = IS_PLAYER(self);
82         entity player = isPlayer ? self : self.owner;
83         entity veh = player.vehicle;
84         return isPlayer || veh.vehicle_energy >= autocvar_g_vehicle_racer_cannon_cost;
85 }
86
87 bool autocvar_g_vehicle_racer;
88
89 float autocvar_g_vehicle_racer_speed_afterburn;
90 float autocvar_g_vehicle_racer_afterburn_cost;
91
92 float autocvar_g_vehicle_racer_waterburn_cost;
93 float autocvar_g_vehicle_racer_waterburn_speed;
94
95 float autocvar_g_vehicle_racer_water_speed_forward;
96 float autocvar_g_vehicle_racer_water_speed_strafe;
97
98 float autocvar_g_vehicle_racer_pitchlimit = 30;
99
100 float autocvar_g_vehicle_racer_water_downforce = 0.03;
101 float autocvar_g_vehicle_racer_water_upforcedamper = 15;
102
103 float autocvar_g_vehicle_racer_anglestabilizer;
104 float autocvar_g_vehicle_racer_downforce;
105
106 float autocvar_g_vehicle_racer_speed_forward;
107 float autocvar_g_vehicle_racer_speed_strafe;
108 float autocvar_g_vehicle_racer_springlength;
109 float autocvar_g_vehicle_racer_upforcedamper;
110 float autocvar_g_vehicle_racer_friction;
111
112 float autocvar_g_vehicle_racer_water_time = 5;
113
114 float autocvar_g_vehicle_racer_hovertype;
115 float autocvar_g_vehicle_racer_hoverpower;
116
117 float autocvar_g_vehicle_racer_turnroll;
118 float autocvar_g_vehicle_racer_turnspeed;
119 float autocvar_g_vehicle_racer_pitchspeed;
120
121 float autocvar_g_vehicle_racer_energy;
122 float autocvar_g_vehicle_racer_energy_regen;
123 float autocvar_g_vehicle_racer_energy_regen_pause;
124
125 float autocvar_g_vehicle_racer_health;
126 float autocvar_g_vehicle_racer_health_regen;
127 float autocvar_g_vehicle_racer_health_regen_pause;
128
129 float autocvar_g_vehicle_racer_shield;
130 float autocvar_g_vehicle_racer_shield_regen;
131 float autocvar_g_vehicle_racer_shield_regen_pause;
132
133 float autocvar_g_vehicle_racer_rocket_accel;
134 float autocvar_g_vehicle_racer_rocket_damage;
135 float autocvar_g_vehicle_racer_rocket_radius;
136 float autocvar_g_vehicle_racer_rocket_force;
137 float autocvar_g_vehicle_racer_rocket_refire;
138 float autocvar_g_vehicle_racer_rocket_speed;
139 float autocvar_g_vehicle_racer_rocket_turnrate;
140
141 float autocvar_g_vehicle_racer_rocket_locktarget;
142 float autocvar_g_vehicle_racer_rocket_locking_time;
143 float autocvar_g_vehicle_racer_rocket_locking_releasetime;
144 float autocvar_g_vehicle_racer_rocket_locked_time;
145 float autocvar_g_vehicle_racer_rocket_locked_maxangle;
146 float autocvar_g_vehicle_racer_rocket_climbspeed;
147
148 float autocvar_g_vehicle_racer_respawntime;
149
150 float autocvar_g_vehicle_racer_blowup_radius;
151 float autocvar_g_vehicle_racer_blowup_coredamage;
152 float autocvar_g_vehicle_racer_blowup_edgedamage;
153 float autocvar_g_vehicle_racer_blowup_forceintensity;
154
155 float autocvar_g_vehicle_racer_bouncefactor;
156 float autocvar_g_vehicle_racer_bouncestop;
157 vector autocvar_g_vehicle_racer_bouncepain;
158
159 .float racer_watertime;
160
161 var vector racer_force_from_tag(string tag_name, float spring_length, float max_power);
162
163 void racer_align4point(float _delta)
164 {SELFPARAM();
165         vector push_vector;
166         float fl_push, fr_push, bl_push, br_push;
167
168         push_vector  = racer_force_from_tag("tag_engine_fr", autocvar_g_vehicle_racer_springlength, autocvar_g_vehicle_racer_hoverpower);
169         fr_push   = force_fromtag_normpower;
170         //vehicles_sweap_collision(force_fromtag_origin, self.velocity, _delta, v_add, autocvar_g_vehicle_racer_collision_multiplier);
171
172         push_vector += racer_force_from_tag("tag_engine_fl", autocvar_g_vehicle_racer_springlength, autocvar_g_vehicle_racer_hoverpower);
173         fl_push   = force_fromtag_normpower;
174         //vehicles_sweap_collision(force_fromtag_origin, self.velocity, _delta, v_add, autocvar_g_vehicle_racer_collision_multiplier);
175
176         push_vector += racer_force_from_tag("tag_engine_br", autocvar_g_vehicle_racer_springlength, autocvar_g_vehicle_racer_hoverpower);
177         br_push   = force_fromtag_normpower;
178         //vehicles_sweap_collision(force_fromtag_origin, self.velocity, _delta, v_add, autocvar_g_vehicle_racer_collision_multiplier);
179
180         push_vector += racer_force_from_tag("tag_engine_bl", autocvar_g_vehicle_racer_springlength, autocvar_g_vehicle_racer_hoverpower);
181         bl_push   = force_fromtag_normpower;
182         //vehicles_sweap_collision(force_fromtag_origin, self.velocity, _delta, v_add, autocvar_g_vehicle_racer_collision_multiplier);
183
184         self.velocity += push_vector * _delta;
185
186         float uforce = autocvar_g_vehicle_racer_upforcedamper;
187
188         int cont = pointcontents(self.origin - '0 0 64');
189         if(cont == CONTENT_WATER || cont == CONTENT_LAVA || cont == CONTENT_SLIME)
190         {
191                 uforce = autocvar_g_vehicle_racer_water_upforcedamper;
192
193                 if(self.owner.BUTTON_CROUCH && time < self.air_finished)
194                         self.velocity_z += 30;
195                 else
196                         self.velocity_z += 200;
197         }
198
199
200         // Anti ocilation
201         if(self.velocity_z > 0)
202                 self.velocity_z *= 1 - uforce * _delta;
203
204         push_vector_x =  (fl_push - bl_push);
205         push_vector_x += (fr_push - br_push);
206         push_vector_x *= 360;
207
208         push_vector_z = (fr_push - fl_push);
209         push_vector_z += (br_push - bl_push);
210         push_vector_z *= 360;
211
212         // Apply angle diffrance
213         self.angles_z += push_vector_z * _delta;
214         self.angles_x += push_vector_x * _delta;
215
216         // Apply stabilizer
217         self.angles_x *= 1 - (autocvar_g_vehicle_racer_anglestabilizer * _delta);
218         self.angles_z *= 1 - (autocvar_g_vehicle_racer_anglestabilizer * _delta);
219 }
220
221 void racer_rocket_groundhugger()
222 {SELFPARAM();
223         vector olddir, newdir;
224         float oldvel, newvel;
225
226         self.nextthink  = time;
227
228         if(self.owner.deadflag != DEAD_NO || self.cnt < time)
229         {
230                 self.use();
231                 return;
232         }
233
234         if(!self.realowner.vehicle)
235         {
236                 UpdateCSQCProjectile(self);
237                 return;
238         }
239
240         olddir = normalize(self.velocity);
241         oldvel = vlen(self.velocity);
242         newvel = oldvel + self.lip;
243
244         tracebox(self.origin, self.mins, self.maxs, self.origin + olddir * 64, MOVE_WORLDONLY,self);
245         if(trace_fraction <= 0.5)
246         {
247                 // Hitting somethign soon, just speed ahead
248                 self.velocity = olddir * newvel;
249                 UpdateCSQCProjectile(self);
250                 return;
251         }
252
253         traceline(trace_endpos, trace_endpos - '0 0 64', MOVE_NORMAL, self);
254         if(trace_fraction != 1.0)
255         {
256                 newdir = normalize(trace_endpos + '0 0 64' - self.origin) * autocvar_g_vehicle_racer_rocket_turnrate;
257                 self.velocity = normalize(olddir + newdir) * newvel;
258         }
259         else
260         {
261                 self.velocity = olddir * newvel;
262                 self.velocity_z -= 1600 * sys_frametime; // 2x grav looks better for this one
263         }
264
265         int cont = pointcontents(self.origin - '0 0 32');
266         if(cont == CONTENT_WATER || cont == CONTENT_LAVA || cont == CONTENT_SLIME)
267                 self.velocity_z += 200;
268
269         UpdateCSQCProjectile(self);
270         return;
271 }
272
273 void racer_rocket_tracker()
274 {SELFPARAM();
275         vector olddir, newdir;
276         float oldvel, newvel;
277
278         self.nextthink  = time;
279
280         if (self.owner.deadflag != DEAD_NO || self.cnt < time)
281         {
282                 self.use();
283                 return;
284         }
285
286         if(!self.realowner.vehicle)
287         {
288                 UpdateCSQCProjectile(self);
289                 return;
290         }
291
292         olddir = normalize(self.velocity);
293         oldvel = vlen(self.velocity);
294         newvel = oldvel + self.lip;
295         makevectors(vectoangles(olddir));
296
297         float time_to_impact = min(vlen(self.enemy.origin - self.origin) / vlen(self.velocity), 1);
298         vector predicted_origin = self.enemy.origin + self.enemy.velocity * time_to_impact;
299
300         traceline(self.origin, self.origin + v_forward * 64 - '0 0 32', MOVE_NORMAL, self);
301         newdir = normalize(predicted_origin - self.origin);
302
303         //vector
304         float height_diff = predicted_origin_z - self.origin_z;
305
306         if(vlen(newdir - v_forward) > autocvar_g_vehicle_racer_rocket_locked_maxangle)
307         {
308                 //bprint("Target lost!\n");
309                 //dprint("OF:", ftos(vlen(newdir - v_forward)), "\n");
310                 self.think = racer_rocket_groundhugger;
311                 return;
312         }
313
314         if(trace_fraction != 1.0 && trace_ent != self.enemy)
315                 newdir_z += 16 * sys_frametime;
316
317         self.velocity = normalize(olddir + newdir * autocvar_g_vehicle_racer_rocket_turnrate) * newvel;
318         self.velocity_z -= 800 * sys_frametime;
319         self.velocity_z += max(height_diff, autocvar_g_vehicle_racer_rocket_climbspeed) * sys_frametime ;
320
321         UpdateCSQCProjectile(self);
322         return;
323 }
324
325 void racer_fire_rocket(vector org, vector dir, entity trg)
326 {SELFPARAM();
327         entity rocket = vehicles_projectile(EFFECT_RACER_ROCKETLAUNCH.eent_eff_name, SND(ROCKET_FIRE),
328                                                    org, dir * autocvar_g_vehicle_racer_rocket_speed,
329                                                    autocvar_g_vehicle_racer_rocket_damage, autocvar_g_vehicle_racer_rocket_radius, autocvar_g_vehicle_racer_rocket_force, 3,
330                                                    DEATH_VH_WAKI_ROCKET, PROJECTILE_WAKIROCKET, 20, false, false, self.owner);
331
332         rocket.lip                        = autocvar_g_vehicle_racer_rocket_accel * sys_frametime;
333         rocket.wait                      = autocvar_g_vehicle_racer_rocket_turnrate;
334         rocket.nextthink                = time;
335         rocket.enemy                    = trg;
336         rocket.cnt                        = time + 15;
337
338         if(trg)
339                 rocket.think                    = racer_rocket_tracker;
340         else
341                 rocket.think                    = racer_rocket_groundhugger;
342 }
343
344 void racer_fire_rocket_aim(string tagname, entity trg)
345 {
346         SELFPARAM();
347         vector v = gettaginfo(self, gettagindex(self, tagname));
348         racer_fire_rocket(v, v_forward, trg);
349 }
350
351 float racer_frame()
352 {SELFPARAM();
353         entity player, racer;
354         vector df;
355         float ftmp;
356
357         if(intermission_running)
358         {
359                 self.vehicle.velocity = '0 0 0';
360                 self.vehicle.avelocity = '0 0 0';
361                 return 1;
362         }
363
364         player  = self;
365         racer   = self.vehicle;
366         setself(racer);
367
368         vehicles_painframe();
369
370         if(pointcontents(racer.origin) != CONTENT_WATER)
371                 racer.air_finished = time + autocvar_g_vehicle_racer_water_time;
372
373         if(racer.deadflag != DEAD_NO)
374         {
375                 setself(player);
376                 player.BUTTON_ATCK = player.BUTTON_ATCK2 = 0;
377                 return 1;
378         }
379
380         racer_align4point(PHYS_INPUT_TIMELENGTH);
381
382         player.BUTTON_ZOOM = player.BUTTON_CROUCH = 0;
383
384         crosshair_trace(player);
385
386         racer.angles_x *= -1;
387
388         // Yaw
389         ftmp = autocvar_g_vehicle_racer_turnspeed * PHYS_INPUT_TIMELENGTH;
390         ftmp = bound(-ftmp, shortangle_f(player.v_angle_y - racer.angles_y, racer.angles_y), ftmp);
391         racer.angles_y = anglemods(racer.angles_y + ftmp);
392
393         // Roll
394         racer.angles_z += -ftmp * autocvar_g_vehicle_racer_turnroll * PHYS_INPUT_TIMELENGTH;
395
396         // Pitch
397         ftmp = autocvar_g_vehicle_racer_pitchspeed  * PHYS_INPUT_TIMELENGTH;
398         ftmp = bound(-ftmp, shortangle_f(player.v_angle_x - racer.angles_x, racer.angles_x), ftmp);
399         racer.angles_x = bound(-autocvar_g_vehicle_racer_pitchlimit, anglemods(racer.angles_x + ftmp), autocvar_g_vehicle_racer_pitchlimit);
400
401         makevectors(racer.angles);
402         racer.angles_x *= -1;
403
404         //ftmp = racer.velocity_z;
405         df = racer.velocity * -autocvar_g_vehicle_racer_friction;
406         //racer.velocity_z = ftmp;
407
408         int cont = pointcontents(racer.origin);
409         if(vlen(player.movement) != 0)
410         {
411                 if(cont == CONTENT_WATER || cont == CONTENT_LAVA || cont == CONTENT_SLIME)
412                 {
413                         if(player.movement_x) { df += v_forward * ((player.movement_x > 0) ? autocvar_g_vehicle_racer_water_speed_forward : -autocvar_g_vehicle_racer_water_speed_forward); }
414                         if(player.movement_y) { df += v_right * ((player.movement_y > 0) ? autocvar_g_vehicle_racer_water_speed_strafe : -autocvar_g_vehicle_racer_water_speed_strafe); }
415                 }
416                 else
417                 {
418                         if(player.movement_x) { df += v_forward * ((player.movement_x > 0) ? autocvar_g_vehicle_racer_speed_forward : -autocvar_g_vehicle_racer_speed_forward); }
419                         if(player.movement_y) { df += v_right * ((player.movement_y > 0) ? autocvar_g_vehicle_racer_speed_strafe : -autocvar_g_vehicle_racer_speed_strafe); }
420                 }
421
422 #ifdef SVQC
423                 if(self.sound_nexttime < time || self.sounds != 1)
424                 {
425                         self.sounds = 1;
426                         self.sound_nexttime = time + 10.922667; //soundlength("vehicles/racer_move.wav");
427                         sound (self, CH_TRIGGER_SINGLE, SND_VEH_RACER_MOVE, VOL_VEHICLEENGINE, ATTEN_NORM);
428                 }
429 #endif
430         }
431 #ifdef SVQC
432         else
433         {
434                 if(self.sound_nexttime < time || self.sounds != 0)
435                 {
436                         self.sounds = 0;
437                         self.sound_nexttime = time + 11.888604; //soundlength("vehicles/racer_idle.wav");
438                         sound (self, CH_TRIGGER_SINGLE, SND_VEH_RACER_IDLE, VOL_VEHICLEENGINE, ATTEN_NORM);
439                 }
440         }
441 #endif
442
443         // Afterburn
444         if (PHYS_INPUT_BUTTON_JUMP(player) && racer.vehicle_energy >= (autocvar_g_vehicle_racer_afterburn_cost * PHYS_INPUT_TIMELENGTH))
445         {
446 #ifdef SVQC
447                 if(time - racer.wait > 0.2)
448                         pointparticles(particleeffectnum(EFFECT_RACER_BOOSTER), self.origin - v_forward * 32, v_forward  * vlen(self.velocity), 1);
449 #endif
450
451                 racer.wait = time;
452
453                 if(cont == CONTENT_WATER || cont == CONTENT_LAVA || cont == CONTENT_SLIME)
454                 {
455                         racer.vehicle_energy -= autocvar_g_vehicle_racer_waterburn_cost * PHYS_INPUT_TIMELENGTH;
456                         df += (v_forward * autocvar_g_vehicle_racer_waterburn_speed);
457                 }
458                 else
459                 {
460                         racer.vehicle_energy -= autocvar_g_vehicle_racer_afterburn_cost * PHYS_INPUT_TIMELENGTH;
461                         df += (v_forward * autocvar_g_vehicle_racer_speed_afterburn);
462                 }
463
464 #ifdef SVQC
465                 if(racer.invincible_finished < time)
466                 {
467                         traceline(racer.origin, racer.origin - '0 0 256', MOVE_NORMAL, self);
468                         if(trace_fraction != 1.0)
469                                 pointparticles(particleeffectnum(EFFECT_SMOKE_SMALL), trace_endpos, '0 0 0', 1);
470
471                         racer.invincible_finished = time + 0.1 + (random() * 0.1);
472                 }
473
474                 if(racer.strength_finished < time)
475                 {
476                         racer.strength_finished = time + 10.922667; //soundlength("vehicles/racer_boost.wav");
477                         sound (racer.tur_head, CH_TRIGGER_SINGLE, SND_VEH_RACER_BOOST, VOL_VEHICLEENGINE, ATTEN_NORM);
478                 }
479 #endif
480         }
481         else
482         {
483                 racer.strength_finished = 0;
484                 sound (racer.tur_head, CH_TRIGGER_SINGLE, SND_Null, VOL_VEHICLEENGINE, ATTEN_NORM);
485         }
486
487         if(cont == CONTENT_WATER || cont == CONTENT_LAVA || cont == CONTENT_SLIME)
488                 racer.racer_watertime = time;
489
490         float dforce = autocvar_g_vehicle_racer_downforce;
491         if(time - racer.racer_watertime <= 3)
492                 dforce = autocvar_g_vehicle_racer_water_downforce;
493
494         df -= v_up * (vlen(racer.velocity) * dforce);
495         player.movement = racer.velocity += df * PHYS_INPUT_TIMELENGTH;
496
497 #ifdef SVQC
498         Weapon wep1 = WEP_RACER;
499         if (!forbidWeaponUse(player))
500         if (player.BUTTON_ATCK)
501         if (wep1.wr_checkammo1(wep1))
502         {
503                 string tagname = (racer.cnt)
504                     ? (racer.cnt = 0, "tag_fire1")
505                     : (racer.cnt = 1, "tag_fire2");
506                 vector org = gettaginfo(self, gettagindex(self, tagname));
507                 w_shotorg = org;
508                 w_shotdir = v_forward;
509                 // Fix z-aim (for chase mode)
510                 crosshair_trace(player);
511                 w_shotdir.z = normalize(trace_endpos - org).z * 0.5;
512                 wep1.wr_think(wep1, true, false);
513         }
514
515         if(autocvar_g_vehicle_racer_rocket_locktarget)
516         {
517                 vehicles_locktarget((1 / autocvar_g_vehicle_racer_rocket_locking_time) * frametime,
518                                                  (1 / autocvar_g_vehicle_racer_rocket_locking_releasetime) * frametime,
519                                                  autocvar_g_vehicle_racer_rocket_locked_time);
520
521                 if(self.lock_target)
522                 {
523                         if(racer.lock_strength == 1)
524                                 UpdateAuxiliaryXhair(player, real_origin(self.lock_target), '1 0 0', 0);
525                         else if(self.lock_strength > 0.5)
526                                 UpdateAuxiliaryXhair(player, real_origin(self.lock_target), '0 1 0', 0);
527                         else if(self.lock_strength < 0.5)
528                                 UpdateAuxiliaryXhair(player, real_origin(self.lock_target), '0 0 1', 0);
529                 }
530         }
531
532         if(!forbidWeaponUse(player))
533         if(time > racer.delay)
534         if(player.BUTTON_ATCK2)
535         {
536                 racer.misc_bulletcounter += 1;
537                 racer.delay = time + 0.3;
538
539                 if(racer.misc_bulletcounter == 1)
540                 {
541                         racer_fire_rocket_aim("tag_rocket_r", (racer.lock_strength == 1 && racer.lock_target) ? racer.lock_target : world);
542                         player.vehicle_ammo2 = 50;
543                 }
544                 else if(racer.misc_bulletcounter == 2)
545                 {
546                         racer_fire_rocket_aim("tag_rocket_l", (racer.lock_strength == 1 && racer.lock_target) ? racer.lock_target : world);
547                         racer.lock_strength  = 0;
548                         racer.lock_target       = world;
549                         racer.misc_bulletcounter = 0;
550                         racer.delay = time + autocvar_g_vehicle_racer_rocket_refire;
551                         racer.lip = time;
552                         player.vehicle_ammo2 = 0;
553                 }
554         }
555         else if(racer.misc_bulletcounter == 0)
556                 player.vehicle_ammo2 = 100;
557
558         player.vehicle_reload2 = bound(0, 100 * ((time - racer.lip) / (racer.delay - racer.lip)), 100);
559
560         if(racer.vehicle_flags  & VHF_SHIELDREGEN)
561                 vehicles_regen(racer.dmg_time, vehicle_shield, autocvar_g_vehicle_racer_shield, autocvar_g_vehicle_racer_shield_regen_pause, autocvar_g_vehicle_racer_shield_regen, frametime, true);
562
563         if(racer.vehicle_flags  & VHF_HEALTHREGEN)
564                 vehicles_regen(racer.dmg_time, vehicle_health, autocvar_g_vehicle_racer_health, autocvar_g_vehicle_racer_health_regen_pause, autocvar_g_vehicle_racer_health_regen, frametime, false);
565
566         if(racer.vehicle_flags  & VHF_ENERGYREGEN)
567                 vehicles_regen(racer.wait, vehicle_energy, autocvar_g_vehicle_racer_energy, autocvar_g_vehicle_racer_energy_regen_pause, autocvar_g_vehicle_racer_energy_regen, frametime, false);
568
569
570         VEHICLE_UPDATE_PLAYER(player, health, racer);
571         VEHICLE_UPDATE_PLAYER(player, energy, racer);
572
573         if(racer.vehicle_flags & VHF_HASSHIELD)
574                 VEHICLE_UPDATE_PLAYER(player, shield, racer);
575
576         player.BUTTON_ATCK = player.BUTTON_ATCK2 = 0;
577 #endif
578
579         setorigin(player,racer.origin + '0 0 32');
580         player.velocity = racer.velocity;
581
582         setself(player);
583         return 1;
584 }
585
586 void racer_think()
587 {SELFPARAM();
588         self.nextthink = time;
589
590         float pushdeltatime = time - self.lastpushtime;
591         if (pushdeltatime > 0.15) pushdeltatime = 0;
592         self.lastpushtime = time;
593         if(!pushdeltatime) return;
594
595         tracebox(self.origin, self.mins, self.maxs, self.origin - ('0 0 1' * autocvar_g_vehicle_racer_springlength), MOVE_NOMONSTERS, self);
596
597         vector df = self.velocity * -autocvar_g_vehicle_racer_friction;
598         df_z += (1 - trace_fraction) * autocvar_g_vehicle_racer_hoverpower + sin(time * 2) * (autocvar_g_vehicle_racer_springlength * 2);
599
600         float forced = autocvar_g_vehicle_racer_upforcedamper;
601
602         int cont = pointcontents(self.origin - '0 0 64');
603         if(cont == CONTENT_WATER || cont == CONTENT_LAVA || cont == CONTENT_SLIME)
604         {
605                 forced = autocvar_g_vehicle_racer_water_upforcedamper;
606                 self.velocity_z += 200;
607         }
608
609         self.velocity += df * pushdeltatime;
610         if(self.velocity_z > 0)
611                 self.velocity_z *= 1 - forced * pushdeltatime;
612
613         self.angles_x *= 1 - (autocvar_g_vehicle_racer_anglestabilizer * pushdeltatime);
614         self.angles_z *= 1 - (autocvar_g_vehicle_racer_anglestabilizer * pushdeltatime);
615
616         CSQCMODEL_AUTOUPDATE(self);
617 }
618
619 void racer_exit(float eject)
620 {SELFPARAM();
621         vector spot;
622
623         self.think        = racer_think;
624         self.nextthink  = time;
625         self.movetype   = MOVETYPE_BOUNCE;
626         sound (self.tur_head, CH_TRIGGER_SINGLE, SND_Null, VOL_VEHICLEENGINE, ATTEN_NORM);
627
628         if(!self.owner)
629                 return;
630
631         makevectors(self.angles);
632         if(eject)
633         {
634                 spot = self.origin + v_forward * 100 + '0 0 64';
635                 spot = vehicles_findgoodexit(spot);
636                 setorigin(self.owner , spot);
637                 self.owner.velocity = (v_up + v_forward * 0.25) * 750;
638                 self.owner.oldvelocity = self.owner.velocity;
639         }
640         else
641         {
642                 if(vlen(self.velocity) > 2 * autocvar_sv_maxairspeed)
643                 {
644                         self.owner.velocity = normalize(self.velocity) * autocvar_sv_maxairspeed * 2;
645                         self.owner.velocity_z += 200;
646                         spot = self.origin + v_forward * 32 + '0 0 32';
647                         spot = vehicles_findgoodexit(spot);
648                 }
649                 else
650                 {
651                         self.owner.velocity = self.velocity * 0.5;
652                         self.owner.velocity_z += 10;
653                         spot = self.origin - v_forward * 200 + '0 0 32';
654                         spot = vehicles_findgoodexit(spot);
655                 }
656                 self.owner.oldvelocity = self.owner.velocity;
657                 setorigin(self.owner , spot);
658         }
659         antilag_clear(self.owner);
660         self.owner = world;
661 }
662
663 void racer_blowup()
664 {SELFPARAM();
665         self.deadflag   = DEAD_DEAD;
666         self.vehicle_exit(VHEF_NORMAL);
667
668         RadiusDamage (self, self.enemy, autocvar_g_vehicle_racer_blowup_coredamage,
669                                         autocvar_g_vehicle_racer_blowup_edgedamage,
670                                         autocvar_g_vehicle_racer_blowup_radius, world, world,
671                                         autocvar_g_vehicle_racer_blowup_forceintensity,
672                                         DEATH_VH_WAKI_DEATH, world);
673
674         self.nextthink  = time + autocvar_g_vehicle_racer_respawntime;
675         self.think        = vehicles_spawn;
676         self.movetype   = MOVETYPE_NONE;
677         self.effects    = EF_NODRAW;
678
679         self.colormod  = '0 0 0';
680         self.avelocity = '0 0 0';
681         self.velocity  = '0 0 0';
682
683         setorigin(self, self.pos1);
684 }
685
686 void racer_blowup_think()
687 {SELFPARAM();
688         self.nextthink = time;
689
690         if(time >= self.delay)
691                 racer_blowup();
692
693         CSQCMODEL_AUTOUPDATE(self);
694 }
695
696 void racer_deadtouch()
697 {SELFPARAM();
698         self.avelocity_x *= 0.7;
699         self.cnt -= 1;
700         if(self.cnt <= 0)
701                 racer_blowup();
702 }
703
704 void spawnfunc_vehicle_racer()
705 {SELFPARAM();
706         if(!autocvar_g_vehicle_racer) { remove(self); return; }
707         if(!vehicle_initialize(VEH_RACER, false)) { remove(self); return; }
708 }
709
710 #endif // SVQC
711
712 #ifdef CSQC
713 #if 0
714 void racer_draw()
715 {SELFPARAM();
716         float pushdeltatime = time - self.lastpushtime;
717         if (pushdeltatime > 0.15) pushdeltatime = 0;
718         self.lastpushtime = time;
719         if(!pushdeltatime) return;
720
721         tracebox(self.move_origin, self.mins, self.maxs, self.move_origin - ('0 0 1' * getstatf(STAT_VEH_RACER_SPRINGLENGTH)), MOVE_NOMONSTERS, self);
722
723         vector df = self.move_velocity * -getstatf(STAT_VEH_RACER_FRICTION);
724         df_z += (1 - trace_fraction) * getstatf(STAT_VEH_RACER_HOVERPOWER) + sin(time * 2) * (getstatf(STAT_VEH_RACER_SPRINGLENGTH) * 2);
725
726         float forced = getstatf(STAT_VEH_RACER_UPFORCEDAMPER);
727
728         int cont = pointcontents(self.move_origin - '0 0 64');
729         if(cont == CONTENT_WATER || cont == CONTENT_LAVA || cont == CONTENT_SLIME)
730         {
731                 forced = getstatf(STAT_VEH_RACER_WATER_UPFORCEDAMPER);
732                 self.move_velocity_z += 200;
733         }
734
735         self.move_velocity += df * pushdeltatime;
736         if(self.move_velocity_z > 0)
737                 self.move_velocity_z *= 1 - forced * pushdeltatime;
738
739         self.move_angles_x *= 1 - (getstatf(STAT_VEH_RACER_ANGLESTABILIZER) * pushdeltatime);
740         self.move_angles_z *= 1 - (getstatf(STAT_VEH_RACER_ANGLESTABILIZER) * pushdeltatime);
741
742         Movetype_Physics_MatchServer(false);
743 }
744 #endif
745 #endif
746
747                 METHOD(Racer, vr_impact, bool(Racer thisveh))
748                 {
749                 #ifdef SVQC
750                         if(autocvar_g_vehicle_racer_bouncepain)
751                                 vehicles_impact(autocvar_g_vehicle_racer_bouncepain_x, autocvar_g_vehicle_racer_bouncepain_y, autocvar_g_vehicle_racer_bouncepain_z);
752                 #endif
753                         return true;
754                 }
755
756                 METHOD(Racer, vr_enter, bool(Racer thisveh))
757                 {
758                 #ifdef SVQC
759                         self.movetype = MOVETYPE_BOUNCE;
760                         self.owner.vehicle_health = (self.vehicle_health / autocvar_g_vehicle_racer_health)  * 100;
761                         self.owner.vehicle_shield = (self.vehicle_shield / autocvar_g_vehicle_racer_shield)  * 100;
762
763                         if(self.owner.flagcarried)
764                            setorigin(self.owner.flagcarried, '-190 0 96');
765                 #elif defined(CSQC)
766
767                         self.move_movetype = MOVETYPE_BOUNCE;
768                 #endif
769
770                         return true;
771                 }
772
773                 METHOD(Racer, vr_spawn, bool(Racer thisveh))
774                 {
775                 #ifdef SVQC
776                         if(self.scale != 0.5)
777                         {
778                                 if(autocvar_g_vehicle_racer_hovertype != 0)
779                                         racer_force_from_tag = vehicles_force_fromtag_maglev;
780                                 else
781                                         racer_force_from_tag = vehicles_force_fromtag_hover;
782
783                                 // FIXME: this be hakkz, fix the models insted (scale body, add tag_viewport to the hudmodel).
784                                 self.scale = 0.5;
785                                 setattachment(self.vehicle_hudmodel, self, "");
786                                 setattachment(self.vehicle_viewport, self, "tag_viewport");
787
788                                 self.mass                          = 900;
789                         }
790
791                         self.think                = racer_think;
792                         self.nextthink    = time;
793                         self.vehicle_health = autocvar_g_vehicle_racer_health;
794                         self.vehicle_shield = autocvar_g_vehicle_racer_shield;
795
796                         self.movetype     = MOVETYPE_TOSS;
797                         self.solid                = SOLID_SLIDEBOX;
798                         self.delay                = time;
799                         self.scale                = 0.5;
800
801                         self.PlayerPhysplug = racer_frame;
802
803                         self.bouncefactor = autocvar_g_vehicle_racer_bouncefactor;
804                         self.bouncestop = autocvar_g_vehicle_racer_bouncestop;
805                         self.damageforcescale = 0.5;
806                         self.vehicle_health = autocvar_g_vehicle_racer_health;
807                         self.vehicle_shield = autocvar_g_vehicle_racer_shield;
808                 #endif
809                         return true;
810                 }
811
812                 METHOD(Racer, vr_death, bool(Racer thisveh))
813                 {
814                 #ifdef SVQC
815                         self.SendEntity         = func_null; // stop networking this racer (for now)
816                         self.health                     = 0;
817                         self.event_damage       = func_null;
818                         self.solid                      = SOLID_CORPSE;
819                         self.takedamage         = DAMAGE_NO;
820                         self.deadflag           = DEAD_DYING;
821                         self.movetype           = MOVETYPE_BOUNCE;
822                         self.wait                       = time;
823                         self.delay                      = 2 + time + random() * 3;
824                         self.cnt                        = 1 + random() * 2;
825                         self.touch                      = racer_deadtouch;
826
827                         Send_Effect(EFFECT_EXPLOSION_MEDIUM, self.origin, '0 0 0', 1);
828
829                         if(random() < 0.5)
830                                 self.avelocity_z = 32;
831                         else
832                                 self.avelocity_z = -32;
833
834                         self.avelocity_x = -vlen(self.velocity) * 0.2;
835                         self.velocity += '0 0 700';
836                         self.colormod = '-0.5 -0.5 -0.5';
837
838                         self.think = racer_blowup_think;
839                         self.nextthink = time;
840                 #endif
841                         return true;
842                 }
843
844 #ifdef CSQC
845                 METHOD(Racer, vr_hud, bool(Racer thisveh))
846                 {
847                         Vehicles_drawHUD(VEH_RACER.m_icon, "vehicle_racer_weapon1", "vehicle_racer_weapon2",
848                                                          "vehicle_icon_ammo1", autocvar_hud_progressbar_vehicles_ammo1_color,
849                                                          "vehicle_icon_ammo2", autocvar_hud_progressbar_vehicles_ammo2_color,
850                                                          vCROSS_GUIDE);
851                         return true;
852                 }
853 #endif
854                 METHOD(Racer, vr_setup, bool(Racer thisveh))
855                 {
856                 #ifdef SVQC
857                         self.vehicle_exit = racer_exit;
858                 #endif
859
860                 #ifdef SVQC
861                         // we have no need to network energy
862                         if(autocvar_g_vehicle_racer_energy)
863                         if(autocvar_g_vehicle_racer_energy_regen)
864                                 self.vehicle_flags |= VHF_ENERGYREGEN;
865
866                         if(autocvar_g_vehicle_racer_shield)
867                                 self.vehicle_flags |= VHF_HASSHIELD;
868
869                         if(autocvar_g_vehicle_racer_shield_regen)
870                                 self.vehicle_flags |= VHF_SHIELDREGEN;
871
872                         if(autocvar_g_vehicle_racer_health_regen)
873                                 self.vehicle_flags |= VHF_HEALTHREGEN;
874
875                         self.respawntime = autocvar_g_vehicle_racer_respawntime;
876                         self.vehicle_health = autocvar_g_vehicle_racer_health;
877                         self.vehicle_shield = autocvar_g_vehicle_racer_shield;
878                         self.max_health = self.vehicle_health;
879                 #endif
880
881                 #ifdef CSQC
882                         AuxiliaryXhair[0].axh_image = vCROSS_LOCK; // Rocket
883                 #endif
884                         return true;
885                 }
886
887                 METHOD(Racer, vr_precache, bool(Racer thisveh))
888                 {
889                         return true;
890                 }
891
892 #endif // REGISTER_VEHICLE