OpenTTD Source 20250328-master-gc3457cd4c0
newgrf_act0_trains.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 "../newgrf_cargo.h"
13#include "../newgrf_engine.h"
14#include "../vehicle_base.h"
15#include "newgrf_bytereader.h"
17#include "newgrf_internal.h"
18
19#include "../safeguards.h"
20
29ChangeInfoResult RailVehicleChangeInfo(uint first, uint last, int prop, ByteReader &buf)
30{
32
33 for (uint id = first; id < last; ++id) {
34 Engine *e = GetNewEngine(_cur.grffile, VEH_TRAIN, id);
35 if (e == nullptr) return CIR_INVALID_ID; // No engine could be allocated, so neither can any next vehicles
36
37 EngineInfo *ei = &e->info;
38 RailVehicleInfo *rvi = &e->u.rail;
39
40 switch (prop) {
41 case 0x05: { // Track type
42 uint8_t tracktype = buf.ReadByte();
43
44 if (tracktype < _cur.grffile->railtype_list.size()) {
45 _gted[e->index].railtypelabel = _cur.grffile->railtype_list[tracktype];
46 break;
47 }
48
49 switch (tracktype) {
50 case 0: _gted[e->index].railtypelabel = rvi->engclass >= 2 ? RAILTYPE_LABEL_ELECTRIC : RAILTYPE_LABEL_RAIL; break;
51 case 1: _gted[e->index].railtypelabel = RAILTYPE_LABEL_MONO; break;
52 case 2: _gted[e->index].railtypelabel = RAILTYPE_LABEL_MAGLEV; break;
53 default:
54 GrfMsg(1, "RailVehicleChangeInfo: Invalid track type {} specified, ignoring", tracktype);
55 break;
56 }
57 break;
58 }
59
60 case 0x08: // AI passenger service
61 /* Tells the AI that this engine is designed for
62 * passenger services and shouldn't be used for freight. */
63 rvi->ai_passenger_only = buf.ReadByte();
64 break;
65
66 case PROP_TRAIN_SPEED: { // 0x09 Speed (1 unit is 1 km-ish/h)
67 uint16_t speed = buf.ReadWord();
68 if (speed == 0xFFFF) speed = 0;
69
70 rvi->max_speed = speed;
71 break;
72 }
73
74 case PROP_TRAIN_POWER: // 0x0B Power
75 rvi->power = buf.ReadWord();
76
77 /* Set engine / wagon state based on power */
78 if (rvi->power != 0) {
79 if (rvi->railveh_type == RAILVEH_WAGON) {
80 rvi->railveh_type = RAILVEH_SINGLEHEAD;
81 }
82 } else {
83 rvi->railveh_type = RAILVEH_WAGON;
84 }
85 break;
86
87 case PROP_TRAIN_RUNNING_COST_FACTOR: // 0x0D Running cost factor
88 rvi->running_cost = buf.ReadByte();
89 break;
90
91 case 0x0E: // Running cost base
92 ConvertTTDBasePrice(buf.ReadDWord(), "RailVehicleChangeInfo", &rvi->running_cost_class);
93 break;
94
95 case 0x12: { // Sprite ID
96 uint8_t spriteid = buf.ReadByte();
97 uint8_t orig_spriteid = spriteid;
98
99 /* TTD sprite IDs point to a location in a 16bit array, but we use it
100 * as an array index, so we need it to be half the original value. */
101 if (spriteid < 0xFD) spriteid >>= 1;
102
103 if (IsValidNewGRFImageIndex<VEH_TRAIN>(spriteid)) {
104 rvi->image_index = spriteid;
105 } else {
106 GrfMsg(1, "RailVehicleChangeInfo: Invalid Sprite {} specified, ignoring", orig_spriteid);
107 rvi->image_index = 0;
108 }
109 break;
110 }
111
112 case 0x13: { // Dual-headed
113 uint8_t dual = buf.ReadByte();
114
115 if (dual != 0) {
116 rvi->railveh_type = RAILVEH_MULTIHEAD;
117 } else {
118 rvi->railveh_type = rvi->power == 0 ?
120 }
121 break;
122 }
123
124 case PROP_TRAIN_CARGO_CAPACITY: // 0x14 Cargo capacity
125 rvi->capacity = buf.ReadByte();
126 break;
127
128 case 0x15: { // Cargo type
129 _gted[e->index].defaultcargo_grf = _cur.grffile;
130 uint8_t ctype = buf.ReadByte();
131
132 if (ctype == 0xFF) {
133 /* 0xFF is specified as 'use first refittable' */
134 ei->cargo_type = INVALID_CARGO;
135 } else {
136 /* Use translated cargo. Might result in INVALID_CARGO (first refittable), if cargo is not defined. */
137 ei->cargo_type = GetCargoTranslation(ctype, _cur.grffile);
138 if (ei->cargo_type == INVALID_CARGO) GrfMsg(2, "RailVehicleChangeInfo: Invalid cargo type {}, using first refittable", ctype);
139 }
140 ei->cargo_label = CT_INVALID;
141 break;
142 }
143
144 case PROP_TRAIN_WEIGHT: // 0x16 Weight
145 SB(rvi->weight, 0, 8, buf.ReadByte());
146 break;
147
148 case PROP_TRAIN_COST_FACTOR: // 0x17 Cost factor
149 rvi->cost_factor = buf.ReadByte();
150 break;
151
152 case 0x18: // AI rank
153 GrfMsg(2, "RailVehicleChangeInfo: Property 0x18 'AI rank' not used by NoAI, ignored.");
154 buf.ReadByte();
155 break;
156
157 case 0x19: { // Engine traction type
158 /* What do the individual numbers mean?
159 * 0x00 .. 0x07: Steam
160 * 0x08 .. 0x27: Diesel
161 * 0x28 .. 0x31: Electric
162 * 0x32 .. 0x37: Monorail
163 * 0x38 .. 0x41: Maglev
164 */
165 uint8_t traction = buf.ReadByte();
166 EngineClass engclass;
167
168 if (traction <= 0x07) {
169 engclass = EC_STEAM;
170 } else if (traction <= 0x27) {
171 engclass = EC_DIESEL;
172 } else if (traction <= 0x31) {
173 engclass = EC_ELECTRIC;
174 } else if (traction <= 0x37) {
175 engclass = EC_MONORAIL;
176 } else if (traction <= 0x41) {
177 engclass = EC_MAGLEV;
178 } else {
179 break;
180 }
181
182 if (_cur.grffile->railtype_list.empty()) {
183 /* Use traction type to select between normal and electrified
184 * rail only when no translation list is in place. */
185 if (_gted[e->index].railtypelabel == RAILTYPE_LABEL_RAIL && engclass >= EC_ELECTRIC) _gted[e->index].railtypelabel = RAILTYPE_LABEL_ELECTRIC;
186 if (_gted[e->index].railtypelabel == RAILTYPE_LABEL_ELECTRIC && engclass < EC_ELECTRIC) _gted[e->index].railtypelabel = RAILTYPE_LABEL_RAIL;
187 }
188
189 rvi->engclass = engclass;
190 break;
191 }
192
193 case 0x1A: // Alter purchase list sort order
195 break;
196
197 case 0x1B: // Powered wagons power bonus
198 rvi->pow_wag_power = buf.ReadWord();
199 break;
200
201 case 0x1C: // Refit cost
202 ei->refit_cost = buf.ReadByte();
203 break;
204
205 case 0x1D: { // Refit cargo
206 uint32_t mask = buf.ReadDWord();
207 _gted[e->index].UpdateRefittability(mask != 0);
208 ei->refit_mask = TranslateRefitMask(mask);
209 _gted[e->index].defaultcargo_grf = _cur.grffile;
210 break;
211 }
212
213 case 0x1E: { // Callback
214 auto mask = ei->callback_mask.base();
215 SB(mask, 0, 8, buf.ReadByte());
217 break;
218 }
219
220 case PROP_TRAIN_TRACTIVE_EFFORT: // 0x1F Tractive effort coefficient
221 rvi->tractive_effort = buf.ReadByte();
222 break;
223
224 case 0x20: // Air drag
225 rvi->air_drag = buf.ReadByte();
226 break;
227
228 case PROP_TRAIN_SHORTEN_FACTOR: // 0x21 Shorter vehicle
229 rvi->shorten_factor = buf.ReadByte();
230 break;
231
232 case 0x22: // Visual effect
233 rvi->visual_effect = buf.ReadByte();
234 /* Avoid accidentally setting visual_effect to the default value
235 * Since bit 6 (disable effects) is set anyways, we can safely erase some bits. */
236 if (rvi->visual_effect == VE_DEFAULT) {
239 }
240 break;
241
242 case 0x23: // Powered wagons weight bonus
243 rvi->pow_wag_weight = buf.ReadByte();
244 break;
245
246 case 0x24: { // High byte of vehicle weight
247 uint8_t weight = buf.ReadByte();
248
249 if (weight > 4) {
250 GrfMsg(2, "RailVehicleChangeInfo: Nonsensical weight of {} tons, ignoring", weight << 8);
251 } else {
252 SB(rvi->weight, 8, 8, weight);
253 }
254 break;
255 }
256
257 case PROP_TRAIN_USER_DATA: // 0x25 User-defined bit mask to set when checking veh. var. 42
258 rvi->user_def_data = buf.ReadByte();
259 break;
260
261 case 0x26: // Retire vehicle early
262 ei->retire_early = buf.ReadByte();
263 break;
264
265 case 0x27: // Miscellaneous flags
266 ei->misc_flags = static_cast<EngineMiscFlags>(buf.ReadByte());
268 break;
269
270 case 0x28: // Cargo classes allowed
271 _gted[e->index].cargo_allowed = CargoClasses{buf.ReadWord()};
272 _gted[e->index].UpdateRefittability(_gted[e->index].cargo_allowed.Any());
273 _gted[e->index].defaultcargo_grf = _cur.grffile;
274 break;
275
276 case 0x29: // Cargo classes disallowed
277 _gted[e->index].cargo_disallowed = CargoClasses{buf.ReadWord()};
278 _gted[e->index].UpdateRefittability(false);
279 break;
280
281 case 0x2A: // Long format introduction date (days since year 0)
282 ei->base_intro = TimerGameCalendar::Date(buf.ReadDWord());
283 break;
284
285 case PROP_TRAIN_CARGO_AGE_PERIOD: // 0x2B Cargo aging period
286 ei->cargo_age_period = buf.ReadWord();
287 break;
288
289 case 0x2C: // CTT refit include list
290 case 0x2D: { // CTT refit exclude list
291 uint8_t count = buf.ReadByte();
292 _gted[e->index].UpdateRefittability(prop == 0x2C && count != 0);
293 if (prop == 0x2C) _gted[e->index].defaultcargo_grf = _cur.grffile;
294 CargoTypes &ctt = prop == 0x2C ? _gted[e->index].ctt_include_mask : _gted[e->index].ctt_exclude_mask;
295 ctt = 0;
296 while (count--) {
297 CargoType ctype = GetCargoTranslation(buf.ReadByte(), _cur.grffile);
298 if (IsValidCargoType(ctype)) SetBit(ctt, ctype);
299 }
300 break;
301 }
302
303 case PROP_TRAIN_CURVE_SPEED_MOD: // 0x2E Curve speed modifier
304 rvi->curve_speed_mod = buf.ReadWord();
305 break;
306
307 case 0x2F: // Engine variant
308 ei->variant_id = static_cast<EngineID>(buf.ReadWord());
309 break;
310
311 case 0x30: // Extra miscellaneous flags
312 ei->extra_flags = static_cast<ExtraEngineFlags>(buf.ReadDWord());
313 break;
314
315 case 0x31: { // Callback additional mask
316 auto mask = ei->callback_mask.base();
317 SB(mask, 8, 8, buf.ReadByte());
319 break;
320 }
321
322 case 0x32: // Cargo classes required for a refit.
323 _gted[e->index].cargo_allowed_required = CargoClasses{buf.ReadWord()};
324 break;
325
326 case 0x33: // Badge list
327 e->badges = ReadBadgeList(buf, GSF_TRAINS);
328 break;
329
330 default:
331 ret = CommonVehicleChangeInfo(ei, prop, buf);
332 break;
333 }
334 }
335
336 return ret;
337}
338
340template <> ChangeInfoResult GrfChangeInfoHandler<GSF_TRAINS>::Activation(uint first, uint last, int prop, ByteReader &buf) { return RailVehicleChangeInfo(first, last, prop, buf); }
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
static constexpr CargoLabel CT_INVALID
Invalid cargo type.
Definition cargo_type.h:72
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:23
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:106
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
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).
uint16_t ReadExtendedByte()
Read a single Extended Byte (8 or 16 bits).
uint8_t ReadByte()
Read a single byte (8 bits).
@ Uses2CC
Vehicle uses two company colours.
EngineClass
Type of rail engine.
Definition engine_type.h:37
@ EC_DIESEL
Diesel rail engine.
Definition engine_type.h:39
@ EC_STEAM
Steam rail engine.
Definition engine_type.h:38
@ EC_MAGLEV
Maglev engine.
Definition engine_type.h:42
@ EC_ELECTRIC
Electric rail engine.
Definition engine_type.h:40
@ EC_MONORAIL
Mono rail engine.
Definition engine_type.h:41
@ RAILVEH_SINGLEHEAD
indicates a "standalone" locomotive
Definition engine_type.h:31
@ RAILVEH_WAGON
simple wagon, not motorized
Definition engine_type.h:33
@ RAILVEH_MULTIHEAD
indicates a combination of two locomotives
Definition engine_type.h:32
Engine * GetNewEngine(const GRFFile *file, VehicleType type, uint16_t internal_id, bool static_access)
Returns the engine associated to a certain internal_id, resp.
Definition newgrf.cpp:207
ReferenceThroughBaseContainer< std::vector< GRFTempEngineData > > _gted
Temporary engine data used during NewGRF loading.
Definition newgrf.cpp:76
void ConvertTTDBasePrice(uint32_t base_pointer, const char *error_location, Price *index)
Converts TTD(P) Base Price pointers into the enum used by OTTD See http://wiki.ttdpatch....
Definition newgrf.cpp:324
GRFLoadedFeatures _loaded_newgrf_features
Indicates which are the newgrf features currently loaded ingame.
Definition newgrf.cpp:72
CargoTypes TranslateRefitMask(uint32_t refit_mask)
Translate the refit mask.
Definition newgrf.cpp:307
ChangeInfoResult CommonVehicleChangeInfo(EngineInfo *ei, int prop, ByteReader &buf)
Define properties common to all vehicles.
std::vector< BadgeID > ReadBadgeList(ByteReader &buf, GrfSpecFeature feature)
Read a list of badges.
ChangeInfoResult RailVehicleChangeInfo(uint first, uint last, int prop, ByteReader &buf)
Define properties for rail vehicles.
NewGRF buffer reader definition.
CargoType GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoType.
void AlterVehicleListOrder(EngineID engine, uint16_t target)
Record a vehicle ListOrderChange.
NewGRF internal processing state.
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
@ CIR_INVALID_ID
Attempt to modify an invalid ID.
@ CIR_UNHANDLED
Variable was parsed but unread.
@ CIR_SUCCESS
Variable was parsed and read.
NewGRF internal processing state for vehicles.
@ PROP_TRAIN_SHORTEN_FACTOR
Shorter vehicles.
@ PROP_TRAIN_CURVE_SPEED_MOD
Modifier to maximum speed in curves.
@ PROP_TRAIN_COST_FACTOR
Purchase cost (if dualheaded: sum of both vehicles)
@ PROP_TRAIN_USER_DATA
User defined data for vehicle variable 0x42.
@ PROP_TRAIN_WEIGHT
Weight in t (if dualheaded: for each single vehicle)
@ PROP_TRAIN_CARGO_CAPACITY
Capacity (if dualheaded: for each single vehicle)
@ PROP_TRAIN_TRACTIVE_EFFORT
Tractive effort coefficient in 1/256.
@ PROP_TRAIN_CARGO_AGE_PERIOD
Number of ticks before carried cargo is aged.
@ PROP_TRAIN_RUNNING_COST_FACTOR
Yearly runningcost (if dualheaded: sum of both vehicles)
@ PROP_TRAIN_POWER
Power in hp (if dualheaded: sum of both vehicles)
@ PROP_TRAIN_SPEED
Max. speed: 1 unit = 1/1.6 mph = 1 km-ish/h.
Information about a vehicle.
uint16_t cargo_age_period
Number of ticks before carried cargo is aged.
EngineMiscFlags misc_flags
Miscellaneous flags.
EngineID variant_id
Engine variant ID. If set, will be treated specially in purchase lists.
VehicleCallbackMasks callback_mask
Bitmask of vehicle callbacks that have to be called.
TimerGameCalendar::Date base_intro
Basic date of engine introduction (without random parts).
int8_t retire_early
Number of years early to retire vehicle.
std::vector< RailTypeLabel > railtype_list
Railtype translation table.
Definition newgrf.h:140
bool has_2CC
Set if any vehicle is loaded which uses 2cc (two company colours).
Definition newgrf.h:184
GRF feature handler.
GRFFile * grffile
Currently processed GRF file.
Tindex index
Index of this pool item.
Information about a rail vehicle.
Definition engine_type.h:46
uint16_t power
Power of engine (hp); For multiheaded engines the sum of both engine powers.
Definition engine_type.h:54
uint8_t user_def_data
Property 0x25: "User-defined bit mask" Used only for (very few) NewGRF vehicles.
Definition engine_type.h:66
uint8_t running_cost
Running cost of engine; For multiheaded engines the sum of both running costs.
Definition engine_type.h:56
uint8_t cost_factor
Purchase cost factor; For multiheaded engines the sum of both engine prices.
Definition engine_type.h:49
uint8_t shorten_factor
length on main map for this type is 8 - shorten_factor
Definition engine_type.h:63
uint16_t pow_wag_power
Extra power applied to consist if wagon should be powered.
Definition engine_type.h:60
uint16_t max_speed
Maximum speed (1 unit = 1/1.6 mph = 1 km-ish/h)
Definition engine_type.h:53
int16_t curve_speed_mod
Modifier to maximum speed in curves (fixed-point binary with 8 fractional bits)
Definition engine_type.h:67
uint16_t weight
Weight of vehicle (tons); For multiheaded engines the weight of each single engine.
Definition engine_type.h:55
uint8_t capacity
Cargo capacity of vehicle; For multiheaded engines the capacity of each single engine.
Definition engine_type.h:59
uint8_t visual_effect
Bitstuffed NewGRF visual effect data.
Definition engine_type.h:62
uint8_t air_drag
Coefficient of air drag.
Definition engine_type.h:65
EngineClass engclass
Class of engine for this vehicle.
Definition engine_type.h:58
uint8_t ai_passenger_only
Bit value to tell AI that this engine is for passenger use only.
Definition engine_type.h:52
uint8_t tractive_effort
Tractive effort coefficient.
Definition engine_type.h:64
uint8_t pow_wag_weight
Extra weight applied to consist if wagon should be powered.
Definition engine_type.h:61
@ VE_TYPE_COUNT
Number of bits used for the effect type.
@ VE_TYPE_START
First bit used for the type of effect.
@ VE_DISABLE_EFFECT
Flag to disable visual effect.
@ VE_DEFAULT
Default value to indicate that visual effect should be based on engine class.
@ VEH_TRAIN
Train vehicle type.