Changeset - r26789:79b05bbf3bd2
[Not reviewed]
master
0 6 0
Rubidium - 21 months ago 2023-01-21 11:14:28
rubidium@openttd.org
Codechange: hide the map's size related fields in the Map structure
6 files changed with 44 insertions and 33 deletions:
0 comments (0 inline, 0 general)
src/crashlog.cpp
Show inline comments
 
@@ -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();
src/map.cpp
Show inline comments
 
@@ -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<Tile>(_map_size);
 
	_me = CallocT<TileExtended>(_map_size);
 
	_m = CallocT<Tile>(Map::size);
 
	_me = CallocT<TileExtended>(Map::size);
 
}
 

	
 

	
src/map_func.h
Show inline comments
 
@@ -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.
 
 *
src/misc.cpp
Show inline comments
 
@@ -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;
src/saveload/map_sl.cpp
Show inline comments
 
@@ -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
src/saveload/oldloader_sl.cpp
Show inline comments
 
@@ -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++) {
0 comments (0 inline, 0 general)