Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local nbt = require("nbt")
- local zip = require"LibDeflate"
- local file,path,scale
- shell.setCompletionFunction(shell.getRunningProgram(),shell.getCompletionInfo()["rom/programs/edit.lua"].fnComplete)
- for k,v in ipairs(args) do
- if type(v) == "string" and fs.exists(v) then
- file = v
- elseif tonumber(v) then
- scale = tonumber(v)
- end
- end
- path = file
- assert(path,'no path found')
- file = assert(fs.open(file,"r"))
- scale = math.floor(math.max(1,math.min(scale or 16,4096)))
- -- scale = 2^math.floor(math.log(scale)/math.log(2) + 0.5)
- local data = file.readAll()
- file.close()
- local unzipped_data = assert(zip:DecompressGzip(data))
- local old_schem,key = assert(nbt.frombinary(unzipped_data))
- local new_schem = nbt.COMPOUND()
- key,old_schem = next(rawget(old_schem,"data"))
- new_schem.palette = {{Name="littletiles:tiles"}}
- new_schem.DataVersion = 2975
- new_schem.size = nbt.LIST(3)
- for i=1,3 do
- new_schem.size[i] = math.ceil(nbt.tonumber(old_schem.size[i])/scale)
- end
- local function getKey(paletteBlock)
- local str = paletteBlock.Name.data
- if paletteBlock.Properties then
- str = str .. "["
- for k,v in pairs(paletteBlock.Properties) do
- str = str .. k .. "=" .. tostring(v) .. ","
- end
- return str:gsub(",$","").."]"
- else
- return str
- end
- end
- local serializedPalette = {}
- for k,v in pairs(old_schem.palette) do
- serializedPalette[k-1] = getKey(v)
- end
- new_schem.blocks = nbt.LIST(10)
- --New simplification
- -- local function getState(tab3d)
- -- local _,state = pcall(function()
- -- local _,t = next(tab3d)
- -- local _,q = next(t)
- -- return q[next(q)] or false end) --Add for 0 size tables
- -- state = _ and state or false
- -- for x,u in pairs(tab3d) do
- -- if type(x) == "number" then
- -- for y,v in pairs(u) do
- -- for z,w in pairs(v) do
- -- if w ~= state then return nil end
- -- end
- -- end
- -- end
- -- end
- -- return state
- -- end
- -- local function split(tab3d)
- -- local sX,sY,sZ,wX,wY,wZ
- -- if tab3d.size then
- -- sX,sY,sZ,wX,wY,wZ = unpack(tab3d.size)
- -- tab3d = tab3d.data
- -- else
- -- sX,sY,sZ,wX,wY,wZ = 0,0,0,#tab3d-1,#tab3d[0]-1,#tab3d[0][0]-1
- -- end
- -- local hX,hY,hZ = sX+math.floor((wX-sX)/2),sY+math.floor((wY-sY)/2),sZ+math.floor((wZ-sZ)/2)
- -- local hx,hy,hz = hX+1,hY+1,hZ+1
- -- local t = {{data={}},{data={}},{data={}},{data={}},{data={}},{data={}},{data={}},{data={}}}
- -- for x=sX,hX do
- -- local tX1,tX2 = {},{}
- -- for y=sY,hY do
- -- local tY = {}
- -- for z=sZ,hZ do
- -- tY[z] = tab3d[x][y][z] or false
- -- end
- -- tX1[y] = tY
- -- local tY = {}
- -- for z=hz,wZ do
- -- tY[z] = tab3d[x][y][z] or false
- -- end
- -- tX2[y] = tY
- -- end
- -- t[1].data[x] = tX1;t[2].data[x] = tX2
- -- local tX1,tX2 = {},{}
- -- for y=hy,wY do
- -- local tY = {}
- -- for z=sZ,hZ do
- -- tY[z] = tab3d[x][y][z] or false
- -- end
- -- tX1[y] = tY
- -- local tY = {}
- -- for z=hz,wZ do
- -- tY[z] = tab3d[x][y][z] or false
- -- end
- -- tX2[y] = tY
- -- end
- -- t[3].data[x] = tX1;t[4].data[x] = tX2
- -- end
- -- t[1].size = {sX,sY,sZ,hX,hY,hZ}
- -- t[2].size = {sX,sY,hz,hX,hY,wZ}
- -- t[3].size = {sX,hy,sZ,hX,wY,hZ}
- -- t[4].size = {sX,hy,hz,hX,wY,wZ}
- -- for x=hx,wX do
- -- local tX1,tX2 = {},{}
- -- for y=sY,hY do
- -- local tY = {}
- -- for z=sZ,hZ do
- -- tY[z] = tab3d[x][y][z] or false
- -- end
- -- tX1[y] = tY
- -- local tY = {}
- -- for z=hz,wZ do
- -- tY[z] = tab3d[x][y][z] or false
- -- end
- -- tX2[y] = tY
- -- end
- -- t[5].data[x] = tX1;t[6].data[x] = tX2
- -- local tX1,tX2 = {},{}
- -- for y=hy,wY do
- -- local tY = {}
- -- for z=sZ,hZ do
- -- tY[z] = tab3d[x][y][z] or false
- -- end
- -- tX1[y] = tY
- -- local tY = {}
- -- for z=hz,wZ do
- -- tY[z] = tab3d[x][y][z] or false
- -- end
- -- tX2[y] = tY
- -- end
- -- t[7].data[x] = tX1;t[8].data[x] = tX2
- -- end
- -- t[5].size = {hx,sY,sZ,wX,hY,hZ}
- -- t[6].size = {hx,sY,hz,wX,hY,wZ}
- -- t[7].size = {hx,hy,sZ,wX,wY,hZ}
- -- t[8].size = {hx,hy,hz,wX,wY,wZ}
- -- return t
- -- end
- -- local function areNeighbors(r1,r2)
- -- local s1,s2 = r1.size,r2.size
- -- local nSame,tIdx = 0,{}
- -- for i=1,3 do
- -- if s1[i] == s2[i] then
- -- nSame,tIdx[i+3] = nSame + 1,true --Check if exactly 2 of their top-left-back coordinates are the same
- -- end
- -- end
- -- if nSame ~= 2 then return false end
- -- for idx in pairs(tIdx) do --Check if those same axes are aligned for their bottom-right-front coordinates
- -- if s1[idx] ~= s2[idx] then return false end
- -- end
- -- return true
- -- end
- -- local function extend(t1,t2)
- -- for k,v in pairs(t2) do
- -- t1[k] = v
- -- end
- -- end
- -- local function merge(t1,t2)
- -- local t = {state=t1.state,size={}}
- -- for i=1,3 do
- -- t.size[i] = math.min(t1.size[i],t2.size[i])
- -- end
- -- for i=4,6 do
- -- t.size[i] = math.max(t1.size[i],t2.size[i])
- -- end
- -- return t
- -- end
- -- local function splitAndMerge(tab3d)
- -- tRegions = {}
- -- local t = split(tab3d)
- -- for k,v in pairs(t) do
- -- os.queueEvent("nosleep")
- -- os.pullEvent()
- -- local state = getState(v.data)
- -- if state then --homogenous
- -- table.insert(tRegions,{state=state,size=v.size})
- -- elseif state == nil then --Non homogenous
- -- extend(tRegions,splitAndMerge(v))
- -- end
- -- --all air (state = false) are ignored
- -- end
- -- local index = #tRegions
- -- while index > 1 do
- -- local r1 = tRegions[index]
- -- local state = r1.state
- -- for i=index-1,1,-1 do
- -- local r2 = tRegions[index]
- -- if r2.state == state and areNeighbors(r1,r2) then
- -- tRegions[r2] = merge(r1,r2)
- -- table.remove(tRegions,index)
- -- break
- -- end
- -- end
- -- index = index - 1
- -- end
- -- return tRegions
- -- end
- -- local blockTab = {}
- -- for k,v in pairs(old_schem.blocks) do
- -- local x,y,z = nbt.tonumber(v.pos[1]),nbt.tonumber(v.pos[2]),nbt.tonumber(v.pos[3])
- -- cX,cY,cZ,bX,bY,bZ = math.floor(x/scale),math.floor(y/scale),math.floor(z/scale),x%scale,y%scale,z%scale
- -- local key = string.format("[%d,%d,%d]",cX,cY,cZ)
- -- if not blockTab[key] then
- -- blockTab[key] = {data={},size={0,0,0,scale-1,scale-1,scale-1}}
- -- for i=0,scale - 1 do
- -- local temp = {}
- -- for j=0,scale - 1 do
- -- temp[j] = {}
- -- end
- -- blockTab[key].data[i] = temp
- -- end
- -- end
- -- blockTab[key].data[bX][bY][bZ] = nbt.tonumber(v.state)
- -- end
- -- for k,v in pairs(blockTab) do
- -- local simplified = splitAndMerge(v)
- -- local tag = nbt.COMPOUND()
- -- tag.state = 0
- -- tag.pos = nbt.fromstring(k)
- -- local t = nbt.COMPOUND()
- -- t.grid = scale
- -- t.id = "littletiles:tiles"
- -- local content = nbt.COMPOUND()
- -- content.tiles = {}
- -- for j,w in pairs(simplified) do
- -- local state = serializedPalette[w.state]
- -- local size = nbt.INT_ARRAY()
- -- size[1] = 0
- -- for i=1,3 do
- -- size[i+1] = w.size[i]
- -- end
- -- for i=4,6 do
- -- size[i+1] = w.size[i]+1
- -- end
- -- -- for l,m in ipairs(w.size) do
- -- -- size[l] = m
- -- -- end
- -- local list = content.tiles[state]
- -- if list then
- -- list[#list+1] = size
- -- else
- -- local temp = nbt.INT_ARRAY()
- -- temp[1] = -1
- -- content.tiles[state] = nbt.LIST(11)
- -- content.tiles[state][1] = temp
- -- content.tiles[state][2] = size
- -- end
- -- end
- -- t.content = content
- -- tag.nbt = t
- -- new_schem.blocks[#new_schem.blocks+1] = tag
- -- end
- --New Simplification
- --test 3
- local function areNeighbors(r1,r2)
- local s1,s2 = r1.size,r2.size
- local nSame,tIdx,nGap,nIdx = 0,{},0,1
- for i=1,3 do
- if s1[i] == s2[i] then
- nSame,tIdx[i+3] = nSame + 1,true --Check if exactly 2 of their top-left-back coordinates are the same
- else
- nIdx,nGap = i,math.abs(s2[i]-s1[i])
- end
- end
- if nSame ~= 2 then return false end
- for idx in pairs(tIdx) do --Check if those same axes are aligned for their bottom-right-front coordinates
- if s1[idx] ~= s2[idx] then return false end
- end
- return (s1[nIdx+3]-s1[nIdx]+1)==nGap or (s2[nIdx+3]-s2[nIdx]+1) == nGap --check if regions touch
- end
- local function merg(t1,t2)
- local t = {state=t1.state,size={}}
- for i=1,3 do
- t.size[i] = math.min(t1.size[i],t2.size[i])
- end
- for i=4,6 do
- t.size[i] = math.max(t1.size[i],t2.size[i])
- end
- return t
- end
- local function merge(blocks)
- local index = #blocks
- while index > 1 do
- local r1 = blocks[index]
- local state = r1.state
- for i=#blocks,1,-1 do
- local r2 = blocks[i]
- if r2 ~= r1 and r2.state == state and areNeighbors(r1,r2) then
- blocks[i] = merg(r1,r2)
- table.remove(blocks,index)
- break
- end
- end
- os.queueEvent("nosleep")
- os.pullEvent()
- index = index - 1
- end
- return blocks
- end
- local blockTab = {}
- for k,v in pairs(old_schem.blocks) do
- local x,y,z = nbt.tonumber(v.pos[1]),nbt.tonumber(v.pos[2]),nbt.tonumber(v.pos[3])
- cX,cY,cZ,bX,bY,bZ = math.floor(x/scale),math.floor(y/scale),math.floor(z/scale),x%scale,y%scale,z%scale
- local key = string.format("[%d,%d,%d]",cX,cY,cZ)
- if not blockTab[key] then
- blockTab[key] = {}
- -- for i=0,scale - 1 do
- -- local temp = {}
- -- for j=0,scale - 1 do
- -- temp[j] = {}
- -- end
- -- blockTab[key].data[i] = temp
- -- end
- end
- table.insert(blockTab[key],{state=nbt.tonumber(v.state),size={bX,bY,bZ,bX,bY,bZ}})
- end
- for k,v in pairs(blockTab) do
- local simplified = merge(v)
- local tag = nbt.COMPOUND()
- tag.state = 0
- tag.pos = nbt.fromstring(k)
- local t = nbt.COMPOUND()
- t.grid = scale
- t.id = "littletiles:tiles"
- local content = nbt.COMPOUND()
- content.tiles = {}
- for j,w in pairs(simplified) do
- local state = serializedPalette[w.state]
- local size = nbt.INT_ARRAY()
- size[1] = 0
- for i=1,3 do
- size[i+1] = w.size[i]
- end
- for i=4,6 do
- size[i+1] = w.size[i]+1
- end
- -- for l,m in ipairs(w.size) do
- -- size[l] = m
- -- end
- local list = content.tiles[state]
- if list then
- list[#list+1] = size
- else
- local temp = nbt.INT_ARRAY()
- temp[1] = -1
- content.tiles[state] = nbt.LIST(11)
- content.tiles[state][1] = temp
- content.tiles[state][2] = size
- end
- end
- t.content = content
- tag.nbt = t
- new_schem.blocks[#new_schem.blocks+1] = tag
- end
- --Deprecated
- -- local blockTab = {}
- -- for k,v in pairs(old_schem.blocks) do
- -- local x,y,z = nbt.tonumber(v.pos[1]),nbt.tonumber(v.pos[2]),nbt.tonumber(v.pos[3])
- -- cX,cY,cZ,bX,bY,bZ = math.floor(x/scale),math.floor(y/scale),math.floor(z/scale),x%scale,y%scale,z%scale
- -- local key = string.format("[%d,%d,%d]",cX,cY,cZ)
- -- local blockKey = serializedPalette[nbt.tonumber(v.state)]
- -- if blockTab[key] then
- -- local b = blockTab[key].content.tiles[blockKey]
- -- if b then
- -- b[#b+1] = nbt.fromstring(string.format("[I;0,%d,%d,%d,%d,%d,%d]",bX,bY,bZ,bX+1,bY+1,bZ+1))
- -- else
- -- blockTab[key].content.tiles[blockKey] = nbt.fromstring(string.format("[[I;-1],[I;0,%d,%d,%d,%d,%d,%d]]",bX,bY,bZ,bX+1,bY+1,bZ+1))
- -- end
- -- else
- -- local tag = nbt.COMPOUND()
- -- tag.grid = scale
- -- tag.id = "littletiles:tiles"
- -- tag.content = nbt.fromstring(string.format("{tiles:{%q:[[I;-1],[I;0,%d,%d,%d,%d,%d,%d]]}}",blockKey,bX,bY,bZ,bX+1,bY+1,bZ+1))
- -- blockTab[key] = tag
- -- end
- -- end
- -- for k,v in pairs(blockTab) do
- -- local tag = nbt.COMPOUND()
- -- tag.state = 0
- -- tag.pos = nbt.fromstring(k)
- -- tag.nbt = v
- -- new_schem.blocks[#new_schem.blocks + 1] = tag
- -- end
- --Deprecated
- local t = nbt.COMPOUND()
- t[key .. "x"..scale] = new_schem
- print(new_schem)
- local binary_schem = nbt.tobinary(t)
- local zipped_schem = assert(zip:CompressGzip(binary_schem))
- local file = fs.open(fs.combine("minified/",path),"w+")
- file.write(zipped_schem)
- file.close()
- print("Finished!")
Advertisement
Add Comment
Please, Sign In to add comment