@name Frog ship
@inputs PodCtrl:wirelink EyeCtrl:wirelink
@outputs
@persist TickTime InertiaConstant RS K Sensitivity InertiaConstantRad
@persist Chair:entity Ship:entity Owner:entity ShipParts:array
@persist Pos:vector Orientation:quaternion NoDriver AngleOffset ##AimVec:vector 
if (tickClk()) {
    
#AdvPodController



    
Ship:applyForce(Ship:mass()*(K*(Pos-Ship:pos()) - Ship:vel()))

if (Chair:driver()) {
    if (NoDriver) {
        timer("driver wait",200)
        AngleOffset = Ship:angles():yaw()
    } else {
        ##Absolute = Chair:pos()+Owner:eye()
        ##AimVec = Chair:toLocal(Absolute):rotate(ang(0,AngleOffset,0))
	Sensitivity = 0.2
Change = ang(EyeCtrl["Y",number],EyeCtrl["X",number],(PodCtrl["TurnLeftKey",number]-PodCtrl["TurnRightKey",number])*10)*Sensitivity
	Orientation = Orientation/quat(Change)
    
        Forward = TickTime*Ship:forward():normalized()
        Right = TickTime*Ship:right():normalized()
        Up = TickTime*Ship:up():normalized()
        if (PodCtrl["R",number]) {
            Forward*=250
            Right*=350
            Up*=200
        } else {
            Forward*=950
            Right*=700
            Up*=600
        }

        if (PodCtrl["Shift",number]) {
            Forward *=2
            Right*=2
            Up*=2
        }
        if (PodCtrl["W",number]) {Pos+=Forward}
        if (PodCtrl["S",number]) {Pos-=Forward}
        
        if (PodCtrl["Alt",number]) {
            if (PodCtrl["D",number]) {Pos+=Up}
            if (PodCtrl["A",number]) {Pos-=Up}
        } else {
            if (PodCtrl["D",number]) {Pos+=Right}
            if (PodCtrl["A",number]) {Pos-=Right}
        }
    }
    EyeCtrl["Enable",number] = 1
} else {
    NoDriver = 1
}



#######################################

QShip = quat(Ship)
QTarget = Orientation
#get needed rotation quaterion
Qrot= qMod(QTarget*inv(QShip)) #need to modulo it to stop angle glich
#get vector representing needed rotation around axis (in rad)
RotationNeeded = vec(2*log(Qrot))

ShipAngVel = Ship:angVelVector()*pi()/180
ActualInertia = Ship:inertia()*InertiaConstantRad
LocalRot = Ship:toLocalAxis(RotationNeeded)#since applyTorque is local to entity
Ship:applyTorque((LocalRot/TickTime - ShipAngVel)*ActualInertia)
#######################################

} elseif (clk("driver wait")) {#provides a delay for when the person enters the pod

    AngleOffset = Ship:angles():yaw()
    NoDriver = 0
    
} elseif (clk("find")) {

    ShipParts = entity():getConstraints()
    Amount = ShipParts:count()
    for (I=1,Amount) {
        CurEntity = ShipParts[I,entity]
        if (CurEntity:type()=="prop_vehicle_prisoner_pod") {
            Chair = CurEntity
        } elseif (CurEntity:model()=="models/props_phx/construct/metal_plate2x2.mdl") {
            Ship = CurEntity
        }
    }
    Chair:setMass(2000)
    Ship:setMass(50000)
    Pos = Ship:pos()
    ##AimVec = Ship:forward()
	Orientation = quat(Ship)
    runOnTick(1)
    
} elseif (first()|duped()) {
EyeCtrl["Enable",number] = 1
TickTime = 0.015 #time for a game tick
InertiaConstant = 27.053079273838 #Universal inertia constant
InertiaConstantRad = 1550.0030838848
#Maximum performance
RS = 1/TickTime
K = 1/TickTime

#Smoothness factor (make higher to make smoother)
#set to 1 for maximum turn performance
RS/=5
K/=5

Owner = owner()

timer("find",50)



}

