asweigart

act (latest, post-book)

Dec 19th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. --[[ "Act" program
  2. By Al Sweigart
  3. https://turtleappstore.com/users/AlSweigart
  4. Does various actions from shell. ]]
  5.  
  6. local slot, waitTime, cliArgs, actions
  7.  
  8. function doAction(action, verbose)
  9. -- TODO verbose mode?
  10. action = string.lower(action)
  11.  
  12. -- look at these strings for the action codes
  13. if action == 'f' then
  14. return turtle.forward()
  15. elseif action == 'b' then
  16. return turtle.back()
  17. elseif action == 'l' then
  18. return turtle.turnLeft()
  19. elseif action == 'r' then
  20. return turtle.turnRight()
  21. elseif action == 'up' then
  22. return turtle.up()
  23. elseif action == 'dn' then
  24. return turtle.down()
  25. elseif action == 'd' then
  26. return turtle.dig()
  27. elseif action == 'du' then
  28. return turtle.digUp()
  29. elseif action == 'dd' then
  30. return turtle.digDown()
  31. elseif action == 'i' then
  32. -- TODO
  33. return turtle.inspect()
  34. elseif action == 'iu' then
  35. -- TODO
  36. return turtle.inspectUp()
  37. elseif action == 'id' then
  38. -- TODO
  39. return turtle.inspectDown()
  40. elseif action == 's' then
  41. return turtle.suck()
  42. elseif action == 'sd' then
  43. return turtle.suckDown()
  44. elseif action == 'su' then
  45. return turtle.suckUp()
  46. elseif action == 'p' then
  47. return turtle.place()
  48. elseif action == 'pu' then
  49. return turtle.placeUp()
  50. elseif action == 'pd' then
  51. return turtle.placeDown()
  52. elseif action == 'dr' then
  53. return turtle.drop()
  54. elseif action == 'dru' then
  55. return turtle.dropUp()
  56. elseif action == 'drd' then
  57. return turtle.dropDown()
  58. elseif action == 'c' then
  59. return turtle.craft()
  60. elseif action == 'el' then
  61. return turtle.equipLeft()
  62. elseif action == 'er' then
  63. return turtle.equipRight()
  64. elseif action == 'a' then
  65. return turtle.attack()
  66. elseif action == 'au' then
  67. return turtle.attackUp()
  68. elseif action == 'ad' then
  69. return turtle.attackDown()
  70. elseif action == 'det' then
  71. return turtle.detect()
  72. elseif action == 'detup' then
  73. return turtle.detectUp()
  74. elseif action == 'detd' then
  75. return turtle.detectDown()
  76. elseif action == 'cmp' then
  77. return turtle.compare()
  78. elseif action == 'cmpu' then
  79. return turtle.compareUp()
  80. elseif action == 'cmpd' then
  81. return turtle.compareDown()
  82. elseif action == 'ref' then
  83. return turtle.refuel()
  84. elseif string.sub(action, 1, 3) == 'sel' then
  85. slot = string.sub(action, 4, 6)
  86. return turtle.select(tonumber(slot))
  87. elseif string.sub(action, 1, 4) == 'xfer' then
  88. --local from = tonnumber(string.sub())
  89. -- TODO - finish
  90. elseif action == 'rf' then
  91. return redstone.setOutput('front', not redstone.getOutput('front'))
  92. elseif action == 'rb' then
  93. return redstone.setOutput('back', not redstone.getOutput('back'))
  94. elseif action == 'rtop' then
  95. return redstone.setOutput('top', not redstone.getOutput('top'))
  96. elseif action == 'rbot' then
  97. return redstone.setOutput('bottom', not redstone.getOutput('bottom'))
  98. elseif action == 'rl' then
  99. return redstone.setOutput('left', not redstone.getOutput('left'))
  100. elseif action == 'rr' then
  101. return redstone.setOutput('right', not redstone.getOutput('right'))
  102. elseif string.sub(action, 1, 1) == 'w' then
  103. waitTime = tonumber(string.sub(action, 2))
  104. os.sleep(waitTime)
  105. return true
  106.  
  107. else
  108. -- not a recognized action
  109. return false, 'Not a recognized action.'
  110. end
  111. end
  112.  
  113.  
  114. function split(str)
  115. -- splits a string into an array of strings
  116. -- Example: 'a b c' -> {'a', 'b', 'c'}
  117. local result, word
  118.  
  119. result = {}
  120. -- Note: The gmatch() function is
  121. -- beyond the scope of this book.
  122. for word in str:gmatch("%w+") do
  123. table.insert(result, word)
  124. end
  125. return result
  126. end
  127.  
  128.  
  129. function doActions(actions, mode)
  130. -- Do a series of actions.
  131. -- actions is a string: "f r 3" will
  132. -- go forward, turn right 3 times.
  133. -- See doAction() for action codes.
  134. -- If mode=='safe', then stop
  135. -- if one action fails.
  136.  
  137. -- if mode=='error', a failure
  138. -- calls error()
  139. -- ('error' is default)
  140.  
  141. -- NOT IMPLEMENTED:
  142.  
  143. -- if mode=='repeat', then repeat
  144. -- the action until it returns true
  145. -- (with 1 sec interval pause)
  146.  
  147. -- if mode=='force', then continue
  148. -- even if the action failed
  149.  
  150. if mode == nil then
  151. mode = 'error'
  152. end
  153.  
  154. -- get array of strings from string
  155. actions = split(actions)
  156.  
  157. while #actions > 0 do
  158. -- check for rep count
  159. if tonumber(actions[2]) ~= nil then
  160. -- reduce the rep count by 1
  161. actions[2] = tonumber(actions[2]) - 1
  162. end
  163. if tonumber(actions[2]) == 0 then
  164. -- if the rep count is 0, remove it
  165. table.remove(actions, 2)
  166. end
  167.  
  168. -- do the action
  169. result1, result2 = doAction(actions[1])
  170.  
  171. if tonumber(actions[2]) == nil then
  172. -- if there's no rep count,
  173. -- remove this action
  174. table.remove(actions, 1)
  175. end
  176.  
  177. -- check for error message
  178. if type(result2) == 'string' and mode == 'safe' then
  179. -- action failed, return the error message
  180. return result2
  181. elseif type(result2) == 'string' and mode == 'error' then
  182. error(result2)
  183. end
  184. end
  185.  
  186. return 'ok' -- all actions done
  187. end
  188.  
  189. d = doActions
  190.  
  191.  
  192. -- display "usage" info
  193. cliArgs = {...}
  194. if cliArgs[1] == '?' then
  195. print('Usage: act [actions]')
  196. print('See source code for actions.')
  197. print('Example: "act l f 3 r b 2" will turn')
  198. print('left, go forward 3 times, turn right,')
  199. print('then move back 2 times.')
  200.  
  201. return
  202. elseif #cliArgs ~= 0 then
  203. actions = table.concat(cliArgs, ' ')
  204. print(doActions(actions))
  205. end
Add Comment
Please, Sign In to add comment