map.c
author tron
Sat, 29 Jan 2005 19:45:14 +0000
changeset 1218 353a7773bc3c
parent 1202 7d8b86bd8ba2
child 1233 2ee427ec9014
permissions -rw-r--r--
(svn r1722) -Feature: Bigger maps - anyone?
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     1
#include "stdafx.h"
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     2
#include "ttd.h"
1218
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
     3
#include "functions.h"
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     4
#include "map.h"
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     5
1218
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
     6
uint _map_log_x;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
     7
uint _map_log_y;
689
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
     8
1218
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
     9
byte   *_map_type_and_height = NULL;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    10
byte   *_map_owner           = NULL;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    11
uint16 *_map2                = NULL;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    12
byte   *_map3_lo             = NULL;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    13
byte   *_map3_hi             = NULL;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    14
byte   *_map5                = NULL;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    15
byte   *_map_extra_bits      = NULL;
863
8d09f9331a80 (svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents: 817
diff changeset
    16
1218
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    17
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    18
void InitMap(uint log_x, uint log_y)
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    19
{
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    20
	uint map_size;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    21
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    22
	assert(log_x <= 15 && log_y <= 15);
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    23
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    24
	_map_log_x = log_x;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    25
	_map_log_y = log_y;
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    26
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    27
	map_size = MapSize();
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    28
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    29
	_map_type_and_height =
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    30
		realloc(_map_type_and_height, map_size * sizeof(_map_type_and_height[0]));
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    31
	_map_owner = realloc(_map_owner, map_size * sizeof(_map_owner[0]));
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    32
	_map2      = realloc(_map2,      map_size * sizeof(_map2[0]));
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    33
	_map3_lo   = realloc(_map3_lo,   map_size * sizeof(_map3_lo[0]));
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    34
	_map3_hi   = realloc(_map3_hi,   map_size * sizeof(_map3_hi[0]));
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    35
	_map5      = realloc(_map5,      map_size * sizeof(_map5[0]));
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    36
	_map_extra_bits =
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    37
		realloc(_map_extra_bits, map_size * sizeof(_map_extra_bits[0] / 4));
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    38
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    39
	// XXX TODO handle memory shortage more gracefully
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    40
	if (_map_type_and_height == NULL ||
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    41
			_map_owner           == NULL ||
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    42
			_map2                == NULL ||
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    43
			_map3_lo             == NULL ||
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    44
			_map3_hi             == NULL ||
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    45
			_map5                == NULL ||
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    46
			_map_extra_bits      == NULL)
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    47
		error("Failed to allocate memory for the map");
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    48
}
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    49
955
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    50
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    51
#ifdef _DEBUG
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    52
TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    53
	const char *exp, const char *file, int line)
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    54
{
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    55
	int dx;
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    56
	int dy;
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    57
	uint x;
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    58
	uint y;
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    59
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    60
	dx = add & MapMaxX();
957
d1dd9429e417 (svn r1449) -Fix: signed/unsigned error on windows
darkvater
parents: 955
diff changeset
    61
	if (dx >= (int)MapSizeX() / 2) dx -= MapSizeX();
955
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    62
	dy = (add - dx) / (int)MapSizeX();
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    63
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    64
	x = TileX(tile) + dx;
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    65
	y = TileY(tile) + dy;
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    66
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    67
	if (x >= MapSizeX() || y >= MapSizeY()) {
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    68
		char buf[512];
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    69
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    70
		sprintf(buf, "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed",
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    71
			exp, tile, add);
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    72
#if !defined(_MSC_VER)
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    73
		fprintf(stderr, "%s:%d %s\n", file, line, buf);
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    74
#else
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    75
		_assert(buf, (char*)file, line);
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    76
#endif
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    77
	}
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    78
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    79
	assert(TILE_XY(x,y) == TILE_MASK(tile + add));
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    80
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    81
	return TILE_XY(x,y);
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    82
}
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    83
#endif
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    84
25bc5b97e3e2 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    85
1202
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    86
uint ScaleByMapSize(uint n)
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    87
{
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    88
	int shift = (int)MapLogX() - 8 + (int)MapLogY() - 8;
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    89
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    90
	if (shift < 0)
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    91
		return (n + (1 << -shift) - 1) >> -shift;
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    92
	else
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    93
		return n << shift;
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    94
}
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    95
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    96
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    97
uint ScaleByMapSize1D(uint n)
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    98
{
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    99
	int shift = ((int)MapLogX() - 8 + (int)MapLogY() - 8) / 2;
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   100
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   101
	if (shift < 0)
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   102
		return (n + (1 << -shift) - 1) >> -shift;
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   103
	else
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   104
		return n << shift;
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   105
}
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   106
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   107
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   108
const TileIndexDiffC _tileoffs_by_dir[] = {
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   109
	{-1,  0},
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   110
	{ 0,  1},
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   111
	{ 1,  0},
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   112
	{ 0, -1}
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   113
};