--Helper.
function p.txt_col(wid, col)
    ga_win_set_front_color(wid, col)
end

function p.interp_col(col1, col2, frac)
    local col = {x=0, y=0, z=0}
    col.x = (1.0 - frac)*col1.x + frac*col2.x
    col.y = (1.0 - frac)*col1.y + frac*col2.y
    col.z = (1.0 - frac)*col1.z + frac*col2.z
    return col
end

function p.render(wid)
    if( ga_get_b("xar.hud.disable_hud") ) then return end

    if( ga_get_b("xar.cheat.inf2.enabled") ) then
        --Infinifrag2 easter egg.
        game_inf2.hud_render(wid)
        return
    end

    ga_win_set_char_size(wid, 0.012, 0.024)

    local col_white  = std.vec(1.0, 1.0, 1.0)
    local col_red    = std.vec(1.0, 0.0, 0.0)
    local col_yellow = std.vec(1.0, 1.0, 0.0)
    local col_green  = std.vec(0.0, 1.0, 0.0)
    local col_blue   = std.vec(0.0, 0.25, 1.0)
    local col_cyan   = std.vec(0.0, 1.0, 1.0)

    --Dark hole charge coloring screen.
    local dh_charge = ga_get_i("xar.player.gun0.charge")
    if( dh_charge > 0 ) then
        if( dh_charge == 2 ) then ga_win_set_background(wid, std.vec(1.0, 0.0, 0.0), 0.1) end
        if( dh_charge == 3 ) then ga_win_set_background(wid, std.vec(1.0, 0.0, 0.0), 0.2) end
        if( dh_charge == 4 ) then ga_win_set_background(wid, std.vec(1.0, 0.0, 0.0), 0.3) end
        if( dh_charge == 5 ) then ga_win_set_background(wid, std.vec(1.0, 0.0, 0.0), 0.4) end
        if( dh_charge == 6 ) then ga_win_set_background(wid, std.vec(1.0, 0.0, 0.0), 0.5) end
        if( dh_charge == 7 ) then ga_win_set_background(wid, std.vec(1.0, 0.0, 0.0), 0.6) end
        if( dh_charge == 8 ) then ga_win_set_background(wid, std.vec(1.0, 0.0, 0.0), 0.7) end
        if( dh_charge == 9 ) then ga_win_set_background(wid, std.vec(1.0, 0.0, 0.0), 0.8) end
        if( dh_charge == 10 ) then ga_win_set_background(wid, std.vec(1.0, 0.0, 0.0), 0.9) end
        if( dh_charge == 11 ) then ga_win_set_background(wid, std.vec(1.0, 0.0, 0.0), 0.95) end
        if( dh_charge == 12 ) then ga_win_set_background(wid, std.vec(1.0, 0.0, 0.0), 0.98) end
        if( dh_charge == 13 ) then ga_win_set_background(wid, std.vec(1.0, 0.0, 0.0), 1.0) end
    end

    --Red boarder if have low shield/health/armor.
    local shield     = ga_get_i("xar.player.shield.amount")
    local shield_max = ga_get_i("xar.player.shield.max")
    local shield_frac = shield / shield_max
    local shield_danger_cutoff = ga_get_f("xar.hud.shield_danger_cutoff")
    local health = ga_get_i("xar.player.health.amount")
    local health_max = ga_get_i("xar.player.health.max")
    local health_frac = health / health_max
    local health_danger_cutoff = ga_get_f("xar.hud.health_danger_cutoff")
    local armor = ga_get_i("xar.player.armor.amount")
    local armor_max = ga_get_i("xar.player.armor.max")
    local armor_frac = armor / armor_max
    local armor_danger_cutoff = ga_get_f("xar.hud.armor_danger_cutoff")
    --
    local cur_showing_border = ga_get_b("xar.hud.showing_border")
    local next_showing_boarder =
      ( shield_frac < shield_danger_cutoff and
        health_frac < health_danger_cutoff and
        armor_frac < armor_danger_cutoff )
    if cur_showing_border ~= next_showing_boarder then
        --Attempting to make a change, but need to worry about epilepsy.
        if( game_flash.safe_for_flash_and_reg() ) then
            --It is epilepsy safe to make a change.
            ga_set_b("xar.hud.showing_border", next_showing_boarder)
            --Will take effect next cycle.
        end
    end
    if( cur_showing_border ) then
        --Making a red boarder around the screen.
        local red = std.vec(1.0, 0.0, 0.0)
        local alpha = 0.7
        local w_thick = 0.03
        local h_thick = 0.05
        ga_win_quad_color_alpha(wid, 0.0, 0.0, 1.0, h_thick, red, alpha)
        ga_win_quad_color_alpha(wid, 0.0, 1.0-h_thick, 1.0, 1.0, red, alpha)
        ga_win_quad_color_alpha(wid, 0.0, h_thick, w_thick, 1.0-h_thick, red, alpha)
        ga_win_quad_color_alpha(wid, 1.0-w_thick, h_thick, 1.0, 1.0-h_thick, red, alpha)
    end

    -- Compass (top middle of the screen).
    local camera_look = ga_get_sys_v("game.player.camera.look")
    local dir_str = ""
    local cutoff = 3.1415926 / 8.0
    if( std.angle_between_vec(camera_look, std.vec(1.0, 0.0, 0.0)) < cutoff ) then
        dir_str = "RIGHT"
    end
    if( std.angle_between_vec(camera_look, std.vec(-1.0, 0.0, 0.0)) < cutoff ) then
        dir_str = "LEFT"
    end
    if( std.angle_between_vec(camera_look, std.vec(0.0, 1.0, 0.0)) < cutoff ) then
        dir_str = "FRONT"
    end
    if( std.angle_between_vec(camera_look, std.vec(0.0, -1.0, 0.0)) < cutoff ) then
        dir_str = "BACK"
    end
    if( std.angle_between_vec(camera_look, std.vec(0.0, 0.0, 1.0)) < cutoff ) then
        dir_str = "UP"
    end
    if( std.angle_between_vec(camera_look, std.vec(0.0, 0.0, -1.0)) < cutoff ) then
        dir_str = "DOWN"
    end
    ga_win_set_front_color(wid, std.vec(0.0, 1.0, 0.0))
    ga_win_set_char_size(wid, 0.02, 0.03)
    ga_win_txt_center(wid, 0.96, dir_str)

    -- DPS (top middle of the screen).
    local in_dps = ga_get_sys_i("stats.last_in_dps")
    if( in_dps > 0 ) then
        local dps_str = "DPS: " .. game_str.add_commas(in_dps)
        ga_win_set_front_color(wid, std.vec(0.0, 1.0, 0.0))
        ga_win_set_char_size(wid, 0.02, 0.03)
        ga_win_txt_center(wid, 0.90, dps_str)
    end
    local max_health = ga_get_i("xar.player.health.max")
    local f = in_dps / max_health
    local icon_height = 0.05
    --icon_width is 0.04.
    local y_min = 0.84
    if( f >= 0.25 and f < 0.5 ) then
        ga_win_quad(wid, 0.48, y_min, 0.52, y_min+icon_height, "icon_skull")
    elseif( f >= 0.5 and f < 1.0 ) then
        ga_win_quad(wid, 0.45, y_min, 0.49, y_min+icon_height, "icon_skull")
        ga_win_quad(wid, 0.51, y_min, 0.55, y_min+icon_height, "icon_skull")
    elseif( f >= 1.0 and f < 2.0 ) then
        ga_win_quad(wid, 0.42, y_min, 0.46, y_min+icon_height, "icon_skull")
        ga_win_quad(wid, 0.48, y_min, 0.52, y_min+icon_height, "icon_skull")
        ga_win_quad(wid, 0.54, y_min, 0.58, y_min+icon_height, "icon_skull")
    elseif( f >= 2.0 ) then
        ga_win_quad(wid, 0.39, y_min, 0.43, y_min+icon_height, "icon_skull")
        ga_win_quad(wid, 0.45, y_min, 0.49, y_min+icon_height, "icon_skull")
        ga_win_quad(wid, 0.51, y_min, 0.55, y_min+icon_height, "icon_skull")
        ga_win_quad(wid, 0.57, y_min, 0.61, y_min+icon_height, "icon_skull")
    end

    --Identified MEnt.
    if( ga_exists("temp.identify_ment") ) then
        local identified_ment_inst_id = ga_get_i("temp.identify_ment")
        if( identified_ment_inst_id >= 0 and
            ga_ment_exists(identified_ment_inst_id) )
        then
            local type = ga_ment_get_type(identified_ment_inst_id)
            if( type ~= "MENT_NOT_FOUND" ) then
                local has_health = game_ment_identify.has_health()
                local txt_color = std.vec(1.0, 1.0, 1.0)
                local back_color1 = std.vec(0.9, 0.0, 0.0)
                local back_color2 = std.vec(0.0, 0.0, 0.0)
                if( not has_health ) then
                    back_color1 = std.vec(0.0, 0.0, 1.0)
                    back_color2 = std.vec(0.0, 0.0, 1.0)
                end

                local base_str = game_ment_identify.get_str()
                local health = game_ment_identify.get_health()
                local max_health = game_ment_identify.get_max_health()
                if( health < 0 ) then health = 0 end
                if( health > max_health ) then health = max_health end
                local health_str = game_str.make_3_digit_big(health)
                local id_str = base_str .. ": " .. health_str
                if( not has_health or
                    ga_ment_static_b_exists_and_true(type, "identity_str_no_health") )
                then
                    id_str = base_str
                    back_color1 = std.vec(0.0, 0.0, 1.0)
                    back_color2 = back_color1
                end
                local id_min_x = ga_win_get_center_text_min_x(0.02, string.len(id_str))
                local id_max_x = 1.0 - id_min_x
                local eps = 0.005
                local frac = health / max_health
                if( max_health == 0 ) then frac = 0 end
                local mid = (1.0 - frac) * (id_min_x-eps) + frac * (id_max_x+eps)
                --
                local min_y = 0.84
                ga_win_quad_color(
                    wid,
                    id_min_x - eps,     --Min X
                    min_y - eps,        --Min Y
                    id_max_x + eps,     --Max X
                    min_y + 0.03 + eps, --Max Y
                    back_color2)
                ga_win_quad_color(
                    wid,
                    id_min_x - eps,      --Min X
                    min_y - eps,         --Min Y
                    mid,                 --Max X
                    min_y + 0.03 + eps,  --Max Y
                    back_color1)
                ga_win_set_front_color(wid, std.vec(1.0, 1.0, 1.0))
                ga_win_txt_center(wid, min_y, id_str)
            end
        end
    end

    ga_win_set_char_size(wid, 0.012, 0.024)

    local ammo_y_min = 0.01
    local ammo_width = 0.059
    local ammo_height = 0.086

    --Hive attack meter.
    local x_min = 0.002
    local hive_frac = game_ment_hive_attack.get_frac()
    ga_win_quad_two(wid,
        x_min, ammo_y_min,
        x_min + 0.01, ammo_y_min + ammo_height,
        "icon_hive_attack_bar", "icon_hive_attack_bar_back", hive_frac)

    -- Shield.
    local x_min = 0.015
    local shield     = ga_get_i("xar.player.shield.amount")
    local shield_max = ga_get_i("xar.player.shield.max")
    local frac = shield / shield_max
    if ga_get_b("xar.hud.show_health") and (not ga_genesis()) then
        ga_win_quad_two(wid, 
            x_min,
            ammo_y_min,
            x_min + ammo_width,
            ammo_y_min + ammo_height,
            "icon_shield", "icon_shield_grey", frac)
    end
    local col = std.vec(0.0, 0.0, 0.0)
    if     (shield < shield_max/4) then col = col_red
    elseif (shield < shield_max/2) then col = col_yellow
    else                                col = col_green
    end
    if ga_get_b("xar.hud.show_health") and (not ga_genesis()) then
        p.txt_col(wid, col)
        local short_str = game_str.make_3_digit_big(shield)
        ga_win_txt_center_at_bg(wid, 
            x_min + 0.5 * ammo_width,
            ammo_y_min + ammo_height,
            short_str)
    end

    -- Health.
    x_min = 0.075
    local health = ga_get_i("xar.player.health.amount")
    local health_max = ga_get_i("xar.player.health.max")
    local value_1 = 0
    local value_2 = health_max
    local value_3 = health_max * 2
    local value_4 = health_max * 5
    local tex1 = ""
    local tex2 = ""
    if( health < value_2 ) then
        frac = (health - value_1) / (value_2 - value_1)
        tex1 = "icon_health"
        tex2 = "icon_health_red"
        if (health <= value_1) then frac = 0.0 end
    elseif( health <= value_3 ) then
        frac = (health - value_2) / (value_3 - value_2)
        tex1 = "icon_health_blue"
        tex2 = "icon_health"
    else
        frac = (health - value_3) / (value_4 - value_3)
        tex1 = "icon_health_dark_blue"
        tex2 = "icon_health_blue"
    end
    if ga_get_b("xar.hud.show_health") and (not ga_genesis()) then
        ga_win_quad_two(wid, 
            x_min,
            ammo_y_min,
            x_min + ammo_width,
            ammo_y_min + ammo_height,
            tex1, tex2, frac)
    end
    if     (health <= health_max/4) then col = col_red
    elseif (health <= health_max/2) then col = col_yellow
    elseif (health <= health_max  ) then col = col_green
    elseif (health <= health_max*2) then col = col_blue
    else                                 col = col_cyan
    end
    if ga_get_b("xar.hud.show_health") and (not ga_genesis()) then
        p.txt_col(wid, col)
        local short_str = game_str.make_3_digit_big(health)
        ga_win_txt_center_at_bg(wid, 
            x_min + 0.5 * ammo_width,
            ammo_y_min + ammo_height,
            short_str)
    end

    -- Armor.
    x_min = 0.135
    local armor = ga_get_i("xar.player.armor.amount")
    local armor_max = ga_get_i("xar.player.armor.max")
    local value_1 = 0
    local value_2 = armor_max
    local value_3 = armor_max * 2
    local value_4 = armor_max * 10
    if( armor < value_2 ) then
        frac = (armor - value_1) / (value_2 - value_1)
        tex1 = "icon_armor"
        tex2 = "icon_armor_grey"
        if (armor <= value_1) then frac = 0.0 end
    elseif( armor <= value_3 ) then
        frac = (armor - value_2) / (value_3 - value_2)
        tex1 = "icon_armor_two"
        tex2 = "icon_armor"
    else
        frac = (armor - value_3) / (value_4 - value_3)
        tex1 = "icon_armor_three"
        tex2 = "icon_armor_two"
    end
    if ga_get_b("xar.hud.show_health") and (not ga_genesis()) then
        ga_win_quad_two(wid, 
            x_min,
            ammo_y_min,
            x_min + ammo_width,
            ammo_y_min + ammo_height,
            tex1, tex2, frac)
    end
    if     (armor <= armor_max/4) then col = col_red
    elseif (armor <= armor_max/2) then col = col_yellow
    elseif (armor <= armor_max  ) then col = col_green
    elseif (armor <= armor_max*2) then col = col_blue
    else                               col = col_cyan
    end
    if ga_get_b("xar.hud.show_health") and (not ga_genesis()) then
        p.txt_col(wid, col)
        local short_str = game_str.make_3_digit_big(armor)
        ga_win_txt_center_at_bg(wid, 
            x_min + 0.5 * ammo_width,
            ammo_y_min + ammo_height,
            short_str)
    end

    -- Gold.
    x_min = 0.94
    local gold = ga_get_i("xar.player.gold.amount");
    if ga_get_b("xar.hud.show_gold") then
        ga_win_quad(wid, 
            x_min,
            ammo_y_min,
            x_min + ammo_width,
            ammo_y_min + ammo_height,
            "icon_gold");
        if     (gold < 100)  then col = col_red
        elseif (gold < 1000) then col = col_yellow
        else                      col = col_green
        end
        p.txt_col(wid, col)
        -- display_gold = gold
        -- if( gold > 99999 ) then display_gold = 99999 end
        local short_str = game_str.make_3_digit_big(gold)
        ga_win_txt_center_at_bg(wid, 
            x_min + 0.5 * ammo_width,
            ammo_y_min + ammo_height,
            short_str)
    end

    -- Markers.
    x_min = 0.88
    local markers = ga_get_i("xar.player.markers.num_short");
    if ga_get_b("xar.hud.show_markers") and (not ga_genesis()) then
        ga_win_quad(wid, 
            x_min,
            ammo_y_min,
            x_min + ammo_width,
            ammo_y_min + ammo_height,
            "icon_markers");
    end
    if     (markers < 10)  then col = col_red
    elseif (markers < 100) then col = col_yellow
    else                       col = col_green
    end
    if ga_get_b("xar.hud.show_markers") and (not ga_genesis()) then
        p.txt_col(wid, col)
        local short_str = game_str.make_3_digit_big(markers)
        ga_win_txt_center_at_bg(wid, 
            x_min + 0.5 * ammo_width,
            ammo_y_min + ammo_height,
            short_str)
    end

    -- Ammo.
    for raw_num = 1,10 do
        local num = raw_num
        if (num == 10) then num = 0 end

        local ammo_x_delta = 0.065
        local x_min = 0.21 + ammo_x_delta * (raw_num-1)
        local x_max = x_min + ammo_width
        local y_min = ammo_y_min
        local y_max = y_min + ammo_height

        local cur_wep = ga_get_i("xar.player.cur_wep")
        if ( (raw_num%10) == cur_wep) then
            local add = 0.02
            y_min = y_min + add
            y_max = y_max + add
        end

        local ammo_var = "xar.player.gun" .. num .. ".ammo"
        local ammo     = ga_get_i(ammo_var)
        local ammo_max = game_wep_modes.get_ammo_max(num)

        --GET RID OF SLASHES!!!
        tex1 = "ammo_gun" .. num 
        tex2 = tex1 .. "_grey"
        frac = (ammo / ammo_max)
        if ga_get_b("xar.hud.show_ammo") and (not ga_genesis()) then
            ga_win_quad_two(wid, x_min, y_min, x_max, y_max,
                tex1, tex2, frac)
        end

        if     (ammo < ammo_max/4) then col = col_red
        elseif (ammo < ammo_max/2) then col = col_yellow
        else                            col = col_green
        end
        if ga_get_b("xar.hud.show_ammo") and (not ga_genesis()) then
            p.txt_col(wid, col)
            local short_str = game_str.make_3_digit_big(ammo)
            ga_win_txt_center_at_bg(wid, 
                x_min + 0.5 * ammo_width,
                y_max, short_str)
        end
    end

    --Experience bar.
    local xp1 = ga_get_i("xar.experience.to_next_level")
    local xp2 = ga_get_i("xar.experience.to_next_level_max")
    local xp_frac = 1.0 - (xp1 / xp2)
    if ga_get_b("xar.hud.show_experience") and (not ga_genesis()) then
        ga_win_quad_two(wid, 
            0.97, --x_min
            0.16, --y_min
            0.99, --x_max
            0.5,  --y_max
            "icon_xp", "icon_xp_back", xp_frac)
    end
    --Displaying the level.
    local player_level = ga_get_i("xar.experience.level")
    if ga_get_b("xar.hud.show_experience") and (not ga_genesis()) then
        local xp_level_center_x = 0.98
        if( player_level >= 1000 ) then xp_level_center_x = 0.97 end
        p.txt_col(wid, col_green)
        ga_win_txt_center_at_bg(wid, 
            xp_level_center_x, --x center
            0.51, --y min
            tostring(player_level))
    end
    --Asking player to press F4 if needed.
    local last_milestone_level = player_level - (player_level % 10)
    local last_claimed_milestone = ga_get_i("xar.experience.last_claimed_milestone_level")
    if( last_claimed_milestone < last_milestone_level ) then
        local game_time = ga_get_game_time()
        local rem = math.floor(game_time) % 2
        if( rem == 0 ) then
            ga_win_txt_center_at_bg(wid, 
                0.945, --center x
                0.54, --y min
                "PRESS F4")
        end
    end
    -- XP from last second.
    --USE A HELPER FUNCTION FOR THIS DIGIT STUFF!!!
    local xp_last_sec = ga_get_i("xar.experience.last_second")
    local num_digits = 1
    if( xp_last_sec < 10 ) then num_digits = 1
    elseif( xp_last_sec < 100 ) then num_digits = 2
    elseif( xp_last_sec < 1000 ) then num_digits = 3
    elseif( xp_last_sec < 10000 ) then num_digits = 4
    elseif( xp_last_sec < 100000 ) then num_digits = 5
    elseif( xp_last_sec < 1000000 ) then num_digits = 6
    elseif( xp_last_sec < 10000000 ) then num_digits = 7
    elseif( xp_last_sec < 100000000 ) then num_digits = 8 end
    local xp_x = 0.92
    if( num_digits == 1 ) then xp_x = 0.98 end
    if( num_digits == 2 ) then xp_x = 0.98 end
    if( num_digits == 3 ) then xp_x = 0.975 end
    if( num_digits == 4 ) then xp_x = 0.97 end
    if( num_digits == 5 ) then xp_x = 0.965 end
    if( num_digits == 6 ) then xp_x = 0.96 end
    if( num_digits == 7 ) then xp_x = 0.955 end
    if( num_digits == 8 ) then xp_x = 0.95 end
    p.txt_col(wid, col_green)
    if ga_get_b("xar.hud.show_experience") and (not ga_genesis()) then
        local short_str = game_str.make_3_digit_big(xp_last_sec)
        ga_win_txt_center_at_bg(wid, xp_x, 0.13, tostring(short_str))
        -- ga_win_txt_center_at_bg(wid, xp_x, 0.13, tostring(xp_last_sec))
    end

    if ga_get_b("xar.hud.show_icons") then
        p.render_icons(wid)
    end

    --Lower left message.
    if( not ga_get_b("xar.hud.msg.disable_msgs") ) then
        ga_win_set_char_size(wid, 0.015, 0.03)
        p.txt_col(wid, col_green)
        for i = 1,5 do
            local msg = ""
            local ttl = 0.0
            if( ga_get_sys_b("game.input.quick_cmd.entering") ) then
                if i == 1 then
                    msg = "/" .. ga_get_sys_s("game.input.quick_cmd.partial")
                    ttl = 10.0 --Ok.
                else
                    local prefix = "xar.messages.slot" .. tostring(i-1)
                    msg = ga_get_s(prefix .. ".msg")
                    ttl = ga_get_f(prefix .. ".ttl")
                end
            else
                local prefix = "xar.messages.slot" .. tostring(i)
                msg = ga_get_s(prefix .. ".msg")
                ttl = ga_get_f(prefix .. ".ttl")
            end
            --
            local col = col_white
            if ttl < 4.0 then col = col_green end
            if( ttl > 4.0 and ttl < 5.0 ) then
                col = p.interp_col(col_green,col_white,ttl-4.0)
            end
            p.txt_col(wid, col)
            --
            local min_x = 0.01
            local min_y = 0.25 + 0.033*(i-1)
            local alpha_fade = 1.0
            if( ttl < 1.0 and ttl >= 0.0 ) then alpha_fade = ttl end
            if ttl > 0.0 then
                local color = std.vec(0.0,0.0,0.0)
                local back_alpha = 0.4 * alpha_fade
                local front_alpha = 1.0 * alpha_fade
                ga_win_set_back_params(wid, color, back_alpha, back_alpha)
                ga_win_txt_alpha_bg(wid, min_x, min_y, front_alpha, msg)
            end
        end
    end

    --more!!!
end

function p.render_icons(wid)
    -- Icons.
    local icon_bot = 0.13
    local icon_top = icon_bot + 0.08
    local icon_width = 0.05

    local step_x = icon_width + 0.01
    local cur_x = 0.01

    local col_green  = std.vec(0.0, 1.0, 0.0)

    if ga_get_sys_b("game.state.saving_in_progress") then
        local tex = "icon_save"
        ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
        cur_x = cur_x + step_x
    end

    --Hardcore modifier.
    if ga_get_b("xar.difficulty.hardcore") then
        local tex = "icon_hardcore"
        ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
        cur_x = cur_x + step_x
    end

    --Game difficulty new way).
    local diff_num = game_difficulty.num()
    local e_diff_num = game_difficulty.easiest_num()
    if( diff_num == e_diff_num ) then
        --Only need to show the current difficult.
        local icon_name = game_difficulty.icon(diff_num)
        if( icon_name ~= "" ) then
            if not ga_genesis() then
                ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, icon_name)
                cur_x = cur_x + step_x
            end
        end
    else
        --Need to show an icon from the easiest difficulty (in grey)
        --and the current difficulty.
        local e_icon_name = game_difficulty.icon_once(e_diff_num)
        ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, e_icon_name)
        cur_x = cur_x + step_x
        --
        local icon_name = game_difficulty.icon(diff_num)
        if( icon_name ~= "" ) then
            if not ga_genesis() then
                ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, icon_name)
                cur_x = cur_x + step_x
            end
        end
    end

    if ga_get_b("xar.movement.rocket") and (not ga_genesis() ) then
        local tex = "icon_rocket"
        ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
        cur_x = cur_x + step_x
    end

    if ga_get_b("xar.movement.brake") then
        local tex = "icon_brake"
        ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
        cur_x = cur_x + step_x
    end    

    if ga_exists("xar.key_time.yellow") then
        local key_time = ga_get_f("xar.key_time.yellow")
        if (key_time > 0.0) then
            local tex = "key_yellow"
            ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
            p.txt_col(wid, col_green)
            ga_win_txt_center_at_bg(wid, 
                cur_x + 0.5*icon_width,
                icon_top,
                math.floor(key_time) )
            cur_x = cur_x + step_x
        end
    end
    
    if ga_exists("xar.key_time.blue") then
        local key_time = ga_get_f("xar.key_time.blue")
        if (key_time > 0.0) then
            local tex = "key_blue"
            ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
            p.txt_col(wid, col_green)
            ga_win_txt_center_at_bg(wid, 
                cur_x + 0.5*icon_width,
                icon_top,
                math.floor(key_time) )
            cur_x = cur_x + step_x
        end
    end

    if ga_exists("xar.key_time.green") then
        local key_time = ga_get_f("xar.key_time.green")
        if (key_time > 0.0) then
            local tex = "key_green"
            ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
            p.txt_col(wid, col_green)
            ga_win_txt_center_at_bg(wid, 
                cur_x + 0.5*icon_width,
                icon_top,
                math.floor(key_time) )
            cur_x = cur_x + step_x
        end
    end

    if ga_exists("xar.key_time.universe") then
        local key_time = ga_get_f("xar.key_time.universe")
        if (key_time > 0.0) then
            local tex = "ent_credits"
            ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
            p.txt_col(wid, col_green)
            ga_win_txt_center_at_bg(wid, 
                cur_x + 0.5*icon_width,
                icon_top,
                math.floor(key_time) )
            cur_x = cur_x + step_x
        end
    end

    if ga_exists("xar.key_time.laser_genesis") then
        local key_time = ga_get_f("xar.key_time.laser_genesis")
        if (key_time > 0.0) then
            local tex = "key_laser_genesis"
            ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
            p.txt_col(wid, col_green)
            ga_win_txt_center_at_bg(wid, 
                cur_x + 0.5*icon_width,
                icon_top,
                math.floor(key_time) )
            cur_x = cur_x + step_x
        end
    end

    if ga_exists("xar.key_time.dans_house") then
        local key_time = ga_get_f("xar.key_time.dans_house")
        if (key_time > 0.0) then
            local tex = "key_dans_house"
            ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
            p.txt_col(wid, col_green)
            ga_win_txt_center_at_bg(wid, 
                cur_x + 0.5*icon_width,
                icon_top,
                math.floor(key_time) )
            cur_x = cur_x + step_x
        end
    end

    if ga_exists("xar.xp_5x_stacking_time") then
        local xp_time = ga_get_f("xar.xp_5x_stacking_time")
        if (xp_time > 0.0) then
            local tex = "icon_xp_5x"
            ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
            p.txt_col(wid, col_green)
            ga_win_txt_center_at_bg(wid, 
                cur_x + 0.5*icon_width,
                icon_top,
                math.floor(xp_time) )
            cur_x = cur_x + step_x
        end
    end

    if ga_exists("xar.damage_5x_stacking_time") then
        local xp_time = ga_get_f("xar.damage_5x_stacking_time")
        if (xp_time > 0.0) then
            local tex = "icon_damage_5x"
            ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
            p.txt_col(wid, col_green)
            ga_win_txt_center_at_bg(wid, 
                cur_x + 0.5*icon_width,
                icon_top,
                math.floor(xp_time) )
            cur_x = cur_x + step_x
        end
    end

    local invun_time = ga_get_f("xar.invun_stacking_time")
    if( invun_time > 0.0 ) then
        local tex = "icon_invun"
        ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
        p.txt_col(wid, col_green)
        ga_win_txt_center_at_bg(wid, 
            cur_x + 0.5*icon_width,
            icon_top,
            math.floor(invun_time) )
        cur_x = cur_x + step_x
    end

    --Tracking marker (beacon).
    game_tracking_marker.refresh_attack_display()
    if game_tracking_marker.exists_and_enabled() then
        local tex = "icon_beacon"
        ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
        p.txt_col(wid, col_green)
        local dist = game_tracking_marker.get_dist()
        local dist_str = game_str.make_3_digit_big(dist)
        ga_win_txt_center_at_bg(wid, 
            cur_x + 0.5*icon_width,
            icon_top,
            dist_str )
        cur_x = cur_x + step_x
    end

    if ga_exists("xar.emp_end_time") then
        local emp_end_time = ga_get_f("xar.emp_end_time")
        local game_time = ga_get_game_time()
        if( emp_end_time > game_time ) then
            local diff = math.floor(emp_end_time - game_time)
            local tex = "ammo_gun8"
            ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
            local emp_text_col = game_ment_emp.get_hud_emp_icon_text_color()
            p.txt_col(wid, emp_text_col)
            ga_win_txt_center_at_bg(wid,
                cur_x + 0.5*icon_width,
                icon_top,
                diff)
            cur_x = cur_x + step_x
        end
    end

    if ga_get_b("xar.player.arcade.inside") then
        local tex = "icon_arcade"
        ga_win_quad(wid, cur_x, icon_bot, cur_x+icon_width, icon_top, tex)
        cur_x = cur_x + step_x
    end

    --Flash of light.
    local flash_time_1 = ga_get_f("xar.hud.flash.last_time")
    local flash_time_2 = flash_time_1 + ga_get_f("xar.hud.flash.duration")
    local game_time = ga_get_game_time()
    if( game_time >= flash_time_1 and
        game_time <= flash_time_2 )
    then
        local frac = (game_time - flash_time_1)
            / (flash_time_2 - flash_time_1)
        local start_alpha = ga_get_f("xar.hud.flash.alpha")
        local flash_alpha = (1.0 - frac) * start_alpha
        local flash_col = std.vec(1.0, 1.0, 1.0)
        ga_win_quad_color_alpha(wid, 0.0, 0.0, 1.0, 1.0, flash_col, flash_alpha)
    end
end
