Remove SELFPARAM() from .think and .touch
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / vehicles / vehicle / bumblebee.qc
1 #ifndef VEHICLE_BUMBLEBEE
2 #define VEHICLE_BUMBLEBEE
3 #include "bumblebee.qh"
4
5 #include "bumblebee_weapons.qh"
6
7 CLASS(Bumblebee, Vehicle)
8 /* spawnflags */ ATTRIB(Bumblebee, spawnflags, int, VHF_DMGSHAKE);
9 /* mins       */ ATTRIB(Bumblebee, mins, vector, '-245 -130 -130');
10 /* maxs       */ ATTRIB(Bumblebee, maxs, vector, '230 130 130');
11 /* view offset*/ ATTRIB(Bumblebee, view_ofs, vector, '0 0 300');
12 /* view dist  */ ATTRIB(Bumblebee, height, float, 450);
13 /* model          */ ATTRIB(Bumblebee, mdl, string, "models/vehicles/bumblebee_body.dpm");
14 /* model          */ ATTRIB(Bumblebee, model, string, "models/vehicles/bumblebee_body.dpm");
15 /* head_model */ ATTRIB(Bumblebee, head_model, string, "");
16 /* hud_model  */ ATTRIB(Bumblebee, hud_model, string, "models/vehicles/spiderbot_cockpit.dpm");
17 /* tags       */ ATTRIB(Bumblebee, tag_head, string, "");
18 /* tags       */ ATTRIB(Bumblebee, tag_hud, string, "");
19 /* tags       */ ATTRIB(Bumblebee, tag_view, string, "tag_viewport");
20 /* netname    */ ATTRIB(Bumblebee, netname, string, "bumblebee");
21 /* fullname   */ ATTRIB(Bumblebee, vehicle_name, string, _("Bumblebee"));
22 /* icon       */ ATTRIB(Bumblebee, m_icon, string, "vehicle_bumble");
23 ENDCLASS(Bumblebee)
24 REGISTER_VEHICLE(BUMBLEBEE, NEW(Bumblebee));
25
26 #ifndef MENUQC
27         MODEL(VEH_BUMBLEBEE_GUNCOCKPIT, "models/vehicles/wakizashi_cockpit.dpm");
28 #endif
29
30 #endif
31
32 #ifdef IMPLEMENTATION
33
34 const float BRG_SETUP = 2;
35 const float BRG_START = 4;
36 const float BRG_END = 8;
37
38 #ifdef SVQC
39 float autocvar_g_vehicle_bumblebee_respawntime = 60;
40
41 float autocvar_g_vehicle_bumblebee_speed_forward = 350;
42 float autocvar_g_vehicle_bumblebee_speed_strafe = 350;
43 float autocvar_g_vehicle_bumblebee_speed_up = 350;
44 float autocvar_g_vehicle_bumblebee_speed_down = 350;
45 float autocvar_g_vehicle_bumblebee_turnspeed = 120;
46 float autocvar_g_vehicle_bumblebee_pitchspeed = 60;
47 float autocvar_g_vehicle_bumblebee_pitchlimit = 60;
48 float autocvar_g_vehicle_bumblebee_friction = 0.5;
49
50 float autocvar_g_vehicle_bumblebee_energy = 500;
51 float autocvar_g_vehicle_bumblebee_energy_regen = 50;
52 float autocvar_g_vehicle_bumblebee_energy_regen_pause = 1;
53
54 float autocvar_g_vehicle_bumblebee_health = 1000;
55 float autocvar_g_vehicle_bumblebee_health_regen = 65;
56 float autocvar_g_vehicle_bumblebee_health_regen_pause = 10;
57
58 float autocvar_g_vehicle_bumblebee_shield = 400;
59 float autocvar_g_vehicle_bumblebee_shield_regen = 150;
60 float autocvar_g_vehicle_bumblebee_shield_regen_pause = 0.75;
61
62 float autocvar_g_vehicle_bumblebee_cannon_ammo = 100;
63 float autocvar_g_vehicle_bumblebee_cannon_ammo_regen = 100;
64 float autocvar_g_vehicle_bumblebee_cannon_ammo_regen_pause = 1;
65
66 float autocvar_g_vehicle_bumblebee_cannon_lock = 0;
67
68 float autocvar_g_vehicle_bumblebee_cannon_turnspeed = 160;
69 float autocvar_g_vehicle_bumblebee_cannon_pitchlimit_down = 60;
70 float autocvar_g_vehicle_bumblebee_cannon_pitchlimit_up = 60;
71 float autocvar_g_vehicle_bumblebee_cannon_turnlimit_in = 20;
72 float autocvar_g_vehicle_bumblebee_cannon_turnlimit_out = 80;
73
74
75 float autocvar_g_vehicle_bumblebee_raygun_turnspeed = 180;
76 float autocvar_g_vehicle_bumblebee_raygun_pitchlimit_down = 20;
77 float autocvar_g_vehicle_bumblebee_raygun_pitchlimit_up = 5;
78 float autocvar_g_vehicle_bumblebee_raygun_turnlimit_sides = 35;
79
80 bool autocvar_g_vehicle_bumblebee_raygun = false;
81 float autocvar_g_vehicle_bumblebee_raygun_range = 2048;
82 float autocvar_g_vehicle_bumblebee_raygun_dps = 250;
83 float autocvar_g_vehicle_bumblebee_raygun_aps = 100;
84 float autocvar_g_vehicle_bumblebee_raygun_fps = 100;
85
86 float autocvar_g_vehicle_bumblebee_healgun_hps = 150;
87 float autocvar_g_vehicle_bumblebee_healgun_hmax = 100;
88 float autocvar_g_vehicle_bumblebee_healgun_aps = 75;
89 float autocvar_g_vehicle_bumblebee_healgun_amax = 100;
90 float autocvar_g_vehicle_bumblebee_healgun_sps = 100;
91 float autocvar_g_vehicle_bumblebee_healgun_locktime = 2.5;
92
93 float autocvar_g_vehicle_bumblebee_blowup_radius = 500;
94 float autocvar_g_vehicle_bumblebee_blowup_coredamage = 500;
95 float autocvar_g_vehicle_bumblebee_blowup_edgedamage = 100;
96 float autocvar_g_vehicle_bumblebee_blowup_forceintensity = 600;
97 vector autocvar_g_vehicle_bumblebee_bouncepain = '1 100 200';
98
99 bool autocvar_g_vehicle_bumblebee = true;
100
101 bool bumblebee_gunner_frame(entity this)
102 {
103         entity vehic = this.vehicle.owner;
104         entity gun = this.vehicle;
105         return = true;
106
107         // this isn't technically a vehicle (yet), let's not do frame functions on it (yet)
108         //vehicles_frame(gun, player);
109
110         vehic.solid = SOLID_NOT;
111         //setorigin(this, vehic.origin);
112         this.velocity = vehic.velocity;
113
114         float _in, _out;
115         vehic.angles_x *= -1;
116         makevectors(vehic.angles);
117         vehic.angles_x *= -1;
118         if(gun == vehic.gun1)
119         {
120                 _in = autocvar_g_vehicle_bumblebee_cannon_turnlimit_in;
121                 _out = autocvar_g_vehicle_bumblebee_cannon_turnlimit_out;
122                 setorigin(this, vehic.origin + v_up * -16 + v_forward * -16 + v_right * 128);
123         }
124         else
125         {
126                 _in = autocvar_g_vehicle_bumblebee_cannon_turnlimit_out;
127                 _out = autocvar_g_vehicle_bumblebee_cannon_turnlimit_in;
128                 setorigin(this, vehic.origin + v_up * -16 + v_forward * -16 + v_right * -128);
129         }
130
131         crosshair_trace(this);
132         vector _ct = trace_endpos;
133         vector ad;
134
135         if(autocvar_g_vehicle_bumblebee_cannon_lock)
136         {
137                 if(gun.lock_time < time)
138                         gun.enemy = world;
139
140                 if(trace_ent)
141                         if(trace_ent.movetype)
142                                 if(trace_ent.takedamage)
143                                         if(!IS_DEAD(trace_ent) && !STAT(FROZEN, trace_ent))
144                                         {
145                                                 if(DIFF_TEAM(trace_ent, this))
146                                                 {
147                                                         gun.enemy = trace_ent;
148                                                         gun.lock_time = time + 5;
149                                                 }
150                                         }
151         }
152
153         if(gun.enemy)
154         {
155                 float distance, impact_time;
156
157                 vector vf = real_origin(gun.enemy);
158                 vector _vel = gun.enemy.velocity;
159                 if(gun.enemy.movetype == MOVETYPE_WALK)
160                         _vel.z *= 0.1;
161
162
163                 ad = vf;
164                 distance = vlen(ad - this.origin);
165                 impact_time = distance / autocvar_g_vehicle_bumblebee_cannon_speed;
166                 ad = vf + _vel * impact_time;
167                 trace_endpos = ad;
168
169
170                 UpdateAuxiliaryXhair(this, ad, '1 0 1', 1);
171                 vehicle_aimturret(vehic, trace_endpos, gun, "fire",
172                                                   autocvar_g_vehicle_bumblebee_cannon_pitchlimit_down * -1, autocvar_g_vehicle_bumblebee_cannon_pitchlimit_up,
173                                                   _out * -1,  _in,  autocvar_g_vehicle_bumblebee_cannon_turnspeed);
174
175         }
176         else
177                 vehicle_aimturret(vehic, _ct, gun, "fire",
178                                                   autocvar_g_vehicle_bumblebee_cannon_pitchlimit_down * -1, autocvar_g_vehicle_bumblebee_cannon_pitchlimit_up,
179                                                   _out * -1,  _in,  autocvar_g_vehicle_bumblebee_cannon_turnspeed);
180
181         if(!forbidWeaponUse(this))
182         if(PHYS_INPUT_BUTTON_ATCK(this))
183                 if(time > gun.attack_finished_single[0])
184                         if(gun.vehicle_energy >= autocvar_g_vehicle_bumblebee_cannon_cost)
185                         {
186                                 gun.vehicle_energy -= autocvar_g_vehicle_bumblebee_cannon_cost;
187                                 bumblebee_fire_cannon(vehic, gun, "fire", this);
188                                 gun.delay = time;
189                                 gun.attack_finished_single[0] = time + autocvar_g_vehicle_bumblebee_cannon_refire;
190                         }
191
192         VEHICLE_UPDATE_PLAYER(this, vehic, health, bumblebee);
193
194         if(vehic.vehicle_flags & VHF_HASSHIELD)
195                 VEHICLE_UPDATE_PLAYER(this, vehic, shield, bumblebee);
196
197         ad = gettaginfo(gun, gettagindex(gun, "fire"));
198         traceline(ad, ad + v_forward * MAX_SHOT_DISTANCE, MOVE_NORMAL, gun);
199
200         UpdateAuxiliaryXhair(this, trace_endpos, ('1 0 0' * this.vehicle_reload1) + ('0 1 0' *(1 - this.vehicle_reload1)), 0);
201
202         if(vehic.owner)
203                 UpdateAuxiliaryXhair(vehic.owner, trace_endpos, ('1 0 0' * this.vehicle_reload1) + ('0 1 0' *(1 - this.vehicle_reload1)), ((this == vehic.gunner1) ? 1 : 2));
204
205         vehic.solid = SOLID_BBOX;
206         PHYS_INPUT_BUTTON_ATCK(this) = PHYS_INPUT_BUTTON_ATCK2(this) = PHYS_INPUT_BUTTON_CROUCH(this) = false;
207         this.vehicle_energy = (gun.vehicle_energy / autocvar_g_vehicle_bumblebee_cannon_ammo) * 100;
208 }
209
210 vector bumblebee_gunner_findgoodexit(vector prefer_spot, entity gunner, entity player)
211 {
212         //vector exitspot;
213         float mysize;
214
215         tracebox(gunner.origin + '0 0 32', STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), prefer_spot, MOVE_NORMAL, player);
216         if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
217                 return prefer_spot;
218
219         mysize = 1.5 * vlen(STAT(PL_MAX, NULL) - STAT(PL_MIN, NULL)); // can't use gunner's size, as they don't have a size
220         float i;
221         vector v, v2;
222         v2 = 0.5 * (gunner.absmin + gunner.absmax);
223         for(i = 0; i < 100; ++i)
224         {
225                 v = randomvec();
226                 v_z = 0;
227                 v = v2 + normalize(v) * mysize;
228                 tracebox(v2, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), v, MOVE_NORMAL, player);
229                 if(trace_fraction == 1.0 && !trace_startsolid && !trace_allsolid)
230                         return v;
231         }
232
233         return prefer_spot; // this should be considered a fallback?!
234 }
235
236 void bumblebee_gunner_exit(int _exitflag)
237 {SELFPARAM();
238         entity player = self;
239         entity gunner = player.vehicle;
240         entity vehic = gunner.owner;
241
242         if(IS_REAL_CLIENT(player))
243         {
244                 msg_entity = player;
245                 WriteByte(MSG_ONE, SVC_SETVIEWPORT);
246                 WriteEntity(MSG_ONE, player);
247
248                 WriteByte(MSG_ONE, SVC_SETVIEWANGLES);
249                 WriteAngle(MSG_ONE, 0);
250                 WriteAngle(MSG_ONE, vehic.angles.y);
251                 WriteAngle(MSG_ONE, 0);
252         }
253
254         CSQCVehicleSetup(player, HUD_NORMAL);
255         setsize(player, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
256
257         player.takedamage     = DAMAGE_AIM;
258         player.solid          = SOLID_SLIDEBOX;
259         player.movetype       = MOVETYPE_WALK;
260         player.effects       &= ~EF_NODRAW;
261         player.alpha          = 1;
262         player.PlayerPhysplug = func_null;
263         player.view_ofs       = STAT(PL_VIEW_OFS, NULL);
264         player.event_damage   = PlayerDamage;
265         player.hud            = HUD_NORMAL;
266         player.teleportable       = TELEPORT_NORMAL;
267         PS(player).m_switchweapon = gunner.m_switchweapon;
268         player.vehicle_enter_delay = time + 2;
269
270         fixedmakevectors(vehic.angles);
271
272         if(player == vehic.gunner1) { vehic.gunner1 = world; }
273         if(player == vehic.gunner2) { vehic.gunner2 = world; v_right *= -1; }
274
275         vector spot = real_origin(gunner);
276         spot = spot + v_up * 128 + v_forward * 300 + v_right * 150;
277         spot = bumblebee_gunner_findgoodexit(spot, gunner, player);
278
279         // TODO: figure a way to move player out of the gunner
280
281         player.velocity = 0.75 * vehic.velocity + normalize(spot - vehic.origin) * 200;
282         player.velocity_z += 10;
283
284         gunner.phase = time + 5;
285         gunner.vehicle_hudmodel.viewmodelforclient = gunner;
286
287         MUTATOR_CALLHOOK(VehicleExit, player, gunner);
288
289         player.vehicle = world;
290 }
291
292 bool bumblebee_gunner_enter(entity this, entity player)
293 {
294         entity vehic = this;
295         entity gunner = world;
296
297         if(!vehic.gunner1 && !vehic.gunner2 && ((time >= vehic.gun1.phase) + (time >= vehic.gun2.phase)) == 2)
298         {
299                 // we can have some fun
300                 if(vlen2(real_origin(vehic.gun2) - player.origin) < vlen2(real_origin(vehic.gun1) - player.origin))
301                 {
302                         gunner = vehic.gun2;
303                         vehic.gunner2 = player;
304                 }
305                 else
306                 {
307                         gunner = vehic.gun1;
308                         vehic.gunner1 = player;
309                 }
310         }
311         else if(!vehic.gunner1 && time >= vehic.gun1.phase)     { gunner = vehic.gun1; vehic.gunner1 = player; }
312         else if(!vehic.gunner2 && time >= vehic.gun2.phase)             { gunner = vehic.gun2; vehic.gunner2 = player; }
313         else { LOG_TRACE("Vehicle is full, fail\n"); return false; }
314
315         player.vehicle                  = gunner;
316         player.angles                   = vehic.angles;
317         player.takedamage               = DAMAGE_NO;
318         player.solid                    = SOLID_NOT;
319         player.alpha                    = -1;
320         player.movetype                 = MOVETYPE_NOCLIP;
321         player.event_damage     = func_null;
322         player.view_ofs                 = '0 0 0';
323         player.hud                              = gunner.hud;
324         player.teleportable     = false;
325         player.PlayerPhysplug   = gunner.PlayerPhysplug;
326         player.vehicle_ammo1    = vehic.vehicle_ammo1;
327         player.vehicle_ammo2    = vehic.vehicle_ammo2;
328         player.vehicle_reload1  = vehic.vehicle_reload1;
329         player.vehicle_reload2  = vehic.vehicle_reload2;
330         player.vehicle_energy   = vehic.vehicle_energy;
331         player.flags               &= ~FL_ONGROUND;
332
333         RemoveGrapplingHook(player);
334
335         gunner.m_switchweapon = PS(player).m_switchweapon;
336         gunner.vehicle_exit = bumblebee_gunner_exit;
337         gunner.vehicle_hudmodel.viewmodelforclient = player;
338
339         if(IS_REAL_CLIENT(player))
340         {
341                 msg_entity = player;
342                 WriteByte(MSG_ONE,              SVC_SETVIEWPORT);
343                 WriteEntity(MSG_ONE,    gunner.vehicle_viewport);
344
345                 WriteByte(MSG_ONE,              SVC_SETVIEWANGLES);
346                 WriteAngle(MSG_ONE,     gunner.angles_x + vehic.angles_x); // tilt
347                 WriteAngle(MSG_ONE,     gunner.angles_y + vehic.angles_y); // yaw
348                 WriteAngle(MSG_ONE,     0); // roll
349         }
350
351         CSQCVehicleSetup(player, player.hud);
352
353         MUTATOR_CALLHOOK(VehicleEnter, player, gunner);
354
355         return true;
356 }
357
358 bool vehicles_valid_pilot()
359 {SELFPARAM();
360         if(IS_BOT_CLIENT(other) && !autocvar_g_vehicles_allow_bots)
361                 return false;
362
363         if((!IS_PLAYER(other))
364         || (IS_DEAD(other))
365         || (other.vehicle)
366         || (DIFF_TEAM(other, self))
367         ) { return false; }
368
369         return true;
370 }
371
372 void bumblebee_touch(entity this)
373 {
374         if(autocvar_g_vehicles_enter) { return; }
375
376         if(self.gunner1 != world && self.gunner2 != world)
377         {
378                 vehicles_touch(self);
379                 return;
380         }
381
382         if(vehicles_valid_pilot())
383         {
384                 float phase_time = (time >= self.gun1.phase) + (time >= self.gun2.phase);
385
386                 if(time >= other.vehicle_enter_delay && phase_time)
387                 if(bumblebee_gunner_enter(self, other))
388                         return;
389         }
390
391         vehicles_touch(self);
392 }
393
394 void bumblebee_regen(entity this)
395 {
396         if(this.gun1.delay + autocvar_g_vehicle_bumblebee_cannon_ammo_regen_pause < time)
397                 this.gun1.vehicle_energy = min(autocvar_g_vehicle_bumblebee_cannon_ammo,
398                                                                            this.gun1.vehicle_energy + autocvar_g_vehicle_bumblebee_cannon_ammo_regen * frametime);
399
400         if(this.gun2.delay + autocvar_g_vehicle_bumblebee_cannon_ammo_regen_pause < time)
401                 this.gun2.vehicle_energy = min(autocvar_g_vehicle_bumblebee_cannon_ammo,
402                                                                            this.gun2.vehicle_energy + autocvar_g_vehicle_bumblebee_cannon_ammo_regen * frametime);
403
404         if(this.vehicle_flags  & VHF_SHIELDREGEN)
405                 vehicles_regen(this, this.dmg_time, vehicle_shield, autocvar_g_vehicle_bumblebee_shield, autocvar_g_vehicle_bumblebee_shield_regen_pause, autocvar_g_vehicle_bumblebee_shield_regen, frametime, true);
406
407         if(this.vehicle_flags  & VHF_HEALTHREGEN)
408                 vehicles_regen(this, this.dmg_time, vehicle_health, autocvar_g_vehicle_bumblebee_health, autocvar_g_vehicle_bumblebee_health_regen_pause, autocvar_g_vehicle_bumblebee_health_regen, frametime, false);
409
410         if(this.vehicle_flags  & VHF_ENERGYREGEN)
411                 vehicles_regen(this, this.wait, vehicle_energy, autocvar_g_vehicle_bumblebee_energy, autocvar_g_vehicle_bumblebee_energy_regen_pause, autocvar_g_vehicle_bumblebee_energy_regen, frametime, false);
412
413 }
414
415 bool bumblebee_pilot_frame(entity this)
416 {
417         entity vehic = this.vehicle;
418         return = true;
419
420         if(intermission_running)
421         {
422                 vehic.velocity = '0 0 0';
423                 vehic.avelocity = '0 0 0';
424                 return;
425         }
426
427         vehicles_frame(vehic, this);
428
429         if(IS_DEAD(vehic))
430         {
431                 PHYS_INPUT_BUTTON_ATCK(this) = PHYS_INPUT_BUTTON_ATCK2(this) = false;
432                 return;
433         }
434
435         bumblebee_regen(vehic);
436
437         crosshair_trace(this);
438
439         vector vang = vehic.angles;
440         vector newvel = vectoangles(normalize(trace_endpos - vehic.origin + '0 0 32'));
441         vang.x *= -1;
442         newvel.x *= -1;
443         if(newvel.x > 180)  newvel.x -= 360;
444         if(newvel.x < -180) newvel.x += 360;
445         if(newvel.y > 180)  newvel.y -= 360;
446         if(newvel.y < -180) newvel.y += 360;
447
448         float ftmp = shortangle_f(this.v_angle.y - vang.y, vang.y);
449         if(ftmp > 180)  ftmp -= 360;
450         if(ftmp < -180) ftmp += 360;
451         vehic.avelocity_y = bound(-autocvar_g_vehicle_bumblebee_turnspeed, ftmp + vehic.avelocity.y * 0.9, autocvar_g_vehicle_bumblebee_turnspeed);
452
453         // Pitch
454         ftmp = 0;
455         if(this.movement.x > 0 && vang.x < autocvar_g_vehicle_bumblebee_pitchlimit)
456                 ftmp = 4;
457         else if(this.movement.x < 0 && vang.x > -autocvar_g_vehicle_bumblebee_pitchlimit)
458                 ftmp = -8;
459
460         newvel.x = bound(-autocvar_g_vehicle_bumblebee_pitchlimit, newvel.x , autocvar_g_vehicle_bumblebee_pitchlimit);
461         ftmp = vang.x - bound(-autocvar_g_vehicle_bumblebee_pitchlimit, newvel.x + ftmp, autocvar_g_vehicle_bumblebee_pitchlimit);
462         vehic.avelocity_x = bound(-autocvar_g_vehicle_bumblebee_pitchspeed, ftmp + vehic.avelocity.x * 0.9, autocvar_g_vehicle_bumblebee_pitchspeed);
463
464         vehic.angles_x = anglemods(vehic.angles.x);
465         vehic.angles_y = anglemods(vehic.angles.y);
466         vehic.angles_z = anglemods(vehic.angles.z);
467
468         makevectors('0 1 0' * vehic.angles.y);
469         newvel = vehic.velocity * -autocvar_g_vehicle_bumblebee_friction;
470
471         if(this.movement.x != 0)
472         {
473                 if(this.movement.x > 0)
474                         newvel += v_forward  * autocvar_g_vehicle_bumblebee_speed_forward;
475                 else if(this.movement.x < 0)
476                         newvel -= v_forward  * autocvar_g_vehicle_bumblebee_speed_forward;
477         }
478
479         if(this.movement.y != 0)
480         {
481                 if(this.movement.y < 0)
482                         newvel -= v_right * autocvar_g_vehicle_bumblebee_speed_strafe;
483                 else if(this.movement.y > 0)
484                         newvel += v_right * autocvar_g_vehicle_bumblebee_speed_strafe;
485                 ftmp = newvel * v_right;
486                 ftmp *= frametime * 0.1;
487                 vehic.angles_z = bound(-15, vehic.angles.z + ftmp, 15);
488         }
489         else
490         {
491                 vehic.angles_z *= 0.95;
492                 if(vehic.angles.z >= -1 && vehic.angles.z <= -1)
493                         vehic.angles_z = 0;
494         }
495
496         if(PHYS_INPUT_BUTTON_CROUCH(this))
497                 newvel -=   v_up * autocvar_g_vehicle_bumblebee_speed_down;
498         else if(PHYS_INPUT_BUTTON_JUMP(this))
499                 newvel +=  v_up * autocvar_g_vehicle_bumblebee_speed_up;
500
501         vehic.velocity  += newvel * frametime;
502         this.velocity = this.movement  = vehic.velocity;
503
504
505         if(autocvar_g_vehicle_bumblebee_healgun_locktime)
506         {
507                 if(vehic.tur_head.lock_time < time || IS_DEAD(vehic.tur_head.enemy) || STAT(FROZEN, vehic.tur_head.enemy))
508                         vehic.tur_head.enemy = world;
509
510                 if(trace_ent)
511                 if(trace_ent.movetype)
512                 if(trace_ent.takedamage)
513                 if(!IS_DEAD(trace_ent) && !STAT(FROZEN, trace_ent))
514                 {
515                         if(teamplay)
516                         {
517                                 if(trace_ent.team == this.team)
518                                 {
519                                         vehic.tur_head.enemy = trace_ent;
520                                         vehic.tur_head.lock_time = time + autocvar_g_vehicle_bumblebee_healgun_locktime;
521                                 }
522                         }
523                         else
524                         {
525                                 vehic.tur_head.enemy = trace_ent;
526                                 vehic.tur_head.lock_time = time + autocvar_g_vehicle_bumblebee_healgun_locktime;
527                         }
528                 }
529
530                 if(vehic.tur_head.enemy)
531                 {
532                         trace_endpos = real_origin(vehic.tur_head.enemy);
533                         UpdateAuxiliaryXhair(this, trace_endpos, '0 0.75 0', 0);
534                 }
535         }
536
537         vang = vehicle_aimturret(vehic, trace_endpos, vehic.gun3, "fire",
538                                           autocvar_g_vehicle_bumblebee_raygun_pitchlimit_down * -1,  autocvar_g_vehicle_bumblebee_raygun_pitchlimit_up,
539                                           autocvar_g_vehicle_bumblebee_raygun_turnlimit_sides * -1,  autocvar_g_vehicle_bumblebee_raygun_turnlimit_sides,  autocvar_g_vehicle_bumblebee_raygun_turnspeed);
540
541         if(!forbidWeaponUse(this))
542         if((PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)) && (vehic.vehicle_energy > autocvar_g_vehicle_bumblebee_raygun_dps * sys_frametime || autocvar_g_vehicle_bumblebee_raygun == 0))
543         {
544                 vehic.gun3.enemy.realowner = this;
545                 vehic.gun3.enemy.effects &= ~EF_NODRAW;
546
547                 vehic.gun3.enemy.hook_start = gettaginfo(vehic.gun3, gettagindex(vehic.gun3, "fire"));
548                 vehic.gun3.enemy.SendFlags |= BRG_START;
549
550                 traceline(vehic.gun3.enemy.hook_start, vehic.gun3.enemy.hook_start + v_forward * autocvar_g_vehicle_bumblebee_raygun_range, MOVE_NORMAL, vehic);
551
552                 if(trace_ent)
553                 {
554                         if(autocvar_g_vehicle_bumblebee_raygun)
555                         {
556                                 Damage(trace_ent, vehic, this, autocvar_g_vehicle_bumblebee_raygun_dps * sys_frametime, DEATH_GENERIC.m_id, trace_endpos, v_forward * autocvar_g_vehicle_bumblebee_raygun_fps * sys_frametime);
557                                 vehic.vehicle_energy -= autocvar_g_vehicle_bumblebee_raygun_aps * sys_frametime;
558                         }
559                         else
560                         {
561                                 if(!IS_DEAD(trace_ent))
562                                         if((teamplay && trace_ent.team == this.team) || !teamplay)
563                                         {
564
565                                                 if(trace_ent.vehicle_flags & VHF_ISVEHICLE)
566                                                 {
567                                                         if(autocvar_g_vehicle_bumblebee_healgun_sps && trace_ent.vehicle_health <= trace_ent.max_health)
568                                                                 trace_ent.vehicle_shield = min(trace_ent.vehicle_shield + autocvar_g_vehicle_bumblebee_healgun_sps * frametime, trace_ent.tur_head.max_health);
569
570                                                         if(autocvar_g_vehicle_bumblebee_healgun_hps)
571                                                                 trace_ent.vehicle_health = min(trace_ent.vehicle_health + autocvar_g_vehicle_bumblebee_healgun_hps * frametime, trace_ent.max_health);
572                                                 }
573                                                 else if(IS_CLIENT(trace_ent))
574                                                 {
575                                                         if(trace_ent.health <= autocvar_g_vehicle_bumblebee_healgun_hmax && autocvar_g_vehicle_bumblebee_healgun_hps)
576                                                                 trace_ent.health = min(trace_ent.health + autocvar_g_vehicle_bumblebee_healgun_hps * frametime, autocvar_g_vehicle_bumblebee_healgun_hmax);
577
578                                                         if(trace_ent.armorvalue <= autocvar_g_vehicle_bumblebee_healgun_amax && autocvar_g_vehicle_bumblebee_healgun_aps)
579                                                                 trace_ent.armorvalue = min(trace_ent.armorvalue + autocvar_g_vehicle_bumblebee_healgun_aps * frametime, autocvar_g_vehicle_bumblebee_healgun_amax);
580
581                                                         trace_ent.health = min(trace_ent.health + autocvar_g_vehicle_bumblebee_healgun_hps * frametime, autocvar_g_vehicle_bumblebee_healgun_hmax);
582                                                 }
583                                                 else if(IS_TURRET(trace_ent))
584                                                 {
585                                                         if(trace_ent.health  <= trace_ent.max_health && autocvar_g_vehicle_bumblebee_healgun_hps)
586                                                                 trace_ent.health = min(trace_ent.health + autocvar_g_vehicle_bumblebee_healgun_hps * frametime, trace_ent.max_health);
587                                                         //else ..hmmm what? ammo?
588
589                                                         trace_ent.SendFlags |= TNSF_STATUS;
590                                                 }
591                                         }
592                         }
593                 }
594
595                 vehic.gun3.enemy.hook_end = trace_endpos;
596                 setorigin(vehic.gun3.enemy, trace_endpos);
597                 vehic.gun3.enemy.SendFlags |= BRG_END;
598
599                 vehic.wait = time + 1;
600         }
601         else
602                 vehic.gun3.enemy.effects |= EF_NODRAW;
603         /*{
604                 if(vehic.gun3.enemy)
605                         remove(vehic.gun3.enemy);
606
607                 vehic.gun3.enemy = world;
608         }
609         */
610
611         VEHICLE_UPDATE_PLAYER(this, vehic, health, bumblebee);
612         VEHICLE_UPDATE_PLAYER(this, vehic, energy, bumblebee);
613
614         this.vehicle_ammo1 = (vehic.gun1.vehicle_energy / autocvar_g_vehicle_bumblebee_cannon_ammo) * 100;
615         this.vehicle_ammo2 = (vehic.gun2.vehicle_energy / autocvar_g_vehicle_bumblebee_cannon_ammo) * 100;
616
617         if(vehic.vehicle_flags & VHF_HASSHIELD)
618                 VEHICLE_UPDATE_PLAYER(this, vehic, shield, bumblebee);
619
620         vehic.angles_x *= -1;
621         makevectors(vehic.angles);
622         vehic.angles_x *= -1;
623         setorigin(this, vehic.origin + v_up * 48 + v_forward * 160);
624
625         PHYS_INPUT_BUTTON_ATCK(this) = PHYS_INPUT_BUTTON_ATCK2(this) = PHYS_INPUT_BUTTON_CROUCH(this) = false;
626 }
627
628 void bumblebee_land(entity this)
629 {
630         float hgt;
631
632         hgt = raptor_altitude(512);
633         self.velocity = (self.velocity * 0.9) + ('0 0 -1800' * (hgt / 256) * sys_frametime);
634         self.angles_x *= 0.95;
635         self.angles_z *= 0.95;
636
637         if(hgt < 16)
638                 setthink(self, vehicles_think);
639
640         self.nextthink = time;
641
642         CSQCMODEL_AUTOUPDATE(self);
643 }
644
645 void bumblebee_exit(float eject)
646 {SELFPARAM();
647         if(self.owner.vehicleid == VEH_BUMBLEBEE.vehicleid)
648         {
649                 bumblebee_gunner_exit(eject);
650                 return;
651         }
652
653         settouch(self, vehicles_touch);
654
655         if(!IS_DEAD(self))
656         {
657                 setthink(self, bumblebee_land);
658                 self.nextthink  = time;
659         }
660
661         self.movetype = MOVETYPE_TOSS;
662
663         if(!self.owner)
664                 return;
665
666         fixedmakevectors(self.angles);
667         vector spot;
668         if(vdist(self.velocity, >, autocvar_g_vehicle_bumblebee_speed_forward * 0.5))
669                 spot = self.origin + v_up * 128 + v_forward * 300;
670         else
671                 spot = self.origin + v_up * 128 - v_forward * 300;
672
673         spot = vehicles_findgoodexit(spot);
674
675         // Hide beam
676         if(self.gun3.enemy || !wasfreed(self.gun3.enemy)) {
677                 self.gun3.enemy.effects |= EF_NODRAW;
678         }
679
680         self.owner.velocity = 0.75 * self.vehicle.velocity + normalize(spot - self.vehicle.origin) * 200;
681         self.owner.velocity_z += 10;
682         setorigin(self.owner, spot);
683
684         antilag_clear(self.owner, CS(self.owner));
685         self.owner = world;
686 }
687
688 void bumblebee_blowup(entity this)
689 {
690         RadiusDamage(self, self.enemy, autocvar_g_vehicle_bumblebee_blowup_coredamage,
691                                  autocvar_g_vehicle_bumblebee_blowup_edgedamage,
692                                  autocvar_g_vehicle_bumblebee_blowup_radius, self, world,
693                                  autocvar_g_vehicle_bumblebee_blowup_forceintensity,
694                                  DEATH_VH_BUMB_DEATH.m_id, world);
695
696         sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
697         Send_Effect(EFFECT_EXPLOSION_BIG, (self.origin + '0 0 100') + (randomvec() * 80), '0 0 0', 1);
698
699         if(self.owner.deadflag == DEAD_DYING)
700                 self.owner.deadflag = DEAD_DEAD;
701
702         remove(self);
703 }
704
705 void bumblebee_diethink(entity this)
706 {
707         if(time >= self.wait)
708                 setthink(self, bumblebee_blowup);
709
710         if(random() < 0.1)
711         {
712                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
713                 Send_Effect(EFFECT_EXPLOSION_SMALL, randomvec() * 80 + (self.origin + '0 0 100'), '0 0 0', 1);
714         }
715
716         self.nextthink = time + 0.1;
717 }
718
719 spawnfunc(vehicle_bumblebee)
720 {
721         if(!autocvar_g_vehicle_bumblebee) { remove(this); return; }
722         if(!vehicle_initialize(this, VEH_BUMBLEBEE, false)) { remove(this); return; }
723 }
724
725 METHOD(Bumblebee, vr_impact, void(Bumblebee thisveh, entity instance))
726 {
727     if(autocvar_g_vehicle_bumblebee_bouncepain)
728         vehicles_impact(instance, autocvar_g_vehicle_bumblebee_bouncepain_x, autocvar_g_vehicle_bumblebee_bouncepain_y, autocvar_g_vehicle_bumblebee_bouncepain_z);
729 }
730 METHOD(Bumblebee, vr_enter, void(Bumblebee thisveh, entity instance))
731 {
732     SELFPARAM();
733     settouch(self, bumblebee_touch);
734     self.nextthink = 0;
735     self.movetype = MOVETYPE_BOUNCEMISSILE;
736 }
737 METHOD(Bumblebee, vr_think, void(Bumblebee thisveh, entity instance))
738 {
739     SELFPARAM();
740     self.angles_z *= 0.8;
741     self.angles_x *= 0.8;
742
743     self.nextthink = time;
744
745     if(!self.owner)
746     {
747         if(self.gunner1)
748         {
749                 entity e = self.gunner1;
750                 WITHSELF(e, self.gun1.vehicle_exit(VHEF_EJECT));
751                 entity oldother = other;
752                 other = e;
753                 self.phase = 0;
754                 gettouch(self)(self);
755                 other = oldother;
756                 return;
757         }
758
759         if(self.gunner2)
760         {
761                 entity e = self.gunner2;
762                 WITHSELF(e, self.gun2.vehicle_exit(VHEF_EJECT));
763                 entity oldother = other;
764                 other = e;
765                 self.phase = 0;
766                 gettouch(self)(self);
767                 other = oldother;
768             return;
769         }
770     }
771 }
772 METHOD(Bumblebee, vr_death, void(Bumblebee thisveh, entity instance))
773 {
774         CSQCModel_UnlinkEntity(instance);
775
776         // hide beam
777         if(instance.gun3.enemy || !wasfreed(instance.gun3.enemy))
778                 instance.gun3.enemy.effects |= EF_NODRAW;
779
780         if(instance.gunner1)
781                 WITHSELF(instance.gunner1, instance.gun1.vehicle_exit(VHEF_EJECT));
782
783         if(instance.gunner2)
784                 WITHSELF(instance.gunner2, instance.gun2.vehicle_exit(VHEF_EJECT));
785
786         WITHSELF(instance, instance.vehicle_exit(VHEF_EJECT));
787
788     fixedmakevectors(instance.angles);
789     vehicle_tossgib(instance, instance.gun1, instance.velocity + v_right * 300 + v_up * 100 + randomvec() * 200, "cannon_right", rint(random()), rint(random()), 6, randomvec() * 200);
790     vehicle_tossgib(instance, instance.gun2, instance.velocity + v_right * -300 + v_up * 100 + randomvec() * 200, "cannon_left", rint(random()), rint(random()), 6, randomvec() * 200);
791     vehicle_tossgib(instance, instance.gun3, instance.velocity + v_forward * 300 + v_up * -100 + randomvec() * 200, "raygun", rint(random()), rint(random()), 6, randomvec() * 300);
792
793     entity _body = vehicle_tossgib(instance, instance, instance.velocity + randomvec() * 200, "", rint(random()), rint(random()), 6, randomvec() * 100);
794
795     if(random() > 0.5)
796         settouch(_body, bumblebee_blowup);
797     else
798         settouch(_body, func_null);
799
800     setthink(_body, bumblebee_diethink);
801     _body.nextthink = time;
802     _body.wait = time + 2 + (random() * 8);
803     _body.owner = instance;
804     _body.enemy = instance.enemy;
805     _body.scale = 1.5;
806     _body.angles = instance.angles;
807
808     Send_Effect(EFFECT_EXPLOSION_MEDIUM, findbetterlocation(instance.origin, 16), '0 0 0', 1);
809
810     instance.health                     = 0;
811     instance.event_damage       = func_null;
812     instance.solid                      = SOLID_NOT;
813     instance.takedamage         = DAMAGE_NO;
814     instance.deadflag           = DEAD_DYING;
815     instance.movetype           = MOVETYPE_NONE;
816     instance.effects            = EF_NODRAW;
817     instance.colormod           = '0 0 0';
818     instance.avelocity          = '0 0 0';
819     instance.velocity           = '0 0 0';
820     settouch(instance, func_null);
821     instance.nextthink          = 0;
822
823     setorigin(instance, instance.pos1);
824 }
825 METHOD(Bumblebee, vr_spawn, void(Bumblebee thisveh, entity instance))
826 {
827     SELFPARAM();
828     if(!self.gun1)
829     {
830         // for some reason, autosizing of the shield entity refuses to work for this one so set it up in advance.
831         self.vehicle_shieldent = spawn();
832         self.vehicle_shieldent.effects = EF_LOWPRECISION;
833         setmodel(self.vehicle_shieldent, MDL_VEH_BUMBLEBEE_SHIELD);
834         setattachment(self.vehicle_shieldent, self, "");
835         setorigin(self.vehicle_shieldent, real_origin(self) - self.origin);
836         self.vehicle_shieldent.scale       = 512 / vlen(self.maxs - self.mins);
837         setthink(self.vehicle_shieldent, shieldhit_think);
838         self.vehicle_shieldent.alpha = -1;
839         self.vehicle_shieldent.effects = EF_LOWPRECISION | EF_NODRAW;
840
841         self.gun1 = new(vehicle_playerslot);
842         self.gun2 = new(vehicle_playerslot);
843         self.gun3 = new(bumblebee_raygun);
844
845         self.vehicle_flags |= VHF_MULTISLOT;
846
847         self.gun1.owner = self;
848         self.gun2.owner = self;
849         self.gun3.owner = self;
850
851         setmodel(self.gun1, MDL_VEH_BUMBLEBEE_CANNON_RIGHT);
852         setmodel(self.gun2, MDL_VEH_BUMBLEBEE_CANNON_LEFT);
853         setmodel(self.gun3, MDL_VEH_BUMBLEBEE_CANNON_CENTER);
854
855         setattachment(self.gun1, self, "cannon_right");
856         setattachment(self.gun2, self, "cannon_left");
857
858         // Angled bones are no fun, messes up gun-aim; so work arround it.
859         self.gun3.pos1 = self.angles;
860         self.angles = '0 0 0';
861         vector ofs = gettaginfo(self, gettagindex(self, "raygun"));
862         ofs -= self.origin;
863         setattachment(self.gun3, self, "");
864         setorigin(self.gun3, ofs);
865         self.angles = self.gun3.pos1;
866
867         vehicle_addplayerslot(self, self.gun1, HUD_BUMBLEBEE_GUN, MDL_VEH_BUMBLEBEE_GUNCOCKPIT, bumblebee_gunner_frame, bumblebee_gunner_exit, bumblebee_gunner_enter);
868         vehicle_addplayerslot(self, self.gun2, HUD_BUMBLEBEE_GUN, MDL_VEH_BUMBLEBEE_GUNCOCKPIT, bumblebee_gunner_frame, bumblebee_gunner_exit, bumblebee_gunner_enter);
869
870         setorigin(self.vehicle_hudmodel, '50 0 -5');    // Move cockpit forward - down.
871         setorigin(self.vehicle_viewport, '5 0 2');    // Move camera forward up
872
873         //fixme-model-bones
874         setorigin(self.gun1.vehicle_hudmodel, '90 -27 -23');
875         setorigin(self.gun1.vehicle_viewport, '-85 0 50');
876         //fixme-model-bones
877         setorigin(self.gun2.vehicle_hudmodel, '90 27 -23');
878         setorigin(self.gun2.vehicle_viewport, '-85 0 50');
879
880         self.scale = 1.5;
881
882         // Raygun beam
883         if(self.gun3.enemy == world)
884         {
885             self.gun3.enemy = spawn();
886             Net_LinkEntity(self.gun3.enemy, true, 0, bumble_raygun_send);
887             self.gun3.enemy.SendFlags = BRG_SETUP;
888             self.gun3.enemy.cnt = autocvar_g_vehicle_bumblebee_raygun;
889             self.gun3.enemy.effects = EF_NODRAW | EF_LOWPRECISION;
890         }
891     }
892
893     self.vehicle_health = autocvar_g_vehicle_bumblebee_health;
894     self.vehicle_shield = autocvar_g_vehicle_bumblebee_shield;
895     self.solid = SOLID_BBOX;
896     self.movetype = MOVETYPE_TOSS;
897     self.damageforcescale = 0.025;
898
899     self.PlayerPhysplug = bumblebee_pilot_frame;
900
901     setorigin(self, self.origin + '0 0 25');
902 }
903 METHOD(Bumblebee, vr_setup, void(Bumblebee thisveh, entity instance))
904 {
905     if(autocvar_g_vehicle_bumblebee_energy)
906     if(autocvar_g_vehicle_bumblebee_energy_regen)
907         instance.vehicle_flags |= VHF_ENERGYREGEN;
908
909     if(autocvar_g_vehicle_bumblebee_shield)
910         instance.vehicle_flags |= VHF_HASSHIELD;
911
912     if(autocvar_g_vehicle_bumblebee_shield_regen)
913         instance.vehicle_flags |= VHF_SHIELDREGEN;
914
915     if(autocvar_g_vehicle_bumblebee_health_regen)
916         instance.vehicle_flags |= VHF_HEALTHREGEN;
917
918     instance.vehicle_exit = bumblebee_exit;
919     instance.respawntime = autocvar_g_vehicle_bumblebee_respawntime;
920     instance.vehicle_health = autocvar_g_vehicle_bumblebee_health;
921     instance.max_health = instance.vehicle_health;
922     instance.vehicle_shield = autocvar_g_vehicle_bumblebee_shield;
923 }
924
925 #endif // SVQC
926 #ifdef CSQC
927
928 void CSQC_BUMBLE_GUN_HUD()
929 {
930         Vehicles_drawHUD("vehicle_gunner", "vehicle_gunner_weapon1", string_null,
931                                          "vehicle_icon_ammo1", autocvar_hud_progressbar_vehicles_ammo1_color,
932                                          string_null, '0 0 0');
933 }
934
935 METHOD(Bumblebee, vr_hud, void(Bumblebee thisveh))
936 {
937     Vehicles_drawHUD(VEH_BUMBLEBEE.m_icon, "vehicle_bumble_weapon1", "vehicle_bumble_weapon2",
938                      "vehicle_icon_ammo1", autocvar_hud_progressbar_vehicles_ammo1_color,
939                      "vehicle_icon_ammo1", autocvar_hud_progressbar_vehicles_ammo1_color);
940
941     float hudAlpha = autocvar_hud_panel_fg_alpha;
942     float blinkValue = 0.55 + sin(time * 7) * 0.45;
943     vector tmpPos  = '0 0 0';
944     vector tmpSize = '1 1 1' * hud_fontsize;
945     tmpPos.x = vehicleHud_Pos.x + vehicleHud_Size.x * (520/768);
946
947     if(!AuxiliaryXhair[1].draw2d)
948     {
949         tmpPos.y = vehicleHud_Pos.y + vehicleHud_Size.y * (96/256) - tmpSize.y;
950         drawstring(tmpPos, _("No right gunner!"), tmpSize, '1 1 1', hudAlpha * blinkValue, DRAWFLAG_NORMAL);
951     }
952
953     if(!AuxiliaryXhair[2].draw2d)
954     {
955         tmpPos.y = vehicleHud_Pos.y + vehicleHud_Size.y * (160/256);
956         drawstring(tmpPos, _("No left gunner!"), tmpSize, '1 1 1', hudAlpha * blinkValue, DRAWFLAG_NORMAL);
957     }
958 }
959 METHOD(Bumblebee, vr_crosshair, void(Bumblebee thisveh))
960 {
961     Vehicles_drawCrosshair(vCROSS_HEAL);
962 }
963 METHOD(Bumblebee, vr_setup, void(Bumblebee thisveh, entity instance))
964 {
965     AuxiliaryXhair[0].axh_image = vCROSS_LOCK;  // Raygun-locked
966     AuxiliaryXhair[1].axh_image = vCROSS_BURST; // Gunner1
967     AuxiliaryXhair[2].axh_image = vCROSS_BURST; // Gunner2
968 }
969
970 #endif
971 #endif