---- Rulix aka Bak --- 2.7.2009

function printf(s, ...)
--	rx_ai.printf("reload:"..s,...)
--	get_console():execute("flush")
end

class "evaluator_reload" (property_evaluator)
function evaluator_reload:__init(npc,name,storage) super (nil,name)
	self.st = storage
end
function evaluator_reload:evaluate()
	local npc = self.object
	if npc:best_enemy() then
		self.st.do_id = nil
		self.st.do_cnt = 0
		self.enemy = true
		return false
	end
	if self.enemy then
		self.enemy = nil
		self.delay = time_global()+math.random(12000)
--		printf("eva[%s]:set delay",npc:character_name())
	end
	if self.delay and self.delay > time_global() then
		return false
	end
	if rx_wmgr and rx_wmgr.wm_modes.enabled == true then
		local wm = rx_wmgr.get_wm(npc)
		if wm.reloadc then
			return true
		end
	else
		local cwpn = npc:best_weapon()
		if cwpn then
			local inmag = cwpn:get_ammo_in_magazine()
			if rx_utils.item_is_fa(cwpn) and inmag < get_mag_size(cwpn:section()) and inmag < cwpn:get_ammo_total() then
				return true
			end
		end
	end
	self.st.do_id = nil
	return false
end

class "action_reload" (action_base)
function action_reload:__init (npc,action_name,storage) super (nil,action_name)
	self.st = storage
end
function action_reload:initialize()
	action_base.initialize(self)
	local npc = self.object
	if npc:body_state() ~= move.crouch then
		npc:clear_animations()
	end
	local state = state_mgr.get_state(npc)
	if state_lib.states[state].weapon ~= "unstrapped" then
		state_mgr.set_state(npc,"patrol")
	end
end
function action_reload:execute()
	action_base.execute(self)
	local npc = self.object
	local wpn = rx_utils.get_weapon(npc)
	if wpn and rx_utils.item_is_fa(wpn) then
		local id = wpn:id()
		local magsize = get_mag_size(wpn:section())
		local elapsed = wpn:get_ammo_in_magazine()
		if elapsed == magsize then
			printf("act[%s].elapsed == magsize",npc:character_name())
			return
		elseif elapsed > 0 and not self.st.do_id or self.st.do_id ~= id then
			printf("act[%s]unload",npc:character_name())
			wpn:unload_magazine()
		end
		if self.st.do_id and self.st.do_id == id then
			self.st.do_cnt = (self.st.do_cnt or 0)+1
			local ucnt = 44
			if self.shotgun then
				ucnt = 66
			end
			if self.cr then
				ucnt = ucnt*2
			end
			if self.st.do_cnt > ucnt then
				printf("act[%s]set on %s",npc:character_name(),self.st.do_cnt)
				wpn:set_ammo_elapsed(magsize)
				npc:set_item(object.idle,wpn)
				self.st.do_id = nil
			end
		else
			self.st.do_id = id
			self.st.do_cnt = 0
			if wpn:clsid() == clsid.wpn_shotgun_s then
				self.shotgun = true
				printf("act[%s]shotgun = true",npc:character_name())
			else
				self.shotgun = nil
			end
			if npc:body_state() == move.crouch then
				self.cr = true
			else
				self.cr = nil
			end
		end
		npc:set_item(object.aim1,wpn)
	else
		self.st.do_id = nil
	end
end

evid_reload = rx_ai.base_id+5
actid_reload = evid_reload

function add_to_binder(npc,ini,scheme,section,storage)
	local manager = npc:motivation_action_manager()
	if npc:character_community() == "zombied" then
		manager:add_evaluator(evid_reload,property_evaluator_const(false))
		return
	else
		manager:add_evaluator(evid_reload,evaluator_reload(npc,"eva_reload",storage))
	end
	local action = action_reload(npc,"act_reload",storage)
	action:add_precondition(world_property(stalker_ids.property_alive,true))
	action:add_precondition(world_property(stalker_ids.property_danger,false))
	action:add_precondition(world_property(stalker_ids.property_enemy,false))
	action:add_precondition(world_property(xr_evaluators_id.sidor_wounded_base,false))
--	action:add_precondition(world_property(xr_actions_id.stohe_meet_base+1,false))
--	action:add_precondition(world_property(xr_evaluators_id.stohe_kamp_base+2,false))
	if rx_bandage then
		action:add_precondition(world_property(rx_bandage.evid_bandage,false))
	end
	if rx_gl then
		action:add_precondition(world_property(rx_gl.evid_gl_reload,false))
	end
	if xrs_grenade and xrs_grenade.evid_aaa_grenade then
		action:add_precondition(world_property(xrs_grenade.evid_aaa_grenade,false))
	end
	if xrs_medic and xrs_medic.evid_self_medic then
		action:add_precondition(world_property(xrs_medic.evid_self_medic,false))
	end
	if watcher_act then
--		action:add_precondition(world_property(watcher_act.evid_see_stuff,false))
		action:add_precondition(world_property(watcher_act.evid_near_stuff,false))
	end
	if blowout_scheme and blowout_scheme.evid_outside then
		action:add_precondition(world_property(blowout_scheme.evid_outside,false))
	end
	action:add_precondition(world_property(evid_reload,true))
	action:add_effect(world_property(evid_reload,false))
	manager:add_action(actid_reload,action)
	for n,id in ipairs({xr_actions_id.alife,xr_actions_id.stohe_meet_base+1,xr_actions_id.stohe_kamp_base+3,xr_actions_id.stohe_kamp_base+5}) do
		action = manager:action(id)
		action:add_precondition(world_property(evid_reload,false))
	end
end

function set_scheme(npc,ini,scheme,section)
	local st = xr_logic.assign_storage_and_bind(npc,ini,scheme,section)
end

function disable_scheme(npc, scheme)
	local st = db.storage[npc:id()][scheme]
	if st then
		st.enabled = false
	end
end

function get_mag_size(section)
	return (rx_wmgr and rx_wmgr.read_wpn_params(section).mag) or rx_utils.read_from_ini(nil,section,"ammo_mag_size",1)
end
