OpenTTD Source 20250312-master-gcdcc6b491d
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 "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
27GRFTownName *GetGRFTownName(uint32_t grfid)
28{
29 auto found = std::ranges::find(_grf_townnames, grfid, &GRFTownName::grfid);
30 if (found != std::end(_grf_townnames)) return &*found;
31 return nullptr;
32}
33
34GRFTownName *AddGRFTownName(uint32_t grfid)
35{
36 GRFTownName *t = GetGRFTownName(grfid);
37 if (t == nullptr) {
38 t = &_grf_townnames.emplace_back();
39 t->grfid = grfid;
40 }
41 return t;
42}
43
44void DelGRFTownName(uint32_t grfid)
45{
46 _grf_townnames.erase(std::ranges::find(_grf_townnames, grfid, &GRFTownName::grfid));
47}
48
49static void RandomPart(StringBuilder &builder, const GRFTownName *t, uint32_t seed, uint8_t id)
50{
51 assert(t != nullptr);
52 for (const auto &partlist : t->partlists[id]) {
53 uint8_t count = partlist.bitcount;
54 uint16_t maxprob = partlist.maxprob;
55 uint32_t r = (GB(seed, partlist.bitstart, count) * maxprob) >> count;
56 for (const auto &part : partlist.parts) {
57 maxprob -= GB(part.prob, 0, 7);
58 if (maxprob > r) continue;
59 if (HasBit(part.prob, 7)) {
60 RandomPart(builder, t, seed, part.id);
61 } else {
62 builder += part.text;
63 }
64 break;
65 }
66 }
67}
68
69void GRFTownNameGenerate(StringBuilder &builder, uint32_t grfid, uint16_t gen, uint32_t seed)
70{
71 const GRFTownName *t = GetGRFTownName(grfid);
72 if (t != nullptr) {
73 assert(gen < t->styles.size());
74 RandomPart(builder, t, seed, t->styles[gen].id);
75 }
76}
77
78
81{
82 _grf_townname_names.clear();
83 for (const auto &t : _grf_townnames) {
84 for (const auto &style : t.styles) {
85 _grf_townname_names.push_back(style.name);
86 }
87 }
88}
89
90const std::vector<StringID> &GetGRFTownNameList()
91{
92 return _grf_townname_names;
93}
94
95StringID GetGRFTownNameName(uint16_t gen)
96{
97 return gen < _grf_townname_names.size() ? _grf_townname_names[gen] : STR_UNDEFINED;
98}
99
100void CleanUpGRFTownNames()
101{
102 _grf_townnames.clear();
103}
104
105uint32_t GetGRFTownNameId(uint16_t gen)
106{
107 for (const auto &t : _grf_townnames) {
108 if (gen < t.styles.size()) return t.grfid;
109 gen -= static_cast<uint16_t>(t.styles.size());
110 }
111 /* Fallback to no NewGRF */
112 return 0;
113}
114
115uint16_t GetGRFTownNameType(uint16_t gen)
116{
117 for (const auto &t : _grf_townnames) {
118 if (gen < t.styles.size()) return gen;
119 gen -= static_cast<uint16_t>(t.styles.size());
120 }
121 /* Fallback to the first built in town name (English). */
123}
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
debug_inline static constexpr 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.
static constexpr StringID SPECSTR_TOWNNAME_START
Special strings for town names.
std::vector< TownNameStyle > styles
Style names defined by the Town Name NewGRF.
uint32_t grfid
GRF ID of NewGRF.