Advertisement
Guest User

Quarry

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