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

OpenTTD Source 15.3

newgrf_act0_aircraft.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 "newgrf_bytereader.h"
16#include "newgrf_internal_vehicle.h"
17#include "newgrf_internal.h"
18
19#include "../safeguards.h"
20
29 static ChangeInfoResult AircraftVehicleChangeInfo(uint first, uint last, int prop, ByteReader &buf)
30{
31 ChangeInfoResult ret = CIR_SUCCESS;
32
33 for (uint id = first; id < last; ++id) {
34 Engine *e = GetNewEngine(_cur_gps.grffile, VEH_AIRCRAFT, 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 AircraftVehicleInfo *avi = &e->VehInfo<AircraftVehicleInfo>();
39
40 switch (prop) {
41 case 0x08: { // Sprite ID
42 uint8_t spriteid = buf.ReadByte();
43 uint8_t orig_spriteid = spriteid;
44
45 /* aircraft have different custom id in the GRF file */
46 if (spriteid == 0xFF) spriteid = CUSTOM_VEHICLE_SPRITENUM;
47
48 if (spriteid < CUSTOM_VEHICLE_SPRITENUM) spriteid >>= 1;
49
50 if (IsValidNewGRFImageIndex<VEH_AIRCRAFT>(spriteid)) {
51 avi->image_index = spriteid;
52 } else {
53 GrfMsg(1, "AircraftVehicleChangeInfo: Invalid Sprite {} specified, ignoring", orig_spriteid);
54 avi->image_index = 0;
55 }
56 break;
57 }
58
59 case 0x09: // Helicopter
60 if (buf.ReadByte() == 0) {
61 avi->subtype = AIR_HELI;
62 } else {
63 SB(avi->subtype, 0, 1, 1); // AIR_CTOL
64 }
65 break;
66
67 case 0x0A: // Large
68 AssignBit(avi->subtype, 1, buf.ReadByte() != 0); // AIR_FAST
69 break;
70
71 case PROP_AIRCRAFT_COST_FACTOR: // 0x0B Cost factor
72 avi->cost_factor = buf.ReadByte();
73 break;
74
75 case PROP_AIRCRAFT_SPEED: // 0x0C Speed (1 unit is 8 mph, we translate to 1 unit is 1 km-ish/h)
76 avi->max_speed = (buf.ReadByte() * 128) / 10;
77 break;
78
79 case 0x0D: // Acceleration
80 avi->acceleration = buf.ReadByte();
81 break;
82
83 case PROP_AIRCRAFT_RUNNING_COST_FACTOR: // 0x0E Running cost factor
84 avi->running_cost = buf.ReadByte();
85 break;
86
87 case PROP_AIRCRAFT_PASSENGER_CAPACITY: // 0x0F Passenger capacity
88 avi->passenger_capacity = buf.ReadWord();
89 break;
90
91 case PROP_AIRCRAFT_MAIL_CAPACITY: // 0x11 Mail capacity
92 avi->mail_capacity = buf.ReadByte();
93 break;
94
95 case 0x12: // SFX
96 avi->sfx = GetNewGRFSoundID(_cur_gps.grffile, buf.ReadByte());
97 break;
98
99 case 0x13: { // Cargoes available for refitting
100 uint32_t mask = buf.ReadDWord();
101 _gted[e->index].UpdateRefittability(mask != 0);
102 ei->refit_mask = TranslateRefitMask(mask);
103 _gted[e->index].defaultcargo_grf = _cur_gps.grffile;
104 break;
105 }
106
107 case 0x14: { // Callback mask
108 auto mask = ei->callback_mask.base();
109 SB(mask, 0, 8, buf.ReadByte());
110 ei->callback_mask = VehicleCallbackMasks{mask};
111 break;
112 }
113
114 case 0x15: // Refit cost
115 ei->refit_cost = buf.ReadByte();
116 break;
117
118 case 0x16: // Retire vehicle early
119 ei->retire_early = buf.ReadByte();
120 break;
121
122 case 0x17: // Miscellaneous flags
123 ei->misc_flags = static_cast<EngineMiscFlags>(buf.ReadByte());
124 _loaded_newgrf_features.has_2CC |= ei->misc_flags.Test(EngineMiscFlag::Uses2CC);
125 break;
126
127 case 0x18: // Cargo classes allowed
128 _gted[e->index].cargo_allowed = CargoClasses{buf.ReadWord()};
129 _gted[e->index].UpdateRefittability(_gted[e->index].cargo_allowed.Any());
130 _gted[e->index].defaultcargo_grf = _cur_gps.grffile;
131 break;
132
133 case 0x19: // Cargo classes disallowed
134 _gted[e->index].cargo_disallowed = CargoClasses{buf.ReadWord()};
135 _gted[e->index].UpdateRefittability(false);
136 break;
137
138 case 0x1A: // Long format introduction date (days since year 0)
139 ei->base_intro = TimerGameCalendar::Date(buf.ReadDWord());
140 break;
141
142 case 0x1B: // Alter purchase list sort order
143 AlterVehicleListOrder(e->index, buf.ReadExtendedByte());
144 break;
145
146 case PROP_AIRCRAFT_CARGO_AGE_PERIOD: // 0x1C Cargo aging period
147 ei->cargo_age_period = buf.ReadWord();
148 break;
149
150 case 0x1D: // CTT refit include list
151 case 0x1E: { // CTT refit exclude list
152 uint8_t count = buf.ReadByte();
153 _gted[e->index].UpdateRefittability(prop == 0x1D && count != 0);
154 if (prop == 0x1D) _gted[e->index].defaultcargo_grf = _cur_gps.grffile;
155 CargoTypes &ctt = prop == 0x1D ? _gted[e->index].ctt_include_mask : _gted[e->index].ctt_exclude_mask;
156 ctt = 0;
157 while (count--) {
158 CargoType ctype = GetCargoTranslation(buf.ReadByte(), _cur_gps.grffile);
159 if (IsValidCargoType(ctype)) SetBit(ctt, ctype);
160 }
161 break;
162 }
163
164 case PROP_AIRCRAFT_RANGE: // 0x1F Max aircraft range
165 avi->max_range = buf.ReadWord();
166 break;
167
168 case 0x20: // Engine variant
169 ei->variant_id = static_cast<EngineID>(buf.ReadWord());
170 break;
171
172 case 0x21: // Extra miscellaneous flags
173 ei->extra_flags = static_cast<ExtraEngineFlags>(buf.ReadDWord());
174 break;
175
176 case 0x22: { // Callback additional mask
177 auto mask = ei->callback_mask.base();
178 SB(mask, 8, 8, buf.ReadByte());
179 ei->callback_mask = VehicleCallbackMasks{mask};
180 break;
181 }
182
183 case 0x23: // Cargo classes required for a refit.
184 _gted[e->index].cargo_allowed_required = CargoClasses{buf.ReadWord()};
185 break;
186
187 case 0x24: // Badge list
188 e->badges = ReadBadgeList(buf, GSF_AIRCRAFT);
189 break;
190
191 default:
192 ret = CommonVehicleChangeInfo(ei, prop, buf);
193 break;
194 }
195 }
196
197 return ret;
198}
199
200template <> ChangeInfoResult GrfChangeInfoHandler<GSF_AIRCRAFT>::Reserve(uint, uint, int, ByteReader &) { return CIR_UNHANDLED; }
201template <> ChangeInfoResult GrfChangeInfoHandler<GSF_AIRCRAFT>::Activation(uint first, uint last, int prop, ByteReader &buf) { return AircraftVehicleChangeInfo(first, last, prop, buf); }
AssignBit
constexpr T AssignBit(T &x, const uint8_t y, bool value)
Assigns a bit in a variable.
Definition bitmath_func.hpp:176
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
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.
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
AircraftVehicleChangeInfo
static ChangeInfoResult AircraftVehicleChangeInfo(uint first, uint last, int prop, ByteReader &buf)
Define properties for aircraft.
Definition newgrf_act0_aircraft.cpp:29
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_AIRCRAFT_PASSENGER_CAPACITY
@ PROP_AIRCRAFT_PASSENGER_CAPACITY
Passenger Capacity.
Definition newgrf_properties.h:52
PROP_AIRCRAFT_RANGE
@ PROP_AIRCRAFT_RANGE
Aircraft range.
Definition newgrf_properties.h:55
PROP_AIRCRAFT_RUNNING_COST_FACTOR
@ PROP_AIRCRAFT_RUNNING_COST_FACTOR
Yearly runningcost.
Definition newgrf_properties.h:51
PROP_AIRCRAFT_SPEED
@ PROP_AIRCRAFT_SPEED
Max. speed: 1 unit = 8 mph = 12.8 km-ish/h.
Definition newgrf_properties.h:50
PROP_AIRCRAFT_MAIL_CAPACITY
@ PROP_AIRCRAFT_MAIL_CAPACITY
Mail Capacity.
Definition newgrf_properties.h:53
PROP_AIRCRAFT_COST_FACTOR
@ PROP_AIRCRAFT_COST_FACTOR
Purchase cost.
Definition newgrf_properties.h:49
PROP_AIRCRAFT_CARGO_AGE_PERIOD
@ PROP_AIRCRAFT_CARGO_AGE_PERIOD
Number of ticks before carried cargo is aged.
Definition newgrf_properties.h:54
GetNewGRFSoundID
SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
Resolve NewGRF sound ID.
Definition newgrf_sound.cpp:125
AircraftVehicleInfo
Information about a aircraft vehicle.
Definition engine_type.h:132
AircraftVehicleInfo::max_speed
uint16_t max_speed
Maximum speed (1 unit = 8 mph = 12.8 km-ish/h)
Definition engine_type.h:138
AircraftVehicleInfo::mail_capacity
uint8_t mail_capacity
Mail capacity (bags).
Definition engine_type.h:140
AircraftVehicleInfo::subtype
uint8_t subtype
Type of aircraft.
Definition engine_type.h:136
AircraftVehicleInfo::passenger_capacity
uint16_t passenger_capacity
Passenger capacity (persons).
Definition engine_type.h:141
AircraftVehicleInfo::max_range
uint16_t max_range
Maximum range of this aircraft.
Definition engine_type.h:142
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
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_AIRCRAFT
@ VEH_AIRCRAFT
Aircraft vehicle type.
Definition vehicle_type.h:28