class Copier : Item { static val READY = 0 static val DONE = 1 static val JAMMED = 2 // The number of items that can be copied @Attribute var times = 3 // The number of copies to make of each item. @Attribute var copies = 1 var copied = 0 var state = 0 var previousOriginal : Item = null var previousCopy : Item = null override fun myTurn() { val above = lookNorth() if ( times <= 0 ) return if ( square.occupant != null ) return // Copy being made still. if ( state == READY ) { if ( !above.isEmpty() && above.copyable() && !above.isMoving() ) { val copyA = above.item().actor.costume.createChild( "copy" ) val copy = copyA.role as Movable copyA.x = actor.x copyA.y = actor.y val side = lookEast() if ( side == previousCopy ) return if ( side.isEmpty() ) { copyA.stage = actor.stage square.addOccupant( copy ) copy.replaceAction( Movement( copy, 1, 0, 8 ) ) previousOriginal = above.item() previousCopy = copy copied ++ if ( copied >= copies ) { times -- copied = 0 state = DONE } } else { state = JAMMED if ( ! isTalking() ) { speak( "Paper Jam\nCheck output tray", 3 ) } } } else { // The item to copy moved out of the way before we had a chance to make copies. if ( copied > 0 ) { copied = 0 times -- } } } else if (state == DONE) { if (!above.isMoving() && (above != previousOriginal)) { state = READY copied = 0 } } else if (state == JAMMED ) { if ( lookEast().isEmpty() ) { state = READY } else { if (!above.isMoving() && (above != previousOriginal)) { state = READY } } } } }