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

E = entity()

if(first()){
    Speed = 49
    Acceleration = 0.3
    HeighOffset = 14
    YawAffect = 2
    Angle = 1010
    Angle2 = 60
    E:setMass(50000)
}

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

FSpeed = E:velL():y()
        
if(changed(E:isUnderWater()) & E:isUnderWater()) {
    WaterL = E:pos():z()
}

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(-Roll,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()
}
