@name testg
@inputs 
@outputs 
@persist Entity:entity WorldDown:vector 
@persist CurrentLeg:string LegData:table Cycle FDir:vector
@trigger none
@model models/hunter/plates/plate1x1.mdl

if ( first() ) {
    
    Entity = entity()
    WorldDown = vec(0, 0, -1)

    LegData["FrontL", table] = table( "Offset" = vec(150, 150, 0)   )
    LegData["FrontR", table] = table( "Offset" = vec(150, -150, 0)  )
    LegData["RearL", table]  = table( "Offset" = vec(-150, 150, 0)  )
    LegData["RearR", table]  = table( "Offset" = vec(-150, -150, 0) )
    
    foreach (K, Leg:table = LegData) { Leg["Output", vector] = Entity:pos() }
    
    holoCreate(1)
    holoCreate(2)
    holoCreate(3)
    holoCreate(4)
}

interval(60)

Cycle = (Cycle + 18) % 360
if ( Cycle <= 90  & Cycle > 0   ) { CurrentLeg = "FrontL" }
if ( Cycle <= 180 & Cycle > 90  ) { CurrentLeg = "RearR"  }
if ( Cycle <= 270 & Cycle > 180 ) { CurrentLeg = "FrontR" }
if ( Cycle <= 360 & Cycle > 270 ) { CurrentLeg = "RearL"  }

Up = vec(0,0,1)

FDot = entity():forward():dot(Up)*0
RDot = entity():right():dot(Up)*0

W = owner():keyPressed("up")
A = owner():keyPressed("left")
S = owner():keyPressed("down")
D = owner():keyPressed("right")

MDir = entity():toWorldAxis( vec(-FDot, RDot, 0)*100 + vec( (W-S), (A-D), 0 ):normalized()*100 )
FDir = mix( MDir, FDir, 0.25 )


LegData[CurrentLeg, table]["Target", vector] = rangerOffset( 1000, Entity:toWorld(LegData[CurrentLeg, table]["Offset", vector]) + FDir, WorldDown ):position()

local BodyPos = vec()
foreach (K, Leg:table = LegData) {
    local Dir = Leg["Target", vector] + vec(0, 0, Leg["Target", vector]:distance(Leg["Output", vector])/3) - Leg["Output", vector]
    Leg["Output", vector] = Leg["Output", vector] + Dir:normalized() * min(Dir:length(), 36)
    BodyPos += Leg["Output", vector]
}
BodyPos = (BodyPos / 4)


local AxisP = Entity:toLocalAxis( 
    (LegData["FrontL", table]["Target", vector] + LegData["FrontR", table]["Target", vector])/2 -
    (LegData["RearL", table]["Target", vector] + LegData["RearR", table]["Target", vector])/2
)

local AxisR = Entity:toLocalAxis( 
    (LegData["FrontR", table]["Target", vector] + LegData["RearR", table]["Target", vector])/2 -
    (LegData["FrontL", table]["Target", vector] + LegData["RearL", table]["Target", vector])/2
)

local Target = owner():eyeTrace():position()
local Bearing = -bearing(Entity:pos(), Entity:angles(), Target) / 5
local Angle = Entity:toWorld(ang( atan(AxisP[1], AxisP[3]) - 90, Bearing, atan(-AxisR[2], AxisR[3]) - 90 ))

Diff = abs((LegData["FrontL", table]["Output", vector] - LegData["FrontR", table]["Output", vector])[3])

Entity:propGravity(0)
Entity:applyForce( ((BodyPos + vec(0, 0, 100 + Diff) - Entity:pos())*10 - Entity:vel()) *Entity:mass() )
Entity:applyAngForce( (Entity:toLocal(Angle)*200 - Entity:angVel()*20) * shiftL(ang(Entity:inertia())) )

holoPos(1, LegData["FrontL", table]["Output", vector])
holoPos(2, LegData["FrontR", table]["Output", vector])
holoPos(3, LegData["RearL", table]["Output", vector])
holoPos(4, LegData["RearR", table]["Output", vector])

