----------------------------------------------------------------------------------------------------
-- Mob Home
----------------------------------------------------------------------------------------------------
-- Разработчик: Jim
----------------------------------------------------------------------------------------------------

local def_min_radius = 10
local def_max_radius = 15

class "mob_home"

----------------------------------------------------------------------------------------------------
-- CONSTRUCTION SCHEME
----------------------------------------------------------------------------------------------------
function mob_home:__init(obj, storage)
	self.object = obj
	self.st		= storage
end

----------------------------------------------------------------------------------------------------
-- RESET SCHEME
----------------------------------------------------------------------------------------------------
function mob_home:reset_scheme()
	--printf("DEBUG: reset_scheme: npc:name()='%s'", self.object:name())

--[[
Добавлено: Haron
Описание: Теперь можно задавать радиусы home_min_radius и home_max_radius во флагах
          пути path_home. Для этого введены флаги minr, maxr. Они должны быть
          прописаны в первой точке пути. У радиусов заданых в секции приоритет
          больший чем для тех что заданы во флагах, т.е. если задано и там и там, то
          берутся значения из секции (сделано для обратной совместимости).
--]]
	mob_state_mgr.set_state		(self.object, db.actor, self.st.state)
    local minr, maxr, midr = def_min_radius, def_max_radius, def_mid_radius
	local ptr = patrol(self.st.home)
	local path_info = utils.parse_waypoint_data(self.st.home, ptr:flags(0), ptr:name(0))				
    local r = 0

    if self.st.home_min_radius then
        minr = self.st.home_min_radius
    else
        r = path_info.minr
        if r then
            r = tonumber(r)
            if r then
                minr = r
            end
        end
    end
    
    if self.st.home_max_radius then
        maxr = self.st.home_max_radius
    else
        r = path_info.maxr
        if r then
            r = tonumber(r)
            if r then
                maxr = r
            end
        end
    end

	-- check min and max radius
	if minr > maxr then
		abort("Mob_Home : Home Min Radius MUST be < Max Radius. Got: min radius = %d, max radius = %d.", minr, maxr)
	end 

	--printf("DEBUG: reset_scheme: [%s] setting home path [%s]", self.object:name(), self.st.home)
    if self.st.home_mid_radius then
        midr = self.st.home_mid_radius
	if midr <= minr or midr >= maxr then
	    midr = minr + (maxr - minr)/2
	end
    else
	midr = minr + (maxr - minr)/2
    end

	self.object:set_home(self.st.home, minr, maxr, self.st.aggressive, midr)
	--self.object:set_home(self.st.home, minr, maxr, self.st.aggressive)
end

----------------------------------------------------------------------------------------------------
-- UPDATE
----------------------------------------------------------------------------------------------------

function mob_home:update(delta)
end

function mob_home:deactivate()
	self.object:remove_home()
end

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

	local new_action = mob_home(npc, storage)

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

----------------------------------------------------------------------------------------------------
-- SET_SCHEME
----------------------------------------------------------------------------------------------------
function set_scheme(npc, ini, scheme, section, gulag_name)
	--printf("DEBUG: set_scheme: npc:name()='%s', scheme='%s', section='%s', gulag_name='%s'", npc:name(), scheme, section, gulag_name)

	local storage				= xr_logic.assign_storage_and_bind(npc, ini, scheme, section)

	storage.logic				= xr_logic.cfg_get_switch_conditions(ini, section, npc)
	storage.state				= mob_state_mgr.get_state(ini, section, npc)
	storage.home				= utils.cfg_get_string(ini, section, "path_home",			npc, true, gulag_name)
	storage.home_min_radius		= utils.cfg_get_number(ini, section, "home_min_radius",		npc, false) --, 20)
	storage.home_mid_radius		= utils.cfg_get_number(ini, section, "home_mid_radius",		npc, false) --, 0)
	storage.home_max_radius		= utils.cfg_get_number(ini, section, "home_max_radius",		npc, false) --, 40)
	
	storage.aggressive			= utils.cfg_get_bool(ini,   section, "aggressive",    npc, false, false)
end

