diff --git a/src/main.lua b/src/main.lua index a9b19ca..cd11cac 100644 --- a/src/main.lua +++ b/src/main.lua @@ -361,12 +361,31 @@ local function placebuf(buf, x, y, z) place("down") 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 selected = nil for x=1,sx,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)] if pbuf[posasstring(x,cy,y)] then needplace = 0 end -- already placed if clayer == 0 or clayer == 2 then @@ -380,6 +399,7 @@ local function getnearestunplaced(buf, pbuf, cx,cy,cz,clayer, sx,sz) end 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 if cd < distance then distance = cd selected = {x,y}