From c60988a1d5d2a140eb5f8ff1b90119ee40ec787e Mon Sep 17 00:00:00 2001 From: rubidium Date: Thu, 2 Aug 2007 22:33:53 +0000 Subject: (svn r10758) -Codechange: make the depot struct use the pool item class as super class. --- src/depot.cpp | 63 ++++++------------------------------------------ src/depot.h | 34 +++++++++----------------- src/oldloader.cpp | 2 +- src/rail_cmd.cpp | 11 ++++++--- src/road_cmd.cpp | 12 ++++++--- src/tunnelbridge_cmd.cpp | 2 +- src/water_cmd.cpp | 9 ++++--- 7 files changed, 41 insertions(+), 92 deletions(-) (limited to 'src') diff --git a/src/depot.cpp b/src/depot.cpp index 3789c0edd..63c6629e1 100644 --- a/src/depot.cpp +++ b/src/depot.cpp @@ -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 @@ -46,51 +32,23 @@ 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(); } @@ -116,12 +74,7 @@ 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); } } diff --git a/src/depot.h b/src/depot.h index 17e951358..1b3de1b86 100644 --- a/src/depot.h +++ b/src/depot.h @@ -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 { + 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 @@ static inline bool CanBuildDepotByTileh(DiagDirection direction, Slope tileh) Depot *GetDepotByTile(TileIndex tile); void InitializeDepots(); -Depot *AllocateDepot(); void DeleteDepotHighlightOfVehicle(const Vehicle *v); diff --git a/src/oldloader.cpp b/src/oldloader.cpp index bd28b4881..adf3af037 100644 --- a/src/oldloader.cpp +++ b/src/oldloader.cpp @@ -520,7 +520,7 @@ static bool LoadOldDepot(LoadgameState *ls, int num) 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); } diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp index ac22758fa..024fd620d 100644 --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -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 CmdRemoveRailroadTrack(TileIndex tile, uint32 flags, uint32 p1, uint */ CommandCost CmdBuildTrainDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { - Depot *d; CommandCost cost; Slope tileh; @@ -609,18 +609,20 @@ CommandCost CmdBuildTrainDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p 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 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 @@ static CommandCost RemoveTrainDepot(TileIndex tile, uint32 flags) if (flags & DC_EXEC) { DiagDirection dir = GetRailDepotDirection(tile); - DeleteDepot(GetDepotByTile(tile)); + DoClearSquare(tile); + delete GetDepotByTile(tile); UpdateSignalsOnSegment(tile, dir); YapfNotifyTrackLayoutChange(tile, TrackdirToTrack(DiagdirToDiagTrackdir(dir))); } diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index b6abc4b35..621338ea9 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -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 CmdRemoveLongRoad(TileIndex end_tile, uint32 flags, uint32 p1, uint3 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 @@ CommandCost CmdBuildRoadDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2 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 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 @@ static CommandCost RemoveRoadDepot(TileIndex tile, uint32 flags) 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); } diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index b2e3245f8..05fac47d8 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -887,7 +887,7 @@ static void DrawBridgeTramBits(int x, int y, byte z, int offset, bool overlay) 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 */ diff --git a/src/water_cmd.cpp b/src/water_cmd.cpp index 792017130..f942349af 100644 --- a/src/water_cmd.cpp +++ b/src/water_cmd.cpp @@ -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 @@ CommandCost CmdBuildShipDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2 TileIndex tile2; CommandCost cost, ret; - Depot *depot; SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); @@ -83,17 +83,18 @@ CommandCost CmdBuildShipDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2 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 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 @@ static CommandCost RemoveShipDepot(TileIndex tile, uint32 flags) 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); -- cgit v1.2.3-70-g09d2