Advertisement
hiphopcanine

Turtle Quarry

Oct 26th, 2023 (edited)
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | Gaming | 0 0
  1. --[[ Turtle Quarry Program ]]--
  2.  
  3. --[[ Smart name compare:
  4. Compare the name of 2 items including name variations
  5. for ores, cobbled versions, etc...
  6. returns true if item exists in inventory
  7. returns false if item does not exist in inventory
  8. ]]--
  9. function smartNameCompare(name, cur_name)
  10.     local bits_to_delete = {"cobbled_", "cobble", "raw_", "_ore"}
  11.     -- delete all the extra bits from them name
  12.     for i, del_name in ipairs(bits_to_delete) do
  13.         local name = string.gsub(name, del_name, "")
  14.         local cur_name = string.gsub(cur_name, del_name, "")
  15.     end
  16.     if name == cur_name then
  17.         return true
  18.     else
  19.         return false
  20.     end
  21. end
  22.  
  23. --[[ Smart mine:
  24. Only mine the block we're looking at if we know we
  25. have room for it in the inventory.
  26. returns true if mined or no block
  27. returns false if no room for block
  28. ]]--
  29. function smartMine()
  30.     local did_mine = false
  31.     -- get the data of the block being looked at
  32.     local success, data = turtle.inspect()
  33.     if success then
  34.         -- loop through the inventory and check if the current item exists in there
  35.         for i = 1,16,1 do
  36.             -- dig if the current inventory slot is empty
  37.             if turtle.getItemDetail(i) == nil then
  38.                 print("Empty inventory slot")
  39.                 did_mine = true
  40.                 turtle.dig()
  41.                 break
  42.             else
  43.                 -- check for the name of the item in the current slot
  44.                 local cur_name = turtle.getItemDetail(i)["name"]
  45.                 -- dig if we have room for the item
  46.                 if ((turtle.getItemCount(i) < 64) and smartNameCompare(data.name, cur_name)) then
  47.                     print("Can stack item")
  48.                     did_mine = true
  49.                     turtle.dig()
  50.                     break
  51.                 end
  52.             end
  53.         end
  54.     else
  55.         did_mine = true
  56.         print("No minable item")
  57.     end
  58.     return did_mine
  59. end
  60.  
  61. --[[ Calculate fuel:
  62. Calculate how much fuel we have left and refuel if needed
  63. returns true if we can get home on our current fuel level
  64. returns false if we need to go back now
  65. ]]--
  66. function calculateFuel(pos)
  67.     -- refuel if we need to
  68.     local cur_fuel = turtle.getFuelLevel() -- current fuel
  69.     if cur_fuel == 0 then
  70.         for i = 1,16,1 do
  71.             if not (turtle.getItemCount(i) == nil) then
  72.                 if turtle.getItemDetail(i)["name"] == "minecraft:coal" then
  73.                     turtle.select(i)
  74.                     turtle.refuel()
  75.                     turtle.select(1)
  76.                     print("Refuelled.")
  77.                     break
  78.                 end
  79.             end
  80.         end
  81.     end
  82.    
  83.     -- calculate if we have enough fuel to get home
  84.     local fuel_needed_to_get_home = pos:dot(vector.new(1, 1, 1)) -- total fuel needed to get home
  85.     for i = 1,16,1 do
  86.         if not (turtle.getItemCount(i) == nil) then
  87.             if turtle.getItemDetail(i)["name"] == "minecraft:coal" then
  88.                 cur_fuel = cur_fuel + 80
  89.             end
  90.         end
  91.     end
  92.     if cur_fuel <= fuel_needed_to_get_home then
  93.         return false
  94.     else
  95.         return true
  96.     end
  97. end
  98.  
  99. function smartMove(dir)
  100.     if dir == "f" then
  101.         turtle.forward()
  102.     elseif dir == "l" then
  103.         turtle.turnLeft()
  104.     elseif dir == "r" then
  105.         turtle.turnRight()
  106.     elseif dir == "b" then
  107.         turtle.back()
  108.     end
  109. end
  110.  
  111. function returnHome()
  112.        
  113. end
  114.  
  115. --[[ main ]]--
  116. local relative_pos = vector.new(0, 0, 0)
  117. if calculateFuel(relative_pos) then
  118.     if not smartMine() then
  119.         print("Cannot mine block. Returning to dropoff point...")
  120.     end
  121. else
  122.     returnHome(relative_pos)
  123. end
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement