From cd4482b5df3e6d24124a244fcf3d34096733f330 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.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 +++++-- 9 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/cargomonitor.h b/src/cargomonitor.h index 82dbccb803..3c95b84e9d 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 4374b5d4ca..a38ad58ab3 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 8de5466bb8..48f091f1e9 100644 --- a/src/script/api/script_goal.cpp +++ b/src/script/api/script_goal.cpp @@ -36,7 +36,7 @@ return (type == GT_NONE && destination == 0) || (type == GT_TILE && ScriptMap::IsValidTile(::TileIndex(destination))) || (type == GT_INDUSTRY && ScriptIndustry::IsValidIndustry(destination)) || - (type == GT_TOWN && ScriptTown::IsValidTown(destination)) || + (type == GT_TOWN && ScriptTown::IsValidTown(static_cast(destination))) || (type == GT_COMPANY && ScriptCompany::ResolveCompanyID(ScriptCompany::ToScriptCompanyID(static_cast<::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 efe67effd7..aaf7b691b1 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(reference)) || - (ref_type == NR_TOWN && ScriptTown::IsValidTown(reference))); + (ref_type == NR_TOWN && ScriptTown::IsValidTown(static_cast(reference)))); ::CompanyID c = ScriptCompany::FromScriptCompanyID(company); diff --git a/src/script/api/script_subsidy.cpp b/src/script/api/script_subsidy.cpp index 9d2ba601b2..7c640a4534 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(from_id)) || (from_type == SPT_TOWN && ScriptTown::IsValidTown(from_id))); - EnforcePrecondition(false, (to_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(to_id)) || (to_type == SPT_TOWN && ScriptTown::IsValidTown(to_id))); + EnforcePrecondition(false, (from_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(from_id)) || (from_type == SPT_TOWN && ScriptTown::IsValidTown(static_cast(from_id)))); + EnforcePrecondition(false, (to_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(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;