4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file newgrf_spritegroup.h Action 2 handling. */
12 #ifndef NEWGRF_SPRITEGROUP_H
13 #define NEWGRF_SPRITEGROUP_H
15 #include "town_type.h"
16 #include "engine_type.h"
17 #include "house_type.h"
19 #include "newgrf_callbacks.h"
20 #include "newgrf_generic.h"
21 #include "newgrf_storage.h"
22 #include "newgrf_commons.h"
25 * Gets the value of a so-called newgrf "register".
26 * @param i index of the register
28 * @return the value of the register
30 static inline uint32
GetRegister(uint i
)
32 extern TemporaryStorageArray
<int32
, 0x110> _temp_store
;
33 return _temp_store
.GetValue(i
);
36 /* List of different sprite group types */
37 enum SpriteGroupType
{
44 SGT_INDUSTRY_PRODUCTION
,
48 typedef uint32 SpriteGroupID
;
49 struct ResolverObject
;
51 /* SPRITE_WIDTH is 24. ECS has roughly 30 sprite groups per real sprite.
52 * Adding an 'extra' margin would be assuming 64 sprite groups per real
53 * sprite. 64 = 2^6, so 2^30 should be enough (for now) */
55 /* Common wrapper for all the different sprite group types */
56 struct SpriteGroup
: PooledItem
<SpriteGroup
, SpriteGroupID
, 1024, 1 << 30, PT_DATA
> {
58 SpriteGroup(SpriteGroupType type
) : type(type
) {}
59 /** Base sprite group resolver */
60 virtual const SpriteGroup
*Resolve(ResolverObject
&object
) const { return this; };
63 virtual ~SpriteGroup() {}
67 virtual SpriteID
GetResult() const { return 0; }
68 virtual byte
GetNumResults() const { return 0; }
69 virtual uint16
GetCallbackResult() const { return CALLBACK_FAILED
; }
71 static const SpriteGroup
*Resolve(const SpriteGroup
*group
, ResolverObject
&object
, bool top_level
= true);
75 /* 'Real' sprite groups contain a list of other result or callback sprite
77 struct RealSpriteGroup
: SpriteGroup
{
78 RealSpriteGroup() : SpriteGroup(SGT_REAL
) {}
81 /* Loaded = in motion, loading = not moving
82 * Each group contains several spritesets, for various loading stages */
84 /* XXX: For stations the meaning is different - loaded is for stations
85 * with small amount of cargo whilst loading is for stations with a lot
88 byte num_loaded
; ///< Number of loaded groups
89 byte num_loading
; ///< Number of loading groups
90 const SpriteGroup
**loaded
; ///< List of loaded groups (can be SpriteIDs or Callback results)
91 const SpriteGroup
**loading
; ///< List of loading groups (can be SpriteIDs or Callback results)
94 const SpriteGroup
*Resolve(ResolverObject
&object
) const;
97 /* Shared by deterministic and random groups. */
98 enum VarSpriteGroupScope
{
101 VSG_SCOPE_SELF
= VSG_BEGIN
, ///< Resolved object itself
102 VSG_SCOPE_PARENT
, ///< Related object of the resolved one
103 VSG_SCOPE_RELATIVE
, ///< Relative position (vehicles only)
107 DECLARE_POSTFIX_INCREMENT(VarSpriteGroupScope
)
109 enum DeterministicSpriteGroupSize
{
115 enum DeterministicSpriteGroupAdjustType
{
121 enum DeterministicSpriteGroupAdjustOperation
{
122 DSGA_OP_ADD
, ///< a + b
123 DSGA_OP_SUB
, ///< a - b
124 DSGA_OP_SMIN
, ///< (signed) min(a, b)
125 DSGA_OP_SMAX
, ///< (signed) max(a, b)
126 DSGA_OP_UMIN
, ///< (unsigned) min(a, b)
127 DSGA_OP_UMAX
, ///< (unsigned) max(a, b)
128 DSGA_OP_SDIV
, ///< (signed) a / b
129 DSGA_OP_SMOD
, ///< (signed) a % b
130 DSGA_OP_UDIV
, ///< (unsigned) a / b
131 DSGA_OP_UMOD
, ///< (unsigned) a & b
132 DSGA_OP_MUL
, ///< a * b
133 DSGA_OP_AND
, ///< a & b
134 DSGA_OP_OR
, ///< a | b
135 DSGA_OP_XOR
, ///< a ^ b
136 DSGA_OP_STO
, ///< store a into temporary storage, indexed by b. return a
137 DSGA_OP_RST
, ///< return b
138 DSGA_OP_STOP
, ///< store a into persistent storage, indexed by b, return a
139 DSGA_OP_ROR
, ///< rotate a b positions to the right
140 DSGA_OP_SCMP
, ///< (signed) comparison (a < b -> 0, a == b = 1, a > b = 2)
141 DSGA_OP_UCMP
, ///< (unsigned) comparison (a < b -> 0, a == b = 1, a > b = 2)
142 DSGA_OP_SHL
, ///< a << b
143 DSGA_OP_SHR
, ///< (unsigned) a >> b
144 DSGA_OP_SAR
, ///< (signed) a >> b
148 struct DeterministicSpriteGroupAdjust
{
149 DeterministicSpriteGroupAdjustOperation operation
;
150 DeterministicSpriteGroupAdjustType type
;
152 byte parameter
; ///< Used for variables between 0x60 and 0x7F inclusive.
157 const SpriteGroup
*subroutine
;
161 struct DeterministicSpriteGroupRange
{
162 const SpriteGroup
*group
;
168 struct DeterministicSpriteGroup
: SpriteGroup
{
169 DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC
) {}
170 ~DeterministicSpriteGroup();
172 VarSpriteGroupScope var_scope
;
173 DeterministicSpriteGroupSize size
;
176 DeterministicSpriteGroupAdjust
*adjusts
;
177 DeterministicSpriteGroupRange
*ranges
; // Dynamically allocated
179 /* Dynamically allocated, this is the sole owner */
180 const SpriteGroup
*default_group
;
183 const SpriteGroup
*Resolve(ResolverObject
&object
) const;
186 enum RandomizedSpriteGroupCompareMode
{
191 struct RandomizedSpriteGroup
: SpriteGroup
{
192 RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED
) {}
193 ~RandomizedSpriteGroup();
195 VarSpriteGroupScope var_scope
; ///< Take this object:
197 RandomizedSpriteGroupCompareMode cmp_mode
; ///< Check for these triggers:
201 byte lowest_randbit
; ///< Look for this in the per-object randomized bitmask:
202 byte num_groups
; ///< must be power of 2
204 const SpriteGroup
**groups
; ///< Take the group with appropriate index:
207 const SpriteGroup
*Resolve(ResolverObject
&object
) const;
211 /* This contains a callback result. A failed callback has a value of
213 struct CallbackResultSpriteGroup
: SpriteGroup
{
215 * Creates a spritegroup representing a callback result
216 * @param value The value that was used to represent this callback result
217 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
219 CallbackResultSpriteGroup(uint16 value
, bool grf_version8
) :
220 SpriteGroup(SGT_CALLBACK
),
223 /* Old style callback results (only valid for version < 8) have the highest byte 0xFF so signify it is a callback result.
224 * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
225 if (!grf_version8
&& (this->result
>> 8) == 0xFF) {
226 this->result
&= ~0xFF00;
228 this->result
&= ~0x8000;
233 uint16
GetCallbackResult() const { return this->result
; }
237 /* A result sprite group returns the first SpriteID and the number of
238 * sprites in the set */
239 struct ResultSpriteGroup
: SpriteGroup
{
241 * Creates a spritegroup representing a sprite number result.
242 * @param sprite The sprite number.
243 * @param num_sprites The number of sprites per set.
244 * @return A spritegroup representing the sprite number result.
246 ResultSpriteGroup(SpriteID sprite
, byte num_sprites
) :
247 SpriteGroup(SGT_RESULT
),
249 num_sprites(num_sprites
)
255 SpriteID
GetResult() const { return this->sprite
; }
256 byte
GetNumResults() const { return this->num_sprites
; }
260 * Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
262 struct TileLayoutSpriteGroup
: SpriteGroup
{
263 TileLayoutSpriteGroup() : SpriteGroup(SGT_TILELAYOUT
) {}
264 ~TileLayoutSpriteGroup() {}
266 NewGRFSpriteLayout dts
;
268 const DrawTileSprites
*ProcessRegisters(uint8
*stage
) const;
271 struct IndustryProductionSpriteGroup
: SpriteGroup
{
272 IndustryProductionSpriteGroup() : SpriteGroup(SGT_INDUSTRY_PRODUCTION
) {}
275 int16 subtract_input
[3]; // signed
276 uint16 add_output
[2]; // unsigned
281 * Interface to query and set values specific to a single #VarSpriteGroupScope (action 2 scope).
283 * Multiple of these interfaces are combined into a #ResolverObject to allow access
284 * to different game entities from a #SpriteGroup-chain (action 1-2-3 chain).
286 struct ScopeResolver
{
287 ResolverObject
&ro
; ///< Surrounding resolver object.
289 ScopeResolver(ResolverObject
&ro
);
290 virtual ~ScopeResolver();
292 virtual uint32
GetRandomBits() const;
293 virtual uint32
GetTriggers() const;
294 virtual void SetTriggers(int triggers
) const;
296 virtual uint32
GetVariable(byte variable
, uint32 parameter
, bool *available
) const;
297 virtual void StorePSA(uint reg
, int32 value
);
301 * Interface for #SpriteGroup-s to access the gamestate.
303 * Using this interface #SpriteGroup-chains (action 1-2-3 chains) can be resolved,
304 * to get the results of callbacks, rerandomisations or normal sprite lookups.
306 struct ResolverObject
{
307 ResolverObject(const GRFFile
*grffile
, CallbackID callback
= CBID_NO_CALLBACK
, uint32 callback_param1
= 0, uint32 callback_param2
= 0);
308 virtual ~ResolverObject();
310 ScopeResolver default_scope
; ///< Default implementation of the grf scope.
312 CallbackID callback
; ///< Callback being resolved.
313 uint32 callback_param1
; ///< First parameter (var 10) of the callback.
314 uint32 callback_param2
; ///< Second parameter (var 18) of the callback.
318 uint32 last_value
; ///< Result of most recent DeterministicSpriteGroup (including procedure calls)
319 uint32 reseed
[VSG_END
]; ///< Collects bits to rerandomise while triggering triggers.
321 const GRFFile
*grffile
; ///< GRFFile the resolved SpriteGroup belongs to
322 const SpriteGroup
*root_spritegroup
; ///< Root SpriteGroup to use for resolving
325 * Resolve SpriteGroup.
326 * @return Result spritegroup.
328 const SpriteGroup
*Resolve()
330 return SpriteGroup::Resolve(this->root_spritegroup
, *this);
335 * @return Callback result.
337 uint16
ResolveCallback()
339 const SpriteGroup
*result
= Resolve();
340 return result
!= NULL
? result
->GetCallbackResult() : CALLBACK_FAILED
;
343 virtual const SpriteGroup
*ResolveReal(const RealSpriteGroup
*group
) const;
345 virtual ScopeResolver
*GetScope(VarSpriteGroupScope scope
= VSG_SCOPE_SELF
, byte relative
= 0);
348 * Returns the OR-sum of all bits that need reseeding
349 * independent of the scope they were accessed with.
350 * @return OR-sum of the bits.
352 uint32
GetReseedSum() const
355 for (VarSpriteGroupScope vsg
= VSG_BEGIN
; vsg
< VSG_END
; vsg
++) {
356 sum
|= this->reseed
[vsg
];
362 * Resets the dynamic state of the resolver object.
363 * To be called before resolving an Action-1-2-3 chain.
367 this->last_value
= 0;
369 memset(this->reseed
, 0, sizeof(this->reseed
));
373 #endif /* NEWGRF_SPRITEGROUP_H */