Avoid iterating vehicle list to release disaster vehicles if there are none

This commit is contained in:
Jonathan G Rennison
2019-09-23 21:36:20 +01:00
parent 1391f8fc59
commit 9354b5c15c
2 changed files with 14 additions and 1 deletions

View File

@@ -56,6 +56,8 @@
/** Delay counter for considering the next disaster. */ /** Delay counter for considering the next disaster. */
uint16 _disaster_delay; uint16 _disaster_delay;
static uint32 _disaster_vehicle_count = 0;
static void DisasterClearSquare(TileIndex tile) static void DisasterClearSquare(TileIndex tile)
{ {
if (EnsureNoVehicleOnGround(tile).Failed()) return; if (EnsureNoVehicleOnGround(tile).Failed()) return;
@@ -121,6 +123,7 @@ DisasterVehicle::DisasterVehicle() :
SpecializedVehicleBase() SpecializedVehicleBase()
{ {
RegisterGameEvents(GEF_DISASTER_VEH); RegisterGameEvents(GEF_DISASTER_VEH);
_disaster_vehicle_count++;
} }
/** /**
@@ -135,6 +138,7 @@ DisasterVehicle::DisasterVehicle(int x, int y, Direction direction, DisasterSubT
SpecializedVehicleBase(), big_ufo_destroyer_target(big_ufo_destroyer_target) SpecializedVehicleBase(), big_ufo_destroyer_target(big_ufo_destroyer_target)
{ {
RegisterGameEvents(GEF_DISASTER_VEH); RegisterGameEvents(GEF_DISASTER_VEH);
_disaster_vehicle_count++;
this->vehstatus = VS_UNCLICKABLE; this->vehstatus = VS_UNCLICKABLE;
@@ -183,6 +187,11 @@ DisasterVehicle::DisasterVehicle(int x, int y, Direction direction, DisasterSubT
this->UpdatePositionAndViewport(); this->UpdatePositionAndViewport();
} }
DisasterVehicle::~DisasterVehicle()
{
_disaster_vehicle_count--;
}
/** /**
* Update the position of the vehicle. * Update the position of the vehicle.
* @param x The new X-coordinate. * @param x The new X-coordinate.
@@ -970,6 +979,8 @@ void StartupDisasters()
*/ */
void ReleaseDisastersTargetingIndustry(IndustryID i) void ReleaseDisastersTargetingIndustry(IndustryID i)
{ {
if (!_disaster_vehicle_count) return;
DisasterVehicle *v; DisasterVehicle *v;
FOR_ALL_DISASTERVEHICLES(v) { FOR_ALL_DISASTERVEHICLES(v) {
/* primary disaster vehicles that have chosen target */ /* primary disaster vehicles that have chosen target */
@@ -986,6 +997,8 @@ void ReleaseDisastersTargetingIndustry(IndustryID i)
*/ */
void ReleaseDisastersTargetingVehicle(VehicleID vehicle) void ReleaseDisastersTargetingVehicle(VehicleID vehicle)
{ {
if (!_disaster_vehicle_count) return;
DisasterVehicle *v; DisasterVehicle *v;
FOR_ALL_DISASTERVEHICLES(v) { FOR_ALL_DISASTERVEHICLES(v) {
/* primary disaster vehicles that have chosen target */ /* primary disaster vehicles that have chosen target */

View File

@@ -45,7 +45,7 @@ struct DisasterVehicle FINAL : public SpecializedVehicle<DisasterVehicle, VEH_DI
DisasterVehicle(); DisasterVehicle();
DisasterVehicle(int x, int y, Direction direction, DisasterSubType subtype, VehicleID big_ufo_destroyer_target = VEH_INVALID); DisasterVehicle(int x, int y, Direction direction, DisasterSubType subtype, VehicleID big_ufo_destroyer_target = VEH_INVALID);
/** We want to 'destruct' the right class. */ /** We want to 'destruct' the right class. */
virtual ~DisasterVehicle() {} virtual ~DisasterVehicle();
void UpdatePosition(int x, int y, int z); void UpdatePosition(int x, int y, int z);
void UpdateDeltaXY(); void UpdateDeltaXY();