From 9b1f154e5be860ae3078e1661e2aac00f4ec8a6b Mon Sep 17 00:00:00 2001 From: Primekick Date: Wed, 27 Nov 2024 14:50:57 +0100 Subject: [PATCH] Extract tile data recreation method --- src/tilemap_layer.cpp | 22 ++++++++++++---------- src/tilemap_layer.h | 1 + 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/tilemap_layer.cpp b/src/tilemap_layer.cpp index 82b49fd4b0..f4e1dff8c3 100644 --- a/src/tilemap_layer.cpp +++ b/src/tilemap_layer.cpp @@ -464,6 +464,12 @@ void TilemapLayer::CreateTileCacheAt(int x, int y, int tile_id) { GetDataCache(x, y) = tile; } +void TilemapLayer::RecreateTileDataAt(int x, int y, int tile_id) { + map_data[x + y * width] = static_cast(tile_id); + Game_Map::ReplaceTileAt(x, y, tile_id, layer); + CreateTileCacheAt(x, y, tile_id); +} + void TilemapLayer::GenerateAutotileAB(short ID, short animID) { // Calculate the block to use // 1: A1 + Upper B (Grass + Coast) @@ -716,6 +722,10 @@ void TilemapLayer::SetMapData(std::vector nmap_data) { map_data = std::move(nmap_data); } +static inline bool IsAutotileAB(int tile_id) { + return tile_id >= BLOCK_A && tile_id < BLOCK_C; +} + void TilemapLayer::SetMapTileDataAt(int x, int y, int tile_id, bool disable_autotile) { if(!IsInMapBounds(x, y)) return; @@ -723,9 +733,7 @@ void TilemapLayer::SetMapTileDataAt(int x, int y, int tile_id, bool disable_auto substitutions = Game_Map::GetTilesLayer(layer); if (disable_autotile) { - map_data[x + y * width] = static_cast(tile_id); - Game_Map::ReplaceTileAt(x, y, tile_id, layer); - CreateTileCacheAt(x, y, tile_id); + RecreateTileDataAt(x, y, tile_id); } else { // Recalculate the replaced tile itself + every neighboring tile static constexpr struct { int dx; int dy; } adjacent[8] = { @@ -747,10 +755,6 @@ void TilemapLayer::SetMapTileDataAt(int x, int y, int tile_id, bool disable_auto SetMapData(map_data); } -static inline bool IsAutotileAB(int tile_id) { - return tile_id >= BLOCK_A && tile_id < BLOCK_C; -} - static inline bool IsAutotileD(int tile_id) { return tile_id >= BLOCK_D && tile_id < BLOCK_E; } @@ -815,9 +819,7 @@ void TilemapLayer::RecalculateAutotile(int x, int y, int tile_id) { // Recalculate tile id using the neighbors -> variant map const int new_tile_id = BLOCK_D + block * BLOCK_D_STRIDE + AUTOTILE_D_VARIANTS_MAP.at(neighbors); - map_data[x + y * width] = static_cast(new_tile_id); - Game_Map::ReplaceTileAt(x, y, new_tile_id, layer); - CreateTileCacheAt(x, y, tile_id); + RecreateTileDataAt(x, y, new_tile_id); } void TilemapLayer::SetPassable(std::vector npassable) { diff --git a/src/tilemap_layer.h b/src/tilemap_layer.h index 91a8adfc80..1cf2c88c43 100644 --- a/src/tilemap_layer.h +++ b/src/tilemap_layer.h @@ -117,6 +117,7 @@ class TilemapLayer { void CreateTileCache(const std::vector& nmap_data); void CreateTileCacheAt(int x, int y, int tile_id); + void RecreateTileDataAt(int x, int y, int tile_id); void GenerateAutotileAB(short ID, short animID); void GenerateAutotileD(short ID); void DrawTile(Bitmap& dst, Bitmap& tile, Bitmap& tone_tile, int x, int y, int row, int col, uint32_t tone_hash, bool allow_fast_blit = true);