----------------------------------------------------------------------------------------------------------------------
--	Схема реакции сталкеров на надоедания игрока
--	автор: Диденко Руслан (Stohe)
--	TODO:
----------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------
--Evaluators
----------------------------------------------------------------------------------------------------------------------
--' Нужно ли пнуть кого то в лицо
class "evaluator_abuse" (property_evaluator)
function evaluator_abuse:__init(name, storage) super (nil, name)
	self.a = storage
end
function evaluator_abuse:evaluate()
	return self.a.abuse_manager:update()
end

----------------------------------------------------------------------------------------------------------------------
--Actions
----------------------------------------------------------------------------------------------------------------------
--' Удар в лицо
class "action_abuse_hit" (action_base)
function action_abuse_hit:__init (npc_name,action_name, storage, char_ini) super (nil, action_name)
	self.a = storage
end
function action_abuse_hit:initialize()
	action_base.initialize(self)
--	self.object:set_node_evaluator()
--	self.object:set_path_evaluator()
	self.object:set_desired_position()
	self.object:set_desired_direction()

	state_mgr.set_state(self.object, "punch", nil, nil, {look_object = db.actor}, {animation = true})
--	xr_sound.set_sound_play(self.object:id(), "use_abuse")
	self.hit_done = true			--' xStream 02.2008
end
function action_abuse_hit:execute ()
	action_base.execute(self)
end
function action_abuse_hit:finalize ()
	action_base.finalize(self)
end

class "CAbuseManager"
function CAbuseManager:__init(npc, storage)
	self.npc = npc
	self.a = storage
	self.enable = true
	self.abuse_rate = 2
	self.abuse_value = 0
	self.abuse_threshold = 5
	self.last_update = nil
	self.hit_done = false				--' xStream 02.2008
end
function CAbuseManager:SetAbuseRate(rate)
	self.abuse_rate = rate
end
function CAbuseManager:abused()
	return self.abuse_value >= self.abuse_threshold
end
function CAbuseManager:update()
	if self.last_update == nil then
		self.last_update = time_global()
	end
	if self.abuse_value > 0 then
		self.abuse_value = self.abuse_value - (time_global() - self.last_update)*0.00005
	else
		self.abuse_value = 0
	end
	--xStream 02.2008--
	if self.abuse_value > self.abuse_threshold*1.1 then
		self.abuse_value = self.abuse_threshold*1.1
	end
	
	if self.hit_done and self.abuse_value < self.abuse_threshold*2/3 then
		self.hit_done = false
	end
	--end--
	self.last_update = time_global()

	--printf("*ABUSE* %s [%s]", self.npc:name(), self.abuse_value)
	if self:abused() then
		return true
	end
	
	return false
end
function CAbuseManager:addAbuse(value)
	if self.enable == true then
		self.abuse_value = self.abuse_value + value*self.abuse_rate
	end
end
function CAbuseManager:clearAbuse()
	self.abuse_value = 0
end
function CAbuseManager:disableAbuse()
	self.enable = false
end
function CAbuseManager:enableAbuse()
	self.enable = true
end

----------------------------------------------------------------------------------------------------------------------
-- binder
----------------------------------------------------------------------------------------------------------------------
function add_to_binder(object, char_ini, scheme, section, st)
	local operators		= {}
	local properties	= {}

	local manager = object:motivation_action_manager()

	properties["abuse"]		= xr_evaluators_id.abuse_base
	properties["wounded"]	= xr_evaluators_id.sidor_wounded_base

	operators["abuse"]		= xr_actions_id.abuse_base

	-- Evaluators
	manager:add_evaluator (properties["abuse"], 		evaluator_abuse("evaluator_abuse", st))
	
	-- Actions
	local action = action_abuse_hit(object:name(),"action_abuse_hit", st, char_ini)
	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(properties["wounded"], 		false))
	action:add_precondition		(world_property(properties["abuse"],		true))
	action:add_effect (world_property(properties["abuse"], false))
	manager:add_action (operators["abuse"], action)

	action = manager:action (xr_actions_id.alife)	
	action:add_precondition		(world_property(properties["abuse"],		false))
	
	st.abuse_manager = CAbuseManager(object, st)
end



------------
-- Вызывается только в начале на чтении логики, создает экшены, эвалуаторы и производит
-- первичную настройку.
function set_abuse(npc, ini, scheme, section)
	local st = xr_logic.assign_storage_and_bind(npc, ini, scheme, section)
end
-- Вызывается при переключении на новую секцию. Производит вычитывание настроек из текущей секции.
function reset_abuse(npc, scheme, st, section)
end


function add_abuse(npc, value)
	local t = db.storage[npc:id()].abuse
	if t then
		t.abuse_manager:addAbuse(value)
	end
end
function clear_abuse(npc)
	local t = db.storage[npc:id()].abuse
	if t then
		t.abuse_manager:clearAbuse()
	end
end
function disable_abuse(npc)
	local t = db.storage[npc:id()].abuse
	if t then
		t.abuse_manager:disableAbuse()
	end
end
function enable_abuse(npc)
	local t = db.storage[npc:id()].abuse
	if t then
		t.abuse_manager:enableAbuse()
	end
end
function is_abuse(npc)
	local t = db.storage[npc:id()].abuse
	if t == nil then
		return false
	end
	return t.abuse_manager.enable
end