----------------------------------------------------------------------------------------------------
-- Sleep zone
----------------------------------------------------------------------------------------------------
-- Author: Oleg Kreptul (Haron) haronk@ukr.net
----------------------------------------------------------------------------------------------------

local state_nowhere = 0 -- схема только что включилась
local state_inside = 1 -- актер внутри
local state_outside = 2 -- актер снаружи

class "action_sleep"

function action_sleep:__init(obj, storage)
	self.object = obj
	self.st = storage
end

function action_sleep:reset_scheme()
	--printf("_bp: sr_idle: action_no_weapon:reset_scheme: self.object:name()='%s'", self.object:name())
--[[
	self.state = state_nowhere -- it's undefined wheather actor in zone or not
	self.cond = self.st.cond
	
	if dream.dream_types[self.st.type] then
	    dream.type = self.st.type
	else
	    dream.type = "all"
	end

	dream.dream_prob = -1
	if type(self.st.dream_prob) == "number" then
	    local p = tonumber(self.st.dream_prob)
	    if p >= 0 and p <= 100 then
	        dream.dream_prob = p
	    end
	end
	
	self:switch_state(db.actor)
--]]
end

function action_sleep:update(delta)
	--printf("_br: sr_idle: action_no_weapon:update()")

	--if not xr_logic.is_active(self.object, self.st) then
	--	return
	--end

	local actor = db.actor

	if xr_logic.try_switch_to_another_section(self.object, self.st, actor) then
		return
	end	
	
	--self:switch_state(actor)
end

function action_sleep:switch_state(actor)
	local state = self.state
	
	if self.object:inside(actor:position()) then
	    if self.cond then
	        local cs = xr_logic.pick_section_from_condlist(db.actor, self.object, self.cond.condlist)
	        dream.can_sleep = (cs == "true" or cs == "")
	    else
	        dream.can_sleep = true
	    end
	else
	    dream.can_sleep = false
	end
end

function action_sleep:deactivate()
    --dream.can_sleep = false
end

---------------------------------------------------------------------------------------------------------------------
function add_to_binder(npc, ini, scheme, section, storage)
	printf("DEBUG: add_to_binder: scheme='%s', section='%s'", scheme, section)

	local new_action = action_sleep(npc, storage)

	-- Зарегистрировать все actions, в которых должен быть вызван метод reset_scheme при изменении настроек схемы:
	xr_logic.subscribe_action_for_events(npc, storage, new_action)
end

function set_scheme(npc, ini, scheme, section, gulag_name)
	local st = xr_logic.assign_storage_and_bind(npc, ini, scheme, section)
	st.logic = xr_logic.cfg_get_switch_conditions(ini, section, npc)
--[[
    st.type = utils.cfg_get_string(ini, section, "type", npc, false, "", "all")
    st.dream_prob = utils.cfg_get_number(ini, section, "dream_prob", npc, false, nil)
	st.cond = xr_logic.cfg_get_condlist(ini, section, "cond", npc)
--]]
end
