From 8a18f05d308789e25ade0062aff57084eb5eaa3b 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 5d3475ee80..e450294d9d 100644 --- a/src/cheat_gui.cpp +++ b/src/cheat_gui.cpp @@ -145,7 +145,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 45c6569d90..65d096fa49 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 411eb514d8..15534c21fe 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 d64b8b54f2..d266d9ff90 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 5df9556bba..6a1df51ad6 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -656,7 +656,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); } @@ -927,7 +927,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); @@ -1339,7 +1339,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)); @@ -1468,7 +1468,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); @@ -1690,7 +1690,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 && @@ -1860,7 +1860,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); @@ -1869,7 +1869,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)) { @@ -2391,7 +2391,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; @@ -2430,7 +2430,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); } @@ -2510,7 +2510,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); @@ -2867,7 +2867,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); @@ -2894,7 +2894,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)); } } @@ -2910,7 +2910,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); } @@ -3128,14 +3128,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)) { @@ -3157,7 +3157,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); } @@ -3248,7 +3248,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 dce806121e..2ef8d2e805 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 4800fcd702..0274558378 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); @@ -3221,7 +3221,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; @@ -3229,7 +3229,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: @@ -4044,7 +4044,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