make getnearestunplaced also take direction into account.

This commit is contained in:
justuswolff
2026-02-17 22:25:20 +01:00
parent 94f825c346
commit ab9d5c7b83

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}