OpenTTD Source 20261112-master-gbcbec5e63f
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 "industry_type.h"
15
16#include "newgrf_callbacks.h"
17#include "newgrf_generic.h"
18#include "newgrf_storage.h"
19#include "newgrf_commons.h"
20
21struct SpriteGroup;
25struct ResolverObject;
26using CallbackResult = uint16_t;
27
34using ResolverResult = std::variant<std::monostate, CallbackResult, const ResultSpriteGroup *, const TileLayoutSpriteGroup *, const IndustryProductionSpriteGroup *>;
35
36/* SPRITE_WIDTH is 24. ECS has roughly 30 sprite groups per real sprite.
37 * Adding an 'extra' margin would be assuming 64 sprite groups per real
38 * sprite. 64 = 2^6, so 2^30 should be enough (for now) */
41extern SpriteGroupPool _spritegroup_pool;
42
44struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> {
45protected:
50 SpriteGroup(SpriteGroupID index) : SpriteGroupPool::PoolItem<&_spritegroup_pool>(index) {}
56 virtual ResolverResult Resolve(ResolverObject &object) const = 0;
57
58public:
60 virtual ~SpriteGroup() = default;
61
62 uint32_t nfo_line = 0;
63
64 static ResolverResult Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level = true);
65};
66
67
71template <class T>
77 inline SpecializedSpriteGroup(SpriteGroupID index) : SpriteGroup(index) {}
78
84 template <typename... Targs>
85 static inline T *Create(Targs &&... args)
86 {
87 return SpriteGroup::Create<T>(std::forward<Targs&&>(args)...);
88 }
89};
90
91
93struct RealSpriteGroup : SpecializedSpriteGroup<RealSpriteGroup> {
99
100 /* Loaded = in motion, loading = not moving
101 * Each group contains several spritesets, for various loading stages */
102
103 /* XXX: For stations the meaning is different - loaded is for stations
104 * with small amount of cargo whilst loading is for stations with a lot
105 * of da stuff. */
106
107 std::vector<const SpriteGroup *> loaded{};
108 std::vector<const SpriteGroup *> loading{};
109
110protected:
111 ResolverResult Resolve(ResolverObject &object) const override;
112};
113
115enum class VarSpriteGroupScope : uint8_t {
119
121};
122
129
136
163
164
168 uint8_t variable = 0;
169 uint8_t parameter = 0;
170 uint8_t shift_num = 0;
171 uint32_t and_mask = 0;
172 uint32_t add_val = 0;
173 uint32_t divmod_val = 0;
174 const SpriteGroup *subroutine = nullptr;
175};
176
177
179 bool calculated_result = false;
180 const SpriteGroup *group = nullptr;
181
182 bool operator==(const DeterministicSpriteGroupResult &) const = default;
183};
184
187 uint32_t low = 0;
188 uint32_t high = 0;
189};
190
191
192struct DeterministicSpriteGroup : SpecializedSpriteGroup<DeterministicSpriteGroup> {
193 DeterministicSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup<DeterministicSpriteGroup>(index) {}
194
195 VarSpriteGroupScope var_scope{};
197 std::vector<DeterministicSpriteGroupAdjust> adjusts{};
198 std::vector<DeterministicSpriteGroupRange> ranges{}; // Dynamically allocated
199
202
203 const SpriteGroup *error_group = nullptr;
204
205protected:
206 ResolverResult Resolve(ResolverObject &object) const override;
207};
208
213};
214
215struct RandomizedSpriteGroup : SpecializedSpriteGroup<RandomizedSpriteGroup> {
216 RandomizedSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup<RandomizedSpriteGroup>(index) {}
217
219
221 uint8_t triggers = 0;
222 uint8_t count = 0;
223
224 uint8_t lowest_randbit = 0;
225
226 std::vector<const SpriteGroup *> groups{};
227
228protected:
229 ResolverResult Resolve(ResolverObject &object) const override;
230};
231
232
234struct CallbackResultSpriteGroup : SpecializedSpriteGroup<CallbackResultSpriteGroup> {
240 CallbackResultSpriteGroup(SpriteGroupID index, CallbackResult value) : SpecializedSpriteGroup<CallbackResultSpriteGroup>(index), result(value) {}
241
242 CallbackResult result = 0;
243
244protected:
245 ResolverResult Resolve(ResolverObject &object) const override;
246};
247
248
250struct ResultSpriteGroup : SpecializedSpriteGroup<ResultSpriteGroup> {
257 ResultSpriteGroup(SpriteGroupID index, SpriteID sprite, uint8_t num_sprites) : SpecializedSpriteGroup<ResultSpriteGroup>(index), num_sprites(num_sprites), sprite(sprite) {}
258
259 uint8_t num_sprites = 0;
260 SpriteID sprite = 0;
261
262protected:
263 ResolverResult Resolve(ResolverObject &) const override { return this; }
264};
265
269struct TileLayoutSpriteGroup : SpecializedSpriteGroup<TileLayoutSpriteGroup> {
270 TileLayoutSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup<TileLayoutSpriteGroup>(index) {}
271
272 NewGRFSpriteLayout dts{};
273
274 SpriteLayoutProcessor ProcessRegisters(const ResolverObject &object, uint8_t *stage) const;
275
276protected:
277 ResolverResult Resolve(ResolverObject &) const override { return this; }
278};
279
280struct IndustryProductionSpriteGroup : SpecializedSpriteGroup<IndustryProductionSpriteGroup> {
281 IndustryProductionSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup<IndustryProductionSpriteGroup>(index) {}
282
283 uint8_t version = 0;
284 uint8_t num_input = 0;
285 std::array<int16_t, INDUSTRY_NUM_INPUTS> subtract_input{};
286 std::array<CargoType, INDUSTRY_NUM_INPUTS> cargo_input{};
287 uint8_t num_output = 0;
288 std::array<uint16_t, INDUSTRY_NUM_OUTPUTS> add_output{};
289 std::array<CargoType, INDUSTRY_NUM_OUTPUTS> cargo_output{};
290 uint8_t again = 0;
291
292protected:
293 ResolverResult Resolve(ResolverObject &) const override { return this; }
294};
295
302struct ScopeResolver {
304
305 ScopeResolver(ResolverObject &ro) : ro(ro) {}
307 virtual ~ScopeResolver() = default;
308
309 virtual uint32_t GetRandomBits() const;
310 virtual uint32_t GetRandomTriggers() const;
311
312 virtual uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const;
313 virtual void StorePSA(uint reg, int32_t value);
314};
315
323private:
325
326public:
338
340 virtual ~ResolverObject() = default;
341
342 ResolverResult DoResolve()
343 {
344 temp_store.ClearChanges();
345 this->last_value = 0;
346 this->used_random_triggers = 0;
347 this->reseed.fill(0);
348 return SpriteGroup::Resolve(this->root_spritegroup, *this);
349 }
350
352
359 inline int32_t GetRegister(uint i) const
360 {
361 return temp_store.GetValue(i);
362 }
363
370 inline void SetRegister(uint i, int32_t value)
371 {
372 temp_store.StoreValue(i, value);
373 }
374
376 uint32_t callback_param1 = 0;
377 uint32_t callback_param2 = 0;
378
379 uint32_t last_value = 0;
380
381protected:
383 uint32_t used_random_triggers = 0;
384public:
386
387 const GRFFile *grffile = nullptr;
388 const SpriteGroup *root_spritegroup = nullptr;
389
395 template <class TSpriteGroup>
396 inline const TSpriteGroup *Resolve()
397 {
398 auto result = this->DoResolve();
399 const auto *group = std::get_if<const TSpriteGroup *>(&result);
400 if (group == nullptr) return nullptr;
401 return *group;
402 }
403
412 {
413 /* The Resolve result has no meaning.
414 * It can be a SpriteSet, a callback result, or even an invalid SpriteGroup reference (nullptr). */
415 this->DoResolve();
416 }
417
423 inline CallbackResult ResolveCallback(std::span<int32_t> regs100)
424 {
425 auto result = this->DoResolve();
426 const auto *value = std::get_if<CallbackResult>(&result);
427 if (value == nullptr) return CALLBACK_FAILED;
428 for (uint i = 0; i < regs100.size(); ++i) {
429 regs100[i] = this->GetRegister(0x100 + i);
430 }
431 return *value;
432 }
433
434 virtual const SpriteGroup *ResolveReal(const RealSpriteGroup &group) const;
435
436 virtual ScopeResolver *GetScope(VarSpriteGroupScope scope = VarSpriteGroupScope::Self, uint8_t relative = 0);
437
443 {
444 return this->waiting_random_triggers;
445 }
446
451 void AddUsedRandomTriggers(uint32_t triggers)
452 {
453 this->used_random_triggers |= triggers;
454 }
455
461 uint32_t GetReseedSum() const
462 {
463 uint32_t sum = 0;
465 sum |= this->reseed[vsg];
466 }
467 return sum;
468 }
469
482 virtual uint32_t GetDebugID() const { return 0; }
483
484 void UnhandledVariable(uint8_t variable) const;
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 */
Iterate a range of enum values.
Add dynamic register values to a sprite layout.
#define T
Climate temperate.
Definition engines.h:91
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
Types related to the industry.
GrfSpecFeature
Definition newgrf.h:81
@ Invalid
An invalid spec feature.
Definition newgrf.h:112
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.
DeterministicSpriteGroupSize
Deterministic sprite group variable size.
@ DWord
Treat variable as a DWord.
@ Word
Treat variable as a Word.
@ Byte
Treat variable as a Byte.
DeterministicSpriteGroupAdjustOperation
Deterministic sprite group adjust operation.
@ Stop
store a into persistent storage, indexed by b, return a
@ Sto
store a into temporary storage, indexed by b. return a
@ UCmp
(unsigned) comparison (a < b -> 0, a == b = 1, a > b = 2)
@ Ror
rotate a b positions to the right
@ SCmp
(signed) comparison (a < b -> 0, a == b = 1, a > b = 2)
VarSpriteGroupScope
Shared by deterministic and random groups.
@ Relative
Relative position (vehicles only).
@ Parent
Related object of the resolved one.
@ Self
Resolved object itself.
RandomizedSpriteGroupCompareMode
Randomized sprite group comparisation mode.
@ All
Match if all bits are triggered.
@ Any
Match if any bit is triggered.
DeterministicSpriteGroupAdjustType
Deterministic sprite group adjust type.
@ Div
Apply divide adjustment to value.
@ Mod
Apply modulus adjustment to value.
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:126
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:50
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".
EnumIndexArray< uint32_t, VarSpriteGroupScope, VarSpriteGroupScope::End > reseed
Collects bits to rerandomise while triggering triggers.
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.
void UnhandledVariable(uint8_t variable) const
Log a message for an unhandled NewGRF variable.
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 ScopeResolver * GetScope(VarSpriteGroupScope scope=VarSpriteGroupScope::Self, uint8_t relative=0)
Get a resolver for the scope.
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.
uint32_t waiting_random_triggers
Waiting triggers to be used by any rerandomisation. (scope independent).
CallbackResult ResolveCallback(std::span< int32_t > regs100)
Resolve callback.
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.