diff --git a/src/crashlog.cpp b/src/crashlog.cpp --- a/src/crashlog.cpp +++ b/src/crashlog.cpp @@ -413,9 +413,9 @@ bool CrashLog::WriteCrashLog(const char */ bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const { - /* If the map array doesn't exist, saving will fail too. If the map got + /* If the map doesn't exist, saving will fail too. If the map got * initialised, there is a big chance the rest is initialised too. */ - if (_m == nullptr) return false; + if (!Map::IsInitialized()) return false; try { GamelogEmergency(); diff --git a/src/map.cpp b/src/map.cpp --- a/src/map.cpp +++ b/src/map.cpp @@ -20,12 +20,12 @@ extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned); #endif -uint _map_log_x; ///< 2^_map_log_x == _map_size_x -uint _map_log_y; ///< 2^_map_log_y == _map_size_y -uint _map_size_x; ///< Size of the map along the X -uint _map_size_y; ///< Size of the map along the Y -uint _map_size; ///< The number of tiles on the map -uint _map_tile_mask; ///< _map_size - 1 (to mask the mapsize) +/* static */ uint Map::log_x; ///< 2^_map_log_x == _map_size_x +/* static */ uint Map::log_y; ///< 2^_map_log_y == _map_size_y +/* static */ uint Map::size_x; ///< Size of the map along the X +/* static */ uint Map::size_y; ///< Size of the map along the Y +/* static */ uint Map::size; ///< The number of tiles on the map +/* static */ uint Map::tile_mask; ///< _map_size - 1 (to mask the mapsize) Tile *_m = nullptr; ///< Tiles of the map TileExtended *_me = nullptr; ///< Extended Tiles of the map @@ -49,18 +49,18 @@ TileExtended *_me = nullptr; ///< Extend Debug(map, 1, "Allocating map of size {}x{}", size_x, size_y); - _map_log_x = FindFirstBit(size_x); - _map_log_y = FindFirstBit(size_y); - _map_size_x = size_x; - _map_size_y = size_y; - _map_size = size_x * size_y; - _map_tile_mask = _map_size - 1; + Map::log_x = FindFirstBit(size_x); + Map::log_y = FindFirstBit(size_y); + Map::size_x = size_x; + Map::size_y = size_y; + Map::size = size_x * size_y; + Map::tile_mask = Map::size - 1; free(_m); free(_me); - _m = CallocT(_map_size); - _me = CallocT(_map_size); + _m = CallocT(Map::size); + _me = CallocT(Map::size); } diff --git a/src/map_func.h b/src/map_func.h --- a/src/map_func.h +++ b/src/map_func.h @@ -35,6 +35,15 @@ extern TileExtended *_me; * Size related data of the map. */ struct Map { +private: + static uint log_x; ///< 2^_map_log_x == _map_size_x + static uint log_y; ///< 2^_map_log_y == _map_size_y + static uint size_x; ///< Size of the map along the X + static uint size_y; ///< Size of the map along the Y + static uint size; ///< The number of tiles on the map + static uint tile_mask; ///< _map_size - 1 (to mask the mapsize) + +public: static void Allocate(uint size_x, uint size_y); /** @@ -44,8 +53,7 @@ struct Map { */ static inline uint LogX() { - extern uint _map_log_x; - return _map_log_x; + return Map::log_x; } /** @@ -55,8 +63,7 @@ struct Map { */ static inline uint LogY() { - extern uint _map_log_y; - return _map_log_y; + return Map::log_y; } /** @@ -65,8 +72,7 @@ struct Map { */ static inline uint SizeX() { - extern uint _map_size_x; - return _map_size_x; + return Map::size_x; } /** @@ -75,8 +81,7 @@ struct Map { */ static inline uint SizeY() { - extern uint _map_size_y; - return _map_size_y; + return Map::size_y; } /** @@ -85,8 +90,7 @@ struct Map { */ static inline uint Size() { - extern uint _map_size; - return _map_size; + return Map::size; } /** @@ -115,8 +119,7 @@ struct Map { */ static inline TileIndex WrapToMap(uint tile) { - extern uint _map_tile_mask; - return tile & _map_tile_mask; + return tile & Map::tile_mask; } /** @@ -145,10 +148,18 @@ struct Map { * just half of it. */ return CeilDiv((n << Map::LogX()) + (n << Map::LogY()), 1 << 9); } + + /** + * Check whether the map has been initialized, as to not try to save the map + * during crashlog when the map is not there yet. + * @return true when the map has been allocated/initialized. + */ + static bool IsInitialized() + { + return _m != nullptr; + } }; -static inline void AllocateMap(uint size_x, uint size_y) { Map::Allocate(size_x, size_y); } - /** * An offset value between two tiles. * diff --git a/src/misc.cpp b/src/misc.cpp --- a/src/misc.cpp +++ b/src/misc.cpp @@ -61,7 +61,7 @@ void InitializeGame(uint size_x, uint si * related to the new game we're about to start/load. */ UnInitWindowSystem(); - AllocateMap(size_x, size_y); + Map::Allocate(size_x, size_y); _pause_mode = PM_UNPAUSED; _game_speed = 100; diff --git a/src/saveload/map_sl.cpp b/src/saveload/map_sl.cpp --- a/src/saveload/map_sl.cpp +++ b/src/saveload/map_sl.cpp @@ -49,7 +49,7 @@ struct MAPSChunkHandler : ChunkHandler { SlGlobList(slt); if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many MAPS entries"); - AllocateMap(_map_dim_x, _map_dim_y); + Map::Allocate(_map_dim_x, _map_dim_y); } void LoadCheck(size_t) const override diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -1465,7 +1465,7 @@ static bool LoadOldGameDifficulty(Loadga static bool LoadOldMapPart1(LoadgameState *ls, int num) { if (_savegame_type == SGT_TTO) { - AllocateMap(OLD_MAP_SIZE, OLD_MAP_SIZE); + Map::Allocate(OLD_MAP_SIZE, OLD_MAP_SIZE); } for (uint i = 0; i < OLD_MAP_SIZE; i++) {