@name Paper Clip Wood-Wars Tank
@inputs SeatLink:wirelink TurretLink:wirelink TankLink:wirelink
@inputs W A S D
@inputs [W1_A W1_B W1_C W2_A W2_B W2_C]:wirelink
@outputs C_pos:vector C_dir:vector C_parent:entity
@persist Seat:entity Turret:entity DesYaw LastYaw Tank:entity Init
@persist [W1_Ae W1_Be W1_Ce W2_Ae W2_Be W2_Ce]:entity
@trigger 
if( first() | duped() )
{
    timer("init",3000)
    exit()
}elseif( clk("init") )
{
    Seat = SeatLink:entity()
    Tank = TankLink:entity()
    Turret = TurretLink:entity()
    Turret:setMass(2000)
    runOnTick(1)
    C_parent = Turret
    C_pos = Turret:massCenter() + 50*Turret:forward() + 45*Turret:up() # <- Change this control the camera position
    C_dir = Seat:forward()
    C_roll = 0
    W1_Ae = W1_A:entity()
    W1_Be = W1_B:entity()
    W1_Ce = W1_C:entity()
    W2_Ae = W2_A:entity()
    W2_Be = W2_B:entity()
    W2_Ce = W2_C:entity()
    
}


if( Seat:driver() )
{
    if( !Init )
    {
        C_parent = Turret
        C_pos = Turret:massCenter() + 50*Turret:forward() + 45*Turret:up() # <- Change this control the camera position
        C_dir = Seat:forward()
        Init = 1
        Turret:soundPlay(1,0,"vehicles/tank_turret_loop1.wav")
    }
    
    #########################
    ##### TANK CONTROL
    #########################
    RightWheelForce = (W-S-D+A)*500000
    LeftWheelForce = -(W-S-A+D)*500000
    SlowFactor = 1000
    W1_Ae:applyAngForce(ang(0,LeftWheelForce-SlowFactor*W1_Ae:angVel():yaw(),0))
    W1_Be:applyAngForce(ang(0,LeftWheelForce-SlowFactor*W1_Be:angVel():yaw(),0))
    W1_Ce:applyAngForce(ang(0,LeftWheelForce-SlowFactor*W1_Ce:angVel():yaw(),0))
    W2_Ae:applyAngForce(ang(0,RightWheelForce-SlowFactor*W2_Ae:angVel():yaw(),0))
    W2_Be:applyAngForce(ang(0,RightWheelForce-SlowFactor*W2_Be:angVel():yaw(),0))
    W2_Ce:applyAngForce(ang(0,RightWheelForce-SlowFactor*W2_Ce:angVel():yaw(),0))
    TractionForce = 2000
    W1_Ae:applyForce(-Tank:up()*TractionForce)
    W1_Be:applyForce(-Tank:up()*TractionForce)
    W1_Ce:applyForce(-Tank:up()*TractionForce)
    W2_Ae:applyForce(-Tank:up()*TractionForce)
    W2_Be:applyForce(-Tank:up()*TractionForce)
    W2_Ce:applyForce(-Tank:up()*TractionForce)
    
    #########################
    ##### TURRET CONTROL
    #########################
    
    # Aim the turret
    
    # perform the projection
    LocalVec = vec(Seat:driver():eye():dot(Seat:forward()),Seat:d  river():eye():dot(Seat:right()),Seat:driver():eye(  ):dot(Seat:up()))

    # Calculate the difference in yaw
    Dif = angnorm(LastYaw-LocalVec:toAngle():yaw())
    LastYaw = LocalVec:toAngle():yaw()
    soundPitch(1,clamp(60+abs(Dif*3),0,100))
    
    # Apply the force
    Turret:applyAngForce(-250000*ang(0,Dif,0))
    
    
    # Control the turret direction
    DesiredDirection = Seat:forward()-Seat:forward():dot(Tank:up())*Tank:up()
    DesiredDirectionRight = DesiredDirection:cross(Tank:up())
    LocalDifference1 = vec(Seat:forward():dot(DesiredDirection),Seat:righ  t():dot(DesiredDirection),Seat:up():dot(DesiredDir  ection))
    LocalDifference2 = vec(Seat:forward():dot(DesiredDirectionRight),Seat  :right():dot(DesiredDirectionRight),Seat:up():dot(  DesiredDirectionRight))
    DifAngle = angnorm(ang( LocalDifference1:toAngle():pitch(), LocalDifference1:toAngle():yaw(), LocalDifference2:toAngle():pitch() ))
    Turret:applyAngForce(50000*ang(-DifAngle:pitch(),0,-DifAngle:roll()) -5000*Turret:angVel())
}else{
    if( Init == 1 )
    {
        Init = 0
        soundPurge()
    }
    
    # Control the turret direction
    DesiredDirection = Seat:forward()-Seat:forward():dot(Tank:up())*Tank:up()
    DesiredDirectionRight = DesiredDirection:cross(Tank:up())
    LocalDifference1 = vec(Seat:forward():dot(DesiredDirection),Seat:righ  t():dot(DesiredDirection),Seat:up():dot(DesiredDir  ection))
    LocalDifference2 = vec(Seat:forward():dot(DesiredDirectionRight),Seat  :right():dot(DesiredDirectionRight),Seat:up():dot(  DesiredDirectionRight))
    DifAngle = angnorm(ang( LocalDifference1:toAngle():pitch(), LocalDifference1:toAngle():yaw(), LocalDifference2:toAngle():pitch() ))
    Turret:applyAngForce(50000*ang(-DifAngle:pitch(),0,-DifAngle:roll()) -5000*Turret:angVel())
    
}
