--This is needed in case the user somehow
--goes far back in time.
function p.lower_cap_at_zero(var)
    local value = ga_get_f(var)
    if( value < 0.0 ) then
        ga_set_f(var, 0.0)
    end
end

function p.update()
    local game_time = ga_get_game_time()
    local elapsed_time = ga_get_sys_f("game.time.elapsed") --elapsed "game" time.

    --Saftey.
    if( elapsed_time < 0.0 ) then
        elapsed_time = 0.0
    end
    if( elapsed_time > 10.0 ) then
        elapsed_time = 10.0
    end

    --Capping gold at one million.
    local max_gold = 1000000;
    if( ga_get_i("xar.player.gold.amount") > max_gold ) then
        ga_set_i("xar.player.gold.amount", max_gold)
    end

    --XP from last second.
    local last_xp_update_time = ga_get_f("xar.experience.last_time") --Actual time.
    if game_time < last_xp_update_time - 10.0 then
        --For saftey.  (In case go back in time).
        last_xp_update_time = game_time
        ga_set_f("xar.experience.last_time", last_xp_update_time)
    end
    if game_time > last_xp_update_time + 1.0 then
        ga_set_f("xar.experience.last_time", game_time)
        local xp_this_second = ga_get_i("xar.experience.this_second")
        ga_set_i("xar.experience.last_second", xp_this_second)
        ga_set_i("xar.experience.this_second", 0)
    end

    --Messages.
    std.set_bydelta_f("xar.messages.slot1.ttl", -elapsed_time)
    std.set_bydelta_f("xar.messages.slot2.ttl", -elapsed_time)
    std.set_bydelta_f("xar.messages.slot3.ttl", -elapsed_time)
    std.set_bydelta_f("xar.messages.slot4.ttl", -elapsed_time)
    std.set_bydelta_f("xar.messages.slot5.ttl", -elapsed_time)

    --Shield.
    p.lower_cap_at_zero("xar.player.shield.counter") --Saftey.
    std.set_bydelta_f("xar.player.shield.counter", elapsed_time)
    local regen = ga_get_f("xar.player.health.regen") * 0.5 --So will regen twice as fast.
    while ga_get_f("xar.player.shield.counter") > regen do
        std.set_bydelta_f("xar.player.shield.counter", -regen)
        local current = ga_get_i("xar.player.shield.amount")
        if current < ga_get_i("xar.player.shield.max") then
            std.set_bydelta_i("xar.player.shield.amount", 1)
        end
    end

    --Regen health.
    p.lower_cap_at_zero("xar.player.health.counter") --Saftey.
    std.set_bydelta_f("xar.player.health.counter", elapsed_time)
    local health_max = ga_get_i("xar.player.health.max")
    if ga_get_i("xar.player.health.amount") >= health_max then
        --Regenerating to 200%.
        local regen = ga_get_f("xar.player.health.extra_regen")
        if regen > 0.0 then
            while ga_get_f("xar.player.health.counter") >= regen do
                if ga_get_i("xar.player.health.amount") < health_max*2 then
                    std.set_bydelta_i("xar.player.health.amount", 1)
                end
                std.set_bydelta_f("xar.player.health.counter", -regen)
            end
        end
    else
        --Regenerating to 100%.
        local regen = ga_get_f("xar.player.health.regen")
        while ga_get_f("xar.player.health.counter") >= regen do
            if ga_get_i("xar.player.health.amount") < health_max then
                std.set_bydelta_i("xar.player.health.amount", 1)
            end
            std.set_bydelta_f("xar.player.health.counter", -regen)
        end
    end

    --Looking health because too much.
    p.lower_cap_at_zero("xar.player.health.too_much_counter") --Saftey.
    std.set_bydelta_f("xar.player.health.too_much_counter", elapsed_time)
    local loss = 1.0
    while ga_get_f("xar.player.health.too_much_counter") >= loss do
        if ga_get_i("xar.player.health.amount") > health_max * 2 then
            std.set_bydelta_i("xar.player.health.amount", -1)
        end
        std.set_bydelta_f("xar.player.health.too_much_counter", -loss)
    end

    --Regen armor.
    p.lower_cap_at_zero("xar.player.armor.regen") --Saftey.
    local regen = ga_get_f("xar.player.armor.regen")
    if regen > 0.0 then
        std.set_bydelta_f("xar.player.armor.counter", elapsed_time)
        while ga_get_f("xar.player.armor.counter") >= regen do
            if ga_get_i("xar.player.armor.amount") < health_max * 2 then
                std.set_bydelta_i("xar.player.armor.amount", 1)
            end
            std.set_bydelta_f("xar.player.armor.counter", -regen)
        end
    end

    --Plasma regeneration.
    p.lower_cap_at_zero("xar.player.gun1.counter") --Saftey.
    std.set_bydelta_f("xar.player.gun1.counter", elapsed_time)
    local regen = game_wep_modes.get_ammo_regen_period(1)
    while ga_get_f("xar.player.gun1.counter") >= regen do
        if  ga_get_i("xar.player.gun1.ammo") <
            game_wep_modes.get_ammo_max(1)
        then
            std.set_bydelta_i("xar.player.gun1.ammo", 1)
        end
        std.set_bydelta_f("xar.player.gun1.counter", -regen)
    end

    --Keys.
    local key_time = ga_get_f("xar.key_time.yellow")
    if key_time > 0.0 then
        std.set_bydelta_f("xar.key_time.yellow", -elapsed_time)
    end
    local key_time = ga_get_f("xar.key_time.blue")
    if key_time > 0.0 then
        std.set_bydelta_f("xar.key_time.blue", -elapsed_time)
    end
    local key_time = ga_get_f("xar.key_time.green")
    if key_time > 0.0 then
        std.set_bydelta_f("xar.key_time.green", -elapsed_time)
    end
    local key_time = ga_get_f("xar.key_time.universe")
    if key_time > 0.0 then
        std.set_bydelta_f("xar.key_time.universe", -elapsed_time)
    end
    local key_time = ga_get_f("xar.key_time.laser_genesis")
    if key_time > 0.0 then
        std.set_bydelta_f("xar.key_time.laser_genesis", -elapsed_time)
    end
    local key_time = ga_get_f("xar.key_time.dans_house")
    if key_time > 0.0 then
        std.set_bydelta_f("xar.key_time.dans_house", -elapsed_time)
    end

    --XP modifier.
    local xp_time = ga_get_f("xar.xp_5x_stacking_time")
    if xp_time > 0.0 then
        std.set_bydelta_f("xar.xp_5x_stacking_time", -elapsed_time)
    end

    --Damage out modifier.
    local damage_time = ga_get_f("xar.damage_5x_stacking_time")
    if damage_time > 0.0 then
        std.set_bydelta_f("xar.damage_5x_stacking_time", -elapsed_time)
    end

    --Invun.
    local invun_time = ga_get_f("xar.invun_stacking_time")
    if invun_time > 0.0 then
       std.set_bydelta_f("xar.invun_stacking_time", -elapsed_time) 
    end
end
