19 #include "strings_internal.h"
21 #include "table/strings.h"
25 static std::vector<GRFTownName> _grf_townnames;
26 static std::vector<StringID> _grf_townname_names;
30 auto found = std::find_if(std::begin(_grf_townnames), std::end(_grf_townnames), [&grfid](
const GRFTownName &t) {
return t.
grfid == grfid; });
31 if (found != std::end(_grf_townnames))
return &*found;
39 t = &_grf_townnames.emplace_back();
45 void DelGRFTownName(uint32_t grfid)
47 _grf_townnames.erase(std::find_if(std::begin(_grf_townnames), std::end(_grf_townnames), [&grfid](
const GRFTownName &t) {
return t.
grfid == grfid; }));
53 for (
const auto &partlist : t->
partlists[
id]) {
54 uint8_t count = partlist.bitcount;
55 uint16_t maxprob = partlist.maxprob;
56 uint32_t r = (
GB(seed, partlist.bitstart, count) * maxprob) >> count;
57 for (
const auto &part : partlist.parts) {
58 maxprob -=
GB(part.prob, 0, 7);
59 if (maxprob > r)
continue;
60 if (
HasBit(part.prob, 7)) {
61 RandomPart(builder, t, seed, part.id);
70 void GRFTownNameGenerate(
StringBuilder &builder, uint32_t grfid, uint16_t gen, uint32_t seed)
74 assert(gen < t->styles.size());
75 RandomPart(builder, t, seed, t->
styles[gen].id);
83 _grf_townname_names.clear();
84 for (
const auto &t : _grf_townnames) {
85 for (
const auto &style : t.
styles) {
86 _grf_townname_names.push_back(style.name);
91 const std::vector<StringID> &GetGRFTownNameList()
93 return _grf_townname_names;
96 StringID GetGRFTownNameName(uint16_t gen)
98 return gen < _grf_townname_names.size() ? _grf_townname_names[gen] : STR_UNDEFINED;
101 void CleanUpGRFTownNames()
103 _grf_townnames.clear();
106 uint32_t GetGRFTownNameId(uint16_t gen)
108 for (
const auto &t : _grf_townnames) {
110 gen -=
static_cast<uint16_t
>(t.
styles.size());
116 uint16_t GetGRFTownNameType(uint16_t gen)
118 for (
const auto &t : _grf_townnames) {
119 if (gen < t.
styles.size())
return gen;
120 gen -=
static_cast<uint16_t
>(t.
styles.size());
123 return SPECSTR_TOWNNAME_ENGLISH;
Functions related to the allocation of memory.
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Equivalent to the std::back_insert_iterator in function, with some convenience helpers for string con...
void InitGRFTownGeneratorNames()
Allocate memory for the NewGRF town names.
Header of Action 0F "universal holder" structure and functions.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
std::vector< NamePartList > partlists[MAX_LISTS]
Lists of town name parts.
std::vector< TownNameStyle > styles
Style names defined by the Town Name NewGRF.
uint32_t grfid
GRF ID of NewGRF.