Bassic

Bassic Miner (Client)

Nov 26th, 2020 (edited)
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.68 KB | None | 0 0
  1. -- Bassic Miner v1.2.4
  2.  
  3. local isMiningVariablesLoaded = require("variables")
  4. local isConfigLoaded = require("config")
  5.  
  6. local levels, area = ...
  7. local levelsToMine = tonumber(levels)
  8. local areaToMine = tonumber(area)
  9.  
  10. if isConfigLoaded then
  11.     fuelSource = selected_fuel
  12. end
  13.  
  14. -- Start of the program
  15. function MineHole()
  16.     print("Starting Robot...")
  17.     local fuelNeeded = CalculateFuel()
  18.     if fuelNeeded ~= nil and fuelNeeded ~= 0 then
  19.         ConsumeFuel(fuelNeeded)
  20.     end
  21.  
  22.     local depth = 1
  23.     while depth <= levelsToMine do
  24.         MineLevel()
  25.         if depth < levelsToMine + 1 then
  26.             turtle.digDown()
  27.             turtle.down()
  28.         end
  29.         if ((areaToMine % 2) == 0) then
  30.             turtle.turnLeft()
  31.         else
  32.             turtle.turnLeft()
  33.             turtle.turnLeft()
  34.         end
  35.         depth = depth + 1
  36.     end
  37.  
  38.     print("Returning To Surface...")
  39.     local movementsUntilSurface = 0
  40.     while movementsUntilSurface <= levelsToMine + 1 do
  41.         ReturnToSurface()
  42.         movementsUntilSurface = movementsUntilSurface + 1
  43.     end
  44.     DepositItems()
  45.     print("Mining Complete.")
  46. end
  47.  
  48. -- calulates how much coal needs to be consumed based on how many moves the turtle will need to do.
  49. function CalculateFuel()
  50.     local steps = (levelsToMine * areaToMine * areaToMine) + levelsToMine
  51.     local currentFuelLevel = turtle.getFuelLevel()
  52.     print(string.format("Total Steps Needed: %d", steps))
  53.     print(string.format("Current Fuel Level: %d", currentFuelLevel))
  54.     local fuelNeeded = 0
  55.     local maxFuel = 0
  56.     if turtle.getFuelLevel() > steps then
  57.         print("No Additional Fuel Needed.")
  58.     else
  59.         if selected_fuel == FuelSources.Coal then
  60.             fuelNeeded = math.ceil((steps - currentFuelLevel) / 80)
  61.             maxFuel = 80 * 64
  62.         elseif selected_fuel == FuelSources.Charcoal then
  63.             fuelNeeded = math.ceil((steps - currentFuelLevel) / 80)
  64.             maxFuel = 80 * 64
  65.         elseif selected_fuel == FuelSources.CoalBlocks then
  66.             fuelNeeded = math.ceil((steps - currentFuelLevel) / 720)
  67.             maxFuel = 720 * 64
  68.         elseif selected_fuel == FuelSources.Lava then
  69.             fuelNeeded = math.ceil((steps - currentFuelLevel) / 1000)
  70.         end
  71.     end
  72.     if maxFuel ~= 0 and fuelNeeded > (maxFuel) then
  73.         error("Error: To Many Steps Needed, Not Enough Fuel To Consume.")
  74.     end
  75.     return fuelNeeded
  76. end
  77.  
  78. -- Looks into the given turtle's inventory to find and consume the selected fuel source.
  79. function ConsumeFuel(fuelNeeded)
  80.     if fuelSource == FuelSources.Lava then
  81.         for i = 0, fuelNeeded, 1 do
  82.             os.sleep(2)
  83.             if turtle.detectUp() then
  84.                 turtle.suckUp()
  85.             else
  86.                 error("Error: No Enderchest Detected For Fueling.")
  87.             end
  88.             if GetItemIndex("minecraft:lava_bucket") ~= 0 and turtle.select(GetItemIndex("minecraft:lava_bucket")) then
  89.                 turtle.refuel()
  90.                 turtle.dropUp()
  91.             else
  92.                 print("Error: There was a problem finding fuel for turtle.")
  93.                 if GetItemIndex("minecraft:bucket") ~= 0 and turtle.select(GetItemIndex("minecraft:bucket")) then
  94.                     turtle.dropUp()
  95.                 end
  96.                 i = i - 1
  97.             end
  98.         end
  99.     elseif fuelSource == FuelSources.Coal then
  100.         if GetItemIndex("minecraft:coal") ~= 0 and turtle.select(GetItemIndex("minecraft:coal")) then
  101.             turtle.select(GetItemIndex("minecraft:coal"))
  102.             turtle.refuel(fuelNeeded)
  103.         else
  104.             error("Error: No coal detected in invenetory.")
  105.         end
  106.     elseif fuelSource == FuelSources.Charcoal then
  107.         if GetItemIndex("minecraft:charcoal") ~= 0 and turtle.select(GetItemIndex("minecraft:charcoal")) then
  108.             turtle.select(GetItemIndex("minecraft:charcoal"))
  109.             turtle.refuel(fuelNeeded)
  110.         else
  111.             error("Error: No charcoal detected in invenetory.")
  112.         end
  113.     elseif fuelSource == FuelSources.CoalBlocks then
  114.         if GetItemIndex("minecraft:coal_block") ~= 0 and turtle.select(GetItemIndex("minecraft:coal_block")) then
  115.             turtle.select(GetItemIndex("minecraft:coal_block"))
  116.             turtle.refuel(fuelNeeded)
  117.         else
  118.             error("Error: No coal blocks detected in invenetory.")
  119.         end
  120.     end
  121. end
  122.  
  123. -- This will mine an square equal to the area specified.
  124. function MineLevel()
  125.     local directionToTurn = true
  126.  
  127.     for rowsCompleted = 1, areaToMine, 1 do
  128.         for stepsForward = 1, areaToMine - 1, 1 do
  129.             DigAndMove()
  130.             if CheckInventoryLevel() then
  131.                 DepositItems()
  132.             end
  133.         end
  134.         if rowsCompleted ~= areaToMine then
  135.             if directionToTurn then
  136.                 turtle.turnLeft()
  137.                 DigAndMove()
  138.                 turtle.turnLeft()
  139.                 directionToTurn = false
  140.             else
  141.                 turtle.turnRight()
  142.                 DigAndMove()
  143.                 turtle.turnRight()
  144.                 directionToTurn = true
  145.             end
  146.         end
  147.     end
  148. end
  149.  
  150. -- Digs everything in front of it then moves one square forward.
  151. function DigAndMove()
  152.     while turtle.detect() do
  153.         turtle.dig()
  154.     end
  155.     turtle.forward()
  156. end
  157.  
  158. function ReturnToSurface()
  159.     while turtle.detectUp() do
  160.         turtle.digUp()
  161.     end
  162.     turtle.up()
  163. end
  164.  
  165.  
  166. function DepositItems()
  167.     -- Select and Place ender chest
  168.     local chestLocation = GetItemIndex("enderstorage:ender_storage")
  169.     if chestLocation ~= 0 then
  170.         turtle.select(chestLocation)
  171.         if turtle.detectUp() then
  172.             turtle.digUp()
  173.         end
  174.         turtle.placeUp()
  175.  
  176.         for i = 1, 16, 1 do
  177.             turtle.select(i)
  178.             turtle.dropUp()
  179.         end
  180.         turtle.select(1)
  181.         turtle.digUp()
  182.     else
  183.         warn("WARNING: No ender chest detected. Items will soon start to spill onto the ground!")
  184.     end
  185. end
  186.  
  187. -- Checks inventory levels
  188. function CheckInventoryLevel()
  189.     -- We start at 2 because 1 will have the enderchest
  190.     local isInventoryFull = true
  191.     for i = 2, 16, 1 do
  192.         if turtle.getItemCount(i) == 0 then
  193.             isInventoryFull = false
  194.         end
  195.     end
  196.     return isInventoryFull
  197. end
  198.  
  199. -- Find the index of a specified item
  200. function GetItemIndex(itemName)
  201.     local index = 0
  202.     for i = 1, 16, 1 do
  203.         turtle.select(i)
  204.         if turtle.getItemCount() > 0 then
  205.             if turtle.getItemDetail(i).name == itemName then
  206.                 index = i
  207.                 break
  208.             end
  209.         end
  210.     end
  211.     return index
  212. end
  213.  
  214. MineHole()
  215.  
Advertisement
Add Comment
Please, Sign In to add comment