OpenTTD Source 20260721-master-g25ec12c62d
newgrf_act0_roadtypes.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
9
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../road.h"
13#include "newgrf_bytereader.h"
14#include "newgrf_internal.h"
16
17#include "../safeguards.h"
18
28static ChangeInfoResult RoadTypeChangeInfo(uint first, uint last, int prop, ByteReader &buf, RoadTramType rtt)
29{
31
32 extern RoadTypeInfo _roadtypes[ROADTYPE_END];
33 const auto &type_map = (rtt == RoadTramType::Tram) ? _cur_gps.grffile->tramtype_map : _cur_gps.grffile->roadtype_map;
34
35 if (last > std::size(type_map)) {
36 GrfMsg(1, "RoadTypeChangeInfo: Road type {} is invalid, max {}, ignoring", last, std::size(type_map));
38 }
39
40 for (uint id = first; id < last; ++id) {
41 RoadType rt = type_map[id];
43
44 RoadTypeInfo *rti = &_roadtypes[rt];
45
46 switch (prop) {
47 case 0x08: // Label of road type
48 /* Skipped here as this is loaded during reservation stage. */
49 buf.ReadDWord();
50 break;
51
52 case 0x09: // Toolbar caption of roadtype
54 break;
55
56 case 0x0A: // Menu text of roadtype
58 break;
59
60 case 0x0B: // Build window caption
62 break;
63
64 case 0x0C: // Autoreplace text
66 break;
67
68 case 0x0D: // New engine text
70 break;
71
72 case 0x0F: // Powered roadtype list
73 case 0x18: // Roadtype list required for date introduction
74 case 0x19: { // Introduced roadtype list
75 /* Road type compatibility bits are added to the existing bits
76 * to allow multiple GRFs to modify compatibility with the
77 * default road types. */
78 int n = buf.ReadByte();
79 for (int j = 0; j != n; j++) {
80 RoadType resolved_rt = GetRoadTypeByLabel(buf.ReadLabel<RoadTypeLabel>(), false);
81 if (resolved_rt != INVALID_ROADTYPE) {
82 switch (prop) {
83 case 0x0F:
84 if (GetRoadTramType(resolved_rt) == rtt) {
85 rti->powered_roadtypes.Set(resolved_rt);
86 } else {
87 GrfMsg(1, "RoadTypeChangeInfo: Powered road type list: Road type {} road/tram type does not match road type {}, ignoring", resolved_rt, rt);
88 }
89 break;
90 case 0x18: rti->introduction_required_roadtypes.Set(resolved_rt); break;
91 case 0x19: rti->introduces_roadtypes.Set(resolved_rt); break;
92 }
93 }
94 }
95 break;
96 }
97
98 case 0x10: // Road Type flags
99 rti->flags = static_cast<RoadTypeFlags>(buf.ReadByte());
100 break;
101
102 case 0x13: // Construction cost factor
103 rti->cost_multiplier = buf.ReadWord();
104 break;
105
106 case 0x14: // Speed limit
107 rti->max_speed = buf.ReadWord();
108 break;
109
110 case 0x16: // Map colour
111 rti->map_colour = PixelColour{buf.ReadByte()};
112 break;
113
114 case 0x17: // Introduction date
116 break;
117
118 case 0x1A: // Sort order
119 rti->sorting_order = buf.ReadByte();
120 break;
121
122 case 0x1B: // Name of roadtype
124 break;
125
126 case 0x1C: // Maintenance cost factor
127 rti->maintenance_multiplier = buf.ReadWord();
128 break;
129
130 case 0x1D: // Alternate road type label list
131 /* Skipped here as this is loaded during reservation stage. */
132 for (int j = buf.ReadByte(); j != 0; j--) buf.ReadDWord();
133 break;
134
135 case 0x1E: // Badge list
136 rti->badges = ReadBadgeList(buf, GrfSpecFeature::RoadTypes);
137 break;
138
139 default:
141 break;
142 }
143 }
144
145 return ret;
146}
147
148static ChangeInfoResult RoadTypeReserveInfo(uint first, uint last, int prop, ByteReader &buf, RoadTramType rtt)
149{
151
152 extern RoadTypeInfo _roadtypes[ROADTYPE_END];
153 auto &type_map = (rtt == RoadTramType::Tram) ? _cur_gps.grffile->tramtype_map : _cur_gps.grffile->roadtype_map;
154
155 if (last > std::size(type_map)) {
156 GrfMsg(1, "RoadTypeReserveInfo: Road type {} is invalid, max {}, ignoring", last, std::size(type_map));
158 }
159
160 for (uint id = first; id < last; ++id) {
161 switch (prop) {
162 case 0x08: { // Label of road type
163 RoadTypeLabel rtl = buf.ReadLabel<RoadTypeLabel>();
164 RoadType rt = GetRoadTypeByLabel(rtl, false);
165 if (rt == INVALID_ROADTYPE) {
166 /* Set up new road type */
167 rt = AllocateRoadType(rtl, rtt);
168 } else if (GetRoadTramType(rt) != rtt) {
169 GrfMsg(1, "RoadTypeReserveInfo: Road type {} is invalid type (road/tram), ignoring", id);
171 }
172
173 type_map[id] = rt;
174 break;
175 }
176 case 0x09: // Toolbar caption of roadtype
177 case 0x0A: // Menu text
178 case 0x0B: // Build window caption
179 case 0x0C: // Autoreplace text
180 case 0x0D: // New loco
181 case 0x13: // Construction cost
182 case 0x14: // Speed limit
183 case 0x1B: // Name of roadtype
184 case 0x1C: // Maintenance cost factor
185 buf.ReadWord();
186 break;
187
188 case 0x1D: // Alternate road type label list
189 if (type_map[id] != INVALID_ROADTYPE) {
190 int n = buf.ReadByte();
191 for (int j = 0; j != n; j++) {
192 _roadtypes[type_map[id]].alternate_labels.insert(buf.ReadLabel<RoadTypeLabel>());
193 }
194 break;
195 }
196 GrfMsg(1, "RoadTypeReserveInfo: Ignoring property 1D for road type {} because no label was set", id);
197 /* FALL THROUGH */
198
199 case 0x0F: // Powered roadtype list
200 case 0x18: // Roadtype list required for date introduction
201 case 0x19: // Introduced roadtype list
202 for (int j = buf.ReadByte(); j != 0; j--) buf.ReadDWord();
203 break;
204
205 case 0x10: // Road Type flags
206 case 0x16: // Map colour
207 case 0x1A: // Sort order
208 buf.ReadByte();
209 break;
210
211 case 0x17: // Introduction date
212 buf.ReadDWord();
213 break;
214
215 case 0x1E: // Badge list
216 SkipBadgeList(buf);
217 break;
218
219 default:
221 break;
222 }
223 }
224
225 return ret;
226}
227
229template <> ChangeInfoResult GrfChangeInfoHandler<GrfSpecFeature::RoadTypes>::Reserve(uint first, uint last, int prop, ByteReader &buf) { return RoadTypeReserveInfo(first, last, prop, buf, RoadTramType::Road); }
231template <> ChangeInfoResult GrfChangeInfoHandler<GrfSpecFeature::RoadTypes>::Activation(uint first, uint last, int prop, ByteReader &buf) { return RoadTypeChangeInfo(first, last, prop, buf, RoadTramType::Road); }
232
234template <> ChangeInfoResult GrfChangeInfoHandler<GrfSpecFeature::TramTypes>::Reserve(uint first, uint last, int prop, ByteReader &buf) { return RoadTypeReserveInfo(first, last, prop, buf, RoadTramType::Tram); }
236template <> ChangeInfoResult GrfChangeInfoHandler<GrfSpecFeature::TramTypes>::Activation(uint first, uint last, int prop, ByteReader &buf) { return RoadTypeChangeInfo(first, last, prop, buf, RoadTramType::Tram); }
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).
T ReadLabel()
Read a label.
uint8_t ReadByte()
Read a single byte (8 bits).
StringID menu_text
Name of this rail type in the main toolbar dropdown.
Definition road.h:81
StringID replace_text
Text used in the autoreplace GUI.
Definition road.h:83
RoadTypes powered_roadtypes
bitmask to the OTHER roadtypes on which a vehicle of THIS roadtype generates power
Definition road.h:98
RoadTypes introduces_roadtypes
Bitmask of which other roadtypes are introduced when this roadtype is introduced.
Definition road.h:153
TimerGameCalendar::Date introduction_date
Introduction date.
Definition road.h:142
uint8_t sorting_order
The sorting order of this roadtype for the toolbar dropdown.
Definition road.h:158
uint16_t maintenance_multiplier
Cost multiplier for maintenance of this road type.
Definition road.h:113
RoadTypeFlags flags
Bit mask of road type flags.
Definition road.h:103
uint16_t max_speed
Maximum speed for vehicles travelling on this road type.
Definition road.h:118
struct RoadTypeInfo::@070000167274302256150317022075324310363002361255 strings
Strings associated with the rail type.
StringID name
Name of this rail type.
Definition road.h:79
StringID toolbar_caption
Caption in the construction toolbar GUI for this rail type.
Definition road.h:80
StringID new_engine
Name of an engine for this type of road in the engine preview GUI.
Definition road.h:84
RoadTypes introduction_required_roadtypes
Bitmask of roadtypes that are required for this roadtype to be introduced at a given introduction_dat...
Definition road.h:148
EnumIndexArray< const GRFFile *, RoadSpriteType, RoadSpriteType::End > grffile
NewGRF providing the Action3 for the roadtype.
Definition road.h:163
uint16_t cost_multiplier
Cost multiplier for building this road type.
Definition road.h:108
PixelColour map_colour
Colour on mini-map.
Definition road.h:133
StringID build_caption
Caption of the build vehicle GUI for this rail type.
Definition road.h:82
StrongType::Typedef< int32_t, DateTag< struct Calendar >, StrongType::Compare, StrongType::Integer > Date
Functions related to debugging.
@ RoadTypes
Road types feature.
Definition newgrf.h:97
std::vector< BadgeID > ReadBadgeList(ByteReader &buf, GrfSpecFeature feature)
Read a list of badges.
void SkipBadgeList(ByteReader &buf)
Skip a list of badges.
static ChangeInfoResult RoadTypeChangeInfo(uint first, uint last, int prop, ByteReader &buf, RoadTramType rtt)
Define properties for roadtypes.
NewGRF buffer reader definition.
NewGRF internal processing state.
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
@ Success
Variable was parsed and read.
@ Unknown
Variable is unknown.
@ InvalidId
Attempt to modify an invalid ID.
void AddStringForMapping(GRFStringID source, std::function< void(StringID)> &&func)
Record a static StringID for getting translated later.
NewGRF string mapping definition.
StrongType::Typedef< uint32_t, struct GRFStringIDTag, StrongType::Compare, StrongType::Integer > GRFStringID
Type for GRF-internal string IDs.
RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels)
Get the road type for a given label.
Definition road.cpp:265
Road specific functions.
EnumBitSet< RoadTypeFlag, uint8_t > RoadTypeFlags
Bitset of RoadTypeFlag elements.
Definition road.h:33
RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt)
Allocate a new road type label.
Definition road_cmd.cpp:132
RoadType
The different roadtypes we support.
Definition road_type.h:24
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition road_type.h:29
@ ROADTYPE_END
Used for iterations.
Definition road_type.h:28
RoadTramType
The different types of road type.
Definition road_type.h:38
@ Tram
Tram type.
Definition road_type.h:40
@ Road
Road type.
Definition road_type.h:39
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
static ChangeInfoResult Reserve(uint first, uint last, int prop, ByteReader &buf)
Implementation of the GrfLoadingStage::Reserve stage of this feature.
static ChangeInfoResult Activation(uint first, uint last, int prop, ByteReader &buf)
Implementation of the GrfLoadingStage::Activation stage of this feature.
Colour for pixel/line drawing.
Definition gfx_type.h:307