local ammo_types = {
	["ammo_9x18_fmj"] = true,
	["ammo_9x18_pbp"] = true,
	["ammo_9x18_pmm"] = true,
	["ammo_9x19_pbp"] = true,
	["ammo_9x19_fmj"] = true,
	["ammo_5.45x39_ap"] = true,
	["ammo_5.45x39_fmj"] = true,
	["ammo_5.56x45_ap"] = true,
	["ammo_5.56x45_ss190"] = true,
	["ammo_5.56x45_he"] = true,
	["ammo_5.7x28_ss190"] = true,
	["ammo_5.7x28_fmj"] = true,
	["ammo_5.7x28_ap"] = true,
	["ammo_7.62x54_ap"] = true,
	["ammo_7.62x54_7h14"] = true,
	["ammo_7.62x54_7h1"] = true,
	["ammo_357mag"] = true,
	["ammo_9x39_sp5"] = true,
	["ammo_9x39_ap"] = true,
	["ammo_9x39_pab9"] = true,
	["ammo_gauss"] = true,
	["ammo_11.43x23_fmj"] = true,
	["ammo_11.43x23_hydro"] = true,
	["ammo_12x76_dart"] = true,
	["ammo_12x76_zhekan"] = true,
	["ammo_12x70_buck"] = true,
	["ammo_11.43x23_eagle"] = true,
	["ammo_7.62x25"] = true,
	["ammo_vog-25p"] = true,
	["ammo_m209"] = true,
}
local ammo_fake_types = {}

local need_convert = false
local need_init = true
local need_update = false
local before_update = true

function on_update()
	if before_update then
		before_update = false
		need_convert = true
		--dbg.log("AMMO_CTRL: first update, may convert")		
	end
	
	if not need_update then return end
	--dbg.log("AMMO_CTRL: update")
	if not inventory.belt_status then return end
	--dbg.log("AMMO_CTRL: update 1")
	
	local t = {}
	db.actor:iterate_inventory(
		function(npc, item)
			if ammo_types[item:section()] and inventory.belt_id[item:id()] == nil then
				--dbg.log("AMMO_CTRL: find item %s", item:name())			
				table.insert(t, item:id())
			end
		end
		, db.actor)
		
	for i, id in ipairs(t) do
		local sim = alife()
		local obj = level.object_by_id(id)
		local cnt = get_ammo_size(obj)
		if cnt ~= 0 then
			sim:release(sim:object(id), true)
			local npc = db.actor
			local section = obj:section()
			local newsect = section.."_fake"
			spawn_ammo_in_inv(newsect, cnt, db.actor)
		end
	end	
	
	need_update = false
end

function on_inventory_open()
	--dbg.log("AMMO_CTRL: on_inventory_open")
	need_convert = false
	local t = {}
	db.actor:iterate_inventory(
		function(npc, item)
			if ammo_fake_types[item:section()] then
				--dbg.log("AMMO_CTRL: find item %s", item:name())
				table.insert(t, item:id())
			end
		end
		, db.actor)
		
	for i, id in ipairs(t) do
		local sim = alife()
		local obj = level.object_by_id(id)
		local cnt = get_ammo_size(obj)
		if cnt ~= 0 then
			sim:release(sim:object(id), true)
			local npc = db.actor
			local section = obj:section()
			local newsect = string.sub(section, 1, string.len(section) - 5)
			spawn_ammo_in_inv(newsect, cnt, db.actor)
		end
	end
end

function on_inventory_close()
	--dbg.log("AMMO_CTRL: on_inventory_close")
	need_convert = true
	need_update = true
end

function on_item_drop(item)
	--dbg.log("AMMO_CTRL: on_item_drop")
end

function on_item_take(item)
	if need_init then init() end

	--dbg.log("AMMO_CTRL: on_item_take")	
	local section = item:section()
	if ammo_types[section] and need_convert then	
		local cnt = get_ammo_size(item)
		if cnt == 0 then return end
		local sim = alife()
		sim:release(sim:object(item:id()), true)
		local newsect = section.."_fake"
		spawn_ammo_in_inv(newsect, cnt, db.actor)
	end
end

function init()
	--dbg.log("AMMO_CTRL: init")
	for k, v in pairs(ammo_types) do
		ammo_fake_types[k.."_fake"] = v
	end
	need_init = false
end

function register()
	--dbg.log("AMMO_CTRL: register")
	mod_man.register_actor_update(on_update)
	mod_man.register_actor_item_take(on_item_take)
	mod_man.register_actor_item_drop(on_item_drop)
	mod_man.register_actor_inventory(on_inventory_open)
	mod_man.register_actor_inventory_hide(on_inventory_close)
end

--------
-- возвращает колличество патрон в пачке
-- вроде подходящих функций нет, пришлось делать через net_packet
--------
function get_ammo_size(obj)
	local se_obj = alife():object(obj:id())
	if se_obj == nil then return 0 end	
	local packet = net_packet()
	cse_alife_item_ammo.STATE_Write(se_obj, packet)
	--packet:r_advance(18)
	--packet:r_stringZ()
	--packet:r_advance(8)
	--packet:r_stringZ()
	--packet:r_advance(5)
	packet:r_seek(packet:w_tell() - 2)
	return packet:r_u16()
end

function spawn_ammo_in_inv(spawn_item,number,npc)
	if npc==nil then 
		npc=db.actor 
	end
	if number > 0 then
		return se_respawn.create_ammo(spawn_item, 
			npc:position(),
			npc:level_vertex_id(),  
			npc:game_vertex_id(),
			npc:id(),
			number)
	end
end