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"
17 #include "engine_type.h"
18 #include "core/pool_type.hpp"
19 #include "house_type.h"
21 #include "newgrf_callbacks.h"
22 #include "newgrf_generic.h"
23 #include "newgrf_storage.h"
24 #include "newgrf_commons.h"
27 * Gets the value of a so-called newgrf "register".
28 * @param i index of the register
30 * @return the value of the register
32 static inline uint32
GetRegister(uint i
)
34 extern TemporaryStorageArray
<int32
, 0x110> _temp_store
;
35 return _temp_store
.GetValue(i
);
39 * Clears the value of a so-called newgrf "register".
40 * @param i index of the register
43 static inline void ClearRegister(uint i
)
45 extern TemporaryStorageArray
<int32
, 0x110> _temp_store
;
46 _temp_store
.StoreValue(i
, 0);
49 /* List of different sprite group types */
50 enum SpriteGroupType
{
57 SGT_INDUSTRY_PRODUCTION
,
61 typedef uint32 SpriteGroupID
;
63 /* SPRITE_WIDTH is 24. ECS has roughly 30 sprite groups per real sprite.
64 * Adding an 'extra' margin would be assuming 64 sprite groups per real
65 * sprite. 64 = 2^6, so 2^30 should be enough (for now) */
66 typedef Pool
<SpriteGroup
, SpriteGroupID
, 1024, 1 << 30, PT_DATA
> SpriteGroupPool
;
67 extern SpriteGroupPool _spritegroup_pool
;
69 /* Common wrapper for all the different sprite group types */
70 struct SpriteGroup
: SpriteGroupPool::PoolItem
<&_spritegroup_pool
> {
72 SpriteGroup(SpriteGroupType type
) : type(type
) {}
73 /** Base sprite group resolver */
74 virtual const SpriteGroup
*Resolve(struct ResolverObject
*object
) const { return this; };
77 virtual ~SpriteGroup() {}
81 virtual SpriteID
GetResult() const { return 0; }
82 virtual byte
GetNumResults() const { return 0; }
83 virtual uint16
GetCallbackResult() const { return CALLBACK_FAILED
; }
86 * ResolverObject (re)entry point.
87 * This cannot be made a call to a virtual function because virtual functions
88 * do not like NULL and checking for NULL *everywhere* is more cumbersome than
89 * this little helper function.
90 * @param group the group to resolve for
91 * @param object information needed to resolve the group
92 * @return the resolved group
94 static const SpriteGroup
*Resolve(const SpriteGroup
*group
, ResolverObject
*object
)
96 return group
== NULL
? NULL
: group
->Resolve(object
);
101 /* 'Real' sprite groups contain a list of other result or callback sprite
103 struct RealSpriteGroup
: SpriteGroup
{
104 RealSpriteGroup() : SpriteGroup(SGT_REAL
) {}
107 /* Loaded = in motion, loading = not moving
108 * Each group contains several spritesets, for various loading stages */
110 /* XXX: For stations the meaning is different - loaded is for stations
111 * with small amount of cargo whilst loading is for stations with a lot
114 byte num_loaded
; ///< Number of loaded groups
115 byte num_loading
; ///< Number of loading groups
116 const SpriteGroup
**loaded
; ///< List of loaded groups (can be SpriteIDs or Callback results)
117 const SpriteGroup
**loading
; ///< List of loading groups (can be SpriteIDs or Callback results)
120 const SpriteGroup
*Resolve(ResolverObject
*object
) const;
123 /* Shared by deterministic and random groups. */
124 enum VarSpriteGroupScope
{
127 VSG_SCOPE_SELF
= VSG_BEGIN
, ///< Resolved object itself
128 VSG_SCOPE_PARENT
, ///< Related object of the resolved one
129 VSG_SCOPE_RELATIVE
, ///< Relative position (vehicles only)
133 DECLARE_POSTFIX_INCREMENT(VarSpriteGroupScope
)
135 enum DeterministicSpriteGroupSize
{
141 enum DeterministicSpriteGroupAdjustType
{
147 enum DeterministicSpriteGroupAdjustOperation
{
148 DSGA_OP_ADD
, ///< a + b
149 DSGA_OP_SUB
, ///< a - b
150 DSGA_OP_SMIN
, ///< (signed) min(a, b)
151 DSGA_OP_SMAX
, ///< (signed) max(a, b)
152 DSGA_OP_UMIN
, ///< (unsigned) min(a, b)
153 DSGA_OP_UMAX
, ///< (unsigned) max(a, b)
154 DSGA_OP_SDIV
, ///< (signed) a / b
155 DSGA_OP_SMOD
, ///< (signed) a % b
156 DSGA_OP_UDIV
, ///< (unsigned) a / b
157 DSGA_OP_UMOD
, ///< (unsigned) a & b
158 DSGA_OP_MUL
, ///< a * b
159 DSGA_OP_AND
, ///< a & b
160 DSGA_OP_OR
, ///< a | b
161 DSGA_OP_XOR
, ///< a ^ b
162 DSGA_OP_STO
, ///< store a into temporary storage, indexed by b. return a
163 DSGA_OP_RST
, ///< return b
164 DSGA_OP_STOP
, ///< store a into persistent storage, indexed by b, return a
165 DSGA_OP_ROR
, ///< rotate a b positions to the right
166 DSGA_OP_SCMP
, ///< (signed) comparision (a < b -> 0, a == b = 1, a > b = 2)
167 DSGA_OP_UCMP
, ///< (unsigned) comparision (a < b -> 0, a == b = 1, a > b = 2)
168 DSGA_OP_SHL
, ///< a << b
169 DSGA_OP_SHR
, ///< (unsigned) a >> b
170 DSGA_OP_SAR
, ///< (signed) a >> b
174 struct DeterministicSpriteGroupAdjust
{
175 DeterministicSpriteGroupAdjustOperation operation
;
176 DeterministicSpriteGroupAdjustType type
;
178 byte parameter
; ///< Used for variables between 0x60 and 0x7F inclusive.
183 const SpriteGroup
*subroutine
;
187 struct DeterministicSpriteGroupRange
{
188 const SpriteGroup
*group
;
194 struct DeterministicSpriteGroup
: SpriteGroup
{
195 DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC
) {}
196 ~DeterministicSpriteGroup();
198 VarSpriteGroupScope var_scope
;
199 DeterministicSpriteGroupSize size
;
202 DeterministicSpriteGroupAdjust
*adjusts
;
203 DeterministicSpriteGroupRange
*ranges
; // Dynamically allocated
205 /* Dynamically allocated, this is the sole owner */
206 const SpriteGroup
*default_group
;
209 const SpriteGroup
*Resolve(ResolverObject
*object
) const;
212 enum RandomizedSpriteGroupCompareMode
{
217 struct RandomizedSpriteGroup
: SpriteGroup
{
218 RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED
) {}
219 ~RandomizedSpriteGroup();
221 VarSpriteGroupScope var_scope
; ///< Take this object:
223 RandomizedSpriteGroupCompareMode cmp_mode
; ///< Check for these triggers:
227 byte lowest_randbit
; ///< Look for this in the per-object randomized bitmask:
228 byte num_groups
; ///< must be power of 2
230 const SpriteGroup
**groups
; ///< Take the group with appropriate index:
233 const SpriteGroup
*Resolve(ResolverObject
*object
) const;
237 /* This contains a callback result. A failed callback has a value of
239 struct CallbackResultSpriteGroup
: SpriteGroup
{
241 * Creates a spritegroup representing a callback result
242 * @param value The value that was used to represent this callback result
244 CallbackResultSpriteGroup(uint16 value
) :
245 SpriteGroup(SGT_CALLBACK
),
248 /* Old style callback results have the highest byte 0xFF so signify it is a callback result
249 * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
250 if ((this->result
>> 8) == 0xFF) {
251 this->result
&= ~0xFF00;
253 this->result
&= ~0x8000;
258 uint16
GetCallbackResult() const { return this->result
; }
262 /* A result sprite group returns the first SpriteID and the number of
263 * sprites in the set */
264 struct ResultSpriteGroup
: SpriteGroup
{
266 * Creates a spritegroup representing a sprite number result.
267 * @param sprite The sprite number.
268 * @param num_sprites The number of sprites per set.
269 * @return A spritegroup representing the sprite number result.
271 ResultSpriteGroup(SpriteID sprite
, byte num_sprites
) :
272 SpriteGroup(SGT_RESULT
),
274 num_sprites(num_sprites
)
280 SpriteID
GetResult() const { return this->sprite
; }
281 byte
GetNumResults() const { return this->num_sprites
; }
285 * Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
287 struct TileLayoutSpriteGroup
: SpriteGroup
{
288 TileLayoutSpriteGroup() : SpriteGroup(SGT_TILELAYOUT
) {}
289 ~TileLayoutSpriteGroup() {}
291 NewGRFSpriteLayout dts
;
293 const DrawTileSprites
*ProcessRegisters(uint8
*stage
) const;
296 struct IndustryProductionSpriteGroup
: SpriteGroup
{
297 IndustryProductionSpriteGroup() : SpriteGroup(SGT_INDUSTRY_PRODUCTION
) {}
300 int16 subtract_input
[3]; // signed
301 uint16 add_output
[2]; // unsigned
306 struct ResolverObject
{
308 uint32 callback_param1
;
309 uint32 callback_param2
;
313 uint32 last_value
; ///< Result of most recent DeterministicSpriteGroup (including procedure calls)
314 uint32 reseed
[VSG_END
]; ///< Collects bits to rerandomise while triggering triggers.
316 VarSpriteGroupScope scope
; ///< Scope of currently resolved DeterministicSpriteGroup resp. RandomizedSpriteGroup
317 byte count
; ///< Additional scope for RandomizedSpriteGroup
319 const GRFFile
*grffile
; ///< GRFFile the resolved SpriteGroup belongs to
323 const struct Vehicle
*self
;
324 const struct Vehicle
*parent
;
326 bool info_view
; ///< Indicates if the item is being drawn in an info window
333 struct BaseStation
*st
;
334 const struct StationSpec
*statspec
;
336 Axis axis
; ///< Station axis, used only for the slope check callback.
340 Town
*town
; ///< Town of this house
342 uint16 initial_random_bits
; ///< Random bits during construction checks
343 bool not_yet_constructed
; ///< True for construction check
352 const struct CargoSpec
*cs
;
356 uint8 default_selection
;
357 uint8 src_industry
; ///< Source industry substitute type. 0xFF for "town", 0xFE for "unknown".
358 uint8 dst_industry
; ///< Destination industry substitute type. 0xFF for "town", 0xFE for "unknown".
360 AIConstructionEvent event
;
365 TileIndex tile
; ///< Tracktile. For track on a bridge this is the southern bridgehead.
366 TileContext context
; ///< Are we resolving sprites for the upper halftile, or on a bridge?
369 struct Station
*st
; ///< Station of the airport for which the callback is run, or NULL for build gui.
370 byte airport_id
; ///< Type of airport for which the callback is run
371 byte layout
; ///< Layout of the airport to build.
372 TileIndex tile
; ///< Tile for the callback, only valid for airporttile callbacks.
375 struct Object
*o
; ///< The object the callback is ran for.
376 TileIndex tile
; ///< The tile related to the object.
377 uint8 view
; ///< The view of the object.
381 uint32 (*GetRandomBits
)(const struct ResolverObject
*);
382 uint32 (*GetTriggers
)(const struct ResolverObject
*);
383 void (*SetTriggers
)(const struct ResolverObject
*, int);
384 uint32 (*GetVariable
)(const struct ResolverObject
*, byte
, byte
, bool*);
385 const SpriteGroup
*(*ResolveReal
)(const struct ResolverObject
*, const RealSpriteGroup
*);
386 void (*StorePSA
)(struct ResolverObject
*, uint
, int32
);
389 * Returns the OR-sum of all bits that need reseeding
390 * independent of the scope they were accessed with.
391 * @return OR-sum of the bits.
393 uint32
GetReseedSum() const
396 for (VarSpriteGroupScope vsg
= VSG_BEGIN
; vsg
< VSG_END
; vsg
++) {
397 sum
|= this->reseed
[vsg
];
403 * Resets the dynamic state of the resolver object.
404 * To be called before resolving an Action-1-2-3 chain.
408 this->last_value
= 0;
410 memset(this->reseed
, 0, sizeof(this->reseed
));
414 #endif /* NEWGRF_SPRITEGROUP_H */