Changeset - r12129:365441530ee1
[Not reviewed]
master
0 2 0
smatz - 15 years ago 2009-06-10 08:18:40
smatz@openttd.org
(svn r16550) -Codechange: move definition of ScaleByMapSize to header file, use shifts instead of mults
2 files changed with 24 insertions and 35 deletions:
0 comments (0 inline, 0 general)
src/map.cpp
Show inline comments
 
@@ -93,37 +93,6 @@ TileIndex TileAdd(TileIndex tile, TileIn
 
#endif
 

	
 
/*!
 
 * 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
 
 */
 
uint ScaleByMapSize(uint n)
 
{
 
	/* First shift by 12 to prevent integer overflow for large values of n.
 
	 * >>12 is safe since the min mapsize is 64x64
 
	 * Add (1<<4)-1 to round upwards. */
 
	return (n * (MapSize() >> 12) + (1 << 4) - 1) >> 4;
 
}
 

	
 

	
 
/*!
 
 * Scales the given value by the maps circumference, where the given
 
 * value is for a 256 by 256 map
 
 * @param n the value to scale
 
 * @return the scaled size
 
 */
 
uint ScaleByMapSize1D(uint n)
 
{
 
	/* Normal circumference for the X+Y is 256+256 = 1<<9
 
	 * Note, not actually taking the full circumference into account,
 
	 * just half of it.
 
	 * (1<<9) - 1 is there to scale upwards. */
 
	return (n * (MapSizeX() + MapSizeY()) + (1 << 9) - 1) >> 9;
 
}
 

	
 

	
 
/*!
 
 * This function checks if we add addx/addy to tile, if we
 
 * do wrap around the edges. For example, tile = (10,2) and
 
 * addx = +3 and addy = -4. This function will now return
src/map_func.h
Show inline comments
 
@@ -111,14 +111,34 @@ static inline uint MapMaxY()
 
}
 

	
 
/**
 
 * Scales relative to the number of tiles.
 
 * 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
 
 */
 
uint ScaleByMapSize(uint);
 
static inline uint ScaleByMapSize(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.
 
	 * Add (1<<4)-1 to round upwards. */
 
	return ((n << (MapLogX() + MapLogY() - 12)) + (1 << 4) - 1) >> 4;
 
}
 

	
 

	
 
/**
 
 * Scale relative to the circumference of the map.
 
 * Scales the given value by the maps circumference, where the given
 
 * value is for a 256 by 256 map
 
 * @param n the value to scale
 
 * @return the scaled size
 
 */
 
uint ScaleByMapSize1D(uint);
 
static inline uint ScaleByMapSize1D(uint n)
 
{
 
	/* Normal circumference for the X+Y is 256+256 = 1<<9
 
	 * Note, not actually taking the full circumference into account,
 
	 * just half of it.
 
	 * (1<<9) - 1 is there to scale upwards. */
 
	return ((n << MapLogX()) + (n << MapLogY()) + (1 << 9) - 1) >> 9;
 
}
 

	
 
/**
 
 * An offset value between to tiles.
0 comments (0 inline, 0 general)