summaryrefslogtreecommitdiff
path: root/map.c
diff options
context:
space:
mode:
authorludde <ludde@openttd.org>2005-07-13 19:51:31 +0000
committerludde <ludde@openttd.org>2005-07-13 19:51:31 +0000
commit6013b327f7d54f9d5a331b105011c238da97046f (patch)
tree24c82eee7c186426a35f3ee245d8d753a296d7e2 /map.c
parent4b006aa6cff4b25f6e9421d3693c2ea51e6b5220 (diff)
downloadopenttd-6013b327f7d54f9d5a331b105011c238da97046f.tar.xz
(svn r2560) Fix: various minor code changes.
Added RandomTile/RandomTileSeed functions to generate a random tile. Changed landscape routines so they don't assume that the Y map side is a power of two. (support for this is not complete, though) Changed some frequently used map macros to not compute the values each time. Silence some warnings on MSVC.
Diffstat (limited to 'map.c')
-rw-r--r--map.c53
1 files changed, 29 insertions, 24 deletions
diff --git a/map.c b/map.c
index 6330c660e..2eeaa8af1 100644
--- a/map.c
+++ b/map.c
@@ -5,28 +5,35 @@
#include "map.h"
uint _map_log_x;
-uint _map_log_y;
+uint _map_size_x;
+uint _map_size_y;
+uint _map_tile_mask;
+uint _map_size;
Tile* _m = NULL;
-void InitMap(uint log_x, uint log_y)
+void AllocateMap(uint size_x, uint size_y)
{
- uint map_size;
-
- if (log_x < 6 || log_x > 11 || log_y < 6 || log_y > 11)
+ // Make sure that the map size is within the limits and that
+ // the x axis size is a power of 2.
+ if (size_x < 64 || size_x > 2048 ||
+ size_y < 64 || size_y > 2048 ||
+ (size_x&(size_x-1)) != 0 ||
+ (size_y&(size_y-1)) != 0)
error("Invalid map size");
- DEBUG(map, 1)("Allocating map of size %dx%d", log_x, log_y);
-
- _map_log_x = log_x;
- _map_log_y = log_y;
+ DEBUG(map, 1)("Allocating map of size %dx%d", size_x, size_y);
- // XXX - MSVC6 workaround
- map_size = 1 << (log_x + log_y);
+ _map_log_x = FindFirstBit(size_x);
+ _map_size_x = size_x;
+ _map_size_y = size_y;
+ _map_size = size_x * size_y;
+ _map_tile_mask = _map_size - 1;
+ // free/malloc uses less memory than realloc.
free(_m);
- _m = malloc(map_size * sizeof(*_m));
+ _m = malloc(_map_size * sizeof(*_m));
// XXX TODO handle memory shortage more gracefully
if (_m == NULL) error("Failed to allocate memory for the map");
@@ -70,23 +77,21 @@ TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
uint ScaleByMapSize(uint n)
{
- int shift = (int)MapLogX() - 8 + (int)MapLogY() - 8;
-
- if (shift < 0)
- return (n + (1 << -shift) - 1) >> -shift;
- else
- return n << shift;
+ // 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;
}
+// Scale relative to the circumference of the map
uint ScaleByMapSize1D(uint n)
{
- int shift = ((int)MapLogX() - 8 + (int)MapLogY() - 8) / 2;
-
- if (shift < 0)
- return (n + (1 << -shift) - 1) >> -shift;
- else
- return n << shift;
+ // 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;
}