Translations update
[openttd/fttd.git] / src / newgrf_spritegroup.h
blob8ddf7ba4daf8c830bed00c764cf7a86b741944e7
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) */
55 /* Common wrapper for all the different sprite group types */
56 struct SpriteGroup : PooledItem <SpriteGroup, SpriteGroupID, 1024, 1 << 30, PT_DATA> {
57 protected:
58 SpriteGroup(SpriteGroupType type) : type(type) {}
59 /** Base sprite group resolver */
60 virtual const SpriteGroup *Resolve(ResolverObject &object) const { return this; };
62 public:
63 virtual ~SpriteGroup() {}
65 SpriteGroupType type;
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);
73 /**
74 * Get a callback result from a SpriteGroup.
75 * @return Callback result.
77 static uint16 CallbackResult (const SpriteGroup *result)
79 return result != NULL ? result->GetCallbackResult() : CALLBACK_FAILED;
84 /* 'Real' sprite groups contain a list of other result or callback sprite
85 * groups. */
86 struct RealSpriteGroup : SpriteGroup {
87 RealSpriteGroup() : SpriteGroup(SGT_REAL) {}
88 ~RealSpriteGroup();
90 /* Loaded = in motion, loading = not moving
91 * Each group contains several spritesets, for various loading stages */
93 /* XXX: For stations the meaning is different - loaded is for stations
94 * with small amount of cargo whilst loading is for stations with a lot
95 * of da stuff. */
97 byte num_loaded; ///< Number of loaded groups
98 byte num_loading; ///< Number of loading groups
99 const SpriteGroup **loaded; ///< List of loaded groups (can be SpriteIDs or Callback results)
100 const SpriteGroup **loading; ///< List of loading groups (can be SpriteIDs or Callback results)
102 protected:
103 const SpriteGroup *Resolve(ResolverObject &object) const;
106 /* Shared by deterministic and random groups. */
107 enum VarSpriteGroupScope {
108 VSG_BEGIN,
110 VSG_SCOPE_SELF = VSG_BEGIN, ///< Resolved object itself
111 VSG_SCOPE_PARENT, ///< Related object of the resolved one
112 VSG_SCOPE_RELATIVE, ///< Relative position (vehicles only)
114 VSG_END
116 DECLARE_POSTFIX_INCREMENT(VarSpriteGroupScope)
118 enum DeterministicSpriteGroupSize {
119 DSG_SIZE_BYTE,
120 DSG_SIZE_WORD,
121 DSG_SIZE_DWORD,
124 enum DeterministicSpriteGroupAdjustType {
125 DSGA_TYPE_NONE,
126 DSGA_TYPE_DIV,
127 DSGA_TYPE_MOD,
130 enum DeterministicSpriteGroupAdjustOperation {
131 DSGA_OP_ADD, ///< a + b
132 DSGA_OP_SUB, ///< a - b
133 DSGA_OP_SMIN, ///< (signed) min(a, b)
134 DSGA_OP_SMAX, ///< (signed) max(a, b)
135 DSGA_OP_UMIN, ///< (unsigned) min(a, b)
136 DSGA_OP_UMAX, ///< (unsigned) max(a, b)
137 DSGA_OP_SDIV, ///< (signed) a / b
138 DSGA_OP_SMOD, ///< (signed) a % b
139 DSGA_OP_UDIV, ///< (unsigned) a / b
140 DSGA_OP_UMOD, ///< (unsigned) a & b
141 DSGA_OP_MUL, ///< a * b
142 DSGA_OP_AND, ///< a & b
143 DSGA_OP_OR, ///< a | b
144 DSGA_OP_XOR, ///< a ^ b
145 DSGA_OP_STO, ///< store a into temporary storage, indexed by b. return a
146 DSGA_OP_RST, ///< return b
147 DSGA_OP_STOP, ///< store a into persistent storage, indexed by b, return a
148 DSGA_OP_ROR, ///< rotate a b positions to the right
149 DSGA_OP_SCMP, ///< (signed) comparison (a < b -> 0, a == b = 1, a > b = 2)
150 DSGA_OP_UCMP, ///< (unsigned) comparison (a < b -> 0, a == b = 1, a > b = 2)
151 DSGA_OP_SHL, ///< a << b
152 DSGA_OP_SHR, ///< (unsigned) a >> b
153 DSGA_OP_SAR, ///< (signed) a >> b
157 struct DeterministicSpriteGroupAdjust {
158 DeterministicSpriteGroupAdjustOperation operation;
159 DeterministicSpriteGroupAdjustType type;
160 byte variable;
161 byte parameter; ///< Used for variables between 0x60 and 0x7F inclusive.
162 byte shift_num;
163 uint32 and_mask;
164 uint32 add_val;
165 uint32 divmod_val;
166 const SpriteGroup *subroutine;
170 struct DeterministicSpriteGroupRange {
171 const SpriteGroup *group;
172 uint32 low;
173 uint32 high;
177 struct DeterministicSpriteGroup : SpriteGroup {
178 DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
179 ~DeterministicSpriteGroup();
181 VarSpriteGroupScope var_scope;
182 DeterministicSpriteGroupSize size;
183 uint num_adjusts;
184 byte num_ranges;
185 DeterministicSpriteGroupAdjust *adjusts;
186 DeterministicSpriteGroupRange *ranges; // Dynamically allocated
188 /* Dynamically allocated, this is the sole owner */
189 const SpriteGroup *default_group;
191 protected:
192 const SpriteGroup *Resolve(ResolverObject &object) const;
195 enum RandomizedSpriteGroupCompareMode {
196 RSG_CMP_ANY,
197 RSG_CMP_ALL,
200 struct RandomizedSpriteGroup : SpriteGroup {
201 RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED) {}
202 ~RandomizedSpriteGroup();
204 VarSpriteGroupScope var_scope; ///< Take this object:
206 RandomizedSpriteGroupCompareMode cmp_mode; ///< Check for these triggers:
207 byte triggers;
208 byte count;
210 byte lowest_randbit; ///< Look for this in the per-object randomized bitmask:
211 byte num_groups; ///< must be power of 2
213 const SpriteGroup **groups; ///< Take the group with appropriate index:
215 protected:
216 const SpriteGroup *Resolve(ResolverObject &object) const;
220 /* This contains a callback result. A failed callback has a value of
221 * CALLBACK_FAILED */
222 struct CallbackResultSpriteGroup : SpriteGroup {
224 * Creates a spritegroup representing a callback result
225 * @param value The value that was used to represent this callback result
226 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
228 CallbackResultSpriteGroup(uint16 value, bool grf_version8) :
229 SpriteGroup(SGT_CALLBACK),
230 result(value)
232 /* Old style callback results (only valid for version < 8) have the highest byte 0xFF so signify it is a callback result.
233 * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
234 if (!grf_version8 && (this->result >> 8) == 0xFF) {
235 this->result &= ~0xFF00;
236 } else {
237 this->result &= ~0x8000;
241 uint16 result;
242 uint16 GetCallbackResult() const { return this->result; }
246 /* A result sprite group returns the first SpriteID and the number of
247 * sprites in the set */
248 struct ResultSpriteGroup : SpriteGroup {
250 * Creates a spritegroup representing a sprite number result.
251 * @param sprite The sprite number.
252 * @param num_sprites The number of sprites per set.
253 * @return A spritegroup representing the sprite number result.
255 ResultSpriteGroup(SpriteID sprite, byte num_sprites) :
256 SpriteGroup(SGT_RESULT),
257 sprite(sprite),
258 num_sprites(num_sprites)
262 SpriteID sprite;
263 byte num_sprites;
264 SpriteID GetResult() const { return this->sprite; }
265 byte GetNumResults() const { return this->num_sprites; }
269 * Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
271 struct TileLayoutSpriteGroup : SpriteGroup {
272 TileLayoutSpriteGroup() : SpriteGroup(SGT_TILELAYOUT) {}
273 ~TileLayoutSpriteGroup() {}
275 NewGRFSpriteLayout dts;
277 const DrawTileSprites *ProcessRegisters(uint8 *stage) const;
280 struct IndustryProductionSpriteGroup : SpriteGroup {
281 IndustryProductionSpriteGroup() : SpriteGroup(SGT_INDUSTRY_PRODUCTION) {}
283 uint8 version;
284 int16 subtract_input[3]; // signed
285 uint16 add_output[2]; // unsigned
286 uint8 again;
290 * Interface to query and set values specific to a single #VarSpriteGroupScope (action 2 scope).
292 * Multiple of these interfaces are combined into a #ResolverObject to allow access
293 * to different game entities from a #SpriteGroup-chain (action 1-2-3 chain).
295 struct ScopeResolver {
296 virtual ~ScopeResolver()
300 virtual uint32 GetRandomBits() const;
301 virtual uint32 GetTriggers() const;
302 virtual void SetTriggers(int triggers) const;
304 virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
305 virtual void StorePSA(uint reg, int32 value);
309 * Interface for #SpriteGroup-s to access the gamestate.
311 * Using this interface #SpriteGroup-chains (action 1-2-3 chains) can be resolved,
312 * to get the results of callbacks, rerandomisations or normal sprite lookups.
314 struct ResolverObject {
315 const GRFFile *const grffile; ///< GRFFile the resolved SpriteGroup belongs to
317 ResolverObject(const GRFFile *grffile, CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
318 virtual ~ResolverObject();
320 ScopeResolver default_scope; ///< Default implementation of the grf scope.
322 CallbackID callback; ///< Callback being resolved.
323 uint32 callback_param1; ///< First parameter (var 10) of the callback.
324 uint32 callback_param2; ///< Second parameter (var 18) of the callback.
326 byte trigger;
328 uint32 last_value; ///< Result of most recent DeterministicSpriteGroup (including procedure calls)
329 uint32 reseed[VSG_END]; ///< Collects bits to rerandomise while triggering triggers.
331 virtual const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
333 virtual ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0);
336 * Returns the OR-sum of all bits that need reseeding
337 * independent of the scope they were accessed with.
338 * @return OR-sum of the bits.
340 uint32 GetReseedSum() const
342 uint32 sum = 0;
343 for (VarSpriteGroupScope vsg = VSG_BEGIN; vsg < VSG_END; vsg++) {
344 sum |= this->reseed[vsg];
346 return sum;
350 * Resets the dynamic state of the resolver object.
351 * To be called before resolving an Action-1-2-3 chain.
353 void ResetState()
355 this->last_value = 0;
356 this->trigger = 0;
357 memset(this->reseed, 0, sizeof(this->reseed));
361 #endif /* NEWGRF_SPRITEGROUP_H */