@name Boat
@inputs Pod:wirelink
@outputs YawVel
@persist A2 D2 Yaw WaterL Water Roll Pitch Speed Angle Angle2 
@persist YawAffect HeighOffset Forwards Backwards Acceleration T 
@trigger 
@model models/hunter/plates/plate1x3.mdl
interval(20)

E = entity()

if(first()){
    Speed = 49
    Acceleration = 0.38
    HeighOffset = -14
    YawAffect = 2
    Angle = 1010
    Angle2 = 60
    Leaning = 0 #-Roll is on, 0 is off
    Size = 2.5
    
    holoCreate(1,E:toWorld(E:boxSize()*vec(0,0.35,0.45)*Size))
    holoAng(1,E:angles()+ang(90,90,0))
    holoParent(1,E)
    holoModel(1,"hq_cone")
    
    E:setMass(50000)
    
    holoCreate(2)
    holoModel(2,"hq_hdome_thin")
    holoPos(2,E:toWorld(vec(0,110,0)*Size))
    holoScale(2,vec(5,5,14)*Size)
    holoAng(2,E:angles()+ang(90,90,0))
    holoParent(2,E)
    
    holoCreate(3)
    holoModel(3,"hq_tube_thin")
    holoPos(3,E:toWorld(vec(0,0,0)*Size))
    holoScale(3,vec(5,5,18.5)*Size)
    holoAng(3,E:angles()+ang(90,90,0))
    holoParent(3,E)
    
    holoCreate(4)
    holoModel(4,"hq_hdome_thin")
    holoPos(4,E:toWorld(vec(0,-110,0)*Size))
    holoScale(4,vec(5,5,18)*Size)
    holoAng(4,E:angles()+ang(90,-90,0))
    holoParent(4,E)
    
    holoCreate(5)
    holoModel(5,"models/hunter/triangles/075x075mirrored.mdl")
    holoPos(5,holoEntity(4):toWorld(vec(0,0,95.5)*Size))
    holoAng(5,E:angles()+ang(180,90,0))
    holoParent(5,E)
    
    holoCreate(6)
    holoModel(6,"models/hunter/triangles/075x075mirrored.mdl")
    holoPos(6,holoEntity(4):toWorld(vec(0,0,95.5)*Size))
    holoAng(6,E:angles()+ang(0,-90,90))
    holoParent(6,E)
    
    holoCreate(7)
    holoModel(7,"hq_tube_thin")
    holoPos(7,E:toWorld(vec(0,0,28)*Size))
    holoScale(7,vec(3,13,1)*Size)
    holoParent(7,E)
    
    holoCreate(8)
    holoModel(8,"hq_tube_thin")
    holoPos(8,E:toWorld(vec(0,-10,30)*Size))
    holoScale(8,vec(2.8,2.8,2.8)*Size)
    holoParent(8,E)
    
    holoCreate(9)
    holoModel(9,"hq_hdome_thin")
    holoPos(9,E:toWorld(vec(0,-10,45.8)*Size))
    holoScale(9,vec(2.8,2.8,2.8)*Size)
    holoParent(9,E)
    
    holoCreate(10)
    holoModel(10,"hq_hdome_thin")
    holoPos(10,E:toWorld(vec(0,0,34)*Size))
    holoScale(10,vec(3,13,1)*Size)
    holoParent(10,E)
    
    for (I = 2,10) {holoMaterial(I,"sprops/textures/sprops_rubber")}
}

W = Pod["W",number]
A = Pod["A",number]
S = Pod["S",number]
D = Pod["D",number]
Alt = Pod["Alt",number]
Light = Pod["Light",number]
Space = Pod["Space",number]
Shift = Pod["Shift",number]
Active = Pod["Active",number]
Seat = Pod["Entity",entity]

FSpeed = E:velL():y()
        
if(Space & E:isUnderWater()) {
    WaterL = E:pos():z() + 53
}
if(Shift) {
    WaterL = E:pos():z() - 20
}
WaterLevel = WaterL + (E:angles():roll() * E:boxSize():y())/130 * E:velL():normalized():y()
Water = (E:pos():z()>WaterLevel-270 & E:pos():z()<WaterLevel+10)

if(!Active) {
    Yaw = E:angles():yaw()
} elseif (Active) {
    Yaw = mod(Yaw + (A2 - D2),360)
}

Forwards = clamp(Forwards + (W-!W*2)*Acceleration,0,Speed)
Backwards = clamp(Backwards + (S-!S*2)*Acceleration/8,0,Speed)
Movement = Forwards - Backwards
A2 = clamp(A2 + (A - !A)/(1),0,1)
D2 = clamp(D2 + (D - !D)/(1),0,1)
Pitch = FSpeed/Angle
Roll = (A2-D2)*FSpeed/20
TarQ2 = quat(ang(Leaning,Yaw,Pitch))
CurQ2 = quat(E:angles())
Q2 = TarQ2/CurQ2
V2 = E:toLocal(rotationVector(Q2)+E:pos())
E:applyTorque((Angle2*V2 - 40*E:angVelVector())*E:inertia()*Water)

E:applyForce((E:pos():setZ(WaterLevel)-(E:pos()-vec(0,0,HeighOffset))-E:vel()*vec(0.05,0.05,0.1))*E:mass()*Water)
Vec = vec(0,1,0):rotate(E:toWorld(ang(E:angles():yaw(),0,0)))
E:applyForce(Vec*E:mass()*Movement*Water)

if(dupefinished()) {
    reset()
}
