From 7dc979f8c47e64b7a72dc806bfcc34f49b03378c Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Sun, 19 May 2024 13:22:24 +0200 Subject: [PATCH] feat: add inferium-upgrader --- inferium-upgrader.lua | 188 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 inferium-upgrader.lua diff --git a/inferium-upgrader.lua b/inferium-upgrader.lua new file mode 100644 index 0000000..b609c47 --- /dev/null +++ b/inferium-upgrader.lua @@ -0,0 +1,188 @@ +local FILL_BLOCK = 'minecraft:cobblestone' +local LEVEL_PER_TIER = 12 + +local formatBlockName = function(tierName) + return "mysticalagriculture:" .. tierName .. "_growth_accelerator" +end + +local BLOCKS_PER_TIER = { + formatBlockName('inferium'), + formatBlockName('prudentium'), + formatBlockName('tertium'), + formatBlockName('imperium'), + formatBlockName('supremium') +} + +local MAX_LEVEL = LEVEL_PER_TIER * #BLOCKS_PER_TIER + +local countItems = function(itemName) + local total = 0 + + for i=1,16,1 do + local details = turtle.getItemDetail(i) + if details and details.name == itemName then + total = total + details.count + end + end + + return total +end + +local assertEnoughFillBlocks = function() + local count = countItems(FILL_BLOCK) + + if count < MAX_LEVEL then + error('Not enough fill blocks, please provide at least ' .. MAX_LEVEL .. ' of ' .. FILL_BLOCK) + end +end + +local assertEnoughFuel = function() + local minFuel = 10 + (MAX_LEVEL * 2) + + if turtle.getFuelLevel() < minFuel then + error('Not enough fuel') + end +end + +local refuel = function() + for i=1, 16, 1 do + local count = turtle.getItemCount(i) + if count > 0 then + turtle.select(i) + turtle.refuel() + end + end +end + +local getTierFromLevel = function(level) + return math.floor((level - 1) / LEVEL_PER_TIER) + 1 +end + +local getBlockNameFromLevel = function(level) + local tier = getTierFromLevel(level) + return BLOCKS_PER_TIER[tier] +end + +local inspectBlockName = function() + local ok, block = turtle.inspect() + + if not ok then + return nil + end + + return block and block.name +end + +local selectBlock = function(blockName) + for i=1, 16, 1 do + local details = turtle.getItemDetail(i) + if details and details.name == blockName then + turtle.select(i) + return true + end + end + + return false +end + +local replaceBlockAt = function(y) + local blockName = getBlockNameFromLevel(y); + + if inspectBlockName() == blockName then + return false + end + + local selected = selectBlock(blockName) + + if not selected then + return 'error' + end + + turtle.dig() + turtle.place() + return true +end + +local placeDownFillBlock = function() + selectBlock(FILL_BLOCK) + turtle.placeDown() +end + +-- Main state and stateful methods +local state = { + y = 1, + initialLevel = 0, + newLevel = 0, + errorLevel = nil +} + +local digAndGoDown = function() + turtle.digDown() + turtle.down() + state.y = state.y + 1 +end + +local goUp = function() + turtle.up() + state.y = state.y - 1 +end + +-- 1. down procedure +local downProcedure = function() + while state.y < 60 do + local replaced = replaceBlockAt(state.y) + + if replaced == false then + state.initialLevel = state.y + state.newLevel = state.initialLevel + end + + if replaced == true then + state.newLevel = state.y + end + + if replaced == 'error' then + state.errorLevel = state.y + break + end + + digAndGoDown() + end +end + +-- 2. up procedure +local upProcedure = function() + while state.y ~= 1 do + goUp() + placeDownFillBlock() + end +end + +-- 3. print report +local printReport = function() + local initialLevel = state.initialLevel + if initialLevel == nil then + initialLevel = 'nil' + end + print('Initial Level: ' .. initialLevel) + print('New Level: ' .. state.newLevel) + + if state.errorLevel then + local tier = getTierFromLevel(state.errorLevel) + + print('Tier ' .. tier .. ' needed for next upgrade') + end +end + +-- Main function +local main = function() + assertEnoughFillBlocks() + refuel() + assertEnoughFuel() + + downProcedure() + upProcedure() + printReport() +end + +main()