From 76cc3438bf394dbd733d370813d04f9810ccd090 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 4 Jan 2025 11:11:55 +0100 Subject: [PATCH] Codechange: make GetSpecialNameString less magic-number heavy --- src/strings.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/strings.cpp b/src/strings.cpp index 3bdbed12bb..5f2e9ed15b 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -191,7 +191,7 @@ bool HaveDParamChanged(const std::span backup) static void StationGetSpecialString(StringBuilder &builder, StationFacility x); static void GetSpecialTownNameString(StringBuilder &builder, int ind, uint32_t seed); -static void GetSpecialNameString(StringBuilder &builder, int ind, StringParameters &args); +static bool GetSpecialNameString(StringBuilder &builder, StringID string, StringParameters &args); static void FormatString(StringBuilder &builder, const char *str, StringParameters &args, uint case_index = 0, bool game_script = false, bool dry_run = false); @@ -278,14 +278,14 @@ void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters break; case TEXT_TAB_SPECIAL: - if (index >= 0xE4 && !game_script) { + if (!game_script) { try { - GetSpecialNameString(builder, index.base() - 0xE4, args); + if (GetSpecialNameString(builder, string, args)) return; } catch (const std::runtime_error &e) { Debug(misc, 0, "GetStringWithArgs: {}", e.what()); builder += "(invalid string parameter)"; + return; } - return; } break; @@ -1873,30 +1873,30 @@ static void GenPresidentName(StringBuilder &builder, uint32_t seed) builder += GetSurname(seed); } -static void GetSpecialNameString(StringBuilder &builder, int ind, StringParameters &args) +static bool GetSpecialNameString(StringBuilder &builder, StringID string, StringParameters &args) { - switch (ind) { - case 1: // not used + switch (string) { + case SPECSTR_SILLY_NAME: // Not used in new companies, but retained for old-loader savegames builder += _silly_company_names[std::min(args.GetNextParameter(), std::size(_silly_company_names) - 1)]; - return; + return true; - case 2: // used for Foobar & Co company names + case SPECSTR_ANDCO_NAME: // used for Foobar & Co company names GenAndCoName(builder, args.GetNextParameter()); - return; + return true; - case 3: // President name + case SPECSTR_PRESIDENT_NAME: // President name GenPresidentName(builder, args.GetNextParameter()); - return; + return true; } - /* town name? */ - if (IsInsideMM(ind - 6, 0, SPECSTR_TOWNNAME_END - SPECSTR_TOWNNAME_START)) { - GetSpecialTownNameString(builder, ind - 6, args.GetNextParameter()); + /* TownName Transport company names, with the appropriate town name. */ + if (IsInsideMM(string, SPECSTR_COMPANY_NAME_START, SPECSTR_COMPANY_NAME_END)) { + GetSpecialTownNameString(builder, string - SPECSTR_COMPANY_NAME_START, args.GetNextParameter()); builder += " Transport"; - return; + return true; } - NOT_REACHED(); + return false; } /**