Skip to content

Commit

Permalink
Change: Keep removed depots in the pool for a while. (based on patch …
Browse files Browse the repository at this point in the history
  • Loading branch information
J0anJosep committed Feb 19, 2022
1 parent b18a59b commit f5f9303
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 14 deletions.
18 changes: 12 additions & 6 deletions src/build_vehicle_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,9 @@ struct BuildVehicleWindow : Window {
break;

case VEH_SHIP:
this->filter.railtypes = this->listview_mode ? INVALID_RAILTYPES : depot->r_types.rail_types;
break;

case VEH_AIRCRAFT:
break;
}
Expand Down Expand Up @@ -1340,14 +1343,17 @@ struct BuildVehicleWindow : Window {
EngineID sel_id = INVALID_ENGINE;
this->eng_list.clear();

for (const Engine *e : Engine::IterateType(VEH_SHIP)) {
if (!this->show_hidden_engines && e->IsHidden(_local_company)) continue;
EngineID eid = e->index;
if (!IsEngineBuildable(eid, VEH_SHIP, _local_company)) continue;
this->eng_list.push_back(eid);
if (this->listview_mode || this->filter.railtypes != RAILTYPES_NONE) {
for (const Engine *e : Engine::IterateType(VEH_SHIP)) {
if (!this->show_hidden_engines && e->IsHidden(_local_company)) continue;
EngineID eid = e->index;
if (!IsEngineBuildable(eid, VEH_SHIP, _local_company)) continue;
this->eng_list.push_back(eid);

if (eid == this->sel_engine) sel_id = eid;
if (eid == this->sel_engine) sel_id = eid;
}
}

this->SelectEngine(sel_id);
}

Expand Down
1 change: 1 addition & 0 deletions src/date_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static const int MONTHS_IN_YEAR = 12; ///< months per year
static const int STATION_RATING_TICKS = 185; ///< cycle duration for updating station rating
static const int STATION_ACCEPTANCE_TICKS = 250; ///< cycle duration for updating station acceptance
static const int STATION_LINKGRAPH_TICKS = 504; ///< cycle duration for cleaning dead links
static const int DEPOT_REMOVAL_TICKS = 250; ///< cycle duration for cleaning demolished depots
static const int CARGO_AGING_TICKS = 185; ///< cycle duration for aging cargo
static const int INDUSTRY_PRODUCE_TICKS = 256; ///< cycle duration for industry production
static const int TOWN_GROWTH_TICKS = 70; ///< cycle duration for towns trying to grow. (this originates from the size of the town array in TTD
Expand Down
20 changes: 19 additions & 1 deletion src/depot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ Depot::~Depot()
CloseWindowById(GetWindowClassForVehicleType(this->veh_type),
VehicleListIdentifier(VL_DEPOT_LIST,
this->veh_type, this->company, this->index).Pack());

InvalidateWindowData(WC_SELECT_DEPOT, this->veh_type);
}

/**
* Schedule deletion of this depot.
*
* This method is ought to be called after demolishing last depot part.
* The depot will be kept in the pool for a while so it can be
* placed again later without messing vehicle orders.
*
* @see Depot::IsInUse
*/
void Depot::Disuse()
{
/* Mark that the depot is demolished and start the countdown. */
this->delete_ctr = 8;
}

/**
Expand Down Expand Up @@ -132,7 +149,8 @@ void Depot::AfterAddRemove(TileArea ta, bool adding)
this->xy = this->depot_tiles[0];
assert(IsDepotTile(this->xy));
} else {
delete this;
assert(this->IsInUse());
this->Disuse();
}

InvalidateWindowData(WC_SELECT_DEPOT, veh_type);
Expand Down
22 changes: 19 additions & 3 deletions src/depot_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ struct Depot : DepotPool::PoolItem<&_depot_pool> {
std::string name;

TileIndex xy;
uint16 town_cn; ///< The N-1th depot for this town (consecutive number)
Date build_date; ///< Date of construction
uint16 town_cn; ///< The N-1th depot for this town (consecutive number)
Date build_date; ///< Date of construction

CompanyID company;
VehicleType veh_type;
byte delete_ctr; ///< Delete counter. If greater than 0 then it is decremented until it reaches 0; the depot is then deleted.

union {
RoadTypes road_types;
Expand Down Expand Up @@ -64,9 +65,24 @@ struct Depot : DepotPool::PoolItem<&_depot_pool> {
*/
inline bool IsOfType(const Depot *d) const
{
return GetTileType(d->xy) == GetTileType(this->xy);
return d->veh_type == this->veh_type;
}

/**
* Check whether the depot currently is in use; in use means
* that it is not scheduled for deletion and that it still has
* a building on the map. Otherwise the building is demolished
* and the depot awaits to be deleted.
* @return true iff still in use
* @see Depot::Disuse
*/
inline bool IsInUse() const
{
return this->delete_ctr == 0;
}

void Disuse();

/* Check we can add some tiles to this depot. */
CommandCost BeforeAddTiles(TileArea ta);

Expand Down
14 changes: 14 additions & 0 deletions src/depot_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ CommandCost CmdRenameDepot(DoCommandFlag flags, DepotID depot_id, const std::str
return CommandCost();
}

void OnTick_Depot()
{
if (_game_mode == GM_EDITOR) return;

/* Clean up demolished depots. */
for (Depot *d : Depot::Iterate()) {
if (d->IsInUse()) continue;
if ((_tick_counter + d->index) % DEPOT_REMOVAL_TICKS != 0) continue;
if (--d->delete_ctr != 0) continue;
delete d;
}
}


/**
* Look for or check depot to join to, building a new one if necessary.
* @param ta The area of the new depot.
Expand Down
1 change: 1 addition & 0 deletions src/depot_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,7 @@ static void DepotSellAllConfirmationCallback(Window *win, bool confirmed)
if (confirmed) {
assert(Depot::IsValidID(win->window_number));
Depot *d = Depot::Get(win->window_number);
if (!d->IsInUse()) return;
Command<CMD_DEPOT_SELL_ALL_VEHICLES>::Post(d->xy, d->veh_type);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/landscape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,7 @@ void GenerateLandscape(byte mode)
void OnTick_Town();
void OnTick_Trees();
void OnTick_Station();
void OnTick_Depot();
void OnTick_Industry();

void OnTick_Companies();
Expand All @@ -1516,6 +1517,7 @@ void CallLandscapeTick()
OnTick_Town();
OnTick_Trees();
OnTick_Station();
OnTick_Depot();
OnTick_Industry();
}

Expand Down
2 changes: 1 addition & 1 deletion src/order_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ CommandCost CmdInsertOrder(DoCommandFlag flags, VehicleID veh, VehicleOrderID se
if ((new_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0) {
const Depot *dp = Depot::GetIfValid(new_order.GetDestination());

if (dp == nullptr) return CMD_ERROR;
if (dp == nullptr || !dp->IsInUse()) return CMD_ERROR;

CommandCost ret = CheckOwnership(dp->company);
if (ret.Failed()) return ret;
Expand Down
1 change: 1 addition & 0 deletions src/saveload/afterload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2807,6 +2807,7 @@ bool AfterLoadGame()
/* It can happen there is no depot here anymore (TTO/TTD savegames) */
depot->veh_type = VEH_INVALID;
depot->company = INVALID_COMPANY;
depot->Disuse();
delete depot;
continue;
}
Expand Down
1 change: 1 addition & 0 deletions src/saveload/depot_sl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static const SaveLoad _depot_desc[] = {
SLE_CONDVAR(Depot, ta.tile, SLE_UINT32, SLV_MULTITILE_DEPOTS, SL_MAX_VERSION),
SLE_CONDVAR(Depot, ta.w, SLE_FILE_U8 | SLE_VAR_U16, SLV_MULTITILE_DEPOTS, SL_MAX_VERSION),
SLE_CONDVAR(Depot, ta.h, SLE_FILE_U8 | SLE_VAR_U16, SLV_MULTITILE_DEPOTS, SL_MAX_VERSION),
SLE_CONDVAR(Depot, delete_ctr, SLE_UINT8, SLV_MULTITILE_DEPOTS, SL_MAX_VERSION),
};

struct DEPTChunkHandler : ChunkHandler {
Expand Down
4 changes: 2 additions & 2 deletions src/script/api/script_depotlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ ScriptDepotList::ScriptDepotList(ScriptTile::TransportType transport_type)
static_assert(VEH_SHIP == (int)ScriptTile::TRANSPORT_WATER);

for (const Depot *depot : Depot::Iterate()) {
if (depot->veh_type != (VehicleType)transport_type) continue;
if (::GetTileOwner(depot->xy) != ScriptObject::GetCompany() && ScriptObject::GetCompany() != OWNER_DEITY) continue;
if (!depot->IsInUse() || depot->veh_type != (VehicleType)transport_type) continue;
if (depot->company != ScriptObject::GetCompany() && ScriptObject::GetCompany() != OWNER_DEITY) continue;

/* It only returns one hangar per airport. Anyway, new aircraft is always built in this hangar and not others. */
for (auto const &tile : depot->depot_tiles) this->AddItem(tile);
Expand Down
2 changes: 1 addition & 1 deletion src/ship_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
uint best_dist = max_distance == 0 ? UINT_MAX : max_distance + 1;

for (const Depot *depot : Depot::Iterate()) {
if (depot->veh_type == VEH_SHIP && depot->company == v->owner) {
if (depot->veh_type == VEH_SHIP && depot->company == v->owner && depot->IsInUse()) {
uint dist = DistanceManhattan(depot->xy, v->tile);
if (dist < best_dist) {
best_dist = dist;
Expand Down

0 comments on commit f5f9303

Please sign in to comment.