27 for (; t !=
nullptr; t = t->next) {
28 if (t->grfid == grfid)
return t;
37 t = CallocT<GRFTownName>(1);
39 t->next = _grf_townnames;
45 void DelGRFTownName(uint32 grfid)
49 for (;t !=
nullptr; p = t, t = t->next)
if (t->grfid == grfid)
break;
51 for (
int i = 0; i < 128; i++) {
52 for (
int j = 0; j < t->nbparts[i]; j++) {
53 for (
int k = 0; k < t->partlist[i][j].partcount; k++) {
54 if (!
HasBit(t->partlist[i][j].parts[k].prob, 7))
free(t->partlist[i][j].parts[k].data.text);
56 free(t->partlist[i][j].parts);
63 _grf_townnames = t->next;
69 static char *RandomPart(
char *buf,
GRFTownName *t, uint32 seed, byte
id,
const char *last)
72 for (
int i = 0; i < t->nbparts[id]; i++) {
73 byte count = t->partlist[id][i].bitcount;
74 uint16 maxprob = t->partlist[id][i].maxprob;
75 uint32 r = (
GB(seed, t->partlist[
id][i].bitstart, count) * maxprob) >> count;
76 for (
int j = 0; j < t->partlist[id][i].partcount; j++) {
77 byte prob = t->partlist[id][i].parts[j].
prob;
78 maxprob -=
GB(prob, 0, 7);
79 if (maxprob > r)
continue;
81 buf = RandomPart(buf, t, seed, t->partlist[
id][i].parts[j].data.
id, last);
83 buf =
strecat(buf, t->partlist[
id][i].parts[j].data.
text, last);
91 char *GRFTownNameGenerate(
char *buf, uint32 grfid, uint16 gen, uint32 seed,
const char *last)
94 for (
GRFTownName *t = _grf_townnames; t !=
nullptr; t = t->next) {
95 if (t->grfid == grfid) {
96 assert(gen < t->nb_gen);
97 buf = RandomPart(buf, t, seed, t->id[gen], last);
106 int nb_names = 0, n = 0;
107 for (
GRFTownName *t = _grf_townnames; t !=
nullptr; t = t->next) nb_names += t->nb_gen;
108 StringID *list = MallocT<StringID>(nb_names + 1);
109 for (
GRFTownName *t = _grf_townnames; t !=
nullptr; t = t->next) {
110 for (
int j = 0; j < t->nb_gen; j++) list[n++] = t->name[j];
116 void CleanUpGRFTownNames()
118 while (_grf_townnames !=
nullptr) DelGRFTownName(_grf_townnames->grfid);
121 uint32 GetGRFTownNameId(
int gen)
123 for (
GRFTownName *t = _grf_townnames; t !=
nullptr; t = t->next) {
124 if (gen < t->nb_gen)
return t->grfid;
131 uint16 GetGRFTownNameType(
int gen)
133 for (
GRFTownName *t = _grf_townnames; t !=
nullptr; t = t->next) {
134 if (gen < t->nb_gen)
return gen;
138 return SPECSTR_TOWNNAME_ENGLISH;
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
char * text
If probability bit 7 is clear.
Header of Action 0F "universal holder" structure and functions.
Functions related to low-level strings.
Functions related to the allocation of memory.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
byte prob
The relative probability of the following name to appear in the bottom 7 bits.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
byte id
If probability bit 7 is set.