OpenTTD Source: src/newgrf_townname.cpp Source File

OpenTTD Source 12.2

newgrf_townname.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
15 #include "stdafx.h"
16 #include "newgrf_townname.h"
17 #include "core/alloc_func.hpp"
18 #include "string_func.h"
19
20 #include "table/strings.h"
21
22 #include "safeguards.h"
23
24 static GRFTownName *_grf_townnames = nullptr;
25 static std::vector<StringID> _grf_townname_names;
26
27 GRFTownName *GetGRFTownName(uint32 grfid)
28 {
29 GRFTownName *t = _grf_townnames;
30 for (; t != nullptr; t = t->next) {
31 if (t->grfid == grfid) return t;
32 }
33 return nullptr;
34 }
35
36 GRFTownName *AddGRFTownName(uint32 grfid)
37 {
38 GRFTownName *t = GetGRFTownName(grfid);
39 if (t == nullptr) {
40 t = CallocT<GRFTownName>(1);
41 t->grfid = grfid;
42 t->next = _grf_townnames;
43 _grf_townnames = t;
44 }
45 return t;
46 }
47
48 void DelGRFTownName(uint32 grfid)
49 {
50 GRFTownName *t = _grf_townnames;
51 GRFTownName *p = nullptr;
52 for (;t != nullptr; p = t, t = t->next) if (t->grfid == grfid) break;
53 if (t != nullptr) {
54 for (int i = 0; i < 128; i++) {
55 for (int j = 0; j < t->nbparts[i]; j++) {
56 for (int k = 0; k < t->partlist[i][j].partcount; k++) {
57 if (!HasBit(t->partlist[i][j].parts[k].prob, 7)) free(t->partlist[i][j].parts[k].data.text);
58 }
59 free(t->partlist[i][j].parts);
60 }
61 free(t->partlist[i]);
62 }
63 if (p != nullptr) {
64 p->next = t->next;
65 } else {
66 _grf_townnames = t->next;
67 }
68 free(t);
69 }
70 }
71
72 static char *RandomPart(char *buf, GRFTownName *t, uint32 seed, byte id, const char *last)
73 {
74 assert(t != nullptr);
75 for (int i = 0; i < t->nbparts[id]; i++) {
76 byte count = t->partlist[id][i].bitcount;
77 uint16 maxprob = t->partlist[id][i].maxprob;
78 uint32 r = (GB(seed, t->partlist[id][i].bitstart, count) * maxprob) >> count;
79 for (int j = 0; j < t->partlist[id][i].partcount; j++) {
80 byte prob = t->partlist[id][i].parts[j].prob;
81 maxprob -= GB(prob, 0, 7);
82 if (maxprob > r) continue;
83 if (HasBit(prob, 7)) {
84 buf = RandomPart(buf, t, seed, t->partlist[id][i].parts[j].data.id, last);
85 } else {
86 buf = strecat(buf, t->partlist[id][i].parts[j].data.text, last);
87 }
88 break;
89 }
90 }
91 return buf;
92 }
93
94 char *GRFTownNameGenerate(char *buf, uint32 grfid, uint16 gen, uint32 seed, const char *last)
95 {
96 strecpy(buf, "", last);
97 for (GRFTownName *t = _grf_townnames; t != nullptr; t = t->next) {
98 if (t->grfid == grfid) {
99 assert(gen < t->nb_gen);
100 buf = RandomPart(buf, t, seed, t->id[gen], last);
101 break;
102 }
103 }
104 return buf;
105 }
106
107
109 void InitGRFTownGeneratorNames()
110 {
111 _grf_townname_names.clear();
112 for (GRFTownName *t = _grf_townnames; t != nullptr; t = t->next) {
113 for (int j = 0; j < t->nb_gen; j++) _grf_townname_names.push_back(t->name[j]);
114 }
115 }
116
117 const std::vector<StringID>& GetGRFTownNameList()
118 {
119 return _grf_townname_names;
120 }
121
122 StringID GetGRFTownNameName(uint gen)
123 {
124 return gen < _grf_townname_names.size() ? _grf_townname_names[gen] : STR_UNDEFINED;
125 }
126
127 void CleanUpGRFTownNames()
128 {
129 while (_grf_townnames != nullptr) DelGRFTownName(_grf_townnames->grfid);
130 }
131
132 uint32 GetGRFTownNameId(int gen)
133 {
134 for (GRFTownName *t = _grf_townnames; t != nullptr; t = t->next) {
135 if (gen < t->nb_gen) return t->grfid;
136 gen -= t->nb_gen;
137 }
138 /* Fallback to no NewGRF */
139 return 0;
140 }
141
142 uint16 GetGRFTownNameType(int gen)
143 {
144 for (GRFTownName *t = _grf_townnames; t != nullptr; t = t->next) {
145 if (gen < t->nb_gen) return gen;
146 gen -= t->nb_gen;
147 }
148 /* Fallback to english original */
149 return SPECSTR_TOWNNAME_ENGLISH;
150 }
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
NamePart::prob
byte prob
The relative probability of the following name to appear in the bottom 7 bits.
Definition: newgrf_townname.h:20
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
newgrf_townname.h
safeguards.h
NamePart::text
char * text
If probability bit 7 is clear.
Definition: newgrf_townname.h:22
stdafx.h
GRFTownName
Definition: newgrf_townname.h:35
string_func.h
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
alloc_func.hpp
InitGRFTownGeneratorNames
void InitGRFTownGeneratorNames()
Allocate memory for the NewGRF town names.
Definition: newgrf_townname.cpp:109
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
strecat
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:84
NamePart::id
byte id
If probability bit 7 is set.
Definition: newgrf_townname.h:23