mdamaged

Quarry with Rednet

Dec 1st, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.59 KB | None | 0 0
  1. -- edit this
  2. turtleID = os.computerID()
  3. print("Turtle "..turtleID.." is online.")
  4.  
  5. --- 2lines below are test
  6. --broadcastID, broadcastMSG = rednet.receive()
  7. --print("Broadcast from "..broadcastID..": "..broadcastMSG)
  8.  
  9. rednet.close("right")
  10. rednet.open("right")
  11.  
  12. xLoc, zLoc, yLoc = gps.locate(10)
  13.  
  14. --xLoc = 10
  15. --zLoc = 11
  16. --yLoc = 12
  17.  
  18. print("Docked at "..xLoc.." "..zLoc.." "..yLoc..".")
  19.  
  20. xCoord = xLoc
  21. zCoord = zLoc
  22. yCoord = yLoc
  23.  
  24. --xCoord = -256
  25. --zCoord = 227
  26. --yCoord = 70
  27.  
  28. xQuarry = 999
  29. zQuarry = 999
  30. yQuarry = 150
  31.  
  32. xProgress = 999
  33. zProgress = 999
  34. yProgress = 150
  35. oProgress = 1
  36.  
  37. xHome = xCoord
  38. zHome = zCoord
  39. yHome = yCoord
  40.  
  41. yTravel = 85
  42.  
  43. orientation = 1
  44.  
  45. orientations = {"north", "east", "south", "west"}
  46.  
  47. zDiff = {-1, 0, 1, 0}
  48. xDiff = {0, 1, 0, -1}
  49.  
  50. lineLength = 2
  51. lines = 3
  52. yMin = 999
  53.  
  54. jobAvailable = true
  55.  
  56. managerID = 28
  57.  
  58. turtle.refuel(10)
  59.  
  60. function inventoryFull()
  61.     turtle.select(9)
  62.     full = turtle.getItemCount(9) > 0
  63.     turtle.select(1)
  64.  return full
  65. end
  66.  
  67. function left()
  68.   orientation = orientation - 1
  69.   orientation = (orientation - 1) % 4
  70.   orientation = orientation + 1
  71.   turtle.turnLeft()
  72. end
  73.  
  74. function right()
  75.     orientation = orientation - 1
  76.   orientation = (orientation + 1) % 4
  77.   orientation = orientation + 1
  78.   turtle.turnRight()
  79. end
  80.  
  81. function moveForward()
  82.   xCoord = xCoord + xDiff[orientation]  
  83.   zCoord = zCoord + zDiff[orientation]
  84.  
  85.     while turtle.detect() do
  86.       turtle.dig()
  87.     end
  88.  
  89.     moved = false
  90.       while not(moved) do
  91.         moved = turtle.forward()
  92.       end
  93. end
  94.  
  95. function moveUp()
  96.   yCoord = yCoord + 1
  97.   turtle.digUp()
  98.   moved = false
  99.   while not(moved) do
  100.     moved = turtle.up()
  101.      end  
  102. end
  103.  
  104. function moveDown()
  105.   yCoord = yCoord - 1
  106.   turtle.digDown()
  107.   moved = false
  108.   while not(moved) do
  109.      moved = turtle.down()
  110.   end
  111.  
  112.     if yMin > yCoord then
  113.     yMin = yCoord
  114.   end
  115. end
  116.  
  117. function look(direction)
  118.   while direction ~= orientations[orientation] do
  119.     right()
  120.   end
  121. end
  122.  
  123. function goto(xTarget, zTarget, yTarget)
  124.   while yTarget < yCoord do
  125.     moveDown()
  126.   end
  127.  
  128.     while yTarget > yCoord do
  129.     moveUp()
  130.   end
  131.  
  132.   if xTarget < xCoord then
  133.     look("west")
  134.     while xTarget < xCoord do
  135.       moveForward()
  136.     end
  137.   end
  138.  
  139.      if xTarget > xCoord then
  140.     look("east")
  141.     while xTarget > xCoord do
  142.       moveForward()
  143.     end
  144.   end
  145.  
  146.     if zTarget < zCoord then
  147.     look("north")
  148.     while zTarget < zCoord do
  149.       moveForward()
  150.     end
  151.   end
  152.  
  153.      if zTarget > zCoord then
  154.     look("south")
  155.     while zTarget > zCoord do
  156.       moveForward()
  157.     end
  158.   end
  159. end
  160.  
  161. function returnItems()
  162.   xProgress = xCoord
  163.   zProgress = zCoord
  164.   yProgress = yCoord
  165.   oProgress = orientation
  166.  
  167.   goto(xHome, zHome, yTravel)
  168.   goto(xHome, zHome, yHome)
  169.  
  170. -- edit this was west
  171.   look("south")
  172.  
  173.   for i = 1,9 do
  174.     turtle.select(i)
  175.     turtle.drop()
  176.   end
  177.  
  178.   turtle.select(1)
  179.   goto(xProgress, zProgress, yTravel)
  180.   goto(xProgress, zProgress, yProgress)
  181.   look(orientations[oProgress])  
  182. end
  183.  
  184. function digLine()
  185.   for i = 1,lineLength do
  186.     if inventoryFull() then
  187.       returnItems()
  188.     end
  189.     moveForward()
  190.   end
  191. end
  192.  
  193. function digLayer()
  194.   for i = 1,lines do
  195.     digLine()
  196.     if (i%2) == 1 and i < lines then
  197.       left()
  198.       moveForward()
  199.       left()
  200.     elseif i < lines then
  201.       right()
  202.       moveForward()
  203.       right()
  204.     end    
  205.   end
  206.  
  207.   goto(xQuarry, zQuarry, yCoord)
  208.   look("north")
  209.   moveDown()
  210. end
  211.  
  212. function digQuarry(xTarget, zTarget, yTarget)
  213.   xQuarry = xTarget
  214.   zQuarry = zTarget
  215.   yQuarry = yTarget
  216.  
  217.   goto(xQuarry, zQuarry, yTravel)
  218.   goto(xQuarry, zQuarry, yQuarry)
  219.   look("north")
  220.  
  221.   while yMin > 7 do
  222.     digLayer()
  223.   end
  224.   goto(xQuarry, zQuarry, yQuarry)
  225.   goto(xHome, zHome, yTravel)
  226.   goto(xHome, zHome, yHome)
  227.  
  228.   yMin = 999
  229. end
  230.  
  231.  
  232. function getJob()
  233.   while jobAvailable do
  234.     print("Requesting job...")
  235. -- need to fill in manager id below
  236.     rednet.send(managerID, "getJob")  
  237.     id, message, dist = rednet.receive()
  238.    
  239.     if message == "yes" then
  240.       id, xRec, dist = rednet.receive()
  241.       id, zRec, dist = rednet.receive()
  242.       id, yRec, dist = rednet.receive()
  243.  
  244.       xQuarry = tonumber(xRec)
  245.       zQuarry = tonumber(zRec)
  246.       yQuarry = tonumber(yRec)
  247.      
  248.       print("Received job, moving to "..xQuarry.." "..zQuarry)
  249.       digQuarry(xQuarry, zQuarry, yQuarry)
  250.       print("Finished job.")
  251.  
  252.     elseif message == "no" then
  253.       print("No more jobs available.")
  254.       jobAvailable = false
  255.     end
  256.   end
  257. end
  258.  
  259. rednet.close("right")
  260. rednet.open("right")
  261.  
  262. getJob()
Advertisement
Add Comment
Please, Sign In to add comment