Compare commits

...

2 Commits

Author SHA1 Message Date
justuswolff
a6d267765d refactor code to provide newest needed argument to getnearestunplaced. 2026-02-17 22:26:11 +01:00
justuswolff
ab9d5c7b83 make getnearestunplaced also take direction into account. 2026-02-17 22:25:20 +01:00

View File

@@ -361,12 +361,31 @@ local function placebuf(buf, x, y, z)
place("down") place("down")
end end
end end
local function getnearestunplaced(buf, pbuf, cx,cy,cz,clayer, sx,sz) local function directiondist(direction, wanted)
if direction == 0 then return 1 end
if direction == 1 and wanted == -1 then return 2 end
if direction == -1 and wanted == 1 then return 2 end
if direction ~= wanted then return 1 end -- failsafes
if direction == wanted then return 0 end
end
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
for x=1,sx,1 do for x=1,sx,1 do
for y=1,sz,1 do for y=1,sz,1 do
local extracost = 0
if x ~= cx and direction ~= 0 then
extracost = extracost + 1
end
if y > cz and direction ~= 1 then
extracost = extracost + directiondist(direction, 1)
end
if y < cz and direction ~= -1 then
extracost = extracost + directiondist(direction, -1)
end
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 if clayer == 0 or clayer == 2 then
@@ -380,6 +399,7 @@ local function getnearestunplaced(buf, pbuf, cx,cy,cz,clayer, sx,sz)
end 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
if cd < distance then if cd < distance then
distance = cd distance = cd
selected = {x,y} selected = {x,y}
@@ -479,7 +499,7 @@ local function printdes(buf, dimensions)
for clayer=1,3,1 do -- build walls for clayer=1,3,1 do -- build walls
local pbuf = {} local pbuf = {}
while true do while true do
local target = getnearestunplaced(buf, pbuf, cx,cy,cz,clayer, dimensions["x"],dimensions["z"]) local target = getnearestunplaced(buf, pbuf, cx,cy,cz,clayer, dimensions["x"],dimensions["z"], direction)
if not target then break end if not target then break end
render(buf, pbuf, cx,cy,cz, target[1],target[2], dimensions["x"],dimensions["z"], setlists[clayer]) render(buf, pbuf, cx,cy,cz, target[1],target[2], dimensions["x"],dimensions["z"], setlists[clayer])
@@ -505,7 +525,7 @@ local function printdes(buf, dimensions)
end end
end end
while true do while true do
local target = getnearestunplaced(cbuf, pbuf, cx,0,cz,0, dimensions["x"],dimensions["z"]) local target = getnearestunplaced(cbuf, pbuf, cx,0,cz,0, dimensions["x"],dimensions["z"], direction)
if not target then break end if not target then break end
render(cbuf, pbuf, cx,0,cz, target[1],target[2], dimensions["x"],dimensions["z"], {1}) render(cbuf, pbuf, cx,0,cz, target[1],target[2], dimensions["x"],dimensions["z"], {1})