OpenTTD Source: src/newgrf/newgrf_act0_roadvehs.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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"
17#include "newgrf_internal_vehicle.h"
18#include "newgrf_internal.h"
19
20#include "../safeguards.h"
21
30 static ChangeInfoResult RoadVehicleChangeInfo(uint first, uint last, int prop, ByteReader &buf)
31{
32 ChangeInfoResult ret = CIR_SUCCESS;
33
34 for (uint id = first; id < last; ++id) {
35 Engine *e = GetNewEngine(_cur_gps.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->VehInfo<RoadVehicleInfo>();
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 = CUSTOM_VEHICLE_SPRITENUM;
66
67 if (spriteid < CUSTOM_VEHICLE_SPRITENUM) 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_gps.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_gps.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_gps.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_gps.grffile;
123 break;
124 }
125
126 case 0x17: { // Callback mask
127 auto mask = ei->callback_mask.base();
128 SB(mask, 0, 8, buf.ReadByte());
129 ei->callback_mask = VehicleCallbackMasks{mask};
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());
151 _loaded_newgrf_features.has_2CC |= ei->misc_flags.Test(EngineMiscFlag::Uses2CC);
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_gps.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
170 AlterVehicleListOrder(e->index, buf.ReadExtendedByte());
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) {
178 assert(HasBit(rvi->visual_effect, VE_DISABLE_EFFECT));
179 SB(rvi->visual_effect, VE_TYPE_START, VE_TYPE_COUNT, 0);
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_gps.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_gps.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());
216 ei->callback_mask = VehicleCallbackMasks{mask};
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
237template <> ChangeInfoResult GrfChangeInfoHandler<GSF_ROADVEHICLES>::Reserve(uint, uint, int, ByteReader &) { return CIR_UNHANDLED; }
238template <> ChangeInfoResult GrfChangeInfoHandler<GSF_ROADVEHICLES>::Activation(uint first, uint last, int prop, ByteReader &buf) { return RoadVehicleChangeInfo(first, last, prop, buf); }
SB
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.
Definition bitmath_func.hpp:58
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition bitmath_func.hpp:121
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
CT_INVALID
static constexpr CargoLabel CT_INVALID
Invalid cargo type.
Definition cargo_type.h:70
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
IsValidCargoType
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:104
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::base
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
Definition base_bitset_type.hpp:219
ByteReader
Class to read from a NewGRF file.
Definition newgrf_bytereader.h:18
ByteReader::ReadDWord
uint32_t ReadDWord()
Read a single DWord (32 bits).
Definition newgrf_bytereader.h:66
ByteReader::ReadWord
uint16_t ReadWord()
Read a single Word (16 bits).
Definition newgrf_bytereader.h:45
ByteReader::ReadExtendedByte
uint16_t ReadExtendedByte()
Read a single Extended Byte (8 or 16 bits).
Definition newgrf_bytereader.h:56
ByteReader::ReadByte
uint8_t ReadByte()
Read a single byte (8 bits).
Definition newgrf_bytereader.h:34
Engine
Definition engine_base.h:38
EnumBitSet< VehicleCallbackMask, uint16_t >
EngineMiscFlag::Uses2CC
@ Uses2CC
Vehicle uses two company colours.
VE_TYPE_COUNT
@ VE_TYPE_COUNT
Number of bits used for the effect type.
Definition engine_type.h:60
VE_TYPE_START
@ VE_TYPE_START
First bit used for the type of effect.
Definition engine_type.h:59
VE_DISABLE_EFFECT
@ VE_DISABLE_EFFECT
Flag to disable visual effect.
Definition engine_type.h:66
VE_DEFAULT
@ VE_DEFAULT
Default value to indicate that visual effect should be based on engine class.
Definition engine_type.h:70
ConvertTTDBasePrice
void ConvertTTDBasePrice(uint32_t base_pointer, std::string_view error_location, Price *index)
Converts TTD(P) Base Price pointers into the enum used by OTTD See http://wiki.ttdpatch....
Definition newgrf.cpp:332
GetNewEngine
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:214
_gted
TypedIndexContainer< std::vector< GRFTempEngineData >, EngineID > _gted
Temporary engine data used during NewGRF loading.
Definition newgrf.cpp:78
_loaded_newgrf_features
GRFLoadedFeatures _loaded_newgrf_features
Indicates which are the newgrf features currently loaded ingame.
Definition newgrf.cpp:74
TranslateRefitMask
CargoTypes TranslateRefitMask(uint32_t refit_mask)
Translate the refit mask.
Definition newgrf.cpp:315
CommonVehicleChangeInfo
ChangeInfoResult CommonVehicleChangeInfo(EngineInfo *ei, int prop, ByteReader &buf)
Define properties common to all vehicles.
Definition newgrf_act0.cpp:33
ReadBadgeList
std::vector< BadgeID > ReadBadgeList(ByteReader &buf, GrfSpecFeature feature)
Read a list of badges.
Definition newgrf_act0.cpp:86
RoadVehicleChangeInfo
static ChangeInfoResult RoadVehicleChangeInfo(uint first, uint last, int prop, ByteReader &buf)
Define properties for road vehicles.
Definition newgrf_act0_roadvehs.cpp:30
newgrf_bytereader.h
NewGRF buffer reader definition.
GetCargoTranslation
CargoType GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoType.
Definition newgrf_cargo.cpp:80
AlterVehicleListOrder
void AlterVehicleListOrder(EngineID engine, uint16_t target)
Record a vehicle ListOrderChange.
Definition newgrf_engine.cpp:1345
newgrf_internal.h
NewGRF internal processing state.
ChangeInfoResult
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
Definition newgrf_internal.h:20
CIR_INVALID_ID
@ CIR_INVALID_ID
Attempt to modify an invalid ID.
Definition newgrf_internal.h:25
CIR_UNHANDLED
@ CIR_UNHANDLED
Variable was parsed but unread.
Definition newgrf_internal.h:23
CIR_SUCCESS
@ CIR_SUCCESS
Variable was parsed and read.
Definition newgrf_internal.h:21
newgrf_internal_vehicle.h
NewGRF internal processing state for vehicles.
PROP_ROADVEH_CARGO_AGE_PERIOD
@ PROP_ROADVEH_CARGO_AGE_PERIOD
Number of ticks before carried cargo is aged.
Definition newgrf_properties.h:40
PROP_ROADVEH_WEIGHT
@ PROP_ROADVEH_WEIGHT
Weight in 1/4 t.
Definition newgrf_properties.h:37
PROP_ROADVEH_RUNNING_COST_FACTOR
@ PROP_ROADVEH_RUNNING_COST_FACTOR
Yearly runningcost.
Definition newgrf_properties.h:33
PROP_ROADVEH_TRACTIVE_EFFORT
@ PROP_ROADVEH_TRACTIVE_EFFORT
Tractive effort coefficient in 1/256.
Definition newgrf_properties.h:39
PROP_ROADVEH_CARGO_CAPACITY
@ PROP_ROADVEH_CARGO_CAPACITY
Capacity.
Definition newgrf_properties.h:34
PROP_ROADVEH_COST_FACTOR
@ PROP_ROADVEH_COST_FACTOR
Purchase cost.
Definition newgrf_properties.h:35
PROP_ROADVEH_SHORTEN_FACTOR
@ PROP_ROADVEH_SHORTEN_FACTOR
Shorter vehicles.
Definition newgrf_properties.h:41
PROP_ROADVEH_POWER
@ PROP_ROADVEH_POWER
Power in 10 HP.
Definition newgrf_properties.h:36
PROP_ROADVEH_SPEED
@ PROP_ROADVEH_SPEED
Max. speed: 1 unit = 1/0.8 mph = 2 km-ish/h.
Definition newgrf_properties.h:38
GetNewGRFSoundID
SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
Resolve NewGRF sound ID.
Definition newgrf_sound.cpp:125
EngineInfo
Information about a vehicle.
Definition engine_type.h:198
EngineInfo::cargo_age_period
uint16_t cargo_age_period
Number of ticks before carried cargo is aged.
Definition engine_type.h:214
EngineInfo::misc_flags
EngineMiscFlags misc_flags
Miscellaneous flags.
Definition engine_type.h:209
EngineInfo::variant_id
EngineID variant_id
Engine variant ID. If set, will be treated specially in purchase lists.
Definition engine_type.h:215
EngineInfo::callback_mask
VehicleCallbackMasks callback_mask
Bitmask of vehicle callbacks that have to be called.
Definition engine_type.h:210
EngineInfo::base_intro
TimerGameCalendar::Date base_intro
Basic date of engine introduction (without random parts).
Definition engine_type.h:199
EngineInfo::retire_early
int8_t retire_early
Number of years early to retire vehicle.
Definition engine_type.h:211
GRFLoadedFeatures::has_2CC
bool has_2CC
Set if any vehicle is loaded which uses 2cc (two company colours).
Definition newgrf.h:189
GrfChangeInfoHandler
GRF feature handler.
Definition newgrf_internal.h:30
GrfProcessingState::grffile
GRFFile * grffile
Currently processed GRF file.
Definition newgrf_internal.h:67
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF >
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
RoadVehicleInfo
Information about a road vehicle.
Definition engine_type.h:146
RoadVehicleInfo::tractive_effort
uint8_t tractive_effort
Coefficient of tractive effort.
Definition engine_type.h:156
RoadVehicleInfo::max_speed
uint16_t max_speed
Maximum speed (1 unit = 1/3.2 mph = 0.5 km-ish/h)
Definition engine_type.h:152
RoadVehicleInfo::air_drag
uint8_t air_drag
Coefficient of air drag.
Definition engine_type.h:157
RoadVehicleInfo::power
uint8_t power
Power in 10hp units.
Definition engine_type.h:155
RoadVehicleInfo::weight
uint8_t weight
Weight in 1/4t units.
Definition engine_type.h:154
RoadVehicleInfo::shorten_factor
uint8_t shorten_factor
length on main map for this type is 8 - shorten_factor
Definition engine_type.h:159
RoadVehicleInfo::visual_effect
uint8_t visual_effect
Bitstuffed NewGRF visual effect data.
Definition engine_type.h:158
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:130
CUSTOM_VEHICLE_SPRITENUM
@ CUSTOM_VEHICLE_SPRITENUM
Vehicle sprite from NewGRF.
Definition vehicle_func.h:28
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition vehicle_type.h:26