Guest User

ComputerCraft - Gom - A Better Go Program For Turtles

a guest
Aug 30th, 2014
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.65 KB | None | 0 0
  1. -- An improved version of the go builtin command.
  2. -- Accepts a sequence of commands of the form "[repetitionNumber]<action>" where the
  3. -- repetitionNumber is optional and defaults to 1. Action can be
  4. -- f -> forward, b --> backwards, l -> move one to the left, r -> ...
  5. -- u -> up, d -> down
  6. -- tl -> turn left, tr -> turn right
  7.  
  8. function terminate(errMsg)
  9.     print(errMsg)
  10.     os.sleep(5)
  11.     os.queueEvent("terminate")
  12. end
  13.  
  14.  
  15. function execute(action, repetitionNumber)
  16.    
  17.     for i=1,repetitionNumber do
  18.         if action == "f" then
  19.             turtle.forward()
  20.         elseif action == "b" then
  21.             turtle.back()
  22.         elseif action == "l" then
  23.             if i == 1 then
  24.                 turtle.turnLeft()
  25.             end
  26.             turtle.forward()
  27.             if i==repetitionNumber then
  28.                 turtle.turnRight()
  29.             end
  30.         elseif action == "r" then
  31.             if i == 1 then
  32.                 turtle.turnRight()
  33.             end
  34.             turtle.forward()
  35.             if i==repetitionNumber then
  36.                 turtle.turnLeft()
  37.             end
  38.         elseif action == "u" then
  39.             turtle.up()
  40.         elseif action == "d" then
  41.             turtle.down()
  42.            
  43.         elseif action == "tl" then
  44.             turtle.turnLeft()
  45.         elseif action == "tr" then
  46.             turtle.turnRight()
  47.        
  48.         else
  49.             terminate(action .. " not a valid action. terminating...")     
  50.         end
  51.     end
  52. end
  53.  
  54. function split(command)
  55.     local repetition = string.match(command, "%d*")
  56.     if repetition == "" then
  57.         repetition = 1
  58.     end
  59.    
  60.     local action = string.match(command, "%a+")
  61.     if action == "" then
  62.         terminate(command .. " not a valid action. terminating...")    
  63.     end
  64.    
  65.     return tonumber(repetition), action
  66. end
  67.  
  68. local args = {...}
  69. for i, command in ipairs(args) do
  70.     local repetitionNumber, action = split(command)
  71.     execute(action, repetitionNumber)
  72. end
Advertisement
Add Comment
Please, Sign In to add comment