function allow_break_dialog(first_speaker, second_speaker, id)
	if second_speaker:profile_name() == "osoznanie" then
		return false
	end

	return true
end


function break_dialog(first_speaker, second_speaker, id)
	first_speaker:stop_talk()
	second_speaker:stop_talk()
end

function disable_talk_self(first_speaker, second_speaker)
	first_speaker:disable_talk()
end
function disable_talk_victim(first_speaker, second_speaker)
	second_speaker:disable_talk()
end

function punch(first_speaker, second_speaker)
	--abort("KICK ASS !!!!")
	--xr_punch.punch[first_speaker:id()] = second_speaker
	db.storage[second_speaker:id()].punch.enabled = true
end

function get_money_then_leave(first_speaker, second_speaker)
	db.storage[first_speaker:id()].meet.enabled = false
	db.storage[first_speaker:id()].robber.enabled = true
end

function is_wounded(first_speaker, second_speaker)
    if db.storage[first_speaker:id()].wounded ~= nil and
       db.storage[first_speaker:id()].wounded.wound_manager.can_use_medkit == true
    then
        return false
    end

	return xr_wounded.is_wounded(first_speaker)
end
function is_opp_wounded(first_speaker, second_speaker, dialog_id)
    if db.storage[second_speaker:id()].wounded ~= nil and
       db.storage[second_speaker:id()].wounded.wound_manager.can_use_medkit == true
    then
        return false
    end
	return	xr_wounded.is_wounded(second_speaker)
end
function is_not_wounded(first_speaker, second_speaker)
	return not this.is_wounded(first_speaker, second_speaker)
end
function actor_have_medkit(first_speaker, second_speaker)
	return first_speaker:object("medkit") ~= nil or
		   first_speaker:object("medkit_army") ~= nil or
		   first_speaker:object("medkit_scientic") ~= nil
end
function actor_have_bandage(first_speaker, second_speaker)
	return first_speaker:object("bandage") ~= nil
end
function transfer_medkit(first_speaker, second_speaker)
	if first_speaker:object("medkit") ~= nil then
		dialogs.relocate_item_section(second_speaker, "medkit", "out")
	elseif first_speaker:object("medkit_army") ~= nil then
		dialogs.relocate_item_section(second_speaker, "medkit_army", "out")
	else
		dialogs.relocate_item_section(second_speaker, "medkit_scientic", "out")
	end

	--' Тут надо анлочить аптечку для использования.
	xr_wounded.unlock_medkit(second_speaker)
	amk.save_variable("pomog_gadu",1)
		
	if second_speaker:relation(first_speaker) ~= game_object.enemy then
		second_speaker:set_relation(game_object.friend, first_speaker)
	end
	first_speaker:change_character_reputation(10);
end
function transfer_bandage(first_speaker, second_speaker)
	dialogs.relocate_item_section(second_speaker, "bandage", "out")
	second_speaker:set_relation(game_object.friend, first_speaker)
end
function kill_yourself(npc, actor)
	npc:kill(actor)
end


--' Obsolete, do not use!!!
function relocate_item(victim, obj, type)
	if db.actor and victim then
		if type == "in" then
			victim:transfer_item(obj, db.actor)
		elseif type == "out" then
			db.actor:transfer_item(obj, victim)
		end
		--'news_manager.relocate_item(db.actor, type, obj)
	end
end

--' Use this function instead.
--/ Добавил количество предметов. Real Wolf
function relocate_item_section(victim, section, type, count)
	if count == nil then
		count = 1
	end
	if (db.actor and victim) and (db.actor:id() ~= victim:id()) then
		for i=1,count do
			if type == "in" then
				--' Трансферить нужно только квестовые предметы.
				if quest_section[section] == true and victim:object(section) ~= nil then
					victim:transfer_item(victim:object(section), db.actor)
				else
					alife():create(section,	
					    db.actor:position(),
						db.actor:level_vertex_id(),	
						db.actor:game_vertex_id(),
						db.actor:id())
				end
			elseif type == "out" then
					db.actor:transfer_item(db.actor:object(section), victim)
			end
		end
		news_manager.relocate_item(db.actor, type, section, count)
	end
end


function relocate_money(victim, num, type)
	if db.actor and victim then
		if type == "in" then
			db.actor:give_money(num)
			game_stats.money_quest_update (num) 
		elseif type == "out" then
			db.actor:transfer_money(num, victim)
			game_stats.money_quest_update (-num) 
		end
		news_manager.relocate_money(db.actor, type, num)
	end
end

--'---------------------------------------------------------------------------------
--' DIALOG ALLOWED
--'---------------------------------------------------------------------------------
function dialog_allowed(object, victim, id)
	if id ~= nil then
--		printf("*DIALOGS*: dialog_allowed: %s", id)
	else
--		printf("*DIALOGS*: dialog_allowed: nil")
	end
	if db.storage[victim:id()].actor_dialogs ~= nil then
		for k,v in pairs(db.storage[victim:id()].actor_dialogs) do
			if v == id then return true end
		end
	end
	return false
end
function dialog_not_disable(object, victim, id)
	if id ~= nil then
--		printf("*DIALOGS*: dialog_disable:%s", id)
	else
--		printf("*DIALOGS*: dialog_disable:nil")
	end
	if db.storage[victim:id()].actor_disable ~= nil then
		for k,v in pairs(db.storage[victim:id()].actor_disable) do
			if v == id then return false end
		end
	end
	return true
end

function allow_wounded_dialog(object, victim, id)
	if db.storage[victim:id()].wounded == nil then
		return false
	end	
	if db.storage[victim:id()].wounded.help_dialog == id then
		return true
	end
	return false	
end


-----------------------------------------------------------------------------------
-- LEVELS
-----------------------------------------------------------------------------------
function level_escape(first_speaker, second_speaker)
	return level.name() == "l01_escape"
end

function level_garbage(first_speaker, second_speaker)
	return level.name() == "l02_garbage"
end

function level_agroprom(first_speaker, second_speaker)
	return level.name() == "l03_agroprom_ai2" or level.name() == "l03_agroprom"
end

-----------------------------------------------------------------------------------
-- Relation functions
-----------------------------------------------------------------------------------
function is_friend(first_speaker, second_speaker)
	return first_speaker:relation(second_speaker) == game_object.friend
end

function is_not_friend(first_speaker, second_speaker)
	return not is_friend(first_speaker, second_speaker)
end

function become_friend(first_speaker, second_speaker)
	first_speaker:set_relation(game_object.friend, second_speaker)
end

-----------------------------------------------------------------------------------
-- Community
-----------------------------------------------------------------------------------
function actor_in_dolg(actor, npc)
	if db.actor then
		return db.actor:character_community() == "actor_dolg"
	end
	return false
end
function actor_not_in_dolg(actor, npc)
	return not actor_in_dolg(actor, npc)
end
function actor_set_dolg(actor, npc)
	printf("ACTOR NOW IN [DOLG] COMMUNITY")
	if db.actor then
		db.actor:set_character_community("actor_dolg", 0, 0)
	end
	return true
end
function actor_clear_community(actor, npc)
	printf("ACTOR NOW IN [NEUTRAL] COMMUNITY")
	if db.actor then
		db.actor:set_character_community("actor", 0, 0)
	end
	return true
end

-----------------------------------------------------------------------------------
-- Money functions
-----------------------------------------------------------------------------------
function has_2000_money(first_speaker, second_speaker)
	return first_speaker:money() >= 2000
end

-----------------------------------------------------------------------------------
-- TRADE
-----------------------------------------------------------------------------------
--' Инициализация торговли
function trade_init(seller, buyer)
	db.storage[seller:id()].meet.begin_wait_to_see.begin = time_global()/1000
	xr_position.setPosition(db.storage[seller:id()].meet.Seller, 
							db.storage[seller:id()].meet.Seller:level_vertex_id())
	db.storage[seller:id()].meet.Buyer = buyer
end

function want_trade(seller, buyer)
	if seller:relation(buyer) == game_object.friend or
		seller:relation(buyer) == game_object.neutral
	then
		return true
	else
		return false
	end
end

function dont_want_trade(seller, buyer)	
	return not want_trade(seller,buyer)
end

------------------------------------------------------------------------------------
-- Companion
------------------------------------------------------------------------------------
local not_follow_type = 0

function check_following(npc, actor)
--------------------------
-- Проверка условий отказа
--------------------------
-- Условия отказа:
-- 1) Это квестовый непись
-- 2) У ГГ хреновая репутация в коммунити
-- 3) Это Толик или кто-то из пленников
-- 4) ГГ ниже опытом чем непись
-- 5) ГГ значительно опытнее непися
-- 6) У ГГ уже есть напарник
-- 7) ГГ из другой группировки
-- 8) с ГГ погибло слишком много народу
------------------------------------------
	not_follow_type = 0
	
	local npc_rank_level = 0
	local actor_rank_level = 0

	local st = db.storage[npc:id()]
	local actor_comm = db.actor:character_community()
	actor_comm = xr_companion.community_by_actor(actor_comm)
	
	local npc_comm = npc:character_community()
	local npc_rank = ranks.get_obj_rank_name(npc)
	local actor_rank = db.actor:character_rank()
	local name = npc:name()
	local profile = npc:profile_name()
	local dead_friends = load_variable("dead_fr", 0)
	
	if npc_rank == "novice" then npc_rank_level = 1
	elseif npc_rank == "experienced" then npc_rank_level = 2
	elseif npc_rank == "veteran" then npc_rank_level = 3
	elseif npc_rank == "master" then npc_rank_level = 4 end

	if actor_rank < 300 then actor_rank_level = 1
	elseif actor_rank >= 300 and actor_rank < 600 then actor_rank_level = 2
	elseif actor_rank >= 600 and actor_rank < 900 then actor_rank_level = 3
	elseif actor_rank >= 900 then actor_rank_level = 4 end	
	
	if not st 
	or (is_quest_npc(npc) and not (string.find(npc:name(), "esc_wolf") and level.name() == "l07_military"))
	or string.find(profile, "sniper")
	or string.find(profile, "leader")
	or string.find(profile, "commander")
	or string.find(name, "val_rob")
	then
		-- 1) Это квестовый непись
		not_follow_type = 1
		return true
	else
		if relation_registry.community_goodwill(npc_comm, db.actor:id()) < -30
		then
			-- 2) У ГГ хреновая репутация в коммунити или он бандит
			not_follow_type = 2
			return true
		else
			if name and (string.find(name, "wounded") or string.find(name, "prisoner"))
			then
				-- 3) Это Толик или кто-то из пленников
				not_follow_type = 3
				return true			
			else
				if npc_rank_level > actor_rank_level then
					-- 4) ГГ ниже опытом чем непись
					not_follow_type = 4
					return true					
				else
					local diff = actor_rank_level - npc_rank_level
					if diff > 1 then
						-- 5) ГГ значительно опытнее непися
						not_follow_type = 5
						return true						
					else
						if xr_companion.number_of_companions > 1 then
						-- 6) У ГГ уже есть напарник
							not_follow_type = 6
							return true	
						else
							if actor_comm ~= npc_comm then
							-- 7) ГГ из другой группировки
								not_follow_type = 7
								return true	
							else
								if dead_friends >= 4 then
								-- 8) с ГГ погибло слишком много народу
									not_follow_type = 8
									return true	
								else
									not_follow_type = 0
									return false
								end
							end
						end
					end
				end
			end
		end			
	end	
	return false
end

function companion_will_follow(npc, actor)
------------------------------------------
-- Проверка на то, согласится ли непись
-- идти вместе с ГГ. Если согласится, то
-- ему присвоится коммунити актора.
-- В итоге схема будет автоматически
-- включаться для неписей с коммунити
-- актора.
------------------------------------------
	if not check_following(npc, actor) then
		return true
	else	
		return false
	end
end

function companion_not_follow(npc, actor)
	if not_follow_type == 1 then
		return true
	else	
		return false
	end
end

function companion_not_follow_bad(npc, actor)
	if not_follow_type == 2 then
		return true
	else	
		return false
	end
end

function companion_not_follow_tired(npc, actor)
	if not_follow_type == 3 then
		return true
	else	
		return false
	end	
end

function companion_not_follow_lowrank(npc, actor)
	if not_follow_type == 4 then
		return true
	else	
		return false
	end	
end

function companion_not_follow_highrank(npc, actor)
	if not_follow_type == 5 then
		return true
	else	
		return false
	end	
end

function companion_not_follow_toomuch(npc, actor)
	if not_follow_type == 6 then
		return true
	else	
		return false
	end	
end

function companion_not_follow_other_community(npc, actor)
	if not_follow_type == 7 then
		return true
	else	
		return false
	end	
end

function companion_not_follow_deadly(npc, actor)
	if not_follow_type == 8 then
		return true
	else	
		return false
	end	
end

function companion_start_following(npc, actor)
	local st = db.storage[npc:id()]
	local st_c = db.storage[npc:id()].companion
	local actor_comm = db.actor:character_community()
	local npc_com = xr_companion.community_by_actor(actor_comm)
	
	if st and st_c then
		xr_gulag.resetJob(npc)
		xr_companion.number_of_companions = xr_companion.number_of_companions + 1
		xr_companion.companions[npc:id()] = "companion"
		
		xr_companion.save_var_companion(npc:id(), "is_companion", 1)
		xr_companion.save_var_companion(npc:id(), "orig_community", npc:character_community())
		xr_companion.save_var_companion(npc:id(), "friendship_status", npc:relation(db.actor))
		xr_companion.save_var_companion(npc:id(), "companion_behavior", xr_companion.beh_walk_simple)
		
		npc:set_character_community(npc_com, 0, 0)
		npc:set_relation(game_object.friend, db.actor) 
		
		trade_manager.trade_init(npc, "misc\\trade_obmen.ltx")
		level.map_add_object_spot_ser(npc:id(), "alife_presentation_companion", "Ваш компаньон")	
		
		st_c.enabled = true
		st_c.behavior = xr_companion.beh_walk_simple
	end	
end

function companion_stop_following(npc, actor)
---------------------------------------------
-- При выключении схемы вернём компаньону
-- его изначальное коммунити. Дернем его из
-- визуала непися.
---------------------------------------------
	db.storage[npc:id()].companion.enabled = false
	xr_companion.number_of_companions = xr_companion.number_of_companions - 1
	
	xr_companion.save_var_companion(npc:id(), "is_companion", 0)
	
	local comm = xr_companion.load_var_companion(npc:id(), "orig_community", nil)
	local relation = xr_companion.load_var_companion(npc:id(), "friendship_status", nil)
	
	npc:set_character_community(comm, 0, 0)
	npc:set_relation(tonumber(relation), db.actor)
	
	trade_manager.trade_init(npc, "misc\\trade_generic.ltx")	
	level.map_remove_object_spot(npc:id(), "alife_presentation_companion")
	xr_companion.companions[npc:id()] = nil
	xr_gulag.resetJob(npc)
end

function is_companion(actor, npc)
	local st = db.storage[npc:id()]
	if not st then
		return false
	end
	if not st.companion then
		return false
	end
	if not st.companion.enabled then
		return false
	end
	return true
end

function not_is_companion(actor, npc)
	if is_quest_npc(npc) and not (string.find(npc:name(), "esc_wolf") and level.name() == "l07_military") then
		return false
	end
	-- if not has_alife_info("esc_kill_bandits_quest_done") then
		-- return false
	-- end
	if xr_wounded.is_wounded(npc) then
		return false
	end
	if npc:relation(actor) == game_object.enemy then
		return false
	end
	if is_companion(actor, npc) then
		return false
	end
	return true
end

---///--------------- прекондишены

function is_stalker_companion(actor, npc)
	local npc_comm = npc:character_community()
	if npc_comm == "actor" then return true else return false end
end


function is_stalker_companion(actor, npc)
	local npc_comm = npc:character_community()
	if npc_comm == "ecolog" then return true else return false end
end


function is_stalker_companion(actor, npc)
	local npc_comm = npc:character_community()
	if npc_comm == "stalker" then return true else return false end
end

function is_dolg_companion(actor, npc)
	local npc_comm = npc:character_community()
	if npc_comm == "dolg" then return true else return false end


end

function is_freedom_companion(actor, npc)
	local npc_comm = npc:character_community()
	if npc_comm == "freedom" then return true else return false end


end

function is_bandit_companion(actor, npc)
	local npc_comm = npc:character_community()
	if npc_comm == "bandit" then return true else return false end


end

function is_zombie_companion(actor, npc)
	local npc_comm = npc:character_community()
	if npc_comm == "zombied" then return true else return false end


end

--///---------------- прекондишены

function companion_beh_walk_simple(actor, npc)
	local st = db.storage[npc:id()]
	if st and st.companion then
		st.companion.behavior = xr_companion.beh_walk_simple
		xr_companion.save_var_companion(npc:id(), "companion_behavior", st.companion.behavior)
		xr_companion.save_var_companion(npc:id(),"w_lvl",nil)
	end
end

function companion_beh_walk_low_aggr(actor, npc)
	local st = db.storage[npc:id()]
	if st and st.companion then
		st.companion.behavior = xr_companion.beh_walk_low_aggr
		xr_companion.save_var_companion(npc:id(), "companion_behavior", st.companion.behavior)
		xr_companion.save_var_companion(npc:id(),"w_lvl",nil)
	end
end

function companion_beh_walk_ignore(actor, npc)
	local st = db.storage[npc:id()]
	if st and st.companion then
		st.companion.behavior = xr_companion.beh_walk_ignore
		xr_companion.save_var_companion(npc:id(), "companion_behavior", st.companion.behavior)
		xr_companion.save_var_companion(npc:id(),"w_lvl",nil)
	end
end

function companion_beh_wait_simple(actor, npc)
	local st = db.storage[npc:id()]
	if st and st.companion then
		st.companion.behavior = xr_companion.beh_wait_simple
		xr_companion.save_var_companion(npc:id(), "companion_behavior", st.companion.behavior)
		xr_companion.save_var_companion(npc:id(),"w_lvl",level.name())
	end
end

function companion_beh_wait_low_aggr(actor, npc)
	local st = db.storage[npc:id()]
	if st and st.companion then
		st.companion.behavior = xr_companion.beh_wait_low_aggr
		xr_companion.save_var_companion(npc:id(), "companion_behavior", st.companion.behavior)
		xr_companion.save_var_companion(npc:id(),"w_lvl",level.name())
	end
end

function companion_beh_wait_ignore(actor, npc)
	local st = db.storage[npc:id()]
	if st and st.companion then
		st.companion.behavior = xr_companion.beh_wait_ignore
		xr_companion.save_var_companion(npc:id(), "companion_behavior", st.companion.behavior)
		xr_companion.save_var_companion(npc:id(),"w_lvl",level.name())
	end
end

function companion_beh_crouch_simple(actor, npc)
	local st = db.storage[npc:id()]
	if st and st.companion then
		st.companion.behavior = xr_companion.beh_crouch_simple
		xr_companion.save_var_companion(npc:id(), "companion_behavior", st.companion.behavior)
		xr_companion.save_var_companion(npc:id(),"w_lvl",nil)
	end
end

function companion_beh_crouch_low_aggr(actor, npc)
	local st = db.storage[npc:id()]
	if st and st.companion then
		st.companion.behavior = xr_companion.beh_crouch_low_aggr
		xr_companion.save_var_companion(npc:id(), "companion_behavior", st.companion.behavior)
		xr_companion.save_var_companion(npc:id(),"w_lvl",nil)
	end
end

function companion_beh_crouch_ignore(actor, npc)
	local st = db.storage[npc:id()]
	if st and st.companion then
		st.companion.behavior = xr_companion.beh_crouch_ignore
		xr_companion.save_var_companion(npc:id(), "companion_behavior", st.companion.behavior)
		xr_companion.save_var_companion(npc:id(),"w_lvl",nil)
	end
end

function companion_beh_hide_simple(actor, npc)
	local st = db.storage[npc:id()]
	if st and st.companion then
		st.companion.behavior = xr_companion.beh_hide_simple
		xr_companion.save_var_companion(npc:id(), "companion_behavior", st.companion.behavior)
		xr_companion.save_var_companion(npc:id(),"w_lvl",level.name())
	end
end

function companion_beh_hide_low_aggr(actor, npc)
	local st = db.storage[npc:id()]
	if st and st.companion then
		st.companion.behavior = xr_companion.beh_hide_low_aggr
		xr_companion.save_var_companion(npc:id(), "companion_behavior", st.companion.behavior)
		xr_companion.save_var_companion(npc:id(),"w_lvl",level.name())
	end
end

function companion_beh_hide_ignore(actor, npc)
	local st = db.storage[npc:id()]
	if st and st.companion then
		st.companion.behavior = xr_companion.beh_hide_ignore
		xr_companion.save_var_companion(npc:id(), "companion_behavior", st.companion.behavior)
		xr_companion.save_var_companion(npc:id(),"w_lvl",level.name())
	end
end

function companion_suicide(actor, npc)

	local h = hit()
	h.power = 1000
	--h.direction = vector_rotate_y(npc:direction(), 90)
	h.direction = npc:direction()
	h.bone = "bip01_head"
	h.draftsman = npc 
	h.impulse = 10
	h.type = hit.wound
	npc:hit(h)

end
