Changeset - r23962:50d7e69becd3
[Not reviewed]
master
0 11 0
glx - 5 years ago 2019-12-16 17:51:20
glx@openttd.org
Codechange: Replace FOR_ALL_INDUSTRIES with range-based for loops
11 files changed with 29 insertions and 61 deletions:
0 comments (0 inline, 0 general)
src/disaster_vehicle.cpp
Show inline comments
 
@@ -742,15 +742,15 @@ static void Disaster_Small_Ufo_Init()
 

	
 
/* Combat airplane which destroys an oil refinery */
 
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;
 
		}
 
	}
 

	
 
@@ -768,15 +768,15 @@ static void Disaster_Airplane_Init()
 

	
 
/** Combat helicopter that destroys a factory */
 
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;
 
		}
 
	}
 

	
 
@@ -855,15 +855,13 @@ static void Disaster_Big_Submarine_Init(
 
static void Disaster_CoalMine_Init()
 
{
 
	int index = GB(Random(), 0, 4);
 
	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
 

	
 
				{
 
					TileIndex tile = i->location.tile;
src/industry.h
Show inline comments
 
@@ -164,15 +164,12 @@ protected:
 
void PlantRandomFarmField(const Industry *i);
 

	
 
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.
 
	byte   min_number;   ///< Smallest number of industries that should exist (either \c 0 or \c 1).
 
	uint16 target_count; ///< Desired number of industries of this type.
 
	uint16 max_wait;     ///< Starting number of turns to wait (copied to #wait_count).
src/industry_cmd.cpp
Show inline comments
 
@@ -1200,14 +1200,13 @@ void OnTick_Industry()
 
			if (_settings_client.sound.ambient) SndPlayTileFx(SND_36_CARTOON_CRASH, _industry_sound_tile);
 
		}
 
	}
 

	
 
	if (_game_mode == GM_EDITOR) return;
 

	
 
	Industry *i;
 
	FOR_ALL_INDUSTRIES(i) {
 
	for (Industry *i : Industry::Iterate()) {
 
		ProduceIndustryGoods(i);
 
	}
 
}
 

	
 
/**
 
 * Check the conditions of #CHECK_NOTHING (Always succeeds).
 
@@ -1388,14 +1387,13 @@ static CheckNewIndustryProc * const _che
 
static CommandCost FindTownForIndustry(TileIndex tile, int type, Town **t)
 
{
 
	*t = ClosestTownFromTile(tile, UINT_MAX);
 

	
 
	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);
 
		}
 
	}
 

	
 
@@ -1626,17 +1624,17 @@ static bool CheckIfCanLevelIndustryPlatf
 
 * @param type Type of the new industry.
 
 * @return Succeeded or failed command.
 
 */
 
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) {
 
				const Industry *i2 = Industry::GetByTile(atile);
 
				if (i == i2) continue;
 
				i = i2;
 
@@ -1648,13 +1646,13 @@ static CommandCost CheckIfFarEnoughFromC
 
				}
 
			}
 
		}
 
		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;
 

	
 
		/* check if there are any conflicting industry types around */
 
		if (i->type == indspec->conflicting[0] ||
 
				i->type == indspec->conflicting[1] ||
 
@@ -2830,14 +2828,13 @@ void IndustryDailyLoop()
 
void IndustryMonthlyLoop()
 
{
 
	Backup<CompanyID> cur_company(_current_company, OWNER_NONE, FILE_LINE);
 

	
 
	_industry_builder.MonthlyLoop();
 

	
 
	Industry *i;
 
	FOR_ALL_INDUSTRIES(i) {
 
	for (Industry *i : Industry::Iterate()) {
 
		UpdateIndustryStatistics(i);
 
		if (i->prod_level == PRODLEVEL_CLOSURE) {
 
			delete i;
 
		} else {
 
			ChangeIndustryProduction(i, true);
 
			SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
src/industry_gui.cpp
Show inline comments
 
@@ -1200,14 +1200,13 @@ protected:
 
	/** (Re)Build industries list */
 
	void BuildSortIndustriesList()
 
	{
 
		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);
 
			}
 

	
 
			this->industries.shrink_to_fit();
 
			this->industries.RebuildDone();
 
			this->vscroll->SetCount((uint)this->industries.size()); // Update scrollbar as well.
src/newgrf_industries.cpp
Show inline comments
 
@@ -88,14 +88,13 @@ uint32 GetIndustryIDAtOffset(TileIndex t
 
	return 0xFF << 8 | indtsp->grf_prop.subst_id; // so just give him the substitute
 
}
 

	
 
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));
 
	}
 

	
 
	return best_dist;
 
@@ -142,14 +141,13 @@ static uint32 GetCountAndDistanceOfClose
 
		 * In either case, just do the regular var67 */
 
		closest_dist = GetClosestIndustry(current->location.tile, ind_index, current);
 
		count = min(Industry::GetIndustryTypeCount(ind_index), UINT8_MAX); // clamp to 8 bit
 
	} 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++;
 
			}
 
		}
 
	}
src/saveload/afterload.cpp
Show inline comments
 
@@ -256,14 +256,13 @@ static void InitializeWindowsAndCaches()
 
	Object *o;
 
	FOR_ALL_OBJECTS(o) {
 
		Object::IncTypeCount(o->type);
 
	}
 

	
 
	/* 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;
 
		}
 
	}
 
	for (Station *s : Station::Iterate()) {
 
@@ -1410,44 +1409,41 @@ bool AfterLoadGame()
 

	
 
	if (!IsSavegameVersionBefore(SLV_27)) AfterLoadStations();
 

	
 
	/* 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;
 
		_cur_year += ORIGINAL_BASE_YEAR;
 

	
 
		for (Station *st : Station::Iterate())   st->build_date      += DAYS_TILL_ORIGINAL_BASE_YEAR;
 
		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;
 
			v->build_year += ORIGINAL_BASE_YEAR;
 
		}
 
	}
 

	
 
	/* From 32 on we save the industry who made the farmland.
 
	 *  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 */
 
				MakeClear(t, CLEAR_GRASS, 3);
 
			}
 
		}
 

	
 
		FOR_ALL_INDUSTRIES(i) {
 
		for (Industry *i : Industry::Iterate()) {
 
			uint j;
 

	
 
			if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
 
				for (j = 0; j != 50; j++) PlantRandomFarmField(i);
 
			}
 
		}
 
@@ -1657,14 +1653,13 @@ 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.
 
	    Replace the owner for those by OWNER_NONE. */
 
	if (IsSavegameVersionBefore(SLV_82)) {
 
		for (TileIndex t = 0; t < map_size; t++) {
 
@@ -1698,15 +1693,14 @@ bool AfterLoadGame()
 
				if (st->goods[c].cargo.AvailableCount() != 0) SetBit(st->goods[c].status, GoodsEntry::GES_RATING);
 
			}
 
		}
 
	}
 

	
 
	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];
 
			}
 
			for (j = 0; j < lengthof(i->accepts_cargo); j++) {
 
				i->accepts_cargo[j] = indsp->accepts_cargo[j];
 
@@ -2757,14 +2751,13 @@ bool AfterLoadGame()
 
	}
 

	
 
	if (IsSavegameVersionBefore(SLV_161)) {
 
		/* 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. */
 
				bool is_empty = true;
 
				for (uint i = 0; i < sizeof(ind->psa->storage); i++) {
 
					if (ind->psa->GetValue(i) != 0) {
 
@@ -3041,14 +3034,13 @@ bool AfterLoadGame()
 
			t->grow_counter = TownTicksToGameTicks(t->grow_counter) + t->index % TOWN_GROWTH_TICKS;
 
		}
 
	}
 

	
 
	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;
 
				i->production_rate[ci] = 0;
 
				i->last_month_production[ci] = 0;
 
				i->last_month_transported[ci] = 0;
 
@@ -3124,14 +3116,13 @@ bool AfterLoadGame()
 
				st->industry = Industry::GetByTile(st->xy + TileDiffXY(0, 1));
 
				st->industry->neutral_station = st;
 
			}
 
		}
 
	} 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)) {
 
		/* Update water class for trees. */
 
		for (TileIndex t = 0; t < map_size; t++) {
 
			if (IsTileType(t, MP_TREES)) SetWaterClass(t, GetTreeGround(t) == TREE_GROUND_SHORE ? WATER_CLASS_SEA : WATER_CLASS_INVALID);
src/saveload/industry_sl.cpp
Show inline comments
 
@@ -74,16 +74,14 @@ static const SaveLoad _industry_desc[] =
 

	
 
	SLE_END()
 
};
 

	
 
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);
 
	}
 
}
 

	
 
static void Save_IIDS()
 
@@ -126,15 +124,13 @@ static void Load_TIDS()
 
{
 
	Load_NewGRFMapping(_industile_mngr);
 
}
 

	
 
static void Ptrs_INDY()
 
{
 
	Industry *i;
 

	
 
	FOR_ALL_INDUSTRIES(i) {
 
	for (Industry *i : Industry::Iterate()) {
 
		SlObject(i, _industry_desc);
 
	}
 
}
 

	
 
/** Description of the data to save and load in #IndustryBuildData. */
 
static const SaveLoad _industry_builder_desc[] = {
src/script/api/script_industrylist.cpp
Show inline comments
 
@@ -12,33 +12,28 @@
 
#include "../../industry.h"
 

	
 
#include "../../safeguards.h"
 

	
 
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);
 
		}
 
	}
 
}
 

	
 
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);
 
		}
 
	}
 
}
src/station.cpp
Show inline comments
 
@@ -382,14 +382,13 @@ static void AddIndustryToDeliver(Industr
 
 * Remove this station from the nearby stations lists of all towns and industries.
 
 */
 
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); }
 
}
 

	
 
/**
 
 * Test if the given town ID is covered by our catchment area.
 
 * This is used when removing a house tile to determine if it was the last house tile
 
 * within our catchment.
src/subsidy.cpp
Show inline comments
 
@@ -130,14 +130,13 @@ static inline void SetPartOfSubsidyFlag(
 
/** Perform a full rebuild of the subsidies cache. */
 
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) {
 
		SetPartOfSubsidyFlag(s->src_type, s->src, POS_SRC);
 
		SetPartOfSubsidyFlag(s->dst_type, s->dst, POS_DST);
 
	}
src/town_cmd.cpp
Show inline comments
 
@@ -109,14 +109,13 @@ Town::~Town()
 

	
 
	/* Delete town authority window
 
	 * and remove from list of sorted towns */
 
	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;
 
	FOR_ALL_OBJECTS(o) assert(o->town != this);
 

	
 
	/* Check no tile is related to us. */
0 comments (0 inline, 0 general)