Fix: Don't try to rename OWNER_DEITY signs in-game (#9716)
[openttd-github.git] / src / newgrf_spritegroup.h
blobb17266761242ab0530e960bc52eb3daf1d2a5ffe
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file newgrf_spritegroup.h Action 2 handling. */
10 #ifndef NEWGRF_SPRITEGROUP_H
11 #define NEWGRF_SPRITEGROUP_H
13 #include "town_type.h"
14 #include "engine_type.h"
15 #include "house_type.h"
16 #include "industry_type.h"
18 #include "newgrf_callbacks.h"
19 #include "newgrf_generic.h"
20 #include "newgrf_storage.h"
21 #include "newgrf_commons.h"
23 /**
24 * Gets the value of a so-called newgrf "register".
25 * @param i index of the register
26 * @pre i < 0x110
27 * @return the value of the register
29 static inline uint32 GetRegister(uint i)
31 extern TemporaryStorageArray<int32, 0x110> _temp_store;
32 return _temp_store.GetValue(i);
35 /* List of different sprite group types */
36 enum SpriteGroupType {
37 SGT_REAL,
38 SGT_DETERMINISTIC,
39 SGT_RANDOMIZED,
40 SGT_CALLBACK,
41 SGT_RESULT,
42 SGT_TILELAYOUT,
43 SGT_INDUSTRY_PRODUCTION,
46 struct SpriteGroup;
47 typedef uint32 SpriteGroupID;
48 struct ResolverObject;
50 /* SPRITE_WIDTH is 24. ECS has roughly 30 sprite groups per real sprite.
51 * Adding an 'extra' margin would be assuming 64 sprite groups per real
52 * sprite. 64 = 2^6, so 2^30 should be enough (for now) */
53 typedef Pool<SpriteGroup, SpriteGroupID, 1024, 1 << 30, PT_DATA> SpriteGroupPool;
54 extern SpriteGroupPool _spritegroup_pool;
56 /* Common wrapper for all the different sprite group types */
57 struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> {
58 protected:
59 SpriteGroup(SpriteGroupType type) : nfo_line(0), type(type) {}
60 /** Base sprite group resolver */
61 virtual const SpriteGroup *Resolve(ResolverObject &object) const { return this; };
63 public:
64 virtual ~SpriteGroup() {}
66 uint32 nfo_line;
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) {}
82 /* Loaded = in motion, loading = not moving
83 * Each group contains several spritesets, for various loading stages */
85 /* XXX: For stations the meaning is different - loaded is for stations
86 * with small amount of cargo whilst loading is for stations with a lot
87 * of da stuff. */
89 std::vector<const SpriteGroup *> loaded; ///< List of loaded groups (can be SpriteIDs or Callback results)
90 std::vector<const SpriteGroup *> loading; ///< List of loading groups (can be SpriteIDs or Callback results)
92 protected:
93 const SpriteGroup *Resolve(ResolverObject &object) const;
96 /* Shared by deterministic and random groups. */
97 enum VarSpriteGroupScope {
98 VSG_BEGIN,
100 VSG_SCOPE_SELF = VSG_BEGIN, ///< Resolved object itself
101 VSG_SCOPE_PARENT, ///< Related object of the resolved one
102 VSG_SCOPE_RELATIVE, ///< Relative position (vehicles only)
104 VSG_END
106 DECLARE_POSTFIX_INCREMENT(VarSpriteGroupScope)
108 enum DeterministicSpriteGroupSize {
109 DSG_SIZE_BYTE,
110 DSG_SIZE_WORD,
111 DSG_SIZE_DWORD,
114 enum DeterministicSpriteGroupAdjustType {
115 DSGA_TYPE_NONE,
116 DSGA_TYPE_DIV,
117 DSGA_TYPE_MOD,
120 enum DeterministicSpriteGroupAdjustOperation {
121 DSGA_OP_ADD, ///< a + b
122 DSGA_OP_SUB, ///< a - b
123 DSGA_OP_SMIN, ///< (signed) min(a, b)
124 DSGA_OP_SMAX, ///< (signed) max(a, b)
125 DSGA_OP_UMIN, ///< (unsigned) min(a, b)
126 DSGA_OP_UMAX, ///< (unsigned) max(a, b)
127 DSGA_OP_SDIV, ///< (signed) a / b
128 DSGA_OP_SMOD, ///< (signed) a % b
129 DSGA_OP_UDIV, ///< (unsigned) a / b
130 DSGA_OP_UMOD, ///< (unsigned) a & b
131 DSGA_OP_MUL, ///< a * b
132 DSGA_OP_AND, ///< a & b
133 DSGA_OP_OR, ///< a | b
134 DSGA_OP_XOR, ///< a ^ b
135 DSGA_OP_STO, ///< store a into temporary storage, indexed by b. return a
136 DSGA_OP_RST, ///< return b
137 DSGA_OP_STOP, ///< store a into persistent storage, indexed by b, return a
138 DSGA_OP_ROR, ///< rotate a b positions to the right
139 DSGA_OP_SCMP, ///< (signed) comparison (a < b -> 0, a == b = 1, a > b = 2)
140 DSGA_OP_UCMP, ///< (unsigned) comparison (a < b -> 0, a == b = 1, a > b = 2)
141 DSGA_OP_SHL, ///< a << b
142 DSGA_OP_SHR, ///< (unsigned) a >> b
143 DSGA_OP_SAR, ///< (signed) a >> b
147 struct DeterministicSpriteGroupAdjust {
148 DeterministicSpriteGroupAdjustOperation operation;
149 DeterministicSpriteGroupAdjustType type;
150 byte variable;
151 byte parameter; ///< Used for variables between 0x60 and 0x7F inclusive.
152 byte shift_num;
153 uint32 and_mask;
154 uint32 add_val;
155 uint32 divmod_val;
156 const SpriteGroup *subroutine;
160 struct DeterministicSpriteGroupRange {
161 const SpriteGroup *group;
162 uint32 low;
163 uint32 high;
167 struct DeterministicSpriteGroup : SpriteGroup {
168 DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
170 VarSpriteGroupScope var_scope;
171 DeterministicSpriteGroupSize size;
172 bool calculated_result;
173 std::vector<DeterministicSpriteGroupAdjust> adjusts;
174 std::vector<DeterministicSpriteGroupRange> ranges; // Dynamically allocated
176 /* Dynamically allocated, this is the sole owner */
177 const SpriteGroup *default_group;
179 const SpriteGroup *error_group; // was first range, before sorting ranges
181 protected:
182 const SpriteGroup *Resolve(ResolverObject &object) const;
185 enum RandomizedSpriteGroupCompareMode {
186 RSG_CMP_ANY,
187 RSG_CMP_ALL,
190 struct RandomizedSpriteGroup : SpriteGroup {
191 RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED) {}
193 VarSpriteGroupScope var_scope; ///< Take this object:
195 RandomizedSpriteGroupCompareMode cmp_mode; ///< Check for these triggers:
196 byte triggers;
197 byte count;
199 byte lowest_randbit; ///< Look for this in the per-object randomized bitmask:
201 std::vector<const SpriteGroup *> groups; ///< Take the group with appropriate index:
203 protected:
204 const SpriteGroup *Resolve(ResolverObject &object) const;
208 /* This contains a callback result. A failed callback has a value of
209 * CALLBACK_FAILED */
210 struct CallbackResultSpriteGroup : SpriteGroup {
212 * Creates a spritegroup representing a callback result
213 * @param value The value that was used to represent this callback result
214 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
216 CallbackResultSpriteGroup(uint16 value, bool grf_version8) :
217 SpriteGroup(SGT_CALLBACK),
218 result(value)
220 /* Old style callback results (only valid for version < 8) have the highest byte 0xFF so signify it is a callback result.
221 * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
222 if (!grf_version8 && (this->result >> 8) == 0xFF) {
223 this->result &= ~0xFF00;
224 } else {
225 this->result &= ~0x8000;
229 uint16 result;
230 uint16 GetCallbackResult() const { return this->result; }
234 /* A result sprite group returns the first SpriteID and the number of
235 * sprites in the set */
236 struct ResultSpriteGroup : SpriteGroup {
238 * Creates a spritegroup representing a sprite number result.
239 * @param sprite The sprite number.
240 * @param num_sprites The number of sprites per set.
241 * @return A spritegroup representing the sprite number result.
243 ResultSpriteGroup(SpriteID sprite, byte num_sprites) :
244 SpriteGroup(SGT_RESULT),
245 sprite(sprite),
246 num_sprites(num_sprites)
250 SpriteID sprite;
251 byte num_sprites;
252 SpriteID GetResult() const { return this->sprite; }
253 byte GetNumResults() const { return this->num_sprites; }
257 * Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
259 struct TileLayoutSpriteGroup : SpriteGroup {
260 TileLayoutSpriteGroup() : SpriteGroup(SGT_TILELAYOUT) {}
261 ~TileLayoutSpriteGroup() {}
263 NewGRFSpriteLayout dts;
265 const DrawTileSprites *ProcessRegisters(uint8 *stage) const;
268 struct IndustryProductionSpriteGroup : SpriteGroup {
269 IndustryProductionSpriteGroup() : SpriteGroup(SGT_INDUSTRY_PRODUCTION) {}
271 uint8 version; ///< Production callback version used, or 0xFF if marked invalid
272 uint8 num_input; ///< How many subtract_input values are valid
273 int16 subtract_input[INDUSTRY_NUM_INPUTS]; ///< Take this much of the input cargo (can be negative, is indirect in cb version 1+)
274 CargoID cargo_input[INDUSTRY_NUM_INPUTS]; ///< Which input cargoes to take from (only cb version 2)
275 uint8 num_output; ///< How many add_output values are valid
276 uint16 add_output[INDUSTRY_NUM_OUTPUTS]; ///< Add this much output cargo when successful (unsigned, is indirect in cb version 1+)
277 CargoID cargo_output[INDUSTRY_NUM_OUTPUTS]; ///< Which output cargoes to add to (only cb version 2)
278 uint8 again;
283 * Interface to query and set values specific to a single #VarSpriteGroupScope (action 2 scope).
285 * Multiple of these interfaces are combined into a #ResolverObject to allow access
286 * to different game entities from a #SpriteGroup-chain (action 1-2-3 chain).
288 struct ScopeResolver {
289 ResolverObject &ro; ///< Surrounding resolver object.
291 ScopeResolver(ResolverObject &ro) : ro(ro) {}
292 virtual ~ScopeResolver() {}
294 virtual uint32 GetRandomBits() const;
295 virtual uint32 GetTriggers() const;
297 virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
298 virtual void StorePSA(uint reg, int32 value);
302 * Interface for #SpriteGroup-s to access the gamestate.
304 * Using this interface #SpriteGroup-chains (action 1-2-3 chains) can be resolved,
305 * to get the results of callbacks, rerandomisations or normal sprite lookups.
307 struct ResolverObject {
309 * Resolver constructor.
310 * @param grffile NewGRF file associated with the object (or \c nullptr if none).
311 * @param callback Callback code being resolved (default value is #CBID_NO_CALLBACK).
312 * @param callback_param1 First parameter (var 10) of the callback (only used when \a callback is also set).
313 * @param callback_param2 Second parameter (var 18) of the callback (only used when \a callback is also set).
315 ResolverObject(const GRFFile *grffile, CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0)
316 : default_scope(*this), callback(callback), callback_param1(callback_param1), callback_param2(callback_param2), grffile(grffile), root_spritegroup(nullptr)
318 this->ResetState();
321 virtual ~ResolverObject() {}
323 ScopeResolver default_scope; ///< Default implementation of the grf scope.
325 CallbackID callback; ///< Callback being resolved.
326 uint32 callback_param1; ///< First parameter (var 10) of the callback.
327 uint32 callback_param2; ///< Second parameter (var 18) of the callback.
329 uint32 last_value; ///< Result of most recent DeterministicSpriteGroup (including procedure calls)
331 uint32 waiting_triggers; ///< Waiting triggers to be used by any rerandomisation. (scope independent)
332 uint32 used_triggers; ///< Subset of cur_triggers, which actually triggered some rerandomisation. (scope independent)
333 uint32 reseed[VSG_END]; ///< Collects bits to rerandomise while triggering triggers.
335 const GRFFile *grffile; ///< GRFFile the resolved SpriteGroup belongs to
336 const SpriteGroup *root_spritegroup; ///< Root SpriteGroup to use for resolving
339 * Resolve SpriteGroup.
340 * @return Result spritegroup.
342 const SpriteGroup *Resolve()
344 return SpriteGroup::Resolve(this->root_spritegroup, *this);
348 * Resolve callback.
349 * @return Callback result.
351 uint16 ResolveCallback()
353 const SpriteGroup *result = Resolve();
354 return result != nullptr ? result->GetCallbackResult() : CALLBACK_FAILED;
357 virtual const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
359 virtual ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0);
362 * Returns the waiting triggers that did not trigger any rerandomisation.
364 uint32 GetRemainingTriggers() const
366 return this->waiting_triggers & ~this->used_triggers;
370 * Returns the OR-sum of all bits that need reseeding
371 * independent of the scope they were accessed with.
372 * @return OR-sum of the bits.
374 uint32 GetReseedSum() const
376 uint32 sum = 0;
377 for (VarSpriteGroupScope vsg = VSG_BEGIN; vsg < VSG_END; vsg++) {
378 sum |= this->reseed[vsg];
380 return sum;
384 * Resets the dynamic state of the resolver object.
385 * To be called before resolving an Action-1-2-3 chain.
387 void ResetState()
389 this->last_value = 0;
390 this->waiting_triggers = 0;
391 this->used_triggers = 0;
392 memset(this->reseed, 0, sizeof(this->reseed));
396 * Get the feature number being resolved for.
397 * This function is mainly intended for the callback profiling feature.
399 virtual GrfSpecFeature GetFeature() const { return GSF_INVALID; }
401 * Get an identifier for the item being resolved.
402 * This function is mainly intended for the callback profiling feature,
403 * and should return an identifier recognisable by the NewGRF developer.
405 virtual uint32 GetDebugID() const { return 0; }
408 #endif /* NEWGRF_SPRITEGROUP_H */