bone_indexes = {}

function hit_check(obj, amount, local_direction, who, bone_index)
	--if (bone_index==14 or bone_index==15 or bone_index==16 or bone_index==17 or bone_index==19) and who:id()==db.actor:id() then
		--db.actor:give_game_news("Хэдшот! ", "Ты меткий стрелок!!!", "ui_iconsTotal_grouping", 0, 5000)
		--xr_sound.set_sound_play(db.actor:id(), "pda_news")		
	--end

    local id = obj:id()
	local snpc = alife():object(id)
	if snpc then
		if not snpc.bonex then
			local bon = snpc.bon
			if bon then
				bone_indexes[id] = bon
			end
			snpc.bonex = true
		end
		
		local bones	= {	
			--  голова
			{14,15,16,17,19},
			--  таз, торс, спина
			{2,11,12,13,20,33},
			--  бёдра, голени
			{3,7,4,8},
			--  плечи, предплечья
			{21,34,22,35},
			--  ступни, кисти
			{5,6,9,10,23,24,27,30,36,37,40,43}
		}
		if bone_indexes[id]==nil then
			bone_indexes[id] = {}
		end
		
		for i=1,#bones do
			if bone_indexes[id][i]==nil then
				bone_indexes[id][i] = 1
				break
			end
			local tbon = bones[i]
			for j=1,#tbon do
			   if tbon[j] == bone_index then 
					bone_indexes[id][i] = bone_indexes[id][i] + 1
				end
			end
		end
		snpc.bon = bone_indexes[id]
   end
end

--  куртки
local table_jacket_outfits = {
stalker_bandit_1 = true,
stalker_neutral_1 = true,
stalker_neutral_1_face_1 = true,
stalker_neutral_1_face_2 = true,
stalker_neutral_1_face_3 = true,
stalker_hero_1 = true,
stalker_hero_stc_strelok = true,
stalker_zombied_1 = true,
stalker_hero_novice_1 = true
	}

--  плащи	
local table_rain_outfits = {
stalker_bandit_3 = true,
stalker_bandit_3_face_1 = true,
stalker_bandit_3_face_2 = true,
stalker_bandit_3_face_3 = true,
stalker_bandit_3_mask = true,
stalker_bandit_4 = true
	}

--  костюмы без шлема или противогаза	
local table_without_helmet_outfits = {
stalker_bandit_2 = true,
stalker_merc_2_mask = true,
stalker_freedom_1 = true,
stalker_nebo_1 = true,
stalker_soldier_1 = true,
stalker_soldier_2_face_1 = true,
stalker_soldier_2_face_1_mask = true,
stalker_soldier_1_face_1 = true,
stalker_soldier_3_face_1 = true,
stalker_soldier_ecolog_face_1 = true,
stalker_dolg_1_face_1 = true,
stalker_dolg_2_face_1 = true,
stalker_dolg_2_face_2 = true,
stalker_dolg_2_mask = true,
stalker_dolg_3_face_1 = true,
stalker_freedom_1_face_1 = true,
stalker_freedom_2_face_1 = true,
stalker_freedom_2_face_2 = true,
stalker_freedom_2_mask = true,
stalker_freedom_3_face_1 = true,
stalker_nebo_2_face_1 = true,
stalker_nebo_2_face_2 = true,
stalker_nebo_2_face_3 = true,
stalker_nebo_2_face_4 = true,
stalker_nebo_2_mask = true,
stalker_neutral_2_face_1 = true,
stalker_neutral_2_face_2 = true,
stalker_neutral_2_face_3 = true,
stalker_neutral_2_face_4 = true,
stalker_neutral_2_face_5 = true,
stalker_neutral_2_face_6 = true,
stalker_neutral_2_face_7 = true,
stalker_neutral_2_mask = true,
stalker_neutral_3_face_1 = true,
stalker_zombied_2 = true,
stalker_zombied_3 = true,
stalker_zombied_4 = true,
stalker_hero_cs_heavy = true,
stalker_hero_dolg_1 = true,
stalker_hero_dolg_2 = true,
stalker_hero_freedom_1 = true,
stalker_hero_freedom_2 = true,
stalker_hero_military = true,
stalker_hero_neutral_nauchniy = true,
stalker_hero_specops = true,
stalker_hero_stalker_1 = true,
stalker_monolith_1_face_1 = true,
stalker_neutral_nauchniy_face_1 = true,
stalker_neutral_nauchniy_face_2 = true,
stalker_neutral_nauchniy_face_3 = true
	}
	
--  костюмы со шлемом или противогазом
local table_with_helmet_outfits = {
stalker_bandit_2_gas = true,
stalker_dolg_2 = true,
stalker_dolg_3 = true,
stalker_freedom_2 = true,
stalker_freedom_3 = true,
stalker_merc_2 = true,
stalker_monolith_2 = true,
stalker_nebo_2 = true,
stalker_nebo_3 = true,
stalker_neutral_2 = true,
stalker_soldier_2 = true,
stalker_soldier_3 = true,
stalker_soldier_4 = true
	}

--  костюмы с замкнутым циклом
local table_cycle_outfits = {
mp_team_blue_armor_sci = true,
stalker_monolith_1 = true,
stalker_dolg_1 = true,
stalker_freedom_5 = true,
stalker_neutral_nauchniy = true,
stalker_monolith_3 = true,
stalker_neutral_3 = true,
stalker_soldier_ecolog_face_1 = true,
stalker_ecolog_military = true
	}

-- экзоскелеты
local table_exo_outfits = {
stalker_dolg_4 = true,
stalker_freedom_4 = true,
stalker_merc_4 = true,
stalker_monolith_4 = true,
stalker_neutral_4 = true,
stalker_hero_exo = true
	}
	
-- шлемы
local table_helmets = {
helm_respirator = true,
helm_respirator_gp7 = true,
helm_hardhat = true,
helm_protective = true,
helm_battle = true,
helm_battle_dolg = true,
helm_tactic = true
}
	
function count_cond(id,vis,rnd)
	local wear_o
	local wear_h
	if not rnd then
		local snpc = alife():object(id)
		if snpc then
		
			local bon = snpc.bon
			if bon then
				bone_indexes[id] = bon
			end
			
			local tabl = bone_indexes[id]
			if tabl and #tabl>0 then
				local wear1,wear2,wear3,wear4,wear5 = 0,0,0,0,0
				
				if table_with_helmet_outfits[vis] then				
					if table_helmets[get_helmet_sect(vis)] then
						if tabl[1] then wear_h = tabl[1]*30 end
						if tabl[2] then wear_o = tabl[2]*20 end
					else
						if tabl[1] then wear_1 = tabl[1]*30 end
						if tabl[2] then wear_2 = tabl[2]*20 end
						wear_o = wear1 + wear2
					end
			
				elseif table_jacket_outfits[vis] then					
					if tabl[2] then wear2 = tabl[2]*20 end
					if tabl[5] then wear5 = tabl[5]*5 end 
					wear_o = wear2 + wear5
					
				elseif table_rain_outfits[vis] then					
					if tabl[2] then wear2 = tabl[2]*20 end
					if tabl[3] then wear3 = tabl[3]*10 end
					if tabl[5] then wear5 = tabl[5]*5 end 
					wear_o = wear2 + wear3 + wear5
			
				elseif table_without_helmet_outfits[vis] then					
					if tabl[2] then wear_o = tabl[2]*20 end
			
				elseif table_cycle_outfits[vis] then					
					if tabl[1] then wear1 = tabl[1]*35 end
					if tabl[2] then wear2 = tabl[2]*20 end
					if tabl[3] then wear3 = tabl[3]*10 end
					if tabl[4] then wear4 = tabl[4]*10 end
					if tabl[5] then wear5 = tabl[5]*5 end 
					wear_o = wear1 + wear2 + wear3 + wear4 + wear5
						
				elseif table_exo_outfits[vis] then					
					if tabl[1] then wear1 = tabl[1]*25 end
					if tabl[2] then wear2 = tabl[2]*15 end
					if tabl[3] then wear3 = tabl[3]*15 end
					if tabl[4] then wear4 = tabl[4]*15 end
					if tabl[5] then wear5 = tabl[5]*5 end 
					wear_o = (wear1 + wear2 + wear3 + wear4 + wear5)
								
				end
				
				bone_indexes[id] = nil
				snpc.bon = {}
			end
		end
	end	
	
	local tmp  = {}

	if	   rnd  			then wear_o = math.random(25,65)
	elseif wear_o == nil  	then wear_o = math.random(5,10)
	elseif wear_o <  5  	then wear_o = math.random(10,15)
	elseif wear_o >  95 	then wear_o = math.random(85,90)
	end
	tmp.cond_o = (100-wear_o)/100
	
	if	   rnd  			then wear_h = math.random(25,65)
	elseif wear_h == nil  	then wear_h = math.random(5,10)
	elseif wear_h <  5  	then wear_h = math.random(10,15)
	elseif wear_h >  95 	then wear_h = math.random(85,90)
	end
	tmp.cond_h = (100-wear_h)/100
		
	return tmp or nil
end

function dead_change(item)
	local sobj = alife():object(item:id())
	if sobj and sobj.imm and sobj.imm==1 then
		local npc_id = sobj.pid
		local snpc = alife():object(npc_id)
		if snpc then
			snpc.offmoney = -1
			if xrs_armor.armors[npc_id] == nil then
				xrs_armor.armors[npc_id] = {}
			end
			table.insert(xrs_armor.armors[npc_id],sobj)
			sobj.pid = nil
			sobj.imm = nil
		end
	end
end

function spawn_outfit(npc_id,rnd)
	local snpc = alife():object(npc_id)
	if snpc then --and snpc:community() ~= "zombied" then
		local sect_o,sect_h,vis
		local vis = imm.read_stalker_params_visual(snpc)
		if vis then
			vis = imm.str_explode("\\",vis)
			vis = vis[3]
			if vis then
				sect_o = xrs_armor.get_current_visual(vis)
				sect_h = get_helmet_sect(vis)
				if sect_o then 
					local soutf,shelm,trans_o,trans_h
					if not rnd then
						local actor = db.actor
						local npc = level.object_by_id(npc_id)
						if npc and actor then			
							local upgr_o = snpc.upgo
							if upgr_o then
								soutf = alife():create(sect_o, actor:position():add(actor:direction():mul(-1)), snpc.m_level_vertex_id, snpc.m_game_vertex_id)
								if soutf then
									local to = imm.read_outfit_params(soutf)
									if to then 
										to.upgrades = upgr_o
										imm.write_outfit_params(to,soutf)
									end
									trans_o = true
								end
							end
							if not soutf then
								soutf = alife():create(sect_o, snpc.position, snpc.m_level_vertex_id, snpc.m_game_vertex_id, npc_id)
							end
							if sect_h then
								local upgr_h = snpc.upgh
								if upgr_h then
									shelm = alife():create(sect_h, actor:position():add(actor:direction():mul(-1)), snpc.m_level_vertex_id, snpc.m_game_vertex_id)
									if shelm then
										local th = imm.read_outfit_params(shelm)
										if th then 
											th.upgrades = upgr_h
											imm.write_outfit_params(th,shelm)
										end
										trans_h = true
									end
								end
								if not shelm then
									shelm = alife():create(sect_h, snpc.position, snpc.m_level_vertex_id, snpc.m_game_vertex_id, npc_id)
								end
							end
						end
					else
						soutf = alife():create(sect_o, snpc.position, snpc.m_level_vertex_id, snpc.m_game_vertex_id, npc_id)
						if sect_h then
							shelm = alife():create(sect_h, snpc.position, snpc.m_level_vertex_id, snpc.m_game_vertex_id, npc_id)
						end
					end
					if soutf then
						soutf.imm = 1
						soutf.pid = npc_id
						local soutf_id = soutf.id
						if xrs_armor.armors[npc_id] == nil then
							xrs_armor.armors[npc_id] = {}
						end
						if xrs_armor.armors[npc_id][soutf_id] == nil then
							xrs_armor.armors[npc_id][soutf_id] = {}
						end
						xrs_armor.armors[npc_id][soutf_id].none = npc_id
						local tbl = count_cond(npc_id,vis,rnd)
						if tbl then
							local cond_o = tbl.cond_o
							local condold
							if cond_o then
								condold = snpc.cono
								if condold then 
									cond_o = cond_o-1+condold
									if cond_o <= 0 then
										cond_o = math.random(5,10)/100
									end
								end
								soutf.con = cond_o
							end							
							if shelm then
								shelm.pid = npc_id
								local cond_h = tbl.cond_h
								if cond_h then
									condold = snpc.conh
									if condold then 
										cond_h = cond_h-1+condold
										if cond_h <= 0 then
											cond_h = math.random(5,10)/100
										end
									end
									shelm.con = cond_h
								end
							end
						end
						if not rnd then
							if trans_o then
								imm.convert_npc[soutf_id]="transfer_outfit"
							end 
							if trans_h then
								imm.convert_npc[shelm.id]="transfer_helmet"
							end 
						end
						snpc.upgo = {}
						snpc.upgh = {}
						snpc.cono = nil
						snpc.conh = nil
						snpc.bon = {}
						if snpc.offmoney ~= -1 then
							snpc.offmoney = nil
						end
					end
				end
			end
		end
	end
end

function get_helmet_sect(vis)
	local helm
	if vis == "stalker_dolg_2" or vis == "stalker_freedom_2" or vis == "stalker_neutral_2" or vis == "stalker_monolith_2" then
		helm = "helm_respirator"
	elseif vis == "stalker_merc_2" or vis == "stalker_bandit_2_gas" then
		helm = "helm_respirator_gp7"
	elseif vis == "stalker_freedom_3" or vis == "stalker_soldier_2" then
		helm = "helm_hardhat"
	elseif vis == "stalker_soldier_3" or vis == "stalker_soldier_4" then
		helm = "helm_battle"
	elseif vis == "stalker_dolg_3" then
		helm = "helm_battle_dolg"
	--elseif vis == "stalker_nebo_2" then
		--helm = "helm_hardhat"
	end
	return helm
end

function get_helmet_sect_can(vis)
	local helm
	if string.find(vis,"stalker_dolg_2") or string.find(vis,"stalker_freedom_2") or string.find(vis,"stalker_neutral_2") or string.find(vis,"stalker_monolith_2") then
		helm = "helm_respirator"
	elseif string.find(vis,"stalker_merc_2") or string.find(vis,"stalker_bandit_2") then
		helm = "helm_respirator_gp7"
	elseif string.find(vis,"stalker_freedom_3") or string.find(vis,"stalker_soldier_2") then
		helm = "helm_hardhat"
	elseif string.find(vis,"stalker_soldier_3") or string.find(vis,"stalker_soldier_4") then
		helm = "helm_battle"
	elseif string.find(vis,"stalker_dolg_3") then
		helm = "helm_battle_dolg"
	--elseif string.find(vis,"stalker_nebo_2") then
		--helm = "helm_hardhat"
	end
	return helm
end
function get_npc_vis(helm,vis)
	if 	string.find(helm, "helm_respirator") then
		if     string.find(vis,"stalker_dolg_2")     	then vis = "stalker_dolg_2"
		elseif string.find(vis,"stalker_freedom_2")  	then vis = "stalker_freedom_2"
		elseif string.find(vis,"stalker_neutral_2")  	then vis = "stalker_neutral_2"
		elseif string.find(vis,"stalker_monolith_2")  	then vis = "stalker_monolith_2"
		end
	elseif string.find(helm, "helm_respirator_gp7") then
		if 	   string.find(vis,"stalker_merc_2") 		then vis = "stalker_merc_2"
		elseif string.find(vis,"stalker_bandit_2")  	then vis = "stalker_bandit_2_gas"
		end
	elseif string.find(helm, "helm_hardhat") then
		if     string.find(vis,"stalker_freedom_3")  then vis = "stalker_freedom_3"
		elseif string.find(vis,"stalker_soldier_2")  then vis = "stalker_soldier_2" 
		end
	elseif helm == "helm_battle" then
		if string.find(vis,"stalker_soldier_3")  then vis = "stalker_soldier_3"
		elseif string.find(vis,"stalker_soldier_4")  then vis = "stalker_soldier_4"
		end
	elseif helm == "helm_battle_dolg" then
		if     string.find(vis,"stalker_dolg_3")     then vis = "stalker_dolg_3"
		end
	--elseif helm == "helm_hardhat" then
		--if     string.find(vis,"stalker_nebo_2")  then vis = "stalker_nebo_2"
		--end
	--elseif helm == "helm_protective" then
		--if     string.find(vis,"stalker_dolg_1")     		then vis = "stalker_dolg_1"
		--elseif string.find(vis,"stalker_freedom_1")  		then vis = "stalker_freedom_1"
		--elseif string.find(vis,"stalker_neutral_nauchniy")  then vis = "stalker_neutral_nauchniy" 
		--elseif string.find(vis,"stalker_monolith_1")  		then vis = "stalker_monolith_1"
		--end
	end
	return vis
end