Fix: Don't allow right-click to close world generation progress window. (#13084)
[openttd-github.git] / src / newgrf_commons.h
bloba45fcdf5924dbb5e0702666ce1a609ce2f43bde6
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 */
8 /**
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
16 #include "sprite.h"
17 #include "core/alloc_type.hpp"
18 #include "command_type.h"
19 #include "direction_type.h"
20 #include "company_type.h"
22 /** Context for tile accesses */
23 enum TileContext {
24 TCX_NORMAL, ///< Nothing special.
25 TCX_UPPER_HALFTILE, ///< Querying information about the upper part of a tile with halftile foundation.
26 TCX_ON_BRIDGE, ///< Querying information about stuff on the bridge (via some bridgehead).
29 /**
30 * Flags to enable register usage in sprite layouts.
32 enum TileLayoutFlags {
33 TLF_NOTHING = 0x00,
35 TLF_DODRAW = 0x01, ///< Only draw sprite if value of register TileLayoutRegisters::dodraw is non-zero.
36 TLF_SPRITE = 0x02, ///< Add signed offset to sprite from register TileLayoutRegisters::sprite.
37 TLF_PALETTE = 0x04, ///< Add signed offset to palette from register TileLayoutRegisters::palette.
38 TLF_CUSTOM_PALETTE = 0x08, ///< Palette is from Action 1 (moved to SPRITE_MODIFIER_CUSTOM_SPRITE in palette during loading).
40 TLF_BB_XY_OFFSET = 0x10, ///< Add signed offset to bounding box X and Y positions from register TileLayoutRegisters::delta.parent[0..1].
41 TLF_BB_Z_OFFSET = 0x20, ///< Add signed offset to bounding box Z positions from register TileLayoutRegisters::delta.parent[2].
43 TLF_CHILD_X_OFFSET = 0x10, ///< Add signed offset to child sprite X positions from register TileLayoutRegisters::delta.child[0].
44 TLF_CHILD_Y_OFFSET = 0x20, ///< Add signed offset to child sprite Y positions from register TileLayoutRegisters::delta.child[1].
46 TLF_SPRITE_VAR10 = 0x40, ///< Resolve sprite with a specific value in variable 10.
47 TLF_PALETTE_VAR10 = 0x80, ///< Resolve palette with a specific value in variable 10.
49 TLF_KNOWN_FLAGS = 0xFF, ///< Known flags. Any unknown set flag will disable the GRF.
51 /** Flags which are still required after loading the GRF. */
52 TLF_DRAWING_FLAGS = ~TLF_CUSTOM_PALETTE,
54 /** Flags which do not work for the (first) ground sprite. */
55 TLF_NON_GROUND_FLAGS = TLF_BB_XY_OFFSET | TLF_BB_Z_OFFSET | TLF_CHILD_X_OFFSET | TLF_CHILD_Y_OFFSET,
57 /** Flags which refer to using multiple action-1-2-3 chains. */
58 TLF_VAR10_FLAGS = TLF_SPRITE_VAR10 | TLF_PALETTE_VAR10,
60 /** Flags which require resolving the action-1-2-3 chain for the sprite, even if it is no action-1 sprite. */
61 TLF_SPRITE_REG_FLAGS = TLF_DODRAW | TLF_SPRITE | TLF_BB_XY_OFFSET | TLF_BB_Z_OFFSET | TLF_CHILD_X_OFFSET | TLF_CHILD_Y_OFFSET,
63 /** Flags which require resolving the action-1-2-3 chain for the palette, even if it is no action-1 palette. */
64 TLF_PALETTE_REG_FLAGS = TLF_PALETTE,
66 DECLARE_ENUM_AS_BIT_SET(TileLayoutFlags)
68 /**
69 * Determines which sprite to use from a spriteset for a specific construction stage.
70 * @param construction_stage Construction stage 0 - 3.
71 * @param num_sprites Number of available sprites to select stage from.
72 * @return Sprite to use
74 inline uint GetConstructionStageOffset(uint construction_stage, uint num_sprites)
76 assert(num_sprites > 0);
77 if (num_sprites > 4) num_sprites = 4;
78 switch (construction_stage) {
79 case 0: return 0;
80 case 1: return num_sprites > 2 ? 1 : 0;
81 case 2: return num_sprites > 2 ? num_sprites - 2 : 0;
82 case 3: return num_sprites - 1;
83 default: NOT_REACHED();
87 /**
88 * Additional modifiers for items in sprite layouts.
90 struct TileLayoutRegisters {
91 TileLayoutFlags flags; ///< Flags defining which members are valid and to be used.
92 uint8_t dodraw; ///< Register deciding whether the sprite shall be drawn at all. Non-zero means drawing.
93 uint8_t sprite; ///< Register specifying a signed offset for the sprite.
94 uint8_t palette; ///< Register specifying a signed offset for the palette.
95 uint16_t max_sprite_offset; ///< Maximum offset to add to the sprite. (limited by size of the spriteset)
96 uint16_t max_palette_offset; ///< Maximum offset to add to the palette. (limited by size of the spriteset)
97 union {
98 uint8_t parent[3]; ///< Registers for signed offsets for the bounding box position of parent sprites.
99 uint8_t child[2]; ///< Registers for signed offsets for the position of child sprites.
100 } delta;
101 uint8_t sprite_var10; ///< Value for variable 10 when resolving the sprite.
102 uint8_t palette_var10; ///< Value for variable 10 when resolving the palette.
105 static const uint TLR_MAX_VAR10 = 7; ///< Maximum value for var 10.
108 * NewGRF supplied spritelayout.
109 * In contrast to #DrawTileSprites this struct is for allocated
110 * layouts on the heap. It allocates data and frees them on destruction.
112 struct NewGRFSpriteLayout : ZeroedMemoryAllocator, DrawTileSprites {
113 const TileLayoutRegisters *registers;
116 * Number of sprites in all referenced spritesets.
117 * If these numbers are inconsistent, then this is 0 and the real values are in \c registers.
119 uint consistent_max_offset;
121 void Allocate(uint num_sprites);
122 void AllocateRegisters();
123 void Clone(const DrawTileSeqStruct *source);
124 void Clone(const NewGRFSpriteLayout *source);
127 * Clone a spritelayout.
128 * @param source The spritelayout to copy.
130 void Clone(const DrawTileSprites *source)
132 assert(source != nullptr && this != source);
133 this->ground = source->ground;
134 this->Clone(source->seq);
137 virtual ~NewGRFSpriteLayout()
139 free(this->seq);
140 free(this->registers);
144 * Tests whether this spritelayout needs preprocessing by
145 * #PrepareLayout() and #ProcessRegisters(), or whether it can be
146 * used directly.
147 * @return true if preprocessing is needed
149 bool NeedsPreprocessing() const
151 return this->registers != nullptr;
154 uint32_t PrepareLayout(uint32_t orig_offset, uint32_t newgrf_ground_offset, uint32_t newgrf_offset, uint constr_stage, bool separate_ground) const;
155 void ProcessRegisters(uint8_t resolved_var10, uint32_t resolved_sprite, bool separate_ground) const;
158 * Returns the result spritelayout after preprocessing.
159 * @pre #PrepareLayout() and #ProcessRegisters() need calling first.
160 * @return result spritelayout
162 const DrawTileSeqStruct *GetLayout(PalSpriteID *ground) const
164 DrawTileSeqStruct *front = result_seq.data();
165 *ground = front->image;
166 return front + 1;
169 private:
170 static std::vector<DrawTileSeqStruct> result_seq; ///< Temporary storage when preprocessing spritelayouts.
174 * Maps an entity id stored on the map to a GRF file.
175 * Entities are objects used ingame (houses, industries, industry tiles) for
176 * which we need to correlate the ids from the grf files with the ones in the
177 * the savegames themselves.
178 * An array of EntityIDMapping structs is saved with the savegame so
179 * that those GRFs can be loaded in a different order, or removed safely. The
180 * index in the array is the entity's ID stored on the map.
182 * The substitute ID is the ID of an original entity that should be used instead
183 * if the GRF containing the new entity is not available.
185 struct EntityIDMapping {
186 uint32_t grfid; ///< The GRF ID of the file the entity belongs to
187 uint16_t entity_id; ///< The entity ID within the GRF file
188 uint16_t substitute_id; ///< The (original) entity ID to use if this GRF is not available
191 class OverrideManagerBase {
192 protected:
193 std::vector<uint16_t> entity_overrides;
194 std::vector<uint32_t> grfid_overrides;
196 uint16_t max_offset; ///< what is the length of the original entity's array of specs
197 uint16_t max_entities; ///< what is the amount of entities, old and new summed
199 uint16_t invalid_id; ///< ID used to detected invalid entities
200 virtual bool CheckValidNewID([[maybe_unused]] uint16_t testid) { return true; }
202 public:
203 std::vector<EntityIDMapping> mappings; ///< mapping of ids from grf files. Public out of convenience
205 OverrideManagerBase(uint16_t offset, uint16_t maximum, uint16_t invalid);
206 virtual ~OverrideManagerBase() = default;
208 void ResetOverride();
209 void ResetMapping();
211 void Add(uint16_t local_id, uint32_t grfid, uint entity_type);
212 virtual uint16_t AddEntityID(uint16_t grf_local_id, uint32_t grfid, uint16_t substitute_id);
214 uint32_t GetGRFID(uint16_t entity_id) const;
215 uint16_t GetSubstituteID(uint16_t entity_id) const;
216 virtual uint16_t GetID(uint16_t grf_local_id, uint32_t grfid) const;
218 inline uint16_t GetMaxMapping() const { return this->max_entities; }
219 inline uint16_t GetMaxOffset() const { return this->max_offset; }
223 struct HouseSpec;
224 class HouseOverrideManager : public OverrideManagerBase {
225 public:
226 HouseOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
227 OverrideManagerBase(offset, maximum, invalid) {}
229 void SetEntitySpec(const HouseSpec *hs);
233 struct IndustrySpec;
234 class IndustryOverrideManager : public OverrideManagerBase {
235 public:
236 IndustryOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
237 OverrideManagerBase(offset, maximum, invalid) {}
239 uint16_t AddEntityID(uint16_t grf_local_id, uint32_t grfid, uint16_t substitute_id) override;
240 uint16_t GetID(uint16_t grf_local_id, uint32_t grfid) const override;
242 void SetEntitySpec(IndustrySpec *inds);
246 struct IndustryTileSpec;
247 class IndustryTileOverrideManager : public OverrideManagerBase {
248 protected:
249 bool CheckValidNewID(uint16_t testid) override { return testid != 0xFF; }
250 public:
251 IndustryTileOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
252 OverrideManagerBase(offset, maximum, invalid) {}
254 void SetEntitySpec(const IndustryTileSpec *indts);
257 struct AirportSpec;
258 class AirportOverrideManager : public OverrideManagerBase {
259 public:
260 AirportOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
261 OverrideManagerBase(offset, maximum, invalid) {}
263 void SetEntitySpec(AirportSpec *inds);
266 struct AirportTileSpec;
267 class AirportTileOverrideManager : public OverrideManagerBase {
268 protected:
269 bool CheckValidNewID(uint16_t testid) override { return testid != 0xFF; }
270 public:
271 AirportTileOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
272 OverrideManagerBase(offset, maximum, invalid) {}
274 void SetEntitySpec(const AirportTileSpec *ats);
277 struct ObjectSpec;
278 class ObjectOverrideManager : public OverrideManagerBase {
279 protected:
280 bool CheckValidNewID(uint16_t testid) override { return testid != 0xFF; }
281 public:
282 ObjectOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
283 OverrideManagerBase(offset, maximum, invalid) {}
285 void SetEntitySpec(ObjectSpec *spec);
288 extern HouseOverrideManager _house_mngr;
289 extern IndustryOverrideManager _industry_mngr;
290 extern IndustryTileOverrideManager _industile_mngr;
291 extern AirportOverrideManager _airport_mngr;
292 extern AirportTileOverrideManager _airporttile_mngr;
293 extern ObjectOverrideManager _object_mngr;
295 uint32_t GetTerrainType(TileIndex tile, TileContext context = TCX_NORMAL);
296 TileIndex GetNearbyTile(uint8_t parameter, TileIndex tile, bool signed_offsets = true, Axis axis = INVALID_AXIS);
297 uint32_t GetNearbyTileInformation(TileIndex tile, bool grf_version8);
298 uint32_t GetCompanyInfo(CompanyID owner, const struct Livery *l = nullptr);
299 CommandCost GetErrorMessageFromLocationCallbackResult(uint16_t cb_res, const GRFFile *grffile, StringID default_error);
301 void ErrorUnknownCallbackResult(uint32_t grfid, uint16_t cbid, uint16_t cb_res);
302 bool ConvertBooleanCallback(const struct GRFFile *grffile, uint16_t cbid, uint16_t cb_res);
303 bool Convert8bitBooleanCallback(const struct GRFFile *grffile, uint16_t cbid, uint16_t cb_res);
306 * Data related to the handling of grf files.
307 * @tparam Tcnt Number of spritegroups
309 template <size_t Tcnt>
310 struct GRFFilePropsBase {
311 uint16_t local_id = 0; ///< id defined by the grf file for this entity
312 const struct GRFFile *grffile = nullptr; ///< grf file that introduced this entity
313 std::array<const struct SpriteGroup *, Tcnt> spritegroup{}; ///< pointers to the different sprites of the entity
316 /** Data related to the handling of grf files. */
317 struct GRFFileProps : GRFFilePropsBase<1> {
318 /** Set all default data constructor for the props. */
319 constexpr GRFFileProps(uint16_t subst_id = 0) : subst_id(subst_id), override(subst_id) {}
321 uint16_t subst_id;
322 uint16_t override; ///< id of the entity been replaced by
325 #endif /* NEWGRF_COMMONS_H */