OpenTTD Source: src/newgrf.h Source File

OpenTTD Source 15.0-beta1

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_text_type.h"
18#include "core/bitmath_func.hpp"
19#include "core/alloc_type.hpp"
20#include "core/mem_func.hpp"
21
26 enum CanalFeature {
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
40 struct CanalProperties {
41 uint8_t callback_mask;
42 uint8_t flags;
43};
44
45enum GrfLoadingStage {
46 GLS_FILESCAN,
47 GLS_SAFETYSCAN,
48 GLS_LABELSCAN,
49 GLS_INIT,
50 GLS_RESERVE,
51 GLS_ACTIVATION,
52 GLS_END,
53};
54
55 DECLARE_POSTFIX_INCREMENT(GrfLoadingStage)
56
57enum GrfMiscBit {
58 GMB_DESERT_TREES_FIELDS = 0, // Unsupported.
59 GMB_DESERT_PAVED_ROADS = 1,
60 GMB_FIELD_BOUNDING_BOX = 2, // Unsupported.
61 GMB_TRAIN_WIDTH_32_PIXELS = 3,
62 GMB_AMBIENT_SOUND_CALLBACK = 4,
63 GMB_CATENARY_ON_3RD_TRACK = 5, // Unsupported.
64 GMB_SECOND_ROCKY_TILE_SET = 6,
65};
66
67 enum GrfSpecFeature {
68 GSF_TRAINS,
69 GSF_ROADVEHICLES,
70 GSF_SHIPS,
71 GSF_AIRCRAFT,
72 GSF_STATIONS,
73 GSF_CANALS,
74 GSF_BRIDGES,
75 GSF_HOUSES,
76 GSF_GLOBALVAR,
77 GSF_INDUSTRYTILES,
78 GSF_INDUSTRIES,
79 GSF_CARGOES,
80 GSF_SOUNDFX,
81 GSF_AIRPORTS,
82 GSF_SIGNALS,
83 GSF_OBJECTS,
84 GSF_RAILTYPES,
85 GSF_AIRPORTTILES,
86 GSF_ROADTYPES,
87 GSF_TRAMTYPES,
88 GSF_ROADSTOPS,
89 GSF_END,
90
91 GSF_FAKE_TOWNS = GSF_END,
92 GSF_FAKE_END,
93
94 GSF_INVALID = 0xFF,
95};
96
97static const uint32_t INVALID_GRFID = 0xFFFFFFFF;
98
99 struct GRFLabel {
100 uint8_t label;
101 uint32_t nfo_line;
102 size_t pos;
103
104 GRFLabel(uint8_t label, uint32_t nfo_line, size_t pos) : label(label), nfo_line(nfo_line), pos(pos) {}
105};
106
108 struct GRFFile : ZeroedMemoryAllocator {
109 std::string filename;
110 uint32_t grfid;
111 uint8_t grf_version;
112
113 uint sound_offset;
114 uint16_t num_sounds;
115
116 std::vector<std::unique_ptr<struct StationSpec>> stations;
117 std::vector<std::unique_ptr<struct HouseSpec>> housespec;
118 std::vector<std::unique_ptr<struct IndustrySpec>> industryspec;
119 std::vector<std::unique_ptr<struct IndustryTileSpec>> indtspec;
120 std::vector<std::unique_ptr<struct ObjectSpec>> objectspec;
121 std::vector<std::unique_ptr<struct AirportSpec>> airportspec;
122 std::vector<std::unique_ptr<struct AirportTileSpec>> airtspec;
123 std::vector<std::unique_ptr<struct RoadStopSpec>> roadstops;
124
125 std::vector<uint32_t> param;
126
127 std::vector<GRFLabel> labels;
128
129 std::vector<CargoLabel> cargo_list;
130 std::array<uint8_t, NUM_CARGO> cargo_map{};
131
132 std::vector<RailTypeLabel> railtype_list;
133 std::array<RailType, RAILTYPE_END> railtype_map{};
134
135 std::vector<RoadTypeLabel> roadtype_list;
136 std::array<RoadType, ROADTYPE_END> roadtype_map{};
137
138 std::vector<RoadTypeLabel> tramtype_list;
139 std::array<RoadType, ROADTYPE_END> tramtype_map{};
140
141 CanalProperties canal_local_properties[CF_END];
142
143 std::unordered_map<uint8_t, LanguageMap> language_map;
144
145 int traininfo_vehicle_pitch;
146 uint traininfo_vehicle_width;
147
148 uint32_t grf_features;
149 PriceMultipliers price_base_multipliers;
150
151 GRFFile(const struct GRFConfig *config);
152
154 uint32_t GetParam(uint number) const
155 {
156 /* Note: We implicitly test for number < this->param.size() and return 0 for invalid parameters.
157 * In fact this is the more important test, as param is zeroed anyway. */
158 return (number < std::size(this->param)) ? this->param[number] : 0;
159 }
160};
161
162 enum ShoreReplacement {
163 SHORE_REPLACE_NONE,
164 SHORE_REPLACE_ACTION_5,
165 SHORE_REPLACE_ACTION_A,
166 SHORE_REPLACE_ONLY_NEW,
167};
168
169 enum TramReplacement {
170 TRAMWAY_REPLACE_DEPOT_NONE,
171 TRAMWAY_REPLACE_DEPOT_WITH_TRACK,
172 TRAMWAY_REPLACE_DEPOT_NO_TRACK,
173};
174
175 struct GRFLoadedFeatures {
176 bool has_2CC;
177 uint64_t used_liveries;
178 ShoreReplacement shore;
179 TramReplacement tram;
180};
181
187 inline bool HasGrfMiscBit(GrfMiscBit bit)
188{
189 extern uint8_t _misc_grf_features;
190 return HasBit(_misc_grf_features, bit);
191}
192
193/* Indicates which are the newgrf features currently loaded ingame */
194extern GRFLoadedFeatures _loaded_newgrf_features;
195
196void LoadNewGRFFile(struct GRFConfig *config, GrfLoadingStage stage, Subdirectory subdir, bool temporary);
197void LoadNewGRF(SpriteID load_index, uint num_baseset);
198void ReloadNewGRFData(); // in saveload/afterload.cpp
199void ResetNewGRFData();
200void ResetPersistentNewGRFData();
201
202void GrfMsgI(int severity, const std::string &msg);
203#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)
204
205bool GetGlobalVariable(uint8_t param, uint32_t *value, const GRFFile *grffile);
206
207 StringID MapGRFStringID(uint32_t grfid, StringID str);
208void ShowNewGRFError();
209
210#endif /* NEWGRF_H */
alloc_type.hpp
Helper types related to the allocation of memory.
bitmath_func.hpp
Functions related to bit mathematics.
HasBit
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
cargotype.h
Types/functions related to cargoes.
ZeroedMemoryAllocator
Base class that provides memory initialization on dynamically created objects.
Definition alloc_type.hpp:86
DECLARE_POSTFIX_INCREMENT
#define DECLARE_POSTFIX_INCREMENT(enum_type)
Some enums need to have allowed incrementing (i.e.
Definition enum_type.hpp:18
fileio_type.h
Types for Standard In/Out file operations.
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:115
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:18
mem_func.hpp
Functions related to memory operations.
_misc_grf_features
uint8_t _misc_grf_features
Miscellaneous GRF features, set by Action 0x0D, parameter 0x9E.
Definition newgrf.cpp:78
GetGlobalVariable
bool GetGlobalVariable(uint8_t param, uint32_t *value, const GRFFile *grffile)
Reads a variable common to VarAction2 and Action7/9/D.
Definition newgrf.cpp:6584
MapGRFStringID
StringID MapGRFStringID(uint32_t grfid, StringID str)
Used when setting an object's property to map to the GRF's strings while taking in consideration the ...
Definition newgrf.cpp:560
ResetPersistentNewGRFData
void ResetPersistentNewGRFData()
Reset NewGRF data which is stored persistently in savegames.
Definition newgrf.cpp:8926
GrfSpecFeature
GrfSpecFeature
Definition newgrf.h:67
GSF_FAKE_TOWNS
@ GSF_FAKE_TOWNS
Fake town GrfSpecFeature for NewGRF debugging (parent scope)
Definition newgrf.h:91
GSF_INVALID
@ GSF_INVALID
An invalid spec feature.
Definition newgrf.h:94
GSF_FAKE_END
@ GSF_FAKE_END
End of the fake features.
Definition newgrf.h:92
HasGrfMiscBit
bool HasGrfMiscBit(GrfMiscBit bit)
Check for grf miscellaneous bits.
Definition newgrf.h:187
LoadNewGRFFile
void LoadNewGRFFile(struct GRFConfig *config, GrfLoadingStage stage, Subdirectory subdir, bool temporary)
Load a particular NewGRF.
Definition newgrf.cpp:9790
GrfMiscBit
GrfMiscBit
Definition newgrf.h:57
GMB_TRAIN_WIDTH_32_PIXELS
@ GMB_TRAIN_WIDTH_32_PIXELS
Use 32 pixels per train vehicle in depot gui and vehicle details. Never set in the global variable;.
Definition newgrf.h:61
_loaded_newgrf_features
GRFLoadedFeatures _loaded_newgrf_features
Indicates which are the newgrf features currently loaded ingame.
Definition newgrf.cpp:84
LoadNewGRF
void LoadNewGRF(SpriteID load_index, uint num_baseset)
Load all the NewGRFs.
Definition newgrf.cpp:10117
ResetNewGRFData
void ResetNewGRFData()
Reset all NewGRF loaded data.
Definition newgrf.cpp:8827
TramReplacement
TramReplacement
Definition newgrf.h:169
TRAMWAY_REPLACE_DEPOT_WITH_TRACK
@ TRAMWAY_REPLACE_DEPOT_WITH_TRACK
Electrified depot graphics with tram track were loaded.
Definition newgrf.h:171
TRAMWAY_REPLACE_DEPOT_NO_TRACK
@ TRAMWAY_REPLACE_DEPOT_NO_TRACK
Electrified depot graphics without tram track were loaded.
Definition newgrf.h:172
TRAMWAY_REPLACE_DEPOT_NONE
@ TRAMWAY_REPLACE_DEPOT_NONE
No tram depot graphics were loaded.
Definition newgrf.h:170
ShoreReplacement
ShoreReplacement
Definition newgrf.h:162
SHORE_REPLACE_ACTION_A
@ SHORE_REPLACE_ACTION_A
Shore sprites were replaced by ActionA (using grass tiles for the corner-shores).
Definition newgrf.h:165
SHORE_REPLACE_NONE
@ SHORE_REPLACE_NONE
No shore sprites were replaced.
Definition newgrf.h:163
SHORE_REPLACE_ONLY_NEW
@ SHORE_REPLACE_ONLY_NEW
Only corner-shores were loaded by Action5 (openttd(w/d).grf only).
Definition newgrf.h:166
SHORE_REPLACE_ACTION_5
@ SHORE_REPLACE_ACTION_5
Shore sprites were replaced by Action5.
Definition newgrf.h:164
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:390
CanalFeature
CanalFeature
List of different canal 'features'.
Definition newgrf.h:26
ShowNewGRFError
void ShowNewGRFError()
Show the first NewGRF error we can find.
Definition newgrf_gui.cpp:45
ReloadNewGRFData
void ReloadNewGRFData()
Reload all NewGRF files during a running game.
Definition afterload.cpp:3393
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:16
CanalProperties
Canal properties local to the NewGRF.
Definition newgrf.h:40
CanalProperties::callback_mask
uint8_t callback_mask
Bitmask of canal callbacks that have to be called.
Definition newgrf.h:41
CanalProperties::flags
uint8_t flags
Flags controlling display.
Definition newgrf.h:42
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition newgrf_config.h:154
GRFFile
Dynamic data of a loaded NewGRF.
Definition newgrf.h:108
GRFFile::cargo_map
std::array< uint8_t, NUM_CARGO > cargo_map
Inverse cargo translation table (CargoID -> local ID)
Definition newgrf.h:130
GRFFile::railtype_list
std::vector< RailTypeLabel > railtype_list
Railtype translation table.
Definition newgrf.h:132
GRFFile::GetParam
uint32_t GetParam(uint number) const
Get GRF Parameter with range checking.
Definition newgrf.h:154
GRFFile::canal_local_properties
CanalProperties canal_local_properties[CF_END]
Canal properties as set by this NewGRF.
Definition newgrf.h:141
GRFFile::roadtype_list
std::vector< RoadTypeLabel > roadtype_list
Roadtype translation table (road)
Definition newgrf.h:135
GRFFile::grf_features
uint32_t grf_features
Bitset of GrfSpecFeature the grf uses.
Definition newgrf.h:148
GRFFile::tramtype_list
std::vector< RoadTypeLabel > tramtype_list
Roadtype translation table (tram)
Definition newgrf.h:138
GRFFile::cargo_list
std::vector< CargoLabel > cargo_list
Cargo translation table (local ID -> label)
Definition newgrf.h:129
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:146
GRFFile::traininfo_vehicle_pitch
int traininfo_vehicle_pitch
Vertical offset for drawing train images in depot GUI and vehicle details.
Definition newgrf.h:145
GRFFile::language_map
std::unordered_map< uint8_t, LanguageMap > language_map
Mappings related to the languages.
Definition newgrf.h:143
GRFFile::labels
std::vector< GRFLabel > labels
List of labels.
Definition newgrf.h:127
GRFFile::price_base_multipliers
PriceMultipliers price_base_multipliers
Price base multipliers as set by the grf.
Definition newgrf.h:149
GRFLabel
Definition newgrf.h:99
GRFLoadedFeatures
Definition newgrf.h:175
GRFLoadedFeatures::has_2CC
bool has_2CC
Set if any vehicle is loaded which uses 2cc (two company colours).
Definition newgrf.h:176
GRFLoadedFeatures::shore
ShoreReplacement shore
In which way shore sprites were replaced.
Definition newgrf.h:178
GRFLoadedFeatures::used_liveries
uint64_t used_liveries
Bitmask of LiveryScheme used by the defined engines.
Definition newgrf.h:177
GRFLoadedFeatures::tram
TramReplacement tram
In which way tram depots were replaced.
Definition newgrf.h:179