From 742a98b179acedb3eaf0de0f32e6247065dda49e Mon Sep 17 00:00:00 2001 From: Maruno17 Date: Mon, 19 Oct 2020 21:48:13 +0100 Subject: [PATCH] Added debug option to properly erase invalid tiles from maps --- Data/Scripts/021_Debug/001_Debug_Menu.rb | 4 ++ Data/Scripts/021_Debug/002_Debug_Actions.rb | 70 +++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/Data/Scripts/021_Debug/001_Debug_Menu.rb b/Data/Scripts/021_Debug/001_Debug_Menu.rb index 551fc2ed3..e0b683471 100644 --- a/Data/Scripts/021_Debug/001_Debug_Menu.rb +++ b/Data/Scripts/021_Debug/001_Debug_Menu.rb @@ -208,6 +208,8 @@ def pbDebugMenuCommands(showall=true) _INTL("Fully compile all data.")) commands.add("othermenu","debugconsole",_INTL("Debug Console"), _INTL("Open the Debug Console.")) + commands.add("othermenu","invalidtiles",_INTL("Fix Invalid Tiles"), + _INTL("Scans all maps and erases non-existent tiles.")) return commands end @@ -795,6 +797,8 @@ def pbDebugMenuActions(cmd="",sprites=nil,viewport=nil) pbDisposeMessageWindow(msgwindow) when "debugconsole" Console::setup_console + when "invalidtiles" + pbDebugFixInvalidTiles end return false end diff --git a/Data/Scripts/021_Debug/002_Debug_Actions.rb b/Data/Scripts/021_Debug/002_Debug_Actions.rb index 7d0495221..4e8a732cd 100644 --- a/Data/Scripts/021_Debug/002_Debug_Actions.rb +++ b/Data/Scripts/021_Debug/002_Debug_Actions.rb @@ -796,6 +796,76 @@ def pbImportAllAnimations end end +#=============================================================================== +# Properly erases all non-existent tiles in maps (including event graphics) +#=============================================================================== +def pbDebugFixInvalidTiles + num_errors = 0 + num_error_maps = 0 + @tilesets = pbLoadRxData("Data/Tilesets") + mapData = MapData.new + t = Time.now.to_i + Graphics.update + for id in mapData.mapinfos.keys.sort + if Time.now.to_i - t >= 5 + Graphics.update + t = Time.now.to_i + end + changed = false + map = mapData.getMap(id) + next if !map || !mapData.mapinfos[id] + Win32API.SetWindowText(_INTL("Processing map {1} ({2})", id, mapData.mapinfos[id].name)) + passages = mapData.getTilesetPassages(map, id) + # Check all tiles in map for non-existent tiles + for x in 0...map.data.xsize + for y in 0...map.data.ysize + for i in 0...map.data.zsize + tile_id = map.data[x, y, i] + next if pbCheckTileValidity(tile_id, map, @tilesets, passages) + map.data[x, y, i] = 0 + changed = true + num_errors += 1 + end + end + end + # Check all events in map for page graphics using a non-existent tile + for key in map.events.keys + event = map.events[key] + for page in event.pages + next if page.graphic.tile_id <= 0 + next if pbCheckTileValidity(page.graphic.tile_id, map, @tilesets, passages) + page.graphic.tile_id = 0 + changed = true + num_errors += 1 + end + end + next if !changed + # Map was changed; save it + num_error_maps += 1 + mapData.saveMap(id) + end + if num_error_maps == 0 + pbMessage(_INTL("No invalid tiles were found.")) + else + pbMessage(_INTL("{1} error(s) were found across {2} map(s) and fixed.", num_errors, num_error_maps)) + pbMessage(_INTL("Close RPG Maker XP to ensure the changes are applied properly.")) + end +end + +def pbCheckTileValidity(tile_id, map, tilesets, passages) + return false if !tile_id + if tile_id > 0 && tile_id < 384 + # Check for defined autotile + autotile_id = tile_id / 48 - 1 + autotile_name = tilesets[map.tileset_id].autotile_names[autotile_id] + return true if autotile_name && autotile_name != "" + else + # Check for tileset data + return true if passages[tile_id] + end + return false +end + #===============================================================================