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/>.
9 * @file newgrf_commons.cpp Implementation of the class %OverrideManagerBase
10 * and its descendance, present and future.
15 #include "landscape.h"
17 #include "industrytype.h"
18 #include "newgrf_config.h"
19 #include "clear_map.h"
20 #include "station_map.h"
22 #include "tunnelbridge_map.h"
23 #include "newgrf_object.h"
25 #include "newgrf_spritegroup.h"
26 #include "newgrf_text.h"
27 #include "company_base.h"
29 #include "strings_func.h"
31 #include "table/strings.h"
33 #include "safeguards.h"
36 * Constructor of generic class
37 * @param offset end of original data for this entity. i.e: houses = 110
38 * @param maximum of entities this manager can deal with. i.e: houses = 512
39 * @param invalid is the ID used to identify an invalid entity id
41 OverrideManagerBase::OverrideManagerBase(uint16 offset
, uint16 maximum
, uint16 invalid
)
44 max_new_entities
= maximum
;
47 mapping_ID
= CallocT
<EntityIDMapping
>(max_new_entities
);
48 entity_overrides
= MallocT
<uint16
>(max_offset
);
49 for (size_t i
= 0; i
< max_offset
; i
++) entity_overrides
[i
] = invalid
;
50 grfid_overrides
= CallocT
<uint32
>(max_offset
);
54 * Destructor of the generic class.
55 * Frees allocated memory of constructor
57 OverrideManagerBase::~OverrideManagerBase()
60 free(entity_overrides
);
61 free(grfid_overrides
);
65 * Since the entity IDs defined by the GRF file does not necessarily correlate
66 * to those used by the game, the IDs used for overriding old entities must be
67 * translated when the entity spec is set.
68 * @param local_id ID in grf file
69 * @param grfid ID of the grf file
70 * @param entity_type original entity type
72 void OverrideManagerBase::Add(uint8 local_id
, uint32 grfid
, uint entity_type
)
74 assert(entity_type
< max_offset
);
75 /* An override can be set only once */
76 if (entity_overrides
[entity_type
] != invalid_ID
) return;
77 entity_overrides
[entity_type
] = local_id
;
78 grfid_overrides
[entity_type
] = grfid
;
81 /** Resets the mapping, which is used while initializing game */
82 void OverrideManagerBase::ResetMapping()
84 memset(mapping_ID
, 0, (max_new_entities
- 1) * sizeof(EntityIDMapping
));
87 /** Resets the override, which is used while initializing game */
88 void OverrideManagerBase::ResetOverride()
90 for (uint16 i
= 0; i
< max_offset
; i
++) {
91 entity_overrides
[i
] = invalid_ID
;
92 grfid_overrides
[i
] = 0;
97 * Return the ID (if ever available) of a previously inserted entity.
98 * @param grf_local_id ID of this entity within the grfID
99 * @param grfid ID of the grf file
100 * @return the ID of the candidate, of the Invalid flag item ID
102 uint16
OverrideManagerBase::GetID(uint8 grf_local_id
, uint32 grfid
) const
104 const EntityIDMapping
*map
;
106 for (uint16 id
= 0; id
< max_new_entities
; id
++) {
107 map
= &mapping_ID
[id
];
108 if (map
->entity_id
== grf_local_id
&& map
->grfid
== grfid
) {
117 * Reserves a place in the mapping array for an entity to be installed
118 * @param grf_local_id is an arbitrary id given by the grf's author. Also known as setid
119 * @param grfid is the id of the grf file itself
120 * @param substitute_id is the original entity from which data is copied for the new one
121 * @return the proper usable slot id, or invalid marker if none is found
123 uint16
OverrideManagerBase::AddEntityID(byte grf_local_id
, uint32 grfid
, byte substitute_id
)
125 uint16 id
= this->GetID(grf_local_id
, grfid
);
126 EntityIDMapping
*map
;
128 /* Look to see if this entity has already been added. This is done
129 * separately from the loop below in case a GRF has been deleted, and there
130 * are any gaps in the array.
132 if (id
!= invalid_ID
) {
136 /* This entity hasn't been defined before, so give it an ID now. */
137 for (id
= max_offset
; id
< max_new_entities
; id
++) {
138 map
= &mapping_ID
[id
];
140 if (CheckValidNewID(id
) && map
->entity_id
== 0 && map
->grfid
== 0) {
141 map
->entity_id
= grf_local_id
;
143 map
->substitute_id
= substitute_id
;
152 * Gives the GRFID of the file the entity belongs to.
153 * @param entity_id ID of the entity being queried.
156 uint32
OverrideManagerBase::GetGRFID(uint16 entity_id
) const
158 return mapping_ID
[entity_id
].grfid
;
162 * Gives the substitute of the entity, as specified by the grf file
163 * @param entity_id of the entity being queried
166 uint16
OverrideManagerBase::GetSubstituteID(uint16 entity_id
) const
168 return mapping_ID
[entity_id
].substitute_id
;
172 * Install the specs into the HouseSpecs array
173 * It will find itself the proper slot on which it will go
174 * @param hs HouseSpec read from the grf file, ready for inclusion
176 void HouseOverrideManager::SetEntitySpec(const HouseSpec
*hs
)
178 HouseID house_id
= this->AddEntityID(hs
->grf_prop
.local_id
, hs
->grf_prop
.grffile
->grfid
, hs
->grf_prop
.subst_id
);
180 if (house_id
== invalid_ID
) {
181 grfmsg(1, "House.SetEntitySpec: Too many houses allocated. Ignoring.");
185 MemCpyT(HouseSpec::Get(house_id
), hs
);
187 /* Now add the overrides. */
188 for (int i
= 0; i
!= max_offset
; i
++) {
189 HouseSpec
*overridden_hs
= HouseSpec::Get(i
);
191 if (entity_overrides
[i
] != hs
->grf_prop
.local_id
|| grfid_overrides
[i
] != hs
->grf_prop
.grffile
->grfid
) continue;
193 overridden_hs
->grf_prop
.override
= house_id
;
194 entity_overrides
[i
] = invalid_ID
;
195 grfid_overrides
[i
] = 0;
200 * Return the ID (if ever available) of a previously inserted entity.
201 * @param grf_local_id ID of this entity within the grfID
202 * @param grfid ID of the grf file
203 * @return the ID of the candidate, of the Invalid flag item ID
205 uint16
IndustryOverrideManager::GetID(uint8 grf_local_id
, uint32 grfid
) const
207 uint16 id
= OverrideManagerBase::GetID(grf_local_id
, grfid
);
208 if (id
!= invalid_ID
) return id
;
210 /* No mapping found, try the overrides */
211 for (id
= 0; id
< max_offset
; id
++) {
212 if (entity_overrides
[id
] == grf_local_id
&& grfid_overrides
[id
] == grfid
) return id
;
219 * Method to find an entity ID and to mark it as reserved for the Industry to be included.
220 * @param grf_local_id ID used by the grf file for pre-installation work (equivalent of TTDPatch's setid
221 * @param grfid ID of the current grf file
222 * @param substitute_id industry from which data has been copied
223 * @return a free entity id (slotid) if ever one has been found, or Invalid_ID marker otherwise
225 uint16
IndustryOverrideManager::AddEntityID(byte grf_local_id
, uint32 grfid
, byte substitute_id
)
227 /* This entity hasn't been defined before, so give it an ID now. */
228 for (uint16 id
= 0; id
< max_new_entities
; id
++) {
229 /* Skip overridden industries */
230 if (id
< max_offset
&& entity_overrides
[id
] != invalid_ID
) continue;
232 /* Get the real live industry */
233 const IndustrySpec
*inds
= GetIndustrySpec(id
);
235 /* This industry must be one that is not available(enabled), mostly because of climate.
236 * And it must not already be used by a grf (grffile == nullptr).
237 * So reserve this slot here, as it is the chosen one */
238 if (!inds
->enabled
&& inds
->grf_prop
.grffile
== nullptr) {
239 EntityIDMapping
*map
= &mapping_ID
[id
];
241 if (map
->entity_id
== 0 && map
->grfid
== 0) {
242 /* winning slot, mark it as been used */
243 map
->entity_id
= grf_local_id
;
245 map
->substitute_id
= substitute_id
;
255 * Method to install the new industry data in its proper slot
256 * The slot assignment is internal of this method, since it requires
257 * checking what is available
258 * @param inds Industryspec that comes from the grf decoding process
260 void IndustryOverrideManager::SetEntitySpec(IndustrySpec
*inds
)
262 /* First step : We need to find if this industry is already specified in the savegame data. */
263 IndustryType ind_id
= this->GetID(inds
->grf_prop
.local_id
, inds
->grf_prop
.grffile
->grfid
);
265 if (ind_id
== invalid_ID
) {
267 * Or it has already been overridden, so you've lost your place.
268 * Or it is a simple substitute.
269 * We need to find a free available slot */
270 ind_id
= this->AddEntityID(inds
->grf_prop
.local_id
, inds
->grf_prop
.grffile
->grfid
, inds
->grf_prop
.subst_id
);
271 inds
->grf_prop
.override
= invalid_ID
; // make sure it will not be detected as overridden
274 if (ind_id
== invalid_ID
) {
275 grfmsg(1, "Industry.SetEntitySpec: Too many industries allocated. Ignoring.");
279 /* Now that we know we can use the given id, copy the spec to its final destination... */
280 _industry_specs
[ind_id
] = *inds
;
281 /* ... and mark it as usable*/
282 _industry_specs
[ind_id
].enabled
= true;
285 void IndustryTileOverrideManager::SetEntitySpec(const IndustryTileSpec
*its
)
287 IndustryGfx indt_id
= this->AddEntityID(its
->grf_prop
.local_id
, its
->grf_prop
.grffile
->grfid
, its
->grf_prop
.subst_id
);
289 if (indt_id
== invalid_ID
) {
290 grfmsg(1, "IndustryTile.SetEntitySpec: Too many industry tiles allocated. Ignoring.");
294 memcpy(&_industry_tile_specs
[indt_id
], its
, sizeof(*its
));
296 /* Now add the overrides. */
297 for (int i
= 0; i
< max_offset
; i
++) {
298 IndustryTileSpec
*overridden_its
= &_industry_tile_specs
[i
];
300 if (entity_overrides
[i
] != its
->grf_prop
.local_id
|| grfid_overrides
[i
] != its
->grf_prop
.grffile
->grfid
) continue;
302 overridden_its
->grf_prop
.override
= indt_id
;
303 overridden_its
->enabled
= false;
304 entity_overrides
[i
] = invalid_ID
;
305 grfid_overrides
[i
] = 0;
310 * Method to install the new object data in its proper slot
311 * The slot assignment is internal of this method, since it requires
312 * checking what is available
313 * @param spec ObjectSpec that comes from the grf decoding process
315 void ObjectOverrideManager::SetEntitySpec(ObjectSpec
*spec
)
317 /* First step : We need to find if this object is already specified in the savegame data. */
318 ObjectType type
= this->GetID(spec
->grf_prop
.local_id
, spec
->grf_prop
.grffile
->grfid
);
320 if (type
== invalid_ID
) {
322 * Or it has already been overridden, so you've lost your place.
323 * Or it is a simple substitute.
324 * We need to find a free available slot */
325 type
= this->AddEntityID(spec
->grf_prop
.local_id
, spec
->grf_prop
.grffile
->grfid
, OBJECT_TRANSMITTER
);
328 if (type
== invalid_ID
) {
329 grfmsg(1, "Object.SetEntitySpec: Too many objects allocated. Ignoring.");
333 extern ObjectSpec _object_specs
[NUM_OBJECTS
];
335 /* Now that we know we can use the given id, copy the spec to its final destination. */
336 memcpy(&_object_specs
[type
], spec
, sizeof(*spec
));
337 ObjectClass::Assign(&_object_specs
[type
]);
341 * Function used by houses (and soon industries) to get information
342 * on type of "terrain" the tile it is queries sits on.
343 * @param tile TileIndex of the tile been queried
344 * @param context The context of the tile.
345 * @return value corresponding to the grf expected format:
346 * Terrain type: 0 normal, 1 desert, 2 rainforest, 4 on or above snowline
348 uint32
GetTerrainType(TileIndex tile
, TileContext context
)
350 switch (_settings_game
.game_creation
.landscape
) {
351 case LT_TROPIC
: return GetTropicZone(tile
);
354 switch (GetTileType(tile
)) {
356 /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
357 if (_generating_world
) goto genworld
;
358 has_snow
= IsSnowTile(tile
) && GetClearDensity(tile
) >= 2;
362 /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
363 if (_generating_world
) goto genworld
; // we do not care about foundations here
364 RailGroundType ground
= GetRailGroundType(tile
);
365 has_snow
= (ground
== RAIL_GROUND_ICE_DESERT
|| (context
== TCX_UPPER_HALFTILE
&& ground
== RAIL_GROUND_HALF_SNOW
));
370 /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
371 if (_generating_world
) goto genworld
; // we do not care about foundations here
372 has_snow
= IsOnSnow(tile
);
376 /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
377 if (_generating_world
) goto genworld
;
378 TreeGround ground
= GetTreeGround(tile
);
379 has_snow
= (ground
== TREE_GROUND_SNOW_DESERT
|| ground
== TREE_GROUND_ROUGH_SNOW
) && GetTreeDensity(tile
) >= 2;
383 case MP_TUNNELBRIDGE
:
384 if (context
== TCX_ON_BRIDGE
) {
385 has_snow
= (GetBridgeHeight(tile
) > GetSnowLine());
387 /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
388 if (_generating_world
) goto genworld
; // we do not care about foundations here
389 has_snow
= HasTunnelBridgeSnowOrDesert(tile
);
397 /* These tiles usually have a levelling foundation. So use max Z */
398 has_snow
= (GetTileMaxZ(tile
) > GetSnowLine());
404 has_snow
= (GetTileZ(tile
) > GetSnowLine());
407 default: NOT_REACHED();
409 return has_snow
? 4 : 0;
416 * Get the tile at the given offset.
417 * @param parameter The NewGRF "encoded" offset.
418 * @param tile The tile to base the offset from.
419 * @param signed_offsets Whether the offsets are to be interpreted as signed or not.
420 * @param axis Axis of a railways station.
421 * @return The tile at the offset.
423 TileIndex
GetNearbyTile(byte parameter
, TileIndex tile
, bool signed_offsets
, Axis axis
)
425 int8 x
= GB(parameter
, 0, 4);
426 int8 y
= GB(parameter
, 4, 4);
428 if (signed_offsets
&& x
>= 8) x
-= 16;
429 if (signed_offsets
&& y
>= 8) y
-= 16;
431 /* Swap width and height depending on axis for railway stations */
432 if (axis
== INVALID_AXIS
&& HasStationTileRail(tile
)) axis
= GetRailStationAxis(tile
);
433 if (axis
== AXIS_Y
) Swap(x
, y
);
435 /* Make sure we never roam outside of the map, better wrap in that case */
436 return TILE_MASK(tile
+ TileDiffXY(x
, y
));
440 * Common part of station var 0x67, house var 0x62, indtile var 0x60, industry var 0x62.
442 * @param tile the tile of interest.
443 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
444 * @return 0czzbbss: c = TileType; zz = TileZ; bb: 7-3 zero, 4-2 TerrainType, 1 water/shore, 0 zero; ss = TileSlope
446 uint32
GetNearbyTileInformation(TileIndex tile
, bool grf_version8
)
448 TileType tile_type
= GetTileType(tile
);
450 /* Fake tile type for trees on shore */
451 if (IsTileType(tile
, MP_TREES
) && GetTreeGround(tile
) == TREE_GROUND_SHORE
) tile_type
= MP_WATER
;
454 Slope tileh
= GetTilePixelSlope(tile
, &z
);
455 /* Return 0 if the tile is a land tile */
456 byte terrain_type
= (HasTileWaterClass(tile
) ? (GetWaterClass(tile
) + 1) & 3 : 0) << 5 | GetTerrainType(tile
) << 2 | (tile_type
== MP_WATER
? 1 : 0) << 1;
457 if (grf_version8
) z
/= TILE_HEIGHT
;
458 return tile_type
<< 24 | Clamp(z
, 0, 0xFF) << 16 | terrain_type
<< 8 | tileh
;
462 * Returns company information like in vehicle var 43 or station var 43.
463 * @param owner Owner of the object.
464 * @param l Livery of the object; nullptr to use default.
465 * @return NewGRF company information.
467 uint32
GetCompanyInfo(CompanyID owner
, const Livery
*l
)
469 if (l
== nullptr && Company::IsValidID(owner
)) l
= &Company::Get(owner
)->livery
[LS_DEFAULT
];
470 return owner
| (Company::IsValidAiID(owner
) ? 0x10000 : 0) | (l
!= nullptr ? (l
->colour1
<< 24) | (l
->colour2
<< 28) : 0);
474 * Get the error message from a shape/location/slope check callback result.
475 * @param cb_res Callback result to translate. If bit 10 is set this is a standard error message, otherwise a NewGRF provided string.
476 * @param grffile NewGRF to use to resolve a custom error message.
477 * @param default_error Error message to use for the generic error.
478 * @return CommandCost indicating success or the error message.
480 CommandCost
GetErrorMessageFromLocationCallbackResult(uint16 cb_res
, const GRFFile
*grffile
, StringID default_error
)
484 if (cb_res
< 0x400) {
485 res
= CommandCost(GetGRFStringID(grffile
->grfid
, 0xD000 + cb_res
));
488 case 0x400: return res
; // No error.
490 default: // unknown reason -> default error
491 case 0x401: res
= CommandCost(default_error
); break;
493 case 0x402: res
= CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST
); break;
494 case 0x403: res
= CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT
); break;
495 case 0x404: res
= CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_ABOVE_SNOW_LINE
); break;
496 case 0x405: res
= CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_BELOW_SNOW_LINE
); break;
497 case 0x406: res
= CommandCost(STR_ERROR_CAN_T_BUILD_ON_SEA
); break;
498 case 0x407: res
= CommandCost(STR_ERROR_CAN_T_BUILD_ON_CANAL
); break;
499 case 0x408: res
= CommandCost(STR_ERROR_CAN_T_BUILD_ON_RIVER
); break;
503 /* Copy some parameters from the registers to the error message text ref. stack */
504 res
.UseTextRefStack(grffile
, 4);
510 * Record that a NewGRF returned an unknown/invalid callback result.
511 * Also show an error to the user.
512 * @param grfid ID of the NewGRF causing the problem.
513 * @param cbid Callback causing the problem.
514 * @param cb_res Invalid result returned by the callback.
516 void ErrorUnknownCallbackResult(uint32 grfid
, uint16 cbid
, uint16 cb_res
)
518 GRFConfig
*grfconfig
= GetGRFConfig(grfid
);
520 if (!HasBit(grfconfig
->grf_bugs
, GBUG_UNKNOWN_CB_RESULT
)) {
521 SetBit(grfconfig
->grf_bugs
, GBUG_UNKNOWN_CB_RESULT
);
522 SetDParamStr(0, grfconfig
->GetName());
524 SetDParam(2, cb_res
);
525 ShowErrorMessage(STR_NEWGRF_BUGGY
, STR_NEWGRF_BUGGY_UNKNOWN_CALLBACK_RESULT
, WL_CRITICAL
);
531 SetDParamStr(0, grfconfig
->GetName());
532 GetString(buffer
, STR_NEWGRF_BUGGY
, lastof(buffer
));
533 Debug(grf
, 0, "{}", buffer
+ 3);
536 SetDParam(2, cb_res
);
537 GetString(buffer
, STR_NEWGRF_BUGGY_UNKNOWN_CALLBACK_RESULT
, lastof(buffer
));
538 Debug(grf
, 0, "{}", buffer
+ 3);
542 * Converts a callback result into a boolean.
543 * For grf version < 8 the result is checked for zero or non-zero.
544 * For grf version >= 8 the callback result must be 0 or 1.
545 * @param grffile NewGRF returning the value.
546 * @param cbid Callback returning the value.
547 * @param cb_res Callback result.
548 * @return Boolean value. True if cb_res != 0.
550 bool ConvertBooleanCallback(const GRFFile
*grffile
, uint16 cbid
, uint16 cb_res
)
552 assert(cb_res
!= CALLBACK_FAILED
); // We do not know what to return
554 if (grffile
->grf_version
< 8) return cb_res
!= 0;
556 if (cb_res
> 1) ErrorUnknownCallbackResult(grffile
->grfid
, cbid
, cb_res
);
561 * Converts a callback result into a boolean.
562 * For grf version < 8 the first 8 bit of the result are checked for zero or non-zero.
563 * For grf version >= 8 the callback result must be 0 or 1.
564 * @param grffile NewGRF returning the value.
565 * @param cbid Callback returning the value.
566 * @param cb_res Callback result.
567 * @return Boolean value. True if cb_res != 0.
569 bool Convert8bitBooleanCallback(const GRFFile
*grffile
, uint16 cbid
, uint16 cb_res
)
571 assert(cb_res
!= CALLBACK_FAILED
); // We do not know what to return
573 if (grffile
->grf_version
< 8) return GB(cb_res
, 0, 8) != 0;
575 if (cb_res
> 1) ErrorUnknownCallbackResult(grffile
->grfid
, cbid
, cb_res
);
580 /* static */ std::vector
<DrawTileSeqStruct
> NewGRFSpriteLayout::result_seq
;
583 * Clone the building sprites of a spritelayout.
584 * @param source The building sprites to copy.
586 void NewGRFSpriteLayout::Clone(const DrawTileSeqStruct
*source
)
588 assert(this->seq
== nullptr);
589 assert(source
!= nullptr);
591 size_t count
= 1; // 1 for the terminator
592 const DrawTileSeqStruct
*element
;
593 foreach_draw_tile_seq(element
, source
) count
++;
595 DrawTileSeqStruct
*sprites
= MallocT
<DrawTileSeqStruct
>(count
);
596 MemCpyT(sprites
, source
, count
);
601 * Clone a spritelayout.
602 * @param source The spritelayout to copy.
604 void NewGRFSpriteLayout::Clone(const NewGRFSpriteLayout
*source
)
606 this->Clone((const DrawTileSprites
*)source
);
608 if (source
->registers
!= nullptr) {
609 size_t count
= 1; // 1 for the ground sprite
610 const DrawTileSeqStruct
*element
;
611 foreach_draw_tile_seq(element
, source
->seq
) count
++;
613 TileLayoutRegisters
*regs
= MallocT
<TileLayoutRegisters
>(count
);
614 MemCpyT(regs
, source
->registers
, count
);
615 this->registers
= regs
;
621 * Allocate a spritelayout for \a num_sprites building sprites.
622 * @param num_sprites Number of building sprites to allocate memory for. (not counting the terminator)
624 void NewGRFSpriteLayout::Allocate(uint num_sprites
)
626 assert(this->seq
== nullptr);
628 DrawTileSeqStruct
*sprites
= CallocT
<DrawTileSeqStruct
>(num_sprites
+ 1);
629 sprites
[num_sprites
].MakeTerminator();
634 * Allocate memory for register modifiers.
636 void NewGRFSpriteLayout::AllocateRegisters()
638 assert(this->seq
!= nullptr);
639 assert(this->registers
== nullptr);
641 size_t count
= 1; // 1 for the ground sprite
642 const DrawTileSeqStruct
*element
;
643 foreach_draw_tile_seq(element
, this->seq
) count
++;
645 this->registers
= CallocT
<TileLayoutRegisters
>(count
);
649 * Prepares a sprite layout before resolving action-1-2-3 chains.
650 * Integrates offsets into the layout and determines which chains to resolve.
651 * @note The function uses statically allocated temporary storage, which is reused every time when calling the function.
652 * That means, you have to use the sprite layout before calling #PrepareLayout() the next time.
653 * @param orig_offset Offset to apply to non-action-1 sprites.
654 * @param newgrf_ground_offset Offset to apply to action-1 ground sprites.
655 * @param newgrf_offset Offset to apply to action-1 non-ground sprites.
656 * @param constr_stage Construction stage (0-3) to apply to all action-1 sprites.
657 * @param separate_ground Whether the ground sprite shall be resolved by a separate action-1-2-3 chain by default.
658 * @return Bitmask of values for variable 10 to resolve action-1-2-3 chains for.
660 uint32
NewGRFSpriteLayout::PrepareLayout(uint32 orig_offset
, uint32 newgrf_ground_offset
, uint32 newgrf_offset
, uint constr_stage
, bool separate_ground
) const
663 uint32 var10_values
= 0;
665 /* Create a copy of the spritelayout, so we can modify some values.
666 * Also include the groundsprite into the sequence for easier processing. */
667 DrawTileSeqStruct
*result
= &result_seq
.emplace_back();
668 result
->image
= ground
;
671 result
->delta_z
= (int8
)0x80;
673 const DrawTileSeqStruct
*dtss
;
674 foreach_draw_tile_seq(dtss
, this->seq
) {
675 result_seq
.push_back(*dtss
);
677 result_seq
.emplace_back().MakeTerminator();
678 /* Determine the var10 values the action-1-2-3 chains needs to be resolved for,
679 * and apply the default sprite offsets (unless disabled). */
680 const TileLayoutRegisters
*regs
= this->registers
;
682 foreach_draw_tile_seq(result
, result_seq
.data()) {
683 TileLayoutFlags flags
= TLF_NOTHING
;
684 if (regs
!= nullptr) flags
= regs
->flags
;
686 /* Record var10 value for the sprite */
687 if (HasBit(result
->image
.sprite
, SPRITE_MODIFIER_CUSTOM_SPRITE
) || (flags
& TLF_SPRITE_REG_FLAGS
)) {
688 uint8 var10
= (flags
& TLF_SPRITE_VAR10
) ? regs
->sprite_var10
: (ground
&& separate_ground
? 1 : 0);
689 SetBit(var10_values
, var10
);
692 /* Add default sprite offset, unless there is a custom one */
693 if (!(flags
& TLF_SPRITE
)) {
694 if (HasBit(result
->image
.sprite
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) {
695 result
->image
.sprite
+= ground
? newgrf_ground_offset
: newgrf_offset
;
696 if (constr_stage
> 0 && regs
!= nullptr) result
->image
.sprite
+= GetConstructionStageOffset(constr_stage
, regs
->max_sprite_offset
);
698 result
->image
.sprite
+= orig_offset
;
702 /* Record var10 value for the palette */
703 if (HasBit(result
->image
.pal
, SPRITE_MODIFIER_CUSTOM_SPRITE
) || (flags
& TLF_PALETTE_REG_FLAGS
)) {
704 uint8 var10
= (flags
& TLF_PALETTE_VAR10
) ? regs
->palette_var10
: (ground
&& separate_ground
? 1 : 0);
705 SetBit(var10_values
, var10
);
708 /* Add default palette offset, unless there is a custom one */
709 if (!(flags
& TLF_PALETTE
)) {
710 if (HasBit(result
->image
.pal
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) {
711 result
->image
.sprite
+= ground
? newgrf_ground_offset
: newgrf_offset
;
712 if (constr_stage
> 0 && regs
!= nullptr) result
->image
.sprite
+= GetConstructionStageOffset(constr_stage
, regs
->max_palette_offset
);
717 if (regs
!= nullptr) regs
++;
724 * Evaluates the register modifiers and integrates them into the preprocessed sprite layout.
725 * @pre #PrepareLayout() needs calling first.
726 * @param resolved_var10 The value of var10 the action-1-2-3 chain was evaluated for.
727 * @param resolved_sprite Result sprite of the action-1-2-3 chain.
728 * @param separate_ground Whether the ground sprite is resolved by a separate action-1-2-3 chain.
729 * @return Resulting spritelayout after processing the registers.
731 void NewGRFSpriteLayout::ProcessRegisters(uint8 resolved_var10
, uint32 resolved_sprite
, bool separate_ground
) const
733 DrawTileSeqStruct
*result
;
734 const TileLayoutRegisters
*regs
= this->registers
;
736 foreach_draw_tile_seq(result
, result_seq
.data()) {
737 TileLayoutFlags flags
= TLF_NOTHING
;
738 if (regs
!= nullptr) flags
= regs
->flags
;
740 /* Is the sprite or bounding box affected by an action-1-2-3 chain? */
741 if (HasBit(result
->image
.sprite
, SPRITE_MODIFIER_CUSTOM_SPRITE
) || (flags
& TLF_SPRITE_REG_FLAGS
)) {
742 /* Does the var10 value apply to this sprite? */
743 uint8 var10
= (flags
& TLF_SPRITE_VAR10
) ? regs
->sprite_var10
: (ground
&& separate_ground
? 1 : 0);
744 if (var10
== resolved_var10
) {
745 /* Apply registers */
746 if ((flags
& TLF_DODRAW
) && GetRegister(regs
->dodraw
) == 0) {
747 result
->image
.sprite
= 0;
749 if (HasBit(result
->image
.sprite
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) result
->image
.sprite
+= resolved_sprite
;
750 if (flags
& TLF_SPRITE
) {
751 int16 offset
= (int16
)GetRegister(regs
->sprite
); // mask to 16 bits to avoid trouble
752 if (!HasBit(result
->image
.sprite
, SPRITE_MODIFIER_CUSTOM_SPRITE
) || (offset
>= 0 && offset
< regs
->max_sprite_offset
)) {
753 result
->image
.sprite
+= offset
;
755 result
->image
.sprite
= SPR_IMG_QUERY
;
759 if (result
->IsParentSprite()) {
760 if (flags
& TLF_BB_XY_OFFSET
) {
761 result
->delta_x
+= (int32
)GetRegister(regs
->delta
.parent
[0]);
762 result
->delta_y
+= (int32
)GetRegister(regs
->delta
.parent
[1]);
764 if (flags
& TLF_BB_Z_OFFSET
) result
->delta_z
+= (int32
)GetRegister(regs
->delta
.parent
[2]);
766 if (flags
& TLF_CHILD_X_OFFSET
) result
->delta_x
+= (int32
)GetRegister(regs
->delta
.child
[0]);
767 if (flags
& TLF_CHILD_Y_OFFSET
) result
->delta_y
+= (int32
)GetRegister(regs
->delta
.child
[1]);
773 /* Is the palette affected by an action-1-2-3 chain? */
774 if (result
->image
.sprite
!= 0 && (HasBit(result
->image
.pal
, SPRITE_MODIFIER_CUSTOM_SPRITE
) || (flags
& TLF_PALETTE_REG_FLAGS
))) {
775 /* Does the var10 value apply to this sprite? */
776 uint8 var10
= (flags
& TLF_PALETTE_VAR10
) ? regs
->palette_var10
: (ground
&& separate_ground
? 1 : 0);
777 if (var10
== resolved_var10
) {
778 /* Apply registers */
779 if (HasBit(result
->image
.pal
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) result
->image
.pal
+= resolved_sprite
;
780 if (flags
& TLF_PALETTE
) {
781 int16 offset
= (int16
)GetRegister(regs
->palette
); // mask to 16 bits to avoid trouble
782 if (!HasBit(result
->image
.pal
, SPRITE_MODIFIER_CUSTOM_SPRITE
) || (offset
>= 0 && offset
< regs
->max_palette_offset
)) {
783 result
->image
.pal
+= offset
;
785 result
->image
.sprite
= SPR_IMG_QUERY
;
786 result
->image
.pal
= PAL_NONE
;
793 if (regs
!= nullptr) regs
++;