From fddcaef74a623a820017172860c5ad58c14e80ab Mon Sep 17 00:00:00 2001 From: SamuXarick <43006711+SamuXarick@users.noreply.github.com> Date: Mon, 11 Dec 2023 17:04:41 +0000 Subject: [PATCH] Codechange: Use town zone constants instead of magic numbers --- src/newgrf_town.cpp | 20 ++++++++++---------- src/road_cmd.cpp | 4 ++++ src/script/api/script_town.cpp | 2 +- src/town_cmd.cpp | 16 ++++++++-------- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/newgrf_town.cpp b/src/newgrf_town.cpp index 83eda4204c..5565551ee0 100644 --- a/src/newgrf_town.cpp +++ b/src/newgrf_town.cpp @@ -51,16 +51,16 @@ case 0x8A: return this->t->grow_counter / Ticks::TOWN_GROWTH_TICKS; case 0x92: return this->t->flags; // In original game, 0x92 and 0x93 are really one word. Since flags is a byte, this is to adjust case 0x93: return 0; - case 0x94: return ClampTo(this->t->cache.squared_town_zone_radius[0]); - case 0x95: return GB(ClampTo(this->t->cache.squared_town_zone_radius[0]), 8, 8); - case 0x96: return ClampTo(this->t->cache.squared_town_zone_radius[1]); - case 0x97: return GB(ClampTo(this->t->cache.squared_town_zone_radius[1]), 8, 8); - case 0x98: return ClampTo(this->t->cache.squared_town_zone_radius[2]); - case 0x99: return GB(ClampTo(this->t->cache.squared_town_zone_radius[2]), 8, 8); - case 0x9A: return ClampTo(this->t->cache.squared_town_zone_radius[3]); - case 0x9B: return GB(ClampTo(this->t->cache.squared_town_zone_radius[3]), 8, 8); - case 0x9C: return ClampTo(this->t->cache.squared_town_zone_radius[4]); - case 0x9D: return GB(ClampTo(this->t->cache.squared_town_zone_radius[4]), 8, 8); + case 0x94: return ClampTo(this->t->cache.squared_town_zone_radius[HZB_TOWN_EDGE]); + case 0x95: return GB(ClampTo(this->t->cache.squared_town_zone_radius[HZB_TOWN_EDGE]), 8, 8); + case 0x96: return ClampTo(this->t->cache.squared_town_zone_radius[HZB_TOWN_OUTSKIRT]); + case 0x97: return GB(ClampTo(this->t->cache.squared_town_zone_radius[HZB_TOWN_OUTSKIRT]), 8, 8); + case 0x98: return ClampTo(this->t->cache.squared_town_zone_radius[HZB_TOWN_OUTER_SUBURB]); + case 0x99: return GB(ClampTo(this->t->cache.squared_town_zone_radius[HZB_TOWN_OUTER_SUBURB]), 8, 8); + case 0x9A: return ClampTo(this->t->cache.squared_town_zone_radius[HZB_TOWN_INNER_SUBURB]); + case 0x9B: return GB(ClampTo(this->t->cache.squared_town_zone_radius[HZB_TOWN_INNER_SUBURB]), 8, 8); + case 0x9C: return ClampTo(this->t->cache.squared_town_zone_radius[HZB_TOWN_CENTRE]); + case 0x9D: return GB(ClampTo(this->t->cache.squared_town_zone_radius[HZB_TOWN_CENTRE]), 8, 8); case 0x9E: return this->t->ratings[0]; case 0x9F: return GB(this->t->ratings[0], 8, 8); case 0xA0: return this->t->ratings[1]; diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index a71d2005e7..53cc8e2278 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -1937,6 +1937,8 @@ static const Roadside _town_road_types[][2] = { { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED } }; +static_assert(lengthof(_town_road_types) == HZB_END); + static const Roadside _town_road_types_2[][2] = { { ROADSIDE_GRASS, ROADSIDE_GRASS }, { ROADSIDE_PAVED, ROADSIDE_PAVED }, @@ -1945,6 +1947,8 @@ static const Roadside _town_road_types_2[][2] = { { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED } }; +static_assert(lengthof(_town_road_types_2) == HZB_END); + static void TileLoop_Road(TileIndex tile) { diff --git a/src/script/api/script_town.cpp b/src/script/api/script_town.cpp index b884374b6c..57e94db7de 100644 --- a/src/script/api/script_town.cpp +++ b/src/script/api/script_town.cpp @@ -206,7 +206,7 @@ if (!IsValidTown(town_id)) return false; const Town *t = ::Town::Get(town_id); - return ((uint32_t)GetDistanceSquareToTile(town_id, tile) <= t->cache.squared_town_zone_radius[0]); + return ((uint32_t)GetDistanceSquareToTile(town_id, tile) <= t->cache.squared_town_zone_radius[HZB_TOWN_EDGE]); } /* static */ bool ScriptTown::HasStatue(TownID town_id) diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 48fbf03940..8a40d3d457 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -1819,7 +1819,7 @@ static bool GrowTown(Town *t) */ void UpdateTownRadius(Town *t) { - static const uint32_t _town_squared_town_zone_radius_data[23][5] = { + static const uint32_t _town_squared_town_zone_radius_data[23][HZB_END] = { { 4, 0, 0, 0, 0}, // 0 { 16, 0, 0, 0, 0}, { 25, 0, 0, 0, 0}, @@ -1852,11 +1852,11 @@ void UpdateTownRadius(Town *t) /* Actually we are proportional to sqrt() but that's right because we are covering an area. * The offsets are to make sure the radii do not decrease in size when going from the table * to the calculated value.*/ - t->cache.squared_town_zone_radius[0] = mass * 15 - 40; - t->cache.squared_town_zone_radius[1] = mass * 9 - 15; - t->cache.squared_town_zone_radius[2] = 0; - t->cache.squared_town_zone_radius[3] = mass * 5 - 5; - t->cache.squared_town_zone_radius[4] = mass * 3 + 5; + t->cache.squared_town_zone_radius[HZB_TOWN_EDGE] = mass * 15 - 40; + t->cache.squared_town_zone_radius[HZB_TOWN_OUTSKIRT] = mass * 9 - 15; + t->cache.squared_town_zone_radius[HZB_TOWN_OUTER_SUBURB] = 0; + t->cache.squared_town_zone_radius[HZB_TOWN_INNER_SUBURB] = mass * 5 - 5; + t->cache.squared_town_zone_radius[HZB_TOWN_CENTRE] = mass * 3 + 5; } } @@ -3482,9 +3482,9 @@ static void ForAllStationsNearTown(Town *t, Func func) * The true radius is not stored or calculated anywhere, only the squared radius. */ /* The efficiency of this search might be improved for large towns and many stations on the map, * by using an integer square root approximation giving a value not less than the true square root. */ - uint search_radius = t->cache.squared_town_zone_radius[0] / 2; + uint search_radius = t->cache.squared_town_zone_radius[HZB_TOWN_EDGE] / 2; ForAllStationsRadius(t->xy, search_radius, [&](const Station * st) { - if (DistanceSquare(st->xy, t->xy) <= t->cache.squared_town_zone_radius[0]) { + if (DistanceSquare(st->xy, t->xy) <= t->cache.squared_town_zone_radius[HZB_TOWN_EDGE]) { func(st); } });