#include "tools.lua"
#include "helpers/entity.lua"

disabled = nil

local custom_rules = {
    shotgun = {
        ammo = 24,
        range = 24,
        damage = 5,
    },
    blowtorch = {
        ammo = 30,
    },
    plank = {
        ammo = 16,
        width = 4,
        length = 56,
    },
    wire = {
        ammo = 24,
        stretch = 4,
    },
    gun = {
        ammo = 18,
        range = 60,
        damage = 2,
    },
    rifle = {
        ammo = 12,
    },
    pipebomb = {
        ammo = 12,
        damage = 3,
    },
    bomb = {
        ammo = 6,
        damage = 4,
    },
    rocket = {
        ammo = 12,
        damage = 3,
    },
    explosive = {
        ammo = 6,
        damage = 6,
    },
    booster = {
        ammo = 6,
        power = 200,
        time = 4,
    },
    leafblower = {
        power = 30,
    },
    turbo = {
        ammo = 6,
        power = 200,
    },
    steroid = {
        ammo = 4,
        time = 6,
    },
}

function tick(dt)
    if disabled then return end
    if GetBool("level.sandbox") then
        disabled = cEntity:EnqueueDeletion()
    else
        local ammoScale = 1
        local s = GetFloat("options.game.campaign.ammo")
        if s == -1 then
            ammoScale = 0
        else
            ammoScale = ammoScale + s/100
        end
        for key, info in pairs(gTools) do
            local is_enabled = GetBool("savegame.mod.tool."..key..".enabled")
            SetBool("game.tool."..key..".enabled", is_enabled)
            local rule = custom_rules[key]
            for j = 1, #info.upgrades do
                local prop = info.upgrades[j].id
                local value = rule and rule[prop] or info.upgrades[j].default
                -- local value = info.upgrades[j].max
                if prop == "ammo" then
                    if ammoScale ~= 1 then
                        value = math.floor(value * ammoScale)
                    end
                    SetInt("game.tool."..key..".ammo.max", value)
                end
                SetInt("game.tool."..key.."."..prop, value)
            end
        end
        SetBool("game.tool.sledge.enabled", true)
        SetBool("game.tool.spraycan.enabled", true)
        SetBool("game.tool.extinguisher.enabled", true)
        SetBool("game.tool.leafblower.enabled", true)
        disabled = cEntity:EnqueueDeletion()
    end
end
