Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Turtle Quarry Program ]]--
- --[[ Smart name compare:
- Compare the name of 2 items including name variations
- for ores, cobbled versions, etc...
- returns true if item exists in inventory
- returns false if item does not exist in inventory
- ]]--
- function smartNameCompare(name, cur_name)
- local bits_to_delete = {"cobbled_", "cobble", "raw_", "_ore"}
- -- delete all the extra bits from them name
- for i, del_name in ipairs(bits_to_delete) do
- local name = string.gsub(name, del_name, "")
- local cur_name = string.gsub(cur_name, del_name, "")
- end
- if name == cur_name then
- return true
- else
- return false
- end
- end
- --[[ Smart mine:
- Only mine the block we're looking at if we know we
- have room for it in the inventory.
- returns true if mined or no block
- returns false if no room for block
- ]]--
- function smartMine()
- local did_mine = false
- -- get the data of the block being looked at
- local success, data = turtle.inspect()
- if success then
- -- loop through the inventory and check if the current item exists in there
- for i = 1,16,1 do
- -- dig if the current inventory slot is empty
- if turtle.getItemDetail(i) == nil then
- print("Empty inventory slot")
- did_mine = true
- turtle.dig()
- break
- else
- -- check for the name of the item in the current slot
- local cur_name = turtle.getItemDetail(i)["name"]
- -- dig if we have room for the item
- if ((turtle.getItemCount(i) < 64) and smartNameCompare(data.name, cur_name)) then
- print("Can stack item")
- did_mine = true
- turtle.dig()
- break
- end
- end
- end
- else
- did_mine = true
- print("No minable item")
- end
- return did_mine
- end
- --[[ Calculate fuel:
- Calculate how much fuel we have left and refuel if needed
- returns true if we can get home on our current fuel level
- returns false if we need to go back now
- ]]--
- function calculateFuel(pos)
- -- refuel if we need to
- local cur_fuel = turtle.getFuelLevel() -- current fuel
- if cur_fuel == 0 then
- for i = 1,16,1 do
- if not (turtle.getItemCount(i) == nil) then
- if turtle.getItemDetail(i)["name"] == "minecraft:coal" then
- turtle.select(i)
- turtle.refuel()
- turtle.select(1)
- print("Refuelled.")
- break
- end
- end
- end
- end
- -- calculate if we have enough fuel to get home
- local fuel_needed_to_get_home = pos:dot(vector.new(1, 1, 1)) -- total fuel needed to get home
- for i = 1,16,1 do
- if not (turtle.getItemCount(i) == nil) then
- if turtle.getItemDetail(i)["name"] == "minecraft:coal" then
- cur_fuel = cur_fuel + 80
- end
- end
- end
- if cur_fuel <= fuel_needed_to_get_home then
- return false
- else
- return true
- end
- end
- function smartMove(dir)
- if dir == "f" then
- turtle.forward()
- elseif dir == "l" then
- turtle.turnLeft()
- elseif dir == "r" then
- turtle.turnRight()
- elseif dir == "b" then
- turtle.back()
- end
- end
- function returnHome()
- end
- --[[ main ]]--
- local relative_pos = vector.new(0, 0, 0)
- if calculateFuel(relative_pos) then
- if not smartMine() then
- print("Cannot mine block. Returning to dropoff point...")
- end
- else
- returnHome(relative_pos)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement