--[[
!!!THIS SCRIPT CONTAINS OVERRIDDEN VERSIONS OF THE FUNCTIONS IN BWO!!!
SHOULD THE OG DEV MAKE A MAJOR CHANGE THESE FUNCTIONS WILL EITHER CAUSE ERRORS OR BE OUTDATED IN LIGHT OF UPDATED FUNCTIONS
--]]

function chanceEvents(){
    local chance = 50
    local chanceFunc = function() if(not FDUtils.RandPer(chance)) then return end end
    
    --not everyone will call the cops, rather assuming someone else will.
    FDUtils.PrependFunction(BWOEvents.CallHazmats,chanceFunc)
    FDUtils.PrependFunction(BWOEvents.CallCops,chanceFunc)
    FDUtils.PrependFunction(BWOEvents.CallMedics,chanceFunc)
    chance = 75
    FDUtils.PrependFunction(BWOEvents.CallSWAT,chanceFunc)
    --firemen are unchanged to prevent the npcs from being too braindead
}

local function CallSWATReplace(params)
    local player = getSpecificPlayer(0)
    local color = player:getSpeakColour()
    player:addLineChatElement("Edited SWAT Func Called", color:getR(), color:getG(), color:getB())
    print("---!!!Edited Function Called!!!---")

    if(not FDUtils.RandPer(50)) then return end

    if not BWOPopControl.SWAT.On then return end
    if BWOPopControl.SWAT.Cooldown > 0 then return end

    local player = getSpecificPlayer(0)
    local x, y = findVehicleSpot2(params.x, params.y)
    if not x or not y then return end

    local vehicleCount = player:getCell():getVehicles():size()
    if vehicleCount < 10 then
        spawnVehicle (x, y, "Base.PickUpVanLightsPolice")
        arrivalSound(x, y, "ZSPoliceCar1")

        local vparams = {}
        vparams.lightbar = true
        BWOScheduler.Add("VehiclesUpdate", vparams, 500)
    end
    
    config = {}
    config.clanId = 0
    config.hasRifleChance = 100
    config.hasPistolChance = 100
    config.rifleMagCount = 4
    config.pistolMagCount = 3

    local event = {}
    event.hostile = params.hostile
    event.occured = false
    event.program = {}
    event.program.name = "Bandit"
    event.program.stage = "Prepare"
    event.x = x + 6
    event.y = y + 6
    event.bandits = {}
            
    local bandit = BanditCreator.MakeFromWave(config)
    bandit.outfit = "ZSPoliceSpecialOps"
    bandit.accuracyBoost = FDUtils.RandFloat(0.8,1.2) --FD CHANGE accuracy boost slash detriment
    bandit.femaleChance = 0
    bandit.weapons.melee = "Base.Nightstick"

    FDUtils.Recur(function() table.insert(event.bandits, bandit) end,math.random(2, 6)) --FD CHANGE make police more varied in number
            
    sendClientCommand(getSpecificPlayer(0), 'Commands', 'SpawnGroup', event)

    BWOPopControl.SWAT.Cooldown = SandboxVars.BanditsWeekOne.SWATCooldown -- 120
end

local function CallCopsReplace(params)
    local player = getSpecificPlayer(0)
    local color = player:getSpeakColour()
    player:addLineChatElement("Edited Cop Func Called", color:getR(), color:getG(), color:getB())
    print("---!!!Edited Function Called!!!---")

    if(not FDUtils.RandPer(50)) then return end

    if not BWOPopControl.Police.On then return end
    if BWOPopControl.Police.Cooldown > 0 then return end

    local player = getSpecificPlayer(0)
    local x, y = findVehicleSpot2(params.x, params.y)
    if not x or not y then return end

    local vehicleCount = player:getCell():getVehicles():size()
    if vehicleCount < 10 then
        spawnVehicle (x, y, "Base.PickUpVanLightsPolice")
        arrivalSound (x, y, "ZSPoliceCar1")

        local vparams = {}
        vparams.lightbar = true
        BWOScheduler.Add("VehiclesUpdate", vparams, 500)
    end

    config = {}
    config.clanId = 0
    config.hasRifleChance = 0
    config.hasPistolChance = 75 --FD CHANGE to 75 to provide more varied combat
    config.rifleMagCount = 0
    config.pistolMagCount = math.random(1, 3) --FD CHANGE varied mag count to provide more varied combat

    local event = {}
    event.hostile = params.hostile
    event.occured = false
    event.program = {}
    event.program.name = "Police"
    event.program.stage = "Prepare"
    event.x = x + 6
    event.y = y + 6
    event.bandits = {}
            
    local bandit = BanditCreator.MakeFromWave(config)
    bandit.outfit = "Police"
    bandit.accuracyBoost = FDUtils.RandFloat(0.6,1) --FD CHANGE accuracy boost slash detriment
    bandit.weapons.melee = "Base.Nightstick"

    FDUtils.Recur(function() table.insert(event.bandits, bandit) end,math.random(1, 2)) --FD CHANGE make police more varied in number
            
    sendClientCommand(getSpecificPlayer(0), 'Commands', 'SpawnGroup', event)


    if SandboxVars.Bandits.General_ArrivalIcon then
        local icon = "media/ui/sheriff.png"
        local color
        if event.hostile then
            color = {r=1, g=0, b=0} -- red
        else
            color = {r=0, g=1, b=0} -- green
        end
        BanditEventMarkerHandler.setOrUpdate(getRandomUUID(), icon, 10, params.x, params.y, color)
    end

    BWOPopControl.Police.Cooldown = SandboxVars.BanditsWeekOne.PoliceCooldown -- 30
end

Events.OnGameBoot.Add(function()
    BWOEvents.CallCops = CallCopsReplace
    BWOEvents.CallSWAT = CallSWATReplace
    print("---!!!Functions PATCHED!!!---")
    --[[

    if(BeyondOneWeek.config.allowInstrustiveChanges) then
        BWOEvents.CallCops = FDIC.CallCops
        BWOEvents.CallSWAT = FDIC.CallSWAT
    end
    if(BeyondOneWeek.config.chanceAndCooldown) then
        chanceEvents()
    end
    --]]
end)