OpenTTD Source  1.11.2
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 <http://www.gnu.org/licenses/>.
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 
19 #include "table/townname.h"
20 
21 #include "safeguards.h"
22 
23 
29  grfid(t->townnamegrfid), // by default, use supplied data
30  type(t->townnametype)
31 {
32  if (t->townnamegrfid != 0 && GetGRFTownName(t->townnamegrfid) == nullptr) {
33  /* Fallback to english original */
34  this->grfid = 0;
35  this->type = SPECSTR_TOWNNAME_ENGLISH;
36  return;
37  }
38 }
39 
40 
49 char *GetTownName(char *buff, const TownNameParams *par, uint32 townnameparts, const char *last)
50 {
51  if (par->grfid == 0) {
52  int64 args_array[1] = { townnameparts };
53  StringParameters tmp_params(args_array);
54  return GetStringWithArgs(buff, par->type, &tmp_params, last);
55  }
56 
57  return GRFTownNameGenerate(buff, par->grfid, par->type, townnameparts, last);
58 }
59 
60 
68 char *GetTownName(char *buff, const Town *t, const char *last)
69 {
70  TownNameParams par(t);
71  return GetTownName(buff, &par, t->townnameparts, last);
72 }
73 
74 
82 bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names)
83 {
84  /* reserve space for extra unicode character and terminating '\0' */
87 
88  GetTownName(buf1, par, r, lastof(buf1));
89 
90  /* Check size and width */
91  if (Utf8StringLength(buf1) >= MAX_LENGTH_TOWN_NAME_CHARS) return false;
92 
93  if (town_names != nullptr) {
94  if (town_names->find(buf1) != town_names->end()) return false;
95  town_names->insert(buf1);
96  } else {
97  for (const Town *t : Town::Iterate()) {
98  /* We can't just compare the numbers since
99  * several numbers may map to a single name. */
100  const char *buf = t->name.empty() ? nullptr : t->name.c_str();
101  if (buf == nullptr) {
102  GetTownName(buf2, t, lastof(buf2));
103  buf = buf2;
104  }
105  if (strcmp(buf1, buf) == 0) return false;
106  }
107  }
108 
109  return true;
110 }
111 
112 
119 bool GenerateTownName(uint32 *townnameparts, TownNames *town_names)
120 {
122 
123  /* This function is called very often without entering the gameloop
124  * in between. So reset layout cache to prevent it from growing too big. */
126 
127  /* Do not set i too low, since when we run out of names, we loop
128  * for #tries only one time anyway - then we stop generating more
129  * towns. Do not set it too high either, since looping through all
130  * the other towns may take considerable amount of time (10000 is
131  * too much). */
132  for (int i = 1000; i != 0; i--) {
133  uint32 r = _generating_world ? Random() : InteractiveRandom();
134  if (!VerifyTownName(r, &par, town_names)) continue;
135 
136  *townnameparts = r;
137  return true;
138  }
139 
140  return false;
141 }
142 
143 
144 
152 static inline uint32 SeedChance(byte shift_by, int max, uint32 seed)
153 {
154  return (GB(seed, shift_by, 16) * max) >> 16;
155 }
156 
157 
165 static inline uint32 SeedModChance(byte shift_by, int max, uint32 seed)
166 {
167  /* This actually gives *MUCH* more even distribution of the values
168  * than SeedChance(), which is absolutely horrible in that. If
169  * you do not believe me, try with i.e. the Czech town names,
170  * compare the words (nicely visible on prefixes) generated by
171  * SeedChance() and SeedModChance(). Do not get discouraged by the
172  * never-use-modulo myths, which hold true only for the linear
173  * congruential generators (and Random() isn't such a generator).
174  * --pasky
175  * TODO: Perhaps we should use it for all the name generators? --pasky */
176  return (seed >> shift_by) % max;
177 }
178 
179 
188 static inline int32 SeedChanceBias(byte shift_by, int max, uint32 seed, int bias)
189 {
190  return SeedChance(shift_by, max + bias, seed) - bias;
191 }
192 
193 
200 static void ReplaceWords(const char *org, const char *rep, char *buf)
201 {
202  assert(strlen(org) == 4 && strlen(rep) == 4 && strlen(buf) >= 4);
203  if (strncmp(buf, org, 4) == 0) memcpy(buf, rep, 4); // Safe as the string in buf is always more than 4 characters long.
204 }
205 
206 
212 static void ReplaceEnglishWords(char *buf, bool original)
213 {
214  ReplaceWords("Cunt", "East", buf);
215  ReplaceWords("Slag", "Pits", buf);
216  ReplaceWords("Slut", "Edin", buf);
217  if (!original) ReplaceWords("Fart", "Boot", buf); // never happens with 'English (Original)'
218  ReplaceWords("Drar", "Quar", buf);
219  ReplaceWords("Dreh", "Bash", buf);
220  ReplaceWords("Frar", "Shor", buf);
221  ReplaceWords("Grar", "Aber", buf);
222  ReplaceWords("Brar", "Over", buf);
223  ReplaceWords("Wrar", original ? "Inve" : "Stan", buf);
224 }
225 
232 static char *MakeEnglishOriginalTownName(char *buf, const char *last, uint32 seed)
233 {
234  char *orig = buf;
235 
236  /* optional first segment */
237  int i = SeedChanceBias(0, lengthof(_name_original_english_1), seed, 50);
238  if (i >= 0) buf = strecpy(buf, _name_original_english_1[i], last);
239 
240  /* mandatory middle segments */
241  buf = strecpy(buf, _name_original_english_2[SeedChance(4, lengthof(_name_original_english_2), seed)], last);
242  buf = strecpy(buf, _name_original_english_3[SeedChance(7, lengthof(_name_original_english_3), seed)], last);
243  buf = strecpy(buf, _name_original_english_4[SeedChance(10, lengthof(_name_original_english_4), seed)], last);
244  buf = strecpy(buf, _name_original_english_5[SeedChance(13, lengthof(_name_original_english_5), seed)], last);
245 
246  /* optional last segment */
247  i = SeedChanceBias(15, lengthof(_name_original_english_6), seed, 60);
248  if (i >= 0) buf = strecpy(buf, _name_original_english_6[i], last);
249 
250  /* Ce, Ci => Ke, Ki */
251  if (orig[0] == 'C' && (orig[1] == 'e' || orig[1] == 'i')) {
252  orig[0] = 'K';
253  }
254 
255  assert(buf - orig >= 4);
256  ReplaceEnglishWords(orig, true);
257 
258  return buf;
259 }
260 
261 
268 static char *MakeEnglishAdditionalTownName(char *buf, const char *last, uint32 seed)
269 {
270  char *orig = buf;
271 
272  /* optional first segment */
273  int i = SeedChanceBias(0, lengthof(_name_additional_english_prefix), seed, 50);
274  if (i >= 0) buf = strecpy(buf, _name_additional_english_prefix[i], last);
275 
276  if (SeedChance(3, 20, seed) >= 14) {
277  buf = strecpy(buf, _name_additional_english_1a[SeedChance(6, lengthof(_name_additional_english_1a), seed)], last);
278  } else {
279  buf = strecpy(buf, _name_additional_english_1b1[SeedChance(6, lengthof(_name_additional_english_1b1), seed)], last);
280  buf = strecpy(buf, _name_additional_english_1b2[SeedChance(9, lengthof(_name_additional_english_1b2), seed)], last);
281  if (SeedChance(11, 20, seed) >= 4) {
282  buf = strecpy(buf, _name_additional_english_1b3a[SeedChance(12, lengthof(_name_additional_english_1b3a), seed)], last);
283  } else {
284  buf = strecpy(buf, _name_additional_english_1b3b[SeedChance(12, lengthof(_name_additional_english_1b3b), seed)], last);
285  }
286  }
287 
288  buf = strecpy(buf, _name_additional_english_2[SeedChance(14, lengthof(_name_additional_english_2), seed)], last);
289 
290  /* optional last segment */
291  i = SeedChanceBias(15, lengthof(_name_additional_english_3), seed, 60);
292  if (i >= 0) buf = strecpy(buf, _name_additional_english_3[i], last);
293 
294  assert(buf - orig >= 4);
295  ReplaceEnglishWords(orig, false);
296 
297  return buf;
298 }
299 
300 
307 static char *MakeAustrianTownName(char *buf, const char *last, uint32 seed)
308 {
309  /* Bad, Maria, Gross, ... */
310  int i = SeedChanceBias(0, lengthof(_name_austrian_a1), seed, 15);
311  if (i >= 0) buf = strecpy(buf, _name_austrian_a1[i], last);
312 
313  int j = 0;
314 
315  i = SeedChance(4, 6, seed);
316  if (i >= 4) {
317  /* Kaisers-kirchen */
318  buf = strecpy(buf, _name_austrian_a2[SeedChance( 7, lengthof(_name_austrian_a2), seed)], last);
319  buf = strecpy(buf, _name_austrian_a3[SeedChance(13, lengthof(_name_austrian_a3), seed)], last);
320  } else if (i >= 2) {
321  /* St. Johann */
322  buf = strecpy(buf, _name_austrian_a5[SeedChance( 7, lengthof(_name_austrian_a5), seed)], last);
323  buf = strecpy(buf, _name_austrian_a6[SeedChance( 9, lengthof(_name_austrian_a6), seed)], last);
324  j = 1; // More likely to have a " an der " or " am "
325  } else {
326  /* Zell */
327  buf = strecpy(buf, _name_austrian_a4[SeedChance( 7, lengthof(_name_austrian_a4), seed)], last);
328  }
329 
330  i = SeedChance(1, 6, seed);
331  if (i >= 4 - j) {
332  /* an der Donau (rivers) */
333  buf = strecpy(buf, _name_austrian_f1[SeedChance(4, lengthof(_name_austrian_f1), seed)], last);
334  buf = strecpy(buf, _name_austrian_f2[SeedChance(5, lengthof(_name_austrian_f2), seed)], last);
335  } else if (i >= 2 - j) {
336  /* am Dachstein (mountains) */
337  buf = strecpy(buf, _name_austrian_b1[SeedChance(4, lengthof(_name_austrian_b1), seed)], last);
338  buf = strecpy(buf, _name_austrian_b2[SeedChance(5, lengthof(_name_austrian_b2), seed)], last);
339  }
340 
341  return buf;
342 }
343 
344 
351 static char *MakeGermanTownName(char *buf, const char *last, uint32 seed)
352 {
353  uint seed_derivative = SeedChance(7, 28, seed);
354 
355  /* optional prefix */
356  if (seed_derivative == 12 || seed_derivative == 19) {
357  uint i = SeedChance(2, lengthof(_name_german_pre), seed);
358  buf = strecpy(buf, _name_german_pre[i], last);
359  }
360 
361  /* mandatory middle segments including option of hardcoded name */
362  uint i = SeedChance(3, lengthof(_name_german_real) + lengthof(_name_german_1), seed);
363  if (i < lengthof(_name_german_real)) {
364  buf = strecpy(buf, _name_german_real[i], last);
365  } else {
366  buf = strecpy(buf, _name_german_1[i - lengthof(_name_german_real)], last);
367 
368  i = SeedChance(5, lengthof(_name_german_2), seed);
369  buf = strecpy(buf, _name_german_2[i], last);
370  }
371 
372  /* optional suffix */
373  if (seed_derivative == 24) {
374  i = SeedChance(9, lengthof(_name_german_4_an_der) + lengthof(_name_german_4_am), seed);
375  if (i < lengthof(_name_german_4_an_der)) {
376  buf = strecpy(buf, _name_german_3_an_der[0], last);
377  buf = strecpy(buf, _name_german_4_an_der[i], last);
378  } else {
379  buf = strecpy(buf, _name_german_3_am[0], last);
380  buf = strecpy(buf, _name_german_4_am[i - lengthof(_name_german_4_an_der)], last);
381  }
382  }
383 
384  return buf;
385 }
386 
387 
394 static char *MakeSpanishTownName(char *buf, const char *last, uint32 seed)
395 {
396  return strecpy(buf, _name_spanish_real[SeedChance(0, lengthof(_name_spanish_real), seed)], last);
397 }
398 
399 
406 static char *MakeFrenchTownName(char *buf, const char *last, uint32 seed)
407 {
408  return strecpy(buf, _name_french_real[SeedChance(0, lengthof(_name_french_real), seed)], last);
409 }
410 
411 
418 static char *MakeSillyTownName(char *buf, const char *last, uint32 seed)
419 {
420  buf = strecpy(buf, _name_silly_1[SeedChance( 0, lengthof(_name_silly_1), seed)], last);
421  buf = strecpy(buf, _name_silly_2[SeedChance(16, lengthof(_name_silly_2), seed)], last);
422 
423  return buf;
424 }
425 
426 
433 static char *MakeSwedishTownName(char *buf, const char *last, uint32 seed)
434 {
435  /* optional first segment */
436  int i = SeedChanceBias(0, lengthof(_name_swedish_1), seed, 50);
437  if (i >= 0) buf = strecpy(buf, _name_swedish_1[i], last);
438 
439  /* mandatory middle segments including option of hardcoded name */
440  if (SeedChance(4, 5, seed) >= 3) {
441  buf = strecpy(buf, _name_swedish_2[SeedChance( 7, lengthof(_name_swedish_2), seed)], last);
442  } else {
443  buf = strecpy(buf, _name_swedish_2a[SeedChance( 7, lengthof(_name_swedish_2a), seed)], last);
444  buf = strecpy(buf, _name_swedish_2b[SeedChance(10, lengthof(_name_swedish_2b), seed)], last);
445  buf = strecpy(buf, _name_swedish_2c[SeedChance(13, lengthof(_name_swedish_2c), seed)], last);
446  }
447 
448  buf = strecpy(buf, _name_swedish_3[SeedChance(16, lengthof(_name_swedish_3), seed)], last);
449 
450  return buf;
451 }
452 
453 
460 static char *MakeDutchTownName(char *buf, const char *last, uint32 seed)
461 {
462  /* optional first segment */
463  int i = SeedChanceBias(0, lengthof(_name_dutch_1), seed, 50);
464  if (i >= 0) buf = strecpy(buf, _name_dutch_1[i], last);
465 
466  /* mandatory middle segments including option of hardcoded name */
467  if (SeedChance(6, 9, seed) > 4) {
468  buf = strecpy(buf, _name_dutch_2[SeedChance( 9, lengthof(_name_dutch_2), seed)], last);
469  } else {
470  buf = strecpy(buf, _name_dutch_3[SeedChance( 9, lengthof(_name_dutch_3), seed)], last);
471  buf = strecpy(buf, _name_dutch_4[SeedChance(12, lengthof(_name_dutch_4), seed)], last);
472  }
473 
474  buf = strecpy(buf, _name_dutch_5[SeedChance(15, lengthof(_name_dutch_5), seed)], last);
475 
476  return buf;
477 }
478 
479 
486 static char *MakeFinnishTownName(char *buf, const char *last, uint32 seed)
487 {
488  char *orig = buf;
489 
490  /* Select randomly if town name should consists of one or two parts. */
491  if (SeedChance(0, 15, seed) >= 10) {
492  return strecpy(buf, _name_finnish_real[SeedChance(2, lengthof(_name_finnish_real), seed)], last);
493  }
494 
495  if (SeedChance(0, 15, seed) >= 5) {
496  /* A two-part name by combining one of _name_finnish_1 + "la"/"lä"
497  * The reason for not having the contents of _name_finnish_{1,2} in the same table is
498  * that the ones in _name_finnish_2 are not good for this purpose. */
499  uint sel = SeedChance( 0, lengthof(_name_finnish_1), seed);
500  buf = strecpy(buf, _name_finnish_1[sel], last);
501  char *end = buf - 1;
502  assert(end >= orig);
503  if (*end == 'i') *end = 'e';
504  if (strstr(orig, "a") != nullptr || strstr(orig, "o") != nullptr || strstr(orig, "u") != nullptr ||
505  strstr(orig, "A") != nullptr || strstr(orig, "O") != nullptr || strstr(orig, "U") != nullptr) {
506  buf = strecpy(buf, "la", last);
507  } else {
508  buf = strecpy(buf, u8"l\u00e4", last);
509  }
510  return buf;
511  }
512 
513  /* A two-part name by combining one of _name_finnish_{1,2} + _name_finnish_3.
514  * Why aren't _name_finnish_{1,2} just one table? See above. */
515  uint sel = SeedChance(2, lengthof(_name_finnish_1) + lengthof(_name_finnish_2), seed);
516  if (sel >= lengthof(_name_finnish_1)) {
517  buf = strecpy(buf, _name_finnish_2[sel - lengthof(_name_finnish_1)], last);
518  } else {
519  buf = strecpy(buf, _name_finnish_1[sel], last);
520  }
521 
522  buf = strecpy(buf, _name_finnish_3[SeedChance(10, lengthof(_name_finnish_3), seed)], last);
523 
524  return buf;
525 }
526 
527 
534 static char *MakePolishTownName(char *buf, const char *last, uint32 seed)
535 {
536  /* optional first segment */
537  uint i = SeedChance(0,
538  lengthof(_name_polish_2_o) + lengthof(_name_polish_2_m) +
539  lengthof(_name_polish_2_f) + lengthof(_name_polish_2_n),
540  seed);
541  uint j = SeedChance(2, 20, seed);
542 
543 
544  if (i < lengthof(_name_polish_2_o)) {
545  return strecpy(buf, _name_polish_2_o[SeedChance(3, lengthof(_name_polish_2_o), seed)], last);
546  }
547 
548  if (i < lengthof(_name_polish_2_m) + lengthof(_name_polish_2_o)) {
549  if (j < 4) {
550  buf = strecpy(buf, _name_polish_1_m[SeedChance(5, lengthof(_name_polish_1_m), seed)], last);
551  }
552 
553  buf = strecpy(buf, _name_polish_2_m[SeedChance(7, lengthof(_name_polish_2_m), seed)], last);
554 
555  if (j >= 4 && j < 16) {
556  buf = strecpy(buf, _name_polish_3_m[SeedChance(10, lengthof(_name_polish_3_m), seed)], last);
557  }
558 
559  return buf;
560  }
561 
562  if (i < lengthof(_name_polish_2_f) + lengthof(_name_polish_2_m) + lengthof(_name_polish_2_o)) {
563  if (j < 4) {
564  buf = strecpy(buf, _name_polish_1_f[SeedChance(5, lengthof(_name_polish_1_f), seed)], last);
565  }
566 
567  buf = strecpy(buf, _name_polish_2_f[SeedChance(7, lengthof(_name_polish_2_f), seed)], last);
568 
569  if (j >= 4 && j < 16) {
570  buf = strecpy(buf, _name_polish_3_f[SeedChance(10, lengthof(_name_polish_3_f), seed)], last);
571  }
572 
573  return buf;
574  }
575 
576  if (j < 4) {
577  buf = strecpy(buf, _name_polish_1_n[SeedChance(5, lengthof(_name_polish_1_n), seed)], last);
578  }
579 
580  buf = strecpy(buf, _name_polish_2_n[SeedChance(7, lengthof(_name_polish_2_n), seed)], last);
581 
582  if (j >= 4 && j < 16) {
583  buf = strecpy(buf, _name_polish_3_n[SeedChance(10, lengthof(_name_polish_3_n), seed)], last);
584  }
585 
586  return buf;
587 }
588 
589 
596 static char *MakeCzechTownName(char *buf, const char *last, uint32 seed)
597 {
598  /* 1:3 chance to use a real name. */
599  if (SeedModChance(0, 4, seed) == 0) {
600  return strecpy(buf, _name_czech_real[SeedModChance(4, lengthof(_name_czech_real), seed)], last);
601  }
602 
603 #ifdef WITH_ASSERT
604  const char *orig = buf;
605 #endif
606 
607  /* Probability of prefixes/suffixes
608  * 0..11 prefix, 12..13 prefix+suffix, 14..17 suffix, 18..31 nothing */
609  int prob_tails = SeedModChance(2, 32, seed);
610  bool do_prefix = prob_tails < 12;
611  bool do_suffix = prob_tails > 11 && prob_tails < 17;
612  bool dynamic_subst;
613 
614  /* IDs of the respective parts */
615  int prefix = 0, ending = 0, suffix = 0;
616  uint postfix = 0;
617  uint stem;
618 
619  /* The select criteria. */
620  CzechGender gender;
621  CzechChoose choose;
622  CzechAllow allow;
623 
624  if (do_prefix) prefix = SeedModChance(5, lengthof(_name_czech_adj) * 12, seed) / 12;
625  if (do_suffix) suffix = SeedModChance(7, lengthof(_name_czech_suffix), seed);
626  /* 3:1 chance 3:1 to use dynamic substantive */
627  stem = SeedModChance(9,
628  lengthof(_name_czech_subst_full) + 3 * lengthof(_name_czech_subst_stem),
629  seed);
630  if (stem < lengthof(_name_czech_subst_full)) {
631  /* That was easy! */
632  dynamic_subst = false;
633  gender = _name_czech_subst_full[stem].gender;
634  choose = _name_czech_subst_full[stem].choose;
635  allow = _name_czech_subst_full[stem].allow;
636  } else {
637  uint map[lengthof(_name_czech_subst_ending)];
638  int ending_start = -1, ending_stop = -1;
639 
640  /* Load the substantive */
641  dynamic_subst = true;
642  stem -= lengthof(_name_czech_subst_full);
643  stem %= lengthof(_name_czech_subst_stem);
644  gender = _name_czech_subst_stem[stem].gender;
645  choose = _name_czech_subst_stem[stem].choose;
646  allow = _name_czech_subst_stem[stem].allow;
647 
648  /* Load the postfix (1:1 chance that a postfix will be inserted) */
649  postfix = SeedModChance(14, lengthof(_name_czech_subst_postfix) * 2, seed);
650 
651  if (choose & CZC_POSTFIX) {
652  /* Always get a real postfix. */
653  postfix %= lengthof(_name_czech_subst_postfix);
654  }
655  if (choose & CZC_NOPOSTFIX) {
656  /* Always drop a postfix. */
657  postfix += lengthof(_name_czech_subst_postfix);
658  }
659  if (postfix < lengthof(_name_czech_subst_postfix)) {
660  choose |= CZC_POSTFIX;
661  } else {
662  choose |= CZC_NOPOSTFIX;
663  }
664 
665  /* Localize the array segment containing a good gender */
666  for (ending = 0; ending < (int)lengthof(_name_czech_subst_ending); ending++) {
667  const CzechNameSubst *e = &_name_czech_subst_ending[ending];
668 
669  if (gender == CZG_FREE ||
670  (gender == CZG_NFREE && e->gender != CZG_SNEUT && e->gender != CZG_PNEUT) ||
671  gender == e->gender) {
672  if (ending_start < 0) {
673  ending_start = ending;
674  }
675  } else if (ending_start >= 0) {
676  ending_stop = ending - 1;
677  break;
678  }
679  }
680  if (ending_stop < 0) {
681  /* Whoa. All the endings matched. */
682  ending_stop = ending - 1;
683  }
684 
685  /* Make a sequential map of the items with good mask */
686  size_t i = 0;
687  for (ending = ending_start; ending <= ending_stop; ending++) {
688  const CzechNameSubst *e = &_name_czech_subst_ending[ending];
689 
690  if ((e->choose & choose) == choose && (e->allow & allow) != 0) {
691  map[i++] = ending;
692  }
693  }
694  assert(i > 0);
695 
696  /* Load the ending */
697  ending = map[SeedModChance(16, (int)i, seed)];
698  /* Override possible CZG_*FREE; this must be a real gender,
699  * otherwise we get overflow when modifying the adjectivum. */
700  gender = _name_czech_subst_ending[ending].gender;
701  assert(gender != CZG_FREE && gender != CZG_NFREE);
702  }
703 
704  if (do_prefix && (_name_czech_adj[prefix].choose & choose) != choose) {
705  /* Throw away non-matching prefix. */
706  do_prefix = false;
707  }
708 
709  /* Now finally construct the name */
710  if (do_prefix) {
711  CzechPattern pattern = _name_czech_adj[prefix].pattern;
712 
713  buf = strecpy(buf, _name_czech_adj[prefix].name, last);
714 
715  char *endpos = buf - 1;
716  /* Find the first character in a UTF-8 sequence */
717  while (GB(*endpos, 6, 2) == 2) endpos--;
718 
719  if (gender == CZG_SMASC && pattern == CZP_PRIVL) {
720  assert(endpos >= orig + 2);
721  /* -ovX -> -uv */
722  *(endpos - 2) = 'u';
723  assert(*(endpos - 1) == 'v');
724  *endpos = '\0';
725  } else {
726  assert(endpos >= orig);
727  endpos = strecpy(endpos, _name_czech_patmod[gender][pattern], last);
728  }
729 
730  buf = strecpy(endpos, " ", last);
731  }
732 
733  if (dynamic_subst) {
734  buf = strecpy(buf, _name_czech_subst_stem[stem].name, last);
735  if (postfix < lengthof(_name_czech_subst_postfix)) {
736  const char *poststr = _name_czech_subst_postfix[postfix];
737  const char *endstr = _name_czech_subst_ending[ending].name;
738 
739  size_t postlen = strlen(poststr);
740  size_t endlen = strlen(endstr);
741  assert(postlen > 0 && endlen > 0);
742 
743  /* Kill the "avava" and "Jananna"-like cases */
744  if (postlen < 2 || postlen > endlen ||
745  ((poststr[1] != 'v' || poststr[1] != endstr[1]) &&
746  poststr[2] != endstr[1])) {
747  buf = strecpy(buf, poststr, last);
748 
749  /* k-i -> c-i, h-i -> z-i */
750  if (endstr[0] == 'i') {
751  switch (*(buf - 1)) {
752  case 'k': *(buf - 1) = 'c'; break;
753  case 'h': *(buf - 1) = 'z'; break;
754  default: break;
755  }
756  }
757  }
758  }
759  buf = strecpy(buf, _name_czech_subst_ending[ending].name, last);
760  } else {
761  buf = strecpy(buf, _name_czech_subst_full[stem].name, last);
762  }
763 
764  if (do_suffix) {
765  buf = strecpy(buf, " ", last);
766  buf = strecpy(buf, _name_czech_suffix[suffix], last);
767  }
768 
769  return buf;
770 }
771 
772 
779 static char *MakeRomanianTownName(char *buf, const char *last, uint32 seed)
780 {
781  return strecpy(buf, _name_romanian_real[SeedChance(0, lengthof(_name_romanian_real), seed)], last);
782 }
783 
784 
791 static char *MakeSlovakTownName(char *buf, const char *last, uint32 seed)
792 {
793  return strecpy(buf, _name_slovak_real[SeedChance(0, lengthof(_name_slovak_real), seed)], last);
794 }
795 
796 
803 static char *MakeNorwegianTownName(char *buf, const char *last, uint32 seed)
804 {
805  /* Use first 4 bit from seed to decide whether or not this town should
806  * have a real name 3/16 chance. Bit 0-3 */
807  if (SeedChance(0, 15, seed) < 3) {
808  /* Use 7bit for the realname table index. Bit 4-10 */
809  return strecpy(buf, _name_norwegian_real[SeedChance(4, lengthof(_name_norwegian_real), seed)], last);
810  }
811 
812  /* Use 7bit for the first fake part. Bit 4-10 */
813  buf = strecpy(buf, _name_norwegian_1[SeedChance(4, lengthof(_name_norwegian_1), seed)], last);
814  /* Use 7bit for the last fake part. Bit 11-17 */
815  buf = strecpy(buf, _name_norwegian_2[SeedChance(11, lengthof(_name_norwegian_2), seed)], last);
816 
817  return buf;
818 }
819 
820 
827 static char *MakeHungarianTownName(char *buf, const char *last, uint32 seed)
828 {
829  if (SeedChance(12, 15, seed) < 3) {
830  return strecpy(buf, _name_hungarian_real[SeedChance(0, lengthof(_name_hungarian_real), seed)], last);
831  }
832 
833  /* optional first segment */
834  uint i = SeedChance(3, lengthof(_name_hungarian_1) * 3, seed);
835  if (i < lengthof(_name_hungarian_1)) buf = strecpy(buf, _name_hungarian_1[i], last);
836 
837  /* mandatory middle segments */
838  buf = strecpy(buf, _name_hungarian_2[SeedChance(3, lengthof(_name_hungarian_2), seed)], last);
839  buf = strecpy(buf, _name_hungarian_3[SeedChance(6, lengthof(_name_hungarian_3), seed)], last);
840 
841  /* optional last segment */
842  i = SeedChance(10, lengthof(_name_hungarian_4) * 3, seed);
843  if (i < lengthof(_name_hungarian_4)) {
844  buf = strecpy(buf, _name_hungarian_4[i], last);
845  }
846 
847  return buf;
848 }
849 
850 
857 static char *MakeSwissTownName(char *buf, const char *last, uint32 seed)
858 {
859  return strecpy(buf, _name_swiss_real[SeedChance(0, lengthof(_name_swiss_real), seed)], last);
860 }
861 
862 
869 static char *MakeDanishTownName(char *buf, const char *last, uint32 seed)
870 {
871  /* optional first segment */
872  int i = SeedChanceBias(0, lengthof(_name_danish_1), seed, 50);
873  if (i >= 0) buf = strecpy(buf, _name_danish_1[i], last);
874 
875  /* middle segments removed as this algorithm seems to create much more realistic names */
876  buf = strecpy(buf, _name_danish_2[SeedChance( 7, lengthof(_name_danish_2), seed)], last);
877  buf = strecpy(buf, _name_danish_3[SeedChance(16, lengthof(_name_danish_3), seed)], last);
878 
879  return buf;
880 }
881 
882 
889 static char *MakeTurkishTownName(char *buf, const char *last, uint32 seed)
890 {
891  uint i = SeedModChance(0, 5, seed);
892 
893  switch (i) {
894  case 0:
895  buf = strecpy(buf, _name_turkish_prefix[SeedModChance( 2, lengthof(_name_turkish_prefix), seed)], last);
896 
897  /* middle segment */
898  buf = strecpy(buf, _name_turkish_middle[SeedModChance( 4, lengthof(_name_turkish_middle), seed)], last);
899 
900  /* optional suffix */
901  if (SeedModChance(0, 7, seed) == 0) {
902  buf = strecpy(buf, _name_turkish_suffix[SeedModChance( 10, lengthof(_name_turkish_suffix), seed)], last);
903  }
904  break;
905 
906  case 1: case 2:
907  buf = strecpy(buf, _name_turkish_prefix[SeedModChance( 2, lengthof(_name_turkish_prefix), seed)], last);
908  buf = strecpy(buf, _name_turkish_suffix[SeedModChance( 4, lengthof(_name_turkish_suffix), seed)], last);
909  break;
910 
911  default:
912  buf = strecpy(buf, _name_turkish_real[SeedModChance( 4, lengthof(_name_turkish_real), seed)], last);
913  break;
914  }
915 
916  return buf;
917 }
918 
919 
926 static char *MakeItalianTownName(char *buf, const char *last, uint32 seed)
927 {
928  if (SeedModChance(0, 6, seed) == 0) { // real city names
929  return strecpy(buf, _name_italian_real[SeedModChance(4, lengthof(_name_italian_real), seed)], last);
930  }
931 
932  static const char * const mascul_femin_italian[] = {
933  "o",
934  "a",
935  };
936 
937  if (SeedModChance(0, 8, seed) == 0) { // prefix
938  buf = strecpy(buf, _name_italian_pref[SeedModChance(11, lengthof(_name_italian_pref), seed)], last);
939  }
940 
941  uint i = SeedChance(0, 2, seed);
942  if (i == 0) { // masculine form
943  buf = strecpy(buf, _name_italian_1m[SeedModChance(4, lengthof(_name_italian_1m), seed)], last);
944  } else { // feminine form
945  buf = strecpy(buf, _name_italian_1f[SeedModChance(4, lengthof(_name_italian_1f), seed)], last);
946  }
947 
948  if (SeedModChance(3, 3, seed) == 0) {
949  buf = strecpy(buf, _name_italian_2[SeedModChance(11, lengthof(_name_italian_2), seed)], last);
950  buf = strecpy(buf, mascul_femin_italian[i], last);
951  } else {
952  buf = strecpy(buf, _name_italian_2i[SeedModChance(16, lengthof(_name_italian_2i), seed)], last);
953  }
954 
955  if (SeedModChance(15, 4, seed) == 0) {
956  if (SeedModChance(5, 2, seed) == 0) { // generic suffix
957  buf = strecpy(buf, _name_italian_3[SeedModChance(4, lengthof(_name_italian_3), seed)], last);
958  } else { // river name suffix
959  buf = strecpy(buf, _name_italian_river1[SeedModChance(4, lengthof(_name_italian_river1), seed)], last);
960  buf = strecpy(buf, _name_italian_river2[SeedModChance(16, lengthof(_name_italian_river2), seed)], last);
961  }
962  }
963 
964  return buf;
965 }
966 
967 
974 static char *MakeCatalanTownName(char *buf, const char *last, uint32 seed)
975 {
976  if (SeedModChance(0, 3, seed) == 0) { // real city names
977  return strecpy(buf, _name_catalan_real[SeedModChance(4, lengthof(_name_catalan_real), seed)], last);
978  }
979 
980  if (SeedModChance(0, 2, seed) == 0) { // prefix
981  buf = strecpy(buf, _name_catalan_pref[SeedModChance(11, lengthof(_name_catalan_pref), seed)], last);
982  }
983 
984  uint i = SeedChance(0, 2, seed);
985  if (i == 0) { // masculine form
986  buf = strecpy(buf, _name_catalan_1m[SeedModChance(4, lengthof(_name_catalan_1m), seed)], last);
987  buf = strecpy(buf, _name_catalan_2m[SeedModChance(11, lengthof(_name_catalan_2m), seed)], last);
988  } else { // feminine form
989  buf = strecpy(buf, _name_catalan_1f[SeedModChance(4, lengthof(_name_catalan_1f), seed)], last);
990  buf = strecpy(buf, _name_catalan_2f[SeedModChance(11, lengthof(_name_catalan_2f), seed)], last);
991  }
992 
993  if (SeedModChance(15, 5, seed) == 0) {
994  if (SeedModChance(5, 2, seed) == 0) { // generic suffix
995  buf = strecpy(buf, _name_catalan_3[SeedModChance(4, lengthof(_name_catalan_3), seed)], last);
996  } else { // river name suffix
997  buf = strecpy(buf, _name_catalan_river1[SeedModChance(4, lengthof(_name_catalan_river1), seed)], last);
998  }
999  }
1000 
1001  return buf;
1002 }
1003 
1004 
1012 typedef char *TownNameGenerator(char *buf, const char *last, uint32 seed);
1013 
1016  byte min;
1018 };
1019 
1022  { 4, MakeEnglishOriginalTownName}, // replaces first 4 characters of name
1023  { 0, MakeFrenchTownName},
1024  { 0, MakeGermanTownName},
1025  { 4, MakeEnglishAdditionalTownName}, // replaces first 4 characters of name
1026  { 0, MakeSpanishTownName},
1027  { 0, MakeSillyTownName},
1028  { 0, MakeSwedishTownName},
1029  { 0, MakeDutchTownName},
1030  { 8, MakeFinnishTownName}, // _name_finnish_1
1031  { 0, MakePolishTownName},
1032  { 0, MakeSlovakTownName},
1033  { 0, MakeNorwegianTownName},
1034  { 0, MakeHungarianTownName},
1035  { 0, MakeAustrianTownName},
1036  { 0, MakeRomanianTownName},
1037  { 28, MakeCzechTownName}, // _name_czech_adj + _name_czech_patmod + 1 + _name_czech_subst_stem + _name_czech_subst_postfix
1038  { 0, MakeSwissTownName},
1039  { 0, MakeDanishTownName},
1040  { 0, MakeTurkishTownName},
1041  { 0, MakeItalianTownName},
1042  { 0, MakeCatalanTownName},
1043 };
1044 
1045 
1054 char *GenerateTownNameString(char *buf, const char *last, size_t lang, uint32 seed)
1055 {
1056  assert(lang < lengthof(_town_name_generators));
1057 
1058  /* Some generators need at least 9 bytes in buffer. English generators need 5 for
1059  * string replacing, others use constructions like strlen(buf)-3 and so on.
1060  * Finnish generator needs to fit all strings from _name_finnish_1.
1061  * Czech generator needs to fit almost whole town name...
1062  * These would break. Using another temporary buffer results in ~40% slower code,
1063  * so use it only when really needed. */
1064  const TownNameGeneratorParams *par = &_town_name_generators[lang];
1065  if (last >= buf + par->min) return par->proc(buf, last, seed);
1066 
1067  char *buffer = AllocaM(char, par->min + 1);
1068  par->proc(buffer, buffer + par->min, seed);
1069 
1070  return strecpy(buf, buffer, last);
1071 }
GenerateTownNameString
char * GenerateTownNameString(char *buf, const char *last, size_t lang, uint32 seed)
Generates town name from given seed.
Definition: townname.cpp:1054
ReplaceEnglishWords
static void ReplaceEnglishWords(char *buf, bool original)
Replaces english curses and ugly letter combinations by nicer ones.
Definition: townname.cpp:212
_town_name_generators
static const TownNameGeneratorParams _town_name_generators[]
Town name generators.
Definition: townname.cpp:1021
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
MakeTurkishTownName
static char * MakeTurkishTownName(char *buf, const char *last, uint32 seed)
Generates Turkish town name from given seed.
Definition: townname.cpp:889
VerifyTownName
bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names)
Verifies the town name is valid and unique.
Definition: townname.cpp:82
MakeRomanianTownName
static char * MakeRomanianTownName(char *buf, const char *last, uint32 seed)
Generates Romanian town name from given seed.
Definition: townname.cpp:779
SeedModChance
static uint32 SeedModChance(byte shift_by, int max, uint32 seed)
Generates a number from given seed.
Definition: townname.cpp:165
MakeDutchTownName
static char * MakeDutchTownName(char *buf, const char *last, uint32 seed)
Generates Dutch town name from given seed.
Definition: townname.cpp:460
TownNameParams::type
uint16 type
town name style
Definition: townname_type.h:32
MakeSwissTownName
static char * MakeSwissTownName(char *buf, const char *last, uint32 seed)
Generates Swiss town name from given seed.
Definition: townname.cpp:857
GameCreationSettings::town_name
byte town_name
the town name generator used for town names
Definition: settings_type.h:307
TownNameParams::grfid
uint32 grfid
newgrf ID (0 if not used)
Definition: townname_type.h:31
MakePolishTownName
static char * MakePolishTownName(char *buf, const char *last, uint32 seed)
Generates Polish town name from given seed.
Definition: townname.cpp:534
town.h
TownNameGeneratorParams
Contains pointer to generator and minimum buffer size (not incl.
Definition: townname.cpp:1015
MakeGermanTownName
static char * MakeGermanTownName(char *buf, const char *last, uint32 seed)
Generates German town name from given seed.
Definition: townname.cpp:351
MakeSpanishTownName
static char * MakeSpanishTownName(char *buf, const char *last, uint32 seed)
Generates Latin-American town name from given seed.
Definition: townname.cpp:394
TownNameGeneratorParams::min
byte min
minimum number of characters that need to be printed for generator to work correctly
Definition: townname.cpp:1016
MakeCatalanTownName
static char * MakeCatalanTownName(char *buf, const char *last, uint32 seed)
Generates Catalan town name from given seed.
Definition: townname.cpp:974
genworld.h
townname_type.h
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:564
MAX_CHAR_LENGTH
static const int MAX_CHAR_LENGTH
Max. length of UTF-8 encoded unicode character.
Definition: strings_type.h:18
GetStringWithArgs
char * GetStringWithArgs(char *buffr, StringID string, StringParameters *args, const char *last, uint case_index, bool game_script)
Get a parsed string with most special stringcodes replaced by the string parameters.
Definition: strings.cpp:223
MakeSillyTownName
static char * MakeSillyTownName(char *buf, const char *last, uint32 seed)
Generates Silly town name from given seed.
Definition: townname.cpp:418
Utf8StringLength
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...
Definition: string.cpp:352
MakeDanishTownName
static char * MakeDanishTownName(char *buf, const char *last, uint32 seed)
Generates Danish town name from given seed.
Definition: townname.cpp:869
MakeFrenchTownName
static char * MakeFrenchTownName(char *buf, const char *last, uint32 seed)
Generates French town name from given seed.
Definition: townname.cpp:406
MakeAustrianTownName
static char * MakeAustrianTownName(char *buf, const char *last, uint32 seed)
Generates Austrian town name from given seed.
Definition: townname.cpp:307
GetTownName
char * GetTownName(char *buff, const TownNameParams *par, uint32 townnameparts, const char *last)
Fills buffer with specified town name.
Definition: townname.cpp:49
MakeSwedishTownName
static char * MakeSwedishTownName(char *buf, const char *last, uint32 seed)
Generates Swedish town name from given seed.
Definition: townname.cpp:433
SeedChanceBias
static int32 SeedChanceBias(byte shift_by, int max, uint32 seed, int bias)
Generates a number from given seed.
Definition: townname.cpp:188
MakeFinnishTownName
static char * MakeFinnishTownName(char *buf, const char *last, uint32 seed)
Generates Finnish town name from given seed.
Definition: townname.cpp:486
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:80
TownNameParams::TownNameParams
TownNameParams(byte town_name)
Initializes this struct from language ID.
Definition: townname_type.h:38
safeguards.h
SeedChance
static uint32 SeedChance(byte shift_by, int max, uint32 seed)
Generates a number from given seed.
Definition: townname.cpp:152
MakeEnglishOriginalTownName
static char * MakeEnglishOriginalTownName(char *buf, const char *last, uint32 seed)
Generates English (Original) town name from given seed.
Definition: townname.cpp:232
gfx_layout.h
stdafx.h
TownNameGeneratorParams::proc
TownNameGenerator * proc
generator itself
Definition: townname.cpp:1017
Layouter::ReduceLineCache
static void ReduceLineCache()
Reduce the size of linecache if necessary to prevent infinite growth.
Definition: gfx_layout.cpp:908
ReplaceWords
static void ReplaceWords(const char *org, const char *rep, char *buf)
Replaces a string beginning in 'org' with 'rep'.
Definition: townname.cpp:200
_generating_world
bool _generating_world
Whether we are generating the map or not.
Definition: genworld.cpp:61
MakeCzechTownName
static char * MakeCzechTownName(char *buf, const char *last, uint32 seed)
Generates Czech town name from given seed.
Definition: townname.cpp:596
string_func.h
MakeHungarianTownName
static char * MakeHungarianTownName(char *buf, const char *last, uint32 seed)
Generates Hungarian town name from given seed.
Definition: townname.cpp:827
townname.h
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:378
strings_func.h
MakeSlovakTownName
static char * MakeSlovakTownName(char *buf, const char *last, uint32 seed)
Generates Slovak town name from given seed.
Definition: townname.cpp:791
StringParameters
Definition: strings_func.h:60
MakeEnglishAdditionalTownName
static char * MakeEnglishAdditionalTownName(char *buf, const char *last, uint32 seed)
Generates English (Additional) town name from given seed.
Definition: townname.cpp:268
TownNameGenerator
char * TownNameGenerator(char *buf, const char *last, uint32 seed)
Type for all town name generator functions.
Definition: townname.cpp:1012
MakeItalianTownName
static char * MakeItalianTownName(char *buf, const char *last, uint32 seed)
Generates Italian town name from given seed.
Definition: townname.cpp:926
TownNameParams
Struct holding parameters used to generate town name.
Definition: townname_type.h:30
CzechNameSubst
Definition: townname.h:1752
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:369
Town
Town data structure.
Definition: town.h:50
random_func.hpp
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:108
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:385
GenerateTownName
bool GenerateTownName(uint32 *townnameparts, TownNames *town_names)
Generates valid town name.
Definition: townname.cpp:119
MakeNorwegianTownName
static char * MakeNorwegianTownName(char *buf, const char *last, uint32 seed)
Generates Norwegian town name from given seed.
Definition: townname.cpp:803
AllocaM
#define AllocaM(T, num_elements)
alloca() has to be called in the parent function, so define AllocaM() as a macro
Definition: alloc_func.hpp:132