#include amxmodx
#include fakemeta

#define fm_create_entity(%1) engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, %1))
#define fm_find_ent_by_class(%1,%2) engfunc(EngFunc_FindEntityByString, %1, "classname", %2)
#define fm_find_ent_in_sphere(%1,%2,%3) engfunc(EngFunc_FindEntityInSphere, %1, %2, %3)
#define fm_entity_set_size(%1,%2,%3) engfunc(EngFunc_SetSize, %1, %2, %3)
#define fm_entity_set_model(%1,%2) engfunc(EngFunc_SetModel, %1, %2)
#define fm_remove_entity(%1) engfunc(EngFunc_RemoveEntity, %1)

enum {
SND_HOK=0,
SND_HNO,
SND_AOK,
SND_ANO,
SND_CNO,
SND_BOK,
SND_BNO,
SND_MAX
}

enum {
MDL_HEL=0,
MDL_ARM,
MDL_MAX
}

new is_precachedmodel[MDL_MAX]

new is_precachedsound[SND_MAX]
new precachesound[SND_MAX][] = {
"items/medshot4.wav",
"items/medshotno1.wav",
"items/suitchargeok1.wav",
"items/suitchargeno1.wav",
"buttons/lightswitch2.wav",
"buttons/button3.wav",
"buttons/button2.wav"
}

new keysmenu = (1<<0|1<<1|1<<2|1<<3|1<<4|1<<5|1<<6|1<<7|1<<8|1<<9)
new mainmenu[556]
new mapname[32]
new mapfile[64]

public plugin_init() {
	register_cvar("ha_healthcharger_renewtime", "30")
	register_cvar("ha_armorcharger_renewtime", "30")
	register_cvar("ha_healthcharger_health", "100")
	register_cvar("ha_armorcharger_armor", "100")
	register_cvar("ha_playerhealth", "100")
	register_cvar("ha_playerarmor", "100")
	register_concmd("ha_menu", "ha_menu_cmd")
	register_concmd("ha_create_healthcharger", "ha_create_healthcharger")
	register_concmd("ha_create_armorcharger", "ha_create_armorcharger")
	get_mapname(mapname, 31)
	format(mapfile, 63, "maps/%s_charges.cfg",mapname)
	register_menucmd(register_menuid("\rМеню Health и Armor Charger"), keysmenu, "commandsmenu")
	register_forward(FM_PlayerPreThink, "forward_playerprethink")
	register_event("HLTV", "renew_all_charges", "a", "1=0", "2=0")
	register_plugin("Health End Armor Charges", "PahanCS", "2.0")
	if(file_exists(mapfile)) 
		server_cmd("exec %s",mapfile)
}

public plugin_precache() {
	register_forward(FM_Spawn, "forward_spawn")
	if(file_exists("models/healthcharger.mdl")) {
		precache_model("models/healthcharger.mdl")
		is_precachedmodel[MDL_HEL] = 1
	}
	if(file_exists("models/armorcharger.mdl")) {
		precache_model("models/armorcharger.mdl")
		is_precachedmodel[MDL_ARM] = 1
	}
	for(new p=0; p<SND_MAX; p++) {
		static soundfile[256]
		format(soundfile, 255, "sound/%s",precachesound[p])
		if(!file_exists(soundfile)) 
			continue
		precache_sound(precachesound[p])
		is_precachedsound[p] = 1
	}
}

public ha_menu_cmd(id) {
	if(id != 1) {
		if(is_precachedsound[SND_BNO] == 1) 
			client_cmd(id, "spk %s",precachesound[SND_BNO])
		return PLUGIN_CONTINUE
	}
	ha_menu(id)
	if(is_precachedsound[SND_BOK] == 1) 
		client_cmd(id, "spk %s",precachesound[SND_BOK])
	return PLUGIN_CONTINUE
}

public ha_menu(id) {
	format(mainmenu, 555, "\rМеню Health и Armor Charger^n^n")
	add(mainmenu, 555, "\r1. \wСоздать Health Charger^n")
	add(mainmenu, 555, "\r2. \wСоздать Armor Charger^n")
	add(mainmenu, 555, "\r3. \wСохранить файл текущей катры^n")
	add(mainmenu, 555, "\r4. \wУдалить текущий Health Charger^n")
	add(mainmenu, 555, "\r5. \wУдалить текущий Armor Charger^n")
	add(mainmenu, 555, "\r6. \wУдалить все Health Chargers^n")
	add(mainmenu, 555, "\r7. \wУдалить все Armor Chargers^n")
	add(mainmenu, 555, "\r8. \wУдалить файл текущей катры^n")
	add(mainmenu, 555, "\r9. \wЗагрузить файл текущей катры^n^n")
	add(mainmenu, 555, "\r0. \wВыход^n")
	show_menu(id, keysmenu, mainmenu, -1)
	return PLUGIN_CONTINUE
}

public commandsmenu(id, key) {
	switch(key) {
		case 0: {create_healthcharger(id); ha_menu(id);}
		case 1: {create_armorcharger(id); ha_menu(id);}
		case 2: {save_current_mapfile(id); ha_menu(id);}
		case 3: {delete_healthcharger(id); ha_menu(id);}
		case 4: {delete_armorcharger(id); ha_menu(id);}
		case 5: {delete_all_healthcharger(id); ha_menu(id);}
		case 6: {delete_all_armorcharger(id); ha_menu(id);}
		case 7: {delete_current_mapfile(id); ha_menu(id);}
		case 8: {load_mapfile(id); ha_menu(id);}
		case 9: {if(is_precachedsound[SND_BOK] == 1) 
					client_cmd(id, "spk %s",precachesound[SND_BOK]);}	//exit 
	}
	return PLUGIN_CONTINUE
}

public forward_spawn(ent) {
	if(!pev_valid(ent)) 
		return FMRES_IGNORED
	static classname[21]
	pev(ent, pev_classname, classname, 20)
	if(equali(classname, "func_healthcharger")) {
		return FMRES_SUPERCEDE
	}
	else if(equali(classname, "func_recharge")) {
		return FMRES_SUPERCEDE
	}
	return FMRES_IGNORED
}

public forward_playerprethink(id) {
	if(!is_user_connected(id)) 
		return FMRES_IGNORED
	if(!is_user_alive(id)) 
		return FMRES_IGNORED
	if((pev(id, pev_button) & IN_USE) && !(pev(id, pev_oldbuttons) & IN_USE)) {
		static ent, iEnd[3], Float:fEnd[3]
		get_user_origin(id, iEnd, 3)
		IVecFVec(iEnd, fEnd)
		ent = fm_find_ent_in_sphere(-1, fEnd, 30.0)
		while(ent != 0) {
			if(!pev_valid(ent)) 
				return FMRES_IGNORED
			static classname[21]
			pev(ent, pev_classname, classname, 20)
			if(equali(classname, "health_charger")) {
				static Float:fOrigin[3], Float:fOriginent[3]
				pev(id, pev_origin, fOrigin)
				pev(ent, pev_origin, fOriginent)
				if(get_distance_f(fOrigin, fOriginent) > 60.0) 
					return FMRES_IGNORED
				if(!fm_is_ent_visible(id, ent)) 
					return FMRES_IGNORED
				static Float:healthid, Float:healthent
				pev(id, pev_health, healthid)
				pev(ent, pev_health, healthent)
				if(healthent <= 0.0) {
					if(is_precachedsound[SND_HNO] == 1) 
						client_cmd(id, "spk %s",precachesound[SND_HNO])
					return FMRES_IGNORED
				}
				if(get_cvar_float("ha_playerhealth") <= 0.0) {
					if(is_precachedsound[SND_CNO] == 1) 
						client_cmd(id, "spk %s",precachesound[SND_CNO])
					return FMRES_IGNORED
				}
				if(get_cvar_float("ha_playerhealth") > 255.0) 
					set_cvar_num("ha_playerhealth", 255)
				if(healthid >= get_cvar_float("ha_playerhealth")) {
					if(is_precachedsound[SND_CNO] == 1) 
						client_cmd(id, "spk %s",precachesound[SND_CNO])
					return FMRES_IGNORED
				}
				if(healthid + healthent > get_cvar_float("ha_playerhealth")) {
					if(is_precachedsound[SND_HOK] == 1) 
						client_cmd(id, "spk %s",precachesound[SND_HOK])
					set_pev(ent, pev_health, (healthid + healthent) - get_cvar_float("ha_playerhealth"))
					set_pev(id, pev_health, get_cvar_float("ha_playerhealth"))
				}
				else {
					if(is_precachedsound[SND_HOK] == 1) 
						client_cmd(id, "spk %s",precachesound[SND_HOK])
					set_pev(id, pev_health, healthid + healthent)
					set_pev(ent, pev_health, 0.0)
					set_pev(ent, pev_body, 1)
					if(get_cvar_float("ha_healthcharger_renewtime") >= 1.0) 
						set_task(get_cvar_float("ha_healthcharger_renewtime"),"renew_health_charger",ent,"",0,"a",1)
				}
				return FMRES_IGNORED
			}
			if(equali(classname, "armor_charger")) {
				static Float:fOrigin[3], Float:fOriginent[3]
				pev(id, pev_origin, fOrigin)
				pev(ent, pev_origin, fOriginent)
				if(get_distance_f(fOrigin, fOriginent) > 60.0) 
					return FMRES_IGNORED
				if(!fm_is_ent_visible(id, ent)) 
					return FMRES_IGNORED
				static Float:armorvalueid, Float:armorvalueent
				pev(id, pev_armorvalue, armorvalueid)
				pev(ent, pev_armorvalue, armorvalueent)
				if(armorvalueent <= 0.0) {
					if(is_precachedsound[SND_ANO] == 1) 
						client_cmd(id, "spk %s",precachesound[SND_ANO])
					return FMRES_IGNORED
				}
				if(get_cvar_float("ha_playerarmor") <= 0.0) {
					if(is_precachedsound[SND_CNO] == 1) 
						client_cmd(id, "spk %s",precachesound[SND_CNO])
					return FMRES_IGNORED
				}
				if(get_cvar_float("ha_playerarmor") > 255.0) 
					set_cvar_num("ha_playerarmor", 255)
				if(armorvalueid >= get_cvar_float("ha_playerarmor")) {
					if(is_precachedsound[SND_CNO] == 1) 
						client_cmd(id, "spk %s",precachesound[SND_CNO])
					return FMRES_IGNORED
				}
				if(armorvalueid + armorvalueent > get_cvar_float("ha_playerarmor")) {
					if(is_precachedsound[SND_AOK] == 1) 
						client_cmd(id, "spk %s",precachesound[SND_AOK])
					set_pev(ent, pev_armorvalue, (armorvalueid + armorvalueent) - get_cvar_float("ha_playerarmor"))
					set_pev(id, pev_armorvalue, get_cvar_float("ha_playerarmor"))
				}
				else {
					if(is_precachedsound[SND_AOK] == 1) 
						client_cmd(id, "spk %s",precachesound[SND_AOK])
					set_pev(id, pev_armorvalue, armorvalueid + armorvalueent)
					set_pev(ent, pev_armorvalue, 0.0)
					set_pev(ent, pev_body, 1)
					if(get_cvar_float("ha_armorcharger_renewtime") >= 1.0) 
						set_task(get_cvar_float("ha_armorcharger_renewtime"),"renew_armor_charger",ent,"",0,"a",1)
				}
			}
			ent = fm_find_ent_in_sphere(ent, fEnd, 30.0)
		}
	}
	return FMRES_IGNORED
}

public renew_health_charger(ent) {
	if(!pev_valid(ent)) 
		return FMRES_IGNORED
	if(get_cvar_float("ha_healthcharger_health") <= 0.0) 
		return FMRES_IGNORED
	set_pev(ent, pev_health, get_cvar_float("ha_healthcharger_health"))
	set_pev(ent, pev_body, 0)
	return FMRES_IGNORED
}

public renew_armor_charger(ent) {
	if(!pev_valid(ent)) 
		return FMRES_IGNORED
	if(get_cvar_float("ha_armorcharger_armor") <= 0.0) 
		return FMRES_IGNORED
	set_pev(ent, pev_armorvalue, get_cvar_float("ha_armorcharger_armor"))
	set_pev(ent, pev_body, 0)
	return FMRES_IGNORED
}

public create_healthcharger(id) {
	static iOrigin[3], Float:fOrigin[3], Float:fAngles[3]
	get_user_origin(id, iOrigin, 3)
	pev(id, pev_angles, fAngles)
	IVecFVec(iOrigin, fOrigin)
	healthcharger_create(id, fOrigin, fAngles, true)
	return PLUGIN_CONTINUE
}

public create_armorcharger(id) {
	static iOrigin[3], Float:fOrigin[3], Float:fAngles[3]
	get_user_origin(id, iOrigin, 3)
	pev(id, pev_angles, fAngles)
	IVecFVec(iOrigin, fOrigin)
	armorcharger_create(id, fOrigin, fAngles, true)
	return PLUGIN_CONTINUE
}

public delete_healthcharger(id) {
	static iOrigin[3], Float:fOrigin[3], Float:fAngles[3], ent
	get_user_origin(id, iOrigin, 3)
	pev(id, pev_angles, fAngles)
	IVecFVec(iOrigin, fOrigin)
	ent = fm_find_ent_in_sphere(-1, fOrigin, 30.0)
	while(ent != 0) {
		if(pev_valid(ent)) {
			static classname[21]
			pev(ent, pev_classname, classname, 20)
			if(equali(classname, "health_charger")) {
				if(task_exists(ent)) 
					remove_task(ent)
				fm_remove_entity(ent)
				if(is_precachedsound[SND_BOK] == 1) 
					client_cmd(id, "spk %s",precachesound[SND_BOK])
				return PLUGIN_CONTINUE
			}
		}
		ent = fm_find_ent_in_sphere(ent, fOrigin, 30.0)
	}
	if(is_precachedsound[SND_BNO] == 1) 
		client_cmd(id, "spk %s",precachesound[SND_BNO])
	return PLUGIN_CONTINUE
}

public delete_armorcharger(id) {
	static iOrigin[3], Float:fOrigin[3], Float:fAngles[3], ent
	get_user_origin(id, iOrigin, 3)
	pev(id, pev_angles, fAngles)
	IVecFVec(iOrigin, fOrigin)
	ent = fm_find_ent_in_sphere(-1, fOrigin, 30.0)
	while(ent != 0) {
		if(pev_valid(ent)) {
			static classname[21]
			pev(ent, pev_classname, classname, 20)
			if(equali(classname, "armor_charger")) {
				if(task_exists(ent)) 
					remove_task(ent)
				fm_remove_entity(ent)
				if(is_precachedsound[SND_BOK] == 1) 
					client_cmd(id, "spk %s",precachesound[SND_BOK])
				return PLUGIN_CONTINUE
			}
		}
		ent = fm_find_ent_in_sphere(ent, fOrigin, 30.0)
	}
	if(is_precachedsound[SND_BNO] == 1) 
		client_cmd(id, "spk %s",precachesound[SND_BNO])
	return PLUGIN_CONTINUE
}

public save_current_mapfile(id) {
	if(file_exists(mapfile)) 
		delete_file(mapfile)
	static fent, countH, countA
	countH = 0
	countA = 0
	write_file(mapfile, "// Do not change nor what^n")
	fent = fm_find_ent_by_class(-1,"health_charger")
	while(fent != 0) {
		write_mapfile(fent, "ha_create_healthcharger")
		countH += 1
		fent = fm_find_ent_by_class(fent,"health_charger")
	}
	fent = fm_find_ent_by_class(-1,"armor_charger")
	while(fent != 0) {
		write_mapfile(fent, "ha_create_armorcharger")
		countA += 1
		fent = fm_find_ent_by_class(fent,"armor_charger")
	}
	if(countH > 0 || countA > 0) {
		if(is_precachedsound[SND_BOK] == 1) 
			client_cmd(id, "spk %s",precachesound[SND_BOK])
		return PLUGIN_CONTINUE
	}
	if(is_precachedsound[SND_BNO] == 1) 
		client_cmd(id, "spk %s",precachesound[SND_BNO])
	return PLUGIN_CONTINUE
}

public write_mapfile(ent, command[]) {
	static Float:fOrigin[3], Float:fAngles[3], fstr[256]
	pev(ent, pev_origin, fOrigin)
	pev(ent, pev_angles, fAngles)
	format(fstr, 255, "%s %f %f %f %f %f %f",command,fOrigin[0],fOrigin[1],fOrigin[2],fAngles[0],fAngles[1],fAngles[2])
	write_file(mapfile, fstr)
	return PLUGIN_CONTINUE
}

public ha_create_healthcharger(id) {
	if(id != 1) 
		return PLUGIN_CONTINUE
	static cmd1[32], cmd2[32], cmd3[32], cmd4[32], cmd5[32], cmd6[32]
	static Float:fOrigin[3], Float:fAngles[3]
	read_argv(1, cmd1, 31)
	read_argv(2, cmd2, 31)
	read_argv(3, cmd3, 31)
	read_argv(4, cmd4, 31)
	read_argv(5, cmd5, 31)
	read_argv(6, cmd6, 31)
	fOrigin[0] = str_to_float(cmd1)
	fOrigin[1] = str_to_float(cmd2)
	fOrigin[2] = str_to_float(cmd3)
	fAngles[0] = str_to_float(cmd4)
	fAngles[1] = str_to_float(cmd5)
	fAngles[2] = str_to_float(cmd6)
	healthcharger_create(id, fOrigin, fAngles, false)
	return PLUGIN_CONTINUE
}

public ha_create_armorcharger(id) {
	if(id != 1) 
		return PLUGIN_CONTINUE
	static cmd1[32], cmd2[32], cmd3[32], cmd4[32], cmd5[32], cmd6[32]
	static Float:fOrigin[3], Float:fAngles[3]
	read_argv(1, cmd1, 31)
	read_argv(2, cmd2, 31)
	read_argv(3, cmd3, 31)
	read_argv(4, cmd4, 31)
	read_argv(5, cmd5, 31)
	read_argv(6, cmd6, 31)
	fOrigin[0] = str_to_float(cmd1)
	fOrigin[1] = str_to_float(cmd2)
	fOrigin[2] = str_to_float(cmd3)
	fAngles[0] = str_to_float(cmd4)
	fAngles[1] = str_to_float(cmd5)
	fAngles[2] = str_to_float(cmd6)
	armorcharger_create(id, fOrigin, fAngles, false)
	return PLUGIN_CONTINUE
}

public healthcharger_create(id, Float:fOrigin[3], Float:fAngles[3], bool:enablesound) {
	if(is_precachedmodel[MDL_HEL] != 1) {
		if(enablesound == true && is_precachedsound[SND_BNO] == 1) 
			client_cmd(id, "spk %s",precachesound[SND_BNO])
		return PLUGIN_CONTINUE
	}
	static ent
	ent = fm_find_ent_in_sphere(-1, fOrigin, 30.0)
	while(ent != 0) {
		if(pev_valid(ent)) {
			static classname[21]
			pev(ent, pev_classname, classname, 20)
			if(equali(classname, "health_charger")) {
				if(enablesound == true && is_precachedsound[SND_BNO] == 1) 
					client_cmd(id, "spk %s",precachesound[SND_BNO])
				return PLUGIN_CONTINUE
			}
			else if(equali(classname, "armor_charger")) {
				if(enablesound == true && is_precachedsound[SND_BNO] == 1) 
					client_cmd(id, "spk %s",precachesound[SND_BNO])
				return PLUGIN_CONTINUE
			}
			else if(equali(classname, "player")) {
				if(enablesound == true && is_precachedsound[SND_BNO] == 1) 
					client_cmd(id, "spk %s",precachesound[SND_BNO])
				return PLUGIN_CONTINUE
			}
			else if(equali(classname, "hostage_entity")) {
				if(enablesound == true && is_precachedsound[SND_BNO] == 1) 
					client_cmd(id, "spk %s",precachesound[SND_BNO])
				return PLUGIN_CONTINUE
			}
		}
		ent = fm_find_ent_in_sphere(ent, fOrigin, 30.0)
	}
	ent = fm_create_entity("info_target")
	if(!ent) {
		if(enablesound == true && is_precachedsound[SND_BNO] == 1) 
			client_cmd(id, "spk %s",precachesound[SND_BNO])
		return PLUGIN_CONTINUE
	}
	static Float:mins[3], Float:maxs[3]
	mins[0] = -10.0; maxs[0] = 10.0;
	mins[1] = -10.0; maxs[1] = 10.0;
	mins[2] = -50.0; maxs[2] = 50.0;
	set_pev(ent, pev_classname, "health_charger")
	set_pev(ent, pev_origin, fOrigin)
	set_pev(ent, pev_angles, fAngles)
	set_pev(ent, pev_movetype, MOVETYPE_PUSHSTEP)
	set_pev(ent, pev_dmg_take, DAMAGE_NO)
	fm_entity_set_model(ent, "models/healthcharger.mdl")
	if(get_cvar_float("ha_healthcharger_health") <= 0.0) {
		set_pev(ent, pev_health, 0.0)
		set_pev(ent, pev_body, 1)
	}
	else {
		set_pev(ent, pev_health, get_cvar_float("ha_healthcharger_health"))
	}
	set_pev(ent, pev_solid, SOLID_BBOX)
	fm_entity_set_size(ent, mins, maxs)
	if(enablesound == true && is_precachedsound[SND_BOK] == 1) 
		client_cmd(id, "spk %s",precachesound[SND_BOK])
	return PLUGIN_CONTINUE
}

public armorcharger_create(id, Float:fOrigin[3], Float:fAngles[3], bool:enablesound) {
	if(is_precachedmodel[MDL_ARM] != 1) {
		if(enablesound == true && is_precachedsound[SND_BNO] == 1) 
			client_cmd(id, "spk %s",precachesound[SND_BNO])
		return PLUGIN_CONTINUE
	}
	static ent
	ent = fm_find_ent_in_sphere(-1, fOrigin, 30.0)
	while(ent != 0) {
		if(pev_valid(ent)) {
			static classname[21]
			pev(ent, pev_classname, classname, 20)
			if(equali(classname, "health_charger")) {
				if(enablesound == true && is_precachedsound[SND_BNO] == 1) 
					client_cmd(id, "spk %s",precachesound[SND_BNO])
				return PLUGIN_CONTINUE
			}
			else if(equali(classname, "armor_charger")) {
				if(enablesound == true && is_precachedsound[SND_BNO] == 1) 
					client_cmd(id, "spk %s",precachesound[SND_BNO])
				return PLUGIN_CONTINUE
			}
			else if(equali(classname, "player")) {
				if(enablesound == true && is_precachedsound[SND_BNO] == 1) 
					client_cmd(id, "spk %s",precachesound[SND_BNO])
				return PLUGIN_CONTINUE
			}
			else if(equali(classname, "hostage_entity")) {
				if(enablesound == true && is_precachedsound[SND_BNO] == 1) 
					client_cmd(id, "spk %s",precachesound[SND_BNO])
				return PLUGIN_CONTINUE
			}
		}
		ent = fm_find_ent_in_sphere(ent, fOrigin, 30.0)
	}
	ent = fm_create_entity("info_target")
	if(!ent) {
		if(enablesound == true && is_precachedsound[SND_BNO] == 1) 
			client_cmd(id, "spk %s",precachesound[SND_BNO])
		return PLUGIN_CONTINUE
	}
	static Float:mins[3], Float:maxs[3]
	mins[0] = -10.0; maxs[0] = 10.0;
	mins[1] = -10.0; maxs[1] = 10.0;
	mins[2] = -50.0; maxs[2] = 50.0;
	set_pev(ent, pev_classname, "armor_charger")
	set_pev(ent, pev_origin, fOrigin)
	set_pev(ent, pev_angles, fAngles)
	set_pev(ent, pev_movetype, MOVETYPE_PUSHSTEP)
	set_pev(ent, pev_dmg_take, DAMAGE_NO)
	fm_entity_set_model(ent, "models/armorcharger.mdl")
	if(get_cvar_float("ha_armorcharger_armor") <= 0.0) {
		set_pev(ent, pev_armorvalue, 0.0)
		set_pev(ent, pev_body, 1)
	}
	else {
		set_pev(ent, pev_armorvalue, get_cvar_float("ha_armorcharger_armor"))
	}
	set_pev(ent, pev_solid, SOLID_BBOX)
	fm_entity_set_size(ent, mins, maxs)
	if(enablesound == true && is_precachedsound[SND_BOK] == 1) 
		client_cmd(id, "spk %s",precachesound[SND_BOK])
	return PLUGIN_CONTINUE
}

public delete_all_healthcharger(id) {
	static fent, countH
	countH = 0
	fent = fm_find_ent_by_class(-1,"health_charger")
	while(fent != 0) {
		if(task_exists(fent)) 
			remove_task(fent)
		fm_remove_entity(fent)
		countH += 1
		fent = fm_find_ent_by_class(fent,"health_charger")
	}
	if(countH > 0) {
		if(is_precachedsound[SND_BOK] == 1) 
			client_cmd(id, "spk %s",precachesound[SND_BOK])
		return PLUGIN_CONTINUE
	}
	if(is_precachedsound[SND_BNO] == 1) 
		client_cmd(id, "spk %s",precachesound[SND_BNO])
	return PLUGIN_CONTINUE
}

public delete_all_armorcharger(id) {
	static fent, countA
	countA = 0
	fent = fm_find_ent_by_class(-1,"armor_charger")
	while(fent != 0) {
		if(task_exists(fent)) 
			remove_task(fent)
		fm_remove_entity(fent)
		countA += 1
		fent = fm_find_ent_by_class(fent,"armor_charger")
	}
	if(countA > 0) {
		if(is_precachedsound[SND_BOK] == 1) 
			client_cmd(id, "spk %s",precachesound[SND_BOK])
		return PLUGIN_CONTINUE
	}
	if(is_precachedsound[SND_BNO] == 1) 
		client_cmd(id, "spk %s",precachesound[SND_BNO])
	return PLUGIN_CONTINUE
}

public delete_current_mapfile(id) {
	if(file_exists(mapfile)) {
		delete_file(mapfile)
		if(is_precachedsound[SND_BOK] == 1) 
			client_cmd(id, "spk %s",precachesound[SND_BOK])
		return PLUGIN_CONTINUE
	}
	if(is_precachedsound[SND_BNO] == 1) 
		client_cmd(id, "spk %s",precachesound[SND_BNO])
	return PLUGIN_CONTINUE
}

public renew_all_charges() {
	static fent
	fent = fm_find_ent_by_class(-1,"health_charger")
	while(fent != 0) {
		if(get_cvar_float("ha_healthcharger_health") <= 0.0) {
			set_pev(fent, pev_health, 0.0)
			set_pev(fent, pev_body, 1)
		}
		else {
			set_pev(fent, pev_health, get_cvar_float("ha_healthcharger_health"))
			set_pev(fent, pev_body, 0)
		}
		fent = fm_find_ent_by_class(fent,"health_charger")
	}
	fent = fm_find_ent_by_class(-1,"armor_charger")
	while(fent != 0) {
		if(get_cvar_float("ha_armorcharger_armor") <= 0.0) {
			set_pev(fent, pev_armorvalue, 0.0)
			set_pev(fent, pev_body, 1)
		}
		else {
			set_pev(fent, pev_armorvalue, get_cvar_float("ha_armorcharger_armor"))
			set_pev(fent, pev_body, 0)
		}
		fent = fm_find_ent_by_class(fent,"armor_charger")
	}
}

public load_mapfile(id) {
	if(file_exists(mapfile)) {
		server_cmd("exec %s",mapfile)
		if(is_precachedsound[SND_BOK] == 1) 
			client_cmd(id, "spk %s",precachesound[SND_BOK])
	}
	else {
		if(is_precachedsound[SND_BNO] == 1) 
			client_cmd(id, "spk %s",precachesound[SND_BNO])
	}
	return PLUGIN_CONTINUE
}

stock bool:fm_is_ent_visible(index, entity, ignoremonsters = 0) {
	static Float:start[3], Float:dest[3], Float:fraction
	pev(index, pev_origin, start)
	pev(index, pev_view_ofs, dest)
	xs_vec_add(start, dest, start)
	pev(entity, pev_origin, dest)
	engfunc(EngFunc_TraceLine, start, dest, ignoremonsters, index, 0)
	get_tr2(0, TR_flFraction, fraction)
	if (fraction == 1.0 || get_tr2(0, TR_pHit) == entity)
		return true
	return false
}

stock xs_vec_add(const Float:in1[], const Float:in2[], Float:out[]) {
	out[0] = in1[0] + in2[0];
	out[1] = in1[1] + in2[1];
	out[2] = in1[2] + in2[2];
}
