OpenTTD Source 20250528-master-g3aca5d62a8
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 <http://www.gnu.org/licenses/>.
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#include "core/bitmath_func.hpp"
21
26enum CanalFeature : uint8_t {
27 CF_WATERSLOPE,
28 CF_LOCKS,
29 CF_DIKES,
30 CF_ICON,
31 CF_DOCKS,
32 CF_RIVER_SLOPE,
33 CF_RIVER_EDGE,
34 CF_RIVER_GUI,
35 CF_BUOY,
36 CF_END,
37};
38
44
45enum GrfLoadingStage : uint8_t {
46 GLS_FILESCAN,
47 GLS_SAFETYSCAN,
48 GLS_LABELSCAN,
49 GLS_INIT,
50 GLS_RESERVE,
51 GLS_ACTIVATION,
52 GLS_END,
53};
54
56
57enum class GrfMiscBit : uint8_t {
58 DesertTreesFields = 0, // Unsupported.
59 DesertPavedRoads = 1,
60 FieldBoundingBox = 2, // Unsupported.
62 AmbientSoundCallback = 4,
63 CatenaryOn3rdTrack = 5, // Unsupported.
64 SecondRockyTileSet = 6,
65};
66
68
69enum GrfSpecFeature : uint8_t {
70 GSF_TRAINS,
71 GSF_ROADVEHICLES,
72 GSF_SHIPS,
73 GSF_AIRCRAFT,
74 GSF_STATIONS,
75 GSF_CANALS,
76 GSF_BRIDGES,
77 GSF_HOUSES,
78 GSF_GLOBALVAR,
79 GSF_INDUSTRYTILES,
80 GSF_INDUSTRIES,
81 GSF_CARGOES,
82 GSF_SOUNDFX,
83 GSF_AIRPORTS,
84 GSF_SIGNALS,
85 GSF_OBJECTS,
86 GSF_RAILTYPES,
87 GSF_AIRPORTTILES,
88 GSF_ROADTYPES,
89 GSF_TRAMTYPES,
90 GSF_ROADSTOPS,
91 GSF_BADGES,
92 GSF_END,
93
94 GSF_DEFAULT = GSF_END,
95 GSF_FAKE_TOWNS = GSF_END,
97
98 GSF_ORIGINAL_STRINGS = 0x48,
99
100 GSF_INVALID = 0xFF,
101};
103
104static const uint32_t INVALID_GRFID = 0xFFFFFFFF;
105
106struct GRFLabel {
107 uint8_t label;
108 uint32_t nfo_line;
109 size_t pos;
110
111 GRFLabel(uint8_t label, uint32_t nfo_line, size_t pos) : label(label), nfo_line(nfo_line), pos(pos) {}
112};
113
115struct GRFFile {
116 std::string filename{};
117 uint32_t grfid = 0;
118 uint8_t grf_version = 0;
119
120 uint sound_offset = 0;
121 uint16_t num_sounds = 0;
122
123 std::vector<std::unique_ptr<struct StationSpec>> stations;
124 std::vector<std::unique_ptr<struct HouseSpec>> housespec;
125 std::vector<std::unique_ptr<struct IndustrySpec>> industryspec;
126 std::vector<std::unique_ptr<struct IndustryTileSpec>> indtspec;
127 std::vector<std::unique_ptr<struct ObjectSpec>> objectspec;
128 std::vector<std::unique_ptr<struct AirportSpec>> airportspec;
129 std::vector<std::unique_ptr<struct AirportTileSpec>> airtspec;
130 std::vector<std::unique_ptr<struct RoadStopSpec>> roadstops;
131
132 std::vector<uint32_t> param{};
133
134 std::vector<GRFLabel> labels{};
135
136 std::vector<CargoLabel> cargo_list{};
137 std::array<uint8_t, NUM_CARGO> cargo_map{};
138
139 std::vector<BadgeID> badge_list{};
140 std::unordered_map<uint16_t, BadgeID> badge_map{};
141
142 std::vector<RailTypeLabel> railtype_list{};
143 std::array<RailType, RAILTYPE_END> railtype_map{};
144
145 std::vector<RoadTypeLabel> roadtype_list{};
146 std::array<RoadType, ROADTYPE_END> roadtype_map{};
147
148 std::vector<RoadTypeLabel> tramtype_list{};
149 std::array<RoadType, ROADTYPE_END> tramtype_map{};
150
151 std::array<CanalProperties, CF_END> canal_local_properties{};
152
153 std::unordered_map<uint8_t, LanguageMap> language_map{};
154
157
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
181
187
194
204
210inline bool HasGrfMiscBit(GrfMiscBit bit)
211{
213 return _misc_grf_features.Test(bit);
214}
215
216/* Indicates which are the newgrf features currently loaded ingame */
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();
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
230StringID MapGRFStringID(uint32_t grfid, GRFStringID str);
231void ShowNewGRFError();
232
233#endif /* NEWGRF_H */
Functions related to bit mathematics.
Types/functions related to cargoes.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
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:63
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
GrfMiscBits _misc_grf_features
Miscellaneous GRF features, set by Action 0x0D, parameter 0x9E.
Definition newgrf.cpp:70
bool GetGlobalVariable(uint8_t param, uint32_t *value, const GRFFile *grffile)
Reads a variable common to VarAction2 and Action7/9/D.
void ResetPersistentNewGRFData()
Reset NewGRF data which is stored persistently in savegames.
Definition newgrf.cpp:497
ShoreReplacement
Definition newgrf.h:175
@ SHORE_REPLACE_ACTION_A
Shore sprites were replaced by ActionA (using grass tiles for the corner-shores).
Definition newgrf.h:178
@ SHORE_REPLACE_NONE
No shore sprites were replaced.
Definition newgrf.h:176
@ 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 sprites were replaced by Action5.
Definition newgrf.h:177
bool HasGrfMiscBit(GrfMiscBit bit)
Check for grf miscellaneous bits.
Definition newgrf.h:210
CanalFeature
List of different canal 'features'.
Definition newgrf.h:26
TramReplacement
Definition newgrf.h:182
@ TRAMWAY_REPLACE_DEPOT_WITH_TRACK
Electrified depot graphics with tram track were loaded.
Definition newgrf.h:184
@ TRAMWAY_REPLACE_DEPOT_NO_TRACK
Electrified depot graphics without tram track were loaded.
Definition newgrf.h:185
@ TRAMWAY_REPLACE_DEPOT_NONE
No tram depot graphics were loaded.
Definition newgrf.h:183
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 ...
GRFLoadedFeatures _loaded_newgrf_features
Indicates which are the newgrf features currently loaded ingame.
Definition newgrf.cpp:73
void LoadNewGRF(SpriteID load_index, uint num_baseset)
Load all the NewGRFs.
Definition newgrf.cpp:1735
void ResetNewGRFData()
Reset all NewGRF loaded data.
Definition newgrf.cpp:403
GrfSpecFeature
Definition newgrf.h:69
@ GSF_FAKE_TOWNS
Fake town GrfSpecFeature for NewGRF debugging (parent scope)
Definition newgrf.h:95
@ GSF_DEFAULT
Unspecified feature, default badge.
Definition newgrf.h:94
@ GSF_INVALID
An invalid spec feature.
Definition newgrf.h:100
@ GSF_FAKE_END
End of the fake features.
Definition newgrf.h:96
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:89
GrfMiscBit
Definition newgrf.h:57
@ TrainWidth32Pixels
Use 32 pixels per train vehicle in depot gui and vehicle details. Never set in the global variable;.
void LoadNewGRFFile(GRFConfig &config, GrfLoadingStage stage, Subdirectory subdir, bool temporary)
Load a particular NewGRF.
Definition newgrf.cpp:1373
void ShowNewGRFError()
Show the first NewGRF error we can find.
void ReloadNewGRFData()
Reload all NewGRF files during a running game.
Types related to NewGRF badges.
Callbacks that NewGRFs could implement.
Header of Action 04 "universal holder" structure.
The different types of rail.
Enums and other types related to roads.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Canal properties local to the NewGRF.
Definition newgrf.h:40
CanalCallbackMasks callback_mask
Bitmask of canal callbacks that have to be called.
Definition newgrf.h:41
uint8_t flags
Flags controlling display.
Definition newgrf.h:42
Information about GRF, used in the game and (part of it) in savegames.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:115
std::array< CanalProperties, CF_END > canal_local_properties
Canal properties as set by this NewGRF.
Definition newgrf.h:151
std::array< uint8_t, NUM_CARGO > cargo_map
Inverse cargo translation table (CargoType -> local ID)
Definition newgrf.h:137
std::vector< RailTypeLabel > railtype_list
Railtype translation table.
Definition newgrf.h:142
uint32_t GetParam(uint number) const
Get GRF Parameter with range checking.
Definition newgrf.h:167
std::vector< RoadTypeLabel > roadtype_list
Roadtype translation table (road)
Definition newgrf.h:145
std::vector< RoadTypeLabel > tramtype_list
Roadtype translation table (tram)
Definition newgrf.h:148
std::vector< CargoLabel > cargo_list
Cargo translation table (local ID -> label)
Definition newgrf.h:136
uint traininfo_vehicle_width
Width (in pixels) of a 8/8 train vehicle in depot GUI and vehicle details.
Definition newgrf.h:156
std::vector< BadgeID > badge_list
Badge translation table (local index -> global index)
Definition newgrf.h:139
GrfSpecFeatures grf_features
Bitset of GrfSpecFeature the grf uses.
Definition newgrf.h:158
int traininfo_vehicle_pitch
Vertical offset for drawing train images in depot GUI and vehicle details.
Definition newgrf.h:155
std::unordered_map< uint8_t, LanguageMap > language_map
Mappings related to the languages.
Definition newgrf.h:153
std::vector< GRFLabel > labels
List of labels.
Definition newgrf.h:134
PriceMultipliers price_base_multipliers
Price base multipliers as set by the grf.
Definition newgrf.h:159
bool has_2CC
Set if any vehicle is loaded which uses 2cc (two company colours).
Definition newgrf.h:189
ShoreReplacement shore
In which way shore sprites were replaced.
Definition newgrf.h:191
uint64_t used_liveries
Bitmask of LiveryScheme used by the defined engines.
Definition newgrf.h:190
TramReplacement tram
In which way tram depots were replaced.
Definition newgrf.h:192
Describes properties of price bases.
Definition newgrf.h:198
Price fallback_price
Fallback price multiplier for new prices but old grfs.
Definition newgrf.h:202
PriceCategory category
Price is affected by certain difficulty settings.
Definition newgrf.h:200
Money start_price
Default value at game start, before adding multipliers.
Definition newgrf.h:199
GrfSpecFeature grf_feature
GRF Feature that decides whether price multipliers apply locally or globally, GSF_END if none.
Definition newgrf.h:201