2bde9a533a649dce2358dd1d9b8f58bdc7fef4bf
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / vehicles / racer.qc
1 #define RACER_MIN '-120 -120 -40'
2 #define RACER_MAX '120 120 40'
3
4 #ifdef SVQC
5 void racer_exit(float eject);
6 void racer_enter();
7
8 // Auto cvars
9 float autocvar_g_vehicle_racer_speed_afterburn;
10 float autocvar_g_vehicle_racer_afterburn_cost;
11
12 float autocvar_g_vehicle_racer_anglestabilizer;
13 float autocvar_g_vehicle_racer_downforce;
14
15 float autocvar_g_vehicle_racer_speed_forward;
16 float autocvar_g_vehicle_racer_speed_strafe;
17 float autocvar_g_vehicle_racer_springlength;
18 float autocvar_g_vehicle_racer_upforcedamper;
19 float autocvar_g_vehicle_racer_friction;
20
21 float autocvar_g_vehicle_racer_hovertype;
22 float autocvar_g_vehicle_racer_hoverpower;
23
24 float autocvar_g_vehicle_racer_turnroll;
25 float autocvar_g_vehicle_racer_turnspeed;
26 float autocvar_g_vehicle_racer_pitchspeed;
27
28 float autocvar_g_vehicle_racer_energy;
29 float autocvar_g_vehicle_racer_energy_regen;
30 float autocvar_g_vehicle_racer_energy_regen_pause;
31
32 float autocvar_g_vehicle_racer_health;
33 float autocvar_g_vehicle_racer_health_regen;
34 float autocvar_g_vehicle_racer_health_regen_pause;
35
36 float autocvar_g_vehicle_racer_shield;
37 float autocvar_g_vehicle_racer_shield_regen;
38 float autocvar_g_vehicle_racer_shield_regen_pause;
39
40 float autocvar_g_vehicle_racer_cannon_cost;
41 float autocvar_g_vehicle_racer_cannon_damage;
42 float autocvar_g_vehicle_racer_cannon_radius;
43 float autocvar_g_vehicle_racer_cannon_refire;
44 float autocvar_g_vehicle_racer_cannon_speed;
45 float autocvar_g_vehicle_racer_cannon_spread;
46 float autocvar_g_vehicle_racer_cannon_force;
47
48 float autocvar_g_vehicle_racer_rocket_accel;
49 float autocvar_g_vehicle_racer_rocket_damage;
50 float autocvar_g_vehicle_racer_rocket_radius;
51 float autocvar_g_vehicle_racer_rocket_force;
52 float autocvar_g_vehicle_racer_rocket_refire;
53 float autocvar_g_vehicle_racer_rocket_speed;
54 float autocvar_g_vehicle_racer_rocket_turnrate;
55
56 float autocvar_g_vehicle_racer_rocket_locktarget;
57 float autocvar_g_vehicle_racer_rocket_locking_time;
58 float autocvar_g_vehicle_racer_rocket_locking_releasetime;
59 float autocvar_g_vehicle_racer_rocket_locked_time;
60 float autocvar_g_vehicle_racer_rocket_locked_maxangle;
61 float autocvar_g_vehicle_racer_rocket_climbspeed;
62
63 float autocvar_g_vehicle_racer_respawntime;
64
65 float autocvar_g_vehicle_racer_blowup_radius;
66 float autocvar_g_vehicle_racer_blowup_coredamage;
67 float autocvar_g_vehicle_racer_blowup_edgedamage;
68 float autocvar_g_vehicle_racer_blowup_forceintensity;
69
70 float autocvar_g_vehicle_racer_bouncefactor;
71 float autocvar_g_vehicle_racer_bouncestop;
72 vector autocvar_g_vehicle_racer_bouncepain;
73
74 var vector racer_force_from_tag(string tag_name, float spring_length, float max_power);
75
76 void racer_align4point(float _delta)
77 {
78     vector push_vector;
79     float fl_push, fr_push, bl_push, br_push;
80
81     push_vector  = racer_force_from_tag("tag_engine_fr", autocvar_g_vehicle_racer_springlength, autocvar_g_vehicle_racer_hoverpower);
82     fr_push      = force_fromtag_normpower;
83     //vehicles_sweap_collision(force_fromtag_origin, self.velocity, _delta, v_add, autocvar_g_vehicle_racer_collision_multiplier);
84
85     push_vector += racer_force_from_tag("tag_engine_fl", autocvar_g_vehicle_racer_springlength, autocvar_g_vehicle_racer_hoverpower);
86     fl_push      = force_fromtag_normpower;
87     //vehicles_sweap_collision(force_fromtag_origin, self.velocity, _delta, v_add, autocvar_g_vehicle_racer_collision_multiplier);
88
89     push_vector += racer_force_from_tag("tag_engine_br", autocvar_g_vehicle_racer_springlength, autocvar_g_vehicle_racer_hoverpower);
90     br_push      = force_fromtag_normpower;
91     //vehicles_sweap_collision(force_fromtag_origin, self.velocity, _delta, v_add, autocvar_g_vehicle_racer_collision_multiplier);
92
93     push_vector += racer_force_from_tag("tag_engine_bl", autocvar_g_vehicle_racer_springlength, autocvar_g_vehicle_racer_hoverpower);
94     bl_push      = force_fromtag_normpower;
95     //vehicles_sweap_collision(force_fromtag_origin, self.velocity, _delta, v_add, autocvar_g_vehicle_racer_collision_multiplier);
96
97    self.velocity += push_vector * _delta;
98
99     // Anti ocilation
100     if(self.velocity_z > 0)
101         self.velocity_z *= 1 - autocvar_g_vehicle_racer_upforcedamper * _delta;
102
103     push_vector_x =  (fl_push - bl_push);
104     push_vector_x += (fr_push - br_push);
105     push_vector_x *= 360;
106
107     push_vector_z = (fr_push - fl_push);
108     push_vector_z += (br_push - bl_push);
109     push_vector_z *= 360;
110
111     // Apply angle diffrance
112     self.angles_z += push_vector_z * _delta;
113     self.angles_x += push_vector_x * _delta;
114
115     // Apply stabilizer
116     self.angles_x *= 1 - (autocvar_g_vehicle_racer_anglestabilizer * _delta);
117     self.angles_z *= 1 - (autocvar_g_vehicle_racer_anglestabilizer * _delta);
118 }
119
120 void racer_fire_cannon(string tagname)
121 {
122     vector v;
123     entity bolt;
124
125     v = gettaginfo(self, gettagindex(self, tagname));
126     bolt = vehicles_projectile("wakizashi_gun_muzzleflash", "weapons/lasergun_fire.wav",
127                            v, normalize(v_forward + randomvec() * autocvar_g_vehicle_racer_cannon_spread) * autocvar_g_vehicle_racer_cannon_speed,
128                            autocvar_g_vehicle_racer_cannon_damage, autocvar_g_vehicle_racer_cannon_radius, autocvar_g_vehicle_racer_cannon_force,  0,
129                            DEATH_WAKIGUN, PROJECTILE_WAKICANNON, 0, TRUE, TRUE);
130
131         // Fix z-aim (for chase mode)
132     v = normalize(trace_endpos - bolt.origin);
133     v_forward_z = v_z * 0.5;
134     bolt.velocity = v_forward * autocvar_g_vehicle_racer_cannon_speed;
135 }
136
137 void racer_rocket_groundhugger()
138 {
139     vector olddir, newdir;
140     float oldvel, newvel;
141
142     self.nextthink  = time;
143
144     if(self.owner.deadflag != DEAD_NO || self.cnt < time)
145     {
146         self.use();
147         return;
148     }
149
150     if not (self.realowner.vehicle)
151     {
152         UpdateCSQCProjectile(self);
153         return;
154     }
155
156     olddir = normalize(self.velocity);
157     oldvel = vlen(self.velocity);
158     newvel = oldvel + self.lip;
159
160     tracebox(self.origin, self.mins, self.maxs, self.origin + olddir * 64, MOVE_WORLDONLY,self);
161     if(trace_fraction <= 0.5)
162     {
163         // Hitting somethign soon, just speed ahead
164         self.velocity = olddir * newvel;
165         UpdateCSQCProjectile(self);
166         return;
167     }
168
169     traceline(trace_endpos, trace_endpos - '0 0 64', MOVE_NORMAL, self);
170     if(trace_fraction != 1.0)
171     {
172         newdir = normalize(trace_endpos + '0 0 64' - self.origin) * autocvar_g_vehicle_racer_rocket_turnrate;
173         self.velocity = normalize(olddir + newdir) * newvel;
174     }
175     else
176     {
177         self.velocity = olddir * newvel;
178         self.velocity_z -= 1600 * sys_frametime; // 2x grav looks better for this one
179     }
180
181     UpdateCSQCProjectile(self);
182     return;
183 }
184
185 void racer_rocket_tracker()
186 {
187     vector olddir, newdir;
188     float oldvel, newvel;
189
190     self.nextthink  = time;
191
192     if (self.owner.deadflag != DEAD_NO || self.cnt < time)
193     {
194         self.use();
195         return;
196     }
197
198     if not (self.realowner.vehicle)
199     {
200         UpdateCSQCProjectile(self);
201         return;
202     }
203
204     olddir = normalize(self.velocity);
205     oldvel = vlen(self.velocity);
206     newvel = oldvel + self.lip;
207     makevectors(vectoangles(olddir));
208
209         float time_to_impact = min(vlen(self.enemy.origin - self.origin) / vlen(self.velocity), 1);
210         vector predicted_origin = self.enemy.origin + self.enemy.velocity * time_to_impact;
211
212     traceline(self.origin, self.origin + v_forward * 64 - '0 0 32', MOVE_NORMAL, self);
213     newdir = normalize(predicted_origin - self.origin);
214
215     //vector
216         float height_diff = predicted_origin_z - self.origin_z;
217
218     if(vlen(newdir - v_forward) > autocvar_g_vehicle_racer_rocket_locked_maxangle)
219     {
220         //bprint("Target lost!\n");
221         //dprint("OF:", ftos(vlen(newdir - v_forward)), "\n");
222         self.think = racer_rocket_groundhugger;
223         return;
224     }
225
226     if(trace_fraction != 1.0 && trace_ent != self.enemy)
227         newdir_z += 16 * sys_frametime;
228
229     self.velocity = normalize(olddir + newdir * autocvar_g_vehicle_racer_rocket_turnrate) * newvel;
230     self.velocity_z -= 800 * sys_frametime;
231     self.velocity_z += max(height_diff, autocvar_g_vehicle_racer_rocket_climbspeed) * sys_frametime ;
232
233     UpdateCSQCProjectile(self);
234     return;
235 }
236
237 void racer_fire_rocket(string tagname, entity trg)
238 {
239     vector v = gettaginfo(self, gettagindex(self, tagname));
240     entity rocket = rocket = vehicles_projectile("wakizashi_rocket_launch", "weapons/rocket_fire.wav",
241                            v, v_forward * autocvar_g_vehicle_racer_rocket_speed,
242                            autocvar_g_vehicle_racer_rocket_damage, autocvar_g_vehicle_racer_rocket_radius, autocvar_g_vehicle_racer_rocket_force, 3,
243                            DEATH_WAKIROCKET, PROJECTILE_WAKIROCKET, 20, FALSE, FALSE);
244
245     rocket.lip              = autocvar_g_vehicle_racer_rocket_accel * sys_frametime;
246     rocket.wait             = autocvar_g_vehicle_racer_rocket_turnrate;
247     rocket.nextthink        = time;
248     rocket.enemy            = trg;
249     rocket.cnt              = time + 15;
250
251     if(trg)
252         rocket.think            = racer_rocket_tracker;
253     else
254         rocket.think            = racer_rocket_groundhugger;
255 }
256
257 float racer_frame()
258 {
259     entity player, racer;
260     vector df;
261     float ftmp;
262
263         if(intermission_running)
264                 return 1;
265
266     player  = self;
267     racer   = self.vehicle;
268     self    = racer;
269
270     player.BUTTON_ZOOM = player.BUTTON_CROUCH = 0;
271
272     vehicles_painframe();
273
274     if(racer.deadflag != DEAD_NO)
275     {
276         self = player;
277         player.BUTTON_ATCK = player.BUTTON_ATCK2 = 0;
278         return 1;
279     }
280
281     racer_align4point(frametime);
282
283     crosshair_trace(player);
284
285     racer.angles_x *= -1;
286
287     // Yaw
288     ftmp = autocvar_g_vehicle_racer_turnspeed * frametime;
289     ftmp = bound(-ftmp, shortangle_f(player.v_angle_y - racer.angles_y, racer.angles_y), ftmp);
290     racer.angles_y = anglemods(racer.angles_y + ftmp);
291
292     // Roll
293     racer.angles_z += -ftmp * autocvar_g_vehicle_racer_turnroll * frametime;
294
295     // Pitch
296     ftmp = autocvar_g_vehicle_racer_pitchspeed  * frametime;
297     ftmp = bound(-ftmp, shortangle_f(player.v_angle_x - racer.angles_x, racer.angles_x), ftmp);
298     racer.angles_x = bound(-30, anglemods(racer.angles_x + ftmp), 30);
299
300     makevectors(racer.angles);
301     racer.angles_x *= -1;
302
303     //ftmp = racer.velocity_z;
304     df = racer.velocity * -autocvar_g_vehicle_racer_friction;
305     //racer.velocity_z = ftmp;
306
307     if(vlen(player.movement) != 0)
308     {
309         if(player.movement_x)
310             df += v_forward * ((player.movement_x > 0) ? autocvar_g_vehicle_racer_speed_forward : -autocvar_g_vehicle_racer_speed_forward);
311
312         if(player.movement_y)
313             df += v_right * ((player.movement_y > 0) ? autocvar_g_vehicle_racer_speed_strafe : -autocvar_g_vehicle_racer_speed_strafe);
314
315         if(self.sound_nexttime < time || self.sounds != 1)
316         {
317             self.sounds = 1;
318             self.sound_nexttime = time + 10.922667; //soundlength("vehicles/racer_move.wav");
319             sound (self, CH_TRIGGER_SINGLE, "vehicles/racer_move.wav", VOL_VEHICLEENGINE, ATTN_NORM);
320         }
321     }
322     else
323     {
324         if(self.sound_nexttime < time || self.sounds != 0)
325         {
326             self.sounds = 0;
327             self.sound_nexttime = time + 11.888604; //soundlength("vehicles/racer_idle.wav");
328             sound (self, CH_TRIGGER_SINGLE, "vehicles/racer