@name Cool Coaster 6
@inputs E:entity Pod:wirelink E2:entity E3:entity E4:entity E5:entity
@outputs Ops CarPos:vector Aim:vector Shoot
@persist UpUp2 UpUp Go AA:array PA:array LastSpawnTime Pos:vector Ang:angle Count
@persist AA3:array PA3:array AA2:array PA2:array
@persist AA4:array PA4:array AA5:array PA5:array
@trigger 
runOnTick(1)
Tracks = 27
Ops = ops()
Aim = Pod["AimPos",vector]
Alt = Pod["Alt",number]
Shift = Pod["Shift",number]
if(duped()){reset()}
if(Shift)
{
Speed = 25
}
else{Speed = 10}
Del = 10
Wait = 500
Up = Pod["W",number]
Down = Pod["S",number]
Left = Pod["A",number]
Right = Pod["D",number]
Active = Pod["Active",number]
Reset = Pod["R",number]
RollL = Pod["Mouse1",number]
RollR = Pod["Mouse2",number]
#Stop = Pod["Alt",number]
Stop = 0
Light = Pod["Space",number]
CarPos = E:pos()

if(Left)
{
Ang = Ang+ang(0,1,0)    
}
if(Right)
{
Ang = Ang-ang(0,1,0)
}
if(Up)
{
Ang = Ang+ang(1,0,0)    
}
if(Down)
{
Ang = Ang-ang(1,0,0)
}

if(RollL)
{
Ang = Ang-ang(0,0,1)
}

if(RollR)
{
Ang = Ang+ang(0,0,1)
}

if(Reset)
{
Ang = ang(0,Ang:yaw(),0)    
}


timer("track",1000/Speed)
if(first()|duped())
{
for(I=1,Tracks)
{
holoCreate(I)
holoScaleUnits(I,vec(10,50,5))
holoMaterial(I,"Straw/StrawTile_diffuse")
    
}
E:setMass(50000)
E2:setMass(50000)
E3:setMass(50000)
E4:setMass(50000)
E5:setMass(50000)
holoCreate(-10)
holoModel(-10,"icosphere3")
holoCreate(-20)
holoModel(-20,"icosphere3")    
holoEntity(-10):setTrails(10,10,5,"phoenix_storms/chrome",vec(255,255,255),255)
holoEntity(-20):setTrails(10,10,5,"phoenix_storms/chrome",vec(255,255,255),255)
holoCreate(-30)
holoModel(-30,"icosphere3")     
holoScale(-30,vec(0,0,0))
Pos = entity():pos()+vec(0,0,200)
Ang = ang(0,0,0)
}
holoPos(-30,Pos)
holoPos(-20,Pos+holoEntity(-30):right()*25)
holoPos(-10,Pos+holoEntity(-30):right()*-25)
if(Active)
{
holoAng(-30,Ang)
Pos = Pos+holoEntity(-30):forward()*Speed
Mediator = holoEntity(-30)

if(!Mediator:pos():isInWorld())
{Ang = ang(-10,Mediator:angles():yaw(),0)}

PA:pushVector(Mediator:pos())
AA:pushAngle(Mediator:angles())

if(clk("track"))
{
Count=Count%Tracks+1  
holoPos(Count,Pos)
holoAng(Count,Ang)    

}


if(UpUp > Wait/Speed & Active & !Stop)
{
P = PA:shiftVector()
Ang3 = AA:shiftAngle()
V = P-E:pos()

TarQ = quat(Ang3) 
CurQ = quat(E)
Q = TarQ/CurQ
Vlock = E:toLocal(rotationVector(Q)+E:pos())
    
E:applyTorque((150*Vlock - 12*E:angVelVector())*E:inertia())  
E:applyForce(V*(E:mass()*10)-E:vel()*(E:mass()/2))

PA2:pushVector(E:pos())
AA2:pushAngle(E:angles())


}

if(UpUp > (Wait+200)/Speed & Active & !Stop)
{
P2 = PA2:shiftVector()
Ang4 = AA2:shiftAngle()

V4 = P2-E2:pos()

TarQ = quat(Ang4) 
CurQ = quat(E2)
Q = TarQ/CurQ
Vlock = E2:toLocal(rotationVector(Q)+E2:pos())
    
E2:applyTorque((150*Vlock - 12*E2:angVelVector())*E2:inertia())  
E2:applyForce(V4*(E2:mass()*10)-E2:vel()*(E2:mass()/2))

PA3:pushVector(E2:pos())
AA3:pushAngle(E2:angles())
    
}

if(UpUp > (Wait+400)/Speed & Active & !Stop)
{
P3 = PA3:shiftVector()
Ang5 = AA3:shiftAngle()

V5 = P3-E3:pos()

TarQ = quat(Ang5) 
CurQ = quat(E3)
Q = TarQ/CurQ
Vlock = E3:toLocal(rotationVector(Q)+E3:pos())
    
E3:applyTorque((150*Vlock - 12*E3:angVelVector())*E3:inertia())  
E3:applyForce(V5*(E3:mass()*10)-E3:vel()*(E3:mass()/2))

PA4:pushVector(E3:pos())
AA4:pushAngle(E3:angles())
     
}

if(UpUp > (Wait+600)/Speed & Active & !Stop)
{
P4 = PA4:shiftVector()
Ang6 = AA4:shiftAngle()

V6 = P4-E4:pos()

TarQ = quat(Ang6) 
CurQ = quat(E4)
Q = TarQ/CurQ
Vlock = E4:toLocal(rotationVector(Q)+E4:pos())
    
E4:applyTorque((150*Vlock - 12*E4:angVelVector())*E4:inertia())  
E4:applyForce(V6*(E4:mass()*10)-E4:vel()*(E4:mass()/2))

PA5:pushVector(E4:pos())
AA5:pushAngle(E4:angles())
     
}

if(UpUp > (Wait+800)/Speed & Active & !Stop)
{
P5 = PA5:shiftVector()
Ang7 = AA5:shiftAngle()

V7 = P5-E5:pos()

TarQ = quat(Ang7) 
CurQ = quat(E5)
Q = TarQ/CurQ
Vlock = E5:toLocal(rotationVector(Q)+E5:pos())
    
E5:applyTorque((150*Vlock - 12*E5:angVelVector())*E5:inertia())  
E5:applyForce(V7*(E5:mass()*10)-E5:vel()*(E5:mass()/2))
    
}
UpUp++
}
if(last())
{
holoDelete(-10)
holoDelete(-20)
holoDelete(-30)    
}

