Translations update
[openttd/fttd.git] / src / townname.cpp
blob76770c455cd1fe4c34ff386fbd05f9319c4ab022
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 <http://www.gnu.org/licenses/>.
8 */
10 /** @file townname.cpp %Town name generators. */
12 #include "stdafx.h"
13 #include "string.h"
14 #include "townname_type.h"
15 #include "town.h"
16 #include "strings_func.h"
17 #include "core/random_func.hpp"
18 #include "genworld.h"
19 #include "gfx_layout.h"
21 #include "table/townname.h"
24 /**
25 * Initializes this struct from town data
26 * @param t town for which we will be printing name later
28 TownNameParams::TownNameParams(const Town *t) :
29 grfid(t->townnamegrfid), // by default, use supplied data
30 type(t->townnametype)
32 if (t->townnamegrfid != 0 && GetGRFTownName(t->townnamegrfid) == NULL) {
33 /* Fallback to english original */
34 this->grfid = 0;
35 this->type = SPECSTR_TOWNNAME_ENGLISH;
36 return;
41 /**
42 * Fills buffer with specified town name
43 * @param buf buffer
44 * @param par town name parameters
45 * @param townnameparts 'encoded' town name
47 void AppendTownName (stringb *buf, const TownNameParams *par, uint32 townnameparts)
49 if (par->grfid == 0) {
50 int64 args_array[1] = { townnameparts };
51 StringParameters tmp_params(args_array);
52 AppendStringWithArgs (buf, par->type, &tmp_params);
53 } else {
54 GRFTownNameGenerate (buf, par->grfid, par->type, townnameparts);
59 /**
60 * Fills buffer with town's name
61 * @param buff buffer start
62 * @param t we want to get name of this town
64 void AppendTownName (stringb *buf, const Town *t)
66 TownNameParams par(t);
67 AppendTownName (buf, &par, t->townnameparts);
71 /**
72 * Verifies the town name is valid and unique.
73 * @param r random bits
74 * @param par town name parameters
75 * @param town_names if a name is generated, check its uniqueness with the set
76 * @return true iff name is valid and unique
78 bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names)
80 /* reserve space for extra unicode character and terminating '\0' */
81 sstring <(MAX_LENGTH_TOWN_NAME_CHARS + 1) * MAX_CHAR_LENGTH> buf1, buf2;
83 AppendTownName (&buf1, par, r);
85 /* Check size and width */
86 if (buf1.utf8length() >= MAX_LENGTH_TOWN_NAME_CHARS) return false;
88 if (town_names != NULL) {
89 if (town_names->find(buf1.c_str()) != town_names->end()) return false;
90 town_names->insert(buf1.c_str());
91 } else {
92 const Town *t;
93 FOR_ALL_TOWNS(t) {
94 /* We can't just compare the numbers since
95 * several numbers may map to a single name. */
96 const char *buf = t->name;
97 if (buf == NULL) {
98 buf2.clear();
99 AppendTownName (&buf2, t);
100 buf = buf2.c_str();
102 if (strcmp(buf1.c_str(), buf) == 0) return false;
106 return true;
111 * Generates valid town name.
112 * @param townnameparts if a name is generated, it's stored there
113 * @param town_names if a name is generated, check its uniqueness with the set
114 * @return true iff a name was generated
116 bool GenerateTownName(uint32 *townnameparts, TownNames *town_names)
118 /* Do not set too low tries, since when we run out of names, we loop
119 * for #tries only one time anyway - then we stop generating more
120 * towns. Do not show it too high neither, since looping through all
121 * the other towns may take considerable amount of time (10000 is
122 * too much). */
123 TownNameParams par(_settings_game.game_creation.town_name);
125 /* This function is called very often without entering the gameloop
126 * inbetween. So reset layout cache to prevent it from growing too big. */
127 Layouter::ReduceLineCache();
129 for (int i = 1000; i != 0; i--) {
130 uint32 r = _generating_world ? Random() : InteractiveRandom();
131 if (!VerifyTownName(r, &par, town_names)) continue;
133 *townnameparts = r;
134 return true;
137 return false;
143 * Generates a number from given seed.
144 * @param shift_by number of bits seed is shifted to the right
145 * @param max generated number is in interval 0...max-1
146 * @param seed seed
147 * @return seed transformed to a number from given range
149 static inline uint32 SeedChance(byte shift_by, int max, uint32 seed)
151 return (GB(seed, shift_by, 16) * max) >> 16;
156 * Generates a number from given seed. Uses different algorithm than SeedChance().
157 * @param shift_by number of bits seed is shifted to the right
158 * @param max generated number is in interval 0...max-1
159 * @param seed seed
160 * @return seed transformed to a number from given range
162 static inline uint32 SeedModChance(byte shift_by, int max, uint32 seed)
164 /* This actually gives *MUCH* more even distribution of the values
165 * than SeedChance(), which is absolutely horrible in that. If
166 * you do not believe me, try with i.e. the Czech town names,
167 * compare the words (nicely visible on prefixes) generated by
168 * SeedChance() and SeedModChance(). Do not get discouraged by the
169 * never-use-modulo myths, which hold true only for the linear
170 * congruential generators (and Random() isn't such a generator).
171 * --pasky
172 * TODO: Perhaps we should use it for all the name generators? --pasky */
173 return (seed >> shift_by) % max;
178 * Generates a number from given seed.
179 * @param shift_by number of bits seed is shifted to the right
180 * @param max generated number is in interval -bias...max-1
181 * @param seed seed
182 * @param bias minimum value that can be returned
183 * @return seed transformed to a number from given range
185 static inline int32 SeedChanceBias(byte shift_by, int max, uint32 seed, int bias)
187 return SeedChance(shift_by, max + bias, seed) - bias;
192 * Replaces english curses and ugly letter combinations by nicer ones.
193 * @param buf buffer with town name
194 * @param original English (Original) generator was used
196 static void ReplaceEnglishWords(char *buf, bool original)
198 static const uint N = 9;
199 static const char org[N + 1][4] = {
200 {'C','u','n','t'}, {'S','l','a','g'}, {'S','l','u','t'},
201 {'F','a','r','t'}, {'D','r','a','r'}, {'D','r','e','h'},
202 {'F','r','a','r'}, {'G','r','a','r'}, {'B','r','a','r'},
203 {'W','r','a','r'}
205 static const char rep[N + 2][4] = {
206 {'E','a','s','t'}, {'P','i','t','s'}, {'E','d','i','n'},
207 {'B','o','o','t'}, {'Q','u','a','r'}, {'B','a','s','h'},
208 {'S','h','o','r'}, {'A','b','e','r'}, {'O','v','e','r'},
209 {'I','n','v','e'}, {'S','t','a','n'}
212 assert (strlen(buf) >= 4);
214 for (uint i = 0; i < N; i++) {
215 if (memcmp (buf, org[i], 4) == 0) {
216 memcpy (buf, rep[i], 4);
217 return;
221 if (memcmp (buf, org[N], 4) == 0) {
222 memcpy (buf, rep[original ? N : N + 1], 4);
227 * Generates English (Original) town name from given seed.
228 * @param buf output buffer
229 * @param seed town name seed
231 static void MakeEnglishOriginalTownName (stringb *buf, uint32 seed)
233 size_t orig_length = buf->length();
235 /* optional first segment */
236 int i = SeedChanceBias(0, lengthof(_name_original_english_1), seed, 50);
237 if (i >= 0) buf->append (_name_original_english_1[i]);
239 /* mandatory middle segments */
240 buf->append (_name_original_english_2[SeedChance( 4, lengthof(_name_original_english_2), seed)]);
241 buf->append (_name_original_english_3[SeedChance( 7, lengthof(_name_original_english_3), seed)]);
242 buf->append (_name_original_english_4[SeedChance(10, lengthof(_name_original_english_4), seed)]);
243 buf->append (_name_original_english_5[SeedChance(13, lengthof(_name_original_english_5), seed)]);
245 /* optional last segment */
246 i = SeedChanceBias(15, lengthof(_name_original_english_6), seed, 60);
247 if (i >= 0) buf->append (_name_original_english_6[i]);
249 /* Ce, Ci => Ke, Ki */
250 if (buf->buffer[orig_length] == 'C' && (buf->buffer[orig_length + 1] == 'e' || buf->buffer[orig_length + 1] == 'i')) {
251 buf->buffer[orig_length] = 'K';
254 assert (buf->length() - orig_length >= 4);
255 ReplaceEnglishWords (&buf->buffer[orig_length], true);
260 * Generates English (Additional) town name from given seed.
261 * @param buf output buffer
262 * @param seed town name seed
264 static void MakeEnglishAdditionalTownName (stringb *buf, uint32 seed)
266 size_t orig_length = buf->length();
268 /* optional first segment */
269 int i = SeedChanceBias(0, lengthof(_name_additional_english_prefix), seed, 50);
270 if (i >= 0) buf->append (_name_additional_english_prefix[i]);
272 if (SeedChance(3, 20, seed) >= 14) {
273 buf->append (_name_additional_english_1a[SeedChance(6, lengthof(_name_additional_english_1a), seed)]);
274 } else {
275 buf->append (_name_additional_english_1b1[SeedChance(6, lengthof(_name_additional_english_1b1), seed)]);
276 buf->append (_name_additional_english_1b2[SeedChance(9, lengthof(_name_additional_english_1b2), seed)]);
277 if (SeedChance(11, 20, seed) >= 4) {
278 buf->append (_name_additional_english_1b3a[SeedChance(12, lengthof(_name_additional_english_1b3a), seed)]);
279 } else {
280 buf->append (_name_additional_english_1b3b[SeedChance(12, lengthof(_name_additional_english_1b3b), seed)]);
284 buf->append (_name_additional_english_2[SeedChance(14, lengthof(_name_additional_english_2), seed)]);
286 /* optional last segment */
287 i = SeedChanceBias(15, lengthof(_name_additional_english_3), seed, 60);
288 if (i >= 0) buf->append (_name_additional_english_3[i]);
290 assert (buf->length() - orig_length >= 4);
291 ReplaceEnglishWords (&buf->buffer[orig_length], false);
296 * Generates Austrian town name from given seed.
297 * @param buf output buffer
298 * @param seed town name seed
300 static void MakeAustrianTownName (stringb *buf, uint32 seed)
302 /* Bad, Maria, Gross, ... */
303 int i = SeedChanceBias(0, lengthof(_name_austrian_a1), seed, 15);
304 if (i >= 0) buf->append (_name_austrian_a1[i]);
306 int j = 0;
308 i = SeedChance(4, 6, seed);
309 if (i >= 4) {
310 /* Kaisers-kirchen */
311 buf->append (_name_austrian_a2[SeedChance( 7, lengthof(_name_austrian_a2), seed)]);
312 buf->append (_name_austrian_a3[SeedChance(13, lengthof(_name_austrian_a3), seed)]);
313 } else if (i >= 2) {
314 /* St. Johann */
315 buf->append (_name_austrian_a5[SeedChance( 7, lengthof(_name_austrian_a5), seed)]);
316 buf->append (_name_austrian_a6[SeedChance( 9, lengthof(_name_austrian_a6), seed)]);
317 j = 1; // More likely to have a " an der " or " am "
318 } else {
319 /* Zell */
320 buf->append (_name_austrian_a4[SeedChance( 7, lengthof(_name_austrian_a4), seed)]);
323 i = SeedChance(1, 6, seed);
324 if (i >= 4 - j) {
325 /* an der Donau (rivers) */
326 buf->append (_name_austrian_f1[SeedChance(4, lengthof(_name_austrian_f1), seed)]);
327 buf->append (_name_austrian_f2[SeedChance(5, lengthof(_name_austrian_f2), seed)]);
328 } else if (i >= 2 - j) {
329 /* am Dachstein (mountains) */
330 buf->append (_name_austrian_b1[SeedChance(4, lengthof(_name_austrian_b1), seed)]);
331 buf->append (_name_austrian_b2[SeedChance(5, lengthof(_name_austrian_b2), seed)]);
337 * Generates German town name from given seed.
338 * @param buf output buffer
339 * @param seed town name seed
341 static void MakeGermanTownName (stringb *buf, uint32 seed)
343 uint seed_derivative = SeedChance(7, 28, seed);
345 /* optional prefix */
346 if (seed_derivative == 12 || seed_derivative == 19) {
347 uint i = SeedChance(2, lengthof(_name_german_pre), seed);
348 buf->append (_name_german_pre[i]);
351 /* mandatory middle segments including option of hardcoded name */
352 uint i = SeedChance(3, lengthof(_name_german_real) + lengthof(_name_german_1), seed);
353 if (i < lengthof(_name_german_real)) {
354 buf->append (_name_german_real[i]);
355 } else {
356 buf->append (_name_german_1[i - lengthof(_name_german_real)]);
358 i = SeedChance(5, lengthof(_name_german_2), seed);
359 buf->append (_name_german_2[i]);
362 /* optional suffix */
363 if (seed_derivative == 24) {
364 i = SeedChance(9, lengthof(_name_german_4_an_der) + lengthof(_name_german_4_am), seed);
365 if (i < lengthof(_name_german_4_an_der)) {
366 buf->append (_name_german_3_an_der[0]);
367 buf->append (_name_german_4_an_der[i]);
368 } else {
369 buf->append (_name_german_3_am[0]);
370 buf->append (_name_german_4_am[i - lengthof(_name_german_4_an_der)]);
377 * Generates Latin-American town name from given seed.
378 * @param buf output buffer
379 * @param seed town name seed
381 static void MakeSpanishTownName (stringb *buf, uint32 seed)
383 buf->append (_name_spanish_real[SeedChance(0, lengthof(_name_spanish_real), seed)]);
388 * Generates French town name from given seed.
389 * @param buf output buffer
390 * @param seed town name seed
392 static void MakeFrenchTownName (stringb *buf, uint32 seed)
394 buf->append (_name_french_real[SeedChance(0, lengthof(_name_french_real), seed)]);
399 * Generates Silly town name from given seed.
400 * @param buf output buffer
401 * @param seed town name seed
403 static void MakeSillyTownName (stringb *buf, uint32 seed)
405 buf->append (_name_silly_1[SeedChance( 0, lengthof(_name_silly_1), seed)]);
406 buf->append (_name_silly_2[SeedChance(16, lengthof(_name_silly_2), seed)]);
411 * Generates Swedish town name from given seed.
412 * @param buf output buffer
413 * @param seed town name seed
415 static void MakeSwedishTownName (stringb *buf, uint32 seed)
417 /* optional first segment */
418 int i = SeedChanceBias(0, lengthof(_name_swedish_1), seed, 50);
419 if (i >= 0) buf->append (_name_swedish_1[i]);
421 /* mandatory middle segments including option of hardcoded name */
422 if (SeedChance(4, 5, seed) >= 3) {
423 buf->append (_name_swedish_2[SeedChance( 7, lengthof(_name_swedish_2), seed)]);
424 } else {
425 buf->append (_name_swedish_2a[SeedChance( 7, lengthof(_name_swedish_2a), seed)]);
426 buf->append (_name_swedish_2b[SeedChance(10, lengthof(_name_swedish_2b), seed)]);
427 buf->append (_name_swedish_2c[SeedChance(13, lengthof(_name_swedish_2c), seed)]);
430 buf->append (_name_swedish_3[SeedChance(16, lengthof(_name_swedish_3), seed)]);
435 * Generates Dutch town name from given seed.
436 * @param buf output buffer
437 * @param seed town name seed
439 static void MakeDutchTownName (stringb *buf, uint32 seed)
441 /* optional first segment */
442 int i = SeedChanceBias(0, lengthof(_name_dutch_1), seed, 50);
443 if (i >= 0) buf->append (_name_dutch_1[i]);
445 /* mandatory middle segments including option of hardcoded name */
446 if (SeedChance(6, 9, seed) > 4) {
447 buf->append (_name_dutch_2[SeedChance( 9, lengthof(_name_dutch_2), seed)]);
448 } else {
449 buf->append (_name_dutch_3[SeedChance( 9, lengthof(_name_dutch_3), seed)]);
450 buf->append (_name_dutch_4[SeedChance(12, lengthof(_name_dutch_4), seed)]);
453 buf->append (_name_dutch_5[SeedChance(15, lengthof(_name_dutch_5), seed)]);
458 * Generates Finnish town name from given seed.
459 * @param buf output buffer
460 * @param seed town name seed
462 static void MakeFinnishTownName (stringb *buf, uint32 seed)
464 /* Select randomly if town name should consists of one or two parts. */
465 if (SeedChance(0, 15, seed) >= 10) {
466 buf->append (_name_finnish_real[SeedChance(2, lengthof(_name_finnish_real), seed)]);
467 return;
470 if (SeedChance(0, 15, seed) >= 5) {
471 const char *orig = &buf->buffer[buf->length()];
473 /* A two-part name by combining one of _name_finnish_1 + "la"/"lä"
474 * The reason for not having the contents of _name_finnish_{1,2} in the same table is
475 * that the ones in _name_finnish_2 are not good for this purpose. */
476 uint sel = SeedChance( 0, lengthof(_name_finnish_1), seed);
477 buf->append (_name_finnish_1[sel]);
478 assert (!buf->empty());
479 char *end = &buf->buffer[buf->length() - 1];
480 assert(end >= orig);
481 if (*end == 'i') *end = 'e';
482 if (strstr(orig, "a") != NULL || strstr(orig, "o") != NULL || strstr(orig, "u") != NULL ||
483 strstr(orig, "A") != NULL || strstr(orig, "O") != NULL || strstr(orig, "U") != NULL) {
484 buf->append ("la");
485 } else {
486 buf->append ("l\xC3\xA4");
488 return;
491 /* A two-part name by combining one of _name_finnish_{1,2} + _name_finnish_3.
492 * Why aren't _name_finnish_{1,2} just one table? See above. */
493 uint sel = SeedChance(2, lengthof(_name_finnish_1) + lengthof(_name_finnish_2), seed);
494 if (sel >= lengthof(_name_finnish_1)) {
495 buf->append (_name_finnish_2[sel - lengthof(_name_finnish_1)]);
496 } else {
497 buf->append (_name_finnish_1[sel]);
500 buf->append (_name_finnish_3[SeedChance(10, lengthof(_name_finnish_3), seed)]);
505 * Generates Polish town name from given seed.
506 * @param buf output buffer
507 * @param seed town name seed
509 static void MakePolishTownName (stringb *buf, uint32 seed)
511 /* optional first segment */
512 uint i = SeedChance(0,
513 lengthof(_name_polish_2_o) + lengthof(_name_polish_2_m) +
514 lengthof(_name_polish_2_f) + lengthof(_name_polish_2_n),
515 seed);
516 uint j = SeedChance(2, 20, seed);
519 if (i < lengthof(_name_polish_2_o)) {
520 buf->append (_name_polish_2_o[SeedChance(3, lengthof(_name_polish_2_o), seed)]);
521 return;
524 if (i < lengthof(_name_polish_2_m) + lengthof(_name_polish_2_o)) {
525 if (j < 4) {
526 buf->append (_name_polish_1_m[SeedChance(5, lengthof(_name_polish_1_m), seed)]);
529 buf->append (_name_polish_2_m[SeedChance(7, lengthof(_name_polish_2_m), seed)]);
531 if (j >= 4 && j < 16) {
532 buf->append (_name_polish_3_m[SeedChance(10, lengthof(_name_polish_3_m), seed)]);
536 if (i < lengthof(_name_polish_2_f) + lengthof(_name_polish_2_m) + lengthof(_name_polish_2_o)) {
537 if (j < 4) {
538 buf->append (_name_polish_1_f[SeedChance(5, lengthof(_name_polish_1_f), seed)]);
541 buf->append (_name_polish_2_f[SeedChance(7, lengthof(_name_polish_2_f), seed)]);
543 if (j >= 4 && j < 16) {
544 buf->append (_name_polish_3_f[SeedChance(10, lengthof(_name_polish_3_f), seed)]);
547 return;
550 if (j < 4) {
551 buf->append (_name_polish_1_n[SeedChance(5, lengthof(_name_polish_1_n), seed)]);
554 buf->append (_name_polish_2_n[SeedChance(7, lengthof(_name_polish_2_n), seed)]);
556 if (j >= 4 && j < 16) {
557 buf->append (_name_polish_3_n[SeedChance(10, lengthof(_name_polish_3_n), seed)]);
563 * Generates Czech town name from given seed.
564 * @param buf output buffer
565 * @param seed town name seed
567 static void MakeCzechTownName (stringb *buf, uint32 seed)
569 /* 1:3 chance to use a real name. */
570 if (SeedModChance(0, 4, seed) == 0) {
571 buf->append (_name_czech_real[SeedModChance(4, lengthof(_name_czech_real), seed)]);
572 return;
575 /* Probability of prefixes/suffixes
576 * 0..11 prefix, 12..13 prefix+suffix, 14..17 suffix, 18..31 nothing */
577 int prob_tails = SeedModChance(2, 32, seed);
578 bool do_prefix = prob_tails < 12;
579 bool do_suffix = prob_tails > 11 && prob_tails < 17;
580 bool dynamic_subst;
582 /* IDs of the respective parts */
583 int prefix = 0, ending = 0, suffix = 0;
584 uint postfix = 0;
585 uint stem;
587 /* The select criteria. */
588 CzechGender gender;
589 CzechChoose choose;
590 CzechAllow allow;
592 if (do_prefix) prefix = SeedModChance(5, lengthof(_name_czech_adj) * 12, seed) / 12;
593 if (do_suffix) suffix = SeedModChance(7, lengthof(_name_czech_suffix), seed);
594 /* 3:1 chance 3:1 to use dynamic substantive */
595 stem = SeedModChance(9,
596 lengthof(_name_czech_subst_full) + 3 * lengthof(_name_czech_subst_stem),
597 seed);
598 if (stem < lengthof(_name_czech_subst_full)) {
599 /* That was easy! */
600 dynamic_subst = false;
601 gender = _name_czech_subst_full[stem].gender;
602 choose = _name_czech_subst_full[stem].choose;
603 allow = _name_czech_subst_full[stem].allow;
604 } else {
605 unsigned int map[lengthof(_name_czech_subst_ending)];
606 int ending_start = -1, ending_stop = -1;
608 /* Load the substantive */
609 dynamic_subst = true;
610 stem -= lengthof(_name_czech_subst_full);
611 stem %= lengthof(_name_czech_subst_stem);
612 gender = _name_czech_subst_stem[stem].gender;
613 choose = _name_czech_subst_stem[stem].choose;
614 allow = _name_czech_subst_stem[stem].allow;
616 /* Load the postfix (1:1 chance that a postfix will be inserted) */
617 postfix = SeedModChance(14, lengthof(_name_czech_subst_postfix) * 2, seed);
619 if (choose & CZC_POSTFIX) {
620 /* Always get a real postfix. */
621 postfix %= lengthof(_name_czech_subst_postfix);
623 if (choose & CZC_NOPOSTFIX) {
624 /* Always drop a postfix. */
625 postfix += lengthof(_name_czech_subst_postfix);
627 if (postfix < lengthof(_name_czech_subst_postfix)) {
628 choose |= CZC_POSTFIX;
629 } else {
630 choose |= CZC_NOPOSTFIX;
633 /* Localize the array segment containing a good gender */
634 for (ending = 0; ending < (int)lengthof(_name_czech_subst_ending); ending++) {
635 const CzechNameSubst *e = &_name_czech_subst_ending[ending];
637 if (gender == CZG_FREE ||
638 (gender == CZG_NFREE && e->gender != CZG_SNEUT && e->gender != CZG_PNEUT) ||
639 gender == e->gender) {
640 if (ending_start < 0) {
641 ending_start = ending;
643 } else if (ending_start >= 0) {
644 ending_stop = ending - 1;
645 break;
648 if (ending_stop < 0) {
649 /* Whoa. All the endings matched. */
650 ending_stop = ending - 1;
653 /* Make a sequential map of the items with good mask */
654 size_t i = 0;
655 for (ending = ending_start; ending <= ending_stop; ending++) {
656 const CzechNameSubst *e = &_name_czech_subst_ending[ending];
658 if ((e->choose & choose) == choose && (e->allow & allow) != 0) {
659 map[i++] = ending;
662 assert(i > 0);
664 /* Load the ending */
665 ending = map[SeedModChance(16, (int)i, seed)];
666 /* Override possible CZG_*FREE; this must be a real gender,
667 * otherwise we get overflow when modifying the adjectivum. */
668 gender = _name_czech_subst_ending[ending].gender;
669 assert(gender != CZG_FREE && gender != CZG_NFREE);
672 if (do_prefix && (_name_czech_adj[prefix].choose & choose) != choose) {
673 /* Throw away non-matching prefix. */
674 do_prefix = false;
677 /* Now finally construct the name */
678 if (do_prefix) {
679 size_t orig_length = buf->length();
681 CzechPattern pattern = _name_czech_adj[prefix].pattern;
683 buf->append (_name_czech_adj[prefix].name);
685 assert (!buf->empty());
686 size_t end_length = buf->length() - 1;
687 /* Find the first character in a UTF-8 sequence */
688 while (GB(buf->buffer[end_length], 6, 2) == 2) end_length--;
690 if (gender == CZG_SMASC && pattern == CZP_PRIVL) {
691 assert (end_length >= orig_length + 2);
692 /* -ovX -> -uv */
693 buf->buffer[end_length - 2] = 'u';
694 assert(buf->buffer[end_length - 1] == 'v');
695 buf->truncate (end_length);
696 } else {
697 assert (end_length >= orig_length);
698 buf->append (_name_czech_patmod[gender][pattern]);
701 buf->append (' ');
704 if (dynamic_subst) {
705 buf->append (_name_czech_subst_stem[stem].name);
706 if (postfix < lengthof(_name_czech_subst_postfix)) {
707 const char *poststr = _name_czech_subst_postfix[postfix];
708 const char *endstr = _name_czech_subst_ending[ending].name;
710 size_t postlen = strlen(poststr);
711 size_t endlen = strlen(endstr);
712 assert(postlen > 0 && endlen > 0);
714 /* Kill the "avava" and "Jananna"-like cases */
715 if (postlen < 2 || postlen > endlen ||
716 ((poststr[1] != 'v' || poststr[1] != endstr[1]) &&
717 poststr[2] != endstr[1])) {
718 buf->append (poststr);
720 /* k-i -> c-i, h-i -> z-i */
721 if (endstr[0] == 'i') {
722 assert (!buf->empty());
723 char *last = &buf->buffer[buf->length() - 1];
724 switch (*last) {
725 case 'k': *last = 'c'; break;
726 case 'h': *last = 'z'; break;
727 default: break;
732 buf->append (_name_czech_subst_ending[ending].name);
733 } else {
734 buf->append (_name_czech_subst_full[stem].name);
737 if (do_suffix) {
738 buf->append (' ');
739 buf->append (_name_czech_suffix[suffix]);
745 * Generates Romanian town name from given seed.
746 * @param buf output buffer
747 * @param seed town name seed
749 static void MakeRomanianTownName (stringb *buf, uint32 seed)
751 buf->append (_name_romanian_real[SeedChance(0, lengthof(_name_romanian_real), seed)]);
756 * Generates Slovak town name from given seed.
757 * @param buf output buffer
758 * @param seed town name seed
760 static void MakeSlovakTownName (stringb *buf, uint32 seed)
762 buf->append (_name_slovak_real[SeedChance(0, lengthof(_name_slovak_real), seed)]);
767 * Generates Norwegian town name from given seed.
768 * @param buf output buffer
769 * @param seed town name seed
771 static void MakeNorwegianTownName (stringb *buf, uint32 seed)
773 /* Use first 4 bit from seed to decide whether or not this town should
774 * have a real name 3/16 chance. Bit 0-3 */
775 if (SeedChance(0, 15, seed) < 3) {
776 /* Use 7bit for the realname table index. Bit 4-10 */
777 buf->append (_name_norwegian_real[SeedChance(4, lengthof(_name_norwegian_real), seed)]);
778 return;
781 /* Use 7bit for the first fake part. Bit 4-10 */
782 buf->append (_name_norwegian_1[SeedChance( 4, lengthof(_name_norwegian_1), seed)]);
783 /* Use 7bit for the last fake part. Bit 11-17 */
784 buf->append (_name_norwegian_2[SeedChance(11, lengthof(_name_norwegian_2), seed)]);
789 * Generates Hungarian town name from given seed.
790 * @param buf output buffer
791 * @param seed town name seed
793 static void MakeHungarianTownName (stringb *buf, uint32 seed)
795 if (SeedChance(12, 15, seed) < 3) {
796 buf->append (_name_hungarian_real[SeedChance(0, lengthof(_name_hungarian_real), seed)]);
797 return;
800 /* optional first segment */
801 uint i = SeedChance(3, lengthof(_name_hungarian_1) * 3, seed);
802 if (i < lengthof(_name_hungarian_1)) buf->append (_name_hungarian_1[i]);
804 /* mandatory middle segments */
805 buf->append (_name_hungarian_2[SeedChance(3, lengthof(_name_hungarian_2), seed)]);
806 buf->append (_name_hungarian_3[SeedChance(6, lengthof(_name_hungarian_3), seed)]);
808 /* optional last segment */
809 i = SeedChance(10, lengthof(_name_hungarian_4) * 3, seed);
810 if (i < lengthof(_name_hungarian_4)) {
811 buf->append (_name_hungarian_4[i]);
817 * Generates Swiss town name from given seed.
818 * @param buf output buffer
819 * @param seed town name seed
821 static void MakeSwissTownName (stringb *buf, uint32 seed)
823 buf->append (_name_swiss_real[SeedChance(0, lengthof(_name_swiss_real), seed)]);
828 * Generates Danish town name from given seed.
829 * @param buf output buffer
830 * @param seed town name seed
832 static void MakeDanishTownName (stringb *buf, uint32 seed)
834 /* optional first segment */
835 int i = SeedChanceBias(0, lengthof(_name_danish_1), seed, 50);
836 if (i >= 0) buf->append (_name_danish_1[i]);
838 /* middle segments removed as this algorithm seems to create much more realistic names */
839 buf->append (_name_danish_2[SeedChance( 7, lengthof(_name_danish_2), seed)]);
840 buf->append (_name_danish_3[SeedChance(16, lengthof(_name_danish_3), seed)]);
845 * Generates Turkish town name from given seed.
846 * @param buf output buffer
847 * @param seed town name seed
849 static void MakeTurkishTownName (stringb *buf, uint32 seed)
851 uint i = SeedModChance(0, 5, seed);
853 switch (i) {
854 case 0:
855 buf->append (_name_turkish_prefix[SeedModChance( 2, lengthof(_name_turkish_prefix), seed)]);
857 /* middle segment */
858 buf->append (_name_turkish_middle[SeedModChance( 4, lengthof(_name_turkish_middle), seed)]);
860 /* optional suffix */
861 if (SeedModChance(0, 7, seed) == 0) {
862 buf->append (_name_turkish_suffix[SeedModChance(10, lengthof(_name_turkish_suffix), seed)]);
864 break;
866 case 1: case 2:
867 buf->append (_name_turkish_prefix[SeedModChance( 2, lengthof(_name_turkish_prefix), seed)]);
868 buf->append (_name_turkish_suffix[SeedModChance( 4, lengthof(_name_turkish_suffix), seed)]);
869 break;
871 default:
872 buf->append (_name_turkish_real[SeedModChance( 4, lengthof(_name_turkish_real), seed)]);
873 break;
879 * Generates Italian town name from given seed.
880 * @param buf output buffer
881 * @param seed town name seed
883 static void MakeItalianTownName (stringb *buf, uint32 seed)
885 if (SeedModChance(0, 6, seed) == 0) { // real city names
886 buf->append (_name_italian_real[SeedModChance(4, lengthof(_name_italian_real), seed)]);
887 return;
890 static const char mascul_femin_italian[2] = { 'o', 'a' };
892 if (SeedModChance(0, 8, seed) == 0) { // prefix
893 buf->append (_name_italian_pref[SeedModChance(11, lengthof(_name_italian_pref), seed)]);
896 uint i = SeedChance(0, 2, seed);
897 if (i == 0) { // masculine form
898 buf->append (_name_italian_1m[SeedModChance(4, lengthof(_name_italian_1m), seed)]);
899 } else { // feminine form
900 buf->append (_name_italian_1f[SeedModChance(4, lengthof(_name_italian_1f), seed)]);
903 if (SeedModChance(3, 3, seed) == 0) {
904 buf->append (_name_italian_2[SeedModChance(11, lengthof(_name_italian_2), seed)]);
905 buf->append (mascul_femin_italian[i]);
906 } else {
907 buf->append (_name_italian_2i[SeedModChance(16, lengthof(_name_italian_2i), seed)]);
910 if (SeedModChance(15, 4, seed) == 0) {
911 if (SeedModChance(5, 2, seed) == 0) { // generic suffix
912 buf->append (_name_italian_3[SeedModChance(4, lengthof(_name_italian_3), seed)]);
913 } else { // river name suffix
914 buf->append (_name_italian_river1[SeedModChance( 4, lengthof(_name_italian_river1), seed)]);
915 buf->append (_name_italian_river2[SeedModChance(16, lengthof(_name_italian_river2), seed)]);
922 * Generates Catalan town name from given seed.
923 * @param buf output buffer
924 * @param seed town name seed
926 static void MakeCatalanTownName (stringb *buf, uint32 seed)
928 if (SeedModChance(0, 3, seed) == 0) { // real city names
929 buf->append (_name_catalan_real[SeedModChance(4, lengthof(_name_catalan_real), seed)]);
930 return;
933 if (SeedModChance(0, 2, seed) == 0) { // prefix
934 buf->append (_name_catalan_pref[SeedModChance(11, lengthof(_name_catalan_pref), seed)]);
937 uint i = SeedChance(0, 2, seed);
938 if (i == 0) { // masculine form
939 buf->append (_name_catalan_1m[SeedModChance( 4, lengthof(_name_catalan_1m), seed)]);
940 buf->append (_name_catalan_2m[SeedModChance(11, lengthof(_name_catalan_2m), seed)]);
941 } else { // feminine form
942 buf->append (_name_catalan_1f[SeedModChance( 4, lengthof(_name_catalan_1f), seed)]);
943 buf->append (_name_catalan_2f[SeedModChance(11, lengthof(_name_catalan_2f), seed)]);
946 if (SeedModChance(15, 5, seed) == 0) {
947 if (SeedModChance(5, 2, seed) == 0) { // generic suffix
948 buf->append (_name_catalan_3[SeedModChance(4, lengthof(_name_catalan_3), seed)]);
949 } else { // river name suffix
950 buf->append (_name_catalan_river1[SeedModChance(4, lengthof(_name_catalan_river1), seed)]);
957 * Type for all town name generator functions.
958 * @param buf The buffer to write the name to.
959 * @param seed The seed of the town name.
961 typedef void TownNameGenerator (stringb *buf, uint32 seed);
963 /** Contains pointer to generator and minimum buffer size (not incl. terminating '\0') */
964 struct TownNameGeneratorParams {
965 byte min; ///< minimum number of characters that need to be printed for generator to work correctly
966 TownNameGenerator *proc; ///< generator itself
969 /** Town name generators */
970 static const TownNameGeneratorParams _town_name_generators[] = {
971 { 4, MakeEnglishOriginalTownName}, // replaces first 4 characters of name
972 { 0, MakeFrenchTownName},
973 { 0, MakeGermanTownName},
974 { 4, MakeEnglishAdditionalTownName}, // replaces first 4 characters of name
975 { 0, MakeSpanishTownName},
976 { 0, MakeSillyTownName},
977 { 0, MakeSwedishTownName},
978 { 0, MakeDutchTownName},
979 { 8, MakeFinnishTownName}, // _name_finnish_1
980 { 0, MakePolishTownName},
981 { 0, MakeSlovakTownName},
982 { 0, MakeNorwegianTownName},
983 { 0, MakeHungarianTownName},
984 { 0, MakeAustrianTownName},
985 { 0, MakeRomanianTownName},
986 { 28, MakeCzechTownName}, // _name_czech_adj + _name_czech_patmod + 1 + _name_czech_subst_stem + _name_czech_subst_postfix
987 { 0, MakeSwissTownName},
988 { 0, MakeDanishTownName},
989 { 0, MakeTurkishTownName},
990 { 0, MakeItalianTownName},
991 { 0, MakeCatalanTownName},
996 * Generates town name from given seed. a language.
997 * @param buf output buffer
998 * @param lang town name language
999 * @param seed generation seed
1001 void GenerateTownNameString (stringb *buf, size_t lang, uint32 seed)
1003 assert(lang < lengthof(_town_name_generators));
1005 /* Some generators need at least 9 bytes in buffer. English generators need 5 for
1006 * string replacing, others use constructions like strlen(buf)-3 and so on.
1007 * Finnish generator needs to fit all strings from _name_finnish_1.
1008 * Czech generator needs to fit almost whole town name...
1009 * These would break. Using another temporary buffer results in ~40% slower code,
1010 * so use it only when really needed. */
1011 const TownNameGeneratorParams *par = &_town_name_generators[lang];
1012 if (buf->capacity > par->min) {
1013 par->proc (buf, seed);
1014 return;
1017 char *buffer = AllocaM(char, par->min + 1);
1018 stringb tmp (par->min + 1, buffer);
1019 par->proc (&tmp, seed);
1021 buf->append (buffer);