add optimization for stacks

This commit is contained in:
justuswolff
2026-02-18 18:52:49 +01:00
parent a6d267765d
commit 529b10e8dd

View File

@@ -369,6 +369,17 @@ local function directiondist(direction, wanted)
if direction ~= wanted then return 1 end -- failsafes if direction ~= wanted then return 1 end -- failsafes
if direction == wanted then return 0 end if direction == wanted then return 0 end
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 function getnearestunplaced(buf, pbuf, cx,cy,cz,clayer, sx,sz, direction)
local distance = math.huge local distance = math.huge
local selected = nil 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)] local needplace = buf[posasstring(x,cy,y)]
if pbuf[posasstring(x,cy,y)] then needplace = 0 end -- already placed if pbuf[posasstring(x,cy,y)] then needplace = 0 end -- already placed
if clayer == 0 or clayer == 2 then needplace = needstobeplaced(needplace, clayer)
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
if needplace then -- needs to be placed, calculate distance 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) local cd = math.abs(y-cz)+math.abs(x-cx) -- raw distance (amount of blocks between)
cd = cd + extracost cd = cd + extracost
@@ -545,6 +548,107 @@ local function printdes(buf, dimensions)
center(direction) center(direction)
end 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 while true do
integritycheck() integritycheck()
local action = selopt({ local action = selopt({