#pragma once
#ifdef SVQC

#include <common/turrets/sv_turrets.qh>
#include "vehicle.qh"

// vehicle cvars
bool autocvar_g_vehicles = true;
AUTOCVAR(g_vehicles_enter, bool, false, "require pressing use key to enter a vehicle");
float autocvar_g_vehicles_enter_radius = 250;
AUTOCVAR(g_vehicles_steal, bool, true, "allow stealing enemy vehicles in teamplay modes");
AUTOCVAR(g_vehicles_steal_show_waypoint, bool, true, "show a waypoint above the thief");
float autocvar_g_vehicles_crush_dmg = 70;
float autocvar_g_vehicles_crush_force = 50;
float autocvar_g_vehicles_crush_minspeed = 100;
bool autocvar_g_vehicles_delayspawn = true;
float autocvar_g_vehicles_delayspawn_jitter = 10;
float autocvar_g_vehicles_allow_bots;
int autocvar_g_vehicles_exit_attempts = 25;
float autocvar_g_vehicles_thinkrate = 0.1;

AUTOCVAR(g_vehicles_teams, bool, true, "allow team specific vehicles");
float autocvar_g_vehicles_teleportable;
float autocvar_g_vehicles_vortex_damagerate = 0.75;
float autocvar_g_vehicles_machinegun_damagerate = 0.75;
float autocvar_g_vehicles_rifle_damagerate = 0.75;
float autocvar_g_vehicles_vaporizer_damagerate = 0.5;
float autocvar_g_vehicles_tag_damagerate = 5;
float autocvar_g_vehicles_weapon_damagerate = 2;

.float vehicle_last_trace;

// flags:
.int vehicle_flags;

// vehicle definitions
.entity gun1;
.entity gun2;
.entity gun3;
.entity vehicle_shieldent;  ///< Entity to disply the shild effect on damage
.entity vehicle;
.entity vehicle_viewport;
.entity vehicle_hudmodel;
.entity vehicle_controller;

.entity gunner1;
.entity gunner2;

const .float vehicle_health = _STAT(VEHICLESTAT_HEALTH);  ///< If ent is player this is 0..100 indicating precentage of health left on vehicle. Vehicle's value is the health resource
const .float vehicle_energy = _STAT(VEHICLESTAT_ENERGY);  ///< If ent is player this is 0..100 indicating precentage of energy left on vehicle. If ent is vehicle, this is the real energy value.
const .float vehicle_shield = _STAT(VEHICLESTAT_SHIELD);  ///< If ent is player this is 0..100 indicating precentage of shield left on vehicle. If ent is vehicle, this is the real shield value.

const .float vehicle_ammo1    = _STAT(VEHICLESTAT_AMMO1);    ///< If ent is player this is 0..100 indicating percentage of primary ammo left UNLESS value is already stored in vehicle_energy. If ent is vehicle, this is the real ammo1 value.
const .float vehicle_reload1  = _STAT(VEHICLESTAT_RELOAD1);  ///< If ent is player this is 0..100 indicating percentage of primary reload status. If ent is vehicle, this is the real reload1 value.
const .float vehicle_ammo2    = _STAT(VEHICLESTAT_AMMO2);    ///< If ent is player this is 0..100 indicating percentage of secondary ammo left. If ent is vehicle, this is the real ammo2 value.
const .float vehicle_reload2  = _STAT(VEHICLESTAT_RELOAD2);  ///< If ent is player this is 0..100 indicating percentage of secondary reload status. If ent is vehicle, this is the real reload2 value.

.float sound_nexttime;
const float VOL_VEHICLEENGINE = 1;

const float SVC_SETVIEWPORT    = 5;    // Net.Protocol 0x05
const float SVC_SETVIEWANGLES  = 10;   // Net.Protocol 0x0A
const float SVC_UPDATEENTITY   = 128;  // Net.Protocol 0x80

const float VHSF_NORMAL   = 0;
const float VHSF_FACTORY  = 2;

.float dmg_time;

.float pain_frame;

.float play_time;

.int volly_counter;

const int MAX_AXH = 4;
.entity AuxiliaryXhair[MAX_AXH];

.entity wps_intruder;

.entity lock_target;
.float  lock_strength;
.float  lock_time;
.float  lock_soundtime;

// vehicle functions
.void(int _spawnflag) vehicle_spawn;  ///< Vehicles custom fucntion to be efecuted when vehicle (re)spawns
.bool(entity this, int _imp) vehicles_impulse;
.void(entity this, int exit_flags) vehicle_exit;
.bool(entity this, entity player) vehicle_enter;
const int VHEF_NORMAL   = 0;  ///< User pressed exit key
const int VHEF_EJECT    = 1;  ///< User pressed exit key 3 times fast (not implemented) or vehicle is dying
const int VHEF_RELEASE  = 2;  ///< Release ownership, client possibly allready dissconnected / went spec / changed team / used "kill" (not implemented)

float vehicles_exit_running;

// macros
#define VEHICLE_UPDATE_PLAYER(ply, vehi, fld, vhname) \
	ply.vehicle_##fld = (vehi.vehicle_##fld / autocvar_g_vehicle_##vhname##_##fld) * 100

#define VEHICLE_UPDATE_PLAYER_RESOURCE(ply, vehi, fld, vhname, res) \
	ply.vehicle_##fld = (GetResource(vehi, res) / autocvar_g_vehicle_##vhname##_##fld) * 100

.float vehicle_enter_delay; ///< Prevent players jumping to and from vehicles instantly

void vehicles_exit(entity vehic, int eject);
bool vehicle_initialize(entity this, Vehicle info, float nodrop);
bool vehicle_impulse(entity this, int imp);
bool vehicles_crushable(entity e);
void vehicles_enter(entity pl, entity veh);

IntrusiveList g_vehicle_returners;
STATIC_INIT(g_vehicle_returners)
{
	g_vehicle_returners = IL_NEW();
}

#endif // SVQC
