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