diff --git a/programs/goo.lua b/programs/goo.lua index 70ee5ba..4639233 100644 --- a/programs/goo.lua +++ b/programs/goo.lua @@ -1,11 +1,21 @@ -local _VERSION = '0.1.0'; +local _VERSION = '0.1.1'; local args = table.pack(...); local command = args[1]; local GOO_BLOCK_PREFIX = 'justdirethings:gooblock_tier'; +local MIN_START_FUEL = 50; +local REFUEL_THRESHOLD = 1000; local WAIT_SECONDS = 2; +local FUEL_ITEMS = { + 'justdirethings:coal_t4', + 'justdirethings:coal_t3', + 'justdirethings:coal_t2', + 'justdirethings:coal_t1', + 'minecraft:coal', +}; + local PROCESS_ITEMS = { ['minecraft:iron_block'] = { tier = 1, label = 'iron' }, ['minecraft:coal_block'] = { tier = 1, label = 'coal' }, @@ -107,6 +117,119 @@ local function findItemSlot(itemName) return nil; end +local function waitForInventory(message) + if message then + print(message); + end + + os.pullEvent('turtle_inventory'); +end + +local function getFuelLevel() + local fuelLevel = turtle.getFuelLevel(); + + if fuelLevel == 'unlimited' then + return nil; + end + + return fuelLevel; +end + +local function getRefuelTarget() + local fuelLimit = turtle.getFuelLimit(); + + if fuelLimit == 'unlimited' or fuelLimit >= REFUEL_THRESHOLD then + return REFUEL_THRESHOLD; + end + + return fuelLimit; +end + +local function findFuelSlot() + for i = 1, #FUEL_ITEMS do + local slot, item = findItemSlot(FUEL_ITEMS[i]); + + if slot then + return slot, item; + end + end + + return nil; +end + +local function refuelFromInventory() + local fuelLevel = getFuelLevel(); + + if not fuelLevel or fuelLevel >= getRefuelTarget() then + return false; + end + + local previousSlot = turtle.getSelectedSlot(); + local consumedFuel = false; + + while fuelLevel < getRefuelTarget() do + local slot, item = findFuelSlot(); + + if not slot then + break; + end + + turtle.select(slot); + + if turtle.refuel(1) then + consumedFuel = true; + print('Refueled with ' .. item.name .. '. Fuel: ' .. tostring(turtle.getFuelLevel())); + fuelLevel = getFuelLevel(); + else + print('Could not refuel with ' .. item.name .. '.'); + break; + end + end + + turtle.select(previousSlot); + + return consumedFuel; +end + +local function ensureStartFuel() + while true do + local fuelLevel = getFuelLevel(); + + if not fuelLevel or fuelLevel >= MIN_START_FUEL then + return; + end + + refuelFromInventory(); + fuelLevel = getFuelLevel(); + + if not fuelLevel or fuelLevel >= MIN_START_FUEL then + return; + end + + waitForInventory('Fuel is below ' .. tostring(MIN_START_FUEL) .. '. Insert fuel to start.'); + end +end + +local function ensureRuntimeFuel() + local fuelLevel = getFuelLevel(); + + if not fuelLevel then + return; + end + + if fuelLevel < getRefuelTarget() then + refuelFromInventory(); + end + + fuelLevel = getFuelLevel(); + + while fuelLevel and fuelLevel < MIN_START_FUEL do + waitForInventory('Fuel is below ' .. tostring(MIN_START_FUEL) .. '. Insert fuel to continue.'); + refuelFromInventory(); + fuelLevel = getFuelLevel(); + end +end + local function findFeedingSlot(gooTier) local feedingItems = FEEDING_ITEMS_BY_TIER[gooTier] or {}; @@ -155,8 +278,7 @@ local function ensureGooAlive() local slot, item = findFeedingSlot(gooTier); if not slot then - print('Goo tier ' .. tostring(gooTier) .. ' is not alive. Waiting for feeding item...'); - os.sleep(WAIT_SECONDS); + waitForInventory('Goo tier ' .. tostring(gooTier) .. ' is not alive. Waiting for feeding item...'); else turtle.select(slot); print('Goo tier ' .. tostring(gooTier) .. ' is not alive. Feeding with ' .. item.name .. '...'); @@ -377,7 +499,11 @@ end print('goo started. Place the turtle directly below the goo block.'); +ensureStartFuel(); + while true do + ensureRuntimeFuel(); + local gooTier = ensureGooAlive(); local hasEligibleBlocks = findEligibleProcessSlot(gooTier) ~= nil; @@ -390,10 +516,9 @@ while true do if not changed then if hasEligibleBlocks then print('No free goo side found. Waiting for processing...'); + os.sleep(WAIT_SECONDS); else - print('No eligible process block found. Waiting for inventory...'); + waitForInventory('No eligible process block found. Waiting for inventory...'); end - - os.sleep(WAIT_SECONDS); end end