function p.__new_game()
    --Setting camera mode.
    game_body.set_default_fly_mode()

    --Maybe need to procedurally create the starting position.
    local game_desc = ga_get_sys_s("game.package.config_desc")
    if( game_desc == "Genesis" or
        game_desc == "Classic" )
    then
        ga_set_s("xar.create_start_pos.state", "not_started")
    end

    --Only adding extra builtin waypoints
    --if using "Cheater Hub" configuration.
    if( ga_get_sys_s("game.package.config_desc") == "Cheater Hub" ) then
        game_cheater_waypoints.add()
    end

    --Setting curve mode on by default.
    --The reason it is not set this way in globals.txt
    --is because I don't want to modify legacy saved games.
    ga_set_b("xar.difficulty.curve.enabled", true)

    --Initializing package vars, if they have not been already.
    game_package_init.init()
end

function p.__load_game()
    ga_print("top.__load_game begin")

    --Setting dependent variables.
    game_var_helpers.set_dep_vars()

    --Adding windows to the hud.
    --Lower numbers are rendered first.
    ga_hud_window_add("win_hud_user_below", 50)
    ga_hud_window_add("win_hud", 100) --Do not change.
    ga_hud_window_add("win_hud_user_above", 150)

    --more?

    ga_print("top.__load_game end")
end

function p.__reboot_game()
    ga_print("Here in top.__reboot_game")

    --Clearing certain env vars.
    ga_set_f("xar.emp_end_time", 0.0)

    local countdown = 10000 --10 thousand.
    while true do
        if ga_reboot_dyn_itr_at_end() then
            --Done the rebooting process.
            return true
        end

        countdown = countdown - 1
        if( countdown <= 0 ) then
            --Too many vars to do now,
            --must do them later.
            return false
        end

        local var = ga_reboot_dyn_itr_get()

        --Put code here to save specific dynamic variables.

        --Only saving the var called
        --"dyn.test.my_favorite_book".
        -- if var == "dyn.test.my_favorite_book" then
        --     ga_reboot_dyn_itr_save()
        -- end

        ga_reboot_dyn_itr_next()
    end

    --Will never reach here.
    return true
end

--Called during a regular (non discrete) update.
--This is called once per frame.
function p.__update()
    -- ga_print("top.update")
    game_update.perform_update()
end

--Gets called when the game is paused,
--such as when in a menu.
function p.__update_passive()
    game_update.perform_update_passive()
end

--Gets called first during a (discrete) update cycle.
--The the engine performs its own discrete update.
--Then the update_discrete_post function gets called
--There are 25 discrete updates per second.
function p.__update_discrete_pre()
    -- ga_print("top.update_discrete_pre")
    game_update.perform_update_discrete_pre()
end

function p.__update_discrete_post()
    -- ga_print("top.update_discrete_post")
    game_update.perform_update_discrete_post()
end

--Note: In the past this function could crash by either calling
--ga_get_viewer_chunk_id or ga_get_viewer_level.
--This was because the mtree was sometimes loading locked.
function p.__game_input(str)
    -- ga_print("top.game_input: " .. str)
    
    if not ga_get_sys_b("game.player.alive") then return "" end

    --New code.
    local split = game_str.split_into_head_and_tail(str)
    local cmd_name = split.head
    local cmd_options = split.tail
    if( game_command_system.command_exists(cmd_name) ) then
        -- ga_print("About to call xar command system")
        local result_str = game_command_system.run_command(cmd_name, cmd_options)
        -- ga_print("Returning string from top.game_input: " .. result_str)
        return result_str
    end

    --Engine defined commands.
    if( str == "__game_saved__" ) then
        if( ga_get_b("xar.hud.msg.show_saved_game_msg") ) then
            game_msg.add("Game saved")
        end
        return ""
    end
    if( cmd_name == "^__spiral_of_death__" ) then
        if( ga_get_b("xar.hud.msg.show_spiral_of_death_msg") ) then
            --num = string.gsub(str, "^__spiral_of_death__ ", "")
            --game_msg.add("*** Danger: spiral of death: " .. num)
            --game_msg.add("SOD: " .. num) --Put this back in!
        end
        return ""
    end
    if( cmd_name == "__screenshot__" ) then
        game_msg.add("Screenshot saved")
        return ""
    end
    if( cmd_name == "__screenshot_failed__" ) then
        game_msg.add("*** Error: the Inner Core is too shy to have a screenshot taken")
        return ""
    end
    if( cmd_name == "__cmd__" ) then
        local mod = "Command: " .. cmd_options
        game_msg.add(mod)
        return ""
    end

    -- ga_console_print("^1Error: unknown string passed to game_input: ")
    -- ga_console_print(str)
    return "error"
end

function p.__game_input_get_all_cmds()
    return game_command_system.get_commands_in_single_str()
end

-- function p.__game_input_cmd_exists(cmd)
--     ga_print("top.game_input_cmd_exists")
--     --MORE!!!
-- end

function p.__game_input_get_help_str(cmd_name)
    return game_command_system.get_help(cmd_name)
end

-- function p.screen_inv_selected()
--     return ga_get_i("xar.player.cur_wep")
-- end

function p.__killed_player()
    ga_print("here in top.__killed_player")
    ga_play_sound_menu("player_death")
end

function p.__respawn_player(param)
    local keep_ammo = false
    if( param == "keepammo" ) then keep_ammo = true end

    ga_print("top.__respawn_player begin")
    --

    if( ga_get_harcore_mode() ) then
        ga_set_sys_b("game.player.alive", false)
        return
    end

    ga_hud_window_remove("win_death")

    game_damage.clear_damage()

    local fixed_cost = 30

    if( keep_ammo ) then
        local respawn_cost = math.min(
            fixed_cost,
            game_respawn.get_keepammo_cost())
        local old_gold = ga_get_i("xar.player.gold.amount")
        local new_gold = old_gold - respawn_cost
        if new_gold < 0 then new_gold = 0 end
        ga_set_i("xar.player.gold.amount", new_gold)
    else
        ga_set_i("xar.player.health.amount",
            ga_get_i("xar.player.health.max"))
        ga_set_i("xar.player.shield.amount", 0)
        ga_set_i("xar.player.armor.amount", 0)
        game_remove_ammo.remove_ammo()
        ga_set_i("xar.player.gun1.ammo",
            game_wep_modes.get_ammo_max(1))
        ga_set_i("xar.player.gun0.ammo",
            game_wep_modes.get_ammo_max(0))
        --
        std.set_bydelta_i("xar.player.gold.amount", -fixed_cost)
        if ga_get_i("xar.player.gold.amount") < 0 then
            ga_set_i("xar.player.gold.amount", 0)
        end
    end
    ga_set_f("xar.key_time.yellow", -1.0)
    ga_set_f("xar.key_time.blue", -1.0)
    ga_set_f("xar.key_time.green", -1.0)
    ga_set_f("xar.key_time.universe", -1.0)

    ga_set_i("xar.player.markers.num_short", 0)

    ga_set_b("xar.first_life", false)

    ga_print("top.__respawn_player end")
    ga_flush()
end
