From a899e40c736705410c1aededb80f8ce540003d42 Mon Sep 17 00:00:00 2001 From: lustlion Date: Mon, 7 Feb 2022 12:07:57 +0100 Subject: [PATCH] Two dimensional optimization, yay --- data/scripts/editor.lua | 1 + data/scripts/level.lua | 59 +++++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/data/scripts/editor.lua b/data/scripts/editor.lua index 425d545..af7ef9b 100644 --- a/data/scripts/editor.lua +++ b/data/scripts/editor.lua @@ -57,6 +57,7 @@ function EditorStep() if Keybind:HasPressed(Keybind.debug.editor) then editor_mode = false + TileCreateObjects() end end diff --git a/data/scripts/level.lua b/data/scripts/level.lua index d924e66..d3f23c5 100644 --- a/data/scripts/level.lua +++ b/data/scripts/level.lua @@ -115,7 +115,6 @@ end function LevelReloadTiles() LevelUpdateDimensions() - TileCreateObjects() end function LevelUpdateDimensions() @@ -245,7 +244,7 @@ function GridDisplay() end function TileOptimizeObjects() - logPrint("new optimization") + logPrint("Optimizing Objects...") local unoptimized = 0 local isTileOptimized = {} @@ -263,25 +262,57 @@ function TileOptimizeObjects() local light = TileData[LevelTiles[i][j].id].light if type == "whole" and not isTileOptimized[i][j] then + isTileOptimized[i][j] = true + local n = 1 local check = true while check do - check = false - if LevelTiles[i][j+n] ~= nil then - if TileData[LevelTiles[i][j+n].id] ~= nil then - local type_check = TileData[LevelTiles[i][j+n].id].type - if type_check == "whole" and not isTileOptimized[i][j+n] then - check = true - isTileOptimized[i][j+n] = true - n = n + 1 - end + check = false + if LevelTiles[i][j+n] ~= nil + and TileData[LevelTiles[i][j+n].id] ~= nil + then + local type_check = TileData[LevelTiles[i][j+n].id].type + if type_check == "whole" + and not isTileOptimized[i][j+n] + then + check = true + isTileOptimized[i][j+n] = true + n = n + 1 end end end - logPrint("Group size: "..n) - unoptimized = unoptimized + n + local m = 1 + local check = true + while check do + check = false + local checkline = true + for l = 0, n-1 do + checkline = false + if LevelTiles[i+m] ~= nil + and LevelTiles[i+m][j+l] ~= nil + and TileData[LevelTiles[i+m][j+l].id] ~= nil + then + local type_check = TileData[LevelTiles[i+m][j+l].id].type + if type_check == "whole" + and not isTileOptimized[i+m][j+n] + then + checkline = true + end + end + end + if checkline then + check = true + for l = 0, n-1 do + isTileOptimized[i+m][j+l] = true + end + m = m + 1 + end + end + + logPrint("Group size: "..m.."x"..n) + unoptimized = unoptimized + m * n local base_x = tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.height) local base_y = tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height) @@ -289,7 +320,7 @@ function TileOptimizeObjects() base_x, base_y, base_x + tileProperties.width * tileProperties.scale * n, - base_y + tileProperties.height * tileProperties.scale + base_y + tileProperties.height * tileProperties.scale * m ) table.insert(LoadedObjects.Collisions,col) end