From a3ee8389da4120250b9bccc559fa955cb9df3751 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 19 Jan 2025 15:18:47 +0100 Subject: [PATCH] Codechange: make TownID an enum --- src/cargomonitor.cpp | 2 +- src/cargomonitor.h | 2 +- src/saveload/afterload.cpp | 4 ++-- src/script/api/script_goal.cpp | 2 +- src/script/api/script_news.cpp | 2 +- src/script/api/script_subsidy.cpp | 4 ++-- src/town.h | 2 +- src/town_gui.cpp | 8 ++++---- src/town_map.h | 2 +- src/town_type.h | 7 +++++-- 10 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/cargomonitor.cpp b/src/cargomonitor.cpp index 5f203af62d..28dc581bc9 100644 --- a/src/cargomonitor.cpp +++ b/src/cargomonitor.cpp @@ -128,7 +128,7 @@ void AddCargoDelivery(CargoType cargo_type, CompanyID company, uint32_t amount, break; } case SourceType::Town: { - CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, src.id); + CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, static_cast(src.id)); CargoMonitorMap::iterator iter = _cargo_pickups.find(num); if (iter != _cargo_pickups.end()) iter->second += amount; break; diff --git a/src/cargomonitor.h b/src/cargomonitor.h index a5396ce43d..86988242d0 100644 --- a/src/cargomonitor.h +++ b/src/cargomonitor.h @@ -136,7 +136,7 @@ inline IndustryID DecodeMonitorIndustry(CargoMonitorID num) inline TownID DecodeMonitorTown(CargoMonitorID num) { if (MonitorMonitorsIndustry(num)) return INVALID_TOWN; - return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH); + return static_cast(GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH)); } void ClearCargoPickupMonitoring(CompanyID company = INVALID_OWNER); diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 2e6482c727..c8e66ff7e0 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -1008,7 +1008,7 @@ bool AfterLoadGame() if ((GB(t.m5(), 4, 2) == ROAD_TILE_CROSSING ? (Owner)t.m3() : GetTileOwner(t)) == OWNER_TOWN) { SetTownIndex(t, CalcClosestTownFromTile(t)->index); } else { - SetTownIndex(t, 0); + SetTownIndex(t, TOWN_BEGIN); } break; @@ -1232,7 +1232,7 @@ bool AfterLoadGame() GetRailType(t) ); } else { - TownID town = IsTileOwner(t, OWNER_TOWN) ? ClosestTownFromTile(t, UINT_MAX)->index : 0; + TownID town = IsTileOwner(t, OWNER_TOWN) ? ClosestTownFromTile(t, UINT_MAX)->index : TOWN_BEGIN; /* MakeRoadNormal */ SetTileType(t, MP_ROAD); diff --git a/src/script/api/script_goal.cpp b/src/script/api/script_goal.cpp index 68ba7d3361..767ccacdf7 100644 --- a/src/script/api/script_goal.cpp +++ b/src/script/api/script_goal.cpp @@ -37,7 +37,7 @@ return (type == GT_NONE && destination == 0) || (type == GT_TILE && ScriptMap::IsValidTile(::TileIndex(destination))) || (type == GT_INDUSTRY && ScriptIndustry::IsValidIndustry(static_cast(destination))) || - (type == GT_TOWN && ScriptTown::IsValidTown(destination)) || + (type == GT_TOWN && ScriptTown::IsValidTown(static_cast(destination))) || (type == GT_COMPANY && ScriptCompany::ResolveCompanyID((ScriptCompany::CompanyID)destination) != ScriptCompany::COMPANY_INVALID) || (type == GT_STORY_PAGE && story_page != nullptr && (c == INVALID_COMPANY ? story_page->company == INVALID_COMPANY : story_page->company == INVALID_COMPANY || story_page->company == c)); } diff --git a/src/script/api/script_news.cpp b/src/script/api/script_news.cpp index 07f910c7a2..94f51e83b2 100644 --- a/src/script/api/script_news.cpp +++ b/src/script/api/script_news.cpp @@ -34,7 +34,7 @@ (ref_type == NR_TILE && ScriptMap::IsValidTile(::TileIndex(reference))) || (ref_type == NR_STATION && ScriptStation::IsValidStation(reference)) || (ref_type == NR_INDUSTRY && ScriptIndustry::IsValidIndustry(static_cast(reference))) || - (ref_type == NR_TOWN && ScriptTown::IsValidTown(reference))); + (ref_type == NR_TOWN && ScriptTown::IsValidTown(static_cast(reference)))); uint8_t c = company; if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY; diff --git a/src/script/api/script_subsidy.cpp b/src/script/api/script_subsidy.cpp index 3e8db2e5e8..1a118ebdb1 100644 --- a/src/script/api/script_subsidy.cpp +++ b/src/script/api/script_subsidy.cpp @@ -37,8 +37,8 @@ EnforcePrecondition(false, ScriptCargo::IsValidCargo(cargo_type)); EnforcePrecondition(false, from_type == SPT_INDUSTRY || from_type == SPT_TOWN); EnforcePrecondition(false, to_type == SPT_INDUSTRY || to_type == SPT_TOWN); - EnforcePrecondition(false, (from_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(static_cast(from_id))) || (from_type == SPT_TOWN && ScriptTown::IsValidTown(from_id))); - EnforcePrecondition(false, (to_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(static_cast(to_id))) || (to_type == SPT_TOWN && ScriptTown::IsValidTown(to_id))); + EnforcePrecondition(false, (from_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(static_cast(from_id))) || (from_type == SPT_TOWN && ScriptTown::IsValidTown(static_cast(from_id)))); + EnforcePrecondition(false, (to_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(static_cast(to_id))) || (to_type == SPT_TOWN && ScriptTown::IsValidTown(static_cast(to_id)))); Source from{static_cast(from_id), static_cast(from_type)}; Source to{static_cast(to_id), static_cast(to_type)}; diff --git a/src/town.h b/src/town.h index 8a08cc12ff..d53a02f18d 100644 --- a/src/town.h +++ b/src/town.h @@ -33,7 +33,7 @@ static const uint TOWN_GROWTH_DESERT = 0xFFFFFFFF; ///< The town needs the cargo static const uint16_t TOWN_GROWTH_RATE_NONE = 0xFFFF; ///< Special value for Town::growth_rate to disable town growth. static const uint16_t MAX_TOWN_GROWTH_TICKS = 930; ///< Max amount of original town ticks that still fit into uint16_t, about equal to UINT16_MAX / TOWN_GROWTH_TICKS but slightly less to simplify calculations -typedef Pool TownPool; +typedef Pool TownPool; extern TownPool _town_pool; /** Data structure with cached data of towns. */ diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 0d961bba41..1a9c471702 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -326,7 +326,7 @@ public: } case WID_TA_EXECUTE: - Command::Post(STR_ERROR_CAN_T_DO_THIS, this->town->xy, this->window_number, this->sel_index); + Command::Post(STR_ERROR_CAN_T_DO_THIS, this->town->xy, static_cast(this->window_number), this->sel_index); break; } } @@ -521,12 +521,12 @@ public: break; case WID_TV_EXPAND: { // expand town - only available on Scenario editor - Command::Post(STR_ERROR_CAN_T_EXPAND_TOWN, this->window_number, 0); + Command::Post(STR_ERROR_CAN_T_EXPAND_TOWN, static_cast(this->window_number), 0); break; } case WID_TV_DELETE: // delete town - only available on Scenario editor - Command::Post(STR_ERROR_TOWN_CAN_T_DELETE, this->window_number); + Command::Post(STR_ERROR_TOWN_CAN_T_DELETE, static_cast(this->window_number)); break; } } @@ -615,7 +615,7 @@ public: { if (!str.has_value()) return; - Command::Post(STR_ERROR_CAN_T_RENAME_TOWN, this->window_number, *str); + Command::Post(STR_ERROR_CAN_T_RENAME_TOWN, static_cast(this->window_number), *str); } IntervalTimer daily_interval = {{TimerGameCalendar::DAY, TimerGameCalendar::Priority::NONE}, [this](auto) { diff --git a/src/town_map.h b/src/town_map.h index 6cd041643a..19f3bc15cc 100644 --- a/src/town_map.h +++ b/src/town_map.h @@ -23,7 +23,7 @@ inline TownID GetTownIndex(Tile t) { assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t))); - return t.m2(); + return static_cast(t.m2()); } /** diff --git a/src/town_type.h b/src/town_type.h index eb20722645..33f6617734 100644 --- a/src/town_type.h +++ b/src/town_type.h @@ -12,8 +12,11 @@ #include "core/enum_type.hpp" -typedef uint16_t TownID; -static const TownID INVALID_TOWN = 0xFFFF; +enum TownID : uint16_t { + TOWN_BEGIN = 0, + TOWN_END = 64000, + INVALID_TOWN = 0xFFFF +}; struct Town;