mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-01-27 17:32:06 +00:00
(svn r1951) Introduced SeedModChance() (which is like SeedChance() but uses simple modulo instead of bitshifting and multiplication), explained why does it work better, used it in MakeCzechTownName() and added a TODO note about possibly using it in the other town name generators too.
This commit is contained in:
parent
6902a8b294
commit
028ef2b5b5
30
namegen.c
30
namegen.c
@ -9,6 +9,20 @@ static inline uint32 SeedChance(int shift_by, int max, uint32 seed)
|
|||||||
return ((uint16)(seed >> shift_by) * max) >> 16;
|
return ((uint16)(seed >> shift_by) * max) >> 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline uint32 SeedModChance(int shift_by, int max, uint32 seed)
|
||||||
|
{
|
||||||
|
/* This actually gives *MUCH* more even distribution of the values
|
||||||
|
* than SeedChance(), which is absolutely horrible in that. If
|
||||||
|
* you do not believe me, try with i.e. the Czech town names,
|
||||||
|
* compare the words (nicely visible on prefixes) generated by
|
||||||
|
* SeedChance() and SeedModChance(). Do not get dicouraged by the
|
||||||
|
* never-use-modulo myths, which hold true only for the linear
|
||||||
|
* congruential generators (and Random() isn't such a generator).
|
||||||
|
* --pasky */
|
||||||
|
// TODO: Perhaps we should use it for all the name generators? --pasky
|
||||||
|
return (seed >> shift_by) % max;
|
||||||
|
}
|
||||||
|
|
||||||
static inline int32 SeedChanceBias(int shift_by, int max, uint32 seed, int bias)
|
static inline int32 SeedChanceBias(int shift_by, int max, uint32 seed, int bias)
|
||||||
{
|
{
|
||||||
return SeedChance(shift_by, max + bias, seed) - bias;
|
return SeedChance(shift_by, max + bias, seed) - bias;
|
||||||
@ -330,22 +344,22 @@ static byte MakeCzechTownName(char *buf, uint32 seed)
|
|||||||
enum CzechAllow allow;
|
enum CzechAllow allow;
|
||||||
|
|
||||||
// 1:3 chance to use a real name.
|
// 1:3 chance to use a real name.
|
||||||
if (SeedChance(0, 4, seed) == 0) {
|
if (SeedModChance(0, 4, seed) == 0) {
|
||||||
strcpy(buf, name_czech_real[SeedChance(1, lengthof(name_czech_real), seed)]);
|
strcpy(buf, name_czech_real[SeedModChance(4, lengthof(name_czech_real), seed)]);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NUL terminates the string for strcat()
|
// NUL terminates the string for strcat()
|
||||||
strcpy(buf, "");
|
strcpy(buf, "");
|
||||||
|
|
||||||
prob_tails = SeedChance(2, 32, seed);
|
prob_tails = SeedModChance(2, 32, seed);
|
||||||
do_prefix = prob_tails < 12;
|
do_prefix = prob_tails < 12;
|
||||||
do_suffix = prob_tails > 11 && prob_tails < 17;
|
do_suffix = prob_tails > 11 && prob_tails < 17;
|
||||||
|
|
||||||
if (do_prefix) prefix = SeedChance(5, lengthof(name_czech_adj), seed);
|
if (do_prefix) prefix = SeedModChance(5, lengthof(name_czech_adj) * 12, seed) / 12;
|
||||||
if (do_suffix) suffix = SeedChance(7, lengthof(name_czech_suffix), seed);
|
if (do_suffix) suffix = SeedModChance(7, lengthof(name_czech_suffix), seed);
|
||||||
// 3:1 chance 3:1 to use dynamic substantive
|
// 3:1 chance 3:1 to use dynamic substantive
|
||||||
stem = SeedChance(9, lengthof(name_czech_subst_full)
|
stem = SeedModChance(9, lengthof(name_czech_subst_full)
|
||||||
+ 3 * lengthof(name_czech_subst_stem),
|
+ 3 * lengthof(name_czech_subst_stem),
|
||||||
seed);
|
seed);
|
||||||
if (stem < (int) lengthof(name_czech_subst_full)) {
|
if (stem < (int) lengthof(name_czech_subst_full)) {
|
||||||
@ -369,7 +383,7 @@ static byte MakeCzechTownName(char *buf, uint32 seed)
|
|||||||
allow = name_czech_subst_stem[stem].allow;
|
allow = name_czech_subst_stem[stem].allow;
|
||||||
|
|
||||||
// Load the postfix (1:1 chance that a postfix will be inserted)
|
// Load the postfix (1:1 chance that a postfix will be inserted)
|
||||||
postfix = SeedChance(14, lengthof(name_czech_subst_postfix) * 2, seed);
|
postfix = SeedModChance(14, lengthof(name_czech_subst_postfix) * 2, seed);
|
||||||
|
|
||||||
if (choose & CZC_POSTFIX) {
|
if (choose & CZC_POSTFIX) {
|
||||||
// Always get a real postfix.
|
// Always get a real postfix.
|
||||||
@ -415,7 +429,7 @@ static byte MakeCzechTownName(char *buf, uint32 seed)
|
|||||||
assert(i > 0);
|
assert(i > 0);
|
||||||
|
|
||||||
// Load the ending
|
// Load the ending
|
||||||
ending = map[SeedChance(16, i, seed)];
|
ending = map[SeedModChance(16, i, seed)];
|
||||||
// Override possible CZG_*FREE; this must be a real gender,
|
// Override possible CZG_*FREE; this must be a real gender,
|
||||||
// otherwise we get overflow when modifying the adjectivum.
|
// otherwise we get overflow when modifying the adjectivum.
|
||||||
gender = name_czech_subst_ending[ending].gender;
|
gender = name_czech_subst_ending[ending].gender;
|
||||||
|
Loading…
Reference in New Issue
Block a user