--'*************************************************************************************
--' Функторы тайтла и описания квеста (должны возвращать строку)
--'*************************************************************************************

function condlist(id, field, p, cached_condlist) -- MTB-marek.siemieniuk: cached condlist
	local cond_string = p

	-- MTB-marek.siemieniuk Start: cached condlist
	local parsed_condlist = cached_condlist[cond_string];
	if parsed_condlist == nil then
		parsed_condlist = xr_logic.parse_condlist(nil, "task", "task_condlist", cond_string)
		cached_condlist[cond_string] = parsed_condlist
	end
	-- MTB-marek.siemieniuk End
	
	return xr_logic.pick_section_from_condlist(get_story_object("actor"), nil, parsed_condlist)
end

function zat_b29_adv_title(id, field, p, cached_condlist) -- MTB-marek.siemieniuk: cached condlist
	local actor = get_story_object("actor")
	for i = 16, 23 do
		if (has_alife_info(dialogs_zaton.zat_b29_infop_bring_table[i]) and actor:object(dialogs_zaton.zat_b29_af_table[i])) then
			title = "zat_b29_simple_bring_title_"..i
			break
		elseif has_alife_info(dialogs_zaton.zat_b29_infop_bring_table[i]) then
			title = "zat_b29_simple_find_title_"..i
			break
		end
	end

	return title
end

function zat_b29_adv_descr(id, field, p, cached_condlist) -- MTB-marek.siemieniuk: cached condlist
	local descr
	local f_af = 0
	local actor = get_story_object("actor")

	for i = 16, 23 do
		if (has_alife_info(dialogs_zaton.zat_b29_infop_bring_table[i]) and actor:object(dialogs_zaton.zat_b29_af_table[i])) then
			f_af = 1
			descr = "zat_b29_simple_bring_text_5"
			if has_alife_info("zat_b29_stalker_rival_1_found_af") and has_alife_info("zat_b29_first_rival_taken_out") and f_af ~= 0 then
				return descr
			elseif has_alife_info("zat_b29_stalker_rival_2_found_af") and has_alife_info("zat_b29_second_rival_taken_out") and f_af ~= 0 then
				return descr
			elseif has_alife_info("zat_b29_linker_take_af_from_rival") then
				descr = "zat_b29_simple_bring_text_4"
			elseif has_alife_info("zat_b29_stalkers_rivals_found_af") then
				descr = "zat_b29_simple_bring_text_3"
			elseif has_alife_info("zat_b29_rivals_search") and has_alife_info("zat_b29_exclusive_conditions") then
				descr = "zat_b29_simple_bring_text_1"
			elseif has_alife_info("zat_b29_rivals_search") then
				descr = "zat_b29_simple_bring_text_2"
			end
			break
		elseif has_alife_info(dialogs_zaton.zat_b29_infop_bring_table[i]) then
			descr = "zat_b29_simple_find_text_5"
			if has_alife_info("zat_b29_stalker_rival_1_found_af") and has_alife_info("zat_b29_first_rival_taken_out") and f_af ~= 0 then
				return descr
			elseif has_alife_info("zat_b29_stalker_rival_2_found_af") and has_alife_info("zat_b29_second_rival_taken_out") and f_af ~= 0 then
				return descr
				elseif has_alife_info("zat_b29_linker_take_af_from_rival") then
					descr = "zat_b29_simple_find_text_4"
				elseif has_alife_info("zat_b29_stalkers_rivals_found_af") then
					descr = "zat_b29_simple_find_text_3"
				elseif has_alife_info("zat_b29_rivals_search") and has_alife_info("zat_b29_exclusive_conditions") then
					descr = "zat_b29_simple_find_text_1"
				elseif has_alife_info("zat_b29_rivals_search") then
					descr = "zat_b29_simple_find_text_2"
				end
			break
		end
	end

	return descr
end

function surge_task_title(id, field, p, cached_condlist) -- MTB-marek.siemieniuk: cached condlist
	if(surge_manager.actor_in_cover()) then
		return "hide_from_surge_name_2"
	else
		return "hide_from_surge_name_1"
	end
end

function surge_task_descr(id, field, p, cached_condlist) -- MTB-marek.siemieniuk: cached condlist
	return surge_manager.get_task_descr()
end
--'*************************************************************************************
--' Функторы цели квеста (должны возвращать id персонажа)
--'*************************************************************************************

function target_condlist(id, field, p, cached_condlist) -- MTB-marek.siemieniuk: cached condlist
	local cond_string = p

	-- MTB-marek.siemieniuk Start: cached condlist
	local parsed_condlist = cached_condlist[cond_string];
	if parsed_condlist == nil then
		parsed_condlist = xr_logic.parse_condlist(nil, "task", "task_condlist", cond_string)
		cached_condlist[cond_string] = parsed_condlist
	end
	-- MTB-marek.siemieniuk End
	
	local value = xr_logic.pick_section_from_condlist(get_story_object("actor"), nil, parsed_condlist)

	if value == nil then
		return nil
	end

	local target_obj_id = get_story_object_id(value)

	if target_obj_id == nil then
	--	abort("There is no object with story_id %s", value)
	end

	return target_obj_id
end

function zat_b29_adv_target(id, field, p, cached_condlist) -- MTB-marek.siemieniuk: cached condlist
	local target_obj_id = "zat_a2_stalker_barmen"
	local af = nil
	local actor = get_story_object("actor")


	for i = 16, 23 do
		if (has_alife_info(dialogs_zaton.zat_b29_infop_bring_table[i])
			and actor:object(dialogs_zaton.zat_b29_af_table[i])) then
			af = dialogs_zaton.zat_b29_af_table[i]
			break
		end
	end

	if (not has_alife_info("zat_b29_linker_take_af_from_rival") and has_alife_info("zat_b29_stalkers_rivals_found_af")) then
		if has_alife_info("zat_b29_stalker_rival_1_found_af") then
			if not has_alife_info("zat_b29_first_rival_taken_out") then
				if has_alife_info("zat_b29_exclusive_conditions") then
					target_obj_id = "zat_b29_stalker_rival_1"
				else
					target_obj_id = "zat_b29_stalker_rival_default_1"
				end
			elseif af == nil then
				if has_alife_info("zat_b29_exclusive_conditions") then
					target_obj_id = "zat_b29_stalker_rival_1"
				else
					target_obj_id = "zat_b29_stalker_rival_default_1"
				end
			end
		elseif has_alife_info("zat_b29_stalker_rival_2_found_af") then
			if not has_alife_info("zat_b29_second_rival_taken_out") then
				if has_alife_info("zat_b29_exclusive_conditions") then
					target_obj_id = "zat_b29_stalker_rival_2"
				else
					target_obj_id = "zat_b29_stalker_rival_default_2"
				end
			elseif af == nil then
 				if has_alife_info("zat_b29_exclusive_conditions") then
					target_obj_id = "zat_b29_stalker_rival_2"
				else
					target_obj_id = "zat_b29_stalker_rival_default_2"
				end
			end
		end
		return get_story_object_id(target_obj_id)
	end

	if af ~= nil then
		return get_story_object_id(target_obj_id)
	end

	return nil
end

function surge_task_target(id, field, p, cached_condlist) -- MTB-marek.siemieniuk: cached condlist
	return surge_manager.get_task_target()
end
