OpenTTD Source 15.0-beta2
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#include "core/mem_func.hpp"
22
27enum CanalFeature : uint8_t {
28 CF_WATERSLOPE,
29 CF_LOCKS,
30 CF_DIKES,
31 CF_ICON,
32 CF_DOCKS,
33 CF_RIVER_SLOPE,
34 CF_RIVER_EDGE,
35 CF_RIVER_GUI,
36 CF_BUOY,
37 CF_END,
38};
39
45
46enum GrfLoadingStage : uint8_t {
47 GLS_FILESCAN,
48 GLS_SAFETYSCAN,
49 GLS_LABELSCAN,
50 GLS_INIT,
51 GLS_RESERVE,
52 GLS_ACTIVATION,
53 GLS_END,
54};
55
57
58enum class GrfMiscBit : uint8_t {
59 DesertTreesFields = 0, // Unsupported.
60 DesertPavedRoads = 1,
61 FieldBoundingBox = 2, // Unsupported.
63 AmbientSoundCallback = 4,
64 CatenaryOn3rdTrack = 5, // Unsupported.
65 SecondRockyTileSet = 6,
66};
67
69
70enum GrfSpecFeature : uint8_t {
71 GSF_TRAINS,
72 GSF_ROADVEHICLES,
73 GSF_SHIPS,
74 GSF_AIRCRAFT,
75 GSF_STATIONS,
76 GSF_CANALS,
77 GSF_BRIDGES,
78 GSF_HOUSES,
79 GSF_GLOBALVAR,
80 GSF_INDUSTRYTILES,
81 GSF_INDUSTRIES,
82 GSF_CARGOES,
83 GSF_SOUNDFX,
84 GSF_AIRPORTS,
85 GSF_SIGNALS,
86 GSF_OBJECTS,
87 GSF_RAILTYPES,
88 GSF_AIRPORTTILES,
89 GSF_ROADTYPES,
90 GSF_TRAMTYPES,
91 GSF_ROADSTOPS,
92 GSF_BADGES,
93 GSF_END,
94
95 GSF_FAKE_TOWNS = GSF_END,
97
98 GSF_INVALID = 0xFF,
99};
101
102static const uint32_t INVALID_GRFID = 0xFFFFFFFF;
103
104struct GRFLabel {
105 uint8_t label;
106 uint32_t nfo_line;
107 size_t pos;
108
109 GRFLabel(uint8_t label, uint32_t nfo_line, size_t pos) : label(label), nfo_line(nfo_line), pos(pos) {}
110};
111
113struct GRFFile {
114 std::string filename{};
115 uint32_t grfid = 0;
116 uint8_t grf_version = 0;
117
118 uint sound_offset = 0;
119 uint16_t num_sounds = 0;
120
121 std::vector<std::unique_ptr<struct StationSpec>> stations;
122 std::vector<std::unique_ptr<struct HouseSpec>> housespec;
123 std::vector<std::unique_ptr<struct IndustrySpec>> industryspec;
124 std::vector<std::unique_ptr<struct IndustryTileSpec>> indtspec;
125 std::vector<std::unique_ptr<struct ObjectSpec>> objectspec;
126 std::vector<std::unique_ptr<struct AirportSpec>> airportspec;
127 std::vector<std::unique_ptr<struct AirportTileSpec>> airtspec;
128 std::vector<std::unique_ptr<struct RoadStopSpec>> roadstops;
129
130 std::vector<uint32_t> param{};
131
132 std::vector<GRFLabel> labels{};
133
134 std::vector<CargoLabel> cargo_list{};
135 std::array<uint8_t, NUM_CARGO> cargo_map{};
136
137 std::vector<BadgeID> badge_list{};
138 std::unordered_map<uint16_t, BadgeID> badge_map{};
139
140 std::vector<RailTypeLabel> railtype_list{};
141 std::array<RailType, RAILTYPE_END> railtype_map{};
142
143 std::vector<RoadTypeLabel> roadtype_list{};
144 std::array<RoadType, ROADTYPE_END> roadtype_map{};
145
146 std::vector<RoadTypeLabel> tramtype_list{};
147 std::array<RoadType, ROADTYPE_END> tramtype_map{};
148
149 std::array<CanalProperties, CF_END> canal_local_properties{};
150
151 std::unordered_map<uint8_t, LanguageMap> language_map{};
152
155
156 uint32_t grf_features = 0;
157 PriceMultipliers price_base_multipliers{};
158
159 GRFFile(const struct GRFConfig &config);
160 GRFFile();
161 GRFFile(GRFFile &&other);
162 ~GRFFile();
163
165 uint32_t GetParam(uint number) const
166 {
167 /* Note: We implicitly test for number < this->param.size() and return 0 for invalid parameters.
168 * In fact this is the more important test, as param is zeroed anyway. */
169 return (number < std::size(this->param)) ? this->param[number] : 0;
170 }
171};
172
179
185
192
198inline bool HasGrfMiscBit(GrfMiscBit bit)
199{
201 return _misc_grf_features.Test(bit);
202}
203
204/* Indicates which are the newgrf features currently loaded ingame */
206
207void LoadNewGRFFile(GRFConfig &config, GrfLoadingStage stage, Subdirectory subdir, bool temporary);
208void LoadNewGRF(SpriteID load_index, uint num_baseset);
209void ReloadNewGRFData(); // in saveload/afterload.cpp
210void ResetNewGRFData();
212
213void GrfMsgI(int severity, const std::string &msg);
214#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)
215
216bool GetGlobalVariable(uint8_t param, uint32_t *value, const GRFFile *grffile);
217
218StringID MapGRFStringID(uint32_t grfid, GRFStringID str);
219void ShowNewGRFError();
220
221#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.
#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.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
Functions related to memory operations.
GrfMiscBits _misc_grf_features
Miscellaneous GRF features, set by Action 0x0D, parameter 0x9E.
Definition newgrf.cpp:69
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:496
ShoreReplacement
Definition newgrf.h:173
@ SHORE_REPLACE_ACTION_A
Shore sprites were replaced by ActionA (using grass tiles for the corner-shores).
Definition newgrf.h:176
@ SHORE_REPLACE_NONE
No shore sprites were replaced.
Definition newgrf.h:174
@ SHORE_REPLACE_ONLY_NEW
Only corner-shores were loaded by Action5 (openttd(w/d).grf only).
Definition newgrf.h:177
@ SHORE_REPLACE_ACTION_5
Shore sprites were replaced by Action5.
Definition newgrf.h:175
bool HasGrfMiscBit(GrfMiscBit bit)
Check for grf miscellaneous bits.
Definition newgrf.h:198
CanalFeature
List of different canal 'features'.
Definition newgrf.h:27
TramReplacement
Definition newgrf.h:180
@ TRAMWAY_REPLACE_DEPOT_WITH_TRACK
Electrified depot graphics with tram track were loaded.
Definition newgrf.h:182
@ TRAMWAY_REPLACE_DEPOT_NO_TRACK
Electrified depot graphics without tram track were loaded.
Definition newgrf.h:183
@ TRAMWAY_REPLACE_DEPOT_NONE
No tram depot graphics were loaded.
Definition newgrf.h:181
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:72
void LoadNewGRF(SpriteID load_index, uint num_baseset)
Load all the NewGRFs.
Definition newgrf.cpp:1718
void ResetNewGRFData()
Reset all NewGRF loaded data.
Definition newgrf.cpp:402
GrfSpecFeature
Definition newgrf.h:70
@ GSF_FAKE_TOWNS
Fake town GrfSpecFeature for NewGRF debugging (parent scope)
Definition newgrf.h:95
@ GSF_INVALID
An invalid spec feature.
Definition newgrf.h:98
@ 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:88
GrfMiscBit
Definition newgrf.h:58
@ 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:1357
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:41
CanalCallbackMasks callback_mask
Bitmask of canal callbacks that have to be called.
Definition newgrf.h:42
uint8_t flags
Flags controlling display.
Definition newgrf.h:43
Information about GRF, used in the game and (part of it) in savegames.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:113
std::array< CanalProperties, CF_END > canal_local_properties
Canal properties as set by this NewGRF.
Definition newgrf.h:149
std::array< uint8_t, NUM_CARGO > cargo_map
Inverse cargo translation table (CargoType -> local ID)
Definition newgrf.h:135
std::vector< RailTypeLabel > railtype_list
Railtype translation table.
Definition newgrf.h:140
uint32_t GetParam(uint number) const
Get GRF Parameter with range checking.
Definition newgrf.h:165
std::vector< RoadTypeLabel > roadtype_list
Roadtype translation table (road)
Definition newgrf.h:143
uint32_t grf_features
Bitset of GrfSpecFeature the grf uses.
Definition newgrf.h:156
std::vector< RoadTypeLabel > tramtype_list
Roadtype translation table (tram)
Definition newgrf.h:146
std::vector< CargoLabel > cargo_list
Cargo translation table (local ID -> label)
Definition newgrf.h:134
uint traininfo_vehicle_width
Width (in pixels) of a 8/8 train vehicle in depot GUI and vehicle details.
Definition newgrf.h:154
std::vector< BadgeID > badge_list
Badge translation table (local index -> global index)
Definition newgrf.h:137
int traininfo_vehicle_pitch
Vertical offset for drawing train images in depot GUI and vehicle details.
Definition newgrf.h:153
std::unordered_map< uint8_t, LanguageMap > language_map
Mappings related to the languages.
Definition newgrf.h:151
std::vector< GRFLabel > labels
List of labels.
Definition newgrf.h:132
PriceMultipliers price_base_multipliers
Price base multipliers as set by the grf.
Definition newgrf.h:157
bool has_2CC
Set if any vehicle is loaded which uses 2cc (two company colours).
Definition newgrf.h:187
ShoreReplacement shore
In which way shore sprites were replaced.
Definition newgrf.h:189
uint64_t used_liveries
Bitmask of LiveryScheme used by the defined engines.
Definition newgrf.h:188
TramReplacement tram
In which way tram depots were replaced.
Definition newgrf.h:190