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