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