Advertisement
Soapy

LevelAreaExcavate

Nov 30th, 2013
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs ~= 2 then
  3. print( "Width and Length needed." )
  4. return
  5. end
  6.  
  7. -- length of area
  8. local xSize = tonumber( tArgs[1] )
  9. -- Width of area
  10. local zSize = tonumber( tArgs[2] )
  11. if xSize < 1 then
  12. print( "Width can not be negative" )
  13. return
  14. end
  15.  
  16. if zSize < 1 then
  17. print( "Length can not be negative" )
  18. return
  19. end
  20.  
  21. local depth = 0
  22. local unloaded = 0
  23. local collected = 0
  24.  
  25. local xPos,zPos = 0,0
  26. local xDir,zDir = 0,1
  27.  
  28. local goTo -- Filled in further down
  29. local refuel -- Filled in further down
  30.  
  31. local function unload( _bKeepOneFuelStack )
  32. print( "Unloading items..." )
  33. for n=1,16 do
  34. local nCount = turtle.getItemCount(n)
  35. if nCount > 0 then
  36. turtle.select(n)
  37. local bDrop = true
  38. if _bKeepOneFuelStack and turtle.refuel(0) then
  39. bDrop = false
  40. _bKeepOneFuelStack = false
  41. end
  42. if bDrop then
  43. turtle.drop()
  44. unloaded = unloaded + nCount
  45. end
  46. end
  47. end
  48. collected = 0
  49. turtle.select(1)
  50. end
  51.  
  52. local function returnSupplies()
  53. local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  54. print( "Returning to surface..." )
  55. goTo( 0,0,0,0,-1 )
  56.  
  57. local fuelNeeded = 2*(x+y+z) + 1
  58. if not refuel( fuelNeeded ) then
  59. unload( true )
  60. print( "Waiting for fuel" )
  61. while not refuel( fuelNeeded ) do
  62. sleep(1)
  63. end
  64. else
  65. unload( true )
  66. end
  67.  
  68. print( "Resuming mining..." )
  69. goTo( x,y,z,xd,zd )
  70. end
  71.  
  72. local function collect()
  73. local bFull = true
  74. local nTotalItems = 0
  75. for n=1,16 do
  76. local nCount = turtle.getItemCount(n)
  77. if nCount == 0 then
  78. bFull = false
  79. end
  80. nTotalItems = nTotalItems + nCount
  81. end
  82.  
  83. if nTotalItems > collected then
  84. collected = nTotalItems
  85. if math.fmod(collected + unloaded, 50) == 0 then
  86. print( "Mined "..(collected + unloaded).." items." )
  87. end
  88. end
  89.  
  90. if bFull then
  91. print( "No empty slots left." )
  92. return false
  93. end
  94. return true
  95. end
  96.  
  97. function refuel( ammount )
  98. local fuelLevel = turtle.getFuelLevel()
  99. if fuelLevel == "unlimited" then
  100. return true
  101. end
  102.  
  103. local needed = ammount or (xPos + zPos + depth + 1)
  104. if turtle.getFuelLevel() < needed then
  105. local fueled = false
  106. for n=1,16 do
  107. if turtle.getItemCount(n) > 0 then
  108. turtle.select(n)
  109. if turtle.refuel(1) then
  110. while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  111. turtle.refuel(1)
  112. end
  113. if turtle.getFuelLevel() >= needed then
  114. turtle.select(1)
  115. return true
  116. end
  117. end
  118. end
  119. end
  120. turtle.select(1)
  121. return false
  122. end
  123.  
  124. return true
  125. end
  126.  
  127. -- The aim of this function is that the turtle detects a block above it, and digs the block if there is one. if there is no block it should go upwards to detect if there was just a hole and there are blocks above the hole.
  128. -- This should be done for two blocks over the last one that was dug. if there are no further blocks, the turtle should go to the position it was before it dug up the first time.
  129. local function upwards()
  130. local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  131. local depth = 0
  132. if not refuel() then
  133. print( "Not enough Fuel" )
  134. returnSupplies()
  135. end
  136. while not turtle.up() do
  137. if turtle.detectUp() then
  138. if turtle.digUp() then
  139. depth = depth -1
  140. if not collect() then
  141. returnSupplies()
  142. end
  143. -- else
  144. -- return false
  145. end
  146. elseif turtle.attackUp() then
  147. if not collect() then
  148. returnSupplies()
  149. end
  150. else turtle.up()
  151. if turtle.detectUp() then
  152. if turtle.digUp() then
  153. depth = depth -1
  154. if not collect() then
  155. returnSupplies()
  156. end
  157. -- else
  158. -- return false
  159. end
  160. elseif turtle.attackUp() then
  161. if not collect() then
  162. returnSupplies()
  163. end
  164. else turtle.up()
  165. if turtle.detectUp() then
  166. if turtle.digUp() then
  167. depth = depth -1
  168. if not collect() then
  169. returnSupplies()
  170. end
  171. -- else
  172. -- return false
  173. end
  174. else goTo( x,0,z,xd,zd )
  175. end
  176. end
  177. end
  178.  
  179. end
  180. goTo( x,0,z,xd,zd )
  181. return true
  182. end
  183.  
  184. -- The aim of this function is that the turtle goes one forward, digs, then uses the upward() function, goes one block forward, digs, etc...
  185.  
  186. local function tryForwards()
  187. if not refuel() then
  188. print( "Not enough Fuel" )
  189. returnSupplies()
  190. end
  191.  
  192. while not turtle.forward() do
  193. if turtle.detect() then
  194. if turtle.dig() then
  195. turtle.forward()
  196. upwards()
  197. end
  198. elseif turtle.attack() then
  199. upwards()
  200. if not collect() then
  201. returnSupplies()
  202. end
  203. else
  204. sleep( 0.5 )
  205. end
  206. end
  207.  
  208. xPos = xPos + xDir
  209. zPos = zPos + zDir
  210. return true
  211. end
  212.  
  213.  
  214. local function turnLeft()
  215. turtle.turnLeft()
  216. xDir, zDir = -zDir, xDir
  217. end
  218.  
  219. local function turnRight()
  220. turtle.turnRight()
  221. xDir, zDir = zDir, -xDir
  222. end
  223.  
  224. function goTo( x, y, z, xd, zd )
  225. while depth > y do
  226. if turtle.up() then
  227. depth = depth - 1
  228. elseif turtle.digUp() or turtle.attackUp() then
  229. collect()
  230. else
  231. sleep( 0.5 )
  232. end
  233. end
  234.  
  235. if xPos > x then
  236. while xDir ~= -1 do
  237. turnLeft()
  238. end
  239. while xPos > x do
  240. if turtle.forward() then
  241. xPos = xPos - 1
  242. elseif turtle.dig() or turtle.attack() then
  243. collect()
  244. else
  245. sleep( 0.5 )
  246. end
  247. end
  248. elseif xPos < x then
  249. while xDir ~= 1 do
  250. turnLeft()
  251. end
  252. while xPos < x do
  253. if turtle.forward() then
  254. xPos = xPos + 1
  255. elseif turtle.dig() or turtle.attack() then
  256. collect()
  257. else
  258. sleep( 0.5 )
  259. end
  260. end
  261. end
  262.  
  263. if zPos > z then
  264. while zDir ~= -1 do
  265. turnLeft()
  266. end
  267. while zPos > z do
  268. if turtle.forward() then
  269. zPos = zPos - 1
  270. elseif turtle.dig() or turtle.attack() then
  271. collect()
  272. else
  273. sleep( 0.5 )
  274. end
  275. end
  276. elseif zPos < z then
  277. while zDir ~= 1 do
  278. turnLeft()
  279. end
  280. while zPos < z do
  281. if turtle.forward() then
  282. zPos = zPos + 1
  283. elseif turtle.dig() or turtle.attack() then
  284. collect()
  285. else
  286. sleep( 0.5 )
  287. end
  288. end
  289. end
  290.  
  291. while depth < y do
  292. if turtle.down() then
  293. depth = depth + 1
  294. elseif turtle.digDown() or turtle.attackDown() then
  295. collect()
  296. else
  297. sleep( 0.5 )
  298. end
  299. end
  300.  
  301. while zDir ~= zd or xDir ~= xd do
  302. turnLeft()
  303. end
  304. end
  305.  
  306. if not refuel() then
  307. print( "Out of Fuel" )
  308. return
  309. end
  310.  
  311. print( "Beggining to level the given area." )
  312.  
  313. -- local reseal = false
  314. -- turtle.select(1)
  315. -- if turtle.digDown() then
  316. -- reseal = true
  317. -- end
  318.  
  319.  
  320. -- this function is slightly (and wrongly) modified from the original excavate program. The turtle shuold dig go one block forward from the start, detect if there are blocks above (the described upwards() function) etc,
  321. -- then come down, go one step further, etc. The are can be chosen in length and width, and when there are no blocks in front of the turtle anymore, it stays in this area.
  322. --when there are blocks in front of the turtle it digs, goes one forward and one up. this repeats until there is no block in front of the turtle.
  323. local alternate = 0
  324. local done = false
  325. while not done do
  326. for n=1,xSize do
  327. for m=1,zSize-1 do
  328. if not tryForwards() then
  329. done = true
  330. break
  331. end
  332. end
  333. if done then
  334. break
  335. end
  336. if n<xSize then
  337. if math.fmod(n + alternate,2) == 0 then
  338. turnLeft()
  339. if not tryForwards() then
  340. done = true
  341. break
  342. end
  343. turnLeft()
  344. else
  345. turnRight()
  346. if not tryForwards() then
  347. done = true
  348. break
  349. end
  350. turnRight()
  351. end
  352. end
  353. end
  354. if done then
  355. break
  356. end
  357.  
  358. if xSize > 1 then
  359. if math.fmod(xSize,2) == 0 then
  360. turnRight()
  361. else
  362. if alternate == 0 then
  363. turnLeft()
  364. else
  365. turnRight()
  366. end
  367. alternate = 1 - alternate
  368. end
  369. end
  370.  
  371.  
  372. end
  373.  
  374. print( "Returning to surface..." )
  375.  
  376. -- Return to where we started
  377. goTo( 0,0,0,0,-1 )
  378. unload( false )
  379. goTo( 0,0,0,0,1 )
  380.  
  381. -- Seal the hole
  382. -- if reseal then
  383. -- turtle.placeDown()
  384. -- end
  385.  
  386. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement