(svn r27985) -Codechange: Convert VA2 switches into ones with non-overlapping ranges...
[openttd.git] / src / newgrf_spritegroup.h
blobb2e764599d868f341ab5eedb5b0357270de63f66
1 /* $Id$ */
3 /*
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/>.
8 */
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"
24 /**
25 * Gets the value of a so-called newgrf "register".
26 * @param i index of the register
27 * @pre i < 0x110
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 {
38 SGT_REAL,
39 SGT_DETERMINISTIC,
40 SGT_RANDOMIZED,
41 SGT_CALLBACK,
42 SGT_RESULT,
43 SGT_TILELAYOUT,
44 SGT_INDUSTRY_PRODUCTION,
47 struct SpriteGroup;
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) */
54 typedef Pool<SpriteGroup, SpriteGroupID, 1024, 1 << 30, PT_DATA> SpriteGroupPool;
55 extern SpriteGroupPool _spritegroup_pool;
57 /* Common wrapper for all the different sprite group types */
58 struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> {
59 protected:
60 SpriteGroup(SpriteGroupType type) : type(type) {}
61 /** Base sprite group resolver */
62 virtual const SpriteGroup *Resolve(ResolverObject &object) const { return this; };
64 public:
65 virtual ~SpriteGroup() {}
67 SpriteGroupType type;
69 virtual SpriteID GetResult() const { return 0; }
70 virtual byte GetNumResults() const { return 0; }
71 virtual uint16 GetCallbackResult() const { return CALLBACK_FAILED; }
73 static const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level = true);
77 /* 'Real' sprite groups contain a list of other result or callback sprite
78 * groups. */
79 struct RealSpriteGroup : SpriteGroup {
80 RealSpriteGroup() : SpriteGroup(SGT_REAL) {}
81 ~RealSpriteGroup();
83 /* Loaded = in motion, loading = not moving
84 * Each group contains several spritesets, for various loading stages */
86 /* XXX: For stations the meaning is different - loaded is for stations
87 * with small amount of cargo whilst loading is for stations with a lot
88 * of da stuff. */
90 byte num_loaded; ///< Number of loaded groups
91 byte num_loading; ///< Number of loading groups
92 const SpriteGroup **loaded; ///< List of loaded groups (can be SpriteIDs or Callback results)
93 const SpriteGroup **loading; ///< List of loading groups (can be SpriteIDs or Callback results)
95 protected:
96 const SpriteGroup *Resolve(ResolverObject &object) const;
99 /* Shared by deterministic and random groups. */
100 enum VarSpriteGroupScope {
101 VSG_BEGIN,
103 VSG_SCOPE_SELF = VSG_BEGIN, ///< Resolved object itself
104 VSG_SCOPE_PARENT, ///< Related object of the resolved one
105 VSG_SCOPE_RELATIVE, ///< Relative position (vehicles only)
107 VSG_END
109 DECLARE_POSTFIX_INCREMENT(VarSpriteGroupScope)
111 enum DeterministicSpriteGroupSize {
112 DSG_SIZE_BYTE,
113 DSG_SIZE_WORD,
114 DSG_SIZE_DWORD,
117 enum DeterministicSpriteGroupAdjustType {
118 DSGA_TYPE_NONE,
119 DSGA_TYPE_DIV,
120 DSGA_TYPE_MOD,
123 enum DeterministicSpriteGroupAdjustOperation {
124 DSGA_OP_ADD, ///< a + b
125 DSGA_OP_SUB, ///< a - b
126 DSGA_OP_SMIN, ///< (signed) min(a, b)
127 DSGA_OP_SMAX, ///< (signed) max(a, b)
128 DSGA_OP_UMIN, ///< (unsigned) min(a, b)
129 DSGA_OP_UMAX, ///< (unsigned) max(a, b)
130 DSGA_OP_SDIV, ///< (signed) a / b
131 DSGA_OP_SMOD, ///< (signed) a % b
132 DSGA_OP_UDIV, ///< (unsigned) a / b
133 DSGA_OP_UMOD, ///< (unsigned) a & b
134 DSGA_OP_MUL, ///< a * b
135 DSGA_OP_AND, ///< a & b
136 DSGA_OP_OR, ///< a | b
137 DSGA_OP_XOR, ///< a ^ b
138 DSGA_OP_STO, ///< store a into temporary storage, indexed by b. return a
139 DSGA_OP_RST, ///< return b
140 DSGA_OP_STOP, ///< store a into persistent storage, indexed by b, return a
141 DSGA_OP_ROR, ///< rotate a b positions to the right
142 DSGA_OP_SCMP, ///< (signed) comparison (a < b -> 0, a == b = 1, a > b = 2)
143 DSGA_OP_UCMP, ///< (unsigned) comparison (a < b -> 0, a == b = 1, a > b = 2)
144 DSGA_OP_SHL, ///< a << b
145 DSGA_OP_SHR, ///< (unsigned) a >> b
146 DSGA_OP_SAR, ///< (signed) a >> b
150 struct DeterministicSpriteGroupAdjust {
151 DeterministicSpriteGroupAdjustOperation operation;
152 DeterministicSpriteGroupAdjustType type;
153 byte variable;
154 byte parameter; ///< Used for variables between 0x60 and 0x7F inclusive.
155 byte shift_num;
156 uint32 and_mask;
157 uint32 add_val;
158 uint32 divmod_val;
159 const SpriteGroup *subroutine;
163 struct DeterministicSpriteGroupRange {
164 const SpriteGroup *group;
165 uint32 low;
166 uint32 high;
170 struct DeterministicSpriteGroup : SpriteGroup {
171 DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
172 ~DeterministicSpriteGroup();
174 VarSpriteGroupScope var_scope;
175 DeterministicSpriteGroupSize size;
176 uint num_adjusts;
177 byte num_ranges;
178 DeterministicSpriteGroupAdjust *adjusts;
179 DeterministicSpriteGroupRange *ranges; // Dynamically allocated
181 /* Dynamically allocated, this is the sole owner */
182 const SpriteGroup *default_group;
184 const SpriteGroup *error_group; // was first range, before sorting ranges
186 protected:
187 const SpriteGroup *Resolve(ResolverObject &object) const;
190 enum RandomizedSpriteGroupCompareMode {
191 RSG_CMP_ANY,
192 RSG_CMP_ALL,
195 struct RandomizedSpriteGroup : SpriteGroup {
196 RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED) {}
197 ~RandomizedSpriteGroup();
199 VarSpriteGroupScope var_scope; ///< Take this object:
201 RandomizedSpriteGroupCompareMode cmp_mode; ///< Check for these triggers:
202 byte triggers;
203 byte count;
205 byte lowest_randbit; ///< Look for this in the per-object randomized bitmask:
206 byte num_groups; ///< must be power of 2
208 const SpriteGroup **groups; ///< Take the group with appropriate index:
210 protected:
211 const SpriteGroup *Resolve(ResolverObject &object) const;
215 /* This contains a callback result. A failed callback has a value of
216 * CALLBACK_FAILED */
217 struct CallbackResultSpriteGroup : SpriteGroup {
219 * Creates a spritegroup representing a callback result
220 * @param value The value that was used to represent this callback result
221 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
223 CallbackResultSpriteGroup(uint16 value, bool grf_version8) :
224 SpriteGroup(SGT_CALLBACK),
225 result(value)
227 /* Old style callback results (only valid for version < 8) have the highest byte 0xFF so signify it is a callback result.
228 * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
229 if (!grf_version8 && (this->result >> 8) == 0xFF) {
230 this->result &= ~0xFF00;
231 } else {
232 this->result &= ~0x8000;
236 uint16 result;
237 uint16 GetCallbackResult() const { return this->result; }
241 /* A result sprite group returns the first SpriteID and the number of
242 * sprites in the set */
243 struct ResultSpriteGroup : SpriteGroup {
245 * Creates a spritegroup representing a sprite number result.
246 * @param sprite The sprite number.
247 * @param num_sprites The number of sprites per set.
248 * @return A spritegroup representing the sprite number result.
250 ResultSpriteGroup(SpriteID sprite, byte num_sprites) :
251 SpriteGroup(SGT_RESULT),
252 sprite(sprite),
253 num_sprites(num_sprites)
257 SpriteID sprite;
258 byte num_sprites;
259 SpriteID GetResult() const { return this->sprite; }
260 byte GetNumResults() const { return this->num_sprites; }
264 * Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
266 struct TileLayoutSpriteGroup : SpriteGroup {
267 TileLayoutSpriteGroup() : SpriteGroup(SGT_TILELAYOUT) {}
268 ~TileLayoutSpriteGroup() {}
270 NewGRFSpriteLayout dts;
272 const DrawTileSprites *ProcessRegisters(uint8 *stage) const;
275 struct IndustryProductionSpriteGroup : SpriteGroup {
276 IndustryProductionSpriteGroup() : SpriteGroup(SGT_INDUSTRY_PRODUCTION) {}
278 uint8 version;
279 int16 subtract_input[3]; // signed
280 uint16 add_output[2]; // unsigned
281 uint8 again;
285 * Interface to query and set values specific to a single #VarSpriteGroupScope (action 2 scope).
287 * Multiple of these interfaces are combined into a #ResolverObject to allow access
288 * to different game entities from a #SpriteGroup-chain (action 1-2-3 chain).
290 struct ScopeResolver {
291 ResolverObject &ro; ///< Surrounding resolver object.
293 ScopeResolver(ResolverObject &ro) : ro(ro) {}
294 virtual ~ScopeResolver() {}
296 virtual uint32 GetRandomBits() const;
297 virtual uint32 GetTriggers() const;
299 virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
300 virtual void StorePSA(uint reg, int32 value);
304 * Interface for #SpriteGroup-s to access the gamestate.
306 * Using this interface #SpriteGroup-chains (action 1-2-3 chains) can be resolved,
307 * to get the results of callbacks, rerandomisations or normal sprite lookups.
309 struct ResolverObject {
311 * Resolver constructor.
312 * @param grffile NewGRF file associated with the object (or \c NULL if none).
313 * @param callback Callback code being resolved (default value is #CBID_NO_CALLBACK).
314 * @param callback_param1 First parameter (var 10) of the callback (only used when \a callback is also set).
315 * @param callback_param2 Second parameter (var 18) of the callback (only used when \a callback is also set).
317 ResolverObject(const GRFFile *grffile, CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0)
318 : default_scope(*this), callback(callback), callback_param1(callback_param1), callback_param2(callback_param2), grffile(grffile), root_spritegroup(NULL)
320 this->ResetState();
323 virtual ~ResolverObject() {}
325 ScopeResolver default_scope; ///< Default implementation of the grf scope.
327 CallbackID callback; ///< Callback being resolved.
328 uint32 callback_param1; ///< First parameter (var 10) of the callback.
329 uint32 callback_param2; ///< Second parameter (var 18) of the callback.
331 uint32 last_value; ///< Result of most recent DeterministicSpriteGroup (including procedure calls)
333 uint32 waiting_triggers; ///< Waiting triggers to be used by any rerandomisation. (scope independent)
334 uint32 used_triggers; ///< Subset of cur_triggers, which actually triggered some rerandomisation. (scope independent)
335 uint32 reseed[VSG_END]; ///< Collects bits to rerandomise while triggering triggers.
337 const GRFFile *grffile; ///< GRFFile the resolved SpriteGroup belongs to
338 const SpriteGroup *root_spritegroup; ///< Root SpriteGroup to use for resolving
341 * Resolve SpriteGroup.
342 * @return Result spritegroup.
344 const SpriteGroup *Resolve()
346 return SpriteGroup::Resolve(this->root_spritegroup, *this);
350 * Resolve callback.
351 * @return Callback result.
353 uint16 ResolveCallback()
355 const SpriteGroup *result = Resolve();
356 return result != NULL ? result->GetCallbackResult() : CALLBACK_FAILED;
359 virtual const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
361 virtual ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0);
364 * Returns the waiting triggers that did not trigger any rerandomisation.
366 uint32 GetRemainingTriggers() const
368 return this->waiting_triggers & ~this->used_triggers;
372 * Returns the OR-sum of all bits that need reseeding
373 * independent of the scope they were accessed with.
374 * @return OR-sum of the bits.
376 uint32 GetReseedSum() const
378 uint32 sum = 0;
379 for (VarSpriteGroupScope vsg = VSG_BEGIN; vsg < VSG_END; vsg++) {
380 sum |= this->reseed[vsg];
382 return sum;
386 * Resets the dynamic state of the resolver object.
387 * To be called before resolving an Action-1-2-3 chain.
389 void ResetState()
391 this->last_value = 0;
392 this->waiting_triggers = 0;
393 this->used_triggers = 0;
394 memset(this->reseed, 0, sizeof(this->reseed));
398 #endif /* NEWGRF_SPRITEGROUP_H */