Files @ r23882:d683a0787bc9
Branch filter:

Location: cpp/openttd-patchpack/source/src/newgrf_spritegroup.h - annotation

Nikolas Nyby
Codechange: Don't use SDL_CreateRGBSurfaceWithFormat()

This function requires libSDL 2.0.5 or higher. It looks like we don't
need to use it, and can just use the original SDL_CreateRGBSurface(),
with the masks set to 0, to trigger the default 8-bit format, which is
SDL_PIXELFORMAT_INDEX8.

Closes #7785

Note: this code path is activated by using an 8-bit blitter, like:

./bin/openttd -b 8bpp-simple
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6348:a905c3e6d8fa
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r8213:466402e95092
r8787:4e60a460f8ef
r12239:4ede50b8e95b
r23041:7dd9cd83d20f
r8787:4e60a460f8ef
r8787:4e60a460f8ef
r8544:31b3976529d7
r8787:4e60a460f8ef
r15743:4f6cef458ee3
r5475:3f5cd13d1b63
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r7168:156fd244b919
r14890:f3cb6e331a95
r17769:40a7de7cde1c
r7168:156fd244b919
r7168:156fd244b919
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r6248:b940b09d7ab8
r11985:d8202dea2203
r21008:be2e3d3c935b
r11993:daa4f143c220
r11993:daa4f143c220
r11993:daa4f143c220
r11993:daa4f143c220
r17360:71e102cec3ce
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11991:9a2b087e564c
r21008:be2e3d3c935b
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11991:9a2b087e564c
r21088:1f99ef8ae88f
r11985:d8202dea2203
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r5475:3f5cd13d1b63
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r11991:9a2b087e564c
r11991:9a2b087e564c
r21008:be2e3d3c935b
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r6248:b940b09d7ab8
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r6248:b940b09d7ab8
r17834:447b5cb3ee1a
r5475:3f5cd13d1b63
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r6248:b940b09d7ab8
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r6521:f9fb7d189c75
r6521:f9fb7d189c75
r7610:6031d09c03ef
r7612:58e7a3f2232d
r19944:25a78576fb5e
r19944:25a78576fb5e
r15657:8639007a57e8
r15657:8639007a57e8
r15657:8639007a57e8
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5617:94e7517dc4e8
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r17908:4f8c767e7fc7
r22761:4149c4b18e24
r22761:4149c4b18e24
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r6348:a905c3e6d8fa
r5475:3f5cd13d1b63
r11991:9a2b087e564c
r22757:59be1e5ea50d
r22757:59be1e5ea50d
r11991:9a2b087e564c
r21008:be2e3d3c935b
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r6348:a905c3e6d8fa
r5475:3f5cd13d1b63
r6348:a905c3e6d8fa
r5475:3f5cd13d1b63
r8756:3b4554f1d3d4
r5475:3f5cd13d1b63
r6348:a905c3e6d8fa
r6348:a905c3e6d8fa
r5475:3f5cd13d1b63
r6348:a905c3e6d8fa
r11991:9a2b087e564c
r11991:9a2b087e564c
r21008:be2e3d3c935b
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r13060:e57594b0ca84
r18290:4e805de55757
r11985:d8202dea2203
r18290:4e805de55757
r11985:d8202dea2203
r11987:ed94bb585dec
r11985:d8202dea2203
r18290:4e805de55757
r11985:d8202dea2203
r18290:4e805de55757
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r5475:3f5cd13d1b63
r11985:d8202dea2203
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
r11985:d8202dea2203
r11985:d8202dea2203
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r11985:d8202dea2203
r11985:d8202dea2203
r17672:2481d27bea50
r11985:d8202dea2203
r17672:2481d27bea50
r17728:6a6e98f25cac
r17728:6a6e98f25cac
r6332:7393965b5322
r6332:7393965b5322
r11985:d8202dea2203
r11985:d8202dea2203
r11985:d8202dea2203
r23470:4f54a54295ed
r23041:7dd9cd83d20f
r23041:7dd9cd83d20f
r23041:7dd9cd83d20f
r23041:7dd9cd83d20f
r23041:7dd9cd83d20f
r23041:7dd9cd83d20f
r7168:156fd244b919
r23041:7dd9cd83d20f
r7168:156fd244b919
r7168:156fd244b919
r19738:837a139219f0
r19738:837a139219f0
r19738:837a139219f0
r19738:837a139219f0
r19738:837a139219f0
r19738:837a139219f0
r19723:ed364738c640
r21008:be2e3d3c935b
r19723:ed364738c640
r22756:8ffd1846f871
r22756:8ffd1846f871
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19738:837a139219f0
r19738:837a139219f0
r19738:837a139219f0
r19738:837a139219f0
r19738:837a139219f0
r19738:837a139219f0
r6248:b940b09d7ab8
r22756:8ffd1846f871
r22756:8ffd1846f871
r23607:36c15679007d
r22756:8ffd1846f871
r22756:8ffd1846f871
r22756:8ffd1846f871
r22756:8ffd1846f871
r22756:8ffd1846f871
r23607:36c15679007d
r22756:8ffd1846f871
r22756:8ffd1846f871
r22756:8ffd1846f871
r22756:8ffd1846f871
r22756:8ffd1846f871
r19723:ed364738c640
r19724:343db83218d4
r19723:ed364738c640
r19738:837a139219f0
r19738:837a139219f0
r19738:837a139219f0
r5475:3f5cd13d1b63
r22707:80b972b362a5
r13157:6b5799323a00
r22707:80b972b362a5
r22707:80b972b362a5
r17834:447b5cb3ee1a
r5475:3f5cd13d1b63
r13157:6b5799323a00
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r21299:8973364c4b52
r23607:36c15679007d
r21299:8973364c4b52
r9750:1e2535e15958
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r19723:ed364738c640
r17833:6e30fcc94ba8
r22707:80b972b362a5
r22707:80b972b362a5
r22707:80b972b362a5
r22707:80b972b362a5
r22707:80b972b362a5
r22707:80b972b362a5
r22707:80b972b362a5
r22707:80b972b362a5
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17834:447b5cb3ee1a
r17833:6e30fcc94ba8
r17833:6e30fcc94ba8
r17833:6e30fcc94ba8
r17833:6e30fcc94ba8
r17833:6e30fcc94ba8
r17833:6e30fcc94ba8
r22707:80b972b362a5
r22707:80b972b362a5
r17834:447b5cb3ee1a
r17833:6e30fcc94ba8
r6248:b940b09d7ab8
r5475:3f5cd13d1b63
r5475:3f5cd13d1b63
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file newgrf_spritegroup.h Action 2 handling. */

#ifndef NEWGRF_SPRITEGROUP_H
#define NEWGRF_SPRITEGROUP_H

#include "town_type.h"
#include "engine_type.h"
#include "house_type.h"
#include "industry_type.h"

#include "newgrf_callbacks.h"
#include "newgrf_generic.h"
#include "newgrf_storage.h"
#include "newgrf_commons.h"

/**
 * Gets the value of a so-called newgrf "register".
 * @param i index of the register
 * @pre i < 0x110
 * @return the value of the register
 */
static inline uint32 GetRegister(uint i)
{
	extern TemporaryStorageArray<int32, 0x110> _temp_store;
	return _temp_store.GetValue(i);
}

/* List of different sprite group types */
enum SpriteGroupType {
	SGT_REAL,
	SGT_DETERMINISTIC,
	SGT_RANDOMIZED,
	SGT_CALLBACK,
	SGT_RESULT,
	SGT_TILELAYOUT,
	SGT_INDUSTRY_PRODUCTION,
};

struct SpriteGroup;
typedef uint32 SpriteGroupID;
struct ResolverObject;

/* SPRITE_WIDTH is 24. ECS has roughly 30 sprite groups per real sprite.
 * Adding an 'extra' margin would be assuming 64 sprite groups per real
 * sprite. 64 = 2^6, so 2^30 should be enough (for now) */
typedef Pool<SpriteGroup, SpriteGroupID, 1024, 1 << 30, PT_DATA> SpriteGroupPool;
extern SpriteGroupPool _spritegroup_pool;

/* Common wrapper for all the different sprite group types */
struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> {
protected:
	SpriteGroup(SpriteGroupType type) : type(type) {}
	/** Base sprite group resolver */
	virtual const SpriteGroup *Resolve(ResolverObject &object) const { return this; };

public:
	virtual ~SpriteGroup() {}

	SpriteGroupType type;

	virtual SpriteID GetResult() const { return 0; }
	virtual byte GetNumResults() const { return 0; }
	virtual uint16 GetCallbackResult() const { return CALLBACK_FAILED; }

	static const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level = true);
};


/* 'Real' sprite groups contain a list of other result or callback sprite
 * groups. */
struct RealSpriteGroup : SpriteGroup {
	RealSpriteGroup() : SpriteGroup(SGT_REAL) {}
	~RealSpriteGroup();

	/* Loaded = in motion, loading = not moving
	 * Each group contains several spritesets, for various loading stages */

	/* XXX: For stations the meaning is different - loaded is for stations
	 * with small amount of cargo whilst loading is for stations with a lot
	 * of da stuff. */

	byte num_loaded;       ///< Number of loaded groups
	byte num_loading;      ///< Number of loading groups
	const SpriteGroup **loaded;  ///< List of loaded groups (can be SpriteIDs or Callback results)
	const SpriteGroup **loading; ///< List of loading groups (can be SpriteIDs or Callback results)

protected:
	const SpriteGroup *Resolve(ResolverObject &object) const;
};

/* Shared by deterministic and random groups. */
enum VarSpriteGroupScope {
	VSG_BEGIN,

	VSG_SCOPE_SELF = VSG_BEGIN, ///< Resolved object itself
	VSG_SCOPE_PARENT,           ///< Related object of the resolved one
	VSG_SCOPE_RELATIVE,         ///< Relative position (vehicles only)

	VSG_END
};
DECLARE_POSTFIX_INCREMENT(VarSpriteGroupScope)

enum DeterministicSpriteGroupSize {
	DSG_SIZE_BYTE,
	DSG_SIZE_WORD,
	DSG_SIZE_DWORD,
};

enum DeterministicSpriteGroupAdjustType {
	DSGA_TYPE_NONE,
	DSGA_TYPE_DIV,
	DSGA_TYPE_MOD,
};

enum DeterministicSpriteGroupAdjustOperation {
	DSGA_OP_ADD,  ///< a + b
	DSGA_OP_SUB,  ///< a - b
	DSGA_OP_SMIN, ///< (signed) min(a, b)
	DSGA_OP_SMAX, ///< (signed) max(a, b)
	DSGA_OP_UMIN, ///< (unsigned) min(a, b)
	DSGA_OP_UMAX, ///< (unsigned) max(a, b)
	DSGA_OP_SDIV, ///< (signed) a / b
	DSGA_OP_SMOD, ///< (signed) a % b
	DSGA_OP_UDIV, ///< (unsigned) a / b
	DSGA_OP_UMOD, ///< (unsigned) a & b
	DSGA_OP_MUL,  ///< a * b
	DSGA_OP_AND,  ///< a & b
	DSGA_OP_OR,   ///< a | b
	DSGA_OP_XOR,  ///< a ^ b
	DSGA_OP_STO,  ///< store a into temporary storage, indexed by b. return a
	DSGA_OP_RST,  ///< return b
	DSGA_OP_STOP, ///< store a into persistent storage, indexed by b, return a
	DSGA_OP_ROR,  ///< rotate a b positions to the right
	DSGA_OP_SCMP, ///< (signed) comparison (a < b -> 0, a == b = 1, a > b = 2)
	DSGA_OP_UCMP, ///< (unsigned) comparison (a < b -> 0, a == b = 1, a > b = 2)
	DSGA_OP_SHL,  ///< a << b
	DSGA_OP_SHR,  ///< (unsigned) a >> b
	DSGA_OP_SAR,  ///< (signed) a >> b
};


struct DeterministicSpriteGroupAdjust {
	DeterministicSpriteGroupAdjustOperation operation;
	DeterministicSpriteGroupAdjustType type;
	byte variable;
	byte parameter; ///< Used for variables between 0x60 and 0x7F inclusive.
	byte shift_num;
	uint32 and_mask;
	uint32 add_val;
	uint32 divmod_val;
	const SpriteGroup *subroutine;
};


struct DeterministicSpriteGroupRange {
	const SpriteGroup *group;
	uint32 low;
	uint32 high;
};


struct DeterministicSpriteGroup : SpriteGroup {
	DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
	~DeterministicSpriteGroup();

	VarSpriteGroupScope var_scope;
	DeterministicSpriteGroupSize size;
	uint num_adjusts;
	uint num_ranges;
	bool calculated_result;
	DeterministicSpriteGroupAdjust *adjusts;
	DeterministicSpriteGroupRange *ranges; // Dynamically allocated

	/* Dynamically allocated, this is the sole owner */
	const SpriteGroup *default_group;

	const SpriteGroup *error_group; // was first range, before sorting ranges

protected:
	const SpriteGroup *Resolve(ResolverObject &object) const;
};

enum RandomizedSpriteGroupCompareMode {
	RSG_CMP_ANY,
	RSG_CMP_ALL,
};

struct RandomizedSpriteGroup : SpriteGroup {
	RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED) {}
	~RandomizedSpriteGroup();

	VarSpriteGroupScope var_scope;  ///< Take this object:

	RandomizedSpriteGroupCompareMode cmp_mode; ///< Check for these triggers:
	byte triggers;
	byte count;

	byte lowest_randbit; ///< Look for this in the per-object randomized bitmask:
	byte num_groups; ///< must be power of 2

	const SpriteGroup **groups; ///< Take the group with appropriate index:

protected:
	const SpriteGroup *Resolve(ResolverObject &object) const;
};


/* This contains a callback result. A failed callback has a value of
 * CALLBACK_FAILED */
struct CallbackResultSpriteGroup : SpriteGroup {
	/**
	 * Creates a spritegroup representing a callback result
	 * @param value The value that was used to represent this callback result
	 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
	 */
	CallbackResultSpriteGroup(uint16 value, bool grf_version8) :
		SpriteGroup(SGT_CALLBACK),
		result(value)
	{
		/* Old style callback results (only valid for version < 8) have the highest byte 0xFF so signify it is a callback result.
		 * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
		if (!grf_version8 && (this->result >> 8) == 0xFF) {
			this->result &= ~0xFF00;
		} else {
			this->result &= ~0x8000;
		}
	}

	uint16 result;
	uint16 GetCallbackResult() const { return this->result; }
};


/* A result sprite group returns the first SpriteID and the number of
 * sprites in the set */
struct ResultSpriteGroup : SpriteGroup {
	/**
	 * Creates a spritegroup representing a sprite number result.
	 * @param sprite The sprite number.
	 * @param num_sprites The number of sprites per set.
	 * @return A spritegroup representing the sprite number result.
	 */
	ResultSpriteGroup(SpriteID sprite, byte num_sprites) :
		SpriteGroup(SGT_RESULT),
		sprite(sprite),
		num_sprites(num_sprites)
	{
	}

	SpriteID sprite;
	byte num_sprites;
	SpriteID GetResult() const { return this->sprite; }
	byte GetNumResults() const { return this->num_sprites; }
};

/**
 * Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
 */
struct TileLayoutSpriteGroup : SpriteGroup {
	TileLayoutSpriteGroup() : SpriteGroup(SGT_TILELAYOUT) {}
	~TileLayoutSpriteGroup() {}

	NewGRFSpriteLayout dts;

	const DrawTileSprites *ProcessRegisters(uint8 *stage) const;
};

struct IndustryProductionSpriteGroup : SpriteGroup {
	IndustryProductionSpriteGroup() : SpriteGroup(SGT_INDUSTRY_PRODUCTION) {}

	uint8 version;                              ///< Production callback version used, or 0xFF if marked invalid
	uint8 num_input;                            ///< How many subtract_input values are valid
	int16 subtract_input[INDUSTRY_NUM_INPUTS];  ///< Take this much of the input cargo (can be negative, is indirect in cb version 1+)
	CargoID cargo_input[INDUSTRY_NUM_INPUTS];   ///< Which input cargoes to take from (only cb version 2)
	uint8 num_output;                           ///< How many add_output values are valid
	uint16 add_output[INDUSTRY_NUM_OUTPUTS];    ///< Add this much output cargo when successful (unsigned, is indirect in cb version 1+)
	CargoID cargo_output[INDUSTRY_NUM_OUTPUTS]; ///< Which output cargoes to add to (only cb version 2)
	uint8 again;

};

/**
 * Interface to query and set values specific to a single #VarSpriteGroupScope (action 2 scope).
 *
 * Multiple of these interfaces are combined into a #ResolverObject to allow access
 * to different game entities from a #SpriteGroup-chain (action 1-2-3 chain).
 */
struct ScopeResolver {
	ResolverObject &ro; ///< Surrounding resolver object.

	ScopeResolver(ResolverObject &ro) : ro(ro) {}
	virtual ~ScopeResolver() {}

	virtual uint32 GetRandomBits() const;
	virtual uint32 GetTriggers() const;

	virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
	virtual void StorePSA(uint reg, int32 value);
};

/**
 * Interface for #SpriteGroup-s to access the gamestate.
 *
 * Using this interface #SpriteGroup-chains (action 1-2-3 chains) can be resolved,
 * to get the results of callbacks, rerandomisations or normal sprite lookups.
 */
struct ResolverObject {
	/**
	 * Resolver constructor.
	 * @param grffile NewGRF file associated with the object (or \c nullptr if none).
	 * @param callback Callback code being resolved (default value is #CBID_NO_CALLBACK).
	 * @param callback_param1 First parameter (var 10) of the callback (only used when \a callback is also set).
	 * @param callback_param2 Second parameter (var 18) of the callback (only used when \a callback is also set).
	 */
	ResolverObject(const GRFFile *grffile, CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0)
		: default_scope(*this), callback(callback), callback_param1(callback_param1), callback_param2(callback_param2), grffile(grffile), root_spritegroup(nullptr)
	{
		this->ResetState();
	}

	virtual ~ResolverObject() {}

	ScopeResolver default_scope; ///< Default implementation of the grf scope.

	CallbackID callback;        ///< Callback being resolved.
	uint32 callback_param1;     ///< First parameter (var 10) of the callback.
	uint32 callback_param2;     ///< Second parameter (var 18) of the callback.

	uint32 last_value;          ///< Result of most recent DeterministicSpriteGroup (including procedure calls)

	uint32 waiting_triggers;    ///< Waiting triggers to be used by any rerandomisation. (scope independent)
	uint32 used_triggers;       ///< Subset of cur_triggers, which actually triggered some rerandomisation. (scope independent)
	uint32 reseed[VSG_END];     ///< Collects bits to rerandomise while triggering triggers.

	const GRFFile *grffile;     ///< GRFFile the resolved SpriteGroup belongs to
	const SpriteGroup *root_spritegroup; ///< Root SpriteGroup to use for resolving

	/**
	 * Resolve SpriteGroup.
	 * @return Result spritegroup.
	 */
	const SpriteGroup *Resolve()
	{
		return SpriteGroup::Resolve(this->root_spritegroup, *this);
	}

	/**
	 * Resolve callback.
	 * @return Callback result.
	 */
	uint16 ResolveCallback()
	{
		const SpriteGroup *result = Resolve();
		return result != nullptr ? result->GetCallbackResult() : CALLBACK_FAILED;
	}

	virtual const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;

	virtual ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0);

	/**
	 * Returns the waiting triggers that did not trigger any rerandomisation.
	 */
	uint32 GetRemainingTriggers() const
	{
		return this->waiting_triggers & ~this->used_triggers;
	}

	/**
	 * Returns the OR-sum of all bits that need reseeding
	 * independent of the scope they were accessed with.
	 * @return OR-sum of the bits.
	 */
	uint32 GetReseedSum() const
	{
		uint32 sum = 0;
		for (VarSpriteGroupScope vsg = VSG_BEGIN; vsg < VSG_END; vsg++) {
			sum |= this->reseed[vsg];
		}
		return sum;
	}

	/**
	 * Resets the dynamic state of the resolver object.
	 * To be called before resolving an Action-1-2-3 chain.
	 */
	void ResetState()
	{
		this->last_value = 0;
		this->waiting_triggers = 0;
		this->used_triggers = 0;
		memset(this->reseed, 0, sizeof(this->reseed));
	}
};

#endif /* NEWGRF_SPRITEGROUP_H */