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