2007-01-14 19:18:50 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-02-23 08:37:33 +00:00
|
|
|
/** @file station.cpp */
|
2007-01-14 19:18:50 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "openttd.h"
|
|
|
|
#include "bridge_map.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "functions.h"
|
|
|
|
#include "station_map.h"
|
|
|
|
#include "table/sprites.h"
|
|
|
|
#include "table/strings.h"
|
|
|
|
#include "map.h"
|
|
|
|
#include "tile.h"
|
|
|
|
#include "station.h"
|
|
|
|
#include "gfx.h"
|
|
|
|
#include "window.h"
|
|
|
|
#include "viewport.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "town.h"
|
|
|
|
#include "vehicle.h"
|
|
|
|
#include "news.h"
|
|
|
|
#include "saveload.h"
|
|
|
|
#include "economy.h"
|
|
|
|
#include "player.h"
|
|
|
|
#include "airport.h"
|
|
|
|
#include "sprite.h"
|
|
|
|
#include "depot.h"
|
|
|
|
#include "train.h"
|
|
|
|
#include "water_map.h"
|
|
|
|
#include "industry_map.h"
|
|
|
|
#include "newgrf_callbacks.h"
|
|
|
|
#include "newgrf_station.h"
|
|
|
|
#include "yapf/yapf.h"
|
|
|
|
#include "date.h"
|
|
|
|
#include "helpers.hpp"
|
|
|
|
|
|
|
|
Station::Station(TileIndex tile)
|
|
|
|
{
|
|
|
|
DEBUG(station, cDebugCtorLevel, "I+%3d", index);
|
|
|
|
|
|
|
|
xy = tile;
|
|
|
|
airport_tile = dock_tile = train_tile = 0;
|
|
|
|
bus_stops = truck_stops = NULL;
|
|
|
|
had_vehicle_of_type = 0;
|
|
|
|
time_since_load = 255;
|
|
|
|
time_since_unload = 255;
|
|
|
|
delete_ctr = 0;
|
|
|
|
facilities = 0;
|
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
last_vehicle_type = VEH_INVALID;
|
2007-01-14 19:18:50 +00:00
|
|
|
|
2007-01-18 09:34:44 +00:00
|
|
|
random_bits = 0; // Random() must be called when station is really built (DC_EXEC)
|
2007-01-14 19:18:50 +00:00
|
|
|
waiting_triggers = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-04-04 01:35:16 +00:00
|
|
|
* Clean up a station by clearing vehicle orders and invalidating windows.
|
|
|
|
* Aircraft-Hangar orders need special treatment here, as the hangars are
|
|
|
|
* actually part of a station (tiletype is STATION), but the order type
|
|
|
|
* is OT_GOTO_DEPOT.
|
|
|
|
*/
|
2007-01-14 19:18:50 +00:00
|
|
|
Station::~Station()
|
|
|
|
{
|
|
|
|
DEBUG(station, cDebugCtorLevel, "I-%3d", index);
|
|
|
|
|
|
|
|
DeleteName(string_id);
|
|
|
|
MarkDirty();
|
|
|
|
RebuildStationLists();
|
|
|
|
InvalidateWindowClasses(WC_STATION_LIST);
|
|
|
|
|
|
|
|
DeleteWindowById(WC_STATION_VIEW, index);
|
|
|
|
|
|
|
|
/* Now delete all orders that go to the station */
|
|
|
|
RemoveOrderFromAllVehicles(OT_GOTO_STATION, index);
|
|
|
|
|
2007-01-14 23:36:25 +00:00
|
|
|
/* Subsidies need removal as well */
|
2007-01-14 19:18:50 +00:00
|
|
|
DeleteSubsidyWithStation(index);
|
|
|
|
|
|
|
|
free(speclist);
|
|
|
|
xy = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* Station::operator new(size_t size)
|
|
|
|
{
|
|
|
|
Station *st = AllocateRaw();
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* Station::operator new(size_t size, int st_idx)
|
|
|
|
{
|
|
|
|
if (!AddBlockIfNeeded(&_Station_pool, st_idx))
|
|
|
|
error("Stations: failed loading savegame: too many stations");
|
|
|
|
|
|
|
|
Station *st = GetStation(st_idx);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Station::operator delete(void *p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Station::operator delete(void *p, int st_idx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-01-18 09:34:44 +00:00
|
|
|
/** Called when new facility is built on the station. If it is the first facility
|
2007-04-04 01:35:16 +00:00
|
|
|
* it initializes also 'xy' and 'random_bits' members */
|
2007-01-18 09:34:44 +00:00
|
|
|
void Station::AddFacility(byte new_facility_bit, TileIndex facil_xy)
|
|
|
|
{
|
|
|
|
if (facilities == 0) {
|
|
|
|
xy = facil_xy;
|
|
|
|
random_bits = Random();
|
|
|
|
}
|
|
|
|
facilities |= new_facility_bit;
|
|
|
|
owner = _current_player;
|
|
|
|
build_date = _date;
|
|
|
|
}
|
|
|
|
|
2007-01-14 19:18:50 +00:00
|
|
|
void Station::MarkDirty() const
|
|
|
|
{
|
|
|
|
if (sign.width_1 != 0) {
|
|
|
|
InvalidateWindowWidget(WC_STATION_VIEW, index, 1);
|
|
|
|
|
2007-05-26 12:19:54 +00:00
|
|
|
/* We use ZOOM_LVL_MAX here, as every viewport can have an other zoom,
|
|
|
|
* and there is no way for us to know which is the biggest. So make the
|
|
|
|
* biggest area dirty, and we are safe for sure. */
|
2007-01-14 19:18:50 +00:00
|
|
|
MarkAllViewportsDirty(
|
|
|
|
sign.left - 6,
|
|
|
|
sign.top,
|
2007-05-26 12:19:54 +00:00
|
|
|
sign.left + ScaleByZoom(sign.width_1 + 12, ZOOM_LVL_MAX),
|
|
|
|
sign.top + ScaleByZoom(12, ZOOM_LVL_MAX));
|
2007-01-14 19:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-08 09:35:39 +00:00
|
|
|
void Station::MarkTilesDirty(bool cargo_change) const
|
2007-01-14 19:18:50 +00:00
|
|
|
{
|
|
|
|
TileIndex tile = train_tile;
|
|
|
|
int w, h;
|
|
|
|
|
2007-01-14 23:36:25 +00:00
|
|
|
/* XXX No station is recorded as 0, not INVALID_TILE... */
|
2007-01-14 19:18:50 +00:00
|
|
|
if (tile == 0) return;
|
|
|
|
|
2007-06-08 09:35:39 +00:00
|
|
|
/* cargo_change is set if we're refreshing the tiles due to cargo moving
|
|
|
|
* around. */
|
|
|
|
if (cargo_change) {
|
|
|
|
/* Don't waste time updating if there are no custom station graphics
|
|
|
|
* that might change. Even if there are custom graphics, they might
|
|
|
|
* not change. Unfortunately we have no way of telling. */
|
|
|
|
if (this->num_specs == 0) return;
|
|
|
|
}
|
|
|
|
|
2007-01-14 19:18:50 +00:00
|
|
|
for (h = 0; h < trainst_h; h++) {
|
|
|
|
for (w = 0; w < trainst_w; w++) {
|
|
|
|
if (TileBelongsToRailStation(tile)) {
|
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
tile += TileDiffXY(1, 0);
|
|
|
|
}
|
|
|
|
tile += TileDiffXY(-w, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Station::TileBelongsToRailStation(TileIndex tile) const
|
|
|
|
{
|
|
|
|
return IsTileType(tile, MP_STATION) && GetStationIndex(tile) == index && IsRailwayStation(tile);
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
/*static*/ Station *Station::AllocateRaw()
|
2007-01-14 19:18:50 +00:00
|
|
|
{
|
|
|
|
Station *st = NULL;
|
|
|
|
|
|
|
|
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
|
|
|
|
* TODO - This is just a temporary stage, this will be removed. */
|
|
|
|
for (st = GetStation(0); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) {
|
2007-02-13 15:42:52 +00:00
|
|
|
if (!st->IsValid()) {
|
2007-01-14 19:18:50 +00:00
|
|
|
StationID index = st->index;
|
|
|
|
|
|
|
|
memset(st, 0, sizeof(Station));
|
|
|
|
st->index = index;
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we can add a block to the pool */
|
|
|
|
if (AddBlockToPool(&_Station_pool)) return AllocateRaw();
|
|
|
|
|
|
|
|
_error_message = STR_3008_TOO_MANY_STATIONS_LOADING;
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-01-14 23:02:12 +00:00
|
|
|
|
|
|
|
|
2007-02-13 16:36:38 +00:00
|
|
|
/** Obtain the length of a platform
|
|
|
|
* @pre tile must be a railway station tile
|
|
|
|
* @param tile A tile that contains the platform in question
|
2007-04-04 01:35:16 +00:00
|
|
|
* @return The length of the platform
|
2007-02-13 16:36:38 +00:00
|
|
|
*/
|
|
|
|
uint Station::GetPlatformLength(TileIndex tile) const
|
|
|
|
{
|
|
|
|
TileIndex t;
|
|
|
|
TileIndexDiff delta;
|
|
|
|
uint len = 0;
|
|
|
|
assert(TileBelongsToRailStation(tile));
|
|
|
|
|
|
|
|
delta = (GetRailStationAxis(tile) == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
|
|
|
|
|
|
|
|
t = tile;
|
|
|
|
do {
|
|
|
|
t -= delta;
|
|
|
|
len++;
|
|
|
|
} while (IsCompatibleTrainStationTile(t, tile));
|
|
|
|
|
|
|
|
t = tile;
|
|
|
|
do {
|
|
|
|
t += delta;
|
|
|
|
len++;
|
|
|
|
} while (IsCompatibleTrainStationTile(t, tile));
|
|
|
|
|
|
|
|
return len - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Determines the REMAINING length of a platform, starting at (and including)
|
|
|
|
* the given tile.
|
|
|
|
* @param tile the tile from which to start searching. Must be a railway station tile
|
|
|
|
* @param dir The direction in which to search.
|
|
|
|
* @return The platform length
|
|
|
|
*/
|
|
|
|
uint Station::GetPlatformLength(TileIndex tile, DiagDirection dir) const
|
|
|
|
{
|
|
|
|
TileIndex start_tile = tile;
|
|
|
|
uint length = 0;
|
|
|
|
assert(IsRailwayStationTile(tile));
|
|
|
|
assert(dir < DIAGDIR_END);
|
|
|
|
|
|
|
|
do {
|
|
|
|
length ++;
|
|
|
|
tile += TileOffsByDiagDir(dir);
|
|
|
|
} while (IsCompatibleTrainStationTile(tile, start_tile));
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2007-02-01 16:48:38 +00:00
|
|
|
/** Determines whether a station is a buoy only.
|
|
|
|
* @todo Ditch this encoding of buoys
|
|
|
|
*/
|
|
|
|
bool Station::IsBuoy() const
|
|
|
|
{
|
2007-02-13 15:44:37 +00:00
|
|
|
return (had_vehicle_of_type & HVOT_BUOY) != 0;
|
2007-02-01 16:48:38 +00:00
|
|
|
}
|
|
|
|
|
2007-02-13 15:42:52 +00:00
|
|
|
/** Determines whether a station exists
|
|
|
|
* @todo replace 0 by INVALID_TILE
|
|
|
|
*/
|
|
|
|
bool Station::IsValid() const
|
|
|
|
{
|
|
|
|
return xy != 0;
|
|
|
|
}
|
|
|
|
|
2007-01-14 23:02:12 +00:00
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* StationRect implementation */
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
StationRect::StationRect()
|
|
|
|
{
|
|
|
|
MakeEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StationRect::MakeEmpty()
|
|
|
|
{
|
|
|
|
left = top = right = bottom = 0;
|
|
|
|
}
|
|
|
|
|
2007-02-02 16:51:10 +00:00
|
|
|
/**
|
|
|
|
* Determines whether a given point (x, y) is within a certain distance of
|
|
|
|
* the station rectangle.
|
|
|
|
* @note x and y are in Tile coordinates
|
|
|
|
* @param x X coordinate
|
|
|
|
* @param y Y coordinate
|
|
|
|
* @param distance The maxmium distance a point may have (L1 norm)
|
|
|
|
* @return true if the point is within distance tiles of the station rectangle
|
|
|
|
*/
|
|
|
|
bool StationRect::PtInExtendedRect(int x, int y, int distance) const
|
2007-01-14 23:02:12 +00:00
|
|
|
{
|
2007-02-02 16:51:10 +00:00
|
|
|
return (left - distance <= x && x <= right + distance && top - distance <= y && y <= bottom + distance);
|
2007-01-14 23:02:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StationRect::IsEmpty() const
|
|
|
|
{
|
|
|
|
return (left == 0 || left > right || top > bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StationRect::BeforeAddTile(TileIndex tile, StationRectMode mode)
|
|
|
|
{
|
|
|
|
int x = TileX(tile);
|
|
|
|
int y = TileY(tile);
|
|
|
|
if (IsEmpty()) {
|
2007-01-14 23:36:25 +00:00
|
|
|
/* we are adding the first station tile */
|
2007-01-14 23:02:12 +00:00
|
|
|
left = right = x;
|
|
|
|
top = bottom = y;
|
2007-01-14 23:36:25 +00:00
|
|
|
|
2007-02-02 16:51:10 +00:00
|
|
|
} else if (!PtInExtendedRect(x, y)) {
|
2007-01-14 23:36:25 +00:00
|
|
|
/* current rect is not empty and new point is outside this rect */
|
|
|
|
/* make new spread-out rectangle */
|
2007-01-14 23:02:12 +00:00
|
|
|
Rect new_rect = {min(x, left), min(y, top), max(x, right), max(y, bottom)};
|
2007-01-14 23:36:25 +00:00
|
|
|
|
|
|
|
/* check new rect dimensions against preset max */
|
2007-01-14 23:02:12 +00:00
|
|
|
int w = new_rect.right - new_rect.left + 1;
|
|
|
|
int h = new_rect.bottom - new_rect.top + 1;
|
|
|
|
if (mode != ADD_FORCE && (w > _patches.station_spread || h > _patches.station_spread)) {
|
|
|
|
assert(mode != ADD_TRY);
|
|
|
|
_error_message = STR_306C_STATION_TOO_SPREAD_OUT;
|
|
|
|
return false;
|
|
|
|
}
|
2007-01-14 23:36:25 +00:00
|
|
|
|
|
|
|
/* spread-out ok, return true */
|
2007-01-14 23:02:12 +00:00
|
|
|
if (mode != ADD_TEST) {
|
2007-01-14 23:36:25 +00:00
|
|
|
/* we should update the station rect */
|
2007-01-14 23:02:12 +00:00
|
|
|
*this = new_rect;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
; // new point is inside the rect, we don't need to do anything
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StationRect::BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode)
|
|
|
|
{
|
|
|
|
return BeforeAddTile(tile, mode) && BeforeAddTile(TILE_ADDXY(tile, w - 1, h - 1), mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ bool StationRect::ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a)
|
|
|
|
{
|
|
|
|
TileIndex top_left = TileXY(left_a, top_a);
|
|
|
|
int width = right_a - left_a + 1;
|
|
|
|
int height = bottom_a - top_a + 1;
|
2007-01-14 23:36:25 +00:00
|
|
|
|
2007-01-14 23:02:12 +00:00
|
|
|
BEGIN_TILE_LOOP(tile, width, height, top_left)
|
|
|
|
if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st_id) return true;
|
|
|
|
END_TILE_LOOP(tile, width, height, top_left);
|
2007-01-14 23:36:25 +00:00
|
|
|
|
2007-01-14 23:02:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StationRect::AfterRemoveTile(Station *st, TileIndex tile)
|
|
|
|
{
|
|
|
|
int x = TileX(tile);
|
|
|
|
int y = TileY(tile);
|
|
|
|
|
2007-01-14 23:36:25 +00:00
|
|
|
/* look if removed tile was on the bounding rect edge
|
|
|
|
* and try to reduce the rect by this edge
|
|
|
|
* do it until we have empty rect or nothing to do */
|
2007-01-14 23:02:12 +00:00
|
|
|
for (;;) {
|
2007-01-14 23:36:25 +00:00
|
|
|
/* check if removed tile is on rect edge */
|
2007-01-14 23:02:12 +00:00
|
|
|
bool left_edge = (x == left);
|
|
|
|
bool right_edge = (x == right);
|
|
|
|
bool top_edge = (y == top);
|
|
|
|
bool bottom_edge = (y == bottom);
|
2007-01-14 23:36:25 +00:00
|
|
|
|
|
|
|
/* can we reduce the rect in either direction? */
|
|
|
|
bool reduce_x = ((left_edge || right_edge) && !ScanForStationTiles(st->index, x, top, x, bottom));
|
|
|
|
bool reduce_y = ((top_edge || bottom_edge) && !ScanForStationTiles(st->index, left, y, right, y));
|
2007-01-14 23:02:12 +00:00
|
|
|
if (!(reduce_x || reduce_y)) break; // nothing to do (can't reduce)
|
2007-01-14 23:36:25 +00:00
|
|
|
|
2007-01-14 23:02:12 +00:00
|
|
|
if (reduce_x) {
|
2007-01-14 23:36:25 +00:00
|
|
|
/* reduce horizontally */
|
2007-01-14 23:02:12 +00:00
|
|
|
if (left_edge) {
|
2007-01-14 23:36:25 +00:00
|
|
|
/* move left edge right */
|
2007-01-14 23:02:12 +00:00
|
|
|
left = x = x + 1;
|
|
|
|
} else {
|
2007-01-14 23:36:25 +00:00
|
|
|
/* move right edge left */
|
2007-01-14 23:02:12 +00:00
|
|
|
right = x = x - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (reduce_y) {
|
2007-01-14 23:36:25 +00:00
|
|
|
/* reduce vertically */
|
2007-01-14 23:02:12 +00:00
|
|
|
if (top_edge) {
|
2007-01-14 23:36:25 +00:00
|
|
|
/* move top edge down */
|
2007-01-14 23:02:12 +00:00
|
|
|
top = y = y + 1;
|
|
|
|
} else {
|
2007-01-14 23:36:25 +00:00
|
|
|
/* move bottom edge up */
|
2007-01-14 23:02:12 +00:00
|
|
|
bottom = y = y - 1;
|
|
|
|
}
|
|
|
|
}
|
2007-01-14 23:36:25 +00:00
|
|
|
|
2007-01-14 23:02:12 +00:00
|
|
|
if (left > right || top > bottom) {
|
2007-01-14 23:36:25 +00:00
|
|
|
/* can't continue, if the remaining rectangle is empty */
|
2007-01-14 23:02:12 +00:00
|
|
|
MakeEmpty();
|
|
|
|
return true; // empty remaining rect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false; // non-empty remaining rect
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StationRect::AfterRemoveRect(Station *st, TileIndex tile, int w, int h)
|
|
|
|
{
|
2007-02-02 16:51:10 +00:00
|
|
|
assert(PtInExtendedRect(TileX(tile), TileY(tile)));
|
|
|
|
assert(PtInExtendedRect(TileX(tile) + w - 1, TileY(tile) + h - 1));
|
2007-01-14 23:36:25 +00:00
|
|
|
|
|
|
|
bool empty = AfterRemoveTile(st, tile);
|
2007-01-14 23:02:12 +00:00
|
|
|
if (w != 1 || h != 1) empty = empty || AfterRemoveTile(st, TILE_ADDXY(tile, w - 1, h - 1));
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
StationRect& StationRect::operator = (Rect src)
|
|
|
|
{
|
|
|
|
left = src.left;
|
|
|
|
top = src.top;
|
|
|
|
right = src.right;
|
|
|
|
bottom = src.bottom;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2007-01-17 11:15:51 +00:00
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* RoadStop implementation */
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
/** Allocates a new RoadStop onto the pool, or recycles an unsed one
|
|
|
|
* @return a pointer to the new roadstop
|
|
|
|
*/
|
|
|
|
void *RoadStop::operator new(size_t size)
|
|
|
|
{
|
|
|
|
RoadStop *rs = AllocateRaw();
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets a RoadStop with a given index and allocates it when needed
|
|
|
|
* @return a pointer to the roadstop
|
|
|
|
*/
|
|
|
|
void *RoadStop::operator new(size_t size, int index)
|
|
|
|
{
|
|
|
|
if (!AddBlockIfNeeded(&_RoadStop_pool, index)) {
|
|
|
|
error("RoadStops: failed loading savegame: too many RoadStops");
|
|
|
|
}
|
|
|
|
|
|
|
|
RoadStop *rs = GetRoadStop(index);
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RoadStop::operator delete(void *p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RoadStop::operator delete(void *p, int index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initializes a RoadStop */
|
2007-01-17 21:14:17 +00:00
|
|
|
RoadStop::RoadStop(TileIndex tile) :
|
2007-01-17 20:34:29 +00:00
|
|
|
xy(tile),
|
|
|
|
status(3), // stop is free
|
|
|
|
num_vehicles(0),
|
2007-01-28 21:54:40 +00:00
|
|
|
next(NULL)
|
2007-01-17 11:15:51 +00:00
|
|
|
{
|
2007-01-17 21:14:17 +00:00
|
|
|
DEBUG(ms, cDebugCtorLevel, "I+ at %d[0x%x]", tile, tile);
|
2007-01-17 11:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** De-Initializes a RoadStops. This includes clearing all slots that vehicles might
|
|
|
|
* have and unlinks it from the linked list of road stops at the given station
|
|
|
|
*/
|
|
|
|
RoadStop::~RoadStop()
|
|
|
|
{
|
|
|
|
/* Clear the slot assignment of all vehicles heading for this road stop */
|
|
|
|
if (num_vehicles != 0) {
|
2007-01-17 20:34:29 +00:00
|
|
|
Vehicle *v;
|
|
|
|
|
2007-01-17 11:15:51 +00:00
|
|
|
FOR_ALL_VEHICLES(v) {
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type == VEH_ROAD && v->u.road.slot == this) ClearSlot(v);
|
2007-01-17 11:15:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(num_vehicles == 0);
|
|
|
|
|
2007-01-17 21:14:17 +00:00
|
|
|
DEBUG(ms, cDebugCtorLevel , "I- at %d[0x%x]", xy, xy);
|
2007-01-17 11:15:51 +00:00
|
|
|
|
|
|
|
xy = INVALID_TILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Low-level function for allocating a RoadStop on the pool */
|
2007-03-07 11:47:46 +00:00
|
|
|
RoadStop *RoadStop::AllocateRaw()
|
2007-01-17 11:15:51 +00:00
|
|
|
{
|
|
|
|
RoadStop *rs;
|
|
|
|
|
|
|
|
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
|
|
|
|
* TODO - This is just a temporary stage, this will be removed. */
|
|
|
|
for (rs = GetRoadStop(0); rs != NULL; rs = (rs->index + 1U < GetRoadStopPoolSize()) ? GetRoadStop(rs->index + 1U) : NULL) {
|
2007-01-25 08:58:09 +00:00
|
|
|
if (!rs->IsValid()) {
|
2007-01-17 11:15:51 +00:00
|
|
|
RoadStopID index = rs->index;
|
|
|
|
|
|
|
|
memset(rs, 0, sizeof(*rs));
|
|
|
|
rs->index = index;
|
|
|
|
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we can add a block to the pool */
|
|
|
|
if (AddBlockToPool(&_RoadStop_pool)) return AllocateRaw();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-01-25 08:58:09 +00:00
|
|
|
|
|
|
|
/** Determines whether a RoadStop is a valid (i.e. existing) one */
|
|
|
|
bool RoadStop::IsValid() const
|
|
|
|
{
|
|
|
|
return xy != INVALID_TILE;
|
|
|
|
}
|
2007-02-13 00:25:42 +00:00
|
|
|
|
|
|
|
/** Checks whether there is a free bay in this road stop */
|
|
|
|
bool RoadStop::HasFreeBay() const
|
|
|
|
{
|
|
|
|
return GB(status, 0, MAX_BAY_COUNT) != 0;
|
|
|
|
}
|
|
|
|
|
2007-02-14 16:37:16 +00:00
|
|
|
/** Checks whether the given bay is free in this road stop */
|
|
|
|
bool RoadStop::IsFreeBay(uint nr) const
|
|
|
|
{
|
|
|
|
assert(nr < MAX_BAY_COUNT);
|
|
|
|
return HASBIT(status, nr);
|
|
|
|
}
|
|
|
|
|
2007-02-13 00:25:42 +00:00
|
|
|
/**
|
|
|
|
* Allocates a bay
|
|
|
|
* @return the allocated bay number
|
|
|
|
* @pre this->HasFreeBay()
|
|
|
|
*/
|
|
|
|
uint RoadStop::AllocateBay()
|
|
|
|
{
|
2007-02-14 09:34:12 +00:00
|
|
|
assert(HasFreeBay());
|
|
|
|
|
2007-02-13 00:25:42 +00:00
|
|
|
/* Find the first free bay. If the bit is set, the bay is free. */
|
2007-02-14 09:34:12 +00:00
|
|
|
uint bay_nr = 0;
|
|
|
|
while (!HASBIT(status, bay_nr)) bay_nr++;
|
2007-02-13 00:25:42 +00:00
|
|
|
|
2007-02-14 09:34:12 +00:00
|
|
|
CLRBIT(status, bay_nr);
|
|
|
|
return bay_nr;
|
2007-02-13 00:25:42 +00:00
|
|
|
}
|
|
|
|
|
2007-02-14 16:37:16 +00:00
|
|
|
/**
|
|
|
|
* Allocates a bay in a drive-through road stop
|
|
|
|
* @param nr the number of the bay to allocate
|
|
|
|
*/
|
|
|
|
void RoadStop::AllocateDriveThroughBay(uint nr)
|
|
|
|
{
|
|
|
|
assert(nr < MAX_BAY_COUNT);
|
|
|
|
CLRBIT(status, nr);
|
|
|
|
}
|
|
|
|
|
2007-02-13 00:25:42 +00:00
|
|
|
/**
|
|
|
|
* Frees the given bay
|
|
|
|
* @param nr the number of the bay to free
|
|
|
|
*/
|
|
|
|
void RoadStop::FreeBay(uint nr)
|
|
|
|
{
|
|
|
|
assert(nr < MAX_BAY_COUNT);
|
|
|
|
SETBIT(status, nr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Checks whether the entrance of the road stop is occupied by a vehicle */
|
|
|
|
bool RoadStop::IsEntranceBusy() const
|
|
|
|
{
|
|
|
|
return HASBIT(status, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Makes an entrance occupied or free */
|
|
|
|
void RoadStop::SetEntranceBusy(bool busy)
|
|
|
|
{
|
2007-02-14 09:34:12 +00:00
|
|
|
SB(status, 7, 1, busy);
|
2007-02-13 00:25:42 +00:00
|
|
|
}
|