DrFair

Turtle Strip chest

Jun 15th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 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 dropOff()
  31. print("Dropping off items, fuel: "..getFuel())
  32. oldx = x
  33. oldy = y
  34. oldDir = nDir
  35. goY(0)
  36. goPos(0,0)
  37. for i=1,16 do
  38. turtle.select(i)
  39. turtle.drop()
  40. end
  41. turtle.select(1)
  42. goPos(oldx,oldy)
  43. while oldDir ~= nDir do
  44. turnRight()
  45. end
  46. end
  47.  
  48. function goDirFor(bool)
  49. if bool then
  50. if dir[nDir] == "+X" then x = x + 1 elseif
  51. dir[nDir] == "-X" then x = x - 1 elseif
  52. dir[nDir] == "+Y" then y = y + 1 elseif
  53. dir[nDir] == "-Y" then y = y - 1 end
  54. else
  55. if dir[nDir] == "+X" then x = x - 1 elseif
  56. dir[nDir] == "-X" then x = x + 1 elseif
  57. dir[nDir] == "+Y" then y = y - 1 elseif
  58. dir[nDir] == "-Y" then y = y + 1 end
  59. end
  60. end
  61.  
  62. function mineFor()
  63. while turtle.detect() do
  64. turtle.dig()
  65. os.sleep(0.5)
  66. end
  67. if not hasSpace() then dropOff() end
  68. return true
  69. end
  70.  
  71. function mineUp()
  72. while turtle.detectUp() do
  73. turtle.digUp()
  74. os.sleep(0.5)
  75. end
  76. if not hasSpace() then dropOff() end
  77. return true
  78. end
  79.  
  80. function mineDown()
  81. turtle.digDown()
  82. if not hasSpace() then dropOff() end
  83. return true
  84. end
  85.  
  86. function goFor()
  87. if turtle.forward() then
  88. goDirFor(true)
  89. else
  90. return false
  91. end
  92. end
  93.  
  94. function goBack()
  95. if turtle.back() then
  96. goDirFor(false)
  97. else
  98. return false
  99. end
  100. end
  101.  
  102. function turnRight()
  103. turtle.turnRight()
  104. if nDir < 4 then
  105. nDir = nDir + 1
  106. else
  107. nDir = 1
  108. end
  109. end
  110.  
  111. function turnLeft()
  112. turtle.turnLeft()
  113. if nDir > 1 then
  114. nDir = nDir - 1
  115. else
  116. nDir = 4
  117. end
  118. end
  119.  
  120. function mine()
  121. mineFor()
  122. mineUp()
  123. mineDown()
  124. goFor()
  125. end
  126.  
  127. function stripFor()
  128. if dirFor then
  129. while x < blocksfor do
  130. mineFor()
  131. mineUp()
  132. mineDown()
  133. goFor()
  134. end
  135. else
  136. while x > 0 do
  137. mineFor()
  138. mineUp()
  139. mineDown()
  140. goFor()
  141. end
  142. end
  143. end
  144.  
  145. function goX(dx)
  146. if dx > x then
  147. while dir[nDir] ~= "+X" do
  148. turnRight()
  149. end
  150. else
  151. while dir[nDir] ~= "-X" do
  152. turnRight()
  153. end
  154. end
  155. while x ~= dx do
  156. goFor()
  157. end
  158. end
  159.  
  160. function goY(dy)
  161. if dy > y then
  162. while dir[nDir] ~= "+Y" do
  163. turnRight()
  164. end
  165. else
  166. while dir[nDir] ~= "-Y" do
  167. turnRight()
  168. end
  169. end
  170. while y ~= dy do
  171. goFor()
  172. end
  173. end
  174.  
  175. function goPos(dx,dy)
  176. if dx > x then
  177. while dir[nDir] ~= "+X" do
  178. turnRight()
  179. end
  180. else
  181. while dir[nDir] ~= "-X" do
  182. turnRight()
  183. end
  184. end
  185. while x ~= dx do
  186. goFor()
  187. end
  188. if dy > y then
  189. while dir[nDir] ~= "+Y" do
  190. turnRight()
  191. end
  192. else
  193. while dir[nDir] ~= "-Y" do
  194. turnRight()
  195. end
  196. end
  197. while y ~= dy do
  198. goFor()
  199. end
  200. while dir[nDir] ~= "-X" do
  201. turnRight()
  202. end
  203. end
  204.  
  205. function stripMine(num)
  206. for i=1,num do
  207. stripFor()
  208. if i < num then
  209. print("Turning around, fuel: "..getFuel())
  210. if boolTurnRight then
  211. turnRight()
  212. mine()
  213. turnRight()
  214. else
  215. turnLeft()
  216. mine()
  217. turnLeft()
  218. end
  219. boolTurnRight = not boolTurnRight
  220. dirFor = not dirFor
  221. end
  222. end
  223. mineUp()
  224. mineDown()
  225. print("Going back to start, fuel: "..getFuel())
  226. goPos(0,0)
  227. dropOff()
  228. print("Job done, fuel: "..getFuel())
  229. end
  230.  
  231. if #Args == 0 then
  232. printUsage()
  233. else
  234. if tonumber(Args[1]) == 0 or tonumber(Args[2]) == 0 then
  235. printUsage()
  236. else
  237. if Args[3] == "right" or Args[3] == "left" then
  238. if Args[3] == "right" then
  239. boolTurnRight = true
  240. else
  241. boolTurnRight = false
  242. end
  243. blocksfor = tonumber(Args[1]) - 1
  244. dirFor = true
  245. stripMine(tonumber(Args[2]))
  246. end
  247. end
  248. end
Advertisement
Add Comment
Please, Sign In to add comment