@name Fgt Mech Mk 2
@inputs [P FL FR BL BR]:entity C:wirelink
@outputs [HFL HFR HBL HBR]:entity
@persist [E B]:entity Ntpl [FLP FRP BLP BRP]:vector [AA OA]:angle Inc Thr Ft

interval(50)

if(first())
{
    E = entity()
    B = E:isConstrainedTo()
    B:setMass(5000)
    
    Ntpl = 30 / (500 / 33)
    
    rangerFilter(array(E, B, P, FL, FR, BL, BR))
    rangerPersist(1)
    
    holoCreate(1)
    holoAlpha(1,0)
    
    local R = 25
    HFL = holoCreate(2,B:toWorld(vec(R, R, 0)))
    HFR = holoCreate(3,B:toWorld(vec(-R, R, 0)))
    HBL = holoCreate(4,B:toWorld(vec(R, -R, 0)))
    HBR = holoCreate(5,B:toWorld(vec(-R, -R, 0)))
    
    holoParent(2,B)
    holoParent(3,B)
    holoParent(4,B)
    holoParent(5,B)
    
    holoModel(2,"hq_sphere")
    holoModel(3,"hq_sphere")
    holoModel(4,"hq_sphere")
    holoModel(5,"hq_sphere")
    
    holoColor(2,vec(40,40,40))
    holoColor(3,vec(40,40,40))
    holoColor(4,vec(40,40,40))
    holoColor(5,vec(40,40,40))
    
    holoMaterial(2,"phoenix_storms/mat/mat_phx_metallic")
    holoMaterial(3,"phoenix_storms/mat/mat_phx_metallic")
    holoMaterial(4,"phoenix_storms/mat/mat_phx_metallic")
    holoMaterial(5,"phoenix_storms/mat/mat_phx_metallic")
    
    #hip
    holoCreate(6)
    holoParent(6,B)
    holoPos(6,B:toWorld(vec(0,0,8)))
    holoAng(6,B:toWorld(ang(0,270,0)))
    holoModel(6,"models/props_vehicles/apc001.mdl")
    holoScale(6,vec(0.5,0.3,0.3))
    holoMaterial(6,"phoenix_storms/mat/mat_phx_metallic")
    holoColor(6,vec(40,40,40))
    
    local OX = 85
    local OY = 85
    local VelV = clamp(B:vel():setZ(0) * 0.45,vec(-75),vec(75))
    FLR = rangerOffset(150^2, B:toWorld(vec(OX, OY, 25)) + VelV, vec(0,0,-1))
    FRR = rangerOffset(150^2, B:toWorld(vec(-OX, OY, 25)) + VelV, vec(0,0,-1))
    BLR = rangerOffset(150^2, B:toWorld(vec(OX, -OY, 25)) + VelV, vec(0,0,-1))
    BRR = rangerOffset(150^2, B:toWorld(vec(-OX, -OY, 25)) + VelV, vec(0,0,-1))
    FL:setPos(FLR:pos() + vec(0,0,30))
    FR:setPos(FRR:pos() + vec(0,0,30))
    BL:setPos(BLR:pos() + vec(0,0,30))
    BR:setPos(BRR:pos() + vec(0,0,30))
    FLP = FL:pos()
    FRP = FR:pos()
    BLP = BL:pos()
    BRP = BR:pos()
}

holoAng(2,ang(0,(BL:pos()-holoEntity(2):pos()):toAngle():yaw()+180,0))
holoAng(3,ang(0,(FR:pos()-holoEntity(3):pos()):toAngle():yaw()+180,0))
holoAng(4,ang(0,(FL:pos()-holoEntity(4):pos()):toAngle():yaw()+180,0))
holoAng(5,ang(0,(BR:pos()-holoEntity(5):pos()):toAngle():yaw()+180,0))

if(P:isValid())
{
    Dri = P:driver()
    Act = Dri:isValid()
    W = Dri:keyForward()
    A = Dri:keyLeft()
    S = Dri:keyBack()
    D = Dri:keyRight()
    Shift = Dri:keySprint()
    
    local Wlk = W|A|S|D
    if(Wlk)
    {
        holoAng(1,Dri:eyeAngles() * ang(0, 1, 0))
        AA = holoEntity(1):toWorldAxis(vec(W - S, A - D, 0)):toAngle()
    }
    Thr += ((Wlk * 13) - Thr)/5
    
    if(Act)
    {
        holoPos(1,B:toWorld(vec(0, 0, 25)) - Dri:eye() * 250)
        holoAng(1,Dri:eyeAngles())
    }
    if(changed(Act))
    {
        C["Activated",number] = Act
        C["Position",vector] = holoEntity(1):pos()
        C["Angle",angle] = Dri:eyeAngles()
        C["Parent",entity] = holoEntity(1)
    }
}

local OX = 85
local OY = 85
local VelV = clamp(B:vel():setZ(0) * 0.45,vec(-75),vec(75))
FLR = rangerOffset(150^2, B:toWorld(vec(OX, OY, 25)) + VelV, vec(0,0,-1))
FRR = rangerOffset(150^2, B:toWorld(vec(-OX, OY, 25)) + VelV, vec(0,0,-1))
BLR = rangerOffset(150^2, B:toWorld(vec(OX, -OY, 25)) + VelV, vec(0,0,-1))
BRR = rangerOffset(150^2, B:toWorld(vec(-OX, -OY, 25)) + VelV, vec(0,0,-1))

Inc = Inc < 199 ? (Inc + ((3 + (B:angVelVector():length() / 20) + VelV:length() / 18))*Ntpl) : 0
Ft = ceil(min(Inc,200) / 50)
if(changed(Ft))
{
    switch(Ft)
    {
        case 1,
        BLP = FLR:pos() - vec(0, 0, 2)
        break
        
        case 2,
        FRP = FRR:pos() - vec(0, 0, 2)
        break
        
        case 3,
        FLP = BLR:pos() - vec(0, 0, 2)
        break
        
        case 4,
        BRP = BRR:pos()
        break
    }
}

FL:applyForce((((FLP + vec(0, 0, FL:pos():distance(FLP)/2)) - FL:pos())*10 - FL:vel()/1.5) * FL:mass())
FR:applyForce((((FRP + vec(0, 0, FR:pos():distance(FRP)/2)) - FR:pos())*10 - FR:vel()/1.5) * FR:mass())
BL:applyForce((((BLP + vec(0, 0, BL:pos():distance(BLP)/2)) - BL:pos())*10 - BL:vel()/1.5) * BL:mass())
BR:applyForce((((BRP + vec(0, 0, BR:pos():distance(BRP)/2)) - BR:pos())*10 - BR:vel()/1.5) * BR:mass())


if(FLR:hit() & FRR:hit() & BLR:hit() & BRR:hit() #[& (B:vel():length()<100)]#)
#if(FLR:distance()<150&FRR:distance()<150&BLR:distance()<150&BRR:distance()<150)
{
    B:applyForce((((B:forward() * Thr) + ((FL:pos() + FR:pos() + BL:pos() + BR:pos())/4 + vec(0, 0, 60) - B:pos())*vec(0, 0, 1))*10 - B:vel()) * B:mass())
}
B:applyAngForce((angnorm(OA + AA - B:angles())*15 - B:angVel()*5) * B:mass())
