OpenTTD Source 20260421-master-gc2fbc6fdeb
newgrf_spritegroup.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_SPRITEGROUP_H
11#define NEWGRF_SPRITEGROUP_H
12
13#include "core/pool_type.hpp"
14#include "town_type.h"
15#include "engine_type.h"
16#include "house_type.h"
17#include "industry_type.h"
18
19#include "newgrf_callbacks.h"
20#include "newgrf_generic.h"
21#include "newgrf_storage.h"
22#include "newgrf_commons.h"
23
24struct SpriteGroup;
28struct ResolverObject;
29using CallbackResult = uint16_t;
30
37using ResolverResult = std::variant<std::monostate, CallbackResult, const ResultSpriteGroup *, const TileLayoutSpriteGroup *, const IndustryProductionSpriteGroup *>;
38
39/* SPRITE_WIDTH is 24. ECS has roughly 30 sprite groups per real sprite.
40 * Adding an 'extra' margin would be assuming 64 sprite groups per real
41 * sprite. 64 = 2^6, so 2^30 should be enough (for now) */
44extern SpriteGroupPool _spritegroup_pool;
45
47struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> {
48protected:
53 SpriteGroup(SpriteGroupID index) : SpriteGroupPool::PoolItem<&_spritegroup_pool>(index) {}
59 virtual ResolverResult Resolve(ResolverObject &object) const = 0;
60
61public:
63 virtual ~SpriteGroup() = default;
64
65 uint32_t nfo_line = 0;
66
67 static ResolverResult Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level = true);
68};
69
70
74template <class T>
80 inline SpecializedSpriteGroup(SpriteGroupID index) : SpriteGroup(index) {}
81
87 template <typename... Targs>
88 static inline T *Create(Targs &&... args)
89 {
90 return SpriteGroup::Create<T>(std::forward<Targs&&>(args)...);
91 }
92};
93
94
96struct RealSpriteGroup : SpecializedSpriteGroup<RealSpriteGroup> {
102
103 /* Loaded = in motion, loading = not moving
104 * Each group contains several spritesets, for various loading stages */
105
106 /* XXX: For stations the meaning is different - loaded is for stations
107 * with small amount of cargo whilst loading is for stations with a lot
108 * of da stuff. */
109
110 std::vector<const SpriteGroup *> loaded{};
111 std::vector<const SpriteGroup *> loading{};
112
113protected:
114 ResolverResult Resolve(ResolverObject &object) const override;
115};
116
118enum VarSpriteGroupScope : uint8_t {
119 VSG_BEGIN,
120
121 VSG_SCOPE_SELF = VSG_BEGIN,
124
125 VSG_END
126};
128
129enum DeterministicSpriteGroupSize : uint8_t {
130 DSG_SIZE_BYTE,
131 DSG_SIZE_WORD,
132 DSG_SIZE_DWORD,
133};
134
135enum DeterministicSpriteGroupAdjustType : uint8_t {
136 DSGA_TYPE_NONE,
137 DSGA_TYPE_DIV,
138 DSGA_TYPE_MOD,
139};
140
166
167
170 DeterministicSpriteGroupAdjustType type{};
171 uint8_t variable = 0;
172 uint8_t parameter = 0;
173 uint8_t shift_num = 0;
174 uint32_t and_mask = 0;
175 uint32_t add_val = 0;
176 uint32_t divmod_val = 0;
177 const SpriteGroup *subroutine = nullptr;
178};
179
180
182 bool calculated_result = false;
183 const SpriteGroup *group = nullptr;
184
185 bool operator==(const DeterministicSpriteGroupResult &) const = default;
186};
187
190 uint32_t low = 0;
191 uint32_t high = 0;
192};
193
194
195struct DeterministicSpriteGroup : SpecializedSpriteGroup<DeterministicSpriteGroup> {
196 DeterministicSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup<DeterministicSpriteGroup>(index) {}
197
198 VarSpriteGroupScope var_scope{};
199 DeterministicSpriteGroupSize size{};
200 std::vector<DeterministicSpriteGroupAdjust> adjusts{};
201 std::vector<DeterministicSpriteGroupRange> ranges{}; // Dynamically allocated
202
205
206 const SpriteGroup *error_group = nullptr;
207
208protected:
209 ResolverResult Resolve(ResolverObject &object) const override;
210};
211
212enum RandomizedSpriteGroupCompareMode : uint8_t {
213 RSG_CMP_ANY,
214 RSG_CMP_ALL,
215};
216
217struct RandomizedSpriteGroup : SpecializedSpriteGroup<RandomizedSpriteGroup> {
218 RandomizedSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup<RandomizedSpriteGroup>(index) {}
219
221
222 RandomizedSpriteGroupCompareMode cmp_mode{};
223 uint8_t triggers = 0;
224 uint8_t count = 0;
225
226 uint8_t lowest_randbit = 0;
227
228 std::vector<const SpriteGroup *> groups{};
229
230protected:
231 ResolverResult Resolve(ResolverObject &object) const override;
232};
233
234
236struct CallbackResultSpriteGroup : SpecializedSpriteGroup<CallbackResultSpriteGroup> {
242 CallbackResultSpriteGroup(SpriteGroupID index, CallbackResult value) : SpecializedSpriteGroup<CallbackResultSpriteGroup>(index), result(value) {}
243
244 CallbackResult result = 0;
245
246protected:
247 ResolverResult Resolve(ResolverObject &object) const override;
248};
249
250
252struct ResultSpriteGroup : SpecializedSpriteGroup<ResultSpriteGroup> {
259 ResultSpriteGroup(SpriteGroupID index, SpriteID sprite, uint8_t num_sprites) : SpecializedSpriteGroup<ResultSpriteGroup>(index), num_sprites(num_sprites), sprite(sprite) {}
260
261 uint8_t num_sprites = 0;
262 SpriteID sprite = 0;
263
264protected:
265 ResolverResult Resolve(ResolverObject &) const override { return this; }
266};
267
271struct TileLayoutSpriteGroup : SpecializedSpriteGroup<TileLayoutSpriteGroup> {
272 TileLayoutSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup<TileLayoutSpriteGroup>(index) {}
273
274 NewGRFSpriteLayout dts{};
275
276 SpriteLayoutProcessor ProcessRegisters(const ResolverObject &object, uint8_t *stage) const;
277
278protected:
279 ResolverResult Resolve(ResolverObject &) const override { return this; }
280};
281
282struct IndustryProductionSpriteGroup : SpecializedSpriteGroup<IndustryProductionSpriteGroup> {
283 IndustryProductionSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup<IndustryProductionSpriteGroup>(index) {}
284
285 uint8_t version = 0;
286 uint8_t num_input = 0;
287 std::array<int16_t, INDUSTRY_NUM_INPUTS> subtract_input{};
288 std::array<CargoType, INDUSTRY_NUM_INPUTS> cargo_input{};
289 uint8_t num_output = 0;
290 std::array<uint16_t, INDUSTRY_NUM_OUTPUTS> add_output{};
291 std::array<CargoType, INDUSTRY_NUM_OUTPUTS> cargo_output{};
292 uint8_t again = 0;
293
294protected:
295 ResolverResult Resolve(ResolverObject &) const override { return this; }
296};
297
304struct ScopeResolver {
306
307 ScopeResolver(ResolverObject &ro) : ro(ro) {}
309 virtual ~ScopeResolver() = default;
310
311 virtual uint32_t GetRandomBits() const;
312 virtual uint32_t GetRandomTriggers() const;
313
314 virtual uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const;
315 virtual void StorePSA(uint reg, int32_t value);
316};
317
325private:
327
328public:
340
342 virtual ~ResolverObject() = default;
343
344 ResolverResult DoResolve()
345 {
346 temp_store.ClearChanges();
347 this->last_value = 0;
348 this->used_random_triggers = 0;
349 this->reseed.fill(0);
350 return SpriteGroup::Resolve(this->root_spritegroup, *this);
351 }
352
354
361 inline int32_t GetRegister(uint i) const
362 {
363 return temp_store.GetValue(i);
364 }
365
372 inline void SetRegister(uint i, int32_t value)
373 {
374 temp_store.StoreValue(i, value);
375 }
376
378 uint32_t callback_param1 = 0;
379 uint32_t callback_param2 = 0;
380
381 uint32_t last_value = 0;
382
383protected:
385 uint32_t used_random_triggers = 0;
386public:
387 std::array<uint32_t, VSG_END> reseed;
388
389 const GRFFile *grffile = nullptr;
390 const SpriteGroup *root_spritegroup = nullptr;
391
397 template <class TSpriteGroup>
398 inline const TSpriteGroup *Resolve()
399 {
400 auto result = this->DoResolve();
401 const auto *group = std::get_if<const TSpriteGroup *>(&result);
402 if (group == nullptr) return nullptr;
403 return *group;
404 }
405
414 {
415 /* The Resolve result has no meaning.
416 * It can be a SpriteSet, a callback result, or even an invalid SpriteGroup reference (nullptr). */
417 this->DoResolve();
418 }
419
425 inline CallbackResult ResolveCallback(std::span<int32_t> regs100)
426 {
427 auto result = this->DoResolve();
428 const auto *value = std::get_if<CallbackResult>(&result);
429 if (value == nullptr) return CALLBACK_FAILED;
430 for (uint i = 0; i < regs100.size(); ++i) {
431 regs100[i] = this->GetRegister(0x100 + i);
432 }
433 return *value;
434 }
435
436 virtual const SpriteGroup *ResolveReal(const RealSpriteGroup &group) const;
437
438 virtual ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0);
439
445 {
446 return this->waiting_random_triggers;
447 }
448
453 void AddUsedRandomTriggers(uint32_t triggers)
454 {
455 this->used_random_triggers |= triggers;
456 }
457
463 uint32_t GetReseedSum() const
464 {
465 uint32_t sum = 0;
466 for (VarSpriteGroupScope vsg = VSG_BEGIN; vsg < VSG_END; vsg++) {
467 sum |= this->reseed[vsg];
468 }
469 return sum;
470 }
471
484 virtual uint32_t GetDebugID() const { return 0; }
485};
486
490template <class RandomTriggers>
493
499 void SetWaitingRandomTriggers(RandomTriggers triggers)
500 {
501 this->waiting_random_triggers = triggers.base();
502 }
503
509 RandomTriggers GetUsedRandomTriggers() const
510 {
511 return static_cast<RandomTriggers>(this->used_random_triggers);
512 }
513};
514
515#endif /* NEWGRF_SPRITEGROUP_H */
Add dynamic register values to a sprite layout.
Types related to engines.
#define T
Climate temperate.
Definition engines.h:91
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:86
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
Declaration of basic house types and enums.
Types related to the industry.
GrfSpecFeature
Definition newgrf.h:72
@ Invalid
An invalid spec feature.
Definition newgrf.h:103
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_OR
a | b
@ DSGA_OP_XOR
a ^ b
@ DSGA_OP_SUB
a - b
@ 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_SHL
a << b
@ DSGA_OP_UCMP
(unsigned) comparison (a < b -> 0, a == b = 1, a > b = 2)
@ DSGA_OP_MUL
a * b
@ 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_ADD
a + b
@ DSGA_OP_UMOD
(unsigned) a & b
@ DSGA_OP_SHR
(unsigned) a >> b
@ DSGA_OP_RST
return b
@ DSGA_OP_AND
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)
VarSpriteGroupScope
Shared by deterministic and random groups.
@ 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.
DeterministicSpriteGroupResult default_result
Dynamically allocated, this is the sole owner.
const SpriteGroup * error_group
Was first range, before sorting ranges.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:118
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.
Definition pool_type.hpp:47
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:
'Real' sprite groups contain a list of other result or callback sprite groups.
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).
RealSpriteGroup(SpriteGroupID index)
Create the RealSpriteGroup.
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.
virtual ~ResolverObject()=default
Ensure the destructor of the sub classes are called as well.
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.
A result sprite group returns the first SpriteID and the number of sprites in the set.
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.
virtual ~ScopeResolver()=default
Ensure the destructor of the sub classes are called as well.
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.
SpecializedSpriteGroup(SpriteGroupID index)
Create the SpecializedSpriteGroup.
Common wrapper for all the different sprite group types.
virtual ~SpriteGroup()=default
Ensure the destructor of the sub classes are called as well.
SpriteGroup(SpriteGroupID index)
Create the SpriteGroup.
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.
Types related to towns.