thaile4ever

excavate2

Dec 26th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. local targs = { ... }
  2.  
  3. local size = tonumber ( targs[1] )
  4.  
  5. local depth = 0
  6.  
  7. if #targs == 2 then
  8. depth = tonumber(tArgs[2])
  9. else
  10. depth = tonumber(tArgs[3]) - tonumber(tArgs[2]))
  11. end
  12.  
  13. local xPos,zPos = 0,0
  14. local xDir,zDir = 0,1
  15.  
  16. local goTo -- Filled in further down
  17. local refuel -- Filled in further down
  18.  
  19. local function unload()
  20. print( "unloading items..." )
  21. for n=1, 16 do
  22. turtle.select(n)
  23. if (turtle.drop()) == false then
  24. return false
  25. end
  26. end
  27. turtle.select(n)
  28. return true
  29. end
  30.  
  31. local function returnSupplies()
  32. local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  33. print( "Returning to surface..." )
  34. goTo( 0,0,0,0,-1 )
  35.  
  36. -- Attempts to unloads
  37. local notStuck == false
  38. repeat
  39. local notStuck = unload()
  40. if notStuck == false then
  41. sleep(5)
  42. end
  43. until notStuck
  44.  
  45. local fuelNeeded = x+y+z + x+y+z + 5
  46. if not refuel( fuelNeeded ) then
  47.  
  48. print( "Waiting for fuel" )
  49. while not refuel( fuelNeeded ) do
  50. sleep(5)
  51. end
  52. end
  53.  
  54. print( "Resuming mining..." )
  55. goTo( x,y,z,xd,zd )
  56. end
  57.  
  58. local function collect()
  59. local bFull = true
  60. for n=1, 16 do
  61. local nCount = turtle.getItemCount(n)
  62. if nCount == 0 then
  63. bFull = false
  64. end
  65. end
  66.  
  67. if bFull then
  68. print("No empty slots left")
  69. return false
  70. end
  71. return true
  72. end
  73.  
  74. function refuel( ammount )
  75. local fuelLevel = turtle.getFuelLevel()
  76. if fuelLevel == "unlimited" then
  77. return true
  78. end
  79.  
  80. local needed = ammount or (xPos + zPos + depth + 1)
  81. if turtle.getFuelLevel() < needed then
  82. if turtle.refuel() then
  83. return true
  84. else
  85. return false
  86. end
  87. end
  88.  
  89. return true
  90. end
  91.  
  92. local function dig()
  93. if turtle.detectUp() then
  94. if turtle.digUp() then
  95. if not collect() then
  96. returnSupplies()
  97. end
  98. else
  99. sleep(1)
  100. end
  101. end
  102.  
  103. if turtle.detectDown() then
  104. if turtle.digDown() then
  105. if not collect() then
  106. returnSupplies()
  107. end
  108. else
  109. sleep(1)
  110. end
  111.  
  112.  
  113. end
  114.  
  115.  
  116. local function tryForwards()
  117. if not refuel() then
  118. print("Not enough Fuel")
  119. returnSupplies()
  120. end
  121.  
  122. while not turtle.forward() do
  123. if turtle.detect() then
  124. if turtle.dig() then
  125. if not collect() then
  126. returnSupplies()
  127. end
  128. else
  129. return false
  130. end
  131. elseif turtle.attack() then
  132. if not collect() then
  133. returnSupplies()
  134. end
  135. else
  136. sleep( 1 )
  137. end
  138. end
  139.  
  140. xPos = xPos + xDir
  141. zPos = zPos + zDir
  142. return true
  143. end
  144.  
  145. local function tryDown()
  146. if not refuel() then
  147. print( "Not enough Fuel" )
  148. returnSupplies()
  149. end
  150.  
  151. while not turtle.down() do
  152. if turtle.detectDown() then
  153. if turtle.digDown() then
  154. if not collect() then
  155. returnSupplies()
  156. end
  157. else
  158. return false
  159. end
  160. elseif turtle.attackDown() then
  161. if not collect() then
  162. returnSupplies()
  163. end
  164. else
  165. sleep( 1 )
  166. end
  167. end
  168.  
  169. depth = depth + 1
  170. return true
  171. end
  172.  
  173.  
  174. local function turnLeft()
  175. turtle.turnLeft()
  176. xDir, zDir = -zDir, xDir
  177. end
  178.  
  179. local function turnRight()
  180. turtle.turnRight()
  181. xDir, zDir = zDir, -xDir
  182. end
  183.  
  184. function goTo( x, y, z, xd, zd )
  185. while depth > y do
  186. if turtle.up() then
  187. depth = depth - 1
  188. elseif turtle.digUp() or turtle.attackUp() then
  189. collect()
  190. else
  191. sleep( 0.5 )
  192. end
  193. end
  194.  
  195. if xPos > x then
  196. while xDir ~= -1 do
  197. turnLeft()
  198. end
  199. while xPos > x do
  200. if turtle.forward() then
  201. xPos = xPos - 1
  202. elseif turtle.dig() or turtle.attack() then
  203. collect()
  204. else
  205. sleep( 0.5 )
  206. end
  207. end
  208. elseif xPos < x then
  209. while xDir ~= 1 do
  210. turnLeft()
  211. end
  212. while xPos < x do
  213. if turtle.forward() then
  214. xPos = xPos + 1
  215. elseif turtle.dig() or turtle.attack() then
  216. collect()
  217. else
  218. sleep( 0.5 )
  219. end
  220. end
  221. end
  222.  
  223. if zPos > z then
  224. while zDir ~= -1 do
  225. turnLeft()
  226. end
  227. while zPos > z do
  228. if turtle.forward() then
  229. zPos = zPos - 1
  230. elseif turtle.dig() or turtle.attack() then
  231. collect()
  232. else
  233. sleep( 0.5 )
  234. end
  235. end
  236. elseif zPos < z then
  237. while zDir ~= 1 do
  238. turnLeft()
  239. end
  240. while zPos < z do
  241. if turtle.forward() then
  242. zPos = zPos + 1
  243. elseif turtle.dig() or turtle.attack() then
  244. collect()
  245. else
  246. sleep( 0.5 )
  247. end
  248. end
  249. end
  250.  
  251. while depth < y do
  252. if turtle.down() then
  253. depth = depth + 1
  254. elseif turtle.digDown() or turtle.attackDown() then
  255. collect()
  256. else
  257. sleep( 0.5 )
  258. end
  259. end
  260.  
  261. while zDir ~= zd or xDir ~= xd do
  262. turnLeft()
  263. end
  264. end
  265.  
  266. -- Here is the start of the program really
  267.  
  268. if not refuel() then
  269. print( "Out of Fuel" )
  270. return
  271. end
  272.  
  273. print( "Excavating..." )
  274.  
  275.  
  276. local alternate = 0
  277. local done = false
  278.  
  279.  
  280. if depth > 0 then
  281. for n=0, depth do
  282. tryDown()
  283. end
  284. end
  285.  
  286. while not done do
  287. for n=1,size do
  288. for m=1,size-1 do
  289.  
  290. dig() -- dig top and bottom
  291.  
  292. if not tryForwards() then
  293. done = true
  294. break
  295. end
  296. end
  297. if done then
  298. break
  299. end
  300. if n<size then
  301. if math.fmod(n + alternate,2) == 0 then
  302. turnLeft()
  303. dig()
  304. if not tryForwards() then
  305. done = true
  306. break
  307. end
  308. dig()
  309. turnLeft()
  310. else
  311. turnRight()
  312. dig()
  313. if not tryForwards() then
  314. done = true
  315. break
  316. end
  317. dig()
  318. turnRight()
  319. end
  320. end
  321. end
  322. if done then
  323. break
  324. end
  325.  
  326. if size > 1 then
  327. if math.fmod(size,2) == 0 then
  328. turnRight()
  329. else
  330. if alternate == 0 then
  331. turnLeft()
  332. else
  333. turnRight()
  334. end
  335. alternate = 1 - alternate
  336. end
  337. end
  338. dig()
  339. tryDown()
  340. tryDown() -- we go down twice
  341.  
  342. if not tryDown() then
  343. done = true
  344. break
  345. end
  346. end
  347.  
  348. print( "Returning to surface..." )
  349.  
  350. -- Return to where we started
  351. goTo( 0,0,0,0,-1 )
  352. unload()
  353. goTo( 0,0,0,0,1 )
Advertisement
Add Comment
Please, Sign In to add comment