ColdIV

miningTurtle

Jun 4th, 2021 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. -- pastebin run FuQ3WvPs VW70dBq0 miningTurtle
  2. local torches = turtle.getItemCount(1)
  3. local fuel = turtle.getItemCount(2)
  4. local buildingMaterial = turtle.getItemCount(3)
  5. local maxFuel = fuel
  6. local placeTorchAt = 8 -- place torch on every N. block
  7. local errorLv = 0
  8. local distance = 0
  9. local mainPathLength = 0
  10. local mainPath = true
  11. local subLength = 32
  12.  
  13. function checkInventory ()
  14.     print ("Checking inventory...\n")
  15.     torches = turtle.getItemCount(1)
  16.     fuel = turtle.getItemCount(2)
  17.     buildingMaterial = turtle.getItemCount(3)
  18.    
  19.     print ("Torches: " .. tostring(torches) .. "\nFuel: " .. tostring(fuel) .. "\nBuilding Material: " .. tostring(buildingMaterial) .. "\n")
  20.    
  21.     if torches <= 1 then errorLv = 1 end
  22.     if fuel <= maxFuel / 2 and turtle.getFuelLevel() < mainPathLength then errorLv = 1 end
  23.     if buildingMaterial <= 1 then errorLv = 1 end
  24.    
  25.     print ("Error Level: " .. tostring(errorLv) .. "\n")
  26. end
  27.  
  28. function home ()
  29.     print ("Returning Home..\n")
  30.     if mainPath then
  31.         print ("On Main Path\n")
  32.         turtle.turnRight()
  33.         turtle.turnRight()
  34.         for i = 1, mainPathLength, 1 do
  35.             turtle.forward()
  36.         end
  37.        
  38.         print ("I am home! Or am I?\n")
  39.     else
  40.         print ("Not on Main Path\n")
  41.         turtle.turnRight()
  42.         turtle.turnRight()
  43.         for i = 1, distance, 1 do
  44.             turtle.forward()
  45.         end
  46.         turtle.turnRight()
  47.         mainPath = true
  48.         home()
  49.     end
  50. end
  51.  
  52. function moveBit (pTorch)
  53.     print ("Moving forward...\n")
  54.     -- Move forward, if not possible -> dig
  55.     while not turtle.forward() do
  56.         turtle.dig()
  57.     end
  58.    
  59.     -- Build floor
  60.     turtle.select(3)
  61.     turtle.placeDown()
  62.    
  63.     -- Build lower left wall
  64.     turtle.turnLeft()
  65.     turtle.place()
  66.    
  67.     -- Move Up
  68.     while not turtle.up() do
  69.         turtle.digUp()
  70.     end
  71.    
  72.     -- Build ceiling
  73.     turtle.placeUp()
  74.    
  75.     -- Build upper left wall
  76.     turtle.place()
  77.    
  78.     -- Build upper right wall
  79.     turtle.turnRight()
  80.     turtle.turnRight()
  81.     turtle.place()
  82.    
  83.     -- Move down
  84.     turtle.down()
  85.    
  86.     -- Build lower right wall
  87.     turtle.place()
  88.    
  89.     -- Place Torch
  90.     if pTorch then
  91.         print ("Placing Torch\n")
  92.         turtle.select(1)
  93.         turtle.placeUp()
  94.         turtle.select(3)
  95.     end
  96.    
  97.     -- Turn back to path
  98.     turtle.turnLeft()
  99.    
  100.     distance = distance + 1
  101.     if mainPath then mainPathLength = mainPathLength + 1 end
  102. end
  103.  
  104. function run ()    
  105.     print ("Starting Program...\n")
  106.     while (true) do
  107.         checkInventory ()
  108.         if errorLv ~= 0 then
  109.             home()
  110.             return
  111.         end
  112.        
  113.         -- Refuel
  114.         if turtle.getFuelLevel() == 0 then
  115.             turtle.select(2)
  116.             turtle.refuel()
  117.         end
  118.        
  119.         -- Strip Mine Thing
  120.         if mainPath and distance >= 3 then
  121.             print ("Leaving Main Path...\n")
  122.             mainPath = false
  123.             distance = 0
  124.             turtle.turnRight()
  125.         elseif not mainPath and distance >= subLength then
  126.             print ("Going back to Main Path...\n")
  127.             turtle.turnRight()
  128.             turtle.turnRight()
  129.             distance = 0
  130.             for i = 1, subLength, 1 do
  131.                 print (tostring(i) .. " of " .. tostring(subLength) .. "\n")
  132.                 turtle.forward()
  133.             end
  134.             turtle.turnRight()
  135.             mainPath = true
  136.         end
  137.        
  138.         -- Move, Dig, Build and place Torch
  139.         moveBit((not mainPath and (((distance + 1) % placeTorchAt == 0) or (distance + 1) == 1)))
  140.     end
  141. end
  142.  
  143. run()
Add Comment
Please, Sign In to add comment