lecouch

Turtle Comb

Sep 3rd, 2015
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.42 KB | None | 0 0
  1. -- Turtle Comb
  2. -- Written by Julian Crouch 8/15
  3. -- Current program is similar to excavate, but only has one call
  4. -- So if program is nammed TC, call TC xx where xx is the distance of blocks you want
  5. -- the turtle to dig down to bedrock.
  6. --
  7. -- Set up n number of turtles where n is the width of the dig.
  8. -- i.e. 16 turtles lined up set to 'TC 32' will dig a 16x32 area.
  9. --
  10. -- Collection is done exactly like excavate with obsidian pipe and iron pipe to controll direction
  11. --
  12. -- For wireless: Save this as 'startup' on each turtle: http://pastebin.com/BMvQv1n4
  13. -- Run this on pocket computer: http://pastebin.com/x3rSATBd
  14. --
  15. -- enjoy!
  16. --
  17. local programParam = { ... }
  18. local startLoc = { 0, 0, 0, 1 }
  19. local lastGoodLoc = { 0, 0, 0, 1 }
  20. local currentLoc = { 0, 0, 0, 1 }
  21. local hitBedrock = false
  22. local doneMining = false
  23. local rtnToStart = false
  24. local gettingFuel = false
  25. local backToMine = false
  26.  
  27. local needFuel, returnAndUnload, travelTo, turnRight, turnLeft, unloadInv
  28.  
  29. -- Save current location for later use
  30. local function saveLoc()
  31.     local newSaveLoc = {};
  32.     newSaveLoc[1] = currentLoc[1]
  33.     newSaveLoc[2] = currentLoc[2]
  34.     newSaveLoc[3] = currentLoc[3]
  35.     newSaveLoc[4] = currentLoc[4]
  36.     return newSaveLoc
  37. end
  38.  
  39. -- When out of fuel, go back to start and wait for fuel
  40. local function goWaitForFuel()
  41.     gettingFuel = true
  42.     rtnToStart = true
  43.     lastGoodLoc = saveLoc()
  44.     travelTo(startLoc)
  45.     turnRight()
  46.     turnRight()
  47.     unloadInv()
  48.     turnLeft()
  49.     turnLeft()
  50.     gettingFuel = false
  51.     rtnToStart = false
  52.     print("NEED MOAR FUELS!")
  53.     while needFuel() do
  54.         os.pullEvent("turtle_inventory")
  55.     end
  56.     backToMine = true
  57.     travelTo(lastGoodLoc)
  58.     backToMine = false
  59. end
  60.  
  61. -- determine if fuel is needed
  62. function needFuel()
  63.     local fuelReq
  64.     -- Check if already getting fuel, if so, don't indicate a need for fuel
  65.     if gettingFuel then
  66.         return false
  67.     else
  68.         if backToMine then
  69.             -- If traveling to last saved location, calculate needed fuel by:
  70.             fuelReq = (math.abs(lastGoodLoc[1]) + math.abs(lastGoodLoc[2]) + math.abs(lastGoodLoc[3]) + 2) 
  71.         else
  72.             -- If traveling to starting location, calculate needed fuel by:
  73.             fuelReq = (math.abs(currentLoc[1]) + math.abs(currentLoc[2]) + math.abs(currentLoc[3]) + 2)
  74.         end
  75.         local fuelNeeded = false
  76.         -- Check for unlimted fuel
  77.         if turtle.getFuelLevel() == "unlimited" then
  78.             return false
  79.         else
  80.             if fuelReq > turtle.getFuelLevel() then
  81.                 fuelNeeded = true
  82.                 for i = 1, 16 do
  83.                     if fuelNeeded then
  84.                         -- Select one inventory slot at a time, then try and burn it.
  85.                         -- If burn successful, repeat until enough fuel has been used.
  86.                         if turtle.getItemCount(i) > 0 then
  87.                             turtle.select(i)
  88.                             if turtle.refuel(1) then
  89.                                 if turtle.getItemCount(i) > 0 then
  90.                                     while fuelReq > turtle.getFuelLevel() and turtle.refuel(1) do
  91.                                     end
  92.                                 end
  93.                                 if fuelReq < turtle.getFuelLevel() then
  94.                                     turtle.select(1)
  95.                                     fuelNeeded = false
  96.                                     break
  97.                                 end
  98.                             end
  99.                         end
  100.                     end
  101.                 end
  102.                 turtle.select(1)
  103.             end
  104.             -- Return result
  105.             if fuelNeeded then
  106.                 return true
  107.             else
  108.                 return false
  109.             end
  110.         end
  111.     end
  112. end
  113.  
  114. -- Check inventory for free space
  115. local function checkInvSpace() 
  116.     local invFull = true
  117.     local totalItemCount = 0
  118.     for i = 1, 16 do
  119.         -- Look for empty inventory spaces
  120.         local itemCount = turtle.getItemCount(i)
  121.         if itemCount == 0 then
  122.             invFull = false
  123.         end
  124.         totalItemCount = totalItemCount + itemCount
  125.     end
  126.     -- Return result
  127.     if invFull then
  128.         print("Inventory full.")
  129.         return false
  130.     else
  131.         return true
  132.     end
  133. end
  134.  
  135. -- Update x, y, movement
  136. local function updateLoc()
  137.     if currentLoc[4] == 1 then
  138.             currentLoc[2] = currentLoc[2] + 1
  139.         elseif currentLoc[4] == 2 then
  140.             currentLoc[1] = currentLoc[1] + 1
  141.         elseif currentLoc[4] == 3 then
  142.             currentLoc[2] = currentLoc[2] - 1
  143.         else
  144.             currentLoc[1] = currentLoc[1] - 1
  145.     end
  146. end
  147.  
  148. -- Forward mining movement
  149. local function moveForward()
  150.     if needFuel() then
  151.         goWaitForFuel()
  152.     end
  153.     while not turtle.forward() do
  154.             turtle.attack()
  155.             turtle.dig()
  156.             if not checkInvSpace() and not rtnToStart then
  157.                 returnAndUnload()
  158.             end
  159.             sleep(0.5)
  160.     end
  161.     local checkSucc, data = turtle.inspectDown()
  162.     if data.name == "minecraft:bedrock" then
  163.         hitBedrock = true
  164.     end
  165.     local checkSucc, data = turtle.inspect()
  166.     if data.name == "minecraft:bedrock" then
  167.         hitBedrock = true
  168.         turtle.up()
  169.         currentLoc[3] = currentLoc[3] + 1
  170.     end
  171.     -- Update location for movement
  172.     updateLoc()
  173. end
  174.  
  175. -- Upward mining movement
  176. local function moveUp()
  177.     if needFuel() then
  178.         goWaitForFuel()
  179.     end
  180.     while not turtle.up() do
  181.         turtle.attackUp()
  182.         turtle.digUp()
  183.         if not checkInvSpace() and not rtnToStart then
  184.             returnAndUnload()
  185.         end
  186.         sleep(0.5)
  187.     end
  188.     -- Update location for movement
  189.     currentLoc[3] = currentLoc[3] + 1
  190. end
  191.  
  192. -- Downward mining movement
  193. local function moveDown()
  194.     if needFuel() then
  195.         goWaitForFuel()
  196.     end
  197.     while not turtle.down() do
  198.         turtle.attackDown()
  199.         turtle.digDown()
  200.         if not checkInvSpace() and not rtnToStart then
  201.             returnAndUnload()
  202.         end
  203.         sleep(0.5)
  204.     end
  205.     -- Update location for movement
  206.     currentLoc[3] = currentLoc[3] - 1
  207. end
  208.  
  209. -- Rotate turtle counterclockwise
  210. function turnLeft()
  211.     if turtle.turnLeft() then
  212.         currentLoc[4] = currentLoc[4] - 1
  213.         if currentLoc[4] < 1 then
  214.             currentLoc[4] = 4
  215.         end
  216.     else
  217.         print("Error!  Unable to turn left.")
  218.     end
  219. end
  220.  
  221. -- Rotate turtle clockwise
  222. function turnRight()
  223.     if turtle.turnRight() then
  224.         currentLoc[4] = currentLoc[4] + 1
  225.         if currentLoc[4] > 4 then
  226.             currentLoc[4] = 1
  227.         end
  228.     else
  229.         print("Error!  Unable to turn right.")
  230.     end
  231. end
  232.  
  233. -- Unload inventory
  234. function unloadInv()
  235.     -- If mining is complete, unload everything
  236.     if doneMining then
  237.         for i = 1, 16 do
  238.             turtle.select(i)
  239.             turtle.drop()
  240.         end
  241.         turtle.select(1)
  242.     else
  243.         -- Unload only what can't be used as fuel
  244.         for i = 1, 16 do
  245.             turtle.select(i)
  246.             if not turtle.refuel(1) then
  247.                 turtle.drop()
  248.             end
  249.         end
  250.         turtle.select(1)
  251.     end
  252. end
  253.  
  254. -- Check if at start
  255. local function isAtStart()
  256.     return currentLoc[1] == startLoc[1] and currentLoc[2] ==startLoc[2] and currentLoc[3] == startLoc[3] and currentLoc[4] == startLoc[4]
  257. end
  258.  
  259. -- Travel to a coordinate
  260. function travelTo(locArrayIn)
  261.     -- Below end location
  262.     if currentLoc[3] < locArrayIn[3] then
  263.         while currentLoc[3] ~= locArrayIn[3] do
  264.             moveUp()
  265.         end
  266.     end
  267.  
  268.     -- If left of end location
  269.     if currentLoc[1] < locArrayIn[1] then
  270.         while currentLoc[4] ~= 2 do
  271.             turnRight()
  272.         end
  273.  
  274.         while currentLoc[1] ~= locArrayIn[1] do
  275.             moveForward()
  276.         end
  277.     end
  278.  
  279.     -- If right of end location
  280.     if currentLoc[1] > locArrayIn[1] then
  281.         while currentLoc[4] ~= 4 do
  282.             turnLeft()
  283.         end
  284.  
  285.         while currentLoc[1] ~= locArrayIn[1] do
  286.             moveForward()
  287.         end
  288.     end
  289.  
  290.     -- If behind end location
  291.     if currentLoc[2] < locArrayIn[2] then
  292.         while currentLoc[4] ~= 1 do
  293.             turnRight()
  294.         end
  295.         while currentLoc[2] ~= locArrayIn[2] do
  296.             moveForward()
  297.         end
  298.     end
  299.  
  300.     -- If in front of end location
  301.     if currentLoc[2] > locArrayIn[2] then
  302.         while currentLoc[4] ~= 3 do
  303.             turnLeft()
  304.         end
  305.         while currentLoc[2] ~= locArrayIn[2] do
  306.             moveForward()
  307.         end
  308.     end
  309.  
  310.     -- Above end location
  311.     if currentLoc[3] > locArrayIn[3] then
  312.         while currentLoc[3] ~= locArrayIn[3] do
  313.             moveDown()
  314.         end
  315.     end
  316.  
  317.     -- Orient turtle to correct location
  318.     while currentLoc[4] ~= locArrayIn[4] do
  319.         turnRight()
  320.     end
  321. end
  322.  
  323. function returnAndUnload()
  324.     -- Set flags so needFuel() and returnAndUnload() can't be called more than once a return
  325.     gettingFuel = true
  326.     rtnToStart = true
  327.     lastGoodLoc = saveLoc()
  328.     print("Returning to start to unload.")
  329.     travelTo(startLoc)
  330.     turnRight()
  331.     turnRight()
  332.     unloadInv()
  333.     -- Reset flags
  334.     gettingFuel = false
  335.     rtnToStart = false
  336.     if not doneMining then
  337.         turnLeft()
  338.         turnLeft()
  339.         backToMine = true
  340.         travelTo(lastGoodLoc)
  341.         backToMine = false
  342.     end
  343.     print("Returning to dig site.")
  344. end
  345.  
  346. -- Start digging that hole!
  347. --
  348. while true do
  349.     -- Dig trench for however long required
  350.     for i = 1, (tonumber(programParam[1])  - 1) do
  351.         moveForward()
  352.     end
  353.     -- Finish the row
  354.     -- See if dig is done
  355.     print(math.abs(currentLoc[3]))
  356.     print(tonumber(programParam[2]))
  357.     if  hitBedrock or math.abs(currentLoc[3]) == tonumber(programParam[2]) then
  358.         doneMining = true
  359.         returnAndUnload()
  360.         print("Job's done.")
  361.         break
  362.     else
  363.         -- If not, turn around, go down, and continue
  364.         turnRight()
  365.         turnRight()
  366.         moveDown()
  367.     end
  368. end
Advertisement
Add Comment
Please, Sign In to add comment