src/map_func.h
author frosch
Thu, 17 Jan 2008 16:55:13 +0000
changeset 8827 d125b82b86f4
parent 8635 3bbb6f87fced
child 8844 b4f9ff470b85
permissions -rw-r--r--
(svn r11897) -Fix: Replace an uint with TileIndex and a small coding style fix.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     2
6527
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 5847
diff changeset
     3
/** @file map.h */
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 5847
diff changeset
     4
8635
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
     5
#ifndef MAP_FUNC_H
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
     6
#define MAP_FUNC_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
     7
8635
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
     8
#include "tile_type.h"
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
     9
#include "map_type.h"
8596
27646407e0bc (svn r11661) -Codechange: some header reworks in order to try to reduce the compile time of OpenTTD by reduce the amount of circular-ish dependencies.
rubidium
parents: 8137
diff changeset
    10
#include "direction_func.h"
1210
05975e9167c4 (svn r1714) Add missing include
tron
parents: 1209
diff changeset
    11
2051
44b6b091beb3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    12
extern uint _map_tile_mask;
7036
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    13
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    14
/**
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    15
 * 'Wraps' the given tile to it is within the map. It does
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    16
 * this by masking the 'high' bits of.
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    17
 * @param x the tile to 'wrap'
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    18
 */
2051
44b6b091beb3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    19
44b6b091beb3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    20
#define TILE_MASK(x) ((x) & _map_tile_mask)
7036
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    21
/**
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    22
 * Asserts when the tile is outside of the map.
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    23
 * @param x the tile to check
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    24
 */
1394
449e84bdc04e (svn r1898) Remove some unused macros from macros.h and move some others to more appropriate headers
tron
parents: 1330
diff changeset
    25
#define TILE_ASSERT(x) assert(TILE_MASK(x) == (x));
926
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
    26
7036
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    27
/**
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    28
 * Pointer to the tile-array.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    29
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    30
 * This variable points to the tile-array which contains the tiles of
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    31
 * the map.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    32
 */
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6574
diff changeset
    33
extern Tile *_m;
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    34
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    35
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    36
 * Pointer to the extended tile-array.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    37
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    38
 * This variable points to the extended tile-array which contains the tiles
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    39
 * of the map.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    40
 */
6658
59048224be55 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6574
diff changeset
    41
extern TileExtended *_me;
1218
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1210
diff changeset
    42
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    43
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    44
 * Allocate a new map with the given size.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
    45
 */
2051
44b6b091beb3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    46
void AllocateMap(uint size_x, uint size_y);
44b6b091beb3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
    47
7036
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    48
/**
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    49
 * Logarithm of the map size along the X side.
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    50
 * @note try to avoid using this one
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    51
 * @return 2^"return value" == MapSizeX()
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    52
 */
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    53
static inline uint MapLogX()
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    54
{
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    55
	extern uint _map_log_x;
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    56
	return _map_log_x;
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    57
}
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    58
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    59
/**
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    60
 * Get the size of the map along the X
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    61
 * @return the number of tiles along the X of the map
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    62
 */
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    63
static inline uint MapSizeX()
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    64
{
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    65
	extern uint _map_size_x;
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    66
	return _map_size_x;
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    67
}
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    68
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    69
/**
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    70
 * Get the size of the map along the Y
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    71
 * @return the number of tiles along the Y of the map
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    72
 */
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    73
static inline uint MapSizeY()
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    74
{
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    75
	extern uint _map_size_y;
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    76
	return _map_size_y;
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    77
}
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    78
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    79
/**
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    80
 * Get the size of the map
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    81
 * @return the number of tiles of the map
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    82
 */
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    83
static inline uint MapSize()
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    84
{
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    85
	extern uint _map_size;
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    86
	return _map_size;
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    87
}
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    88
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    89
/**
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    90
 * Gets the maximum X coordinate within the map, including MP_VOID
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    91
 * @return the maximum X coordinate
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    92
 */
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    93
static inline uint MapMaxX()
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    94
{
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    95
	return MapSizeX() - 1;
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    96
}
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    97
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    98
/**
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
    99
 * Gets the maximum X coordinate within the map, including MP_VOID
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   100
 * @return the maximum X coordinate
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   101
 */
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   102
static inline uint MapMaxY()
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   103
{
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   104
	return MapSizeY() - 1;
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   105
}
689
1412bc834a8d (svn r1130) Add helper functions to query map size
tron
parents: 679
diff changeset
   106
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   107
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   108
 * Scales relative to the number of tiles.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   109
 */
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   110
uint ScaleByMapSize(uint);
1202
7d8b86bd8ba2 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
   111
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   112
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   113
 * Scale relative to the circumference of the map.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   114
 */
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   115
uint ScaleByMapSize1D(uint);
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   116
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   117
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   118
 * An offset value between to tiles.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   119
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   120
 * This value is used fro the difference between
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   121
 * to tiles. It can be added to a tileindex to get
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   122
 * the resulting tileindex of the start tile applied
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   123
 * with this saved difference.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   124
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   125
 * @see TileDiffXY(int, int)
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   126
 */
1981
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   127
typedef int32 TileIndexDiff;
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   128
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   129
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   130
 * Returns the TileIndex of a coordinate.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   131
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   132
 * @param x The x coordinate of the tile
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   133
 * @param y The y coordinate of the tile
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   134
 * @return The TileIndex calculated by the coordinate
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   135
 */
1981
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   136
static inline TileIndex TileXY(uint x, uint y)
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   137
{
2051
44b6b091beb3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
   138
	return (y * MapSizeX()) + x;
1981
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   139
}
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   140
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   141
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   142
 * Calculates an offset for the given coordinate(-offset).
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   143
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   144
 * This function calculate an offset value which can be added to an
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   145
 * #TileIndex. The coordinates can be negative.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   146
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   147
 * @param x The offset in x direction
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   148
 * @param y The offset in y direction
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   149
 * @return The resulting offset value of the given coordinate
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   150
 * @see ToTileIndexDiff(TileIndexDiffC)
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   151
 */
1981
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   152
static inline TileIndexDiff TileDiffXY(int x, int y)
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   153
{
6527
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 5847
diff changeset
   154
	/* Multiplication gives much better optimization on MSVC than shifting.
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 5847
diff changeset
   155
	 * 0 << shift isn't optimized to 0 properly.
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 5847
diff changeset
   156
	 * Typically x and y are constants, and then this doesn't result
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 5847
diff changeset
   157
	 * in any actual multiplication in the assembly code.. */
2051
44b6b091beb3 (svn r2560) Fix: various minor code changes.
ludde
parents: 2049
diff changeset
   158
	return (y * MapSizeX()) + x;
1981
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   159
}
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1247
diff changeset
   160
1980
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   161
static inline TileIndex TileVirtXY(uint x, uint y)
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   162
{
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   163
	return (y >> 4 << MapLogX()) + (x >> 4);
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   164
}
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   165
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1247
diff changeset
   166
7036
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   167
/**
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   168
 * Get the X component of a tile
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   169
 * @param tile the tile to get the X component of
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   170
 * @return the X component
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   171
 */
926
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
   172
static inline uint TileX(TileIndex tile)
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
   173
{
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
   174
	return tile & MapMaxX();
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
   175
}
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
   176
7036
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   177
/**
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   178
 * Get the Y component of a tile
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   179
 * @param tile the tile to get the Y component of
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   180
 * @return the Y component
9f23930e7ded (svn r9729) -Documentation: add some documentation in various places
rubidium
parents: 6987
diff changeset
   181
 */
926
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
   182
static inline uint TileY(TileIndex tile)
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
   183
{
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
   184
	return tile >> MapLogX();
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
   185
}
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 909
diff changeset
   186
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   187
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   188
 * Return the offset between to tiles from a TileIndexDiffC struct.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   189
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   190
 * This function works like #TileDiffXY(int, int) and returns the
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   191
 * difference between two tiles.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   192
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   193
 * @param tidc The coordinate of the offset as TileIndexDiffC
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   194
 * @return The difference between two tiles.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   195
 * @see TileDiffXY(int, int)
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   196
 */
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   197
static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   198
{
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   199
	return (tidc.y << MapLogX()) + tidc.x;
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   200
}
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   201
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
   202
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
   203
#ifndef _DEBUG
8635
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   204
	/**
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   205
	 * Adds to tiles together.
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   206
	 *
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   207
	 * @param x One tile
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   208
	 * @param y An other tile to add
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   209
	 * @return The resulting tile(index)
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   210
	 */
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
   211
	#define TILE_ADD(x,y) ((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
   212
#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
   213
	extern 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
   214
		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
   215
	#define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __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
   216
#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
   217
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   218
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   219
 * Adds a given offset to a tile.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   220
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   221
 * @param tile The tile to add an offset on it
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   222
 * @param x The x offset to add to the tile
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   223
 * @param y The y offset to add to the tile
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   224
 */
1981
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   225
#define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y))
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
   226
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   227
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   228
 * Adds an offset to a tile and check if we are still on the map.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   229
 */
8827
d125b82b86f4 (svn r11897) -Fix: Replace an uint with TileIndex and a small coding style fix.
frosch
parents: 8635
diff changeset
   230
TileIndex TileAddWrap(TileIndex tile, int addx, int addy);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   231
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   232
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   233
 * Returns the TileIndexDiffC offset from a DiagDirection.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   234
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   235
 * @param dir The given direction
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   236
 * @return The offset as TileIndexDiffC value
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   237
 */
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7067
diff changeset
   238
static inline TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir)
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7067
diff changeset
   239
{
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7067
diff changeset
   240
	extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END];
4561
0779e5c11331 (svn r6408) -Fix(r6406): compilation was broken
glx
parents: 4559
diff changeset
   241
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7067
diff changeset
   242
	assert(IsValidDiagDirection(dir));
4561
0779e5c11331 (svn r6408) -Fix(r6406): compilation was broken
glx
parents: 4559
diff changeset
   243
	return _tileoffs_by_diagdir[dir];
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   244
}
8635
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   245
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   246
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   247
 * Add a TileIndexDiffC to a TileIndex and returns the new one.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   248
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   249
 * Returns tile + the diff given in diff. If the result tile would end up
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   250
 * outside of the map, INVALID_TILE is returned instead.
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   251
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   252
 * @param tile The base tile to add the offset on
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   253
 * @param diff The offset to add on the tile
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   254
 * @return The resulting TileIndex
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   255
 */
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7067
diff changeset
   256
static inline TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff)
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7067
diff changeset
   257
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   258
	int x = TileX(tile) + diff.x;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   259
	int y = TileY(tile) + diff.y;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   260
	if (x < 0 || y < 0 || x > (int)MapMaxX() || y > (int)MapMaxY())
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   261
		return INVALID_TILE;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   262
	else
1981
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1980
diff changeset
   263
		return TileXY(x, y);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   264
}
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
   265
7067
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   266
/**
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   267
 * Returns the diff between two tiles
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   268
 *
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   269
 * @param tile_a from tile
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   270
 * @param tile_b to tile
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   271
 * @return the difference between tila_a and tile_b
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   272
 */
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   273
static inline TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b)
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   274
{
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   275
	TileIndexDiffC difference;
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   276
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   277
	difference.x = TileX(tile_a) - TileX(tile_b);
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   278
	difference.y = TileY(tile_a) - TileY(tile_b);
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   279
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   280
	return difference;
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   281
}
ea55b6046015 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 7036
diff changeset
   282
6527
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 5847
diff changeset
   283
/* Functions to calculate distances */
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 5847
diff changeset
   284
uint DistanceManhattan(TileIndex, TileIndex); ///< also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads)
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 5847
diff changeset
   285
uint DistanceSquare(TileIndex, TileIndex); ///< euclidian- or L2-Norm squared
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 5847
diff changeset
   286
uint DistanceMax(TileIndex, TileIndex); ///< also known as L-Infinity-Norm
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 5847
diff changeset
   287
uint DistanceMaxPlusManhattan(TileIndex, TileIndex); ///< Max + Manhattan
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 5847
diff changeset
   288
uint DistanceFromEdge(TileIndex); ///< shortest distance from any edge of the map
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1218
diff changeset
   289
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   290
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   291
 * Starts a loop which iterates to a square of tiles
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   292
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   293
 * This macro starts 2 nested loops which iterates over a square of tiles.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   294
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   295
 * @param var The name of the variable which contains the current tile
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   296
 * @param w The width (x-width) of the square
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   297
 * @param h The heigth (y-width) of the square
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   298
 * @param tile The start tile of the square
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   299
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6658
diff changeset
   300
#define BEGIN_TILE_LOOP(var, w, h, tile)                      \
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   301
	{                                                        \
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   302
		int h_cur = h;                                         \
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   303
		uint var = tile;                                       \
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   304
		do {                                                   \
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   305
			int w_cur = w;                                       \
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   306
			do {
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   307
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   308
 * Ends the square-loop used before
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   309
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   310
 * @see BEGIN_TILE_LOOP
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   311
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6658
diff changeset
   312
#define END_TILE_LOOP(var, w, h, tile)                        \
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   313
			} while (++var, --w_cur != 0);                       \
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   314
		} while (var += TileDiffXY(0, 1) - (w), --h_cur != 0); \
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   315
	}
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   316
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   317
 * Convert a DiagDirection to a TileIndexDiff
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   318
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   319
 * @param dir The DiagDirection
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   320
 * @return The resulting TileIndexDiff
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   321
 * @see TileIndexDiffCByDiagDir
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   322
 */
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7067
diff changeset
   323
static inline TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4344
diff changeset
   324
{
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7067
diff changeset
   325
	extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END];
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2051
diff changeset
   326
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7067
diff changeset
   327
	assert(IsValidDiagDirection(dir));
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4344
diff changeset
   328
	return ToTileIndexDiff(_tileoffs_by_diagdir[dir]);
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4344
diff changeset
   329
}
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4344
diff changeset
   330
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   331
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   332
 * Convert a Direction to a TileIndexDiff.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   333
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   334
 * @param dir The direction to convert from
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   335
 * @return The resulting TileIndexDiff
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   336
 */
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7067
diff changeset
   337
static inline TileIndexDiff TileOffsByDir(Direction dir)
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   338
{
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7067
diff changeset
   339
	extern const TileIndexDiffC _tileoffs_by_dir[DIR_END];
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   340
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7067
diff changeset
   341
	assert(IsValidDirection(dir));
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   342
	return ToTileIndexDiff(_tileoffs_by_dir[dir]);
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   343
}
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 863
diff changeset
   344
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   345
/**
8137
67a9579abd74 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 8042
diff changeset
   346
 * Adds a DiagDir to a tile.
67a9579abd74 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 8042
diff changeset
   347
 *
67a9579abd74 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 8042
diff changeset
   348
 * @param tile The current tile
67a9579abd74 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 8042
diff changeset
   349
 * @param dir The direction in which we want to step
67a9579abd74 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 8042
diff changeset
   350
 * @return the moved tile
67a9579abd74 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 8042
diff changeset
   351
 */
67a9579abd74 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 8042
diff changeset
   352
static inline TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
67a9579abd74 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 8042
diff changeset
   353
{
67a9579abd74 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 8042
diff changeset
   354
	return TILE_ADD(tile, TileOffsByDiagDir(dir));
67a9579abd74 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 8042
diff changeset
   355
}
67a9579abd74 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 8042
diff changeset
   356
67a9579abd74 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 8042
diff changeset
   357
/**
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   358
 * A callback function type for searching tiles.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   359
 *
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   360
 * @param tile The tile to test
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   361
 * @param data additional data for the callback function to use
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   362
 * @return A boolean value, depend on the definition of the function.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   363
 */
5118
5a56a0f12206 (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 4849
diff changeset
   364
typedef bool TestTileOnSearchProc(TileIndex tile, uint32 data);
8042
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   365
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   366
/**
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   367
 * Searches for some cirumstances of a tile around a given tile with a helper function.
1ccbcf3295b5 (svn r11066) -Documentation [FS#1091]: of map.*. Patch by Progman.
rubidium
parents: 7813
diff changeset
   368
 */
5118
5a56a0f12206 (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 4849
diff changeset
   369
bool CircularTileSearch(TileIndex tile, uint size, TestTileOnSearchProc proc, uint32 data);
5a56a0f12206 (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 4849
diff changeset
   370
8635
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   371
/**
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   372
 * Get a random tile out of a given seed.
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   373
 * @param r the random 'seed'
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   374
 * @return a valid tile
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   375
 */
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   376
static inline TileIndex RandomTileSeed(uint32 r)
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   377
{
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   378
	return TILE_MASK(r);
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   379
}
1677
c18884ca76d5 (svn r2181) - Add: DistanceTrack() to calculate the distance over optimally laid out tracks.
matthijs
parents: 1433
diff changeset
   380
8635
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   381
/**
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   382
 * Get a valid random tile.
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   383
 * @note a define so 'random' gets inserted in the place where it is actually
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   384
 *       called, thus making the random traces more explicit.
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   385
 * @return a valid tile
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   386
 */
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   387
#define RandomTile() RandomTileSeed(Random())
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   388
3bbb6f87fced (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8634
diff changeset
   389
#endif /* MAP_FUNC_H */