(svn r23005) -Fix (r23004): Of course there's still the 16-sprite version for shore...
[openttd/fttd.git] / src / newgrf_spritegroup.h
blob91c018c359037ab4a7ba46501f1c80de58ee730b
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 "gfx_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"
26 /**
27 * Gets the value of a so-called newgrf "register".
28 * @param i index of the register
29 * @pre i < 0x110
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);
38 /**
39 * Clears the value of a so-called newgrf "register".
40 * @param i index of the register
41 * @pre i < 0x110
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 {
51 SGT_REAL,
52 SGT_DETERMINISTIC,
53 SGT_RANDOMIZED,
54 SGT_CALLBACK,
55 SGT_RESULT,
56 SGT_TILELAYOUT,
57 SGT_INDUSTRY_PRODUCTION,
60 struct SpriteGroup;
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> {
71 protected:
72 SpriteGroup(SpriteGroupType type) : type(type) {}
73 /** Base sprite group resolver */
74 virtual const SpriteGroup *Resolve(struct ResolverObject *object) const { return this; };
76 public:
77 virtual ~SpriteGroup() {}
79 SpriteGroupType type;
81 virtual SpriteID GetResult() const { return 0; }
82 virtual byte GetNumResults() const { return 0; }
83 virtual uint16 GetCallbackResult() const { return CALLBACK_FAILED; }
85 /**
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
102 * groups. */
103 struct RealSpriteGroup : SpriteGroup {
104 RealSpriteGroup() : SpriteGroup(SGT_REAL) {}
105 ~RealSpriteGroup();
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
112 * of da stuff. */
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)
119 protected:
120 const SpriteGroup *Resolve(ResolverObject *object) const;
123 /* Shared by deterministic and random groups. */
124 enum VarSpriteGroupScope {
125 VSG_BEGIN,
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)
131 VSG_END
133 DECLARE_POSTFIX_INCREMENT(VarSpriteGroupScope)
135 enum DeterministicSpriteGroupSize {
136 DSG_SIZE_BYTE,
137 DSG_SIZE_WORD,
138 DSG_SIZE_DWORD,
141 enum DeterministicSpriteGroupAdjustType {
142 DSGA_TYPE_NONE,
143 DSGA_TYPE_DIV,
144 DSGA_TYPE_MOD,
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;
177 byte variable;
178 byte parameter; ///< Used for variables between 0x60 and 0x7F inclusive.
179 byte shift_num;
180 uint32 and_mask;
181 uint32 add_val;
182 uint32 divmod_val;
183 const SpriteGroup *subroutine;
187 struct DeterministicSpriteGroupRange {
188 const SpriteGroup *group;
189 uint32 low;
190 uint32 high;
194 struct DeterministicSpriteGroup : SpriteGroup {
195 DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
196 ~DeterministicSpriteGroup();
198 VarSpriteGroupScope var_scope;
199 DeterministicSpriteGroupSize size;
200 uint num_adjusts;
201 byte num_ranges;
202 DeterministicSpriteGroupAdjust *adjusts;
203 DeterministicSpriteGroupRange *ranges; // Dynamically allocated
205 /* Dynamically allocated, this is the sole owner */
206 const SpriteGroup *default_group;
208 protected:
209 const SpriteGroup *Resolve(ResolverObject *object) const;
212 enum RandomizedSpriteGroupCompareMode {
213 RSG_CMP_ANY,
214 RSG_CMP_ALL,
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:
224 byte triggers;
225 byte count;
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:
232 protected:
233 const SpriteGroup *Resolve(ResolverObject *object) const;
237 /* This contains a callback result. A failed callback has a value of
238 * CALLBACK_FAILED */
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),
246 result(value)
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;
252 } else {
253 this->result &= ~0x8000;
257 uint16 result;
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),
273 sprite(sprite),
274 num_sprites(num_sprites)
278 SpriteID sprite;
279 byte 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) {}
299 uint8 version;
300 int16 subtract_input[3]; // signed
301 uint16 add_output[2]; // unsigned
302 uint8 again;
306 struct ResolverObject {
307 CallbackID callback;
308 uint32 callback_param1;
309 uint32 callback_param2;
311 byte trigger;
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
321 union {
322 struct {
323 const struct Vehicle *self;
324 const struct Vehicle *parent;
325 EngineID self_type;
326 bool info_view; ///< Indicates if the item is being drawn in an info window
327 } vehicle;
328 struct {
329 TileIndex tile;
330 } canal;
331 struct {
332 TileIndex tile;
333 struct BaseStation *st;
334 const struct StationSpec *statspec;
335 CargoID cargo_type;
336 Axis axis; ///< Station axis, used only for the slope check callback.
337 } station;
338 struct {
339 TileIndex tile;
340 Town *town; ///< Town of this house
341 HouseID house_id;
342 uint16 initial_random_bits; ///< Random bits during construction checks
343 bool not_yet_constructed; ///< True for construction check
344 } house;
345 struct {
346 TileIndex tile;
347 Industry *ind;
348 IndustryGfx gfx;
349 IndustryType type;
350 } industry;
351 struct {
352 const struct CargoSpec *cs;
353 } cargo;
354 struct {
355 CargoID cargo_type;
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".
359 uint8 distance;
360 AIConstructionEvent event;
361 uint8 count;
362 uint8 station_size;
363 } generic;
364 struct {
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?
367 } routes;
368 struct {
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.
373 } airport;
374 struct {
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.
378 } object;
379 } u;
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
395 uint32 sum = 0;
396 for (VarSpriteGroupScope vsg = VSG_BEGIN; vsg < VSG_END; vsg++) {
397 sum |= this->reseed[vsg];
399 return sum;
403 * Resets the dynamic state of the resolver object.
404 * To be called before resolving an Action-1-2-3 chain.
406 void ResetState()
408 this->last_value = 0;
409 this->trigger = 0;
410 memset(this->reseed, 0, sizeof(this->reseed));
414 #endif /* NEWGRF_SPRITEGROUP_H */