OpenTTD Source 20260531-master-g0e951f3528
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 "cargotype.h"
14#include "livery.h"
15#include "rail_type.h"
16#include "road_type.h"
17#include "fileio_type.h"
18#include "newgrf_badge_type.h"
19#include "newgrf_callbacks.h"
20#include "newgrf_text_type.h"
21#include "vehicle_type.h"
22
23struct GRFConfig;
24
29enum CanalFeature : uint8_t {
30 CF_WATERSLOPE,
31 CF_LOCKS,
32 CF_DIKES,
33 CF_ICON,
34 CF_DOCKS,
35 CF_RIVER_SLOPE,
36 CF_RIVER_EDGE,
37 CF_RIVER_GUI,
38 CF_BUOY,
39 CF_END,
40};
41
44 CanalCallbackMasks callback_mask;
45 uint8_t flags;
46};
47
57
59
60
70
71using GrfMiscBits = EnumBitSet<GrfMiscBit, uint8_t>;
72
107
108static const uint32_t INVALID_GRFID = 0xFFFFFFFF;
109
110struct GRFLabel {
111 uint8_t label;
112 uint32_t nfo_line;
113 size_t pos;
114
115 GRFLabel(uint8_t label, uint32_t nfo_line, size_t pos) : label(label), nfo_line(nfo_line), pos(pos) {}
116};
117
119struct GRFFile {
120 std::string filename{};
121 uint32_t grfid = 0;
122 uint8_t grf_version = 0;
123
124 uint sound_offset = 0;
125 uint16_t num_sounds = 0;
126
127 std::vector<std::unique_ptr<struct StationSpec>> stations;
128 std::vector<std::unique_ptr<struct HouseSpec>> housespec;
129 std::vector<std::unique_ptr<struct IndustrySpec>> industryspec;
130 std::vector<std::unique_ptr<struct IndustryTileSpec>> indtspec;
131 std::vector<std::unique_ptr<struct ObjectSpec>> objectspec;
132 std::vector<std::unique_ptr<struct AirportSpec>> airportspec;
133 std::vector<std::unique_ptr<struct AirportTileSpec>> airtspec;
134 std::vector<std::unique_ptr<struct RoadStopSpec>> roadstops;
135
136 std::vector<uint32_t> param{};
137
138 std::vector<GRFLabel> labels{};
139
140 std::vector<CargoLabel> cargo_list{};
141 std::array<uint8_t, NUM_CARGO> cargo_map{};
142
143 std::vector<BadgeID> badge_list{};
144 std::unordered_map<uint16_t, BadgeID> badge_map{};
145
146 std::vector<RailTypeLabel> railtype_list{};
147 std::array<RailType, RAILTYPE_END> railtype_map{};
148
149 std::vector<RoadTypeLabel> roadtype_list{};
150 std::array<RoadType, ROADTYPE_END> roadtype_map{};
151
152 std::vector<RoadTypeLabel> tramtype_list{};
153 std::array<RoadType, ROADTYPE_END> tramtype_map{};
154
155 std::array<CanalProperties, CF_END> canal_local_properties{};
156
157 std::unordered_map<uint8_t, LanguageMap> language_map{};
158
162
163 GrfSpecFeatures grf_features{};
164 PriceMultipliers price_base_multipliers{};
165
166 GRFFile(const GRFConfig &config);
167 GRFFile();
168 GRFFile(GRFFile &&other);
169 ~GRFFile();
170
176 uint32_t GetParam(uint number) const
177 {
178 /* Note: We implicitly test for number < this->param.size() and return 0 for invalid parameters.
179 * In fact this is the more important test, as param is zeroed anyway. */
180 return (number < std::size(this->param)) ? this->param[number] : 0;
181 }
182};
183
191
198
206
216
222inline bool HasGrfMiscBit(GrfMiscBit bit)
223{
224 extern GrfMiscBits _misc_grf_features;
225 return _misc_grf_features.Test(bit);
226}
227
228/* Indicates which are the newgrf features currently loaded ingame */
230
231void LoadNewGRFFile(GRFConfig &config, GrfLoadingStage stage, Subdirectory subdir, bool temporary);
232void LoadNewGRF(SpriteID load_index, uint num_baseset);
233void ReloadNewGRFData(); // in saveload/afterload.cpp
234void ResetNewGRFData();
236
237void GrfMsgI(int severity, const std::string &msg);
238#define GrfMsg(severity, format_string, ...) do { if ((severity) == 0 || _debug_grf_level >= (severity)) GrfMsgI(severity, fmt::format(FMT_STRING(format_string) __VA_OPT__(,) __VA_ARGS__)); } while (false)
239
240bool GetGlobalVariable(uint8_t param, uint32_t *value, const GRFFile *grffile);
241
242StringID MapGRFStringID(uint32_t grfid, GRFStringID str);
243void ShowNewGRFError();
244
247
248#endif /* NEWGRF_H */
Types/functions related to cargoes.
Global state for badge definitions.
Enum-as-bit-set wrapper.
PriceCategory
Categories of a price bases.
Price
Enumeration of all base prices for use with Prices.
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:86
Types for standard in/out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
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:65
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.
GrfLoadingStage
Stages of loading all NewGRFs.
Definition newgrf.h:49
@ SafetyScan
Checks whether the NewGRF can be used in a static context.
Definition newgrf.h:51
@ Reserve
Third step of NewGRF loading; reserve features and GRMs.
Definition newgrf.h:54
@ Init
Second step of NewGRF loading; load all actions into memory.
Definition newgrf.h:53
@ LabelScan
First step of NewGRF loading; find the 'goto' labels in the NewGRF.
Definition newgrf.h:52
@ Activation
Forth step of NewGRF loading; activate the features.
Definition newgrf.h:55
@ FileScan
Load the Action 8 metadata (GRF ID, name).
Definition newgrf.h:50
void ResetPersistentNewGRFData()
Reset NewGRF data which is stored persistently in savegames.
Definition newgrf.cpp:511
ShoreReplacement
Type of shore replacement loaded by NewGRFs.
Definition newgrf.h:185
@ Action5
Shore sprites were replaced by Action5.
Definition newgrf.h:187
@ ActionA
Shore sprites were replaced by ActionA (using grass tiles for the corner-shores).
Definition newgrf.h:188
@ OnlyNew
Only corner-shores were loaded by Action5 (openttd(w/d).grf only).
Definition newgrf.h:189
bool HasGrfMiscBit(GrfMiscBit bit)
Check for grf miscellaneous bits.
Definition newgrf.h:222
CanalFeature
List of different canal 'features'.
Definition newgrf.h:29
TramDepotReplacement
Type of tram depot replacement loaded by NewGRFs.
Definition newgrf.h:193
@ WithTrack
Electrified depot graphics with tram track were loaded.
Definition newgrf.h:195
@ WithoutTrack
Electrified depot graphics without tram track were loaded.
Definition newgrf.h:196
StringID MapGRFStringID(uint32_t grfid, GRFStringID str)
Used when setting an object's property to map to the GRF's strings while taking in consideration the ...
void LoadNewGRF(SpriteID load_index, uint num_baseset)
Load all the NewGRFs.
Definition newgrf.cpp:1764
void ResetNewGRFData()
Reset all NewGRF loaded data.
Definition newgrf.cpp:414
GrfSpecFeature GetGrfSpecFeature(VehicleType type)
Get the GrfSpecFeature associated with a VehicleType.
Definition newgrf.cpp:1893
GrfSpecFeature
Definition newgrf.h:73
@ RoadStops
Road stops feature.
Definition newgrf.h:94
@ Trains
Trains feature.
Definition newgrf.h:74
@ FakeTowns
Fake town GrfSpecFeature for NewGRF debugging (parent scope).
Definition newgrf.h:99
@ SoundEffects
Sound effects feature.
Definition newgrf.h:86
@ Cargoes
Cargoes feature.
Definition newgrf.h:85
@ RoadVehicles
Road vehicles feature.
Definition newgrf.h:75
@ TramTypes
Tram types feature.
Definition newgrf.h:93
@ Invalid
An invalid spec feature.
Definition newgrf.h:104
@ Ships
Ships feature.
Definition newgrf.h:76
@ Houses
Houses feature.
Definition newgrf.h:81
@ FakeEnd
End of the fake features.
Definition newgrf.h:100
@ OriginalStrings
Pseudo unsupported 'feature' for replacing original strings.
Definition newgrf.h:102
@ Default
Unspecified feature, default badge.
Definition newgrf.h:98
@ Stations
Stations feature.
Definition newgrf.h:78
@ Airports
Airports feature.
Definition newgrf.h:87
@ GlobalVar
Global variables feature.
Definition newgrf.h:82
@ Bridges
Bridges feature.
Definition newgrf.h:80
@ IndustryTiles
Industry tiles feature.
Definition newgrf.h:83
@ Objects
Objects feature.
Definition newgrf.h:89
@ Signals
Signals feature.
Definition newgrf.h:88
@ Canals
Canals feature.
Definition newgrf.h:79
@ Industries
Industries feature.
Definition newgrf.h:84
void GrfMsgI(int severity, const std::string &msg)
Debug() function dedicated to newGRF debugging messages Function is essentially the same as Debug(grf...
Definition newgrf.cpp:91
GrfMiscBit
Bits of NewGRF's GlobalVariable 1E/9E.
Definition newgrf.h:61
@ TrainWidth32Pixels
Use 32 pixels per train vehicle in depot gui and vehicle details. Never set in the global variable;.
Definition newgrf.h:65
@ SecondRockyTileSet
Enable using the second rocky tile set.
Definition newgrf.h:68
@ CatenaryOn3rdTrack
Unsupported: enable catenaries over third track type.
Definition newgrf.h:67
@ DesertTreesFields
Unsupported: allow trees and fields in desert climate.
Definition newgrf.h:62
@ AmbientSoundCallback
Enable ambient sound effect callback 144.
Definition newgrf.h:66
@ DesertPavedRoads
Show pavement and lights in desert towns.
Definition newgrf.h:63
@ FieldBoundingBox
Unsupported: fields have a height.
Definition newgrf.h:64
void LoadNewGRFFile(GRFConfig &config, GrfLoadingStage stage, Subdirectory subdir, bool temporary)
Load a particular NewGRF.
Definition newgrf.cpp:1398
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:1909
Types related to NewGRF badges.
Callbacks that NewGRFs could implement.
Header of Action 04 "universal holder" structure.
StrongType::Typedef< uint32_t, struct GRFStringIDTag, StrongType::Compare, StrongType::Integer > GRFStringID
Type for GRF-internal string IDs.
The different types of rail.
EnumBitSet< RailType, uint64_t > RailTypes
Allow incrementing of Track variables.
Definition rail_type.h:38
Enums and other types related to roads.
uint32_t 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:73
Canal properties local to the NewGRF.
Definition newgrf.h:43
CanalCallbackMasks callback_mask
Bitmask of canal callbacks that have to be called.
Definition newgrf.h:44
uint8_t flags
Flags controlling display.
Definition newgrf.h:45
Information about GRF, used in the game and (part of it) in savegames.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:119
std::array< CanalProperties, CF_END > canal_local_properties
Canal properties as set by this NewGRF.
Definition newgrf.h:155
std::array< uint8_t, NUM_CARGO > cargo_map
Inverse cargo translation table (CargoType -> local ID).
Definition newgrf.h:141
std::vector< RailTypeLabel > railtype_list
Railtype translation table.
Definition newgrf.h:146
uint32_t GetParam(uint number) const
Get GRF Parameter with range checking.
Definition newgrf.h:176
std::vector< RoadTypeLabel > roadtype_list
Roadtype translation table (road).
Definition newgrf.h:149
bool cargo_list_is_fallback
Set if cargo types have been created but a cargo list has not been installed.
Definition newgrf.h:161
std::vector< RoadTypeLabel > tramtype_list
Roadtype translation table (tram).
Definition newgrf.h:152
GRFFile(const GRFConfig &config)
Constructor for GRFFile.
Definition newgrf.cpp:577
std::vector< CargoLabel > cargo_list
Cargo translation table (local ID -> label).
Definition newgrf.h:140
uint traininfo_vehicle_width
Width (in pixels) of a 8/8 train vehicle in depot GUI and vehicle details.
Definition newgrf.h:160
std::vector< BadgeID > badge_list
Badge translation table (local index -> global index).
Definition newgrf.h:143
GrfSpecFeatures grf_features
Bitset of GrfSpecFeature the grf uses.
Definition newgrf.h:163
int traininfo_vehicle_pitch
Vertical offset for drawing train images in depot GUI and vehicle details.
Definition newgrf.h:159
std::unordered_map< uint8_t, LanguageMap > language_map
Mappings related to the languages.
Definition newgrf.h:157
std::vector< GRFLabel > labels
List of labels.
Definition newgrf.h:138
PriceMultipliers price_base_multipliers
Price base multipliers as set by the grf.
Definition newgrf.h:164
State of features loaded by NewGRFs.
Definition newgrf.h:200
bool has_2CC
Set if any vehicle is loaded which uses 2cc (two company colours).
Definition newgrf.h:201
LiverySchemes used_liveries
Bitmask of LiveryScheme used by the defined engines.
Definition newgrf.h:202
TramDepotReplacement tram
In which way tram depots were replaced.
Definition newgrf.h:204
ShoreReplacement shore
In which way shore sprites were replaced.
Definition newgrf.h:203
Describes properties of price bases.
Definition newgrf.h:210
Price fallback_price
Fallback price multiplier for new prices but old grfs.
Definition newgrf.h:214
PriceCategory category
Price is affected by certain difficulty settings.
Definition newgrf.h:212
Money start_price
Default value at game start, before adding multipliers.
Definition newgrf.h:211
GrfSpecFeature grf_feature
GRF Feature that decides whether price multipliers apply locally or globally, GrfSpecFeature::End if ...
Definition newgrf.h:213
@ End
End marker.
Definition vehicle.cpp:2829
@ None
No visual effect.
Definition vehicle.cpp:2824
Types related to vehicles.
VehicleType
Available vehicle types.