@name HoverCar2
@inputs Pod:wirelink 
@inputs [Chassis LF RF LB RB]:entity 
@outputs 
@persist Wheels:array 
@trigger 
interval(20)

Height = 45

if (first() | dupefinished()) {
    for (WModel = 1,4) {
        holoCreate(WModel)
        holoParent(WModel,Chassis)
        holoModel(WModel,"models/sprops/trans/wheel_d/t_wheel35.mdl")
        holoScale(WModel,vec(0.8,0.8,0.8))
        holoBodygroup(WModel,1,2)
        holoSkin(WModel,3)
        holoColor(WModel,vec(150,0,0))
    }
    
    TurnRate = 4
    IsRearWheelDrive = 0
    
    Wheels = array(LF,RF,LB,RB)
    
    for (I = 1,Wheels:count()) {
        Entity = Wheels[I,entity]
        Entity:setAlpha(0)
    }
}

local Seat = Pod["Entity",entity]
local Driver = Seat:driver()
local W = Driver:keyForward()
local A = Driver:keyLeft()
local S = Driver:keyBack()
local D = Driver:keyRight()
local R = Driver:keyReload()
local Mouse1 = Driver:keyAttack1()
local Mouse2 = Driver:keyAttack2()
local Space = Driver:keyJump()
local Shift = Driver:keySprint()
local Active = Driver:isPlayer()

rangerFilter(LF)
rangerFilter(players())
Rng1 = rangerOffset(Height,LF:pos(),-LF:up())

rangerFilter(RF)
rangerFilter(players())
Rng2 = rangerOffset(Height,RF:pos(),-RF:up())

rangerFilter(LB)
rangerFilter(players())
Rng3 = rangerOffset(Height,LB:pos(),-LB:up())

rangerFilter(RB)
rangerFilter(players())
Rng4 = rangerOffset(Height,RB:pos(),-RB:up())

if (Rng1:hit()) {Diff1 = Height - Rng1:distance() LF:applyForce(vec(0,0,Diff1 * 10 - LF:velL()[3]) * LF:mass())}

if (Rng2:hit()) {Diff2 = Height - Rng2:distance() RF:applyForce(vec(0,0,Diff2 * 10 - RF:velL()[3]) * RF:mass())}

if (Rng3:hit()) {Diff3 = Height - Rng3:distance() LB:applyForce(vec(0,0,Diff3 * 10 - LB:velL()[3]) * LB:mass())}

if (Rng4:hit()) {Diff4 = Height - Rng4:distance() RB:applyForce(vec(0,0,Diff4 * 10 - RB:velL()[3]) * RB:mass())}

holoAng(1,LF:toWorld(ang(0,180,90)))
holoAng(2,RF:toWorld(ang(0,0,90)))
holoAng(3,LB:toWorld(ang(0,0,90)))
holoAng(4,RB:toWorld(ang(0,180,90)))

holoPos(1,LF:toWorld(vec(0,0,-14.7721175)))
holoPos(2,RF:toWorld(vec(0,0,-14.7721175)))
holoPos(3,LB:toWorld(vec(0,0,-14.7721175)))
holoPos(4,RB:toWorld(vec(0,0,-14.7721175)))

if (Rng1:hit() | Rng2:hit() | Rng3:hit() | Rng4:hit()) {
    for (I = 1,Wheels:count()) {
        Entity = Wheels[I,entity]
        
        IsFrontWheel = inrange(I,1,2)
        IsPoweredWheel = IsRearWheelDrive ? !IsFrontWheel : IsFrontWheel
        
        ForwardForce = 40
        Grip = 60
        GripLoss = ForwardForce - Grip
        Grip = 60 - GripLoss

        Entity:applyForce(Entity:right() * clamp(Entity:velL():y(),-Grip,Grip) * Entity:mass())
        Entity:applyForce(-Entity:forward() * (W - S) * -clamp(Entity:velL():x(),-Grip,-Grip) * IsPoweredWheel * Entity:mass())
        Entity:applyForce(-Entity:forward() * (Space) * -clamp(Entity:velL():x(),Grip,-Grip) * IsPoweredWheel * Entity:mass())
    }
}
