Advertisement
asweigart

Untitled

Mar 6th, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 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. local tArgs = {...}
  8. if #tArgs < 1 then
  9. print('Usage: do <cmd> [<repeat>] [more]')
  10. print(' Commands are:')
  11. print(' f, b, up, dn, l, r - move')
  12. print(' d, du, dd - dig (up, down)')
  13. print(' i, iu, id - inspect (up, down)')
  14. print(' sel <num> - select inv num')
  15. print(' s, su, sd - suck items (up, down)')
  16. print(' dr, dru, drd - drop (up, down)')
  17. print(' p, pu, pd - place (up, down)')
  18. print('Example: do r f 2')
  19. print('(Turn right, move forward twice.)')
  20. return
  21. end
  22.  
  23. local i, j, k, v
  24.  
  25. local function outOfFuel()
  26. if turtle.getFuelLevel() == 0 then
  27. print('Out of fuel!')
  28. return true
  29. else
  30. return false
  31. end
  32. end
  33.  
  34. local function inspectAndPrintResults(direction)
  35. local success, inspectResults, icmd
  36. if direction == '' then
  37. success, inspectResults = turtle.inspect()
  38. elseif direction == 'down' then
  39. success, inspectResults = turtle.inspectDown()
  40. elseif direction == 'up' then
  41. success, inspectResults = turtle.inspect()
  42. end
  43.  
  44. if direction == '' then
  45. icmd = 'inspect '
  46. elseif direction == 'down' then
  47. icmd = 'inspectDown '
  48. elseif direction == 'up' then
  49. icmd = 'inspectUp '
  50. end
  51.  
  52. if not success then
  53. print(icmd .. ': false')
  54. else
  55. print(icmd .. ': true')
  56. for k, v in pairs(inspectResults) do
  57. print(k, '=', v)
  58. end
  59. io.write('\n')
  60. end
  61. end
  62.  
  63. -- main program
  64. for i = 1,#tArgs do
  65. cmd = tArgs[i] -- get the command
  66. if #tArgs < i + 1 then
  67. reps = 1 -- end of cmdline args, so set this to 1
  68. else
  69. if tonumber(tArgs[i+1]) ~= nil then -- check if next arg is numeric
  70. reps = tonumber(tArgs[i+1]) -- set
  71. else
  72. -- "reps" is actually the next command, so set it to 1
  73. reps = 1
  74. end
  75. end
  76. if tArgs[i] == 'f' then
  77. for j = 1,reps do
  78. success, errMsg = turtle.forward()
  79. print('forward: ' .. tostring(success) .. ' ' .. errMsg)
  80. if not success then return success, errMsg end
  81. end
  82. elseif tArgs[i] == 'b' then
  83. for j = 1,reps do
  84. success, errMsg = turtle.back()
  85. print('back: ' .. tostring(success) .. ' ' .. errMsg)
  86. if not success then return success, errMsg end
  87.  
  88. end
  89. elseif tArgs[i] == 'l' then
  90. for j = 1,reps do
  91. print('left: ' .. tostring(turtle.turnLeft()))
  92. end
  93. elseif tArgs[i] == 'r' then
  94. for j = 1,reps do
  95. print('right: ' .. tostring(turtle.turnRight()))
  96. end
  97. elseif tArgs[i] == 'up' then
  98. for j = 1,reps do
  99. success, errMsg = turtle.up()
  100. print('up: ' .. tostring(success) .. ' ' .. errMsg)
  101. if not success then return success, errMsg end
  102. end
  103. elseif tArgs[i] == 'dn' then
  104. for j = 1,reps do
  105. success, errMsg = turtle.down()
  106. print('down: ' .. tostring(success) .. ' ' .. errMsg)
  107. if not success then return success, errMsg end
  108. end
  109. elseif tArgs[i] == 'd' then
  110. for j = 1,reps do
  111. success, errMsg = turtle.dig()
  112. print('dig: ' .. tostring(success) .. ' ' .. errMsg)
  113. if not success then return success, errMsg end
  114. end
  115. elseif tArgs[i] == 'du' then
  116. for j = 1,reps do
  117. success, errMsg = turtle.digUp()
  118. print('digUp: ' .. tostring(success) .. ' ' .. errMsg)
  119. if not success then return success, errMsg end
  120. end
  121. elseif tArgs[i] == 'dd' then
  122. for j = 1,reps do
  123. success, errMsg = turtle.digDown()
  124. print('digDown: ' .. tostring(success) .. ' ' .. errMsg)
  125. if not success then return success, errMsg end
  126. end
  127. elseif tArgs[i] == 'i' then
  128. for j = 1,reps do
  129. inspectAndPrintResults('')
  130. end
  131. elseif tArgs[i] == 'iu' then
  132. for j = 1,reps do
  133. inspectAndPrintResults('up')
  134. end
  135. elseif tArgs[i] == 'id' then
  136. for j = 1,reps do
  137. inspectAndPrintResults('down')
  138. end
  139. elseif tArgs[i] == 'sel' then
  140. -- in this case, reps is the inventory number
  141. print('select ' .. reps .. ': ' .. tostring(turtle.select(reps)))
  142. elseif tArgs[i] == 'item' then
  143. -- display info about the item stack
  144. local itemData = turtle.getItemDetail()
  145. if itemData ~= nil then
  146. for k, v in pairs(itemDetail) do
  147. print(k, '=', v)
  148. end
  149. else
  150. print('No item at slot #' .. tostring(turtle.getSelectedSlot()))
  151. end
  152. elseif tArgs[i] == 's' then
  153. for j = 1,reps do
  154. print('suck: ' .. tostring(turtle.suck()))
  155. end
  156. elseif tArgs[i] == 'su' then
  157. for j = 1,reps do
  158. print('suckUp: ' .. tostring(turtle.suckUp()))
  159. end
  160. elseif tArgs[i] == 'sd' then
  161. for j = 1,reps do
  162. print('suckDown: ' .. tostring(turtle.suckDown()))
  163. end
  164. elseif tArgs[i] == 'p' then
  165. for j = 1,reps do
  166. print('place: ' .. tostring(turtle.place()))
  167. end
  168. elseif tArgs[i] == 'pu' then
  169. for j = 1,reps do
  170. print('placeUp: ' .. tostring(turtle.placeUp()))
  171. end
  172. elseif tArgs[i] == 'pd' then
  173. for j = 1,reps do
  174. print('placeDown: ' .. tostring(turtle.placeDown()))
  175. end
  176. elseif tArgs[i] == 'dr' then
  177. for j = 1,reps do
  178. print('drop: ' .. tostring(turtle.drop()))
  179. end
  180. elseif tArgs[i] == 'dru' then
  181. for j = 1,reps do
  182. print('dropUp: ' .. tostring(turtle.dropUp()))
  183. end
  184. elseif tArgs[i] == 'drd' then
  185. for j = 1,reps do
  186. print('dropDown: ' .. tostring(turtle.dropDown()))
  187. end
  188. end
  189. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement