DrFair

Strip mine

Feb 16th, 2013
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. Args = {...}
  2. x,y = 0,0
  3. dir = { [1]="+X", [2]="+Y", [3]="-X", [4]="-Y" }
  4. nDir = 1
  5.  
  6. function printUsage()
  7. print("Usage:")
  8. print("strip <BLOCKS> <WIDE> <left/right>")
  9. print("Strip mines <BLOCKS> blocks infront and <WIDE> blocks to the side.")
  10. end
  11.  
  12. function printFail()
  13. print("Failed at a task.")
  14. end
  15.  
  16. function hasSpace()
  17. local space = false
  18. for i=1,16 do
  19. if turtle.getItemCount(i) == 0 then
  20. space = true
  21. end
  22. end
  23. return space
  24. end
  25.  
  26. function getFuel()
  27. return turtle.getFuelLevel()
  28. end
  29.  
  30. function hasFuel()
  31. if getFuel() == 0 then
  32. return false
  33. else
  34. return true
  35. end
  36. end
  37.  
  38. function dropOff()
  39. print("Dropping off items, fuel: "..getFuel())
  40. turtle.digDown()
  41. turtle.select(1)
  42. if turtle.placeDown() then
  43. for i=2,16 do
  44. turtle.select(i)
  45. turtle.dropDown()
  46. end
  47. turtle.select(1)
  48. turtle.digDown()
  49. end
  50. end
  51.  
  52. function goDirFor(bool)
  53. if bool then
  54. if dir[nDir] == "+X" then x = x + 1 elseif
  55. dir[nDir] == "-X" then x = x - 1 elseif
  56. dir[nDir] == "+Y" then y = y + 1 elseif
  57. dir[nDir] == "-Y" then y = y - 1 end
  58. else
  59. if dir[nDir] == "+X" then x = x - 1 elseif
  60. dir[nDir] == "-X" then x = x + 1 elseif
  61. dir[nDir] == "+Y" then y = y - 1 elseif
  62. dir[nDir] == "-Y" then y = y + 1 end
  63. end
  64. end
  65.  
  66. function mineFor()
  67. while turtle.detect() do
  68. turtle.dig()
  69. os.sleep(0.5)
  70. end
  71. if not hasSpace() then dropOff() end
  72. return true
  73. end
  74.  
  75. function mineUp()
  76. while detectUp() do
  77. turtle.digUp()
  78. os.sleep(0.5)
  79. end
  80. if not hasSpace() then dropOff() end
  81. return true
  82. end
  83.  
  84. function mineDown()
  85. turtle.digDown()
  86. if not hasSpace() then dropOff() end
  87. return true
  88. end
  89.  
  90. function goFor()
  91. if turtle.forward() then
  92. goDirFor(true)
  93. else
  94. return false
  95. end
  96. end
  97.  
  98. function goBack()
  99. if turtle.back() then
  100. goDirFor(false)
  101. else
  102. return false
  103. end
  104. end
  105.  
  106. function turnRight()
  107. turtle.turnRight()
  108. if nDir < 4 then
  109. nDir = nDir + 1
  110. else
  111. nDir = 1
  112. end
  113. end
  114.  
  115. function turnLeft()
  116. turtle.turnLeft()
  117. if nDir > 1 then
  118. nDir = nDir - 1
  119. else
  120. nDir = 4
  121. end
  122. end
  123.  
  124. function detectFor()
  125. return turtle.detect()
  126. end
  127.  
  128. function detectUp()
  129. return turtle.detectUp()
  130. end
  131.  
  132. function detectDown()
  133. return turtle.detectDown()
  134. end
  135.  
  136. function mine()
  137. mineFor()
  138. mineUp()
  139. mineDown()
  140. goFor()
  141. end
  142.  
  143. function stripFor()
  144. if dirFor then
  145. while x < blocksfor do
  146. mineFor()
  147. mineUp()
  148. mineDown()
  149. goFor()
  150. end
  151. else
  152. while x > 0 do
  153. mineFor()
  154. mineUp()
  155. mineDown()
  156. goFor()
  157. end
  158. end
  159. end
  160.  
  161. function resetPos()
  162. while dir[nDir] ~= "-X" do
  163. turnRight()
  164. end
  165. while x ~= 0 do
  166. goFor()
  167. end
  168. if y < 0 then
  169. while dir[nDir] ~= "+Y" do
  170. turnLeft()
  171. end
  172. else
  173. while dir[nDir] ~= "-Y" do
  174. turnLeft()
  175. end
  176. end
  177. while y ~= 0 do
  178. goFor()
  179. end
  180. if dir[nDir] == "+Y" then
  181. turnRight()
  182. elseif dir[nDir] == "-Y" then
  183. turnLeft()
  184. end
  185. end
  186.  
  187. function stripMine(num)
  188. for i=1,num do
  189. stripFor()
  190. if i < num then
  191. print("Turning around, fuel: "..getFuel())
  192. if boolTurnRight then
  193. turnRight()
  194. mine()
  195. turnRight()
  196. else
  197. turnLeft()
  198. mine()
  199. turnLeft()
  200. end
  201. boolTurnRight = not boolTurnRight
  202. dirFor = not dirFor
  203. end
  204. end
  205. mineUp()
  206. mineDown()
  207. print("Going back to start, fuel: "..getFuel())
  208. resetPos()
  209. dropOff()
  210. print("Job done, fuel: "..getFuel())
  211. end
  212.  
  213. if #Args == 0 then
  214. printUsage()
  215. else
  216. if tonumber(Args[1]) == 0 or tonumber(Args[2]) == 0 then
  217. printUsage()
  218. else
  219. if Args[3] == "right" or Args[3] == "left" then
  220. if Args[3] == "right" then
  221. boolTurnRight = true
  222. else
  223. boolTurnRight = false
  224. end
  225. blocksfor = tonumber(Args[1]) - 1
  226. dirFor = true
  227. stripMine(tonumber(Args[2]))
  228. end
  229. end
  230. end
Advertisement
Add Comment
Please, Sign In to add comment