mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Don't scan vehicle pool to find targeting disaster vehicle when deleting any vehicle.
When deleting a vehicle, the vehicle pool is scanned to find a targetting disaster vehicle. With lots of vehicles this can take some time, especially when deleting multiple consecutive vehicles. Disasters vehicles can actually only target road vehicles. Store the DisasterVehicle index in the road vehicle, so that no pool scan is necessary.pull/12064/head
parent
9ec9c8d8b5
commit
b1c9a0f628
|
@ -345,12 +345,13 @@ static bool DisasterTick_Ufo(DisasterVehicle *v)
|
||||||
}
|
}
|
||||||
|
|
||||||
n = RandomRange(n); // Choose one of them.
|
n = RandomRange(n); // Choose one of them.
|
||||||
for (const RoadVehicle *u : RoadVehicle::Iterate()) {
|
for (RoadVehicle *u : RoadVehicle::Iterate()) {
|
||||||
/* Find (n+1)-th road vehicle. */
|
/* Find (n+1)-th road vehicle. */
|
||||||
if (u->IsFrontEngine() && (n-- == 0)) {
|
if (u->IsFrontEngine() && (n-- == 0)) {
|
||||||
/* Target it. */
|
/* Target it. */
|
||||||
v->dest_tile = u->index;
|
v->dest_tile = u->index;
|
||||||
v->age = 0;
|
v->age = 0;
|
||||||
|
u->disaster_vehicle = v->index;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -379,6 +380,7 @@ static bool DisasterTick_Ufo(DisasterVehicle *v)
|
||||||
v->age++;
|
v->age++;
|
||||||
if (u->crashed_ctr == 0) {
|
if (u->crashed_ctr == 0) {
|
||||||
u->Crash();
|
u->Crash();
|
||||||
|
u->disaster_vehicle = INVALID_VEHICLE;
|
||||||
|
|
||||||
AddTileNewsItem(STR_NEWS_DISASTER_SMALL_UFO, NT_ACCIDENT, u->tile);
|
AddTileNewsItem(STR_NEWS_DISASTER_SMALL_UFO, NT_ACCIDENT, u->tile);
|
||||||
|
|
||||||
|
@ -970,20 +972,20 @@ void ReleaseDisastersTargetingIndustry(IndustryID i)
|
||||||
* Notify disasters that we are about to delete a vehicle. So make them head elsewhere.
|
* Notify disasters that we are about to delete a vehicle. So make them head elsewhere.
|
||||||
* @param vehicle deleted vehicle
|
* @param vehicle deleted vehicle
|
||||||
*/
|
*/
|
||||||
void ReleaseDisastersTargetingVehicle(VehicleID vehicle)
|
void ReleaseDisasterVehicle(VehicleID vehicle)
|
||||||
{
|
{
|
||||||
for (DisasterVehicle *v : DisasterVehicle::Iterate()) {
|
DisasterVehicle *v = DisasterVehicle::GetIfValid(vehicle);
|
||||||
/* primary disaster vehicles that have chosen target */
|
if (v == nullptr) return;
|
||||||
if (v->subtype == ST_SMALL_UFO) {
|
|
||||||
if (v->state != 0 && v->dest_tile == vehicle) {
|
/* primary disaster vehicles that have chosen target */
|
||||||
/* Revert to target-searching */
|
assert(v->subtype == ST_SMALL_UFO);
|
||||||
v->state = 0;
|
assert(v->state != 0);
|
||||||
v->dest_tile = RandomTile();
|
|
||||||
GetAircraftFlightLevelBounds(v, &v->z_pos, nullptr);
|
/* Revert to target-searching */
|
||||||
v->age = 0;
|
v->state = 0;
|
||||||
}
|
v->dest_tile = RandomTile();
|
||||||
}
|
GetAircraftFlightLevelBounds(v, &v->z_pos, nullptr);
|
||||||
}
|
v->age = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DisasterVehicle::UpdateDeltaXY()
|
void DisasterVehicle::UpdateDeltaXY()
|
||||||
|
|
|
@ -116,6 +116,8 @@ struct RoadVehicle final : public GroundVehicle<RoadVehicle, VEH_ROAD> {
|
||||||
RoadType roadtype; //!< Roadtype of this vehicle.
|
RoadType roadtype; //!< Roadtype of this vehicle.
|
||||||
RoadTypes compatible_roadtypes; //!< Roadtypes this consist is powered on.
|
RoadTypes compatible_roadtypes; //!< Roadtypes this consist is powered on.
|
||||||
|
|
||||||
|
VehicleID disaster_vehicle = INVALID_VEHICLE; ///< NOSAVE: Disaster vehicle targetting this vehicle.
|
||||||
|
|
||||||
/** We don't want GCC to zero our struct! It already is zeroed and has an index! */
|
/** We don't want GCC to zero our struct! It already is zeroed and has an index! */
|
||||||
RoadVehicle() : GroundVehicleBase() {}
|
RoadVehicle() : GroundVehicleBase() {}
|
||||||
/** We want to 'destruct' the right class. */
|
/** We want to 'destruct' the right class. */
|
||||||
|
|
|
@ -496,6 +496,16 @@ void AfterLoadVehicles(bool part_of_load)
|
||||||
UpdateAircraftCache(Aircraft::From(v), true);
|
UpdateAircraftCache(Aircraft::From(v), true);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case VEH_DISASTER: {
|
||||||
|
auto *dv = DisasterVehicle::From(v);
|
||||||
|
if (dv->subtype == ST_SMALL_UFO && dv->state != 0) {
|
||||||
|
RoadVehicle *u = RoadVehicle::GetIfValid(v->dest_tile.base());
|
||||||
|
if (u != nullptr && u->IsFrontEngine()) u->disaster_vehicle = dv->index;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -862,6 +862,8 @@ void Vehicle::PreDestructor()
|
||||||
/* Leave the drive through roadstop, when you have not already left it. */
|
/* Leave the drive through roadstop, when you have not already left it. */
|
||||||
RoadStop::GetByTile(v->tile, GetRoadStopType(v->tile))->Leave(v);
|
RoadStop::GetByTile(v->tile, GetRoadStopType(v->tile))->Leave(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (v->disaster_vehicle != INVALID_VEHICLE) ReleaseDisasterVehicle(v->disaster_vehicle);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->Previous() == nullptr) {
|
if (this->Previous() == nullptr) {
|
||||||
|
@ -884,8 +886,6 @@ void Vehicle::PreDestructor()
|
||||||
DeleteDepotHighlightOfVehicle(this);
|
DeleteDepotHighlightOfVehicle(this);
|
||||||
|
|
||||||
StopGlobalFollowVehicle(this);
|
StopGlobalFollowVehicle(this);
|
||||||
|
|
||||||
ReleaseDisastersTargetingVehicle(this->index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vehicle::~Vehicle()
|
Vehicle::~Vehicle()
|
||||||
|
|
|
@ -168,7 +168,7 @@ bool CanVehicleUseStation(EngineID engine_type, const struct Station *st);
|
||||||
bool CanVehicleUseStation(const Vehicle *v, const struct Station *st);
|
bool CanVehicleUseStation(const Vehicle *v, const struct Station *st);
|
||||||
StringID GetVehicleCannotUseStationReason(const Vehicle *v, const Station *st);
|
StringID GetVehicleCannotUseStationReason(const Vehicle *v, const Station *st);
|
||||||
|
|
||||||
void ReleaseDisastersTargetingVehicle(VehicleID vehicle);
|
void ReleaseDisasterVehicle(VehicleID vehicle);
|
||||||
|
|
||||||
typedef std::vector<VehicleID> VehicleSet;
|
typedef std::vector<VehicleID> VehicleSet;
|
||||||
void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8_t num_vehicles);
|
void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8_t num_vehicles);
|
||||||
|
|
Loading…
Reference in New Issue