OpenTTD Source: src/newgrf_townname.cpp Source File

OpenTTD Source 15.3

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
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
27 GRFTownName *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
34 GRFTownName *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
80 void InitGRFTownGeneratorNames()
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
95 StringID 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). */
122 return SPECSTR_TOWNNAME_START;
123}
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
StringBuilder
Compose data into a growing std::string.
Definition string_builder.hpp:65
InitGRFTownGeneratorNames
void InitGRFTownGeneratorNames()
Allocate memory for the NewGRF town names.
Definition newgrf_townname.cpp:80
newgrf_townname.h
Header of Action 0F "universal holder" structure and functions.
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
string_func.h
Functions related to low-level strings.
strings_internal.h
Types and functions related to the internal workings of formatting OpenTTD's strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
SPECSTR_TOWNNAME_START
static constexpr StringID SPECSTR_TOWNNAME_START
Special strings for town names.
Definition strings_type.h:64
GRFTownName
Definition newgrf_townname.h:38
GRFTownName::styles
std::vector< TownNameStyle > styles
Style names defined by the Town Name NewGRF.
Definition newgrf_townname.h:42
GRFTownName::grfid
uint32_t grfid
GRF ID of NewGRF.
Definition newgrf_townname.h:41