 max_commands = 9
 
 addCommandButtonAction = (level) ->
+  if level.mode == levelMode.TURNAROUND || level.mode == levelMode.PLAYREV
+    return false
   if level.commands.length >= max_commands
+    # TODO: display error
     return false
 
-  switch level.mode
-    when levelMode.PLAYING, levelMode.FULLPLAYING
-      level.commands[level.commands.length] = @cmd
-    when levelMode.REVPLAYING, levelMode.TURNAROUND
-      level.reset()
-      level.commands[level.commands.length] = @cmd
-
-  true
+  level.commands[level.commands.length] = @cmd
+
+  if level.mode == levelMode.STOPDONE
+    level.reset()
+  if level.mode == levelMode.STOP
+    level.mode = levelMode.PLAY
+    level.animation_mode = levelAnimMode.READY
+    level.last_t = Date.now()/1000
+
+  return true
 
 playButtonAction = (level) ->
   level.resetAndRun()
-  level.mode = levelMode.FULLPLAYING
+  level.mode = levelMode.PLAYFWD
   return
 
 undoButtonAction = (level) ->

    item_location: item_location
    bot: (render: renderBot)
    commands: [ ]
    current_command: 0
    render: renderLevel
    animate: animateLevel
    step: stepLevelSimulation

    mode: levelMode.PLAYING
    animation_mode: levelAnimMode.READY

    advanceCurrentCommand: ->
      switch @mode
        when levelMode.PLAYING, levelMode.FULLPLAYING
          @current_command++
        when levelMode.REVPLAYING
          @current_command--
    reset: resetLevel
    resetAndRun: resetAndRunLevel
  o.reset()


  level.commands = level.commands[...-1]

  level.resetAndRun()
  while level.next_command < level.commands.length && level.step()
    true

  level.mode = levelMode.STOP

arrowButtonRender = (ctx) ->
  renderArrow(ctx, @dir.theta, inner_control_size)
