/* TECHNICIAN
 * Created by the 29th Infantry Division
 * www.29th.org
 * www.dodrealism.branzone.com -- Revolutionizing Day of Defeat Realism
 *
 * DESCRIPTION
 * -Will give the Bazooka class an m1 carbine instead of a pistol, and the
 *  panzerjaeger class an mp40 instead of a pistol. Uses limited magazines
 *  for secondary weapons to provide balance.
 *
 * CVARS
 * -dod_technician <1/0>
 *  Enables or disables the plugin
 *
 * CREDITS
 * -Sgt. Theel
 * -2Lt. Nobles
 * -DOD: Source...for the idea
 * -Rukia for pdata and teaching me how to be a man
 *
 * FAQ
 * -Can I customise this to give different multiple weapons?
 *  Answer: Theoretically yes. This is the foundation of a dual weapons plugin
 *          and could be transformed into such
 * -Will this plugin make me better at Day of Defeat?
 *  Answer: No, if you such you will still continue to suck. But this will give you
 *          and extra weapon.
 *
 * KNOWN BUGS
 * -Drop both weapons, pick up carbine, and press slot2.
 *  It will give you another carbine with full ammo until you switch weapons
 *
 */

#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <fun> 
#include <dodx>
#include <dodfun>

#define PLUGIN "Technician"
#define VERSION "1.0"
#define AUTHOR "29th ID"

#define AMMO_SMG      1
#define AMMO_CARBINE  2
#define AMMO_GARAND   3
#define AMMO_ROCKET  13

#define FIND_ENT_BY_CLASS(%1,%2)        engfunc(EngFunc_FindEntityByString,%1,"classname",%2)

new g_bullets[33][4];
new g_hasPrimary[33];
new g_hasSecondary[33];
new g_isSwitching[33];
new g_isDropping[33];
new g_pCvarEnabled;


public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR);
	g_pCvarEnabled = register_cvar("dod_technician", "1");
	register_cvar("dod_technician_stats", "1.0", FCVAR_SERVER|FCVAR_SPONLY);
	
	// The old system of selecting weapons - no need anymore
	//register_clcmd("tech_primary", "give_primary")
	//register_clcmd("tech_secondary", "give_secondary")
	//register_clcmd("tech_melee", "give_melee")
	
	// Hook drop so that you can register the weapon drop in the memory
	register_clcmd("drop", "drop_protect");
	
	//////////////////////////////////////////
	// Hook the slot1, slot2, slot3 cmds to our cmds
	// Americans
	register_clcmd("weapon_amerknife", "func_clcmd");
	register_clcmd("weapon_colt", "func_clcmd");
	register_clcmd("weapon_bazooka", "func_clcmd");
	register_clcmd("weapon_m1carbine", "func_clcmd");
	register_clcmd("weapon_fcarbine", "func_clcmd");
	// Germans
	register_clcmd("weapon_spade", "func_clcmd");
	register_clcmd("weapon_gerknife", "func_clcmd");
	register_clcmd("weapon_luger", "func_clcmd");
	register_clcmd("weapon_pschreck", "func_clcmd");
	register_clcmd("weapon_mp40", "func_clcmd");
	// British
	register_clcmd("weapon_webley", "func_clcmd");
	register_clcmd("weapon_piat", "func_clcmd");
	register_clcmd("weapon_sten", "func_clcmd");
	//////////////////////////////////////////
	
	// Hook spawn to give correct ammo settings to rocket class
	// Used dod_client_spawn instead....technicality
	//register_event("ResetHUD", "reset_hud", "be");
	
	// Hook weapon pickup to register weapon posession in memory - also makes sure pistol is not displayed
	register_event("CurWeapon", "current_weapon", "be", "1=1");
	
	// Hook Sound Emission to block the "weapon pickup" sound on weapon "change"
	register_forward(FM_EmitSound, "fn_EmitSound");
	
	// Hook Entity Touch to allow rocket class to pick his weapon back up
	register_forward(FM_Touch, "fn_Touch");
}

// Hook Sound Emission to block the "weapon pickup" sound on weapon "change"
public fn_EmitSound(entity, channel, const sound[]) {
	if(!get_pcvar_num(g_pCvarEnabled))
		return PLUGIN_CONTINUE;
	if(equal(sound, "items/weaponpickup.wav") && g_isSwitching[entity])
	{
		return FMRES_SUPERCEDE;
	}
	return FMRES_IGNORED;
}

// Hook Entity Touch to allow rocket class to pick his weapon back up
public fn_Touch(ptd,ptr) {
	if(!get_pcvar_num(g_pCvarEnabled) || !pev_valid(ptd) || !pev_valid(ptr))
		return PLUGIN_CONTINUE;
		
	// Get toucher and touched information
	new g_clsptr[32], g_clsptd[32];
	pev(ptr,pev_classname,g_clsptr,31);
	pev(ptd,pev_classname,g_clsptd,31);
	
	// If the touch is a player and a weaponbox
	if(equal(g_clsptr,"player") && equal(g_clsptd,"weaponbox"))
	{
		///////////////////////////////////////////////////////////////////////////////////
		// The following subs check the toucher's team and then what weapon toucher is touching
		new m_iPrimary, m_iSecondary, m_iPtrClass = dod_get_user_class(ptr);
		// Americans
		if(m_iPtrClass == DODC_BAZOOKA && !g_isDropping[ptr]) {
			if(!g_hasPrimary[ptr])
			{
				m_iPrimary = find_ent_by_owner(1,"weapon_bazooka",ptd);
			}
			if(!g_hasSecondary[ptr])
			{
				m_iSecondary = find_ent_by_owner(1,"weapon_m1carbine",ptd);
				if(!m_iSecondary)
				{
					m_iSecondary = find_ent_by_owner(1,"weapon_fcarbine",ptd);
				}
			}
		}
		// Germans
		else if(m_iPtrClass == DODC_PANZERJAGER && !g_isDropping[ptr]) {
			if(!g_hasPrimary[ptr])
			{
				m_iPrimary = find_ent_by_owner(1,"weapon_pschreck",ptd);
			}
			if(!g_hasSecondary[ptr])
			{
				m_iSecondary = find_ent_by_owner(1,"weapon_mp40",ptd);
			}
		}
		// British
		else if(m_iPtrClass == DODC_PIAT && !g_isDropping[ptr]) {
			if(!g_hasPrimary[ptr])
			{
				m_iPrimary = find_ent_by_owner(1,"weapon_piat",ptd);
			}
			if(!g_hasSecondary[ptr])
			{
				m_iSecondary = find_ent_by_owner(1,"weapon_sten",ptd);
			}
		}
		///////////////////////////////////////////////////////////////////////////////////
		// If it is the correct primary weapon, give it to him
		if(m_iPrimary)// && !is_holding_new_wpn(ptr, m_iPtrClass))
		{
			g_hasPrimary[ptr] = 1;
			engfunc(EngFunc_RemoveEntity, m_iPrimary);
			engfunc(EngFunc_RemoveEntity, ptd);
			
			// If player was holding a knife, pull out new weapon
			if(is_holding_knife(ptr))
				give_primary(ptr,0);
				
			// play sound code will go here
		}
		// If it is the correct secondary weapon, give it to him
		if(m_iSecondary)// && !is_holding_new_wpn(ptr, m_iPtrClass))
		{
			g_hasSecondary[ptr] = 1;
			engfunc(EngFunc_RemoveEntity, m_iSecondary);
			engfunc(EngFunc_RemoveEntity, ptd);
			
			// If player was holding a knife, pull out new weapon
			if(is_holding_knife(ptr))
				give_secondary(ptr);
				
			// Give client a pistol so he can select the secondary weapon properly again
			if(m_iPtrClass == DODC_BAZOOKA) give_item(ptr, "weapon_colt");
			else if(m_iPtrClass == DODC_PANZERJAGER) give_item(ptr, "weapon_luger");
			else if(m_iPtrClass == DODC_PIAT) give_item(ptr, "weapon_webley");
			// play sound code will go here
		}
	}
	return PLUGIN_CONTINUE;
}

stock is_holding_new_wpn(id,class)
{
	new rtn, m_aWeapons[32], m_iNum;
	get_user_weapons(id, m_aWeapons, m_iNum);
	if(class == DODC_BAZOOKA)
	{
		for (new i; i < m_iNum; i++)
		{
			if( (m_aWeapons[i] != DODW_AMERKNIFE) && (m_aWeapons[i] != DODW_M1_CARBINE) && (m_aWeapons[i] != DODW_FOLDING_CARBINE) && (m_aWeapons[i] != DODW_BAZOOKA) )
				rtn = 1
		}
	}
	// Germans
	if(class == DODC_PANZERJAGER)
	{
		for (new i; i < m_iNum; i++)
		{
			if( (m_aWeapons[i] != DODW_GERKNIFE) && (m_aWeapons[i] != DODW_SPADE) && (m_aWeapons[i] != DODW_MP40) && (m_aWeapons[i] != DODW_PANZERSCHRECK) )
				rtn = 1
		}
	}
	// British
	if(class == DODC_PIAT)
	{
		for (new i; i < m_iNum; i++)
		{
			if( (m_aWeapons[i] != DODW_AMERKNIFE) && (m_aWeapons[i] != DODW_STEN) && (m_aWeapons[i] != DODW_PIAT) )
				rtn = 1
		}
	}
	return rtn;
}
	

stock is_holding_knife(id)
{
	new clip, ammo, m_iWeapon = get_user_weapon(id, clip, ammo);
	if(m_iWeapon == DODW_AMERKNIFE || m_iWeapon == DODW_GERKNIFE || m_iWeapon == DODW_SPADE)
		return 1;
	return 0;
}

stock find_ent_by_owner(startent,classname[],ownerid)
{
        while ((startent=FIND_ENT_BY_CLASS(startent,classname)))
        {
                if (pev_valid(startent) && pev(startent,pev_owner)==ownerid)
                        return startent;
        }
 
        return 0;
} 

stock has_tech_weapon(id,wpn)
{
	new rtn, m_aWeapons[32], m_iNum
	get_user_weapons(id, m_aWeapons, m_iNum);
	for (new i; i < m_iNum; i++)
	{
		if(m_aWeapons[i] != wpn)
			rtn = 1
	}
	return rtn;
}

// Hooks slot1, slot2, slot3 to select tech weapons instead
public func_clcmd(id) {
	if(!get_pcvar_num(g_pCvarEnabled) || !is_user_alive(id))
		return PLUGIN_CONTINUE;
		
	new m_szWpn[200], m_iClass = dod_get_user_class(id);
	// Read client command
	read_argv(0,m_szWpn,199);
	
	// Allies
	if(m_iClass == DODC_BAZOOKA) {
		if(equal(m_szWpn,"weapon_amerknife"))
			give_melee(id);
		else if(equal(m_szWpn,"weapon_colt"))
			give_secondary(id);
		else if(equal(m_szWpn,"weapon_bazooka") || equal(m_szWpn,"weapon_m1carbine") || equal(m_szWpn,"weapon_fcarbine"))
			give_primary(id,0);
	}
	// Germans
	else if(m_iClass == DODC_PANZERJAGER) {
		if(equal(m_szWpn,"weapon_spade") || equal(m_szWpn,"weapon_gerknife"))
			give_melee(id);
		else if(equal(m_szWpn,"weapon_luger"))
			give_secondary(id);
		else if(equal(m_szWpn,"weapon_pschreck") || equal(m_szWpn,"weapon_mp40"))
			give_primary(id,0);
	}
	// British
	else if(m_iClass == DODC_PIAT) {
		if(equal(m_szWpn,"weapon_amerknife"))
			give_melee(id);
		else if(equal(m_szWpn,"weapon_webley"))
			give_secondary(id);
		else if(equal(m_szWpn,"weapon_piat") || equal(m_szWpn,"weapon_sten"))
			give_primary(id,0);
	}
	return PLUGIN_CONTINUE;
}

// Hook spawn to give proper ammo settings
public dod_client_spawn(id) {
	if(!get_pcvar_num(g_pCvarEnabled))
		return PLUGIN_CONTINUE;
	new m_iClass = dod_get_user_class(id);
	if(m_iClass == DODC_BAZOOKA)
	{
		g_bullets[id][0] = 1;  // Bazooka Clip
		g_bullets[id][1] = 5;  // Bazooka Backpack
		g_bullets[id][2] = 15; // Carbine Clip
		g_bullets[id][3] = 30; // Carbine Backpack
		g_hasPrimary[id] = 1;
		g_hasSecondary[id] = 1;
	}
	else if(m_iClass == DODC_PANZERJAGER)
	{
		g_bullets[id][0] = 1;  // Bazooka Clip
		g_bullets[id][1] = 5;  // Bazooka Backpack
		g_bullets[id][2] = 30; // MP40 Clip
		g_bullets[id][3] = 60; // MP40 Backpack
		g_hasPrimary[id] = 1;
		g_hasSecondary[id] = 1;
	}
	else if(m_iClass == DODC_PIAT)
	{
		g_bullets[id][0] = 1;  // Bazooka Clip
		g_bullets[id][1] = 5;  // Bazooka Backpack
		g_bullets[id][2] = 30; // Sten Clip
		g_bullets[id][3] = 60; // Sten Backpack
		g_hasPrimary[id] = 1;
		g_hasSecondary[id] = 1;
	}
	return PLUGIN_CONTINUE;
}

// Delayed setting switching memory to 0
public is_switching(id) {
	g_isSwitching[id] = 0;
}

public give_primary(id,reset) {
	if(!get_pcvar_num(g_pCvarEnabled) || !g_hasPrimary[id])
		return PLUGIN_HANDLED;
	
	// Get current class/weapon settings
	new clip, ammo, m_iWeapon = dod_get_user_weapon(id, clip, ammo);
	new m_iClass = dod_get_user_class(id);
	
	// Americans
	if(m_iClass == DODC_BAZOOKA) {
		// Save ammunition count if last weapon was tech weapon
		// (Regular and Para Carbine)
		if(m_iWeapon == DODW_M1_CARBINE || m_iWeapon == DODW_FOLDING_CARBINE) {
			g_bullets[id][2] = get_clip(id, "weapon_m1carbine");
			g_bullets[id][3] = get_ammo(id, DODW_M1_CARBINE);
		}
		else if(m_iWeapon == DODW_BAZOOKA && (!reset)) {
			return PLUGIN_HANDLED;
		}
			
		// Give new weapon with saved ammo
		g_isSwitching[id] = 1;
		strip_user_weapons(id);
		if(g_hasSecondary[id])
			give_item(id, "weapon_colt");
		give_item(id, "weapon_amerknife");
		give_item(id, "weapon_bazooka");
		set_task(0.1, "is_switching", id); // Blocks pickup sound
		set_clip(id, "weapon_bazooka", g_bullets[id][0]);
		set_ammo(id, DODW_BAZOOKA, AMMO_ROCKET, g_bullets[id][1]);
	}
	// Germans
	else if(m_iClass == DODC_PANZERJAGER) {
		// Save ammunition count if last weapon was tech weapon
		// (Regular and Para Carbine)
		if(m_iWeapon == DODW_MP40) {
			g_bullets[id][2] = get_clip(id, "weapon_mp40");
			g_bullets[id][3] = get_ammo(id, DODW_MP40);
		}
		else if(m_iWeapon == DODW_PANZERSCHRECK && (!reset)) {
			return PLUGIN_HANDLED;
		}
			
		// Give new weapon with saved ammo
		g_isSwitching[id] = 1;
		strip_user_weapons(id);
		if(g_hasSecondary[id])
			give_item(id, "weapon_luger");
		give_item(id, "weapon_spade");
		give_item(id, "weapon_pschreck");
		set_task(0.1, "is_switching", id); // Blocks pickup sound
		set_clip(id, "weapon_pschreck", g_bullets[id][0]);
		set_ammo(id, DODW_PANZERSCHRECK, AMMO_ROCKET, g_bullets[id][1]);
	}
	// British
	else if(m_iClass == DODC_PIAT) {
		// Save ammunition count if last weapon was tech weapon
		// (Regular and Para Carbine)
		if(m_iWeapon == DODW_STEN) {
			g_bullets[id][2] = get_clip(id, "weapon_sten");
			g_bullets[id][3] = get_ammo(id, DODW_STEN);
		}
		else if(m_iWeapon == DODW_PIAT && (!reset)) {
			return PLUGIN_HANDLED;
		}
			
		// Give new weapon with saved ammo
		g_isSwitching[id] = 1;
		strip_user_weapons(id);
		if(g_hasSecondary[id])
			give_item(id, "weapon_webley");
		give_item(id, "weapon_amerknife");
		give_item(id, "weapon_piat");
		set_task(0.1, "is_switching", id); // Blocks pickup sound
		set_clip(id, "weapon_piat", g_bullets[id][0]);
		set_ammo(id, DODW_PIAT, AMMO_ROCKET, g_bullets[id][1]);
	}
	return PLUGIN_HANDLED;
}

public give_secondary(id) {
	if(!get_pcvar_num(g_pCvarEnabled) || !g_hasSecondary[id])
		return PLUGIN_HANDLED;
	
	// Get current class/weapon settings
	new clip, ammo, m_iWeapon = dod_get_user_weapon(id, clip, ammo);
	new m_iClass = dod_get_user_class(id);
	
	// Americans
	if(m_iClass == DODC_BAZOOKA) {
		// Save ammunition count if last weapon was tech weapon
		if(m_iWeapon == DODW_M1_CARBINE || m_iWeapon == DODW_FOLDING_CARBINE) {
			return PLUGIN_HANDLED;
		}
		else if(m_iWeapon == DODW_BAZOOKA) {
			g_bullets[id][0] = get_clip(id, "weapon_bazooka");
			g_bullets[id][1] = get_ammo(id, DODW_BAZOOKA);
		}
			
		// Give new weapon with saved ammo
		g_isSwitching[id] = 1;
		strip_user_weapons(id);
		give_item(id, "weapon_amerknife");
		give_item(id, "weapon_m1carbine");
		set_task(0.1, "is_switching", id); // Blocks pickup sound
		set_clip(id, "weapon_m1carbine", g_bullets[id][2]);
		set_clip(id, "weapon_fcarbine", g_bullets[id][2]);
		set_ammo(id, DODW_M1_CARBINE, AMMO_CARBINE, g_bullets[id][3]);
		set_ammo(id, DODW_FOLDING_CARBINE, AMMO_CARBINE, g_bullets[id][3]);
	}
	// Germans
	else if(m_iClass == DODC_PANZERJAGER) {
		// Save ammunition count if last weapon was tech weapon
		if(m_iWeapon == DODW_MP40) {
			return PLUGIN_HANDLED;
		}
		else if(m_iWeapon == DODW_PANZERSCHRECK) {
			g_bullets[id][0] = get_clip(id, "weapon_pschreck");
			g_bullets[id][1] = get_ammo(id, DODW_PANZERSCHRECK);
		}
			
		// Give new weapon with saved ammo
		g_isSwitching[id] = 1;
		strip_user_weapons(id);
		give_item(id, "weapon_spade");
		give_item(id, "weapon_mp40");
		set_task(0.1, "is_switching", id); // Blocks pickup sound
		set_clip(id, "weapon_mp40", g_bullets[id][2]);
		set_ammo(id, DODW_MP40, AMMO_SMG, g_bullets[id][3]);
	}
	// British
	else if(m_iClass == DODC_PIAT) {
		// Save ammunition count if last weapon was tech weapon
		if(m_iWeapon == DODW_STEN) {
			return PLUGIN_HANDLED;
		}
		else if(m_iWeapon == DODW_PIAT) {
			g_bullets[id][0] = get_clip(id, "weapon_piat");
			g_bullets[id][1] = get_ammo(id, DODW_PIAT);
		}
			
		// Give new weapon with saved ammo
		g_isSwitching[id] = 1;
		strip_user_weapons(id);
		give_item(id, "weapon_amerknife");
		give_item(id, "weapon_sten");
		set_task(0.1, "is_switching", id); // Blocks pickup sound
		set_clip(id, "weapon_sten", g_bullets[id][2]);
		set_ammo(id, DODW_STEN, AMMO_SMG, g_bullets[id][3]);
	}
	return PLUGIN_HANDLED;
}

public give_melee(id) {
	if(!get_pcvar_num(g_pCvarEnabled))
		return PLUGIN_HANDLED;
	
	// Get current class/weapon settings
	new clip, ammo, m_iWeapon = dod_get_user_weapon(id, clip, ammo);
	new m_iClass = dod_get_user_class(id);
	
	// Americans
	if(m_iClass == DODC_BAZOOKA) {
		// Save ammunition count if last weapon was tech weapon
		// (Regular and Para Carbine)
		if(m_iWeapon == DODW_AMERKNIFE) {
			return PLUGIN_HANDLED;
		}
		else if(m_iWeapon == DODW_M1_CARBINE || m_iWeapon == DODW_FOLDING_CARBINE) {
			g_bullets[id][2] = get_clip(id, "weapon_m1carbine");
			g_bullets[id][3] = get_ammo(id, DODW_M1_CARBINE);
		}
		else if(m_iWeapon == DODW_BAZOOKA) {
			g_bullets[id][0] = get_clip(id, "weapon_bazooka");
			g_bullets[id][1] = get_ammo(id, DODW_BAZOOKA);
		}
			
		// Give new weapon (no ammo for knife)
		//strip_user_weapons(id)
		g_isSwitching[id] = 1;
		if(g_hasSecondary[id])
			give_item(id, "weapon_colt");
		//give_item(id, "weapon_amerknife");
		set_task(0.1, "is_switching", id); // Blocks pickup sound
		client_cmd(id, "weapon_amerknife");
	}
	// Germans
	else if(m_iClass == DODC_PANZERJAGER) {
		// Save ammunition count if last weapon was tech weapon
		// (Regular and Para Carbine)
		if(m_iWeapon == DODW_GERKNIFE || m_iWeapon == DODW_SPADE) {
			return PLUGIN_HANDLED;
		}
		else if(m_iWeapon == DODW_MP40) {
			g_bullets[id][2] = get_clip(id, "weapon_mp40");
			g_bullets[id][3] = get_ammo(id, DODW_MP40);
		}
		else if(m_iWeapon == DODW_PANZERSCHRECK) {
			g_bullets[id][0] = get_clip(id, "weapon_pschreck");
			g_bullets[id][1] = get_ammo(id, DODW_PANZERSCHRECK);
		}
			
		// Give new weapon (no ammo for knife)
		//strip_user_weapons(id)
		g_isSwitching[id] = 1;
		if(g_hasSecondary[id])
			give_item(id, "weapon_luger");
		//give_item(id, "weapon_spade");
		//give_item(id, "weapon_gerknife");
		set_task(0.1, "is_switching", id); // Blocks pickup sound
		client_cmd(id, "weapon_spade");
		client_cmd(id, "weapon_gerknife");
	}
	// British
	else if(m_iClass == DODC_PIAT) {
		// Save ammunition count if last weapon was tech weapon
		// (Regular and Para Carbine)
		if(m_iWeapon == DODW_BRITKNIFE) {
			return PLUGIN_HANDLED;
		}
		else if(m_iWeapon == DODW_MP40) {
			g_bullets[id][2] = get_clip(id, "weapon_sten");
			g_bullets[id][3] = get_ammo(id, DODW_STEN);
		}
		else if(m_iWeapon == DODW_PANZERSCHRECK) {
			g_bullets[id][0] = get_clip(id, "weapon_piat");
			g_bullets[id][1] = get_ammo(id, DODW_PIAT);
		}
			
		// Give new weapon (no ammo for knife)
		//strip_user_weapons(id)
		g_isSwitching[id] = 1;
		if(g_hasSecondary[id])
			give_item(id, "weapon_webley");
		//give_item(id, "weapon_amerknife");
		set_task(0.1, "is_switching", id); // Blocks pickup sound
		client_cmd(id, "weapon_amerknife");
	}
	return PLUGIN_HANDLED;
}

// Set clip ammunition with pdata
public set_clip(id,const szWeapon[],clip) {

	new currentent = -1, m_iWpn = 0;

	// Get client's origin
	new Float:fOrigin[3];
	pev(id,pev_origin,fOrigin);
	
	// Search for the weapon the client is holding
	while((currentent = engfunc(EngFunc_FindEntityInSphere, currentent, fOrigin, Float:1.0)) != 0) {
		// Get found entity's info
		new szClassname[32];
		pev(currentent,pev_classname, szClassname ,31);
	
		// If the entity is the desired weapon, save it
		if(equal(szClassname,szWeapon))
			m_iWpn = currentent;
	
	}
	
	// Set the found weapon's clip ammo
	set_pdata_int(m_iWpn,108,clip,4);
}

// Set backpack ammo with AmmoX and DoD native
public set_ammo(id,weapon,channel,ammo) {
	
	// Set game memory with new ammo
	dod_set_user_ammo(id, weapon, ammo);
	
	// Update user's HUD with new ammo
	message_begin(MSG_ONE,get_user_msgid("AmmoX"),{0,0,0},id);
	write_byte(channel);
	write_byte(ammo);
	message_end();
}

// Get clip ammo with pdata
public get_clip(id,const szWeapon[]) {
	
	new currentent = -1, m_iWpn = 0;

	// Get client's origin
	new Float:fOrigin[3];
	pev(id,pev_origin,fOrigin);
	
	// Search for the weapon the client is holding
	while((currentent = engfunc(EngFunc_FindEntityInSphere, currentent, fOrigin, Float:1.0)) != 0) {
		// Get found entity's info
		new szClassname[32];
		pev(currentent,pev_classname, szClassname ,31);
	
		// If the entity is the desired weapon, save it
		if(equal(szClassname,szWeapon))
			m_iWpn = currentent;
	
	}
	
	// Get the found weapon's clip ammo
	new clip = get_pdata_int(m_iWpn,108,4);
	
	// Return the ammo
	return clip;
}

// Get backpack ammo using DoD native
public get_ammo(id,weapon) {
	new ammo = dod_get_user_ammo(id, weapon);
	
	// Return the ammo
	return ammo;
}

// Hook weapon drop to register it in the memory
public drop_protect(id) {
	if(!get_pcvar_num(g_pCvarEnabled))
		return PLUGIN_CONTINUE;
	
	// Get users current class/weapon
	new clip, ammo, m_iWeapon = dod_get_user_weapon(id, clip, ammo);
	new m_iClass = dod_get_user_class(id);
	
	// Americans
	if(m_iClass == DODC_BAZOOKA) {
		// If dropping primary
		if(m_iWeapon == DODW_BAZOOKA) {
			g_hasPrimary[id] = 0;
			g_isDropping[id] = 1;
			g_bullets[id][0] = get_clip(id, "weapon_bazooka");
			g_bullets[id][1] = get_ammo(id, DODW_BAZOOKA);
			if(g_hasSecondary[id])
				set_task(0.1, "give_secondary", id);
			set_task(0.5, "undrop", id);
		}
		// If dropping secondary
		else if(m_iWeapon == DODW_M1_CARBINE || m_iWeapon == DODW_FOLDING_CARBINE) {
			g_hasSecondary[id] = 0;
			g_isDropping[id] = 1;
			g_bullets[id][2] = get_clip(id, "weapon_m1carbine");
			g_bullets[id][3] = get_ammo(id, DODW_M1_CARBINE);
			if(g_hasPrimary[id])
				set_task(0.1, "give_primary", id);
			set_task(0.5, "undrop", id);
		}
	}
	// Germans
	else if(m_iClass == DODC_PANZERJAGER) {
		// If dropping primary
		if(m_iWeapon == DODW_PANZERSCHRECK) {
			g_hasPrimary[id] = 0;
			g_isDropping[id] = 1;
			g_bullets[id][0] = get_clip(id, "weapon_pschreck");
			g_bullets[id][1] = get_ammo(id, DODW_PANZERSCHRECK);
			if(g_hasSecondary[id])
				set_task(0.1, "give_secondary", id);
			set_task(0.5, "undrop", id);
		}
		// If dropping secondary
		else if(m_iWeapon == DODW_MP40) {
			g_hasSecondary[id] = 0;
			g_isDropping[id] = 1;
			g_bullets[id][2] = get_clip(id, "weapon_mp40");
			g_bullets[id][3] = get_ammo(id, DODW_MP40);
			if(g_hasPrimary[id])
				set_task(0.1, "give_primary", id);
			set_task(0.5, "undrop", id);
		}
	}
	// British
	else if(m_iClass == DODC_PIAT) {
		// If dropping primary
		if(m_iWeapon == DODW_PIAT) {
			g_hasPrimary[id] = 0;
			g_isDropping[id] = 1;
			g_bullets[id][0] = get_clip(id, "weapon_piat");
			g_bullets[id][1] = get_ammo(id, DODW_PIAT);
			if(g_hasSecondary[id])
				set_task(0.1, "give_secondary", id);
			set_task(0.5, "undrop", id);
		}
		// If dropping secondary
		else if(m_iWeapon == DODW_STEN) {
			g_hasSecondary[id] = 0;
			g_isDropping[id] = 1;
			g_bullets[id][2] = get_clip(id, "weapon_sten");
			g_bullets[id][3] = get_ammo(id, DODW_STEN);
			if(g_hasPrimary[id])
				set_task(0.1, "give_primary", id);
			set_task(0.5, "undrop", id);
		}
	}
	return PLUGIN_CONTINUE;
}

// Hook weapon pickup to register possession in memory & make sure pistol is not displayed
public current_weapon(id) {
	if(!get_pcvar_num(g_pCvarEnabled))
		return PLUGIN_CONTINUE;
		
	// Get users current class/weapon
	new clip, ammo, m_iWeapon = dod_get_user_weapon(id, clip, ammo);
	new m_iClass = dod_get_user_class(id);
	
	// Americans
	if(m_iClass == DODC_BAZOOKA) {
		if(m_iWeapon == DODW_BAZOOKA)
			g_hasPrimary[id] = 1;
		else if(m_iWeapon == DODW_M1_CARBINE || m_iWeapon == DODW_FOLDING_CARBINE)
			g_hasSecondary[id] = 1;
		//else if(m_iWeapon == DODW_COLT)
			//give_secondary(id);
	}
	// Germans
	else if(m_iClass == DODC_PANZERJAGER) {
		if(m_iWeapon == DODW_PANZERSCHRECK)
			g_hasPrimary[id] = 1;
		else if(m_iWeapon == DODW_MP40)
			g_hasSecondary[id] = 1;
		//else if(m_iWeapon == DODW_LUGER)
			//give_secondary(id);
	}
	// British
	else if(m_iClass == DODC_PIAT) {
		if(m_iWeapon == DODW_PIAT)
			g_hasPrimary[id] = 1;
		else if(m_iWeapon == DODW_STEN)
			g_hasSecondary[id] = 1;
		//else if(m_iWeapon == DODW_WEBLEY)
			//give_secondary(id);
	}
	return PLUGIN_CONTINUE;
}

// Delayed setting of "dropping" to 0
public undrop(id) {
	g_isDropping[id] = 0;
}
