--------------------------------------------------------------------------------
-- Dialog manager --------------------------------------------------------------
-- Made by Peacemaker ----------------------------------------------------------
-- 11.01.08 --------------------------------------------------------------------
--------------------------------------------------------------------------------
local disabled_phrases = {} -- temporary table of phrases which have been disabled during a conversation
local id_counter = 1 -- start from 1 because of adding root phrases
phrase_table = {
                hello = {},
                information = {},
                occupation = {},
                place = {},
               }
priority_table = {
	                hello = {},
	                information = {},
	                occupation = {},
	                place = {},
				 }
-- Generate id for phrase
function get_id()
    id_counter = id_counter + 1
    return id_counter
end
-- Parse ini file and store all phrases and their parameters into phrase table
function fill_phrase_table()
	printf("fill_phrase_table")
-- local ltx file
	local dm_ini_file = ini_file("misc\\dialog_manager.ltx")
    local id = ""
    local value = ""
    local category = ""
    for i=0,dm_ini_file:line_count("list")-1 do
        temp1, id, temp2 = dm_ini_file:r_line("list", i, "", "")
        if(dm_ini_file:line_exist(id, "category")) then
            category = dm_ini_file:r_string(id, "category")
			if(category=="intro") then
				category = "hello"
			elseif(category=="cool") then
				category = "information"
			elseif(category=="action") then
				category = "occupation"
			elseif(category=="about") then
				category = "place"
			else
				category = "default"
			end
        else
-- if there is no set category we don't know where we must store the phrase
            abort("Dialog manager error: not categoried section [%s].", id)
        end
        if(category~="default") then
	        local temp_table = {}
	        temp_table.name = id
-- npc community: all or {dolg,freedom,bandit,military,zombied,ecolog,killer,monolith,csky,arena_enemy,...}
	        if(dm_ini_file:line_exist(id, "npc_community")) then
	            local temp = dm_ini_file:r_string(id, "npc_community")
	            temp_table.npc_community = parse_names(temp)
	        else
	            temp_table.npc_community = "not_set"
	        end
-- level: all or level name
	        if dm_ini_file:line_exist(id, "level") then
	            local temp = dm_ini_file:r_string(id, "level")
	            temp_table.level = parse_names(temp)
	        else
	            temp_table.level = "not_set"
	        end
-- actor community: all or {actor_dolg, actor, ...}
	        if(dm_ini_file:line_exist(id, "actor_community")) then
	            local temp = dm_ini_file:r_string(id, "actor_community")
	            temp_table.actor_community = parse_names(temp)
	        else
	            temp_table.actor_community = "not_set"
	        end
-- is npc wounded? true, false
	        if dm_ini_file:line_exist(id, "wounded") then
	            temp_table.wounded = dm_ini_file:r_string(id, "wounded")
	        else
	            temp_table.wounded = "false"
	        end
-- phrase is said once: true, always, false(!!!don't set or will no say this phrase)
	        if dm_ini_file:line_exist(id, "once") then
	            temp_table.once = dm_ini_file:r_string(id, "once")
	        else
	            temp_table.once = "always"
	        end
	        temp_table.id = tostring(get_id())
	        phrase_table[category][temp_table.id] = temp_table
		end
    end
end
-- Save
function save_npc(packet, npc_id)
	set_save_marker(packet, "save", false, "dialog_manager")
	if(priority_table.hello[npc_id]~=nil) then
        packet:w_bool(true)
--[[
		local counter = 0
		for phrase_id,priority in pairs(priority_table.hello[npc_id]) do
			counter = counter + 1
		end
        packet:w_u16(counter)
		for phrase_id,priority in pairs(priority_table.hello[npc_id]) do
            if(phrase_id~="ignore_once") then
                packet:w_stringZ(tostring(phrase_id))
                packet:w_u8(priority)
            else
                packet:w_stringZ(phrase_id)
                packet:w_bool(priority)
            end
		end
]]
	else
        packet:w_bool(false)
	end

	if(priority_table.information[npc_id]~=nil) then
        packet:w_bool(true)
--[[
		local counter = 0
		for phrase_id,priority in pairs(priority_table.information[npc_id]) do
			counter = counter + 1
		end
        packet:w_u16(counter)
		for phrase_id,priority in pairs(priority_table.information[npc_id]) do
            if(phrase_id~="ignore_once") then
                packet:w_stringZ(tostring(phrase_id))
                packet:w_u8(priority)
            else
                packet:w_stringZ(phrase_id)
                packet:w_bool(priority)
            end
		end
]]
	else
        packet:w_bool(false)
	end

	if(priority_table.occupation[npc_id]~=nil) then
        packet:w_bool(true)
--[[
		local counter = 0
		for phrase_id,priority in pairs(priority_table.occupation[npc_id]) do
			counter = counter + 1
		end
        packet:w_u16(counter)
		for phrase_id,priority in pairs(priority_table.occupation[npc_id]) do
            if(phrase_id~="ignore_once") then
                packet:w_stringZ(tostring(phrase_id))
                packet:w_u8(priority)
            else
                packet:w_stringZ(phrase_id)
                packet:w_bool(priority)
            end
		end
]]
	else
        packet:w_bool(false)
	end

	if(priority_table.place[npc_id]~=nil) then
        packet:w_bool(true)
--[[
		local counter = 0
		for phrase_id,priority in pairs(priority_table.place[npc_id]) do
			counter = counter + 1
		end
        packet:w_u16(counter)
		for phrase_id,priority in pairs(priority_table.place[npc_id]) do
            if(phrase_id~="ignore_once") then
                packet:w_stringZ(tostring(phrase_id))
                packet:w_u8(priority)
            else
                packet:w_stringZ(phrase_id)
                packet:w_bool(priority)
            end
		end
]]
	else
        packet:w_bool(false)
	end
	set_save_marker(packet, "save", true, "dialog_manager")
end
-- Load
function load_npc(reader, npc_id)
	set_save_marker(reader, "load", false, "dialog_manager")
    local is_saved = reader:r_bool()
--[[
	if(is_saved) then
        local counter = reader:r_u16()
		priority_table.hello[npc_id] = {}
		for i=1,counter do
			local phrase_id = reader:r_stringZ()
			if(phrase_id~="ignore_once") then
				priority_table.hello[npc_id][phrase_id] = reader:r_u8()
			else
				priority_table.hello[npc_id][phrase_id] = reader:r_bool()
			end
		end
	end
]]
    is_saved = reader:r_bool()
--[[
	if(is_saved) then
        local counter = reader:r_u16()
		priority_table.information[npc_id] = {}
		for i=1,counter do
			local phrase_id = reader:r_stringZ()
			if(phrase_id~="ignore_once") then
				priority_table.information[npc_id][phrase_id] = reader:r_u8()
			else
				priority_table.information[npc_id][phrase_id] = reader:r_bool()
			end
		end
	end
]]

    is_saved = reader:r_bool()
--[[
	if(is_saved) then
        local counter = reader:r_u16()
		priority_table.occupation[npc_id] = {}
		for i=1,counter do
			local phrase_id = reader:r_stringZ()
			if(phrase_id~="ignore_once") then
				priority_table.occupation[npc_id][phrase_id] = reader:r_u8()
			else
				priority_table.occupation[npc_id][phrase_id] = reader:r_bool()
			end
		end
	end
]]

    is_saved = reader:r_bool()
--[[
	if(is_saved) then
        local counter = reader:r_u16()
		priority_table.place[npc_id] = {}
		for i=1,counter do
			local phrase_id = reader:r_stringZ()
			if(phrase_id~="ignore_once") then
				priority_table.place[npc_id][phrase_id] = reader:r_u8()
			else
				priority_table.place[npc_id][phrase_id] = reader:r_bool()
			end
		end
	end
]]
	set_save_marker(reader, "load", true, "dialog_manager")
end
-- Initialize npc start dialog
function init_start_dialogs(dialog, str)
	printf(string.format("init_%s_dialogs", str))
    local phrase = dialog:AddPhrase("", tostring(0), "", -10000)
    phrase = dialog:AddPhrase("", tostring(1), tostring(0), -10000)
    local script = phrase:GetPhraseScript()
    script:AddAction(string.format("dialog_manager.fill_priority_%s_table", str))
    local ph = false
	for k,v in pairs(phrase_table[str]) do
		ph = true
        phrase = dialog:AddPhrase(v.name, tostring(v.id), tostring(1), -10000)
		log("--->"..tostring(v.name))
        if(phrase) then
            script = phrase:GetPhraseScript()
            script:AddPrecondition(string.format("dialog_manager.precondition_%s_dialogs", str))
            script:AddPrecondition("dialog_manager.precondition_not_sim_action_dialog")
            script:AddAction(string.format("dialog_manager.action_%s_dialogs", str))
		    script:AddAction("dialog_manager.action_sim_action_dialog")
			if(v.wounded=="true") then
				log("--->"..tostring(v.wounded))
	            script:AddPrecondition("dialogs.is_wounded")
--				local bandage_id = get_id()
				local medkit_id = get_id()
				local sorry_id = get_id()
				local thanks_id = get_id()
--		        phrase = dialog:AddPhrase("dm_wounded_bandage", tostring(bandage_id), tostring(v.id), -10000)
--	            script = phrase:GetPhraseScript()
--	            script:AddPrecondition("dialogs.actor_have_bandage")
--	            script:AddAction("dialogs.transfer_bandage")
--	            script:AddAction("dialogs.break_dialog")
--		        phrase = dialog:AddPhrase("dm_wounded_thanks", tostring(thanks_id), tostring(bandage_id), -10000)

		        phrase = dialog:AddPhrase("dm_wounded_medkit", tostring(medkit_id), tostring(v.id), -10000)
        if(phrase) then
			log("ok1")
		end
	            script = phrase:GetPhraseScript()
	            script:AddPrecondition("dialogs.actor_have_medkit")
	            script:AddAction("dialogs.transfer_medkit")
	            script:AddAction("dialogs.break_dialog")
		        phrase = dialog:AddPhrase("dm_wounded_thanks", tostring(thanks_id), tostring(medkit_id), -10000)
        if(phrase) then
			log("ok2")
		end

		        phrase = dialog:AddPhrase("dm_wounded_sorry", tostring(sorry_id), tostring(v.id), -10000)
        if(phrase) then
			log("ok3")
		end
	            script = phrase:GetPhraseScript()
	            script:AddAction("dialogs.break_dialog")
			else
	            script:AddPrecondition("dialogs.is_not_wounded")
			end
        end
    end
    phrase = dialog:AddPhrase("dm_sim_action_attack_point_none", tostring(get_id()), tostring(1), -10000)
    script = phrase:GetPhraseScript()
    script:AddPrecondition("dialog_manager.precondition_sim_action_dialog")
    script:AddPrecondition("dialog_manager.precondition_sim_action_attack_point_none")
    script:AddAction("dialog_manager.action_sim_action_dialog")

    phrase = dialog:AddPhrase("dm_sim_action_attack_point_friend", tostring(get_id()), tostring(1), -10000)
    script = phrase:GetPhraseScript()
    script:AddPrecondition("dialog_manager.precondition_sim_action_dialog")
    script:AddPrecondition("dialog_manager.precondition_sim_action_attack_point_friend")
    script:AddAction("dialog_manager.action_sim_action_dialog")

    phrase = dialog:AddPhrase("dm_sim_action_attack_point_enemy", tostring(get_id()), tostring(1), -10000)
    script = phrase:GetPhraseScript()
    script:AddPrecondition("dialog_manager.precondition_sim_action_dialog")
    script:AddPrecondition("dialog_manager.precondition_sim_action_attack_point_enemy")
    script:AddAction("dialog_manager.action_sim_action_dialog")

    phrase = dialog:AddPhrase("dm_sim_action_stay_point_none_near", tostring(get_id()), tostring(1), -10000)
    script = phrase:GetPhraseScript()
    script:AddPrecondition("dialog_manager.precondition_sim_action_dialog")
    script:AddPrecondition("dialog_manager.precondition_sim_action_stay_point_none_near")
    script:AddAction("dialog_manager.action_sim_action_dialog")

    phrase = dialog:AddPhrase("dm_sim_action_stay_point_enemy_near", tostring(get_id()), tostring(1), -10000)
    script = phrase:GetPhraseScript()
    script:AddPrecondition("dialog_manager.precondition_sim_action_dialog")
    script:AddPrecondition("dialog_manager.precondition_sim_action_stay_point_enemy_near")
    script:AddAction("dialog_manager.action_sim_action_dialog")
	if not(ph) then
        phrase = dialog:AddPhrase(string.format("dm_%s_general", str), tostring(v.id), tostring(1), -10000)
	end
end
-- Initialize selected actor dialog
function init_actor_dialogs(dialog, str)
	printf(string.format("init_%s_dialogs", str))
-- add actor question
    local phrase = dialog:AddPhrase(string.format("dm_%s_general", str), tostring(0), "", -10000)
    local script = phrase:GetPhraseScript()
-- set fill priority callback
--    script:AddPrecondition("dialog_manager.precondition_is_phrase_disabled")
    script:AddAction(string.format("dialog_manager.fill_priority_%s_table", str))
    script:AddAction("dialog_manager.action_disable_phrase")
-- set default answer (if no avaliable)
    local answer = dialog:AddPhrase(string.format("dm_%s_no_more", str), tostring(1), tostring(0), -10000)
    local script = answer:GetPhraseScript()
    script:AddPrecondition(string.format("dialog_manager.precondition_%s_dialogs_no_more", str))
-- add npc answers
    for k,v in pairs(phrase_table[str]) do
        phrase = dialog:AddPhrase(v.name, tostring(v.id), tostring(0), -10000)
        if(phrase) then
            script = phrase:GetPhraseScript()
            script:AddPrecondition(string.format("dialog_manager.precondition_%s_dialogs", str))
            script:AddAction(string.format("dialog_manager.action_%s_dialogs", str))
        end
    end
end
-- Fill selected priority table
function fill_priority_table(npc, PT_subtable, PRT_subtable)
    local npc_id = npc:id()
	if(PRT_subtable[npc_id]==nil) then
-- if subtable for npc is not set - create it
		PRT_subtable[npc_id] = {}
	end
    for num, phrase in pairs(PT_subtable) do
-- calculate priority for each phrase
		local pr = calculate_priority(PRT_subtable, phrase, npc, phrase.id)
		log("--->phrase ["..phrase.name.."] priority ["..pr.."]")
	end
end
-- Calculate precondition for default phrase in information dialog
function precondition_no_more(npc, str)
	local pr, id = get_highest_priority_phrase(phrase_table[str], priority_table[str], npc)
    if(pr==-1) or (id==0) then
-- if there is no phrase with priority greater than -1 or there is no phrases in table
		printf("there is no avaliable "..str.." dialogs")
-- show default phrase
        return true
    else
        return false
    end
end
-- Calculate phrase's preconditions
function precondition(npc, PT_subtable, PRT_subtable, phrase_id)
-- recalculate current phrase priority
	local priority = calculate_priority(PRT_subtable, PT_subtable[phrase_id], npc, phrase_id)
-- if current phrase is with highest priority - show it
    return is_highest_priority_phrase(PT_subtable, PRT_subtable, npc, phrase_id)
end
-- Calculate phrase priority
function calculate_priority(PRT_subtable, PTID_subtable, npc, phrase_id)
	local priority = -1
    local npc_id = npc:id()
	if(PTID_subtable.npc_community=="not_set") then
    	priority = priority + 0
	elseif(PTID_subtable.npc_community[1]=="all") then
    	priority = priority + 1
	else
		for i=1,#PTID_subtable.npc_community do
			if(PTID_subtable.npc_community[i]==npc:character_community()) then
		        priority = priority + 3
--		        priority = priority + 2
				break
			end
		end
	    priority = priority - 1 -- if community hasn't match - then decrease priority
	end
	if(PTID_subtable.level=="not_set") then
    	priority = priority + 0
	elseif(PTID_subtable.level[1]=="all") then
    	priority = priority + 1
	else
		for i=1,#PTID_subtable.level do
			if(PTID_subtable.level[i]==level.name()) then
		        priority = priority + 3
--		        priority = priority + 2
				break
			end
		end
	    priority = priority - 1 -- if level hasn't match - then decrease priority
	end
	if(PTID_subtable.actor_community=="not_set") then
    	priority = priority + 0
	elseif(PTID_subtable.actor_community=="all") then
    	priority = priority + 1
	else
		for i=1,#PTID_subtable.actor_community do
			if(PTID_subtable.actor_community[i]==db.actor:character_community()) then
		        priority = priority + 2
				break
			end
		end
	end
    if(PTID_subtable.wounded=="true") then
        if not(xr_wounded.is_heavy_wounded_by_id(npc:id())) then
	        priority = -1
		else
	        priority = priority + 1
        end
    else
        if(xr_wounded.is_heavy_wounded_by_id(npc:id())) then
	        priority = -1
		else
	        priority = priority + 1
        end
    end
	if(PRT_subtable[npc:id()].ignore_once) then
	    if(PTID_subtable.once=="true") then
            priority = -1
		end
	end
	if(PRT_subtable[npc_id][phrase_id]~=nil) and (PRT_subtable[npc_id][phrase_id]==255) then
-- if there was set the highest priority for phrase
        priority = 255
	end
    PRT_subtable[npc_id][phrase_id] = priority
	return priority
end
-- Set phrase end action
function action(PT_subtable, PRT_subtable, cur_phrase_id, npc)
	if not(PRT_subtable[npc:id()].ignore_once) then
	    if(PT_subtable[cur_phrase_id].once=="true") then
	        set_phrase_highest_priority(PRT_subtable, npc:id(), cur_phrase_id)
		end
        PRT_subtable[npc:id()].ignore_once = true
	end
end
-- Set the highest priority to selected phrase
function set_phrase_highest_priority(PRT_subtable, npc_id, phrase_id)
	if(PRT_subtable[npc_id]~=nil) then
		PRT_subtable[npc_id][phrase_id] = 255
	else
		PRT_subtable[npc_id] = {}
		PRT_subtable[npc_id][phrase_id] = 255
	end
end
-- Reset phrase priority
function reset_phrase_priority(PT_subtable, PRT_subtable, npc, phrase_id)
	local npc_id = npc:id()
	if(PRT_subtable[npc_id]~=nil) then
		PRT_subtable[npc_id][phrase_id] = -1
	else
-- if there is no such npc id in table then create it
		PRT_subtable[npc_id] = {}
-- and calculate priority for this phrase
		PRT_subtable[npc_id][phrase_id] = calculate_priority(PRT_subtable, PT_subtable[phrase_id], npc, phrase_id)
	end
end
-- Is the phrase priority the highest?
function is_highest_priority_phrase(PT_subtable, PRT_subtable, npc, phrase_id)
	local npc_id = npc:id()
	if(PRT_subtable[npc_id]~=nil) then
-- if there is a subtable for this npc
		local id = phrase_id
		local pr = PRT_subtable[npc_id][phrase_id]
	    for phr_id, priority in pairs(PRT_subtable[npc_id]) do
-- iterate through array to get if selected phrase is with the highest priority
			if(phr_id~="ignore_once") then
	            if(priority>pr) then
	                return false
	            end
			end
	    end
		return true
	else
-- if there is no subtable for this npc - create it
        reset_phrase_priority(PT_subtable, PRT_subtable, npc, phrase_id)
		return false
	end
end
-- Get the phrase with the highest priority
function get_highest_priority_phrase(PT_subtable, PRT_subtable, npc)
	local npc_id = npc:id()
	if(PRT_subtable[npc_id]~=nil) then
-- if there is a subtable for this npc
		local id = 0
		local pr = -1
	    for phr_id, priority in pairs(PRT_subtable[npc_id]) do
-- iterate through array for the highest priority phrase
			if(phr_id~="ignore_once") then
	            if(priority>pr) then
	                pr = priority
	                id = phr_id
	            end
			end
	    end
		return pr, id
	else
-- if there is no subtable for this npc - create it
        reset_phrase_priority(PT_subtable, PRT_subtable, npc, phrase_id)
		return -1, 0
	end
end
--------------------------------------------------------------------------------
-- Initializing, filling priority tables, setting preconditions and actions
--------------------------------------------------------------------------------
-- Initialize npc hello start dialog
function init_hello_dialogs(dialog)
	init_start_dialogs(dialog, "hello")
end
-- Initialize actor information dialog (like "News", or "What interesting can you say?")
function init_information_dialogs(dialog)
	init_actor_dialogs(dialog, "information")
end
-- Initialize actor occupation dialog (like "What are you doing here?")
function init_occupation_dialogs(dialog)
	init_actor_dialogs(dialog, "occupation")
end
-- Initialize actor place dialog (like "What is this place like?")
function init_place_dialogs(dialog)
	init_actor_dialogs(dialog, "place")
end
-- Fill phrase priority table for hello start dialog
function fill_priority_hello_table(actor, npc, dialog_name, phrase_id)
	fill_priority_table(npc, phrase_table.hello, priority_table.hello)
end
-- Fill phrase priority table for information dialog
function fill_priority_information_table(actor, npc, dialog_name, phrase_id)
	fill_priority_table(npc, phrase_table.information, priority_table.information)
end
-- Fill phrase priority table for occupation dialog
function fill_priority_occupation_table(actor, npc, dialog_name, phrase_id)
	fill_priority_table(npc, phrase_table.occupation, priority_table.occupation)
end
-- Fill phrase priority table for place dialog
function fill_priority_place_table(actor, npc, dialog_name, phrase_id)
	fill_priority_table(npc, phrase_table.place, priority_table.place)
end
-- Calculate precondition for phrases in hello start dialog
function precondition_hello_dialogs(npc, actor, dialog_name, parent_id, id)
    return precondition(npc, phrase_table.hello, priority_table.hello, id)
end
-- Set phrase end action for hello start dialog
function action_hello_dialogs(npc, actor, dialog_name, id)
    action(phrase_table.hello, priority_table.hello, id, npc)
end
-- Calculate precondition for default phrase in information dialog
function precondition_information_dialogs_no_more(npc, actor, dialog_name, parent_id, id)
    return precondition_no_more(npc, "information")
end
-- Calculate preconditions for phrases in information dialog
function precondition_information_dialogs(npc, actor, dialog_name, parent_id, id)
    return precondition(npc, phrase_table.information, priority_table.information, id)
end
-- Set phrase end action for information dialog
function action_information_dialogs(npc, actor, dialog_name, id)
    action(phrase_table.information, priority_table.information, id, npc)
end
-- Calculate precondition for default phrase in occupation dialog
function precondition_occupation_dialogs_no_more(npc, actor, dialog_name, parent_id, id)
    return precondition_no_more(npc, "occupation")
end
-- Calculate preconditions for phrases in occupation dialog
function precondition_occupation_dialogs(npc, actor, dialog_name, parent_id, id)
    return precondition(npc, phrase_table.occupation, priority_table.occupation, id)
end
-- Set phrase end action for occupation dialog
function action_occupation_dialogs(npc, actor, dialog_name, id)
    action(phrase_table.occupation, priority_table.occupation, id, npc)
end
-- Calculate precondition for default phrase in place dialog
function precondition_place_dialogs_no_more(npc, actor, dialog_name, parent_id, id)
    return precondition_no_more(npc, "place")
end
-- Calculate preconditions for phrases in place dialog
function precondition_place_dialogs(npc, actor, dialog_name, parent_id, id)
    return precondition(npc, phrase_table.place, priority_table.place, id)
end
-- Set phrase end action for place dialog
function action_place_dialogs(npc, actor, dialog_name, id)
    action(phrase_table.place, priority_table.place, id, npc)
end

local rnd = 0
-- Set phrase end action for place dialog
function action_sim_action_dialog(npc, actor, dialog_name, id)
    rnd = 0
end
-- Precondition for simulation answer
function precondition_sim_action_dialog(npc, actor, dialog_name)
	local squad = db.storage[npc:id()].squad_obj
	if(xr_wounded.is_heavy_wounded_by_id(npc:id())) then
		return false
	end
	if(squad~=nil) then
		if(squad.current_action~=nil) then
			if(rnd==0) then
				rnd = math.random(1, 100)
			end
			if(rnd>0) then
				return true
			end
		end
	end
	return false
end
-- Precondition for simulation answer
function precondition_not_sim_action_dialog(npc, actor, dialog_name)
    return not(precondition_sim_action_dialog(npc, actor, dialog_name))
end
-- Precondition for simulation answer
function precondition_sim_action_attack_point_none(npc, actor, dialog_name)
	local squad = db.storage[npc:id()].squad_obj
    if(squad.current_action.name=="attack_point") then
		local smart = sim_board.get_sim_board().smarts[squad.current_action.dest_smrt_id].smrt
		if(smart.player_name=="none") then
		    return true
		end
	end
	return false
end
-- Precondition for simulation answer
function precondition_sim_action_attack_point_friend(npc, actor, dialog_name)
	local squad = db.storage[npc:id()].squad_obj
    if(squad.current_action.name=="attack_point") then
		local smart = sim_board.get_sim_board().smarts[squad.current_action.dest_smrt_id].smrt
		if(smart.player_name==squad.player_id) then
		    return true
		end
	end
	return false
end
-- Precondition for simulation answer
function precondition_sim_action_attack_point_enemy(npc, actor, dialog_name)
	local squad = db.storage[npc:id()].squad_obj
    if(squad.current_action.name=="attack_point") then
		local smart = sim_board.get_sim_board().smarts[squad.current_action.dest_smrt_id].smrt
		if(smart.player_name~=squad.player_id) and (smart.player_name~="none") then
		    return true
		end
	end
	return false
end
-- Precondition for simulation answer
function precondition_sim_action_stay_point_none_near(npc, actor, dialog_name)
	local squad = db.storage[npc:id()].squad_obj
    if(squad.current_action.name~="attack_point") then
		local smart = sim_board.get_sim_board().smarts[squad.current_action.src_smrt_id]
		local name = "none"
		for k,v in pairs(smart.targets) do
			if(v.player_name~="none") then
				name = v.player_name
				break
			end
		end
		if(name=="none") then
			return true
		end
	end
	return false
end
-- Precondition for simulation answer
function precondition_sim_action_stay_point_enemy_near(npc, actor, dialog_name)
	local squad = db.storage[npc:id()].squad_obj
    if(squad.current_action.name~="attack_point") then
		local smart = sim_board.get_sim_board().smarts[squad.current_action.src_smrt_id]
		local name = "none"
		for k,v in pairs(smart.targets) do
			if(v.player_name~="none") then
				name = v.player_name
				break
			end
		end
		if(name~="none") then
			return true
		end
	end
	return false
end
function precondition_is_phrase_disabled(fs,ss,dn,ppi,pi)
	local npc = dialogs.who_is_npc(fs, ss)
	if(pi=="") then
		pi = dn
	end
	if(disabled_phrases[npc:id()]) and (disabled_phrases[npc:id()][pi]) then
		return false
	else
		return true
	end
end

function action_disable_phrase(fs,ss,dn,pi)
	local npc = dialogs.who_is_npc(fs, ss)
	if(pi=="0") then
		pi = dn
	end
	if(disabled_phrases[npc:id()]==nil) then
		disabled_phrases[npc:id()] = {}
	end
    disabled_phrases[npc:id()][pi] = true
end

function restore_npc_disabled_phrases(npc_id)
    disabled_phrases[npc_id] = nil
end

function create_bye_phrase()
	if(rnd==0) then
		rnd = math.random(1, 99)
	end
	if(rnd>=66) then
		return game.translate_string("actor_break_dialog_1")
	elseif(rnd>=33) then
		return game.translate_string("actor_break_dialog_2")
	else
		return game.translate_string("actor_break_dialog_3")
	end
end

function news_dialog_precondition(fs,ss,dn,pi)
	local npc = ss
	if(disabled_phrases[npc:id()]~=nil) then
		if(disabled_phrases[npc:id()]["dm_information_dialog"]~=nil) and
		  (disabled_phrases[npc:id()]["dm_occupation_dialog"]~=nil) and
		  (disabled_phrases[npc:id()]["dm_place_dialog"]~=nil) then
			return true
		end
	end
	return false
end

function create_question_phrase()
	if(rnd==0) then
		rnd = math.random(1, 99)
	end
	if(rnd>=66) then
		return game.translate_string("dm_actor_news_1")
	elseif(rnd>=33) then
		return game.translate_string("dm_actor_news_2")
	else
		return game.translate_string("dm_actor_news_3")
	end
end

function create_answer_phrase(fs,dn,pi)
	local npc = fs
	local community = npc:character_community()
	if(community~="bandit") and (community~="army") and (community~="dolg") and
	  (community~="freedom") and (community~="csky") then
		community = "default"
	end

	if(rnd==0) then
		rnd = math.random(1, 80)
	end
	if(rnd>=60) then
		return game.translate_string("dm_"..community.."_no_news_1")
	elseif(rnd>=40) then
		return game.translate_string("dm_"..community.."_no_news_2")
	elseif(rnd>=20) then
		return game.translate_string("dm_"..community.."_no_news_3")
	else
		return game.translate_string("dm_"..community.."_no_news_4")
	end
end