Skip to content

Commit

Permalink
Feature: Try to reuse a removed depot when placing a new one. (based …
Browse files Browse the repository at this point in the history
…on patch by adf88, OpenTTD#6328, OpenTTD#7051)
  • Loading branch information
J0anJosep committed Dec 22, 2021
1 parent cca36f9 commit 12aaf70
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/depot.cpp
Expand Up @@ -59,6 +59,20 @@ Depot::~Depot()
InvalidateWindowData(WC_SELECT_DEPOT, this->veh_type);
}

/**
* Cancel deletion of this depot (reuse it).
* @param xy New location of the depot.
* @see Depot::IsInUse
* @see Depot::Disuse
*/
void Depot::Reuse(TileIndex xy)
{
this->delete_ctr = 0;
this->xy = xy;
this->ta.tile = xy;
this->ta.h = this->ta.w = 1;
}

/**
* Schedule deletion of this depot.
*
Expand All @@ -67,6 +81,7 @@ Depot::~Depot()
* placed again later without messing vehicle orders.
*
* @see Depot::IsInUse
* @see Depot::Reuse
*/
void Depot::Disuse()
{
Expand Down Expand Up @@ -102,7 +117,7 @@ CommandCost Depot::BeforeAddTiles(TileArea ta)
{
assert(ta.tile != INVALID_TILE);

if (this->ta.tile != INVALID_TILE) {
if (this->ta.tile != INVALID_TILE && this->IsInUse()) {
/* Important when the old rect is completely inside the new rect, resp. the old one was empty. */
ta.Add(this->ta.tile);
ta.Add(TILE_ADDXY(this->ta.tile, this->ta.w - 1, this->ta.h - 1));
Expand Down Expand Up @@ -145,8 +160,13 @@ void Depot::AfterAddRemove(TileArea ta, bool adding)
} else {
assert(this->IsInUse());
this->Disuse();
TileIndex old_tile = this->xy;
this->RescanDepotTiles();
assert(this->depot_tiles.size() == 0);
this->xy = old_tile;
}

InvalidateWindowData(WC_VEHICLE_DEPOT, this->index);
InvalidateWindowData(WC_SELECT_DEPOT, veh_type);
}

Expand Down
2 changes: 2 additions & 0 deletions src/depot_base.h
Expand Up @@ -75,12 +75,14 @@ struct Depot : DepotPool::PoolItem<&_depot_pool> {
* and the depot awaits to be deleted.
* @return true iff still in use
* @see Depot::Disuse
* @see Depot::Reuse
*/
inline bool IsInUse() const
{
return this->delete_ctr == 0;
}

void Reuse(TileIndex xy);
void Disuse();

/* Check we can add some tiles to this depot. */
Expand Down
24 changes: 24 additions & 0 deletions src/depot_cmd.cpp
Expand Up @@ -77,6 +77,23 @@ CommandCost CmdRenameDepot(DoCommandFlag flags, DepotID depot_id, const std::str
return CommandCost();
}

/**
* Find a demolished depot close to a tile.
* @param ta Tile area to search for.
* @param type Depot type.
* @param cid Previous owner of the depot.
* @return The index of a demolished nearby depot, or INVALID_DEPOT if none.
*/
DepotID FindDeletedDepotCloseTo(TileArea ta, VehicleType type, CompanyID cid)
{
for (Depot *depot : Depot::Iterate()) {
if (depot->IsInUse() || depot->veh_type != type || depot->company != cid) continue;
if (ta.Contains(depot->xy)) return depot->index;
}

return INVALID_DEPOT;
}

void OnTick_Depot()
{
if (_game_mode == GM_EDITOR) return;
Expand Down Expand Up @@ -128,6 +145,12 @@ CommandCost FindJoiningDepot(TileArea ta, VehicleType veh_type, DepotID &join_to
}
}

if (closest_depot == INVALID_DEPOT) {
/* Check for close unused depots. */
check_area.Expand(7); // total distance of 8
closest_depot = FindDeletedDepotCloseTo(check_area, veh_type, _current_company);
}

if (closest_depot != INVALID_DEPOT) {
assert(Depot::IsValidID(closest_depot));
depot = Depot::Get(closest_depot);
Expand All @@ -151,6 +174,7 @@ CommandCost FindJoiningDepot(TileArea ta, VehicleType veh_type, DepotID &join_to
depot = Depot::Get(join_to);
assert(depot->company == _current_company);
assert(depot->veh_type == veh_type);
if (!depot->IsInUse() && (flags & DC_EXEC)) depot->Reuse(ta.tile);
return depot->BeforeAddTiles(ta);
}

Expand Down
9 changes: 9 additions & 0 deletions src/depot_gui.cpp
Expand Up @@ -1214,6 +1214,15 @@ static const Depot *FindDepotsNearby(TileArea ta, VehicleType veh_type, bool dis
}
}

/* Add reusable depots. */
ta.Expand(8);
for (Depot *depot : Depot::Iterate()) {
if (depot->IsInUse()) continue;
if (depot->veh_type != veh_type || depot->company != _current_company) continue;
if (!ta.Contains(depot->xy)) continue;
_depots_nearby_list.push_back(depot->index);
}

return nullptr;
}

Expand Down

0 comments on commit 12aaf70

Please sign in to comment.