@name Fulton Recovery System
@persist [E O T B]:entity
@persist A CF M R R2 [V OP]:vector
@model models/hunter/plates/plate1x1.mdl
if(first())
{
    runOnTick(1)
    
    E = entity()
    O = owner()
    
    E:setMaterial("models/props_combine/metal_combinebridge001")
    
    holoCreate(1, E:toWorld(vec(0, 0, 2.25)), vec(3.65, 3.65, 0.125))
    holoMaterial(1, "models/props_canal/metalwall005b")
    holoParent(1, E)
    
    local TP = propSpawn("models/sprops/misc/alphanum/alphanum_h.mdl", 1) #Check if sprops is on server
    if(TP:isValid())
    {
        TP:propDelete()
        holoCreate(2, E:toWorld(vec(0, 0, 2.75)), vec(3, 0.25, 3), E:toWorld(ang(0, 0, 90)))
        holoColor(2, vec(245, 215, 0))
        holoModel(2, "models/sprops/misc/alphanum/alphanum_star_2.mdl")
        holoMaterial(2, "models/props_debris/plasterwall034a")
        holoParent(2, E)
    }
}

KM = O:keySprint()

if(A & !M)
{
    local AE = O:aimEntity()
    if(AE:isValid())
    {
        CF = O:aimPos():distance(O:shootPos()) < 100 & AE:pos():distance(E:pos()) > 1000 & AE != E  & AE:type() != "player" & !AE:type():find("npc")
        print(4, (CF ? "Can Fulton." : "Cannot Fulton."))
        
        if(CF)
        {
            local K = KM & O:keyReload()
            if(K)
            {
                T = AE
                
                M = 1
                
                holoCreate(3, O:aimPos(), vec(0.5), O:eyeTrace():hitNormal():toAngle()+ang(90, 0, 0))
                holoModel(3, "hq_dome")
                holoMaterial(3, "sprops/textures/sprops_metal3")
                holoParent(3, AE)
                
                B = propSpawn("models/hunter/blocks/cube025x025x025.mdl", O:aimPos()+vec(0, 0, 15), ang(), 1)
                B:setColor(0, 0, 0, 0)
                B:propNotSolid(1)
                
                holoCreate(4, B:pos(), vec(0), ang())
                holoModel(4, "models/maxofs2d/balloon_classic.mdl")
                holoParent(4, B)
                
                rope(1, T, T:toLocal(O:aimPos()), B, vec(), 150, 3, "cable/cable2")
                
                OP = T:pos()
            }
        }
    }
}

if(M>0)
{
    if(changed(M))
    {
        switch(M)
        {
            case 2, 
                holoScale(4, vec(7.5))
                R2 = 0
            break
            
            case 3, 
                R = 0
                
                timer("pc", 1500)
            break
            
            case 4, 
                V = (E:pos()+vec(0, 0, 250) - B:pos()):normalized()
            break
            
            case 5, 
                soundPlay("do", 0.53, "buttons/weapon_confirm.wav")
            
                T:constraintBreak(B)
                
                holoDelete(3)
                
                timer("pc", 3000)
            break
            
            case 6, 
                holoDelete(4)
                
                propSpawn("models/props_phx/ww2bomb.mdl", B:pos(), 1):propBreak()
                
                B:propDelete()
                
                T = noentity()
                R = 0
                R2 = 0
                M = 0
            break
        }
    }else
    {
        switch(M)
        {
            case 1, 
                R += 0.03125
                B:setPos(B:pos()+vec(0, 0, R))
                
                if(R2 < 7.499)
                {
                    R2 += (7.5 - R2)/50
                    holoScale(4, vec(R2))
                }
                
                M += (OP:z() - E:pos():z()) > 20 ? (abs(B:pos():z() - (OP:z()+750)) < 5) : abs(B:pos():z() - (E:pos():z()+750)) < 5
            break
            
            case 2, 
                R -= 0.0625
                B:setPos(B:pos()+vec(0, 0, R))
                
                M += R<=0.015625
            break
            
            case 4, 
                R += 0.03125
                B:setPos(B:pos() + V*R)
                
                M += B:pos():distance(E:pos()+vec(0, 0, 250)) < 150
            break
            
            case 5, 
                R -= 0.125
                R2 += 0.0625
                B:setPos(B:pos() + V*R + vec(0, 0, R2))
            break
        }
        R = clamp(R, 0, 5)
        
        if(clk("pc"))
        {
            stoptimer("pc")
            M++
        }
    }
    
    if(M < 5 & T:mass() > 2000)
    {
        T:applyAngForce((-T:angVel() * 3) * T:mass())
    }
    
    if(!T:isValid() | !B:isValid())
    {
        B:propDelete()
        
        holoDelete(3)
        holoDelete(4)
        
        propSpawn("models/props_phx/ww2bomb.mdl", B:pos(), 1):propBreak()
        
        R = 0
        R2 = 0
        M = 0
    }
}

local K = KM & O:keyZoom()
if(changed(K)&K)
{
    A = !A
    print(4, "Fulton "+(A ? "activated." : "deactivated."))
    O:soundPlay("a", A ? 0.28 : 0.572, A ? "buttons/blip2.wav" : "buttons/button6.wav")
}
