OpenTTD Source 20250218-master-g53dd1258a7
newgrf_commons.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
13#ifndef NEWGRF_COMMONS_H
14#define NEWGRF_COMMONS_H
15
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"
21
28
67
68
74inline uint GetConstructionStageOffset(uint construction_stage, uint num_sprites)
75{
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();
84 }
85}
86
92 uint8_t dodraw;
93 uint8_t sprite;
94 uint8_t palette;
97 union {
98 uint8_t parent[3];
99 uint8_t child[2];
100 } delta;
101 uint8_t sprite_var10;
103};
104
105static const uint TLR_MAX_VAR10 = 7;
106
113 std::vector<DrawTileSeqStruct> seq{};
114 std::vector<TileLayoutRegisters> registers{};
115
121
122 void Allocate(uint num_sprites);
123 void AllocateRegisters();
124
132 {
133 return !this->registers.empty();
134 }
135
136 uint32_t PrepareLayout(uint32_t orig_offset, uint32_t newgrf_ground_offset, uint32_t newgrf_offset, uint constr_stage, bool separate_ground) const;
137 void ProcessRegisters(uint8_t resolved_var10, uint32_t resolved_sprite, bool separate_ground) const;
138
144 std::span<DrawTileSeqStruct> GetLayout(PalSpriteID *ground) const
145 {
146 *ground = result_seq[0].image;
147 return {++result_seq.begin(), result_seq.end()};
148 }
149
150 std::span<const DrawTileSeqStruct> GetSequence() const override { return {this->seq.begin(), this->seq.end()}; }
151
152
153private:
154 static std::vector<DrawTileSeqStruct> result_seq;
155};
156
170 uint32_t grfid;
171 uint16_t entity_id;
172 uint16_t substitute_id;
173};
174
176protected:
177 std::vector<uint16_t> entity_overrides;
178 std::vector<uint32_t> grfid_overrides;
179
180 uint16_t max_offset;
181 uint16_t max_entities;
182
183 uint16_t invalid_id;
184 virtual bool CheckValidNewID([[maybe_unused]] uint16_t testid) { return true; }
185
186public:
187 std::vector<EntityIDMapping> mappings;
188
189 OverrideManagerBase(uint16_t offset, uint16_t maximum, uint16_t invalid);
190 virtual ~OverrideManagerBase() = default;
191
192 void ResetOverride();
193 void ResetMapping();
194
195 void Add(uint16_t local_id, uint32_t grfid, uint entity_type);
196 virtual uint16_t AddEntityID(uint16_t grf_local_id, uint32_t grfid, uint16_t substitute_id);
197
198 uint32_t GetGRFID(uint16_t entity_id) const;
199 uint16_t GetSubstituteID(uint16_t entity_id) const;
200 virtual uint16_t GetID(uint16_t grf_local_id, uint32_t grfid) const;
201
202 inline uint16_t GetMaxMapping() const { return this->max_entities; }
203 inline uint16_t GetMaxOffset() const { return this->max_offset; }
204};
205
206
207struct HouseSpec;
209public:
210 HouseOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
211 OverrideManagerBase(offset, maximum, invalid) {}
212
213 void SetEntitySpec(const HouseSpec *hs);
214};
215
216
217struct IndustrySpec;
219public:
220 IndustryOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
221 OverrideManagerBase(offset, maximum, invalid) {}
222
223 uint16_t AddEntityID(uint16_t grf_local_id, uint32_t grfid, uint16_t substitute_id) override;
224 uint16_t GetID(uint16_t grf_local_id, uint32_t grfid) const override;
225
226 void SetEntitySpec(IndustrySpec *inds);
227};
228
229
230struct IndustryTileSpec;
232protected:
233 bool CheckValidNewID(uint16_t testid) override { return testid != 0xFF; }
234public:
235 IndustryTileOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
236 OverrideManagerBase(offset, maximum, invalid) {}
237
238 void SetEntitySpec(const IndustryTileSpec *indts);
239};
240
241struct AirportSpec;
243public:
244 AirportOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
245 OverrideManagerBase(offset, maximum, invalid) {}
246
247 void SetEntitySpec(AirportSpec *inds);
248};
249
250struct AirportTileSpec;
252protected:
253 bool CheckValidNewID(uint16_t testid) override { return testid != 0xFF; }
254public:
255 AirportTileOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
256 OverrideManagerBase(offset, maximum, invalid) {}
257
258 void SetEntitySpec(const AirportTileSpec *ats);
259};
260
261struct ObjectSpec;
263protected:
264 bool CheckValidNewID(uint16_t testid) override { return testid != 0xFF; }
265public:
266 ObjectOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
267 OverrideManagerBase(offset, maximum, invalid) {}
268
269 void SetEntitySpec(ObjectSpec *spec);
270};
271
272extern HouseOverrideManager _house_mngr;
273extern IndustryOverrideManager _industry_mngr;
274extern IndustryTileOverrideManager _industile_mngr;
275extern AirportOverrideManager _airport_mngr;
276extern AirportTileOverrideManager _airporttile_mngr;
277extern ObjectOverrideManager _object_mngr;
278
279uint32_t GetTerrainType(TileIndex tile, TileContext context = TCX_NORMAL);
280TileIndex GetNearbyTile(uint8_t parameter, TileIndex tile, bool signed_offsets = true, Axis axis = INVALID_AXIS);
281uint32_t GetNearbyTileInformation(TileIndex tile, bool grf_version8);
282uint32_t GetCompanyInfo(CompanyID owner, const struct Livery *l = nullptr);
283CommandCost GetErrorMessageFromLocationCallbackResult(uint16_t cb_res, const GRFFile *grffile, StringID default_error);
284
285void ErrorUnknownCallbackResult(uint32_t grfid, uint16_t cbid, uint16_t cb_res);
286bool ConvertBooleanCallback(const struct GRFFile *grffile, uint16_t cbid, uint16_t cb_res);
287bool Convert8bitBooleanCallback(const struct GRFFile *grffile, uint16_t cbid, uint16_t cb_res);
288
293 uint16_t local_id = 0;
294 uint32_t grfid = 0;
295 const struct GRFFile *grffile = nullptr;
296
301 inline bool HasGrfFile() const { return this->grffile != nullptr; }
302};
303
308template <size_t Tcount>
310 std::array<const struct SpriteGroup *, Tcount> spritegroups{};
311
317 const struct SpriteGroup *GetSpriteGroup(size_t index = 0) const { return this->spritegroups[index]; }
318
324 void SetSpriteGroup(size_t index, const struct SpriteGroup *spritegroup) { this->spritegroups[index] = spritegroup; }
325};
326
331 using CargoSpriteGroup = std::pair<size_t, const struct SpriteGroup *>;
332 std::vector<CargoSpriteGroup> spritegroups;
333
334 const struct SpriteGroup *GetSpriteGroup(size_t index) const;
335 void SetSpriteGroup(size_t index, const struct SpriteGroup *spritegroup);
336};
337
341 constexpr GRFFileProps(uint16_t subst_id = 0) : subst_id(subst_id), override(subst_id) {}
342
343 uint16_t subst_id;
344 uint16_t override;
345};
346
348template <typename T>
350 T label = {};
351 uint8_t subtype = 0;
352};
353
354#endif /* NEWGRF_COMMONS_H */
Helper types related to the allocation of memory.
Common return value for all commands.
void SetEntitySpec(const HouseSpec *hs)
Install the specs into the HouseSpecs array It will find itself the proper slot on which it will go.
uint16_t AddEntityID(uint16_t grf_local_id, uint32_t grfid, uint16_t substitute_id) override
Method to find an entity ID and to mark it as reserved for the Industry to be included.
void SetEntitySpec(IndustrySpec *inds)
Method to install the new industry data in its proper slot The slot assignment is internal of this me...
uint16_t GetID(uint16_t grf_local_id, uint32_t grfid) const override
Return the ID (if ever available) of a previously inserted entity.
void SetEntitySpec(ObjectSpec *spec)
Method to install the new object data in its proper slot The slot assignment is internal of this meth...
uint32_t GetGRFID(uint16_t entity_id) const
Gives the GRFID of the file the entity belongs to.
void ResetMapping()
Resets the mapping, which is used while initializing game.
uint16_t max_entities
what is the amount of entities, old and new summed
virtual uint16_t GetID(uint16_t grf_local_id, uint32_t grfid) const
Return the ID (if ever available) of a previously inserted entity.
uint16_t invalid_id
ID used to detected invalid entities.
void Add(uint16_t local_id, uint32_t grfid, uint entity_type)
Since the entity IDs defined by the GRF file does not necessarily correlate to those used by the game...
std::vector< EntityIDMapping > mappings
mapping of ids from grf files. Public out of convenience
void ResetOverride()
Resets the override, which is used while initializing game.
uint16_t GetSubstituteID(uint16_t entity_id) const
Gives the substitute of the entity, as specified by the grf file.
uint16_t max_offset
what is the length of the original entity's array of specs
virtual uint16_t AddEntityID(uint16_t grf_local_id, uint32_t grfid, uint16_t substitute_id)
Reserves a place in the mapping array for an entity to be installed.
Types related to commands.
Types related to companies.
Different types to 'show' directions.
Axis
Allow incrementing of DiagDirDiff variables.
@ INVALID_AXIS
Flag for an invalid Axis.
#define DECLARE_ENUM_AS_BIT_SET(enum_type)
Operators to allow to work with enum as with type safe bit set in C++.
Definition enum_type.hpp:70
bool Convert8bitBooleanCallback(const GRFFile *grffile, uint16_t cbid, uint16_t cb_res)
Converts a callback result into a boolean.
uint32_t GetCompanyInfo(CompanyID owner, const Livery *l)
Returns company information like in vehicle var 43 or station var 43.
bool ConvertBooleanCallback(const GRFFile *grffile, uint16_t cbid, uint16_t cb_res)
Converts a callback result into a boolean.
TileContext
Context for tile accesses.
@ TCX_UPPER_HALFTILE
Querying information about the upper part of a tile with halftile foundation.
@ TCX_ON_BRIDGE
Querying information about stuff on the bridge (via some bridgehead).
@ TCX_NORMAL
Nothing special.
TileLayoutFlags
Flags to enable register usage in sprite layouts.
@ TLF_BB_Z_OFFSET
Add signed offset to bounding box Z positions from register TileLayoutRegisters::delta....
@ TLF_CUSTOM_PALETTE
Palette is from Action 1 (moved to SPRITE_MODIFIER_CUSTOM_SPRITE in palette during loading).
@ TLF_SPRITE
Add signed offset to sprite from register TileLayoutRegisters::sprite.
@ TLF_CHILD_X_OFFSET
Add signed offset to child sprite X positions from register TileLayoutRegisters::delta....
@ TLF_DRAWING_FLAGS
Flags which are still required after loading the GRF.
@ TLF_DODRAW
Only draw sprite if value of register TileLayoutRegisters::dodraw is non-zero.
@ TLF_PALETTE_REG_FLAGS
Flags which require resolving the action-1-2-3 chain for the palette, even if it is no action-1 palet...
@ TLF_NON_GROUND_FLAGS
Flags which do not work for the (first) ground sprite.
@ TLF_BB_XY_OFFSET
Add signed offset to bounding box X and Y positions from register TileLayoutRegisters::delta....
@ TLF_SPRITE_REG_FLAGS
Flags which require resolving the action-1-2-3 chain for the sprite, even if it is no action-1 sprite...
@ TLF_PALETTE_VAR10
Resolve palette with a specific value in variable 10.
@ TLF_SPRITE_VAR10
Resolve sprite with a specific value in variable 10.
@ TLF_KNOWN_FLAGS
Known flags. Any unknown set flag will disable the GRF.
@ TLF_VAR10_FLAGS
Flags which refer to using multiple action-1-2-3 chains.
@ TLF_PALETTE
Add signed offset to palette from register TileLayoutRegisters::palette.
@ TLF_CHILD_Y_OFFSET
Add signed offset to child sprite Y positions from register TileLayoutRegisters::delta....
static const uint TLR_MAX_VAR10
Maximum value for var 10.
CommandCost GetErrorMessageFromLocationCallbackResult(uint16_t cb_res, const GRFFile *grffile, StringID default_error)
Get the error message from a shape/location/slope check callback result.
void ErrorUnknownCallbackResult(uint32_t grfid, uint16_t cbid, uint16_t cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
uint32_t GetTerrainType(TileIndex tile, TileContext context=TCX_NORMAL)
Function used by houses (and soon industries) to get information on type of "terrain" the tile it is ...
uint32_t GetNearbyTileInformation(TileIndex tile, bool grf_version8)
Common part of station var 0x67, house var 0x62, indtile var 0x60, industry var 0x62.
uint GetConstructionStageOffset(uint construction_stage, uint num_sprites)
Determines which sprite to use from a spriteset for a specific construction stage.
TileIndex GetNearbyTile(uint8_t parameter, TileIndex tile, bool signed_offsets=true, Axis axis=INVALID_AXIS)
Get the tile at the given offset.
Base for drawing complex sprites.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Defines the data structure for an airport.
Defines the data structure of each individual tile of an airport.
Ground palette sprite of a tile, together with its sprite layout.
Definition sprite.h:46
PalSpriteID ground
Palette and sprite for the ground.
Definition sprite.h:47
Maps an entity id stored on the map to a GRF file.
uint16_t substitute_id
The (original) entity ID to use if this GRF is not available.
uint32_t grfid
The GRF ID of the file the entity belongs to.
uint16_t entity_id
The entity ID within the GRF file.
Fixed-length list of sprite groups for an entity.
void SetSpriteGroup(size_t index, const struct SpriteGroup *spritegroup)
Set the SpriteGroup at the specified index.
const struct SpriteGroup * GetSpriteGroup(size_t index=0) const
Get the SpriteGroup at the specified index.
std::array< const struct SpriteGroup *, Tcount > spritegroups
pointers to the different sprite groups of the entity
Base data related to the handling of grf files.
const struct GRFFile * grffile
grf file that introduced this entity
uint16_t local_id
id defined by the grf file for this entity
uint32_t grfid
grfid that introduced this entity.
bool HasGrfFile() const
Test if this entity was introduced by NewGRF.
Data related to the handling of grf files.
constexpr GRFFileProps(uint16_t subst_id=0)
Set all default data constructor for the props.
uint16_t override
id of the entity been replaced by
Dynamic data of a loaded NewGRF.
Definition newgrf.h:112
Defines the data structure for constructing industry.
Defines the data structure of each individual tile of an industry.
Container for a label for rail or road type conversion.
uint8_t subtype
Subtype of type (road or tram).
T label
Label of rail or road type.
Information about a particular livery.
Definition livery.h:78
NewGRF supplied spritelayout.
void Allocate(uint num_sprites)
Allocate a spritelayout for num_sprites building sprites.
uint32_t PrepareLayout(uint32_t orig_offset, uint32_t newgrf_ground_offset, uint32_t newgrf_offset, uint constr_stage, bool separate_ground) const
Prepares a sprite layout before resolving action-1-2-3 chains.
uint consistent_max_offset
Number of sprites in all referenced spritesets.
void ProcessRegisters(uint8_t resolved_var10, uint32_t resolved_sprite, bool separate_ground) const
Evaluates the register modifiers and integrates them into the preprocessed sprite layout.
bool NeedsPreprocessing() const
Tests whether this spritelayout needs preprocessing by PrepareLayout() and ProcessRegisters(),...
std::span< DrawTileSeqStruct > GetLayout(PalSpriteID *ground) const
Returns the result spritelayout after preprocessing.
void AllocateRegisters()
Allocate memory for register modifiers.
static std::vector< DrawTileSeqStruct > result_seq
Temporary storage when preprocessing spritelayouts.
Allow incrementing of ObjectClassID variables.
Combination of a palette sprite and a 'real' sprite.
Definition gfx_type.h:22
Additional modifiers for items in sprite layouts.
uint8_t parent[3]
Registers for signed offsets for the bounding box position of parent sprites.
TileLayoutFlags flags
Flags defining which members are valid and to be used.
uint8_t dodraw
Register deciding whether the sprite shall be drawn at all. Non-zero means drawing.
uint16_t max_sprite_offset
Maximum offset to add to the sprite. (limited by size of the spriteset)
uint8_t palette
Register specifying a signed offset for the palette.
uint8_t sprite_var10
Value for variable 10 when resolving the sprite.
uint16_t max_palette_offset
Maximum offset to add to the palette. (limited by size of the spriteset)
uint8_t palette_var10
Value for variable 10 when resolving the palette.
uint8_t child[2]
Registers for signed offsets for the position of child sprites.
uint8_t sprite
Register specifying a signed offset for the sprite.
Variable-length list of sprite groups for an entity.
void SetSpriteGroup(size_t index, const struct SpriteGroup *spritegroup)
Set the SpriteGroup at the specified index.
std::vector< CargoSpriteGroup > spritegroups
pointers to the different sprite groups of the entity
const struct SpriteGroup * GetSpriteGroup(size_t index) const
Get the SpriteGroup at the specified index.