OpenTTD Source 20260208-master-g43af8e94d0
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef NEWGRF_COMMONS_H
11#define NEWGRF_COMMONS_H
12
13#include "sprite.h"
14#include "command_type.h"
15#include "direction_type.h"
16#include "company_type.h"
17#include "cargo_type.h"
18#include "core/bitmath_func.hpp"
19
26
65
66
72inline uint GetConstructionStageOffset(uint construction_stage, uint num_sprites)
73{
74 assert(num_sprites > 0);
75 if (num_sprites > 4) num_sprites = 4;
76 switch (construction_stage) {
77 case 0: return 0;
78 case 1: return num_sprites > 2 ? 1 : 0;
79 case 2: return num_sprites > 2 ? num_sprites - 2 : 0;
80 case 3: return num_sprites - 1;
81 default: NOT_REACHED();
82 }
83}
84
90 uint8_t dodraw;
91 uint8_t sprite;
92 uint8_t palette;
95 union {
96 uint8_t parent[3];
97 uint8_t child[2];
98 } delta;
99 uint8_t sprite_var10;
101};
102
103static const uint TLR_MAX_VAR10 = 7;
104
110struct NewGRFSpriteLayout : DrawTileSprites {
111 std::vector<DrawTileSeqStruct> seq{};
112 std::vector<TileLayoutRegisters> registers{};
113
119
120 void Allocate(uint num_sprites);
121 void AllocateRegisters();
122
129 {
130 return !this->registers.empty();
131 }
132
133 std::span<const DrawTileSeqStruct> GetSequence() const override { return {this->seq.begin(), this->seq.end()}; }
134};
135
139class SpriteLayoutProcessor {
140 const NewGRFSpriteLayout *raw_layout = nullptr;
141 std::vector<DrawTileSeqStruct> result_seq;
142 uint32_t var10_values = 0;
143 bool separate_ground = false;
144public:
145 SpriteLayoutProcessor() = default;
146
148 SpriteLayoutProcessor(const NewGRFSpriteLayout &raw_layout) : raw_layout(&raw_layout) {}
149
150 SpriteLayoutProcessor(const NewGRFSpriteLayout &raw_layout, uint32_t orig_offset, uint32_t newgrf_ground_offset, uint32_t newgrf_offset, uint constr_stage, bool separate_ground);
151
156 SetBitIterator<uint8_t, uint32_t> Var10Values() const { return this->var10_values; }
157
158 void ProcessRegisters(const struct ResolverObject &object, uint8_t resolved_var10, uint32_t resolved_sprite);
159
165 {
166 assert(this->raw_layout != nullptr);
167 if (this->result_seq.empty()) {
168 /* Simple layout without preprocessing. */
169 return {this->raw_layout->ground, this->raw_layout->seq};
170 } else {
171 /* Dynamic layout with preprocessing. */
172 return {this->result_seq[0].image, {++this->result_seq.begin(), this->result_seq.end()}};
173 }
174 }
175};
176
190 uint32_t grfid;
191 uint16_t entity_id;
192 uint16_t substitute_id;
193};
194
196protected:
197 std::vector<uint16_t> entity_overrides;
198 std::vector<uint32_t> grfid_overrides;
199
200 uint16_t max_offset;
201 uint16_t max_entities;
202
203 uint16_t invalid_id;
209 virtual bool CheckValidNewID([[maybe_unused]] uint16_t testid) { return true; }
210
211public:
212 std::vector<EntityIDMapping> mappings;
213
214 OverrideManagerBase(uint16_t offset, uint16_t maximum, uint16_t invalid);
215 virtual ~OverrideManagerBase() = default;
216
217 void ResetOverride();
218 void ResetMapping();
219
220 void Add(uint16_t local_id, uint32_t grfid, uint entity_type);
221 virtual uint16_t AddEntityID(uint16_t grf_local_id, uint32_t grfid, uint16_t substitute_id);
222
223 uint32_t GetGRFID(uint16_t entity_id) const;
224 uint16_t GetSubstituteID(uint16_t entity_id) const;
225 virtual uint16_t GetID(uint16_t grf_local_id, uint32_t grfid) const;
226
227 inline uint16_t GetMaxMapping() const { return this->max_entities; }
228 inline uint16_t GetMaxOffset() const { return this->max_offset; }
229};
230
231
232struct HouseSpec;
233class HouseOverrideManager : public OverrideManagerBase {
234public:
235 HouseOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
236 OverrideManagerBase(offset, maximum, invalid) {}
237
238 void SetEntitySpec(HouseSpec &&hs);
239};
240
241
242struct IndustrySpec;
243class IndustryOverrideManager : public OverrideManagerBase {
244public:
245 IndustryOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
246 OverrideManagerBase(offset, maximum, invalid) {}
247
248 uint16_t AddEntityID(uint16_t grf_local_id, uint32_t grfid, uint16_t substitute_id) override;
249 uint16_t GetID(uint16_t grf_local_id, uint32_t grfid) const override;
250
251 void SetEntitySpec(IndustrySpec &&inds);
252};
253
254
255struct IndustryTileSpec;
256class IndustryTileOverrideManager : public OverrideManagerBase {
257protected:
258 bool CheckValidNewID(uint16_t testid) override { return testid != 0xFF; }
259public:
260 IndustryTileOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
261 OverrideManagerBase(offset, maximum, invalid) {}
262
263 void SetEntitySpec(IndustryTileSpec &&indts);
264};
265
266struct AirportSpec;
267class AirportOverrideManager : public OverrideManagerBase {
268public:
269 AirportOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
270 OverrideManagerBase(offset, maximum, invalid) {}
271
272 void SetEntitySpec(AirportSpec &&inds);
273};
274
275struct AirportTileSpec;
276class AirportTileOverrideManager : public OverrideManagerBase {
277protected:
278 bool CheckValidNewID(uint16_t testid) override { return testid != 0xFF; }
279public:
280 AirportTileOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
281 OverrideManagerBase(offset, maximum, invalid) {}
282
283 void SetEntitySpec(AirportTileSpec &&ats);
284};
285
286struct ObjectSpec;
287class ObjectOverrideManager : public OverrideManagerBase {
288protected:
289 bool CheckValidNewID(uint16_t testid) override { return testid != 0xFF; }
290public:
291 ObjectOverrideManager(uint16_t offset, uint16_t maximum, uint16_t invalid) :
292 OverrideManagerBase(offset, maximum, invalid) {}
293
294 void SetEntitySpec(ObjectSpec &&spec);
295};
296
297extern HouseOverrideManager _house_mngr;
298extern IndustryOverrideManager _industry_mngr;
299extern IndustryTileOverrideManager _industile_mngr;
300extern AirportOverrideManager _airport_mngr;
301extern AirportTileOverrideManager _airporttile_mngr;
302extern ObjectOverrideManager _object_mngr;
303
304uint32_t GetTerrainType(TileIndex tile, TileContext context = TCX_NORMAL);
305TileIndex GetNearbyTile(uint8_t parameter, TileIndex tile, bool signed_offsets = true, Axis axis = INVALID_AXIS);
306uint32_t GetNearbyTileInformation(TileIndex tile, bool grf_version8);
307uint32_t GetCompanyInfo(CompanyID owner, const struct Livery *l = nullptr);
308CommandCost GetErrorMessageFromLocationCallbackResult(uint16_t cb_res, std::span<const int32_t> textstack, const GRFFile *grffile, StringID default_error);
309
310void ErrorUnknownCallbackResult(uint32_t grfid, uint16_t cbid, uint16_t cb_res);
311bool ConvertBooleanCallback(const struct GRFFile *grffile, uint16_t cbid, uint16_t cb_res);
312bool Convert8bitBooleanCallback(const struct GRFFile *grffile, uint16_t cbid, uint16_t cb_res);
313
318 uint16_t local_id = 0;
319 uint32_t grfid = 0;
320 const struct GRFFile *grffile = nullptr;
321
322 void SetGRFFile(const struct GRFFile *grffile);
323
328 inline bool HasGrfFile() const { return this->grffile != nullptr; }
329};
330
336template <class Tkey, size_t Tcount>
338 std::array<const struct SpriteGroup *, Tcount> spritegroups{};
339
345 const struct SpriteGroup *GetSpriteGroup(Tkey index) const { return this->spritegroups[static_cast<size_t>(index)]; }
346
352 const struct SpriteGroup *GetFirstSpriteGroupOf(std::initializer_list<Tkey> indices) const
353 {
354 for (auto key : indices) {
355 auto *result = GetSpriteGroup(key);
356 if (result != nullptr) return result;
357 }
358 return nullptr;
359 }
360
366 void SetSpriteGroup(Tkey index, const struct SpriteGroup *spritegroup) { this->spritegroups[static_cast<size_t>(index)] = spritegroup; }
367};
368
377
381struct StandardGRFFileProps : FixedGRFFileProps<StandardSpriteGroup, static_cast<size_t>(StandardSpriteGroup::End)> {
383
387 bool HasSpriteGroups() const
388 {
390 }
391
396 const struct SpriteGroup *GetSpriteGroup(bool entity_exists) const
397 {
398 auto *res = entity_exists ? nullptr : GetSpriteGroup(StandardSpriteGroup::Purchase);
400 }
401};
402
407template <class Tkey>
409 using ValueType = std::pair<Tkey, const struct SpriteGroup *>;
410 std::vector<ValueType> spritegroups;
411
417 const SpriteGroup *GetSpriteGroup(Tkey index) const
418 {
419 auto it = std::ranges::lower_bound(this->spritegroups, index, std::less{}, &ValueType::first);
420 if (it == std::end(this->spritegroups) || it->first != index) return nullptr;
421 return it->second;
422 }
423
429 const struct SpriteGroup *GetFirstSpriteGroupOf(std::initializer_list<Tkey> indices) const
430 {
431 for (auto key : indices) {
432 auto *result = GetSpriteGroup(key);
433 if (result != nullptr) return result;
434 }
435 return nullptr;
436 }
437
443 void SetSpriteGroup(Tkey index, const SpriteGroup *spritegroup)
444 {
445 auto it = std::ranges::lower_bound(this->spritegroups, index, std::less{}, &ValueType::first);
446 if (it == std::end(this->spritegroups) || it->first != index) {
447 this->spritegroups.emplace(it, index, spritegroup);
448 } else {
449 it->second = spritegroup;
450 }
451 }
452};
453
458 static constexpr CargoType SG_DEFAULT = NUM_CARGO;
459 static constexpr CargoType SG_PURCHASE = NUM_CARGO + 1;
460 static constexpr CargoType SG_DEFAULT_NA = NUM_CARGO + 2;
461};
462
468 constexpr SubstituteGRFFileProps(uint16_t subst_id = 0) : subst_id(subst_id), override_id(subst_id) {}
469
470 uint16_t subst_id;
471 uint16_t override_id;
472};
473
475template <typename T>
477 T label = {};
478 uint8_t subtype = 0;
479};
480
481#endif /* NEWGRF_COMMONS_H */
Functions related to bit mathematics.
Types related to cargoes...
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:73
bool CheckValidNewID(uint16_t testid) override
Checks whether the given ID is valid in the context of this override manager.
Common return value for all commands.
void SetEntitySpec(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.
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(IndustrySpec &&inds)
Method to install the new industry data in its proper slot The slot assignment is internal of this me...
bool CheckValidNewID(uint16_t testid) override
Checks whether the given ID is valid in the context of this override manager.
bool CheckValidNewID(uint16_t testid) override
Checks whether the given ID is valid in the context of this override manager.
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...
OverrideManagerBase(uint16_t offset, uint16_t maximum, uint16_t invalid)
Constructor of generic class.
std::vector< EntityIDMapping > mappings
mapping of ids from grf files. Public out of convenience
virtual bool CheckValidNewID(uint16_t testid)
Checks whether the given ID is valid in the context of this override manager.
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.
Add dynamic register values to a sprite layout.
DrawTileSpriteSpan GetLayout() const
Returns the result spritelayout after preprocessing.
void ProcessRegisters(const struct ResolverObject &object, uint8_t resolved_var10, uint32_t resolved_sprite)
Evaluates the register modifiers and integrates them into the preprocessed sprite layout.
SpriteLayoutProcessor(const NewGRFSpriteLayout &raw_layout)
Constructor for spritelayout, which do not need preprocessing.
SetBitIterator< uint8_t, uint32_t > Var10Values() const
Get values for variable 10 to resolve sprites for.
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++.
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.
StandardSpriteGroup
Standard sprite groups.
@ Purchase
Used before an entity exists.
@ Default
Default type used when no more-specific group matches.
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.
void ErrorUnknownCallbackResult(uint32_t grfid, uint16_t cbid, uint16_t cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
CommandCost GetErrorMessageFromLocationCallbackResult(uint16_t cb_res, std::span< const int32_t > textstack, const GRFFile *grffile, StringID default_error)
Get the error message from a shape/location/slope check 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.
Sprite groups indexed by CargoType.
static constexpr CargoType SG_PURCHASE
Used in purchase lists before an item exists.
static constexpr CargoType SG_DEFAULT
Default type used when no more-specific cargo matches.
static constexpr CargoType SG_DEFAULT_NA
Used only by stations and roads when no more-specific cargo matches.
Ground palette sprite of a tile, together with its sprite layout.
Definition sprite.h:67
PalSpriteID ground
Palette and sprite for the ground.
Definition sprite.h:53
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.
const struct SpriteGroup * GetSpriteGroup(Tkey index) const
Get the SpriteGroup at the specified index.
void SetSpriteGroup(Tkey index, const struct SpriteGroup *spritegroup)
Set the SpriteGroup at the specified index.
std::array< const struct SpriteGroup *, Tcount > spritegroups
pointers to the different sprite groups of the entity
const struct SpriteGroup * GetFirstSpriteGroupOf(std::initializer_list< Tkey > indices) const
Get the first existing SpriteGroup from a list of options.
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.
void SetGRFFile(const struct GRFFile *grffile)
Set the NewGRF file, and its grfid, associated with grf props.
bool HasGrfFile() const
Test if this entity was introduced by NewGRF.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:117
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:79
NewGRF supplied spritelayout.
void Allocate(uint num_sprites)
Allocate a spritelayout for num_sprites building sprites.
uint consistent_max_offset
Number of sprites in all referenced spritesets.
bool NeedsPreprocessing() const
Tests whether this spritelayout needs preprocessing by SpriteLayoutProcessor, or whether it can be us...
void AllocateRegisters()
Allocate memory for register modifiers.
Allow incrementing of ObjectClassID variables.
Interface for SpriteGroup-s to access the gamestate.
Iterable ensemble of each set bit in a value.
Container for standard sprite groups.
const struct SpriteGroup * GetSpriteGroup(bool entity_exists) const
Get the standard sprite group.
bool HasSpriteGroups() const
Check whether the entity has sprite groups.
uint16_t override_id
id of the entity been replaced by
constexpr SubstituteGRFFileProps(uint16_t subst_id=0)
Set all default data constructor for the props.
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.
std::vector< ValueType > spritegroups
pointers to the different sprite groups of the entity
const struct SpriteGroup * GetFirstSpriteGroupOf(std::initializer_list< Tkey > indices) const
Get the first existing SpriteGroup from a list of options.
const SpriteGroup * GetSpriteGroup(Tkey index) const
Get the SpriteGroup at the specified index.
void SetSpriteGroup(Tkey index, const SpriteGroup *spritegroup)
Set the SpriteGroup at the specified index.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92