diff --git a/src/main.lua b/src/main.lua index 6fe3453..ec49305 100644 --- a/src/main.lua +++ b/src/main.lua @@ -74,6 +74,32 @@ local function load(name) content = textutils.unserialiseJSON(content) return content["buf"],content["dimensions"] 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 buf = {} local dimensions = { @@ -102,6 +128,9 @@ local function newdesign() term.blit("W", colors.toBlit(colors.black), colors.toBlit(colors.white)) end end + local shift = false + + fill() while true do -- render buf @@ -142,12 +171,21 @@ local function newdesign() event[4] = event[4]-camy if event[3] <= dimensions["x"] and event[4] <= dimensions["z"] then 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 end 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 end 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 end end @@ -162,6 +200,9 @@ local function newdesign() currentblock = blocks[blockindex] end if event[1] == "key" then + if keys.getName(event[2]) == "leftShift" then + shift = true + end if keys.getName(event[2]) == "leftCtrl" then -- menu local action = selopt({ "Save", @@ -240,6 +281,9 @@ local function newdesign() camx = camx - 1 end end + if event[1] == "key_up" then + if keys.getName(event[2]) == "leftShift" then shift = false end + end end end local function move(direction)