local level_scripts = {
	escape_tasks,
	garbage_tasks,
	agroprom_tasks,
	darkvalley_tasks,
	bar_rostok_tasks,
    yantar_tasks,
    mil_tasks,
	radar_tasks,
	pripyat_tasks
}


local jump_level_by_name = {
	l01_escape				= "l02_garbage",
	l02_garbage				= "l03_agroprom",
	l03_agroprom			= "l03u_agr_underground",
	l03u_agr_underground	= "l04_darkvalley",
	l04_darkvalley			= "l04u_labx18",
	l04u_labx18				= "l05_bar",
	l05_bar					= "l06_rostok",
	l06_rostok				= "l07_military",
	l07_military			= "l08_yantar",
	l08_yantar				= "l08u_brainlab",
	l08u_brainlab			= "l10_radar",
	l10_radar				= "l10u_bunker",
	l10u_bunker				= "l11_pripyat",
	l11_pripyat				= "l12_stancia",
	l12_stancia				= "l12u_sarcofag",
	l12u_sarcofag			= "l12u_control_monolith",
	l12u_control_monolith	= "l12_stancia_2",
	l12_stancia_2			= "l01_escape"
}

function proceed()
	if db.actor == nil then
		return
	end
	for k, v in pairs( level_scripts ) do
		v.proceed()
	end

	if db.actor:has_info("autojump_enabled") then
		local jname = jump_level_by_name[level.name()]
		if jname ~= nil then
			get_console():execute("flush")
			get_console():execute("jump_to_level "..jname)
		end
	end
end

function autojump()
	db.actor:give_info_portion("autojump_enabled")
end

function process_info_portion(info_id)
	if db.actor == nil then
		return
	end
	for k, v in pairs( level_scripts ) do
		if v.process_info_portion(info_id) then
			return
		end
	end
end

function add_location(sid, location, hint)
	local sim = alife()
	if sim then
		local obj = sim:story_object(sid)
		
		if obj then
			if hint then
				level.map_add_object_spot(obj.id, location, hint)
			else
				level.map_add_object_spot(obj.id, location, "")
			end
		end
	end
end

function remove_location(sid, location)
	local sim = alife()
	if sim then
		local obj = sim:story_object(sid)
		if obj and level.map_has_object_spot(obj.id, location) then
			level.map_remove_object_spot(obj.id, location)
		end
	end
end

function remove_location_safe(sid, location)
	local sim = alife()
	if sim then
		local obj = sim:story_object(sid)
		if obj then
		   for a = 1, level.map_has_object_spot(obj.id, location), 1 do
			   level.map_remove_object_spot(obj.id, location)
		   end	
		end
	end
end

function is_map_spot_present(sid, location)
	local sim = alife()
	if sim then
		local obj = sim:story_object(sid)
		if obj and level.map_has_object_spot(obj.id, location) ~= 0 then
		   return true
		end
	end
	return false
end
  

function addportals()

 if db.storage[db.actor:id()].pstor == nil then db.storage[db.actor:id()].pstor = {} end
    local port_levels = {"l01_escape", "l02_garbage", "l03_agroprom", "l05_bar", "l06_rostok", "l04_darkvalley", "l08_yantar", "l07_military", "l10_radar", "l11_pripyat", "l12_stancia_2", "l12_stancia", "l12u_sarcofag", "l12u_control_monolith", "l04u_labx18", "l08u_brainlab", "l10u_bunker", "l03u_agr_underground"}
	local clvl
    for clvl = 1, 18 do
		local port_lvl_name = port_levels[clvl]
		if db.storage[db.actor:id()].pstor[port_lvl_name .. "_a_id"] ~= nil then
		  local lvl_b_name = db.storage[db.actor:id()].pstor[port_lvl_name .. "_b_name"]
		  if lvl_b_name ~= nil then			
			local obja_id = db.storage[db.actor:id()].pstor[port_lvl_name .. "_a_id"]
			local objb_id = db.storage[db.actor:id()].pstor[port_lvl_name .. "_b_id"]
			local a_time = db.storage[db.actor:id()].pstor[port_lvl_name .. "_a_time"]
			local b_time = db.storage[db.actor:id()].pstor[port_lvl_name .. "_b_time"]
			local a_num = db.storage[db.actor:id()].pstor[port_lvl_name .. "_a_num"]
			local b_num = db.storage[db.actor:id()].pstor[port_lvl_name .. "_b_num"]
			local lvl_a_name = port_lvl_name

			if obja_id ~= nil then
			 if not is_map_spot_present(obja_id, "level_changer") then
			   level.map_add_object_spot(obja_id, "level_changer", "Portal (" .. a_num .. ")" .. game.translate_string(lvl_a_name) .. " to (" .. b_num .. ")" .. game.translate_string(lvl_b_name) .. " :: " .. a_time)
			 end
			end
			if objb_id ~= nil then
			 if not is_map_spot_present(objb_id, "level_changer") then
			   level.map_add_object_spot(objb_id, "level_changer", "Portal (" .. b_num .. ")" .. game.translate_string(lvl_b_name).. " to (" .. a_num .. ")" .. game.translate_string(lvl_a_name) .. " :: " .. b_time)
			 end
			end

		  else
			logf("lvl_b_name is nil")
			local obja_id = db.storage[db.actor:id()].pstor[port_lvl_name .. "_a_id"]
			local objb_id = db.storage[db.actor:id()].pstor[port_lvl_name .. "_b_id"]
			local a_time = db.storage[db.actor:id()].pstor[port_lvl_name .. "_a_time"]
			local b_time = db.storage[db.actor:id()].pstor[port_lvl_name .. "_b_time"]
			local a_num = db.storage[db.actor:id()].pstor[port_lvl_name .. "_a_num"]
			local b_num = db.storage[db.actor:id()].pstor[port_lvl_name .. "_b_num"]
			lvl_b_name = port_lvl_name
			local lvl_a_name = db.storage[db.actor:id()].pstor[port_lvl_name .. "_a_name"]

			if obja_id ~= nil then
			logf("obja_id ~= nil")
			 if not is_map_spot_present(obja_id, "level_changer") then
			   level.map_add_object_spot(obja_id, "level_changer", "Portal (" .. a_num .. ")" .. game.translate_string(lvl_a_name) .. " to (" .. b_num .. ")" .. game.translate_string(lvl_b_name) .. " :: " .. a_time)
			 end
			end
			if objb_id ~= nil then
			logf("objb_id ~= nil")
			 if not is_map_spot_present(objb_id, "level_changer") then
			   level.map_add_object_spot(objb_id, "level_changer", "Portal (" .. b_num .. ")" .. game.translate_string(lvl_b_name) .. " to (" .. a_num .. ")" .. game.translate_string(lvl_a_name) .. " :: " .. b_time)
			 end
			end

		  end
	   end

		if db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_a_id"] ~= nil then
		  local lvl_b_name = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_b_name"]
		  if lvl_b_name ~= nil then			
			local obja_id = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_a_id"]
			local objb_id = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_b_id"]
			local a_time = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_a_time"]
			local b_time = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_b_time"]
			local a_num = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_a_num"]
			local b_num = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_b_num"]
			local lvl_a_name = port_lvl_name

			if obja_id ~= nil then
			 if not is_map_spot_present(obja_id, "level_changer") then
			   level.map_add_object_spot(obja_id, "level_changer", "Portal (" .. a_num .. ")" .. game.translate_string(lvl_a_name) .. " to (" .. b_num .. ")" .. game.translate_string(lvl_b_name) .. " :: " .. a_time)
			 end
			end
			if objb_id ~= nil then
			 if not is_map_spot_present(objb_id, "level_changer") then
			   level.map_add_object_spot(objb_id, "level_changer", "Portal (" .. b_num .. ")" .. game.translate_string(lvl_b_name) .. " to (" .. a_num .. ")" .. game.translate_string(lvl_a_name) .. " :: " .. b_time)
			 end
			end

		  else
			logf("lvl_b_name is nil")
			local obja_id = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_a_id"]
			local objb_id = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_b_id"]
			local a_time = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_a_time"]
			local b_time = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_b_time"]
			local a_num = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_a_num"]
			local b_num = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_b_num"]
			lvl_b_name = port_lvl_name
			local lvl_a_name = db.storage[db.actor:id()].pstor[port_lvl_name .. "_sl_a_name"]

			if obja_id ~= nil then
			logf("obja_id ~= nil")
			 if not is_map_spot_present(obja_id, "level_changer") then
			   level.map_add_object_spot(obja_id, "level_changer", "Portal (" .. a_num .. ")" .. game.translate_string(lvl_a_name) .. " to (" .. b_num .. ")" .. game.translate_string(lvl_b_name) .. " :: " .. a_time)
			 end
			end
			if objb_id ~= nil then
			logf("objb_id ~= nil")
			 if not is_map_spot_present(objb_id, "level_changer") then
			   level.map_add_object_spot(objb_id, "level_changer", "Portal (" .. b_num .. ")" .. game.translate_string(lvl_b_name) .. " to (" .. a_num .. ")" .. game.translate_string(lvl_a_name) .. " :: " .. b_time)
			 end
			end

		  end
	   end


	end

end


function add_lchanger_location()
	local sim = alife()

	-- portals
	if db.remove_portal_spot == "no" then addportals() end
	-- end

	if sim then
		-- escape
		local obj = sim:story_object(91)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_garbage")
		end
		local obj = sim:story_object(8000)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_darkvalley")
		end
		-- garbage
		obj = sim:story_object(191)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_agroprom")
		end
		obj = sim:story_object(192)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_agroprom")
		end
		obj = sim:story_object(193)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_escape")
		end
		obj = sim:story_object(194)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_bar")
		end
		obj = sim:story_object(195)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_darkvalley")
		end
		obj = sim:story_object(196)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_darkvalley")
		end
		
		-- agroprom
		obj = sim:story_object(391)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_garbage")
		end
		obj = sim:story_object(392)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_garbage")
		end

		-- darkvalley
		obj = sim:story_object(415)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_garbage")
		end
		obj = sim:story_object(416)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_garbage")
		end
		obj = sim:story_object(417)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_escape")
		end

		-- rostok
		obj = sim:story_object(608)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_yantar")
		end
		
		-- bar
		obj = sim:story_object(592)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_military")
		end
		obj = sim:story_object(593)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_garbage")
		end
		obj = sim:story_object(594)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "bar_rostok")
		end

		-- military
		obj = sim:story_object(771)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_bar")
		end
		obj = sim:story_object(773)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_radar")
		end

		-- yantar
		obj = sim:story_object(925)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_rostok")
		end

		-- radar
		obj = sim:story_object(1007)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_pripyat")
		end
		obj = sim:story_object(1008)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_military")
		end

		-- pripyat
		obj = sim:story_object(833)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_radar")
		end
		obj = sim:story_object(832)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_aes")
		end
		obj = sim:story_object(8001)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_pripyat")
		end

		-- testing stancia2 apenas para ver o level_changer e depois remover daqui
		obj = sim:story_object(8002)
		if obj then
			-- level.map_add_object_spot(obj.id, "level_changer", "to_pripyat")
		end
		-- fim

		obj = sim:story_object(8003)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_aes")
		end

-- esc to everywhere testing
		obj = sim:story_object(8004)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_pripyat")
		end
		obj = sim:story_object(8005)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_aes")
		end
		obj = sim:story_object(8006)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_aes2")
		end
		obj = sim:story_object(8007)
		if obj then
			level.map_add_object_spot(obj.id, "level_changer", "to_monolith")
		end

-- portal testing
		obj = sim:story_object(8008)
		if obj then	
		--	level.map_add_object_spot(obj.id, "level_changer", "to_garbage")
		end
		obj = sim:story_object(8009)
		if obj then
		--	level.map_add_object_spot(obj.id, "level_changer", "to_escape")
		end

	end
end


function set_task_state(id_state, id_task, id_subtask)
	if db.actor then
		db.actor:set_task_state(id_state, id_task, id_subtask)
	end
end

function notify_new_task()
end
