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/>.
9 * @file newgrf_commons.h This file simplyfies and embeds a common mechanism of
10 * loading/saving and mapping of grf entities.
13 #ifndef NEWGRF_COMMONS_H
14 #define NEWGRF_COMMONS_H
17 #include "core/alloc_type.hpp"
18 #include "core/smallvec_type.hpp"
19 #include "command_type.h"
20 #include "direction_type.h"
21 #include "company_type.h"
23 /** Context for tile accesses */
25 TCX_NORMAL
, ///< Nothing special.
26 TCX_UPPER_HALFTILE
, ///< Querying information about the upper part of a tile with halftile foundation.
27 TCX_ON_BRIDGE
, ///< Querying information about stuff on the bridge (via some bridgehead).
31 * Flags to enable register usage in sprite layouts.
33 enum TileLayoutFlags
{
36 TLF_DODRAW
= 0x01, ///< Only draw sprite if value of register TileLayoutRegisters::dodraw is non-zero.
37 TLF_SPRITE
= 0x02, ///< Add signed offset to sprite from register TileLayoutRegisters::sprite.
38 TLF_PALETTE
= 0x04, ///< Add signed offset to palette from register TileLayoutRegisters::palette.
39 TLF_CUSTOM_PALETTE
= 0x08, ///< Palette is from Action 1 (moved to SPRITE_MODIFIER_CUSTOM_SPRITE in palette during loading).
41 TLF_BB_XY_OFFSET
= 0x10, ///< Add signed offset to bounding box X and Y positions from register TileLayoutRegisters::delta.parent[0..1].
42 TLF_BB_Z_OFFSET
= 0x20, ///< Add signed offset to bounding box Z positions from register TileLayoutRegisters::delta.parent[2].
44 TLF_CHILD_X_OFFSET
= 0x10, ///< Add signed offset to child sprite X positions from register TileLayoutRegisters::delta.child[0].
45 TLF_CHILD_Y_OFFSET
= 0x20, ///< Add signed offset to child sprite Y positions from register TileLayoutRegisters::delta.child[1].
47 TLF_SPRITE_VAR10
= 0x40, ///< Resolve sprite with a specific value in variable 10.
48 TLF_PALETTE_VAR10
= 0x80, ///< Resolve palette with a specific value in variable 10.
50 TLF_KNOWN_FLAGS
= 0xFF, ///< Known flags. Any unknown set flag will disable the GRF.
52 /** Flags which are still required after loading the GRF. */
53 TLF_DRAWING_FLAGS
= ~TLF_CUSTOM_PALETTE
,
55 /** Flags which do not work for the (first) ground sprite. */
56 TLF_NON_GROUND_FLAGS
= TLF_BB_XY_OFFSET
| TLF_BB_Z_OFFSET
| TLF_CHILD_X_OFFSET
| TLF_CHILD_Y_OFFSET
,
58 /** Flags which refer to using multiple action-1-2-3 chains. */
59 TLF_VAR10_FLAGS
= TLF_SPRITE_VAR10
| TLF_PALETTE_VAR10
,
61 /** Flags which require resolving the action-1-2-3 chain for the sprite, even if it is no action-1 sprite. */
62 TLF_SPRITE_REG_FLAGS
= TLF_DODRAW
| TLF_SPRITE
| TLF_BB_XY_OFFSET
| TLF_BB_Z_OFFSET
| TLF_CHILD_X_OFFSET
| TLF_CHILD_Y_OFFSET
,
64 /** Flags which require resolving the action-1-2-3 chain for the palette, even if it is no action-1 palette. */
65 TLF_PALETTE_REG_FLAGS
= TLF_PALETTE
,
67 DECLARE_ENUM_AS_BIT_SET(TileLayoutFlags
)
70 * Determines which sprite to use from a spriteset for a specific construction stage.
71 * @param construction_stage Construction stage 0 - 3.
72 * @param num_sprites Number of available sprites to select stage from.
73 * @return Sprite to use
75 static inline uint
GetConstructionStageOffset(uint construction_stage
, uint num_sprites
)
77 assert(num_sprites
> 0);
78 if (num_sprites
> 4) num_sprites
= 4;
79 switch (construction_stage
) {
81 case 1: return num_sprites
> 2 ? 1 : 0;
82 case 2: return num_sprites
> 2 ? num_sprites
- 2 : 0;
83 case 3: return num_sprites
- 1;
84 default: NOT_REACHED();
89 * Additional modifiers for items in sprite layouts.
91 struct TileLayoutRegisters
{
92 TileLayoutFlags flags
; ///< Flags defining which members are valid and to be used.
93 uint8 dodraw
; ///< Register deciding whether the sprite shall be drawn at all. Non-zero means drawing.
94 uint8 sprite
; ///< Register specifying a signed offset for the sprite.
95 uint8 palette
; ///< Register specifying a signed offset for the palette.
96 uint16 max_sprite_offset
; ///< Maximum offset to add to the sprite. (limited by size of the spriteset)
97 uint16 max_palette_offset
; ///< Maximum offset to add to the palette. (limited by size of the spriteset)
99 uint8 parent
[3]; ///< Registers for signed offsets for the bounding box position of parent sprites.
100 uint8 child
[2]; ///< Registers for signed offsets for the position of child sprites.
102 uint8 sprite_var10
; ///< Value for variable 10 when resolving the sprite.
103 uint8 palette_var10
; ///< Value for variable 10 when resolving the palette.
106 static const uint TLR_MAX_VAR10
= 7; ///< Maximum value for var 10.
109 * NewGRF supplied spritelayout.
110 * In contrast to #DrawTileSprites this struct is for allocated
111 * layouts on the heap. It allocates data and frees them on destruction.
113 struct NewGRFSpriteLayout
: ZeroedMemoryAllocator
, DrawTileSprites
{
114 const TileLayoutRegisters
*registers
;
117 * Number of sprites in all referenced spritesets.
118 * If these numbers are inconsistent, then this is 0 and the real values are in \c registers.
120 uint consistent_max_offset
;
122 void Allocate(uint num_sprites
);
123 void AllocateRegisters();
124 void Clone(const DrawTileSeqStruct
*source
);
125 void Clone(const NewGRFSpriteLayout
*source
);
128 * Clone a spritelayout.
129 * @param source The spritelayout to copy.
131 void Clone(const DrawTileSprites
*source
)
133 assert(source
!= nullptr && this != source
);
134 this->ground
= source
->ground
;
135 this->Clone(source
->seq
);
138 virtual ~NewGRFSpriteLayout()
141 free(this->registers
);
145 * Tests whether this spritelayout needs preprocessing by
146 * #PrepareLayout() and #ProcessRegisters(), or whether it can be
148 * @return true if preprocessing is needed
150 bool NeedsPreprocessing() const
152 return this->registers
!= nullptr;
155 uint32
PrepareLayout(uint32 orig_offset
, uint32 newgrf_ground_offset
, uint32 newgrf_offset
, uint constr_stage
, bool separate_ground
) const;
156 void ProcessRegisters(uint8 resolved_var10
, uint32 resolved_sprite
, bool separate_ground
) const;
159 * Returns the result spritelayout after preprocessing.
160 * @pre #PrepareLayout() and #ProcessRegisters() need calling first.
161 * @return result spritelayout
163 const DrawTileSeqStruct
*GetLayout(PalSpriteID
*ground
) const
165 DrawTileSeqStruct
*front
= result_seq
.data();
166 *ground
= front
->image
;
171 static std::vector
<DrawTileSeqStruct
> result_seq
; ///< Temporary storage when preprocessing spritelayouts.
175 * Maps an entity id stored on the map to a GRF file.
176 * Entities are objects used ingame (houses, industries, industry tiles) for
177 * which we need to correlate the ids from the grf files with the ones in the
178 * the savegames themselves.
179 * An array of EntityIDMapping structs is saved with the savegame so
180 * that those GRFs can be loaded in a different order, or removed safely. The
181 * index in the array is the entity's ID stored on the map.
183 * The substitute ID is the ID of an original entity that should be used instead
184 * if the GRF containing the new entity is not available.
186 struct EntityIDMapping
{
187 uint32 grfid
; ///< The GRF ID of the file the entity belongs to
188 uint8 entity_id
; ///< The entity ID within the GRF file
189 uint8 substitute_id
; ///< The (original) entity ID to use if this GRF is not available
192 class OverrideManagerBase
{
194 uint16
*entity_overrides
;
195 uint32
*grfid_overrides
;
197 uint16 max_offset
; ///< what is the length of the original entity's array of specs
198 uint16 max_new_entities
; ///< what is the amount of entities, old and new summed
200 uint16 invalid_ID
; ///< ID used to detected invalid entities;
201 virtual bool CheckValidNewID(uint16 testid
) { return true; }
204 EntityIDMapping
*mapping_ID
; ///< mapping of ids from grf files. Public out of convenience
206 OverrideManagerBase(uint16 offset
, uint16 maximum
, uint16 invalid
);
207 virtual ~OverrideManagerBase();
209 void ResetOverride();
212 void Add(uint8 local_id
, uint32 grfid
, uint entity_type
);
213 virtual uint16
AddEntityID(byte grf_local_id
, uint32 grfid
, byte substitute_id
);
215 uint32
GetGRFID(uint16 entity_id
) const;
216 uint16
GetSubstituteID(uint16 entity_id
) const;
217 virtual uint16
GetID(uint8 grf_local_id
, uint32 grfid
) const;
219 inline uint16
GetMaxMapping() const { return max_new_entities
; }
220 inline uint16
GetMaxOffset() const { return max_offset
; }
225 class HouseOverrideManager
: public OverrideManagerBase
{
227 HouseOverrideManager(uint16 offset
, uint16 maximum
, uint16 invalid
) :
228 OverrideManagerBase(offset
, maximum
, invalid
) {}
230 void SetEntitySpec(const HouseSpec
*hs
);
235 class IndustryOverrideManager
: public OverrideManagerBase
{
237 IndustryOverrideManager(uint16 offset
, uint16 maximum
, uint16 invalid
) :
238 OverrideManagerBase(offset
, maximum
, invalid
) {}
240 uint16
AddEntityID(byte grf_local_id
, uint32 grfid
, byte substitute_id
) override
;
241 uint16
GetID(uint8 grf_local_id
, uint32 grfid
) const override
;
243 void SetEntitySpec(IndustrySpec
*inds
);
247 struct IndustryTileSpec
;
248 class IndustryTileOverrideManager
: public OverrideManagerBase
{
250 virtual bool CheckValidNewID(uint16 testid
) { return testid
!= 0xFF; }
252 IndustryTileOverrideManager(uint16 offset
, uint16 maximum
, uint16 invalid
) :
253 OverrideManagerBase(offset
, maximum
, invalid
) {}
255 void SetEntitySpec(const IndustryTileSpec
*indts
);
259 class AirportOverrideManager
: public OverrideManagerBase
{
261 AirportOverrideManager(uint16 offset
, uint16 maximum
, uint16 invalid
) :
262 OverrideManagerBase(offset
, maximum
, invalid
) {}
264 void SetEntitySpec(AirportSpec
*inds
);
267 struct AirportTileSpec
;
268 class AirportTileOverrideManager
: public OverrideManagerBase
{
270 virtual bool CheckValidNewID(uint16 testid
) { return testid
!= 0xFF; }
272 AirportTileOverrideManager(uint16 offset
, uint16 maximum
, uint16 invalid
) :
273 OverrideManagerBase(offset
, maximum
, invalid
) {}
275 void SetEntitySpec(const AirportTileSpec
*ats
);
279 class ObjectOverrideManager
: public OverrideManagerBase
{
281 virtual bool CheckValidNewID(uint16 testid
) { return testid
!= 0xFF; }
283 ObjectOverrideManager(uint16 offset
, uint16 maximum
, uint16 invalid
) :
284 OverrideManagerBase(offset
, maximum
, invalid
) {}
286 void SetEntitySpec(ObjectSpec
*spec
);
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
;
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
= nullptr);
300 CommandCost
GetErrorMessageFromLocationCallbackResult(uint16 cb_res
, const GRFFile
*grffile
, StringID default_error
);
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
);
307 * Data related to the handling of grf files.
308 * @tparam Tcnt Number of spritegroups
310 template <size_t Tcnt
>
311 struct GRFFilePropsBase
{
312 GRFFilePropsBase() : local_id(0), grffile(nullptr)
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
));
319 uint16 local_id
; ///< id defined by the grf file for this entity
320 const struct GRFFile
*grffile
; ///< grf file that introduced this entity
321 const struct SpriteGroup
*spritegroup
[Tcnt
]; ///< pointer to the different sprites of the entity
324 /** Data related to the handling of grf files. */
325 struct GRFFileProps
: GRFFilePropsBase
<1> {
326 /** Set all default data constructor for the props. */
327 GRFFileProps(uint16 subst_id
= 0) :
328 GRFFilePropsBase
<1>(), subst_id(subst_id
), override(subst_id
)
333 uint16 override
; ///< id of the entity been replaced by
336 #endif /* NEWGRF_COMMONS_H */