Advertisement
nagoL2015

Quarry - Turtle

Feb 12th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.32 KB | None | 0 0
  1. --[[
  2. The next 8 variables are user controlled.
  3. The current coordinates of the turtle are used in the
  4. variables, xCoord, zCoord, yCoord. Pretty sure you
  5. are able to figure out which are which.  The looking
  6. variable is simply which way the turtle is looking.
  7. 1 is north, 2 is east, 3 is south, and 4 is west.
  8. The manager variable is simply the id of your
  9. manager computer.  If you don't want to have multiple
  10. turtles going then simply comment out the getJob()
  11. function and call the digQuarry() function with the
  12. coordinates of the quarry. yTravel is at what height
  13. the turtle will travel at that height to get to and
  14. from.  lineLength is the size of the quarry. qDirection
  15. is the direction the quarry is dug in. The turtle will
  16. go to the starting point of the quarry and then look
  17. in the direction specified.  The quarry is dug forward
  18. and to the left of the turtle. qBottom specifies the
  19. bottom layer of the quarry.
  20. ]]--
  21.  
  22. local xCoord = 1754
  23. local zCoord = 1754
  24. local yCoord = 64
  25. local looking = 2
  26.  
  27. local manager = 349
  28. local yTravel = 75
  29. local lineLength = 10
  30. local qDirection = 3
  31. local qBottom = 7
  32.  
  33. -- Variables
  34.  
  35. local orientation = looking
  36. local orientations = {"north", "east", "south", "west"}
  37.  
  38. local xQuarry = 999
  39. local zQuarry = 999
  40. local yQuarry = 150
  41.  
  42. local xProgress = xCoord
  43. local zProgress = zCoord
  44. local yProgress = yCoord
  45. local oProgress = orientation
  46.  
  47. local xFProgress = xCoord
  48. local zFProgress = zCoord
  49. local yFProgress = yCoord
  50. local oFProgress = orientation
  51.  
  52. local xHome = xCoord
  53. local zHome = zCoord
  54. local yHome = yCoord
  55. local oHome = orientations[orientation]
  56.  
  57. local zDiff = {-1, 0, 1, 0}
  58. local xDiff = {0, 1, 0, -1}
  59.  
  60. local jobAvailable = true
  61.  
  62. -- Program
  63.  
  64. -- Checks if the inventory is full.
  65. function inventoryFull()
  66.   turtle.select(16)
  67.   full = turtle.getItemCount(16) > 0
  68.   turtle.select(1)
  69.   return full
  70. end
  71.  
  72. -- Custom turn left.
  73. function left()
  74.   orientation = orientation - 1
  75.   orientation = (orientation - 1) % 4
  76.   orientation = orientation + 1
  77.  
  78.   turtle.turnLeft()
  79. end
  80.  
  81. -- Custom turn right.
  82. function right()
  83.   orientation = orientation - 1
  84.   orientation = (orientation + 1) % 4
  85.   orientation = orientation + 1
  86.  
  87.   turtle.turnRight()
  88. end
  89.  
  90. -- Custom forward.
  91. function forward()
  92.   xCoord = xCoord + xDiff[orientation]
  93.   zCoord = zCoord + zDiff[orientation]
  94.  
  95.   while turtle.detect() do
  96.     turtle.dig()
  97.   end
  98.  
  99.   moved = false
  100.   while not moved do
  101.     moved = turtle.forward()
  102.   end
  103. end
  104.  
  105. -- Custom up.
  106. function up()
  107.   yCoord = yCoord + 1
  108.  
  109.   turtle.digUp()
  110.  
  111.   moved = false
  112.   while not moved do
  113.     moved = turtle.up()
  114.   end
  115. end
  116.  
  117. -- Custom down.
  118. function down()
  119.   yCoord = yCoord - 1
  120.  
  121.   turtle.digDown()
  122.  
  123.   moved = false
  124.   while not moved do
  125.     moved = turtle.down()
  126.   end
  127. end
  128.  
  129. -- Look in a certain direction.
  130. function look(direction)
  131.   while direction ~= orientations[orientation] do
  132.     right()
  133.   end
  134. end
  135.  
  136. -- Goto.
  137. function goto(xTarget, zTarget, yTarget)
  138.   while yTarget < yCoord do
  139.     down()
  140.   end
  141.   while yTarget > yCoord do
  142.     up()
  143.   end
  144.  
  145.   if xTarget < xCoord then
  146.     look("west")
  147.     while xTarget < xCoord do
  148.       forward()
  149.     end
  150.   end
  151.   if xTarget > xCoord then
  152.     look("east")
  153.     while xTarget > xCoord do
  154.       forward()
  155.     end
  156.   end
  157.   if zTarget < zCoord then
  158.     look("north")
  159.     while zTarget < zCoord do
  160.       forward()
  161.     end
  162.   end
  163.   if zTarget > zCoord then
  164.     look("south")
  165.     while zTarget > zCoord do
  166.       forward()
  167.     end
  168.   end
  169. end
  170.  
  171. -- Drops off all items.
  172. function returnItems()
  173.   xProgress = xCoord
  174.   zProgress = zCoord
  175.   yProgress = yCoord
  176.   oProgress = orientation
  177.  
  178.   goto(xHome, zHome, yTravel)
  179.   goto(xHome, zHome, yHome)
  180.   look(oHome)
  181.   for i = 1, 16 do
  182.     details = turtle.getItemDetail(i)
  183.     turtle.select(i)
  184.     if details.name == "minecraft:coal" then
  185.       left()
  186.       turtle.drop()
  187.       right()
  188.     else
  189.       turtle.drop()
  190.     end
  191.   end
  192.   turtle.select(1)
  193.  
  194.   goto(xProgress, zProgress, yTravel)
  195.   goto(xProgress, zProgress, yProgress)
  196.   look(orientations[oProgress])
  197. end
  198.  
  199. -- Gets fuel from chest.
  200. function getFuel()
  201.   xFProgress = xCoord
  202.   zFProgress = zCoord
  203.   yFProgress = yCoord
  204.   oFProgress = orientation
  205.  
  206.   goto(xHome, zHome, yTravel)
  207.   goto(xHome, zHome, yHome)
  208.   look(oHome)
  209.   left()
  210.   turtle.select(16)
  211.   turtle.suck(64)
  212.   turtle.refuel(64)
  213.   turtle.select(1)
  214.   goto(xFProgress, zFProgress, yTravel)
  215.   goto(xFProgress, zFProgress, yFProgress)
  216.   look(orientations[oFProgress])
  217. end
  218.  
  219. -- Digs a line of the quarry.
  220. function digLine()
  221.   for i = 1, (lineLength - 1) do
  222.     if inventoryFull() then
  223.       returnItems()
  224.     end
  225.     forward()
  226.   end
  227.  
  228.   if turtle.getFuelLevel() < 300 then
  229.     getFuel()
  230.   end
  231. end
  232.  
  233. -- Digs a layer of the quarry.
  234. function digLayer()
  235.   for i = 1, lineLength do
  236.     digLine()
  237.     if (i%2) == 1 and i < lineLength then
  238.       left()
  239.       forward()
  240.       left()
  241.     elseif i < lineLength then
  242.       right()
  243.       forward()
  244.       right()
  245.     end
  246.   end
  247.   goto(xQuarry, zQuarry, yCoord)
  248.   look(orientations[qDirection])
  249.   down()
  250. end
  251.  
  252. -- Digs the whole quarry.
  253. function digQuarry(xTarget, zTarget, yTarget)
  254.   xQuarry = xTarget
  255.   zQuarry = zTarget
  256.   yQuarry = yTarget
  257.  
  258.   goto(xQuarry, zQuarry, yTravel)
  259.   goto(xQuarry, zQuarry, yQuarry)
  260.   look("south")
  261.   while yCoord > qBottom do
  262.     digLayer()
  263.   end
  264.   goto(xQuarry, zQuarry, yQuarry)
  265.   goto(xHome, zHome, yTravel)
  266.   goto(xHome, zHome, yHome)
  267.   yMin = 999
  268. end
  269.  
  270. -- Gets a job from the manager.
  271. function getJob()
  272.   while jobAvailable do
  273.     rednet.send(manager, "getJob")
  274.     id, message, dist = rednet.receive()
  275.    
  276.     if message == "yes" then
  277.       id, xRec, dist = rednet.receive()
  278.       id, zRec, dist = rednet.receive()
  279.       id, yRec, dist = rednet.receive()
  280.      
  281.       xQuarry = tonumber(xRec)
  282.       zQuarry = tonumber(zRec)
  283.       yQuarry = tonumber(yRec)
  284.      
  285.       digQuarry(xQuarry, zQuarry, yQuarry)
  286.     elseif message == "no" then
  287.       jobAvailable = false
  288.     end
  289.   end
  290. end
  291.  
  292. -- Checks all sides for modems and opens them.
  293. local function openRednet()
  294.   for _,side in ipairs(rs.getSides()) do
  295.     if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  296.       rednet.open(side)
  297.       return side
  298.     end
  299.   end
  300. end
  301.  
  302. -- Open rednet.
  303. openRednet()
  304.  
  305. -- Starts the program.
  306. getJob()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement