10#ifndef NEWGRF_SPRITEGROUP_H
11#define NEWGRF_SPRITEGROUP_H
29using CallbackResult = uint16_t;
37using ResolverResult = std::variant<std::monostate, CallbackResult, const ResultSpriteGroup *, const TileLayoutSpriteGroup *, const IndustryProductionSpriteGroup *>;
44extern SpriteGroupPool _spritegroup_pool;
47struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> {
49 SpriteGroup(SpriteGroupID
index) : SpriteGroupPool::PoolItem<&_spritegroup_pool>(
index) {}
58 virtual ~SpriteGroup() =
default;
60 uint32_t nfo_line = 0;
70struct SpecializedSpriteGroup :
public SpriteGroup {
71 inline SpecializedSpriteGroup(SpriteGroupID
index) : SpriteGroup(
index) {}
78 template <
typename... Targs>
79 static inline T *
Create(Targs &&... args)
88struct RealSpriteGroup : SpecializedSpriteGroup<RealSpriteGroup> {
89 RealSpriteGroup(SpriteGroupID
index) : SpecializedSpriteGroup<RealSpriteGroup>(
index) {}
98 std::vector<const SpriteGroup *>
loaded{};
99 std::vector<const SpriteGroup *>
loading{};
117enum DeterministicSpriteGroupSize : uint8_t {
123enum DeterministicSpriteGroupAdjustType : uint8_t {
158 DeterministicSpriteGroupAdjustType type{};
159 uint8_t variable = 0;
161 uint8_t shift_num = 0;
162 uint32_t and_mask = 0;
163 uint32_t add_val = 0;
164 uint32_t divmod_val = 0;
170 bool calculated_result =
false;
183struct DeterministicSpriteGroup : SpecializedSpriteGroup<DeterministicSpriteGroup> {
184 DeterministicSpriteGroup(SpriteGroupID
index) : SpecializedSpriteGroup<DeterministicSpriteGroup>(
index) {}
187 DeterministicSpriteGroupSize size{};
188 std::vector<DeterministicSpriteGroupAdjust> adjusts{};
189 std::vector<DeterministicSpriteGroupRange> ranges{};
200enum RandomizedSpriteGroupCompareMode : uint8_t {
205struct RandomizedSpriteGroup : SpecializedSpriteGroup<RandomizedSpriteGroup> {
206 RandomizedSpriteGroup(SpriteGroupID
index) : SpecializedSpriteGroup<RandomizedSpriteGroup>(
index) {}
211 uint8_t triggers = 0;
216 std::vector<const SpriteGroup *>
groups{};
233 CallbackResult result = 0;
252 uint8_t num_sprites = 0;
262struct TileLayoutSpriteGroup : SpecializedSpriteGroup<TileLayoutSpriteGroup> {
263 TileLayoutSpriteGroup(SpriteGroupID
index) : SpecializedSpriteGroup<TileLayoutSpriteGroup>(
index) {}
273struct IndustryProductionSpriteGroup : SpecializedSpriteGroup<IndustryProductionSpriteGroup> {
274 IndustryProductionSpriteGroup(SpriteGroupID
index) : SpecializedSpriteGroup<IndustryProductionSpriteGroup>(
index) {}
295struct ScopeResolver {
304 virtual uint32_t
GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter,
bool &available)
const;
305 virtual void StorePSA(uint reg, int32_t value);
335 temp_store.ClearChanges();
352 return temp_store.GetValue(i);
363 temp_store.StoreValue(i, value);
386 template <
class TSpriteGroup>
389 auto result = this->DoResolve();
390 const auto *group = std::get_if<const TSpriteGroup *>(&result);
391 if (group ==
nullptr)
return nullptr;
416 auto result = this->DoResolve();
417 const auto *value = std::get_if<CallbackResult>(&result);
419 for (uint i = 0; i < regs100.size(); ++i) {
434 return this->waiting_random_triggers;
442 this->used_random_triggers |= triggers;
454 sum |= this->reseed[vsg];
475template <
class RandomTriggers>
Add dynamic register values to a sprite layout.
Types related to engines.
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Declaration of basic house types and enums.
Types related to the industry.
@ GSF_INVALID
An invalid spec feature.
Callbacks that NewGRFs could implement.
CallbackID
List of implemented NewGRF callbacks.
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
This file simplifies and embeds a common mechanism of loading/saving and mapping of grf entities.
Functions related to generic callbacks.
DeterministicSpriteGroupAdjustOperation
@ DSGA_OP_STOP
store a into persistent storage, indexed by b, return a
@ DSGA_OP_ROR
rotate a b positions to the right
@ DSGA_OP_UCMP
(unsigned) comparison (a < b -> 0, a == b = 1, a > b = 2)
@ DSGA_OP_SAR
(signed) a >> b
@ DSGA_OP_STO
store a into temporary storage, indexed by b. return a
@ DSGA_OP_SMOD
(signed) a % b
@ DSGA_OP_UDIV
(unsigned) a / b
@ DSGA_OP_UMAX
(unsigned) max(a, b)
@ DSGA_OP_SMIN
(signed) min(a, b)
@ DSGA_OP_UMOD
(unsigned) a & b
@ DSGA_OP_SHR
(unsigned) a >> b
@ DSGA_OP_SDIV
(signed) a / b
@ DSGA_OP_SCMP
(signed) comparison (a < b -> 0, a == b = 1, a > b = 2)
@ DSGA_OP_UMIN
(unsigned) min(a, b)
@ DSGA_OP_SMAX
(signed) max(a, b)
@ VSG_SCOPE_SELF
Resolved object itself.
@ VSG_SCOPE_PARENT
Related object of the resolved one.
@ VSG_SCOPE_RELATIVE
Relative position (vehicles only).
std::variant< std::monostate, CallbackResult, const ResultSpriteGroup *, const TileLayoutSpriteGroup *, const IndustryProductionSpriteGroup * > ResolverResult
Result of resolving sprite groups:
Functionality related to the temporary and persistent storage arrays for NewGRFs.
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
ResolverResult Resolve(ResolverObject &object) const override
Resolves a callback or rerandomisation callback to a NewGRF.
CallbackResultSpriteGroup(SpriteGroupID index, CallbackResult value)
Creates a spritegroup representing a callback result.
uint8_t parameter
Used for variables between 0x60 and 0x7F inclusive.
ResolverResult Resolve(ResolverObject &object) const override
Resolves a callback or rerandomisation callback to a NewGRF.
Dynamic data of a loaded NewGRF.
std::array< uint16_t, INDUSTRY_NUM_OUTPUTS > add_output
Add this much output cargo when successful (unsigned, is indirect in cb version 1+).
std::array< CargoType, INDUSTRY_NUM_OUTPUTS > cargo_output
Which output cargoes to add to (only cb version 2).
std::array< CargoType, INDUSTRY_NUM_INPUTS > cargo_input
Which input cargoes to take from (only cb version 2).
std::array< int16_t, INDUSTRY_NUM_INPUTS > subtract_input
Take this much of the input cargo (can be negative, is indirect in cb version 1+).
uint8_t num_input
How many subtract_input values are valid.
uint8_t version
Production callback version used, or 0xFF if marked invalid.
uint8_t num_output
How many add_output values are valid.
ResolverResult Resolve(ResolverObject &) const override
Resolves a callback or rerandomisation callback to a NewGRF.
NewGRF supplied spritelayout.
Templated helper to make a PoolID a single POD value.
static T * Create(Targs &&... args)
const SpriteGroupID index
Base class for all pools.
uint8_t lowest_randbit
Look for this in the per-object randomized bitmask:
VarSpriteGroupScope var_scope
Take this object:
std::vector< const SpriteGroup * > groups
Take the group with appropriate index:
ResolverResult Resolve(ResolverObject &object) const override
Resolves a callback or rerandomisation callback to a NewGRF.
RandomizedSpriteGroupCompareMode cmp_mode
Check for these triggers:
ResolverResult Resolve(ResolverObject &object) const override
Resolves a callback or rerandomisation callback to a NewGRF.
std::vector< const SpriteGroup * > loaded
List of loaded groups (can be SpriteIDs or Callback results).
std::vector< const SpriteGroup * > loading
List of loading groups (can be SpriteIDs or Callback results).
Interface for SpriteGroup-s to access the gamestate.
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
virtual uint32_t GetDebugID() const
Get an identifier for the item being resolved.
void SetRegister(uint i, int32_t value)
Sets the value of a so-called newgrf "register".
uint32_t GetWaitingRandomTriggers() const
Used by RandomizedSpriteGroup: Triggers for rerandomisation.
void AddUsedRandomTriggers(uint32_t triggers)
Used by RandomizedSpriteGroup: Consume triggers.
uint32_t callback_param2
Second parameter (var 18) of the callback.
const TSpriteGroup * Resolve()
Resolve SpriteGroup.
uint32_t used_random_triggers
Subset of cur_triggers, which actually triggered some rerandomisation. (scope independent).
int32_t GetRegister(uint i) const
Gets the value of a so-called newgrf "register".
uint32_t GetReseedSum() const
Returns the OR-sum of all bits that need reseeding independent of the scope they were accessed with.
ResolverObject(const GRFFile *grffile, CallbackID callback=CBID_NO_CALLBACK, uint32_t callback_param1=0, uint32_t callback_param2=0)
Resolver constructor.
ScopeResolver default_scope
Default implementation of the grf scope.
CallbackID callback
Callback being resolved.
uint32_t callback_param1
First parameter (var 10) of the callback.
virtual const SpriteGroup * ResolveReal(const RealSpriteGroup &group) const
Get the real sprites of the grf.
uint32_t last_value
Result of most recent DeterministicSpriteGroup (including procedure calls).
virtual GrfSpecFeature GetFeature() const
Get the feature number being resolved for.
void ResolveRerandomisation()
Resolve bits to be rerandomised.
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0)
Get a resolver for the scope.
uint32_t waiting_random_triggers
Waiting triggers to be used by any rerandomisation. (scope independent).
CallbackResult ResolveCallback(std::span< int32_t > regs100)
Resolve callback.
std::array< uint32_t, VSG_END > reseed
Collects bits to rerandomise while triggering triggers.
ResolverResult Resolve(ResolverObject &) const override
Resolves a callback or rerandomisation callback to a NewGRF.
ResultSpriteGroup(SpriteGroupID index, SpriteID sprite, uint8_t num_sprites)
Creates a spritegroup representing a sprite number result.
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
virtual void StorePSA(uint reg, int32_t value)
Store a value into the persistent storage area (PSA).
virtual uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const
Get a variable value.
ResolverObject & ro
Surrounding resolver object.
virtual uint32_t GetRandomTriggers() const
Get the triggers.
virtual uint32_t GetRandomBits() const
Get a few random bits.
Specialization of ResolverObject with type-safe access to RandomTriggers.
void SetWaitingRandomTriggers(RandomTriggers triggers)
Set waiting triggers for rerandomisation.
RandomTriggers GetUsedRandomTriggers() const
Get the triggers, which were "consumed" by some rerandomisation.
ResolverObject(const GRFFile *grffile, CallbackID callback=CBID_NO_CALLBACK, uint32_t callback_param1=0, uint32_t callback_param2=0)
Resolver constructor.
static T * Create(Targs &&... args)
Creates a new T-object in the SpriteGroup pool.
virtual ResolverResult Resolve(ResolverObject &object) const =0
Resolves a callback or rerandomisation callback to a NewGRF.
Class for temporary storage of data.
Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
SpriteLayoutProcessor ProcessRegisters(const ResolverObject &object, uint8_t *stage) const
Process registers and the construction stage into the sprite layout.
ResolverResult Resolve(ResolverObject &) const override
Resolves a callback or rerandomisation callback to a NewGRF.