OpenTTD Source 20260721-master-g25ec12c62d
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 "string_func.h"
18#include "strings_internal.h"
19
20#include "table/strings.h"
21
22#include "safeguards.h"
23
24static std::vector<GRFTownName> _grf_townnames;
25static std::vector<StringID> _grf_townname_names;
26
33{
34 auto found = std::ranges::find(_grf_townnames, grfid, &GRFTownName::grfid);
35 if (found != std::end(_grf_townnames)) return &*found;
36 return nullptr;
37}
38
45{
46 GRFTownName *t = GetGRFTownName(grfid);
47 if (t == nullptr) {
48 t = &_grf_townnames.emplace_back();
49 t->grfid = grfid;
50 }
51 return t;
52}
53
59{
60 _grf_townnames.erase(std::ranges::find(_grf_townnames, grfid, &GRFTownName::grfid));
61}
62
63static void RandomPart(StringBuilder &builder, const GRFTownName *t, uint32_t seed, uint8_t id)
64{
65 assert(t != nullptr);
66 for (const auto &partlist : t->partlists[id]) {
67 uint8_t count = partlist.bitcount;
68 uint16_t maxprob = partlist.maxprob;
69 uint32_t r = (GB(seed, partlist.bitstart, count) * maxprob) >> count;
70 for (const auto &part : partlist.parts) {
71 maxprob -= GB(part.prob, 0, 7);
72 if (maxprob > r) continue;
73 if (HasBit(part.prob, 7)) {
74 RandomPart(builder, t, seed, part.id);
75 } else {
76 builder += part.text;
77 }
78 break;
79 }
80 }
81}
82
90void GRFTownNameGenerate(StringBuilder &builder, GrfID grfid, uint16_t gen, uint32_t seed)
91{
92 const GRFTownName *t = GetGRFTownName(grfid);
93 if (t != nullptr) {
94 assert(gen < t->styles.size());
95 RandomPart(builder, t, seed, t->styles[gen].id);
96 }
97}
98
99
102{
103 _grf_townname_names.clear();
104 for (const auto &t : _grf_townnames) {
105 for (const auto &style : t.styles) {
106 _grf_townname_names.push_back(style.name);
107 }
108 }
109}
110
111const std::vector<StringID> &GetGRFTownNameList()
112{
113 return _grf_townname_names;
114}
115
116StringID GetGRFTownNameName(uint16_t gen)
117{
118 return gen < _grf_townname_names.size() ? _grf_townname_names[gen] : STR_UNDEFINED;
119}
120
121void CleanUpGRFTownNames()
122{
123 _grf_townnames.clear();
124}
125
126GrfID GetGRFTownNameId(uint16_t gen)
127{
128 for (const auto &t : _grf_townnames) {
129 if (gen < t.styles.size()) return t.grfid;
130 gen -= static_cast<uint16_t>(t.styles.size());
131 }
132 /* Fallback to no NewGRF */
133 return {};
134}
135
136uint16_t GetGRFTownNameType(uint16_t gen)
137{
138 for (const auto &t : _grf_townnames) {
139 if (gen < t.styles.size()) return gen;
140 gen -= static_cast<uint16_t>(t.styles.size());
141 }
142 /* Fallback to the first built in town name (English). */
144}
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.
Functions related to low-level strings.
Types and functions related to the internal workings of formatting OpenTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
static constexpr StringID SPECSTR_TOWNNAME_START
Special strings for town names.
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.