Advertisement
ceribia

tunnel

Jun 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. --------------------------------------------------------
  2. --Position Management
  3. --------------------------------------------------------
  4. local depth = 0
  5. local unloaded = 0
  6. local collected = 0
  7. local xPos,zPos = 0,0
  8. local xDir,zDir = 0,1
  9.  
  10. movements = {}
  11. movements["forward"] = {}
  12. movements["forward"]["detect"] = turtle.detect
  13. movements["forward"]["dig"] = turtle.dig
  14. movements["forward"]["move"] = turtle.forward
  15. movements["forward"]["attack"] = turtle.attack
  16.  
  17. movements["up"] = {}
  18. movements["up"]["detect"] = turtle.detectUp
  19. movements["up"]["dig"] = turtle.digUp
  20. movements["up"]["move"] = turtle.up
  21. movements["up"]["attack"] = turtle.attackUp
  22.  
  23. movements["down"] = {}
  24. movements["down"]["detect"] = turtle.detectDown
  25. movements["down"]["dig"] = turtle.digDown
  26. movements["down"]["move"] = turtle.down
  27. movements["down"]["attack"] = turtle.attackDown
  28.  
  29. local function move(direction)
  30. --print("Attempting to move with " .. direction)
  31. while not movements[direction].move() do
  32. if movements[direction].detect() then
  33. if movements[direction].dig() == false then
  34. error("Failed to dig block")
  35. end
  36. elseif movements[direction].attack() then
  37. -- Do nothing, next loop after attacking
  38. else
  39. sleep( 0.5 )
  40. end
  41. end
  42.  
  43. if direction == "forward" then
  44. xPos = xPos + xDir
  45. zPos = zPos + zDir
  46. elseif direction == "up" then
  47. depth = depth - 1
  48. elseif direction == "down" then
  49. depth = depth + 1
  50. end
  51. end
  52.  
  53. local function turnLeft()
  54. turtle.turnLeft()
  55. xDir, zDir = -zDir, xDir
  56. end
  57.  
  58. local function turnRight()
  59. turtle.turnRight()
  60. xDir, zDir = zDir, -xDir
  61. end
  62.  
  63. function alignXDir( xGoalDir )
  64. if xGoalDir == xDir then
  65. return
  66. elseif xGoalDir == zDir then
  67. turnRight()
  68. elseif xGoalDir == -zDir then
  69. turnLeft()
  70. else
  71. turnRight()
  72. turnRight()
  73. end
  74.  
  75. if xGoalDir ~= xDir then
  76. error("Failed to fix direction")
  77. end
  78. end
  79.  
  80. function alignZDir( zGoalDir )
  81. if zGoalDir == zDir then
  82. return
  83. elseif zGoalDir == xDir then
  84. turnLeft()
  85. elseif zGoalDir == -xDir then
  86. turnRight()
  87. else
  88. turnRight()
  89. turnRight()
  90. end
  91.  
  92. if zGoalDir ~= zDir then
  93. error("Failed to fix direction")
  94. end
  95. end
  96.  
  97. function goTo( x, y, z, atEachSqurare, xd, zd)
  98. --print( "GoTo: " .. x .. "," .. y .. "," .. z .. " From: " .. xPos .. "," .. depth .. "," .. zPos)
  99.  
  100. if atEachSqurare == nil then
  101. atEachSqurare = function() end
  102. end
  103.  
  104. while depth ~= y do
  105. -- print("Depth = " .. depth .. " | y = " .. y)
  106. if depth > y then
  107. move("up")
  108. elseif depth < y then
  109. move("down")
  110. end
  111. atEachSqurare()
  112. end
  113.  
  114. local xGoalDir = (x - xPos) / math.abs(x - xPos)
  115. while xPos ~= x do
  116. alignXDir(xGoalDir)
  117. move("forward")
  118. atEachSqurare()
  119. end
  120.  
  121. local zGoalDir = (z - zPos) / math.abs(z - zPos)
  122. while zPos ~= z do
  123. alignZDir(zGoalDir)
  124. move("forward")
  125. atEachSqurare()
  126. end
  127.  
  128. --Fix alignment
  129. if xd then
  130. alignXDir(xd)
  131. end
  132.  
  133. if zd then
  134. alignZDir(zd)
  135. end
  136. end
  137.  
  138. --------------------------------------------------------
  139. --Inventory Management
  140. --------------------------------------------------------
  141. local fuel = {}
  142. fuel['chest'] = 1
  143. fuel['material'] = 2
  144.  
  145. local wall = {}
  146. wall['chest'] = 3
  147. wall['material'] = 4
  148.  
  149. local torch = {}
  150. torch['chest'] = 5
  151. torch['material'] = 6
  152.  
  153. local foundation = {}
  154. foundation['chest'] = 7
  155. foundation['material'] = 8
  156.  
  157. local loot = {}
  158. loot['chest'] = 9
  159. loot['material'] = 10
  160. loot['lastItem'] = true
  161.  
  162. local firstSlot = loot['material']
  163.  
  164. local chestZDir = -1
  165. local chestXDir = 0
  166.  
  167. local preChestZDir;
  168. local preChestXDir;
  169.  
  170. local function placeChest(item)
  171. preChestXdir = xDir
  172. preChestZdir = zDir
  173. alignZDir(chestZDir)
  174. alignXDir(chestXDir)
  175.  
  176. if turtle.detect() then
  177. error("Block where chest should go")
  178. end
  179.  
  180. turtle.select(item.chest)
  181.  
  182. if turtle.place() == false then
  183. error("Failed to place chest from slot " .. item.chest)
  184. end
  185. end
  186.  
  187. local function getChest(item)
  188. turtle.select(item.chest)
  189.  
  190. if turtle.dig() == false then
  191. error("Failed to retreive chest to slot " .. item.chest)
  192. end
  193.  
  194. alignZDir(preChestZdir)
  195. alignXDir(preChestXdir)
  196. end
  197.  
  198. local function unload(item)
  199. placeChest(item)
  200.  
  201. if item.lastItem then
  202. for n=item.material,16 do
  203. turtle.select(n)
  204. turtle.drop()
  205. end
  206. else
  207. turtle.select(item.material)
  208. turtle.drop()
  209. end
  210.  
  211. getChest(item)
  212. end
  213.  
  214. local function reload(item)
  215. if turtle.getItemSpace(item.material) == 0 then
  216. return
  217. end
  218.  
  219. placeChest(item)
  220. turtle.select(item.material)
  221. while turtle.getItemSpace() > 0 do
  222. if turtle.suck(turtle.getItemSpace()) == false then
  223. print("Couldn't get an item for slot " .. item.material)
  224. sleep(30)
  225. end
  226. end
  227. getChest(item)
  228. end
  229.  
  230. local function refuel(item, fuelLimit)
  231. if fuelLimit == nil then
  232. fuelLimit = turtle.getFuelLimit()
  233. end
  234.  
  235. while turtle.getFuelLevel() < fuelLimit do
  236. turtle.select(item.material)
  237. if turtle.getItemCount() > 1 then
  238. turtle.refuel(turtle.getItemCount() - 1)
  239. else
  240. reload(item)
  241. end
  242. end
  243. end
  244.  
  245. local Item = {}
  246. Item["detect"] = {}
  247. Item["detect"]["forward"] = turtle.detect
  248. Item["detect"]["up"] = turtle.detectUp
  249. Item["detect"]["down"] = turtle.detectDown
  250. Item["place"] = {}
  251. Item["place"]["forward"] = turtle.place
  252. Item["place"]["up"] = turtle.placeUp
  253. Item["place"]["down"] = turtle.placeDown
  254. Item["compare"] = {}
  255. Item["compare"]["forward"] = turtle.compare
  256. Item["compare"]["up"] = turtle.compareUp
  257. Item["compare"]["down"] = turtle.compareDown
  258. local function place(item, direction)
  259. if turtle.getItemCount(item.material) <= 1 then
  260. reload(item)
  261. end
  262.  
  263. turtle.select(item.material)
  264. while Item.detect[direction]() == true do
  265. if Item.compare[direction]() == false then
  266. if movements[direction]["dig"]() == false then
  267. error("Failed to dig a block different than item.material")
  268. end
  269. else
  270. break
  271. end
  272. end
  273.  
  274. if Item.detect[direction]() == false then
  275. while Item.place[direction]() == false do
  276. print("Failed to place an item from slot " .. item.material)
  277. movements[direction]["attack"]()
  278. end
  279. end
  280. end
  281.  
  282. local function isFull()
  283. for n=firstSlot,16 do
  284. if turtle.getItemCount(n) == 0 then
  285. --Empty slots
  286. return false;
  287. end
  288. end
  289. print( "No empty slots left." )
  290. return true;
  291. end
  292.  
  293. -----------------------------------------------------------------------------
  294. --Main Program Functions
  295. -----------------------------------------------------------------------------
  296.  
  297. function stock()
  298. if turtle.getFuelLimit() ~= 0 and turtle.getFuelLevel() < 10000 then
  299. refuel(fuel, 20000)
  300. end
  301.  
  302. if isFull() then
  303. unload(loot)
  304. end
  305. end
  306.  
  307. function edge()
  308. if xPos == 0 then
  309. alignXDir(-1)
  310. place(wall, "forward")
  311. elseif xPos == 2 then
  312. alignXDir(1)
  313. place(wall, "forward")
  314. end
  315.  
  316. if depth == 0 then
  317. place(foundation, "down")
  318. elseif depth == -2 then
  319. place(wall, "up")
  320. end
  321. end
  322.  
  323. function placeTorch()
  324. alignZDir(-1)
  325. place(torch, "forward")
  326. end
  327.  
  328. function mineFace(z)
  329. local afterEach = function() stock() edge() end
  330.  
  331. goTo(0, 0, z, afterEach)
  332.  
  333. if z % 5 == 0 then
  334. placeTorch()
  335. end
  336.  
  337. goTo(2, 0, z, afterEach)
  338. goTo(2, -2, z, afterEach)
  339. goTo(1, -2, z, afterEach)
  340. turtle.digDown()
  341. goTo(0, -2, z, afterEach)
  342. goTo(0, 0, z, afterEach)
  343. end
  344.  
  345. -----------------------------------------------------------------------------
  346. --Main Program
  347. -----------------------------------------------------------------------------
  348.  
  349. local tArgs = { ... }
  350. if #tArgs ~= 1 then
  351. print( "My Version Usage: tunnel length" )
  352. return
  353. end
  354.  
  355. local length = tonumber( tArgs[1] ) -1
  356. if length < 0 then
  357. print( "All arguments must be positive" )
  358. return
  359. end
  360.  
  361. print( "Tunneling" )
  362. reload(fuel)
  363. reload(torch)
  364. reload(wall)
  365. reload(foundation)
  366.  
  367. for z=0, length do
  368. print("Mining face " .. z)
  369. mineFace(z)
  370. end
  371.  
  372. print( "Emptying" )
  373. unload(fuel)
  374. unload(torch)
  375. unload(wall)
  376. unload(foundation)
  377. unload(loot)
  378. alignZDir(1)
  379.  
  380. print("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement