@name HoverBike
@inputs  Pod:wirelink
@outputs Acc
@persist [E Seat]:entity  Ang:angle ChairOn
@persist TarPos:vector TarAng:angle 
@persist W A S D R Shift Alt Space Active Mouse1 Mouse2 Direction
@persist Speed L T N I Rotation
@model models/hunter/plates/plate1x2.mdl
runOnTick(100)

entity():setColor(vec(), 0)

Color = vec(145,145,145)
Material = "models/debug/debugwhite"

### Pod Controller ###
W           = Pod["W",      number]
A           = Pod["A",      number]
S           = Pod["S",      number]
D           = Pod["D",      number]
R           = Pod["R",      number]
Shift       = Pod["Shift",  number]
Alt         = Pod["Alt",    number]
Space       = Pod["Space",  number]
Active      = Pod["Active", number]
Mouse1      = Pod["Mouse1", number]
Mouse2      = Pod["Mouse2", number]
if (W) {Direction = 1}
if (S) {Direction = -1}

### Ranger ###
rangerFilter(entity())
rangerFilter(players())
rangerHitWater(1)
RD = rangerOffset(entity():pos(), entity():pos():setZ(-100000))

### Smoothers ###
#Leaning
if     (A & !D)          {L += 1.5}
elseif (D & !A)          {L -= 1.5}
else                     {L -= L/20}
L = clamp(L, -20, 20)

#Turning
if     (A | D)           {T += 0.05}
else                     {T -= T/10}
T = clamp(T, 0, 1)
 
#Accelerating
if     (W & !S)           {Acc += Shift ? 0.1 : 0.05}
elseif (S & !W & Acc>0.7) {Acc -= Acc/5}
elseif (S & !W)           {Acc -= 0.04}
else                      {Acc -= Acc/25}
if     (Alt  & Acc>0.8)   {Acc -= 0.1} 
Acc = clamp(Acc, -1 , 2)
### Position ###
MultiplierP = 30*Acc
if (RD:distance() < 50 & Space & !entity():isUnderWater()) {
    entity():applyForce(entity():up()*5000)
}
elseif (RD:distance() < 50 & !entity():isUnderWater()) {
    TarPos = entity():pos():setZ(RD:position():z() + 40) + entity():right()*MultiplierP
    entity():applyForce(((TarPos - entity():pos()) * 9 - entity():vel()) * entity():mass())
}

### Angle ###
MultiplierA = (3 + (Speed/4000))*(A-D)
TarAng = ang(Acc>-0.1 ? L : -L, entity():angles():yaw() + MultiplierA*T, 0)
entity():applyAngForce(((TarAng - entity():angles()) * 40 - entity():angVel()) * entity():mass()*4)

###  Holos ###
if (first()|duped()) {
    timer("delay", 1000)
    
    holoCreate(1)
    holoModel(1,"models/hunter/tubes/tube2x2x025d.mdl")
    holoAng(1,ang(135,90,90))
    holoPos(1,entity():toWorld(vec(0,-26.5,-31.8)))
    holoParent(1,entity())
    holoMaterial(1,Material)
    holoColor(1,Color)
    
    holoCreate(2)
    holoModel(2,"models/hunter/tubes/tube4x4x025d.mdl")
    holoAng(2,ang(-45,90,90))
    holoPos(2,entity():toWorld(vec(-5.955,6,68)))
    holoParent(2,entity())
    holoMaterial(2,Material)
    holoColor(2,Color)
    
    holoCreate(3)
    holoModel(3,"models/props_c17/signpole001.mdl")
    holoAng(3,ang(0,0,130))
    holoPos(3,entity():toWorld(vec(3,-57,3)))
    holoScale(3,vec(1,1,0.2))
    holoParent(3,entity())
    
    holoCreate(4)
    holoModel(4,"models/props_c17/signpole001.mdl")
    holoAng(4,ang(0,0,130))
    holoPos(4,entity():toWorld(vec(-3,-57,3)))
    holoScale(4,vec(1,1,0.2))
    holoParent(4,entity())
    
    holoCreate(5)
    holoModel(5,"models/hunter/misc/shell2x2a.mdl")
    holoAng(5,ang(0,0,-10))
    holoPos(5,entity():toWorld(vec(0,-75,-18)))
    holoScale(5,vec(0.2,0.4,0.3))
    holoParent(5,entity())
    holoMaterial(5,Material)
    holoColor(5,Color)
    
    holoCreate(6)
    holoModel(6,"models/hunter/misc/shell2x2a.mdl")
    holoAng(6,ang(0,0,38))
    holoPos(6,entity():toWorld(vec(0,80,-8)))
    holoScale(6,vec(0.25,0.8,0.2))
    holoParent(6,entity())
    holoMaterial(6,Material)
    holoColor(6,Color)
    
    holoCreate(7)
    holoColor(7,vec(0,0,0))
    holoModel(7,"models/hunter/tubes/tube1x1x2.mdl")
    holoAng(7,ang(0,0,38))
    holoPos(7,entity():toWorld(vec(0,80,-8)))
    holoScale(7,vec(0.4,1.3,0.04))
    holoParent(7,entity())
    holoMaterial(7,Material)
    
    holoCreate(9)
    holoColor(9,vec(255,0,0))
    holoModel(9,"models/hunter/misc/shell2x2a.mdl")
    holoAng(9,ang(0,0,38))
    holoPos(9,entity():toWorld(vec(0,80,-8)))
    holoScale(9,vec(0.25,0.8,0.04))
    holoParent(9,entity())
    holoMaterial(9,Material)
    
    holoCreate(10)
    holoColor(10,vec(0,0,0))
    holoModel(10,"models/hunter/tubes/tube1x1x2.mdl")
    holoAng(10,ang(0,0,38))
    holoPos(10,entity():toWorld(vec(0,80,-8)))
    holoScale(10,vec(0.2,1,0.04))
    holoParent(10,entity())
    holoMaterial(10,Material)
    
    holoEntity(7):setTrails(10,0,1,"trails/smoke",vec(145,145,145), 255)
}

if (entity():vel():length() > 10 & Active) {
    Rotation += Direction*entity():vel():length()/100
}
for (I = 1, N) {
    holoAng(I, entity():angles():setRoll(Rotation + (360/N)*I))
    holoPos(I, entity():pos() + holoEntity(I):up()*38)
    holoColor(I, vec(I%2==1?50 : 0, I%2==1?50 : 0,I%2==1?190+(60*sin(curtime()*360)) : 0))
    holoMaterial(I, "models/shiny")
}

if (clk("delay") | $Active) {
    entity():isWeldedTo(1):setMass(0.01)
    entity():isWeldedTo(2):setMass(0.01)
    entity():isWeldedTo(3):setMass(0.01)
    entity():isWeldedTo(4):setMass(0.01)
}
#Seat
    findByClass("prop_vehicle_prisoner_pod")
    findSortByDistance(entity():pos())
    if(find():pos():distance(entity():pos())<175&&find():owner()==owner()&&find():model()=="models/nova/airboat_seat.mdl")
    {
        Seat = find()
        Seat:setPos(entity():toWorld(vec(0,35,-10)))
        Seat:setAng(entity():toWorld(ang(0,180,-20)))
        Seat:setMaterial(Material)
        Seat:setColor(vec(72,72,72))
        Seat:propFreeze(1)
        timer("parent",100)
    }
    if(clk("parent"))
    {
        stoptimer("parent")
        Seat:parentTo(entity())
        holoDelete(23000)
        ChairOn = 1
    }
if (first()|duped()) {
    print("Hover Bike Made By Wrain!")
    #entity():soundPlay(0,0,"ambient/levels/labs/teleport_rings_loop2.wav")
    soundVolume(2,0.4)
    soundPitch(2,30)
}
