src/depot.h
author rubidium
Tue, 18 Dec 2007 19:52:14 +0000
changeset 8596 27646407e0bc
parent 8418 b49fc6be1ab9
child 8604 8afdd9877afd
permissions -rw-r--r--
(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.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2153
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2153
diff changeset
     2
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5726
diff changeset
     3
/** @file depot.h Header files for depots (not hangars) */
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5726
diff changeset
     4
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
     5
#ifndef DEPOT_H
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
     6
#define DEPOT_H
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
     7
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: 8418
diff changeset
     8
#include "direction_type.h"
5216
d581e4db95b6 (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
     9
#include "oldpool.h"
1330
8a67d04016ce (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
    10
#include "tile.h"
2153
91e89aa8c299 (svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents: 2085
diff changeset
    11
#include "variables.h"
6508
8bd54af67f0b (svn r8954) -Codechange: remove direct map accesses from non-map-accessor headers.
rubidium
parents: 6460
diff changeset
    12
#include "road_map.h"
8bd54af67f0b (svn r8954) -Codechange: remove direct map accesses from non-map-accessor headers.
rubidium
parents: 6460
diff changeset
    13
#include "rail_map.h"
8bd54af67f0b (svn r8954) -Codechange: remove direct map accesses from non-map-accessor headers.
rubidium
parents: 6460
diff changeset
    14
#include "water_map.h"
8008
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    15
#include "station_map.h"
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    16
7885
4eb9b01e37ff (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7866
diff changeset
    17
struct Depot;
5216
d581e4db95b6 (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
    18
DECLARE_OLD_POOL(Depot, Depot, 3, 8000)
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    19
7885
4eb9b01e37ff (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7866
diff changeset
    20
struct Depot : PoolItem<Depot, DepotID, &_Depot_pool> {
4eb9b01e37ff (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7866
diff changeset
    21
	TileIndex xy;
4eb9b01e37ff (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7866
diff changeset
    22
	TownID town_index;
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4297
diff changeset
    23
7885
4eb9b01e37ff (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7866
diff changeset
    24
	Depot(TileIndex xy = 0) : xy(xy) {}
4eb9b01e37ff (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7866
diff changeset
    25
	~Depot();
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4347
diff changeset
    26
7992
8ac3fcd8d570 (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: 7885
diff changeset
    27
	inline bool IsValid() const { return this->xy != 0; }
7885
4eb9b01e37ff (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7866
diff changeset
    28
};
4388
e5a166837162 (svn r6141) -Codechange: introduced DepotID and used it as much as possible
truelight
parents: 4352
diff changeset
    29
7885
4eb9b01e37ff (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7866
diff changeset
    30
static inline bool IsValidDepotID(DepotID index)
4388
e5a166837162 (svn r6141) -Codechange: introduced DepotID and used it as much as possible
truelight
parents: 4352
diff changeset
    31
{
7885
4eb9b01e37ff (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7866
diff changeset
    32
	return index < GetDepotPoolSize() && GetDepot(index)->IsValid();
4388
e5a166837162 (svn r6141) -Codechange: introduced DepotID and used it as much as possible
truelight
parents: 4352
diff changeset
    33
}
e5a166837162 (svn r6141) -Codechange: introduced DepotID and used it as much as possible
truelight
parents: 4352
diff changeset
    34
7134
c5a90dd41166 (svn r9869) -Codechange: replace some bytes with VehicleType, i.e. more type strictness.
rubidium
parents: 6573
diff changeset
    35
void ShowDepotWindow(TileIndex tile, VehicleType type);
4638
8abe4f10b94b (svn r6513) -Codechange: unified the code to draw depot windows
bjarni
parents: 4549
diff changeset
    36
7885
4eb9b01e37ff (svn r10758) -Codechange: make the depot struct use the pool item class as super class.
rubidium
parents: 7866
diff changeset
    37
#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
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    38
#define FOR_ALL_DEPOTS(d) FOR_ALL_DEPOTS_FROM(d, 0)
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    39
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    40
#define MIN_SERVINT_PERCENT  5
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    41
#define MAX_SERVINT_PERCENT 90
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    42
#define MIN_SERVINT_DAYS    30
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    43
#define MAX_SERVINT_DAYS   800
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
    44
4388
e5a166837162 (svn r6141) -Codechange: introduced DepotID and used it as much as possible
truelight
parents: 4352
diff changeset
    45
/**
e5a166837162 (svn r6141) -Codechange: introduced DepotID and used it as much as possible
truelight
parents: 4352
diff changeset
    46
 * Get the service interval domain.
1790
4afb4b4e4278 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1718
diff changeset
    47
 * Get the new proposed service interval for the vehicle is indeed, clamped
4afb4b4e4278 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1718
diff changeset
    48
 * within the given bounds. @see MIN_SERVINT_PERCENT ,etc.
4afb4b4e4278 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1718
diff changeset
    49
 * @param index proposed service interval
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5726
diff changeset
    50
 * @return service interval
1790
4afb4b4e4278 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1718
diff changeset
    51
 */
4297
47ce9665b4af (svn r5934) -Cleanup: forgot some conversions to Year and to Date
rubidium
parents: 4077
diff changeset
    52
static inline Date GetServiceIntervalClamped(uint index)
1790
4afb4b4e4278 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1718
diff changeset
    53
{
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8008
diff changeset
    54
	return (_patches.servint_ispercent) ? Clamp(index, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : Clamp(index, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS);
1790
4afb4b4e4278 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1718
diff changeset
    55
}
4afb4b4e4278 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1718
diff changeset
    56
1330
8a67d04016ce (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
/**
8a67d04016ce (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
 * Check if a tile is a depot of the given type.
8a67d04016ce (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
    59
 */
8a67d04016ce (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
    60
static inline bool IsTileDepotType(TileIndex tile, TransportType type)
8a67d04016ce (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
    61
{
4000
bab1ebc37da0 (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
    62
	switch (type) {
1330
8a67d04016ce (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
    63
		case TRANSPORT_RAIL:
6508
8bd54af67f0b (svn r8954) -Codechange: remove direct map accesses from non-map-accessor headers.
rubidium
parents: 6460
diff changeset
    64
			return IsTileType(tile, MP_RAILWAY) && GetRailTileType(tile)  == RAIL_TILE_DEPOT;
1959
fc150d5a23cf (svn r2465) Remove some unreachable code
tron
parents: 1944
diff changeset
    65
1330
8a67d04016ce (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
    66
		case TRANSPORT_ROAD:
8008
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    67
			return IsTileType(tile, MP_ROAD)    && GetRoadTileType(tile)  == ROAD_TILE_DEPOT;
1959
fc150d5a23cf (svn r2465) Remove some unreachable code
tron
parents: 1944
diff changeset
    68
1330
8a67d04016ce (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
    69
		case TRANSPORT_WATER:
6508
8bd54af67f0b (svn r8954) -Codechange: remove direct map accesses from non-map-accessor headers.
rubidium
parents: 6460
diff changeset
    70
			return IsTileType(tile, MP_WATER)   && GetWaterTileType(tile) == WATER_TILE_DEPOT;
1959
fc150d5a23cf (svn r2465) Remove some unreachable code
tron
parents: 1944
diff changeset
    71
1330
8a67d04016ce (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
    72
		default:
6508
8bd54af67f0b (svn r8954) -Codechange: remove direct map accesses from non-map-accessor headers.
rubidium
parents: 6460
diff changeset
    73
			NOT_REACHED();
1330
8a67d04016ce (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
    74
			return false;
8a67d04016ce (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
    75
	}
8a67d04016ce (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
    76
}
8a67d04016ce (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
    77
8008
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    78
/**
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    79
 * Is the given tile a tile with a depot on it?
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    80
 * @param tile the tile to check
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    81
 * @return true if and only if there is a depot on the tile.
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    82
 */
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    83
static inline bool IsDepotTile(TileIndex tile)
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    84
{
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    85
	switch (GetTileType(tile)) {
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    86
		case MP_ROAD:    return GetRoadTileType(tile)  == ROAD_TILE_DEPOT;
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    87
		case MP_WATER:   return GetWaterTileType(tile) == WATER_TILE_DEPOT;
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    88
		case MP_RAILWAY: return GetRailTileType(tile)  == RAIL_TILE_DEPOT;
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    89
		case MP_STATION: return IsHangar(tile);
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    90
		default:         return false;
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    91
	}
82de75c83c3e (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7992
diff changeset
    92
}
1650
12a20779af79 (svn r2154) - Fix: [NPF] Vehicles should no longer try to drive through the back of depots and road stations.
matthijs
parents: 1330
diff changeset
    93
2085
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
    94
/**
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    95
 * Find out if the slope of the tile is suitable to build a depot of given direction
6460
b3017e083031 (svn r8876) -Fix
tron
parents: 6451
diff changeset
    96
 * @param direction The direction in which the depot's exit points
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    97
 * @param tileh The slope of the tile in question
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
    98
 * @return true if the construction is possible
2085
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
    99
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   100
 * This is checked by the ugly 0x4C >> direction magic, which does the following:
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   101
 * 0x4C is 0100 1100 and tileh has only bits 0..3 set (steep tiles are ruled out)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   102
 * So: for direction (only the significant bits are shown)<p>
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   103
 * 00 (exit towards NE) we need either bit 2 or 3 set in tileh: 0x4C >> 0 = 1100<p>
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   104
 * 01 (exit towards SE) we need either bit 1 or 2 set in tileh: 0x4C >> 1 = 0110<p>
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   105
 * 02 (exit towards SW) we need either bit 0 or 1 set in tileh: 0x4C >> 2 = 0011<p>
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   106
 * 03 (exit towards NW) we need either bit 0 or 4 set in tileh: 0x4C >> 3 = 1001<p>
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   107
 * So ((0x4C >> direction) & tileh) determines whether the depot can be built on the current tileh
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   108
 */
6460
b3017e083031 (svn r8876) -Fix
tron
parents: 6451
diff changeset
   109
static inline bool CanBuildDepotByTileh(DiagDirection direction, Slope tileh)
2085
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   110
{
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3884
diff changeset
   111
	return ((0x4C >> direction) & tileh) != 0;
2085
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   112
}
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   113
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1959
diff changeset
   114
Depot *GetDepotByTile(TileIndex tile);
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6508
diff changeset
   115
void InitializeDepots();
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
   116
5255
b8445fda3fe2 (svn r7385) -Fix: FS#418 Deleting Train in depot with autoreplace failes
bjarni
parents: 5216
diff changeset
   117
void DeleteDepotHighlightOfVehicle(const Vehicle *v);
b8445fda3fe2 (svn r7385) -Fix: FS#418 Deleting Train in depot with autoreplace failes
bjarni
parents: 5216
diff changeset
   118
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
diff changeset
   119
#endif /* DEPOT_H */