@name Chaqua Nano Suit
@outputs Gr Dst
@persist Ar ArP Cr CrP GrP
@persist [E O FG]:entity
@persist [B]:array MB [Ball]:string #Balls
interval(15)
if(first())
{
    E = entity()
    E:setColor(0,0,0,0)
    E:propNotSolid(1)
    
    O = owner()
    O:setMaterial("phoenix_storms/chrome")
    
    FG = E:isConstrainedTo()
    if(FG:type()=="gmod_wire_field_device")
    {
        Dst = 120
        FG:createWire(E,"Distance","Dst")
        FG:createWire(E,"Active","Gr")
        FG:propNotSolid(1)
        FG:propFreeze(1)
        FG:setColor(0,0,0,0)
    }else
    {
        print("Spawn the E2 on a field gen.")
        selfDestruct()
    }
    
    runOnLast(1)
    
    function number entity:interceptR(T:entity#[Owner]#)
    {
        local DifPos = T:pos()-This:pos()
        local DifVel = This:eye()
        return acosr(DifVel:normalized():dot(DifPos:normalized()))*180/pi()<90 ? sinr(acosr(DifVel:normalized():dot(DifPos:normalized())))*DifPos:length() : 0
    }
    
    function vector entity:interceptP(T:entity#[Owner]#,R:number#[Radius]#)
    {
        local DifPos = T:pos()-This:pos()
        local DifVel = This:eye()
        return This:interceptR(T)<R&&acosr(DifVel:normalized():dot(DifPos:normalized()))*180/pi()<90 ? DifVel:normalized()*(DifPos:length()-R)+This:shootPos() : T:pos()+vec(0,0,200)
    }
    
    Ball = "models/hunter/misc/sphere1x1.mdl"
    MB = 4
    for(I = 1, MB)
    {
        local TE = propSpawn(Ball,E:pos()+vec(0,0,30),1)
        TE:setColor(0,0,0,0)
        TE:propNotSolid(1)
        B:pushEntity(TE)
    }
}

#Armor
if(changed(O:keyPressed("DELETE"))&O:keyPressed("DELETE"))
{
    if(ArP)
    {
        stoptimer("ar")
        Ar = !Ar
        ArP = 0
        hint("Armor: "+(Ar ? "Activated" : "Deactivated"),3)
    }else
    {
        timer("ar",300)
        ArP = 1
    }
}
if(clk("ar"))
{
    stoptimer("ar")
    ArP = 0
}
if(changed(Ar))
{
    foreach(I, TE:entity = B)
    {
        TE:propNotSolid(!Ar)
    }
    if(Ar)
    {
        timer("tick",50)
    }else
    {
        stoptimer("tick")
    }
}
if(Ar&O:health()>=75)
{
    if(clk("tick"))
    {
        stoptimer("tick")
        timer("tick",50)
        local Tr = array()
        
        foreach(I, Ply:entity = players())
        {
            if(Ply:interceptR(O)<175&Tr:count()<MB)
            {
                Tr:pushEntity(Ply)
            }
        }
        
        if(Tr:count()>0)
        {
            foreach(I, Ply:entity = Tr)
            {
                local TE = B[I,entity]
                if(Ply:pos():distance(O:pos())>250&!O:keyAttack2()&Ply:interceptP(O,175):isInWorld())
                {
                    TE:setPos(Ply:interceptP(O,175))
                    TE:propNotSolid(0)
                }else
                {
                    TE:propNotSolid(1)
                }
            }
        }
    }
}

#Gravity
if(changed(O:keyJump())&O:keyJump())
{
    if(GrP)
    {
        stoptimer("gr")
        Gr = !Gr
        GrP = 0
        hint("Anti-Graivty: "+(Gr ? "Activated" : "Deactivated"),3)
    }else
    {
        timer("gr",300)
        GrP = 1
    }
}
if(clk("gr"))
{
    stoptimer("gr")
    GrP = 0
}
FG:setPos(O:pos()-vec(0,0,30))

#Cloak
if(changed(O:keyDuck())&O:keyDuck())
{
    if(CrP)
    {
        stoptimer("Cr")
        Cr = !Cr
        CrP = 0
        hint("Cloak: "+(Cr ? "Activated" : "Deactivated"),3)
    }else
    {
        timer("cr",300)
        CrP = 1
    }
}
if(clk("cr"))
{
    stoptimer("cr")
    CrP = 0
}
if(Cr)
{
    O:weapon():setAlpha(0)
}
if(changed(Cr))
{
    if(Cr)
    {
        O:setMaterial("models/effects/vol_light001")
    }else
    {
        O:setMaterial("phoenix_storms/chrome")
        O:weapon():setAlpha(255)
    }
}

if(duped()||dupefinished()|!O:isAlive())
{
    B:propDelete()
    reset()
}

if(last())
{
    B:propDelete()
}
