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