/*
* Empires API
* 
* Created By Theowningone
* Version: 1.1
* http://www.theowningone.info/
* http://www.borgamers.com/
*/


/******************
* --> Defines <-- *
******************/

#if defined _empires_vehicle_stocks_included
#endinput
#endif
#define _empires_is_vehicle_included


/*******************
* --> Includes <-- *
*******************/

#include <sourcemod>
#include <sdktools>'
#include "empires_is_stocks"


/*************************
* --> Vehicle Stocks <-- *
*************************/

/**
* Get Vehicle Occupants
*
* @param ent			Vehicle Entity Index
* @return   			Amount of clients in vehicle
*/
stock GetVehicleOccupants(ent)
{
	if(!IsValidEntity(ent))return -1;
	new String:class[256];
	GetEntityNetClass(ent,class,sizeof(class));
	if(!StrEqual(class,"CEmpTank",false))return -1;
	new dat,out=0;
	dat=GetEntDataEnt(ent,FindSendPropOffs("CEmpTank","m_hPlayer"));
	if(dat)out++;
	dat=GetEntDataEnt(ent,FindSendPropOffs("CEmpTank","m_hPlayer2"));
	if(dat)out++;
	dat=GetEntDataEnt(ent,FindSendPropOffs("CEmpTank","m_hPlayer3"));
	if(dat)out++;
	dat=GetEntDataEnt(ent,FindSendPropOffs("CEmpTank","m_hPlayer4"));
	if(dat)out++;
	dat=GetEntDataEnt(ent,FindSendPropOffs("CEmpTank","m_hPlayer5"));
	if(dat)out++;
	dat=GetEntDataEnt(ent,FindSendPropOffs("CEmpTank","m_hPlayer6"));
	if(dat)out++;
	dat=GetEntDataEnt(ent,FindSendPropOffs("CEmpTank","m_hPlayer7"));
	if(dat)out++;
	dat=GetEntDataEnt(ent,FindSendPropOffs("CEmpTank","m_hPlayer8"));
	if(dat)out++;
	return out;
}

/**
* Get Vehicle Driver
*
* @param ent			Vehicle Entity Index
* @return 				Client Index of Driver (0 if none, -1 if invalid entity)
*/
stock GetVehicleDriver(ent)
{
	if(!IsValidEntity(ent))return -1;
	new String:class[256];
	GetEntityNetClass(ent,class,sizeof(class));
	if(!StrEqual(class,"CEmpTank",false))return -1;
	return GetEntDataEnt(ent,FindSendPropOffs("CEmpTank","m_hPlayer"));

}

/**
* Set Vehicle Armor
*
* @param ent			Vehicle Entity Index
* @param side			side of armor (1 is front, 2 is back, 3 is left, 4 is right)
* @param front			Health to set armor to
* @return				-1 on error, 0 on success
*/
stock SetVehicleArmor(ent, side, Float:health)
{
	if(!IsValidEntity(ent))return -1;
	new String:class[256];
	GetEntityNetClass(ent,class,sizeof(class));
	if(!StrEqual(class,"CEmpTank",false))return -1;
	if(side==1){
		SetEntDataFloat(ent,FindSendPropOffs("CEmpTank","m_flVehicleArmor"),health,true);
	}else if(side==2){
		SetEntDataFloat(ent,FindSendPropOffs("CEmpTank","m_flVehicleArmor")+4,health,true);
	}else if(side==3){
		SetEntDataFloat(ent,FindSendPropOffs("CEmpTank","m_flVehicleArmor")+8,health,true);
	}else if(side==4){
		SetEntDataFloat(ent,FindSendPropOffs("CEmpTank","m_flVehicleArmor")+12,health,true);
	}
	return 0;
}

/**
* Get Vehicle Armor
*
* @param ent			Vehicle Entity Index
* @param side			side of armor (1 is front, 2 is back, 3 is left, 4 is right)
* @return				Armor Health (-1.0 on error)
*/
stock Float:GetVehicleArmor(ent, side)
{
	if(!IsValidEntity(ent))return -1.0;
	new String:class[256];
	GetEntityNetClass(ent,class,sizeof(class));
	if(!StrEqual(class,"CEmpTank",false)) return -1.0;
	new Float:dat=-1.0;
	if(side==1){
		dat=GetEntDataFloat(ent,FindSendPropOffs("CEmpTank","m_flVehicleArmor"));
	}else if(side==2){
		dat=GetEntDataFloat(ent,FindSendPropOffs("CEmpTank","m_flVehicleArmor")+4);
	}else if(side==3){
		dat=GetEntDataFloat(ent,FindSendPropOffs("CEmpTank","m_flVehicleArmor")+8);
	}else if(side==4){
		dat=GetEntDataFloat(ent,FindSendPropOffs("CEmpTank","m_flVehicleArmor")+12);
	}
	return dat;
}

/**
* Set Vehicle Hull
*
* @param ent			Vehicle Entity Index
* @param health			Health to set hull to
* @return				-1 on error, 0 on success
*/
stock SetVehicleHull(ent, Float:health)
{
	if(!IsValidEntity(ent))return -1;
	new String:class[256];
	GetEntityNetClass(ent,class,sizeof(class));
	if(!StrEqual(class,"CEmpTank",false))return -1;
	SetEntDataFloat(ent,FindSendPropOffs("CEmpTank", "m_flTotalHealth"),health,true);
	return 0;
}

/**
* Get Vehicle Hull
*
* @param ent			Vehicle Entity Index
* @return				Hull Health (-1.0 on error)
*/
stock Float:GetVehicleHull(ent)
{
	if(!IsValidEntity(ent))return -1.0;
	new String:class[256];
	GetEntityNetClass(ent,class,sizeof(class));
	if(!StrEqual(class,"CEmpTank", false))return -1.0;
	return GetEntDataFloat(ent,FindSendPropOffs("CEmpTank","m_flTotalHealth"));
}

/**
* Set Vehicle Heat
*
* @param ent			Vehicle Entity Index
* @param heat			Heat to set for vehicle
* @return				-1 on error, 0 on success
*/
stock SetVehicleHeat(ent, Float:heat)
{
	if(!IsValidEntity(ent))return -1;
	new String:class[256];
	GetEntityNetClass(ent,class,sizeof(class));
	if(!StrEqual(class,"CEmpTank",false))return -1;
	SetEntDataFloat(ent,FindSendPropOffs("CEmpTank", "m_flVehicleHeat"),heat,true);
	return 0;
}

/**
* Get Vehicle Heat
*
* @param ent			Vehicle Entity Index
* @return				Vehicle Heat (-1.0 on error)
*/
stock Float:GetVehicleHeat(ent)
{
	if(!IsValidEntity(ent))return -1.0;
	new String:class[256];
	GetEntityNetClass(ent,class,sizeof(class));
	if(!StrEqual(class,"CEmpTank",false))return -1.0;
	return GetEntDataFloat(ent,FindSendPropOffs("CEmpTank","m_flVehicleHeat"));

}

/**
* Get Vehicle Class
*
* @param ent			Vehicle Entity Index
* @return				Vehicle Class (-1 on error)
*/
stock GetVehicleClass(ent)
{
	if(!IsValidEntity(ent))return -1;
	new String:class[256];
	GetEntityNetClass(ent,class,sizeof(class));
	if(!StrEqual(class,"CEmpTank",false))return -1;
	return GetEntData(ent,FindSendPropOffs("CEmpTank","m_iChassis"),4);
}

/**
* Get Vehicle Class Name
*
* @param ent			Vehicle Entity Index
* @param buffer			Buffer to hold the vehicle class name
* @param size			Len of the buffer
* @return				Vehicle Class (-1 on error)
*/
stock GetVehicleClassName(ent, String:buffer[], size)
{
	if(!IsValidEntity(ent))return -1;
	new String:class[256];
	GetEntityNetClass(ent,class,sizeof(class));
	if(!StrEqual(class,"CEmpTank",false))return -1;
	new out=GetEntData(ent,FindSendPropOffs("CEmpTank","m_iChassis"),4);
	new Handle:VehicleNames=CreateTrie();
	SetTrieString(VehicleNames,"0","NF Jeep",true);
	SetTrieString(VehicleNames,"1","NF APC",true);
	SetTrieString(VehicleNames,"2","NF Light Tank",true);
	SetTrieString(VehicleNames,"3","NF Med Tank",true);
	SetTrieString(VehicleNames,"4","NF Heavy Tank",true);
	SetTrieString(VehicleNames,"5","NF Arty Tank",true);
	SetTrieString(VehicleNames,"6","Imperial Jeep",true);
	SetTrieString(VehicleNames,"7","Imperial APC",true);
	SetTrieString(VehicleNames,"8","Imperial AFV",true);
	SetTrieString(VehicleNames,"9","Imperial Med Tank",true);
	SetTrieString(VehicleNames,"10","Imperial Heavy Tank",true);
	SetTrieString(VehicleNames,"11","Imperial Arty Tank",true);
	SetTrieString(VehicleNames,"12","Imperial Commander",true);
	SetTrieString(VehicleNames,"13","NF Commander",true);
	decl String:vehicle[64],String:vehiclename[64];
	IntToString(out,vehicle,sizeof(vehicle));
	GetTrieString(VehicleNames,vehicle,vehiclename,sizeof(vehiclename));
	CloseHandle(VehicleNames);
	strcopy(buffer,size,vehiclename);
	return out;
}

/**
* Get Client Vehicle
*
* @param client			Client Index
* @return				Client Vehicle Entity Index (-1 is error, 0 is none)
*/
stock GetPlayerVehicle(client)
{
	new team = GetClientTeam(client);
	new maxents = GetMaxEntities();
	decl String:classname[64];
	new ent, out = -1;
	if(team == 2){
		for(new i = 0; i <= maxents; i++){
			if(IsValidEntity(i)){
				GetEntityNetClass(i, classname, sizeof(classname));
				if(StrEqual(classname, "CPlayerResource_NF")){
					ent = i;
					break;
				}
			}
		}
		new offset = FindSendPropOffs("CPlayerResource_NF", "m_iVehicle_NF");
		out = GetEntData(ent, offset + (client*4), 4);
	}else if(team == 3){
		for(new i = 0; i <= maxents; i++){
			if(IsValidEntity(i)){
				GetEntityNetClass(i, classname, sizeof(classname));
				if(StrEqual(classname, "CPlayerResource_IMP")){
					ent = i;
					break;
				}
			}
		}
		new offset = FindSendPropOffs("CPlayerResource_IMP", "m_iVehicle_IMP");
		out = GetEntData(ent, offset + (client*4), 4);
	}
	if(IsCommander(client)){
		if(team == 2){
			for(new i = 0; i <= maxents; i++){
				if(IsValidEntity(i)){
					GetEdictClassname(i, classname, sizeof(classname));
					if(StrEqual(classname, "emp_nf_commander")){
						out = i;
						break;
					}
				}
			}
		}else if(team == 3){
			for(new i = 0; i <= maxents; i++){
				if(IsValidEntity(i)){
					GetEdictClassname(i, classname, sizeof(classname));
					if(StrEqual(classname, "emp_imp_commander")){
						out = i;
						break;
					}
				}
			}
		}
	}
	return out;
}

/**
* Get Cannon Entity
*
* @param ent			Vehicle Entity Index
* @return				Cannon Entity Index
*/
stock GetCannonEnt(ent)
{
	if(!IsValidEntity(ent))return -1;
	new String:class[256];
	GetEntityNetClass(ent,class,sizeof(class));
	if(!StrEqual(class,"CEmpTank",false))return -1;
	return GetEntDataEnt(ent,FindSendPropOffs("CEmpTank","m_hCannon"));
}

/**
* Get Turret Entity
*
* @param ent			Vehicle Entity Index
* @return				Turret Entity Index
*/
stock GetTurretEnt(ent)
{
	if(!IsValidEntity(ent))return -1;
	new String:class[256];
	GetEntityNetClass(ent,class,sizeof(class));
	if(!StrEqual(class,"CEmpTank",false))return -1;
	return GetEntDataEnt(ent,FindSendPropOffs("CEmpTank","m_hTurret"));
}