Changeset - r7389:43e81eaed707
[Not reviewed]
master
0 7 0
rubidium - 17 years ago 2007-08-02 22:33:53
rubidium@openttd.org
(svn r10758) -Codechange: make the depot struct use the pool item class as super class.
7 files changed with 43 insertions and 94 deletions:
0 comments (0 inline, 0 general)
src/depot.cpp
Show inline comments
 
@@ -11,27 +11,13 @@
 
#include "tile.h"
 
#include "map.h"
 
#include "table/strings.h"
 
#include "saveload.h"
 
#include "order.h"
 

	
 

	
 
/**
 
 * Called if a new block is added to the depot-pool
 
 */
 
static void DepotPoolNewBlock(uint start_item)
 
{
 
	Depot *d;
 

	
 
	/* 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 (d = GetDepot(start_item); d != NULL; d = (d->index + 1U < GetDepotPoolSize()) ? GetDepot(d->index + 1U) : NULL) d->index = start_item++;
 
}
 

	
 
DEFINE_OLD_POOL(Depot, Depot, DepotPoolNewBlock, NULL)
 

	
 
DEFINE_OLD_POOL_GENERIC(Depot, Depot)
 

	
 
/**
 
 * Gets a depot from a tile
 
 *
 
 * @return Returns the depot if the tile had a depot, else it returns NULL
 
 */
 
@@ -44,56 +30,28 @@ Depot *GetDepotByTile(TileIndex tile)
 
	}
 

	
 
	return NULL;
 
}
 

	
 
/**
 
 * Allocate a new depot
 
 */
 
Depot *AllocateDepot()
 
{
 
	Depot *d;
 

	
 
	/* 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 (d = GetDepot(0); d != NULL; d = (d->index + 1U < GetDepotPoolSize()) ? GetDepot(d->index + 1U) : NULL) {
 
		if (!IsValidDepot(d)) {
 
			DepotID index = d->index;
 

	
 
			memset(d, 0, sizeof(Depot));
 
			d->index = index;
 

	
 
			return d;
 
		}
 
	}
 

	
 
	/* Check if we can add a block to the pool */
 
	if (AddBlockToPool(&_Depot_pool)) return AllocateDepot();
 

	
 
	return NULL;
 
}
 

	
 
/**
 
 * Clean up a depot
 
 */
 
void DestroyDepot(Depot *depot)
 
Depot::~Depot()
 
{
 
	/* Clear the tile */
 
	DoClearSquare(depot->xy);
 

	
 
	/* Clear the depot from all order-lists */
 
	RemoveOrderFromAllVehicles(OT_GOTO_DEPOT, depot->index);
 
	RemoveOrderFromAllVehicles(OT_GOTO_DEPOT, this->index);
 

	
 
	/* Delete the depot-window */
 
	DeleteWindowById(WC_VEHICLE_DEPOT, depot->xy);
 
	DeleteWindowById(WC_VEHICLE_DEPOT, this->xy);
 
	this->xy = 0;
 
}
 

	
 
void InitializeDepots()
 
{
 
	CleanPool(&_Depot_pool);
 
	AddBlockToPool(&_Depot_pool);
 
	_Depot_pool.CleanPool();
 
	_Depot_pool.AddBlockToPool();
 
}
 

	
 

	
 
static const SaveLoad _depot_desc[] = {
 
	SLE_CONDVAR(Depot, xy,         SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
 
	SLE_CONDVAR(Depot, xy,         SLE_UINT32,                 6, SL_MAX_VERSION),
 
@@ -113,18 +71,13 @@ static void Save_DEPT()
 

	
 
static void Load_DEPT()
 
{
 
	int index;
 

	
 
	while ((index = SlIterateArray()) != -1) {
 
		Depot *depot;
 

	
 
		if (!AddBlockIfNeeded(&_Depot_pool, index))
 
			error("Depots: failed loading savegame: too many depots");
 

	
 
		depot = GetDepot(index);
 
		Depot *depot = new (index) Depot();
 
		SlObject(depot, _depot_desc);
 
	}
 
}
 

	
 
extern const ChunkHandler _depot_chunk_handlers[] = {
 
	{ 'DEPT', Save_DEPT, Load_DEPT, CH_ARRAY | CH_LAST},
src/depot.h
Show inline comments
 
@@ -10,44 +10,33 @@
 
#include "tile.h"
 
#include "variables.h"
 
#include "road_map.h"
 
#include "rail_map.h"
 
#include "water_map.h"
 

	
 
struct Depot {
 
	TileIndex xy;
 
	TownID town_index;
 
	DepotID index;
 
};
 

	
 
struct Depot;
 
DECLARE_OLD_POOL(Depot, Depot, 3, 8000)
 

	
 
/**
 
 * Check if a depot really exists.
 
 */
 
static inline bool IsValidDepot(const Depot *depot)
 
{
 
	return depot != NULL && depot->xy != 0;
 
}
 
struct Depot : PoolItem<Depot, DepotID, &_Depot_pool> {
 
	TileIndex xy;
 
	TownID town_index;
 

	
 
static inline bool IsValidDepotID(uint index)
 
	Depot(TileIndex xy = 0) : xy(xy) {}
 
	~Depot();
 

	
 
	bool IsValid() const { return this->xy != 0; }
 
};
 

	
 
static inline bool IsValidDepotID(DepotID index)
 
{
 
	return index < GetDepotPoolSize() && IsValidDepot(GetDepot(index));
 
}
 

	
 
void DestroyDepot(Depot *depot);
 

	
 
static inline void DeleteDepot(Depot *depot)
 
{
 
	DestroyDepot(depot);
 
	depot->xy = 0;
 
	return index < GetDepotPoolSize() && GetDepot(index)->IsValid();
 
}
 

	
 
void ShowDepotWindow(TileIndex tile, VehicleType type);
 

	
 
#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))
 
#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())
 
#define FOR_ALL_DEPOTS(d) FOR_ALL_DEPOTS_FROM(d, 0)
 

	
 
#define MIN_SERVINT_PERCENT  5
 
#define MAX_SERVINT_PERCENT 90
 
#define MIN_SERVINT_DAYS    30
 
#define MAX_SERVINT_DAYS   800
 
@@ -105,11 +94,10 @@ static inline bool CanBuildDepotByTileh(
 
{
 
	return ((0x4C >> direction) & tileh) != 0;
 
}
 

	
 
Depot *GetDepotByTile(TileIndex tile);
 
void InitializeDepots();
 
Depot *AllocateDepot();
 

	
 
void DeleteDepotHighlightOfVehicle(const Vehicle *v);
 

	
 
#endif /* DEPOT_H */
src/oldloader.cpp
Show inline comments
 
@@ -517,13 +517,13 @@ static bool LoadOldDepot(LoadgameState *
 
{
 
	if (!AddBlockIfNeeded(&_Depot_pool, num))
 
		error("Depots: failed loading savegame: too many depots");
 

	
 
	if (!LoadChunk(ls, GetDepot(num), depot_chunk)) return false;
 

	
 
	if (IsValidDepot(GetDepot(num))) {
 
	if (IsValidDepotID(num)) {
 
		GetDepot(num)->town_index = REMAP_TOWN_IDX(_old_town_index);
 
	}
 

	
 
	return true;
 
}
 

	
src/rail_cmd.cpp
Show inline comments
 
@@ -34,12 +34,13 @@
 
#include "railtypes.h" // include table for railtypes
 
#include "newgrf.h"
 
#include "yapf/yapf.h"
 
#include "newgrf_callbacks.h"
 
#include "newgrf_station.h"
 
#include "train.h"
 
#include "misc/autoptr.hpp"
 

	
 
const byte _track_sloped_sprites[14] = {
 
	14, 15, 22, 13,
 
	 0, 21, 17, 12,
 
	23,  0, 18, 20,
 
	19, 16
 
@@ -571,13 +572,12 @@ CommandCost CmdRemoveRailroadTrack(TileI
 
 *
 
 * @todo When checking for the tile slope,
 
 * distingush between "Flat land required" and "land sloped in wrong direction"
 
 */
 
CommandCost CmdBuildTrainDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Depot *d;
 
	CommandCost cost;
 
	Slope tileh;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 

	
 
	/* check railtype and valid direction for depot (0 through 3), 4 in total */
 
@@ -606,24 +606,26 @@ CommandCost CmdBuildTrainDepot(TileIndex
 

	
 
	cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (CmdFailed(cost)) return CMD_ERROR;
 

	
 
	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
 

	
 
	d = AllocateDepot();
 
	Depot *d = new Depot(tile);
 

	
 
	if (d == NULL) return CMD_ERROR;
 
	AutoPtrT<Depot> d_auto_delete = d;
 

	
 
	if (flags & DC_EXEC) {
 
		MakeRailDepot(tile, _current_player, dir, (RailType)p1);
 
		MarkTileDirtyByTile(tile);
 

	
 
		d->xy = tile;
 
		d->town_index = ClosestTownFromTile(tile, (uint)-1)->index;
 

	
 
		UpdateSignalsOnSegment(tile, dir);
 
		YapfNotifyTrackLayoutChange(tile, TrackdirToTrack(DiagdirToDiagTrackdir(dir)));
 
		d_auto_delete.Detach();
 
	}
 

	
 
	return cost.AddCost(_price.build_train_depot);
 
}
 

	
 
/** Build signals, alternate between double/single, signal/semaphore,
 
@@ -1107,13 +1109,14 @@ static CommandCost RemoveTrainDepot(Tile
 
	if (!EnsureNoVehicleOnGround(tile))
 
		return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		DiagDirection dir = GetRailDepotDirection(tile);
 

	
 
		DeleteDepot(GetDepotByTile(tile));
 
		DoClearSquare(tile);
 
		delete GetDepotByTile(tile);
 
		UpdateSignalsOnSegment(tile, dir);
 
		YapfNotifyTrackLayoutChange(tile, TrackdirToTrack(DiagdirToDiagTrackdir(dir)));
 
	}
 

	
 
	return CommandCost(_price.remove_train_depot);
 
}
src/road_cmd.cpp
Show inline comments
 
@@ -28,12 +28,13 @@
 
#include "sound.h"
 
#include "yapf/yapf.h"
 
#include "depot.h"
 
#include "newgrf.h"
 
#include "station_map.h"
 
#include "tunnel_map.h"
 
#include "misc/autoptr.hpp"
 

	
 

	
 
static uint CountRoadBits(RoadBits r)
 
{
 
	uint count = 0;
 

	
 
@@ -718,13 +719,12 @@ CommandCost CmdRemoveLongRoad(TileIndex 
 
 * @todo When checking for the tile slope,
 
 * distingush between "Flat land required" and "land sloped in wrong direction"
 
 */
 
CommandCost CmdBuildRoadDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	CommandCost cost;
 
	Depot *dep;
 
	Slope tileh;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 

	
 
	DiagDirection dir = Extract<DiagDirection, 0>(p1);
 
	RoadType rt = (RoadType)GB(p1, 2, 2);
 
@@ -742,33 +742,37 @@ CommandCost CmdBuildRoadDepot(TileIndex 
 

	
 
	cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (CmdFailed(cost)) return CMD_ERROR;
 

	
 
	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
 

	
 
	dep = AllocateDepot();
 
	Depot *dep = new Depot(tile);
 
	if (dep == NULL) return CMD_ERROR;
 
	AutoPtrT<Depot> d_auto_delete = dep;
 

	
 
	if (flags & DC_EXEC) {
 
		dep->xy = tile;
 
		dep->town_index = ClosestTownFromTile(tile, (uint)-1)->index;
 

	
 
		MakeRoadDepot(tile, _current_player, dir, rt);
 
		MarkTileDirtyByTile(tile);
 
		d_auto_delete.Detach();
 
	}
 
	return cost.AddCost(_price.build_road_depot);
 
}
 

	
 
static CommandCost RemoveRoadDepot(TileIndex tile, uint32 flags)
 
{
 
	if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER)
 
		return CMD_ERROR;
 

	
 
	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) DeleteDepot(GetDepotByTile(tile));
 
	if (flags & DC_EXEC) {
 
		DoClearSquare(tile);
 
		delete GetDepotByTile(tile);
 
	}
 

	
 
	return CommandCost(_price.remove_road_depot);
 
}
 

	
 
static CommandCost ClearTile_Road(TileIndex tile, byte flags)
 
{
src/tunnelbridge_cmd.cpp
Show inline comments
 
@@ -884,13 +884,13 @@ static void DrawBridgeTramBits(int x, in
 
	static const SpriteID back_offsets[6]    =   {  95,  96,  99, 102, 100, 101 };
 
	static const SpriteID front_offsets[6]   =   {  97,  98, 103, 106, 104, 105 };
 

	
 
	static const uint size_x[6] = { 11, 16, 16, 16, 16, 16 };
 
	static const uint size_y[6] = { 16, 11, 16, 16, 16, 16 };
 

	
 
	AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + tram_offsets[overlay][offset], PAL_NONE, x, y, size_x[offset], size_y[offset], offset >= 2 ? 1 : 0, z);
 
	AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + tram_offsets[overlay][offset], PAL_NONE, x, y, size_x[offset], size_y[offset], offset >= 2 ? 1 : 0, z, HASBIT(_transparent_opt, TO_BRIDGES));
 

	
 
	AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + back_offsets[offset],  PAL_NONE, x, y, size_x[offset], size_y[offset], 0, z, HASBIT(_transparent_opt, TO_BUILDINGS));
 
	/* For sloped sprites the bounding box needs to be higher, as the pylons stop on a higher point */
 
	AddSortableSpriteToDraw(SPR_TRAMWAY_BASE + front_offsets[offset], PAL_NONE, x, y, size_x[offset], size_y[offset], offset >= 2 ? 0x30 : 0x10, z, HASBIT(_transparent_opt, TO_BUILDINGS));
 
}
 

	
src/water_cmd.cpp
Show inline comments
 
@@ -25,12 +25,13 @@
 
#include "vehicle_gui.h"
 
#include "train.h"
 
#include "roadveh.h"
 
#include "water_map.h"
 
#include "newgrf.h"
 
#include "newgrf_canal.h"
 
#include "misc/autoptr.hpp"
 

	
 
static const SpriteID _water_shore_sprites[] = {
 
	0,
 
	SPR_SHORE_TILEH_1,
 
	SPR_SHORE_TILEH_2,
 
	SPR_SHORE_TILEH_3,
 
@@ -59,13 +60,12 @@ static void FloodVehicle(Vehicle *v);
 
 */
 
CommandCost CmdBuildShipDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	TileIndex tile2;
 

	
 
	CommandCost cost, ret;
 
	Depot *depot;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 

	
 
	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
 

	
 
	Axis axis = Extract<Axis, 0>(p1);
 
@@ -80,23 +80,24 @@ CommandCost CmdBuildShipDepot(TileIndex 
 

	
 
	ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (CmdFailed(ret)) return CMD_ERROR;
 
	ret = DoCommand(tile2, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (CmdFailed(ret)) return CMD_ERROR;
 

	
 
	depot = AllocateDepot();
 
	Depot *depot = new Depot(tile);
 
	if (depot == NULL) return CMD_ERROR;
 
	AutoPtrT<Depot> d_auto_delete = depot;
 

	
 
	if (flags & DC_EXEC) {
 
		depot->xy = tile;
 
		depot->town_index = ClosestTownFromTile(tile, (uint)-1)->index;
 

	
 
		MakeShipDepot(tile,  _current_player, DEPOT_NORTH, axis);
 
		MakeShipDepot(tile2, _current_player, DEPOT_SOUTH, axis);
 
		MarkTileDirtyByTile(tile);
 
		MarkTileDirtyByTile(tile2);
 
		d_auto_delete.Detach();
 
	}
 

	
 
	return cost.AddCost(_price.build_ship_depot);
 
}
 

	
 
static CommandCost RemoveShipDepot(TileIndex tile, uint32 flags)
 
@@ -110,13 +111,13 @@ static CommandCost RemoveShipDepot(TileI
 
	tile2 = GetOtherShipDepotTile(tile);
 

	
 
	if (!EnsureNoVehicle(tile2)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		/* Kill the depot, which is registered at the northernmost tile. Use that one */
 
		DeleteDepot(GetDepotByTile(tile2 < tile ? tile2 : tile));
 
		delete GetDepotByTile(tile2 < tile ? tile2 : tile);
 

	
 
		MakeWater(tile);
 
		MakeWater(tile2);
 
		MarkTileDirtyByTile(tile);
 
		MarkTileDirtyByTile(tile2);
 
	}
0 comments (0 inline, 0 general)