lecouch

Untitled

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