Advertisement
asweigart

act (book)

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