Codechange: let town name generation use StringBuilder

This commit is contained in:
Rubidium 2023-05-11 21:30:58 +02:00 committed by rubidium42
parent 6a5ab4a9fb
commit 4e39a58c59
4 changed files with 244 additions and 272 deletions

View File

@ -1664,7 +1664,7 @@ static void StationGetSpecialString(StringBuilder &builder, int x)
static void GetSpecialTownNameString(StringBuilder &builder, int ind, uint32 seed)
{
builder.AddViaStreCallback([&](auto buff, auto last) { return GenerateTownNameString(buff, last, ind, seed); });
GenerateTownNameString(builder, ind, seed);
}
static const char * const _silly_company_names[] = {

View File

@ -20,7 +20,8 @@
* extra functions to ease the migration from char buffers to std::string.
*/
class StringBuilder {
char **current; ///< The current location to add strings
char **current; ///< The current location to add strings.
char *start; ///< The begin of the string.
const char *last; ///< The last element of the buffer.
public:
@ -36,7 +37,7 @@ public:
* @param start The start location to write to.
* @param last The last location to write to.
*/
StringBuilder(char **start, const char *last) : current(start), last(last) {}
StringBuilder(char **start, const char *last) : current(start), start(*start), last(last) {}
/* Required operators for this to be an output_iterator; mimics std::back_insert_iterator, which has no-ops. */
StringBuilder &operator++() { return *this; }
@ -100,6 +101,15 @@ public:
return true;
}
/**
* Remove the given amount of characters from the back of the string.
* @param amount The amount of characters to remove.
*/
void RemoveElementsFromBack(size_t amount)
{
*this->current = std::max(this->start, *this->current - amount);
}
/**
* Get the pointer to the this->last written element in the buffer.
* This call does '\0' terminate the string, whereas other calls do not
@ -122,19 +132,28 @@ public:
}
/**
* Add a string using the strecpy-esque calling signature.
* @param function The function to pass the current and last location to,
* that will then return the new current location.
* Get the current index in the string.
* @return The index.
*/
void AddViaStreCallback(std::function<char*(char*, const char*)> function)
size_t CurrentIndex()
{
*this->current = function(*this->current, this->last);
return *this->current - this->start;
}
/**
* Get the reference to the character at the given index.
* @return The reference to the character.
*/
char &operator[](size_t index)
{
return this->start[index];
}
};
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters *args, uint case_index = 0, bool game_script = false);
/* Do not leak the StringBuilder to everywhere. */
void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed);
void GetTownName(StringBuilder &builder, const struct Town *t);
void GRFTownNameGenerate(StringBuilder &builder, uint32 grfid, uint16 gen, uint32 seed);

View File

@ -220,303 +220,282 @@ static inline int32 SeedChanceBias(byte shift_by, int max, uint32 seed, int bias
/**
* Replaces a string beginning in 'org' with 'rep'.
* @param org string to replace, has to be 4 characters long
* @param rep string to be replaced with, has to be 4 characters long
* @param buf buffer with string
* @param org string to replace, has to be 4 characters long
* @param rep string to be replaced with, has to be 4 characters long
* @param builder string builder of the town name
* @param start the start index within the builder for the town name
*/
static void ReplaceWords(const char *org, const char *rep, char *buf)
static void ReplaceWords(const char *org, const char *rep, StringBuilder &builder, size_t start)
{
assert(strlen(org) == 4 && strlen(rep) == 4 && strlen(buf) >= 4);
if (strncmp(buf, org, 4) == 0) memcpy(buf, rep, 4); // Safe as the string in buf is always more than 4 characters long.
assert(strlen(org) == 4 && strlen(rep) == 4 && builder.CurrentIndex() - start >= 4);
if (strncmp(&builder[start], org, 4) == 0) memcpy(&builder[start], rep, 4); // Safe as the string in buf is always more than 4 characters long.
}
/**
* Replaces english curses and ugly letter combinations by nicer ones.
* @param buf buffer with town name
* @param builder The builder with the town name
* @param start The start index into the builder for the first town name
* @param original English (Original) generator was used
*/
static void ReplaceEnglishWords(char *buf, bool original)
static void ReplaceEnglishWords(StringBuilder &builder, size_t start, bool original)
{
ReplaceWords("Cunt", "East", buf);
ReplaceWords("Slag", "Pits", buf);
ReplaceWords("Slut", "Edin", buf);
if (!original) ReplaceWords("Fart", "Boot", buf); // never happens with 'English (Original)'
ReplaceWords("Drar", "Quar", buf);
ReplaceWords("Dreh", "Bash", buf);
ReplaceWords("Frar", "Shor", buf);
ReplaceWords("Grar", "Aber", buf);
ReplaceWords("Brar", "Over", buf);
ReplaceWords("Wrar", original ? "Inve" : "Stan", buf);
ReplaceWords("Cunt", "East", builder, start);
ReplaceWords("Slag", "Pits", builder, start);
ReplaceWords("Slut", "Edin", builder, start);
if (!original) ReplaceWords("Fart", "Boot", builder, start); // never happens with 'English (Original)'
ReplaceWords("Drar", "Quar", builder, start);
ReplaceWords("Dreh", "Bash", builder, start);
ReplaceWords("Frar", "Shor", builder, start);
ReplaceWords("Grar", "Aber", builder, start);
ReplaceWords("Brar", "Over", builder, start);
ReplaceWords("Wrar", original ? "Inve" : "Stan", builder, start);
}
/**
* Generates English (Original) town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeEnglishOriginalTownName(char *buf, const char *last, uint32 seed)
static void MakeEnglishOriginalTownName(StringBuilder &builder, uint32_t seed)
{
char *orig = buf;
size_t start = builder.CurrentIndex();
/* optional first segment */
int i = SeedChanceBias(0, lengthof(_name_original_english_1), seed, 50);
if (i >= 0) buf = strecpy(buf, _name_original_english_1[i], last);
if (i >= 0) builder += _name_original_english_1[i];
/* mandatory middle segments */
buf = strecpy(buf, _name_original_english_2[SeedChance(4, lengthof(_name_original_english_2), seed)], last);
buf = strecpy(buf, _name_original_english_3[SeedChance(7, lengthof(_name_original_english_3), seed)], last);
buf = strecpy(buf, _name_original_english_4[SeedChance(10, lengthof(_name_original_english_4), seed)], last);
buf = strecpy(buf, _name_original_english_5[SeedChance(13, lengthof(_name_original_english_5), seed)], last);
builder += _name_original_english_2[SeedChance(4, lengthof(_name_original_english_2), seed)];
builder += _name_original_english_3[SeedChance(7, lengthof(_name_original_english_3), seed)];
builder += _name_original_english_4[SeedChance(10, lengthof(_name_original_english_4), seed)];
builder += _name_original_english_5[SeedChance(13, lengthof(_name_original_english_5), seed)];
/* optional last segment */
i = SeedChanceBias(15, lengthof(_name_original_english_6), seed, 60);
if (i >= 0) buf = strecpy(buf, _name_original_english_6[i], last);
if (i >= 0) builder += _name_original_english_6[i];
/* Ce, Ci => Ke, Ki */
if (orig[0] == 'C' && (orig[1] == 'e' || orig[1] == 'i')) {
orig[0] = 'K';
if (builder[start] == 'C' && (builder[start + 1] == 'e' || builder[start + 1] == 'i')) {
builder[start] = 'K';
}
assert(buf - orig >= 4);
ReplaceEnglishWords(orig, true);
return buf;
assert(builder.CurrentIndex() - start >= 4);
ReplaceEnglishWords(builder, start, true);
}
/**
* Generates English (Additional) town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeEnglishAdditionalTownName(char *buf, const char *last, uint32 seed)
static void MakeEnglishAdditionalTownName(StringBuilder &builder, uint32_t seed)
{
char *orig = buf;
size_t start = builder.CurrentIndex();
/* optional first segment */
int i = SeedChanceBias(0, lengthof(_name_additional_english_prefix), seed, 50);
if (i >= 0) buf = strecpy(buf, _name_additional_english_prefix[i], last);
if (i >= 0) builder += _name_additional_english_prefix[i];
if (SeedChance(3, 20, seed) >= 14) {
buf = strecpy(buf, _name_additional_english_1a[SeedChance(6, lengthof(_name_additional_english_1a), seed)], last);
builder += _name_additional_english_1a[SeedChance(6, lengthof(_name_additional_english_1a), seed)];
} else {
buf = strecpy(buf, _name_additional_english_1b1[SeedChance(6, lengthof(_name_additional_english_1b1), seed)], last);
buf = strecpy(buf, _name_additional_english_1b2[SeedChance(9, lengthof(_name_additional_english_1b2), seed)], last);
builder += _name_additional_english_1b1[SeedChance(6, lengthof(_name_additional_english_1b1), seed)];
builder += _name_additional_english_1b2[SeedChance(9, lengthof(_name_additional_english_1b2), seed)];
if (SeedChance(11, 20, seed) >= 4) {
buf = strecpy(buf, _name_additional_english_1b3a[SeedChance(12, lengthof(_name_additional_english_1b3a), seed)], last);
builder += _name_additional_english_1b3a[SeedChance(12, lengthof(_name_additional_english_1b3a), seed)];
} else {
buf = strecpy(buf, _name_additional_english_1b3b[SeedChance(12, lengthof(_name_additional_english_1b3b), seed)], last);
builder += _name_additional_english_1b3b[SeedChance(12, lengthof(_name_additional_english_1b3b), seed)];
}
}
buf = strecpy(buf, _name_additional_english_2[SeedChance(14, lengthof(_name_additional_english_2), seed)], last);
builder += _name_additional_english_2[SeedChance(14, lengthof(_name_additional_english_2), seed)];
/* optional last segment */
i = SeedChanceBias(15, lengthof(_name_additional_english_3), seed, 60);
if (i >= 0) buf = strecpy(buf, _name_additional_english_3[i], last);
if (i >= 0) builder += _name_additional_english_3[i];
assert(buf - orig >= 4);
ReplaceEnglishWords(orig, false);
return buf;
assert(builder.CurrentIndex() - start >= 4);
ReplaceEnglishWords(builder, start, false);
}
/**
* Generates Austrian town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeAustrianTownName(char *buf, const char *last, uint32 seed)
static void MakeAustrianTownName(StringBuilder &builder, uint32_t seed)
{
/* Bad, Maria, Gross, ... */
int i = SeedChanceBias(0, lengthof(_name_austrian_a1), seed, 15);
if (i >= 0) buf = strecpy(buf, _name_austrian_a1[i], last);
if (i >= 0) builder += _name_austrian_a1[i];
int j = 0;
i = SeedChance(4, 6, seed);
if (i >= 4) {
/* Kaisers-kirchen */
buf = strecpy(buf, _name_austrian_a2[SeedChance( 7, lengthof(_name_austrian_a2), seed)], last);
buf = strecpy(buf, _name_austrian_a3[SeedChance(13, lengthof(_name_austrian_a3), seed)], last);
builder += _name_austrian_a2[SeedChance( 7, lengthof(_name_austrian_a2), seed)];
builder += _name_austrian_a3[SeedChance(13, lengthof(_name_austrian_a3), seed)];
} else if (i >= 2) {
/* St. Johann */
buf = strecpy(buf, _name_austrian_a5[SeedChance( 7, lengthof(_name_austrian_a5), seed)], last);
buf = strecpy(buf, _name_austrian_a6[SeedChance( 9, lengthof(_name_austrian_a6), seed)], last);
builder += _name_austrian_a5[SeedChance( 7, lengthof(_name_austrian_a5), seed)];
builder += _name_austrian_a6[SeedChance( 9, lengthof(_name_austrian_a6), seed)];
j = 1; // More likely to have a " an der " or " am "
} else {
/* Zell */
buf = strecpy(buf, _name_austrian_a4[SeedChance( 7, lengthof(_name_austrian_a4), seed)], last);
builder += _name_austrian_a4[SeedChance( 7, lengthof(_name_austrian_a4), seed)];
}
i = SeedChance(1, 6, seed);
if (i >= 4 - j) {
/* an der Donau (rivers) */
buf = strecpy(buf, _name_austrian_f1[SeedChance(4, lengthof(_name_austrian_f1), seed)], last);
buf = strecpy(buf, _name_austrian_f2[SeedChance(5, lengthof(_name_austrian_f2), seed)], last);
builder += _name_austrian_f1[SeedChance(4, lengthof(_name_austrian_f1), seed)];
builder += _name_austrian_f2[SeedChance(5, lengthof(_name_austrian_f2), seed)];
} else if (i >= 2 - j) {
/* am Dachstein (mountains) */
buf = strecpy(buf, _name_austrian_b1[SeedChance(4, lengthof(_name_austrian_b1), seed)], last);
buf = strecpy(buf, _name_austrian_b2[SeedChance(5, lengthof(_name_austrian_b2), seed)], last);
builder += _name_austrian_b1[SeedChance(4, lengthof(_name_austrian_b1), seed)];
builder += _name_austrian_b2[SeedChance(5, lengthof(_name_austrian_b2), seed)];
}
return buf;
}
/**
* Generates German town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeGermanTownName(char *buf, const char *last, uint32 seed)
static void MakeGermanTownName(StringBuilder &builder, uint32_t seed)
{
uint seed_derivative = SeedChance(7, 28, seed);
/* optional prefix */
if (seed_derivative == 12 || seed_derivative == 19) {
uint i = SeedChance(2, lengthof(_name_german_pre), seed);
buf = strecpy(buf, _name_german_pre[i], last);
builder += _name_german_pre[i];
}
/* mandatory middle segments including option of hardcoded name */
uint i = SeedChance(3, lengthof(_name_german_real) + lengthof(_name_german_1), seed);
if (i < lengthof(_name_german_real)) {
buf = strecpy(buf, _name_german_real[i], last);
builder += _name_german_real[i];
} else {
buf = strecpy(buf, _name_german_1[i - lengthof(_name_german_real)], last);
builder += _name_german_1[i - lengthof(_name_german_real)];
i = SeedChance(5, lengthof(_name_german_2), seed);
buf = strecpy(buf, _name_german_2[i], last);
builder += _name_german_2[i];
}
/* optional suffix */
if (seed_derivative == 24) {
i = SeedChance(9, lengthof(_name_german_4_an_der) + lengthof(_name_german_4_am), seed);
if (i < lengthof(_name_german_4_an_der)) {
buf = strecpy(buf, _name_german_3_an_der[0], last);
buf = strecpy(buf, _name_german_4_an_der[i], last);
builder += _name_german_3_an_der[0];
builder += _name_german_4_an_der[i];
} else {
buf = strecpy(buf, _name_german_3_am[0], last);
buf = strecpy(buf, _name_german_4_am[i - lengthof(_name_german_4_an_der)], last);
builder += _name_german_3_am[0];
builder += _name_german_4_am[i - lengthof(_name_german_4_an_der)];
}
}
return buf;
}
/**
* Generates Latin-American town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeSpanishTownName(char *buf, const char *last, uint32 seed)
static void MakeSpanishTownName(StringBuilder &builder, uint32_t seed)
{
return strecpy(buf, _name_spanish_real[SeedChance(0, lengthof(_name_spanish_real), seed)], last);
builder += _name_spanish_real[SeedChance(0, lengthof(_name_spanish_real), seed)];
}
/**
* Generates French town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeFrenchTownName(char *buf, const char *last, uint32 seed)
static void MakeFrenchTownName(StringBuilder &builder, uint32_t seed)
{
return strecpy(buf, _name_french_real[SeedChance(0, lengthof(_name_french_real), seed)], last);
builder += _name_french_real[SeedChance(0, lengthof(_name_french_real), seed)];
}
/**
* Generates Silly town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeSillyTownName(char *buf, const char *last, uint32 seed)
static void MakeSillyTownName(StringBuilder &builder, uint32_t seed)
{
buf = strecpy(buf, _name_silly_1[SeedChance( 0, lengthof(_name_silly_1), seed)], last);
buf = strecpy(buf, _name_silly_2[SeedChance(16, lengthof(_name_silly_2), seed)], last);
return buf;
builder += _name_silly_1[SeedChance( 0, lengthof(_name_silly_1), seed)];
builder += _name_silly_2[SeedChance(16, lengthof(_name_silly_2), seed)];
}
/**
* Generates Swedish town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeSwedishTownName(char *buf, const char *last, uint32 seed)
static void MakeSwedishTownName(StringBuilder &builder, uint32_t seed)
{
/* optional first segment */
int i = SeedChanceBias(0, lengthof(_name_swedish_1), seed, 50);
if (i >= 0) buf = strecpy(buf, _name_swedish_1[i], last);
if (i >= 0) builder += _name_swedish_1[i];
/* mandatory middle segments including option of hardcoded name */
if (SeedChance(4, 5, seed) >= 3) {
buf = strecpy(buf, _name_swedish_2[SeedChance( 7, lengthof(_name_swedish_2), seed)], last);
builder += _name_swedish_2[SeedChance( 7, lengthof(_name_swedish_2), seed)];
} else {
buf = strecpy(buf, _name_swedish_2a[SeedChance( 7, lengthof(_name_swedish_2a), seed)], last);
buf = strecpy(buf, _name_swedish_2b[SeedChance(10, lengthof(_name_swedish_2b), seed)], last);
buf = strecpy(buf, _name_swedish_2c[SeedChance(13, lengthof(_name_swedish_2c), seed)], last);
builder += _name_swedish_2a[SeedChance( 7, lengthof(_name_swedish_2a), seed)];
builder += _name_swedish_2b[SeedChance(10, lengthof(_name_swedish_2b), seed)];
builder += _name_swedish_2c[SeedChance(13, lengthof(_name_swedish_2c), seed)];
}
buf = strecpy(buf, _name_swedish_3[SeedChance(16, lengthof(_name_swedish_3), seed)], last);
return buf;
builder += _name_swedish_3[SeedChance(16, lengthof(_name_swedish_3), seed)];
}
/**
* Generates Dutch town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeDutchTownName(char *buf, const char *last, uint32 seed)
static void MakeDutchTownName(StringBuilder &builder, uint32_t seed)
{
/* optional first segment */
int i = SeedChanceBias(0, lengthof(_name_dutch_1), seed, 50);
if (i >= 0) buf = strecpy(buf, _name_dutch_1[i], last);
if (i >= 0) builder += _name_dutch_1[i];
/* mandatory middle segments including option of hardcoded name */
if (SeedChance(6, 9, seed) > 4) {
buf = strecpy(buf, _name_dutch_2[SeedChance( 9, lengthof(_name_dutch_2), seed)], last);
builder += _name_dutch_2[SeedChance( 9, lengthof(_name_dutch_2), seed)];
} else {
buf = strecpy(buf, _name_dutch_3[SeedChance( 9, lengthof(_name_dutch_3), seed)], last);
buf = strecpy(buf, _name_dutch_4[SeedChance(12, lengthof(_name_dutch_4), seed)], last);
builder += _name_dutch_3[SeedChance( 9, lengthof(_name_dutch_3), seed)];
builder += _name_dutch_4[SeedChance(12, lengthof(_name_dutch_4), seed)];
}
buf = strecpy(buf, _name_dutch_5[SeedChance(15, lengthof(_name_dutch_5), seed)], last);
return buf;
builder += _name_dutch_5[SeedChance(15, lengthof(_name_dutch_5), seed)];
}
/**
* Generates Finnish town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeFinnishTownName(char *buf, const char *last, uint32 seed)
static void MakeFinnishTownName(StringBuilder &builder, uint32_t seed)
{
char *orig = buf;
size_t start = builder.CurrentIndex();
/* Select randomly if town name should consists of one or two parts. */
if (SeedChance(0, 15, seed) >= 10) {
return strecpy(buf, _name_finnish_real[SeedChance(2, lengthof(_name_finnish_real), seed)], last);
builder += _name_finnish_real[SeedChance(2, lengthof(_name_finnish_real), seed)];
return;
}
if (SeedChance(0, 15, seed) >= 5) {
@ -524,41 +503,38 @@ static char *MakeFinnishTownName(char *buf, const char *last, uint32 seed)
* The reason for not having the contents of _name_finnish_{1,2} in the same table is
* that the ones in _name_finnish_2 are not good for this purpose. */
uint sel = SeedChance( 0, lengthof(_name_finnish_1), seed);
buf = strecpy(buf, _name_finnish_1[sel], last);
char *end = buf - 1;
assert(end >= orig);
if (*end == 'i') *end = 'e';
if (strstr(orig, "a") != nullptr || strstr(orig, "o") != nullptr || strstr(orig, "u") != nullptr ||
strstr(orig, "A") != nullptr || strstr(orig, "O") != nullptr || strstr(orig, "U") != nullptr) {
buf = strecpy(buf, "la", last);
builder += _name_finnish_1[sel];
size_t last = builder.CurrentIndex() - 1;
if (builder[last] == 'i') builder[last] = 'e';
std::string_view view(&builder[start], builder.CurrentIndex() - start);
if (view.find_first_of("aouAOU") != std::string_view::npos) {
builder += "la";
} else {
buf = strecpy(buf, u8"l\u00e4", last);
builder += u8"l\u00e4";
}
return buf;
return;
}
/* A two-part name by combining one of _name_finnish_{1,2} + _name_finnish_3.
* Why aren't _name_finnish_{1,2} just one table? See above. */
uint sel = SeedChance(2, lengthof(_name_finnish_1) + lengthof(_name_finnish_2), seed);
if (sel >= lengthof(_name_finnish_1)) {
buf = strecpy(buf, _name_finnish_2[sel - lengthof(_name_finnish_1)], last);
builder += _name_finnish_2[sel - lengthof(_name_finnish_1)];
} else {
buf = strecpy(buf, _name_finnish_1[sel], last);
builder += _name_finnish_1[sel];
}
buf = strecpy(buf, _name_finnish_3[SeedChance(10, lengthof(_name_finnish_3), seed)], last);
return buf;
builder += _name_finnish_3[SeedChance(10, lengthof(_name_finnish_3), seed)];
}
/**
* Generates Polish town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakePolishTownName(char *buf, const char *last, uint32 seed)
static void MakePolishTownName(StringBuilder &builder, uint32_t seed)
{
/* optional first segment */
uint i = SeedChance(0,
@ -569,66 +545,65 @@ static char *MakePolishTownName(char *buf, const char *last, uint32 seed)
if (i < lengthof(_name_polish_2_o)) {
return strecpy(buf, _name_polish_2_o[SeedChance(3, lengthof(_name_polish_2_o), seed)], last);
builder += _name_polish_2_o[SeedChance(3, lengthof(_name_polish_2_o), seed)];
return;
}
if (i < lengthof(_name_polish_2_m) + lengthof(_name_polish_2_o)) {
if (j < 4) {
buf = strecpy(buf, _name_polish_1_m[SeedChance(5, lengthof(_name_polish_1_m), seed)], last);
builder += _name_polish_1_m[SeedChance(5, lengthof(_name_polish_1_m), seed)];
}
buf = strecpy(buf, _name_polish_2_m[SeedChance(7, lengthof(_name_polish_2_m), seed)], last);
builder += _name_polish_2_m[SeedChance(7, lengthof(_name_polish_2_m), seed)];
if (j >= 4 && j < 16) {
buf = strecpy(buf, _name_polish_3_m[SeedChance(10, lengthof(_name_polish_3_m), seed)], last);
builder += _name_polish_3_m[SeedChance(10, lengthof(_name_polish_3_m), seed)];
}
return buf;
return;
}
if (i < lengthof(_name_polish_2_f) + lengthof(_name_polish_2_m) + lengthof(_name_polish_2_o)) {
if (j < 4) {
buf = strecpy(buf, _name_polish_1_f[SeedChance(5, lengthof(_name_polish_1_f), seed)], last);
builder += _name_polish_1_f[SeedChance(5, lengthof(_name_polish_1_f), seed)];
}
buf = strecpy(buf, _name_polish_2_f[SeedChance(7, lengthof(_name_polish_2_f), seed)], last);
builder += _name_polish_2_f[SeedChance(7, lengthof(_name_polish_2_f), seed)];
if (j >= 4 && j < 16) {
buf = strecpy(buf, _name_polish_3_f[SeedChance(10, lengthof(_name_polish_3_f), seed)], last);
builder += _name_polish_3_f[SeedChance(10, lengthof(_name_polish_3_f), seed)];
}
return buf;
return;
}
if (j < 4) {
buf = strecpy(buf, _name_polish_1_n[SeedChance(5, lengthof(_name_polish_1_n), seed)], last);
builder += _name_polish_1_n[SeedChance(5, lengthof(_name_polish_1_n), seed)];
}
buf = strecpy(buf, _name_polish_2_n[SeedChance(7, lengthof(_name_polish_2_n), seed)], last);
builder += _name_polish_2_n[SeedChance(7, lengthof(_name_polish_2_n), seed)];
if (j >= 4 && j < 16) {
buf = strecpy(buf, _name_polish_3_n[SeedChance(10, lengthof(_name_polish_3_n), seed)], last);
builder += _name_polish_3_n[SeedChance(10, lengthof(_name_polish_3_n), seed)];
}
return buf;
return;
}
/**
* Generates Czech town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeCzechTownName(char *buf, const char *last, uint32 seed)
static void MakeCzechTownName(StringBuilder &builder, uint32_t seed)
{
/* 1:3 chance to use a real name. */
if (SeedModChance(0, 4, seed) == 0) {
return strecpy(buf, _name_czech_real[SeedModChance(4, lengthof(_name_czech_real), seed)], last);
builder += _name_czech_real[SeedModChance(4, lengthof(_name_czech_real), seed)];
return;
}
[[maybe_unused]] const char *orig = buf;
/* Probability of prefixes/suffixes
* 0..11 prefix, 12..13 prefix+suffix, 14..17 suffix, 18..31 nothing */
int prob_tails = SeedModChance(2, 32, seed);
@ -735,28 +710,25 @@ static char *MakeCzechTownName(char *buf, const char *last, uint32 seed)
if (do_prefix) {
CzechPattern pattern = _name_czech_adj[prefix].pattern;
buf = strecpy(buf, _name_czech_adj[prefix].name, last);
builder += _name_czech_adj[prefix].name;
char *endpos = buf - 1;
size_t endpos = builder.CurrentIndex() - 1;
/* Find the first character in a UTF-8 sequence */
while (GB(*endpos, 6, 2) == 2) endpos--;
while (GB(builder[endpos], 6, 2) == 2) endpos--;
builder.RemoveElementsFromBack(builder.CurrentIndex() - endpos);
if (gender == CZG_SMASC && pattern == CZP_PRIVL) {
assert(endpos >= orig + 2);
/* -ovX -> -uv */
*(endpos - 2) = 'u';
assert(*(endpos - 1) == 'v');
*endpos = '\0';
builder[endpos - 2] = 'u';
} else {
assert(endpos >= orig);
endpos = strecpy(endpos, _name_czech_patmod[gender][pattern], last);
builder += _name_czech_patmod[gender][pattern];
}
buf = strecpy(endpos, " ", last);
builder += ' ';
}
if (dynamic_subst) {
buf = strecpy(buf, _name_czech_subst_stem[stem].name, last);
builder += _name_czech_subst_stem[stem].name;
if (postfix < lengthof(_name_czech_subst_postfix)) {
const char *poststr = _name_czech_subst_postfix[postfix];
const char *endstr = _name_czech_subst_ending[ending].name;
@ -769,189 +741,175 @@ static char *MakeCzechTownName(char *buf, const char *last, uint32 seed)
if (postlen < 2 || postlen > endlen ||
((poststr[1] != 'v' || poststr[1] != endstr[1]) &&
poststr[2] != endstr[1])) {
buf = strecpy(buf, poststr, last);
builder += poststr;
/* k-i -> c-i, h-i -> z-i */
if (endstr[0] == 'i') {
switch (*(buf - 1)) {
case 'k': *(buf - 1) = 'c'; break;
case 'h': *(buf - 1) = 'z'; break;
size_t last = builder.CurrentIndex() - 1;
switch (builder[last]) {
case 'k': builder[last] = 'c'; break;
case 'h': builder[last] = 'z'; break;
default: break;
}
}
}
}
buf = strecpy(buf, _name_czech_subst_ending[ending].name, last);
builder += _name_czech_subst_ending[ending].name;
} else {
buf = strecpy(buf, _name_czech_subst_full[stem].name, last);
builder += _name_czech_subst_full[stem].name;
}
if (do_suffix) {
buf = strecpy(buf, " ", last);
buf = strecpy(buf, _name_czech_suffix[suffix], last);
builder += " ";
builder += _name_czech_suffix[suffix];
}
return buf;
}
/**
* Generates Romanian town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeRomanianTownName(char *buf, const char *last, uint32 seed)
static void MakeRomanianTownName(StringBuilder &builder, uint32_t seed)
{
return strecpy(buf, _name_romanian_real[SeedChance(0, lengthof(_name_romanian_real), seed)], last);
builder += _name_romanian_real[SeedChance(0, lengthof(_name_romanian_real), seed)];
}
/**
* Generates Slovak town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeSlovakTownName(char *buf, const char *last, uint32 seed)
static void MakeSlovakTownName(StringBuilder &builder, uint32_t seed)
{
return strecpy(buf, _name_slovak_real[SeedChance(0, lengthof(_name_slovak_real), seed)], last);
builder += _name_slovak_real[SeedChance(0, lengthof(_name_slovak_real), seed)];
}
/**
* Generates Norwegian town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeNorwegianTownName(char *buf, const char *last, uint32 seed)
static void MakeNorwegianTownName(StringBuilder &builder, uint32_t 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 (SeedChance(0, 15, seed) < 3) {
/* Use 7bit for the realname table index. Bit 4-10 */
return strecpy(buf, _name_norwegian_real[SeedChance(4, lengthof(_name_norwegian_real), seed)], last);
builder += _name_norwegian_real[SeedChance(4, lengthof(_name_norwegian_real), seed)];
return;
}
/* Use 7bit for the first fake part. Bit 4-10 */
buf = strecpy(buf, _name_norwegian_1[SeedChance(4, lengthof(_name_norwegian_1), seed)], last);
builder += _name_norwegian_1[SeedChance(4, lengthof(_name_norwegian_1), seed)];
/* Use 7bit for the last fake part. Bit 11-17 */
buf = strecpy(buf, _name_norwegian_2[SeedChance(11, lengthof(_name_norwegian_2), seed)], last);
return buf;
builder += _name_norwegian_2[SeedChance(11, lengthof(_name_norwegian_2), seed)];
}
/**
* Generates Hungarian town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeHungarianTownName(char *buf, const char *last, uint32 seed)
static void MakeHungarianTownName(StringBuilder &builder, uint32_t seed)
{
if (SeedChance(12, 15, seed) < 3) {
return strecpy(buf, _name_hungarian_real[SeedChance(0, lengthof(_name_hungarian_real), seed)], last);
builder += _name_hungarian_real[SeedChance(0, lengthof(_name_hungarian_real), seed)];
return;
}
/* optional first segment */
uint i = SeedChance(3, lengthof(_name_hungarian_1) * 3, seed);
if (i < lengthof(_name_hungarian_1)) buf = strecpy(buf, _name_hungarian_1[i], last);
if (i < lengthof(_name_hungarian_1)) builder += _name_hungarian_1[i];
/* mandatory middle segments */
buf = strecpy(buf, _name_hungarian_2[SeedChance(3, lengthof(_name_hungarian_2), seed)], last);
buf = strecpy(buf, _name_hungarian_3[SeedChance(6, lengthof(_name_hungarian_3), seed)], last);
builder += _name_hungarian_2[SeedChance(3, lengthof(_name_hungarian_2), seed)];
builder += _name_hungarian_3[SeedChance(6, lengthof(_name_hungarian_3), seed)];
/* optional last segment */
i = SeedChance(10, lengthof(_name_hungarian_4) * 3, seed);
if (i < lengthof(_name_hungarian_4)) {
buf = strecpy(buf, _name_hungarian_4[i], last);
builder += _name_hungarian_4[i];
}
return buf;
}
/**
* Generates Swiss town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeSwissTownName(char *buf, const char *last, uint32 seed)
static void MakeSwissTownName(StringBuilder &builder, uint32_t seed)
{
return strecpy(buf, _name_swiss_real[SeedChance(0, lengthof(_name_swiss_real), seed)], last);
builder += _name_swiss_real[SeedChance(0, lengthof(_name_swiss_real), seed)];
}
/**
* Generates Danish town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeDanishTownName(char *buf, const char *last, uint32 seed)
static void MakeDanishTownName(StringBuilder &builder, uint32_t seed)
{
/* optional first segment */
int i = SeedChanceBias(0, lengthof(_name_danish_1), seed, 50);
if (i >= 0) buf = strecpy(buf, _name_danish_1[i], last);
if (i >= 0) builder += _name_danish_1[i];
/* middle segments removed as this algorithm seems to create much more realistic names */
buf = strecpy(buf, _name_danish_2[SeedChance( 7, lengthof(_name_danish_2), seed)], last);
buf = strecpy(buf, _name_danish_3[SeedChance(16, lengthof(_name_danish_3), seed)], last);
return buf;
builder += _name_danish_2[SeedChance( 7, lengthof(_name_danish_2), seed)];
builder += _name_danish_3[SeedChance(16, lengthof(_name_danish_3), seed)];
}
/**
* Generates Turkish town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeTurkishTownName(char *buf, const char *last, uint32 seed)
static void MakeTurkishTownName(StringBuilder &builder, uint32_t seed)
{
uint i = SeedModChance(0, 5, seed);
switch (i) {
case 0:
buf = strecpy(buf, _name_turkish_prefix[SeedModChance( 2, lengthof(_name_turkish_prefix), seed)], last);
builder += _name_turkish_prefix[SeedModChance( 2, lengthof(_name_turkish_prefix), seed)];
/* middle segment */
buf = strecpy(buf, _name_turkish_middle[SeedModChance( 4, lengthof(_name_turkish_middle), seed)], last);
builder += _name_turkish_middle[SeedModChance( 4, lengthof(_name_turkish_middle), seed)];
/* optional suffix */
if (SeedModChance(0, 7, seed) == 0) {
buf = strecpy(buf, _name_turkish_suffix[SeedModChance( 10, lengthof(_name_turkish_suffix), seed)], last);
builder += _name_turkish_suffix[SeedModChance( 10, lengthof(_name_turkish_suffix), seed)];
}
break;
case 1: case 2:
buf = strecpy(buf, _name_turkish_prefix[SeedModChance( 2, lengthof(_name_turkish_prefix), seed)], last);
buf = strecpy(buf, _name_turkish_suffix[SeedModChance( 4, lengthof(_name_turkish_suffix), seed)], last);
builder += _name_turkish_prefix[SeedModChance( 2, lengthof(_name_turkish_prefix), seed)];
builder += _name_turkish_suffix[SeedModChance( 4, lengthof(_name_turkish_suffix), seed)];
break;
default:
buf = strecpy(buf, _name_turkish_real[SeedModChance( 4, lengthof(_name_turkish_real), seed)], last);
builder += _name_turkish_real[SeedModChance( 4, lengthof(_name_turkish_real), seed)];
break;
}
return buf;
}
/**
* Generates Italian town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeItalianTownName(char *buf, const char *last, uint32 seed)
static void MakeItalianTownName(StringBuilder &builder, uint32_t seed)
{
if (SeedModChance(0, 6, seed) == 0) { // real city names
return strecpy(buf, _name_italian_real[SeedModChance(4, lengthof(_name_italian_real), seed)], last);
builder += _name_italian_real[SeedModChance(4, lengthof(_name_italian_real), seed)];
return;
}
static const char * const mascul_femin_italian[] = {
@ -960,70 +918,66 @@ static char *MakeItalianTownName(char *buf, const char *last, uint32 seed)
};
if (SeedModChance(0, 8, seed) == 0) { // prefix
buf = strecpy(buf, _name_italian_pref[SeedModChance(11, lengthof(_name_italian_pref), seed)], last);
builder += _name_italian_pref[SeedModChance(11, lengthof(_name_italian_pref), seed)];
}
uint i = SeedChance(0, 2, seed);
if (i == 0) { // masculine form
buf = strecpy(buf, _name_italian_1m[SeedModChance(4, lengthof(_name_italian_1m), seed)], last);
builder += _name_italian_1m[SeedModChance(4, lengthof(_name_italian_1m), seed)];
} else { // feminine form
buf = strecpy(buf, _name_italian_1f[SeedModChance(4, lengthof(_name_italian_1f), seed)], last);
builder += _name_italian_1f[SeedModChance(4, lengthof(_name_italian_1f), seed)];
}
if (SeedModChance(3, 3, seed) == 0) {
buf = strecpy(buf, _name_italian_2[SeedModChance(11, lengthof(_name_italian_2), seed)], last);
buf = strecpy(buf, mascul_femin_italian[i], last);
builder += _name_italian_2[SeedModChance(11, lengthof(_name_italian_2), seed)];
builder += mascul_femin_italian[i];
} else {
buf = strecpy(buf, _name_italian_2i[SeedModChance(16, lengthof(_name_italian_2i), seed)], last);
builder += _name_italian_2i[SeedModChance(16, lengthof(_name_italian_2i), seed)];
}
if (SeedModChance(15, 4, seed) == 0) {
if (SeedModChance(5, 2, seed) == 0) { // generic suffix
buf = strecpy(buf, _name_italian_3[SeedModChance(4, lengthof(_name_italian_3), seed)], last);
builder += _name_italian_3[SeedModChance(4, lengthof(_name_italian_3), seed)];
} else { // river name suffix
buf = strecpy(buf, _name_italian_river1[SeedModChance(4, lengthof(_name_italian_river1), seed)], last);
buf = strecpy(buf, _name_italian_river2[SeedModChance(16, lengthof(_name_italian_river2), seed)], last);
builder += _name_italian_river1[SeedModChance(4, lengthof(_name_italian_river1), seed)];
builder += _name_italian_river2[SeedModChance(16, lengthof(_name_italian_river2), seed)];
}
}
return buf;
}
/**
* Generates Catalan town name from given seed.
* @param buf output buffer
* @param builder string builder
* @param seed town name seed
* @param last end of buffer
*/
static char *MakeCatalanTownName(char *buf, const char *last, uint32 seed)
static void MakeCatalanTownName(StringBuilder &builder, uint32_t seed)
{
if (SeedModChance(0, 3, seed) == 0) { // real city names
return strecpy(buf, _name_catalan_real[SeedModChance(4, lengthof(_name_catalan_real), seed)], last);
builder += _name_catalan_real[SeedModChance(4, lengthof(_name_catalan_real), seed)];
return;
}
if (SeedModChance(0, 2, seed) == 0) { // prefix
buf = strecpy(buf, _name_catalan_pref[SeedModChance(11, lengthof(_name_catalan_pref), seed)], last);
builder += _name_catalan_pref[SeedModChance(11, lengthof(_name_catalan_pref), seed)];
}
uint i = SeedChance(0, 2, seed);
if (i == 0) { // masculine form
buf = strecpy(buf, _name_catalan_1m[SeedModChance(4, lengthof(_name_catalan_1m), seed)], last);
buf = strecpy(buf, _name_catalan_2m[SeedModChance(11, lengthof(_name_catalan_2m), seed)], last);
builder += _name_catalan_1m[SeedModChance(4, lengthof(_name_catalan_1m), seed)];
builder += _name_catalan_2m[SeedModChance(11, lengthof(_name_catalan_2m), seed)];
} else { // feminine form
buf = strecpy(buf, _name_catalan_1f[SeedModChance(4, lengthof(_name_catalan_1f), seed)], last);
buf = strecpy(buf, _name_catalan_2f[SeedModChance(11, lengthof(_name_catalan_2f), seed)], last);
builder += _name_catalan_1f[SeedModChance(4, lengthof(_name_catalan_1f), seed)];
builder += _name_catalan_2f[SeedModChance(11, lengthof(_name_catalan_2f), seed)];
}
if (SeedModChance(15, 5, seed) == 0) {
if (SeedModChance(5, 2, seed) == 0) { // generic suffix
buf = strecpy(buf, _name_catalan_3[SeedModChance(4, lengthof(_name_catalan_3), seed)], last);
builder += _name_catalan_3[SeedModChance(4, lengthof(_name_catalan_3), seed)];
} else { // river name suffix
buf = strecpy(buf, _name_catalan_river1[SeedModChance(4, lengthof(_name_catalan_river1), seed)], last);
builder += _name_catalan_river1[SeedModChance(4, lengthof(_name_catalan_river1), seed)];
}
}
return buf;
}
@ -1034,7 +988,7 @@ static char *MakeCatalanTownName(char *buf, const char *last, uint32 seed)
* @param seed The seed of the town name.
* @return The end of the filled buffer.
*/
typedef char *TownNameGenerator(char *buf, const char *last, uint32 seed);
typedef void TownNameGenerator(StringBuilder &builder, uint32 seed);
/** Contains pointer to generator and minimum buffer size (not incl. terminating '\0') */
struct TownNameGeneratorParams {
@ -1070,13 +1024,11 @@ static const TownNameGeneratorParams _town_name_generators[] = {
/**
* Generates town name from given seed.
* @param buf output buffer
* @param last end of buffer
* @param lang town name language
* @param seed generation seed
* @return last character ('/0')
* @param builder string builder to write to
* @param lang town name language
* @param seed generation seed
*/
char *GenerateTownNameString(char *buf, const char *last, size_t lang, uint32 seed)
void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32 seed)
{
assert(lang < lengthof(_town_name_generators));
@ -1087,10 +1039,12 @@ char *GenerateTownNameString(char *buf, const char *last, size_t lang, uint32 se
* These would break. Using another temporary buffer results in ~40% slower code,
* so use it only when really needed. */
const TownNameGeneratorParams *par = &_town_name_generators[lang];
if (last >= buf + par->min) return par->proc(buf, last, seed);
if (builder.Remaining() >= par->min) return par->proc(builder, seed);
std::string buffer(par->min + 1, '\0');
par->proc(buffer.data(), buffer.data() + par->min, seed);
char *state = buffer.data();
StringBuilder buffer_builder(&state, buffer.data() + par->min);
par->proc(buffer_builder, seed);
return strecpy(buf, buffer.c_str(), last);
builder += buffer;
}

View File

@ -13,7 +13,6 @@
#include "core/random_func.hpp"
#include "townname_type.h"
char *GenerateTownNameString(char *buf, const char *last, size_t lang, uint32 seed);
char *GetTownName(char *buff, const TownNameParams *par, uint32 townnameparts, const char *last);
char *GetTownName(char *buff, const Town *t, const char *last);
bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names = nullptr);