--------------------------------------------------------------------------------
-- Dialog manager --------------------------------------------------------------
-- Made by Peacemaker ----------------------------------------------------------
-- 11.01.08 --------------------------------------------------------------------
--------------------------------------------------------------------------------
-- modified by Alundaio

RandomVal = 0
disabled_phrases = {} -- temporary table of phrases which have been disabled during a conversation
local quest_disabled_phrases = {} -- temporary table of phrases which have been disabled during a conversation
local id_counter = 6 -- start from 5 because of adding root phrases

phrase_table = {
					hello = {},
					job = {},
					anomalies = {},
					place = {},
					information = {},
					tips = {}
				}
priority_table = {
					hello = {},
					job = {},
					anomalies = {},
					place = {},
					information = {},
					tips = {}
				 }


----------------------- Alundaio easy scripted_dialog
local saved_dialog
--local cond_list = {}
function add_dialog(p_id,id,phrase_id,cond,act)
	if not ( saved_dialog ) then
		return
	end

	local phrase = saved_dialog:AddPhrase(phrase_id,tostring(id),tostring(p_id),-10000)
	if not ( phrase ) then
		return
	end

	--printf("phrase_id = %s",phrase_id)

	local phrase_script = phrase:GetPhraseScript()
	if ( cond ) then
		if ( type(cond) == "table" ) then
			for key, value in pairs(cond) do
				if (alun_utils.findfunction(value,_G)) then
					phrase_script:AddPrecondition(value)
					--table.insert(cond_list,value)
				else
					printf("dialog_manager:Error: No such function exists '%s'",value)
				end
			end
		else
			if (alun_utils.findfunction(cond,_G)) then
				phrase_script:AddPrecondition(cond)
				--table.insert(cond_list,value)
			else
				printf("dialog_manager:Error: No such function exists '%s'",cond)
			end
		end
	end

	if ( act ) then
		if ( type(act) == "table" ) then
			for key, value in pairs(act) do
				if (alun_utils.findfunction(value,_G)) then
					phrase_script:AddAction(value)
				else
					printf("dialog_manager:Error: No such function exists '%s'",value)
				end
			end
		else
			if (alun_utils.findfunction(act,_G)) then
				phrase_script:AddAction(act)
			else
				printf("dialog_manager:Error: No such function exists '%s'",act)
			end
		end
	end
	return phrase_script
end

function add_script_dialog(p_id,id,phrase_id,cond,act)
	local d = add_dialog(p_id,id,phrase_id,cond,act)
	if (d) then
		d:SetScriptText(phrase_id)
	end
end

-----------------------------------------------------
-- Dynamic task Dialog
-----------------------------------------------------
function dm_init_dynamic_task_dialog(dialog)
	saved_dialog = dialog

	-- loop is necessary to allow skipping of unwanted tasks
	local base_id = 0 
	for i=1,10 do
		add_dialog("",base_id,"st_ordered_task_query")																												-- Actor
			add_script_dialog(base_id,base_id+1,"dialogs.text_npc_has_ordered_task_to_give")																				-- NPC
				add_dialog(base_id+1,base_id+2,"st_ordered_task_accept",{"dialogs.not_task_is_fetch_task","dialogs.npc_has_ordered_task_to_give"},"dialogs.npc_give_last_task_id") 			-- Actor
					add_dialog(base_id+2,base_id+3,"st_accepted_task_"..math.random(1,3))																							-- NPC
						add_dialog(base_id+3,base_id+4,"dm_universal_actor_exit_thanks",nil,"dialogs.npc_clear_skipped_tasks")									-- Actor
				add_dialog(base_id+1,base_id+5,"st_ordered_task_accept",{"dialogs.task_is_fetch_task","dialogs.npc_has_ordered_task_to_give"}) 					-- Actor					
					add_script_dialog(base_id+5,base_id+6,"dialogs.text_fetch_task",nil,"dialogs.npc_give_last_task_id")								-- NPC
						add_dialog(base_id+6,base_id+7,"dm_universal_actor_exit_thanks",nil,"dialogs.npc_clear_skipped_tasks")										-- Actor
				add_dialog(base_id+1,base_id+10,"st_ordered_task_next","dialogs.npc_has_ordered_task_to_give","dialogs.npc_skip_first_ordered_task")				-- Actor
				add_dialog(base_id+1,base_id+8,"dm_universal_actor_exit",nil,"dialogs.npc_clear_skipped_tasks") 													-- Actor
		base_id = base_id+10
	end
end

function dm_init_dynamic_task_completed_dialog(dialog)
	saved_dialog = dialog
	-- finished task dialog
	add_dialog("",0,"st_ordered_task_query_finished")														-- Actor
		add_script_dialog(0,1,"dialogs.text_task_finish","dialogs.actor_has_finished_ordered_task","dialogs.npc_set_finished_task_complete")		-- NPC
			add_dialog(1,2,"dm_universal_actor_exit_thanks") 																						-- Actor	
		--add_dialog(0,3,"st_ordered_task_not_given_task","dialogs.actor_has_not_finished_ordered_task")												-- NPC
		--	add_dialog(1,2,"dm_universal_actor_exit") 																								-- Actor
end 

function dm_init_sim_dynamic_task_dialog(dialog)
	saved_dialog = dialog

	-- loop is necessary to allow skipping of unwanted tasks
	local base_id = 0 
	for i=1,10 do
		add_dialog("",base_id,"st_sim_ordered_task_query")																												-- Actor
			add_script_dialog(base_id,base_id+1,"dialogs.text_sim_npc_has_ordered_task_to_give")																				-- NPC
				add_dialog(base_id+1,base_id+2,"st_ordered_task_accept",{"dialogs.not_task_is_fetch_task","dialogs.npc_has_ordered_task_to_give"},"dialogs.npc_sim_give_last_task_id") 			-- Actor
					add_dialog(base_id+2,base_id+3,"st_accepted_task_"..math.random(1,3))																							-- NPC
						add_dialog(base_id+3,base_id+4,"dm_universal_actor_exit_thanks",nil,"dialogs.npc_clear_skipped_tasks")									-- Actor
				add_dialog(base_id+1,base_id+5,"st_ordered_task_accept",{"dialogs.task_is_fetch_task","dialogs.npc_has_ordered_task_to_give"}) 					-- Actor					
					add_script_dialog(base_id+5,base_id+6,"dialogs.text_sim_fetch_task",nil,"dialogs.npc_sim_give_last_task_id")								-- NPC
						add_dialog(base_id+6,base_id+7,"dm_universal_actor_exit_thanks",nil,"dialogs.npc_clear_skipped_tasks")										-- Actor
				add_dialog(base_id+1,base_id+10,"st_ordered_task_next","dialogs.npc_has_ordered_task_to_give","dialogs.npc_sim_skip_first_ordered_task")				-- Actor
				add_dialog(base_id+1,base_id+8,"dm_universal_actor_exit",nil,"dialogs.npc_clear_skipped_tasks") 													-- Actor
		base_id = base_id+10
	end
end

function dm_init_sim_dynamic_task_completed_dialog(dialog)
	saved_dialog = dialog
	-- finished task dialog
	add_dialog("",0,"st_sim_ordered_task_query_finished")														-- Actor
		add_script_dialog(0,1,"dialogs.text_sim_task_finish","dialogs.actor_sim_has_finished_ordered_task","dialogs.npc_sim_set_finished_task_complete")		-- NPC
			add_dialog(1,2,"dm_universal_actor_exit_thanks") 																						-- Actor	
		--add_dialog(0,3,"st_ordered_task_not_given_task","dialogs.actor_has_not_finished_ordered_task")												-- NPC
		--	add_dialog(1,2,"dm_universal_actor_exit") 																								-- Actor
end

--[[ TODO: Implement this feature
function dm_init_sim_dynamic_task_giveup_dialog(dialog)
	saved_dialog = dialog
	-- finished task dialog
	add_dialog("",0,"st_ordered_task_query_giveup")														-- Actor
		add_script_dialog(0,1,"dialogs.text_sim_task_finish","dialogs.actor_has_not_finished_ordered_task","dialogs.npc_sim_set_finished_task_complete")		-- NPC
			add_dialog(1,2,"dm_universal_actor_exit_thanks") 																						-- Actor	
		--add_dialog(0,3,"st_ordered_task_not_given_task","dialogs.actor_has_not_finished_ordered_task")												-- NPC
		--	add_dialog(1,2,"dm_universal_actor_exit") 																								-- Actor
end 
--]]

-----------------------------------------------------
-- Dynamic Broker Dialog
-----------------------------------------------------
function dm_init_dynamic_broker_dialog(dialog)
	saved_dialog = dialog

	add_dialog("",0,"st_broker_query") 																		-- Actor
		add_dialog(0,1,"st_broker_npc_start_"..math.random(1,3)) 											-- NPC
			add_script_dialog(1,2,"dialogs.text_actor_has_pda","dialogs.condition_actor_has_pda") 			-- Actor
				add_dialog(2,3,"st_broker_npc_answer_pda_query") 											-- NPC
					add_dialog(3,4,"st_broker_give_pda") 													-- Actor
						add_script_dialog(4,5,"dialogs.text_trade_npc_pda") 								-- NPC
							add_dialog(5,0,"st_broker_query_more")											-- Actor
							add_dialog(5,1111,"dm_universal_actor_exit_thanks")								-- Actor

			add_dialog(1,1111,"dm_universal_actor_exit") 													-- Actor
end

-----------------------------------------------------
-- Dynamic Repair Dialog
-----------------------------------------------------
function dm_init_dynamic_repair_dialog(dialog)
	saved_dialog = dialog

	add_dialog("",0,"st_mechanic_query_items")

	local script = add_dialog(0,1,"st_mechanic_repair_not_done","inventory_upgrades.dont_has_repaired_items")
	script:SetScriptText("inventory_upgrades.dm_repair_not_done")
	add_dialog(0,2,"st_mechanic_repair_done","inventory_upgrades.has_repaired_items","inventory_upgrades.give_repaired_items")
end


-----------------------------------------------------
-- Dynamic Surrender Dialog
-----------------------------------------------------
function dm_init_surrender_dialog(dialog)
	saved_dialog = dialog

	add_dialog("",0,"st_surrender_victim")

	add_dialog(0,1,"st_surrender_query_victim_4","dialogs.victim_is_bounty")
		add_script_dialog(1,9,"dialogs.surrender_victim_answers_bounty")
			add_dialog(9,20,"st_surrender_actor_2",nil,{"dialogs.set_victim_surrendered","dialogs.break_dialog"})
			add_dialog(9,21,"st_surrender_actor_3",nil,{"dialogs.set_victim_surrendered","dialogs.break_dialog"})
			
	add_dialog(0,2,"st_surrender_query_victim_1")
		add_script_dialog(2,10,"dialogs.surrender_victim_answers_1")
			add_dialog(10,20,"st_surrender_actor_2",nil,{"dialogs.set_victim_surrendered","dialogs.break_dialog"})
			add_dialog(10,21,"st_surrender_actor_3",nil,{"dialogs.set_victim_surrendered","dialogs.break_dialog"})

	add_dialog(0,3,"st_surrender_query_victim_2")
		add_script_dialog(3,11,"dialogs.surrender_victim_answers_1")
			add_dialog(11,20,"st_surrender_actor_2",nil,{"dialogs.set_victim_surrendered","dialogs.break_dialog"})
			add_dialog(11,21,"st_surrender_actor_3",nil,{"dialogs.set_victim_surrendered","dialogs.break_dialog"})

	add_dialog(0,4,"st_surrender_query_victim_3")
		add_script_dialog(4,12,"dialogs.surrender_victim_answers_2")
			add_dialog(12,20,"st_surrender_actor_1",nil,{"dialogs.set_victim_surrendered","dialogs.break_dialog"})
end


-- End Alundaio
-----------------------------------------------------------------------------

-- 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()
	local ini = alun_utils.cfg_file(getFS():update_path('$game_config$', '').."misc\\dialog_manager.ltx")
	local category, val, temp_table
	for section,t in pairs(ini.root) do
		val = ini.root[section]["category"]
		category = (val == "hello" or val == "anomalies" or val == "place" or val == "job" or val == "information" or val == "tips") and val or "default"
		if (category ~= "default") then
			temp_table = {}
			temp_table.info = {}
			temp_table.id = tostring(get_id())
			temp_table.name = section
			temp_table.npc_community = ini.root[section]["npc_community"] and parse_names(ini.root[section]["npc_community"]) or "not_set"
			temp_table.actor_community = ini.root[section]["actor_community"] and parse_names(ini.root[section]["actor_community"]) or "not_set"
			temp_table.level = ini.root[section]["level"] and parse_names(ini.root[section]["level"]) or "not_set"
			temp_table.once = ini.root[section]["once"] == "true" and "true" or ini.root[section]["once"] == "false" and "false" or "always"
			temp_table.wounded = ini.root[section]["wounded"] == "true" and "true" or "false"

			if (ini.root[section]["info"]) then
				xr_logic.parse_infop1(temp_table.info, ini.root[section]["info"])
			end

			temp_table.smart = ini.root[section]["smart"] or ""


			phrase_table[category][temp_table.id] = temp_table
		end
	end
end

-- Initialize npc start dialog
function init_start_dialogs(dialog, str)
	saved_dialog = dialog
	--cond_list = empty_table(cond_list)

	add_dialog("",0,"")
	add_dialog(0,1,"",nil,"dialog_manager.fill_priority_"..str.."_table")

	local ph
	local p,d
	local script
	for k,v in pairs(phrase_table[str]) do
		if (v.wounded == "true") then
			script = add_dialog(1,v.id,v.name,{"dialogs.is_wounded","dialog_manager.precondition_"..str.."_dialogs"},"dialog_manager.action_"..str.."_dialogs")
			if (script) then
				ph = true
				add_dialog(v.id,get_id(),"dm_wounded_medkit",{"dialogs.actor_have_medkit"},{"dialogs.transfer_medkit","dialogs.break_dialog"})
				add_dialog(v.id,get_id(),"dm_wounded_sorry",nil,"dialogs.break_dialog")
			end
		else
			add_dialog(1,v.id,v.name,{"dialogs.is_not_wounded","dialog_manager.precondition_"..str.."_dialogs"},"dialog_manager.action_"..str.."_dialogs")
			ph = true
		end
	end

	if not(ph) then
		add_dialog(1,2,"dm_"..str.."_general")
	end
end

-- Initialize new actor dialog
function init_new_dialog(dialog)
	local actor_table = { "job", "anomalies", "information", "tips" }

	saved_dialog = dialog
	add_dialog("",0,"dm_universal_actor_start")
	add_dialog(0,1,"dm_universal_npc_start_"..math.random(1,4))

	local ph,id,str

	for i = 1,4 do
		local id = get_id()
		str = actor_table[i]

		add_dialog(1,id,"dm_"..str.."_general_"..math.random(1,3),nil,"dialog_manager.fill_priority_"..str.."_table")
			local id2 = get_id()
			add_dialog(id,id2,"dm_no_more_"..math.random(1,10),"dialog_manager.precondition_"..str.."_dialogs_no_more")
			add_dialog(id,id2,"dm_do_not_know_"..math.random(1,10),"dialog_manager.precondition_"..str.."_dialogs_do_not_know")

		for k,v in pairs(phrase_table[str]) do
			ph = true
			add_dialog(id,v.id,v.name,"dialog_manager.precondition_"..str.."_dialogs","dialog_manager.action_"..str.."_dialogs")
		end

		if not (ph) then
			add_dialog(id,id2,"dm_"..str.."_do_not_know_"..math.random(1,3))
		end
	end
	add_dialog(1,get_id(),"dm_universal_actor_exit")
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
		PRT_subtable[npc_id] = {}
	end

	for num, phrase in pairs(PT_subtable) do
		if (phrase) then
			calculate_priority(PRT_subtable, phrase, npc, phrase.id)
		end
	end
end

function is_told(npc, str)
	local id = npc:id()
	if (priority_table[str] == nil or priority_table[str][id] == nil) then
		return true
	end
	if priority_table[str][id].told == true then
		return true
	end
	return false
end
-- Calculate precondition for default phrase in information dialog
function precondition_no_more(npc, str, phrase_id)
	local id = npc:id()
	if (priority_table[str] == nil or priority_table[str][id] == nil) then
		return true
	end

	local pr, id = get_highest_priority_phrase(phrase_table[str], priority_table[str], npc, phrase_id)
	if(pr<0) or (id==0) then
		printf("there is no avaliable "..str.." dialogs")
		return true
	end

	if (PRT_subtable[id][phrase_id] == nil or PRT_subtable[id][phrase_id] == -1) then
		return true
	end

	return false
end

-- Calculate phrase's preconditions
function precondition(npc, PT_subtable, PRT_subtable, phrase_id,dialog_name)
	if not (phrase_id) then
		printf("dialog_manager: no pharse_id for dialog %s",dialog_name)
	end

	if not (PRT_subtable[npc:id()]) then
		--printf("dialog_manager:Error:No PRT_subtable for %s with phrase_id=%s and dialog_name=%s",npc:name(),phrase_id,dialog_name)
		reset_phrase_priority(PT_subtable, PRT_subtable, npc, phrase_id)
		return false
	end

	if PRT_subtable[npc:id()] and PRT_subtable[npc:id()].told and PRT_subtable[npc:id()].told == true then
		return false
	end
-- 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)
	if not (PTID_subtable) then
		return
	end

	local f_level = false
	local f_comm = false
	local priority = -1
	local npc_id = npc:id()

	if(PTID_subtable.npc_community=="not_set") then
		f_comm = true
	elseif(PTID_subtable.npc_community[1]=="all") then
		priority = priority + 1
		f_comm = true
	else
		for i=1,#PTID_subtable.npc_community do
			if(PTID_subtable.npc_community[i]==character_community(npc)) then
				priority = priority + 2
				f_comm = true
				break
			end
		end
		priority = priority - 1
	end

	if(PTID_subtable.level=="not_set") then
		f_level = true
	elseif(PTID_subtable.level[1]=="all") then
		priority = priority + 1
		f_level = true
	else
		for i=1,#PTID_subtable.level do
			if(PTID_subtable.level[i]==level.name()) then
				priority = priority + 2
				f_level = true
				break
			end
		end
	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]==character_community(db.actor)) then
					priority = priority + 2
				break
			end
		end
	end

	if(PTID_subtable.wounded=="true") then
		if not(xr_wounded.is_wounded(npc)) then
			priority = -1
		else
			priority = priority + 1
		end
	else

	if(xr_wounded.is_wounded(npc)) then
		priority = -1
	else
		priority = priority + 1
		end
	end

	if f_comm == false or f_level == false then
		priority = -1
	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

	for k,v in pairs(PTID_subtable.info) do
		if v.name then
			if v.required == true then
				if not has_alife_info(v.name) then
					priority = -1
					break
				end
			else
				if has_alife_info(v.name) then
					priority = -1
					break
				end
			end
		end
	end


	PRT_subtable[npc_id][phrase_id] = priority
	return priority
end

-- Set phrase end action
function told(PRT_subtable, npc)
	PRT_subtable[npc:id()].told = true
end

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(PT_subtable,PRT_subtable, npc, 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(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] = 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)
	if not (phrase_id) then
		return
	end

	local npc_id = npc:id()
	if(PRT_subtable[npc_id]) then
		PRT_subtable[npc_id][phrase_id] = -1
	else
		PRT_subtable[npc_id] = {}
		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]
		if pr == nil or pr < 0 then
			return false
		end
		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" and phr_id~="told") 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, phrase_id)
	local npc_id = npc:id()
	if(PRT_subtable[npc_id]) 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" and phr_id~="told") 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

-- 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 new dialog
function fill_priority_job_table(actor, npc, dialog_name, phrase_id)
	fill_priority_table(npc, phrase_table.job, priority_table.job)
end
function fill_priority_anomalies_table(actor, npc, dialog_name, phrase_id)
	fill_priority_table(npc, phrase_table.anomalies, priority_table.anomalies)
end
--function fill_priority_place_table(actor, npc, dialog_name, phrase_id)
--	fill_priority_table(npc, phrase_table.place, priority_table.place)
--end
function fill_priority_information_table(actor, npc, dialog_name, phrase_id)
	fill_priority_table(npc, phrase_table.information, priority_table.information)
end

function fill_priority_tips_table(actor, npc, dialog_name, phrase_id)
	fill_priority_table(npc, phrase_table.tips, priority_table.tips)
end

-- Calculate precondition for phrases in hello start dialog
function precondition_hello_dialogs(npc, actor, dialog_name, parent_id, id)
	--printf("hello")
	return precondition(npc, phrase_table.hello, priority_table.hello, id,dialog_name)
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 occupation dialog
function precondition_job_dialogs_no_more(npc, actor, dialog_name, parent_id, id)
	if is_told(npc, "job") == true then
		return true
	end
	return false
end

function precondition_job_dialogs_do_not_know(npc, actor, dialog_name, parent_id, id)
	return precondition_no_more(npc, "job", id)
end

-- Calculate preconditions for phrases in occupation dialog
function precondition_job_dialogs(npc, actor, dialog_name, parent_id, id)
	--printf("job")
	return precondition(npc, phrase_table.job, priority_table.job, id,dialog_name)
end
-- Set phrase end action for occupation dialog
function action_job_dialogs(npc, actor, dialog_name, id)
	action(phrase_table.job, priority_table.job, id, npc)
	told(priority_table.job, npc)
end

-- Calculate precondition for default phrase in anomalies dialog
function precondition_anomalies_dialogs_no_more(npc, actor, dialog_name, parent_id, id)
	if is_told(npc, "anomalies") == true then
		return true
	end
	return false
end

function precondition_anomalies_dialogs_do_not_know(npc, actor, dialog_name, parent_id, id)
	return precondition_no_more(npc, "anomalies", id)
end
-- Calculate preconditions for phrases in anomalies dialog
function precondition_anomalies_dialogs(npc, actor, dialog_name, parent_id, id)
	local smart = xr_gulag.get_npc_smart(npc)
	if priority_table.anomalies and priority_table.anomalies[npc:id()] and smart ~= nil and tostring(smart:name()) == phrase_table.anomalies[id].smart then
		priority_table.anomalies[npc:id()][id] = -1
		return false
	end
	--printf("anomalies")
	return precondition(npc, phrase_table.anomalies, priority_table.anomalies, id,dialog_name)
end
-- Set phrase end action for information dialog
function action_anomalies_dialogs(npc, actor, dialog_name, id)
	action(phrase_table.anomalies, priority_table.anomalies, id, npc)
	told(priority_table.anomalies, npc)
end

-- Calculate precondition for default phrase in information dialog
function precondition_information_dialogs_no_more(npc, actor, dialog_name, parent_id, id)
	if is_told(npc, "information") == true then
		return true
	end
	return false
end

function precondition_information_dialogs_do_not_know(npc, actor, dialog_name, parent_id, id)
	return precondition_no_more(npc, "information", id)
end
-- Calculate preconditions for phrases in information dialog
function precondition_information_dialogs(npc, actor, dialog_name, parent_id, id)
	--printf("info")
	return precondition(npc, phrase_table.information, priority_table.information, id,dialog_name)
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)
	told(priority_table.information, npc)
end

-- Tips
-- Calculate precondition for default phrase in information dialog
function precondition_tips_dialogs_no_more(npc, actor, dialog_name, parent_id, id)
	if is_told(npc, "tips") == true then
		return true
	end
	return false
end

function precondition_tips_dialogs_do_not_know(npc, actor, dialog_name, parent_id, id)
	return precondition_no_more(npc, "tips", id)
end
-- Calculate preconditions for phrases in tips dialog
function precondition_tips_dialogs(npc, actor, dialog_name, parent_id, id)
	--printf("tips")
	return precondition(npc, phrase_table.tips, priority_table.tips, id,dialog_name)
end
-- Set phrase end action for tips dialog
function action_tips_dialogs(npc, actor, dialog_name, id)
	action(phrase_table.tips, priority_table.tips, id, npc)
	told(priority_table.tips, npc)
end

local rnd = 0

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]) or
		 (quest_disabled_phrases[npc:id()] and quest_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 action_disable_quest_phrase(fs,ss,dn,pi)
	local npc = dialogs.who_is_npc(fs, ss)
	if(pi=="0") then
		pi = dn
	end
	if(quest_disabled_phrases[npc:id()]==nil) then
		quest_disabled_phrases[npc:id()] = {}
	end
	quest_disabled_phrases[npc:id()][pi] = true
end

function create_bye_phrase()
	return game.translate_string("actor_break_dialog_"..RandomVal)
end

function uni_dialog_precond(first_speaker, second_speaker)
	local npc = dialogs.who_is_npc(first_speaker, second_speaker)
	return character_community(npc) ~= "zombied" and character_community(npc) ~= "monolith"
end
