OpenTTD Source 20261123-master-g1c55c72fd0
newgrf.h
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#ifndef NEWGRF_H
11#define NEWGRF_H
12
13#include "cargo_type.h"
14#include "economy_type.h"
15#include "strings_type.h"
16#include "debug_type.h"
17#include "livery.h"
18#include "rail_type.h"
19#include "road_type.h"
20#include "fileio_type.h"
21#include "newgrf_badge_type.h"
22#include "newgrf_callbacks.h"
23#include "newgrf_text_type.h"
24#include "vehicle_type.h"
25
42
44enum class CanalFeatureFlag : uint8_t {
46};
47
49
55
66
77
80
114
117
119struct GRFLabel {
120 uint8_t label;
121 uint32_t nfo_line;
122 size_t pos;
123};
124
126struct GRFFile {
127 std::string filename{};
129 uint8_t grf_version = 0;
130
131 uint sound_offset = 0;
132 uint16_t num_sounds = 0;
133
134 std::vector<std::unique_ptr<struct StationSpec>> stations;
135 std::vector<std::unique_ptr<struct HouseSpec>> housespec;
136 std::vector<std::unique_ptr<struct IndustrySpec>> industryspec;
137 std::vector<std::unique_ptr<struct IndustryTileSpec>> indtspec;
138 std::vector<std::unique_ptr<struct ObjectSpec>> objectspec;
139 std::vector<std::unique_ptr<struct AirportSpec>> airportspec;
140 std::vector<std::unique_ptr<struct AirportTileSpec>> airtspec;
141 std::vector<std::unique_ptr<struct RoadStopSpec>> roadstops;
142
143 std::vector<uint32_t> param{};
144
145 std::vector<GRFLabel> labels{};
146
147 std::vector<CargoLabel> cargo_list{};
148 std::array<uint8_t, NUM_CARGO> cargo_map{};
149
150 std::vector<BadgeID> badge_list{};
151 std::unordered_map<uint16_t, BadgeID> badge_map{};
152
153 std::vector<RailTypeLabel> railtype_list{};
154 std::array<RailType, RAILTYPE_END> railtype_map{};
155
156 std::vector<RoadTypeLabel> roadtype_list{};
157 std::array<RoadType, ROADTYPE_END> roadtype_map{};
158
159 std::vector<RoadTypeLabel> tramtype_list{};
160 std::array<RoadType, ROADTYPE_END> tramtype_map{};
161
163
164 std::unordered_map<GRFLanguage, LanguageMap> language_map{};
165
169
171 PriceMultipliers price_base_multipliers{};
172
173 GRFFile(const GRFConfig &config);
174 GRFFile();
175 GRFFile(GRFFile &&other);
176 ~GRFFile();
177
183 uint32_t GetParam(uint number) const
184 {
185 /* Note: We implicitly test for number < this->param.size() and return 0 for invalid parameters.
186 * In fact this is the more important test, as param is zeroed anyway. */
187 return (number < std::size(this->param)) ? this->param[number] : 0;
188 }
189};
190
198
205
213
223
229inline bool HasGrfMiscBit(GrfMiscBit bit)
230{
232 return _misc_grf_features.Test(bit);
233}
234
235/* Indicates which are the newgrf features currently loaded ingame */
237
238void LoadNewGRFFile(GRFConfig &config, GrfLoadingStage stage, Subdirectory subdir, bool temporary);
239void LoadNewGRF(SpriteID load_index, uint num_baseset);
240void ReloadNewGRFData(); // in saveload/afterload.cpp
241void ResetNewGRFData();
244
245void GrfMsgI(Severity severity, const std::string &msg);
246#define GrfMsg(severity, format_string, ...) do { if ((severity) == Severity::Critical || IsVisibleSeverity(Facility::Grf, (severity))) GrfMsgI(severity, fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__)); } while (false)
247
248bool GetGlobalVariable(uint8_t param, uint32_t *value, const GRFFile *grffile);
249
251void ShowNewGRFError();
252
255
261template <typename T> requires std::is_base_of_v<BaseLabel, T>
262constexpr uint32_t FlattenNewGRFLabel(T label) { return label[0] | label[1] << 8 | label[2] << 16 | label[3] << 24; }
263
269template <typename T> requires std::is_base_of_v<BaseLabel, T>
270constexpr T UnflattenNewGRFLabel(uint32_t value)
271{
272 uint8_t buf[]{
273 static_cast<uint8_t>(GB(value, 0, 8)),
274 static_cast<uint8_t>(GB(value, 8, 8)),
275 static_cast<uint8_t>(GB(value, 16, 8)),
276 static_cast<uint8_t>(GB(value, 24, 8))
277 };
278 return buf;
279}
280
281#endif /* NEWGRF_H */
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Types related to cargoes...
Global state for badge definitions.
Enum-as-bit-set wrapper.
Types related to debugging.
Severity
Debug message severity levels.
Definition debug_type.h:14
Types related to the economy.
PriceCategory
Categories of a price bases.
Price
Enumeration of all base prices for use with Prices.
#define T
Climate temperate.
Definition engines.h:91
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
Types for standard in/out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:87
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
Functions/types related to livery colours.
EnumBitSet< LiveryScheme, uint32_t, LiveryScheme::End > LiverySchemes
Bitset of LiveryScheme elements.
Definition livery.h:64
GRFLoadedFeatures _loaded_newgrf_features
Indicates which are the newgrf features currently loaded ingame.
Definition newgrf.cpp:75
GrfMiscBits _misc_grf_features
Miscellaneous GRF features, set by Action 0x0D, parameter 0x9E.
Definition newgrf.cpp:72
bool GetGlobalVariable(uint8_t param, uint32_t *value, const GRFFile *grffile)
Reads a variable common to VarAction2 and Action7/9/D.
constexpr uint32_t FlattenNewGRFLabel(T label)
Flatten a NewGRF related label to a 32 bits integer.
Definition newgrf.h:262
EnumBitSet< CanalFeatureFlag, uint8_t > CanalFeatureFlags
CanalFeatureFlag bitmask.
Definition newgrf.h:48
GrfLoadingStage
Stages of loading all NewGRFs.
Definition newgrf.h:57
@ SafetyScan
Checks whether the NewGRF can be used in a static context.
Definition newgrf.h:59
@ Reserve
Third step of NewGRF loading; reserve features and GRMs.
Definition newgrf.h:62
@ Init
Second step of NewGRF loading; load all actions into memory.
Definition newgrf.h:61
@ LabelScan
First step of NewGRF loading; find the 'goto' labels in the NewGRF.
Definition newgrf.h:60
@ Activation
Forth step of NewGRF loading; activate the features.
Definition newgrf.h:63
@ FileScan
Load the Action 8 metadata (GRF ID, name).
Definition newgrf.h:58
void ResetPersistentNewGRFData()
Reset NewGRF data which is stored persistently in savegames.
Definition newgrf.cpp:511
constexpr T UnflattenNewGRFLabel(uint32_t value)
Unflatten a NewGRF related label from a 32 bits integer.
Definition newgrf.h:270
ShoreReplacement
Type of shore replacement loaded by NewGRFs.
Definition newgrf.h:192
@ None
No shore sprites were replaced.
Definition newgrf.h:193
@ Action5
Shore sprites were replaced by Action5.
Definition newgrf.h:194
@ ActionA
Shore sprites were replaced by ActionA (using grass tiles for the corner-shores).
Definition newgrf.h:195
@ OnlyNew
Only corner-shores were loaded by Action5 (openttd(w/d).grf only).
Definition newgrf.h:196
bool HasGrfMiscBit(GrfMiscBit bit)
Check for grf miscellaneous bits.
Definition newgrf.h:229
CanalFeature
List of different canal 'features'.
Definition newgrf.h:30
@ RiverEdge
The river banks.
Definition newgrf.h:37
@ Locks
The sides of the lock.
Definition newgrf.h:32
@ Icon
Unused: the TTDP UI icon for canals.
Definition newgrf.h:34
@ End
End marker.
Definition newgrf.h:40
@ Buoy
Buoy without underlying water.
Definition newgrf.h:39
@ LockWaterSlope
The sloped water tiles in locks.
Definition newgrf.h:31
@ RiverSlope
The sloped water tiles for rivers.
Definition newgrf.h:36
@ RiverIcon
Unused: the TTDP UI icons for rivers.
Definition newgrf.h:38
@ Dikes
The canal dikes/embankment.
Definition newgrf.h:33
@ FlatDocks
Unused: the graphics for TTDP flat docks.
Definition newgrf.h:35
TramDepotReplacement
Type of tram depot replacement loaded by NewGRFs.
Definition newgrf.h:200
@ WithTrack
Electrified depot graphics with tram track were loaded.
Definition newgrf.h:202
@ WithoutTrack
Electrified depot graphics without tram track were loaded.
Definition newgrf.h:203
StringID MapGRFStringID(GrfID grfid, GRFStringID str)
Used when setting an object's property to map to the GRF's strings while taking in consideration the ...
void GrfMsgI(Severity severity, const std::string &msg)
Debug() function dedicated to newGRF debugging messages Function is essentially the same as Debug(Fac...
Definition newgrf.cpp:91
void LoadNewGRF(SpriteID load_index, uint num_baseset)
Load all the NewGRFs.
Definition newgrf.cpp:1760
EnumBitSet< GrfMiscBit, uint8_t > GrfMiscBits
Bitset of GrfMiscBit elements.
Definition newgrf.h:79
void ResetNewGRFData()
Reset all NewGRF loaded data.
Definition newgrf.cpp:413
GrfSpecFeature GetGrfSpecFeature(VehicleType type)
Get the GrfSpecFeature associated with a VehicleType.
Definition newgrf.cpp:1889
void ResetUnhandledVariableWarnings()
Reset unhandled variable warnings.
GrfSpecFeature
Definition newgrf.h:81
@ RoadStops
Road stops feature.
Definition newgrf.h:102
@ Trains
Trains feature.
Definition newgrf.h:82
@ FakeTowns
Fake town GrfSpecFeature for NewGRF debugging (parent scope).
Definition newgrf.h:107
@ SoundEffects
Sound effects feature.
Definition newgrf.h:94
@ Cargoes
Cargoes feature.
Definition newgrf.h:93
@ RoadVehicles
Road vehicles feature.
Definition newgrf.h:83
@ TramTypes
Tram types feature.
Definition newgrf.h:101
@ Ships
Ships feature.
Definition newgrf.h:84
@ Houses
Houses feature.
Definition newgrf.h:89
@ FakeEnd
End of the fake features.
Definition newgrf.h:108
@ OriginalStrings
Pseudo unsupported 'feature' for replacing original strings.
Definition newgrf.h:110
@ Default
Unspecified feature, default badge.
Definition newgrf.h:106
@ Stations
Stations feature.
Definition newgrf.h:86
@ Airports
Airports feature.
Definition newgrf.h:95
@ GlobalVar
Global variables feature.
Definition newgrf.h:90
@ Bridges
Bridges feature.
Definition newgrf.h:88
@ IndustryTiles
Industry tiles feature.
Definition newgrf.h:91
@ Objects
Objects feature.
Definition newgrf.h:97
@ Signals
Signals feature.
Definition newgrf.h:96
@ Canals
Canals feature.
Definition newgrf.h:87
@ Industries
Industries feature.
Definition newgrf.h:92
GrfMiscBit
Bits of NewGRF's GlobalVariable 1E/9E.
Definition newgrf.h:68
@ TrainWidth32Pixels
Use 32 pixels per train vehicle in depot gui and vehicle details. Never set in the global variable;.
Definition newgrf.h:72
@ SecondRockyTileSet
Enable using the second rocky tile set.
Definition newgrf.h:75
@ CatenaryOn3rdTrack
Unsupported: enable catenaries over third track type.
Definition newgrf.h:74
@ DesertTreesFields
Unsupported: allow trees and fields in desert climate.
Definition newgrf.h:69
@ AmbientSoundCallback
Enable ambient sound effect callback 144.
Definition newgrf.h:73
@ DesertPavedRoads
Show pavement and lights in desert towns.
Definition newgrf.h:70
@ FieldBoundingBox
Unsupported: fields have a height.
Definition newgrf.h:71
void LoadNewGRFFile(GRFConfig &config, GrfLoadingStage stage, Subdirectory subdir, bool temporary)
Load a particular NewGRF.
Definition newgrf.cpp:1394
void ShowNewGRFError()
Show the first NewGRF error we can find.
void ReloadNewGRFData()
Reload all NewGRF files during a running game.
VehicleType GetVehicleType(GrfSpecFeature feature)
Get the VehicleType associated with a GrfSpecFeature.
Definition newgrf.cpp:1905
EnumBitSet< GrfSpecFeature, uint32_t, GrfSpecFeature::End > GrfSpecFeatures
Bitset of GrfSpecFeature elements.
Definition newgrf.h:116
CanalFeatureFlag
Flags controlling the display of canals.
Definition newgrf.h:44
@ HasFlatSprite
Additional flat ground sprite in the beginning.
Definition newgrf.h:45
Types related to NewGRF badges.
Callbacks that NewGRFs could implement.
EnumBitSet< CanalCallbackMask, uint8_t > CanalCallbackMasks
Bitset of CanalCallbackMask elements.
Header of Action 04 "universal holder" structure.
StrongType::Typedef< uint32_t, struct GRFStringIDTag, StrongType::Compare, StrongType::Integer > GRFStringID
Type for GRF-internal string IDs.
Label< struct GrfIDTag > GrfID
The unique identifier of a NewGRF.
Definition newgrf_type.h:17
The different types of rail.
EnumBitSet< RailType, uint64_t > RailTypes
Bitset of RailType elements.
Definition rail_type.h:37
Enums and other types related to roads.
EnumBitSet< RoadType, uint64_t > RoadTypes
Bitset of RoadType elements.
Definition road_type.h:33
@ Invalid
broken savegame (used internally)
Definition saveload.h:438
Types related to strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
Aircraft, helicopters, rotors and their shadows belong to this class.
Definition aircraft.h:75
Canal properties local to the NewGRF.
Definition newgrf.h:51
CanalCallbackMasks callback_mask
Bitmask of canal callbacks that have to be called.
Definition newgrf.h:52
CanalFeatureFlags flags
Flags controlling display.
Definition newgrf.h:53
Information about GRF, used in the game and (part of it) in savegames.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:126
std::vector< uint32_t > param
Parameters, initialised by GRFConfig.
Definition newgrf.h:143
std::array< RoadType, ROADTYPE_END > roadtype_map
Mapping from local id to global road type id.
Definition newgrf.h:157
GrfID grfid
The GRF ID.
Definition newgrf.h:128
std::vector< std::unique_ptr< struct IndustrySpec > > industryspec
Industry definitions.
Definition newgrf.h:136
std::array< RailType, RAILTYPE_END > railtype_map
Mapping from local id to global rail type id.
Definition newgrf.h:154
std::array< uint8_t, NUM_CARGO > cargo_map
Inverse cargo translation table (CargoType -> local ID).
Definition newgrf.h:148
uint8_t grf_version
GRF version (See https://newgrf-specs.tt-wiki.net/wiki/Version_numbers#GRF_version).
Definition newgrf.h:129
std::vector< RailTypeLabel > railtype_list
Railtype translation table.
Definition newgrf.h:153
uint32_t GetParam(uint number) const
Get GRF Parameter with range checking.
Definition newgrf.h:183
std::vector< std::unique_ptr< struct RoadStopSpec > > roadstops
RoadStop definitions.
Definition newgrf.h:141
std::vector< RoadTypeLabel > roadtype_list
Roadtype translation table (road).
Definition newgrf.h:156
bool cargo_list_is_fallback
Set if cargo types have been created but a cargo list has not been installed.
Definition newgrf.h:168
std::unordered_map< uint16_t, BadgeID > badge_map
Mapping from local id to global badge id.
Definition newgrf.h:151
std::vector< std::unique_ptr< struct StationSpec > > stations
Station definitions.
Definition newgrf.h:134
std::vector< std::unique_ptr< struct HouseSpec > > housespec
House definitions.
Definition newgrf.h:135
std::vector< RoadTypeLabel > tramtype_list
Roadtype translation table (tram).
Definition newgrf.h:159
GRFFile(const GRFConfig &config)
Constructor for GRFFile.
Definition newgrf.cpp:577
std::vector< std::unique_ptr< struct ObjectSpec > > objectspec
Object definitions.
Definition newgrf.h:138
std::vector< CargoLabel > cargo_list
Cargo translation table (local ID -> label).
Definition newgrf.h:147
std::vector< std::unique_ptr< struct AirportTileSpec > > airtspec
Airport Tile definitions.
Definition newgrf.h:140
uint sound_offset
Index of first sound.
Definition newgrf.h:131
uint16_t num_sounds
Number of sounds.
Definition newgrf.h:132
std::unordered_map< GRFLanguage, LanguageMap > language_map
Mappings related to the languages.
Definition newgrf.h:164
std::array< RoadType, ROADTYPE_END > tramtype_map
Mapping from local id to global tram type id.
Definition newgrf.h:160
uint traininfo_vehicle_width
Width (in pixels) of a 8/8 train vehicle in depot GUI and vehicle details.
Definition newgrf.h:167
std::vector< BadgeID > badge_list
Badge translation table (local index -> global index).
Definition newgrf.h:150
GrfSpecFeatures grf_features
Bitset of GrfSpecFeature the grf uses.
Definition newgrf.h:170
EnumIndexArray< CanalProperties, CanalFeature, CanalFeature::End > canal_local_properties
Canal properties as set by this NewGRF.
Definition newgrf.h:162
std::string filename
The filename.
Definition newgrf.h:127
std::vector< std::unique_ptr< struct AirportSpec > > airportspec
Airport definitions.
Definition newgrf.h:139
std::vector< std::unique_ptr< struct IndustryTileSpec > > indtspec
Industry Tile definitions.
Definition newgrf.h:137
int traininfo_vehicle_pitch
Vertical offset for drawing train images in depot GUI and vehicle details.
Definition newgrf.h:166
std::vector< GRFLabel > labels
List of labels.
Definition newgrf.h:145
PriceMultipliers price_base_multipliers
Price base multipliers as set by the grf.
Definition newgrf.h:171
Information about a NewGRF goto-label.
Definition newgrf.h:119
size_t pos
Position in file.
Definition newgrf.h:122
uint8_t label
Non-unique label identifier.
Definition newgrf.h:120
uint32_t nfo_line
Line within NewGRF of label.
Definition newgrf.h:121
State of features loaded by NewGRFs.
Definition newgrf.h:207
bool has_2CC
Set if any vehicle is loaded which uses 2cc (two company colours).
Definition newgrf.h:208
LiverySchemes used_liveries
Bitmask of LiveryScheme used by the defined engines.
Definition newgrf.h:209
TramDepotReplacement tram
In which way tram depots were replaced.
Definition newgrf.h:211
ShoreReplacement shore
In which way shore sprites were replaced.
Definition newgrf.h:210
Describes properties of price bases.
Definition newgrf.h:217
Price fallback_price
Fallback price multiplier for new prices but old grfs.
Definition newgrf.h:221
PriceCategory category
Price is affected by certain difficulty settings.
Definition newgrf.h:219
Money start_price
Default value at game start, before adding multipliers.
Definition newgrf.h:218
GrfSpecFeature grf_feature
GRF Feature that decides whether price multipliers apply locally or globally, GrfSpecFeature::End if ...
Definition newgrf.h:220
Types related to vehicles.
VehicleType
Available vehicle types.