Advertisement
NickM13

[ComputerCraft] Efficient Excavate

Jun 4th, 2015
1,545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. --Programmed by Nick Mead(NickM13)
  2.  
  3. dimension = {}
  4. coordinate = {}
  5. rotation = 0
  6. back = false
  7. blocksMined = 0
  8. minimumFuel = 40
  9. finish = false
  10. name = nil
  11.  
  12. term.clear()
  13. term.setCursorPos(1, 1)
  14. print("Enter dimensions you would like in the form x z y, eg: 5 5 2")
  15. print("")
  16. print("Height of the area is multiplied by 3 for efficiency")
  17. print("")
  18. print("Entering only x and z will assume you want to dig to the bottom of the map.")
  19. print("")
  20. io.write("Size: ")
  21. input = io.read()
  22.  
  23. args = {}
  24. i=0
  25. for s in input:gmatch("%S+") do
  26. i=i+1
  27. args[i] = s
  28. end
  29.  
  30. local function setup()
  31. dimension.x = tonumber(args[1])
  32. dimension.z = tonumber(args[2])
  33. if #args == 3 then
  34. dimension.y = tonumber(args[3])
  35. else
  36. dimension.y = 256
  37. end
  38. term.clear()
  39. term.setCursorPos(1, 1)
  40. print("Width(Right): "..dimension.x.."\nLength(Forward): "..dimension.z.."\nDepth(Down): "..dimension.y * 3)
  41. coordinate.x = 0
  42. coordinate.y = 0
  43. coordinate.z = 0
  44. print("\nAll fuel will be input at start")
  45. print("Press the any key to begin")
  46. os.pullEvent("key")
  47. if turtle.getItemCount(2) ~= 0 then
  48. turtle.select(2)
  49. if turtle.refuel() then
  50. print("Fuel found")
  51. else
  52. print("No fuel found")
  53. end
  54. end
  55. print("Starting fuel: "..turtle.getFuelLevel().." units")
  56. turtle.select(1)
  57. end
  58.  
  59. local function deposit()
  60. slot = 0
  61. for i=1, 16 do
  62. name = turtle.getItemDetail(i)
  63. if name ~= nil and name.name == "minecraft:chest" then
  64. slot = i
  65. break
  66. end
  67. end
  68. if slot == 0 then return false end
  69. turtle.select(slot)
  70. if not turtle.placeUp() then
  71. if not turtle.placeDown() then
  72. print("no place for chest")
  73. return false
  74. else chestdir = "down" end
  75. else chestdir = "up" end
  76. for i=1, 16 do
  77. name = turtle.getItemDetail(i)
  78. if name ~= nil and name.name ~= "minecraft:chest" then
  79. turtle.select(i)
  80. if chestdir == "up" then turtle.dropUp()
  81. else turtle.dropDown() end
  82. end
  83. end
  84. return true
  85. end
  86.  
  87. local function dig(dir)
  88. local buffer = 5
  89. if dir == "up" then
  90. os.sleep(0.5)
  91. while turtle.detectUp() do
  92. if not turtle.digUp() then buffer = buffer - 1 if buffer <= 0 then return false end else
  93. buffer = 5
  94. blocksMined = blocksMined + 1
  95. end
  96. os.sleep(0.5)
  97. end
  98. elseif dir == "front" or dir == "forward" then
  99. while turtle.detect() do
  100. if not turtle.dig() then buffer = buffer - 1 if buffer <= 0 then return false end else
  101. buffer = 5
  102. end
  103. blocksMined = blocksMined + 1
  104. os.sleep(0.1)
  105. end
  106. elseif dir == "down" then
  107. while turtle.detectDown() do
  108. if not turtle.digDown() then buffer = buffer - 1 if buffer <= 0 then return false end else
  109. buffer = 5
  110. end
  111. blocksMined = blocksMined + 1
  112. end
  113. else
  114. error("dig error")
  115. end
  116. return true
  117. end
  118.  
  119. local function refuel()
  120. for i=1, 16 do
  121. name = turtle.getItemDetail(i)
  122. if name ~= nil and name.name == "minecraft:coal" then
  123. turtle.select(i)
  124. turtle.refuel(1)
  125. return true
  126. end
  127. end
  128. return false
  129. end
  130.  
  131. local function move(dir)
  132. local moved = false
  133. if turtle.getFuelLevel() < minimumFuel then
  134. print("Low fuel: "..turtle.getFuelLevel())
  135. refuel()
  136. end
  137. while turtle.getFuelLevel() == 0 do
  138. print("Out of fuel")
  139. repeat until (refuel())
  140. end
  141. turtle.select(1)
  142. if dir == "forward" then
  143. while not moved do
  144. if turtle.forward() then
  145. moved = true
  146. if rotation == 0 then
  147. coordinate.x = coordinate.x + 1
  148. elseif rotation == 1 then
  149. coordinate.z = coordinate.z + 1
  150. elseif rotation == 2 then
  151. coordinate.x = coordinate.x - 1
  152. elseif rotation == 3 then
  153. coordinate.z = coordinate.z - 1
  154. else
  155. error("rotation error in move function")
  156. end
  157. end
  158. if not moved then
  159. if turtle.detect() then if not dig"front" then finish = true return end end
  160. end
  161. end
  162. elseif dir == "up" then
  163. while not moved do
  164. if turtle.up() then
  165. moved = true
  166. coordinate.y = coordinate.y + 1
  167. end
  168. if not moved then
  169. if turtle.detectUp() then if not dig"up" then finish = true return end end
  170. end
  171. end
  172. elseif dir == "down" then
  173. while not moved do
  174. if turtle.down() then
  175. moved = true
  176. coordinate.y = coordinate.y - 1
  177. end
  178. if not moved then
  179. if turtle.detectDown() then if not dig"down" then finish = true return end end
  180. end
  181. end
  182. else
  183. error("move error")
  184. end
  185. return moved
  186. end
  187.  
  188. local function turn(dir)
  189. if dir == "right" then
  190. turtle.turnRight()
  191. rotation = rotation + 1
  192. elseif dir == "left" then
  193. turtle.turnLeft()
  194. rotation = rotation - 1
  195. else
  196. error("turn error")
  197. end
  198. rotation = rotation%4
  199. end
  200.  
  201. local function chunk()
  202. if turtle.getSelectedSlot() ~= 1 then
  203. turtle.select(1)
  204. end
  205. if (not dig"front" and turtle.detect()) then finish = true return end
  206. move"forward"
  207. if (not dig"down" and turtle.detectDown()) then finish = true return end
  208. if (not dig"up" and turtle.detectUp()) then finish = true return end
  209. if turtle.getItemCount(16) ~= 0 then
  210. deposit()
  211. end
  212. end
  213.  
  214. local function recall()
  215. local c = 1
  216. while (c < 4 and coordinate.y < 0) do
  217. move"up"
  218. c = c + 1
  219. end
  220. while rotation ~= 2 do turn"right" end
  221. while coordinate.x > 0 do
  222. move"forward"
  223. end
  224. while rotation ~= 3 do turn"right" end
  225. while coordinate.z > 0 do
  226. move"forward"
  227. end
  228. while rotation ~= 0 do turn"right" end
  229. while coordinate.y < 0 do
  230. move"up"
  231. end
  232. end
  233.  
  234. setup()
  235.  
  236. dig"up"
  237. dig"down"
  238.  
  239. for y=1, dimension.y do
  240. for z=1, dimension.z, 1 do
  241. for x=1, dimension.x-1, 1 do
  242. chunk()
  243. if finish then break end
  244. end
  245. if finish then break end
  246. if z == dimension.z then break end
  247. if back then
  248. turn"left"
  249. chunk()
  250. turn"left"
  251. else
  252. turn"right"
  253. chunk()
  254. turn"right"
  255. end
  256. back = not back
  257. end
  258. if finish then break end
  259. if y < dimension.y then
  260. for j=1, 3, 1 do
  261. dig"down"
  262. move"down"
  263. end
  264. dig"down"
  265. turn"right"
  266. turn"right"
  267. end
  268. end
  269.  
  270. deposit()
  271.  
  272. recall()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement