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

local STATE_START_LOOK		= 1
local STATE_WAIT_LOOK_END	= 2
local STATE_JUMP			= 3

class "mob_jump"

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

----------------------------------------------------------------------------------------------------
-- RESET SCHEME
----------------------------------------------------------------------------------------------------
function mob_jump:reset_scheme()
    -- printf("Jump: reset_scheme: %s", self.object:name())
	xr_logic.mob_capture		(self.object, true)

	-- reset signals
	self.st.signals				= {}
	
	-- initialize jump point
	self.jump_path				= nil
	if self.st.jump_path_name then
		self.jump_path = patrol(self.st.jump_path_name)
	else 
		self.st.jump_path_name = "[not defined]"
	end

	if not self.jump_path then
		abort("object '%s': unable to find jump_path '%s' on the map",
				self.object:name(), self.st.jump_path_name)
	end

	
	self.point = vector().add(self.jump_path:point(0), self.st.offset)

	self.state_current  = STATE_START_LOOK
	
end

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

function mob_jump:update(delta)
	
	if xr_logic.try_switch_to_another_section(self.object, self.st, db.actor) then
		return
	end

	if (self.state_current == STATE_START_LOOK) then
		if not self.object:action() then
			action(	self.object, 
					look(look.point, self.point), 
					cond(cond.look_end)
			)
			
			self.state_current = STATE_WAIT_LOOK_END
		end
	elseif (self.state_current == STATE_WAIT_LOOK_END) then
		if not self.object:action() then
			self.state_current = STATE_JUMP
		end
	end
	
	if self.state_current == STATE_JUMP then 
		self.object:jump(self.point, self.st.ph_jump_factor)
		self.st.signals["jumped"] = true
		xr_logic.mob_release(self.object)
	end
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_jump(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)
	local storage			= xr_logic.assign_storage_and_bind(npc, ini, scheme, section)

	storage.logic			= xr_logic.cfg_get_switch_conditions(ini, section, npc)
	
	storage.jump_path_name	= utils.cfg_get_string(ini, section, "path_jump",		npc, false, gulag_name)
	storage.ph_jump_factor	= utils.cfg_get_number(ini, section, "ph_jump_factor",	npc, false, 1.8)
	
	local offset_str		= utils.cfg_get_string(ini, section, "offset",			npc, true, "") 
	-- printf( "offset_str=%s", offset_str )

	local elems				= parse_names(offset_str)
	-- printf( "elems = %s | %s | %s", tostring(elems[1]), tostring(elems[2]), tostring(elems[3]) )
	
	storage.offset			= vector():set(tonumber(elems[1]), tonumber(elems[2]), tonumber(elems[3]))
	
	if	not ini:line_exist( section, "on_signal" ) then
		utils.abort("Bad jump scheme usage! `on_signal` line must be specified")
	end 
end

