Advertisement
Guest User

quarry

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