Change: Big UFO disaster targets current location of a random train (#10290)

This commit is contained in:
Charles Pigott 2023-01-06 13:05:09 +00:00 committed by GitHub
parent 1b1aa682a6
commit e00996a18a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 9 deletions

View File

@ -40,6 +40,7 @@
#include "sound_func.h" #include "sound_func.h"
#include "effectvehicle_func.h" #include "effectvehicle_func.h"
#include "roadveh.h" #include "roadveh.h"
#include "train.h"
#include "ai/ai.hpp" #include "ai/ai.hpp"
#include "game/game.hpp" #include "game/game.hpp"
#include "company_base.h" #include "company_base.h"
@ -578,17 +579,34 @@ static bool DisasterTick_Big_Ufo(DisasterVehicle *v)
} }
v->current_order.SetDestination(1); v->current_order.SetDestination(1);
TileIndex tile_org = RandomTile(); const auto is_valid_target = [](const Train *t) {
TileIndex tile = tile_org; return t->IsFrontEngine() // Only the engines
do { && Company::IsHumanID(t->owner) // Don't break AIs
if (IsPlainRailTile(tile) && && IsPlainRailTile(t->tile) // No tunnels
Company::IsHumanID(GetTileOwner(tile))) { && (t->vehstatus & VS_CRASHED) == 0; // Not crashed
};
uint n = 0; // Total number of targetable trains.
for (const Train *t : Train::Iterate()) {
if (is_valid_target(t)) n++;
}
if (n == 0) {
/* If there are no targetable trains, destroy the UFO. */
delete v;
return false;
}
n = RandomRange(n); // Choose one of them.
for (const Train *t : Train::Iterate()) {
/* Find (n+1)-th train. */
if (is_valid_target(t) && (n-- == 0)) {
/* Target it. */
v->dest_tile = t->tile;
v->age = 0;
break; break;
} }
tile = TILE_MASK(tile + 1); }
} while (tile != tile_org);
v->dest_tile = tile;
v->age = 0;
} }
return true; return true;