OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

newgrf_commons.h
Go to the documentation of this file.
1 /* $Id: newgrf_commons.h 27534 2016-04-02 16:02:22Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
15 #ifndef NEWGRF_COMMONS_H
16 #define NEWGRF_COMMONS_H
17
18 #include "sprite.h"
19 #include "core/alloc_type.hpp"
20 #include "core/smallvec_type.hpp"
21 #include "command_type.h"
22 #include "direction_type.h"
23 #include "company_type.h"
24
26 enum TileContext {
27 TCX_NORMAL,
28 TCX_UPPER_HALFTILE,
29 TCX_ON_BRIDGE,
30 };
31
35 enum TileLayoutFlags {
36 TLF_NOTHING = 0x00,
37
38 TLF_DODRAW = 0x01,
39 TLF_SPRITE = 0x02,
40 TLF_PALETTE = 0x04,
41 TLF_CUSTOM_PALETTE = 0x08,
42
43 TLF_BB_XY_OFFSET = 0x10,
44 TLF_BB_Z_OFFSET = 0x20,
45
46 TLF_CHILD_X_OFFSET = 0x10,
47 TLF_CHILD_Y_OFFSET = 0x20,
48
49 TLF_SPRITE_VAR10 = 0x40,
50 TLF_PALETTE_VAR10 = 0x80,
51
52 TLF_KNOWN_FLAGS = 0xFF,
53
55 TLF_DRAWING_FLAGS = ~TLF_CUSTOM_PALETTE,
56
58 TLF_NON_GROUND_FLAGS = TLF_BB_XY_OFFSET | TLF_BB_Z_OFFSET | TLF_CHILD_X_OFFSET | TLF_CHILD_Y_OFFSET,
59
61 TLF_VAR10_FLAGS = TLF_SPRITE_VAR10 | TLF_PALETTE_VAR10,
62
64 TLF_SPRITE_REG_FLAGS = TLF_DODRAW | TLF_SPRITE | TLF_BB_XY_OFFSET | TLF_BB_Z_OFFSET | TLF_CHILD_X_OFFSET | TLF_CHILD_Y_OFFSET,
65
67 TLF_PALETTE_REG_FLAGS = TLF_PALETTE,
68 };
69 DECLARE_ENUM_AS_BIT_SET(TileLayoutFlags)
70
71
77 static inline uint GetConstructionStageOffset(uint construction_stage, uint num_sprites)
78 {
79 assert(num_sprites > 0);
80 if (num_sprites > 4) num_sprites = 4;
81 switch (construction_stage) {
82 case 0: return 0;
83 case 1: return num_sprites > 2 ? 1 : 0;
84 case 2: return num_sprites > 2 ? num_sprites - 2 : 0;
85 case 3: return num_sprites - 1;
86 default: NOT_REACHED();
87 }
88 }
89
93 struct TileLayoutRegisters {
94 TileLayoutFlags flags;
95 uint8 dodraw;
96 uint8 sprite;
97 uint8 palette;
98 uint16 max_sprite_offset;
99 uint16 max_palette_offset;
100 union {
101 uint8 parent[3];
102 uint8 child[2];
103 } delta;
104 uint8 sprite_var10;
105 uint8 palette_var10;
106 };
107
108 static const uint TLR_MAX_VAR10 = 7;
109
115 struct NewGRFSpriteLayout : ZeroedMemoryAllocator, DrawTileSprites {
116 const TileLayoutRegisters *registers;
117
122 uint consistent_max_offset;
123
124 void Allocate(uint num_sprites);
125 void AllocateRegisters();
126 void Clone(const DrawTileSeqStruct *source);
127 void Clone(const NewGRFSpriteLayout *source);
128
133 void Clone(const DrawTileSprites *source)
134 {
135 assert(source != NULL && this != source);
136 this->ground = source->ground;
137 this->Clone(source->seq);
138 }
139
140 virtual ~NewGRFSpriteLayout()
141 {
142 free(this->seq);
143 free(this->registers);
144 }
145
152 bool NeedsPreprocessing() const
153 {
154 return this->registers != NULL;
155 }
156
157 uint32 PrepareLayout(uint32 orig_offset, uint32 newgrf_ground_offset, uint32 newgrf_offset, uint constr_stage, bool separate_ground) const;
158 void ProcessRegisters(uint8 resolved_var10, uint32 resolved_sprite, bool separate_ground) const;
159
165 const DrawTileSeqStruct *GetLayout(PalSpriteID *ground) const
166 {
167 DrawTileSeqStruct *front = result_seq.Begin();
168 *ground = front->image;
169 return front + 1;
170 }
171
172 private:
173 static SmallVector<DrawTileSeqStruct, 8> result_seq;
174 };
175
188 struct EntityIDMapping {
189 uint32 grfid;
190 uint8 entity_id;
191 uint8 substitute_id;
192 };
193
194 class OverrideManagerBase {
195 protected:
196 uint16 *entity_overrides;
197 uint32 *grfid_overrides;
198
199 uint16 max_offset;
200 uint16 max_new_entities;
201
202 uint16 invalid_ID;
203 virtual bool CheckValidNewID(uint16 testid) { return true; }
204
205 public:
206 EntityIDMapping *mapping_ID;
207
208 OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid);
209 virtual ~OverrideManagerBase();
210
211 void ResetOverride();
212 void ResetMapping();
213
214 void Add(uint8 local_id, uint32 grfid, uint entity_type);
215 virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
216
217 uint32 GetGRFID(uint16 entity_id) const;
218 uint16 GetSubstituteID(uint16 entity_id) const;
219 virtual uint16 GetID(uint8 grf_local_id, uint32 grfid) const;
220
221 inline uint16 GetMaxMapping() const { return max_new_entities; }
222 inline uint16 GetMaxOffset() const { return max_offset; }
223 };
224
225
226 struct HouseSpec;
227 class HouseOverrideManager : public OverrideManagerBase {
228 public:
229 HouseOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
230 OverrideManagerBase(offset, maximum, invalid) {}
231 void SetEntitySpec(const HouseSpec *hs);
232 };
233
234
235 struct IndustrySpec;
236 class IndustryOverrideManager : public OverrideManagerBase {
237 public:
238 IndustryOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
239 OverrideManagerBase(offset, maximum, invalid) {}
240
241 virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
242 virtual uint16 GetID(uint8 grf_local_id, uint32 grfid) const;
243 void SetEntitySpec(IndustrySpec *inds);
244 };
245
246
247 struct IndustryTileSpec;
248 class IndustryTileOverrideManager : public OverrideManagerBase {
249 protected:
250 virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
251 public:
252 IndustryTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
253 OverrideManagerBase(offset, maximum, invalid) {}
254
255 void SetEntitySpec(const IndustryTileSpec *indts);
256 };
257
258 struct AirportSpec;
259 class AirportOverrideManager : public OverrideManagerBase {
260 public:
261 AirportOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
262 OverrideManagerBase(offset, maximum, invalid) {}
263
264 void SetEntitySpec(AirportSpec *inds);
265 };
266
267 struct AirportTileSpec;
268 class AirportTileOverrideManager : public OverrideManagerBase {
269 protected:
270 virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
271 public:
272 AirportTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
273 OverrideManagerBase(offset, maximum, invalid) {}
274
275 void SetEntitySpec(const AirportTileSpec *ats);
276 };
277
278 struct ObjectSpec;
279 class ObjectOverrideManager : public OverrideManagerBase {
280 protected:
281 virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
282 public:
283 ObjectOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
284 OverrideManagerBase(offset, maximum, invalid) {}
285
286 void SetEntitySpec(ObjectSpec *spec);
287 };
288
289 extern HouseOverrideManager _house_mngr;
290 extern IndustryOverrideManager _industry_mngr;
291 extern IndustryTileOverrideManager _industile_mngr;
292 extern AirportOverrideManager _airport_mngr;
293 extern AirportTileOverrideManager _airporttile_mngr;
294 extern ObjectOverrideManager _object_mngr;
295
296 uint32 GetTerrainType(TileIndex tile, TileContext context = TCX_NORMAL);
297 TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets = true, Axis axis = INVALID_AXIS);
298 uint32 GetNearbyTileInformation(TileIndex tile, bool grf_version8);
299 uint32 GetCompanyInfo(CompanyID owner, const struct Livery *l = NULL);
300 CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, const GRFFile *grffile, StringID default_error);
301
302 void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res);
303 bool ConvertBooleanCallback(const struct GRFFile *grffile, uint16 cbid, uint16 cb_res);
304 bool Convert8bitBooleanCallback(const struct GRFFile *grffile, uint16 cbid, uint16 cb_res);
305
310 template <size_t Tcnt>
311 struct GRFFilePropsBase {
312 GRFFilePropsBase() : local_id(0), grffile(0)
313 {
314 /* The lack of some compilers to provide default constructors complying to the specs
315 * requires us to zero the stuff ourself. */
316 memset(spritegroup, 0, sizeof(spritegroup));
317 }
318
319 uint16 local_id;
320 const struct GRFFile *grffile;
321 const struct SpriteGroup *spritegroup[Tcnt];
322 };
323
325 struct GRFFileProps : GRFFilePropsBase<1> {
327 GRFFileProps(uint16 subst_id = 0) :
328 GRFFilePropsBase<1>(), subst_id(subst_id), override(subst_id)
329 {
330 }
331
332 uint16 subst_id;
333 uint16 override;
334 };
335
336 #endif /* NEWGRF_COMMONS_H */