townname.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "string_func.h"
00014 #include "townname_type.h"
00015 #include "town.h"
00016 #include "core/alloc_func.hpp"
00017 #include "strings_func.h"
00018 #include "core/random_func.hpp"
00019 #include "genworld.h"
00020 
00021 #include "table/townname.h"
00022 
00023 
00028 TownNameParams::TownNameParams(const Town *t) :
00029     grfid(t->townnamegrfid), // by default, use supplied data
00030     type(t->townnametype)
00031 {
00032   if (t->townnamegrfid != 0 && GetGRFTownName(t->townnamegrfid) == NULL) {
00033     /* Fallback to english original */
00034     this->grfid = 0;
00035     this->type = SPECSTR_TOWNNAME_ENGLISH;
00036     return;
00037   }
00038 }
00039 
00040 
00049 char *GetTownName(char *buff, const TownNameParams *par, uint32 townnameparts, const char *last)
00050 {
00051   if (par->grfid == 0) {
00052     int64 args[1] = { townnameparts };
00053     return GetStringWithArgs(buff, par->type, args, endof(args), last);
00054   }
00055 
00056   return GRFTownNameGenerate(buff, par->grfid, par->type, townnameparts, last);
00057 }
00058 
00059 
00067 char *GetTownName(char *buff, const Town *t, const char *last)
00068 {
00069   TownNameParams par(t);
00070   return GetTownName(buff, &par, t->townnameparts, last);
00071 }
00072 
00073 
00080 bool VerifyTownName(uint32 r, const TownNameParams *par)
00081 {
00082   /* reserve space for extra unicode character and terminating '\0' */
00083   char buf1[(MAX_LENGTH_TOWN_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00084   char buf2[(MAX_LENGTH_TOWN_NAME_CHARS + 1) * MAX_CHAR_LENGTH];
00085 
00086   GetTownName(buf1, par, r, lastof(buf1));
00087 
00088   /* Check size and width */
00089   if (Utf8StringLength(buf1) >= MAX_LENGTH_TOWN_NAME_CHARS) return false;
00090 
00091   const Town *t;
00092   FOR_ALL_TOWNS(t) {
00093     /* We can't just compare the numbers since
00094      * several numbers may map to a single name. */
00095     const char *buf = t->name;
00096     if (buf == NULL) {
00097       GetTownName(buf2, t, lastof(buf2));
00098       buf = buf2;
00099     }
00100     if (strcmp(buf1, buf2) == 0) return false;
00101   }
00102 
00103   return true;
00104 }
00105 
00106 
00112 bool GenerateTownName(uint32 *townnameparts)
00113 {
00114   /* Do not set too low tries, since when we run out of names, we loop
00115    * for #tries only one time anyway - then we stop generating more
00116    * towns. Do not show it too high neither, since looping through all
00117    * the other towns may take considerable amount of time (10000 is
00118    * too much). */
00119   TownNameParams par(_settings_game.game_creation.town_name);
00120 
00121   for (int i = 1000; i != 0; i--) {
00122     uint32 r = _generating_world ? Random() : InteractiveRandom();
00123     if (!VerifyTownName(r, &par)) continue;
00124 
00125     *townnameparts = r;
00126     return true;
00127   }
00128 
00129   return false;
00130 }
00131 
00132 
00133 
00141 static inline uint32 SeedChance(byte shift_by, int max, uint32 seed)
00142 {
00143   return (GB(seed, shift_by, 16) * max) >> 16;
00144 }
00145 
00146 
00154 static inline uint32 SeedModChance(byte shift_by, int max, uint32 seed)
00155 {
00156   /* This actually gives *MUCH* more even distribution of the values
00157    * than SeedChance(), which is absolutely horrible in that. If
00158    * you do not believe me, try with i.e. the Czech town names,
00159    * compare the words (nicely visible on prefixes) generated by
00160    * SeedChance() and SeedModChance(). Do not get dicouraged by the
00161    * never-use-modulo myths, which hold true only for the linear
00162    * congruential generators (and Random() isn't such a generator).
00163    * --pasky
00164    * TODO: Perhaps we should use it for all the name generators? --pasky */
00165   return (seed >> shift_by) % max;
00166 }
00167 
00168 
00177 static inline int32 SeedChanceBias(byte shift_by, int max, uint32 seed, int bias)
00178 {
00179   return SeedChance(shift_by, max + bias, seed) - bias;
00180 }
00181 
00182 
00189 static void ReplaceWords(const char *org, const char *rep, char *buf)
00190 {
00191   if (strncmp(buf, org, 4) == 0) strncpy(buf, rep, 4); // Safe as the string in buf is always more than 4 characters long.
00192 }
00193 
00194 
00200 static void ReplaceEnglishWords(char *buf, bool original)
00201 {
00202   ReplaceWords("Cunt", "East", buf);
00203   ReplaceWords("Slag", "Pits", buf);
00204   ReplaceWords("Slut", "Edin", buf);
00205   if (!original) ReplaceWords("Fart", "Boot", buf); // never happens with 'English (Original)'
00206   ReplaceWords("Drar", "Quar", buf);
00207   ReplaceWords("Dreh", "Bash", buf);
00208   ReplaceWords("Frar", "Shor", buf);
00209   ReplaceWords("Grar", "Aber", buf);
00210   ReplaceWords("Brar", "Over", buf);
00211   ReplaceWords("Wrar", original ? "Inve" : "Stan", buf);
00212 }
00213 
00220 static char *MakeEnglishOriginalTownName(char *buf, const char *last, uint32 seed)
00221 {
00222   char *orig = buf;
00223 
00224   /* optional first segment */
00225   int i = SeedChanceBias(0, lengthof(_name_original_english_1), seed, 50);
00226   if (i >= 0) buf = strecpy(buf, _name_original_english_1[i], last);
00227 
00228   /* mandatory middle segments */
00229   buf = strecpy(buf, _name_original_english_2[SeedChance(4,  lengthof(_name_original_english_2), seed)], last);
00230   buf = strecpy(buf, _name_original_english_3[SeedChance(7,  lengthof(_name_original_english_3), seed)], last);
00231   buf = strecpy(buf, _name_original_english_4[SeedChance(10, lengthof(_name_original_english_4), seed)], last);
00232   buf = strecpy(buf, _name_original_english_5[SeedChance(13, lengthof(_name_original_english_5), seed)], last);
00233 
00234   /* optional last segment */
00235   i = SeedChanceBias(15, lengthof(_name_original_english_6), seed, 60);
00236   if (i >= 0) buf = strecpy(buf, _name_original_english_6[i], last);
00237 
00238   /* Ce, Ci => Ke, Ki */
00239   if (orig[0] == 'C' && (orig[1] == 'e' || orig[1] == 'i')) {
00240     orig[0] = 'K';
00241   }
00242 
00243   assert(buf - orig >= 4);
00244   ReplaceEnglishWords(orig, true);
00245 
00246   return buf;
00247 }
00248 
00249 
00256 static char *MakeEnglishAdditionalTownName(char *buf, const char *last, uint32 seed)
00257 {
00258   char *orig = buf;
00259 
00260   /* optional first segment */
00261   int i = SeedChanceBias(0, lengthof(_name_additional_english_prefix), seed, 50);
00262   if (i >= 0) buf = strecpy(buf, _name_additional_english_prefix[i], last);
00263 
00264   if (SeedChance(3, 20, seed) >= 14) {
00265     buf = strecpy(buf, _name_additional_english_1a[SeedChance(6, lengthof(_name_additional_english_1a), seed)], last);
00266   } else {
00267     buf = strecpy(buf, _name_additional_english_1b1[SeedChance(6, lengthof(_name_additional_english_1b1), seed)], last);
00268     buf = strecpy(buf, _name_additional_english_1b2[SeedChance(9, lengthof(_name_additional_english_1b2), seed)], last);
00269     if (SeedChance(11, 20, seed) >= 4) {
00270       buf = strecpy(buf, _name_additional_english_1b3a[SeedChance(12, lengthof(_name_additional_english_1b3a), seed)], last);
00271     } else {
00272       buf = strecpy(buf, _name_additional_english_1b3b[SeedChance(12, lengthof(_name_additional_english_1b3b), seed)], last);
00273     }
00274   }
00275 
00276   buf = strecpy(buf, _name_additional_english_2[SeedChance(14, lengthof(_name_additional_english_2), seed)], last);
00277 
00278   /* optional last segment */
00279   i = SeedChanceBias(15, lengthof(_name_additional_english_3), seed, 60);
00280   if (i >= 0) buf = strecpy(buf, _name_additional_english_3[i], last);
00281 
00282   assert(buf - orig >= 4);
00283   ReplaceEnglishWords(orig, false);
00284 
00285   return buf;
00286 }
00287 
00288 
00295 static char *MakeAustrianTownName(char *buf, const char *last, uint32 seed)
00296 {
00297   /* Bad, Maria, Gross, ... */
00298   int i = SeedChanceBias(0, lengthof(_name_austrian_a1), seed, 15);
00299   if (i >= 0) buf = strecpy(buf, _name_austrian_a1[i], last);
00300 
00301   int j = 0;
00302 
00303   i = SeedChance(4, 6, seed);
00304   if (i >= 4) {
00305     /* Kaisers-kirchen */
00306     buf = strecpy(buf, _name_austrian_a2[SeedChance( 7, lengthof(_name_austrian_a2), seed)], last);
00307     buf = strecpy(buf, _name_austrian_a3[SeedChance(13, lengthof(_name_austrian_a3), seed)], last);
00308   } else if (i >= 2) {
00309     /* St. Johann */
00310     buf = strecpy(buf, _name_austrian_a5[SeedChance( 7, lengthof(_name_austrian_a5), seed)], last);
00311     buf = strecpy(buf, _name_austrian_a6[SeedChance( 9, lengthof(_name_austrian_a6), seed)], last);
00312     j = 1; // More likely to have a " an der " or " am "
00313   } else {
00314     /* Zell */
00315     buf = strecpy(buf, _name_austrian_a4[SeedChance( 7, lengthof(_name_austrian_a4), seed)], last);
00316   }
00317 
00318   i = SeedChance(1, 6, seed);
00319   if (i >= 4 - j) {
00320     /* an der Donau (rivers) */
00321     buf = strecpy(buf, _name_austrian_f1[SeedChance(4, lengthof(_name_austrian_f1), seed)], last);
00322     buf = strecpy(buf, _name_austrian_f2[SeedChance(5, lengthof(_name_austrian_f2), seed)], last);
00323   } else if (i >= 2 - j) {
00324     /* am Dachstein (mountains) */
00325     buf = strecpy(buf, _name_austrian_b1[SeedChance(4, lengthof(_name_austrian_b1), seed)], last);
00326     buf = strecpy(buf, _name_austrian_b2[SeedChance(5, lengthof(_name_austrian_b2), seed)], last);
00327   }
00328 
00329   return buf;
00330 }
00331 
00332 
00339 static char *MakeGermanTownName(char *buf, const char *last, uint32 seed)
00340 {
00341   uint seed_derivative = SeedChance(7, 28, seed);
00342 
00343   /* optional prefix */
00344   if (seed_derivative == 12 || seed_derivative == 19) {
00345     uint i = SeedChance(2, lengthof(_name_german_pre), seed);
00346     buf = strecpy(buf, _name_german_pre[i], last);
00347   }
00348 
00349   /* mandatory middle segments including option of hardcoded name */
00350   uint i = SeedChance(3, lengthof(_name_german_real) + lengthof(_name_german_1), seed);
00351   if (i < lengthof(_name_german_real)) {
00352     buf = strecpy(buf, _name_german_real[i], last);
00353   } else {
00354     buf = strecpy(buf, _name_german_1[i - lengthof(_name_german_real)], last);
00355 
00356     i = SeedChance(5, lengthof(_name_german_2), seed);
00357     buf = strecpy(buf, _name_german_2[i], last);
00358   }
00359 
00360   /* optional suffix */
00361   if (seed_derivative == 24) {
00362     i = SeedChance(9, lengthof(_name_german_4_an_der) + lengthof(_name_german_4_am), seed);
00363     if (i < lengthof(_name_german_4_an_der)) {
00364       buf = strecpy(buf, _name_german_3_an_der[0], last);
00365       buf = strecpy(buf, _name_german_4_an_der[i], last);
00366     } else {
00367       buf = strecpy(buf, _name_german_3_am[0], last);
00368       buf = strecpy(buf, _name_german_4_am[i - lengthof(_name_german_4_an_der)], last);
00369     }
00370   }
00371 
00372   return buf;
00373 }
00374 
00375 
00382 static char *MakeSpanishTownName(char *buf, const char *last, uint32 seed)
00383 {
00384   return strecpy(buf, _name_spanish_real[SeedChance(0, lengthof(_name_spanish_real), seed)], last);
00385 }
00386 
00387 
00394 static char *MakeFrenchTownName(char *buf, const char *last, uint32 seed)
00395 {
00396   return strecpy(buf, _name_french_real[SeedChance(0, lengthof(_name_french_real), seed)], last);
00397 }
00398 
00399 
00406 static char *MakeSillyTownName(char *buf, const char *last, uint32 seed)
00407 {
00408   buf = strecpy(buf, _name_silly_1[SeedChance( 0, lengthof(_name_silly_1), seed)], last);
00409   buf = strecpy(buf, _name_silly_2[SeedChance(16, lengthof(_name_silly_2), seed)], last);
00410 
00411   return buf;
00412 }
00413 
00414 
00421 static char *MakeSwedishTownName(char *buf, const char *last, uint32 seed)
00422 {
00423   /* optional first segment */
00424   int i = SeedChanceBias(0, lengthof(_name_swedish_1), seed, 50);
00425   if (i >= 0) buf = strecpy(buf, _name_swedish_1[i], last);
00426 
00427   /* mandatory middle segments including option of hardcoded name */
00428   if (SeedChance(4, 5, seed) >= 3) {
00429     buf = strecpy(buf, _name_swedish_2[SeedChance( 7, lengthof(_name_swedish_2), seed)], last);
00430   } else {
00431     buf = strecpy(buf, _name_swedish_2a[SeedChance( 7, lengthof(_name_swedish_2a), seed)], last);
00432     buf = strecpy(buf, _name_swedish_2b[SeedChance(10, lengthof(_name_swedish_2b), seed)], last);
00433     buf = strecpy(buf, _name_swedish_2c[SeedChance(13, lengthof(_name_swedish_2c), seed)], last);
00434   }
00435 
00436   buf = strecpy(buf, _name_swedish_3[SeedChance(16, lengthof(_name_swedish_3), seed)], last);
00437 
00438   return buf;
00439 }
00440 
00441 
00448 static char *MakeDutchTownName(char *buf, const char *last, uint32 seed)
00449 {
00450   /* optional first segment */
00451   int i = SeedChanceBias(0, lengthof(_name_dutch_1), seed, 50);
00452   if (i >= 0) buf = strecpy(buf, _name_dutch_1[i], last);
00453 
00454   /* mandatory middle segments including option of hardcoded name */
00455   if (SeedChance(6, 9, seed) > 4) {
00456     buf = strecpy(buf, _name_dutch_2[SeedChance( 9, lengthof(_name_dutch_2), seed)], last);
00457   } else {
00458     buf = strecpy(buf, _name_dutch_3[SeedChance( 9, lengthof(_name_dutch_3), seed)], last);
00459     buf = strecpy(buf, _name_dutch_4[SeedChance(12, lengthof(_name_dutch_4), seed)], last);
00460   }
00461 
00462   buf = strecpy(buf, _name_dutch_5[SeedChance(15, lengthof(_name_dutch_5), seed)], last);
00463 
00464   return buf;
00465 }
00466 
00467 
00474 static char *MakeFinnishTownName(char *buf, const char *last, uint32 seed)
00475 {
00476   char *orig = buf;
00477 
00478   /* Select randomly if town name should consists of one or two parts. */
00479   if (SeedChance(0, 15, seed) >= 10) {
00480     return strecpy(buf, _name_finnish_real[SeedChance(2, lengthof(_name_finnish_real), seed)], last);
00481   }
00482 
00483   if (SeedChance(0, 15, seed) >= 5) {
00484     /* A two-part name by combining one of _name_finnish_1 + "la"/"lä"
00485      * The reason for not having the contents of _name_finnish_{1,2} in the same table is
00486      * that the ones in _name_finnish_2 are not good for this purpose. */
00487     uint sel = SeedChance( 0, lengthof(_name_finnish_1), seed);
00488     buf = strecpy(buf, _name_finnish_1[sel], last);
00489     char *end = buf - 1;
00490     assert(end >= orig);
00491     if (*end == 'i') *end = 'e';
00492     if (strstr(orig, "a") != NULL || strstr(orig, "o") != NULL || strstr(orig, "u") != NULL ||
00493         strstr(orig, "A") != NULL || strstr(orig, "O") != NULL || strstr(orig, "U")  != NULL) {
00494       buf = strecpy(buf, "la", last);
00495     } else {
00496       buf = strecpy(buf, "l\xC3\xA4", last);
00497     }
00498     return buf;
00499   }
00500 
00501   /* A two-part name by combining one of _name_finnish_{1,2} + _name_finnish_3.
00502    * Why aren't _name_finnish_{1,2} just one table? See above. */
00503   uint sel = SeedChance(2, lengthof(_name_finnish_1) + lengthof(_name_finnish_2), seed);
00504   if (sel >= lengthof(_name_finnish_1)) {
00505     buf = strecpy(buf, _name_finnish_2[sel - lengthof(_name_finnish_1)], last);
00506   } else {
00507     buf = strecpy(buf, _name_finnish_1[sel], last);
00508   }
00509 
00510   buf = strecpy(buf, _name_finnish_3[SeedChance(10, lengthof(_name_finnish_3), seed)], last);
00511 
00512   return buf;
00513 }
00514 
00515 
00522 static char *MakePolishTownName(char *buf, const char *last, uint32 seed)
00523 {
00524   /* optional first segment */
00525   uint i = SeedChance(0,
00526       lengthof(_name_polish_2_o) + lengthof(_name_polish_2_m) +
00527       lengthof(_name_polish_2_f) + lengthof(_name_polish_2_n),
00528       seed);
00529   uint j = SeedChance(2, 20, seed);
00530 
00531 
00532   if (i < lengthof(_name_polish_2_o)) {
00533     return strecpy(buf, _name_polish_2_o[SeedChance(3, lengthof(_name_polish_2_o), seed)], last);
00534   }
00535 
00536   if (i < lengthof(_name_polish_2_m) + lengthof(_name_polish_2_o)) {
00537     if (j < 4) {
00538       buf = strecpy(buf, _name_polish_1_m[SeedChance(5, lengthof(_name_polish_1_m), seed)], last);
00539     }
00540 
00541     buf = strecpy(buf, _name_polish_2_m[SeedChance(7, lengthof(_name_polish_2_m), seed)], last);
00542 
00543     if (j >= 4 && j < 16) {
00544       buf = strecpy(buf, _name_polish_3_m[SeedChance(10, lengthof(_name_polish_3_m), seed)], last);
00545     }
00546 
00547     return buf;
00548   }
00549 
00550   if (i < lengthof(_name_polish_2_f) + lengthof(_name_polish_2_m) + lengthof(_name_polish_2_o)) {
00551     if (j < 4) {
00552       buf = strecpy(buf, _name_polish_1_f[SeedChance(5, lengthof(_name_polish_1_f), seed)], last);
00553     }
00554 
00555     buf = strecpy(buf, _name_polish_2_f[SeedChance(7, lengthof(_name_polish_2_f), seed)], last);
00556 
00557     if (j >= 4 && j < 16) {
00558       buf = strecpy(buf, _name_polish_3_f[SeedChance(10, lengthof(_name_polish_3_f), seed)], last);
00559     }
00560 
00561     return buf;
00562   }
00563 
00564   if (j < 4) {
00565     buf = strecpy(buf, _name_polish_1_n[SeedChance(5, lengthof(_name_polish_1_n), seed)], last);
00566   }
00567 
00568   buf = strecpy(buf, _name_polish_2_n[SeedChance(7, lengthof(_name_polish_2_n), seed)], last);
00569 
00570   if (j >= 4 && j < 16) {
00571     buf = strecpy(buf, _name_polish_3_n[SeedChance(10, lengthof(_name_polish_3_n), seed)], last);
00572   }
00573 
00574   return buf;
00575 }
00576 
00577 
00584 static char *MakeCzechTownName(char *buf, const char *last, uint32 seed)
00585 {
00586   /* 1:3 chance to use a real name. */
00587   if (SeedModChance(0, 4, seed) == 0) {
00588     return strecpy(buf, _name_czech_real[SeedModChance(4, lengthof(_name_czech_real), seed)], last);
00589   }
00590 
00591   const char *orig = buf;
00592 
00593   /* Probability of prefixes/suffixes
00594    * 0..11 prefix, 12..13 prefix+suffix, 14..17 suffix, 18..31 nothing */
00595   int prob_tails = SeedModChance(2, 32, seed);
00596   bool do_prefix = prob_tails < 12;
00597   bool do_suffix = prob_tails > 11 && prob_tails < 17;
00598   bool dynamic_subst;
00599 
00600   /* IDs of the respective parts */
00601   int prefix = 0, ending = 0, suffix = 0;
00602   uint postfix = 0;
00603   uint stem;
00604 
00605   /* The select criteria. */
00606   CzechGender gender;
00607   CzechChoose choose;
00608   CzechAllow allow;
00609 
00610   if (do_prefix) prefix = SeedModChance(5, lengthof(_name_czech_adj) * 12, seed) / 12;
00611   if (do_suffix) suffix = SeedModChance(7, lengthof(_name_czech_suffix), seed);
00612   /* 3:1 chance 3:1 to use dynamic substantive */
00613   stem = SeedModChance(9,
00614       lengthof(_name_czech_subst_full) + 3 * lengthof(_name_czech_subst_stem),
00615       seed);
00616   if (stem < lengthof(_name_czech_subst_full)) {
00617     /* That was easy! */
00618     dynamic_subst = false;
00619     gender = _name_czech_subst_full[stem].gender;
00620     choose = _name_czech_subst_full[stem].choose;
00621     allow = _name_czech_subst_full[stem].allow;
00622   } else {
00623     unsigned int map[lengthof(_name_czech_subst_ending)];
00624     int ending_start = -1, ending_stop = -1;
00625 
00626     /* Load the substantive */
00627     dynamic_subst = true;
00628     stem -= lengthof(_name_czech_subst_full);
00629     stem %= lengthof(_name_czech_subst_stem);
00630     gender = _name_czech_subst_stem[stem].gender;
00631     choose = _name_czech_subst_stem[stem].choose;
00632     allow = _name_czech_subst_stem[stem].allow;
00633 
00634     /* Load the postfix (1:1 chance that a postfix will be inserted) */
00635     postfix = SeedModChance(14, lengthof(_name_czech_subst_postfix) * 2, seed);
00636 
00637     if (choose & CZC_POSTFIX) {
00638       /* Always get a real postfix. */
00639       postfix %= lengthof(_name_czech_subst_postfix);
00640     }
00641     if (choose & CZC_NOPOSTFIX) {
00642       /* Always drop a postfix. */
00643       postfix += lengthof(_name_czech_subst_postfix);
00644     }
00645     if (postfix < lengthof(_name_czech_subst_postfix)) {
00646       choose |= CZC_POSTFIX;
00647     } else {
00648       choose |= CZC_NOPOSTFIX;
00649     }
00650 
00651     /* Localize the array segment containing a good gender */
00652     for (ending = 0; ending < (int)lengthof(_name_czech_subst_ending); ending++) {
00653       const CzechNameSubst *e = &_name_czech_subst_ending[ending];
00654 
00655       if (gender == CZG_FREE ||
00656           (gender == CZG_NFREE && e->gender != CZG_SNEUT && e->gender != CZG_PNEUT) ||
00657            gender == e->gender) {
00658         if (ending_start < 0) {
00659           ending_start = ending;
00660         }
00661       } else if (ending_start >= 0) {
00662         ending_stop = ending - 1;
00663         break;
00664       }
00665     }
00666     if (ending_stop < 0) {
00667       /* Whoa. All the endings matched. */
00668       ending_stop = ending - 1;
00669     }
00670 
00671     /* Make a sequential map of the items with good mask */
00672     size_t i = 0;
00673     for (ending = ending_start; ending <= ending_stop; ending++) {
00674       const CzechNameSubst *e = &_name_czech_subst_ending[ending];
00675 
00676       if ((e->choose & choose) == choose && (e->allow & allow) != 0) {
00677         map[i++] = ending;
00678       }
00679     }
00680     assert(i > 0);
00681 
00682     /* Load the ending */
00683     ending = map[SeedModChance(16, (int)i, seed)];
00684     /* Override possible CZG_*FREE; this must be a real gender,
00685      * otherwise we get overflow when modifying the adjectivum. */
00686     gender = _name_czech_subst_ending[ending].gender;
00687     assert(gender != CZG_FREE && gender != CZG_NFREE);
00688   }
00689 
00690   if (do_prefix && (_name_czech_adj[prefix].choose & choose) != choose) {
00691     /* Throw away non-matching prefix. */
00692     do_prefix = false;
00693   }
00694 
00695   /* Now finally construct the name */
00696   if (do_prefix) {
00697     CzechPattern pattern = _name_czech_adj[prefix].pattern;
00698 
00699     buf = strecpy(buf, _name_czech_adj[prefix].name, last);
00700 
00701     char *endpos = buf - 1;
00702     /* Find the first character in a UTF-8 sequence */
00703     while (GB(*endpos, 6, 2) == 2) endpos--;
00704 
00705     if (gender == CZG_SMASC && pattern == CZP_PRIVL) {
00706       assert(endpos >= orig + 2);
00707       /* -ovX -> -uv */
00708       *(endpos - 2) = 'u';
00709       assert(*(endpos - 1) == 'v');
00710       *endpos = '\0';
00711     } else {
00712       assert(endpos >= orig);
00713       endpos = strecpy(endpos, _name_czech_patmod[gender][pattern], last);
00714     }
00715 
00716     buf = strecpy(endpos, " ", last);
00717   }
00718 
00719   if (dynamic_subst) {
00720     buf = strecpy(buf, _name_czech_subst_stem[stem].name, last);
00721     if (postfix < lengthof(_name_czech_subst_postfix)) {
00722       const char *poststr = _name_czech_subst_postfix[postfix];
00723       const char *endstr = _name_czech_subst_ending[ending].name;
00724 
00725       size_t postlen = strlen(poststr);
00726       size_t endlen = strlen(endstr);
00727       assert(postlen > 0 && endlen > 0);
00728 
00729       /* Kill the "avava" and "Jananna"-like cases */
00730       if (postlen < 2 || postlen > endlen ||
00731           ((poststr[1] != 'v' || poststr[1] != endstr[1]) &&
00732           poststr[2] != endstr[1])) {
00733         buf = strecpy(buf, poststr, last);
00734 
00735         /* k-i -> c-i, h-i -> z-i */
00736         if (endstr[0] == 'i') {
00737           switch (*(buf - 1)) {
00738             case 'k': *(buf - 1) = 'c'; break;
00739             case 'h': *(buf - 1) = 'z'; break;
00740             default: break;
00741           }
00742         }
00743       }
00744     }
00745     buf = strecpy(buf, _name_czech_subst_ending[ending].name, last);
00746   } else {
00747     buf = strecpy(buf, _name_czech_subst_full[stem].name, last);
00748   }
00749 
00750   if (do_suffix) {
00751     buf = strecpy(buf, " ", last);
00752     buf = strecpy(buf, _name_czech_suffix[suffix], last);
00753   }
00754 
00755   return buf;
00756 }
00757 
00758 
00765 static char *MakeRomanianTownName(char *buf, const char *last, uint32 seed)
00766 {
00767   return strecpy(buf, _name_romanian_real[SeedChance(0, lengthof(_name_romanian_real), seed)], last);
00768 }
00769 
00770 
00777 static char *MakeSlovakTownName(char *buf, const char *last, uint32 seed)
00778 {
00779   return strecpy(buf, _name_slovak_real[SeedChance(0, lengthof(_name_slovak_real), seed)], last);
00780 }
00781 
00782 
00789 static char *MakeNorwegianTownName(char *buf, const char *last, uint32 seed)
00790 {
00791   /* Use first 4 bit from seed to decide whether or not this town should
00792    * have a real name 3/16 chance.  Bit 0-3 */
00793   if (SeedChance(0, 15, seed) < 3) {
00794     /* Use 7bit for the realname table index.  Bit 4-10 */
00795     return strecpy(buf, _name_norwegian_real[SeedChance(4, lengthof(_name_norwegian_real), seed)], last);
00796   }
00797 
00798   /* Use 7bit for the first fake part.  Bit 4-10 */
00799   buf = strecpy(buf, _name_norwegian_1[SeedChance(4, lengthof(_name_norwegian_1), seed)], last);
00800   /* Use 7bit for the last fake part.  Bit 11-17 */
00801   buf = strecpy(buf, _name_norwegian_2[SeedChance(11, lengthof(_name_norwegian_2), seed)], last);
00802 
00803   return buf;
00804 }
00805 
00806 
00813 static char *MakeHungarianTownName(char *buf, const char *last, uint32 seed)
00814 {
00815   if (SeedChance(12, 15, seed) < 3) {
00816     return strecpy(buf, _name_hungarian_real[SeedChance(0, lengthof(_name_hungarian_real), seed)], last);
00817   }
00818 
00819   /* optional first segment */
00820   uint i = SeedChance(3, lengthof(_name_hungarian_1) * 3, seed);
00821   if (i < lengthof(_name_hungarian_1)) buf = strecpy(buf, _name_hungarian_1[i], last);
00822 
00823   /* mandatory middle segments */
00824   buf = strecpy(buf, _name_hungarian_2[SeedChance(3, lengthof(_name_hungarian_2), seed)], last);
00825   buf = strecpy(buf, _name_hungarian_3[SeedChance(6, lengthof(_name_hungarian_3), seed)], last);
00826 
00827   /* optional last segment */
00828   i = SeedChance(10, lengthof(_name_hungarian_4) * 3, seed);
00829   if (i < lengthof(_name_hungarian_4)) {
00830     buf = strecpy(buf, _name_hungarian_4[i], last);
00831   }
00832 
00833   return buf;
00834 }
00835 
00836 
00843 static char *MakeSwissTownName(char *buf, const char *last, uint32 seed)
00844 {
00845   return strecpy(buf, _name_swiss_real[SeedChance(0, lengthof(_name_swiss_real), seed)], last);
00846 }
00847 
00848 
00855 static char *MakeDanishTownName(char *buf, const char *last, uint32 seed)
00856 {
00857   /* optional first segment */
00858   int i = SeedChanceBias(0, lengthof(_name_danish_1), seed, 50);
00859   if (i >= 0) buf = strecpy(buf, _name_danish_1[i], last);
00860 
00861   /* middle segments removed as this algorithm seems to create much more realistic names */
00862   buf = strecpy(buf, _name_danish_2[SeedChance( 7, lengthof(_name_danish_2), seed)], last);
00863   buf = strecpy(buf, _name_danish_3[SeedChance(16, lengthof(_name_danish_3), seed)], last);
00864 
00865   return buf;
00866 }
00867 
00868 
00875 static char *MakeTurkishTownName(char *buf, const char *last, uint32 seed)
00876 {
00877   uint i = SeedModChance(0, 5, seed);
00878 
00879   switch (i) {
00880     case 0:
00881       buf = strecpy(buf, _name_turkish_prefix[SeedModChance( 2, lengthof(_name_turkish_prefix), seed)], last);
00882 
00883       /* middle segment */
00884       buf = strecpy(buf, _name_turkish_middle[SeedModChance( 4, lengthof(_name_turkish_middle), seed)], last);
00885 
00886       /* optional suffix */
00887       if (SeedModChance(0, 7, seed) == 0) {
00888         buf = strecpy(buf, _name_turkish_suffix[SeedModChance( 10, lengthof(_name_turkish_suffix), seed)], last);
00889       }
00890       break;
00891 
00892     case 1: case 2:
00893       buf = strecpy(buf, _name_turkish_prefix[SeedModChance( 2, lengthof(_name_turkish_prefix), seed)], last);
00894       buf = strecpy(buf, _name_turkish_suffix[SeedModChance( 4, lengthof(_name_turkish_suffix), seed)], last);
00895       break;
00896 
00897     default:
00898       buf = strecpy(buf, _name_turkish_real[SeedModChance( 4, lengthof(_name_turkish_real), seed)], last);
00899       break;
00900   }
00901 
00902   return buf;
00903 }
00904 
00905 
00912 static char *MakeItalianTownName(char *buf, const char *last, uint32 seed)
00913 {
00914   if (SeedModChance(0, 6, seed) == 0) { // real city names
00915     return strecpy(buf, _name_italian_real[SeedModChance(4, lengthof(_name_italian_real), seed)], last);
00916   }
00917 
00918   static const char * const mascul_femin_italian[] = {
00919     "o",
00920     "a",
00921   };
00922 
00923   if (SeedModChance(0, 8, seed) == 0) { // prefix
00924     buf = strecpy(buf, _name_italian_pref[SeedModChance(11, lengthof(_name_italian_pref), seed)], last);
00925   }
00926 
00927   uint i = SeedChance(0, 2, seed);
00928   if (i == 0) { // masculine form
00929     buf = strecpy(buf, _name_italian_1m[SeedModChance(4, lengthof(_name_italian_1m), seed)], last);
00930   } else { // feminine form
00931     buf = strecpy(buf, _name_italian_1f[SeedModChance(4, lengthof(_name_italian_1f), seed)], last);
00932   }
00933 
00934   if (SeedModChance(3, 3, seed) == 0) {
00935     buf = strecpy(buf, _name_italian_2[SeedModChance(11, lengthof(_name_italian_2), seed)], last);
00936     buf = strecpy(buf, mascul_femin_italian[i], last);
00937   } else {
00938     buf = strecpy(buf, _name_italian_2i[SeedModChance(16, lengthof(_name_italian_2i), seed)], last);
00939   }
00940 
00941   if (SeedModChance(15, 4, seed) == 0) {
00942     if (SeedModChance(5, 2, seed) == 0) { // generic suffix
00943       buf = strecpy(buf, _name_italian_3[SeedModChance(4, lengthof(_name_italian_3), seed)], last);
00944     } else { // river name suffix
00945       buf = strecpy(buf, _name_italian_river1[SeedModChance(4, lengthof(_name_italian_river1), seed)], last);
00946       buf = strecpy(buf, _name_italian_river2[SeedModChance(16, lengthof(_name_italian_river2), seed)], last);
00947     }
00948   }
00949 
00950   return buf;
00951 }
00952 
00953 
00960 static char *MakeCatalanTownName(char *buf, const char *last, uint32 seed)
00961 {
00962   if (SeedModChance(0, 3, seed) == 0) { // real city names
00963     return strecpy(buf, _name_catalan_real[SeedModChance(4, lengthof(_name_catalan_real), seed)], last);
00964   }
00965 
00966   if (SeedModChance(0, 2, seed) == 0) { // prefix
00967     buf = strecpy(buf, _name_catalan_pref[SeedModChance(11, lengthof(_name_catalan_pref), seed)], last);
00968   }
00969 
00970   uint i = SeedChance(0, 2, seed);
00971   if (i == 0) { // masculine form
00972     buf = strecpy(buf, _name_catalan_1m[SeedModChance(4, lengthof(_name_catalan_1m), seed)], last);
00973     buf = strecpy(buf, _name_catalan_2m[SeedModChance(11, lengthof(_name_catalan_2m), seed)], last);
00974   } else { // feminine form
00975     buf = strecpy(buf, _name_catalan_1f[SeedModChance(4, lengthof(_name_catalan_1f), seed)], last);
00976     buf = strecpy(buf, _name_catalan_2f[SeedModChance(11, lengthof(_name_catalan_2f), seed)], last);
00977   }
00978 
00979   if (SeedModChance(15, 5, seed) == 0) {
00980     if (SeedModChance(5, 2, seed) == 0) { // generic suffix
00981       buf = strecpy(buf, _name_catalan_3[SeedModChance(4, lengthof(_name_catalan_3), seed)], last);
00982     } else { // river name suffix
00983       buf = strecpy(buf, _name_catalan_river1[SeedModChance(4, lengthof(_name_catalan_river1), seed)], last);
00984     }
00985   }
00986 
00987   return buf;
00988 }
00989 
00990 
00991 typedef char *TownNameGenerator(char *buf, const char *last, uint32 seed);
00992 
00994 struct TownNameGeneratorParams {
00995   byte min; 
00996   TownNameGenerator *proc; 
00997 };
00998 
01000 static const TownNameGeneratorParams _town_name_generators[] = {
01001   {  4, MakeEnglishOriginalTownName},  // replaces first 4 characters of name
01002   {  0, MakeFrenchTownName},
01003   {  0, MakeGermanTownName},
01004   {  4, MakeEnglishAdditionalTownName}, // replaces first 4 characters of name
01005   {  0, MakeSpanishTownName},
01006   {  0, MakeSillyTownName},
01007   {  0, MakeSwedishTownName},
01008   {  0, MakeDutchTownName},
01009   {  8, MakeFinnishTownName}, // _name_finnish_1
01010   {  0, MakePolishTownName},
01011   {  0, MakeSlovakTownName},
01012   {  0, MakeNorwegianTownName},
01013   {  0, MakeHungarianTownName},
01014   {  0, MakeAustrianTownName},
01015   {  0, MakeRomanianTownName},
01016   { 28, MakeCzechTownName}, // _name_czech_adj + _name_czech_patmod + 1 + _name_czech_subst_stem + _name_czech_subst_postfix
01017   {  0, MakeSwissTownName},
01018   {  0, MakeDanishTownName},
01019   {  0, MakeTurkishTownName},
01020   {  0, MakeItalianTownName},
01021   {  0, MakeCatalanTownName},
01022 };
01023 
01024 
01033 char *GenerateTownNameString(char *buf, const char *last, size_t lang, uint32 seed)
01034 {
01035   assert(lang < lengthof(_town_name_generators));
01036 
01037   /* Some generators need at least 9 bytes in buffer.  English generators need 5 for
01038    * string replacing, others use constructions like strlen(buf)-3 and so on.
01039    * Finnish generator needs to fit all strings from _name_finnish_1.
01040    * Czech generator needs to fit almost whole town name...
01041    * These would break. Using another temporary buffer results in ~40% slower code,
01042    * so use it only when really needed. */
01043   const TownNameGeneratorParams *par = &_town_name_generators[lang];
01044   if (last >= buf + par->min) return par->proc(buf, last, seed);
01045 
01046   char *buffer = AllocaM(char, par->min + 1);
01047   par->proc(buffer, buffer + par->min, seed);
01048 
01049   return strecpy(buf, buffer, last);
01050 }

Generated on Thu Apr 14 00:48:22 2011 for OpenTTD by  doxygen 1.6.1