Advertisement
asweigart

Untitled

Feb 17th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 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. end
  86. elseif tArgs[i] == 'r' then
  87. for j = 1,reps do
  88. print('right ' .. tostring(turtle.turnRight()))
  89. end
  90. elseif tArgs[i] == 'up' then
  91. for j = 1,reps do
  92. print('up ' .. tostring(turtle.up()))
  93. if outOfFuel() then return end
  94. end
  95. elseif tArgs[i] == 'dn' then
  96. for j = 1,reps do
  97. print('down ' .. tostring(turtle.down()))
  98. if outOfFuel() then return end
  99. end
  100. elseif tArgs[i] == 'd' then
  101. for j = 1,reps do
  102. print('dig ' .. tostring(turtle.dig()))
  103. end
  104. elseif tArgs[i] == 'du' then
  105. for j = 1,reps do
  106. print('digUp ' .. tostring(turtle.digUp()))
  107. end
  108. elseif tArgs[i] == 'dd' then
  109. for j = 1,reps do
  110. print('digDown ' .. tostring(turtle.digDown()))
  111. end
  112. elseif tArgs[i] == 'i' then
  113. for j = 1,reps do
  114. inspectAndPrintResults('')
  115. end
  116. elseif tArgs[i] == 'iu' then
  117. for j = 1,reps do
  118. inspectAndPrintResults('up')
  119. end
  120. elseif tArgs[i] == 'id' then
  121. for j = 1,reps do
  122. inspectAndPrintResults('down')
  123. end
  124. elseif tArgs[i] == 'sel' then
  125. -- in this case, reps is the inventory number
  126. print('select ' .. reps .. ' ' .. tostring(turtle.select(reps)))
  127. elseif tArgs[i] == 'item' then
  128. -- display info about the item stack
  129. local itemData = turtle.getItemDetail()
  130. if itemData ~= nil then
  131. for k, v in pairs(itemDetail) do
  132. print(k, '=', v)
  133. end
  134. else
  135. print('No item at slot #' .. tostring(turtle.getSelectedSlot()))
  136. end
  137. elseif tArgs[i] == 's' then
  138. for j = 1,reps do
  139. print('suck ' .. tostring(turtle.suck()))
  140. end
  141. elseif tArgs[i] == 'su' then
  142. for j = 1,reps do
  143. print('suckUp ' .. tostring(turtle.suckUp()))
  144. end
  145. elseif tArgs[i] == 'sd' then
  146. for j = 1,reps do
  147. print('suckDown ' .. tostring(turtle.suckDown()))
  148. end
  149. elseif tArgs[i] == 'p' then
  150. for j = 1,reps do
  151. print('place ' .. tostring(turtle.place()))
  152. end
  153. elseif tArgs[i] == 'pu' then
  154. for j = 1,reps do
  155. print('placeUp ' .. tostring(turtle.placeUp()))
  156. end
  157. elseif tArgs[i] == 'pd' then
  158. for j = 1,reps do
  159. print('placeDown ' .. tostring(turtle.placeDown()))
  160. end
  161. elseif tArgs[i] == 'dr' then
  162. for j = 1,reps do
  163. print('drop ' .. tostring(turtle.drop()))
  164. end
  165. elseif tArgs[i] == 'dru' then
  166. for j = 1,reps do
  167. print('dropUp ' .. tostring(turtle.dropUp()))
  168. end
  169. elseif tArgs[i] == 'drd' then
  170. for j = 1,reps do
  171. print('dropDown ' .. tostring(turtle.dropDown()))
  172. end
  173. end
  174. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement