----------------------------------------------------------------------------------------------------
-- Apply directional hit to the object
----------------------------------------------------------------------------------------------------
-- Исходный скрипт: Oleg Hryptul (Haron) haronk@ukr.net
----------------------------------------------------------------------------------------------------

class "action_hit"

----------------------------------------------------------------------------------------------------
-- Constructor
----------------------------------------------------------------------------------------------------
function action_hit:__init(obj, storage)
	self.object = obj
	self.st = storage
end

function action_hit:reset_scheme()
	printf("_hr: action_hit:reset_scheme: self.object:name()='%s'", self.object:name())
	
	local p1 = patrol(self.st.dir_path):point(0)
	local p2 = self.object:position()
	local h = hit()
	h.power     = self.st.power
	h.impulse   = self.st.impulse
	h:bone(self.st.bone)
	h.type      = hit.strike
	h.direction = utils.vector_copy_by_val(p1):sub(p2)
	h.draftsman = self.object
	self.object:hit(h)
	
	--printf("_hr: action_hit:reset_scheme: p1=%f,%f,%f", p1.x, p1.y, p1.z)
	--printf("_hr: action_hit:reset_scheme: p2=%f,%f,%f", p2.x, p2.y, p2.z)
	--printf("_hr: action_hit:reset_scheme: bone = %d", h.bone)
	printf("_hr: action_hit:reset_scheme: direction=%f,%f,%f", h.direction.x, h.direction.y, h.direction.z)
end

function action_hit:update(delta)
	--printf("_hr: action_hit:update()")

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

	local actor = level.actor()
	if not actor then
		return
	end

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

--[[
function action_hit:hit_callback(door, actor)
	if self.st.locked then
		if self.st.snd_open_start then
			self:door_play_snd_from_set(self.st.snd_open_start)
		end
		return
	end

	local angle = self.joint:get_axis_angle(90)

	if angle - self.low_limits > self.hi_limits - angle then
		self:open_door()
	else
		self:close_door(false)
	end
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_hit(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.power    = utils.cfg_get_number(ini, section, "power", npc, false, 0)
	st.impulse  = utils.cfg_get_number(ini, section, "impulse", npc, false, 1000)
	st.bone     = utils.cfg_get_string(ini, section, "bone", npc, true, "")
	st.dir_path = utils.cfg_get_string(ini, section, "dir_path", npc, true, "")
end
