From 10e2d1ca3622ae7c33775a7f02d54c89491a374c Mon Sep 17 00:00:00 2001 From: SamuXarick <43006711+SamuXarick@users.noreply.github.com> Date: Sun, 27 Oct 2024 18:52:57 +0000 Subject: [PATCH] Codechange: Use Map::Iterate() to iterate tiles --- src/cheat_gui.cpp | 2 +- src/disaster_vehicle.cpp | 2 +- src/industry_gui.cpp | 2 +- src/landscape.cpp | 2 +- src/map.cpp | 2 +- src/rail_gui.cpp | 2 +- src/road_cmd.cpp | 2 +- src/saveload/afterload.cpp | 34 +++++++++++++++++----------------- src/saveload/company_sl.cpp | 2 +- src/saveload/labelmaps_sl.cpp | 2 +- src/saveload/town_sl.cpp | 6 +++--- src/screenshot.cpp | 2 +- src/settings_table.cpp | 2 +- src/town_cmd.cpp | 8 ++++---- src/water_cmd.cpp | 2 +- 15 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/cheat_gui.cpp b/src/cheat_gui.cpp index 064c645209..d226591add 100644 --- a/src/cheat_gui.cpp +++ b/src/cheat_gui.cpp @@ -146,7 +146,7 @@ static int32_t ClickChangeMaxHlCheat(int32_t new_value, int32_t) /* Check if at least one mountain on the map is higher than the new value. * If yes, disallow the change. */ - for (TileIndex t = 0; t < Map::Size(); t++) { + for (const auto t : Map::Iterate()) { if ((int32_t)TileHeight(t) > new_value) { ShowErrorMessage(STR_CONFIG_SETTING_TOO_HIGH_MOUNTAIN, INVALID_STRING_ID, WL_ERROR); /* Return old, unchanged value */ diff --git a/src/disaster_vehicle.cpp b/src/disaster_vehicle.cpp index f5b311f835..cb909520f7 100644 --- a/src/disaster_vehicle.cpp +++ b/src/disaster_vehicle.cpp @@ -408,7 +408,7 @@ static bool DisasterTick_Ufo(DisasterVehicle *v) static void DestructIndustry(Industry *i) { - for (TileIndex tile = 0; tile != Map::Size(); tile++) { + for (const auto tile : Map::Iterate()) { if (i->TileBelongsToIndustry(tile)) { ResetIndustryConstructionStage(tile); MarkTileDirtyByTile(tile); diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index ac984b1a9f..ba3b9dc3d1 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -626,7 +626,7 @@ public: for (Industry *industry : Industry::Iterate()) delete industry; /* Clear farmland. */ - for (TileIndex tile = 0; tile < Map::Size(); tile++) { + for (const auto tile : Map::Iterate()) { if (IsTileType(tile, MP_CLEAR) && GetRawClearGround(tile) == CLEAR_FIELDS) { MakeClear(tile, CLEAR_GRASS, 3); } diff --git a/src/landscape.cpp b/src/landscape.cpp index 7e416546a9..4a2f2f6d06 100644 --- a/src/landscape.cpp +++ b/src/landscape.cpp @@ -1471,7 +1471,7 @@ static uint CalculateCoverageLine(uint coverage, uint edge_multiplier) std::array edge_histogram = {}; /* Build a histogram of the map height. */ - for (TileIndex tile = 0; tile < Map::Size(); tile++) { + for (const auto tile : Map::Iterate()) { uint h = TileHeight(tile); histogram[h]++; diff --git a/src/map.cpp b/src/map.cpp index eeddf8c5a2..c69ef4eadf 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -367,7 +367,7 @@ uint GetClosestWaterDistance(TileIndex tile, bool water) if (!water) { /* no land found - is this a water-only map? */ - for (TileIndex t = 0; t < Map::Size(); t++) { + for (const auto t : Map::Iterate()) { if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF; } } diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 2dfac4188a..215203eacd 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1928,7 +1928,7 @@ static void SetDefaultRailGui() /* Find the most used rail type */ uint count[RAILTYPE_END]; memset(count, 0, sizeof(count)); - for (TileIndex t = 0; t < Map::Size(); t++) { + for (const auto t : Map::Iterate()) { if (IsTileType(t, MP_RAILWAY) || IsLevelCrossingTile(t) || HasStationTileRail(t) || (IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL)) { count[GetRailType(t)]++; diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index 433715d584..f29f778f3b 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -1919,7 +1919,7 @@ void UpdateNearestTownForRoadTiles(bool invalidate) { assert(!invalidate || _generating_world); - for (TileIndex t = 0; t < Map::Size(); t++) { + for (const auto t : Map::Iterate()) { if (IsTileType(t, MP_ROAD) && !IsRoadDepot(t) && !HasTownOwnedRoad(t)) { TownID tid = INVALID_TOWN; if (!invalidate) { diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index c39ccf6c46..60765ba924 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -657,7 +657,7 @@ bool AfterLoadGame() * (4.3) version, so I just check when versions are older, and then * walk through the whole map.. */ if (IsSavegameVersionBefore(SLV_4, 3)) { - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { if (IsTileType(t, MP_WATER) && GetTileOwner(t) >= MAX_COMPANIES) { SetTileOwner(t, OWNER_WATER); } @@ -928,7 +928,7 @@ bool AfterLoadGame() } } - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { switch (GetTileType(t)) { case MP_STATION: { BaseStation *bst = BaseStation::GetByTile(t); @@ -1340,7 +1340,7 @@ bool AfterLoadGame() } /* .. so we convert the entire map from normal to elrail (so maintain "fairness") */ - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { switch (GetTileType(t)) { case MP_RAILWAY: SetRailType(t, UpdateRailType(GetRailType(t), min_rail)); @@ -1469,7 +1469,7 @@ bool AfterLoadGame() * To give this prettiness to old savegames, we remove all farmfields and * plant new ones. */ if (IsSavegameVersionBefore(SLV_32)) { - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { if (IsTileType(t, MP_CLEAR) && IsClearGround(t, CLEAR_FIELDS)) { /* remove fields */ MakeClear(t, CLEAR_GRASS, 3); @@ -1691,7 +1691,7 @@ bool AfterLoadGame() /* 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 (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { if (IsTileType(t, MP_WATER) && GetWaterTileType(t) == WATER_TILE_CLEAR && GetTileOwner(t) == OWNER_WATER && @@ -1861,7 +1861,7 @@ bool AfterLoadGame() /* Update locks, depots, docks and buoys to have a water class based * on its neighbouring tiles. Done after river and canal updates to * ensure neighbours are correct. */ - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { if (!IsTileFlat(t)) continue; if (IsTileType(t, MP_WATER) && IsLock(t)) SetWaterClassDependingOnSurroundings(t, false); @@ -1870,7 +1870,7 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_87)) { - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { /* skip oil rigs at borders! */ if ((IsTileType(t, MP_WATER) || IsBuoyTile(t)) && (TileX(t) == 0 || TileY(t) == 0 || TileX(t) == Map::MaxX() - 1 || TileY(t) == Map::MaxY() - 1)) { @@ -2409,7 +2409,7 @@ bool AfterLoadGame() {119, 15}, // 14 unused tiles (radar) {140, 4}, // APT_GRASS_FENCE_NE_FLAG_2 }; - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { if (IsAirportTile(t)) { StationGfx old_gfx = GetStationGfx(t); uint8_t offset = 0; @@ -2448,7 +2448,7 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_141)) { - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { /* Reset tropic zone for VOID tiles, they shall not have any. */ if (IsTileType(t, MP_VOID)) SetTropicZone(t, TROPICZONE_NORMAL); } @@ -2528,7 +2528,7 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_149)) { - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { if (!IsTileType(t, MP_STATION)) continue; if (!IsBuoy(t) && !IsOilRig(t) && !(IsDock(t) && IsTileFlat(t))) { SetWaterClass(t, WATER_CLASS_INVALID); @@ -2885,7 +2885,7 @@ bool AfterLoadGame() /* The road owner of standard road stops was not properly accounted for. */ if (IsSavegameVersionBefore(SLV_172)) { - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { if (!IsBayRoadStopTile(t)) continue; Owner o = GetTileOwner(t); SetRoadOwner(t, RTT_ROAD, o); @@ -2912,7 +2912,7 @@ bool AfterLoadGame() { /* Station blocked, wires and pylon flags need to be stored in the map. This is effectively cached data, so no * version check is necessary. This is done here as the SLV_182 check below needs the blocked status. */ - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { if (HasStationTileRail(t)) SetRailStationTileFlags(t, GetStationSpec(t)); } } @@ -2928,7 +2928,7 @@ bool AfterLoadGame() /* Blocked tiles could be reserved due to a bug, which causes * other places to assert upon e.g. station reconstruction. */ - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { if (HasStationTileRail(t) && IsStationTileBlocked(t)) { SetRailStationReservation(t, false); } @@ -3146,14 +3146,14 @@ bool AfterLoadGame() if (IsSavegameVersionBefore(SLV_TREES_WATER_CLASS)) { /* Update water class for trees. */ - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { if (IsTileType(t, MP_TREES)) SetWaterClass(t, GetTreeGround(t) == TREE_GROUND_SHORE ? WATER_CLASS_SEA : WATER_CLASS_INVALID); } } /* Update structures for multitile docks */ if (IsSavegameVersionBefore(SLV_MULTITILE_DOCKS)) { - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { /* Clear docking tile flag from relevant tiles as it * was not previously cleared. */ if (IsTileType(t, MP_WATER) || IsTileType(t, MP_RAILWAY) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE)) { @@ -3175,7 +3175,7 @@ bool AfterLoadGame() if (IsSavegameVersionBeforeOrAt(SLV_ENDING_YEAR)) { /* Reset roadtype/streetcartype info for non-road bridges. */ - for (auto t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { if (IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) != TRANSPORT_ROAD) { SetRoadTypes(t, INVALID_ROADTYPE, INVALID_ROADTYPE); } @@ -3266,7 +3266,7 @@ bool AfterLoadGame() } /* Refresh all level crossings to bar adjacent crossing tiles, if needed. */ - for (auto tile : Map::Iterate()) { + for (const auto tile : Map::Iterate()) { if (IsLevelCrossingTile(tile)) UpdateLevelCrossing(tile, false); } } diff --git a/src/saveload/company_sl.cpp b/src/saveload/company_sl.cpp index 62be5ba5e2..7bc7f8bc8f 100644 --- a/src/saveload/company_sl.cpp +++ b/src/saveload/company_sl.cpp @@ -106,7 +106,7 @@ void AfterLoadCompanyStats() } Company *c; - for (TileIndex tile = 0; tile < Map::Size(); tile++) { + for (const auto tile : Map::Iterate()) { switch (GetTileType(tile)) { case MP_RAILWAY: c = Company::GetIfValid(GetTileOwner(tile)); diff --git a/src/saveload/labelmaps_sl.cpp b/src/saveload/labelmaps_sl.cpp index 9a35ec4ab8..03789db56f 100644 --- a/src/saveload/labelmaps_sl.cpp +++ b/src/saveload/labelmaps_sl.cpp @@ -52,7 +52,7 @@ static void ConvertRailTypes() } if (!needs_conversion) return; - for (TileIndex t : Map::Iterate()) { + for (const auto t : Map::Iterate()) { switch (GetTileType(t)) { case MP_RAILWAY: SetRailType(t, railtype_conversion_map[GetRailType(t)]); diff --git a/src/saveload/town_sl.cpp b/src/saveload/town_sl.cpp index 0f2f5be9aa..9299330cc4 100644 --- a/src/saveload/town_sl.cpp +++ b/src/saveload/town_sl.cpp @@ -35,7 +35,7 @@ void RebuildTownCaches() town->cache.num_houses = 0; } - for (TileIndex t = 0; t < Map::Size(); t++) { + for (const auto t : Map::Iterate()) { if (!IsTileType(t, MP_HOUSE)) continue; HouseID house_id = GetHouseType(t); @@ -63,7 +63,7 @@ void RebuildTownCaches() */ void UpdateHousesAndTowns() { - for (TileIndex t = 0; t < Map::Size(); t++) { + for (const auto t : Map::Iterate()) { if (!IsTileType(t, MP_HOUSE)) continue; HouseID house_id = GetCleanHouseType(t); @@ -76,7 +76,7 @@ void UpdateHousesAndTowns() } /* Check for cases when a NewGRF has set a wrong house substitute type. */ - for (TileIndex t = 0; t < Map::Size(); t++) { + for (const auto t : Map::Iterate()) { if (!IsTileType(t, MP_HOUSE)) continue; HouseID house_type = GetCleanHouseType(t); diff --git a/src/screenshot.cpp b/src/screenshot.cpp index a5641fc5e9..099257f116 100644 --- a/src/screenshot.cpp +++ b/src/screenshot.cpp @@ -819,7 +819,7 @@ bool MakeHeightmapScreenshot(const char *filename) } _heightmap_highest_peak = 0; - for (TileIndex tile = 0; tile < Map::Size(); tile++) { + for (const auto tile : Map::Iterate()) { uint h = TileHeight(tile); _heightmap_highest_peak = std::max(h, _heightmap_highest_peak); } diff --git a/src/settings_table.cpp b/src/settings_table.cpp index 9e06fe4e12..2ebb856d41 100644 --- a/src/settings_table.cpp +++ b/src/settings_table.cpp @@ -533,7 +533,7 @@ static bool CheckMaxHeightLevel(int32_t &new_value) /* Check if at least one mountain on the map is higher than the new value. * If yes, disallow the change. */ - for (TileIndex t = 0; t < Map::Size(); t++) { + for (const auto t : Map::Iterate()) { if ((int32_t)TileHeight(t) > new_value) { ShowErrorMessage(STR_CONFIG_SETTING_TOO_HIGH_MOUNTAIN, INVALID_STRING_ID, WL_ERROR); /* Return old, unchanged value */ diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 1042f3de18..dc9a17e7cc 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -128,7 +128,7 @@ Town::~Town() #endif /* WITH_ASSERT */ /* Check no tile is related to us. */ - for (TileIndex tile = 0; tile < Map::Size(); ++tile) { + for (const auto tile : Map::Iterate()) { switch (GetTileType(tile)) { case MP_HOUSE: assert(GetTownIndex(tile) != this->index); @@ -3248,7 +3248,7 @@ CommandCost CmdDeleteTown(DoCommandFlag flags, TownID town_id) * these do not directly have an owner so we need to check adjacent * tiles. This won't work correctly in the same loop if the adjacent * tile was already deleted earlier in the loop. */ - for (TileIndex current_tile = 0; current_tile < Map::Size(); ++current_tile) { + for (const auto current_tile : Map::Iterate()) { if (IsTileType(current_tile, MP_TUNNELBRIDGE) && TestTownOwnsBridge(current_tile, t)) { CommandCost ret = Command::Do(flags, current_tile); if (ret.Failed()) return ret; @@ -3256,7 +3256,7 @@ CommandCost CmdDeleteTown(DoCommandFlag flags, TownID town_id) } /* Check all remaining tiles for town ownership. */ - for (TileIndex current_tile = 0; current_tile < Map::Size(); ++current_tile) { + for (const auto current_tile : Map::Iterate()) { bool try_clear = false; switch (GetTileType(current_tile)) { case MP_ROAD: @@ -4071,7 +4071,7 @@ static IntervalTimer _economy_towns_monthly({TimerGameEconomy: static IntervalTimer _economy_towns_yearly({TimerGameEconomy::YEAR, TimerGameEconomy::Priority::TOWN}, [](auto) { /* Increment house ages */ - for (TileIndex t = 0; t < Map::Size(); t++) { + for (const auto t : Map::Iterate()) { if (!IsTileType(t, MP_HOUSE)) continue; IncrementHouseAge(t); } diff --git a/src/water_cmd.cpp b/src/water_cmd.cpp index 88a8fc9360..318f56a076 100644 --- a/src/water_cmd.cpp +++ b/src/water_cmd.cpp @@ -1299,7 +1299,7 @@ void TileLoop_Water(TileIndex tile) void ConvertGroundTilesIntoWaterTiles() { - for (TileIndex tile = 0; tile < Map::Size(); ++tile) { + for (const auto tile : Map::Iterate()) { auto [slope, z] = GetTileSlopeZ(tile); if (IsTileType(tile, MP_CLEAR) && z == 0) { /* Make both water for tiles at level 0