OpenTTD Source 20250524-master-gc366e6a48e
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 <http://www.gnu.org/licenses/>.
6 */
7
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
24/* List of different sprite group types */
25enum SpriteGroupType : uint8_t {
26 SGT_REAL,
27 SGT_DETERMINISTIC,
28 SGT_RANDOMIZED,
29 SGT_CALLBACK,
30 SGT_RESULT,
31 SGT_TILELAYOUT,
32 SGT_INDUSTRY_PRODUCTION,
33};
34
35struct SpriteGroup;
36struct ResolverObject;
37using CallbackResult = uint16_t;
38
45using ResolverResult = std::variant<std::monostate, CallbackResult, const SpriteGroup *>;
46
47/* SPRITE_WIDTH is 24. ECS has roughly 30 sprite groups per real sprite.
48 * Adding an 'extra' margin would be assuming 64 sprite groups per real
49 * sprite. 64 = 2^6, so 2^30 should be enough (for now) */
52extern SpriteGroupPool _spritegroup_pool;
53
54/* Common wrapper for all the different sprite group types */
55struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> {
56protected:
57 SpriteGroup(SpriteGroupType type) : type(type) {}
59 virtual ResolverResult Resolve([[maybe_unused]] ResolverObject &object) const { return this; };
60
61public:
62 virtual ~SpriteGroup() = default;
63
64 uint32_t nfo_line = 0;
65 SpriteGroupType type{};
66
67 static ResolverResult Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level = true);
68};
69
70
71/* 'Real' sprite groups contain a list of other result or callback sprite
72 * groups. */
74 RealSpriteGroup() : SpriteGroup(SGT_REAL) {}
75
76 /* Loaded = in motion, loading = not moving
77 * Each group contains several spritesets, for various loading stages */
78
79 /* XXX: For stations the meaning is different - loaded is for stations
80 * with small amount of cargo whilst loading is for stations with a lot
81 * of da stuff. */
82
83 std::vector<const SpriteGroup *> loaded{};
84 std::vector<const SpriteGroup *> loading{};
85
86protected:
87 ResolverResult Resolve(ResolverObject &object) const override;
88};
89
90/* Shared by deterministic and random groups. */
91enum VarSpriteGroupScope : uint8_t {
92 VSG_BEGIN,
93
94 VSG_SCOPE_SELF = VSG_BEGIN,
97
98 VSG_END
99};
101
102enum DeterministicSpriteGroupSize : uint8_t {
103 DSG_SIZE_BYTE,
104 DSG_SIZE_WORD,
105 DSG_SIZE_DWORD,
106};
107
108enum DeterministicSpriteGroupAdjustType : uint8_t {
109 DSGA_TYPE_NONE,
110 DSGA_TYPE_DIV,
111 DSGA_TYPE_MOD,
112};
113
139
140
143 DeterministicSpriteGroupAdjustType type{};
144 uint8_t variable = 0;
145 uint8_t parameter = 0;
146 uint8_t shift_num = 0;
147 uint32_t and_mask = 0;
148 uint32_t add_val = 0;
149 uint32_t divmod_val = 0;
150 const SpriteGroup *subroutine = nullptr;
151};
152
153
155 bool calculated_result = false;
156 const SpriteGroup *group = nullptr;
157
158 bool operator==(const DeterministicSpriteGroupResult &) const = default;
159};
160
163 uint32_t low = 0;
164 uint32_t high = 0;
165};
166
167
169 DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
170
171 VarSpriteGroupScope var_scope{};
172 DeterministicSpriteGroupSize size{};
173 std::vector<DeterministicSpriteGroupAdjust> adjusts{};
174 std::vector<DeterministicSpriteGroupRange> ranges{}; // Dynamically allocated
175
176 /* Dynamically allocated, this is the sole owner */
177 DeterministicSpriteGroupResult default_result;
178
179 const SpriteGroup *error_group = nullptr; // was first range, before sorting ranges
180
181protected:
182 ResolverResult Resolve(ResolverObject &object) const override;
183};
184
185enum RandomizedSpriteGroupCompareMode : uint8_t {
186 RSG_CMP_ANY,
187 RSG_CMP_ALL,
188};
189
191 RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED) {}
192
194
195 RandomizedSpriteGroupCompareMode cmp_mode{};
196 uint8_t triggers = 0;
197 uint8_t count = 0;
198
199 uint8_t lowest_randbit = 0;
200
201 std::vector<const SpriteGroup *> groups{};
202
203protected:
204 ResolverResult Resolve(ResolverObject &object) const override;
205};
206
207
208/* This contains a callback result. A failed callback has a value of
209 * CALLBACK_FAILED */
215 explicit CallbackResultSpriteGroup(CallbackResult value) : SpriteGroup(SGT_CALLBACK), result(value) {}
216
217 CallbackResult result = 0;
218
219protected:
220 ResolverResult Resolve(ResolverObject &object) const override;
221};
222
223
224/* A result sprite group returns the first SpriteID and the number of
225 * sprites in the set */
227 static constexpr SpriteGroupType TYPE = SGT_RESULT;
228
235 ResultSpriteGroup(SpriteID sprite, uint8_t num_sprites) :
236 SpriteGroup(TYPE),
237 num_sprites(num_sprites),
238 sprite(sprite)
239 {
240 }
241
242 uint8_t num_sprites = 0;
243 SpriteID sprite = 0;
244};
245
250 static constexpr SpriteGroupType TYPE = SGT_TILELAYOUT;
251
254
255 NewGRFSpriteLayout dts{};
256
257 SpriteLayoutProcessor ProcessRegisters(const ResolverObject &object, uint8_t *stage) const;
258};
259
261 static constexpr SpriteGroupType TYPE = SGT_INDUSTRY_PRODUCTION;
262
264
265 uint8_t version = 0;
266 uint8_t num_input = 0;
267 std::array<int16_t, INDUSTRY_NUM_INPUTS> subtract_input{};
268 std::array<CargoType, INDUSTRY_NUM_INPUTS> cargo_input{};
269 uint8_t num_output = 0;
270 std::array<uint16_t, INDUSTRY_NUM_OUTPUTS> add_output{};
271 std::array<CargoType, INDUSTRY_NUM_OUTPUTS> cargo_output{};
272 uint8_t again = 0;
273
274};
275
284
286 virtual ~ScopeResolver() = default;
287
288 virtual uint32_t GetRandomBits() const;
289 virtual uint32_t GetRandomTriggers() const;
290
291 virtual uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const;
292 virtual void StorePSA(uint reg, int32_t value);
293};
294
302private:
304
305public:
317
318 virtual ~ResolverObject() = default;
319
320 ResolverResult DoResolve()
321 {
322 temp_store.ClearChanges();
323 this->last_value = 0;
324 this->used_random_triggers = 0;
325 this->reseed.fill(0);
326 return SpriteGroup::Resolve(this->root_spritegroup, *this);
327 }
328
330
337 inline int32_t GetRegister(uint i) const
338 {
339 return temp_store.GetValue(i);
340 }
341
348 inline void SetRegister(uint i, int32_t value)
349 {
350 temp_store.StoreValue(i, value);
351 }
352
354 uint32_t callback_param1 = 0;
355 uint32_t callback_param2 = 0;
356
357 uint32_t last_value = 0;
358
359protected:
361 uint32_t used_random_triggers = 0;
362public:
363 std::array<uint32_t, VSG_END> reseed;
364
365 const GRFFile *grffile = nullptr;
366 const SpriteGroup *root_spritegroup = nullptr;
367
373 template <class TSpriteGroup>
374 inline const TSpriteGroup *Resolve()
375 {
376 auto result = this->DoResolve();
377 const auto *group = std::get_if<const SpriteGroup *>(&result);
378 if (group == nullptr || *group == nullptr || (*group)->type != TSpriteGroup::TYPE) return nullptr;
379 return static_cast<const TSpriteGroup *>(*group);
380 }
381
390 {
391 /* The Resolve result has no meaning.
392 * It can be a SpriteSet, a callback result, or even an invalid SpriteGroup reference (nullptr). */
393 this->DoResolve();
394 }
395
401 inline CallbackResult ResolveCallback(std::span<int32_t> regs100)
402 {
403 auto result = this->DoResolve();
404 const auto *value = std::get_if<CallbackResult>(&result);
405 if (value == nullptr) return CALLBACK_FAILED;
406 for (uint i = 0; i < regs100.size(); ++i) {
407 regs100[i] = this->GetRegister(0x100 + i);
408 }
409 return *value;
410 }
411
412 virtual const SpriteGroup *ResolveReal(const RealSpriteGroup &group) const;
413
414 virtual ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0);
415
420 {
421 return this->waiting_random_triggers;
422 }
423
427 void AddUsedRandomTriggers(uint32_t triggers)
428 {
429 this->used_random_triggers |= triggers;
430 }
431
437 uint32_t GetReseedSum() const
438 {
439 uint32_t sum = 0;
440 for (VarSpriteGroupScope vsg = VSG_BEGIN; vsg < VSG_END; vsg++) {
441 sum |= this->reseed[vsg];
442 }
443 return sum;
444 }
445
450 virtual GrfSpecFeature GetFeature() const { return GSF_INVALID; }
456 virtual uint32_t GetDebugID() const { return 0; }
457};
458
462template <class RandomTriggers>
465
470 void SetWaitingRandomTriggers(RandomTriggers triggers)
471 {
472 this->waiting_random_triggers = triggers.base();
473 }
474
479 RandomTriggers GetUsedRandomTriggers() const
480 {
481 return static_cast<RandomTriggers>(this->used_random_triggers);
482 }
483};
484
485#endif /* NEWGRF_SPRITEGROUP_H */
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.
Definition enum_type.hpp:63
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:69
@ GSF_INVALID
An invalid spec feature.
Definition newgrf.h:100
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 simplyfies 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
@ 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 SpriteGroup * > 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
Base sprite group resolver.
CallbackResultSpriteGroup(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
Base sprite group resolver.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:115
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.
NewGRF supplied spritelayout.
Templated helper to make a PoolID a single POD value.
Definition pool_type.hpp:43
Base class for all PoolItems.
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
Base sprite group resolver.
RandomizedSpriteGroupCompareMode cmp_mode
Check for these triggers:
ResolverResult Resolve(ResolverObject &object) const override
Base sprite group resolver.
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.
ResultSpriteGroup(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.
virtual ResolverResult Resolve(ResolverObject &object) const
Base sprite group resolver.
Class for temporary storage of data.
TYPE GetValue(uint pos) const
Gets the value from a given position.
void StoreValue(uint pos, int32_t value)
Stores some value at a given position.
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.
Types related to towns.