Advertisement
asweigart

do.lua

Mar 18th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. -- This is a ComputerCraft script to easily run turtle API commands from the turtle's command line.
  2. -- Written by al@inventwithpython.com
  3. -- Find my other scripts at https://github.com/asweigart/al-computercraft
  4. -- or http://turtlescripts.com/profile/AlSweigart
  5.  
  6.  
  7.  
  8. local VALID_ACTIONS = {'f', 'forward', 'b', 'back', 'l', 'left', 'r', 'right', 'up', 'dn', 'down', 'fn', 'fs', 'fe', 'fw', 'facenorth', 'facesouth', 'faceeast', 'facewest', 'no', 'north', 'so', 'south', 'ea', 'east', 'we', 'west', 'd', 'dig', 'du', 'digup', 'dd', 'digdown', 'i', 'inspect', 'iu', 'inspectup', 'id', 'inspectdown', 'sel', 'select', 's', 'suck', 'su', 'suckup', 'sd', 'suckdown', 'p', 'place', 'pu', 'placeup', 'pd', 'placedown', 'dr', 'drop', 'dru', 'dropup', 'drd', 'dropdown'}
  9.  
  10.  
  11. local tArgs = {...}
  12. if #tArgs < 1 then
  13. print('Usage: do <cmd> [<repeat>] [more]')
  14. print(' Commands are:')
  15. print(' f, b, up, dn, l, r - move')
  16. print(' d, du, dd - dig (up, down)')
  17. print(' i, iu, id - inspect (up, down)')
  18. print(' sel <num> - select inv num')
  19. print(' s, su, sd - suck items (up, down)')
  20. print(' dr, dru, drd - drop (up, down)')
  21. print(' p, pu, pd - place (up, down)')
  22. print('Example: do r f 2')
  23. print('(Turn right, move forward twice.)')
  24. return
  25. end
  26.  
  27.  
  28.  
  29.  
  30. function table.contains(table, element)
  31. for _, value in pairs(table) do
  32. if value == element then
  33. return true
  34. end
  35. end
  36. return false
  37. end
  38.  
  39.  
  40. function split(str)
  41. -- splits a string up into an array of strings
  42. str = string.lower(str)
  43. local actions = {}
  44. local word
  45. for word in str:gmatch("%w+") do table.insert(actions, word) end
  46. return actions
  47. end
  48.  
  49.  
  50.  
  51.  
  52. function isValidActionString(actionStr)
  53. local actions = split(actionStr)
  54. local expectCmd = true -- commands are expected after other commands or rep numbers or at the start
  55. for i=1,#actions do
  56. if expectCmd and not table.contains(VALID_ACTIONS, actions[i]) then
  57. return false, 'Expected a command but found "' .. tostring(actions[i]) .. '"'
  58. end
  59.  
  60. if not expectCmd and (not table.contains(VALID_ACTIONS, actions[i]) and tonumber(actions[i]) == nil) then
  61. return false, 'Expected a command or reps number but found "' .. tostring(actions[i]) .. '"'
  62. end
  63.  
  64. if expectCmd then
  65. expectCmd = false -- next action can be a command OR reps number, it doesn't HAVE to be a command
  66. end
  67.  
  68. if tonumber(actions[i]) == nil then
  69. -- the command was a reps number, so the next action MUST be a command
  70. expectCmd = true
  71. end
  72. end
  73. return true
  74. end
  75.  
  76.  
  77.  
  78. function doActions(actionsStr, safeMode)
  79. --[[
  80. Complete list of commands:
  81. f - move forward
  82. b - move backward
  83.  
  84. ]]
  85. actionsStr = string.lower(actionsStr)
  86. local actions = split(actionsStr)
  87.  
  88. if safeMode == nil then safeMode = false end
  89.  
  90. local i, j, k, v, success, errMsg
  91.  
  92. if safeMode then -- check that there are no invalid commands
  93. success, errMsg = isValidActionString(actionsStr)
  94. if not success then return false, errMsg end
  95. end
  96.  
  97. for i = 1,#actions do
  98. cmd = actions[i] -- get the command
  99. if #actions < i + 1 then
  100. reps = 1 -- end of actions, so set this to 1
  101. else
  102. if tonumber(actions[i+1]) ~= nil then -- check if next arg is numeric
  103. reps = tonumber(actions[i+1]) -- set
  104. else
  105. -- "reps" is actually the next command, so set it to 1
  106. reps = 1
  107. end
  108. end
  109. if actions[i] == 'f' or actions[i] == 'forward' then
  110. for j=1,reps do
  111. success, errMsg = turtle.forward()
  112. if safeMode and not success then return success, errMsg end
  113. end
  114. elseif actions[i] == 'b' or actions[i] == 'back' then
  115. for j=1,reps do
  116. success, errMsg = turtle.back()
  117. if safeMode and not success then return success, errMsg end
  118. end
  119. elseif actions[i] == 'l' or actions[i] == 'left' then
  120. for j=1,reps do
  121. turtle.turnLeft()
  122. end
  123. elseif actions[i] == 'r' or actions[i] == 'right' then
  124. for j=1,reps do
  125. turtle.turnRight()
  126. end
  127. elseif actions[i] == 'up' then
  128. for j=1,reps do
  129. success, errMsg = turtle.up()
  130. if safeMode and not success then return success, errMsg end
  131. end
  132. elseif actions[i] == 'dn' or actions[i] == 'down' then
  133. for j=1,reps do
  134. success, errMsg = turtle.down()
  135. if safeMode and not success then return success, errMsg end
  136. end
  137. elseif actions[i] == 'd' or actions[i] == 'dig' then
  138. for j = 1,reps do
  139. success, errMsg = turtle.dig()
  140. --print('dig: ' .. tostring(success) .. ' ' .. errMsg)
  141. if safeMode and not success then return success, errMsg end
  142. end
  143. elseif actions[i] == 'du' or actions[i] == 'digup' then
  144. for j = 1,reps do
  145. success, errMsg = turtle.digUp()
  146. --print('digUp: ' .. tostring(success) .. ' ' .. errMsg)
  147. if safeMode and not success then return success, errMsg end
  148. end
  149. elseif actions[i] == 'dd' or actions[i] == 'digdown' then
  150. for j = 1,reps do
  151. success, errMsg = turtle.digDown()
  152. --print('digDown: ' .. tostring(success) .. ' ' .. errMsg)
  153. if safeMode and not success then return success, errMsg end
  154. end
  155. elseif actions[i] == 'i' or actions[i] == 'inspect' then
  156. for j = 1,reps do
  157. success, inspectResults = turtle.inspect()
  158. if safeMode and not success then return success, errMsg end
  159. end
  160. elseif actions[i] == 'iu' or actions[i] == 'inspectup' then
  161. for j = 1,reps do
  162. success, inspectResults = turtle.inspectUp()
  163. if safeMode and not success then return success, errMsg end
  164. end
  165. elseif actions[i] == 'id' or actions[i] == 'inspectdown' then
  166. for j = 1,reps do
  167. success, inspectResults = turtle.inspectDown()
  168. if safeMode and not success then return success, errMsg end
  169. end
  170. elseif actions[i] == 'sel' or actions[i] == 'select' then
  171. -- in this case, reps is the inventory number
  172. success, errMsg = turtle.select(reps)
  173. if safeMode and not success then return success, errMsg end
  174. elseif actions[i] == 's' or actions[i] == 'suck' then
  175. success, errMsg = turtle.suck(reps)
  176. if safeMode and not success then return success, errMsg end
  177. elseif actions[i] == 'su' or actions[i] == 'suckup' then
  178. success, errMsg = turtle.suckUp(reps)
  179. if safeMode and not success then return success, errMsg end
  180. elseif actions[i] == 'sd' or actions[i] == 'suckdown' then
  181. success, errMsg = turtle.suckDown(reps)
  182. if safeMode and not success then return success, errMsg end
  183. elseif actions[i] == 'p' or actions[i] == 'place' then
  184. for j = 1,reps do
  185. success, errMsg = turtle.place()
  186. if safeMode and not success then return success, errMsg end
  187. end
  188. elseif actions[i] == 'pu' or actions[i] == 'placeup' then
  189. for j = 1,reps do
  190. success, errMsg = turtle.placeUp()
  191. if safeMode and not success then return success, errMsg end
  192. end
  193. elseif actions[i] == 'pd' or actions[i] == 'placedown' then
  194. for j = 1,reps do
  195. success, errMsg = turtle.placeDown()
  196. if safeMode and not success then return success, errMsg end
  197. end
  198. elseif actions[i] == 'dr' or actions[i] == 'drop' then
  199. success, errMsg = turtle.drop(reps)
  200. if safeMode and not success then return success, errMsg end
  201. elseif actions[i] == 'dru' or actions[i] == 'dropup' then
  202. success, errMsg = turtle.dropUp(reps)
  203. if safeMode and not success then return success, errMsg end
  204. elseif actions[i] == 'drd' or actions[i] == 'dropdown' then
  205. success, errMsg = turtle.dropDown(reps)
  206. if safeMode and not success then return success, errMsg end
  207. end
  208. end
  209. end
  210.  
  211.  
  212. doActions(table.concat(tArgs, ' '))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement