################
# > Deju Vu: Record the movement of a prop, then play it backwards.
# > How to use: Spawn on a prop. Type '!start' to begin recording. Type '!stop' to end recording. Type '!run' to play the recording.
################

@name Deja vu
@outputs Position:array Angles:array Prop:entity Play Record Index Remove

interval(10)

if (first()|duped()) {
    Prop = entity():isWeldedTo()
    Position = array()
    Angles = array()
}

if (lastSpoke()==owner()) {
    if (changed(lastSaid())&lastSaid()=="!start") {
        hint("Recording: Started.",8)
        Record=1
    }
    if (changed(lastSaid())&lastSaid()=="!stop") {
        hint("Recording: Stopped. Length: "+round(Position:count()/65)+" seconds.",8)
        Record=0
    }
    if (changed(lastSaid())&lastSaid()=="!run") {
        hint("Recording: Playing.",8)
        Play=1
    }
}

if (!changed(Record)&Record) {
    Position[Position:count()+1,vector]=Prop:pos()
    Angles[Angles:count()+1,angle]=Prop:angles()
}

if (!changed(Play)&Play) {
    Prop:setPos(Position[Position:count()+1-Index,vector])
    Prop:setAng(Angles[Angles:count()+1-Index,angle])
    
    Index+=1
    if (Index>Position:count()) {
        for (Remove=1,Position:count()) {
            Position:remove(1)
            Angles:remove(1)
        }
        Remove=0
        Play=0
        Index=0
    }
}
