@name Missile
@inputs Fire Spawner:wirelink
@persist Missiles:array Target:entity Active Busy Count
@persist Delay Stop I K V

if (clk("Push"))
{
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 35)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    V++, Missile = Missiles:entity(V)
    if (Missile)
    {
        Missile:applyForce((Missile:eye() / Missile:eye():length()) * 100000)
        Direction = Target:pos() - Missile:pos() + vec(0, 0, 30)
        Missile:applyOffsetForce(Missile:mass() * (Direction / Direction:length()), Missile:toWorld(Missile:boxCenter() + vec(500, 0, 0)))
        Missile:applyAngForce(-2 * Missile:mass() * Missile:angVel())
        Missile:applyForce(vec(0, 0, (Missile:mass() + 1) * 8.97))
    }
    else
    {
        Missiles:remove(V)
    }
    
    if (Missiles:count() == 0)
    {
        Active = 0
        stoptimer("Push")
        exit()
    }
    
    if (V >= Missiles:count())
    {
        V = 0
    }
    
    timer("Push", 1)
    exit()
}

if (Fire & ~Fire & !Busy)
{
    if (!Target)
    {
        hint("No target selected.", 7)
        hint("Command: -target (<player>)", 7)
        exit()
    }
    
    Busy = 1

    timer("Spawn missile", 0)
    exit()
}

if (chatClk(owner()))
{
    Command = owner():lastSaid():explode(" "):string(1)
    Argument = owner():lastSaid():explode(" "):string(2)
    
    if (Command == "-t" | Command == "-target")
    {
        if (Argument)
        {
            Player = findPlayerByName(Argument)
        
            if (Player)
            {
                Target = Player
            }
            else
            {
                hint("Couldn't detect player.", 7)
                hint("Case-sensitive.", 7)
            }
        }
        else
        {
            Entity = owner():aimEntity()
            
            if (Entity)
            {
                Target = Entity
            }
            else
            {
                hint("You're not aiming at an entity.", 7)
                exit()
            }
        }
    }
    elseif (Command == "-f" | Command == "-fire" & !Busy)
    {
        if (!Target)
        {
            hint("No target selected.", 7)
            hint("Command: -target (<player>)", 7)
            exit()
        }
        
        if (Argument)
        {
            Count = Argument:toNumber()
            
            if (Count == 1)
            {
                if (Command == "-f")
                {
                    hint("Use '-f' instead of '-f 1'", 7)
                }
                else
                {
                    hint("Use '-fire' instead of '-fire 1'", 7)
                }
                
                exit()
            }
            elseif (Count < 1)
            {
                if (Command == "-f")
                {
                    hint("Command: -f (<count>)", 7)
                }
                else
                {
                    hint("Command: -fire (<count>)", 7)
                }
                
                hint("Count > 1", 7)
                exit()
            }
            
            Busy = 1
            Stop = 0
            K = 0
            
            timer("Multiple missiles", 0)
        }
        else
        {
            Busy = 1
            timer("Spawn missile", 0)
        }
    }
    elseif (Command == "-d" | Command == "-delay")
    {
        if (!Argument | Argument:toNumber() < 0)
        {
            if (Command == "-f")
            {
                hint("Command: -d <delay>", 7)
            }
            else
            {
                hint("Command: -delay <delay>", 7)
            }
            
            hint("Delay > 0", 7)
            exit()
        }
        
        if (Argument:toNumber() < 135)
        {
            hint("The delay is recommended to be at least 135.", 7)
            hint("A low delay may cause problems.", 7)
        }
        
        Delay = Argument:toNumber()
    }
    elseif (Command == "-s" | Command == "-stop")
    {
        Stop = 1
    }
    
    exit()
}

if (clk("Spawn missile"))
{
    Spawner:setNumber("Spawn", 1)
    Spawner:setNumber("Spawn", 0)
    
    timer("Ready", Delay)
    timer("Missile spawned", 30)
    
    exit()
}

if (clk("Missile spawned"))
{
    findInSphere(Spawner:entity():pos(), 50)
    
    if (findClipToModel("models/props_phx/amraam.mdl") < 2)
    {
        hint("Could not detect missile.", 5)
    }
    else
    {
        I = 1
        timer("Filter missiles", 0)
    }
    
    exit()
}

if (clk("Filter missiles"))
{
    Missile = findResult(I)
    
    if (!Missile)
    {
        hint("Could not detect missile.", 5)
        exit()
    }
    
    if (Missile:id() != Spawner:entity():id() & Missile:owner() == owner())
    {
        Missile:setMass(50)
        Missiles:pushEntity(Missile)
        
        if (!Active)
        {
            Active = 1
            timer("Push", 0)
        }
        
        exit()
    }
    
    I++
    
    timer("Filter missiles", 0)
    exit()
}

if (clk("Multiple missiles"))
{
    if (Stop)
    {
        Busy = 0
        hint("Stopped.", 5)
        exit()
    }
    
    Spawner:setNumber("Spawn", 1)
    Spawner:setNumber("Spawn", 0)
    
    timer("Missile spawned", 30)
    
    K++
    
    if (K < Count)
    {
        timer("Multiple missiles", Delay)
    }
    else
    {
        timer("Ready", Delay)
    }
    
    exit()
}

if (clk("Ready"))
{
    Busy = 0
}

if (first() | duped())
{
    Delay = 300
    runOnChat(1)
}
