----------------------------------------------------------------------------------------------------
-- Mob Home
----------------------------------------------------------------------------------------------------
-- Ðàçðàáîò÷èê: Jim
----------------------------------------------------------------------------------------------------

-- xcvb mutant tables
local humanoid_t = {
	zombie = true,
	fracture = true,
	snork = true,
	SM_POLTER_G = true,
	SM_PYRO_G = true,
	SM_PSEUDO_G = true,
	SM_KARLIK = true,
	burer = true,
	bloodsucker = true,
	SM_PSYSUCKER = true,
	controller = true,
}

local animal_t = {
	tushkano = true,
	flesh = true,
	dog = true,
	boar = true,
	cat = true,
	pseudodog = true,
	SM_LURKER = true,
	chimera = true,
	giant = true,
}

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()
	mob_state_mgr.set_state(self.object, db.actor, self.st.state)
	
	local minr = self.st.home_min_radius or 10
	local midr = self.st.home_mid_radius or 25
	local maxr = self.st.home_max_radius or 70
	
	if (midr <= minr) then 
		midr = minr+5 
	end 
	if (maxr <= midr) then 
		maxr = midr+5 
	end 

	if self.st.gulag_point then
		local se_obj = alife_object(self.object:id())
		if (se_obj) then
			local smart = se_obj.m_smart_terrain_id ~= 65535 and alife_object(se_obj.m_smart_terrain_id)
			if (smart) then
				local str = ini_sys:r_string_ex(se_obj:section_name(), "kind") or ini_sys:r_string_ex(se_obj:section_name(), "species") or nil
				local hum_wp = humanoid_t[str] and level.patrol_path_exists(smart:name() .. "_hum") and patrol(smart:name() .. "_hum")
				local anim_wp = animal_t[str] and level.patrol_path_exists(smart:name() .. "_anim") and patrol(smart:name() .. "_anim")
				-- humanoid
				if hum_wp then
					self.object:set_home(hum_wp:level_vertex_id(0), 10, 30, self.st.aggressive, 20)
					return
				-- animal
				elseif anim_wp then
					self.object:set_home(anim_wp:level_vertex_id(0), minr, maxr, self.st.aggressive, midr)
					return
				-- default
				else
					self.object:set_home(smart.m_level_vertex_id, minr, maxr, self.st.aggressive, midr)
					return
				end
			end
		end
	end 
	
	if (self.st.home and patrol(self.st.home)) then
		self.object:set_home(self.st.home, minr, maxr, self.st.aggressive, midr)
	end
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_data.prefix_r_string(ini,section,"path_home",gulag_name)
	storage.gulag_point			= ini:r_bool_ex(section,"gulag_point",false)
	storage.home_min_radius		= ini:r_float_ex(section,"home_min_radius") or 10
	storage.home_mid_radius		= ini:r_float_ex(section,"home_mid_radius") or 25
	storage.home_max_radius		= ini:r_float_ex(section,"home_max_radius") or 70
	storage.aggressive			= ini:r_bool_ex(section,"aggressive",false)
end