
--function printf()
--end

class "action_process_use"
function action_process_use:__init(obj, storage)
	self.object = obj
	self.st = storage
end
function action_process_use:use_callback(victim, who)
	--printf("[%s] used by [%s]", victim:name(), who:name())
	if db.storage[self.object:id()].active_scheme then
		if db.actor then
			if xr_logic.try_switch_to_another_section(victim, self.st, db.actor) then
				return
			end
		end
	end
end


function notify_on_use(victim, who)
	printf("NOTIFY [%s] used by [%s]", victim:name(), who:name())
--'	if true then	
--'		get_console():execute("dump_infos")
--'	end

	-- Если мы мертвы - на юзание не реагировать
	if not victim:alive() then
		return
	end
	
	-- Согласны ли мы говорить при этом юзании или нет.
	if victim:is_talk_enabled() == true then
--[[
		if victim:active_sound_count() ~= 0 then
			return
		end
		-- Мы согласны говорить
		if victim:best_enemy() ~= nil then
			-- Во время боя
			xr_sound.set_sound_play(victim, "use_yes_fight")
		elseif xr_wounded.is_wounded(victim) then
			-- Юзание раненого
			return
		elseif db.storage[victim:id()].state_mgr.animstate:state() == "sleep" then
			-- Юзание спящего
			xr_sound.set_sound_play(victim, "use_yes_sleep")
		elseif db.storage[victim:id()].active_scheme == "kamp" then
			-- Юзание чувака в лагере
			xr_sound.set_sound_play(victim, "use_yes_kamp")
		else
			-- Дефолт
			xr_sound.set_sound_play(victim, "use_yes_default")
		end
]]
	else
		if victim:active_sound_count() ~= 0 then
			return
		end
		if xr_abuse.is_abuse(victim) == false then
			return
		end
		
		-- Мы не согласны говорить
		if victim:best_enemy() ~= nil then
			if victim:relation(db.actor) ~= game_object.enemy then
				-- Во время боя
				xr_sound.set_sound_play(victim, "use_no_fight")
			end
		elseif xr_wounded.is_wounded(victim) then
			-- Юзание раненого
		elseif xr_sleeper.is_npc_asleep(victim) then
			-- Юзание спящего
			xr_sound.set_sound_play(victim, "use_no_sleep")
		elseif xr_meet.cannot_talk_by_weapon(victim) then
			-- Необходимо спрятать оружие
			xr_sound.set_sound_play(victim, "use_no_weapon")
			xr_abuse.add_abuse(victim, 1)
		elseif db.storage[victim:id()].active_scheme == "kamp" then
			-- Юзание чувака в лагере
		else
			-- Дефолт
			xr_sound.set_sound_play(victim, "use_no_default")
			xr_abuse.add_abuse(victim, 1)
		end
	end
------------------------------------------------------- ARENA_EXTENSION_MOD--------------------------------------------------------	
	if xr_conditions.aem_check_blow() then
		if string.find(victim:name(), "aem_a") then
			victim:disable_talk()
			if victim:is_talking() then
				victim:stop_talk()
			end
			if victim:active_sound_count() ~= 0 then
				return
			end
			xr_sound.set_sound_play(victim, "use_no_fight")
			xr_abuse.add_abuse(victim, 2)
		end
	end
------------------------------------------------------- ARENA_EXTENSION_MOD--------------------------------------------------------
end
----------------------------------------------------------------------------------------------------------------------
-- binder
----------------------------------------------------------------------------------------------------------------------
function add_to_binder(npc, ini, scheme, section, storage) 	
	local action = this.action_process_use(npc, storage)
	storage.action = action
end

function set_use_checker(npc, ini, scheme, section)
	local st = xr_logic.assign_storage_and_bind(npc, ini, scheme, section)
	st.logic = xr_logic.cfg_get_switch_conditions(ini, section, npc)
	xr_logic.subscribe_action_for_events(npc, st, st.action)
end

function disable_scheme(npc, scheme)
	local st = db.storage[npc:id()][scheme]
	if st then
		xr_logic.unsubscribe_action_from_events(npc, st, st.action)
	end
end