----------------------------------------------------------------------------------------------------
-- Do nothing
----------------------------------------------------------------------------------------------------

local i,k,v = 0,0,0

local teleport_idle = 0
local teleport_activated = 1

class "action_teleport"
function action_teleport:__init(obj, storage)
	self.object = obj
	self.st = storage
	
	self.state = teleport_idle
	self.timer = nil
end
function action_teleport:update(delta)
	if not db.actor then
		return
	end

	if self.state == teleport_idle then	
		if self.object:inside(db.actor:position()) then
			-- Обработка телепорта
			self.state = teleport_activated
			self.timer = time_global()	
			level.add_pp_effector ("teleport.ppe", 2006, false)
			--set_postprocess("scripts\\teleport.ltx")
		end
	end
	if self.state == teleport_activated then	
		if time_global() - self.timer >= self.st.timeout then
			-- Производим телепортацию
			local temp = {}
			local max_rnd = 0
			for k,v in pairs(self.st.points) do		
				-- Определяются допустимые состояния для лагеря.
				temp[k] = v
				max_rnd = max_rnd + v.prob
			end
			-- Осуществляется рандомный взвешенный переход.
			local p = math.random(0,max_rnd)
			for k,v in pairs(temp) do
				p = p - v.prob
				if p <= 0 then
					-- телепорт в выбранную точку
					printf("teleporting to [%s] look [%s]", v.point, v.look)
					db.actor:set_actor_position(patrol(v.point):point(0))
					local dir = patrol(v.look):point(0):sub(patrol(v.point):point(0))
					db.actor:set_actor_direction(-dir:getH())
					local snd_obj = sound_object("affects\\tinnitus3a")
					snd_obj:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)					
					break
				end
			end
			self.state = teleport_idle
		else
			return
		end
	end
	if xr_logic.try_switch_to_another_section(self.object, self.st, db.actor) then
		return
	end	
		
end

---------------------------------------------------------------------------------------------------------------------
function add_to_binder(npc, ini, scheme, section, storage)
	local action = action_teleport(npc, storage)

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

function set_scheme(obj, ini, scheme, section, gulag_name)
	local st = xr_logic.assign_storage_and_bind(obj, ini, scheme, section)
	st.logic = xr_logic.cfg_get_switch_conditions(ini, section, obj)
	
	st.timeout = utils.cfg_get_number(ini, section, "timeout", obj, false, 900)
	
	st.points = {}
	for i=1,10 do
		local t = {
			point = utils.cfg_get_string(ini, section, "point"..tostring(i), obj, false, "", "none"),
			look = utils.cfg_get_string(ini, section, "look"..tostring(i), obj, false, "", "none"),
			prob = utils.cfg_get_number(ini, section, "prob"..tostring(i), obj, false, 100)
		}
		if t.point == "none" or t.look == "none" then
			break
		end
		table.insert(st.points, t)
	end
	if #st.points == 0 then
		abort("Wrong point nums in sr_teleport [%s]", tostring(section))
	end
end

