@name DroneTypo
@inputs 
@outputs 
@persist [VFrontRight VFrontLeft VBackRight VBackLeft]:vector TFrontRight TFrontLeft TBackRight TBackLeft MaxTilt Sensitivity
@trigger 
runOnTick(1)

Distance = 12
Size = 0.5

if (first()) {
    TFrontRight = 2.3 #Throttle
    TFrontLeft = 2.2
    TBackRight = 2.2
    TBackLeft = 2.3
    MaxTilt = 0.1
    Sensitivity = 25 #higher is less sensitive
    
    holoCreate(1)
    holoModel(1,"hq_torus")
    holoPos(1,entity():toWorld(vec(Distance,Distance,0)))
    holoScale(1,vec(2,2,2))
    holoParent(1,entity())
    
    holoCreate(2)
    holoModel(2,"hq_torus")
    holoPos(2,entity():toWorld(vec(-Distance,Distance,0)))
    holoScale(2,vec(2,2,2))
    holoParent(2,entity())
    
    holoCreate(3)
    holoModel(3,"hq_torus")
    holoPos(3,entity():toWorld(vec(-Distance,-Distance,0)))
    holoScale(3,vec(2,2,2))
    holoParent(3,entity())
    
    holoCreate(4)
    holoModel(4,"hq_torus")
    holoPos(4,entity():toWorld(vec(Distance,-Distance,0)))
    holoScale(4,vec(2,2,2))
    holoParent(4,entity())
    
    entity():setMass(251)
}
local FrontRight = entity():toWorld(vec(Distance,Distance,0)) - entity():pos()
local FrontLeft = entity():toWorld(vec(Distance,-Distance,0)) - entity():pos()
local BackRight = entity():toWorld(vec(-Distance,Distance,0)) - entity():pos()
local BackLeft = entity():toWorld(vec(-Distance,-Distance,0)) - entity():pos()

local FFrontRight = TFrontRight - clamp((FrontRight:z() + (FrontLeft:z() + BackRight:z() + BackLeft:z()) / 3) / Sensitivity, -MaxTilt, MaxTilt) #if average z of other holos is above this, make it push harder
local FFrontLeft = TFrontLeft - clamp((FrontLeft:z() + (FrontRight:z() + BackRight:z() + BackLeft:z()) / 3) / Sensitivity, -MaxTilt, MaxTilt)
local FBackRight = TBackRight - clamp((BackRight:z() + (FrontLeft:z() + FrontRight:z() + BackLeft:z()) / 3) / Sensitivity, -MaxTilt, MaxTilt)
local FBackLeft = TBackLeft - clamp((BackLeft:z() + (FrontLeft:z() + BackRight:z() + FrontRight:z()) / 3) / Sensitivity, -MaxTilt, MaxTilt)

VFrontRight = entity():up()*entity():mass()*FFrontRight
VFrontLeft = entity():up()*entity():mass()*FFrontLeft
VBackRight = entity():up()*entity():mass()*FBackRight
VBackLeft = entity():up()*entity():mass()*FBackLeft
applyOffsetForce(VFrontRight + $VFrontRight*5, entity():toWorld(vec(Distance,Distance,0))) #frontright, the $ (delta) stabilizes it
applyOffsetForce(VFrontLeft + $VFrontLeft*5, entity():toWorld(vec(Distance,-Distance,0))) #frontleft
applyOffsetForce(VBackRight + $VBackRight*5, entity():toWorld(vec(-Distance,Distance,0))) #backright
applyOffsetForce(VBackLeft + $VBackLeft*5, entity():toWorld(vec(-Distance,-Distance,0))) #backleft
