OpenTTD: src/newgrf_spritegroup.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

newgrf_spritegroup.h
Go to the documentation of this file.
1 /* $Id: newgrf_spritegroup.h 27989 2018-03-11 15:08:51Z frosch $ */
2
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 */
9
12 #ifndef NEWGRF_SPRITEGROUP_H
13 #define NEWGRF_SPRITEGROUP_H
14
15 #include "town_type.h"
16 #include "engine_type.h"
17 #include "house_type.h"
18
19 #include "newgrf_callbacks.h"
20 #include "newgrf_generic.h"
21 #include "newgrf_storage.h"
22 #include "newgrf_commons.h"
23
30 static inline uint32 GetRegister(uint i)
31 {
32 extern TemporaryStorageArray<int32, 0x110> _temp_store;
33 return _temp_store.GetValue(i);
34 }
35
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,
45 };
46
47 struct SpriteGroup;
48 typedef uint32 SpriteGroupID;
49 struct ResolverObject;
50
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;
56
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) {}
62 virtual const SpriteGroup *Resolve(ResolverObject &object) const { return this; };
63
64 public:
65 virtual ~SpriteGroup() {}
66
67 SpriteGroupType type;
68
69 virtual SpriteID GetResult() const { return 0; }
70 virtual byte GetNumResults() const { return 0; }
71 virtual uint16 GetCallbackResult() const { return CALLBACK_FAILED; }
72
73 static const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level = true);
74 };
75
76
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();
82
83 /* Loaded = in motion, loading = not moving
84 * Each group contains several spritesets, for various loading stages */
85
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. */
89
90 byte num_loaded;
91 byte num_loading;
92 const SpriteGroup **loaded;
93 const SpriteGroup **loading;
94
95 protected:
96 const SpriteGroup *Resolve(ResolverObject &object) const;
97 };
98
99 /* Shared by deterministic and random groups. */
100 enum VarSpriteGroupScope {
101 VSG_BEGIN,
102
103 VSG_SCOPE_SELF = VSG_BEGIN,
104 VSG_SCOPE_PARENT,
105 VSG_SCOPE_RELATIVE,
106
107 VSG_END
108 };
109 DECLARE_POSTFIX_INCREMENT(VarSpriteGroupScope)
110
111 enum DeterministicSpriteGroupSize {
112 DSG_SIZE_BYTE,
113 DSG_SIZE_WORD,
114 DSG_SIZE_DWORD,
115 };
116
117 enum DeterministicSpriteGroupAdjustType {
118 DSGA_TYPE_NONE,
119 DSGA_TYPE_DIV,
120 DSGA_TYPE_MOD,
121 };
122
123 enum DeterministicSpriteGroupAdjustOperation {
124 DSGA_OP_ADD,
125 DSGA_OP_SUB,
126 DSGA_OP_SMIN,
127 DSGA_OP_SMAX,
128 DSGA_OP_UMIN,
129 DSGA_OP_UMAX,
130 DSGA_OP_SDIV,
131 DSGA_OP_SMOD,
132 DSGA_OP_UDIV,
133 DSGA_OP_UMOD,
134 DSGA_OP_MUL,
135 DSGA_OP_AND,
136 DSGA_OP_OR,
137 DSGA_OP_XOR,
138 DSGA_OP_STO,
139 DSGA_OP_RST,
140 DSGA_OP_STOP,
141 DSGA_OP_ROR,
142 DSGA_OP_SCMP,
143 DSGA_OP_UCMP,
144 DSGA_OP_SHL,
145 DSGA_OP_SHR,
146 DSGA_OP_SAR,
147 };
148
149
150 struct DeterministicSpriteGroupAdjust {
151 DeterministicSpriteGroupAdjustOperation operation;
152 DeterministicSpriteGroupAdjustType type;
153 byte variable;
154 byte parameter;
155 byte shift_num;
156 uint32 and_mask;
157 uint32 add_val;
158 uint32 divmod_val;
159 const SpriteGroup *subroutine;
160 };
161
162
163 struct DeterministicSpriteGroupRange {
164 const SpriteGroup *group;
165 uint32 low;
166 uint32 high;
167 };
168
169
170 struct DeterministicSpriteGroup : SpriteGroup {
171 DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
172 ~DeterministicSpriteGroup();
173
174 VarSpriteGroupScope var_scope;
175 DeterministicSpriteGroupSize size;
176 uint num_adjusts;
177 uint num_ranges;
178 bool calculated_result;
179 DeterministicSpriteGroupAdjust *adjusts;
180 DeterministicSpriteGroupRange *ranges; // Dynamically allocated
181
182 /* Dynamically allocated, this is the sole owner */
183 const SpriteGroup *default_group;
184
185 const SpriteGroup *error_group; // was first range, before sorting ranges
186
187 protected:
188 const SpriteGroup *Resolve(ResolverObject &object) const;
189 };
190
191 enum RandomizedSpriteGroupCompareMode {
192 RSG_CMP_ANY,
193 RSG_CMP_ALL,
194 };
195
196 struct RandomizedSpriteGroup : SpriteGroup {
197 RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED) {}
198 ~RandomizedSpriteGroup();
199
200 VarSpriteGroupScope var_scope;
201
202 RandomizedSpriteGroupCompareMode cmp_mode;
203 byte triggers;
204 byte count;
205
206 byte lowest_randbit;
207 byte num_groups;
208
209 const SpriteGroup **groups;
210
211 protected:
212 const SpriteGroup *Resolve(ResolverObject &object) const;
213 };
214
215
216 /* This contains a callback result. A failed callback has a value of
217 * CALLBACK_FAILED */
218 struct CallbackResultSpriteGroup : SpriteGroup {
224 CallbackResultSpriteGroup(uint16 value, bool grf_version8) :
225 SpriteGroup(SGT_CALLBACK),
226 result(value)
227 {
228 /* Old style callback results (only valid for version < 8) have the highest byte 0xFF so signify it is a callback result.
229 * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
230 if (!grf_version8 && (this->result >> 8) == 0xFF) {
231 this->result &= ~0xFF00;
232 } else {
233 this->result &= ~0x8000;
234 }
235 }
236
237 uint16 result;
238 uint16 GetCallbackResult() const { return this->result; }
239 };
240
241
242 /* A result sprite group returns the first SpriteID and the number of
243 * sprites in the set */
244 struct ResultSpriteGroup : SpriteGroup {
251 ResultSpriteGroup(SpriteID sprite, byte num_sprites) :
252 SpriteGroup(SGT_RESULT),
253 sprite(sprite),
254 num_sprites(num_sprites)
255 {
256 }
257
258 SpriteID sprite;
259 byte num_sprites;
260 SpriteID GetResult() const { return this->sprite; }
261 byte GetNumResults() const { return this->num_sprites; }
262 };
263
267 struct TileLayoutSpriteGroup : SpriteGroup {
268 TileLayoutSpriteGroup() : SpriteGroup(SGT_TILELAYOUT) {}
269 ~TileLayoutSpriteGroup() {}
270
271 NewGRFSpriteLayout dts;
272
273 const DrawTileSprites *ProcessRegisters(uint8 *stage) const;
274 };
275
276 struct IndustryProductionSpriteGroup : SpriteGroup {
277 IndustryProductionSpriteGroup() : SpriteGroup(SGT_INDUSTRY_PRODUCTION) {}
278
279 uint8 version;
280 int16 subtract_input[3]; // signed
281 uint16 add_output[2]; // unsigned
282 uint8 again;
283 };
284
291 struct ScopeResolver {
292 ResolverObject &ro;
293
294 ScopeResolver(ResolverObject &ro) : ro(ro) {}
295 virtual ~ScopeResolver() {}
296
297 virtual uint32 GetRandomBits() const;
298 virtual uint32 GetTriggers() const;
299
300 virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
301 virtual void StorePSA(uint reg, int32 value);
302 };
303
310 struct ResolverObject {
318 ResolverObject(const GRFFile *grffile, CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0)
319 : default_scope(*this), callback(callback), callback_param1(callback_param1), callback_param2(callback_param2), grffile(grffile), root_spritegroup(NULL)
320 {
321 this->ResetState();
322 }
323
324 virtual ~ResolverObject() {}
325
326 ScopeResolver default_scope;
327
328 CallbackID callback;
329 uint32 callback_param1;
330 uint32 callback_param2;
331
332 uint32 last_value;
333
334 uint32 waiting_triggers;
335 uint32 used_triggers;
336 uint32 reseed[VSG_END];
337
338 const GRFFile *grffile;
339 const SpriteGroup *root_spritegroup;
340
345 const SpriteGroup *Resolve()
346 {
347 return SpriteGroup::Resolve(this->root_spritegroup, *this);
348 }
349
354 uint16 ResolveCallback()
355 {
356 const SpriteGroup *result = Resolve();
357 return result != NULL ? result->GetCallbackResult() : CALLBACK_FAILED;
358 }
359
360 virtual const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
361
362 virtual ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0);
363
367 uint32 GetRemainingTriggers() const
368 {
369 return this->waiting_triggers & ~this->used_triggers;
370 }
371
377 uint32 GetReseedSum() const
378 {
379 uint32 sum = 0;
380 for (VarSpriteGroupScope vsg = VSG_BEGIN; vsg < VSG_END; vsg++) {
381 sum |= this->reseed[vsg];
382 }
383 return sum;
384 }
385
390 void ResetState()
391 {
392 this->last_value = 0;
393 this->waiting_triggers = 0;
394 this->used_triggers = 0;
395 memset(this->reseed, 0, sizeof(this->reseed));
396 }
397 };
398
399 #endif /* NEWGRF_SPRITEGROUP_H */