src/depot.h
changeset 5475 2e6990a8c7c4
parent 5255 b693a9941b8c
child 6125 a6fff965707c
equal deleted inserted replaced
5474:ac55aefc54f3 5475:2e6990a8c7c4
       
     1 /* $Id$ */
       
     2 
       
     3 #ifndef DEPOT_H
       
     4 #define DEPOT_H
       
     5 
       
     6 /** @file depot.h Header files for depots (not hangars)
       
     7  *  @see depot.c */
       
     8 
       
     9 #include "direction.h"
       
    10 #include "oldpool.h"
       
    11 #include "tile.h"
       
    12 #include "variables.h"
       
    13 
       
    14 struct Depot {
       
    15 	TileIndex xy;
       
    16 	TownID town_index;
       
    17 	DepotID index;
       
    18 };
       
    19 
       
    20 DECLARE_OLD_POOL(Depot, Depot, 3, 8000)
       
    21 
       
    22 /**
       
    23  * Check if a depot really exists.
       
    24  */
       
    25 static inline bool IsValidDepot(const Depot *depot)
       
    26 {
       
    27 	return depot != NULL && depot->xy != 0;
       
    28 }
       
    29 
       
    30 static inline bool IsValidDepotID(uint index)
       
    31 {
       
    32 	return index < GetDepotPoolSize() && IsValidDepot(GetDepot(index));
       
    33 }
       
    34 
       
    35 void DestroyDepot(Depot *depot);
       
    36 
       
    37 static inline void DeleteDepot(Depot *depot)
       
    38 {
       
    39 	DestroyDepot(depot);
       
    40 	depot->xy = 0;
       
    41 }
       
    42 
       
    43 void ShowDepotWindow(TileIndex tile, byte type);
       
    44 
       
    45 #define FOR_ALL_DEPOTS_FROM(d, start) for (d = GetDepot(start); d != NULL; d = (d->index + 1U < GetDepotPoolSize()) ? GetDepot(d->index + 1U) : NULL) if (IsValidDepot(d))
       
    46 #define FOR_ALL_DEPOTS(d) FOR_ALL_DEPOTS_FROM(d, 0)
       
    47 
       
    48 #define MIN_SERVINT_PERCENT  5
       
    49 #define MAX_SERVINT_PERCENT 90
       
    50 #define MIN_SERVINT_DAYS    30
       
    51 #define MAX_SERVINT_DAYS   800
       
    52 
       
    53 /**
       
    54  * Get the service interval domain.
       
    55  * Get the new proposed service interval for the vehicle is indeed, clamped
       
    56  * within the given bounds. @see MIN_SERVINT_PERCENT ,etc.
       
    57  * @param index proposed service interval
       
    58  */
       
    59 static inline Date GetServiceIntervalClamped(uint index)
       
    60 {
       
    61 	return (_patches.servint_ispercent) ? clamp(index, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : clamp(index, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS);
       
    62 }
       
    63 
       
    64 /**
       
    65  * Check if a tile is a depot of the given type.
       
    66  */
       
    67 static inline bool IsTileDepotType(TileIndex tile, TransportType type)
       
    68 {
       
    69 	switch (type) {
       
    70 		case TRANSPORT_RAIL:
       
    71 			return IsTileType(tile, MP_RAILWAY) && (_m[tile].m5 & 0xFC) == 0xC0;
       
    72 
       
    73 		case TRANSPORT_ROAD:
       
    74 			return IsTileType(tile, MP_STREET) && (_m[tile].m5 & 0xF0) == 0x20;
       
    75 
       
    76 		case TRANSPORT_WATER:
       
    77 			return IsTileType(tile, MP_WATER) && (_m[tile].m5 & ~3) == 0x80;
       
    78 
       
    79 		default:
       
    80 			assert(0);
       
    81 			return false;
       
    82 	}
       
    83 }
       
    84 
       
    85 
       
    86 /**
       
    87  * Find out if the slope of the tile is suitable to build a depot of given direction
       
    88  * @param direction The direction in which the depot's exit points. Starts with 0 as NE and goes Clockwise
       
    89  * @param tileh The slope of the tile in question
       
    90  * @return true if the construction is possible
       
    91 
       
    92  * This is checked by the ugly 0x4C >> direction magic, which does the following:
       
    93  * 0x4C is 0100 1100 and tileh has only bits 0..3 set (steep tiles are ruled out)
       
    94  * So: for direction (only the significant bits are shown)<p>
       
    95  * 00 (exit towards NE) we need either bit 2 or 3 set in tileh: 0x4C >> 0 = 1100<p>
       
    96  * 01 (exit towards SE) we need either bit 1 or 2 set in tileh: 0x4C >> 1 = 0110<p>
       
    97  * 02 (exit towards SW) we need either bit 0 or 1 set in tileh: 0x4C >> 2 = 0011<p>
       
    98  * 03 (exit towards NW) we need either bit 0 or 4 set in tileh: 0x4C >> 3 = 1001<p>
       
    99  * So ((0x4C >> direction) & tileh) determines whether the depot can be built on the current tileh
       
   100  */
       
   101 static inline bool CanBuildDepotByTileh(uint32 direction, Slope tileh)
       
   102 {
       
   103 	return ((0x4C >> direction) & tileh) != 0;
       
   104 }
       
   105 
       
   106 Depot *GetDepotByTile(TileIndex tile);
       
   107 void InitializeDepots(void);
       
   108 Depot *AllocateDepot(void);
       
   109 
       
   110 void DeleteDepotHighlightOfVehicle(const Vehicle *v);
       
   111 
       
   112 #endif /* DEPOT_H */