OpenTTD Source: src/townname.cpp Source File

OpenTTD Source 15.0-RC2

townname.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "string_func.h"
12#include "townname_type.h"
13#include "town.h"
14#include "strings_func.h"
15#include "core/random_func.hpp"
16#include "genworld.h"
17#include "gfx_layout.h"
18#include "strings_internal.h"
19
20#include "table/townname.h"
21
22#include "safeguards.h"
23
24
29 TownNameParams::TownNameParams(const Town *t) :
30 grfid(t->townnamegrfid), // by default, use supplied data
31 type(t->townnametype)
32{
33 if (t->townnamegrfid != 0 && GetGRFTownName(t->townnamegrfid) == nullptr) {
34 /* Fallback to the first built in town name (English). */
35 this->grfid = 0;
36 this->type = SPECSTR_TOWNNAME_START;
37 return;
38 }
39}
40
41
48 static void GetTownName(StringBuilder &builder, const TownNameParams *par, uint32_t townnameparts)
49{
50 if (par->grfid == 0) {
51 auto tmp_params = MakeParameters(townnameparts);
52 GetStringWithArgs(builder, par->type, tmp_params);
53 return;
54 }
55
56 GRFTownNameGenerate(builder, par->grfid, par->type, townnameparts);
57}
58
65std::string GetTownName(const TownNameParams *par, uint32_t townnameparts)
66{
67 std::string result;
68 StringBuilder builder(result);
69 GetTownName(builder, par, townnameparts);
70 return result;
71}
72
78 void GetTownName(StringBuilder &builder, const Town *t)
79{
80 TownNameParams par(t);
81 GetTownName(builder, &par, t->townnameparts);
82}
83
89std::string GetTownName(const Town *t)
90{
91 TownNameParams par(t);
92 return GetTownName(&par, t->townnameparts);
93}
94
95
103 bool VerifyTownName(uint32_t r, const TownNameParams *par, TownNames *town_names)
104{
105 std::string name = GetTownName(par, r);
106
107 /* Check size and width */
108 if (Utf8StringLength(name) >= MAX_LENGTH_TOWN_NAME_CHARS) return false;
109
110 if (town_names != nullptr) {
111 if (town_names->find(name) != town_names->end()) return false;
112 town_names->insert(std::move(name));
113 } else {
114 for (const Town *t : Town::Iterate()) {
115 /* We can't just compare the numbers since
116 * several numbers may map to a single name. */
117 if (t->name.empty()) {
118 if (name == GetTownName(t)) return false;
119 } else {
120 if (name == t->name) return false;
121 }
122 }
123 }
124
125 return true;
126}
127
128
136 bool GenerateTownName(Randomizer &randomizer, uint32_t *townnameparts, TownNames *town_names)
137{
138 TownNameParams par(_settings_game.game_creation.town_name);
139
140 /* Do not set i too low, since when we run out of names, we loop
141 * for #tries only one time anyway - then we stop generating more
142 * towns. Do not set it too high either, since looping through all
143 * the other towns may take considerable amount of time (10000 is
144 * too much). */
145 for (int i = 1000; i != 0; i--) {
146 uint32_t r = randomizer.Next();
147 if (!VerifyTownName(r, &par, town_names)) continue;
148
149 *townnameparts = r;
150 return true;
151 }
152
153 return false;
154}
155
156
157
165 static inline uint32_t SeedChance(uint8_t shift_by, size_t max, uint32_t seed)
166{
167 return (GB(seed, shift_by, 16) * ClampTo<uint16_t>(max)) >> 16;
168}
169
170
178 static inline uint32_t SeedModChance(uint8_t shift_by, size_t max, uint32_t seed)
179{
180 /* This actually gives *MUCH* more even distribution of the values
181 * than SeedChance(), which is absolutely horrible in that. If
182 * you do not believe me, try with i.e. the Czech town names,
183 * compare the words (nicely visible on prefixes) generated by
184 * SeedChance() and SeedModChance(). Do not get discouraged by the
185 * never-use-modulo myths, which hold true only for the linear
186 * congruential generators (and Random() isn't such a generator).
187 * --pasky
188 * TODO: Perhaps we should use it for all the name generators? --pasky */
189 return (seed >> shift_by) % max;
190}
191
192
201 static inline int32_t SeedChanceBias(uint8_t shift_by, size_t max, uint32_t seed, int bias)
202{
203 return SeedChance(shift_by, max + bias, seed) - bias;
204}
205
206
214 static void ReplaceWords(std::string_view org, std::string_view rep, std::string &str, size_t start)
215{
216 if (str.compare(start, org.size(), org) == 0) str.replace(start, org.size(), rep);
217}
218
219
226 static void ReplaceEnglishWords(std::string &str, size_t start, bool original)
227{
228 if (original) ReplaceWords("Ce", "Ke", str, start);
229 if (original) ReplaceWords("Ci", "Ki", str, start);
230 ReplaceWords("Cunt", "East", str, start);
231 ReplaceWords("Slag", "Pits", str, start);
232 ReplaceWords("Slut", "Edin", str, start);
233 if (!original) ReplaceWords("Fart", "Boot", str, start); // never happens with 'English (Original)'
234 ReplaceWords("Drar", "Quar", str, start);
235 ReplaceWords("Dreh", "Bash", str, start);
236 ReplaceWords("Frar", "Shor", str, start);
237 ReplaceWords("Grar", "Aber", str, start);
238 ReplaceWords("Brar", "Over", str, start);
239 ReplaceWords("Wrar", original ? "Inve" : "Stan", str, start);
240}
241
247 static void MakeEnglishOriginalTownName(StringBuilder &builder, uint32_t seed)
248{
249 size_t start = builder.GetBytesWritten();
250
251 /* optional first segment */
252 int i = SeedChanceBias(0, std::size(_name_original_english_1), seed, 50);
253 if (i >= 0) builder += _name_original_english_1[i];
254
255 /* mandatory middle segments */
256 builder += _name_original_english_2[SeedChance(4, std::size(_name_original_english_2), seed)];
257 builder += _name_original_english_3[SeedChance(7, std::size(_name_original_english_3), seed)];
258 builder += _name_original_english_4[SeedChance(10, std::size(_name_original_english_4), seed)];
259 builder += _name_original_english_5[SeedChance(13, std::size(_name_original_english_5), seed)];
260
261 /* optional last segment */
262 i = SeedChanceBias(15, std::size(_name_original_english_6), seed, 60);
263 if (i >= 0) builder += _name_original_english_6[i];
264
265 ReplaceEnglishWords(builder.GetString(), start, true);
266}
267
268
274 static void MakeEnglishAdditionalTownName(StringBuilder &builder, uint32_t seed)
275{
276 size_t start = builder.GetString().size();
277
278 /* optional first segment */
279 int i = SeedChanceBias(0, std::size(_name_additional_english_prefix), seed, 50);
280 if (i >= 0) builder += _name_additional_english_prefix[i];
281
282 if (SeedChance(3, 20, seed) >= 14) {
283 builder += _name_additional_english_1a[SeedChance(6, std::size(_name_additional_english_1a), seed)];
284 } else {
285 builder += _name_additional_english_1b1[SeedChance(6, std::size(_name_additional_english_1b1), seed)];
286 builder += _name_additional_english_1b2[SeedChance(9, std::size(_name_additional_english_1b2), seed)];
287 if (SeedChance(11, 20, seed) >= 4) {
288 builder += _name_additional_english_1b3a[SeedChance(12, std::size(_name_additional_english_1b3a), seed)];
289 } else {
290 builder += _name_additional_english_1b3b[SeedChance(12, std::size(_name_additional_english_1b3b), seed)];
291 }
292 }
293
294 builder += _name_additional_english_2[SeedChance(14, std::size(_name_additional_english_2), seed)];
295
296 /* optional last segment */
297 i = SeedChanceBias(15, std::size(_name_additional_english_3), seed, 60);
298 if (i >= 0) builder += _name_additional_english_3[i];
299
300 ReplaceEnglishWords(builder.GetString(), start, false);
301}
302
303
309 static void MakeAustrianTownName(StringBuilder &builder, uint32_t seed)
310{
311 /* Bad, Maria, Gross, ... */
312 int i = SeedChanceBias(0, std::size(_name_austrian_a1), seed, 15);
313 if (i >= 0) builder += _name_austrian_a1[i];
314
315 int j = 0;
316
317 i = SeedChance(4, 6, seed);
318 if (i >= 4) {
319 /* Kaisers-kirchen */
320 builder += _name_austrian_a2[SeedChance( 7, std::size(_name_austrian_a2), seed)];
321 builder += _name_austrian_a3[SeedChance(13, std::size(_name_austrian_a3), seed)];
322 } else if (i >= 2) {
323 /* St. Johann */
324 builder += _name_austrian_a5[SeedChance( 7, std::size(_name_austrian_a5), seed)];
325 builder += _name_austrian_a6[SeedChance( 9, std::size(_name_austrian_a6), seed)];
326 j = 1; // More likely to have a " an der " or " am "
327 } else {
328 /* Zell */
329 builder += _name_austrian_a4[SeedChance( 7, std::size(_name_austrian_a4), seed)];
330 }
331
332 i = SeedChance(1, 6, seed);
333 if (i >= 4 - j) {
334 /* an der Donau (rivers) */
335 builder += _name_austrian_f1[SeedChance(4, std::size(_name_austrian_f1), seed)];
336 builder += _name_austrian_f2[SeedChance(5, std::size(_name_austrian_f2), seed)];
337 } else if (i >= 2 - j) {
338 /* am Dachstein (mountains) */
339 builder += _name_austrian_b1[SeedChance(4, std::size(_name_austrian_b1), seed)];
340 builder += _name_austrian_b2[SeedChance(5, std::size(_name_austrian_b2), seed)];
341 }
342}
343
344
350 static void MakeGermanTownName(StringBuilder &builder, uint32_t seed)
351{
352 uint seed_derivative = SeedChance(7, 28, seed);
353
354 /* optional prefix */
355 if (seed_derivative == 12 || seed_derivative == 19) {
356 uint i = SeedChance(2, std::size(_name_german_pre), seed);
357 builder += _name_german_pre[i];
358 }
359
360 /* mandatory middle segments including option of hardcoded name */
361 uint i = SeedChance(3, std::size(_name_german_real) + std::size(_name_german_1), seed);
362 if (i < std::size(_name_german_real)) {
363 builder += _name_german_real[i];
364 } else {
365 builder += _name_german_1[i - std::size(_name_german_real)];
366
367 i = SeedChance(5, std::size(_name_german_2), seed);
368 builder += _name_german_2[i];
369 }
370
371 /* optional suffix */
372 if (seed_derivative == 24) {
373 i = SeedChance(9, std::size(_name_german_4_an_der) + std::size(_name_german_4_am), seed);
374 if (i < std::size(_name_german_4_an_der)) {
375 builder += _name_german_3_an_der[0];
376 builder += _name_german_4_an_der[i];
377 } else {
378 builder += _name_german_3_am[0];
379 builder += _name_german_4_am[i - std::size(_name_german_4_an_der)];
380 }
381 }
382}
383
384
390 static void MakeSpanishTownName(StringBuilder &builder, uint32_t seed)
391{
392 builder += _name_spanish_real[SeedChance(0, std::size(_name_spanish_real), seed)];
393}
394
395
401 static void MakeFrenchTownName(StringBuilder &builder, uint32_t seed)
402{
403 builder += _name_french_real[SeedChance(0, std::size(_name_french_real), seed)];
404}
405
406
412 static void MakeSillyTownName(StringBuilder &builder, uint32_t seed)
413{
414 builder += _name_silly_1[SeedChance( 0, std::size(_name_silly_1), seed)];
415 builder += _name_silly_2[SeedChance(16, std::size(_name_silly_2), seed)];
416}
417
418
424 static void MakeSwedishTownName(StringBuilder &builder, uint32_t seed)
425{
426 /* optional first segment */
427 int i = SeedChanceBias(0, std::size(_name_swedish_1), seed, 50);
428 if (i >= 0) builder += _name_swedish_1[i];
429
430 /* mandatory middle segments including option of hardcoded name */
431 if (SeedChance(4, 5, seed) >= 3) {
432 builder += _name_swedish_2[SeedChance( 7, std::size(_name_swedish_2), seed)];
433 } else {
434 builder += _name_swedish_2a[SeedChance( 7, std::size(_name_swedish_2a), seed)];
435 builder += _name_swedish_2b[SeedChance(10, std::size(_name_swedish_2b), seed)];
436 builder += _name_swedish_2c[SeedChance(13, std::size(_name_swedish_2c), seed)];
437 }
438
439 builder += _name_swedish_3[SeedChance(16, std::size(_name_swedish_3), seed)];
440}
441
442
448 static void MakeDutchTownName(StringBuilder &builder, uint32_t seed)
449{
450 /* optional first segment */
451 int i = SeedChanceBias(0, std::size(_name_dutch_1), seed, 50);
452 if (i >= 0) builder += _name_dutch_1[i];
453
454 /* mandatory middle segments including option of hardcoded name */
455 if (SeedChance(6, 9, seed) > 4) {
456 builder += _name_dutch_2[SeedChance( 9, std::size(_name_dutch_2), seed)];
457 } else {
458 builder += _name_dutch_3[SeedChance( 9, std::size(_name_dutch_3), seed)];
459 builder += _name_dutch_4[SeedChance(12, std::size(_name_dutch_4), seed)];
460 }
461
462 builder += _name_dutch_5[SeedChance(15, std::size(_name_dutch_5), seed)];
463}
464
465
471 static void MakeFinnishTownName(StringBuilder &builder, uint32_t seed)
472{
473 size_t start = builder.GetString().size();
474
475 /* Select randomly if town name should consists of one or two parts. */
476 if (SeedChance(0, 15, seed) >= 10) {
477 builder += _name_finnish_real[SeedChance(2, std::size(_name_finnish_real), seed)];
478 return;
479 }
480
481 if (SeedChance(0, 15, seed) >= 5) {
482 /* A two-part name by combining one of _name_finnish_1 + "la"/"lä"
483 * The reason for not having the contents of _name_finnish_{1,2} in the same table is
484 * that the ones in _name_finnish_2 are not good for this purpose. */
485 uint sel = SeedChance( 0, std::size(_name_finnish_1), seed);
486 builder += _name_finnish_1[sel];
487
488 std::string &str = builder.GetString();
489 if (str.back() == 'i') str.back() = 'e';
490
491 if (str.find_first_of("aouAOU", start) != std::string_view::npos) {
492 builder += "la";
493 } else {
494 builder += "l\u00e4";
495 }
496 return;
497 }
498
499 /* A two-part name by combining one of _name_finnish_{1,2} + _name_finnish_3.
500 * Why aren't _name_finnish_{1,2} just one table? See above. */
501 uint sel = SeedChance(2, std::size(_name_finnish_1) + std::size(_name_finnish_2), seed);
502 if (sel >= std::size(_name_finnish_1)) {
503 builder += _name_finnish_2[sel - std::size(_name_finnish_1)];
504 } else {
505 builder += _name_finnish_1[sel];
506 }
507
508 builder += _name_finnish_3[SeedChance(10, std::size(_name_finnish_3), seed)];
509}
510
511
517 static void MakePolishTownName(StringBuilder &builder, uint32_t seed)
518{
519 /* optional first segment */
520 uint i = SeedChance(0,
521 std::size(_name_polish_2_o) + std::size(_name_polish_2_m) +
522 std::size(_name_polish_2_f) + std::size(_name_polish_2_n),
523 seed);
524 uint j = SeedChance(2, 20, seed);
525
526
527 if (i < std::size(_name_polish_2_o)) {
528 builder += _name_polish_2_o[SeedChance(3, std::size(_name_polish_2_o), seed)];
529 return;
530 }
531
532 if (i < std::size(_name_polish_2_m) + std::size(_name_polish_2_o)) {
533 if (j < 4) {
534 builder += _name_polish_1_m[SeedChance(5, std::size(_name_polish_1_m), seed)];
535 }
536
537 builder += _name_polish_2_m[SeedChance(7, std::size(_name_polish_2_m), seed)];
538
539 if (j >= 4 && j < 16) {
540 builder += _name_polish_3_m[SeedChance(10, std::size(_name_polish_3_m), seed)];
541 }
542
543 return;
544 }
545
546 if (i < std::size(_name_polish_2_f) + std::size(_name_polish_2_m) + std::size(_name_polish_2_o)) {
547 if (j < 4) {
548 builder += _name_polish_1_f[SeedChance(5, std::size(_name_polish_1_f), seed)];
549 }
550
551 builder += _name_polish_2_f[SeedChance(7, std::size(_name_polish_2_f), seed)];
552
553 if (j >= 4 && j < 16) {
554 builder += _name_polish_3_f[SeedChance(10, std::size(_name_polish_3_f), seed)];
555 }
556
557 return;
558 }
559
560 if (j < 4) {
561 builder += _name_polish_1_n[SeedChance(5, std::size(_name_polish_1_n), seed)];
562 }
563
564 builder += _name_polish_2_n[SeedChance(7, std::size(_name_polish_2_n), seed)];
565
566 if (j >= 4 && j < 16) {
567 builder += _name_polish_3_n[SeedChance(10, std::size(_name_polish_3_n), seed)];
568 }
569
570 return;
571}
572
573
579 static void MakeCzechTownName(StringBuilder &builder, uint32_t seed)
580{
581 /* 1:3 chance to use a real name. */
582 if (SeedModChance(0, 4, seed) == 0) {
583 builder += _name_czech_real[SeedModChance(4, std::size(_name_czech_real), seed)];
584 return;
585 }
586
587 /* Probability of prefixes/suffixes
588 * 0..11 prefix, 12..13 prefix+suffix, 14..17 suffix, 18..31 nothing */
589 int prob_tails = SeedModChance(2, 32, seed);
590 bool do_prefix = prob_tails < 12;
591 bool do_suffix = prob_tails > 11 && prob_tails < 17;
592 bool dynamic_subst;
593
594 /* IDs of the respective parts */
595 int prefix = 0, ending = 0, suffix = 0;
596 size_t postfix = 0;
597 size_t stem;
598
599 /* The select criteria. */
600 CzechGender gender;
601 CzechChooseFlags choose;
602 CzechAllowFlags allow;
603
604 if (do_prefix) prefix = SeedModChance(5, std::size(_name_czech_adj) * 12, seed) / 12;
605 if (do_suffix) suffix = SeedModChance(7, std::size(_name_czech_suffix), seed);
606 /* 3:1 chance 3:1 to use dynamic substantive */
607 stem = SeedModChance(9,
608 std::size(_name_czech_subst_full) + 3 * std::size(_name_czech_subst_stem),
609 seed);
610 if (stem < std::size(_name_czech_subst_full)) {
611 /* That was easy! */
612 dynamic_subst = false;
613 gender = _name_czech_subst_full[stem].gender;
614 choose = _name_czech_subst_full[stem].choose;
615 allow = _name_czech_subst_full[stem].allow;
616 } else {
617 uint map[std::size(_name_czech_subst_ending)];
618 int ending_start = -1, ending_stop = -1;
619
620 /* Load the substantive */
621 dynamic_subst = true;
622 stem -= std::size(_name_czech_subst_full);
623 stem %= std::size(_name_czech_subst_stem);
624 gender = _name_czech_subst_stem[stem].gender;
625 choose = _name_czech_subst_stem[stem].choose;
626 allow = _name_czech_subst_stem[stem].allow;
627
628 /* Load the postfix (1:1 chance that a postfix will be inserted) */
629 postfix = SeedModChance(14, std::size(_name_czech_subst_postfix) * 2, seed);
630
631 if (choose.Test(CzechChooseFlag::Postfix)) {
632 /* Always get a real postfix. */
633 postfix %= std::size(_name_czech_subst_postfix);
634 }
635 if (choose.Test(CzechChooseFlag::NoPostfix)) {
636 /* Always drop a postfix. */
637 postfix += std::size(_name_czech_subst_postfix);
638 }
639 if (postfix < std::size(_name_czech_subst_postfix)) {
640 choose.Set(CzechChooseFlag::Postfix);
641 } else {
642 choose.Set(CzechChooseFlag::NoPostfix);
643 }
644
645 /* Localize the array segment containing a good gender */
646 for (ending = 0; ending < (int)std::size(_name_czech_subst_ending); ending++) {
647 const CzechNameSubst *e = &_name_czech_subst_ending[ending];
648
649 if (gender == CZG_FREE ||
650 (gender == CZG_NFREE && e->gender != CZG_SNEUT && e->gender != CZG_PNEUT) ||
651 gender == e->gender) {
652 if (ending_start < 0) {
653 ending_start = ending;
654 }
655 } else if (ending_start >= 0) {
656 ending_stop = ending - 1;
657 break;
658 }
659 }
660 if (ending_stop < 0) {
661 /* Whoa. All the endings matched. */
662 ending_stop = ending - 1;
663 }
664
665 /* Make a sequential map of the items with good mask */
666 size_t i = 0;
667 for (ending = ending_start; ending <= ending_stop; ending++) {
668 const CzechNameSubst *e = &_name_czech_subst_ending[ending];
669
670 if (e->choose.All(choose) && e->allow.Any(allow)) {
671 map[i++] = ending;
672 }
673 }
674 assert(i > 0);
675
676 /* Load the ending */
677 ending = map[SeedModChance(16, (int)i, seed)];
678 /* Override possible CZG_*FREE; this must be a real gender,
679 * otherwise we get overflow when modifying the adjectivum. */
680 gender = _name_czech_subst_ending[ending].gender;
681 assert(gender != CZG_FREE && gender != CZG_NFREE);
682 }
683
684 if (do_prefix && !_name_czech_adj[prefix].choose.All(choose)) {
685 /* Throw away non-matching prefix. */
686 do_prefix = false;
687 }
688
689 /* Now finally construct the name */
690 if (do_prefix) {
691 CzechPattern pattern = _name_czech_adj[prefix].pattern;
692
693 builder += _name_czech_adj[prefix].name;
694 builder += _name_czech_patmod[gender][pattern];
695 builder.PutChar(' ');
696 }
697
698 if (dynamic_subst) {
699 builder += _name_czech_subst_stem[stem].name;
700 if (postfix < std::size(_name_czech_subst_postfix)) {
701 std::string_view poststr = _name_czech_subst_postfix[postfix];
702 std::string_view endstr = _name_czech_subst_ending[ending].name;
703
704 size_t postlen = poststr.size();
705 size_t endlen = endstr.size();
706 assert(postlen > 0 && endlen > 0);
707
708 /* Kill the "avava" and "Jananna"-like cases */
709 if (postlen < 2 || postlen > endlen ||
710 ((poststr[1] != 'v' || poststr[1] != endstr[1]) &&
711 poststr[2] != endstr[1])) {
712 builder += poststr;
713
714 /* k-i -> c-i, h-i -> z-i */
715 if (endstr[0] == 'i') {
716 std::string &str = builder.GetString();
717 switch (str.back()) {
718 case 'k': str.back() = 'c'; break;
719 case 'h': str.back() = 'z'; break;
720 default: break;
721 }
722 }
723 }
724 }
725 builder += _name_czech_subst_ending[ending].name;
726 } else {
727 builder += _name_czech_subst_full[stem].name;
728 }
729
730 if (do_suffix) {
731 builder += " ";
732 builder += _name_czech_suffix[suffix];
733 }
734}
735
736
742 static void MakeRomanianTownName(StringBuilder &builder, uint32_t seed)
743{
744 builder += _name_romanian_real[SeedChance(0, std::size(_name_romanian_real), seed)];
745}
746
747
753 static void MakeSlovakTownName(StringBuilder &builder, uint32_t seed)
754{
755 builder += _name_slovak_real[SeedChance(0, std::size(_name_slovak_real), seed)];
756}
757
758
764 static void MakeNorwegianTownName(StringBuilder &builder, uint32_t seed)
765{
766 /* Use first 4 bit from seed to decide whether or not this town should
767 * have a real name 3/16 chance. Bit 0-3 */
768 if (SeedChance(0, 15, seed) < 3) {
769 /* Use 7bit for the realname table index. Bit 4-10 */
770 builder += _name_norwegian_real[SeedChance(4, std::size(_name_norwegian_real), seed)];
771 return;
772 }
773
774 /* Use 7bit for the first fake part. Bit 4-10 */
775 builder += _name_norwegian_1[SeedChance(4, std::size(_name_norwegian_1), seed)];
776 /* Use 7bit for the last fake part. Bit 11-17 */
777 builder += _name_norwegian_2[SeedChance(11, std::size(_name_norwegian_2), seed)];
778}
779
780
786 static void MakeHungarianTownName(StringBuilder &builder, uint32_t seed)
787{
788 if (SeedChance(12, 15, seed) < 3) {
789 builder += _name_hungarian_real[SeedChance(0, std::size(_name_hungarian_real), seed)];
790 return;
791 }
792
793 /* optional first segment */
794 uint i = SeedChance(3, std::size(_name_hungarian_1) * 3, seed);
795 if (i < std::size(_name_hungarian_1)) builder += _name_hungarian_1[i];
796
797 /* mandatory middle segments */
798 builder += _name_hungarian_2[SeedChance(3, std::size(_name_hungarian_2), seed)];
799 builder += _name_hungarian_3[SeedChance(6, std::size(_name_hungarian_3), seed)];
800
801 /* optional last segment */
802 i = SeedChance(10, std::size(_name_hungarian_4) * 3, seed);
803 if (i < std::size(_name_hungarian_4)) {
804 builder += _name_hungarian_4[i];
805 }
806}
807
808
814 static void MakeSwissTownName(StringBuilder &builder, uint32_t seed)
815{
816 builder += _name_swiss_real[SeedChance(0, std::size(_name_swiss_real), seed)];
817}
818
819
825 static void MakeDanishTownName(StringBuilder &builder, uint32_t seed)
826{
827 /* optional first segment */
828 int i = SeedChanceBias(0, std::size(_name_danish_1), seed, 50);
829 if (i >= 0) builder += _name_danish_1[i];
830
831 /* middle segments removed as this algorithm seems to create much more realistic names */
832 builder += _name_danish_2[SeedChance( 7, std::size(_name_danish_2), seed)];
833 builder += _name_danish_3[SeedChance(16, std::size(_name_danish_3), seed)];
834}
835
836
842 static void MakeTurkishTownName(StringBuilder &builder, uint32_t seed)
843{
844 uint i = SeedModChance(0, 5, seed);
845
846 switch (i) {
847 case 0:
848 builder += _name_turkish_prefix[SeedModChance( 2, std::size(_name_turkish_prefix), seed)];
849
850 /* middle segment */
851 builder += _name_turkish_middle[SeedModChance( 4, std::size(_name_turkish_middle), seed)];
852
853 /* optional suffix */
854 if (SeedModChance(0, 7, seed) == 0) {
855 builder += _name_turkish_suffix[SeedModChance( 10, std::size(_name_turkish_suffix), seed)];
856 }
857 break;
858
859 case 1: case 2:
860 builder += _name_turkish_prefix[SeedModChance( 2, std::size(_name_turkish_prefix), seed)];
861 builder += _name_turkish_suffix[SeedModChance( 4, std::size(_name_turkish_suffix), seed)];
862 break;
863
864 default:
865 builder += _name_turkish_real[SeedModChance( 4, std::size(_name_turkish_real), seed)];
866 break;
867 }
868}
869
870
876 static void MakeItalianTownName(StringBuilder &builder, uint32_t seed)
877{
878 if (SeedModChance(0, 6, seed) == 0) { // real city names
879 builder += _name_italian_real[SeedModChance(4, std::size(_name_italian_real), seed)];
880 return;
881 }
882
883 static const std::string_view mascul_femin_italian[] = {
884 "o",
885 "a",
886 };
887
888 if (SeedModChance(0, 8, seed) == 0) { // prefix
889 builder += _name_italian_pref[SeedModChance(11, std::size(_name_italian_pref), seed)];
890 }
891
892 uint i = SeedChance(0, 2, seed);
893 if (i == 0) { // masculine form
894 builder += _name_italian_1m[SeedModChance(4, std::size(_name_italian_1m), seed)];
895 } else { // feminine form
896 builder += _name_italian_1f[SeedModChance(4, std::size(_name_italian_1f), seed)];
897 }
898
899 if (SeedModChance(3, 3, seed) == 0) {
900 builder += _name_italian_2[SeedModChance(11, std::size(_name_italian_2), seed)];
901 builder += mascul_femin_italian[i];
902 } else {
903 builder += _name_italian_2i[SeedModChance(16, std::size(_name_italian_2i), seed)];
904 }
905
906 if (SeedModChance(15, 4, seed) == 0) {
907 if (SeedModChance(5, 2, seed) == 0) { // generic suffix
908 builder += _name_italian_3[SeedModChance(4, std::size(_name_italian_3), seed)];
909 } else { // river name suffix
910 builder += _name_italian_river1[SeedModChance(4, std::size(_name_italian_river1), seed)];
911 builder += _name_italian_river2[SeedModChance(16, std::size(_name_italian_river2), seed)];
912 }
913 }
914}
915
916
922 static void MakeCatalanTownName(StringBuilder &builder, uint32_t seed)
923{
924 if (SeedModChance(0, 3, seed) == 0) { // real city names
925 builder += _name_catalan_real[SeedModChance(4, std::size(_name_catalan_real), seed)];
926 return;
927 }
928
929 if (SeedModChance(0, 2, seed) == 0) { // prefix
930 builder += _name_catalan_pref[SeedModChance(11, std::size(_name_catalan_pref), seed)];
931 }
932
933 uint i = SeedChance(0, 2, seed);
934 if (i == 0) { // masculine form
935 builder += _name_catalan_1m[SeedModChance(4, std::size(_name_catalan_1m), seed)];
936 builder += _name_catalan_2m[SeedModChance(11, std::size(_name_catalan_2m), seed)];
937 } else { // feminine form
938 builder += _name_catalan_1f[SeedModChance(4, std::size(_name_catalan_1f), seed)];
939 builder += _name_catalan_2f[SeedModChance(11, std::size(_name_catalan_2f), seed)];
940 }
941
942 if (SeedModChance(15, 5, seed) == 0) {
943 if (SeedModChance(5, 2, seed) == 0) { // generic suffix
944 builder += _name_catalan_3[SeedModChance(4, std::size(_name_catalan_3), seed)];
945 } else { // river name suffix
946 builder += _name_catalan_river1[SeedModChance(4, std::size(_name_catalan_river1), seed)];
947 }
948 }
949}
950
951
957 typedef void TownNameGenerator(StringBuilder &builder, uint32_t seed);
958
960 static TownNameGenerator *const _town_name_generators[] = {
961 MakeEnglishOriginalTownName, // replaces first 4 characters of name
962 MakeFrenchTownName,
963 MakeGermanTownName,
964 MakeEnglishAdditionalTownName, // replaces first 4 characters of name
965 MakeSpanishTownName,
966 MakeSillyTownName,
967 MakeSwedishTownName,
968 MakeDutchTownName,
969 MakeFinnishTownName, // _name_finnish_1
970 MakePolishTownName,
971 MakeSlovakTownName,
972 MakeNorwegianTownName,
973 MakeHungarianTownName,
974 MakeAustrianTownName,
975 MakeRomanianTownName,
976 MakeCzechTownName, // _name_czech_adj + _name_czech_patmod + 1 + _name_czech_subst_stem + _name_czech_subst_postfix
977 MakeSwissTownName,
978 MakeDanishTownName,
979 MakeTurkishTownName,
980 MakeItalianTownName,
981 MakeCatalanTownName,
982};
983
984
991 void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed)
992{
993 assert(lang < std::size(_town_name_generators));
994 return _town_name_generators[lang](builder, seed);
995}
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
BaseBitSet::All
constexpr bool All(const Timpl &other) const
Test if all of the values are set.
Definition base_bitset_type.hpp:151
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
BaseBitSet::Any
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Definition base_bitset_type.hpp:170
BaseStringBuilder::PutChar
void PutChar(char c)
Append 8-bit char.
Definition string_builder.cpp:105
EnumBitSet< CzechChooseFlag, uint8_t >
StringBuilder
Compose data into a growing std::string.
Definition string_builder.hpp:65
StringBuilder::GetString
std::string & GetString() noexcept
Get mutable already written data.
Definition string_builder.hpp:89
StringBuilder::GetBytesWritten
size_type GetBytesWritten() const noexcept
Get number of already written bytes.
Definition string_builder.hpp:81
genworld.h
Functions related to world/map generation.
gfx_layout.h
Functions related to laying out the texts.
random_func.hpp
Pseudo random number generator.
safeguards.h
A number of safeguards to prevent using unsafe methods.
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
Utf8StringLength
size_t Utf8StringLength(std::string_view str)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Definition string.cpp:349
string_func.h
Functions related to low-level strings.
GetStringWithArgs
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.
Definition strings.cpp:336
strings_func.h
Functions related to OTTD's strings.
MakeParameters
auto MakeParameters(Args &&... args)
Helper to create the StringParameters with its own buffer with the given parameter values.
Definition strings_func.h:107
strings_internal.h
Types and functions related to the internal workings of formatting OpenTTD's strings.
SPECSTR_TOWNNAME_START
static constexpr StringID SPECSTR_TOWNNAME_START
Special strings for town names.
Definition strings_type.h:64
CzechNameSubst
Definition townname.h:1751
GameCreationSettings::town_name
uint8_t town_name
the town name generator used for town names
Definition settings_type.h:394
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition settings_type.h:634
Pool::PoolItem<&_town_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition pool_type.hpp:436
Randomizer
Structure to encapsulate the pseudo random number generators.
Definition random_func.hpp:27
Randomizer::Next
uint32_t Next()
Generate the next pseudo random number.
Definition random_func.cpp:42
TownNameParams
Struct holding parameters used to generate town name.
Definition townname_type.h:26
TownNameParams::TownNameParams
TownNameParams(uint8_t town_name)
Initializes this struct from language ID.
Definition townname_type.h:34
TownNameParams::type
uint16_t type
town name style
Definition townname_type.h:28
TownNameParams::grfid
uint32_t grfid
newgrf ID (0 if not used)
Definition townname_type.h:27
Town
Town data structure.
Definition town.h:63
town.h
Base of the town class.
MAX_LENGTH_TOWN_NAME_CHARS
static const uint MAX_LENGTH_TOWN_NAME_CHARS
The maximum length of a town name in characters including '\0'.
Definition town_type.h:119
MakeHungarianTownName
static void MakeHungarianTownName(StringBuilder &builder, uint32_t seed)
Generates Hungarian town name from given seed.
Definition townname.cpp:786
ReplaceEnglishWords
static void ReplaceEnglishWords(std::string &str, size_t start, bool original)
Replaces english curses and ugly letter combinations by nicer ones.
Definition townname.cpp:226
MakeCatalanTownName
static void MakeCatalanTownName(StringBuilder &builder, uint32_t seed)
Generates Catalan town name from given seed.
Definition townname.cpp:922
SeedModChance
static uint32_t SeedModChance(uint8_t shift_by, size_t max, uint32_t seed)
Generates a number from given seed.
Definition townname.cpp:178
MakePolishTownName
static void MakePolishTownName(StringBuilder &builder, uint32_t seed)
Generates Polish town name from given seed.
Definition townname.cpp:517
MakeSlovakTownName
static void MakeSlovakTownName(StringBuilder &builder, uint32_t seed)
Generates Slovak town name from given seed.
Definition townname.cpp:753
MakeSwedishTownName
static void MakeSwedishTownName(StringBuilder &builder, uint32_t seed)
Generates Swedish town name from given seed.
Definition townname.cpp:424
MakeFrenchTownName
static void MakeFrenchTownName(StringBuilder &builder, uint32_t seed)
Generates French town name from given seed.
Definition townname.cpp:401
MakeItalianTownName
static void MakeItalianTownName(StringBuilder &builder, uint32_t seed)
Generates Italian town name from given seed.
Definition townname.cpp:876
MakeNorwegianTownName
static void MakeNorwegianTownName(StringBuilder &builder, uint32_t seed)
Generates Norwegian town name from given seed.
Definition townname.cpp:764
VerifyTownName
bool VerifyTownName(uint32_t r, const TownNameParams *par, TownNames *town_names)
Verifies the town name is valid and unique.
Definition townname.cpp:103
ReplaceWords
static void ReplaceWords(std::string_view org, std::string_view rep, std::string &str, size_t start)
Replaces a string beginning in 'org' with 'rep'.
Definition townname.cpp:214
MakeCzechTownName
static void MakeCzechTownName(StringBuilder &builder, uint32_t seed)
Generates Czech town name from given seed.
Definition townname.cpp:579
MakeGermanTownName
static void MakeGermanTownName(StringBuilder &builder, uint32_t seed)
Generates German town name from given seed.
Definition townname.cpp:350
MakeDanishTownName
static void MakeDanishTownName(StringBuilder &builder, uint32_t seed)
Generates Danish town name from given seed.
Definition townname.cpp:825
MakeEnglishOriginalTownName
static void MakeEnglishOriginalTownName(StringBuilder &builder, uint32_t seed)
Generates English (Original) town name from given seed.
Definition townname.cpp:247
_town_name_generators
static TownNameGenerator *const _town_name_generators[]
Town name generators.
Definition townname.cpp:960
MakeEnglishAdditionalTownName
static void MakeEnglishAdditionalTownName(StringBuilder &builder, uint32_t seed)
Generates English (Additional) town name from given seed.
Definition townname.cpp:274
MakeFinnishTownName
static void MakeFinnishTownName(StringBuilder &builder, uint32_t seed)
Generates Finnish town name from given seed.
Definition townname.cpp:471
TownNameGenerator
void TownNameGenerator(StringBuilder &builder, uint32_t seed)
Type for all town name generator functions.
Definition townname.cpp:957
MakeSpanishTownName
static void MakeSpanishTownName(StringBuilder &builder, uint32_t seed)
Generates Latin-American town name from given seed.
Definition townname.cpp:390
GenerateTownName
bool GenerateTownName(Randomizer &randomizer, uint32_t *townnameparts, TownNames *town_names)
Generates valid town name.
Definition townname.cpp:136
MakeRomanianTownName
static void MakeRomanianTownName(StringBuilder &builder, uint32_t seed)
Generates Romanian town name from given seed.
Definition townname.cpp:742
MakeSillyTownName
static void MakeSillyTownName(StringBuilder &builder, uint32_t seed)
Generates Silly town name from given seed.
Definition townname.cpp:412
MakeTurkishTownName
static void MakeTurkishTownName(StringBuilder &builder, uint32_t seed)
Generates Turkish town name from given seed.
Definition townname.cpp:842
MakeDutchTownName
static void MakeDutchTownName(StringBuilder &builder, uint32_t seed)
Generates Dutch town name from given seed.
Definition townname.cpp:448
MakeSwissTownName
static void MakeSwissTownName(StringBuilder &builder, uint32_t seed)
Generates Swiss town name from given seed.
Definition townname.cpp:814
SeedChanceBias
static int32_t SeedChanceBias(uint8_t shift_by, size_t max, uint32_t seed, int bias)
Generates a number from given seed.
Definition townname.cpp:201
SeedChance
static uint32_t SeedChance(uint8_t shift_by, size_t max, uint32_t seed)
Generates a number from given seed.
Definition townname.cpp:165
GetTownName
static void GetTownName(StringBuilder &builder, const TownNameParams *par, uint32_t townnameparts)
Fills builder with specified town name.
Definition townname.cpp:48
GenerateTownNameString
void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed)
Generates town name from given seed.
Definition townname.cpp:991
MakeAustrianTownName
static void MakeAustrianTownName(StringBuilder &builder, uint32_t seed)
Generates Austrian town name from given seed.
Definition townname.cpp:309
townname.h
Namepart tables for the town name generator.
townname_type.h
Definition of structures used for generating town names.