Codechange: Replace FOR_ALL_INDUSTRIES with range-based for loops

This commit is contained in:
glx 2019-12-16 18:51:20 +01:00 committed by Niels Martin Hansen
parent 4ae829cb27
commit 00c2a98cf3
11 changed files with 29 additions and 61 deletions

View File

@ -745,9 +745,9 @@ static void Disaster_Airplane_Init()
{
if (!Vehicle::CanAllocateItem(2)) return;
Industry *i, *found = nullptr;
Industry *found = nullptr;
FOR_ALL_INDUSTRIES(i) {
for (Industry *i : Industry::Iterate()) {
if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_AIRPLANE_ATTACKS) &&
(found == nullptr || Chance16(1, 2))) {
found = i;
@ -771,9 +771,9 @@ static void Disaster_Helicopter_Init()
{
if (!Vehicle::CanAllocateItem(3)) return;
Industry *i, *found = nullptr;
Industry *found = nullptr;
FOR_ALL_INDUSTRIES(i) {
for (Industry *i : Industry::Iterate()) {
if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CHOPPER_ATTACKS) &&
(found == nullptr || Chance16(1, 2))) {
found = i;
@ -858,9 +858,7 @@ static void Disaster_CoalMine_Init()
uint m;
for (m = 0; m < 15; m++) {
const Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (const Industry *i : Industry::Iterate()) {
if ((GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_CAN_SUBSIDENCE) && --index < 0) {
SetDParam(0, i->town->index);
AddTileNewsItem(STR_NEWS_DISASTER_COAL_MINE_SUBSIDENCE, NT_ACCIDENT, i->location.tile + TileDiffXY(1, 1)); // keep the news, even when the mine closes

View File

@ -167,9 +167,6 @@ void ReleaseDisastersTargetingIndustry(IndustryID);
bool IsTileForestIndustry(TileIndex tile);
#define FOR_ALL_INDUSTRIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Industry, industry_index, var, start)
#define FOR_ALL_INDUSTRIES(var) FOR_ALL_INDUSTRIES_FROM(var, 0)
/** Data for managing the number of industries of a single industry type. */
struct IndustryTypeBuildData {
uint32 probability; ///< Relative probability of building this industry.

View File

@ -1203,8 +1203,7 @@ void OnTick_Industry()
if (_game_mode == GM_EDITOR) return;
Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (Industry *i : Industry::Iterate()) {
ProduceIndustryGoods(i);
}
}
@ -1391,8 +1390,7 @@ static CommandCost FindTownForIndustry(TileIndex tile, int type, Town **t)
if (_settings_game.economy.multiple_industry_per_town) return CommandCost();
const Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (const Industry *i : Industry::Iterate()) {
if (i->type == (byte)type && i->town == *t) {
*t = nullptr;
return_cmd_error(STR_ERROR_ONLY_ONE_ALLOWED_PER_TOWN);
@ -1629,11 +1627,11 @@ static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlag flags,
static CommandCost CheckIfFarEnoughFromConflictingIndustry(TileIndex tile, int type)
{
const IndustrySpec *indspec = GetIndustrySpec(type);
const Industry *i = nullptr;
/* On a large map with many industries, it may be faster to check an area. */
static const int dmax = 14;
if (Industry::GetNumItems() > (size_t) (dmax * dmax * 2)) {
const Industry* i = nullptr;
TileArea tile_area = TileArea(tile, 1, 1).Expand(dmax);
TILE_AREA_LOOP(atile, tile_area) {
if (GetTileType(atile) == MP_INDUSTRY) {
@ -1651,7 +1649,7 @@ static CommandCost CheckIfFarEnoughFromConflictingIndustry(TileIndex tile, int t
return CommandCost();
}
FOR_ALL_INDUSTRIES(i) {
for (const Industry *i : Industry::Iterate()) {
/* Within 14 tiles from another industry is considered close */
if (DistanceMax(tile, i->location.tile) > 14) continue;
@ -2833,8 +2831,7 @@ void IndustryMonthlyLoop()
_industry_builder.MonthlyLoop();
Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (Industry *i : Industry::Iterate()) {
UpdateIndustryStatistics(i);
if (i->prod_level == PRODLEVEL_CLOSURE) {
delete i;

View File

@ -1203,8 +1203,7 @@ protected:
if (this->industries.NeedRebuild()) {
this->industries.clear();
const Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (const Industry *i : Industry::Iterate()) {
this->industries.push_back(i);
}

View File

@ -91,8 +91,7 @@ uint32 GetIndustryIDAtOffset(TileIndex tile, const Industry *i, uint32 cur_grfid
static uint32 GetClosestIndustry(TileIndex tile, IndustryType type, const Industry *current)
{
uint32 best_dist = UINT32_MAX;
const Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (const Industry *i : Industry::Iterate()) {
if (i->type != type || i == current) continue;
best_dist = min(best_dist, DistanceManhattan(tile, i->location.tile));
@ -145,8 +144,7 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout
} else {
/* Count only those who match the same industry type and layout filter
* Unfortunately, we have to do it manually */
const Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (const Industry *i : Industry::Iterate()) {
if (i->type == ind_index && i != current && (i->selected_layout == layout_filter || layout_filter == 0) && (!town_filter || i->town == current->town)) {
closest_dist = min(closest_dist, DistanceManhattan(current->location.tile, i->location.tile));
count++;

View File

@ -259,8 +259,7 @@ static void InitializeWindowsAndCaches()
}
/* Identify owners of persistent storage arrays */
Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (Industry *i : Industry::Iterate()) {
if (i->psa != nullptr) {
i->psa->feature = GSF_INDUSTRIES;
i->psa->tile = i->location.tile;
@ -1413,7 +1412,6 @@ bool AfterLoadGame()
/* Time starts at 0 instead of 1920.
* Account for this in older games by adding an offset */
if (IsSavegameVersionBefore(SLV_31)) {
Industry *i;
Vehicle *v;
_date += DAYS_TILL_ORIGINAL_BASE_YEAR;
@ -1423,7 +1421,7 @@ bool AfterLoadGame()
for (Waypoint *wp : Waypoint::Iterate()) wp->build_date += DAYS_TILL_ORIGINAL_BASE_YEAR;
for (Engine *e : Engine::Iterate()) e->intro_date += DAYS_TILL_ORIGINAL_BASE_YEAR;
for (Company *c : Company::Iterate()) c->inaugurated_year += ORIGINAL_BASE_YEAR;
FOR_ALL_INDUSTRIES(i) i->last_prod_year += ORIGINAL_BASE_YEAR;
for (Industry *i : Industry::Iterate()) i->last_prod_year += ORIGINAL_BASE_YEAR;
FOR_ALL_VEHICLES(v) {
v->date_of_last_service += DAYS_TILL_ORIGINAL_BASE_YEAR;
@ -1435,8 +1433,6 @@ bool AfterLoadGame()
* To give this prettiness to old savegames, we remove all farmfields and
* plant new ones. */
if (IsSavegameVersionBefore(SLV_32)) {
Industry *i;
for (TileIndex t = 0; t < map_size; t++) {
if (IsTileType(t, MP_CLEAR) && IsClearGround(t, CLEAR_FIELDS)) {
/* remove fields */
@ -1444,7 +1440,7 @@ bool AfterLoadGame()
}
}
FOR_ALL_INDUSTRIES(i) {
for (Industry *i : Industry::Iterate()) {
uint j;
if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
@ -1660,8 +1656,7 @@ bool AfterLoadGame()
if (IsSavegameVersionBefore(SLV_70)) {
/* Added variables to support newindustries */
Industry *i;
FOR_ALL_INDUSTRIES(i) i->founder = OWNER_NONE;
for (Industry *i : Industry::Iterate()) i->founder = OWNER_NONE;
}
/* From version 82, old style canals (above sealevel (0), WATER owner) are no longer supported.
@ -1701,9 +1696,8 @@ bool AfterLoadGame()
}
if (IsSavegameVersionBefore(SLV_78)) {
Industry *i;
uint j;
FOR_ALL_INDUSTRIES(i) {
for (Industry * i : Industry::Iterate()) {
const IndustrySpec *indsp = GetIndustrySpec(i->type);
for (j = 0; j < lengthof(i->produced_cargo); j++) {
i->produced_cargo[j] = indsp->produced_cargo[j];
@ -2760,8 +2754,7 @@ bool AfterLoadGame()
/* Before savegame version 161, persistent storages were not stored in a pool. */
if (!IsSavegameVersionBefore(SLV_76)) {
Industry *ind;
FOR_ALL_INDUSTRIES(ind) {
for (Industry *ind : Industry::Iterate()) {
assert(ind->psa != nullptr);
/* Check if the old storage was empty. */
@ -3044,8 +3037,7 @@ bool AfterLoadGame()
if (IsSavegameVersionBefore(SLV_EXTEND_INDUSTRY_CARGO_SLOTS)) {
/* Make sure added industry cargo slots are cleared */
Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (Industry *i : Industry::Iterate()) {
for (size_t ci = 2; ci < lengthof(i->produced_cargo); ci++) {
i->produced_cargo[ci] = CT_INVALID;
i->produced_cargo_waiting[ci] = 0;
@ -3127,8 +3119,7 @@ bool AfterLoadGame()
}
} else {
/* Link neutral station back to industry, as this is not saved. */
Industry *ind;
FOR_ALL_INDUSTRIES(ind) if (ind->neutral_station != nullptr) ind->neutral_station->industry = ind;
for (Industry *ind : Industry::Iterate()) if (ind->neutral_station != nullptr) ind->neutral_station->industry = ind;
}
if (IsSavegameVersionBefore(SLV_TREES_WATER_CLASS)) {

View File

@ -77,10 +77,8 @@ static const SaveLoad _industry_desc[] = {
static void Save_INDY()
{
Industry *ind;
/* Write the industries */
FOR_ALL_INDUSTRIES(ind) {
for (Industry *ind : Industry::Iterate()) {
SlSetArrayIndex(ind->index);
SlObject(ind, _industry_desc);
}
@ -129,9 +127,7 @@ static void Load_TIDS()
static void Ptrs_INDY()
{
Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (Industry *i : Industry::Iterate()) {
SlObject(i, _industry_desc);
}
}

View File

@ -15,17 +15,14 @@
ScriptIndustryList::ScriptIndustryList()
{
Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (const Industry *i : Industry::Iterate()) {
this->AddItem(i->index);
}
}
ScriptIndustryList_CargoAccepting::ScriptIndustryList_CargoAccepting(CargoID cargo_id)
{
const Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (const Industry *i : Industry::Iterate()) {
for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
if (i->accepts_cargo[j] == cargo_id) this->AddItem(i->index);
}
@ -34,9 +31,7 @@ ScriptIndustryList_CargoAccepting::ScriptIndustryList_CargoAccepting(CargoID car
ScriptIndustryList_CargoProducing::ScriptIndustryList_CargoProducing(CargoID cargo_id)
{
const Industry *i;
FOR_ALL_INDUSTRIES(i) {
for (const Industry *i : Industry::Iterate()) {
for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] == cargo_id) this->AddItem(i->index);
}

View File

@ -385,8 +385,7 @@ void Station::RemoveFromAllNearbyLists()
{
Town *t;
FOR_ALL_TOWNS(t) { t->stations_near.erase(this); }
Industry *i;
FOR_ALL_INDUSTRIES(i) { i->stations_near.erase(this); }
for (Industry *i : Industry::Iterate()) { i->stations_near.erase(this); }
}
/**

View File

@ -133,8 +133,7 @@ void RebuildSubsidisedSourceAndDestinationCache()
Town *t;
FOR_ALL_TOWNS(t) t->cache.part_of_subsidy = POS_NONE;
Industry *i;
FOR_ALL_INDUSTRIES(i) i->part_of_subsidy = POS_NONE;
for (Industry *i : Industry::Iterate()) i->part_of_subsidy = POS_NONE;
const Subsidy *s;
FOR_ALL_SUBSIDIES(s) {

View File

@ -112,8 +112,7 @@ Town::~Town()
DeleteWindowById(WC_TOWN_VIEW, this->index);
/* Check no industry is related to us. */
const Industry *i;
FOR_ALL_INDUSTRIES(i) assert(i->town != this);
for (const Industry *i : Industry::Iterate()) assert(i->town != this);
/* ... and no object is related to us. */
const Object *o;