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