From 529b10e8dd691c480a0d5b3b59913234f1cee790 Mon Sep 17 00:00:00 2001 From: justuswolff Date: Wed, 18 Feb 2026 18:52:49 +0100 Subject: [PATCH] add optimization for stacks --- src/main.lua | 122 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 113 insertions(+), 9 deletions(-) diff --git a/src/main.lua b/src/main.lua index aab1408..783497a 100644 --- a/src/main.lua +++ b/src/main.lua @@ -369,6 +369,17 @@ local function directiondist(direction, wanted) if direction ~= wanted then return 1 end -- failsafes if direction == wanted then return 0 end end +local function needstobeplaced(placecode, clayer) + if clayer == 0 or clayer == 2 then + return placecode == 1 + end + if clayer == 1 then + return placecode == 1 or placecode == 4 + end + if clayer == 3 then + return placecode == 1 or placecode == 4 or placecode == 3 + end +end local function getnearestunplaced(buf, pbuf, cx,cy,cz,clayer, sx,sz, direction) local distance = math.huge local selected = nil @@ -388,15 +399,7 @@ local function getnearestunplaced(buf, pbuf, cx,cy,cz,clayer, sx,sz, direction) local needplace = buf[posasstring(x,cy,y)] if pbuf[posasstring(x,cy,y)] then needplace = 0 end -- already placed - if clayer == 0 or clayer == 2 then - needplace = needplace == 1 - end - if clayer == 1 then - needplace = needplace == 1 or needplace == 4 - end - if clayer == 3 then - needplace = needplace == 1 or needplace == 4 or needplace == 3 - end + needplace = needstobeplaced(needplace, clayer) if needplace then -- needs to be placed, calculate distance local cd = math.abs(y-cz)+math.abs(x-cx) -- raw distance (amount of blocks between) cd = cd + extracost @@ -545,6 +548,107 @@ local function printdes(buf, dimensions) center(direction) end +-- vertical stack printing +local function VP_optimizestack(target) + -- go from top to bottom and erase false entries until we encounter a true one. + local index = #target + while #target > 0 do + if target[index] then break else + table.remove(target, index) + index = index - 1 + end + end + + return target +end +local function VP_createstack(buf, dimensions) + local fout = { + ["height"] = dimensions["y"]*(4)+1, + ["dimensions"] = dimensions, + } + local out = {} + + + for x=1,dimensions["x"],1 do + for z=1,dimensions["z"],1 do + + local tempbuf = {} + for y=1,dimensions["y"],1 do -- for each y dimension + local placecode = buf[posasstring(x,y,z)] + for layer=1,3,1 do -- layers + table.insert(tempbuf, needstobeplaced(placecode, layer)) + end + -- ceiling + table.insert(tempbuf, buf[posasstring(_cx,cy,_cz)] ~= 2) + end + VP_optimizestack(tempbuf) + + if #tempbuf > 0 then out[posasstring(x,z)] = tempbuf end + end + end + + fout["stacks"] = out + return fout +end +local function VP_splitstack(_stack, x) + local stack = _stack["stacks"] + local dimensions = _stack["dimensions"] + local sx = dimensions["sx"] + local sz = dimensions["sz"] + local height = _stack["height"] + local stacks = {} + local unevenstack = {} + local stackmod = math.fmod(#stack, x) + local stackdiv = math.floor(#stack/x) + + if stackmod ~= 0 then + for tempstackindex=0,stackmod,1 do + table.insert(unevenstack, stack[posasstring(math.fmod(tempstackindex, sx),math.floor(tempstackindex/sx))]) + end + end + + if stackdiv ~= 0 then + local eind = 0 + local x2 = x + if stackmod ~= 0 then x2 = x - 1 end + for _=0,x2,1 do + local buf = {} + for stackind=eind,stackdiv+eind,1 do + table.insert(buf, stack[posasstring(math.fmod(stackind, sx),math.floor(stackind/sx))]) + end + table.insert(stacks, buf) + eind = eind + stackdiv + end + end + + local out = {} + if #unevenstack > 0 then table.insert(out,unevenstack) end + for _,v in pairs(stacks) do + table.insert(out,v) + end + return out +end +--[[local function VP_inspectsplitted(stacks, dimensions) -- I will probably never finish this. + local currentselected = 1 + + while true do + reset() -- render + local _,y = term.getSize() + term.setCursorPos(1,y) + term.setcol(colors.yellow, colors.black) + term.write("Currentlayer: ") + term.write(tostring(currentselected)) + + local event = table.pack(os.pullEvent()) + if event[1] == "key" then + + end + end +end]] +local function VP_printstack(buf, dimensions) + +end + while true do integritycheck() local action = selopt({