OpenTTD Source 20250524-master-gc366e6a48e
newgrf_act2.cpp
Go to the documentation of this file.
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 */
7
10#include "../stdafx.h"
11#include <ranges>
12#include "../debug.h"
13#include "../newgrf_engine.h"
14#include "../newgrf_cargo.h"
15#include "../error.h"
16#include "../vehicle_base.h"
17#include "../road.h"
18#include "newgrf_bytereader.h"
19#include "newgrf_internal.h"
20
21#include "table/strings.h"
22
23#include "../safeguards.h"
24
25constexpr uint16_t GROUPID_CALLBACK_FAILED = 0x7FFF;
26constexpr uint16_t GROUPID_CALCULATED_RESULT = 0x7FFE;
27
33{
34 if (HasBit(grf_sprite->pal, 14)) {
35 ClrBit(grf_sprite->pal, 14);
37 }
38
39 if (HasBit(grf_sprite->sprite, 14)) {
40 ClrBit(grf_sprite->sprite, 14);
42 }
43
44 if (HasBit(grf_sprite->sprite, 15)) {
45 ClrBit(grf_sprite->sprite, 15);
47 }
48}
49
63TileLayoutFlags ReadSpriteLayoutSprite(ByteReader &buf, bool read_flags, bool invert_action1_flag, bool use_cur_spritesets, GrfSpecFeature feature, PalSpriteID *grf_sprite, uint16_t *max_sprite_offset, uint16_t *max_palette_offset)
64{
65 grf_sprite->sprite = buf.ReadWord();
66 grf_sprite->pal = buf.ReadWord();
67 TileLayoutFlags flags = read_flags ? (TileLayoutFlags)buf.ReadWord() : TLF_NOTHING;
68
69 MapSpriteMappingRecolour(grf_sprite);
70
71 bool custom_sprite = HasBit(grf_sprite->pal, 15) != invert_action1_flag;
72 ClrBit(grf_sprite->pal, 15);
73 if (custom_sprite) {
74 /* Use sprite from Action 1 */
75 uint index = GB(grf_sprite->sprite, 0, 14);
76 if (use_cur_spritesets && (!_cur_gps.IsValidSpriteSet(feature, index) || _cur_gps.GetNumEnts(feature, index) == 0)) {
77 GrfMsg(1, "ReadSpriteLayoutSprite: Spritelayout uses undefined custom spriteset {}", index);
78 grf_sprite->sprite = SPR_IMG_QUERY;
79 grf_sprite->pal = PAL_NONE;
80 } else {
81 SpriteID sprite = use_cur_spritesets ? _cur_gps.GetSprite(feature, index) : index;
82 if (max_sprite_offset != nullptr) *max_sprite_offset = use_cur_spritesets ? _cur_gps.GetNumEnts(feature, index) : UINT16_MAX;
83 SB(grf_sprite->sprite, 0, SPRITE_WIDTH, sprite);
85 }
86 } else if ((flags & TLF_SPRITE_VAR10) && !(flags & TLF_SPRITE_REG_FLAGS)) {
87 GrfMsg(1, "ReadSpriteLayoutSprite: Spritelayout specifies var10 value for non-action-1 sprite");
88 DisableGrf(STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT);
89 return flags;
90 }
91
92 if (flags & TLF_CUSTOM_PALETTE) {
93 /* Use palette from Action 1 */
94 uint index = GB(grf_sprite->pal, 0, 14);
95 if (use_cur_spritesets && (!_cur_gps.IsValidSpriteSet(feature, index) || _cur_gps.GetNumEnts(feature, index) == 0)) {
96 GrfMsg(1, "ReadSpriteLayoutSprite: Spritelayout uses undefined custom spriteset {} for 'palette'", index);
97 grf_sprite->pal = PAL_NONE;
98 } else {
99 SpriteID sprite = use_cur_spritesets ? _cur_gps.GetSprite(feature, index) : index;
100 if (max_palette_offset != nullptr) *max_palette_offset = use_cur_spritesets ? _cur_gps.GetNumEnts(feature, index) : UINT16_MAX;
101 SB(grf_sprite->pal, 0, SPRITE_WIDTH, sprite);
103 }
104 } else if ((flags & TLF_PALETTE_VAR10) && !(flags & TLF_PALETTE_REG_FLAGS)) {
105 GrfMsg(1, "ReadSpriteLayoutRegisters: Spritelayout specifies var10 value for non-action-1 palette");
106 DisableGrf(STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT);
107 return flags;
108 }
109
110 return flags;
111}
112
121static void ReadSpriteLayoutRegisters(ByteReader &buf, TileLayoutFlags flags, bool is_parent, NewGRFSpriteLayout *dts, uint index)
122{
123 if (!(flags & TLF_DRAWING_FLAGS)) return;
124
125 if (dts->registers.empty()) dts->AllocateRegisters();
126 TileLayoutRegisters &regs = const_cast<TileLayoutRegisters&>(dts->registers[index]);
127 regs.flags = flags & TLF_DRAWING_FLAGS;
128
129 if (flags & TLF_DODRAW) regs.dodraw = buf.ReadByte();
130 if (flags & TLF_SPRITE) regs.sprite = buf.ReadByte();
131 if (flags & TLF_PALETTE) regs.palette = buf.ReadByte();
132
133 if (is_parent) {
134 if (flags & TLF_BB_XY_OFFSET) {
135 regs.delta.parent[0] = buf.ReadByte();
136 regs.delta.parent[1] = buf.ReadByte();
137 }
138 if (flags & TLF_BB_Z_OFFSET) regs.delta.parent[2] = buf.ReadByte();
139 } else {
140 if (flags & TLF_CHILD_X_OFFSET) regs.delta.child[0] = buf.ReadByte();
141 if (flags & TLF_CHILD_Y_OFFSET) regs.delta.child[1] = buf.ReadByte();
142 }
143
144 if (flags & TLF_SPRITE_VAR10) {
145 regs.sprite_var10 = buf.ReadByte();
146 if (regs.sprite_var10 > TLR_MAX_VAR10) {
147 GrfMsg(1, "ReadSpriteLayoutRegisters: Spritelayout specifies var10 ({}) exceeding the maximal allowed value {}", regs.sprite_var10, TLR_MAX_VAR10);
148 DisableGrf(STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT);
149 return;
150 }
151 }
152
153 if (flags & TLF_PALETTE_VAR10) {
154 regs.palette_var10 = buf.ReadByte();
155 if (regs.palette_var10 > TLR_MAX_VAR10) {
156 GrfMsg(1, "ReadSpriteLayoutRegisters: Spritelayout specifies var10 ({}) exceeding the maximal allowed value {}", regs.palette_var10, TLR_MAX_VAR10);
157 DisableGrf(STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT);
158 return;
159 }
160 }
161}
162
174bool ReadSpriteLayout(ByteReader &buf, uint num_building_sprites, bool use_cur_spritesets, GrfSpecFeature feature, bool allow_var10, bool no_z_position, NewGRFSpriteLayout *dts)
175{
176 bool has_flags = HasBit(num_building_sprites, 6);
177 ClrBit(num_building_sprites, 6);
178 TileLayoutFlags valid_flags = TLF_KNOWN_FLAGS;
179 if (!allow_var10) valid_flags &= ~TLF_VAR10_FLAGS;
180 dts->Allocate(num_building_sprites); // allocate before reading groundsprite flags
181
182 std::vector<uint16_t> max_sprite_offset(num_building_sprites + 1, 0);
183 std::vector<uint16_t> max_palette_offset(num_building_sprites + 1, 0);
184
185 /* Groundsprite */
186 TileLayoutFlags flags = ReadSpriteLayoutSprite(buf, has_flags, false, use_cur_spritesets, feature, &dts->ground, max_sprite_offset.data(), max_palette_offset.data());
187 if (_cur_gps.skip_sprites < 0) return true;
188
189 if (flags & ~(valid_flags & ~TLF_NON_GROUND_FLAGS)) {
190 GrfMsg(1, "ReadSpriteLayout: Spritelayout uses invalid flag 0x{:X} for ground sprite", flags & ~(valid_flags & ~TLF_NON_GROUND_FLAGS));
191 DisableGrf(STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT);
192 return true;
193 }
194
195 ReadSpriteLayoutRegisters(buf, flags, false, dts, 0);
196 if (_cur_gps.skip_sprites < 0) return true;
197
198 for (uint i = 0; i < num_building_sprites; i++) {
199 DrawTileSeqStruct *seq = const_cast<DrawTileSeqStruct*>(&dts->seq[i]);
200
201 flags = ReadSpriteLayoutSprite(buf, has_flags, false, use_cur_spritesets, feature, &seq->image, max_sprite_offset.data() + i + 1, max_palette_offset.data() + i + 1);
202 if (_cur_gps.skip_sprites < 0) return true;
203
204 if (flags & ~valid_flags) {
205 GrfMsg(1, "ReadSpriteLayout: Spritelayout uses unknown flag 0x{:X}", flags & ~valid_flags);
206 DisableGrf(STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT);
207 return true;
208 }
209
210 seq->delta_x = buf.ReadByte();
211 seq->delta_y = buf.ReadByte();
212
213 if (!no_z_position) seq->delta_z = buf.ReadByte();
214
215 if (seq->IsParentSprite()) {
216 seq->size_x = buf.ReadByte();
217 seq->size_y = buf.ReadByte();
218 seq->size_z = buf.ReadByte();
219 }
220
221 ReadSpriteLayoutRegisters(buf, flags, seq->IsParentSprite(), dts, i + 1);
222 if (_cur_gps.skip_sprites < 0) return true;
223 }
224
225 /* Check if the number of sprites per spriteset is consistent */
226 bool is_consistent = true;
227 dts->consistent_max_offset = 0;
228 for (uint i = 0; i < num_building_sprites + 1; i++) {
229 if (max_sprite_offset[i] > 0) {
230 if (dts->consistent_max_offset == 0) {
231 dts->consistent_max_offset = max_sprite_offset[i];
232 } else if (dts->consistent_max_offset != max_sprite_offset[i]) {
233 is_consistent = false;
234 break;
235 }
236 }
237 if (max_palette_offset[i] > 0) {
238 if (dts->consistent_max_offset == 0) {
239 dts->consistent_max_offset = max_palette_offset[i];
240 } else if (dts->consistent_max_offset != max_palette_offset[i]) {
241 is_consistent = false;
242 break;
243 }
244 }
245 }
246
247 /* When the Action1 sets are unknown, everything should be 0 (no spriteset usage) or UINT16_MAX (some spriteset usage) */
248 assert(use_cur_spritesets || (is_consistent && (dts->consistent_max_offset == 0 || dts->consistent_max_offset == UINT16_MAX)));
249
250 if (!is_consistent || !dts->registers.empty()) {
251 dts->consistent_max_offset = 0;
252 if (dts->registers.empty()) dts->AllocateRegisters();
253
254 for (uint i = 0; i < num_building_sprites + 1; i++) {
255 TileLayoutRegisters &regs = const_cast<TileLayoutRegisters&>(dts->registers[i]);
256 regs.max_sprite_offset = max_sprite_offset[i];
257 regs.max_palette_offset = max_palette_offset[i];
258 }
259 }
260
261 return false;
262}
263
264using CachedCallback = std::pair<uint16_t, SpriteGroupID>;
265static std::vector<CachedCallback> _cached_callback_groups;
266
267void ResetCallbacks(bool final)
268{
270 if (final) _cached_callback_groups.shrink_to_fit();
271}
272
273static const SpriteGroup *GetCallbackResultGroup(uint16_t value)
274{
275 /* Old style callback results (only valid for version < 8) have the highest byte 0xFF to signify it is a callback result.
276 * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
277 if (_cur_gps.grffile->grf_version < 8 && GB(value, 8, 8) == 0xFF) {
278 value &= ~0xFF00;
279 } else {
280 value &= ~0x8000;
281 }
282
283 /* Find position for value within the cached callback list. */
284 auto it = std::ranges::lower_bound(_cached_callback_groups, value, std::less{}, &CachedCallback::first);
285 if (it != std::end(_cached_callback_groups) && it->first == value) return SpriteGroup::Get(it->second);
286
287 /* Result value is not present, so make it and add to cache. */
289 const SpriteGroup *group = new CallbackResultSpriteGroup(value);
290 it = _cached_callback_groups.emplace(it, value, group->index);
291 return group;
292}
293
294/* Helper function to either create a callback or link to a previously
295 * defined spritegroup. */
296static const SpriteGroup *GetGroupFromGroupID(uint8_t setid, uint8_t type, uint16_t groupid)
297{
298 if (HasBit(groupid, 15)) return GetCallbackResultGroup(groupid);
299 if (groupid == GROUPID_CALLBACK_FAILED) return nullptr;
300
301 if (groupid > MAX_SPRITEGROUP || _cur_gps.spritegroups[groupid] == nullptr) {
302 GrfMsg(1, "GetGroupFromGroupID(0x{:02X}:0x{:02X}): Groupid 0x{:04X} does not exist, leaving empty", setid, type, groupid);
303 return nullptr;
304 }
305
306 return _cur_gps.spritegroups[groupid];
307}
308
317static const SpriteGroup *CreateGroupFromGroupID(GrfSpecFeature feature, uint8_t setid, uint8_t type, uint16_t spriteid)
318{
319 if (HasBit(spriteid, 15)) return GetCallbackResultGroup(spriteid);
320
321 if (!_cur_gps.IsValidSpriteSet(feature, spriteid)) {
322 GrfMsg(1, "CreateGroupFromGroupID(0x{:02X}:0x{:02X}): Sprite set {} invalid", setid, type, spriteid);
323 return nullptr;
324 }
325
326 SpriteID spriteset_start = _cur_gps.GetSprite(feature, spriteid);
327 uint num_sprites = _cur_gps.GetNumEnts(feature, spriteid);
328
329 /* Ensure that the sprites are loeded */
330 assert(spriteset_start + num_sprites <= _cur_gps.spriteid);
331
333 return new ResultSpriteGroup(spriteset_start, num_sprites);
334}
335
336/* Action 0x02 */
337static void NewSpriteGroup(ByteReader &buf)
338{
339 /* <02> <feature> <set-id> <type/num-entries> <feature-specific-data...>
340 *
341 * B feature see action 1
342 * B set-id ID of this particular definition
343 * B type/num-entries
344 * if 80 or greater, this is a randomized or variational
345 * list definition, see below
346 * otherwise it specifies a number of entries, the exact
347 * meaning depends on the feature
348 * V feature-specific-data (huge mess, don't even look it up --pasky) */
349 const SpriteGroup *act_group = nullptr;
350
351 GrfSpecFeature feature{buf.ReadByte()};
352 if (feature >= GSF_END) {
353 GrfMsg(1, "NewSpriteGroup: Unsupported feature 0x{:02X}, skipping", feature);
354 return;
355 }
356
357 uint8_t setid = buf.ReadByte();
358 uint8_t type = buf.ReadByte();
359
360 /* Sprite Groups are created here but they are allocated from a pool, so
361 * we do not need to delete anything if there is an exception from the
362 * ByteReader. */
363
364 switch (type) {
365 /* Deterministic Sprite Group */
366 case 0x81: // Self scope, byte
367 case 0x82: // Parent scope, byte
368 case 0x85: // Self scope, word
369 case 0x86: // Parent scope, word
370 case 0x89: // Self scope, dword
371 case 0x8A: // Parent scope, dword
372 {
373 uint8_t varadjust;
374 uint8_t varsize;
375
378 group->nfo_line = _cur_gps.nfo_line;
379 act_group = group;
380 group->var_scope = HasBit(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
381
382 switch (GB(type, 2, 2)) {
383 default: NOT_REACHED();
384 case 0: group->size = DSG_SIZE_BYTE; varsize = 1; break;
385 case 1: group->size = DSG_SIZE_WORD; varsize = 2; break;
386 case 2: group->size = DSG_SIZE_DWORD; varsize = 4; break;
387 }
388
389 /* Loop through the var adjusts. Unfortunately we don't know how many we have
390 * from the outset, so we shall have to keep reallocing. */
391 do {
392 DeterministicSpriteGroupAdjust &adjust = group->adjusts.emplace_back();
393
394 /* The first var adjust doesn't have an operation specified, so we set it to add. */
395 adjust.operation = group->adjusts.size() == 1 ? DSGA_OP_ADD : (DeterministicSpriteGroupAdjustOperation)buf.ReadByte();
396 adjust.variable = buf.ReadByte();
397 if (adjust.variable == 0x7E) {
398 /* Link subroutine group */
399 adjust.subroutine = GetGroupFromGroupID(setid, type, buf.ReadByte());
400 } else {
401 adjust.parameter = IsInsideMM(adjust.variable, 0x60, 0x80) ? buf.ReadByte() : 0;
402 }
403
404 varadjust = buf.ReadByte();
405 adjust.shift_num = GB(varadjust, 0, 5);
406 adjust.type = (DeterministicSpriteGroupAdjustType)GB(varadjust, 6, 2);
407 adjust.and_mask = buf.ReadVarSize(varsize);
408
409 if (adjust.type != DSGA_TYPE_NONE) {
410 adjust.add_val = buf.ReadVarSize(varsize);
411 adjust.divmod_val = buf.ReadVarSize(varsize);
412 if (adjust.divmod_val == 0) adjust.divmod_val = 1; // Ensure that divide by zero cannot occur
413 } else {
414 adjust.add_val = 0;
415 adjust.divmod_val = 0;
416 }
417
418 /* Continue reading var adjusts while bit 5 is set. */
419 } while (HasBit(varadjust, 5));
420
421 std::vector<DeterministicSpriteGroupRange> ranges;
422 ranges.resize(buf.ReadByte());
423 for (auto &range : ranges) {
424 auto groupid = buf.ReadWord();
425 if (groupid == GROUPID_CALCULATED_RESULT) {
426 range.result.calculated_result = true;
427 } else {
428 range.result.group = GetGroupFromGroupID(setid, type, groupid);
429 }
430 range.low = buf.ReadVarSize(varsize);
431 range.high = buf.ReadVarSize(varsize);
432 }
433
434 auto defgroupid = buf.ReadWord();
435 if (defgroupid == GROUPID_CALCULATED_RESULT) {
436 group->default_result.calculated_result = true;
437 } else {
438 group->default_result.group = GetGroupFromGroupID(setid, type, defgroupid);
439 }
440 /* 'calculated_result' makes no sense for the 'error' case. Use callback failure (nullptr) instead */
441 group->error_group = ranges.empty() ? group->default_result.group : ranges[0].result.group;
442 /* nvar == 0 is a special case:
443 * - set "default_result" to "calculated_result".
444 * - the old value specifies the "error_group". */
445 if (ranges.empty()) {
446 group->default_result.calculated_result = true;
447 group->default_result.group = nullptr;
448 }
449
450 /* Sort ranges ascending. When ranges overlap, this may required clamping or splitting them */
451 std::vector<uint32_t> bounds;
452 bounds.reserve(ranges.size());
453 for (const auto &range : ranges) {
454 bounds.push_back(range.low);
455 if (range.high != UINT32_MAX) bounds.push_back(range.high + 1);
456 }
457 std::sort(bounds.begin(), bounds.end());
458 bounds.erase(std::unique(bounds.begin(), bounds.end()), bounds.end());
459
460 std::vector<DeterministicSpriteGroupResult> target;
461 target.reserve(bounds.size());
462 for (const auto &bound : bounds) {
463 auto t = group->default_result;
464 for (const auto &range : ranges) {
465 if (range.low <= bound && bound <= range.high) {
466 t = range.result;
467 break;
468 }
469 }
470 target.push_back(t);
471 }
472 assert(target.size() == bounds.size());
473
474 for (uint j = 0; j < bounds.size(); ) {
475 if (target[j] != group->default_result) {
476 DeterministicSpriteGroupRange &r = group->ranges.emplace_back();
477 r.result = target[j];
478 r.low = bounds[j];
479 while (j < bounds.size() && target[j] == r.result) {
480 j++;
481 }
482 r.high = j < bounds.size() ? bounds[j] - 1 : UINT32_MAX;
483 } else {
484 j++;
485 }
486 }
487
488 break;
489 }
490
491 /* Randomized Sprite Group */
492 case 0x80: // Self scope
493 case 0x83: // Parent scope
494 case 0x84: // Relative scope
495 {
498 group->nfo_line = _cur_gps.nfo_line;
499 act_group = group;
500 group->var_scope = HasBit(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
501
502 if (HasBit(type, 2)) {
503 if (feature <= GSF_AIRCRAFT) group->var_scope = VSG_SCOPE_RELATIVE;
504 group->count = buf.ReadByte();
505 }
506
507 uint8_t triggers = buf.ReadByte();
508 group->triggers = GB(triggers, 0, 7);
509 group->cmp_mode = HasBit(triggers, 7) ? RSG_CMP_ALL : RSG_CMP_ANY;
510 group->lowest_randbit = buf.ReadByte();
511
512 uint8_t num_groups = buf.ReadByte();
513 if (!HasExactlyOneBit(num_groups)) {
514 GrfMsg(1, "NewSpriteGroup: Random Action 2 nrand should be power of 2");
515 }
516
517 group->groups.reserve(num_groups);
518 for (uint i = 0; i < num_groups; i++) {
519 group->groups.push_back(GetGroupFromGroupID(setid, type, buf.ReadWord()));
520 }
521
522 break;
523 }
524
525 /* Neither a variable or randomized sprite group... must be a real group */
526 default:
527 {
528 switch (feature) {
529 case GSF_TRAINS:
530 case GSF_ROADVEHICLES:
531 case GSF_SHIPS:
532 case GSF_AIRCRAFT:
533 case GSF_STATIONS:
534 case GSF_CANALS:
535 case GSF_CARGOES:
536 case GSF_AIRPORTS:
537 case GSF_RAILTYPES:
538 case GSF_ROADTYPES:
539 case GSF_TRAMTYPES:
540 case GSF_BADGES:
541 {
542 uint8_t num_loaded = type;
543 uint8_t num_loading = buf.ReadByte();
544
545 if (!_cur_gps.HasValidSpriteSets(feature)) {
546 GrfMsg(0, "NewSpriteGroup: No sprite set to work on! Skipping");
547 return;
548 }
549
550 GrfMsg(6, "NewSpriteGroup: New SpriteGroup 0x{:02X}, {} loaded, {} loading",
551 setid, num_loaded, num_loading);
552
553 if (num_loaded + num_loading == 0) {
554 GrfMsg(1, "NewSpriteGroup: no result, skipping invalid RealSpriteGroup");
555 break;
556 }
557
558 if (num_loaded + num_loading == 1) {
559 /* Avoid creating 'Real' sprite group if only one option. */
560 uint16_t spriteid = buf.ReadWord();
561 act_group = CreateGroupFromGroupID(feature, setid, type, spriteid);
562 GrfMsg(8, "NewSpriteGroup: one result, skipping RealSpriteGroup = subset {}", spriteid);
563 break;
564 }
565
566 std::vector<uint16_t> loaded;
567 std::vector<uint16_t> loading;
568
569 loaded.reserve(num_loaded);
570 for (uint i = 0; i < num_loaded; i++) {
571 loaded.push_back(buf.ReadWord());
572 GrfMsg(8, "NewSpriteGroup: + rg->loaded[{}] = subset {}", i, loaded[i]);
573 }
574
575 loading.reserve(num_loading);
576 for (uint i = 0; i < num_loading; i++) {
577 loading.push_back(buf.ReadWord());
578 GrfMsg(8, "NewSpriteGroup: + rg->loading[{}] = subset {}", i, loading[i]);
579 }
580
581 bool loaded_same = !loaded.empty() && std::adjacent_find(loaded.begin(), loaded.end(), std::not_equal_to<>()) == loaded.end();
582 bool loading_same = !loading.empty() && std::adjacent_find(loading.begin(), loading.end(), std::not_equal_to<>()) == loading.end();
583 if (loaded_same && loading_same && loaded[0] == loading[0]) {
584 /* Both lists only contain the same value, so don't create 'Real' sprite group */
585 act_group = CreateGroupFromGroupID(feature, setid, type, loaded[0]);
586 GrfMsg(8, "NewSpriteGroup: same result, skipping RealSpriteGroup = subset {}", loaded[0]);
587 break;
588 }
589
591 RealSpriteGroup *group = new RealSpriteGroup();
592 group->nfo_line = _cur_gps.nfo_line;
593 act_group = group;
594
595 if (loaded_same && loaded.size() > 1) loaded.resize(1);
596 group->loaded.reserve(loaded.size());
597 for (uint16_t spriteid : loaded) {
598 const SpriteGroup *t = CreateGroupFromGroupID(feature, setid, type, spriteid);
599 group->loaded.push_back(t);
600 }
601
602 if (loading_same && loading.size() > 1) loading.resize(1);
603 group->loading.reserve(loading.size());
604 for (uint16_t spriteid : loading) {
605 const SpriteGroup *t = CreateGroupFromGroupID(feature, setid, type, spriteid);
606 group->loading.push_back(t);
607 }
608
609 break;
610 }
611
612 case GSF_HOUSES:
613 case GSF_AIRPORTTILES:
614 case GSF_OBJECTS:
615 case GSF_INDUSTRYTILES:
616 case GSF_ROADSTOPS: {
617 uint8_t num_building_sprites = std::max((uint8_t)1, type);
618
621 group->nfo_line = _cur_gps.nfo_line;
622 act_group = group;
623
624 /* On error, bail out immediately. Temporary GRF data was already freed */
625 if (ReadSpriteLayout(buf, num_building_sprites, true, feature, false, type == 0, &group->dts)) return;
626 break;
627 }
628
629 case GSF_INDUSTRIES: {
630 if (type > 2) {
631 GrfMsg(1, "NewSpriteGroup: Unsupported industry production version {}, skipping", type);
632 break;
633 }
634
637 group->nfo_line = _cur_gps.nfo_line;
638 act_group = group;
639 group->version = type;
640 if (type == 0) {
642 for (uint i = 0; i < INDUSTRY_ORIGINAL_NUM_INPUTS; i++) {
643 group->subtract_input[i] = (int16_t)buf.ReadWord(); // signed
644 }
646 for (uint i = 0; i < INDUSTRY_ORIGINAL_NUM_OUTPUTS; i++) {
647 group->add_output[i] = buf.ReadWord(); // unsigned
648 }
649 group->again = buf.ReadByte();
650 } else if (type == 1) {
652 for (uint i = 0; i < INDUSTRY_ORIGINAL_NUM_INPUTS; i++) {
653 group->subtract_input[i] = buf.ReadByte();
654 }
656 for (uint i = 0; i < INDUSTRY_ORIGINAL_NUM_OUTPUTS; i++) {
657 group->add_output[i] = buf.ReadByte();
658 }
659 group->again = buf.ReadByte();
660 } else if (type == 2) {
661 group->num_input = buf.ReadByte();
662 if (group->num_input > std::size(group->subtract_input)) {
663 GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
664 error->data = "too many inputs (max 16)";
665 return;
666 }
667 for (uint i = 0; i < group->num_input; i++) {
668 uint8_t rawcargo = buf.ReadByte();
669 CargoType cargo = GetCargoTranslation(rawcargo, _cur_gps.grffile);
670 if (!IsValidCargoType(cargo)) {
671 /* The mapped cargo is invalid. This is permitted at this point,
672 * as long as the result is not used. Mark it invalid so this
673 * can be tested later. */
674 group->version = 0xFF;
675 } else if (auto v = group->cargo_input | std::views::take(i); std::ranges::find(v, cargo) != v.end()) {
676 GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
677 error->data = "duplicate input cargo";
678 return;
679 }
680 group->cargo_input[i] = cargo;
681 group->subtract_input[i] = buf.ReadByte();
682 }
683 group->num_output = buf.ReadByte();
684 if (group->num_output > std::size(group->add_output)) {
685 GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
686 error->data = "too many outputs (max 16)";
687 return;
688 }
689 for (uint i = 0; i < group->num_output; i++) {
690 uint8_t rawcargo = buf.ReadByte();
691 CargoType cargo = GetCargoTranslation(rawcargo, _cur_gps.grffile);
692 if (!IsValidCargoType(cargo)) {
693 /* Mark this result as invalid to use */
694 group->version = 0xFF;
695 } else if (auto v = group->cargo_output | std::views::take(i); std::ranges::find(v, cargo) != v.end()) {
696 GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
697 error->data = "duplicate output cargo";
698 return;
699 }
700 group->cargo_output[i] = cargo;
701 group->add_output[i] = buf.ReadByte();
702 }
703 group->again = buf.ReadByte();
704 } else {
705 NOT_REACHED();
706 }
707 break;
708 }
709
710 /* Loading of Tile Layout and Production Callback groups would happen here */
711 default: GrfMsg(1, "NewSpriteGroup: Unsupported feature 0x{:02X}, skipping", feature);
712 }
713 }
714 }
715
716 _cur_gps.spritegroups[setid] = act_group;
717}
718
719template <> void GrfActionHandler<0x02>::FileScan(ByteReader &) { }
721template <> void GrfActionHandler<0x02>::LabelScan(ByteReader &) { }
722template <> void GrfActionHandler<0x02>::Init(ByteReader &) { }
723template <> void GrfActionHandler<0x02>::Reserve(ByteReader &) { }
724template <> void GrfActionHandler<0x02>::Activation(ByteReader &buf) { NewSpriteGroup(buf); }
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr bool HasExactlyOneBit(T value)
Test whether value has exactly 1 bit set.
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:23
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:106
Class to read from a NewGRF file.
uint16_t ReadWord()
Read a single Word (16 bits).
uint32_t ReadVarSize(uint8_t size)
Read a value of the given number of bytes.
uint8_t ReadByte()
Read a single byte (8 bits).
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
static const int INDUSTRY_ORIGINAL_NUM_INPUTS
Original number of accepted cargo types.
static const int INDUSTRY_ORIGINAL_NUM_OUTPUTS
Original number of produced cargo types.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
GRFError * DisableGrf(StringID message, GRFConfig *config)
Disable a GRF.
Definition newgrf.cpp:133
GrfSpecFeature
Definition newgrf.h:69
constexpr uint16_t GROUPID_CALLBACK_FAILED
Explicit "failure" result.
constexpr uint16_t GROUPID_CALCULATED_RESULT
Return calculated result from VarAction2.
TileLayoutFlags ReadSpriteLayoutSprite(ByteReader &buf, bool read_flags, bool invert_action1_flag, bool use_cur_spritesets, GrfSpecFeature feature, PalSpriteID *grf_sprite, uint16_t *max_sprite_offset, uint16_t *max_palette_offset)
Read a sprite and a palette from the GRF and convert them into a format suitable to OpenTTD.
static void ReadSpriteLayoutRegisters(ByteReader &buf, TileLayoutFlags flags, bool is_parent, NewGRFSpriteLayout *dts, uint index)
Preprocess the TileLayoutFlags and read register modifiers from the GRF.
bool ReadSpriteLayout(ByteReader &buf, uint num_building_sprites, bool use_cur_spritesets, GrfSpecFeature feature, bool allow_var10, bool no_z_position, NewGRFSpriteLayout *dts)
Read a spritelayout from the GRF.
void MapSpriteMappingRecolour(PalSpriteID *grf_sprite)
Map the colour modifiers of TTDPatch to those that Open is using.
static std::vector< CachedCallback > _cached_callback_groups
Sorted list of cached callback result spritegroups.
static const SpriteGroup * CreateGroupFromGroupID(GrfSpecFeature feature, uint8_t setid, uint8_t type, uint16_t spriteid)
Helper function to either create a callback or a result sprite group.
NewGRF buffer reader definition.
CargoType GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoType.
TileLayoutFlags
Flags to enable register usage in sprite layouts.
@ TLF_BB_Z_OFFSET
Add signed offset to bounding box Z positions from register TileLayoutRegisters::delta....
@ TLF_CUSTOM_PALETTE
Palette is from Action 1 (moved to SPRITE_MODIFIER_CUSTOM_SPRITE in palette during loading).
@ TLF_SPRITE
Add signed offset to sprite from register TileLayoutRegisters::sprite.
@ TLF_CHILD_X_OFFSET
Add signed offset to child sprite X positions from register TileLayoutRegisters::delta....
@ TLF_DRAWING_FLAGS
Flags which are still required after loading the GRF.
@ TLF_DODRAW
Only draw sprite if value of register TileLayoutRegisters::dodraw is non-zero.
@ TLF_PALETTE_REG_FLAGS
Flags which require resolving the action-1-2-3 chain for the palette, even if it is no action-1 palet...
@ TLF_NON_GROUND_FLAGS
Flags which do not work for the (first) ground sprite.
@ TLF_BB_XY_OFFSET
Add signed offset to bounding box X and Y positions from register TileLayoutRegisters::delta....
@ TLF_SPRITE_REG_FLAGS
Flags which require resolving the action-1-2-3 chain for the sprite, even if it is no action-1 sprite...
@ TLF_PALETTE_VAR10
Resolve palette with a specific value in variable 10.
@ TLF_SPRITE_VAR10
Resolve sprite with a specific value in variable 10.
@ TLF_KNOWN_FLAGS
Known flags. Any unknown set flag will disable the GRF.
@ TLF_PALETTE
Add signed offset to palette from register TileLayoutRegisters::palette.
@ TLF_CHILD_Y_OFFSET
Add signed offset to child sprite Y positions from register TileLayoutRegisters::delta....
static const uint TLR_MAX_VAR10
Maximum value for var 10.
NewGRF internal processing state.
static constexpr uint MAX_SPRITEGROUP
Maximum GRF-local ID for a spritegroup.
DeterministicSpriteGroupAdjustOperation
@ DSGA_OP_ADD
a + b
@ VSG_SCOPE_SELF
Resolved object itself.
@ VSG_SCOPE_PARENT
Related object of the resolved one.
@ VSG_SCOPE_RELATIVE
Relative position (vehicles only)
static constexpr uint8_t SPRITE_MODIFIER_OPAQUE
Set when a sprite must not ever be displayed transparently.
Definition sprites.h:1550
static constexpr uint8_t PALETTE_MODIFIER_TRANSPARENT
when a sprite is to be displayed transparently, this bit needs to be set.
Definition sprites.h:1551
static constexpr uint8_t SPRITE_MODIFIER_CUSTOM_SPRITE
these masks change the colours of the palette for a sprite.
Definition sprites.h:1549
static constexpr uint8_t SPRITE_WIDTH
number of bits for the sprite number
Definition sprites.h:1539
static constexpr uint8_t PALETTE_MODIFIER_COLOUR
this bit is set when a recolouring process is in action
Definition sprites.h:1552
uint8_t parameter
Used for variables between 0x60 and 0x7F inclusive.
A tile child sprite and palette to draw for stations etc, with 3D bounding box.
Definition sprite.h:22
int8_t delta_z
0x80 identifies child sprites
Definition sprite.h:25
bool IsParentSprite() const
Check whether this is a parent sprite with a boundingbox.
Definition sprite.h:32
PalSpriteID ground
Palette and sprite for the ground.
Definition sprite.h:44
Information about why GRF had problems during initialisation.
std::string data
Additional data for message and custom_message.
GRF action handler.
GRFFile * grffile
Currently processed GRF file.
bool HasValidSpriteSets(GrfSpecFeature feature) const
Check whether there are any valid spritesets for a feature.
uint32_t nfo_line
Currently processed pseudo sprite number in the GRF.
SpriteID GetSprite(GrfSpecFeature feature, uint set) const
Returns the first sprite of a spriteset.
SpriteID spriteid
First available SpriteID for loading realsprites.
bool IsValidSpriteSet(GrfSpecFeature feature, uint set) const
Check whether a specific set is defined.
uint GetNumEnts(GrfSpecFeature feature, uint set) const
Returns the number of sprites in a spriteset.
int skip_sprites
Number of pseudo sprites to skip before processing the next one. (-1 to skip to end of file)
std::array< uint16_t, INDUSTRY_NUM_OUTPUTS > add_output
Add this much output cargo when successful (unsigned, is indirect in cb version 1+)
std::array< CargoType, INDUSTRY_NUM_OUTPUTS > cargo_output
Which output cargoes to add to (only cb version 2)
std::array< CargoType, INDUSTRY_NUM_INPUTS > cargo_input
Which input cargoes to take from (only cb version 2)
std::array< int16_t, INDUSTRY_NUM_INPUTS > subtract_input
Take this much of the input cargo (can be negative, is indirect in cb version 1+)
uint8_t num_input
How many subtract_input values are valid.
uint8_t version
Production callback version used, or 0xFF if marked invalid.
uint8_t num_output
How many add_output values are valid.
NewGRF supplied spritelayout.
void Allocate(uint num_sprites)
Allocate a spritelayout for num_sprites building sprites.
uint consistent_max_offset
Number of sprites in all referenced spritesets.
void AllocateRegisters()
Allocate memory for register modifiers.
Combination of a palette sprite and a 'real' sprite.
Definition gfx_type.h:22
SpriteID sprite
The 'real' sprite.
Definition gfx_type.h:23
PaletteID pal
The palette (use PAL_NONE) if not needed)
Definition gfx_type.h:24
static Titem * Get(auto index)
Returns Titem with given index.
Tindex index
Index of this pool item.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
uint8_t lowest_randbit
Look for this in the per-object randomized bitmask:
VarSpriteGroupScope var_scope
Take this object:
std::vector< const SpriteGroup * > groups
Take the group with appropriate index:
RandomizedSpriteGroupCompareMode cmp_mode
Check for these triggers:
std::vector< const SpriteGroup * > loaded
List of loaded groups (can be SpriteIDs or Callback results)
std::vector< const SpriteGroup * > loading
List of loading groups (can be SpriteIDs or Callback results)
Additional modifiers for items in sprite layouts.
uint8_t parent[3]
Registers for signed offsets for the bounding box position of parent sprites.
TileLayoutFlags flags
Flags defining which members are valid and to be used.
uint8_t dodraw
Register deciding whether the sprite shall be drawn at all. Non-zero means drawing.
uint16_t max_sprite_offset
Maximum offset to add to the sprite. (limited by size of the spriteset)
uint8_t palette
Register specifying a signed offset for the palette.
uint8_t sprite_var10
Value for variable 10 when resolving the sprite.
uint16_t max_palette_offset
Maximum offset to add to the palette. (limited by size of the spriteset)
uint8_t palette_var10
Value for variable 10 when resolving the palette.
uint8_t child[2]
Registers for signed offsets for the position of child sprites.
uint8_t sprite
Register specifying a signed offset for the sprite.
Action 2 sprite layout for houses, industry tiles, objects and airport tiles.