Advertisement
jdanner95

Quarry v1.2

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