@name Physics Recorder
@inputs C:entity
@persist M I [E]:entity [A V]:array
if(first())
{
    runOnTick(1)
    runOnChat(1)
	
	   function printChat()
	   {
		      printColor(vec(255,100,0),"Commands:")
        printColor(vec(255,100,0),"-r to record")
        printColor(vec(255,100,0),"-p to play recording")
        printColor(vec(255,100,0),"-s to stop recording or pause playback")
        printColor(vec(255,100,0),"-rs to stop recording and reset playback to the first frame")
        printColor(vec(255,100,0),"-rw to rewind recording")
        printColor(vec(255,100,0),"-d to delete recording")
		      printColor(vec(255,100,0),"-help to print the commands onto the chat again.")
	   } 
    
    I = 2
    
    E = entity()
    
    #[
    if(C:isValid())
    {
        printChat()
    }else
    {
        printColor(vec(255,100,0),"Spawn this E2 on an entity.")
        selfDestruct()
    }
    ]#
}

if(chatClk(owner()))
{
    local LS = owner():lastSaid():lower()
    if(LS[1]=="-")
    {
        hideChat(1)
    }
    
    switch(LS)
    {
        case "-r",
            M = 1
			         I = 2
            C:propFreeze(0)
            hint("Recording...",3)
        break
        
        case "-p",
            M = 2
            C:propFreeze(1)
            hint("Playing...",3)
        break
        
        case "-s",
            M = 0
            hint("Stopping...",3)
        break
        
        case "-rs",
            M = 0
            I = 2
            hint("Resetting playback...",3)
        break
        
        case "-rw",
            if(M<=1)
            {
                I = V:count()-1
            }
            M = 3
            C:propFreeze(1)
            hint("Rewinding...",3)
        break
        
        case "-d",
            M = 0
            I = 2
            V:clear()
            A:clear()
			         C:propFreeze(0)
            hint("Deleting frames...",3)
        break
		
		      case "-help",
			         printChat()
		      break
    }
}

I = clamp(I,2,V:count()-1)

switch(M)
{
    
    case 1,
        V:pushVector(C:pos())
        A:pushAngle(C:angles())
    break
    
    case 2,
        I++
        C:setPos(V[I,vector])
        C:setAng(A[I,angle])
    break
    
    case 3,
        I--
        C:setPos(V[I,vector])
        C:setAng(A[I,angle])
    break
}
