@name Car
@inputs W A S D Space
@outputs Hohe L1RD L2RD R1RD R2RD LX ToR
@persist Rotation LX WHEELMODEL:string
@trigger 
@model models/hunter/blocks/cube1x2x025.mdl

interval(10)
E = entity()
WHEELMODEL = "models/sprops/trans/wheel_d/t_wheel35.mdl"
Heigth=40
if(dupefinished()) { reset() }
if(first()) { E:setMass(1000) }

RF = array(E)


rangerFilter(RF) L1R = rangerOffset(Heigth, E:pos()+E:right()*70-E:forward()*55, -E:up())
rangerFilter(RF) L2R = rangerOffset(Heigth, E:pos()-E:right()*70-E:forward()*55, -E:up())

rangerFilter(RF) R1R = rangerOffset(Heigth, E:pos()+E:right()*70+E:forward()*55, -E:up())
rangerFilter(RF) R2R = rangerOffset(Heigth, E:pos()-E:right()*70+E:forward()*55, -E:up())

Hohe = (L1R:distance()+L2R:distance()+R1R:distance()+R2R:distance())/4

if(Hohe<Heigth) {

E:applyForce(E:up()* (20 - Hohe - E:velL():z()/9 ) * E:mass()*5 )
E:applyOffsetForce( E:forward() * -E:velL():x() * E:mass()*0.1,E:pos()+E:right()*11)
E:applyOffsetForce(-E:right() *clamp( -E:velL():y(),-40,40) * E:mass()/(50-Space*49),E:pos()-vec(0,0,Space))


L1RD = L1R:distance()
L2RD = L2R:distance()

R1RD = R1R:distance()
R2RD = R2R:distance()

E:applyAngForce( (ang( (R1RD+R2RD)-(L1RD+L2RD),0,(L1RD+R1RD)-(R2RD+L2RD)) - E:angVel()/5) *E:mass()*7)
if(!A&!D){E:applyAngForce(ang(E:angVel():pitch(),-E:angVel():yaw()*33,E:angVel():roll())*E:mass()/5)}


L1RP = L1R:pos()
L2RP = L2R:pos()

R1RP = R1R:pos()
R2RP = R2R:pos()

if(first()) {
    holoCreate(1) holoModel(1,WHEELMODEL) holoParent(1,E)
    holoCreate(2) holoModel(2,WHEELMODEL) holoParent(2,E)
    holoCreate(3) holoModel(3,WHEELMODEL) holoParent(3,E)
    holoCreate(4) holoModel(4,WHEELMODEL) holoParent(4,E)
}

holoPos(1,L1RP+vec(0,0,24) )
holoPos(2,L2RP+vec(0,0,24) )
holoPos(3,R1RP+vec(0,0,24) )
holoPos(4,R2RP+vec(0,0,24) )

ToR = (toUnit("mph",(entity():velL():y())))
Rotation = Rotation + ToR  
if(A){LX = clamp(LX+1,-15,15)}
if(D){LX = clamp(LX-1,-15,15)}
if(!A&LX>0){LX = clamp(LX-1,0,25)}
if(!D&LX<0){LX = clamp(LX+1,-25,0)}
if(inrange(ToR,-2,2)) { LX = 0 }



if(A){E:applyOffsetForce(-E:forward()* (A-D) *(100*LX)*clamp(round(ToR),0,2), E:pos()-E:right()*32)}
if(D){E:applyOffsetForce(E:forward()* (A-D) *(100*LX)*clamp(round(ToR),0,2), E:pos()-E:right()*32)}

holoAng(1, entity():toWorld(ang(Rotation,90,0)))
holoAng(2, entity():toWorld(ang(Rotation,90+LX,0)))
holoAng(3, entity():toWorld(ang(-Rotation,-90,0)))
holoAng(4, entity():toWorld(ang(-Rotation,-90+LX,0)))

E:applyForce(-E:right()*(W-S)*7000)

}
if(Hohe > 22) {
    E:applyForce(E:up()*E:vel():z()*E:mass()/200)
}

