feat(mystical-upgrader): multiple rows + refactor
This commit is contained in:
parent
8e053ddacf
commit
1da3d12cdc
@ -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 FILL_BLOCK = 'minecraft:cobblestone'
|
||||||
|
|
||||||
local LEVEL_PER_TIER = 12
|
local LEVEL_PER_TIER = 12
|
||||||
|
|
||||||
local formatBlockName = function(tierName)
|
local formatBlockName = function(tierName)
|
||||||
@ -54,6 +59,33 @@ local refuel = function()
|
|||||||
end
|
end
|
||||||
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)
|
local getTierFromLevel = function(level)
|
||||||
return math.floor((level - 1) / LEVEL_PER_TIER) + 1
|
return math.floor((level - 1) / LEVEL_PER_TIER) + 1
|
||||||
end
|
end
|
||||||
@ -73,10 +105,10 @@ local inspectBlockName = function()
|
|||||||
return block and block.name
|
return block and block.name
|
||||||
end
|
end
|
||||||
|
|
||||||
local selectBlock = function(blockName)
|
local selectBlock = function(itemName)
|
||||||
for i=1, 16, 1 do
|
for i=1, 16, 1 do
|
||||||
local details = turtle.getItemDetail(i)
|
local details = turtle.getItemDetail(i)
|
||||||
if details and details.name == blockName then
|
if details and details.name == itemName then
|
||||||
turtle.select(i)
|
turtle.select(i)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -109,26 +141,31 @@ local placeDownFillBlock = function()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Main state and stateful methods
|
-- Main state and stateful methods
|
||||||
local state = {
|
local function getInitialState()
|
||||||
|
return {
|
||||||
|
firstPickedItem = nil,
|
||||||
y = 1,
|
y = 1,
|
||||||
initialLevel = 0,
|
initialLevel = 0,
|
||||||
newLevel = 0,
|
newLevel = 0,
|
||||||
errorLevel = nil
|
errorLevel = nil
|
||||||
}
|
}
|
||||||
|
end
|
||||||
|
|
||||||
local digAndGoDown = function()
|
-- local state = getInitialState()
|
||||||
|
|
||||||
|
local digAndGoDown = function(state)
|
||||||
turtle.digDown()
|
turtle.digDown()
|
||||||
turtle.down()
|
turtle.down()
|
||||||
state.y = state.y + 1
|
state.y = state.y + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local goUp = function()
|
local goUp = function(state)
|
||||||
turtle.up()
|
turtle.up()
|
||||||
state.y = state.y - 1
|
state.y = state.y - 1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 1. down procedure
|
-- 1. down procedure
|
||||||
local downProcedure = function()
|
local downProcedure = function(state)
|
||||||
while state.y < 60 do
|
while state.y < 60 do
|
||||||
local replaced = replaceBlockAt(state.y)
|
local replaced = replaceBlockAt(state.y)
|
||||||
|
|
||||||
@ -146,20 +183,20 @@ local downProcedure = function()
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
digAndGoDown()
|
digAndGoDown(state)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 2. up procedure
|
-- 2. up procedure
|
||||||
local upProcedure = function()
|
local upProcedure = function(state)
|
||||||
while state.y ~= 1 do
|
while state.y ~= 1 do
|
||||||
goUp()
|
goUp(state)
|
||||||
placeDownFillBlock()
|
placeDownFillBlock()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 3. print report
|
-- 3. print report
|
||||||
local printReport = function()
|
local printReport = function(state)
|
||||||
local initialLevel = state.initialLevel
|
local initialLevel = state.initialLevel
|
||||||
if initialLevel == nil then
|
if initialLevel == nil then
|
||||||
initialLevel = 'nil'
|
initialLevel = 'nil'
|
||||||
@ -174,15 +211,32 @@ local printReport = function()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local upgradeProcedure = function()
|
||||||
|
turtleUtils.compactInventory()
|
||||||
|
|
||||||
|
local state = getInitialState()
|
||||||
|
|
||||||
|
downProcedure(state)
|
||||||
|
upProcedure(state)
|
||||||
|
printReport(state)
|
||||||
|
|
||||||
|
return state
|
||||||
|
end
|
||||||
|
|
||||||
-- Main function
|
-- Main function
|
||||||
local main = function()
|
local main = function()
|
||||||
assertEnoughFillBlocks()
|
assertEnoughFillBlocks()
|
||||||
refuel()
|
refuel()
|
||||||
assertEnoughFuel()
|
assertEnoughFuel()
|
||||||
|
|
||||||
downProcedure()
|
for i=1, NB_ROWS, 16 do
|
||||||
upProcedure()
|
upgradeProcedure()
|
||||||
printReport()
|
|
||||||
|
if i ~= NB_ROWS then
|
||||||
|
MOVES[DIRECTION]()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user