map.c
author tron
Fri, 22 Apr 2005 05:41:09 +0000
changeset 1718 96d76767ea93
parent 1677 d534f0c8c845
child 1891 862800791170
permissions -rw-r--r--
(svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
- Check if the vehicle exists
- Check if the vehicle belongs to the correct player
- Check if the new order is valid (type, destination, flags) (CmdInsertOrder)
679
04ca2cd69420 (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"
04ca2cd69420 (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"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1247
diff changeset
     3
#include "debug.h"
1218
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
     4
#include "functions.h"
679
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     5
#include "map.h"
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
diff changeset
     6
1218
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
     7
uint _map_log_x;
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
     8
uint _map_log_y;
689
5a4b1536db82 (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
     9
1218
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    10
byte   *_map_type_and_height = NULL;
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    11
byte   *_map_owner           = NULL;
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    12
uint16 *_map2                = NULL;
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    13
byte   *_map3_lo             = NULL;
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    14
byte   *_map3_hi             = NULL;
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    15
byte   *_map5                = NULL;
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    16
byte   *_map_extra_bits      = NULL;
863
6a1444534f62 (svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents: 817
diff changeset
    17
1218
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    18
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    19
void InitMap(uint log_x, uint log_y)
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    20
{
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    21
	uint map_size;
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    22
1244
421dd4852f17 (svn r1748) Enforce map size limits
tron
parents: 1233
diff changeset
    23
	if (log_x < 6 || log_x > 11 || log_y < 6 || log_y > 11)
421dd4852f17 (svn r1748) Enforce map size limits
tron
parents: 1233
diff changeset
    24
		error("Invalid map size");
1218
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    25
1233
3c020872c8b9 (svn r1737) Add DEBUG category "map" and use it to print the map size when allocating the map
tron
parents: 1218
diff changeset
    26
	DEBUG(map, 1)("Allocating map of size %dx%d", log_x, log_y);
3c020872c8b9 (svn r1737) Add DEBUG category "map" and use it to print the map size when allocating the map
tron
parents: 1218
diff changeset
    27
1410
6d20e503a15e (svn r1914) - Fix: [ 1119147 ] Stop startup memory corruption crash using optimized MSVC6. MSVC6 workaround as it's too stupid again for its own good
Darkvater
parents: 1299
diff changeset
    28
	// XXX - MSVC6 volatile workaround
6d20e503a15e (svn r1914) - Fix: [ 1119147 ] Stop startup memory corruption crash using optimized MSVC6. MSVC6 workaround as it's too stupid again for its own good
Darkvater
parents: 1299
diff changeset
    29
	*(volatile uint*)&_map_log_x = log_x;
6d20e503a15e (svn r1914) - Fix: [ 1119147 ] Stop startup memory corruption crash using optimized MSVC6. MSVC6 workaround as it's too stupid again for its own good
Darkvater
parents: 1299
diff changeset
    30
	*(volatile uint*)&_map_log_y = log_y;
1218
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    31
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    32
	map_size = MapSize();
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    33
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    34
	_map_type_and_height =
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    35
		realloc(_map_type_and_height, map_size * sizeof(_map_type_and_height[0]));
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    36
	_map_owner = realloc(_map_owner, map_size * sizeof(_map_owner[0]));
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    37
	_map2      = realloc(_map2,      map_size * sizeof(_map2[0]));
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    38
	_map3_lo   = realloc(_map3_lo,   map_size * sizeof(_map3_lo[0]));
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    39
	_map3_hi   = realloc(_map3_hi,   map_size * sizeof(_map3_hi[0]));
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    40
	_map5      = realloc(_map5,      map_size * sizeof(_map5[0]));
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    41
	_map_extra_bits =
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    42
		realloc(_map_extra_bits, map_size * sizeof(_map_extra_bits[0] / 4));
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    43
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    44
	// XXX TODO handle memory shortage more gracefully
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    45
	if (_map_type_and_height == NULL ||
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    46
			_map_owner           == NULL ||
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    47
			_map2                == NULL ||
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    48
			_map3_lo             == NULL ||
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    49
			_map3_hi             == NULL ||
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    50
			_map5                == NULL ||
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    51
			_map_extra_bits      == NULL)
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    52
		error("Failed to allocate memory for the map");
c6a624956ac6 (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1202
diff changeset
    53
}
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
    54
955
62b8588f50c8 (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
62b8588f50c8 (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
#ifdef _DEBUG
62b8588f50c8 (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
TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
62b8588f50c8 (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
	const char *exp, const char *file, int line)
62b8588f50c8 (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
{
62b8588f50c8 (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
	int dx;
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    61
	int dy;
62b8588f50c8 (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
	uint x;
62b8588f50c8 (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
	uint y;
62b8588f50c8 (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
62b8588f50c8 (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
	dx = add & MapMaxX();
957
a1cb5aac6a6f (svn r1449) -Fix: signed/unsigned error on windows
darkvater
parents: 955
diff changeset
    66
	if (dx >= (int)MapSizeX() / 2) dx -= MapSizeX();
955
62b8588f50c8 (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
	dy = (add - dx) / (int)MapSizeX();
62b8588f50c8 (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
62b8588f50c8 (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
	x = TileX(tile) + dx;
62b8588f50c8 (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
	y = TileY(tile) + dy;
62b8588f50c8 (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
62b8588f50c8 (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 (x >= MapSizeX() || y >= MapSizeY()) {
62b8588f50c8 (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
		char buf[512];
62b8588f50c8 (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
62b8588f50c8 (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
		sprintf(buf, "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed",
62b8588f50c8 (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
			exp, tile, add);
62b8588f50c8 (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
#if !defined(_MSC_VER)
62b8588f50c8 (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
		fprintf(stderr, "%s:%d %s\n", file, line, buf);
62b8588f50c8 (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
#else
62b8588f50c8 (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
		_assert(buf, (char*)file, line);
62b8588f50c8 (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
#endif
62b8588f50c8 (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
	}
62b8588f50c8 (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
62b8588f50c8 (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
	assert(TILE_XY(x,y) == TILE_MASK(tile + add));
62b8588f50c8 (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
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    86
	return TILE_XY(x,y);
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    87
}
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    88
#endif
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    89
62b8588f50c8 (svn r1447) Move TILE_ADD(), TILE_ADDXY() and SafeTileAdd() to map.[ch] and make the latter map size agnostic
tron
parents: 927
diff changeset
    90
1202
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    91
uint ScaleByMapSize(uint n)
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    92
{
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    93
	int shift = (int)MapLogX() - 8 + (int)MapLogY() - 8;
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    94
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    95
	if (shift < 0)
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    96
		return (n + (1 << -shift) - 1) >> -shift;
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    97
	else
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    98
		return n << shift;
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
    99
}
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   100
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   101
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   102
uint ScaleByMapSize1D(uint n)
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   103
{
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   104
	int shift = ((int)MapLogX() - 8 + (int)MapLogY() - 8) / 2;
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   105
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   106
	if (shift < 0)
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   107
		return (n + (1 << -shift) - 1) >> -shift;
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   108
	else
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   109
		return n << shift;
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   110
}
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   111
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 957
diff changeset
   112
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   113
// This function checks if we add addx/addy to tile, if we
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   114
//  do wrap around the edges. For example, tile = (10,2) and
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   115
//  addx = +3 and addy = -4. This function will now return
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   116
//  INVALID_TILE, because the y is wrapped. This is needed in
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   117
//  for example, farmland. When the tile is not wrapped,
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   118
//  the result will be tile + TILE_XY(addx, addy)
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   119
uint TileAddWrap(TileIndex tile, int addx, int addy)
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   120
{
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   121
	uint x, y;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   122
	x = TileX(tile) + addx;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   123
	y = TileY(tile) + addy;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   124
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   125
	// Are we about to wrap?
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   126
		if (x < MapMaxX() && y < MapMaxY())
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   127
		return tile + TILE_XY(addx, addy);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   128
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   129
	return INVALID_TILE;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   130
}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   131
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   132
const TileIndexDiffC _tileoffs_by_dir[] = {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   133
	{-1,  0},
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   134
	{ 0,  1},
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   135
	{ 1,  0},
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   136
	{ 0, -1}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   137
};
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   138
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   139
uint DistanceManhattan(TileIndex t0, TileIndex t1)
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   140
{
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   141
	const uint dx = abs(TileX(t0) - TileX(t1));
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   142
	const uint dy = abs(TileY(t0) - TileY(t1));
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   143
	return dx + dy;
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   144
}
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   145
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   146
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   147
uint DistanceSquare(TileIndex t0, TileIndex t1)
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   148
{
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   149
	const int dx = TileX(t0) - TileX(t1);
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   150
	const int dy = TileY(t0) - TileY(t1);
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   151
	return dx * dx + dy * dy;
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   152
}
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   153
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   154
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   155
uint DistanceMax(TileIndex t0, TileIndex t1)
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   156
{
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   157
	const uint dx = abs(TileX(t0) - TileX(t1));
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   158
	const uint dy = abs(TileY(t0) - TileY(t1));
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   159
	return dx > dy ? dx : dy;
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   160
}
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   161
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   162
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   163
uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   164
{
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   165
	const uint dx = abs(TileX(t0) - TileX(t1));
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   166
	const uint dy = abs(TileY(t0) - TileY(t1));
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   167
	return dx > dy ? 2 * dx + dy : 2 * dy + dx;
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   168
}
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   169
1677
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   170
uint DistanceTrack(TileIndex t0, TileIndex t1)
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   171
{
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   172
	const uint dx = abs(TileX(t0) - TileX(t1));
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   173
	const uint dy = abs(TileY(t0) - TileY(t1));
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   174
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   175
	const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   176
	/* OPTIMISATION:
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   177
	 * Original: diagTracks = max(dx, dy) - min(dx,dy);
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   178
	 * Proof:
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   179
	 * (dx-dy) - straightTracks  == (min + max) - straightTracks = min + // max - 2 * min = max - min */
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   180
	const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   181
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   182
	return diagTracks + straightTracks * STRAIGHT_TRACK_LENGTH;
d534f0c8c845 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1410
diff changeset
   183
}
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   184
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   185
uint DistanceFromEdge(TileIndex tile)
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   186
{
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   187
	const uint xl = TileX(tile);
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   188
	const uint yl = TileY(tile);
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   189
	const uint xh = MapSizeX() - 1 - xl;
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   190
	const uint yh = MapSizeY() - 1 - yl;
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   191
	const uint minl = xl < yl ? xl : yl;
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   192
	const uint minh = xh < yh ? xh : yh;
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   193
	return minl < minh ? minl : minh;
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   194
}
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1244
diff changeset
   195