OpenTTD Source 20250328-master-gc3457cd4c0
newgrf_act0_railtypes.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
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../rail.h"
13#include "newgrf_bytereader.h"
14#include "newgrf_internal.h"
16
17#include "../safeguards.h"
18
27static ChangeInfoResult RailTypeChangeInfo(uint first, uint last, int prop, ByteReader &buf)
28{
30
31 extern RailTypeInfo _railtypes[RAILTYPE_END];
32
33 if (last > RAILTYPE_END) {
34 GrfMsg(1, "RailTypeChangeInfo: Rail type {} is invalid, max {}, ignoring", last, RAILTYPE_END);
35 return CIR_INVALID_ID;
36 }
37
38 for (uint id = first; id < last; ++id) {
39 RailType rt = _cur.grffile->railtype_map[id];
40 if (rt == INVALID_RAILTYPE) return CIR_INVALID_ID;
41
42 RailTypeInfo *rti = &_railtypes[rt];
43
44 switch (prop) {
45 case 0x08: // Label of rail type
46 /* Skipped here as this is loaded during reservation stage. */
47 buf.ReadDWord();
48 break;
49
50 case 0x09: { // Toolbar caption of railtype (sets name as well for backwards compatibility for grf ver < 8)
51 GRFStringID str{buf.ReadWord()};
53 if (_cur.grffile->grf_version < 8) {
55 }
56 break;
57 }
58
59 case 0x0A: // Menu text of railtype
61 break;
62
63 case 0x0B: // Build window caption
65 break;
66
67 case 0x0C: // Autoreplace text
69 break;
70
71 case 0x0D: // New locomotive text
73 break;
74
75 case 0x0E: // Compatible railtype list
76 case 0x0F: // Powered railtype list
77 case 0x18: // Railtype list required for date introduction
78 case 0x19: // Introduced railtype list
79 {
80 /* Rail type compatibility bits are added to the existing bits
81 * to allow multiple GRFs to modify compatibility with the
82 * default rail types. */
83 int n = buf.ReadByte();
84 for (int j = 0; j != n; j++) {
85 RailTypeLabel label = buf.ReadDWord();
86 RailType resolved_rt = GetRailTypeByLabel(std::byteswap(label), false);
87 if (resolved_rt != INVALID_RAILTYPE) {
88 switch (prop) {
89 case 0x0F: rti->powered_railtypes.Set(resolved_rt); [[fallthrough]]; // Powered implies compatible.
90 case 0x0E: rti->compatible_railtypes.Set(resolved_rt); break;
91 case 0x18: rti->introduction_required_railtypes.Set(resolved_rt); break;
92 case 0x19: rti->introduces_railtypes.Set(resolved_rt); break;
93 }
94 }
95 }
96 break;
97 }
98
99 case 0x10: // Rail Type flags
100 rti->flags = static_cast<RailTypeFlags>(buf.ReadByte());
101 break;
102
103 case 0x11: // Curve speed advantage
104 rti->curve_speed = buf.ReadByte();
105 break;
106
107 case 0x12: // Station graphic
108 rti->fallback_railtype = Clamp(buf.ReadByte(), 0, 2);
109 break;
110
111 case 0x13: // Construction cost factor
112 rti->cost_multiplier = buf.ReadWord();
113 break;
114
115 case 0x14: // Speed limit
116 rti->max_speed = buf.ReadWord();
117 break;
118
119 case 0x15: // Acceleration model
120 rti->acceleration_type = Clamp(buf.ReadByte(), 0, 2);
121 break;
122
123 case 0x16: // Map colour
124 rti->map_colour = buf.ReadByte();
125 break;
126
127 case 0x17: // Introduction date
128 rti->introduction_date = TimerGameCalendar::Date(buf.ReadDWord());
129 break;
130
131 case 0x1A: // Sort order
132 rti->sorting_order = buf.ReadByte();
133 break;
134
135 case 0x1B: // Name of railtype (overridden by prop 09 for grf ver < 8)
137 break;
138
139 case 0x1C: // Maintenance cost factor
140 rti->maintenance_multiplier = buf.ReadWord();
141 break;
142
143 case 0x1D: // Alternate rail type label list
144 /* Skipped here as this is loaded during reservation stage. */
145 for (int j = buf.ReadByte(); j != 0; j--) buf.ReadDWord();
146 break;
147
148 case 0x1E: // Badge list
149 rti->badges = ReadBadgeList(buf, GSF_RAILTYPES);
150 break;
151
152 default:
153 ret = CIR_UNKNOWN;
154 break;
155 }
156 }
157
158 return ret;
159}
160
161static ChangeInfoResult RailTypeReserveInfo(uint first, uint last, int prop, ByteReader &buf)
162{
164
165 extern RailTypeInfo _railtypes[RAILTYPE_END];
166
167 if (last > RAILTYPE_END) {
168 GrfMsg(1, "RailTypeReserveInfo: Rail type {} is invalid, max {}, ignoring", last, RAILTYPE_END);
169 return CIR_INVALID_ID;
170 }
171
172 for (uint id = first; id < last; ++id) {
173 switch (prop) {
174 case 0x08: // Label of rail type
175 {
176 RailTypeLabel rtl = buf.ReadDWord();
177 rtl = std::byteswap(rtl);
178
179 RailType rt = GetRailTypeByLabel(rtl, false);
180 if (rt == INVALID_RAILTYPE) {
181 /* Set up new rail type */
182 rt = AllocateRailType(rtl);
183 }
184
185 _cur.grffile->railtype_map[id] = rt;
186 break;
187 }
188
189 case 0x09: // Toolbar caption of railtype
190 case 0x0A: // Menu text
191 case 0x0B: // Build window caption
192 case 0x0C: // Autoreplace text
193 case 0x0D: // New loco
194 case 0x13: // Construction cost
195 case 0x14: // Speed limit
196 case 0x1B: // Name of railtype
197 case 0x1C: // Maintenance cost factor
198 buf.ReadWord();
199 break;
200
201 case 0x1D: // Alternate rail type label list
202 if (_cur.grffile->railtype_map[id] != INVALID_RAILTYPE) {
203 int n = buf.ReadByte();
204 for (int j = 0; j != n; j++) {
205 _railtypes[_cur.grffile->railtype_map[id]].alternate_labels.push_back(std::byteswap(buf.ReadDWord()));
206 }
207 break;
208 }
209 GrfMsg(1, "RailTypeReserveInfo: Ignoring property 1D for rail type {} because no label was set", id);
210 [[fallthrough]];
211
212 case 0x0E: // Compatible railtype list
213 case 0x0F: // Powered railtype list
214 case 0x18: // Railtype list required for date introduction
215 case 0x19: // Introduced railtype list
216 for (int j = buf.ReadByte(); j != 0; j--) buf.ReadDWord();
217 break;
218
219 case 0x10: // Rail Type flags
220 case 0x11: // Curve speed advantage
221 case 0x12: // Station graphic
222 case 0x15: // Acceleration model
223 case 0x16: // Map colour
224 case 0x1A: // Sort order
225 buf.ReadByte();
226 break;
227
228 case 0x17: // Introduction date
229 buf.ReadDWord();
230 break;
231
232 case 0x1E: // Badge list
233 SkipBadgeList(buf);
234 break;
235
236 default:
237 ret = CIR_UNKNOWN;
238 break;
239 }
240 }
241
242 return ret;
243}
244
245template <> ChangeInfoResult GrfChangeInfoHandler<GSF_RAILTYPES>::Reserve(uint first, uint last, int prop, ByteReader &buf) { return RailTypeReserveInfo(first, last, prop, buf); }
246template <> ChangeInfoResult GrfChangeInfoHandler<GSF_RAILTYPES>::Activation(uint first, uint last, int prop, ByteReader &buf) { return RailTypeChangeInfo(first, last, prop, buf); }
constexpr enable_if_t< is_integral_v< T >, T > byteswap(T x) noexcept
Custom implementation of std::byteswap; remove once we build with C++23.
constexpr Timpl & Set()
Set all bits.
Class to read from a NewGRF file.
uint32_t ReadDWord()
Read a single DWord (32 bits).
uint16_t ReadWord()
Read a single Word (16 bits).
uint8_t ReadByte()
Read a single byte (8 bits).
This struct contains all the info that is needed to draw and construct tracks.
Definition rail.h:118
uint16_t max_speed
Maximum speed for vehicles travelling on this rail type.
Definition rail.h:222
TimerGameCalendar::Date introduction_date
Introduction date.
Definition rail.h:246
RailTypes powered_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype generates power
Definition rail.h:179
RailTypes introduces_railtypes
Bitmask of which other railtypes are introduced when this railtype is introduced.
Definition rail.h:257
RailTypes introduction_required_railtypes
Bitmask of railtypes that are required for this railtype to be introduced at a given introduction_dat...
Definition rail.h:252
uint8_t sorting_order
The sorting order of this railtype for the toolbar dropdown.
Definition rail.h:262
struct RailTypeInfo::@24 strings
Strings associated with the rail type.
uint16_t maintenance_multiplier
Cost multiplier for maintenance of this rail type.
Definition rail.h:212
uint8_t map_colour
Colour on mini-map.
Definition rail.h:237
StringID menu_text
Name of this rail type in the main toolbar dropdown.
Definition rail.h:169
StringID name
Name of this rail type.
Definition rail.h:167
RailTypes compatible_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel
Definition rail.h:182
uint8_t fallback_railtype
Original railtype number to use when drawing non-newgrf railtypes, or when drawing stations.
Definition rail.h:192
StringID toolbar_caption
Caption in the construction toolbar GUI for this rail type.
Definition rail.h:168
RailTypeFlags flags
Bit mask of rail type flags.
Definition rail.h:202
uint8_t curve_speed
Multiplier for curve maximum speed advantage.
Definition rail.h:197
uint16_t cost_multiplier
Cost multiplier for building this rail type.
Definition rail.h:207
StringID replace_text
Text used in the autoreplace GUI.
Definition rail.h:171
RailTypeLabelList alternate_labels
Rail type labels this type provides in addition to the main label.
Definition rail.h:232
StringID build_caption
Caption of the build vehicle GUI for this rail type.
Definition rail.h:170
StringID new_loco
Name of an engine for this type of rail in the engine preview GUI.
Definition rail.h:172
uint8_t acceleration_type
Acceleration type of this rail type.
Definition rail.h:217
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
std::vector< BadgeID > ReadBadgeList(ByteReader &buf, GrfSpecFeature feature)
Read a list of badges.
void SkipBadgeList(ByteReader &buf)
Skip a list of badges.
static ChangeInfoResult RailTypeChangeInfo(uint first, uint last, int prop, ByteReader &buf)
Define properties for railtypes.
NewGRF buffer reader definition.
NewGRF internal processing state.
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
@ CIR_INVALID_ID
Attempt to modify an invalid ID.
@ CIR_UNKNOWN
Variable is unknown.
@ CIR_SUCCESS
Variable was parsed and read.
void AddStringForMapping(GRFStringID source, std::function< void(StringID)> &&func)
Record a static StringID for getting translated later.
NewGRF string mapping definition.
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
Get the rail type for a given label.
Definition rail.cpp:313
RailType AllocateRailType(RailTypeLabel label)
Allocate a new rail type label.
Definition rail_cmd.cpp:149
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:25
@ RAILTYPE_END
Used for iterations.
Definition rail_type.h:31
@ INVALID_RAILTYPE
Flag for invalid railtype.
Definition rail_type.h:32
GRF feature handler.
GRFFile * grffile
Currently processed GRF file.