(svn r10758) -Codechange: make the depot struct use the pool item class as super class.
authorrubidium
Thu, 02 Aug 2007 22:33:53 +0000
changeset 7885 4eb9b01e37ff
parent 7884 79fa68c2a6ae
child 7886 b02aa3532d1d
(svn r10758) -Codechange: make the depot struct use the pool item class as super class.
src/depot.cpp
src/depot.h
src/oldloader.cpp
src/rail_cmd.cpp
src/road_cmd.cpp
src/tunnelbridge_cmd.cpp
src/water_cmd.cpp
--- a/src/depot.cpp	Thu Aug 02 22:32:47 2007 +0000
+++ b/src/depot.cpp	Thu Aug 02 22:33:53 2007 +0000
@@ -14,21 +14,7 @@
 #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
@@ -47,50 +33,22 @@
 }
 
 /**
- * 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();
 }
 
 
@@ -116,12 +74,7 @@
 	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);
 	}
 }
--- a/src/depot.h	Thu Aug 02 22:32:47 2007 +0000
+++ b/src/depot.h	Thu Aug 02 22:33:53 2007 +0000
@@ -13,38 +13,27 @@
 #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)
-{
-	return index < GetDepotPoolSize() && IsValidDepot(GetDepot(index));
-}
+	Depot(TileIndex xy = 0) : xy(xy) {}
+	~Depot();
 
-void DestroyDepot(Depot *depot);
+	bool IsValid() const { return this->xy != 0; }
+};
 
-static inline void DeleteDepot(Depot *depot)
+static inline bool IsValidDepotID(DepotID index)
 {
-	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
@@ -108,7 +97,6 @@
 
 Depot *GetDepotByTile(TileIndex tile);
 void InitializeDepots();
-Depot *AllocateDepot();
 
 void DeleteDepotHighlightOfVehicle(const Vehicle *v);
 
--- a/src/oldloader.cpp	Thu Aug 02 22:32:47 2007 +0000
+++ b/src/oldloader.cpp	Thu Aug 02 22:33:53 2007 +0000
@@ -520,7 +520,7 @@
 
 	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);
 	}
 
--- a/src/rail_cmd.cpp	Thu Aug 02 22:32:47 2007 +0000
+++ b/src/rail_cmd.cpp	Thu Aug 02 22:33:53 2007 +0000
@@ -37,6 +37,7 @@
 #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,
@@ -574,7 +575,6 @@
  */
 CommandCost CmdBuildTrainDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
-	Depot *d;
 	CommandCost cost;
 	Slope tileh;
 
@@ -609,18 +609,20 @@
 
 	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);
@@ -1110,7 +1112,8 @@
 	if (flags & DC_EXEC) {
 		DiagDirection dir = GetRailDepotDirection(tile);
 
-		DeleteDepot(GetDepotByTile(tile));
+		DoClearSquare(tile);
+		delete GetDepotByTile(tile);
 		UpdateSignalsOnSegment(tile, dir);
 		YapfNotifyTrackLayoutChange(tile, TrackdirToTrack(DiagdirToDiagTrackdir(dir)));
 	}
--- a/src/road_cmd.cpp	Thu Aug 02 22:32:47 2007 +0000
+++ b/src/road_cmd.cpp	Thu Aug 02 22:33:53 2007 +0000
@@ -31,6 +31,7 @@
 #include "newgrf.h"
 #include "station_map.h"
 #include "tunnel_map.h"
+#include "misc/autoptr.hpp"
 
 
 static uint CountRoadBits(RoadBits r)
@@ -721,7 +722,6 @@
 CommandCost CmdBuildRoadDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
 	CommandCost cost;
-	Depot *dep;
 	Slope tileh;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
@@ -745,15 +745,16 @@
 
 	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);
 }
@@ -765,7 +766,10 @@
 
 	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);
 }
--- a/src/tunnelbridge_cmd.cpp	Thu Aug 02 22:32:47 2007 +0000
+++ b/src/tunnelbridge_cmd.cpp	Thu Aug 02 22:33:53 2007 +0000
@@ -887,7 +887,7 @@
 	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 */
--- a/src/water_cmd.cpp	Thu Aug 02 22:32:47 2007 +0000
+++ b/src/water_cmd.cpp	Thu Aug 02 22:33:53 2007 +0000
@@ -28,6 +28,7 @@
 #include "water_map.h"
 #include "newgrf.h"
 #include "newgrf_canal.h"
+#include "misc/autoptr.hpp"
 
 static const SpriteID _water_shore_sprites[] = {
 	0,
@@ -62,7 +63,6 @@
 	TileIndex tile2;
 
 	CommandCost cost, ret;
-	Depot *depot;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
@@ -83,17 +83,18 @@
 	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);
@@ -113,7 +114,7 @@
 
 	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);