OpenTTD Source 20261123-master-g1c55c72fd0
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
14
15#include "stdafx.h"
16#include "newgrf_townname.h"
17#include "strings_internal.h"
18
19#include "table/strings.h"
20
21#include "safeguards.h"
22
23static std::vector<GRFTownName> _grf_townnames;
24static std::vector<StringID> _grf_townname_names;
25
32{
33 auto found = std::ranges::find(_grf_townnames, grfid, &GRFTownName::grfid);
34 if (found != std::end(_grf_townnames)) return &*found;
35 return nullptr;
36}
37
44{
45 GRFTownName *t = GetGRFTownName(grfid);
46 if (t == nullptr) {
47 t = &_grf_townnames.emplace_back();
48 t->grfid = grfid;
49 }
50 return t;
51}
52
58{
59 _grf_townnames.erase(std::ranges::find(_grf_townnames, grfid, &GRFTownName::grfid));
60}
61
62static void RandomPart(StringBuilder &builder, const GRFTownName *t, uint32_t seed, uint8_t id)
63{
64 assert(t != nullptr);
65 for (const auto &partlist : t->partlists[id]) {
66 uint8_t count = partlist.bitcount;
67 uint16_t maxprob = partlist.maxprob;
68 uint32_t r = (GB(seed, partlist.bitstart, count) * maxprob) >> count;
69 for (const auto &part : partlist.parts) {
70 maxprob -= GB(part.prob, 0, 7);
71 if (maxprob > r) continue;
72 if (HasBit(part.prob, 7)) {
73 RandomPart(builder, t, seed, part.id);
74 } else {
75 builder += part.text;
76 }
77 break;
78 }
79 }
80}
81
89void GRFTownNameGenerate(StringBuilder &builder, GrfID grfid, uint16_t gen, uint32_t seed)
90{
91 const GRFTownName *t = GetGRFTownName(grfid);
92 if (t != nullptr) {
93 assert(gen < t->styles.size());
94 RandomPart(builder, t, seed, t->styles[gen].id);
95 }
96}
97
98
101{
102 _grf_townname_names.clear();
103 for (const auto &t : _grf_townnames) {
104 for (const auto &style : t.styles) {
105 _grf_townname_names.push_back(style.name);
106 }
107 }
108}
109
110const std::vector<StringID> &GetGRFTownNameList()
111{
112 return _grf_townname_names;
113}
114
115StringID GetGRFTownNameName(uint16_t gen)
116{
117 return gen < _grf_townname_names.size() ? _grf_townname_names[gen] : STR_UNDEFINED;
118}
119
120void CleanUpGRFTownNames()
121{
122 _grf_townnames.clear();
123}
124
125GrfID GetGRFTownNameId(uint16_t gen)
126{
127 for (const auto &t : _grf_townnames) {
128 if (gen < t.styles.size()) return t.grfid;
129 gen -= static_cast<uint16_t>(t.styles.size());
130 }
131 /* Fallback to no NewGRF */
132 return {};
133}
134
135uint16_t GetGRFTownNameType(uint16_t gen)
136{
137 for (const auto &t : _grf_townnames) {
138 if (gen < t.styles.size()) return gen;
139 gen -= static_cast<uint16_t>(t.styles.size());
140 }
141 /* Fallback to the first built in town name (English). */
143}
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Compose data into a growing std::string.
GRFTownName * GetGRFTownName(GrfID grfid)
Get the GRFTownName for the given GRFID.
void DelGRFTownName(GrfID grfid)
Remove the GRFTownName mapping for the given GRFID.
void GRFTownNameGenerate(StringBuilder &builder, GrfID grfid, uint16_t gen, uint32_t seed)
Construct a NewGRF town name into the builder.
GRFTownName * AddGRFTownName(GrfID grfid)
Get the GRFTownName for the given GRFID or allocate one if it does not exist.
void InitGRFTownGeneratorNames()
Allocate memory for the NewGRF town names.
Header of Action 0F "universal holder" structure and functions.
Label< struct GrfIDTag > GrfID
The unique identifier of a NewGRF.
Definition newgrf_type.h:17
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Types and functions related to the internal workings of formatting OpenTTD's strings.
static constexpr uint16_t SPECSTR_TOWNNAME_START
Special strings for town names.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
std::vector< NamePartList > partlists[MAX_LISTS]
Lists of town name parts.
GrfID grfid
GRF ID of NewGRF.
std::vector< TownNameStyle > styles
Style names defined by the Town Name NewGRF.