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
);
37 * Clears the value of a so-called newgrf "register".
38 * @param i index of the register
41 static inline void ClearRegister(uint i
)
43 extern TemporaryStorageArray
<int32
, 0x110> _temp_store
;
44 _temp_store
.StoreValue(i
, 0);
47 /* List of different sprite group types */
48 enum SpriteGroupType
{
55 SGT_INDUSTRY_PRODUCTION
,
59 typedef uint32 SpriteGroupID
;
61 /* SPRITE_WIDTH is 24. ECS has roughly 30 sprite groups per real sprite.
62 * Adding an 'extra' margin would be assuming 64 sprite groups per real
63 * sprite. 64 = 2^6, so 2^30 should be enough (for now) */
64 typedef Pool
<SpriteGroup
, SpriteGroupID
, 1024, 1 << 30, PT_DATA
> SpriteGroupPool
;
65 extern SpriteGroupPool _spritegroup_pool
;
67 /* Common wrapper for all the different sprite group types */
68 struct SpriteGroup
: SpriteGroupPool::PoolItem
<&_spritegroup_pool
> {
70 SpriteGroup(SpriteGroupType type
) : type(type
) {}
71 /** Base sprite group resolver */
72 virtual const SpriteGroup
*Resolve(struct ResolverObject
*object
) const { return this; };
75 virtual ~SpriteGroup() {}
79 virtual SpriteID
GetResult() const { return 0; }
80 virtual byte
GetNumResults() const { return 0; }
81 virtual uint16
GetCallbackResult() const { return CALLBACK_FAILED
; }
84 * ResolverObject (re)entry point.
85 * This cannot be made a call to a virtual function because virtual functions
86 * do not like NULL and checking for NULL *everywhere* is more cumbersome than
87 * this little helper function.
88 * @param group the group to resolve for
89 * @param object information needed to resolve the group
90 * @return the resolved group
92 static const SpriteGroup
*Resolve(const SpriteGroup
*group
, ResolverObject
*object
)
94 return group
== NULL
? NULL
: group
->Resolve(object
);
99 /* 'Real' sprite groups contain a list of other result or callback sprite
101 struct RealSpriteGroup
: SpriteGroup
{
102 RealSpriteGroup() : SpriteGroup(SGT_REAL
) {}
105 /* Loaded = in motion, loading = not moving
106 * Each group contains several spritesets, for various loading stages */
108 /* XXX: For stations the meaning is different - loaded is for stations
109 * with small amount of cargo whilst loading is for stations with a lot
112 byte num_loaded
; ///< Number of loaded groups
113 byte num_loading
; ///< Number of loading groups
114 const SpriteGroup
**loaded
; ///< List of loaded groups (can be SpriteIDs or Callback results)
115 const SpriteGroup
**loading
; ///< List of loading groups (can be SpriteIDs or Callback results)
118 const SpriteGroup
*Resolve(ResolverObject
*object
) const;
121 /* Shared by deterministic and random groups. */
122 enum VarSpriteGroupScope
{
125 VSG_SCOPE_SELF
= VSG_BEGIN
, ///< Resolved object itself
126 VSG_SCOPE_PARENT
, ///< Related object of the resolved one
127 VSG_SCOPE_RELATIVE
, ///< Relative position (vehicles only)
131 DECLARE_POSTFIX_INCREMENT(VarSpriteGroupScope
)
133 enum DeterministicSpriteGroupSize
{
139 enum DeterministicSpriteGroupAdjustType
{
145 enum DeterministicSpriteGroupAdjustOperation
{
146 DSGA_OP_ADD
, ///< a + b
147 DSGA_OP_SUB
, ///< a - b
148 DSGA_OP_SMIN
, ///< (signed) min(a, b)
149 DSGA_OP_SMAX
, ///< (signed) max(a, b)
150 DSGA_OP_UMIN
, ///< (unsigned) min(a, b)
151 DSGA_OP_UMAX
, ///< (unsigned) max(a, b)
152 DSGA_OP_SDIV
, ///< (signed) a / b
153 DSGA_OP_SMOD
, ///< (signed) a % b
154 DSGA_OP_UDIV
, ///< (unsigned) a / b
155 DSGA_OP_UMOD
, ///< (unsigned) a & b
156 DSGA_OP_MUL
, ///< a * b
157 DSGA_OP_AND
, ///< a & b
158 DSGA_OP_OR
, ///< a | b
159 DSGA_OP_XOR
, ///< a ^ b
160 DSGA_OP_STO
, ///< store a into temporary storage, indexed by b. return a
161 DSGA_OP_RST
, ///< return b
162 DSGA_OP_STOP
, ///< store a into persistent storage, indexed by b, return a
163 DSGA_OP_ROR
, ///< rotate a b positions to the right
164 DSGA_OP_SCMP
, ///< (signed) comparison (a < b -> 0, a == b = 1, a > b = 2)
165 DSGA_OP_UCMP
, ///< (unsigned) comparison (a < b -> 0, a == b = 1, a > b = 2)
166 DSGA_OP_SHL
, ///< a << b
167 DSGA_OP_SHR
, ///< (unsigned) a >> b
168 DSGA_OP_SAR
, ///< (signed) a >> b
172 struct DeterministicSpriteGroupAdjust
{
173 DeterministicSpriteGroupAdjustOperation operation
;
174 DeterministicSpriteGroupAdjustType type
;
176 byte parameter
; ///< Used for variables between 0x60 and 0x7F inclusive.
181 const SpriteGroup
*subroutine
;
185 struct DeterministicSpriteGroupRange
{
186 const SpriteGroup
*group
;
192 struct DeterministicSpriteGroup
: SpriteGroup
{
193 DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC
) {}
194 ~DeterministicSpriteGroup();
196 VarSpriteGroupScope var_scope
;
197 DeterministicSpriteGroupSize size
;
200 DeterministicSpriteGroupAdjust
*adjusts
;
201 DeterministicSpriteGroupRange
*ranges
; // Dynamically allocated
203 /* Dynamically allocated, this is the sole owner */
204 const SpriteGroup
*default_group
;
207 const SpriteGroup
*Resolve(ResolverObject
*object
) const;
210 enum RandomizedSpriteGroupCompareMode
{
215 struct RandomizedSpriteGroup
: SpriteGroup
{
216 RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED
) {}
217 ~RandomizedSpriteGroup();
219 VarSpriteGroupScope var_scope
; ///< Take this object:
221 RandomizedSpriteGroupCompareMode cmp_mode
; ///< Check for these triggers:
225 byte lowest_randbit
; ///< Look for this in the per-object randomized bitmask:
226 byte num_groups
; ///< must be power of 2
228 const SpriteGroup
**groups
; ///< Take the group with appropriate index:
231 const SpriteGroup
*Resolve(ResolverObject
*object
) const;
235 /* This contains a callback result. A failed callback has a value of
237 struct CallbackResultSpriteGroup
: SpriteGroup
{
239 * Creates a spritegroup representing a callback result
240 * @param value The value that was used to represent this callback result
241 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
243 CallbackResultSpriteGroup(uint16 value
, bool grf_version8
) :
244 SpriteGroup(SGT_CALLBACK
),
247 /* Old style callback results (only valid for version < 8) have the highest byte 0xFF so signify it is a callback result.
248 * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
249 if (!grf_version8
&& (this->result
>> 8) == 0xFF) {
250 this->result
&= ~0xFF00;
252 this->result
&= ~0x8000;
257 uint16
GetCallbackResult() const { return this->result
; }
261 /* A result sprite group returns the first SpriteID and the number of
262 * sprites in the set */
263 struct ResultSpriteGroup
: SpriteGroup
{
265 * Creates a spritegroup representing a sprite number result.
266 * @param sprite The sprite number.
267 * @param num_sprites The number of sprites per set.
268 * @return A spritegroup representing the sprite number result.
270 ResultSpriteGroup(SpriteID sprite
, byte num_sprites
) :
271 SpriteGroup(SGT_RESULT
),
273 num_sprites(num_sprites
)
279 SpriteID
GetResult() const { return this->sprite
; }
280 byte
GetNumResults() const { return this->num_sprites
; }
284 * Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
286 struct TileLayoutSpriteGroup
: SpriteGroup
{
287 TileLayoutSpriteGroup() : SpriteGroup(SGT_TILELAYOUT
) {}
288 ~TileLayoutSpriteGroup() {}
290 NewGRFSpriteLayout dts
;
292 const DrawTileSprites
*ProcessRegisters(uint8
*stage
) const;
295 struct IndustryProductionSpriteGroup
: SpriteGroup
{
296 IndustryProductionSpriteGroup() : SpriteGroup(SGT_INDUSTRY_PRODUCTION
) {}
299 int16 subtract_input
[3]; // signed
300 uint16 add_output
[2]; // unsigned
304 struct ResolverObject
;
307 * Interface to query and set values specific to a single #VarSpriteGroupScope (action 2 scope).
309 * Multiple of these interfaces are combined into a #ResolverObject to allow access
310 * to different game entities from a #SpriteGroup-chain (action 1-2-3 chain).
312 struct ScopeResolver
{
313 ResolverObject
*ro
; ///< Surrounding resolver object.
315 ScopeResolver(ResolverObject
*ro
);
316 virtual ~ScopeResolver();
318 virtual uint32
GetRandomBits() const;
319 virtual uint32
GetTriggers() const;
320 virtual void SetTriggers(int triggers
) const;
322 virtual uint32
GetVariable(byte variable
, uint32 parameter
, bool *available
) const;
323 virtual void StorePSA(uint reg
, int32 value
);
327 * Interface for #SpriteGroup-s to access the gamestate.
329 * Using this interface #SpriteGroup-chains (action 1-2-3 chains) can be resolved,
330 * to get the results of callbacks, rerandomisations or normal sprite lookups.
332 struct ResolverObject
{
333 ResolverObject(const GRFFile
*grffile
, CallbackID callback
= CBID_NO_CALLBACK
, uint32 callback_param1
= 0, uint32 callback_param2
= 0);
334 virtual ~ResolverObject();
336 ScopeResolver default_scope
; ///< Default implementation of the grf scope.
338 CallbackID callback
; ///< Callback being resolved.
339 uint32 callback_param1
; ///< First parameter (var 10) of the callback.
340 uint32 callback_param2
; ///< Second parameter (var 18) of the callback.
344 uint32 last_value
; ///< Result of most recent DeterministicSpriteGroup (including procedure calls)
345 uint32 reseed
[VSG_END
]; ///< Collects bits to rerandomise while triggering triggers.
347 const GRFFile
*grffile
; ///< GRFFile the resolved SpriteGroup belongs to
349 virtual const SpriteGroup
*ResolveReal(const RealSpriteGroup
*group
) const;
351 virtual ScopeResolver
*GetScope(VarSpriteGroupScope scope
= VSG_SCOPE_SELF
, byte relative
= 0);
354 * Returns the OR-sum of all bits that need reseeding
355 * independent of the scope they were accessed with.
356 * @return OR-sum of the bits.
358 uint32
GetReseedSum() const
361 for (VarSpriteGroupScope vsg
= VSG_BEGIN
; vsg
< VSG_END
; vsg
++) {
362 sum
|= this->reseed
[vsg
];
368 * Resets the dynamic state of the resolver object.
369 * To be called before resolving an Action-1-2-3 chain.
373 this->last_value
= 0;
375 memset(this->reseed
, 0, sizeof(this->reseed
));
379 #endif /* NEWGRF_SPRITEGROUP_H */