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 
8 /** @file newgrf.h Base for the NewGRF implementation. */
9 
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 "core/bitmath_func.hpp"
18 #include "core/alloc_type.hpp"
19 #include "core/smallvec_type.hpp"
20 
21 /**
22  * List of different canal 'features'.
23  * Each feature gets an entry in the canal spritegroup table
24  */
25 enum CanalFeature {
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 
38 /** Canal properties local to the NewGRF */
39 struct CanalProperties {
40 	uint8 callback_mask;  ///< Bitmask of canal callbacks that have to be called.
41 	uint8 flags;          ///< Flags controlling display.
42 };
43 
44 enum GrfLoadingStage {
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_POSTFIX_INCREMENT(GrfLoadingStage)
55 
56 enum GrfMiscBit {
57 	GMB_DESERT_TREES_FIELDS    = 0, // Unsupported.
58 	GMB_DESERT_PAVED_ROADS     = 1,
59 	GMB_FIELD_BOUNDING_BOX     = 2, // Unsupported.
60 	GMB_TRAIN_WIDTH_32_PIXELS  = 3, ///< Use 32 pixels per train vehicle in depot gui and vehicle details. Never set in the global variable; @see GRFFile::traininfo_vehicle_width
61 	GMB_AMBIENT_SOUND_CALLBACK = 4,
62 	GMB_CATENARY_ON_3RD_TRACK  = 5, // Unsupported.
63 	GMB_SECOND_ROCKY_TILE_SET  = 6,
64 };
65 
66 enum GrfSpecFeature {
67 	GSF_TRAINS,
68 	GSF_ROADVEHICLES,
69 	GSF_SHIPS,
70 	GSF_AIRCRAFT,
71 	GSF_STATIONS,
72 	GSF_CANALS,
73 	GSF_BRIDGES,
74 	GSF_HOUSES,
75 	GSF_GLOBALVAR,
76 	GSF_INDUSTRYTILES,
77 	GSF_INDUSTRIES,
78 	GSF_CARGOES,
79 	GSF_SOUNDFX,
80 	GSF_AIRPORTS,
81 	GSF_SIGNALS,
82 	GSF_OBJECTS,
83 	GSF_RAILTYPES,
84 	GSF_AIRPORTTILES,
85 	GSF_ROADTYPES,
86 	GSF_TRAMTYPES,
87 	GSF_END,
88 
89 	GSF_FAKE_TOWNS = GSF_END, ///< Fake town GrfSpecFeature for NewGRF debugging (parent scope)
90 	GSF_FAKE_END,             ///< End of the fake features
91 
92 	GSF_INVALID = 0xFF,       ///< An invalid spec feature
93 };
94 
95 static const uint32 INVALID_GRFID = 0xFFFFFFFF;
96 
97 struct GRFLabel {
98 	byte label;
99 	uint32 nfo_line;
100 	size_t pos;
101 	struct GRFLabel *next;
102 };
103 
104 /** Dynamic data of a loaded NewGRF */
105 struct GRFFile : ZeroedMemoryAllocator {
106 	char *filename;
107 	uint32 grfid;
108 	byte grf_version;
109 
110 	uint sound_offset;
111 	uint16 num_sounds;
112 
113 	struct StationSpec **stations;
114 	struct HouseSpec **housespec;
115 	struct IndustrySpec **industryspec;
116 	struct IndustryTileSpec **indtspec;
117 	struct ObjectSpec **objectspec;
118 	struct AirportSpec **airportspec;
119 	struct AirportTileSpec **airtspec;
120 
121 	uint32 param[0x80];
122 	uint param_end;  ///< one more than the highest set parameter
123 
124 	GRFLabel *label; ///< Pointer to the first label. This is a linked list, not an array.
125 
126 	std::vector<CargoLabel> cargo_list;             ///< Cargo translation table (local ID -> label)
127 	uint8 cargo_map[NUM_CARGO];                     ///< Inverse cargo translation table (CargoID -> local ID)
128 
129 	std::vector<RailTypeLabel> railtype_list;       ///< Railtype translation table
130 	RailType railtype_map[RAILTYPE_END];
131 
132 	std::vector<RoadTypeLabel> roadtype_list;       ///< Roadtype translation table (road)
133 	RoadType roadtype_map[ROADTYPE_END];
134 
135 	std::vector<RoadTypeLabel> tramtype_list;       ///< Roadtype translation table (tram)
136 	RoadType tramtype_map[ROADTYPE_END];
137 
138 	CanalProperties canal_local_properties[CF_END]; ///< Canal properties as set by this NewGRF
139 
140 	struct LanguageMap *language_map; ///< Mappings related to the languages.
141 
142 	int traininfo_vehicle_pitch;  ///< Vertical offset for drawing train images in depot GUI and vehicle details
143 	uint traininfo_vehicle_width; ///< Width (in pixels) of a 8/8 train vehicle in depot GUI and vehicle details
144 
145 	uint32 grf_features;                     ///< Bitset of GrfSpecFeature the grf uses
146 	PriceMultipliers price_base_multipliers; ///< Price base multipliers as set by the grf.
147 
148 	GRFFile(const struct GRFConfig *config);
149 	~GRFFile();
150 
151 	/** Get GRF Parameter with range checking */
GetParamGRFFile152 	uint32 GetParam(uint number) const
153 	{
154 		/* Note: We implicitly test for number < lengthof(this->param) and return 0 for invalid parameters.
155 		 *       In fact this is the more important test, as param is zeroed anyway. */
156 		assert(this->param_end <= lengthof(this->param));
157 		return (number < this->param_end) ? this->param[number] : 0;
158 	}
159 };
160 
161 enum ShoreReplacement {
162 	SHORE_REPLACE_NONE,       ///< No shore sprites were replaced.
163 	SHORE_REPLACE_ACTION_5,   ///< Shore sprites were replaced by Action5.
164 	SHORE_REPLACE_ACTION_A,   ///< Shore sprites were replaced by ActionA (using grass tiles for the corner-shores).
165 	SHORE_REPLACE_ONLY_NEW,   ///< Only corner-shores were loaded by Action5 (openttd(w/d).grf only).
166 };
167 
168 enum TramReplacement {
169 	TRAMWAY_REPLACE_DEPOT_NONE,       ///< No tram depot graphics were loaded.
170 	TRAMWAY_REPLACE_DEPOT_WITH_TRACK, ///< Electrified depot graphics with tram track were loaded.
171 	TRAMWAY_REPLACE_DEPOT_NO_TRACK,   ///< Electrified depot graphics without tram track were loaded.
172 };
173 
174 struct GRFLoadedFeatures {
175 	bool has_2CC;             ///< Set if any vehicle is loaded which uses 2cc (two company colours).
176 	uint64 used_liveries;     ///< Bitmask of #LiveryScheme used by the defined engines.
177 	ShoreReplacement shore;   ///< In which way shore sprites were replaced.
178 	TramReplacement tram;     ///< In which way tram depots were replaced.
179 };
180 
181 /**
182  * Check for grf miscellaneous bits
183  * @param bit The bit to check.
184  * @return Whether the bit is set.
185  */
HasGrfMiscBit(GrfMiscBit bit)186 static inline bool HasGrfMiscBit(GrfMiscBit bit)
187 {
188 	extern byte _misc_grf_features;
189 	return HasBit(_misc_grf_features, bit);
190 }
191 
192 /* Indicates which are the newgrf features currently loaded ingame */
193 extern GRFLoadedFeatures _loaded_newgrf_features;
194 
195 void LoadNewGRFFile(struct GRFConfig *config, GrfLoadingStage stage, Subdirectory subdir, bool temporary);
196 void LoadNewGRF(uint load_index, uint num_baseset);
197 void ReloadNewGRFData(); // in saveload/afterload.cpp
198 void ResetNewGRFData();
199 void ResetPersistentNewGRFData();
200 
201 void CDECL grfmsg(int severity, const char *str, ...) WARN_FORMAT(2, 3);
202 
203 bool GetGlobalVariable(byte param, uint32 *value, const GRFFile *grffile);
204 
205 StringID MapGRFStringID(uint32 grfid, StringID str);
206 void ShowNewGRFError();
207 
208 #endif /* NEWGRF_H */
209