From 89d7829c1ee27ee2c34d22f2c01a2d1ecab51634 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 24 Apr 2022 16:25:17 +0200 Subject: [PATCH] Don't crash in TilingWFC due to the result containing bad indices. This will help with debugging. --- modules/wfc/tiling_wave_form_collapse.cpp | 11 ++++++++++- modules/wfc/tiling_wave_form_collapse.h | 8 +++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/modules/wfc/tiling_wave_form_collapse.cpp b/modules/wfc/tiling_wave_form_collapse.cpp index 92827d5ec..1dfcab34c 100644 --- a/modules/wfc/tiling_wave_form_collapse.cpp +++ b/modules/wfc/tiling_wave_form_collapse.cpp @@ -36,6 +36,7 @@ Tile::ActionMap Tile::generate_action_map(const WaveFormCollapse::Symmetry symme ActionMap action_map; action_map.set_size(size); + action_map.zero(); for (int i = 0; i < size; ++i) { action_map.map[0].write[i] = i; @@ -499,7 +500,15 @@ Array2D TilingWaveFormCollapse::id_to_tiling(Array2D ids) { for (int i = 0; i < ids.height; i++) { for (int j = 0; j < ids.width; j++) { - IdToTilePair oriented_tile = id_to_oriented_tile[ids.get(i, j)]; + int id = ids.get(i, j); + + if (id < 0 || id >= id_to_oriented_tile.size()) { + id = 0; + + ERR_PRINT("id < 0 || id >= id_to_oriented_tile.size()"); + } + + IdToTilePair oriented_tile = id_to_oriented_tile[id]; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { diff --git a/modules/wfc/tiling_wave_form_collapse.h b/modules/wfc/tiling_wave_form_collapse.h index 694f99b38..31836546a 100644 --- a/modules/wfc/tiling_wave_form_collapse.h +++ b/modules/wfc/tiling_wave_form_collapse.h @@ -15,6 +15,12 @@ struct Tile { map[i].resize(size); } } + + void zero() { + for (int i = 0; i < 8; ++i) { + map[i].fill(0); + } + } }; static const uint8_t rotation_map[6][9]; @@ -163,4 +169,4 @@ private: Vector> oriented_tile_ids; }; -#endif +#endif