t8rt0t

schematic_minifier.lua

Jul 24th, 2024 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.47 KB | None | 0 0
  1. local args = {...}
  2. local nbt = require("nbt")
  3. local zip = require"LibDeflate"
  4. local file,path,scale
  5.  
  6. shell.setCompletionFunction(shell.getRunningProgram(),shell.getCompletionInfo()["rom/programs/edit.lua"].fnComplete)
  7.  
  8. for k,v in ipairs(args) do
  9.     if type(v) == "string" and fs.exists(v) then
  10.         file = v
  11.     elseif tonumber(v) then
  12.         scale = tonumber(v)
  13.     end
  14. end
  15.  
  16. path = file
  17. assert(path,'no path found')
  18. file = assert(fs.open(file,"r"))
  19. scale = math.floor(math.max(1,math.min(scale or 16,4096)))
  20. -- scale = 2^math.floor(math.log(scale)/math.log(2) + 0.5)
  21.  
  22. local data = file.readAll()
  23. file.close()
  24.  
  25. local unzipped_data = assert(zip:DecompressGzip(data))
  26. local old_schem,key = assert(nbt.frombinary(unzipped_data))
  27.  
  28. local new_schem = nbt.COMPOUND()
  29. key,old_schem = next(rawget(old_schem,"data"))
  30.  
  31. new_schem.palette = {{Name="littletiles:tiles"}}
  32. new_schem.DataVersion = 2975
  33. new_schem.size = nbt.LIST(3)
  34. for i=1,3 do
  35.     new_schem.size[i] = math.ceil(nbt.tonumber(old_schem.size[i])/scale)
  36. end
  37.  
  38. local function getKey(paletteBlock)
  39.     local str = paletteBlock.Name.data
  40.     if paletteBlock.Properties then
  41.         str = str .. "["
  42.         for k,v in pairs(paletteBlock.Properties) do
  43.             str = str .. k .. "=" .. tostring(v) .. ","
  44.         end
  45.         return str:gsub(",$","").."]"
  46.     else
  47.         return str
  48.     end
  49. end
  50.            
  51. local serializedPalette = {}
  52. for k,v in pairs(old_schem.palette) do
  53.     serializedPalette[k-1] = getKey(v)
  54. end
  55.  
  56. new_schem.blocks = nbt.LIST(10)
  57.  
  58.  
  59. --New simplification
  60. -- local function getState(tab3d)
  61. --     local _,state = pcall(function()
  62. --             local _,t = next(tab3d)
  63. --             local _,q = next(t)
  64. --             return q[next(q)] or false end) --Add for 0 size tables
  65. --     state = _ and state or false
  66. --     for x,u in pairs(tab3d) do
  67. --         if type(x) == "number" then
  68. --             for y,v in pairs(u) do
  69. --                 for z,w in pairs(v) do
  70. --                     if w ~= state then return nil end
  71. --                 end
  72. --             end
  73. --         end
  74. --     end
  75. --     return state
  76. -- end
  77.  
  78. -- local function split(tab3d)
  79. --     local sX,sY,sZ,wX,wY,wZ
  80. --     if tab3d.size then
  81. --         sX,sY,sZ,wX,wY,wZ = unpack(tab3d.size)
  82. --         tab3d = tab3d.data
  83. --     else
  84. --         sX,sY,sZ,wX,wY,wZ = 0,0,0,#tab3d-1,#tab3d[0]-1,#tab3d[0][0]-1
  85. --     end
  86. --     local hX,hY,hZ = sX+math.floor((wX-sX)/2),sY+math.floor((wY-sY)/2),sZ+math.floor((wZ-sZ)/2)
  87. --     local hx,hy,hz = hX+1,hY+1,hZ+1
  88. --     local t = {{data={}},{data={}},{data={}},{data={}},{data={}},{data={}},{data={}},{data={}}}
  89. --     for x=sX,hX do
  90. --         local tX1,tX2 = {},{}
  91. --         for y=sY,hY do
  92. --             local tY = {}
  93. --             for z=sZ,hZ do
  94. --                 tY[z] = tab3d[x][y][z] or false
  95. --             end
  96. --             tX1[y] = tY
  97. --             local tY = {}
  98. --             for z=hz,wZ do
  99. --                 tY[z] = tab3d[x][y][z] or false
  100. --             end
  101. --             tX2[y] = tY
  102. --         end
  103. --         t[1].data[x] = tX1;t[2].data[x] = tX2
  104. --         local tX1,tX2 = {},{}
  105. --         for y=hy,wY do
  106. --             local tY = {}
  107. --             for z=sZ,hZ do
  108. --                 tY[z] = tab3d[x][y][z] or false
  109. --             end
  110. --             tX1[y] = tY
  111. --             local tY = {}
  112. --             for z=hz,wZ do
  113. --                 tY[z] = tab3d[x][y][z] or false
  114. --             end
  115. --             tX2[y] = tY
  116. --         end
  117. --         t[3].data[x] = tX1;t[4].data[x] = tX2
  118. --     end
  119. --     t[1].size = {sX,sY,sZ,hX,hY,hZ}
  120. --     t[2].size = {sX,sY,hz,hX,hY,wZ}
  121. --     t[3].size = {sX,hy,sZ,hX,wY,hZ}
  122. --     t[4].size = {sX,hy,hz,hX,wY,wZ}
  123. --     for x=hx,wX do
  124. --         local tX1,tX2 = {},{}
  125. --         for y=sY,hY do
  126. --             local tY = {}
  127. --             for z=sZ,hZ do
  128. --                 tY[z] = tab3d[x][y][z] or false
  129. --             end
  130. --             tX1[y] = tY
  131. --             local tY = {}
  132. --             for z=hz,wZ do
  133. --                 tY[z] = tab3d[x][y][z] or false
  134. --             end
  135. --             tX2[y] = tY
  136. --         end
  137. --         t[5].data[x] = tX1;t[6].data[x] = tX2
  138. --         local tX1,tX2 = {},{}
  139. --         for y=hy,wY do
  140. --             local tY = {}
  141. --             for z=sZ,hZ do
  142. --                 tY[z] = tab3d[x][y][z] or false
  143. --             end
  144. --             tX1[y] = tY
  145. --             local tY = {}
  146. --             for z=hz,wZ do
  147. --                 tY[z] = tab3d[x][y][z] or false
  148. --             end
  149. --             tX2[y] = tY
  150. --         end
  151. --         t[7].data[x] = tX1;t[8].data[x] = tX2
  152. --     end
  153. --     t[5].size = {hx,sY,sZ,wX,hY,hZ}
  154. --     t[6].size = {hx,sY,hz,wX,hY,wZ}
  155. --     t[7].size = {hx,hy,sZ,wX,wY,hZ}
  156. --     t[8].size = {hx,hy,hz,wX,wY,wZ}
  157. --     return t
  158. -- end
  159.  
  160. -- local function areNeighbors(r1,r2)
  161. --     local s1,s2 = r1.size,r2.size
  162. --     local nSame,tIdx = 0,{}
  163. --     for i=1,3 do
  164. --         if s1[i] == s2[i] then
  165. --             nSame,tIdx[i+3] = nSame + 1,true --Check if exactly 2 of their top-left-back coordinates are the same
  166. --         end
  167. --     end
  168. --     if nSame ~= 2 then return false end
  169. --     for idx in pairs(tIdx) do --Check if those same axes are aligned for their bottom-right-front coordinates
  170. --         if s1[idx] ~= s2[idx] then return false end
  171. --     end
  172. --     return true
  173. -- end
  174.  
  175. -- local function extend(t1,t2)
  176. --     for k,v in pairs(t2) do
  177. --         t1[k] = v
  178. --     end
  179. -- end
  180.  
  181. -- local function merge(t1,t2)
  182. --     local t = {state=t1.state,size={}}
  183. --     for i=1,3 do
  184. --         t.size[i] = math.min(t1.size[i],t2.size[i])
  185. --     end
  186. --     for i=4,6 do
  187. --         t.size[i] = math.max(t1.size[i],t2.size[i])
  188. --     end
  189. --     return t
  190. -- end
  191.    
  192.  
  193. -- local function splitAndMerge(tab3d)
  194. --     tRegions = {}
  195. --     local t = split(tab3d)
  196. --     for k,v in pairs(t) do
  197. --         os.queueEvent("nosleep")
  198. --         os.pullEvent()
  199. --         local state = getState(v.data)
  200. --         if state then --homogenous
  201. --             table.insert(tRegions,{state=state,size=v.size})
  202. --         elseif state == nil then --Non homogenous
  203. --             extend(tRegions,splitAndMerge(v))
  204. --         end
  205. --         --all air (state = false) are ignored
  206. --     end
  207. --     local index = #tRegions
  208. --     while index > 1 do
  209. --         local r1 = tRegions[index]
  210. --         local state = r1.state
  211. --         for i=index-1,1,-1 do
  212. --             local r2 = tRegions[index]
  213. --             if r2.state == state and areNeighbors(r1,r2) then
  214. --                 tRegions[r2] = merge(r1,r2)
  215. --                 table.remove(tRegions,index)
  216. --                 break
  217. --             end
  218. --         end
  219. --         index = index - 1
  220. --     end
  221. --     return tRegions
  222. -- end
  223.  
  224. -- local blockTab = {}
  225. -- for k,v in pairs(old_schem.blocks) do
  226. --     local x,y,z = nbt.tonumber(v.pos[1]),nbt.tonumber(v.pos[2]),nbt.tonumber(v.pos[3])
  227. --     cX,cY,cZ,bX,bY,bZ = math.floor(x/scale),math.floor(y/scale),math.floor(z/scale),x%scale,y%scale,z%scale
  228. --     local key = string.format("[%d,%d,%d]",cX,cY,cZ)
  229. --     if not blockTab[key] then
  230. --         blockTab[key] = {data={},size={0,0,0,scale-1,scale-1,scale-1}}
  231. --         for i=0,scale - 1 do
  232. --             local temp = {}
  233. --             for j=0,scale - 1 do
  234. --                 temp[j] = {}
  235. --             end
  236. --             blockTab[key].data[i] = temp
  237. --         end
  238. --     end
  239. --     blockTab[key].data[bX][bY][bZ] = nbt.tonumber(v.state)
  240. -- end
  241. -- for k,v in pairs(blockTab) do
  242. --     local simplified = splitAndMerge(v)
  243. --     local tag = nbt.COMPOUND()
  244. --     tag.state = 0
  245. --     tag.pos = nbt.fromstring(k)
  246. --     local t = nbt.COMPOUND()
  247. --     t.grid = scale
  248. --     t.id = "littletiles:tiles"
  249. --     local content = nbt.COMPOUND()
  250. --     content.tiles = {}
  251. --     for j,w in pairs(simplified) do
  252. --         local state = serializedPalette[w.state]
  253. --         local size = nbt.INT_ARRAY()
  254. --         size[1] = 0
  255. --         for i=1,3 do
  256. --             size[i+1] = w.size[i]
  257. --         end
  258. --         for i=4,6 do
  259. --             size[i+1] = w.size[i]+1
  260. --         end
  261. --         -- for l,m in ipairs(w.size) do
  262. --         --     size[l] = m
  263. --         -- end
  264. --         local list = content.tiles[state]
  265. --         if list then
  266. --             list[#list+1] = size
  267. --         else
  268. --             local temp = nbt.INT_ARRAY()
  269. --             temp[1] = -1
  270. --             content.tiles[state] = nbt.LIST(11)
  271. --             content.tiles[state][1] = temp
  272. --             content.tiles[state][2] = size
  273. --         end
  274. --     end
  275. --     t.content = content
  276. --     tag.nbt = t
  277. --     new_schem.blocks[#new_schem.blocks+1] = tag
  278. -- end
  279. --New Simplification
  280.  
  281.  
  282. --test 3
  283. local function areNeighbors(r1,r2)
  284.     local s1,s2 = r1.size,r2.size
  285.     local nSame,tIdx,nGap,nIdx = 0,{},0,1
  286.     for i=1,3 do
  287.         if s1[i] == s2[i] then
  288.             nSame,tIdx[i+3] = nSame + 1,true --Check if exactly 2 of their top-left-back coordinates are the same
  289.         else
  290.             nIdx,nGap = i,math.abs(s2[i]-s1[i])
  291.         end
  292.     end
  293.     if nSame ~= 2 then return false end
  294.     for idx in pairs(tIdx) do --Check if those same axes are aligned for their bottom-right-front coordinates
  295.         if s1[idx] ~= s2[idx] then return false end
  296.     end
  297.     return (s1[nIdx+3]-s1[nIdx]+1)==nGap or (s2[nIdx+3]-s2[nIdx]+1) == nGap --check if regions touch
  298. end
  299.  
  300. local function merg(t1,t2)
  301.     local t = {state=t1.state,size={}}
  302.     for i=1,3 do
  303.         t.size[i] = math.min(t1.size[i],t2.size[i])
  304.     end
  305.     for i=4,6 do
  306.         t.size[i] = math.max(t1.size[i],t2.size[i])
  307.     end
  308.     return t
  309. end
  310.    
  311.  
  312. local function merge(blocks)
  313.     local index = #blocks
  314.     while index > 1 do
  315.         local r1 = blocks[index]
  316.         local state = r1.state
  317.         for i=#blocks,1,-1 do
  318.             local r2 = blocks[i]
  319.             if r2 ~= r1 and r2.state == state and areNeighbors(r1,r2) then
  320.                 blocks[i] = merg(r1,r2)
  321.                 table.remove(blocks,index)
  322.                 break
  323.             end
  324.         end
  325.         os.queueEvent("nosleep")
  326.         os.pullEvent()
  327.         index = index - 1
  328.     end
  329.     return blocks
  330. end
  331.  
  332. local blockTab = {}
  333. for k,v in pairs(old_schem.blocks) do
  334.     local x,y,z = nbt.tonumber(v.pos[1]),nbt.tonumber(v.pos[2]),nbt.tonumber(v.pos[3])
  335.     cX,cY,cZ,bX,bY,bZ = math.floor(x/scale),math.floor(y/scale),math.floor(z/scale),x%scale,y%scale,z%scale
  336.     local key = string.format("[%d,%d,%d]",cX,cY,cZ)
  337.     if not blockTab[key] then
  338.         blockTab[key] = {}
  339.         -- for i=0,scale - 1 do
  340.         --     local temp = {}
  341.         --     for j=0,scale - 1 do
  342.         --         temp[j] = {}
  343.         --     end
  344.         --     blockTab[key].data[i] = temp
  345.         -- end
  346.     end
  347.     table.insert(blockTab[key],{state=nbt.tonumber(v.state),size={bX,bY,bZ,bX,bY,bZ}})
  348. end
  349. for k,v in pairs(blockTab) do
  350.     local simplified = merge(v)
  351.     local tag = nbt.COMPOUND()
  352.     tag.state = 0
  353.     tag.pos = nbt.fromstring(k)
  354.     local t = nbt.COMPOUND()
  355.     t.grid = scale
  356.     t.id = "littletiles:tiles"
  357.     local content = nbt.COMPOUND()
  358.     content.tiles = {}
  359.     for j,w in pairs(simplified) do
  360.         local state = serializedPalette[w.state]
  361.         local size = nbt.INT_ARRAY()
  362.         size[1] = 0
  363.         for i=1,3 do
  364.             size[i+1] = w.size[i]
  365.         end
  366.         for i=4,6 do
  367.             size[i+1] = w.size[i]+1
  368.         end
  369.         -- for l,m in ipairs(w.size) do
  370.         --     size[l] = m
  371.         -- end
  372.         local list = content.tiles[state]
  373.         if list then
  374.             list[#list+1] = size
  375.         else
  376.             local temp = nbt.INT_ARRAY()
  377.             temp[1] = -1
  378.             content.tiles[state] = nbt.LIST(11)
  379.             content.tiles[state][1] = temp
  380.             content.tiles[state][2] = size
  381.         end
  382.     end
  383.     t.content = content
  384.     tag.nbt = t
  385.     new_schem.blocks[#new_schem.blocks+1] = tag
  386. end
  387.  
  388.  
  389. --Deprecated
  390. -- local blockTab = {}
  391. -- for k,v in pairs(old_schem.blocks) do
  392. --     local x,y,z = nbt.tonumber(v.pos[1]),nbt.tonumber(v.pos[2]),nbt.tonumber(v.pos[3])
  393. --     cX,cY,cZ,bX,bY,bZ = math.floor(x/scale),math.floor(y/scale),math.floor(z/scale),x%scale,y%scale,z%scale
  394. --     local key = string.format("[%d,%d,%d]",cX,cY,cZ)
  395. --     local blockKey = serializedPalette[nbt.tonumber(v.state)]
  396. --     if blockTab[key] then
  397. --         local b = blockTab[key].content.tiles[blockKey]
  398. --         if b then
  399. --             b[#b+1] = nbt.fromstring(string.format("[I;0,%d,%d,%d,%d,%d,%d]",bX,bY,bZ,bX+1,bY+1,bZ+1))
  400. --         else
  401. --             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))
  402. --         end
  403. --     else
  404. --         local tag = nbt.COMPOUND()
  405. --         tag.grid = scale
  406. --         tag.id = "littletiles:tiles"
  407. --         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))
  408. --         blockTab[key] = tag
  409. --     end
  410. -- end    
  411. -- for k,v in pairs(blockTab) do
  412. --     local tag = nbt.COMPOUND()
  413. --     tag.state = 0
  414. --     tag.pos = nbt.fromstring(k)
  415. --     tag.nbt = v
  416. --     new_schem.blocks[#new_schem.blocks + 1] = tag
  417. -- end
  418. --Deprecated
  419.  
  420. local t = nbt.COMPOUND()
  421. t[key .. "x"..scale] = new_schem
  422.  
  423. print(new_schem)
  424.  
  425. local binary_schem = nbt.tobinary(t)
  426. local zipped_schem = assert(zip:CompressGzip(binary_schem))
  427. local file = fs.open(fs.combine("minified/",path),"w+")
  428. file.write(zipped_schem)
  429. file.close()
  430. print("Finished!")
Advertisement
Add Comment
Please, Sign In to add comment