/** * Clean up a station by clearing vehicle orders, invalidating windows and * removing link stats. * Aircraft-Hangar orders need special treatment here, as the hangars are * actually part of a station (tiletype is STATION), but the order type * is OT_GOTO_DEPOT. */ Station::~Station() { if (CleaningPool()) { for (CargoID c = 0; c < NUM_CARGO; c++) { this->goods[c].cargo.OnCleanPool(); } return; } while (!this->loading_vehicles.empty()) { this->loading_vehicles.front()->LeaveStation(); } Aircraft *a; FOR_ALL_AIRCRAFT(a) { if (!a->IsNormalAircraft()) continue; if (a->targetairport == this->index) a->targetairport = INVALID_STATION; } for (CargoID c = 0; c < NUM_CARGO; ++c) { LinkGraph *lg = LinkGraph::GetIfValid(this->goods[c].link_graph); if (lg == NULL) continue; for (NodeID node = 0; node < lg->Size(); ++node) { Station *st = Station::Get((*lg)[node].Station()); st->goods[c].flows.erase(this->index); if ((*lg)[node][this->goods[c].node].LastUpdate() != INVALID_DATE) { st->goods[c].flows.DeleteFlows(this->index); RerouteCargo(st, c, this->index, st->index); } } lg->RemoveNode(this->goods[c].node); if (lg->Size() == 0) { LinkGraphSchedule::instance.Unqueue(lg); delete lg; } } Vehicle *v; FOR_ALL_VEHICLES(v) { /* Forget about this station if this station is removed */ if (v->last_station_visited == this->index) { v->last_station_visited = INVALID_STATION; } if (v->last_loading_station == this->index) { v->last_loading_station = INVALID_STATION; } } /* Clear the persistent storage. */ delete this->airport.psa; if (this->owner == OWNER_NONE) { /* Invalidate all in case of oil rigs. */ InvalidateWindowClassesData(WC_STATION_LIST, 0); } else { InvalidateWindowData(WC_STATION_LIST, this->owner, 0); } if (Overlays::Instance()->HasStation(Station::Get(this->index))) { Overlays::Instance()->ToggleStation(Station::Get(this->index)); }; DeleteWindowById(WC_STATION_VIEW, index); /* Now delete all orders that go to the station */ RemoveOrderFromAllVehicles(OT_GOTO_STATION, this->index); /* Remove all news items */ DeleteStationNews(this->index); for (CargoID c = 0; c < NUM_CARGO; c++) { this->goods[c].cargo.Truncate(); } CargoPacket::InvalidateAllFrom(this->index); }
/** * Join the link graph job and destroy it. */ LinkGraphJob::~LinkGraphJob() { this->JoinThread(); /* Don't update stuff from other pools, when everything is being removed. * Accessing other pools may be invalid. */ if (CleaningPool()) return; /* Link graph has been merged into another one. */ if (!LinkGraph::IsValidID(this->link_graph.index)) return; uint size = this->Size(); for (NodeID node_id = 0; node_id < size; ++node_id) { Node from = (*this)[node_id]; /* The station can have been deleted. Remove all flows originating from it then. */ Station *st = Station::GetIfValid(from.Station()); if (st == NULL) { this->EraseFlows(node_id); continue; } /* Link graph merging and station deletion may change around IDs. Make * sure that everything is still consistent or ignore it otherwise. */ GoodsEntry &ge = st->goods[this->Cargo()]; if (ge.link_graph != this->link_graph.index || ge.node != node_id) { this->EraseFlows(node_id); continue; } LinkGraph *lg = LinkGraph::Get(ge.link_graph); FlowStatMap &flows = from.Flows(); for (EdgeIterator it(from.Begin()); it != from.End(); ++it) { if (from[it->first].Flow() == 0) continue; StationID to = (*this)[it->first].Station(); Station *st2 = Station::GetIfValid(to); if (st2 == NULL || st2->goods[this->Cargo()].link_graph != this->link_graph.index || st2->goods[this->Cargo()].node != it->first || (*lg)[node_id][it->first].LastUpdate() == INVALID_DATE) { /* Edge has been removed. Delete flows. */ StationIDStack erased = flows.DeleteFlows(to); /* Delete old flows for source stations which have been deleted * from the new flows. This avoids flow cycles between old and * new flows. */ while (!erased.IsEmpty()) ge.flows.erase(erased.Pop()); } else if ((*lg)[node_id][it->first].LastUnrestrictedUpdate() == INVALID_DATE) { /* Edge is fully restricted. */ flows.RestrictFlows(to); } } /* Swap shares and invalidate ones that are completely deleted. Don't * really delete them as we could then end up with unroutable cargo * somewhere. Do delete them and also reroute relevant cargo if * automatic distribution has been turned off for that cargo. */ for (FlowStatMap::iterator it(ge.flows.begin()); it != ge.flows.end();) { FlowStatMap::iterator new_it = flows.find(it->first); if (new_it == flows.end()) { if (_settings_game.linkgraph.GetDistributionType(this->Cargo()) != DT_MANUAL) { it->second.Invalidate(); ++it; } else { FlowStat shares(INVALID_STATION, 1); it->second.SwapShares(shares); ge.flows.erase(it++); for (FlowStat::SharesMap::const_iterator shares_it(shares.GetShares()->begin()); shares_it != shares.GetShares()->end(); ++shares_it) { RerouteCargo(st, this->Cargo(), shares_it->second, st->index); } } } else { it->second.SwapShares(new_it->second); flows.erase(new_it); ++it; } } ge.flows.insert(flows.begin(), flows.end()); InvalidateWindowData(WC_STATION_VIEW, st->index, this->Cargo()); } }