Advertisement
Pampour

[ComputerCraft] Quarry: Mining Turtle

Apr 28th, 2013
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.84 KB | None | 0 0
  1. xCoord = 267
  2. yCoord = 69
  3. zCoord = -1594
  4.  
  5. xHome = 267
  6. yHome = 69
  7. zHome = -1594
  8.  
  9. xQuarry = 999
  10. zQuarry = 999
  11. yQuarry = 150
  12.  
  13. xProgress = 999
  14. zProgress = 999
  15. yProgress = 150
  16. oProgress = 1
  17.  
  18. orientation = 3
  19. orientations = {"north", "east", "south", "west"}
  20.  
  21. zDiff = {-1, 0, 1, 0}
  22. xDiff = {0, 1, 0, -1}
  23.  
  24. lineLength = 10
  25. lines = 10
  26. yMin = 999
  27.  
  28. jobAvailable = true
  29. manager = 9
  30.  
  31. function inventoryFull()
  32.   turtle.select(9)
  33.   full = turtle.getItemCount(9) > 0
  34.   turtle.select(1)
  35.   return full
  36. end
  37.  
  38. function look(direction)
  39.   while direction ~= orientations[orientation] do
  40.     right()
  41.   end
  42. end
  43.  
  44. function moveForward()
  45.   xCoord = xCoord + xDiff[orientation]
  46.   zCoord = zCoord + zDiff[orientation]
  47.  
  48.   turtle.dig()
  49.   moved = turtle.forward()
  50.   while not(moved) do
  51.     turtle.dig()
  52.     moved = turtle.forward()
  53.   end
  54. end
  55.  
  56.  
  57. function left()
  58.   orientation = orientation - 1
  59.   orientation = (orientation - 1) % 4
  60.   orientation = orientation + 1
  61.  
  62.   turtle.turnLeft()
  63. end
  64.  
  65. function right()
  66.   orientation = orientation - 1
  67.   orientation = (orientation + 1) % 4
  68.   orientation = orientation + 1
  69.  
  70.   turtle.turnRight()
  71. end
  72.  
  73. function moveUp()
  74.   yCoord = yCoord + 1
  75.  
  76.   turtle.digUp()
  77.  
  78.   moved = false
  79.   while not(moved) do
  80.     moved = turtle.up()
  81.   end
  82. end
  83.  
  84. function moveDown()
  85.   yCoord = yCoord - 1
  86.  
  87.   turtle.digDown()
  88.  
  89.   moved = false
  90.   while not(moved) do
  91.     moved = turtle.down()
  92.   end
  93.  
  94.   if yMin > yCoord then
  95.     yMin = yCoord
  96.   end
  97. end
  98.  
  99. function goto(xTarget, zTarget, yTarget)
  100.   while yTarget < yCoord do
  101.      moveDown()
  102.   end
  103.   while yTarget > yCoord do
  104.     moveUp()
  105.   end
  106.  
  107.   if xTarget < xCoord then
  108.     look("west")
  109.     while xTarget < xCoord do
  110.        moveForward()
  111.     end
  112.   end
  113.   if xTarget > xCoord then
  114.     look("east")
  115.     while xTarget > xCoord do
  116.       moveForward()
  117.     end
  118.   end
  119.   if zTarget < zCoord then
  120.     look("north")
  121.     while zTarget < zCoord do
  122.       moveForward()
  123.     end
  124.   end
  125.   if zTarget > zCoord then
  126.     look("south")
  127.     while zTarget > zCoord do
  128.       moveForward()
  129.     end
  130.   end
  131. end
  132.  
  133. function returnItems()
  134.   xProgress = xCoord
  135.   zProgress = zCoord
  136.   yProgress = yCoord
  137.   oProgress = orientation
  138.  
  139.   goto(xHome, zHome, yHome)
  140.   look("north")
  141.   for i = 1,9 do
  142.     turtle.select(i)
  143.     turtle.drop()
  144.   end
  145.   turtle.select(1)
  146.  
  147.   goto(xProgress, zProgress, yProgress)
  148.   look(orientations[oProgress])
  149. end
  150.  
  151. function digLine()
  152.   for i = 1,lineLength do
  153.     if inventoryFull() then
  154.       returnItems()
  155.     end
  156.     moveForward()
  157.   end
  158. end
  159.  
  160. function digLayer()
  161.   for i = 1,lines do
  162.     digLine()
  163.     if (i%2) == 1 and i < lines then
  164.       left()
  165.       moveForward()
  166.       left()
  167.     elseif i < lines then
  168.       right()
  169.       moveForward()
  170.       right()
  171.     end
  172.   end
  173.   goto(xQuarry, zQuarry, yCoord)
  174.   look("north")
  175.   moveDown()
  176. end
  177.  
  178. function digQuarry(xTarget, zTarget, yTarget)
  179.   xQuarry = xTarget
  180.   zQuarry = zTarget
  181.   yQuarry = yTarget
  182.  
  183.   goto(xQuarry, zQuarry, yQuarry)
  184.   while yMin > 7 do
  185.     digLayer()
  186.   end
  187.   goto(xQuarry, zQuarry, yQuarry)
  188.   goto(xHome, zHome, yHome)
  189.   yMin = 999
  190. end
  191.  
  192. function getJob()
  193.   while jobAvailable do
  194.     print("Requesting job")
  195.     rednet.send(manager, "getJob")
  196.     id, message, dis = rednet.receive()
  197.    
  198.     if message == "yes" then
  199.       id, xRec, dis = rednet.receive()
  200.       id, zRec, dis = rednet.receive()
  201.       id, yRec, dis = rednet.receive()
  202.      
  203.       xQuarry = tonumber(xRec)
  204.       zQuarry = tonumber(zRec)
  205.       yQuarry = tonumber(yRec)
  206.       print("Job at "..xQuarry.." "..zQuarry.." "..yQuarry)
  207.      
  208.       digQuarry(xQuarry, zQuarry, yQuarry)
  209.       print("Finished job")
  210.     elseif message == "no" then
  211.       print("No more jobs")
  212.       jobAvailable = false
  213.     end
  214.   end
  215. end
  216.  
  217. rednet.close("right")
  218. rednet.open("right")
  219.  
  220. getJob(273, 69, -1594)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement