Skip to content

Instantly share code, notes, and snippets.

@Frityet
Created September 6, 2024 21:21
Show Gist options
  • Save Frityet/ca624da38d8a70f1b2e30c1feb753b39 to your computer and use it in GitHub Desktop.
Save Frityet/ca624da38d8a70f1b2e30c1feb753b39 to your computer and use it in GitHub Desktop.
-- Constants for chunk size
local arg = {...}
---@param called string
---@return (string | integer)?
local function get_arg(called)
local tofind = "--"..called
for i, v in ipairs(arg) do
if v == tofind then
local val = arg[i+1]
if val then
return tonumber(val) or val
else
error("No value provided for argument "..called)
end
end
end
end
local CHUNK_SIZE = get_arg "chunk-size" or 16
local HEIGHT = get_arg "height" or 64
---@type 0 | ccTweaked.turtle.slot
local fuel_slot = 0
local acceptable_fuels = {
["minecraft:coal"] = true,
["minecraft:charcoal"] = true,
["minecraft:blaze_rod"] = true,
["minecraft:lava_bucket"] = true,
["minecraft:coal_block"] = true
}
for i = 1, 16 do
if turtle.getItemCount(i) > 0 then
local item = assert(turtle.getItemDetail(i, true))
if acceptable_fuels[item.name] then
fuel_slot = i
print("Fuel found at slot "..i)
break
end
end
end
if fuel_slot == 0 then
error("No fuel found")
end
--[[@cast fuel_slot ccTweaked.turtle.slot]]
---@type thread[]
local threads = {}
---@param thr fun()
local function schedule(thr) threads[#threads+1] = coroutine.create(thr) end
---@async
local function refuel()
while true do
local fuel_level = turtle.getFuelLevel()
if fuel_level < 10 then
print("Refueling")
local prev_slot = turtle.getSelectedSlot()
turtle.select(fuel_slot)
assert(turtle.refuel())
turtle.select(prev_slot)
end
coroutine.yield()
end
end
---@async
local function mine_layers()
while true do
if turtle.getFuelLevel() < 10 then
coroutine.yield()
end
for i = 1, HEIGHT do
for j = 1, CHUNK_SIZE do
for k = 1, CHUNK_SIZE do
turtle.dig()
turtle.forward()
end
if j % 2 == 0 then
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.turnRight()
else
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.turnLeft()
end
coroutine.yield()
end
if i % 2 == 0 then
turtle.turnRight()
turtle.turnRight()
for _ = 1, CHUNK_SIZE do
turtle.forward()
end
turtle.turnRight()
turtle.turnRight()
else
turtle.turnLeft()
turtle.turnLeft()
for _ = 1, CHUNK_SIZE do
turtle.forward()
end
turtle.turnLeft()
turtle.turnLeft()
end
coroutine.yield()
end
end
end
schedule(refuel)
schedule(mine_layers)
local done = false
while not done do
for i, thread in ipairs(threads) do
if coroutine.status(thread) ~= "dead" then
coroutine.resume(thread)
else
table.remove(threads, i)
end
end
coroutine.yield()
done = #threads == 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment