Codechange: Replace FOR_ALL_TOWNS with range-based for loops

pull/128/head
glx 4 years ago committed by Niels Martin Hansen
parent 0b489f9924
commit ee7a8eebca

@ -302,8 +302,6 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
assert(old_owner != _local_company);
}
Town *t;
assert(old_owner != new_owner);
{
@ -357,7 +355,7 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
if (new_owner == INVALID_OWNER) RebuildSubsidisedSourceAndDestinationCache();
/* Take care of rating and transport rights in towns */
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
/* If a company takes over, give the ratings to that company. */
if (new_owner != INVALID_OWNER) {
if (HasBit(t->have_ratings, old_owner)) {

@ -347,9 +347,7 @@ struct NetworkChatWindow : public Window {
* Not that the following assumes all town indices are adjacent, ie no
* towns have been deleted. */
if (*item < (uint)MAX_CLIENT_SLOTS + Town::GetPoolSize()) {
const Town *t;
FOR_ALL_TOWNS_FROM(t, *item - MAX_CLIENT_SLOTS) {
for (const Town *t : Town::Iterate(*item - MAX_CLIENT_SLOTS)) {
/* Get the town-name via the string-system */
SetDParam(0, t->index);
GetString(chat_tab_temp_buffer, STR_TOWN_NAME, lastof(chat_tab_temp_buffer));

@ -83,8 +83,7 @@ void InitializeBuildingCounts()
{
memset(&_building_counts, 0, sizeof(_building_counts));
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
memset(&t->cache.building_counts, 0, sizeof(t->cache.building_counts));
}
}

@ -1188,8 +1188,7 @@ static void CheckCaches()
/* Check the town caches. */
std::vector<TownCache> old_town_caches;
Town *t;
FOR_ALL_TOWNS(t) {
for (const Town *t : Town::Iterate()) {
old_town_caches.push_back(t->cache);
}
@ -1198,7 +1197,7 @@ static void CheckCaches()
RebuildSubsidisedSourceAndDestinationCache();
uint i = 0;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
if (MemCmpT(old_town_caches.data() + i, &t->cache) != 0) {
DEBUG(desync, 2, "town cache mismatch: town %i", (int)t->index);
}

@ -169,9 +169,7 @@ static void ConvertTownOwner()
/* since savegame version 4.1, exclusive transport rights are stored at towns */
static void UpdateExclusiveRights()
{
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
t->exclusivity = INVALID_COMPANY;
}
@ -270,8 +268,7 @@ static void InitializeWindowsAndCaches()
s->airport.psa->tile = s->airport.tile;
}
}
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
for (std::list<PersistentStorage *>::iterator it = t->psa_list.begin(); it != t->psa_list.end(); ++it) {
(*it)->feature = GSF_FAKE_TOWNS;
(*it)->tile = t->xy;
@ -651,8 +648,7 @@ bool AfterLoadGame()
if (st->name != nullptr) st->string_id = STR_SV_STNAME_FALLBACK;
}
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
t->name = CopyFromOldName(t->townnametype);
if (t->name != nullptr) t->townnametype = SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name;
}
@ -980,8 +976,7 @@ bool AfterLoadGame()
/* From version 9.0, we update the max passengers of a town (was sometimes negative
* before that. */
if (IsSavegameVersionBefore(SLV_9)) {
Town *t;
FOR_ALL_TOWNS(t) UpdateTownMaxPass(t);
for (Town *t : Town::Iterate()) UpdateTownMaxPass(t);
}
/* From version 16.0, we included autorenew on engines, which are now saved, but
@ -1570,9 +1565,7 @@ bool AfterLoadGame()
* fast was added in version 54. From version 56 this is now saved in the
* town as cities can be built specifically in the scenario editor. */
if (IsSavegameVersionBefore(SLV_56)) {
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
if (_settings_game.economy.larger_towns != 0 && (t->index % _settings_game.economy.larger_towns) == 0) {
t->larger_town = true;
}
@ -2018,8 +2011,7 @@ bool AfterLoadGame()
if (e->company_avail == 0xFF) e->company_avail = 0xFFFF;
}
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
if (t->have_ratings == 0xFF) t->have_ratings = 0xFFFF;
for (uint i = 8; i != MAX_COMPANIES; i++) t->ratings[i] = RATING_INITIAL;
}
@ -2108,8 +2100,7 @@ bool AfterLoadGame()
/* Initialize layout of all towns. Older versions were using different
* generator for random town layout, use it if needed. */
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
if (_settings_game.economy.town_layout != TL_RANDOM) {
t->layout = _settings_game.economy.town_layout;
continue;
@ -2791,9 +2782,7 @@ bool AfterLoadGame()
if (IsSavegameVersionBefore(SLV_164)) FixupTrainLengths();
if (IsSavegameVersionBefore(SLV_165)) {
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
/* Set the default cargo requirement for town growth */
switch (_settings_game.game_creation.landscape) {
case LT_ARCTIC:
@ -2827,8 +2816,7 @@ bool AfterLoadGame()
Town::Get(GetTownIndex(t))->cargo_accepted.Add(t);
}
Town *town;
FOR_ALL_TOWNS(town) {
for (Town *town : Town::Iterate()) {
UpdateTownCargoes(town);
}
}
@ -2984,8 +2972,7 @@ bool AfterLoadGame()
if (IsSavegameVersionBefore(SLV_198)) {
/* Convert towns growth_rate and grow_counter to ticks */
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
/* 0x8000 = TOWN_GROWTH_RATE_CUSTOM previously */
if (t->growth_rate & 0x8000) SetBit(t->flags, TOWN_CUSTOM_GROWTH);
if (t->growth_rate != TOWN_GROWTH_RATE_NONE) {

@ -152,10 +152,8 @@ static uint32 RemapOldTownName(uint32 townnameparts, byte old_town_name_type)
static void FixOldTowns()
{
Town *town;
/* Convert town-names if needed */
FOR_ALL_TOWNS(town) {
for (Town *town : Town::Iterate()) {
if (IsInsideMM(town->townnametype, 0x20C1, 0x20C3)) {
town->townnametype = SPECSTR_TOWNNAME_ENGLISH + _settings_game.game_creation.town_name;
town->townnameparts = RemapOldTownName(town->townnameparts, _settings_game.game_creation.town_name);

@ -24,12 +24,11 @@
*/
void RebuildTownCaches()
{
Town *town;
InitializeBuildingCounts();
RebuildTownKdtree();
/* Reset town population and num_houses */
FOR_ALL_TOWNS(town) {
for (Town *town : Town::Iterate()) {
town->cache.population = 0;
town->cache.num_houses = 0;
}
@ -38,7 +37,7 @@ void RebuildTownCaches()
if (!IsTileType(t, MP_HOUSE)) continue;
HouseID house_id = GetHouseType(t);
town = Town::GetByTile(t);
Town *town = Town::GetByTile(t);
IncreaseBuildingCount(town, house_id);
if (IsHouseCompleted(t)) town->cache.population += HouseSpec::Get(house_id)->population;
@ -47,7 +46,7 @@ void RebuildTownCaches()
}
/* Update the population and num_house dependent values */
FOR_ALL_TOWNS(town) {
for (Town *town : Town::Iterate()) {
UpdateTownRadius(town);
UpdateTownCargoes(town);
}
@ -263,9 +262,7 @@ static void RealSave_Town(Town *t)
static void Save_TOWN()
{
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
SlSetArrayIndex(t->index);
SlAutolength((AutolengthProc*)RealSave_Town, t);
}
@ -311,8 +308,7 @@ static void Ptrs_TOWN()
/* Don't run when savegame version lower than 161. */
if (IsSavegameVersionBefore(SLV_161)) return;
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
SlObject(t, _town_desc);
}
}

@ -15,8 +15,7 @@
ScriptTownList::ScriptTownList()
{
Town *t;
FOR_ALL_TOWNS(t) {
for (const Town *t : Town::Iterate()) {
this->AddItem(t->index);
}
}

@ -914,8 +914,7 @@ void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) co
*/
void SmallMapWindow::DrawTowns(const DrawPixelInfo *dpi) const
{
const Town *t;
FOR_ALL_TOWNS(t) {
for (const Town *t : Town::Iterate()) {
/* Remap the town coordinate */
Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);

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

@ -2185,10 +2185,10 @@ uint8 GetAirportNoiseLevelForDistance(const AirportSpec *as, uint distance)
*/
Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint &mindist)
{
Town *t, *nearest = nullptr;
Town *nearest = nullptr;
uint add = as->size_x + as->size_y - 2; // GetMinimalAirportDistanceToTile can differ from DistanceManhattan by this much
mindist = UINT_MAX - add; // prevent overflow
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
if (DistanceManhattan(t->xy, it) < mindist + add) { // avoid calling GetMinimalAirportDistanceToTile too often
TileIterator *copy = it.Clone();
uint dist = GetMinimalAirportDistanceToTile(*copy, t->xy);
@ -2207,9 +2207,7 @@ Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint
/** Recalculate the noise generated by the airports of each town */
void UpdateAirportsNoise()
{
Town *t;
FOR_ALL_TOWNS(t) t->noise_reached = 0;
for (Town *t : Town::Iterate()) t->noise_reached = 0;
for (const Station *st : Station::Iterate()) {
if (st->airport.tile != INVALID_TILE && st->airport.type != AT_OILRIG) {

@ -130,8 +130,7 @@ static inline void SetPartOfSubsidyFlag(SourceType type, SourceID index, PartOfS
/** Perform a full rebuild of the subsidies cache. */
void RebuildSubsidisedSourceAndDestinationCache()
{
Town *t;
FOR_ALL_TOWNS(t) t->cache.part_of_subsidy = POS_NONE;
for (Town *t : Town::Iterate()) t->cache.part_of_subsidy = POS_NONE;
for (Industry *i : Industry::Iterate()) i->part_of_subsidy = POS_NONE;

@ -185,9 +185,6 @@ TileIndexDiff GetHouseNorthPart(HouseID &house);
Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX);
#define FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start)
#define FOR_ALL_TOWNS(var) FOR_ALL_TOWNS_FROM(var, 0)
void ResetHouses();
void ClearTownHouse(Town *t, TileIndex tile);

@ -67,8 +67,7 @@ TownKdtree _town_kdtree(&Kdtree_TownXYFunc);
void RebuildTownKdtree()
{
std::vector<TownID> townids;
Town *town;
FOR_ALL_TOWNS(town) {
for (const Town *town : Town::Iterate()) {
townids.push_back(town->index);
}
_town_kdtree.Build(townids.begin(), townids.end());
@ -408,9 +407,7 @@ void Town::UpdateVirtCoord()
/** Update the virtual coords needed to draw the town sign for all towns. */
void UpdateAllTownVirtCoords()
{
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
t->UpdateVirtCoord();
}
}
@ -437,9 +434,7 @@ static void ChangePopulation(Town *t, int mod)
uint32 GetWorldPopulation()
{
uint32 pop = 0;
const Town *t;
FOR_ALL_TOWNS(t) pop += t->cache.population;
for (const Town *t : Town::Iterate()) pop += t->cache.population;
return pop;
}
@ -844,10 +839,9 @@ void UpdateTownCargoes(Town *t)
/** Updates the bitmap of all cargoes accepted by houses. */
void UpdateTownCargoBitmap()
{
Town *town;
_town_cargoes_accepted = 0;
FOR_ALL_TOWNS(town) {
for (const Town *town : Town::Iterate()) {
_town_cargoes_accepted |= town->cargo_accepted_total;
}
}
@ -874,8 +868,7 @@ void OnTick_Town()
{
if (_game_mode == GM_EDITOR) return;
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
TownTickHandler(t);
}
}
@ -1842,9 +1835,7 @@ static CommandCost TownCanBePlacedHere(TileIndex tile)
*/
static bool IsUniqueTownName(const char *name)
{
const Town *t;
FOR_ALL_TOWNS(t) {
for (const Town *t : Town::Iterate()) {
if (t->name != nullptr && strcmp(t->name, name) == 0) return false;
}
@ -3642,9 +3633,7 @@ CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType
void TownsMonthlyLoop()
{
Town *t;
FOR_ALL_TOWNS(t) {
for (Town *t : Town::Iterate()) {
if (t->road_build_months != 0) t->road_build_months--;
if (t->exclusive_counter != 0) {

@ -689,8 +689,7 @@ private:
if (this->towns.NeedRebuild()) {
this->towns.clear();
const Town *t;
FOR_ALL_TOWNS(t) {
for (const Town *t : Town::Iterate()) {
this->towns.push_back(t);
}
@ -991,8 +990,7 @@ public:
} else {
this->towns.clear();
const Town *t;
FOR_ALL_TOWNS(t) {
for (const Town *t : Town::Iterate()) {
this->string_filter.ResetState();
SetDParam(0, t->index);

@ -94,8 +94,7 @@ bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names)
if (town_names->find(buf1) != town_names->end()) return false;
town_names->insert(buf1);
} else {
const Town *t;
FOR_ALL_TOWNS(t) {
for (const Town *t : Town::Iterate()) {
/* We can't just compare the numbers since
* several numbers may map to a single name. */
const char *buf = t->name;

@ -2255,8 +2255,7 @@ void RebuildViewportKdtree()
if (wp->sign.kdtree_valid) items.push_back(ViewportSignKdtreeItem::MakeWaypoint(wp->index));
}
const Town *town;
FOR_ALL_TOWNS(town) {
for (const Town *town : Town::Iterate()) {
if (town->cache.sign.kdtree_valid) items.push_back(ViewportSignKdtreeItem::MakeTown(town->index));
}

Loading…
Cancel
Save