keep direction for an more optimised moving scheme

This commit is contained in:
Justus Wolff
2026-02-15 19:43:14 +01:00
parent b82bbbe5d5
commit c26ab898f0

View File

@@ -292,45 +292,43 @@ local function getnearestunplaced(buf, pbuf, cx,cy,cz,co, sx,sz, setceiling)
end end
return selected return selected
end end
local function moveto(x,y,cx,cz) local function center(direction, wanted)
local direction = 0
local function center(wanted)
if direction == 1 and wanted ~= 1 then if direction == 1 and wanted ~= 1 then
move("left") move("left")
end end
if direction == -1 and wanted ~= -1 then if direction == -1 and wanted ~= -1 then
move("right") move("right")
end end
end end
local function moveto(x,y,cx,cz,direction)
while x ~= cx or y ~= cz do while x ~= cx or y ~= cz do
if x > cx then -- x if x > cx then -- x
center() center(direction)
cx = cx + 1 cx = cx + 1
move("forward") move("forward")
end end
if x < cx then if x < cx then
center() center(direction)
cx = cx - 1 cx = cx - 1
move("back") move("back")
end end
if y > cz then -- z if y > cz then -- z
center(1) center(direction, 1)
cz = cz + 1 cz = cz + 1
move("right") move("right")
move("forward") move("forward")
direction = 1 direction = 1
end end
if y < cz then if y < cz then
center(-1) center(direction, -1)
cz = cz - 1 cz = cz - 1
move("left") move("left")
move("forward") move("forward")
direction = -1 direction = -1
end end
end end
center() return cx,cz,direction
return cx,cz
end end
local function printdes(buf, dimensions) local function printdes(buf, dimensions)
move("up") move("up")
@@ -338,13 +336,14 @@ local function printdes(buf, dimensions)
local cx = 1 local cx = 1
local cz = 1 local cz = 1
local direction = 0
for cy=1,dimensions["y"],1 do for cy=1,dimensions["y"],1 do
for _=1,3,1 do -- build walls for _=1,3,1 do -- build walls
local pbuf = {} local pbuf = {}
while true do while true do
local target = getnearestunplaced(buf, pbuf, cx,cy,cz,nil, dimensions["x"],dimensions["z"], false) local target = getnearestunplaced(buf, pbuf, cx,cy,cz,nil, dimensions["x"],dimensions["z"], false)
if not target then break end if not target then break end
cx,cz = moveto(target[1],target[2],cx,cz) cx,cz = moveto(target[1],target[2],cx,cz,direction)
place("down") place("down")
pbuf[posasstring(cx,cy,cz)] = true pbuf[posasstring(cx,cy,cz)] = true
end end
@@ -376,16 +375,17 @@ local function printdes(buf, dimensions)
-- build ceiling/floor -- build ceiling/floor
for _cz=1,dimensions["z"],1 do for _cz=1,dimensions["z"],1 do
for _cx=1,dimensions["x"],1 do for _cx=1,dimensions["x"],1 do
moveto(_cx,_cz,cx,cz) moveto(_cx,_cz,cx,cz,direction)
if buf[posasstring(_cx,cy,_cz)] ~= 2 then if buf[posasstring(_cx,cy,_cz)] ~= 2 then
place("down") place("down")
end end
end end
end end
-- return to standard pos but +1 to y -- return to standard pos but +1 to y
moveto(1,1,cx,cz) moveto(1,1,cx,cz,direction)
move("up") move("up")
end end
center(direction)
end end
while true do while true do