diff --git a/src/depot.cpp b/src/depot.cpp --- 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 @@ -47,50 +33,22 @@ Depot *GetDepotByTile(TileIndex tile) } /** - * 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 --- 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) + 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 @@ -108,7 +97,6 @@ static inline bool CanBuildDepotByTileh( Depot *GetDepotByTile(TileIndex tile); void InitializeDepots(); -Depot *AllocateDepot(); void DeleteDepotHighlightOfVehicle(const Vehicle *v); diff --git a/src/oldloader.cpp b/src/oldloader.cpp --- a/src/oldloader.cpp +++ b/src/oldloader.cpp @@ -520,7 +520,7 @@ static bool LoadOldDepot(LoadgameState * 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 --- 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(TileI */ CommandCost CmdBuildTrainDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { - Depot *d; CommandCost cost; Slope tileh; @@ -609,18 +609,20 @@ CommandCost CmdBuildTrainDepot(TileIndex 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(Tile 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 --- 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 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 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(TileI 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 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -887,7 +887,7 @@ static void DrawBridgeTramBits(int x, in 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 --- 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 TileIndex tile2; CommandCost cost, ret; - Depot *depot; SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); @@ -83,17 +83,18 @@ CommandCost CmdBuildShipDepot(TileIndex 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(TileI 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);