Changeset - r5665:f732864744b6
[Not reviewed]
master
0 6 1
KUDr - 17 years ago 2007-01-14 19:18:50
kudr@openttd.org
(svn r8125) -Codechange: Station is now constructed/destroyed using new/delete operators (don't worry, they still use the same memory pool). Few station related functions turned into Station::methods (just first step). All this new stuff moved from station_cmd.cpp to the new file (station.cpp).
7 files changed with 296 insertions and 162 deletions:
0 comments (0 inline, 0 general)
projects/openttd.vcproj
Show inline comments
 
@@ -338,12 +338,15 @@
 
				RelativePath=".\..\src\sound.cpp">
 
			</File>
 
			<File
 
				RelativePath=".\..\src\spritecache.cpp">
 
			</File>
 
			<File
 
				RelativePath=".\..\src\station.cpp">
 
			</File>
 
			<File
 
				RelativePath=".\..\src\string.cpp">
 
			</File>
 
			<File
 
				RelativePath=".\..\src\strings.cpp">
 
			</File>
 
			<File
projects/openttd_vs80.vcproj
Show inline comments
 
@@ -621,12 +621,15 @@
 
				RelativePath=".\..\src\sound.cpp">
 
			</File>
 
			<File
 
				RelativePath=".\..\src\spritecache.cpp">
 
			</File>
 
			<File
 
				RelativePath=".\..\src\station.cpp">
 
			</File>
 
			<File
 
				RelativePath=".\..\src\string.cpp">
 
			</File>
 
			<File
 
				RelativePath=".\..\src\strings.cpp">
 
			</File>
 
			<File
source.list
Show inline comments
 
@@ -60,12 +60,13 @@ screenshot.cpp
 
	sdl.cpp
 
#end
 
settings.cpp
 
signs.cpp
 
sound.cpp
 
spritecache.cpp
 
station.cpp
 
string.cpp
 
strings.cpp
 
texteff.cpp
 
tgp.cpp
 
thread.cpp
 
tile.cpp
src/economy.cpp
Show inline comments
 
@@ -1523,13 +1523,13 @@ int LoadUnloadVehicle(Vehicle *v, bool j
 
	if (completely_empty) {
 
		TriggerVehicle(v, VEHICLE_TRIGGER_EMPTY);
 
	}
 

	
 
	if (result != 0) {
 
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
 
		MarkStationTilesDirty(st);
 
		st->MarkTilesDirty();
 

	
 
		if (result & 2) InvalidateWindow(WC_STATION_VIEW, last_visited);
 

	
 
		if (profit != 0) {
 
			v->profit_this_year += profit;
 
			SubtractMoneyFromPlayer(-profit);
src/station.cpp
Show inline comments
 
new file 100644
 
/* $Id$ */
 

	
 
/** @file station_cmd.c */
 

	
 
#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"
 

	
 
void StationRect_Init(Station *st); // don't worry, will be removed soon
 

	
 
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;
 

	
 
	last_vehicle_type = VEH_Invalid;
 

	
 
	for (GoodsEntry *ge = goods; ge != endof(goods); ge++) {
 
		ge->waiting_acceptance = 0;
 
		ge->days_since_pickup = 0;
 
		ge->enroute_from = INVALID_STATION;
 
		ge->rating = 175;
 
		ge->last_speed = 0;
 
		ge->last_age = 0xFF;
 
		ge->feeder_profit = 0;
 
	}
 

	
 
	random_bits = Random();
 
	waiting_triggers = 0;
 

	
 
	StationRect_Init(this);
 
}
 

	
 
/**
 
* 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.
 
* @param st Station to be deleted
 
*/
 
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);
 

	
 
	//Subsidies need removal as well
 
	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)
 
{
 
}
 

	
 
void Station::MarkDirty() const
 
{
 
	if (sign.width_1 != 0) {
 
		InvalidateWindowWidget(WC_STATION_VIEW, index, 1);
 

	
 
		MarkAllViewportsDirty(
 
			sign.left - 6,
 
			sign.top,
 
			sign.left + (sign.width_1 << 2) + 12,
 
			sign.top + 48);
 
	}
 
}
 

	
 
void Station::MarkTilesDirty() const
 
{
 
	TileIndex tile = train_tile;
 
	int w, h;
 

	
 
	// XXX No station is recorded as 0, not INVALID_TILE...
 
	if (tile == 0) return;
 

	
 
	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);
 
}
 

	
 
/*static*/ Station *Station::AllocateRaw(void)
 
{
 
	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) {
 
		if (!IsValidStation(st)) {
 
			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;
 
}
src/station.h
Show inline comments
 
/* $Id$ */
 

	
 
#ifndef STATION_H
 
#define STATION_H
 

	
 
#include <deque>
 
#include "player.h"
 
#include "oldpool.h"
 
#include "sprite.h"
 
#include "tile.h"
 
#include "newgrf_station.h"
 

	
 
@@ -94,12 +95,32 @@ struct Station {
 

	
 
	byte truck_stop_status_obsolete;
 
	byte bus_stop_status_obsolete;
 
	byte blocked_months_obsolete;
 

	
 
	Rect rect; ///< Station spread out rectangle (not saved) maintained by StationRect_xxx() functions
 

	
 
	static const int cDebugCtorLevel = 1;
 

	
 
	Station(TileIndex tile = 0);
 
	~Station();
 

	
 
	/* normal new/delete operators. Used when building/removing station */
 
	void* operator new (size_t size);
 
	void operator delete(void *p);
 

	
 
	/* new/delete operators accepting station index. Used when loading station from savegame. */
 
	void* operator new (size_t size, int st_idx);
 
	void operator delete(void *p, int st_idx);
 

	
 
	void MarkDirty() const;
 
	void MarkTilesDirty() const;
 
	bool TileBelongsToRailStation(TileIndex tile) const;
 

	
 
protected:
 
	static Station *AllocateRaw(void);
 
};
 

	
 
enum {
 
	FACIL_TRAIN      = 0x01,
 
	FACIL_TRUCK_STOP = 0x02,
 
	FACIL_BUS_STOP   = 0x04,
 
@@ -173,20 +194,12 @@ static inline bool IsValidStation(const 
 

	
 
static inline bool IsValidStationID(StationID index)
 
{
 
	return index < GetStationPoolSize() && IsValidStation(GetStation(index));
 
}
 

	
 
void DestroyStation(Station *st);
 

	
 
static inline void DeleteStation(Station *st)
 
{
 
	DestroyStation(st);
 
	st->xy = 0;
 
}
 

	
 
#define FOR_ALL_STATIONS_FROM(st, start) for (st = GetStation(start); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) if (IsValidStation(st))
 
#define FOR_ALL_STATIONS(st) FOR_ALL_STATIONS_FROM(st, 0)
 

	
 

	
 
/* Stuff for ROADSTOPS */
 

	
 
@@ -216,13 +229,12 @@ static inline void DeleteRoadStop(RoadSt
 

	
 
void AfterLoadStations(void);
 
void GetProductionAroundTiles(AcceptedCargo produced, TileIndex tile, int w, int h, int rad);
 
void GetAcceptanceAroundTiles(AcceptedCargo accepts, TileIndex tile, int w, int h, int rad);
 
uint GetStationPlatforms(const Station *st, TileIndex tile);
 
uint GetPlatformLength(TileIndex tile, DiagDirection dir);
 
void MarkStationTilesDirty(const Station *st);
 

	
 

	
 
const DrawTileSprites *GetStationTileLayout(byte gfx);
 
void StationPickerDrawSprite(int x, int y, RailType railtype, int image);
 

	
 
RoadStop * GetRoadStopByTile(TileIndex tile, RoadStopType type);
src/station_cmd.cpp
Show inline comments
 
@@ -39,13 +39,13 @@ typedef enum StationRectModes
 
{
 
	RECT_MODE_TEST = 0,
 
	RECT_MODE_TRY,
 
	RECT_MODE_FORCE
 
} StationRectMode;
 

	
 
static void StationRect_Init(Station *st);
 
void StationRect_Init(Station *st);
 
static bool StationRect_IsEmpty(Station *st);
 
static bool StationRect_BeforeAddTile(Station *st, TileIndex tile, StationRectMode mode);
 
static bool StationRect_BeforeAddRect(Station *st, TileIndex tile, int w, int h, StationRectMode mode);
 
static bool StationRect_AfterRemoveTile(Station *st, TileIndex tile);
 
static bool StationRect_AfterRemoveRect(Station *st, TileIndex tile, int w, int h);
 

	
 
@@ -67,12 +67,13 @@ static void StationPoolCleanBlock(uint s
 
	uint i;
 

	
 
	for (i = start_item; i <= end_item; i++) {
 
		Station *st = GetStation(i);
 
		free(st->speclist);
 
		st->speclist = NULL;
 
		if (IsValidStation(st)) st->~Station();
 
	}
 
}
 

	
 
/**
 
 * Called if a new block is added to the roadstop-pool
 
 */
 
@@ -88,49 +89,12 @@ static void RoadStopPoolNewBlock(uint st
 
DEFINE_OLD_POOL(Station, Station, StationPoolNewBlock, StationPoolCleanBlock)
 
DEFINE_OLD_POOL(RoadStop, RoadStop, RoadStopPoolNewBlock, NULL)
 

	
 

	
 
extern void UpdateAirplanesOnNewStation(Station *st);
 

	
 
static bool TileBelongsToRailStation(const Station *st, TileIndex tile)
 
{
 
	return IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st->index && IsRailwayStation(tile);
 
}
 

	
 
void MarkStationTilesDirty(const Station *st)
 
{
 
	TileIndex tile = st->train_tile;
 
	int w, h;
 

	
 
	// XXX No station is recorded as 0, not INVALID_TILE...
 
	if (tile == 0) return;
 

	
 
	for (h = 0; h < st->trainst_h; h++) {
 
		for (w = 0; w < st->trainst_w; w++) {
 
			if (TileBelongsToRailStation(st, tile)) {
 
				MarkTileDirtyByTile(tile);
 
			}
 
			tile += TileDiffXY(1, 0);
 
		}
 
		tile += TileDiffXY(-w, 1);
 
	}
 
}
 

	
 
static void MarkStationDirty(const Station* st)
 
{
 
	if (st->sign.width_1 != 0) {
 
		InvalidateWindowWidget(WC_STATION_VIEW, st->index, 1);
 

	
 
		MarkAllViewportsDirty(
 
			st->sign.left - 6,
 
			st->sign.top,
 
			st->sign.left + (st->sign.width_1 << 2) + 12,
 
			st->sign.top + 48);
 
	}
 
}
 

	
 
static void InitializeRoadStop(RoadStop *road_stop, RoadStop *previous, TileIndex tile, StationID index)
 
{
 
	road_stop->xy = tile;
 
	road_stop->used = true;
 
	road_stop->status = 3; //stop is free
 
	road_stop->next = NULL;
 
@@ -260,13 +224,12 @@ static Station *AllocateStation(void)
 
	for (st = GetStation(0); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) {
 
		if (!IsValidStation(st)) {
 
			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 AllocateStation();
 
@@ -453,43 +416,12 @@ static Station* GetClosestStationFromTil
 
		}
 
	}
 

	
 
	return best_station;
 
}
 

	
 
static void StationInitialize(Station *st, TileIndex tile)
 
{
 
	GoodsEntry *ge;
 

	
 
	st->xy = tile;
 
	st->airport_tile = st->dock_tile = st->train_tile = 0;
 
	st->bus_stops = st->truck_stops = NULL;
 
	st->had_vehicle_of_type = 0;
 
	st->time_since_load = 255;
 
	st->time_since_unload = 255;
 
	st->delete_ctr = 0;
 
	st->facilities = 0;
 

	
 
	st->last_vehicle_type = VEH_Invalid;
 

	
 
	for (ge = st->goods; ge != endof(st->goods); ge++) {
 
		ge->waiting_acceptance = 0;
 
		ge->days_since_pickup = 0;
 
		ge->enroute_from = INVALID_STATION;
 
		ge->rating = 175;
 
		ge->last_speed = 0;
 
		ge->last_age = 0xFF;
 
		ge->feeder_profit = 0;
 
	}
 

	
 
	st->random_bits = Random();
 
	st->waiting_triggers = 0;
 

	
 
	StationRect_Init(st);
 
}
 

	
 
// Update the virtual coords needed to draw the station sign.
 
// st = Station to update for.
 
static void UpdateStationVirtCoord(Station *st)
 
{
 
	Point pt = RemapCoords2(TileX(st->xy) * TILE_SIZE, TileY(st->xy) * TILE_SIZE);
 

	
 
@@ -511,15 +443,15 @@ void UpdateAllStationVirtCoord(void)
 
	}
 
}
 

	
 
// Update the station virt coords while making the modified parts dirty.
 
static void UpdateStationVirtCoordDirty(Station *st)
 
{
 
	MarkStationDirty(st);
 
	st->MarkDirty();
 
	UpdateStationVirtCoord(st);
 
	MarkStationDirty(st);
 
	st->MarkDirty();
 
}
 

	
 
// Get a mask of the cargo types that the station accepts.
 
static uint GetAcceptanceMask(const Station *st)
 
{
 
	uint mask = 0;
 
@@ -1017,12 +949,16 @@ int32 CmdBuildRailroadStation(TileIndex 
 
	// See if there is a deleted station close to us.
 
	if (st == NULL) {
 
		st = GetClosestStationFromTile(tile_org, 8, _current_player);
 
		if (st != NULL && st->facilities) st = NULL;
 
	}
 

	
 
	/* In case of new station if DC_EXEC is NOT set we still need to create the station
 
	 * to test if everything is OK. In this case we need to delete it before return. */
 
	std::auto_ptr<Station> st_auto_delete;
 

	
 
	if (st != NULL) {
 
		// Reuse an existing station.
 
		if (st->owner != OWNER_NONE && st->owner != _current_player)
 
			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
 

	
 
		if (st->train_tile != 0) {
 
@@ -1033,23 +969,25 @@ int32 CmdBuildRailroadStation(TileIndex 
 
				return CMD_ERROR;
 
		}
 

	
 
		//XXX can't we pack this in the "else" part of the if above?
 
		if (!StationRect_BeforeAddRect(st, tile_org, w_org, h_org, RECT_MODE_TEST)) return CMD_ERROR;
 
	} else {
 
		// Create a new station
 
		st = AllocateStation();
 
		/* allocate and initialize new station */
 
		st = new Station(tile_org);
 
		if (st == NULL) return CMD_ERROR;
 

	
 
		/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
 
		st_auto_delete = std::auto_ptr<Station>(st);
 

	
 
		st->town = ClosestTownFromTile(tile_org, (uint)-1);
 
		if (IsValidPlayer(_current_player) && (flags & DC_EXEC))
 
		if (!GenerateStationName(st, tile_org, 0)) return CMD_ERROR;
 

	
 
		if (IsValidPlayer(_current_player) && (flags & DC_EXEC) != 0) {
 
			SETBIT(st->town->have_ratings, _current_player);
 

	
 
		if (!GenerateStationName(st, tile_org, 0)) return CMD_ERROR;
 

	
 
		if (flags & DC_EXEC) StationInitialize(st, tile_org);
 
		}
 
	}
 

	
 
	/* Check if the given station class is valid */
 
	if (GB(p2, 8, 8) >= STAT_CLASS_MAX) return CMD_ERROR;
 

	
 
	/* Check if we can allocate a custom stationspec to this station */
 
@@ -1123,17 +1061,19 @@ int32 CmdBuildRailroadStation(TileIndex 
 
			} while (--w);
 
			SetSignalsOnBothDir(tile_org, track);
 
			YapfNotifyTrackLayoutChange(tile_org, track);
 
			tile_org += tile_delta ^ TileDiffXY(1, 1); // perpendicular to tile_delta
 
		} while (--numtracks);
 

	
 
		MarkStationTilesDirty(st);
 
		st->MarkTilesDirty();
 
		UpdateStationVirtCoordDirty(st);
 
		UpdateStationAcceptance(st, false);
 
		RebuildStationLists();
 
		InvalidateWindow(WC_STATION_LIST, st->owner);
 
		/* success, so don't delete the new station */
 
		st_auto_delete.release();
 
	}
 

	
 
	return cost;
 
}
 

	
 
static void MakeRailwayStationAreaSmaller(Station *st)
 
@@ -1145,42 +1085,42 @@ static void MakeRailwayStationAreaSmalle
 

	
 
restart:
 

	
 
	// too small?
 
	if (w != 0 && h != 0) {
 
		// check the left side, x = constant, y changes
 
		for (i = 0; !TileBelongsToRailStation(st, tile + TileDiffXY(0, i));) {
 
		for (i = 0; !st->TileBelongsToRailStation(tile + TileDiffXY(0, i));) {
 
			// the left side is unused?
 
			if (++i == h) {
 
				tile += TileDiffXY(1, 0);
 
				w--;
 
				goto restart;
 
			}
 
		}
 

	
 
		// check the right side, x = constant, y changes
 
		for (i = 0; !TileBelongsToRailStation(st, tile + TileDiffXY(w - 1, i));) {
 
		for (i = 0; !st->TileBelongsToRailStation(tile + TileDiffXY(w - 1, i));) {
 
			// the right side is unused?
 
			if (++i == h) {
 
				w--;
 
				goto restart;
 
			}
 
		}
 

	
 
		// check the upper side, y = constant, x changes
 
		for (i = 0; !TileBelongsToRailStation(st, tile + TileDiffXY(i, 0));) {
 
		for (i = 0; !st->TileBelongsToRailStation(tile + TileDiffXY(i, 0));) {
 
			// the left side is unused?
 
			if (++i == w) {
 
				tile += TileDiffXY(0, 1);
 
				h--;
 
				goto restart;
 
			}
 
		}
 

	
 
		// check the lower side, y = constant, x changes
 
		for (i = 0; !TileBelongsToRailStation(st, tile + TileDiffXY(i, h - 1));) {
 
		for (i = 0; !st->TileBelongsToRailStation(tile + TileDiffXY(i, h - 1));) {
 
			// the left side is unused?
 
			if (++i == w) {
 
				h--;
 
				goto restart;
 
			}
 
		}
 
@@ -1221,13 +1161,13 @@ int32 CmdRemoveFromRailroadStation(TileI
 

	
 
		DeallocateSpecFromStation(st, specindex);
 

	
 
		// now we need to make the "spanned" area of the railway station smaller if we deleted something at the edges.
 
		// we also need to adjust train_tile.
 
		MakeRailwayStationAreaSmaller(st);
 
		MarkStationTilesDirty(st);
 
		st->MarkTilesDirty();
 
		UpdateStationSignCoord(st);
 

	
 
		// if we deleted the whole station, delete the train facility.
 
		if (st->train_tile == 0) {
 
			st->facilities &= ~FACIL_TRAIN;
 
			UpdateStationVirtCoordDirty(st);
 
@@ -1241,31 +1181,31 @@ int32 CmdRemoveFromRailroadStation(TileI
 
uint GetStationPlatforms(const Station *st, TileIndex tile)
 
{
 
	TileIndex t;
 
	TileIndexDiff delta;
 
	Axis axis;
 
	uint len;
 
	assert(TileBelongsToRailStation(st, tile));
 
	assert(st->TileBelongsToRailStation(tile));
 

	
 
	len = 0;
 
	axis = GetRailStationAxis(tile);
 
	delta = (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
 

	
 
	// find starting tile..
 
	t = tile;
 
	do {
 
		t -= delta;
 
		len++;
 
	} while (TileBelongsToRailStation(st, t) && GetRailStationAxis(t) == axis);
 
	} while (st->TileBelongsToRailStation(t) && GetRailStationAxis(t) == axis);
 

	
 
	// find ending tile
 
	t = tile;
 
	do {
 
		t += delta;
 
		len++;
 
	} while (TileBelongsToRailStation(st, t) && GetRailStationAxis(t) == axis);
 
	} while (st->TileBelongsToRailStation(t) && GetRailStationAxis(t) == axis);
 

	
 
	return len - 1;
 
}
 

	
 
/** Determines the REMAINING length of a platform, starting at (and including)
 
 * the given tile.
 
@@ -1311,13 +1251,13 @@ static int32 RemoveRailroadStation(Stati
 

	
 
	/* clear all areas of the station */
 
	do {
 
		int w_bak = w;
 
		do {
 
			// for nonuniform stations, only remove tiles that are actually train station tiles
 
			if (TileBelongsToRailStation(st, tile)) {
 
			if (st->TileBelongsToRailStation(tile)) {
 
				if (!EnsureNoVehicle(tile))
 
					return CMD_ERROR;
 
				cost += _price.remove_rail_station;
 
				if (flags & DC_EXEC) {
 
					Track track = GetRailStationTrack(tile);
 
					DoClearSquare(tile);
 
@@ -1453,39 +1393,43 @@ int32 CmdBuildRoadStop(TileIndex tile, u
 

	
 
	if (st != NULL &&
 
			GetNumRoadStopsInStation(st, RS_BUS) + GetNumRoadStopsInStation(st, RS_TRUCK) >= ROAD_STOP_LIMIT) {
 
		return_cmd_error(type ? STR_3008B_TOO_MANY_TRUCK_STOPS : STR_3008A_TOO_MANY_BUS_STOPS);
 
	}
 

	
 
	/* In case of new station if DC_EXEC is NOT set we still need to create the station
 
	* to test if everything is OK. In this case we need to delete it before return. */
 
	std::auto_ptr<Station> st_auto_delete;
 

	
 
	if (st != NULL) {
 
		if (st->owner != OWNER_NONE && st->owner != _current_player) {
 
			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
 
		}
 

	
 
		if (!StationRect_BeforeAddTile(st, tile, RECT_MODE_TEST)) return CMD_ERROR;
 

	
 
		FindRoadStopSpot(type, st, &currstop, &prev);
 
	} else {
 
		Town *t;
 

	
 
		st = AllocateStation();
 
		/* allocate and initialize new station */
 
		st = new Station(tile);
 
		if (st == NULL) return CMD_ERROR;
 

	
 
		st->town = t = ClosestTownFromTile(tile, (uint)-1);
 
		/* ensure that in case of error (or no DC_EXEC) the new station gets deleted upon return */
 
		st_auto_delete = std::auto_ptr<Station>(st);
 

	
 

	
 
		Town *t = st->town = ClosestTownFromTile(tile, (uint)-1);
 
		if (!GenerateStationName(st, tile, 0)) return CMD_ERROR;
 

	
 
		FindRoadStopSpot(type, st, &currstop, &prev);
 

	
 
		if (IsValidPlayer(_current_player) && (flags & DC_EXEC)) {
 
		if (IsValidPlayer(_current_player) && (flags & DC_EXEC) != 0) {
 
			SETBIT(t->have_ratings, _current_player);
 
		}
 

	
 
		st->sign.width_1 = 0;
 

	
 
		if (!GenerateStationName(st, tile, 0)) return CMD_ERROR;
 

	
 
		if (flags & DC_EXEC) StationInitialize(st, tile);
 
	}
 

	
 
	cost += (type) ? _price.build_truck_station : _price.build_bus_station;
 

	
 
	if (flags & DC_EXEC) {
 
		//point to the correct item in the _busstops or _truckstops array
 
@@ -1504,12 +1448,14 @@ int32 CmdBuildRoadStop(TileIndex tile, u
 
		MakeRoadStop(tile, st->owner, st->index, type ? RS_TRUCK : RS_BUS, (DiagDirection)p1);
 

	
 
		UpdateStationVirtCoordDirty(st);
 
		UpdateStationAcceptance(st, false);
 
		RebuildStationLists();
 
		InvalidateWindow(WC_STATION_LIST, st->owner);
 
		/* success, so don't delete the new station */
 
		st_auto_delete.release();
 
	}
 
	return cost;
 
}
 

	
 
// Remove a bus station
 
static int32 RemoveRoadStop(Station *st, uint32 flags, TileIndex tile)
 
@@ -1708,39 +1654,46 @@ int32 CmdBuildAirport(TileIndex tile, ui
 

	
 
	if (w > _patches.station_spread || h > _patches.station_spread) {
 
		_error_message = STR_306C_STATION_TOO_SPREAD_OUT;
 
		return CMD_ERROR;
 
	}
 

	
 
	/* In case of new station if DC_EXEC is NOT set we still need to create the station
 
	 * to test if everything is OK. In this case we need to delete it before return. */
 
	std::auto_ptr<Station> st_auto_delete;
 

	
 
	if (st != NULL) {
 
		if (st->owner != OWNER_NONE && st->owner != _current_player)
 
			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
 

	
 
		if (!StationRect_BeforeAddRect(st, tile, w, h, RECT_MODE_TEST)) return CMD_ERROR;
 

	
 
		if (st->airport_tile != 0)
 
			return_cmd_error(STR_300D_TOO_CLOSE_TO_ANOTHER_AIRPORT);
 
	} else {
 
		airport_upgrade = false;
 

	
 
		st = AllocateStation();
 
		/* allocate and initialize new station */
 
		st = new Station(tile);
 
		if (st == NULL) return CMD_ERROR;
 

	
 
		/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
 
		st_auto_delete = std::auto_ptr<Station>(st);
 

	
 
		st->town = t;
 

	
 
		if (IsValidPlayer(_current_player) && (flags & DC_EXEC))
 
		if (IsValidPlayer(_current_player) && (flags & DC_EXEC) != 0) {
 
			SETBIT(t->have_ratings, _current_player);
 
		}
 

	
 
		st->sign.width_1 = 0;
 

	
 
		// if airport type equals Heliport then generate
 
		// type 5 name, which is heliport, otherwise airport names (1)
 
		if (!GenerateStationName(st, tile, (p1 == AT_HELIPORT)||(p1 == AT_HELIDEPOT)||(p1 == AT_HELISTATION) ? 5 : 1))
 
			return CMD_ERROR;
 

	
 
		if (flags & DC_EXEC) StationInitialize(st, tile);
 
	}
 

	
 
	cost += _price.build_airport * w * h;
 

	
 
	if (flags & DC_EXEC) {
 
		st->owner = _current_player;
 
@@ -1772,12 +1725,14 @@ int32 CmdBuildAirport(TileIndex tile, ui
 
		}
 

	
 
		UpdateStationVirtCoordDirty(st);
 
		UpdateStationAcceptance(st, false);
 
		RebuildStationLists();
 
		InvalidateWindow(WC_STATION_LIST, st->owner);
 
		/* success, so don't delete the new station */
 
		st_auto_delete.release();
 
	}
 

	
 
	return cost;
 
}
 

	
 
static int32 RemoveAirport(Station *st, uint32 flags)
 
@@ -1838,22 +1793,25 @@ int32 CmdBuildBuoy(TileIndex tile, uint3
 
	Station *st;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 

	
 
	if (!IsClearWaterTile(tile) || tile == 0) return_cmd_error(STR_304B_SITE_UNSUITABLE);
 

	
 
	st = AllocateStation();
 
	/* allocate and initialize new station */
 
	st = new Station(tile);
 
	if (st == NULL) return CMD_ERROR;
 

	
 
	/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
 
	std::auto_ptr<Station> st_auto_delete = std::auto_ptr<Station>(st);
 

	
 
	st->town = ClosestTownFromTile(tile, (uint)-1);
 
	st->sign.width_1 = 0;
 

	
 
	if (!GenerateStationName(st, tile, 4)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		StationInitialize(st, tile);
 
		st->dock_tile = tile;
 
		st->facilities |= FACIL_DOCK;
 
		/* Buoys are marked in the Station struct by this flag. Yes, it is this
 
		 * braindead.. */
 
		st->had_vehicle_of_type |= HVOT_BUOY;
 
		st->owner = OWNER_NONE;
 
@@ -1863,12 +1821,14 @@ int32 CmdBuildBuoy(TileIndex tile, uint3
 
		MakeBuoy(tile, st->index);
 

	
 
		UpdateStationVirtCoordDirty(st);
 
		UpdateStationAcceptance(st, false);
 
		RebuildStationLists();
 
		InvalidateWindow(WC_STATION_LIST, st->owner);
 
		/* success, so don't delete the new station */
 
		st_auto_delete.release();
 
	}
 

	
 
	return _price.build_dock;
 
}
 

	
 
/* Checks if any ship is servicing the buoy specified. Returns yes or no */
 
@@ -1980,35 +1940,40 @@ int32 CmdBuildDock(TileIndex tile, uint3
 
	/* Find a station close to us */
 
	if (st == NULL) {
 
		st = GetClosestStationFromTile(tile, 8, _current_player);
 
		if (st!=NULL && st->facilities) st = NULL;
 
	}
 

	
 
	/* In case of new station if DC_EXEC is NOT set we still need to create the station
 
	* to test if everything is OK. In this case we need to delete it before return. */
 
	std::auto_ptr<Station> st_auto_delete;
 

	
 
	if (st != NULL) {
 
		if (st->owner != OWNER_NONE && st->owner != _current_player)
 
			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
 

	
 
		if (!StationRect_BeforeAddRect(st, tile, _dock_w_chk[direction], _dock_h_chk[direction], RECT_MODE_TEST)) return CMD_ERROR;
 

	
 
		if (st->dock_tile != 0) return_cmd_error(STR_304C_TOO_CLOSE_TO_ANOTHER_DOCK);
 
	} else {
 
		Town *t;
 

	
 
		st = AllocateStation();
 
		/* allocate and initialize new station */
 
		st = new Station(tile);
 
		if (st == NULL) return CMD_ERROR;
 

	
 
		st->town = t = ClosestTownFromTile(tile, (uint)-1);
 

	
 
		if (IsValidPlayer(_current_player) && (flags & DC_EXEC))
 
		/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
 
		st_auto_delete = std::auto_ptr<Station>(st);
 

	
 
		Town *t = st->town = ClosestTownFromTile(tile, (uint)-1);
 

	
 
		if (IsValidPlayer(_current_player) && (flags & DC_EXEC) != 0) {
 
			SETBIT(t->have_ratings, _current_player);
 
		}
 

	
 
		st->sign.width_1 = 0;
 

	
 
		if (!GenerateStationName(st, tile, 3)) return CMD_ERROR;
 

	
 
		if (flags & DC_EXEC) StationInitialize(st, tile);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		st->dock_tile = tile;
 
		if (!st->facilities) st->xy = tile;
 
		st->facilities |= FACIL_DOCK;
 
@@ -2021,12 +1986,14 @@ int32 CmdBuildDock(TileIndex tile, uint3
 
		MakeDock(tile, st->owner, st->index, direction);
 

	
 
		UpdateStationVirtCoordDirty(st);
 
		UpdateStationAcceptance(st, false);
 
		RebuildStationLists();
 
		InvalidateWindow(WC_STATION_LIST, st->owner);
 
		/* success, so don't delete the new station */
 
		st_auto_delete.release();
 
	}
 
	return _price.build_dock;
 
}
 

	
 
static int32 RemoveDock(Station *st, uint32 flags)
 
{
 
@@ -2399,56 +2366,28 @@ void DestroyRoadStop(RoadStop* rs)
 
	assert(rs->num_vehicles == 0);
 

	
 
	if (rs->prev != NULL) rs->prev->next = rs->next;
 
	if (rs->next != NULL) rs->next->prev = rs->prev;
 
}
 

	
 
/**
 
 * 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.
 
 * @param st Station to be deleted
 
 */
 
void DestroyStation(Station *st)
 
{
 
	StationID index;
 

	
 
	index = st->index;
 

	
 
	DeleteName(st->string_id);
 
	MarkStationDirty(st);
 
	RebuildStationLists();
 
	InvalidateWindowClasses(WC_STATION_LIST);
 

	
 
	DeleteWindowById(WC_STATION_VIEW, index);
 

	
 
	/* Now delete all orders that go to the station */
 
	RemoveOrderFromAllVehicles(OT_GOTO_STATION, index);
 

	
 
	//Subsidies need removal as well
 
	DeleteSubsidyWithStation(index);
 

	
 
	free(st->speclist);
 
}
 

	
 
void DeleteAllPlayerStations(void)
 
{
 
	Station *st;
 

	
 
	FOR_ALL_STATIONS(st) {
 
		if (IsValidPlayer(st->owner)) DeleteStation(st);
 
		if (IsValidPlayer(st->owner)) delete st;
 
	}
 
}
 

	
 
/* this function is called for one station each tick */
 
static void StationHandleBigTick(Station *st)
 
{
 
	UpdateStationAcceptance(st, true);
 

	
 
	if (st->facilities == 0 && ++st->delete_ctr >= 8) DeleteStation(st);
 
	if (st->facilities == 0 && ++st->delete_ctr >= 8) delete st;
 

	
 
}
 

	
 
static inline void byte_inc_sat(byte *p) { byte b = *p + 1; if (b != 0) *p = b; }
 

	
 
static void UpdateStationRating(Station *st)
 
@@ -2606,13 +2545,13 @@ static void UpdateStationWaiting(Station
 
		min(0xFFF, GB(st->goods[type].waiting_acceptance, 0, 12) + amount)
 
	);
 

	
 
	st->goods[type].enroute_time = 0;
 
	st->goods[type].enroute_from = st->index;
 
	InvalidateWindow(WC_STATION_VIEW, st->index);
 
	MarkStationTilesDirty(st);
 
	st->MarkTilesDirty();
 
}
 

	
 
/** Rename a station
 
 * @param tile unused
 
 * @param p1 station ID that is to be renamed
 
 * @param p2 unused
 
@@ -2783,13 +2722,13 @@ uint MoveGoodsToStation(TileIndex tile, 
 
	return moved;
 
}
 

	
 
void BuildOilRig(TileIndex tile)
 
{
 
	uint j;
 
	Station *st = AllocateStation();
 
	Station *st = new Station();
 

	
 
	if (st == NULL) {
 
		DEBUG(misc, 0, "Can't allocate station for oilrig at 0x%X, reverting to oilrig only", tile);
 
		return;
 
	}
 
	if (!GenerateStationName(st, tile, 2)) {
 
@@ -2840,13 +2779,13 @@ void DeleteOilRig(TileIndex tile)
 

	
 
	st->dock_tile = 0;
 
	st->airport_tile = 0;
 
	st->facilities &= ~(FACIL_AIRPORT | FACIL_DOCK);
 
	st->airport_flags = 0;
 
	UpdateStationVirtCoordDirty(st);
 
	DeleteStation(st);
 
	if (st->facilities == 0) delete st;
 
}
 

	
 
static void ChangeTileOwner_Station(TileIndex tile, PlayerID old_player, PlayerID new_player)
 
{
 
	if (!IsTileOwner(tile, old_player)) return;
 

	
 
@@ -3081,18 +3020,14 @@ static void Save_STNS(void)
 
}
 

	
 
static void Load_STNS(void)
 
{
 
	int index;
 
	while ((index = SlIterateArray()) != -1) {
 
		Station *st;
 

	
 
		if (!AddBlockIfNeeded(&_Station_pool, index))
 
			error("Stations: failed loading savegame: too many stations");
 

	
 
		st = GetStation(index);
 
		Station *st = new (index) Station();
 

	
 
		SaveLoad_STNS(st);
 

	
 
		// this means it's an oldstyle savegame without support for nonuniform stations
 
		if (st->train_tile != 0 && st->trainst_h == 0) {
 
			uint w = GB(st->trainst_w, 4, 4);
 
			uint h = GB(st->trainst_w, 0, 4);
 
@@ -3164,13 +3099,13 @@ extern const ChunkHandler _station_chunk
 

	
 
static inline bool PtInRectXY(Rect *r, int x, int y)
 
{
 
	return (r->left <= x && x <= r->right && r->top <= y && y <= r->bottom);
 
}
 

	
 
static void StationRect_Init(Station *st)
 
void StationRect_Init(Station *st)
 
{
 
	Rect *r = &st->rect;
 
	r->left = r->top = r->right = r->bottom = 0;
 
}
 

	
 
static bool StationRect_IsEmpty(Station *st)
0 comments (0 inline, 0 general)