src/depot.h
author rubidium
Fri, 11 Apr 2008 08:14:43 +0000
changeset 8890 8a0fa7ff70a0
parent 8768 4bcc90746175
child 8954 3993bae3bfb8
permissions -rw-r--r--
(svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2153
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2153
diff changeset
     2
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5475
diff changeset
     3
/** @file depot.h Header files for depots (not hangars) */
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5475
diff changeset
     4
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
     5
#ifndef DEPOT_H
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
     6
#define DEPOT_H
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
     7
8100
6bc08f98ec16 (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: 7922
diff changeset
     8
#include "direction_type.h"
8768
4bcc90746175 (svn r12467) -Codechange: move DepotID to a more logical location.
rubidium
parents: 8563
diff changeset
     9
#include "depot_type.h"
5216
8bd14ee39af2 (svn r7331) - Codechange: Rename all memory pool macro's and types to "old pool", so the new pool implementation can be committed alongside it.
matthijs
parents: 5081
diff changeset
    10
#include "oldpool.h"
6182
7337aca829f1 (svn r8954) -Codechange: remove direct map accesses from non-map-accessor headers.
rubidium
parents: 6134
diff changeset
    11
#include "road_map.h"
7337aca829f1 (svn r8954) -Codechange: remove direct map accesses from non-map-accessor headers.
rubidium
parents: 6134
diff changeset
    12
#include "rail_map.h"
7337aca829f1 (svn r8954) -Codechange: remove direct map accesses from non-map-accessor headers.
rubidium
parents: 6134
diff changeset
    13
#include "water_map.h"
7512
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    14
#include "station_map.h"
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    15
5216
8bd14ee39af2 (svn r7331) - Codechange: Rename all memory pool macro's and types to "old pool", so the new pool implementation can be committed alongside it.
matthijs
parents: 5081
diff changeset
    16
DECLARE_OLD_POOL(Depot, Depot, 3, 8000)
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    17
7389
08d6a6265ced (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    18
struct Depot : PoolItem<Depot, DepotID, &_Depot_pool> {
08d6a6265ced (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    19
	TileIndex xy;
08d6a6265ced (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    20
	TownID town_index;
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4297
diff changeset
    21
7389
08d6a6265ced (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    22
	Depot(TileIndex xy = 0) : xy(xy) {}
08d6a6265ced (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    23
	~Depot();
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4347
diff changeset
    24
7496
49cec492627b (svn r11009) -Codechange: unvirtualise IsValid as that isn't needed with templates. This gives up to 10% performance increase in games with lots of vehicles.
rubidium
parents: 7389
diff changeset
    25
	inline bool IsValid() const { return this->xy != 0; }
7389
08d6a6265ced (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    26
};
4388
c8a66d26e146 (svn r6141) -Codechange: introduced DepotID and used it as much as possible
truelight
parents: 4352
diff changeset
    27
7389
08d6a6265ced (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    28
static inline bool IsValidDepotID(DepotID index)
4388
c8a66d26e146 (svn r6141) -Codechange: introduced DepotID and used it as much as possible
truelight
parents: 4352
diff changeset
    29
{
7389
08d6a6265ced (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    30
	return index < GetDepotPoolSize() && GetDepot(index)->IsValid();
4388
c8a66d26e146 (svn r6141) -Codechange: introduced DepotID and used it as much as possible
truelight
parents: 4352
diff changeset
    31
}
c8a66d26e146 (svn r6141) -Codechange: introduced DepotID and used it as much as possible
truelight
parents: 4352
diff changeset
    32
6638
09c5d7bf69a5 (svn r9869) -Codechange: replace some bytes with VehicleType, i.e. more type strictness.
rubidium
parents: 6247
diff changeset
    33
void ShowDepotWindow(TileIndex tile, VehicleType type);
4638
05955c6cb536 (svn r6513) -Codechange: unified the code to draw depot windows
bjarni
parents: 4549
diff changeset
    34
7389
08d6a6265ced (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    35
#define FOR_ALL_DEPOTS_FROM(d, start) for (d = GetDepot(start); d != NULL; d = (d->index + 1U < GetDepotPoolSize()) ? GetDepot(d->index + 1U) : NULL) if (d->IsValid())
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    36
#define FOR_ALL_DEPOTS(d) FOR_ALL_DEPOTS_FROM(d, 0)
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    37
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    38
/**
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    39
 * Check if a tile is a depot of the given type.
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    40
 */
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    41
static inline bool IsTileDepotType(TileIndex tile, TransportType type)
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    42
{
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3957
diff changeset
    43
	switch (type) {
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    44
		case TRANSPORT_RAIL:
6182
7337aca829f1 (svn r8954) -Codechange: remove direct map accesses from non-map-accessor headers.
rubidium
parents: 6134
diff changeset
    45
			return IsTileType(tile, MP_RAILWAY) && GetRailTileType(tile)  == RAIL_TILE_DEPOT;
1959
c2c3a9850c2e (svn r2465) Remove some unreachable code
tron
parents: 1944
diff changeset
    46
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    47
		case TRANSPORT_ROAD:
8563
13b1a41e46f1 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 8211
diff changeset
    48
			return IsRoadDepotTile(tile);
1959
c2c3a9850c2e (svn r2465) Remove some unreachable code
tron
parents: 1944
diff changeset
    49
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    50
		case TRANSPORT_WATER:
6182
7337aca829f1 (svn r8954) -Codechange: remove direct map accesses from non-map-accessor headers.
rubidium
parents: 6134
diff changeset
    51
			return IsTileType(tile, MP_WATER)   && GetWaterTileType(tile) == WATER_TILE_DEPOT;
1959
c2c3a9850c2e (svn r2465) Remove some unreachable code
tron
parents: 1944
diff changeset
    52
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    53
		default:
6182
7337aca829f1 (svn r8954) -Codechange: remove direct map accesses from non-map-accessor headers.
rubidium
parents: 6134
diff changeset
    54
			NOT_REACHED();
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    55
			return false;
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    56
	}
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    57
}
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
    58
7512
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    59
/**
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    60
 * Is the given tile a tile with a depot on it?
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    61
 * @param tile the tile to check
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    62
 * @return true if and only if there is a depot on the tile.
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    63
 */
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    64
static inline bool IsDepotTile(TileIndex tile)
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    65
{
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    66
	switch (GetTileType(tile)) {
8563
13b1a41e46f1 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 8211
diff changeset
    67
		case MP_ROAD:    return IsRoadDepot(tile);
7512
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    68
		case MP_WATER:   return GetWaterTileType(tile) == WATER_TILE_DEPOT;
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    69
		case MP_RAILWAY: return GetRailTileType(tile)  == RAIL_TILE_DEPOT;
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    70
		case MP_STATION: return IsHangar(tile);
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    71
		default:         return false;
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    72
	}
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7496
diff changeset
    73
}
1650
4a5141e10b72 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1330
diff changeset
    74
2085
876f20a0e843 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
    75
/**
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    76
 * Find out if the slope of the tile is suitable to build a depot of given direction
6134
871305fd9ab7 (svn r8876) -Fix
tron
parents: 6125
diff changeset
    77
 * @param direction The direction in which the depot's exit points
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    78
 * @param tileh The slope of the tile in question
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    79
 * @return true if the construction is possible
2085
876f20a0e843 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
    80
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    81
 * This is checked by the ugly 0x4C >> direction magic, which does the following:
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    82
 * 0x4C is 0100 1100 and tileh has only bits 0..3 set (steep tiles are ruled out)
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    83
 * So: for direction (only the significant bits are shown)<p>
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    84
 * 00 (exit towards NE) we need either bit 2 or 3 set in tileh: 0x4C >> 0 = 1100<p>
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    85
 * 01 (exit towards SE) we need either bit 1 or 2 set in tileh: 0x4C >> 1 = 0110<p>
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    86
 * 02 (exit towards SW) we need either bit 0 or 1 set in tileh: 0x4C >> 2 = 0011<p>
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    87
 * 03 (exit towards NW) we need either bit 0 or 4 set in tileh: 0x4C >> 3 = 1001<p>
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    88
 * So ((0x4C >> direction) & tileh) determines whether the depot can be built on the current tileh
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    89
 */
6134
871305fd9ab7 (svn r8876) -Fix
tron
parents: 6125
diff changeset
    90
static inline bool CanBuildDepotByTileh(DiagDirection direction, Slope tileh)
2085
876f20a0e843 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
    91
{
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3884
diff changeset
    92
	return ((0x4C >> direction) & tileh) != 0;
2085
876f20a0e843 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
    93
}
876f20a0e843 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
    94
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1959
diff changeset
    95
Depot *GetDepotByTile(TileIndex tile);
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6182
diff changeset
    96
void InitializeDepots();
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    97
5255
b693a9941b8c (svn r7385) -Fix: FS#418 Deleting Train in depot with autoreplace failes
bjarni
parents: 5216
diff changeset
    98
void DeleteDepotHighlightOfVehicle(const Vehicle *v);
b693a9941b8c (svn r7385) -Fix: FS#418 Deleting Train in depot with autoreplace failes
bjarni
parents: 5216
diff changeset
    99
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
   100
#endif /* DEPOT_H */