OpenTTD Source 20250328-master-gc3457cd4c0
newgrf_act0_roadvehs.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 "../newgrf_sound.h"
15#include "../vehicle_base.h"
16#include "newgrf_bytereader.h"
18#include "newgrf_internal.h"
19
20#include "../safeguards.h"
21
30static ChangeInfoResult RoadVehicleChangeInfo(uint first, uint last, int prop, ByteReader &buf)
31{
33
34 for (uint id = first; id < last; ++id) {
35 Engine *e = GetNewEngine(_cur.grffile, VEH_ROAD, id);
36 if (e == nullptr) return CIR_INVALID_ID; // No engine could be allocated, so neither can any next vehicles
37
38 EngineInfo *ei = &e->info;
39 RoadVehicleInfo *rvi = &e->u.road;
40
41 switch (prop) {
42 case 0x05: // Road/tram type
43 /* RoadTypeLabel is looked up later after the engine's road/tram
44 * flag is set, however 0 means the value has not been set. */
45 _gted[e->index].roadtramtype = buf.ReadByte() + 1;
46 break;
47
48 case 0x08: // Speed (1 unit is 0.5 kmh)
49 rvi->max_speed = buf.ReadByte();
50 break;
51
52 case PROP_ROADVEH_RUNNING_COST_FACTOR: // 0x09 Running cost factor
53 rvi->running_cost = buf.ReadByte();
54 break;
55
56 case 0x0A: // Running cost base
57 ConvertTTDBasePrice(buf.ReadDWord(), "RoadVehicleChangeInfo", &rvi->running_cost_class);
58 break;
59
60 case 0x0E: { // Sprite ID
61 uint8_t spriteid = buf.ReadByte();
62 uint8_t orig_spriteid = spriteid;
63
64 /* cars have different custom id in the GRF file */
65 if (spriteid == 0xFF) spriteid = 0xFD;
66
67 if (spriteid < 0xFD) spriteid >>= 1;
68
69 if (IsValidNewGRFImageIndex<VEH_ROAD>(spriteid)) {
70 rvi->image_index = spriteid;
71 } else {
72 GrfMsg(1, "RoadVehicleChangeInfo: Invalid Sprite {} specified, ignoring", orig_spriteid);
73 rvi->image_index = 0;
74 }
75 break;
76 }
77
78 case PROP_ROADVEH_CARGO_CAPACITY: // 0x0F Cargo capacity
79 rvi->capacity = buf.ReadByte();
80 break;
81
82 case 0x10: { // Cargo type
83 _gted[e->index].defaultcargo_grf = _cur.grffile;
84 uint8_t ctype = buf.ReadByte();
85
86 if (ctype == 0xFF) {
87 /* 0xFF is specified as 'use first refittable' */
88 ei->cargo_type = INVALID_CARGO;
89 } else {
90 /* Use translated cargo. Might result in INVALID_CARGO (first refittable), if cargo is not defined. */
91 ei->cargo_type = GetCargoTranslation(ctype, _cur.grffile);
92 if (ei->cargo_type == INVALID_CARGO) GrfMsg(2, "RoadVehicleChangeInfo: Invalid cargo type {}, using first refittable", ctype);
93 }
94 ei->cargo_label = CT_INVALID;
95 break;
96 }
97
98 case PROP_ROADVEH_COST_FACTOR: // 0x11 Cost factor
99 rvi->cost_factor = buf.ReadByte();
100 break;
101
102 case 0x12: // SFX
103 rvi->sfx = GetNewGRFSoundID(_cur.grffile, buf.ReadByte());
104 break;
105
106 case PROP_ROADVEH_POWER: // Power in units of 10 HP.
107 rvi->power = buf.ReadByte();
108 break;
109
110 case PROP_ROADVEH_WEIGHT: // Weight in units of 1/4 tons.
111 rvi->weight = buf.ReadByte();
112 break;
113
114 case PROP_ROADVEH_SPEED: // Speed in mph/0.8
115 _gted[e->index].rv_max_speed = buf.ReadByte();
116 break;
117
118 case 0x16: { // Cargoes available for refitting
119 uint32_t mask = buf.ReadDWord();
120 _gted[e->index].UpdateRefittability(mask != 0);
121 ei->refit_mask = TranslateRefitMask(mask);
122 _gted[e->index].defaultcargo_grf = _cur.grffile;
123 break;
124 }
125
126 case 0x17: { // Callback mask
127 auto mask = ei->callback_mask.base();
128 SB(mask, 0, 8, buf.ReadByte());
130 break;
131 }
132
133 case PROP_ROADVEH_TRACTIVE_EFFORT: // Tractive effort coefficient in 1/256.
134 rvi->tractive_effort = buf.ReadByte();
135 break;
136
137 case 0x19: // Air drag
138 rvi->air_drag = buf.ReadByte();
139 break;
140
141 case 0x1A: // Refit cost
142 ei->refit_cost = buf.ReadByte();
143 break;
144
145 case 0x1B: // Retire vehicle early
146 ei->retire_early = buf.ReadByte();
147 break;
148
149 case 0x1C: // Miscellaneous flags
150 ei->misc_flags = static_cast<EngineMiscFlags>(buf.ReadByte());
152 break;
153
154 case 0x1D: // Cargo classes allowed
155 _gted[e->index].cargo_allowed = CargoClasses{buf.ReadWord()};
156 _gted[e->index].UpdateRefittability(_gted[e->index].cargo_allowed.Any());
157 _gted[e->index].defaultcargo_grf = _cur.grffile;
158 break;
159
160 case 0x1E: // Cargo classes disallowed
161 _gted[e->index].cargo_disallowed = CargoClasses{buf.ReadWord()};
162 _gted[e->index].UpdateRefittability(false);
163 break;
164
165 case 0x1F: // Long format introduction date (days since year 0)
166 ei->base_intro = TimerGameCalendar::Date(buf.ReadDWord());
167 break;
168
169 case 0x20: // Alter purchase list sort order
171 break;
172
173 case 0x21: // Visual effect
174 rvi->visual_effect = buf.ReadByte();
175 /* Avoid accidentally setting visual_effect to the default value
176 * Since bit 6 (disable effects) is set anyways, we can safely erase some bits. */
177 if (rvi->visual_effect == VE_DEFAULT) {
180 }
181 break;
182
183 case PROP_ROADVEH_CARGO_AGE_PERIOD: // 0x22 Cargo aging period
184 ei->cargo_age_period = buf.ReadWord();
185 break;
186
187 case PROP_ROADVEH_SHORTEN_FACTOR: // 0x23 Shorter vehicle
188 rvi->shorten_factor = buf.ReadByte();
189 break;
190
191 case 0x24: // CTT refit include list
192 case 0x25: { // CTT refit exclude list
193 uint8_t count = buf.ReadByte();
194 _gted[e->index].UpdateRefittability(prop == 0x24 && count != 0);
195 if (prop == 0x24) _gted[e->index].defaultcargo_grf = _cur.grffile;
196 CargoTypes &ctt = prop == 0x24 ? _gted[e->index].ctt_include_mask : _gted[e->index].ctt_exclude_mask;
197 ctt = 0;
198 while (count--) {
199 CargoType ctype = GetCargoTranslation(buf.ReadByte(), _cur.grffile);
200 if (IsValidCargoType(ctype)) SetBit(ctt, ctype);
201 }
202 break;
203 }
204
205 case 0x26: // Engine variant
206 ei->variant_id = static_cast<EngineID>(buf.ReadWord());
207 break;
208
209 case 0x27: // Extra miscellaneous flags
210 ei->extra_flags = static_cast<ExtraEngineFlags>(buf.ReadDWord());
211 break;
212
213 case 0x28: { // Callback additional mask
214 auto mask = ei->callback_mask.base();
215 SB(mask, 8, 8, buf.ReadByte());
217 break;
218 }
219
220 case 0x29: // Cargo classes required for a refit.
221 _gted[e->index].cargo_allowed_required = CargoClasses{buf.ReadWord()};
222 break;
223
224 case 0x2A: // Badge list
225 e->badges = ReadBadgeList(buf, GSF_ROADVEHICLES);
226 break;
227
228 default:
229 ret = CommonVehicleChangeInfo(ei, prop, buf);
230 break;
231 }
232 }
233
234 return ret;
235}
236
238template <> ChangeInfoResult GrfChangeInfoHandler<GSF_ROADVEHICLES>::Activation(uint first, uint last, int prop, ByteReader &buf) { return RoadVehicleChangeInfo(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.
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.
static ChangeInfoResult RoadVehicleChangeInfo(uint first, uint last, int prop, ByteReader &buf)
Define properties for road 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_ROADVEH_CARGO_AGE_PERIOD
Number of ticks before carried cargo is aged.
@ PROP_ROADVEH_WEIGHT
Weight in 1/4 t.
@ PROP_ROADVEH_RUNNING_COST_FACTOR
Yearly runningcost.
@ PROP_ROADVEH_TRACTIVE_EFFORT
Tractive effort coefficient in 1/256.
@ PROP_ROADVEH_CARGO_CAPACITY
Capacity.
@ PROP_ROADVEH_COST_FACTOR
Purchase cost.
@ PROP_ROADVEH_SHORTEN_FACTOR
Shorter vehicles.
@ PROP_ROADVEH_POWER
Power in 10 HP.
@ PROP_ROADVEH_SPEED
Max. speed: 1 unit = 1/0.8 mph = 2 km-ish/h.
SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
Resolve NewGRF sound ID.
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.
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 road vehicle.
uint8_t tractive_effort
Coefficient of tractive effort.
uint16_t max_speed
Maximum speed (1 unit = 1/3.2 mph = 0.5 km-ish/h)
uint8_t air_drag
Coefficient of air drag.
uint8_t power
Power in 10hp units.
uint8_t weight
Weight in 1/4t units.
uint8_t shorten_factor
length on main map for this type is 8 - shorten_factor
uint8_t visual_effect
Bitstuffed NewGRF visual effect data.
@ 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_ROAD
Road vehicle type.