--[[GAMEMODE CONFIGURATION START]]--

LeadBot.RespawnAllowed = false
LeadBot.SetModel = false
LeadBot.Gamemode = "assassins"
LeadBot.TeamPlay = false
LeadBot.LerpAim = true
LeadBot.NoSprint = true
LeadBot.NoFlashlight = true

--[[GAMEMODE CONFIGURATION END]]--

function LeadBot.StartCommand(bot, cmd)
    local controller = bot.ControllerBot
    if !IsValid(controller) or bot:IsFrozen() then return end
    local buttons = (controller.SprintTime and controller.SprintTime > CurTime() and IN_SPEED) or 0

    if bot:GetMoveType() == MOVETYPE_LADDER then
        local pos = controller.goalPos
        local ang = ((pos + bot:GetCurrentViewOffset()) - bot:GetShootPos()):Angle()

        if pos.z > controller:GetPos().z then
            controller.LookAt = Angle(-30, ang.y, 0)
        else
            controller.LookAt = Angle(30, ang.y, 0)
        end

        controller.LookAtTime = CurTime() + 0.1
        controller.NextJump = -1
        buttons = buttons + IN_FORWARD
    end

    if controller.NextDuck > CurTime() then
        buttons = buttons + IN_DUCK
    elseif controller.NextJump == 0 then
        controller.NextJump = CurTime() + 1
        buttons = buttons + IN_JUMP
    end

    if !bot:IsOnGround() and controller.NextJump > CurTime() then
        buttons = buttons + IN_DUCK
    end

    cmd:ClearButtons()
    cmd:ClearMovement()
    cmd:SetButtons(buttons)
end

function LeadBot.Think()
    for _, bot in pairs(player.GetAll()) do
        if bot:IsLBot() then
            if bot.NextSpawnTime and !bot:Alive() and bot.NextSpawnTime < CurTime() then
                bot:SpawnForRound()
                return
            end
        end
    end
end

function LeadBot.PlayerMove(bot, cmd, mv)
    if bot:IsFrozen() then return end

    local controller = bot.ControllerBot

    if IsValid(bot:GetAutoPilot()) then
        autopilot.Interrupt(bot)
    end

    if !IsValid(controller) then
        bot.ControllerBot = ents.Create("leadbot_navigator")
        bot.ControllerBot:Spawn()
        bot.ControllerBot:SetOwner(bot)
        controller = bot.ControllerBot
    end

    --[[local min, max = controller:GetModelBounds()
    debugoverlay.Box(controller:GetPos(), min, max, 0.1, Color(255, 0, 0, 0), true)]]

    -- force a recompute
    if controller.PosGen and controller.P and controller.TPos ~= controller.PosGen then
        controller.TPos = controller.PosGen
        controller.P:Compute(controller, controller.PosGen)
    end

    if controller:GetPos() ~= bot:GetPos() then
        controller:SetPos(bot:GetPos())
    end

    if controller:GetAngles() ~= bot:EyeAngles() then
        controller:SetAngles(bot:EyeAngles())
    end

    mv:SetForwardSpeed(1200)

    if IsValid(bot:GetTarget()) then
        controller.Target = bot:GetTarget()
    end

    if (controller.TargetTime and controller.TargetTime < CurTime()) or (bot.NextSpawnTime and bot.NextSpawnTime + 1 > CurTime()) or !IsValid(controller.Target) or controller.Target:Health() < 1 or (controller.Target.Alive and !controller.Target:Alive() or !controller.Target.Alive) then
        controller.Target = nil
        controller.TargetTime = nil
    end

    local dt = util.QuickTrace(bot:EyePos(), bot:GetForward() * 45, bot)

    if IsValid(dt.Entity) then
        local class = dt.Entity:GetClass()
        if class == "prop_door_rotating" then
            dt.Entity:Fire("OpenAwayFrom", bot, 0)
        elseif class == "func_breakable_surf" and !dt.Entity.Breaking then
            dt.Entity.Breaking = true
            hook.Call("PlayerUse", gmod.GetGamemode(), bot, dt.Entity)
        end
    end

    if !IsValid(controller.Target) and (!controller.PosGen or bot:GetPos():DistToSqr(controller.PosGen) < 1000 or controller.LastSegmented < CurTime()) then
        -- find a random spot on the map, and in 10 seconds do it again!
        controller.PosGen = controller:FindSpot("random", {radius = 12500})
        controller.LastSegmented = CurTime() + 10
    elseif IsValid(controller.Target) then
        -- move to our target
        local distance = controller.Target:GetPos():DistToSqr(bot:GetPos())

        if controller.Target.Disguised then
            controller.PosGen = controller:FindSpot("random", {radius = 800, pos = controller.Target:GetPos()})
        else
            controller.PosGen = controller.Target:GetPos()

            if distance <= 10000 then
                if controller.AttackTime or controller.TargetTime then
                    if controller.TargetTime or controller.AttackTime < CurTime() then
                        hook.Call("PlayerUse", gmod.GetGamemode(), bot, controller.Target)
                        controller.Target = nil
                    end
                else
                    controller.AttackTime = CurTime() + math.Rand(0.4, 1.2)
                end
                -- mv:SetForwardSpeed(-1200)
            else
                controller.AttackTime = nil
            end
        end
    end

    for _, pursuer in pairs(bot.Pursuers) do
        if !IsValid(pursuer) or !pursuer:IsPlayer() then continue end
        local distance = pursuer:GetPos():DistToSqr(bot:GetPos())
        if distance <= 20000 or (math.random(4000) == 1 and util.QuickTrace(bot:EyePos(), pursuer:EyePos() - bot:EyePos(), bot).Entity == pursuer) then
            if !pursuer.TimeP then
                pursuer.TimeP = CurTime() + 3
            elseif pursuer.TimeP < CurTime() then
                pursuer.TimeP = nil
                -- fight or flight
                if math.random(2) == 1 then
                    controller.Target = pursuer
                    controller.TargetTime = CurTime() + 3
                else
                    controller.SprintTime = CurTime() + 4
                end
            end
        else
            pursuer.TimeP = nil
        end
    end

    if math.random(3000) == 1 then -- and #bot.Pursuers > 0
        hook.Run("PlayerUseEquipment", bot, "disguiser")
    end

    -- movement also has a similar issue, but it's more severe...
    if !controller.P then
        return
    end

    local segments = controller.P:GetAllSegments()

    if !segments then return end

    local cur_segment = controller.cur_segment
    local curgoal = segments[cur_segment]

    -- got nowhere to go, why keep moving?
    if !curgoal then
        mv:SetForwardSpeed(0)
        return
    end

    -- think every step of the way!
    if segments[cur_segment + 1] and Vector(bot:GetPos().x, bot:GetPos().y, 0):DistToSqr(Vector(curgoal.pos.x, curgoal.pos.y)) < 100 then
        controller.cur_segment = controller.cur_segment + 1
        curgoal = segments[controller.cur_segment]
    end

    local goalpos = curgoal.pos

    -- waaay too slow during gamplay
    --[[if bot:GetVelocity():Length2DSqr() <= 225 and controller.NextCenter == 0 and controller.NextCenter < CurTime() then
        controller.NextCenter = CurTime() + math.Rand(0.5, 0.65)
    end

    if controller.NextCenter ~= 0 and controller.NextCenter < CurTime() then
        if bot:GetVelocity():Length2DSqr() <= 225 then
            controller.strafeAngle = ((controller.strafeAngle == 1 and 2) or 1)
        end

        controller.NextCenter = 0
    end]]

    if bot:GetVelocity():Length2DSqr() <= 225 then
        if controller.NextCenter < CurTime() then
            controller.strafeAngle = ((controller.strafeAngle == 1 and 2) or 1)
            controller.NextCenter = CurTime() + math.Rand(0.3, 0.65)
        elseif controller.nextStuckJump < CurTime() then
            if !bot:Crouching() then
                controller.NextJump = 0
            end
            controller.nextStuckJump = CurTime() + math.Rand(1, 2)
        end
    end

    if controller.NextCenter > CurTime() then
        if controller.strafeAngle == 1 then
            mv:SetSideSpeed(1500)
        elseif controller.strafeAngle == 2 then
            mv:SetSideSpeed(-1500)
        else
            mv:SetForwardSpeed(-1500)
        end
    end

    -- jump
    if controller.NextJump ~= 0 and curgoal.type > 1 and controller.NextJump < CurTime() then
        controller.NextJump = 0
    end

    -- duck
    if curgoal.area:GetAttributes() == NAV_MESH_CROUCH then
        controller.NextDuck = CurTime() + 0.1
    end

    controller.goalPos = goalpos

    if GetConVar("developer"):GetBool() then
        controller.P:Draw()
    end

    -- eyesight
    local lerp = FrameTime() * math.random(4, 6)
    local lerpc = FrameTime() * 3

    if !LeadBot.LerpAim then
        lerp = 1
        lerpc = 1
    end

    local mva = ((goalpos + bot:GetCurrentViewOffset()) - bot:GetShootPos()):Angle()

    mv:SetMoveAngles(mva)

    if IsValid(controller.Target) and util.QuickTrace(bot:EyePos(), (controller.Target:EyePos() - bot:EyePos()), bot).Entity == controller.Target then
        bot:SetEyeAngles(LerpAngle(lerp, bot:EyeAngles(), (controller.Target:EyePos() - bot:GetShootPos()):Angle()))
        return
    else
        if controller.LookAtTime > CurTime() then
            local ang = LerpAngle(lerpc, bot:EyeAngles(), controller.LookAt)
            bot:SetEyeAngles(Angle(ang.p, ang.y, 0))
        else
            local ang = LerpAngle(lerpc, bot:EyeAngles(), mva)
            bot:SetEyeAngles(Angle(ang.p, ang.y, 0))
        end
    end
end

-- fix for timers

hook.Add("Initialize", "LeadBot_TimerFix", function()
    function DelayNewTarget(ply, delay)
        ply:SetTarget(NULL, true)
        timer.Create("ass_plytargetdelay" .. ply:EntIndex(), delay or ConVars.Server.newTargetDelay:GetFloat(), 1, function()
            if IsValid(ply) then
                ply:SetTarget(GAMEMODE:GetNewTarget(ply))
            end
        end)
    end
end)
