diff --git a/mystical-upgrader.lua b/mystical-upgrader.lua index b609c47..754698e 100644 --- a/mystical-upgrader.lua +++ b/mystical-upgrader.lua @@ -1,4 +1,9 @@ +local turtleUtils = require('libs/turtle-utils') + +local NB_ROWS = 1 +local DIRECTION = 'right' -- 'left' | 'right' local FILL_BLOCK = 'minecraft:cobblestone' + local LEVEL_PER_TIER = 12 local formatBlockName = function(tierName) @@ -54,6 +59,33 @@ local refuel = function() end end +local goToTheLeft = function() + turtle.turnLeft() + local ok, reason = turtle.forward() + + if not ok then + error('cannot forward because ' .. tostring(reason), 0) + end + + turtle.turnRight() +end + +local goToTheRight = function() + turtle.turnRight() + local ok, reason = turtle.forward() + + if not ok then + error('cannot forward because ' .. tostring(reason), 0) + end + + turtle.turnLeft() +end + +local MOVES = { + left = goToTheLeft, + right = goToTheRight, +} + local getTierFromLevel = function(level) return math.floor((level - 1) / LEVEL_PER_TIER) + 1 end @@ -73,10 +105,10 @@ local inspectBlockName = function() return block and block.name end -local selectBlock = function(blockName) +local selectBlock = function(itemName) for i=1, 16, 1 do local details = turtle.getItemDetail(i) - if details and details.name == blockName then + if details and details.name == itemName then turtle.select(i) return true end @@ -109,26 +141,31 @@ local placeDownFillBlock = function() end -- Main state and stateful methods -local state = { - y = 1, - initialLevel = 0, - newLevel = 0, - errorLevel = nil -} +local function getInitialState() + return { + firstPickedItem = nil, + y = 1, + initialLevel = 0, + newLevel = 0, + errorLevel = nil + } +end -local digAndGoDown = function() +-- local state = getInitialState() + +local digAndGoDown = function(state) turtle.digDown() turtle.down() state.y = state.y + 1 end -local goUp = function() +local goUp = function(state) turtle.up() state.y = state.y - 1 end -- 1. down procedure -local downProcedure = function() +local downProcedure = function(state) while state.y < 60 do local replaced = replaceBlockAt(state.y) @@ -146,20 +183,20 @@ local downProcedure = function() break end - digAndGoDown() + digAndGoDown(state) end end -- 2. up procedure -local upProcedure = function() +local upProcedure = function(state) while state.y ~= 1 do - goUp() + goUp(state) placeDownFillBlock() end end -- 3. print report -local printReport = function() +local printReport = function(state) local initialLevel = state.initialLevel if initialLevel == nil then initialLevel = 'nil' @@ -174,15 +211,32 @@ local printReport = function() end end +local upgradeProcedure = function() + turtleUtils.compactInventory() + + local state = getInitialState() + + downProcedure(state) + upProcedure(state) + printReport(state) + + return state +end + -- Main function local main = function() assertEnoughFillBlocks() refuel() assertEnoughFuel() - downProcedure() - upProcedure() - printReport() + for i=1, NB_ROWS, 16 do + upgradeProcedure() + + if i ~= NB_ROWS then + MOVES[DIRECTION]() + end + end + end main()