summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortron <tron@openttd.org>2005-01-12 22:10:50 +0000
committertron <tron@openttd.org>2005-01-12 22:10:50 +0000
commit131f4c50fdd698030d5839d41799e0ff283e91d6 (patch)
tree76c5f05a325a58e7b1e56b4fbf000a5196c71ff5
parent253a02e7126514a53ee80f2f0a9181c21d72a000 (diff)
downloadopenttd-131f4c50fdd698030d5839d41799e0ff283e91d6.tar.xz
(svn r1494) Give GetNumberBasedOnSeed() a bit more sane name: SeedChance()
Add SeedChanceBias() which subtracts a bias from SeedChance() to reduce code duplication While touching the lines anyway sprinkle a bit holy ANSI water (static before inline) and move assignments in the conditions of ifs before the ifs
-rw-r--r--namegen.c176
1 files changed, 93 insertions, 83 deletions
diff --git a/namegen.c b/namegen.c
index e6fee0daa..99d220389 100644
--- a/namegen.c
+++ b/namegen.c
@@ -3,9 +3,14 @@
#include "table/namegen.h"
-inline static uint32 GetNumberBasedOnSeed(int x, int y, uint32 seed)
+static inline uint32 SeedChance(int shift_by, int max, uint32 seed)
{
- return (((uint16)(seed >> x) * (y))>>16);
+ return ((uint16)(seed >> shift_by) * max) >> 16;
+}
+
+static inline int32 SeedChanceBias(int shift_by, int max, uint32 seed, int bias)
+{
+ return SeedChance(shift_by, max + bias, seed) - bias;
}
static void ReplaceWords(byte a, byte b, byte c, byte d, byte e, byte f, byte g, byte h, byte *buf)
@@ -26,17 +31,19 @@ static byte MakeEnglishOriginalTownName(byte *buf, uint32 seed)
strcpy(buf, "");
// optional first segment
- if ((i = GetNumberBasedOnSeed(0, lengthof(name_original_english_1) + 50, seed) - 50) >= 0)
+ i = SeedChanceBias(0, lengthof(name_original_english_1), seed, 50);
+ if (i >= 0)
strcat(buf,name_original_english_1[i]);
//mandatory middle segments
- strcat(buf, name_original_english_2[GetNumberBasedOnSeed(4, lengthof(name_original_english_2), seed)]);
- strcat(buf, name_original_english_3[GetNumberBasedOnSeed(7, lengthof(name_original_english_3), seed)]);
- strcat(buf, name_original_english_4[GetNumberBasedOnSeed(10, lengthof(name_original_english_4), seed)]);
- strcat(buf, name_original_english_5[GetNumberBasedOnSeed(13, lengthof(name_original_english_5), seed)]);
+ strcat(buf, name_original_english_2[SeedChance(4, lengthof(name_original_english_2), seed)]);
+ strcat(buf, name_original_english_3[SeedChance(7, lengthof(name_original_english_3), seed)]);
+ strcat(buf, name_original_english_4[SeedChance(10, lengthof(name_original_english_4), seed)]);
+ strcat(buf, name_original_english_5[SeedChance(13, lengthof(name_original_english_5), seed)]);
//optional last segment
- if ((i = GetNumberBasedOnSeed(15, lengthof(name_original_english_6) + 60, seed) - 60) >= 0)
+ i = SeedChanceBias(15, lengthof(name_original_english_6), seed, 60);
+ if (i >= 0)
strcat(buf, name_original_english_6[i]);
if (buf[0] == 'C' && (buf[1] == 'e' || buf[1] == 'i'))
@@ -65,25 +72,27 @@ static byte MakeEnglishAdditionalTownName(byte *buf, uint32 seed)
strcpy(buf, "");
// optional first segment
- if ((i = GetNumberBasedOnSeed(0, lengthof(name_additional_english_prefix) + 50, seed) - 50) >= 0)
+ i = SeedChanceBias(0, lengthof(name_additional_english_prefix), seed, 50);
+ if (i >= 0)
strcat(buf,name_additional_english_prefix[i]);
- if (GetNumberBasedOnSeed(3, 20, seed) >= 14) {
- strcat(buf, name_additional_english_1a[GetNumberBasedOnSeed(6, lengthof(name_additional_english_1a), seed)]);
+ if (SeedChance(3, 20, seed) >= 14) {
+ strcat(buf, name_additional_english_1a[SeedChance(6, lengthof(name_additional_english_1a), seed)]);
} else {
- strcat(buf, name_additional_english_1b1[GetNumberBasedOnSeed(6, lengthof(name_additional_english_1b1), seed)]);
- strcat(buf, name_additional_english_1b2[GetNumberBasedOnSeed(9, lengthof(name_additional_english_1b2), seed)]);
- if (GetNumberBasedOnSeed(11, 20, seed) >= 4) {
- strcat(buf, name_additional_english_1b3a[GetNumberBasedOnSeed(12, lengthof(name_additional_english_1b3a), seed)]);
+ strcat(buf, name_additional_english_1b1[SeedChance(6, lengthof(name_additional_english_1b1), seed)]);
+ strcat(buf, name_additional_english_1b2[SeedChance(9, lengthof(name_additional_english_1b2), seed)]);
+ if (SeedChance(11, 20, seed) >= 4) {
+ strcat(buf, name_additional_english_1b3a[SeedChance(12, lengthof(name_additional_english_1b3a), seed)]);
} else {
- strcat(buf, name_additional_english_1b3b[GetNumberBasedOnSeed(12, lengthof(name_additional_english_1b3b), seed)]);
+ strcat(buf, name_additional_english_1b3b[SeedChance(12, lengthof(name_additional_english_1b3b), seed)]);
}
}
- strcat(buf, name_additional_english_2[GetNumberBasedOnSeed(14, lengthof(name_additional_english_2), seed)]);
+ strcat(buf, name_additional_english_2[SeedChance(14, lengthof(name_additional_english_2), seed)]);
//optional last segment
- if ((i = GetNumberBasedOnSeed(15, lengthof(name_additional_english_3) + 60, seed) - 60) >= 0)
+ i = SeedChanceBias(15, lengthof(name_additional_english_3), seed, 60);
+ if (i >= 0)
strcat(buf, name_additional_english_3[i]);
ReplaceWords('C','u','n','t', 'E','a','s','t', buf);
@@ -106,34 +115,33 @@ static byte MakeAustrianTownName(byte *buf, uint32 seed)
strcpy(buf, "");
// Bad, Maria, Gross, ...
- i = GetNumberBasedOnSeed(0, lengthof(name_austrian_a1) + 15,seed) - 15;
-
+ i = SeedChanceBias(0, lengthof(name_austrian_a1), seed, 15);
if (i >= 0) strcat(buf, name_austrian_a1[i]);
- i = GetNumberBasedOnSeed(4, 6, seed);
+ i = SeedChance(4, 6, seed);
if (i >= 4) {
// Kaisers-kirchen
- strcat(buf, name_austrian_a2[GetNumberBasedOnSeed( 7, lengthof(name_austrian_a2), seed)]);
- strcat(buf, name_austrian_a3[GetNumberBasedOnSeed(13, lengthof(name_austrian_a3), seed)]);
+ strcat(buf, name_austrian_a2[SeedChance( 7, lengthof(name_austrian_a2), seed)]);
+ strcat(buf, name_austrian_a3[SeedChance(13, lengthof(name_austrian_a3), seed)]);
} else if (i >= 2) {
// St. Johann
- strcat(buf, name_austrian_a5[GetNumberBasedOnSeed( 7, lengthof(name_austrian_a5), seed)]);
- strcat(buf, name_austrian_a6[GetNumberBasedOnSeed( 9, lengthof(name_austrian_a6), seed)]);
+ strcat(buf, name_austrian_a5[SeedChance( 7, lengthof(name_austrian_a5), seed)]);
+ strcat(buf, name_austrian_a6[SeedChance( 9, lengthof(name_austrian_a6), seed)]);
j = 1; // More likely to have a " an der " or " am "
} else {
// Zell
- strcat(buf, name_austrian_a4[GetNumberBasedOnSeed( 7, lengthof(name_austrian_a4), seed)]);
+ strcat(buf, name_austrian_a4[SeedChance( 7, lengthof(name_austrian_a4), seed)]);
}
- i = GetNumberBasedOnSeed(1, 6, seed);
+ i = SeedChance(1, 6, seed);
if (i >= 4 - j) {
// an der Donau (rivers)
- strcat(buf, name_austrian_f1[GetNumberBasedOnSeed(4, lengthof(name_austrian_f1), seed)]);
- strcat(buf, name_austrian_f2[GetNumberBasedOnSeed(5, lengthof(name_austrian_f2), seed)]);
+ strcat(buf, name_austrian_f1[SeedChance(4, lengthof(name_austrian_f1), seed)]);
+ strcat(buf, name_austrian_f2[SeedChance(5, lengthof(name_austrian_f2), seed)]);
} else if (i >= 2 - j) {
// am Dachstein (mountains)
- strcat(buf, name_austrian_b1[GetNumberBasedOnSeed(4, lengthof(name_austrian_b1), seed)]);
- strcat(buf, name_austrian_b2[GetNumberBasedOnSeed(5, lengthof(name_austrian_b2), seed)]);
+ strcat(buf, name_austrian_b1[SeedChance(4, lengthof(name_austrian_b1), seed)]);
+ strcat(buf, name_austrian_b2[SeedChance(5, lengthof(name_austrian_b2), seed)]);
}
return 0;
@@ -147,29 +155,28 @@ static byte MakeGermanTownName(byte *buf, uint32 seed)
//null terminates the string for strcat
strcpy(buf, "");
- seed_derivative = GetNumberBasedOnSeed(7, 28, seed);
+ seed_derivative = SeedChance(7, 28, seed);
//optional prefix
if (seed_derivative == 12 || seed_derivative == 19) {
- i = GetNumberBasedOnSeed(2, lengthof(name_german_pre), seed);
+ i = SeedChance(2, lengthof(name_german_pre), seed);
strcat(buf,name_german_pre[i]);
}
- i = GetNumberBasedOnSeed(3, lengthof(name_german_hardcoded) + lengthof(name_german_1), seed);
-
// mandatory middle segments including option of hardcoded name
+ i = SeedChance(3, lengthof(name_german_hardcoded) + lengthof(name_german_1), seed);
if (i < lengthof(name_german_hardcoded)) {
strcat(buf,name_german_hardcoded[i]);
} else {
strcat(buf, name_german_1[i - lengthof(name_german_hardcoded)]);
- i = GetNumberBasedOnSeed(5, lengthof(name_german_2), seed);
+ i = SeedChance(5, lengthof(name_german_2), seed);
strcat(buf, name_german_2[i]);
}
// optional suffix
if (seed_derivative == 24) {
- i = GetNumberBasedOnSeed(9,
+ i = SeedChance(9,
lengthof(name_german_4_an_der) + lengthof(name_german_4_am), seed);
if (i < lengthof(name_german_4_an_der)) {
strcat(buf, name_german_3_an_der[0]);
@@ -184,20 +191,20 @@ static byte MakeGermanTownName(byte *buf, uint32 seed)
static byte MakeSpanishTownName(byte *buf, uint32 seed)
{
- strcpy(buf, name_spanish_1[GetNumberBasedOnSeed(0, lengthof(name_spanish_1), seed)]);
+ strcpy(buf, name_spanish_1[SeedChance(0, lengthof(name_spanish_1), seed)]);
return 0;
}
static byte MakeFrenchTownName(byte *buf, uint32 seed)
{
- strcpy(buf, name_french_1[GetNumberBasedOnSeed(0, lengthof(name_french_1), seed)]);
+ strcpy(buf, name_french_1[SeedChance(0, lengthof(name_french_1), seed)]);
return 0;
}
static byte MakeSillyTownName(byte *buf, uint32 seed)
{
- strcpy(buf, name_silly_1[GetNumberBasedOnSeed( 0, lengthof(name_silly_1), seed)]);
- strcat(buf, name_silly_2[GetNumberBasedOnSeed(16, lengthof(name_silly_2), seed)]);
+ strcpy(buf, name_silly_1[SeedChance( 0, lengthof(name_silly_1), seed)]);
+ strcat(buf, name_silly_2[SeedChance(16, lengthof(name_silly_2), seed)]);
return 0;
}
@@ -209,19 +216,20 @@ static byte MakeSwedishTownName(byte *buf, uint32 seed)
strcpy(buf, "");
// optional first segment
- if ((i = GetNumberBasedOnSeed(0, lengthof(name_swedish_1) + 50, seed) - 50) >= 0)
+ i = SeedChanceBias(0, lengthof(name_swedish_1), seed, 50);
+ if (i >= 0)
strcat(buf, name_swedish_1[i]);
// mandatory middle segments including option of hardcoded name
- if (GetNumberBasedOnSeed(4, 5, seed) >= 3) {
- strcat(buf, name_swedish_2[GetNumberBasedOnSeed( 7, lengthof(name_swedish_2), seed)]);
+ if (SeedChance(4, 5, seed) >= 3) {
+ strcat(buf, name_swedish_2[SeedChance( 7, lengthof(name_swedish_2), seed)]);
} else {
- strcat(buf, name_swedish_2a[GetNumberBasedOnSeed( 7, lengthof(name_swedish_2a), seed)]);
- strcat(buf, name_swedish_2b[GetNumberBasedOnSeed(10, lengthof(name_swedish_2b), seed)]);
- strcat(buf, name_swedish_2c[GetNumberBasedOnSeed(13, lengthof(name_swedish_2c), seed)]);
+ strcat(buf, name_swedish_2a[SeedChance( 7, lengthof(name_swedish_2a), seed)]);
+ strcat(buf, name_swedish_2b[SeedChance(10, lengthof(name_swedish_2b), seed)]);
+ strcat(buf, name_swedish_2c[SeedChance(13, lengthof(name_swedish_2c), seed)]);
}
- strcat(buf, name_swedish_3[GetNumberBasedOnSeed(16, lengthof(name_swedish_3), seed)]);
+ strcat(buf, name_swedish_3[SeedChance(16, lengthof(name_swedish_3), seed)]);
return 0;
}
@@ -234,17 +242,18 @@ static byte MakeDutchTownName(byte *buf, uint32 seed)
strcpy(buf, "");
// optional first segment
- if ((i = GetNumberBasedOnSeed(0, lengthof(name_dutch_1) + 50, seed) - 50) >= 0)
+ i = SeedChanceBias(0, lengthof(name_dutch_1), seed, 50);
+ if (i >= 0)
strcat(buf, name_dutch_1[i]);
// mandatory middle segments including option of hardcoded name
- if (GetNumberBasedOnSeed(6, 9, seed) > 4) {
- strcat(buf, name_dutch_2[GetNumberBasedOnSeed( 9, lengthof(name_dutch_2), seed)]);
+ if (SeedChance(6, 9, seed) > 4) {
+ strcat(buf, name_dutch_2[SeedChance( 9, lengthof(name_dutch_2), seed)]);
} else {
- strcat(buf, name_dutch_3[GetNumberBasedOnSeed( 9, lengthof(name_dutch_3), seed)]);
- strcat(buf, name_dutch_4[GetNumberBasedOnSeed(12, lengthof(name_dutch_4), seed)]);
+ strcat(buf, name_dutch_3[SeedChance( 9, lengthof(name_dutch_3), seed)]);
+ strcat(buf, name_dutch_4[SeedChance(12, lengthof(name_dutch_4), seed)]);
}
- strcat(buf, name_dutch_5[GetNumberBasedOnSeed(15, lengthof(name_dutch_5), seed)]);
+ strcat(buf, name_dutch_5[SeedChance(15, lengthof(name_dutch_5), seed)]);
return 0;
}
@@ -255,11 +264,11 @@ static byte MakeFinnishTownName(byte *buf, uint32 seed)
strcpy(buf, "");
// Select randomly if town name should consists of one or two parts.
- if (GetNumberBasedOnSeed(0, 15, seed) >= 10) {
- strcat(buf, name_finnish_1[GetNumberBasedOnSeed( 2, lengthof(name_finnish_1), seed)]);
+ if (SeedChance(0, 15, seed) >= 10) {
+ strcat(buf, name_finnish_1[SeedChance( 2, lengthof(name_finnish_1), seed)]);
} else {
- strcat(buf, name_finnish_2a[GetNumberBasedOnSeed( 2, lengthof(name_finnish_2a), seed)]);
- strcat(buf, name_finnish_2b[GetNumberBasedOnSeed(10, lengthof(name_finnish_2b), seed)]);
+ strcat(buf, name_finnish_2a[SeedChance( 2, lengthof(name_finnish_2a), seed)]);
+ strcat(buf, name_finnish_2b[SeedChance(10, lengthof(name_finnish_2b), seed)]);
}
return 0;
@@ -274,58 +283,58 @@ static byte MakePolishTownName(byte *buf, uint32 seed)
strcpy(buf, "");
// optional first segment
- i = GetNumberBasedOnSeed(0,
+ i = SeedChance(0,
lengthof(name_polish_2_o) + lengthof(name_polish_2_m) +
lengthof(name_polish_2_f) + lengthof(name_polish_2_n),
seed);
- j = GetNumberBasedOnSeed(2, 20, seed);
+ j = SeedChance(2, 20, seed);
if (i < lengthof(name_polish_2_o)) {
- strcat(buf, name_polish_2_o[GetNumberBasedOnSeed(3, lengthof(name_polish_2_o), seed)]);
+ strcat(buf, name_polish_2_o[SeedChance(3, lengthof(name_polish_2_o), seed)]);
} else if (i < lengthof(name_polish_2_m) + lengthof(name_polish_2_o)) {
if (j < 4)
- strcat(buf, name_polish_1_m[GetNumberBasedOnSeed(5, lengthof(name_polish_1_m), seed)]);
+ strcat(buf, name_polish_1_m[SeedChance(5, lengthof(name_polish_1_m), seed)]);
- strcat(buf, name_polish_2_m[GetNumberBasedOnSeed(7, lengthof(name_polish_2_m), seed)]);
+ strcat(buf, name_polish_2_m[SeedChance(7, lengthof(name_polish_2_m), seed)]);
if (j >= 4 && j < 16)
- strcat(buf, name_polish_3_m[GetNumberBasedOnSeed(10, lengthof(name_polish_3_m), seed)]);
+ strcat(buf, name_polish_3_m[SeedChance(10, lengthof(name_polish_3_m), seed)]);
} else if (i < lengthof(name_polish_2_f) + lengthof(name_polish_2_m) + lengthof(name_polish_2_o)) {
if (j < 4)
- strcat(buf, name_polish_1_f[GetNumberBasedOnSeed(5, lengthof(name_polish_1_f), seed)]);
+ strcat(buf, name_polish_1_f[SeedChance(5, lengthof(name_polish_1_f), seed)]);
- strcat(buf, name_polish_2_f[GetNumberBasedOnSeed(7, lengthof(name_polish_2_f), seed)]);
+ strcat(buf, name_polish_2_f[SeedChance(7, lengthof(name_polish_2_f), seed)]);
if (j >= 4 && j < 16)
- strcat(buf, name_polish_3_f[GetNumberBasedOnSeed(10, lengthof(name_polish_3_f), seed)]);
+ strcat(buf, name_polish_3_f[SeedChance(10, lengthof(name_polish_3_f), seed)]);
} else {
if (j < 4)
- strcat(buf, name_polish_1_n[GetNumberBasedOnSeed(5, lengthof(name_polish_1_n), seed)]);
+ strcat(buf, name_polish_1_n[SeedChance(5, lengthof(name_polish_1_n), seed)]);
- strcat(buf, name_polish_2_n[GetNumberBasedOnSeed(7, lengthof(name_polish_2_n), seed)]);
+ strcat(buf, name_polish_2_n[SeedChance(7, lengthof(name_polish_2_n), seed)]);
if (j >= 4 && j < 16)
- strcat(buf, name_polish_3_n[GetNumberBasedOnSeed(10, lengthof(name_polish_3_n), seed)]);
+ strcat(buf, name_polish_3_n[SeedChance(10, lengthof(name_polish_3_n), seed)]);
}
return 0;
}
static byte MakeCzechTownName(byte *buf, uint32 seed)
{
- strcpy(buf, name_czech_1[GetNumberBasedOnSeed(0, lengthof(name_czech_1), seed)]);
+ strcpy(buf, name_czech_1[SeedChance(0, lengthof(name_czech_1), seed)]);
return 0;
}
static byte MakeRomanianTownName(byte *buf, uint32 seed)
{
- strcpy(buf, name_romanian_1[GetNumberBasedOnSeed(0, lengthof(name_romanian_1), seed)]);
+ strcpy(buf, name_romanian_1[SeedChance(0, lengthof(name_romanian_1), seed)]);
return 0;
}
static byte MakeSlovakTownName(byte *buf, uint32 seed)
{
- strcpy(buf, name_slovakish_1[GetNumberBasedOnSeed(0, lengthof(name_slovakish_1), seed)]);
+ strcpy(buf, name_slovakish_1[SeedChance(0, lengthof(name_slovakish_1), seed)]);
return 0;
}
@@ -335,14 +344,14 @@ static byte MakeNorwegianTownName(byte *buf, uint32 seed)
// Use first 4 bit from seed to decide whether or not this town should
// have a real name 3/16 chance. Bit 0-3
- if (GetNumberBasedOnSeed(0, 15, seed) < 3) {
+ if (SeedChance(0, 15, seed) < 3) {
// Use 7bit for the realname table index. Bit 4-10
- strcat(buf, name_norwegian_real[GetNumberBasedOnSeed(4, lengthof(name_norwegian_real), seed)]);
+ strcat(buf, name_norwegian_real[SeedChance(4, lengthof(name_norwegian_real), seed)]);
} else {
// Use 7bit for the first fake part. Bit 4-10
- strcat(buf, name_norwegian_1[GetNumberBasedOnSeed(4, lengthof(name_norwegian_1), seed)]);
+ strcat(buf, name_norwegian_1[SeedChance(4, lengthof(name_norwegian_1), seed)]);
// Use 7bit for the last fake part. Bit 11-17
- strcat(buf, name_norwegian_2[GetNumberBasedOnSeed(11, lengthof(name_norwegian_2), seed)]);
+ strcat(buf, name_norwegian_2[SeedChance(11, lengthof(name_norwegian_2), seed)]);
}
return 0;
@@ -355,20 +364,21 @@ static byte MakeHungarianTownName(byte *buf, uint32 seed)
//null terminates the string for strcat
strcpy(buf, "");
- if (GetNumberBasedOnSeed(12, 15, seed) < 3) {
- strcat(buf, name_hungarian_real[GetNumberBasedOnSeed(0, lengthof(name_hungarian_real), seed)]);
+ if (SeedChance(12, 15, seed) < 3) {
+ strcat(buf, name_hungarian_real[SeedChance(0, lengthof(name_hungarian_real), seed)]);
} else {
// optional first segment
- i = GetNumberBasedOnSeed(3, lengthof(name_hungarian_1) * 3, seed);
+ i = SeedChance(3, lengthof(name_hungarian_1) * 3, seed);
if (i < lengthof(name_hungarian_1))
strcat(buf, name_hungarian_1[i]);
// mandatory middle segments
- strcat(buf, name_hungarian_2[GetNumberBasedOnSeed(3, lengthof(name_hungarian_2), seed)]);
- strcat(buf, name_hungarian_3[GetNumberBasedOnSeed(6, lengthof(name_hungarian_3), seed)]);
+ strcat(buf, name_hungarian_2[SeedChance(3, lengthof(name_hungarian_2), seed)]);
+ strcat(buf, name_hungarian_3[SeedChance(6, lengthof(name_hungarian_3), seed)]);
// optional last segment
- if ((i = GetNumberBasedOnSeed(10, lengthof(name_hungarian_4) * 3, seed)) < lengthof(name_hungarian_4)) {
+ i = SeedChance(10, lengthof(name_hungarian_4) * 3, seed);
+ if (i < lengthof(name_hungarian_4)) {
strcat(buf, name_hungarian_4[i]);
}
}