Codechange: add map size related functions to Map structure

This commit is contained in:
Rubidium 2023-01-21 10:27:00 +01:00 committed by rubidium42
parent 9c1a3b17e3
commit d481f78b24
2 changed files with 104 additions and 89 deletions

View File

@ -36,7 +36,7 @@ TileExtended *_me = nullptr; ///< Extended Tiles of the map
* @param size_x the width of the map along the NE/SW edge * @param size_x the width of the map along the NE/SW edge
* @param size_y the 'height' of the map along the SE/NW edge * @param size_y the 'height' of the map along the SE/NW edge
*/ */
void AllocateMap(uint size_x, uint size_y) /* static */ void Map::Allocate(uint size_x, uint size_y)
{ {
/* Make sure that the map size is within the limits and that /* Make sure that the map size is within the limits and that
* size of both axes is a power of 2. */ * size of both axes is a power of 2. */

View File

@ -41,105 +41,120 @@ extern Tile *_m;
*/ */
extern TileExtended *_me; extern TileExtended *_me;
void AllocateMap(uint size_x, uint size_y);
/** /**
* Logarithm of the map size along the X side. * Size related data of the map.
* @note try to avoid using this one
* @return 2^"return value" == MapSizeX()
*/ */
static inline uint MapLogX() struct Map {
{ static void Allocate(uint size_x, uint size_y);
extern uint _map_log_x;
return _map_log_x;
}
/** /**
* Logarithm of the map size along the y side. * Logarithm of the map size along the X side.
* @note try to avoid using this one * @note try to avoid using this one
* @return 2^"return value" == MapSizeY() * @return 2^"return value" == Map::SizeX()
*/ */
static inline uint MapLogY() static inline uint LogX()
{ {
extern uint _map_log_y; extern uint _map_log_x;
return _map_log_y; return _map_log_x;
} }
/** /**
* Get the size of the map along the X * Logarithm of the map size along the y side.
* @return the number of tiles along the X of the map * @note try to avoid using this one
*/ * @return 2^"return value" == Map::SizeY()
static inline uint MapSizeX() */
{ static inline uint LogY()
extern uint _map_size_x; {
return _map_size_x; extern uint _map_log_y;
} return _map_log_y;
}
/** /**
* Get the size of the map along the Y * Get the size of the map along the X
* @return the number of tiles along the Y of the map * @return the number of tiles along the X of the map
*/ */
static inline uint MapSizeY() static inline uint SizeX()
{ {
extern uint _map_size_y; extern uint _map_size_x;
return _map_size_y; return _map_size_x;
} }
/** /**
* Get the size of the map * Get the size of the map along the Y
* @return the number of tiles of the map * @return the number of tiles along the Y of the map
*/ */
static inline uint MapSize() static inline uint SizeY()
{ {
extern uint _map_size; extern uint _map_size_y;
return _map_size; return _map_size_y;
} }
/** /**
* Gets the maximum X coordinate within the map, including MP_VOID * Get the size of the map
* @return the maximum X coordinate * @return the number of tiles of the map
*/ */
static inline uint MapMaxX() static inline uint Size()
{ {
return MapSizeX() - 1; extern uint _map_size;
} return _map_size;
}
/** /**
* Gets the maximum Y coordinate within the map, including MP_VOID * Gets the maximum X coordinate within the map, including MP_VOID
* @return the maximum Y coordinate * @return the maximum X coordinate
*/ */
static inline uint MapMaxY() static inline uint MaxX()
{ {
return MapSizeY() - 1; return Map::SizeX() - 1;
} }
/** /**
* Scales the given value by the map size, where the given value is * Gets the maximum Y coordinate within the map, including MP_VOID
* for a 256 by 256 map. * @return the maximum Y coordinate
* @param n the value to scale */
* @return the scaled size static inline uint MaxY()
*/ {
static inline uint ScaleByMapSize(uint n) return Map::SizeY() - 1;
{ }
/* Subtract 12 from shift in order to prevent integer overflow
* for large values of n. It's safe since the min mapsize is 64x64. */
return CeilDiv(n << (MapLogX() + MapLogY() - 12), 1 << 4);
}
/**
* 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
* @return the scaled size
*/
static inline uint ScaleBySize(uint n)
{
/* Subtract 12 from shift in order to prevent integer overflow
* for large values of n. It's safe since the min mapsize is 64x64. */
return CeilDiv(n << (Map::LogX() + Map::LogY() - 12), 1 << 4);
}
/** /**
* Scales the given value by the maps circumference, where the given * Scales the given value by the maps circumference, where the given
* value is for a 256 by 256 map * value is for a 256 by 256 map
* @param n the value to scale * @param n the value to scale
* @return the scaled size * @return the scaled size
*/ */
static inline uint ScaleByMapSize1D(uint n) static inline uint ScaleBySize1D(uint n)
{ {
/* Normal circumference for the X+Y is 256+256 = 1<<9 /* Normal circumference for the X+Y is 256+256 = 1<<9
* Note, not actually taking the full circumference into account, * Note, not actually taking the full circumference into account,
* just half of it. */ * just half of it. */
return CeilDiv((n << MapLogX()) + (n << MapLogY()), 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); }
static inline uint MapLogX() { return Map::LogX(); }
static inline uint MapLogY() { return Map::LogY(); }
static inline uint MapSizeX() { return Map::SizeX(); }
static inline uint MapSizeY() { return Map::SizeY(); }
static inline uint MapSize() { return Map::Size(); }
static inline uint MapMaxX() { return Map::MaxX(); }
static inline uint MapMaxY() { return Map::MaxY(); }
static inline uint ScaleByMapSize(uint n) { return Map::ScaleBySize(n); }
static inline uint ScaleByMapSize1D(uint n) { return Map::ScaleBySize1D(n); }
/** /**
* An offset value between two tiles. * An offset value between two tiles.