summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2021-01-12 22:11:12 +0100
committerfrosch <github@elsenhans.name>2021-02-14 23:14:07 +0100
commit4ce941bbc2ac7f2d40d46909af4a3f75fe24ecef (patch)
treeaa11f2f738ee1b7ff3be8b91d01c691e87798290
parentd17226910d319cb552edc5f43c4c4315109dbe04 (diff)
downloadopenttd-4ce941bbc2ac7f2d40d46909af4a3f75fe24ecef.tar.xz
Codechange: turn a constant variable into a real constant.
-rw-r--r--src/settings_gui.cpp10
-rw-r--r--src/town_cmd.cpp13
-rw-r--r--src/townname_type.h10
3 files changed, 15 insertions, 18 deletions
diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp
index 6abc4260f..6f1901284 100644
--- a/src/settings_gui.cpp
+++ b/src/settings_gui.cpp
@@ -17,6 +17,7 @@
#include "town.h"
#include "settings_internal.h"
#include "newgrf_townname.h"
+#include "townname_type.h"
#include "strings_func.h"
#include "window_func.h"
#include "string_func.h"
@@ -73,7 +74,6 @@ static const StringID _font_zoom_dropdown[] = {
INVALID_STRING_ID,
};
-int _nb_orig_names = SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1; ///< Number of original town names.
static StringID *_grf_names = nullptr; ///< Pointer to town names defined by NewGRFs.
static int _nb_grf_names = 0; ///< Number of town names defined by NewGRFs.
@@ -97,8 +97,8 @@ void InitGRFTownGeneratorNames()
*/
static inline StringID TownName(int town_name)
{
- if (town_name < _nb_orig_names) return STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + town_name;
- town_name -= _nb_orig_names;
+ if (town_name < BUILTIN_TOWNNAME_GENERATOR_COUNT) return STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + town_name;
+ town_name -= BUILTIN_TOWNNAME_GENERATOR_COUNT;
if (town_name < _nb_grf_names) return _grf_names[town_name];
return STR_UNDEFINED;
}
@@ -245,7 +245,7 @@ struct GameOptionsWindow : Window {
/* Add and sort newgrf townnames generators */
for (int i = 0; i < _nb_grf_names; i++) {
- int result = _nb_orig_names + i;
+ int result = BUILTIN_TOWNNAME_GENERATOR_COUNT + i;
list.emplace_back(new DropDownListStringItem(_grf_names[i], result, enabled_item != result && enabled_item >= 0));
}
std::sort(list.begin(), list.end(), DropDownListStringItem::NatSortFunc);
@@ -258,7 +258,7 @@ struct GameOptionsWindow : Window {
}
/* Add and sort original townnames generators */
- for (int i = 0; i < _nb_orig_names; i++) {
+ for (int i = 0; i < BUILTIN_TOWNNAME_GENERATOR_COUNT; i++) {
list.emplace_back(new DropDownListStringItem(STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + i, i, enabled_item != i && enabled_item >= 0));
}
std::sort(list.begin() + newgrf_size, list.end(), DropDownListStringItem::NatSortFunc);
diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp
index c2d148a1a..a37c36ed7 100644
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -1841,15 +1841,10 @@ static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSize
t->exclusive_counter = 0;
t->statues = 0;
- extern int _nb_orig_names;
- if (_settings_game.game_creation.town_name < _nb_orig_names) {
- /* Original town name */
- t->townnamegrfid = 0;
- t->townnametype = SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name;
- } else {
- /* Newgrf town name */
- t->townnamegrfid = GetGRFTownNameId(_settings_game.game_creation.town_name - _nb_orig_names);
- t->townnametype = GetGRFTownNameType(_settings_game.game_creation.town_name - _nb_orig_names);
+ {
+ TownNameParams tnp(_settings_game.game_creation.town_name);
+ t->townnamegrfid = tnp.grfid;
+ t->townnametype = tnp.type;
}
t->townnameparts = townnameparts;
diff --git a/src/townname_type.h b/src/townname_type.h
index dd95f8f70..2dc23567f 100644
--- a/src/townname_type.h
+++ b/src/townname_type.h
@@ -15,11 +15,14 @@
#include "newgrf_townname.h"
#include "town_type.h"
+#include "string_type.h"
#include <set>
#include <string>
typedef std::set<std::string> TownNames;
+static constexpr uint BUILTIN_TOWNNAME_GENERATOR_COUNT = SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1; ///< Number of built-in town name generators.
+
/**
* Struct holding parameters used to generate town name.
* Speeds things up a bit because these values are computed only once per name generation.
@@ -34,10 +37,9 @@ struct TownNameParams {
*/
TownNameParams(byte town_name)
{
- extern int _nb_orig_names;
- bool grf = town_name >= _nb_orig_names;
- this->grfid = grf ? GetGRFTownNameId(town_name - _nb_orig_names) : 0;
- this->type = grf ? GetGRFTownNameType(town_name - _nb_orig_names) : SPECSTR_TOWNNAME_START + town_name;
+ bool grf = town_name >= BUILTIN_TOWNNAME_GENERATOR_COUNT;
+ this->grfid = grf ? GetGRFTownNameId(town_name - BUILTIN_TOWNNAME_GENERATOR_COUNT) : 0;
+ this->type = grf ? GetGRFTownNameType(town_name - BUILTIN_TOWNNAME_GENERATOR_COUNT) : SPECSTR_TOWNNAME_START + town_name;
}
TownNameParams(const Town *t);