Advertisement
asweigart

Untitled

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