Changeset - r26789:79b05bbf3bd2
[Not reviewed]
master
0 6 0
Rubidium - 20 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
 
@@ -410,15 +410,15 @@ bool CrashLog::WriteCrashLog(const char 
 
 * @param filename      Output for the filename of the written file.
 
 * @param filename_last The last position in the filename buffer.
 
 * @return true when the crash save was successfully made.
 
 */
 
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();
 

	
 
		this->CreateFileName(filename, filename_last, ".sav");
 

	
src/map.cpp
Show inline comments
 
@@ -17,18 +17,18 @@
 

	
 
#if defined(_MSC_VER)
 
/* Why the hell is that not in all MSVC headers?? */
 
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
 

	
 

	
 
/**
 
@@ -46,24 +46,24 @@ TileExtended *_me = nullptr; ///< Extend
 
			(size_y & (size_y - 1)) != 0) {
 
		error("Invalid map size");
 
	}
 

	
 
	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);
 
}
 

	
 

	
 
#ifdef _DEBUG
 
TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
 
	const char *exp, const char *file, int line)
src/map_func.h
Show inline comments
 
@@ -32,64 +32,68 @@ extern Tile *_m;
 
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);
 

	
 
	/**
 
	 * Logarithm of the map size along the X side.
 
	 * @note try to avoid using this one
 
	 * @return 2^"return value" == Map::SizeX()
 
	 */
 
	static inline uint LogX()
 
	{
 
		extern uint _map_log_x;
 
		return _map_log_x;
 
		return Map::log_x;
 
	}
 

	
 
	/**
 
	 * Logarithm of the map size along the y side.
 
	 * @note try to avoid using this one
 
	 * @return 2^"return value" == Map::SizeY()
 
	 */
 
	static inline uint LogY()
 
	{
 
		extern uint _map_log_y;
 
		return _map_log_y;
 
		return Map::log_y;
 
	}
 

	
 
	/**
 
	 * Get the size of the map along the X
 
	 * @return the number of tiles along the X of the map
 
	 */
 
	static inline uint SizeX()
 
	{
 
		extern uint _map_size_x;
 
		return _map_size_x;
 
		return Map::size_x;
 
	}
 

	
 
	/**
 
	 * Get the size of the map along the Y
 
	 * @return the number of tiles along the Y of the map
 
	 */
 
	static inline uint SizeY()
 
	{
 
		extern uint _map_size_y;
 
		return _map_size_y;
 
		return Map::size_y;
 
	}
 

	
 
	/**
 
	 * Get the size of the map
 
	 * @return the number of tiles of the map
 
	 */
 
	static inline uint Size()
 
	{
 
		extern uint _map_size;
 
		return _map_size;
 
		return Map::size;
 
	}
 

	
 
	/**
 
	 * Gets the maximum X coordinate within the map, including MP_VOID
 
	 * @return the maximum X coordinate
 
	 */
 
@@ -112,14 +116,13 @@ struct Map {
 
	 * 'Wraps' the given "tile" so it is within the map.
 
	 * It does this by masking the 'high' bits of.
 
	 * @param tile the tile to 'wrap'
 
	 */
 
	static inline TileIndex WrapToMap(uint tile)
 
	{
 
		extern uint _map_tile_mask;
 
		return tile & _map_tile_mask;
 
		return tile & Map::tile_mask;
 
	}
 

	
 
	/**
 
	 * Scales the given value by the map size, where the given value is
 
	 * for a 256 by 256 map.
 
	 * @param n the value to scale
 
@@ -142,16 +145,24 @@ struct Map {
 
	{
 
		/* Normal circumference for the X+Y is 256+256 = 1<<9
 
		 * Note, not actually taking the full circumference into account,
 
		 * 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.
 
 *
 
 * This value is used for the difference between
 
 * two tiles. It can be added to a TileIndex to get
 
 * the resulting TileIndex of the start tile applied
src/misc.cpp
Show inline comments
 
@@ -58,13 +58,13 @@ void InitializeOldNames();
 
void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings)
 
{
 
	/* Make sure there isn't any window that can influence anything
 
	 * 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;
 
	_tick_counter = 0;
 
	_cur_tileloop_tile = 1;
 
	_thd.redsq = INVALID_TILE;
src/saveload/map_sl.cpp
Show inline comments
 
@@ -46,13 +46,13 @@ struct MAPSChunkHandler : ChunkHandler {
 
		const std::vector<SaveLoad> slt = SlCompatTableHeader(_map_desc, _map_sl_compat);
 

	
 
		if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
 
		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
 
	{
 
		const std::vector<SaveLoad> slt = SlCompatTableHeader(_map_desc, _map_sl_compat);
 

	
src/saveload/oldloader_sl.cpp
Show inline comments
 
@@ -1462,13 +1462,13 @@ 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++) {
 
		_m[i].m1 = ReadByte(ls);
 
	}
 
	for (uint i = 0; i < OLD_MAP_SIZE; i++) {
0 comments (0 inline, 0 general)