mirror of https://github.com/OpenTTD/OpenTTD
Codechange: hide the map's size related fields in the Map structure
parent
de6bc8e692
commit
7cdc23fd64
|
@ -413,9 +413,9 @@ bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *fil
|
||||||
*/
|
*/
|
||||||
bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
|
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. */
|
* initialised, there is a big chance the rest is initialised too. */
|
||||||
if (_m == nullptr) return false;
|
if (!Map::IsInitialized()) return false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
GamelogEmergency();
|
GamelogEmergency();
|
||||||
|
|
28
src/map.cpp
28
src/map.cpp
|
@ -20,12 +20,12 @@
|
||||||
extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned);
|
extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
uint _map_log_x; ///< 2^_map_log_x == _map_size_x
|
/* static */ uint Map::log_x; ///< 2^_map_log_x == _map_size_x
|
||||||
uint _map_log_y; ///< 2^_map_log_y == _map_size_y
|
/* static */ uint Map::log_y; ///< 2^_map_log_y == _map_size_y
|
||||||
uint _map_size_x; ///< Size of the map along the X
|
/* static */ uint Map::size_x; ///< Size of the map along the X
|
||||||
uint _map_size_y; ///< Size of the map along the Y
|
/* static */ uint Map::size_y; ///< Size of the map along the Y
|
||||||
uint _map_size; ///< The number of tiles on the map
|
/* static */ uint Map::size; ///< The number of tiles on the map
|
||||||
uint _map_tile_mask; ///< _map_size - 1 (to mask the mapsize)
|
/* static */ uint Map::tile_mask; ///< _map_size - 1 (to mask the mapsize)
|
||||||
|
|
||||||
Tile *_m = nullptr; ///< Tiles of the map
|
Tile *_m = nullptr; ///< Tiles of the map
|
||||||
TileExtended *_me = nullptr; ///< Extended Tiles of the map
|
TileExtended *_me = nullptr; ///< Extended Tiles of the map
|
||||||
|
@ -49,18 +49,18 @@ TileExtended *_me = nullptr; ///< Extended Tiles of the map
|
||||||
|
|
||||||
Debug(map, 1, "Allocating map of size {}x{}", size_x, size_y);
|
Debug(map, 1, "Allocating map of size {}x{}", size_x, size_y);
|
||||||
|
|
||||||
_map_log_x = FindFirstBit(size_x);
|
Map::log_x = FindFirstBit(size_x);
|
||||||
_map_log_y = FindFirstBit(size_y);
|
Map::log_y = FindFirstBit(size_y);
|
||||||
_map_size_x = size_x;
|
Map::size_x = size_x;
|
||||||
_map_size_y = size_y;
|
Map::size_y = size_y;
|
||||||
_map_size = size_x * size_y;
|
Map::size = size_x * size_y;
|
||||||
_map_tile_mask = _map_size - 1;
|
Map::tile_mask = Map::size - 1;
|
||||||
|
|
||||||
free(_m);
|
free(_m);
|
||||||
free(_me);
|
free(_me);
|
||||||
|
|
||||||
_m = CallocT<Tile>(_map_size);
|
_m = CallocT<Tile>(Map::size);
|
||||||
_me = CallocT<TileExtended>(_map_size);
|
_me = CallocT<TileExtended>(Map::size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,15 @@ extern TileExtended *_me;
|
||||||
* Size related data of the map.
|
* Size related data of the map.
|
||||||
*/
|
*/
|
||||||
struct 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);
|
static void Allocate(uint size_x, uint size_y);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,8 +53,7 @@ struct Map {
|
||||||
*/
|
*/
|
||||||
static inline uint LogX()
|
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()
|
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()
|
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()
|
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()
|
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)
|
static inline TileIndex WrapToMap(uint tile)
|
||||||
{
|
{
|
||||||
extern uint _map_tile_mask;
|
return tile & Map::tile_mask;
|
||||||
return tile & _map_tile_mask;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -145,9 +148,17 @@ struct Map {
|
||||||
* just half of it. */
|
* just half of it. */
|
||||||
return CeilDiv((n << Map::LogX()) + (n << Map::LogY()), 1 << 9);
|
return CeilDiv((n << Map::LogX()) + (n << Map::LogY()), 1 << 9);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
static inline void AllocateMap(uint size_x, uint size_y) { Map::Allocate(size_x, size_y); }
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An offset value between two tiles.
|
* An offset value between two tiles.
|
||||||
|
|
|
@ -61,7 +61,7 @@ void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settin
|
||||||
* related to the new game we're about to start/load. */
|
* related to the new game we're about to start/load. */
|
||||||
UnInitWindowSystem();
|
UnInitWindowSystem();
|
||||||
|
|
||||||
AllocateMap(size_x, size_y);
|
Map::Allocate(size_x, size_y);
|
||||||
|
|
||||||
_pause_mode = PM_UNPAUSED;
|
_pause_mode = PM_UNPAUSED;
|
||||||
_game_speed = 100;
|
_game_speed = 100;
|
||||||
|
|
|
@ -49,7 +49,7 @@ struct MAPSChunkHandler : ChunkHandler {
|
||||||
SlGlobList(slt);
|
SlGlobList(slt);
|
||||||
if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many MAPS entries");
|
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
|
void LoadCheck(size_t) const override
|
||||||
|
|
|
@ -1465,7 +1465,7 @@ static bool LoadOldGameDifficulty(LoadgameState *ls, int num)
|
||||||
static bool LoadOldMapPart1(LoadgameState *ls, int num)
|
static bool LoadOldMapPart1(LoadgameState *ls, int num)
|
||||||
{
|
{
|
||||||
if (_savegame_type == SGT_TTO) {
|
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++) {
|
for (uint i = 0; i < OLD_MAP_SIZE; i++) {
|
||||||
|
|
Loading…
Reference in New Issue