OpenTTD Source: src/newgrf.h Source File

OpenTTD Source 15.3

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
10#ifndef NEWGRF_H
11#define NEWGRF_H
12
13#include "cargotype.h"
14#include "rail_type.h"
15#include "road_type.h"
16#include "fileio_type.h"
17#include "newgrf_badge_type.h"
18#include "newgrf_callbacks.h"
19#include "newgrf_text_type.h"
20
25 enum CanalFeature : uint8_t {
26 CF_WATERSLOPE,
27 CF_LOCKS,
28 CF_DIKES,
29 CF_ICON,
30 CF_DOCKS,
31 CF_RIVER_SLOPE,
32 CF_RIVER_EDGE,
33 CF_RIVER_GUI,
34 CF_BUOY,
35 CF_END,
36};
37
39 struct CanalProperties {
40 CanalCallbackMasks callback_mask;
41 uint8_t flags;
42};
43
44enum GrfLoadingStage : uint8_t {
45 GLS_FILESCAN,
46 GLS_SAFETYSCAN,
47 GLS_LABELSCAN,
48 GLS_INIT,
49 GLS_RESERVE,
50 GLS_ACTIVATION,
51 GLS_END,
52};
53
54 DECLARE_INCREMENT_DECREMENT_OPERATORS(GrfLoadingStage)
55
56enum class GrfMiscBit : uint8_t {
57 DesertTreesFields = 0, // Unsupported.
58 DesertPavedRoads = 1,
59 FieldBoundingBox = 2, // Unsupported.
60 TrainWidth32Pixels = 3,
61 AmbientSoundCallback = 4,
62 CatenaryOn3rdTrack = 5, // Unsupported.
63 SecondRockyTileSet = 6,
64};
65
66using GrfMiscBits = EnumBitSet<GrfMiscBit, uint8_t>;
67
68 enum GrfSpecFeature : uint8_t {
69 GSF_TRAINS,
70 GSF_ROADVEHICLES,
71 GSF_SHIPS,
72 GSF_AIRCRAFT,
73 GSF_STATIONS,
74 GSF_CANALS,
75 GSF_BRIDGES,
76 GSF_HOUSES,
77 GSF_GLOBALVAR,
78 GSF_INDUSTRYTILES,
79 GSF_INDUSTRIES,
80 GSF_CARGOES,
81 GSF_SOUNDFX,
82 GSF_AIRPORTS,
83 GSF_SIGNALS,
84 GSF_OBJECTS,
85 GSF_RAILTYPES,
86 GSF_AIRPORTTILES,
87 GSF_ROADTYPES,
88 GSF_TRAMTYPES,
89 GSF_ROADSTOPS,
90 GSF_BADGES,
91 GSF_END,
92
93 GSF_DEFAULT = GSF_END,
94 GSF_FAKE_TOWNS = GSF_END,
95 GSF_FAKE_END,
96
97 GSF_ORIGINAL_STRINGS = 0x48,
98
99 GSF_INVALID = 0xFF,
100};
101using GrfSpecFeatures = EnumBitSet<GrfSpecFeature, uint32_t, GrfSpecFeature::GSF_END>;
102
103static const uint32_t INVALID_GRFID = 0xFFFFFFFF;
104
105 struct GRFLabel {
106 uint8_t label;
107 uint32_t nfo_line;
108 size_t pos;
109
110 GRFLabel(uint8_t label, uint32_t nfo_line, size_t pos) : label(label), nfo_line(nfo_line), pos(pos) {}
111};
112
114 struct GRFFile {
115 std::string filename{};
116 uint32_t grfid = 0;
117 uint8_t grf_version = 0;
118
119 uint sound_offset = 0;
120 uint16_t num_sounds = 0;
121
122 std::vector<std::unique_ptr<struct StationSpec>> stations;
123 std::vector<std::unique_ptr<struct HouseSpec>> housespec;
124 std::vector<std::unique_ptr<struct IndustrySpec>> industryspec;
125 std::vector<std::unique_ptr<struct IndustryTileSpec>> indtspec;
126 std::vector<std::unique_ptr<struct ObjectSpec>> objectspec;
127 std::vector<std::unique_ptr<struct AirportSpec>> airportspec;
128 std::vector<std::unique_ptr<struct AirportTileSpec>> airtspec;
129 std::vector<std::unique_ptr<struct RoadStopSpec>> roadstops;
130
131 std::vector<uint32_t> param{};
132
133 std::vector<GRFLabel> labels{};
134
135 std::vector<CargoLabel> cargo_list{};
136 std::array<uint8_t, NUM_CARGO> cargo_map{};
137
138 std::vector<BadgeID> badge_list{};
139 std::unordered_map<uint16_t, BadgeID> badge_map{};
140
141 std::vector<RailTypeLabel> railtype_list{};
142 std::array<RailType, RAILTYPE_END> railtype_map{};
143
144 std::vector<RoadTypeLabel> roadtype_list{};
145 std::array<RoadType, ROADTYPE_END> roadtype_map{};
146
147 std::vector<RoadTypeLabel> tramtype_list{};
148 std::array<RoadType, ROADTYPE_END> tramtype_map{};
149
150 std::array<CanalProperties, CF_END> canal_local_properties{};
151
152 std::unordered_map<uint8_t, LanguageMap> language_map{};
153
154 int traininfo_vehicle_pitch = 0;
155 uint traininfo_vehicle_width = 0;
156 bool cargo_list_is_fallback = false;
157
158 GrfSpecFeatures grf_features{};
159 PriceMultipliers price_base_multipliers{};
160
161 GRFFile(const struct GRFConfig &config);
162 GRFFile();
163 GRFFile(GRFFile &&other);
164 ~GRFFile();
165
167 uint32_t GetParam(uint number) const
168 {
169 /* Note: We implicitly test for number < this->param.size() and return 0 for invalid parameters.
170 * In fact this is the more important test, as param is zeroed anyway. */
171 return (number < std::size(this->param)) ? this->param[number] : 0;
172 }
173};
174
175 enum ShoreReplacement : uint8_t {
176 SHORE_REPLACE_NONE,
177 SHORE_REPLACE_ACTION_5,
178 SHORE_REPLACE_ACTION_A,
179 SHORE_REPLACE_ONLY_NEW,
180};
181
182 enum TramReplacement : uint8_t {
183 TRAMWAY_REPLACE_DEPOT_NONE,
184 TRAMWAY_REPLACE_DEPOT_WITH_TRACK,
185 TRAMWAY_REPLACE_DEPOT_NO_TRACK,
186};
187
188 struct GRFLoadedFeatures {
189 bool has_2CC;
190 uint64_t used_liveries;
191 ShoreReplacement shore;
192 TramReplacement tram;
193};
194
198 struct PriceBaseSpec {
199 Money start_price;
200 PriceCategory category;
201 GrfSpecFeature grf_feature;
202 Price fallback_price;
203};
204
210 inline bool HasGrfMiscBit(GrfMiscBit bit)
211{
212 extern GrfMiscBits _misc_grf_features;
213 return _misc_grf_features.Test(bit);
214}
215
216/* Indicates which are the newgrf features currently loaded ingame */
217extern GRFLoadedFeatures _loaded_newgrf_features;
218
219void LoadNewGRFFile(GRFConfig &config, GrfLoadingStage stage, Subdirectory subdir, bool temporary);
220void LoadNewGRF(SpriteID load_index, uint num_baseset);
221void ReloadNewGRFData(); // in saveload/afterload.cpp
222void ResetNewGRFData();
223void ResetPersistentNewGRFData();
224
225void GrfMsgI(int severity, const std::string &msg);
226#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)
227
228bool GetGlobalVariable(uint8_t param, uint32_t *value, const GRFFile *grffile);
229
230 StringID MapGRFStringID(uint32_t grfid, GRFStringID str);
231void ShowNewGRFError();
232
233#endif /* NEWGRF_H */
cargotype.h
Types/functions related to cargoes.
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
EnumBitSet< CanalCallbackMask, uint8_t >
OverflowSafeInt< int64_t >
PriceCategory
PriceCategory
Categories of a price bases.
Definition economy_type.h:204
Price
Price
Enumeration of all base prices for use with Prices.
Definition economy_type.h:95
DECLARE_INCREMENT_DECREMENT_OPERATORS
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:63
fileio_type.h
Types for Standard In/Out file operations.
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
_misc_grf_features
GrfMiscBits _misc_grf_features
Miscellaneous GRF features, set by Action 0x0D, parameter 0x9E.
Definition newgrf.cpp:71
GetGlobalVariable
bool GetGlobalVariable(uint8_t param, uint32_t *value, const GRFFile *grffile)
Reads a variable common to VarAction2 and Action7/9/D.
Definition newgrf_act0_globalvar.cpp:422
ResetPersistentNewGRFData
void ResetPersistentNewGRFData()
Reset NewGRF data which is stored persistently in savegames.
Definition newgrf.cpp:505
ShoreReplacement
ShoreReplacement
Definition newgrf.h:175
SHORE_REPLACE_ACTION_A
@ SHORE_REPLACE_ACTION_A
Shore sprites were replaced by ActionA (using grass tiles for the corner-shores).
Definition newgrf.h:178
SHORE_REPLACE_NONE
@ SHORE_REPLACE_NONE
No shore sprites were replaced.
Definition newgrf.h:176
SHORE_REPLACE_ONLY_NEW
@ SHORE_REPLACE_ONLY_NEW
Only corner-shores were loaded by Action5 (openttd(w/d).grf only).
Definition newgrf.h:179
SHORE_REPLACE_ACTION_5
@ SHORE_REPLACE_ACTION_5
Shore sprites were replaced by Action5.
Definition newgrf.h:177
HasGrfMiscBit
bool HasGrfMiscBit(GrfMiscBit bit)
Check for grf miscellaneous bits.
Definition newgrf.h:210
CanalFeature
CanalFeature
List of different canal 'features'.
Definition newgrf.h:25
TramReplacement
TramReplacement
Definition newgrf.h:182
TRAMWAY_REPLACE_DEPOT_WITH_TRACK
@ TRAMWAY_REPLACE_DEPOT_WITH_TRACK
Electrified depot graphics with tram track were loaded.
Definition newgrf.h:184
TRAMWAY_REPLACE_DEPOT_NO_TRACK
@ TRAMWAY_REPLACE_DEPOT_NO_TRACK
Electrified depot graphics without tram track were loaded.
Definition newgrf.h:185
TRAMWAY_REPLACE_DEPOT_NONE
@ TRAMWAY_REPLACE_DEPOT_NONE
No tram depot graphics were loaded.
Definition newgrf.h:183
MapGRFStringID
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 ...
Definition newgrf_stringmapping.cpp:129
_loaded_newgrf_features
GRFLoadedFeatures _loaded_newgrf_features
Indicates which are the newgrf features currently loaded ingame.
Definition newgrf.cpp:74
LoadNewGRF
void LoadNewGRF(SpriteID load_index, uint num_baseset)
Load all the NewGRFs.
Definition newgrf.cpp:1758
ResetNewGRFData
void ResetNewGRFData()
Reset all NewGRF loaded data.
Definition newgrf.cpp:408
GrfSpecFeature
GrfSpecFeature
Definition newgrf.h:68
GSF_FAKE_TOWNS
@ GSF_FAKE_TOWNS
Fake town GrfSpecFeature for NewGRF debugging (parent scope)
Definition newgrf.h:94
GSF_DEFAULT
@ GSF_DEFAULT
Unspecified feature, default badge.
Definition newgrf.h:93
GSF_INVALID
@ GSF_INVALID
An invalid spec feature.
Definition newgrf.h:99
GSF_FAKE_END
@ GSF_FAKE_END
End of the fake features.
Definition newgrf.h:95
GrfMsgI
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:90
GrfMiscBit
GrfMiscBit
Definition newgrf.h:56
GrfMiscBit::TrainWidth32Pixels
@ TrainWidth32Pixels
Use 32 pixels per train vehicle in depot gui and vehicle details. Never set in the global variable;.
LoadNewGRFFile
void LoadNewGRFFile(GRFConfig &config, GrfLoadingStage stage, Subdirectory subdir, bool temporary)
Load a particular NewGRF.
Definition newgrf.cpp:1391
ShowNewGRFError
void ShowNewGRFError()
Show the first NewGRF error we can find.
Definition newgrf_gui.cpp:47
ReloadNewGRFData
void ReloadNewGRFData()
Reload all NewGRF files during a running game.
Definition afterload.cpp:3445
newgrf_badge_type.h
Types related to NewGRF badges.
newgrf_callbacks.h
Callbacks that NewGRFs could implement.
newgrf_text_type.h
Header of Action 04 "universal holder" structure.
rail_type.h
The different types of rail.
road_type.h
Enums and other types related to roads.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
CanalProperties
Canal properties local to the NewGRF.
Definition newgrf.h:39
CanalProperties::callback_mask
CanalCallbackMasks callback_mask
Bitmask of canal callbacks that have to be called.
Definition newgrf.h:40
CanalProperties::flags
uint8_t flags
Flags controlling display.
Definition newgrf.h:41
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition newgrf_config.h:158
GRFFile
Dynamic data of a loaded NewGRF.
Definition newgrf.h:114
GRFFile::canal_local_properties
std::array< CanalProperties, CF_END > canal_local_properties
Canal properties as set by this NewGRF.
Definition newgrf.h:150
GRFFile::cargo_map
std::array< uint8_t, NUM_CARGO > cargo_map
Inverse cargo translation table (CargoType -> local ID)
Definition newgrf.h:136
GRFFile::railtype_list
std::vector< RailTypeLabel > railtype_list
Railtype translation table.
Definition newgrf.h:141
GRFFile::GetParam
uint32_t GetParam(uint number) const
Get GRF Parameter with range checking.
Definition newgrf.h:167
GRFFile::roadtype_list
std::vector< RoadTypeLabel > roadtype_list
Roadtype translation table (road)
Definition newgrf.h:144
GRFFile::cargo_list_is_fallback
bool cargo_list_is_fallback
Set if cargo types have been created but a cargo list has not been installed.
Definition newgrf.h:156
GRFFile::tramtype_list
std::vector< RoadTypeLabel > tramtype_list
Roadtype translation table (tram)
Definition newgrf.h:147
GRFFile::cargo_list
std::vector< CargoLabel > cargo_list
Cargo translation table (local ID -> label)
Definition newgrf.h:135
GRFFile::traininfo_vehicle_width
uint traininfo_vehicle_width
Width (in pixels) of a 8/8 train vehicle in depot GUI and vehicle details.
Definition newgrf.h:155
GRFFile::badge_list
std::vector< BadgeID > badge_list
Badge translation table (local index -> global index)
Definition newgrf.h:138
GRFFile::grf_features
GrfSpecFeatures grf_features
Bitset of GrfSpecFeature the grf uses.
Definition newgrf.h:158
GRFFile::traininfo_vehicle_pitch
int traininfo_vehicle_pitch
Vertical offset for drawing train images in depot GUI and vehicle details.
Definition newgrf.h:154
GRFFile::language_map
std::unordered_map< uint8_t, LanguageMap > language_map
Mappings related to the languages.
Definition newgrf.h:152
GRFFile::labels
std::vector< GRFLabel > labels
List of labels.
Definition newgrf.h:133
GRFFile::price_base_multipliers
PriceMultipliers price_base_multipliers
Price base multipliers as set by the grf.
Definition newgrf.h:159
GRFLabel
Definition newgrf.h:105
GRFLoadedFeatures
Definition newgrf.h:188
GRFLoadedFeatures::has_2CC
bool has_2CC
Set if any vehicle is loaded which uses 2cc (two company colours).
Definition newgrf.h:189
GRFLoadedFeatures::shore
ShoreReplacement shore
In which way shore sprites were replaced.
Definition newgrf.h:191
GRFLoadedFeatures::used_liveries
uint64_t used_liveries
Bitmask of LiveryScheme used by the defined engines.
Definition newgrf.h:190
GRFLoadedFeatures::tram
TramReplacement tram
In which way tram depots were replaced.
Definition newgrf.h:192
PriceBaseSpec
Describes properties of price bases.
Definition newgrf.h:198
PriceBaseSpec::fallback_price
Price fallback_price
Fallback price multiplier for new prices but old grfs.
Definition newgrf.h:202
PriceBaseSpec::category
PriceCategory category
Price is affected by certain difficulty settings.
Definition newgrf.h:200
PriceBaseSpec::start_price
Money start_price
Default value at game start, before adding multipliers.
Definition newgrf.h:199
PriceBaseSpec::grf_feature
GrfSpecFeature grf_feature
GRF Feature that decides whether price multipliers apply locally or globally, GSF_END if none.
Definition newgrf.h:201
StrongType::Typedef< uint32_t, struct GRFStringIDTag, StrongType::Compare, StrongType::Integer >