add an way to "fill" things

This commit is contained in:
justuswolff
2026-02-17 21:51:11 +01:00
parent 57e0872455
commit 9d60c43a7c

View File

@@ -74,6 +74,32 @@ local function load(name)
content = textutils.unserialiseJSON(content) content = textutils.unserialiseJSON(content)
return content["buf"],content["dimensions"] return content["buf"],content["dimensions"]
end end
local function bufistype(buf, x,y,z, targettype)
return buf[posasstring(x,y,z)] == targettype or (targettype == 0 and not buf[posasstring(x,y,z)])
end
local function fill_getneighbors(x,y,z, sx,sz, seltype,buf)
local out = {}
if x ~= sx and bufistype(buf, x+1,y,z, seltype) then table.insert(out, {x+1,z}) end
if x ~= 1 and bufistype(buf, x-1,y,z, seltype) then table.insert(out, {x-1,z}) end
if z ~= sz and bufistype(buf, x,y,z+1, seltype) then table.insert(out, {x,z+1}) end
if z ~= 1 and bufistype(buf, x,y,z-1, seltype) then table.insert(out, {x,z-1}) end
return out
end
local function fill(ntype, x,y,z, buf, sx,sz)
local inittype = buf[posasstring(x,y,z)]
local neighbors = fill_getneighbors(x,y,z, sx,sz, inittype,buf)
buf[posasstring(x,y,z)] = ntype
while #neighbors > 0 do
local cn = table.remove(neighbors, 1)
local cx,cz = cn[1],cn[2]
local newneigh = fill_getneighbors(cx,y,cz, sx,sz, inittype,buf)
for _,v in pairs(newneigh) do
table.insert(neighbors, newneigh)
end
buf[posasstring(x,y,z)] = ntype
end
end
local function newdesign() local function newdesign()
local buf = {} local buf = {}
local dimensions = { local dimensions = {
@@ -102,6 +128,9 @@ local function newdesign()
term.blit("W", colors.toBlit(colors.black), colors.toBlit(colors.white)) term.blit("W", colors.toBlit(colors.black), colors.toBlit(colors.white))
end end
end end
local shift = false
fill()
while true do while true do
-- render buf -- render buf
@@ -142,12 +171,21 @@ local function newdesign()
event[4] = event[4]-camy event[4] = event[4]-camy
if event[3] <= dimensions["x"] and event[4] <= dimensions["z"] then if event[3] <= dimensions["x"] and event[4] <= dimensions["z"] then
if event[2] == 1 then -- left button, set if event[2] == 1 then -- left button, set
if shift then
fill(1, event[3],currentfloor,event[4], buf,dimensions["x"],dimensions["z"])
end
buf[posasstring(event[3], currentfloor, event[4])] = 1 buf[posasstring(event[3], currentfloor, event[4])] = 1
end end
if event[2] == 3 then -- middle button, set special block if event[2] == 3 then -- middle button, set special block
if shift then
fill(currentblock, event[3],currentfloor,event[4], buf,dimensions["x"],dimensions["z"])
end
buf[posasstring(event[3], currentfloor, event[4])] = currentblock buf[posasstring(event[3], currentfloor, event[4])] = currentblock
end end
if event[2] == 2 then -- right button, erase if event[2] == 2 then -- right button, erase
if shift then
fill(0, event[3],currentfloor,event[4], buf,dimensions["x"],dimensions["z"])
end
buf[posasstring(event[3], currentfloor, event[4])] = 0 buf[posasstring(event[3], currentfloor, event[4])] = 0
end end
end end
@@ -162,6 +200,9 @@ local function newdesign()
currentblock = blocks[blockindex] currentblock = blocks[blockindex]
end end
if event[1] == "key" then if event[1] == "key" then
if keys.getName(event[2]) == "leftShift" then
shift = true
end
if keys.getName(event[2]) == "leftCtrl" then -- menu if keys.getName(event[2]) == "leftCtrl" then -- menu
local action = selopt({ local action = selopt({
"Save", "Save",
@@ -240,6 +281,9 @@ local function newdesign()
camx = camx - 1 camx = camx - 1
end end
end end
if event[1] == "key_up" then
if keys.getName(event[2]) == "leftShift" then shift = false end
end
end end
end end
local function move(direction) local function move(direction)