18#include "strings_internal.h"
30 grfid(t->townnamegrfid),
33 if (t->townnamegrfid != 0 && GetGRFTownName(t->townnamegrfid) ==
nullptr) {
36 this->
type = SPECSTR_TOWNNAME_ENGLISH;
50 if (par->
grfid == 0) {
51 auto tmp_params = MakeParameters(townnameparts);
56 GRFTownNameGenerate(builder, par->
grfid, par->
type, townnameparts);
110 if (town_names !=
nullptr) {
111 if (town_names->find(name) != town_names->end())
return false;
112 town_names->insert(name);
117 if (t->name.empty()) {
120 if (name == t->name)
return false;
149 for (
int i = 1000; i != 0; i--) {
150 uint32_t r = randomizer.
Next();
169static inline uint32_t
SeedChance(uint8_t shift_by,
size_t max, uint32_t seed)
171 return (
GB(seed, shift_by, 16) * ClampTo<uint16_t>(max)) >> 16;
182static inline uint32_t
SeedModChance(uint8_t shift_by,
size_t max, uint32_t seed)
193 return (seed >> shift_by) % max;
205static inline int32_t
SeedChanceBias(uint8_t shift_by,
size_t max, uint32_t seed,
int bias)
207 return SeedChance(shift_by, max + bias, seed) - bias;
220 assert(strlen(org) == 4 && strlen(rep) == 4 && builder.
CurrentIndex() - start >= 4);
221 if (strncmp(&builder[start], org, 4) == 0) memcpy(&builder[start], rep, 4);
236 if (!original)
ReplaceWords(
"Fart",
"Boot", builder, start);
242 ReplaceWords(
"Wrar", original ?
"Inve" :
"Stan", builder, start);
255 int i =
SeedChanceBias(0, std::size(_name_original_english_1), seed, 50);
256 if (i >= 0) builder += _name_original_english_1[i];
259 builder += _name_original_english_2[
SeedChance(4, std::size(_name_original_english_2), seed)];
260 builder += _name_original_english_3[
SeedChance(7, std::size(_name_original_english_3), seed)];
261 builder += _name_original_english_4[
SeedChance(10, std::size(_name_original_english_4), seed)];
262 builder += _name_original_english_5[
SeedChance(13, std::size(_name_original_english_5), seed)];
265 i =
SeedChanceBias(15, std::size(_name_original_english_6), seed, 60);
266 if (i >= 0) builder += _name_original_english_6[i];
269 if (builder[start] ==
'C' && (builder[start + 1] ==
'e' || builder[start + 1] ==
'i')) {
270 builder[start] =
'K';
288 int i =
SeedChanceBias(0, std::size(_name_additional_english_prefix), seed, 50);
289 if (i >= 0) builder += _name_additional_english_prefix[i];
292 builder += _name_additional_english_1a[
SeedChance(6, std::size(_name_additional_english_1a), seed)];
294 builder += _name_additional_english_1b1[
SeedChance(6, std::size(_name_additional_english_1b1), seed)];
295 builder += _name_additional_english_1b2[
SeedChance(9, std::size(_name_additional_english_1b2), seed)];
297 builder += _name_additional_english_1b3a[
SeedChance(12, std::size(_name_additional_english_1b3a), seed)];
299 builder += _name_additional_english_1b3b[
SeedChance(12, std::size(_name_additional_english_1b3b), seed)];
303 builder += _name_additional_english_2[
SeedChance(14, std::size(_name_additional_english_2), seed)];
306 i =
SeedChanceBias(15, std::size(_name_additional_english_3), seed, 60);
307 if (i >= 0) builder += _name_additional_english_3[i];
322 int i =
SeedChanceBias(0, std::size(_name_austrian_a1), seed, 15);
323 if (i >= 0) builder += _name_austrian_a1[i];
330 builder += _name_austrian_a2[
SeedChance( 7, std::size(_name_austrian_a2), seed)];
331 builder += _name_austrian_a3[
SeedChance(13, std::size(_name_austrian_a3), seed)];
334 builder += _name_austrian_a5[
SeedChance( 7, std::size(_name_austrian_a5), seed)];
335 builder += _name_austrian_a6[
SeedChance( 9, std::size(_name_austrian_a6), seed)];
339 builder += _name_austrian_a4[
SeedChance( 7, std::size(_name_austrian_a4), seed)];
345 builder += _name_austrian_f1[
SeedChance(4, std::size(_name_austrian_f1), seed)];
346 builder += _name_austrian_f2[
SeedChance(5, std::size(_name_austrian_f2), seed)];
347 }
else if (i >= 2 - j) {
349 builder += _name_austrian_b1[
SeedChance(4, std::size(_name_austrian_b1), seed)];
350 builder += _name_austrian_b2[
SeedChance(5, std::size(_name_austrian_b2), seed)];
362 uint seed_derivative =
SeedChance(7, 28, seed);
365 if (seed_derivative == 12 || seed_derivative == 19) {
366 uint i =
SeedChance(2, std::size(_name_german_pre), seed);
367 builder += _name_german_pre[i];
371 uint i =
SeedChance(3, std::size(_name_german_real) + std::size(_name_german_1), seed);
372 if (i < std::size(_name_german_real)) {
373 builder += _name_german_real[i];
375 builder += _name_german_1[i - std::size(_name_german_real)];
377 i =
SeedChance(5, std::size(_name_german_2), seed);
378 builder += _name_german_2[i];
382 if (seed_derivative == 24) {
383 i =
SeedChance(9, std::size(_name_german_4_an_der) + std::size(_name_german_4_am), seed);
384 if (i < std::size(_name_german_4_an_der)) {
385 builder += _name_german_3_an_der[0];
386 builder += _name_german_4_an_der[i];
388 builder += _name_german_3_am[0];
389 builder += _name_german_4_am[i - std::size(_name_german_4_an_der)];
402 builder += _name_spanish_real[
SeedChance(0, std::size(_name_spanish_real), seed)];
413 builder += _name_french_real[
SeedChance(0, std::size(_name_french_real), seed)];
424 builder += _name_silly_1[
SeedChance( 0, std::size(_name_silly_1), seed)];
425 builder += _name_silly_2[
SeedChance(16, std::size(_name_silly_2), seed)];
438 if (i >= 0) builder += _name_swedish_1[i];
442 builder += _name_swedish_2[
SeedChance( 7, std::size(_name_swedish_2), seed)];
444 builder += _name_swedish_2a[
SeedChance( 7, std::size(_name_swedish_2a), seed)];
445 builder += _name_swedish_2b[
SeedChance(10, std::size(_name_swedish_2b), seed)];
446 builder += _name_swedish_2c[
SeedChance(13, std::size(_name_swedish_2c), seed)];
449 builder += _name_swedish_3[
SeedChance(16, std::size(_name_swedish_3), seed)];
462 if (i >= 0) builder += _name_dutch_1[i];
466 builder += _name_dutch_2[
SeedChance( 9, std::size(_name_dutch_2), seed)];
468 builder += _name_dutch_3[
SeedChance( 9, std::size(_name_dutch_3), seed)];
469 builder += _name_dutch_4[
SeedChance(12, std::size(_name_dutch_4), seed)];
472 builder += _name_dutch_5[
SeedChance(15, std::size(_name_dutch_5), seed)];
487 builder += _name_finnish_real[
SeedChance(2, std::size(_name_finnish_real), seed)];
495 uint sel =
SeedChance( 0, std::size(_name_finnish_1), seed);
496 builder += _name_finnish_1[sel];
498 if (builder[last] ==
'i') builder[last] =
'e';
500 std::string_view view(&builder[start], builder.
CurrentIndex() - start);
501 if (view.find_first_of(
"aouAOU") != std::string_view::npos) {
504 builder +=
"l\u00e4";
511 uint sel =
SeedChance(2, std::size(_name_finnish_1) + std::size(_name_finnish_2), seed);
512 if (sel >= std::size(_name_finnish_1)) {
513 builder += _name_finnish_2[sel - std::size(_name_finnish_1)];
515 builder += _name_finnish_1[sel];
518 builder += _name_finnish_3[
SeedChance(10, std::size(_name_finnish_3), seed)];
531 std::size(_name_polish_2_o) + std::size(_name_polish_2_m) +
532 std::size(_name_polish_2_f) + std::size(_name_polish_2_n),
537 if (i < std::size(_name_polish_2_o)) {
538 builder += _name_polish_2_o[
SeedChance(3, std::size(_name_polish_2_o), seed)];
542 if (i < std::size(_name_polish_2_m) + std::size(_name_polish_2_o)) {
544 builder += _name_polish_1_m[
SeedChance(5, std::size(_name_polish_1_m), seed)];
547 builder += _name_polish_2_m[
SeedChance(7, std::size(_name_polish_2_m), seed)];
549 if (j >= 4 && j < 16) {
550 builder += _name_polish_3_m[
SeedChance(10, std::size(_name_polish_3_m), seed)];
556 if (i < std::size(_name_polish_2_f) + std::size(_name_polish_2_m) + std::size(_name_polish_2_o)) {
558 builder += _name_polish_1_f[
SeedChance(5, std::size(_name_polish_1_f), seed)];
561 builder += _name_polish_2_f[
SeedChance(7, std::size(_name_polish_2_f), seed)];
563 if (j >= 4 && j < 16) {
564 builder += _name_polish_3_f[
SeedChance(10, std::size(_name_polish_3_f), seed)];
571 builder += _name_polish_1_n[
SeedChance(5, std::size(_name_polish_1_n), seed)];
574 builder += _name_polish_2_n[
SeedChance(7, std::size(_name_polish_2_n), seed)];
576 if (j >= 4 && j < 16) {
577 builder += _name_polish_3_n[
SeedChance(10, std::size(_name_polish_3_n), seed)];
593 builder += _name_czech_real[
SeedModChance(4, std::size(_name_czech_real), seed)];
600 bool do_prefix = prob_tails < 12;
601 bool do_suffix = prob_tails > 11 && prob_tails < 17;
605 int prefix = 0, ending = 0, suffix = 0;
614 if (do_prefix) prefix =
SeedModChance(5, std::size(_name_czech_adj) * 12, seed) / 12;
615 if (do_suffix) suffix =
SeedModChance(7, std::size(_name_czech_suffix), seed);
618 std::size(_name_czech_subst_full) + 3 * std::size(_name_czech_subst_stem),
620 if (stem < std::size(_name_czech_subst_full)) {
622 dynamic_subst =
false;
623 gender = _name_czech_subst_full[stem].gender;
624 choose = _name_czech_subst_full[stem].choose;
625 allow = _name_czech_subst_full[stem].allow;
627 uint map[std::size(_name_czech_subst_ending)];
628 int ending_start = -1, ending_stop = -1;
631 dynamic_subst =
true;
632 stem -= std::size(_name_czech_subst_full);
633 stem %= std::size(_name_czech_subst_stem);
634 gender = _name_czech_subst_stem[stem].gender;
635 choose = _name_czech_subst_stem[stem].choose;
636 allow = _name_czech_subst_stem[stem].allow;
639 postfix =
SeedModChance(14, std::size(_name_czech_subst_postfix) * 2, seed);
641 if (choose & CZC_POSTFIX) {
643 postfix %= std::size(_name_czech_subst_postfix);
645 if (choose & CZC_NOPOSTFIX) {
647 postfix += std::size(_name_czech_subst_postfix);
649 if (postfix < std::size(_name_czech_subst_postfix)) {
650 choose |= CZC_POSTFIX;
652 choose |= CZC_NOPOSTFIX;
656 for (ending = 0; ending < (int)std::size(_name_czech_subst_ending); ending++) {
659 if (gender == CZG_FREE ||
660 (gender == CZG_NFREE && e->gender != CZG_SNEUT && e->gender != CZG_PNEUT) ||
661 gender == e->gender) {
662 if (ending_start < 0) {
663 ending_start = ending;
665 }
else if (ending_start >= 0) {
666 ending_stop = ending - 1;
670 if (ending_stop < 0) {
672 ending_stop = ending - 1;
677 for (ending = ending_start; ending <= ending_stop; ending++) {
680 if ((e->choose & choose) == choose && (e->allow & allow) != 0) {
690 gender = _name_czech_subst_ending[ending].gender;
691 assert(gender != CZG_FREE && gender != CZG_NFREE);
694 if (do_prefix && (_name_czech_adj[prefix].choose & choose) != choose) {
701 CzechPattern pattern = _name_czech_adj[prefix].pattern;
703 builder += _name_czech_adj[prefix].name;
707 while (
GB(builder[endpos], 6, 2) == 2) endpos--;
710 if (gender == CZG_SMASC && pattern == CZP_PRIVL) {
712 builder[endpos - 2] =
'u';
714 builder += _name_czech_patmod[gender][pattern];
721 builder += _name_czech_subst_stem[stem].name;
722 if (postfix < std::size(_name_czech_subst_postfix)) {
723 const char *poststr = _name_czech_subst_postfix[postfix];
724 const char *endstr = _name_czech_subst_ending[ending].name;
726 size_t postlen = strlen(poststr);
727 size_t endlen = strlen(endstr);
728 assert(postlen > 0 && endlen > 0);
731 if (postlen < 2 || postlen > endlen ||
732 ((poststr[1] !=
'v' || poststr[1] != endstr[1]) &&
733 poststr[2] != endstr[1])) {
737 if (endstr[0] ==
'i') {
739 switch (builder[last]) {
740 case 'k': builder[last] =
'c';
break;
741 case 'h': builder[last] =
'z';
break;
747 builder += _name_czech_subst_ending[ending].name;
749 builder += _name_czech_subst_full[stem].name;
754 builder += _name_czech_suffix[suffix];
766 builder += _name_romanian_real[
SeedChance(0, std::size(_name_romanian_real), seed)];
777 builder += _name_slovak_real[
SeedChance(0, std::size(_name_slovak_real), seed)];
792 builder += _name_norwegian_real[
SeedChance(4, std::size(_name_norwegian_real), seed)];
797 builder += _name_norwegian_1[
SeedChance(4, std::size(_name_norwegian_1), seed)];
799 builder += _name_norwegian_2[
SeedChance(11, std::size(_name_norwegian_2), seed)];
811 builder += _name_hungarian_real[
SeedChance(0, std::size(_name_hungarian_real), seed)];
816 uint i =
SeedChance(3, std::size(_name_hungarian_1) * 3, seed);
817 if (i < std::size(_name_hungarian_1)) builder += _name_hungarian_1[i];
820 builder += _name_hungarian_2[
SeedChance(3, std::size(_name_hungarian_2), seed)];
821 builder += _name_hungarian_3[
SeedChance(6, std::size(_name_hungarian_3), seed)];
824 i =
SeedChance(10, std::size(_name_hungarian_4) * 3, seed);
825 if (i < std::size(_name_hungarian_4)) {
826 builder += _name_hungarian_4[i];
838 builder += _name_swiss_real[
SeedChance(0, std::size(_name_swiss_real), seed)];
851 if (i >= 0) builder += _name_danish_1[i];
854 builder += _name_danish_2[
SeedChance( 7, std::size(_name_danish_2), seed)];
855 builder += _name_danish_3[
SeedChance(16, std::size(_name_danish_3), seed)];
870 builder += _name_turkish_prefix[
SeedModChance( 2, std::size(_name_turkish_prefix), seed)];
873 builder += _name_turkish_middle[
SeedModChance( 4, std::size(_name_turkish_middle), seed)];
877 builder += _name_turkish_suffix[
SeedModChance( 10, std::size(_name_turkish_suffix), seed)];
882 builder += _name_turkish_prefix[
SeedModChance( 2, std::size(_name_turkish_prefix), seed)];
883 builder += _name_turkish_suffix[
SeedModChance( 4, std::size(_name_turkish_suffix), seed)];
887 builder += _name_turkish_real[
SeedModChance( 4, std::size(_name_turkish_real), seed)];
901 builder += _name_italian_real[
SeedModChance(4, std::size(_name_italian_real), seed)];
905 static const char *
const mascul_femin_italian[] = {
911 builder += _name_italian_pref[
SeedModChance(11, std::size(_name_italian_pref), seed)];
916 builder += _name_italian_1m[
SeedModChance(4, std::size(_name_italian_1m), seed)];
918 builder += _name_italian_1f[
SeedModChance(4, std::size(_name_italian_1f), seed)];
922 builder += _name_italian_2[
SeedModChance(11, std::size(_name_italian_2), seed)];
923 builder += mascul_femin_italian[i];
925 builder += _name_italian_2i[
SeedModChance(16, std::size(_name_italian_2i), seed)];
930 builder += _name_italian_3[
SeedModChance(4, std::size(_name_italian_3), seed)];
932 builder += _name_italian_river1[
SeedModChance(4, std::size(_name_italian_river1), seed)];
933 builder += _name_italian_river2[
SeedModChance(16, std::size(_name_italian_river2), seed)];
947 builder += _name_catalan_real[
SeedModChance(4, std::size(_name_catalan_real), seed)];
952 builder += _name_catalan_pref[
SeedModChance(11, std::size(_name_catalan_pref), seed)];
957 builder += _name_catalan_1m[
SeedModChance(4, std::size(_name_catalan_1m), seed)];
958 builder += _name_catalan_2m[
SeedModChance(11, std::size(_name_catalan_2m), seed)];
960 builder += _name_catalan_1f[
SeedModChance(4, std::size(_name_catalan_1f), seed)];
961 builder += _name_catalan_2f[
SeedModChance(11, std::size(_name_catalan_2f), seed)];
966 builder += _name_catalan_3[
SeedModChance(4, std::size(_name_catalan_3), seed)];
968 builder += _name_catalan_river1[
SeedModChance(4, std::size(_name_catalan_river1), seed)];
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
static void ReduceLineCache()
Reduce the size of linecache if necessary to prevent infinite growth.
Equivalent to the std::back_insert_iterator in function, with some convenience helpers for string con...
void RemoveElementsFromBack(size_t amount)
Remove the given amount of characters from the back of the string.
size_t CurrentIndex()
Get the current index in the string.
Functions related to world/map generation.
Functions related to laying out the texts.
Pseudo random number generator.
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition of base types and functions in a cross-platform compatible way.
size_t Utf8StringLength(const char *s)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Functions related to low-level strings.
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index, bool game_script)
Get a parsed string with most special stringcodes replaced by the string parameters.
Functions related to OTTD's strings.
uint8_t town_name
the town name generator used for town names
GameCreationSettings game_creation
settings used during the creation of a game (map)
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Structure to encapsulate the pseudo random number generators.
uint32_t Next()
Generate the next pseudo random number.
Struct holding parameters used to generate town name.
TownNameParams(uint8_t town_name)
Initializes this struct from language ID.
uint16_t type
town name style
uint32_t grfid
newgrf ID (0 if not used)
static const uint MAX_LENGTH_TOWN_NAME_CHARS
The maximum length of a town name in characters including '\0'.
static void MakeHungarianTownName(StringBuilder &builder, uint32_t seed)
Generates Hungarian town name from given seed.
static void MakeCatalanTownName(StringBuilder &builder, uint32_t seed)
Generates Catalan town name from given seed.
static uint32_t SeedModChance(uint8_t shift_by, size_t max, uint32_t seed)
Generates a number from given seed.
static void MakePolishTownName(StringBuilder &builder, uint32_t seed)
Generates Polish town name from given seed.
static void MakeSlovakTownName(StringBuilder &builder, uint32_t seed)
Generates Slovak town name from given seed.
static void MakeSwedishTownName(StringBuilder &builder, uint32_t seed)
Generates Swedish town name from given seed.
static void MakeFrenchTownName(StringBuilder &builder, uint32_t seed)
Generates French town name from given seed.
static void MakeItalianTownName(StringBuilder &builder, uint32_t seed)
Generates Italian town name from given seed.
static void MakeNorwegianTownName(StringBuilder &builder, uint32_t seed)
Generates Norwegian town name from given seed.
bool VerifyTownName(uint32_t r, const TownNameParams *par, TownNames *town_names)
Verifies the town name is valid and unique.
static void MakeCzechTownName(StringBuilder &builder, uint32_t seed)
Generates Czech town name from given seed.
static void MakeGermanTownName(StringBuilder &builder, uint32_t seed)
Generates German town name from given seed.
static void MakeDanishTownName(StringBuilder &builder, uint32_t seed)
Generates Danish town name from given seed.
static void MakeEnglishOriginalTownName(StringBuilder &builder, uint32_t seed)
Generates English (Original) town name from given seed.
static void ReplaceWords(const char *org, const char *rep, StringBuilder &builder, size_t start)
Replaces a string beginning in 'org' with 'rep'.
static void MakeEnglishAdditionalTownName(StringBuilder &builder, uint32_t seed)
Generates English (Additional) town name from given seed.
static void MakeFinnishTownName(StringBuilder &builder, uint32_t seed)
Generates Finnish town name from given seed.
static TownNameGenerator * _town_name_generators[]
Town name generators.
void TownNameGenerator(StringBuilder &builder, uint32_t seed)
Type for all town name generator functions.
static void MakeSpanishTownName(StringBuilder &builder, uint32_t seed)
Generates Latin-American town name from given seed.
static void ReplaceEnglishWords(StringBuilder &builder, size_t start, bool original)
Replaces english curses and ugly letter combinations by nicer ones.
bool GenerateTownName(Randomizer &randomizer, uint32_t *townnameparts, TownNames *town_names)
Generates valid town name.
static void MakeRomanianTownName(StringBuilder &builder, uint32_t seed)
Generates Romanian town name from given seed.
static void MakeSillyTownName(StringBuilder &builder, uint32_t seed)
Generates Silly town name from given seed.
static void MakeTurkishTownName(StringBuilder &builder, uint32_t seed)
Generates Turkish town name from given seed.
static void MakeDutchTownName(StringBuilder &builder, uint32_t seed)
Generates Dutch town name from given seed.
static void MakeSwissTownName(StringBuilder &builder, uint32_t seed)
Generates Swiss town name from given seed.
static int32_t SeedChanceBias(uint8_t shift_by, size_t max, uint32_t seed, int bias)
Generates a number from given seed.
static uint32_t SeedChance(uint8_t shift_by, size_t max, uint32_t seed)
Generates a number from given seed.
static void GetTownName(StringBuilder &builder, const TownNameParams *par, uint32_t townnameparts)
Fills builder with specified town name.
void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed)
Generates town name from given seed.
static void MakeAustrianTownName(StringBuilder &builder, uint32_t seed)
Generates Austrian town name from given seed.
Namepart tables for the town name generator.
Definition of structures used for generating town names.