||:@include: lib/rpn.nv

|:rewind: yes? :playfield: ___ | :behind: ___
	:: increment playfield size

|:comment: if theres a dot in front of pac eat it |
|:rewind: yes? :playfield: (pacman dot) |
    :behind: ___
	:: increment playfield size
	:playfield: pacman

|:comment: if theres a ghost in front of pac eat it |
|:rewind: yes? :playfield: (pacman ghost) |
    :behind: ___
	:: increment playfield size
	:playfield: pacman

|:comment: if pac hits a wall set the done flag |
|:rewind: yes? :playfield: (pacman wall) | :status: done
	:ahead: (wall pacman)
	:: increment playfield size
	:: increment playfield size

|:comment: otherwise move everything to the tracking tapes |
|:rewind: yes? :playfield: pacman | :ahead: pacman
	:: increment playfield size
|:rewind: yes? :playfield: dot | :ahead: dot
	:: increment playfield size
|:rewind: yes? :playfield: dot | :ahead: ghost
	:: increment playfield size
|:rewind: yes :playfield: wall | :ahead: wall
	:: increment playfield size

|:comment: use RPN for counting |
|:: increment playfield size :@rpn data: $id |
	:playfield size: $id
|:: increment playfield size? :playfield size: $id |
	:@rpn: ($id 1 +)

|:comment: make a note of how long the playfield was |
|:: :ahead: wall? :playfield size: $limit |
	:layout: ahead
	:full run: $limit

|:comment: restore the playfield from the tapes |
|:layout: ahead :ahead: pacman |	:playfield: pacman
	:layout: behind
	:: decrement playfield size
|:layout: ahead? :ahead: $a | :playfield: $a
	:: decrement playfield size
|:layout: behind? :behind: $b | :playfield: $b
	:: decrement playfield size

|:full run: $x |:playfield size: $x
|:: decrement playfield size :@rpn data: $id |
	:playfield size: $id
|:: decrement playfield size? :playfield size: $id |
	:@rpn: ($id 1 -)

|:comment: check if the frame is done and if the run is done |
|:playfield size: 0? :layout: $x :status: done | :rewind: no 
|:playfield size: 0? :layout: $x | :rewind: yes 

|| :playfield: (___ ___ pacman dot dot dot ghost dot wall) 
|| :playfield size: 0
|| :rewind: yes