55 #include "table/strings.h"
72 const std::vector<GRFFile *> &GetAllGRFFiles()
121 this->skip_sprites = 0;
123 for (uint i = 0; i < GSF_END; i++) {
124 this->spritesets[i].clear();
127 memset(this->spritegroups, 0,
sizeof(this->spritegroups));
140 assert(feature < GSF_END);
141 for (uint i = 0; i < numsets; i++) {
142 SpriteSet &set = this->spritesets[feature][first_set + i];
143 set.
sprite = first_sprite + i * numents;
156 assert(feature < GSF_END);
157 return !this->spritesets[feature].empty();
169 assert(feature < GSF_END);
170 return this->spritesets[feature].find(set) != this->spritesets[feature].end();
182 return this->spritesets[feature].find(set)->second.sprite;
194 return this->spritesets[feature].find(set)->second.num_sprites;
207 template <VehicleType T>
210 return image_index == 0xFD || IsValidImageIndex<T>(image_index);
222 ByteReader(uint8_t *data, uint8_t *end) : data(data), end(end) { }
224 inline uint8_t *ReadBytes(
size_t size)
226 if (data + size >= end) {
237 inline uint8_t ReadByte()
239 if (data < end)
return *(data)++;
245 uint16_t val = ReadByte();
246 return val | (ReadByte() << 8);
249 uint16_t ReadExtendedByte()
251 uint16_t val = ReadByte();
252 return val == 0xFF ? ReadWord() : val;
257 uint32_t val = ReadWord();
258 return val | (ReadWord() << 16);
264 return this->ReadDWord();
267 uint32_t ReadVarSize(uint8_t size)
270 case 1:
return ReadByte();
271 case 2:
return ReadWord();
272 case 4:
return ReadDWord();
279 std::string_view ReadString()
281 char *
string =
reinterpret_cast<char *
>(data);
282 size_t string_length =
ttd_strnlen(
string, Remaining());
285 Skip(std::min(string_length + 1, Remaining()));
287 return std::string_view(
string, string_length);
290 inline size_t Remaining()
const
295 inline bool HasData(
size_t count = 1)
const
297 return data + count <= end;
300 inline void Skip(
size_t len)
309 typedef void (*SpecialSpriteHandler)(
ByteReader &buf);
325 RailTypeLabel railtypelabel;
326 uint8_t roadtramtype;
341 }
else if (this->refittability ==
UNSET) {
342 this->refittability =
EMPTY;
347 static std::vector<GRFTempEngineData>
_gted;
362 GRFLocation(uint32_t grfid, uint32_t nfoline) : grfid(grfid), nfoline(nfoline) { }
366 return this->grfid < other.grfid || (this->grfid == other.grfid && this->nfoline < other.nfoline);
371 return this->grfid == other.grfid && this->nfoline == other.nfoline;
375 static std::map<GRFLocation, std::pair<SpriteID, uint16_t>> _grm_sprites;
376 typedef std::map<GRFLocation, std::vector<uint8_t>> GRFLineToSpriteOverride;
377 static GRFLineToSpriteOverride _grf_line_to_action6_sprite_override;
389 void GrfMsgI(
int severity,
const std::string &msg)
402 if (file->grfid == grfid)
return file;
415 if (file->filename == filename)
return file;
435 if (config !=
nullptr) {
446 if (message == STR_NULL)
return nullptr;
448 config->error = {STR_NEWGRF_ERROR_MSG_FATAL, message};
450 return &config->error.value();
498 static const StringID units_volume[] = {
499 STR_ITEMS, STR_PASSENGERS, STR_TONS, STR_BAGS,
500 STR_LITERS, STR_ITEMS, STR_CRATES, STR_TONS,
501 STR_TONS, STR_TONS, STR_TONS, STR_BAGS,
502 STR_TONS, STR_TONS, STR_TONS, STR_BAGS,
503 STR_TONS, STR_TONS, STR_BAGS, STR_LITERS,
504 STR_TONS, STR_LITERS, STR_TONS, STR_ITEMS,
505 STR_BAGS, STR_LITERS, STR_TONS, STR_ITEMS,
506 STR_TONS, STR_ITEMS, STR_LITERS, STR_ITEMS
512 #define TEXTID_TO_STRINGID(begin, end, stringid, stringend) \
513 static_assert(stringend - stringid == end - begin); \
514 if (str >= begin && str <= end) return str + (stringid - begin)
517 TEXTID_TO_STRINGID(0x000E, 0x002D, STR_CARGO_PLURAL_NOTHING, STR_CARGO_PLURAL_FIZZY_DRINKS);
518 TEXTID_TO_STRINGID(0x002E, 0x004D, STR_CARGO_SINGULAR_NOTHING, STR_CARGO_SINGULAR_FIZZY_DRINK);
519 if (str >= 0x004E && str <= 0x006D)
return units_volume[str - 0x004E];
520 TEXTID_TO_STRINGID(0x006E, 0x008D, STR_QUANTITY_NOTHING, STR_QUANTITY_FIZZY_DRINKS);
521 TEXTID_TO_STRINGID(0x008E, 0x00AD, STR_ABBREV_NOTHING, STR_ABBREV_FIZZY_DRINKS);
522 TEXTID_TO_STRINGID(0x00D1, 0x00E0, STR_COLOUR_DARK_BLUE, STR_COLOUR_WHITE);
527 TEXTID_TO_STRINGID(0x200F, 0x201F, STR_TOWN_BUILDING_NAME_TALL_OFFICE_BLOCK_1, STR_TOWN_BUILDING_NAME_OLD_HOUSES_1);
528 TEXTID_TO_STRINGID(0x2036, 0x2041, STR_TOWN_BUILDING_NAME_COTTAGES_1, STR_TOWN_BUILDING_NAME_SHOPPING_MALL_1);
529 TEXTID_TO_STRINGID(0x2059, 0x205C, STR_TOWN_BUILDING_NAME_IGLOO_1, STR_TOWN_BUILDING_NAME_PIGGY_BANK_1);
532 TEXTID_TO_STRINGID(0x4802, 0x4826, STR_INDUSTRY_NAME_COAL_MINE, STR_INDUSTRY_NAME_SUGAR_MINE);
533 TEXTID_TO_STRINGID(0x482D, 0x482E, STR_NEWS_INDUSTRY_CONSTRUCTION, STR_NEWS_INDUSTRY_PLANTED);
534 TEXTID_TO_STRINGID(0x4832, 0x4834, STR_NEWS_INDUSTRY_CLOSURE_GENERAL, STR_NEWS_INDUSTRY_CLOSURE_LACK_OF_TREES);
535 TEXTID_TO_STRINGID(0x4835, 0x4838, STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_GENERAL, STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_FARM);
536 TEXTID_TO_STRINGID(0x4839, 0x483A, STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_GENERAL, STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_FARM);
539 case 0x4830:
return STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY;
540 case 0x4831:
return STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED;
541 case 0x483B:
return STR_ERROR_CAN_ONLY_BE_POSITIONED;
543 #undef TEXTID_TO_STRINGID
545 if (str == STR_NULL)
return STR_EMPTY;
547 Debug(grf, 0,
"Unknown StringID 0x{:04X} remapped to STR_EMPTY. Please open a Feature Request if you need it", str);
586 static std::map<uint32_t, uint32_t> _grf_id_overrides;
595 if (target_grfid == 0) {
596 _grf_id_overrides.erase(source_grfid);
597 GrfMsg(5,
"SetNewGRFOverride: Removed override of 0x{:X}",
BSWAP32(source_grfid));
599 _grf_id_overrides[source_grfid] = target_grfid;
600 GrfMsg(5,
"SetNewGRFOverride: Added override of 0x{:X} to 0x{:X}",
BSWAP32(source_grfid),
BSWAP32(target_grfid));
616 uint32_t scope_grfid = INVALID_GRFID;
619 scope_grfid = file->grfid;
620 if (
auto it = _grf_id_overrides.find(file->grfid); it != std::end(_grf_id_overrides)) {
621 scope_grfid = it->second;
623 if (grf_match ==
nullptr) {
624 GrfMsg(5,
"Tried mapping from GRFID {:x} to {:x} but target is not loaded",
BSWAP32(file->grfid),
BSWAP32(scope_grfid));
626 GrfMsg(5,
"Mapping from GRFID {:x} to {:x}",
BSWAP32(file->grfid),
BSWAP32(scope_grfid));
631 EngineID engine = _engine_mngr.
GetID(type, internal_id, scope_grfid);
640 EngineID engine = _engine_mngr.
GetID(type, internal_id, INVALID_GRFID);
646 GrfMsg(5,
"Replaced engine at index {} for GRFID {:x}, type {}, index {}", e->
index,
BSWAP32(file->grfid), type, internal_id);
650 if (!static_access) {
652 eid->
grfid = scope_grfid;
658 if (static_access)
return nullptr;
661 GrfMsg(0,
"Can't allocate any more engines");
672 assert(_engine_mngr.size() == e->
index);
673 _engine_mngr.push_back({
688 GrfMsg(5,
"Created new engine at index {} for GRFID {:x}, type {}, index {}", e->
index,
BSWAP32(file->grfid), type, internal_id);
705 uint32_t scope_grfid = INVALID_GRFID;
707 scope_grfid = file->grfid;
708 if (
auto it = _grf_id_overrides.find(file->grfid); it != std::end(_grf_id_overrides)) {
709 scope_grfid = it->second;
713 return _engine_mngr.
GetID(type, internal_id, scope_grfid);
753 grf_sprite->
sprite = buf.ReadWord();
754 grf_sprite->
pal = buf.ReadWord();
759 bool custom_sprite =
HasBit(grf_sprite->
pal, 15) != invert_action1_flag;
763 uint index =
GB(grf_sprite->
sprite, 0, 14);
765 GrfMsg(1,
"ReadSpriteLayoutSprite: Spritelayout uses undefined custom spriteset {}", index);
766 grf_sprite->
sprite = SPR_IMG_QUERY;
767 grf_sprite->
pal = PAL_NONE;
770 if (max_sprite_offset !=
nullptr) *max_sprite_offset = use_cur_spritesets ? _cur.
GetNumEnts(feature, index) : UINT16_MAX;
775 GrfMsg(1,
"ReadSpriteLayoutSprite: Spritelayout specifies var10 value for non-action-1 sprite");
776 DisableGrf(STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT);
782 uint index =
GB(grf_sprite->
pal, 0, 14);
784 GrfMsg(1,
"ReadSpriteLayoutSprite: Spritelayout uses undefined custom spriteset {} for 'palette'", index);
785 grf_sprite->
pal = PAL_NONE;
788 if (max_palette_offset !=
nullptr) *max_palette_offset = use_cur_spritesets ? _cur.
GetNumEnts(feature, index) : UINT16_MAX;
793 GrfMsg(1,
"ReadSpriteLayoutRegisters: Spritelayout specifies var10 value for non-action-1 palette");
794 DisableGrf(STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT);
823 regs.delta.
parent[0] = buf.ReadByte();
824 regs.delta.
parent[1] = buf.ReadByte();
835 GrfMsg(1,
"ReadSpriteLayoutRegisters: Spritelayout specifies var10 ({}) exceeding the maximal allowed value {}", regs.
sprite_var10,
TLR_MAX_VAR10);
836 DisableGrf(STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT);
844 GrfMsg(1,
"ReadSpriteLayoutRegisters: Spritelayout specifies var10 ({}) exceeding the maximal allowed value {}", regs.
palette_var10,
TLR_MAX_VAR10);
845 DisableGrf(STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT);
864 bool has_flags =
HasBit(num_building_sprites, 6);
865 ClrBit(num_building_sprites, 6);
868 dts->
Allocate(num_building_sprites);
870 std::vector<uint16_t> max_sprite_offset(num_building_sprites + 1, 0);
871 std::vector<uint16_t> max_palette_offset(num_building_sprites + 1, 0);
878 GrfMsg(1,
"ReadSpriteLayout: Spritelayout uses invalid flag 0x{:X} for ground sprite", flags & ~(valid_flags & ~
TLF_NON_GROUND_FLAGS));
879 DisableGrf(STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT);
886 for (uint i = 0; i < num_building_sprites; i++) {
889 flags =
ReadSpriteLayoutSprite(buf, has_flags,
false, use_cur_spritesets, feature, &seq->image, max_sprite_offset.data() + i + 1, max_palette_offset.data() + i + 1);
892 if (flags & ~valid_flags) {
893 GrfMsg(1,
"ReadSpriteLayout: Spritelayout uses unknown flag 0x{:X}", flags & ~valid_flags);
894 DisableGrf(STR_NEWGRF_ERROR_INVALID_SPRITE_LAYOUT);
899 seq->delta_y = buf.ReadByte();
901 if (!no_z_position) seq->
delta_z = buf.ReadByte();
904 seq->size_x = buf.ReadByte();
905 seq->size_y = buf.ReadByte();
906 seq->size_z = buf.ReadByte();
914 bool is_consistent =
true;
916 for (uint i = 0; i < num_building_sprites + 1; i++) {
917 if (max_sprite_offset[i] > 0) {
921 is_consistent =
false;
925 if (max_palette_offset[i] > 0) {
929 is_consistent =
false;
938 if (!is_consistent || dts->registers !=
nullptr) {
942 for (uint i = 0; i < num_building_sprites + 1; i++) {
957 CargoTypes result = 0;
975 if (base_pointer == 0) {
976 *index = INVALID_PRICE;
980 static const uint32_t start = 0x4B34;
981 static const uint32_t size = 6;
983 if (base_pointer < start || (base_pointer - start) % size != 0 || (base_pointer - start) / size >= PR_END) {
984 GrfMsg(1,
"{}: Unsupported running cost base 0x{:04X}, ignoring", error_location, base_pointer);
988 *index = (
Price)((base_pointer - start) / size);
1017 ei->decay_speed = buf.ReadByte();
1034 ei->load_amount = buf.ReadByte();
1056 for (
int i = 0; i < numinfo; i++) {
1065 uint8_t tracktype = buf.ReadByte();
1067 if (tracktype < _cur.grffile->railtype_list.size()) {
1072 switch (tracktype) {
1073 case 0:
_gted[e->
index].railtypelabel = rvi->
engclass >= 2 ? RAILTYPE_LABEL_ELECTRIC : RAILTYPE_LABEL_RAIL;
break;
1074 case 1:
_gted[e->
index].railtypelabel = RAILTYPE_LABEL_MONO;
break;
1075 case 2:
_gted[e->
index].railtypelabel = RAILTYPE_LABEL_MAGLEV;
break;
1077 GrfMsg(1,
"RailVehicleChangeInfo: Invalid track type {} specified, ignoring", tracktype);
1090 uint16_t speed = buf.ReadWord();
1091 if (speed == 0xFFFF) speed = 0;
1098 rvi->
power = buf.ReadWord();
1101 if (rvi->
power != 0) {
1119 uint8_t spriteid = buf.ReadByte();
1120 uint8_t orig_spriteid = spriteid;
1124 if (spriteid < 0xFD) spriteid >>= 1;
1126 if (IsValidNewGRFImageIndex<VEH_TRAIN>(spriteid)) {
1127 rvi->image_index = spriteid;
1129 GrfMsg(1,
"RailVehicleChangeInfo: Invalid Sprite {} specified, ignoring", orig_spriteid);
1130 rvi->image_index = 0;
1136 uint8_t dual = buf.ReadByte();
1141 rvi->railveh_type = rvi->
power == 0 ?
1153 uint8_t ctype = buf.ReadByte();
1155 if (ctype == 0xFF) {
1157 ei->cargo_type = INVALID_CARGO;
1158 }
else if (_cur.
grffile->grf_version >= 8) {
1163 ei->cargo_type = ctype;
1165 ei->cargo_type = INVALID_CARGO;
1166 GrfMsg(2,
"RailVehicleChangeInfo: Invalid cargo type {}, using first refittable", ctype);
1173 SB(rvi->
weight, 0, 8, buf.ReadByte());
1181 GrfMsg(2,
"RailVehicleChangeInfo: Property 0x18 'AI rank' not used by NoAI, ignored.");
1193 uint8_t traction = buf.ReadByte();
1196 if (traction <= 0x07) {
1198 }
else if (traction <= 0x27) {
1200 }
else if (traction <= 0x31) {
1202 }
else if (traction <= 0x37) {
1204 }
else if (traction <= 0x41) {
1230 ei->refit_cost = buf.ReadByte();
1234 uint32_t mask = buf.ReadDWord();
1235 _gted[e->
index].UpdateRefittability(mask != 0);
1272 uint8_t weight = buf.ReadByte();
1275 GrfMsg(2,
"RailVehicleChangeInfo: Nonsensical weight of {} tons, ignoring", weight << 8);
1296 _gted[e->
index].cargo_allowed = buf.ReadWord();
1302 _gted[e->
index].cargo_disallowed = buf.ReadWord();
1316 uint8_t count = buf.ReadByte();
1317 _gted[e->
index].UpdateRefittability(prop == 0x2C && count != 0);
1319 CargoTypes &ctt = prop == 0x2C ?
_gted[e->
index].ctt_include_mask :
_gted[e->
index].ctt_exclude_mask;
1365 for (
int i = 0; i < numinfo; i++) {
1376 _gted[e->
index].roadtramtype = buf.ReadByte() + 1;
1384 rvi->running_cost = buf.ReadByte();
1392 uint8_t spriteid = buf.ReadByte();
1393 uint8_t orig_spriteid = spriteid;
1396 if (spriteid == 0xFF) spriteid = 0xFD;
1398 if (spriteid < 0xFD) spriteid >>= 1;
1400 if (IsValidNewGRFImageIndex<VEH_ROAD>(spriteid)) {
1401 rvi->image_index = spriteid;
1403 GrfMsg(1,
"RoadVehicleChangeInfo: Invalid Sprite {} specified, ignoring", orig_spriteid);
1404 rvi->image_index = 0;
1410 rvi->capacity = buf.ReadByte();
1415 uint8_t ctype = buf.ReadByte();
1417 if (ctype == 0xFF) {
1419 ei->cargo_type = INVALID_CARGO;
1420 }
else if (_cur.
grffile->grf_version >= 8) {
1425 ei->cargo_type = ctype;
1427 ei->cargo_type = INVALID_CARGO;
1428 GrfMsg(2,
"RailVehicleChangeInfo: Invalid cargo type {}, using first refittable", ctype);
1435 rvi->cost_factor = buf.ReadByte();
1443 rvi->
power = buf.ReadByte();
1447 rvi->
weight = buf.ReadByte();
1451 _gted[e->
index].rv_max_speed = buf.ReadByte();
1455 uint32_t mask = buf.ReadDWord();
1456 _gted[e->
index].UpdateRefittability(mask != 0);
1475 ei->refit_cost = buf.ReadByte();
1488 _gted[e->
index].cargo_allowed = buf.ReadWord();
1494 _gted[e->
index].cargo_disallowed = buf.ReadWord();
1526 uint8_t count = buf.ReadByte();
1527 _gted[e->
index].UpdateRefittability(prop == 0x24 && count != 0);
1529 CargoTypes &ctt = prop == 0x24 ?
_gted[e->
index].ctt_include_mask :
_gted[e->
index].ctt_exclude_mask;
1571 for (
int i = 0; i < numinfo; i++) {
1580 uint8_t spriteid = buf.ReadByte();
1581 uint8_t orig_spriteid = spriteid;
1584 if (spriteid == 0xFF) spriteid = 0xFD;
1586 if (spriteid < 0xFD) spriteid >>= 1;
1588 if (IsValidNewGRFImageIndex<VEH_SHIP>(spriteid)) {
1589 svi->image_index = spriteid;
1591 GrfMsg(1,
"ShipVehicleChangeInfo: Invalid Sprite {} specified, ignoring", orig_spriteid);
1592 svi->image_index = 0;
1602 svi->cost_factor = buf.ReadByte();
1611 uint8_t ctype = buf.ReadByte();
1613 if (ctype == 0xFF) {
1615 ei->cargo_type = INVALID_CARGO;
1616 }
else if (_cur.
grffile->grf_version >= 8) {
1621 ei->cargo_type = ctype;
1623 ei->cargo_type = INVALID_CARGO;
1624 GrfMsg(2,
"ShipVehicleChangeInfo: Invalid cargo type {}, using first refittable", ctype);
1631 svi->capacity = buf.ReadWord();
1635 svi->running_cost = buf.ReadByte();
1643 uint32_t mask = buf.ReadDWord();
1644 _gted[e->
index].UpdateRefittability(mask != 0);
1655 ei->refit_cost = buf.ReadByte();
1676 _gted[e->
index].cargo_allowed = buf.ReadWord();
1682 _gted[e->
index].cargo_disallowed = buf.ReadWord();
1710 uint8_t count = buf.ReadByte();
1711 _gted[e->
index].UpdateRefittability(prop == 0x1E && count != 0);
1713 CargoTypes &ctt = prop == 0x1E ?
_gted[e->
index].ctt_include_mask :
_gted[e->
index].ctt_exclude_mask;
1739 svi->
acceleration = std::max<uint8_t>(1, buf.ReadByte());
1763 for (
int i = 0; i < numinfo; i++) {
1772 uint8_t spriteid = buf.ReadByte();
1773 uint8_t orig_spriteid = spriteid;
1776 if (spriteid == 0xFF) spriteid = 0xFD;
1778 if (spriteid < 0xFD) spriteid >>= 1;
1780 if (IsValidNewGRFImageIndex<VEH_AIRCRAFT>(spriteid)) {
1781 avi->image_index = spriteid;
1783 GrfMsg(1,
"AircraftVehicleChangeInfo: Invalid Sprite {} specified, ignoring", orig_spriteid);
1784 avi->image_index = 0;
1790 if (buf.ReadByte() == 0) {
1802 avi->cost_factor = buf.ReadByte();
1806 avi->
max_speed = (buf.ReadByte() * 128) / 10;
1810 avi->acceleration = buf.ReadByte();
1814 avi->running_cost = buf.ReadByte();
1830 uint32_t mask = buf.ReadDWord();
1831 _gted[e->
index].UpdateRefittability(mask != 0);
1842 ei->refit_cost = buf.ReadByte();
1855 _gted[e->
index].cargo_allowed = buf.ReadWord();
1861 _gted[e->
index].cargo_disallowed = buf.ReadWord();
1879 uint8_t count = buf.ReadByte();
1880 _gted[e->
index].UpdateRefittability(prop == 0x1D && count != 0);
1882 CargoTypes &ctt = prop == 0x1D ?
_gted[e->
index].ctt_include_mask :
_gted[e->
index].ctt_exclude_mask;
1929 GrfMsg(1,
"StationChangeInfo: Station {} is invalid, max {}, ignoring", stid + numinfo,
NUM_STATIONS_PER_GRF);
1934 if (_cur.
grffile->stations.size() < stid + numinfo) _cur.
grffile->stations.resize(stid + numinfo);
1936 for (
int i = 0; i < numinfo; i++) {
1937 auto &statspec = _cur.
grffile->stations[stid + i];
1940 if (statspec ==
nullptr && prop != 0x08) {
1941 GrfMsg(2,
"StationChangeInfo: Attempt to modify undefined station {}, ignoring", stid + i);
1948 if (statspec ==
nullptr) {
1949 statspec = std::make_unique<StationSpec>();
1953 uint32_t classid = buf.ReadDWord();
1959 uint16_t tiles = buf.ReadExtendedByte();
1960 statspec->renderdata.clear();
1961 statspec->renderdata.reserve(tiles);
1963 for (uint t = 0; t < tiles; t++) {
1967 if (buf.HasData(4) && buf.PeekDWord() == 0) {
1970 dts->
Clone(&_station_display_datas_rail[t % 8]);
1978 static std::vector<DrawTileSeqStruct> tmp_layout;
1985 dtss.
delta_x = buf.ReadByte();
1987 dtss.delta_y = buf.ReadByte();
1988 dtss.
delta_z = buf.ReadByte();
1989 dtss.size_x = buf.ReadByte();
1990 dtss.size_y = buf.ReadByte();
1991 dtss.size_z = buf.ReadByte();
1997 dts->
Clone(tmp_layout.data());
2001 if (statspec->renderdata.size() & 1) {
2002 GrfMsg(1,
"StationChangeInfo: Station {} defines an odd number of sprite layouts, dropping the last item", stid + i);
2003 statspec->renderdata.pop_back();
2009 uint16_t srcid = buf.ReadExtendedByte();
2010 const StationSpec *srcstatspec = srcid >= _cur.
grffile->stations.size() ? nullptr : _cur.
grffile->stations[srcid].get();
2012 if (srcstatspec ==
nullptr) {
2013 GrfMsg(1,
"StationChangeInfo: Station {} is not defined, cannot copy sprite layout to {}.", srcid, stid + i);
2017 statspec->renderdata.clear();
2018 statspec->renderdata.reserve(srcstatspec->
renderdata.size());
2020 for (
const auto &it : srcstatspec->
renderdata) {
2028 statspec->callback_mask = buf.ReadByte();
2032 statspec->disallowed_platforms = buf.ReadByte();
2036 statspec->disallowed_lengths = buf.ReadByte();
2040 while (buf.HasData()) {
2041 uint8_t length = buf.ReadByte();
2042 uint8_t number = buf.ReadByte();
2044 if (length == 0 || number == 0)
break;
2046 const uint8_t *buf_layout = buf.ReadBytes(length * number);
2050 layout.assign(buf_layout, buf_layout + length * number);
2053 for (
auto &tile : layout) {
2054 if ((tile & ~1U) != tile) {
2055 GrfMsg(1,
"StationChangeInfo: Invalid tile {} in layout {}x{}", tile, length, number);
2063 uint16_t srcid = buf.ReadExtendedByte();
2064 const StationSpec *srcstatspec = srcid >= _cur.
grffile->stations.size() ? nullptr : _cur.
grffile->stations[srcid].get();
2066 if (srcstatspec ==
nullptr) {
2067 GrfMsg(1,
"StationChangeInfo: Station {} is not defined, cannot copy tile layout to {}.", srcid, stid + i);
2071 statspec->layouts = srcstatspec->
layouts;
2076 statspec->cargo_threshold = buf.ReadWord();
2080 uint8_t pylons = buf.ReadByte();
2081 if (statspec->tileflags.size() < 8) statspec->tileflags.resize(8);
2082 for (
int j = 0; j < 8; ++j) {
2086 statspec->tileflags[j] &= ~
StationSpec::TileFlags::Pylons;
2093 if (_cur.
grffile->grf_version >= 7) {
2096 statspec->cargo_triggers = (CargoTypes)buf.ReadDWord();
2101 statspec->flags = buf.ReadByte();
2105 uint8_t wires = buf.ReadByte();
2106 if (statspec->tileflags.size() < 8) statspec->tileflags.resize(8);
2107 for (
int j = 0; j < 8; ++j) {
2111 statspec->tileflags[j] &= ~
StationSpec::TileFlags::NoWires;
2118 uint8_t blocked = buf.ReadByte();
2119 if (statspec->tileflags.size() < 8) statspec->tileflags.resize(8);
2120 for (
int j = 0; j < 8; ++j) {
2121 if (
HasBit(blocked, j)) {
2124 statspec->tileflags[j] &= ~
StationSpec::TileFlags::Blocked;
2131 statspec->animation.
frames = buf.ReadByte();
2132 statspec->animation.status = buf.ReadByte();
2136 statspec->animation.speed = buf.ReadByte();
2140 statspec->animation.triggers = buf.ReadWord();
2146 uint16_t tiles = buf.ReadExtendedByte();
2147 statspec->renderdata.clear();
2148 statspec->renderdata.reserve(tiles);
2150 for (uint t = 0; t < tiles; t++) {
2152 uint num_building_sprites = buf.ReadByte();
2158 if (statspec->renderdata.size() & 1) {
2159 GrfMsg(1,
"StationChangeInfo: Station {} defines an odd number of sprite layouts, dropping the last item", stid + i);
2160 statspec->renderdata.pop_back();
2177 AddStringForMapping(buf.ReadWord(), [statspec = statspec.get()](
StringID str) { StationClass::Get(statspec->class_index)->name = str; });
2181 uint16_t tiles = buf.ReadExtendedByte();
2183 statspec->tileflags.assign(flags, flags + tiles);
2208 if (
id + numinfo > CF_END) {
2209 GrfMsg(1,
"CanalChangeInfo: Canal feature 0x{:02X} is invalid, max {}, ignoring",
id + numinfo, CF_END);
2213 for (
int i = 0; i < numinfo; i++) {
2222 cp->
flags = buf.ReadByte();
2247 GrfMsg(1,
"BridgeChangeInfo: Bridge {} is invalid, max {}, ignoring", brid + numinfo,
MAX_BRIDGES);
2251 for (
int i = 0; i < numinfo; i++) {
2257 uint8_t year = buf.ReadByte();
2272 bridge->
price = buf.ReadByte();
2276 bridge->
speed = buf.ReadWord();
2277 if (bridge->
speed == 0) bridge->
speed = UINT16_MAX;
2281 uint8_t tableid = buf.ReadByte();
2282 uint8_t numtables = buf.ReadByte();
2289 for (; numtables-- != 0; tableid++) {
2291 GrfMsg(1,
"BridgeChangeInfo: Table {} >= 7, skipping", tableid);
2292 for (uint8_t sprite = 0; sprite < 32; sprite++) buf.ReadDWord();
2297 bridge->
sprite_table[tableid] = MallocT<PalSpriteID>(32);
2300 for (uint8_t sprite = 0; sprite < 32; sprite++) {
2314 bridge->
flags = buf.ReadByte();
2323 if (newone != STR_UNDEFINED) bridge->
material = newone;
2330 if (newone != STR_UNDEFINED) bridge->
transport_name[prop - 0x11] = newone;
2335 bridge->
price = buf.ReadWord();
2392 for (uint j = 0; j < 4; j++) buf.ReadByte();
2396 uint8_t count = buf.ReadByte();
2397 for (uint8_t j = 0; j < count; j++) buf.ReadByte();
2402 buf.Skip(buf.ReadByte() * 2);
2425 GrfMsg(1,
"TownHouseChangeInfo: Too many houses loaded ({}), max ({}). Ignoring.", hid + numinfo,
NUM_HOUSES_PER_GRF);
2430 if (_cur.
grffile->housespec.size() < hid + numinfo) _cur.
grffile->housespec.resize(hid + numinfo);
2432 for (
int i = 0; i < numinfo; i++) {
2433 auto &housespec = _cur.
grffile->housespec[hid + i];
2435 if (prop != 0x08 && housespec ==
nullptr) {
2438 if (cir > ret) ret = cir;
2444 uint8_t subs_id = buf.ReadByte();
2445 if (subs_id == 0xFF) {
2452 GrfMsg(2,
"TownHouseChangeInfo: Attempt to use new house {} as substitute house for {}. Ignoring.", subs_id, hid + i);
2457 if (housespec ==
nullptr) {
2459 housespec = std::make_unique<HouseSpec>(*
HouseSpec::Get(subs_id));
2461 housespec->enabled =
true;
2462 housespec->grf_prop.local_id = hid + i;
2463 housespec->grf_prop.subst_id = subs_id;
2464 housespec->grf_prop.grffile = _cur.
grffile;
2466 housespec->random_colour[0] = COLOUR_RED;
2467 housespec->random_colour[1] = COLOUR_BLUE;
2468 housespec->random_colour[2] = COLOUR_ORANGE;
2469 housespec->random_colour[3] = COLOUR_GREEN;
2472 housespec->building_flags &= ~(BUILDING_IS_CHURCH | BUILDING_IS_STADIUM);
2478 CargoID cid = housespec->accepts_cargo[2];
2479 if (!
IsValidCargoID(cid)) cid = GetCargoIDByLabel(housespec->accepts_cargo_label[2]);
2481 housespec->cargo_acceptance[2] = 0;
2488 housespec->building_flags = (BuildingFlags)buf.ReadByte();
2492 uint16_t years = buf.ReadWord();
2499 housespec->population = buf.ReadByte();
2503 housespec->mail_generation = buf.ReadByte();
2508 housespec->cargo_acceptance[prop - 0x0D] = buf.ReadByte();
2512 int8_t goods = buf.ReadByte();
2522 housespec->accepts_cargo[2] = cid;
2523 housespec->accepts_cargo_label[2] =
CT_INVALID;
2524 housespec->cargo_acceptance[2] =
abs(goods);
2529 housespec->remove_rating_decrease = buf.ReadWord();
2533 housespec->removal_cost = buf.ReadByte();
2541 housespec->building_availability = (
HouseZones)buf.ReadWord();
2545 housespec->callback_mask |= buf.ReadByte();
2549 uint8_t
override = buf.ReadByte();
2553 GrfMsg(2,
"TownHouseChangeInfo: Attempt to override new house {} with house id {}. Ignoring.",
override, hid + i);
2557 _house_mngr.
Add(hid + i, _cur.
grffile->grfid,
override);
2562 housespec->processing_time = std::min<uint8_t>(buf.ReadByte(), 63u);
2566 for (uint j = 0; j < 4; j++) housespec->random_colour[j] =
static_cast<Colours
>(
GB(buf.ReadByte(), 0, 4));
2570 housespec->probability = buf.ReadByte();
2578 housespec->animation.frames = buf.ReadByte();
2579 housespec->animation.status =
GB(housespec->animation.frames, 7, 1);
2580 SB(housespec->animation.frames, 7, 1, 0);
2584 housespec->animation.speed =
Clamp(buf.ReadByte(), 2, 16);
2588 housespec->class_id = AllocateHouseClassID(buf.ReadByte(), _cur.
grffile->grfid);
2592 housespec->callback_mask |= (buf.ReadByte() << 8);
2596 uint32_t cargotypes = buf.ReadDWord();
2599 if (cargotypes == 0xFFFFFFFF)
break;
2603 uint8_t cargo_part =
GB(cargotypes, 8 * j, 8);
2608 housespec->cargo_acceptance[j] = 0;
2610 housespec->accepts_cargo[j] = cargo;
2612 housespec->accepts_cargo_label[j] =
CT_INVALID;
2618 housespec->minimum_life = buf.ReadByte();
2622 uint8_t count = buf.ReadByte();
2623 for (uint8_t j = 0; j < count; j++) {
2631 housespec->min_year = buf.ReadWord();
2635 housespec->max_year = buf.ReadWord();
2640 uint count = buf.ReadByte();
2641 if (count >
lengthof(housespec->accepts_cargo)) {
2649 for (uint i = 0; i <
lengthof(housespec->accepts_cargo); i++) {
2652 housespec->cargo_acceptance[i] = buf.ReadByte();
2654 housespec->accepts_cargo[i] = INVALID_CARGO;
2655 housespec->cargo_acceptance[i] = 0;
2657 housespec->accepts_cargo_label[i] =
CT_INVALID;
2681 if (grffile ==
nullptr)
return nullptr;
2684 if (it == std::end(grffile->
language_map))
return nullptr;
2698 template <
typename T>
2702 GrfMsg(1,
"LoadTranslationTable: {} translation table must start at zero", name);
2706 translation_table.clear();
2707 translation_table.reserve(numinfo);
2708 for (
int i = 0; i < numinfo; i++) {
2709 translation_table.push_back(T(
BSWAP32(buf.ReadDWord())));
2724 for (
int i = 0; i < 4; i++) output.push_back(reader.ReadByte());
2758 for (
int i = 0; i < numinfo; i++) {
2761 int factor = buf.ReadByte();
2762 uint price = gvid + i;
2764 if (price < PR_END) {
2767 GrfMsg(1,
"GlobalVarChangeInfo: Price {} out of range, ignoring", price);
2776 if ((newone != STR_UNDEFINED) && (curidx <
CURRENCY_END)) {
2785 uint32_t rate = buf.ReadDWord();
2793 GrfMsg(1,
"GlobalVarChangeInfo: Currency multipliers {} out of range, ignoring", curidx);
2800 uint16_t options = buf.ReadWord();
2809 GrfMsg(1,
"GlobalVarChangeInfo: Currency option {} out of range, ignoring", curidx);
2821 GrfMsg(1,
"GlobalVarChangeInfo: Currency symbol {} out of range, ignoring", curidx);
2833 GrfMsg(1,
"GlobalVarChangeInfo: Currency symbol {} out of range, ignoring", curidx);
2845 GrfMsg(1,
"GlobalVarChangeInfo: Euro intro date {} out of range, ignoring", curidx);
2852 GrfMsg(1,
"GlobalVarChangeInfo: The snowline can only be set once ({})", numinfo);
2854 GrfMsg(1,
"GlobalVarChangeInfo: Not enough entries set in the snowline table ({})", buf.Remaining());
2860 table[i][j] = buf.ReadByte();
2861 if (_cur.
grffile->grf_version >= 8) {
2864 if (table[i][j] >= 128) {
2886 uint curidx = gvid + i;
2888 if (lang ==
nullptr) {
2889 GrfMsg(1,
"GlobalVarChangeInfo: Language {} is not known, ignoring", curidx);
2894 while (buf.ReadByte() != 0) {
2902 uint plural_form = buf.ReadByte();
2903 if (plural_form >= LANGUAGE_MAX_PLURAL) {
2904 GrfMsg(1,
"GlobalVarChanceInfo: Plural form {} is out of range, ignoring", plural_form);
2911 uint8_t newgrf_id = buf.ReadByte();
2912 while (newgrf_id != 0) {
2913 std::string_view name = buf.ReadString();
2928 GrfMsg(1,
"GlobalVarChangeInfo: Gender name {} is not known, ignoring",
StrMakeValid(name));
2935 GrfMsg(1,
"GlobalVarChangeInfo: Case name {} is not known, ignoring",
StrMakeValid(name));
2940 newgrf_id = buf.ReadByte();
2976 for (
int i = 0; i < numinfo; i++) {
3000 uint32_t s = buf.ReadDWord();
3001 uint32_t t = buf.ReadDWord();
3008 while (buf.ReadByte() != 0) {
3036 GrfMsg(2,
"CargoChangeInfo: Cargo type {} out of range (max {})", cid + numinfo,
NUM_CARGO - 1);
3040 for (
int i = 0; i < numinfo; i++) {
3045 cs->
bitnum = buf.ReadByte();
3084 cs->
sprite = buf.ReadWord();
3088 cs->
weight = buf.ReadByte();
3092 cs->transit_periods[0] = buf.ReadByte();
3096 cs->transit_periods[1] = buf.ReadByte();
3104 cs->rating_colour = buf.ReadByte();
3108 cs->legend_colour = buf.ReadByte();
3125 uint8_t substitute_type = buf.ReadByte();
3127 switch (substitute_type) {
3134 GrfMsg(1,
"CargoChangeInfo: Unknown town growth substitute value {}, setting to none.", substitute_type);
3150 cs->
multiplier = std::max<uint16_t>(1u, buf.ReadWord());
3154 uint8_t substitute_type = buf.ReadByte();
3156 switch (substitute_type) {
3160 GrfMsg(1,
"CargoChangeInfo: Unknown town production substitute value {}, setting to none.", substitute_type);
3193 if (_cur.
grffile->sound_offset == 0) {
3194 GrfMsg(1,
"SoundEffectChangeInfo: No effects defined, skipping");
3199 GrfMsg(1,
"SoundEffectChangeInfo: Attempting to change undefined sound effect ({}), max ({}). Ignoring.", sid + numinfo,
ORIGINAL_SAMPLE_COUNT + _cur.
grffile->num_sounds);
3203 for (
int i = 0; i < numinfo; i++) {
3208 sound->volume =
Clamp(buf.ReadByte(), 0, SOUND_EFFECT_MAX_VOLUME);
3212 sound->priority = buf.ReadByte();
3216 SoundID orig_sound = buf.ReadByte();
3219 GrfMsg(1,
"SoundEffectChangeInfo: Original sound {} not defined (max {})", orig_sound,
ORIGINAL_SAMPLE_COUNT);
3221 SoundEntry *old_sound = GetSound(orig_sound);
3224 *old_sound = *sound;
3266 buf.Skip(buf.ReadByte() * 2);
3289 GrfMsg(1,
"IndustryTilesChangeInfo: Too many industry tiles loaded ({}), max ({}). Ignoring.", indtid + numinfo,
NUM_INDUSTRYTILES_PER_GRF);
3294 if (_cur.
grffile->indtspec.size() < indtid + numinfo) _cur.
grffile->indtspec.resize(indtid + numinfo);
3296 for (
int i = 0; i < numinfo; i++) {
3297 auto &tsp = _cur.
grffile->indtspec[indtid + i];
3299 if (prop != 0x08 && tsp ==
nullptr) {
3301 if (cir > ret) ret = cir;
3307 uint8_t subs_id = buf.ReadByte();
3310 GrfMsg(2,
"IndustryTilesChangeInfo: Attempt to use new industry tile {} as substitute industry tile for {}. Ignoring.", subs_id, indtid + i);
3315 if (tsp ==
nullptr) {
3316 tsp = std::make_unique<IndustryTileSpec>(_industry_tile_specs[subs_id]);
3318 tsp->enabled =
true;
3326 tsp->grf_prop.local_id = indtid + i;
3327 tsp->grf_prop.subst_id = subs_id;
3328 tsp->grf_prop.grffile = _cur.
grffile;
3335 uint8_t ovrid = buf.ReadByte();
3339 GrfMsg(2,
"IndustryTilesChangeInfo: Attempt to override new industry tile {} with industry tile id {}. Ignoring.", ovrid, indtid + i);
3343 _industile_mngr.
Add(indtid + i, _cur.
grffile->grfid, ovrid);
3350 uint16_t acctp = buf.ReadWord();
3352 tsp->acceptance[prop - 0x0A] =
Clamp(
GB(acctp, 8, 8), 0, 16);
3353 tsp->accepts_cargo_label[prop - 0x0A] =
CT_INVALID;
3358 tsp->slopes_refused = (
Slope)buf.ReadByte();
3362 tsp->callback_mask = buf.ReadByte();
3366 tsp->animation.frames = buf.ReadByte();
3367 tsp->animation.status = buf.ReadByte();
3371 tsp->animation.speed = buf.ReadByte();
3375 tsp->animation.triggers = buf.ReadByte();
3383 uint8_t num_cargoes = buf.ReadByte();
3384 if (num_cargoes > std::size(tsp->acceptance)) {
3389 for (uint i = 0; i < std::size(tsp->acceptance); i++) {
3390 if (i < num_cargoes) {
3393 tsp->acceptance[i] = (int8_t)buf.ReadByte();
3395 tsp->accepts_cargo[i] = INVALID_CARGO;
3396 tsp->acceptance[i] = 0;
3458 uint8_t num_table = buf.ReadByte();
3459 for (uint8_t j = 0; j < num_table; j++) {
3460 for (uint k = 0;; k++) {
3461 uint8_t x = buf.ReadByte();
3462 if (x == 0xFE && k == 0) {
3468 uint8_t y = buf.ReadByte();
3469 if (x == 0 && y == 0x80)
break;
3471 uint8_t gfx = buf.ReadByte();
3472 if (gfx == 0xFE) buf.ReadWord();
3486 buf.Skip(buf.ReadByte());
3490 int num_inputs = buf.ReadByte();
3491 int num_outputs = buf.ReadByte();
3492 buf.Skip(num_inputs * num_outputs * 2);
3510 const size_t size = layout.size();
3511 if (size == 0)
return false;
3513 for (
size_t i = 0; i < size - 1; i++) {
3514 for (
size_t j = i + 1; j < size; j++) {
3515 if (layout[i].ti.x == layout[j].ti.x &&
3516 layout[i].ti.y == layout[j].ti.y) {
3522 bool have_regular_tile =
false;
3523 for (
const auto &tilelayout : layout) {
3525 have_regular_tile =
true;
3530 return have_regular_tile;
3546 GrfMsg(1,
"IndustriesChangeInfo: Too many industries loaded ({}), max ({}). Ignoring.", indid + numinfo,
NUM_INDUSTRYTYPES_PER_GRF);
3551 if (_cur.
grffile->industryspec.size() < indid + numinfo) _cur.
grffile->industryspec.resize(indid + numinfo);
3553 for (
int i = 0; i < numinfo; i++) {
3554 auto &indsp = _cur.
grffile->industryspec[indid + i];
3556 if (prop != 0x08 && indsp ==
nullptr) {
3558 if (cir > ret) ret = cir;
3564 uint8_t subs_id = buf.ReadByte();
3565 if (subs_id == 0xFF) {
3568 _industry_specs[indid + i].
enabled =
false;
3572 GrfMsg(2,
"_industry_specs: Attempt to use new industry {} as substitute industry for {}. Ignoring.", subs_id, indid + i);
3579 if (indsp ==
nullptr) {
3580 indsp = std::make_unique<IndustrySpec>(_origin_industry_specs[subs_id]);
3582 indsp->enabled =
true;
3583 indsp->grf_prop.local_id = indid + i;
3584 indsp->grf_prop.subst_id = subs_id;
3585 indsp->grf_prop.grffile = _cur.
grffile;
3594 uint8_t ovrid = buf.ReadByte();
3598 GrfMsg(2,
"IndustriesChangeInfo: Attempt to override new industry {} with industry id {}. Ignoring.", ovrid, indid + i);
3601 indsp->grf_prop.override = ovrid;
3602 _industry_mngr.
Add(indid + i, _cur.
grffile->grfid, ovrid);
3607 uint8_t new_num_layouts = buf.ReadByte();
3608 uint32_t definition_size = buf.ReadDWord();
3609 uint32_t bytes_read = 0;
3610 std::vector<IndustryTileLayout> new_layouts;
3613 for (uint8_t j = 0; j < new_num_layouts; j++) {
3615 layout.reserve(new_num_layouts);
3617 for (uint k = 0;; k++) {
3618 if (bytes_read >= definition_size) {
3619 GrfMsg(3,
"IndustriesChangeInfo: Incorrect size for industry tile layout definition for industry {}.", indid);
3621 definition_size = UINT32_MAX;
3627 it.ti.
x = buf.ReadByte();
3630 if (it.ti.
x == 0xFE && k == 0) {
3632 IndustryType type = buf.ReadByte();
3633 uint8_t laynbr = buf.ReadByte();
3636 if (type >=
lengthof(_origin_industry_specs)) {
3637 GrfMsg(1,
"IndustriesChangeInfo: Invalid original industry number for layout import, industry {}", indid);
3641 if (laynbr >= _origin_industry_specs[type].layouts.size()) {
3642 GrfMsg(1,
"IndustriesChangeInfo: Invalid original industry layout index for layout import, industry {}", indid);
3646 layout = _origin_industry_specs[type].layouts[laynbr];
3650 it.ti.
y = buf.ReadByte();
3653 if (it.ti.
x == 0 && it.ti.
y == 0x80) {
3659 it.gfx = buf.ReadByte();
3662 if (it.gfx == 0xFE) {
3664 int local_tile_id = buf.ReadWord();
3668 int tempid = _industile_mngr.
GetID(local_tile_id, _cur.
grffile->grfid);
3671 GrfMsg(2,
"IndustriesChangeInfo: Attempt to use industry tile {} with industry id {}, not yet defined. Ignoring.", local_tile_id, indid);
3677 it.ti.
x = (int8_t)
GB(it.ti.
x, 0, 8);
3678 it.ti.
y = (int8_t)
GB(it.ti.
y, 0, 8);
3687 if (_cur.
grffile->grf_version < 8 && it.ti.
x < 0) it.ti.
y += 1;
3693 GrfMsg(1,
"IndustriesChangeInfo: Invalid industry layout for industry id {}. Ignoring", indid);
3697 new_layouts.push_back(layout);
3702 indsp->layouts = new_layouts;
3723 indsp->cost_multiplier = buf.ReadByte();
3743 indsp->production_rate[prop - 0x12] = buf.ReadByte();
3747 indsp->minimal_cargo = buf.ReadByte();
3751 uint8_t num_sounds = buf.ReadByte();
3753 std::vector<uint8_t> sounds;
3754 sounds.reserve(num_sounds);
3755 for (uint8_t j = 0; j < num_sounds; ++j) {
3756 sounds.push_back(buf.ReadByte());
3759 indsp->random_sounds = std::move(sounds);
3764 for (uint8_t j = 0; j < 3; j++) indsp->conflicting[j] = buf.ReadByte();
3776 indsp->map_colour = buf.ReadByte();
3790 uint32_t multiples = buf.ReadDWord();
3791 indsp->input_cargo_multiplier[prop - 0x1C][0] =
GB(multiples, 0, 16);
3792 indsp->input_cargo_multiplier[prop - 0x1C][1] =
GB(multiples, 16, 16);
3801 indsp->prospecting_chance = buf.ReadDWord();
3806 uint8_t aflag = buf.ReadByte();
3807 SB(indsp->callback_mask, (prop - 0x21) * 8, 8, aflag);
3812 indsp->removal_cost_multiplier = buf.ReadDWord();
3816 uint16_t str = buf.ReadWord();
3818 indsp->station_name = STR_NULL;
3826 uint8_t num_cargoes = buf.ReadByte();
3827 if (num_cargoes > std::size(indsp->produced_cargo)) {
3832 for (
size_t i = 0; i < std::size(indsp->produced_cargo); i++) {
3833 if (i < num_cargoes) {
3835 indsp->produced_cargo[i] = cargo;
3837 indsp->produced_cargo[i] = INVALID_CARGO;
3845 uint8_t num_cargoes = buf.ReadByte();
3846 if (num_cargoes > std::size(indsp->accepts_cargo)) {
3851 for (
size_t i = 0; i < std::size(indsp->accepts_cargo); i++) {
3852 if (i < num_cargoes) {
3854 indsp->accepts_cargo[i] = cargo;
3856 indsp->accepts_cargo[i] = INVALID_CARGO;
3864 uint8_t num_cargoes = buf.ReadByte();
3865 if (num_cargoes >
lengthof(indsp->production_rate)) {
3870 for (uint i = 0; i <
lengthof(indsp->production_rate); i++) {
3871 if (i < num_cargoes) {
3872 indsp->production_rate[i] = buf.ReadByte();
3874 indsp->production_rate[i] = 0;
3881 uint8_t num_inputs = buf.ReadByte();
3882 uint8_t num_outputs = buf.ReadByte();
3883 if (num_inputs > std::size(indsp->accepts_cargo) || num_outputs > std::size(indsp->produced_cargo)) {
3888 for (
size_t i = 0; i < std::size(indsp->accepts_cargo); i++) {
3889 for (
size_t j = 0; j < std::size(indsp->produced_cargo); j++) {
3891 if (i < num_inputs && j < num_outputs) mult = buf.ReadWord();
3892 indsp->input_cargo_multiplier[i][j] = mult;
3920 GrfMsg(1,
"AirportChangeInfo: Too many airports, trying id ({}), max ({}). Ignoring.", airport + numinfo,
NUM_AIRPORTS_PER_GRF);
3925 if (_cur.
grffile->airportspec.size() < airport + numinfo) _cur.
grffile->airportspec.resize(airport + numinfo);
3927 for (
int i = 0; i < numinfo; i++) {
3928 auto &as = _cur.
grffile->airportspec[airport + i];
3930 if (as ==
nullptr && prop != 0x08 && prop != 0x09) {
3931 GrfMsg(2,
"AirportChangeInfo: Attempt to modify undefined airport {}, ignoring", airport + i);
3937 uint8_t subs_id = buf.ReadByte();
3938 if (subs_id == 0xFF) {
3945 GrfMsg(2,
"AirportChangeInfo: Attempt to use new airport {} as substitute airport for {}. Ignoring.", subs_id, airport + i);
3952 if (as ==
nullptr) {
3956 as->grf_prop.local_id = airport + i;
3957 as->grf_prop.subst_id = subs_id;
3958 as->grf_prop.grffile = _cur.
grffile;
3960 _airport_mngr.
Add(airport + i, _cur.
grffile->grfid, subs_id);
3966 uint8_t num_layouts = buf.ReadByte();
3971 std::vector<AirportTileLayout> layouts;
3972 layouts.reserve(num_layouts);
3974 for (uint8_t j = 0; j != num_layouts; ++j) {
3975 auto &layout = layouts.emplace_back();
3976 layout.rotation =
static_cast<Direction>(buf.ReadByte() & 6);
3979 auto &tile = layout.tiles.emplace_back();
3980 tile.ti.x = buf.ReadByte();
3981 tile.ti.y = buf.ReadByte();
3982 if (tile.ti.x == 0 && tile.ti.y == 0x80) {
3990 tile.gfx = buf.ReadByte();
3992 if (tile.gfx == 0xFE) {
3994 int local_tile_id = buf.ReadWord();
3997 uint16_t tempid = _airporttile_mngr.
GetID(local_tile_id, _cur.
grffile->grfid);
4000 GrfMsg(2,
"AirportChangeInfo: Attempt to use airport tile {} with airport id {}, not yet defined. Ignoring.", local_tile_id, airport + i);
4005 }
else if (tile.gfx == 0xFF) {
4006 tile.ti.x =
static_cast<int8_t
>(
GB(tile.ti.x, 0, 8));
4007 tile.ti.y =
static_cast<int8_t
>(
GB(tile.ti.y, 0, 8));
4011 if (layout.rotation ==
DIR_E || layout.rotation ==
DIR_W) {
4012 size_x = std::max<uint8_t>(size_x, tile.ti.y + 1);
4013 size_y = std::max<uint8_t>(size_y, tile.ti.x + 1);
4015 size_x = std::max<uint8_t>(size_x, tile.ti.x + 1);
4016 size_y = std::max<uint8_t>(size_y, tile.ti.y + 1);
4020 as->layouts = std::move(layouts);
4021 as->size_x = size_x;
4022 as->size_y = size_y;
4027 as->min_year = buf.ReadWord();
4028 as->max_year = buf.ReadWord();
4041 as->noise_level = buf.ReadByte();
4049 as->maintenance_cost = buf.ReadWord();
4119 GrfMsg(1,
"ObjectChangeInfo: Too many objects loaded ({}), max ({}). Ignoring.",
id + numinfo,
NUM_OBJECTS_PER_GRF);
4124 if (_cur.
grffile->objectspec.size() <
id + numinfo) _cur.
grffile->objectspec.resize(
id + numinfo);
4126 for (
int i = 0; i < numinfo; i++) {
4127 auto &spec = _cur.
grffile->objectspec[
id + i];
4129 if (prop != 0x08 && spec ==
nullptr) {
4132 if (cir > ret) ret = cir;
4139 if (spec ==
nullptr) {
4140 spec = std::make_unique<ObjectSpec>();
4146 uint32_t classid = buf.ReadDWord();
4161 spec->climate = buf.ReadByte();
4165 spec->size = buf.ReadByte();
4166 if (
GB(spec->size, 0, 4) == 0 ||
GB(spec->size, 4, 4) == 0) {
4167 GrfMsg(0,
"ObjectChangeInfo: Invalid object size requested (0x{:X}) for object id {}. Ignoring.", spec->size,
id + i);
4173 spec->build_cost_multiplier = buf.ReadByte();
4174 spec->clear_cost_multiplier = spec->build_cost_multiplier;
4178 spec->introduction_date = buf.ReadDWord();
4182 spec->end_of_life_date = buf.ReadDWord();
4191 spec->animation.frames = buf.ReadByte();
4192 spec->animation.status = buf.ReadByte();
4196 spec->animation.speed = buf.ReadByte();
4200 spec->animation.triggers = buf.ReadWord();
4204 spec->clear_cost_multiplier = buf.ReadByte();
4208 spec->callback_mask = buf.ReadWord();
4212 spec->height = buf.ReadByte();
4216 spec->views = buf.ReadByte();
4217 if (spec->views != 1 && spec->views != 2 && spec->views != 4) {
4218 GrfMsg(2,
"ObjectChangeInfo: Invalid number of views ({}) for object id {}. Ignoring.", spec->views,
id + i);
4224 spec->generate_amount = buf.ReadByte();
4251 GrfMsg(1,
"RailTypeChangeInfo: Rail type {} is invalid, max {}, ignoring",
id + numinfo,
RAILTYPE_END);
4255 for (
int i = 0; i < numinfo; i++) {
4268 uint16_t str = buf.ReadWord();
4270 if (_cur.
grffile->grf_version < 8) {
4300 int n = buf.ReadByte();
4301 for (
int j = 0; j != n; j++) {
4302 RailTypeLabel label = buf.ReadDWord();
4362 for (
int j = buf.ReadByte(); j != 0; j--) buf.ReadDWord();
4381 GrfMsg(1,
"RailTypeReserveInfo: Rail type {} is invalid, max {}, ignoring",
id + numinfo,
RAILTYPE_END);
4385 for (
int i = 0; i < numinfo; i++) {
4389 RailTypeLabel rtl = buf.ReadDWord();
4398 _cur.
grffile->railtype_map[
id + i] = rt;
4416 int n = buf.ReadByte();
4417 for (
int j = 0; j != n; j++) {
4422 GrfMsg(1,
"RailTypeReserveInfo: Ignoring property 1D for rail type {} because no label was set",
id + i);
4429 for (
int j = buf.ReadByte(); j != 0; j--) buf.ReadDWord();
4470 GrfMsg(1,
"RoadTypeChangeInfo: Road type {} is invalid, max {}, ignoring",
id + numinfo,
ROADTYPE_END);
4474 for (
int i = 0; i < numinfo; i++) {
4487 uint16_t str = buf.ReadWord();
4514 int n = buf.ReadByte();
4515 for (
int j = 0; j != n; j++) {
4516 RoadTypeLabel label = buf.ReadDWord();
4521 if (GetRoadTramType(resolved_rt) == rtt) {
4524 GrfMsg(1,
"RoadTypeChangeInfo: Powered road type list: Road type {} road/tram type does not match road type {}, ignoring", resolved_rt, rt);
4569 for (
int j = buf.ReadByte(); j != 0; j--) buf.ReadDWord();
4600 GrfMsg(1,
"RoadTypeReserveInfo: Road type {} is invalid, max {}, ignoring",
id + numinfo,
ROADTYPE_END);
4604 for (
int i = 0; i < numinfo; i++) {
4607 RoadTypeLabel rtl = buf.ReadDWord();
4614 }
else if (GetRoadTramType(rt) != rtt) {
4615 GrfMsg(1,
"RoadTypeReserveInfo: Road type {} is invalid type (road/tram), ignoring",
id + numinfo);
4619 type_map[
id + i] = rt;
4636 int n = buf.ReadByte();
4637 for (
int j = 0; j != n; j++) {
4642 GrfMsg(1,
"RoadTypeReserveInfo: Ignoring property 1D for road type {} because no label was set",
id + i);
4648 for (
int j = buf.ReadByte(); j != 0; j--) buf.ReadDWord();
4672 return RoadTypeReserveInfo(
id, numinfo, prop, buf, RTT_ROAD);
4677 return RoadTypeReserveInfo(
id, numinfo, prop, buf, RTT_TRAM);
4685 GrfMsg(1,
"AirportTileChangeInfo: Too many airport tiles loaded ({}), max ({}). Ignoring.", airtid + numinfo,
NUM_AIRPORTTILES_PER_GRF);
4690 if (_cur.
grffile->airtspec.size() < airtid + numinfo) _cur.
grffile->airtspec.resize(airtid + numinfo);
4692 for (
int i = 0; i < numinfo; i++) {
4693 auto &tsp = _cur.
grffile->airtspec[airtid + i];
4695 if (prop != 0x08 && tsp ==
nullptr) {
4696 GrfMsg(2,
"AirportTileChangeInfo: Attempt to modify undefined airport tile {}. Ignoring.", airtid + i);
4702 uint8_t subs_id = buf.ReadByte();
4705 GrfMsg(2,
"AirportTileChangeInfo: Attempt to use new airport tile {} as substitute airport tile for {}. Ignoring.", subs_id, airtid + i);
4710 if (tsp ==
nullptr) {
4713 tsp->enabled =
true;
4717 tsp->grf_prop.local_id = airtid + i;
4718 tsp->grf_prop.subst_id = subs_id;
4719 tsp->grf_prop.grffile = _cur.
grffile;
4726 uint8_t
override = buf.ReadByte();
4730 GrfMsg(2,
"AirportTileChangeInfo: Attempt to override new airport tile {} with airport tile id {}. Ignoring.",
override, airtid + i);
4734 _airporttile_mngr.
Add(airtid + i, _cur.
grffile->grfid,
override);
4739 tsp->callback_mask = buf.ReadByte();
4743 tsp->animation.frames = buf.ReadByte();
4744 tsp->animation.status = buf.ReadByte();
4748 tsp->animation.speed = buf.ReadByte();
4752 tsp->animation.triggers = buf.ReadByte();
4809 GrfMsg(1,
"RoadStopChangeInfo: RoadStop {} is invalid, max {}, ignoring",
id + numinfo,
NUM_ROADSTOPS_PER_GRF);
4813 if (_cur.
grffile->roadstops.size() <
id + numinfo) _cur.
grffile->roadstops.resize(
id + numinfo);
4815 for (
int i = 0; i < numinfo; i++) {
4816 auto &rs = _cur.
grffile->roadstops[
id + i];
4818 if (rs ==
nullptr && prop != 0x08) {
4819 GrfMsg(1,
"RoadStopChangeInfo: Attempt to modify undefined road stop {}, ignoring",
id + i);
4821 if (cir > ret) ret = cir;
4827 if (rs ==
nullptr) {
4828 rs = std::make_unique<RoadStopSpec>();
4831 uint32_t classid = buf.ReadDWord();
4857 rs->animation.frames = buf.ReadByte();
4858 rs->animation.status = buf.ReadByte();
4862 rs->animation.speed = buf.ReadByte();
4866 rs->animation.triggers = buf.ReadWord();
4870 rs->callback_mask = buf.ReadByte();
4874 rs->flags = (uint16_t)buf.ReadDWord();
4878 rs->build_cost_multiplier = buf.ReadByte();
4879 rs->clear_cost_multiplier = buf.ReadByte();
4891 static bool HandleChangeInfoResult(
const char *caller,
ChangeInfoResult cir, uint8_t feature, uint8_t property)
4894 default: NOT_REACHED();
4904 GrfMsg(1,
"{}: Ignoring property 0x{:02X} of feature 0x{:02X} (not implemented)", caller, property, feature);
4908 GrfMsg(0,
"{}: Unknown property 0x{:02X} of feature 0x{:02X}, disabling", caller, property, feature);
4921 static void FeatureChangeInfo(
ByteReader &buf)
4934 static const VCI_Handler handler[] = {
4952 AirportTilesChangeInfo,
4957 static_assert(GSF_END ==
lengthof(handler));
4959 uint8_t feature = buf.ReadByte();
4960 uint8_t numprops = buf.ReadByte();
4961 uint numinfo = buf.ReadByte();
4962 uint engine = buf.ReadExtendedByte();
4964 if (feature >= GSF_END) {
4965 GrfMsg(1,
"FeatureChangeInfo: Unsupported feature 0x{:02X}, skipping", feature);
4969 GrfMsg(6,
"FeatureChangeInfo: Feature 0x{:02X}, {} properties, to apply to {}+{}",
4970 feature, numprops, engine, numinfo);
4972 if (handler[feature] ==
nullptr) {
4973 if (feature != GSF_CARGOES) GrfMsg(1,
"FeatureChangeInfo: Unsupported feature 0x{:02X}, skipping", feature);
4980 while (numprops-- && buf.HasData()) {
4981 uint8_t prop = buf.ReadByte();
4984 if (HandleChangeInfoResult(
"FeatureChangeInfo", cir, feature, prop))
return;
4991 uint8_t feature = buf.ReadByte();
4992 uint8_t numprops = buf.ReadByte();
4993 uint numinfo = buf.ReadByte();
4994 buf.ReadExtendedByte();
4996 if (feature == GSF_BRIDGES && numprops == 1) {
4997 uint8_t prop = buf.ReadByte();
5000 if (prop == 0x0D)
return;
5001 }
else if (feature == GSF_GLOBALVAR && numprops == 1) {
5002 uint8_t prop = buf.ReadByte();
5005 bool is_safe =
true;
5006 for (uint i = 0; i < numinfo; i++) {
5007 uint32_t s = buf.ReadDWord();
5015 if (is_safe)
return;
5026 static void ReserveChangeInfo(
ByteReader &buf)
5028 uint8_t feature = buf.ReadByte();
5030 if (feature != GSF_CARGOES && feature != GSF_GLOBALVAR && feature != GSF_RAILTYPES && feature != GSF_ROADTYPES && feature != GSF_TRAMTYPES)
return;
5032 uint8_t numprops = buf.ReadByte();
5033 uint8_t numinfo = buf.ReadByte();
5034 uint8_t index = buf.ReadExtendedByte();
5036 while (numprops-- && buf.HasData()) {
5037 uint8_t prop = buf.ReadByte();
5041 default: NOT_REACHED();
5047 cir = GlobalVarReserveInfo(index, numinfo, prop, buf);
5051 cir = RailTypeReserveInfo(index, numinfo, prop, buf);
5055 cir = RoadTypeReserveInfo(index, numinfo, prop, buf);
5059 cir = TramTypeReserveInfo(index, numinfo, prop, buf);
5063 if (HandleChangeInfoResult(
"ReserveChangeInfo", cir, feature, prop))
return;
5084 uint8_t feature = buf.ReadByte();
5085 uint16_t num_sets = buf.ReadByte();
5086 uint16_t first_set = 0;
5088 if (num_sets == 0 && buf.HasData(3)) {
5091 first_set = buf.ReadExtendedByte();
5092 num_sets = buf.ReadExtendedByte();
5094 uint16_t num_ents = buf.ReadExtendedByte();
5096 if (feature >= GSF_END) {
5098 GrfMsg(1,
"NewSpriteSet: Unsupported feature 0x{:02X}, skipping {} sprites", feature, _cur.
skip_sprites);
5104 GrfMsg(7,
"New sprite set at {} of feature 0x{:02X}, consisting of {} sets with {} views each (total {})",
5105 _cur.
spriteid, feature, num_sets, num_ents, num_sets * num_ents
5108 for (
int i = 0; i < num_sets * num_ents; i++) {
5118 uint16_t num_sets = buf.ReadByte();
5120 if (num_sets == 0 && buf.HasData(3)) {
5123 buf.ReadExtendedByte();
5124 num_sets = buf.ReadExtendedByte();
5126 uint16_t num_ents = buf.ReadExtendedByte();
5130 GrfMsg(3,
"SkipAct1: Skipping {} sprites", _cur.
skip_sprites);
5135 static const SpriteGroup *GetGroupFromGroupID(uint8_t setid, uint8_t type, uint16_t groupid)
5137 if (
HasBit(groupid, 15)) {
5142 if (groupid >
MAX_SPRITEGROUP || _cur.spritegroups[groupid] ==
nullptr) {
5143 GrfMsg(1,
"GetGroupFromGroupID(0x{:02X}:0x{:02X}): Groupid 0x{:04X} does not exist, leaving empty", setid, type, groupid);
5147 return _cur.spritegroups[groupid];
5160 if (
HasBit(spriteid, 15)) {
5166 GrfMsg(1,
"CreateGroupFromGroupID(0x{:02X}:0x{:02X}): Sprite set {} invalid", setid, type, spriteid);
5171 uint num_sprites = _cur.
GetNumEnts(feature, spriteid);
5174 assert(spriteset_start + num_sprites <= _cur.
spriteid);
5195 uint8_t feature = buf.ReadByte();
5196 if (feature >= GSF_END) {
5197 GrfMsg(1,
"NewSpriteGroup: Unsupported feature 0x{:02X}, skipping", feature);
5201 uint8_t setid = buf.ReadByte();
5202 uint8_t type = buf.ReadByte();
5226 switch (
GB(type, 2, 2)) {
5227 default: NOT_REACHED();
5228 case 0: group->size = DSG_SIZE_BYTE; varsize = 1;
break;
5229 case 1: group->size = DSG_SIZE_WORD; varsize = 2;
break;
5230 case 2: group->size = DSG_SIZE_DWORD; varsize = 4;
break;
5240 adjust.variable = buf.ReadByte();
5241 if (adjust.variable == 0x7E) {
5243 adjust.subroutine = GetGroupFromGroupID(setid, type, buf.ReadByte());
5248 varadjust = buf.ReadByte();
5249 adjust.shift_num =
GB(varadjust, 0, 5);
5250 adjust.type = (DeterministicSpriteGroupAdjustType)
GB(varadjust, 6, 2);
5251 adjust.and_mask = buf.ReadVarSize(varsize);
5253 if (adjust.type != DSGA_TYPE_NONE) {
5254 adjust.add_val = buf.ReadVarSize(varsize);
5255 adjust.divmod_val = buf.ReadVarSize(varsize);
5258 adjust.divmod_val = 0;
5262 }
while (
HasBit(varadjust, 5));
5264 std::vector<DeterministicSpriteGroupRange> ranges;
5265 ranges.resize(buf.ReadByte());
5266 for (
auto &range : ranges) {
5267 range.group = GetGroupFromGroupID(setid, type, buf.ReadWord());
5268 range.low = buf.ReadVarSize(varsize);
5269 range.high = buf.ReadVarSize(varsize);
5272 group->default_group = GetGroupFromGroupID(setid, type, buf.ReadWord());
5273 group->error_group = ranges.empty() ? group->default_group : ranges[0].group;
5275 group->calculated_result = ranges.empty();
5278 std::vector<uint32_t> bounds;
5279 bounds.reserve(ranges.size());
5280 for (
const auto &range : ranges) {
5281 bounds.push_back(range.low);
5282 if (range.high != UINT32_MAX) bounds.push_back(range.high + 1);
5284 std::sort(bounds.begin(), bounds.end());
5285 bounds.erase(std::unique(bounds.begin(), bounds.end()), bounds.end());
5287 std::vector<const SpriteGroup *> target;
5288 target.reserve(bounds.size());
5289 for (
const auto &bound : bounds) {
5291 for (
const auto &range : ranges) {
5292 if (range.low <= bound && bound <= range.high) {
5297 target.push_back(t);
5299 assert(target.size() == bounds.size());
5301 for (uint j = 0; j < bounds.size(); ) {
5302 if (target[j] != group->default_group) {
5304 r.group = target[j];
5306 while (j < bounds.size() && target[j] == r.group) {
5309 r.high = j < bounds.size() ? bounds[j] - 1 : UINT32_MAX;
5331 group->count = buf.ReadByte();
5334 uint8_t triggers = buf.ReadByte();
5335 group->triggers =
GB(triggers, 0, 7);
5336 group->
cmp_mode =
HasBit(triggers, 7) ? RSG_CMP_ALL : RSG_CMP_ANY;
5339 uint8_t num_groups = buf.ReadByte();
5341 GrfMsg(1,
"NewSpriteGroup: Random Action 2 nrand should be power of 2");
5344 group->
groups.reserve(num_groups);
5345 for (uint i = 0; i < num_groups; i++) {
5346 group->
groups.push_back(GetGroupFromGroupID(setid, type, buf.ReadWord()));
5357 case GSF_ROADVEHICLES:
5368 uint8_t num_loaded = type;
5369 uint8_t num_loading = buf.ReadByte();
5372 GrfMsg(0,
"NewSpriteGroup: No sprite set to work on! Skipping");
5376 GrfMsg(6,
"NewSpriteGroup: New SpriteGroup 0x{:02X}, {} loaded, {} loading",
5377 setid, num_loaded, num_loading);
5379 if (num_loaded + num_loading == 0) {
5380 GrfMsg(1,
"NewSpriteGroup: no result, skipping invalid RealSpriteGroup");
5384 if (num_loaded + num_loading == 1) {
5386 uint16_t spriteid = buf.ReadWord();
5388 GrfMsg(8,
"NewSpriteGroup: one result, skipping RealSpriteGroup = subset {}", spriteid);
5392 std::vector<uint16_t> loaded;
5393 std::vector<uint16_t> loading;
5395 loaded.reserve(num_loaded);
5396 for (uint i = 0; i < num_loaded; i++) {
5397 loaded.push_back(buf.ReadWord());
5398 GrfMsg(8,
"NewSpriteGroup: + rg->loaded[{}] = subset {}", i, loaded[i]);
5401 loading.reserve(num_loading);
5402 for (uint i = 0; i < num_loading; i++) {
5403 loading.push_back(buf.ReadWord());
5404 GrfMsg(8,
"NewSpriteGroup: + rg->loading[{}] = subset {}", i, loading[i]);
5407 bool loaded_same = !loaded.empty() && std::adjacent_find(loaded.begin(), loaded.end(), std::not_equal_to<>()) == loaded.end();
5408 bool loading_same = !loading.empty() && std::adjacent_find(loading.begin(), loading.end(), std::not_equal_to<>()) == loading.end();
5409 if (loaded_same && loading_same && loaded[0] == loading[0]) {
5412 GrfMsg(8,
"NewSpriteGroup: same result, skipping RealSpriteGroup = subset {}", loaded[0]);
5421 if (loaded_same && loaded.size() > 1) loaded.resize(1);
5422 group->
loaded.reserve(loaded.size());
5423 for (uint16_t spriteid : loaded) {
5425 group->
loaded.push_back(t);
5428 if (loading_same && loading.size() > 1) loading.resize(1);
5429 group->
loading.reserve(loading.size());
5430 for (uint16_t spriteid : loading) {
5439 case GSF_AIRPORTTILES:
5441 case GSF_INDUSTRYTILES:
5442 case GSF_ROADSTOPS: {
5443 uint8_t num_building_sprites = std::max((uint8_t)1, type);
5451 if (
ReadSpriteLayout(buf, num_building_sprites,
true, feature,
false, type == 0, &group->dts))
return;
5455 case GSF_INDUSTRIES: {
5457 GrfMsg(1,
"NewSpriteGroup: Unsupported industry production version {}, skipping", type);
5475 group->again = buf.ReadByte();
5476 }
else if (type == 1) {
5485 group->again = buf.ReadByte();
5486 }
else if (type == 2) {
5490 error->
data =
"too many inputs (max 16)";
5493 for (uint i = 0; i < group->
num_input; i++) {
5494 uint8_t rawcargo = buf.ReadByte();
5503 error->
data =
"duplicate input cargo";
5512 error->
data =
"too many outputs (max 16)";
5515 for (uint i = 0; i < group->
num_output; i++) {
5516 uint8_t rawcargo = buf.ReadByte();
5523 error->
data =
"duplicate output cargo";
5529 group->again = buf.ReadByte();
5537 default: GrfMsg(1,
"NewSpriteGroup: Unsupported feature 0x{:02X}, skipping", feature);
5542 _cur.spritegroups[setid] = act_group;
5545 static CargoID TranslateCargo(uint8_t feature, uint8_t ctype)
5554 GrfMsg(1,
"TranslateCargo: Cargo bitnum {} out of range (max 31), skipping.", ctype);
5555 return INVALID_CARGO;
5559 if (cs->bitnum == ctype) {
5560 GrfMsg(6,
"TranslateCargo: Cargo bitnum {} mapped to cargo type {}.", ctype, cs->Index());
5565 GrfMsg(5,
"TranslateCargo: Cargo bitnum {} not available in this climate, skipping.", ctype);
5566 return INVALID_CARGO;
5571 GrfMsg(1,
"TranslateCargo: Cargo type {} out of range (max {}), skipping.", ctype, (
unsigned int)_cur.
grffile->
cargo_list.size() - 1);
5572 return INVALID_CARGO;
5578 GrfMsg(5,
"TranslateCargo: Cargo type {} not available in this climate, skipping.", ctype);
5579 return INVALID_CARGO;
5582 CargoID cid = GetCargoIDByLabel(cl);
5584 GrfMsg(5,
"TranslateCargo: Cargo '{:c}{:c}{:c}{:c}' unsupported, skipping.",
GB(cl.base(), 24, 8),
GB(cl.base(), 16, 8),
GB(cl.base(), 8, 8),
GB(cl.base(), 0, 8));
5585 return INVALID_CARGO;
5588 GrfMsg(6,
"TranslateCargo: Cargo '{:c}{:c}{:c}{:c}' mapped to cargo type {}.",
GB(cl.base(), 24, 8),
GB(cl.base(), 16, 8),
GB(cl.base(), 8, 8),
GB(cl.base(), 0, 8), cid);
5593 static bool IsValidGroupID(uint16_t groupid,
const char *
function)
5595 if (groupid >
MAX_SPRITEGROUP || _cur.spritegroups[groupid] ==
nullptr) {
5596 GrfMsg(1,
"{}: Spritegroup 0x{:04X} out of range or empty, skipping.",
function, groupid);
5603 static void VehicleMapSpriteGroup(
ByteReader &buf, uint8_t feature, uint8_t idcount)
5605 static std::vector<EngineID> last_engines;
5606 bool wagover =
false;
5609 if (
HasBit(idcount, 7)) {
5612 idcount =
GB(idcount, 0, 7);
5614 if (last_engines.empty()) {
5615 GrfMsg(0,
"VehicleMapSpriteGroup: WagonOverride: No engine to do override with");
5619 GrfMsg(6,
"VehicleMapSpriteGroup: WagonOverride: {} engines, {} wagons", last_engines.size(), idcount);
5621 last_engines.resize(idcount);
5624 std::vector<EngineID> engines;
5625 engines.reserve(idcount);
5626 for (uint i = 0; i < idcount; i++) {
5632 HandleChangeInfoResult(
"VehicleMapSpriteGroup",
CIR_INVALID_ID, 0, 0);
5636 engines.push_back(e->
index);
5637 if (!wagover) last_engines[i] = engines[i];
5640 uint8_t cidcount = buf.ReadByte();
5641 for (uint c = 0; c < cidcount; c++) {
5642 uint8_t ctype = buf.ReadByte();
5643 uint16_t groupid = buf.ReadWord();
5644 if (!IsValidGroupID(groupid,
"VehicleMapSpriteGroup"))
continue;
5646 GrfMsg(8,
"VehicleMapSpriteGroup: * [{}] Cargo type 0x{:X}, group id 0x{:02X}", c, ctype, groupid);
5648 CargoID cid = TranslateCargo(feature, ctype);
5651 for (uint i = 0; i < idcount; i++) {
5654 GrfMsg(7,
"VehicleMapSpriteGroup: [{}] Engine {}...", i, engine);
5657 SetWagonOverrideSprites(engine, cid, _cur.spritegroups[groupid], last_engines);
5659 SetCustomEngineSprites(engine, cid, _cur.spritegroups[groupid]);
5664 uint16_t groupid = buf.ReadWord();
5665 if (!IsValidGroupID(groupid,
"VehicleMapSpriteGroup"))
return;
5667 GrfMsg(8,
"-- Default group id 0x{:04X}", groupid);
5669 for (uint i = 0; i < idcount; i++) {
5682 static void CanalMapSpriteGroup(
ByteReader &buf, uint8_t idcount)
5684 std::vector<uint16_t> cfs;
5685 cfs.reserve(idcount);
5686 for (uint i = 0; i < idcount; i++) {
5687 cfs.push_back(buf.ReadExtendedByte());
5690 uint8_t cidcount = buf.ReadByte();
5691 buf.Skip(cidcount * 3);
5693 uint16_t groupid = buf.ReadWord();
5694 if (!IsValidGroupID(groupid,
"CanalMapSpriteGroup"))
return;
5696 for (
auto &cf : cfs) {
5698 GrfMsg(1,
"CanalMapSpriteGroup: Canal subset {} out of range, skipping", cf);
5708 static void StationMapSpriteGroup(
ByteReader &buf, uint8_t idcount)
5710 if (_cur.
grffile->stations.empty()) {
5711 GrfMsg(1,
"StationMapSpriteGroup: No stations defined, skipping");
5715 std::vector<uint16_t> stations;
5716 stations.reserve(idcount);
5717 for (uint i = 0; i < idcount; i++) {
5718 stations.push_back(buf.ReadExtendedByte());
5721 uint8_t cidcount = buf.ReadByte();
5722 for (uint c = 0; c < cidcount; c++) {
5723 uint8_t ctype = buf.ReadByte();
5724 uint16_t groupid = buf.ReadWord();
5725 if (!IsValidGroupID(groupid,
"StationMapSpriteGroup"))
continue;
5727 ctype = TranslateCargo(GSF_STATIONS, ctype);
5730 for (
auto &station : stations) {
5733 if (statspec ==
nullptr) {
5734 GrfMsg(1,
"StationMapSpriteGroup: Station {} undefined, skipping", station);
5742 uint16_t groupid = buf.ReadWord();
5743 if (!IsValidGroupID(groupid,
"StationMapSpriteGroup"))
return;
5745 for (
auto &station : stations) {
5748 if (statspec ==
nullptr) {
5749 GrfMsg(1,
"StationMapSpriteGroup: Station {} undefined, skipping", station);
5754 GrfMsg(1,
"StationMapSpriteGroup: Station {} mapped multiple times, skipping", station);
5766 static void TownHouseMapSpriteGroup(
ByteReader &buf, uint8_t idcount)
5768 if (_cur.
grffile->housespec.empty()) {
5769 GrfMsg(1,
"TownHouseMapSpriteGroup: No houses defined, skipping");
5773 std::vector<uint16_t> houses;
5774 houses.reserve(idcount);
5775 for (uint i = 0; i < idcount; i++) {
5776 houses.push_back(buf.ReadExtendedByte());
5780 uint8_t cidcount = buf.ReadByte();
5781 buf.Skip(cidcount * 3);
5783 uint16_t groupid = buf.ReadWord();
5784 if (!IsValidGroupID(groupid,
"TownHouseMapSpriteGroup"))
return;
5786 for (
auto &house : houses) {
5789 if (hs ==
nullptr) {
5790 GrfMsg(1,
"TownHouseMapSpriteGroup: House {} undefined, skipping.", house);
5798 static void IndustryMapSpriteGroup(
ByteReader &buf, uint8_t idcount)
5800 if (_cur.
grffile->industryspec.empty()) {
5801 GrfMsg(1,
"IndustryMapSpriteGroup: No industries defined, skipping");
5805 std::vector<uint16_t> industries;
5806 industries.reserve(idcount);
5807 for (uint i = 0; i < idcount; i++) {
5808 industries.push_back(buf.ReadExtendedByte());
5812 uint8_t cidcount = buf.ReadByte();
5813 buf.Skip(cidcount * 3);
5815 uint16_t groupid = buf.ReadWord();
5816 if (!IsValidGroupID(groupid,
"IndustryMapSpriteGroup"))
return;
5818 for (
auto &industry : industries) {
5821 if (indsp ==
nullptr) {
5822 GrfMsg(1,
"IndustryMapSpriteGroup: Industry {} undefined, skipping", industry);
5830 static void IndustrytileMapSpriteGroup(
ByteReader &buf, uint8_t idcount)
5832 if (_cur.
grffile->indtspec.empty()) {
5833 GrfMsg(1,
"IndustrytileMapSpriteGroup: No industry tiles defined, skipping");
5837 std::vector<uint16_t> indtiles;
5838 indtiles.reserve(idcount);
5839 for (uint i = 0; i < idcount; i++) {
5840 indtiles.push_back(buf.ReadExtendedByte());
5844 uint8_t cidcount = buf.ReadByte();
5845 buf.Skip(cidcount * 3);
5847 uint16_t groupid = buf.ReadWord();
5848 if (!IsValidGroupID(groupid,
"IndustrytileMapSpriteGroup"))
return;
5850 for (
auto &indtile : indtiles) {
5853 if (indtsp ==
nullptr) {
5854 GrfMsg(1,
"IndustrytileMapSpriteGroup: Industry tile {} undefined, skipping", indtile);
5862 static void CargoMapSpriteGroup(
ByteReader &buf, uint8_t idcount)
5864 std::vector<uint16_t> cargoes;
5865 cargoes.reserve(idcount);
5866 for (uint i = 0; i < idcount; i++) {
5867 cargoes.push_back(buf.ReadExtendedByte());
5871 uint8_t cidcount = buf.ReadByte();
5872 buf.Skip(cidcount * 3);
5874 uint16_t groupid = buf.ReadWord();
5875 if (!IsValidGroupID(groupid,
"CargoMapSpriteGroup"))
return;
5877 for (
auto &cid : cargoes) {
5879 GrfMsg(1,
"CargoMapSpriteGroup: Cargo ID {} out of range, skipping", cid);
5885 cs->group = _cur.spritegroups[groupid];
5889 static void ObjectMapSpriteGroup(
ByteReader &buf, uint8_t idcount)
5891 if (_cur.
grffile->objectspec.empty()) {
5892 GrfMsg(1,
"ObjectMapSpriteGroup: No object tiles defined, skipping");
5896 std::vector<uint16_t> objects;
5897 objects.reserve(idcount);
5898 for (uint i = 0; i < idcount; i++) {
5899 objects.push_back(buf.ReadExtendedByte());
5902 uint8_t cidcount = buf.ReadByte();
5903 for (uint c = 0; c < cidcount; c++) {
5904 uint8_t ctype = buf.ReadByte();
5905 uint16_t groupid = buf.ReadWord();
5906 if (!IsValidGroupID(groupid,
"ObjectMapSpriteGroup"))
continue;
5909 if (ctype != 0xFF) {
5910 GrfMsg(1,
"ObjectMapSpriteGroup: Invalid cargo bitnum {} for objects, skipping.", ctype);
5914 for (
auto &
object : objects) {
5917 if (spec ==
nullptr) {
5918 GrfMsg(1,
"ObjectMapSpriteGroup: Object {} undefined, skipping",
object);
5926 uint16_t groupid = buf.ReadWord();
5927 if (!IsValidGroupID(groupid,
"ObjectMapSpriteGroup"))
return;
5929 for (
auto &
object : objects) {
5932 if (spec ==
nullptr) {
5933 GrfMsg(1,
"ObjectMapSpriteGroup: Object {} undefined, skipping",
object);
5938 GrfMsg(1,
"ObjectMapSpriteGroup: Object {} mapped multiple times, skipping",
object);
5948 static void RailTypeMapSpriteGroup(
ByteReader &buf, uint8_t idcount)
5950 std::vector<uint8_t> railtypes;
5951 railtypes.reserve(idcount);
5952 for (uint i = 0; i < idcount; i++) {
5953 uint16_t
id = buf.ReadExtendedByte();
5954 railtypes.push_back(id < RAILTYPE_END ? _cur.grffile->railtype_map[
id] :
INVALID_RAILTYPE);
5957 uint8_t cidcount = buf.ReadByte();
5958 for (uint c = 0; c < cidcount; c++) {
5959 uint8_t ctype = buf.ReadByte();
5960 uint16_t groupid = buf.ReadWord();
5961 if (!IsValidGroupID(groupid,
"RailTypeMapSpriteGroup"))
continue;
5963 if (ctype >= RTSG_END)
continue;
5966 for (
auto &railtype : railtypes) {
5971 rti->
group[ctype] = _cur.spritegroups[groupid];
5980 static void RoadTypeMapSpriteGroup(
ByteReader &buf, uint8_t idcount, RoadTramType rtt)
5984 std::vector<uint8_t> roadtypes;
5985 roadtypes.reserve(idcount);
5986 for (uint i = 0; i < idcount; i++) {
5987 uint16_t
id = buf.ReadExtendedByte();
5991 uint8_t cidcount = buf.ReadByte();
5992 for (uint c = 0; c < cidcount; c++) {
5993 uint8_t ctype = buf.ReadByte();
5994 uint16_t groupid = buf.ReadWord();
5995 if (!IsValidGroupID(groupid,
"RoadTypeMapSpriteGroup"))
continue;
5997 if (ctype >= ROTSG_END)
continue;
6000 for (
auto &roadtype : roadtypes) {
6005 rti->
group[ctype] = _cur.spritegroups[groupid];
6014 static void AirportMapSpriteGroup(
ByteReader &buf, uint8_t idcount)
6016 if (_cur.
grffile->airportspec.empty()) {
6017 GrfMsg(1,
"AirportMapSpriteGroup: No airports defined, skipping");
6021 std::vector<uint16_t> airports;
6022 airports.reserve(idcount);
6023 for (uint i = 0; i < idcount; i++) {
6024 airports.push_back(buf.ReadExtendedByte());
6028 uint8_t cidcount = buf.ReadByte();
6029 buf.Skip(cidcount * 3);
6031 uint16_t groupid = buf.ReadWord();
6032 if (!IsValidGroupID(groupid,
"AirportMapSpriteGroup"))
return;
6034 for (
auto &airport : airports) {
6037 if (as ==
nullptr) {
6038 GrfMsg(1,
"AirportMapSpriteGroup: Airport {} undefined, skipping", airport);
6046 static void AirportTileMapSpriteGroup(
ByteReader &buf, uint8_t idcount)
6048 if (_cur.
grffile->airtspec.empty()) {
6049 GrfMsg(1,
"AirportTileMapSpriteGroup: No airport tiles defined, skipping");
6053 std::vector<uint16_t> airptiles;
6054 airptiles.reserve(idcount);
6055 for (uint i = 0; i < idcount; i++) {
6056 airptiles.push_back(buf.ReadExtendedByte());
6060 uint8_t cidcount = buf.ReadByte();
6061 buf.Skip(cidcount * 3);
6063 uint16_t groupid = buf.ReadWord();
6064 if (!IsValidGroupID(groupid,
"AirportTileMapSpriteGroup"))
return;
6066 for (
auto &airptile : airptiles) {
6069 if (airtsp ==
nullptr) {
6070 GrfMsg(1,
"AirportTileMapSpriteGroup: Airport tile {} undefined, skipping", airptile);
6078 static void RoadStopMapSpriteGroup(
ByteReader &buf, uint8_t idcount)
6080 if (_cur.
grffile->roadstops.empty()) {
6081 GrfMsg(1,
"RoadStopMapSpriteGroup: No roadstops defined, skipping");
6085 std::vector<uint16_t> roadstops;
6086 roadstops.reserve(idcount);
6087 for (uint i = 0; i < idcount; i++) {
6088 roadstops.push_back(buf.ReadExtendedByte());
6091 uint8_t cidcount = buf.ReadByte();
6092 for (uint c = 0; c < cidcount; c++) {
6093 uint8_t ctype = buf.ReadByte();
6094 uint16_t groupid = buf.ReadWord();
6095 if (!IsValidGroupID(groupid,
"RoadStopMapSpriteGroup"))
continue;
6097 ctype = TranslateCargo(GSF_ROADSTOPS, ctype);
6100 for (
auto &roadstop : roadstops) {
6103 if (roadstopspec ==
nullptr) {
6104 GrfMsg(1,
"RoadStopMapSpriteGroup: Road stop {} undefined, skipping", roadstop);
6112 uint16_t groupid = buf.ReadWord();
6113 if (!IsValidGroupID(groupid,
"RoadStopMapSpriteGroup"))
return;
6115 for (
auto &roadstop : roadstops) {
6118 if (roadstopspec ==
nullptr) {
6119 GrfMsg(1,
"RoadStopMapSpriteGroup: Road stop {} undefined, skipping.", roadstop);
6124 GrfMsg(1,
"RoadStopMapSpriteGroup: Road stop {} mapped multiple times, skipping", roadstop);
6136 static void FeatureMapSpriteGroup(
ByteReader &buf)
6152 uint8_t feature = buf.ReadByte();
6153 uint8_t idcount = buf.ReadByte();
6155 if (feature >= GSF_END) {
6156 GrfMsg(1,
"FeatureMapSpriteGroup: Unsupported feature 0x{:02X}, skipping", feature);
6164 uint16_t groupid = buf.ReadWord();
6165 if (!IsValidGroupID(groupid,
"FeatureMapSpriteGroup"))
return;
6167 GrfMsg(6,
"FeatureMapSpriteGroup: Adding generic feature callback for feature 0x{:02X}", feature);
6176 GrfMsg(6,
"FeatureMapSpriteGroup: Feature 0x{:02X}, {} ids", feature, idcount);
6180 case GSF_ROADVEHICLES:
6183 VehicleMapSpriteGroup(buf, feature, idcount);
6187 CanalMapSpriteGroup(buf, idcount);
6191 StationMapSpriteGroup(buf, idcount);
6195 TownHouseMapSpriteGroup(buf, idcount);
6198 case GSF_INDUSTRIES:
6199 IndustryMapSpriteGroup(buf, idcount);
6202 case GSF_INDUSTRYTILES:
6203 IndustrytileMapSpriteGroup(buf, idcount);
6207 CargoMapSpriteGroup(buf, idcount);
6211 AirportMapSpriteGroup(buf, idcount);
6215 ObjectMapSpriteGroup(buf, idcount);
6219 RailTypeMapSpriteGroup(buf, idcount);
6223 RoadTypeMapSpriteGroup(buf, idcount, RTT_ROAD);
6227 RoadTypeMapSpriteGroup(buf, idcount, RTT_TRAM);
6230 case GSF_AIRPORTTILES:
6231 AirportTileMapSpriteGroup(buf, idcount);
6235 RoadStopMapSpriteGroup(buf, idcount);
6239 GrfMsg(1,
"FeatureMapSpriteGroup: Unsupported feature 0x{:02X}, skipping", feature);
6263 bool new_scheme = _cur.
grffile->grf_version >= 7;
6265 uint8_t feature = buf.ReadByte();
6266 if (feature >= GSF_END && feature != 0x48) {
6267 GrfMsg(1,
"FeatureNewName: Unsupported feature 0x{:02X}, skipping", feature);
6271 uint8_t lang = buf.ReadByte();
6272 uint8_t num = buf.ReadByte();
6273 bool generic =
HasBit(lang, 7);
6276 id = buf.ReadWord();
6277 }
else if (feature <= GSF_AIRCRAFT) {
6278 id = buf.ReadExtendedByte();
6280 id = buf.ReadByte();
6285 uint16_t endid =
id + num;
6287 GrfMsg(6,
"FeatureNewName: About to rename engines {}..{} (feature 0x{:02X}) in language 0x{:02X}",
6288 id, endid, feature, lang);
6290 for (;
id < endid && buf.HasData();
id++) {
6291 const std::string_view name = buf.ReadString();
6292 GrfMsg(8,
"FeatureNewName: 0x{:04X} <- {}",
id,
StrMakeValid(name));
6296 case GSF_ROADVEHICLES:
6301 if (e ==
nullptr)
break;
6315 switch (
GB(
id, 8, 8)) {
6317 if (
GB(
id, 0, 8) >= _cur.
grffile->stations.size() || _cur.
grffile->stations[
GB(
id, 0, 8)] ==
nullptr) {
6318 GrfMsg(1,
"FeatureNewName: Attempt to name undefined station 0x{:X}, ignoring",
GB(
id, 0, 8));
6326 if (
GB(
id, 0, 8) >= _cur.
grffile->stations.size() || _cur.
grffile->stations[
GB(
id, 0, 8)] ==
nullptr) {
6327 GrfMsg(1,
"FeatureNewName: Attempt to name undefined station 0x{:X}, ignoring",
GB(
id, 0, 8));
6334 if (
GB(
id, 0, 8) >= _cur.
grffile->airtspec.size() || _cur.
grffile->airtspec[
GB(
id, 0, 8)] ==
nullptr) {
6335 GrfMsg(1,
"FeatureNewName: Attempt to name undefined airport tile 0x{:X}, ignoring",
GB(
id, 0, 8));
6342 if (
GB(
id, 0, 8) >= _cur.
grffile->housespec.size() || _cur.
grffile->housespec[
GB(
id, 0, 8)] ==
nullptr) {
6343 GrfMsg(1,
"FeatureNewName: Attempt to name undefined house 0x{:X}, ignoring.",
GB(
id, 0, 8));
6345 _cur.
grffile->housespec[
GB(
id, 0, 8)]->building_name =
AddGRFString(_cur.
grffile->grfid,
id, lang, new_scheme,
false, name, STR_UNDEFINED);
6350 GrfMsg(7,
"FeatureNewName: Unsupported ID (0x{:04X})",
id);
6369 if (offset >= max_sprites) {
6370 GrfMsg(1,
"GraphicsNew: {} sprite offset must be less than {}, skipping", name, max_sprites);
6371 uint orig_num = num;
6376 if (offset + num > max_sprites) {
6377 GrfMsg(4,
"GraphicsNew: {} sprite overflow, truncating...", name);
6378 uint orig_num = num;
6379 num = std::max(max_sprites - offset, 0);
6380 return orig_num - num;
6394 {
A5BLOCK_ALLOW_OFFSET, SPR_SIGNALS_BASE, 1, PRESIGNAL_SEMAPHORE_AND_PBS_SPRITE_COUNT,
"Signal graphics" },
6396 {
A5BLOCK_ALLOW_OFFSET, SPR_SLOPES_BASE, 1, NORMAL_AND_HALFTILE_FOUNDATION_SPRITE_COUNT,
"Foundation graphics" },
6436 uint8_t type = buf.ReadByte();
6437 uint16_t num = buf.ReadExtendedByte();
6438 uint16_t offset =
HasBit(type, 7) ? buf.ReadExtendedByte() : 0;
6444 GrfMsg(2,
"GraphicsNew: Loading 10 missing shore sprites from extra grf.");
6461 GrfMsg(2,
"GraphicsNew: Custom graphics (type 0x{:02X}) sprite block of length {} (unimplemented, ignoring)", type, num);
6472 GrfMsg(1,
"GraphicsNew: {} (type 0x{:02X}) do not allow an <offset> field. Ignoring offset.", action5_type->
name, type);
6479 GrfMsg(1,
"GraphicsNew: {} (type 0x{:02X}) count must be at least {}. Only {} were specified. Skipping.", action5_type->
name, type, action5_type->
min_sprites, num);
6489 GrfMsg(2,
"GraphicsNew: Replacing sprites {} to {} of {} (type 0x{:02X}) at SpriteID 0x{:04X}", offset, offset + num - 1, action5_type->
name, type, replace);
6501 bool dup_oneway_sprites = ((type == 0x09) && (offset + num <= SPR_ONEWAY_SLOPE_N_OFFSET));
6503 for (; num > 0; num--) {
6505 int load_index = (replace == 0 ? _cur.
spriteid++ : replace++);
6507 if (dup_oneway_sprites) {
6508 DupSprite(load_index, load_index + SPR_ONEWAY_SLOPE_N_OFFSET);
6509 DupSprite(load_index, load_index + SPR_ONEWAY_SLOPE_S_OFFSET);
6525 GrfMsg(3,
"SkipAct5: Skipping {} sprites", _cur.
skip_sprites);
6578 *value = (major << 24) | (minor << 20) | (revision << 16) | build;
6608 *value = _game_mode;
6642 *value =
Clamp(snowline * (grffile->grf_version >= 8 ? 1 :
TILE_HEIGHT), 0, 0xFE);
6651 *value = _openttd_newgrf_version;
6666 default:
return false;
6670 static uint32_t GetParamVal(uint8_t param, uint32_t *cond_val)
6689 if (cond_val ==
nullptr) {
6693 uint32_t index = *cond_val / 0x20;
6709 GrfMsg(1,
"Unsupported in-game variable 0x{:02X}", param);
6730 size_t pos = file.
GetPos();
6736 GrfMsg(2,
"CfgApply: Ignoring (next sprite is real, unsupported)");
6739 file.
SeekTo(pos, SEEK_SET);
6745 std::vector<uint8_t> &preload_sprite = _grf_line_to_action6_sprite_override[location];
6748 if (preload_sprite.empty()) {
6749 preload_sprite.resize(num);
6750 file.
ReadBlock(preload_sprite.data(), num);
6754 file.
SeekTo(pos, SEEK_SET);
6765 param_num = buf.ReadByte();
6766 if (param_num == 0xFF)
break;
6770 param_size = buf.ReadByte();
6774 add_value =
HasBit(param_size, 7);
6775 param_size =
GB(param_size, 0, 7);
6778 offset = buf.ReadExtendedByte();
6782 if (param_num < 0x80 && (param_num + (param_size - 1) / 4) >= _cur.
grffile->
param_end) {
6783 GrfMsg(2,
"CfgApply: Ignoring (param {} not set)", (param_num + (param_size - 1) / 4));
6787 GrfMsg(8,
"CfgApply: Applying {} bytes from parameter 0x{:02X} at offset 0x{:04X}", param_size, param_num, offset);
6790 for (i = 0; i < param_size && offset + i < num; i++) {
6791 uint32_t value = GetParamVal(param_num + i / 4,
nullptr);
6794 if (i % 4 == 0) carry =
false;
6797 uint new_value = preload_sprite[offset + i] +
GB(value, (i % 4) * 8, 8) + (carry ? 1 : 0);
6798 preload_sprite[offset + i] =
GB(new_value, 0, 8);
6800 carry = new_value >= 256;
6802 preload_sprite[offset + i] =
GB(value, (i % 4) * 8, 8);
6834 uint32_t cond_val = 0;
6838 uint8_t param = buf.ReadByte();
6839 uint8_t paramsize = buf.ReadByte();
6840 uint8_t condtype = buf.ReadByte();
6847 switch (paramsize) {
6848 case 8: cond_val = buf.ReadDWord(); mask = buf.ReadDWord();
break;
6849 case 4: cond_val = buf.ReadDWord(); mask = 0xFFFFFFFF;
break;
6850 case 2: cond_val = buf.ReadWord(); mask = 0x0000FFFF;
break;
6851 case 1: cond_val = buf.ReadByte(); mask = 0x000000FF;
break;
6855 if (param < 0x80 && _cur.grffile->param_end <= param) {
6856 GrfMsg(7,
"SkipIf: Param {} undefined, skipping test", param);
6860 GrfMsg(7,
"SkipIf: Test condtype {}, param 0x{:02X}, condval 0x{:08X}", condtype, param, cond_val);
6865 if (condtype >= 0x0B) {
6896 default: GrfMsg(1,
"SkipIf: Unsupported condition type {:02X}. Ignoring", condtype);
return;
6898 }
else if (param == 0x88) {
6908 if (condtype != 10 && c ==
nullptr) {
6909 GrfMsg(7,
"SkipIf: GRFID 0x{:08X} unknown, skipping test",
BSWAP32(cond_val));
6936 default: GrfMsg(1,
"SkipIf: Unsupported GRF condition type {:02X}. Ignoring", condtype);
return;
6940 uint32_t param_val = GetParamVal(param, &cond_val);
6942 case 0x00: result = !!(param_val & (1 << cond_val));
6944 case 0x01: result = !(param_val & (1 << cond_val));
6946 case 0x02: result = (param_val & mask) == cond_val;
6948 case 0x03: result = (param_val & mask) != cond_val;
6950 case 0x04: result = (param_val & mask) < cond_val;
6952 case 0x05: result = (param_val & mask) > cond_val;
6954 default: GrfMsg(1,
"SkipIf: Unsupported condition type {:02X}. Ignoring", condtype);
return;
6959 GrfMsg(2,
"SkipIf: Not skipping sprites, test was false");
6963 uint8_t numsprites = buf.ReadByte();
6971 if (label.label != numsprites)
continue;
6974 if (choice ==
nullptr) choice = &label;
6976 if (label.nfo_line > _cur.
nfo_line) {
6982 if (choice !=
nullptr) {
6983 GrfMsg(2,
"SkipIf: Jumping to label 0x{:X} at line {}, test was true", choice->label, choice->nfo_line);
6989 GrfMsg(2,
"SkipIf: Skipping {} sprites, test was true", numsprites);
7008 uint8_t grf_version = buf.ReadByte();
7009 uint32_t grfid = buf.ReadDWord();
7010 std::string_view name = buf.ReadString();
7014 if (grf_version < 2 || grf_version > 8) {
7024 if (buf.HasData()) {
7025 std::string_view info = buf.ReadString();
7043 uint8_t version = buf.ReadByte();
7044 uint32_t grfid = buf.ReadDWord();
7045 std::string_view name = buf.ReadString();
7048 DisableGrf(STR_NEWGRF_ERROR_MULTIPLE_ACTION_8);
7052 if (_cur.
grffile->grfid != grfid) {
7053 Debug(grf, 0,
"GRFInfo: GRFID {:08X} in FILESCAN stage does not match GRFID {:08X} in INIT/RESERVE/ACTIVATION stage",
BSWAP32(_cur.
grffile->grfid),
BSWAP32(grfid));
7057 _cur.
grffile->grf_version = version;
7072 for (
const auto &grm_sprite : _grm_sprites) {
7073 if (grm_sprite.first.grfid != _cur.
grffile->grfid)
continue;
7074 if (grm_sprite.second.first <= first_sprite && grm_sprite.second.first + grm_sprite.second.second >= first_sprite + num_sprites)
return true;
7090 uint8_t num_sets = buf.ReadByte();
7092 for (uint i = 0; i < num_sets; i++) {
7093 uint8_t num_sprites = buf.ReadByte();
7094 uint16_t first_sprite = buf.ReadWord();
7096 GrfMsg(2,
"SpriteReplace: [Set {}] Changing {} sprites, beginning with {}",
7097 i, num_sprites, first_sprite
7103 GrfMsg(0,
"SpriteReplace: [Set {}] Changing {} sprites, beginning with {}, above limit of {} and not within reserved range, ignoring.",
7112 for (uint j = 0; j < num_sprites; j++) {
7113 int load_index = first_sprite + j;
7119 if (
IsInsideMM(load_index, SPR_ORIGINALSHORE_START, SPR_ORIGINALSHORE_END + 1)) {
7129 uint8_t num_sets = buf.ReadByte();
7131 for (uint i = 0; i < num_sets; i++) {
7138 GrfMsg(3,
"SkipActA: Skipping {} sprites", _cur.
skip_sprites);
7160 STR_NEWGRF_ERROR_VERSION_NUMBER,
7161 STR_NEWGRF_ERROR_DOS_OR_WINDOWS,
7162 STR_NEWGRF_ERROR_UNSET_SWITCH,
7163 STR_NEWGRF_ERROR_INVALID_PARAMETER,
7164 STR_NEWGRF_ERROR_LOAD_BEFORE,
7165 STR_NEWGRF_ERROR_LOAD_AFTER,
7166 STR_NEWGRF_ERROR_OTTD_VERSION_NUMBER,
7170 STR_NEWGRF_ERROR_MSG_INFO,
7171 STR_NEWGRF_ERROR_MSG_WARNING,
7172 STR_NEWGRF_ERROR_MSG_ERROR,
7173 STR_NEWGRF_ERROR_MSG_FATAL
7176 uint8_t severity = buf.ReadByte();
7177 uint8_t lang = buf.ReadByte();
7178 uint8_t message_id = buf.ReadByte();
7181 if (!CheckGrfLangID(lang, _cur.
grffile->grf_version))
return;
7185 if (!
HasBit(severity, 7) && _cur.
stage == GLS_INIT) {
7186 GrfMsg(7,
"GRFLoadError: Skipping non-fatal GRFLoadError in stage {}", _cur.
stage);
7191 if (severity >=
lengthof(sevstr)) {
7192 GrfMsg(7,
"GRFLoadError: Invalid severity id {}. Setting to 2 (non-fatal error).", severity);
7194 }
else if (severity == 3) {
7203 if (message_id >=
lengthof(msgstr) && message_id != 0xFF) {
7204 GrfMsg(7,
"GRFLoadError: Invalid message id.");
7208 if (buf.Remaining() <= 1) {
7209 GrfMsg(7,
"GRFLoadError: No message data supplied.");
7219 if (message_id == 0xFF) {
7221 if (buf.HasData()) {
7222 std::string_view message = buf.ReadString();
7226 GrfMsg(7,
"GRFLoadError: No custom message supplied.");
7230 error->
message = msgstr[message_id];
7233 if (buf.HasData()) {
7234 std::string_view data = buf.ReadString();
7238 GrfMsg(7,
"GRFLoadError: No message data supplied.");
7239 error->
data.clear();
7243 for (uint i = 0; i < error->
param_value.size() && buf.HasData(); i++) {
7244 uint param_number = buf.ReadByte();
7256 if (!buf.HasData())
return;
7258 std::string_view text = buf.ReadString();
7265 uint8_t target = buf.ReadByte();
7268 if (target < 0x80 || target == 0x9E)
return;
7282 static uint32_t GetPatchVariable(uint8_t param)
7292 case 0x0F:
return 0;
7308 case 0x11:
return SPR_2CCMAP_BASE;
7322 uint8_t map_bits = 0;
7325 uint8_t max_edge = std::max(log_X, log_Y);
7327 if (log_X == log_Y) {
7330 if (max_edge == log_Y)
SetBit(map_bits, 1);
7333 return (map_bits << 24) | (std::min(log_X, log_Y) << 20) | (max_edge << 16) |
7334 (log_X << 12) | (log_Y << 8) | (log_X + log_Y);
7343 return SPR_SLOPES_BASE;
7354 GrfMsg(2,
"ParamSet: Unknown Patch variable 0x{:02X}.", param);
7360 static uint32_t PerformGRM(uint32_t *grm, uint16_t num_ids, uint16_t count, uint8_t op, uint8_t target,
const char *type)
7373 for (uint i = start; i < num_ids; i++) {
7377 if (op == 2 || op == 3)
break;
7382 if (size == count)
break;
7385 if (size == count) {
7387 if (op == 0 || op == 3) {
7388 GrfMsg(2,
"ParamSet: GRM: Reserving {} {} at {}", count, type, start);
7389 for (uint i = 0; i < count; i++) grm[start + i] = _cur.
grffile->grfid;
7395 if (op != 4 && op != 5) {
7397 GrfMsg(0,
"ParamSet: GRM: Unable to allocate {} {}, deactivating", count, type);
7402 GrfMsg(1,
"ParamSet: GRM: Unable to allocate {} {}", count, type);
7432 uint8_t target = buf.ReadByte();
7433 uint8_t oper = buf.ReadByte();
7434 uint32_t src1 = buf.ReadByte();
7435 uint32_t src2 = buf.ReadByte();
7438 if (buf.Remaining() >= 4) data = buf.ReadDWord();
7447 if (target < 0x80 && target < _cur.grffile->param_end) {
7448 GrfMsg(7,
"ParamSet: Param {} already defined, skipping", target);
7452 oper =
GB(oper, 0, 7);
7456 if (
GB(data, 0, 8) == 0xFF) {
7457 if (data == 0x0000FFFF) {
7459 src1 = GetPatchVariable(src1);
7463 uint8_t feature =
GB(data, 8, 8);
7464 uint16_t count =
GB(data, 16, 16);
7466 if (_cur.
stage == GLS_RESERVE) {
7467 if (feature == 0x08) {
7471 if (_cur.
spriteid + count >= 16384) {
7472 GrfMsg(0,
"ParamSet: GRM: Unable to allocate {} sprites; try changing NewGRF order", count);
7478 GrfMsg(4,
"ParamSet: GRM: Allocated {} sprites at {}", count, _cur.
spriteid);
7485 }
else if (_cur.
stage == GLS_ACTIVATION) {
7514 GrfMsg(4,
"ParamSet: GRM: Using pre-allocated sprites at {}", src1);
7522 GrfMsg(1,
"ParamSet: GRM: Unsupported operation {} for general sprites", op);
7533 default: GrfMsg(1,
"ParamSet: GRM: Unsupported feature 0x{:X}", feature);
return;
7550 }
else if (src1 == 0xFE) {
7562 src1 = (src1 == 0xFF) ? data : GetParamVal(src1,
nullptr);
7563 src2 = (src2 == 0xFF) ? data : GetParamVal(src2,
nullptr);
7585 res = (int32_t)src1 * (int32_t)src2;
7589 if ((int32_t)src2 < 0) {
7590 res = src1 >> -(int32_t)src2;
7592 res = src1 << (src2 & 0x1F);
7597 if ((int32_t)src2 < 0) {
7598 res = (int32_t)src1 >> -(int32_t)src2;
7600 res = (int32_t)src1 << (src2 & 0x1F);
7624 res = (int32_t)src1 / (int32_t)src2;
7640 res = (int32_t)src1 % (int32_t)src2;
7644 default: GrfMsg(0,
"ParamSet: Unknown operation {}, skipping", oper);
return;
7673 GrfMsg(7,
"ParamSet: Skipping unimplemented target 0x{:02X}", target);
7684 uint32_t safe_bits = 0;
7685 SetBit(safe_bits, GMB_SECOND_ROCKY_TILE_SET);
7694 GrfMsg(7,
"ParamSet: Skipping unimplemented target 0x{:02X}", target);
7698 if (target < 0x80) {
7699 _cur.
grffile->param[target] = res;
7703 GrfMsg(7,
"ParamSet: Skipping unknown target 0x{:02X}", target);
7717 uint8_t num = buf.ReadByte();
7719 for (uint i = 0; i < num; i++) {
7720 uint32_t grfid = buf.ReadDWord();
7742 uint8_t num = buf.ReadByte();
7744 for (uint i = 0; i < num; i++) {
7745 uint32_t grfid = buf.ReadDWord();
7749 if (file !=
nullptr && file != _cur.
grfconfig) {
7750 GrfMsg(2,
"GRFInhibit: Deactivating file '{}'", file->
filename);
7767 uint32_t grfid = _cur.
grffile->grfid;
7771 uint8_t
id = buf.ReadByte();
7772 GrfMsg(6,
"FeatureTownName: definition 0x{:02X}",
id & 0x7F);
7777 bool new_scheme = _cur.
grffile->grf_version >= 7;
7779 uint8_t lang = buf.ReadByte();
7785 std::string_view name = buf.ReadString();
7788 GrfMsg(6,
"FeatureTownName: lang 0x{:X} -> '{}'", lang, lang_name);
7790 style =
AddGRFString(grfid,
id, lang, new_scheme,
false, name, STR_UNDEFINED);
7792 lang = buf.ReadByte();
7793 }
while (lang != 0);
7794 townname->
styles.emplace_back(style,
id);
7797 uint8_t parts = buf.ReadByte();
7798 GrfMsg(6,
"FeatureTownName: {} parts", parts);
7801 for (uint partnum = 0; partnum < parts; partnum++) {
7803 uint8_t texts = buf.ReadByte();
7804 partlist.
bitstart = buf.ReadByte();
7805 partlist.
bitcount = buf.ReadByte();
7807 GrfMsg(6,
"FeatureTownName: part {} contains {} texts and will use GB(seed, {}, {})", partnum, texts, partlist.
bitstart, partlist.
bitcount);
7809 partlist.
parts.reserve(texts);
7810 for (uint textnum = 0; textnum < texts; textnum++) {
7812 part.
prob = buf.ReadByte();
7815 uint8_t ref_id = buf.ReadByte();
7817 GrfMsg(0,
"FeatureTownName: definition 0x{:02X} doesn't exist, deactivating", ref_id);
7818 DelGRFTownName(grfid);
7823 GrfMsg(6,
"FeatureTownName: part {}, text {}, uses intermediate definition 0x{:02X} (with probability {})", partnum, textnum, ref_id, part.
prob & 0x7F);
7825 std::string_view text = buf.ReadString();
7827 GrfMsg(6,
"FeatureTownName: part {}, text {}, '{}' (with probability {})", partnum, textnum, part.
text, part.
prob);
7831 GrfMsg(6,
"FeatureTownName: part {}, total probability {}", partnum, partlist.
maxprob);
7843 uint8_t nfo_label = buf.ReadByte();
7847 GrfMsg(2,
"DefineGotoLabel: GOTO target with label 0x{:02X}", nfo_label);
7861 if (file ==
nullptr || file->sound_offset == 0) {
7862 GrfMsg(1,
"ImportGRFSound: Source file not available");
7866 if (sound_id >= file->num_sounds) {
7867 GrfMsg(1,
"ImportGRFSound: Sound effect {} is invalid", sound_id);
7871 GrfMsg(2,
"ImportGRFSound: Copying sound {} ({}) from file {:x}", sound_id, file->sound_offset + sound_id, grfid);
7873 *sound = *GetSound(file->sound_offset + sound_id);
7876 sound->volume = SOUND_EFFECT_MAX_VOLUME;
7877 sound->priority = 0;
7888 sound->volume = SOUND_EFFECT_MAX_VOLUME;
7889 sound->priority = 0;
7891 if (offs != SIZE_MAX) {
7893 sound->file = _cur.
file;
7894 sound->file_offset = offs;
7906 uint16_t num = buf.ReadWord();
7907 if (num == 0)
return;
7910 if (_cur.
grffile->sound_offset == 0) {
7911 _cur.
grffile->sound_offset = GetNumSounds();
7912 _cur.
grffile->num_sounds = num;
7915 sound = GetSound(_cur.
grffile->sound_offset);
7920 for (
int i = 0; i < num; i++) {
7925 bool invalid = i >= _cur.
grffile->num_sounds;
7927 size_t offs = file.
GetPos();
7932 if (grf_container_version >= 2 && type == 0xFD) {
7935 GrfMsg(1,
"GRFSound: Sound index out of range (multiple Action 11?)");
7937 }
else if (len != 4) {
7938 GrfMsg(1,
"GRFSound: Invalid sprite section import");
7948 GrfMsg(1,
"GRFSound: Unexpected RealSprite found, skipping");
7955 GrfMsg(1,
"GRFSound: Sound index out of range (multiple Action 11?)");
7963 if (_cur.
stage == GLS_INIT) {
7964 if (grf_container_version >= 2) {
7965 GrfMsg(1,
"GRFSound: Inline sounds are not supported for container version >= 2");
7974 if (_cur.
stage == GLS_ACTIVATION) {
7977 if (file.
ReadByte() != 0) GrfMsg(1,
"GRFSound: Import type mismatch");
7985 GrfMsg(1,
"GRFSound: Unexpected Action {:x} found, skipping", action);
8001 GrfMsg(3,
"SkipAct11: Skipping {} sprites", _cur.
skip_sprites);
8014 uint8_t num_def = buf.ReadByte();
8016 for (uint i = 0; i < num_def; i++) {
8018 uint8_t num_char = buf.ReadByte();
8019 uint16_t base_char = buf.ReadWord();
8021 if (size >= FS_END) {
8022 GrfMsg(1,
"LoadFontGlyph: Size {} is not supported, ignoring", size);
8025 GrfMsg(7,
"LoadFontGlyph: Loading {} glyph(s) at 0x{:04X} for size {}", num_char, base_char, size);
8027 for (uint c = 0; c < num_char; c++) {
8045 uint8_t num_def = buf.ReadByte();
8047 for (uint i = 0; i < num_def; i++) {
8058 GrfMsg(3,
"SkipAct12: Skipping {} sprites", _cur.
skip_sprites);
8071 uint32_t grfid = buf.ReadDWord();
8074 GrfMsg(7,
"TranslateGRFStrings: GRFID 0x{:08X} unknown, skipping action 13",
BSWAP32(grfid));
8083 error->
data =
GetString(STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE);
8093 uint8_t language = _cur.
grffile->grf_version >= 8 ? buf.ReadByte() : 0x7F;
8094 uint8_t num_strings = buf.ReadByte();
8095 uint16_t first_id = buf.ReadWord();
8097 if (!((first_id >= 0xD000 && first_id + num_strings <= 0xD400) || (first_id >= 0xD800 && first_id + num_strings <= 0xE000))) {
8098 GrfMsg(7,
"TranslateGRFStrings: Attempting to set out-of-range string IDs in action 13 (first: 0x{:04X}, number: 0x{:02X})", first_id, num_strings);
8102 for (uint i = 0; i < num_strings && buf.HasData(); i++) {
8103 std::string_view
string = buf.ReadString();
8105 if (
string.empty()) {
8106 GrfMsg(7,
"TranslateGRFString: Ignoring empty string.");
8110 AddGRFString(grfid, first_id + i, language,
true,
true,
string, STR_UNDEFINED);
8139 GrfMsg(2,
"StaticGRFInfo: expected only 1 byte for 'INFO'->'NPAR' but got {}, ignoring this field", len);
8151 GrfMsg(2,
"StaticGRFInfo: expected only 1 byte for 'INFO'->'PALS' but got {}, ignoring this field", len);
8154 char data = buf.ReadByte();
8162 GrfMsg(2,
"StaticGRFInfo: unexpected value '{:02X}' for 'INFO'->'PALS', ignoring this field", data);
8177 GrfMsg(2,
"StaticGRFInfo: expected only 1 byte for 'INFO'->'BLTR' but got {}, ignoring this field", len);
8180 char data = buf.ReadByte();
8186 GrfMsg(2,
"StaticGRFInfo: unexpected value '{:02X}' for 'INFO'->'BLTR', ignoring this field", data);
8199 GrfMsg(2,
"StaticGRFInfo: expected 4 bytes for 'INFO'->'VRSN' but got {}, ignoring this field", len);
8212 GrfMsg(2,
"StaticGRFInfo: expected 4 bytes for 'INFO'->'MINV' but got {}, ignoring this field", len);
8217 GrfMsg(2,
"StaticGRFInfo: 'MINV' defined before 'VRSN' or 'VRSN' set to 0, ignoring this field");
8248 GrfMsg(2,
"StaticGRFInfo: expected 1 byte for 'INFO'->'PARA'->'TYPE' but got {}, ignoring this field", len);
8255 GrfMsg(3,
"StaticGRFInfo: unknown parameter type {}, ignoring this field", type);
8265 GrfMsg(2,
"StaticGRFInfo: 'INFO'->'PARA'->'LIMI' is only valid for parameters with type uint/enum, ignoring this field");
8267 }
else if (len != 8) {
8268 GrfMsg(2,
"StaticGRFInfo: expected 8 bytes for 'INFO'->'PARA'->'LIMI' but got {}, ignoring this field", len);
8271 uint32_t min_value = buf.ReadDWord();
8272 uint32_t max_value = buf.ReadDWord();
8273 if (min_value <= max_value) {
8277 GrfMsg(2,
"StaticGRFInfo: 'INFO'->'PARA'->'LIMI' values are incoherent, ignoring this field");
8286 if (len < 1 || len > 3) {
8287 GrfMsg(2,
"StaticGRFInfo: expected 1 to 3 bytes for 'INFO'->'PARA'->'MASK' but got {}, ignoring this field", len);
8290 uint8_t param_nr = buf.ReadByte();
8292 GrfMsg(2,
"StaticGRFInfo: invalid parameter number in 'INFO'->'PARA'->'MASK', param {}, ignoring this field", param_nr);
8308 GrfMsg(2,
"StaticGRFInfo: expected 4 bytes for 'INFO'->'PARA'->'DEFA' but got {}, ignoring this field", len);
8344 this->handler.data = handler;
8356 this->handler.text = handler;
8368 this->handler.call_handler =
true;
8369 this->handler.u.branch = handler;
8381 this->handler.call_handler =
false;
8382 this->handler.u.subtags =
subtags;
8411 uint8_t type = buf.ReadByte();
8413 uint32_t
id = buf.ReadDWord();
8415 GrfMsg(2,
"StaticGRFInfo: all child nodes of 'INFO'->'PARA'->param_num->'VALU' should have type 't' and the value/bit number as id");
8417 type = buf.ReadByte();
8421 uint8_t langid = buf.ReadByte();
8422 std::string_view name_string = buf.ReadString();
8433 type = buf.ReadByte();
8458 uint8_t type = buf.ReadByte();
8460 uint32_t
id = buf.ReadDWord();
8462 GrfMsg(2,
"StaticGRFInfo: all child nodes of 'INFO'->'PARA' should have type 'C' and their parameter number as id");
8464 type = buf.ReadByte();
8477 type = buf.ReadByte();
8514 uint8_t new_type = buf.ReadByte();
8515 while (new_type != 0) {
8518 new_type = buf.ReadByte();
8529 uint16_t size = buf.ReadWord();
8553 while ((tag = &subtags[i++])->type != 0) {
8556 default: NOT_REACHED();
8559 uint8_t langid = buf.ReadByte();
8560 return tag->handler.
text(langid, buf.ReadString());
8564 size_t len = buf.ReadWord();
8565 if (buf.Remaining() < len)
return false;
8566 return tag->handler.
data(len, buf);
8571 return tag->handler.u.branch(buf);
8577 GrfMsg(2,
"StaticGRFInfo: unknown type/id combination found, type={:c}, id={:x}", type,
id);
8589 uint8_t type = buf.ReadByte();
8591 uint32_t
id = buf.ReadDWord();
8592 if (!
HandleNode(type,
id, buf, subtags))
return false;
8593 type = buf.ReadByte();
8714 file->stations.clear();
8722 file->housespec.clear();
8730 file->airportspec.clear();
8731 file->airtspec.clear();
8739 file->industryspec.clear();
8740 file->indtspec.clear();
8748 file->objectspec.clear();
8752 static void ResetCustomRoadStops()
8755 file->roadstops.clear();
8784 CleanUpGRFTownNames();
8844 ResetCustomRoadStops();
8870 _grf_id_overrides.clear();
8872 InitializeSoundPool();
8899 if (!cs->IsValid())
continue;
8919 if (newfile !=
nullptr) {
8925 newfile =
new GRFFile(config);
8943 for (
Price i = PR_BEGIN; i < PR_END; i++) {
8948 std::fill(std::begin(this->railtype_map), std::end(this->railtype_map),
INVALID_RAILTYPE);
8955 std::fill(std::begin(this->roadtype_map), std::end(this->roadtype_map),
INVALID_ROADTYPE);
8959 std::fill(std::begin(this->tramtype_map), std::end(this->tramtype_map),
INVALID_ROADTYPE);
8964 this->param = config->
param;
8976 CargoID cid = GetCargoIDByLabel(label);
8977 if (cid != INVALID_CARGO)
return label;
8989 if (std::holds_alternative<CargoLabel>(label))
return std::get<CargoLabel>(label);
8990 if (std::holds_alternative<MixedCargoType>(label)) {
8991 switch (std::get<MixedCargoType>(label)) {
8995 default: NOT_REACHED();
9006 CargoTypes original_known_cargoes = 0;
9014 bool only_defaultcargo;
9022 if (
_gted[engine].defaultcargo_grf ==
nullptr) {
9025 static constexpr uint8_t T = 1 << LT_TEMPERATE;
9026 static constexpr uint8_t A = 1 << LT_ARCTIC;
9027 static constexpr uint8_t S = 1 << LT_TROPIC;
9028 static constexpr uint8_t Y = 1 << LT_TOYLAND;
9029 static const struct DefaultRefitMasks {
9034 } _default_refit_masks[] = {
9036 {T | A | S , CT_MAIL,
CC_MAIL, 0},
9039 {T | A , CT_COAL,
CC_BULK, 0},
9040 { S , CT_COPPER_ORE,
CC_BULK, 0},
9056 switch (label.base()) {
9060 _gted[engine].cargo_disallowed = 0;
9065 _gted[engine].cargo_disallowed = 0;
9084 _gted[engine].cargo_disallowed = 0;
9088 for (
const auto &drm : _default_refit_masks) {
9090 if (drm.cargo_label != label)
continue;
9092 _gted[engine].cargo_allowed = drm.cargo_allowed;
9093 _gted[engine].cargo_disallowed = drm.cargo_disallowed;
9098 _gted[engine].ctt_exclude_mask = original_known_cargoes;
9101 _gted[engine].UpdateRefittability(
_gted[engine].cargo_allowed != 0);
9108 CargoTypes mask = 0;
9109 CargoTypes not_mask = 0;
9110 CargoTypes xor_mask = ei->refit_mask;
9116 if (
_gted[engine].cargo_allowed != 0) {
9119 if (
_gted[engine].cargo_allowed & cs->classes)
SetBit(mask, cs->Index());
9120 if (
_gted[engine].cargo_disallowed & cs->classes)
SetBit(not_mask, cs->Index());
9124 ei->refit_mask = ((mask & ~not_mask) ^ xor_mask) &
_cargo_mask;
9127 ei->refit_mask |=
_gted[engine].ctt_include_mask;
9128 ei->refit_mask &= ~
_gted[engine].ctt_exclude_mask;
9137 ei->cargo_type = INVALID_CARGO;
9145 if (file ==
nullptr) file = e->
GetGRF();
9146 if (file !=
nullptr && file->grf_version >= 8 && !file->
cargo_list.empty()) {
9148 uint8_t best_local_slot = UINT8_MAX;
9150 uint8_t local_slot = file->
cargo_map[cargo_type];
9151 if (local_slot < best_local_slot) {
9152 best_local_slot = local_slot;
9153 ei->cargo_type = cargo_type;
9183 for (uint i = 0; i < CF_END; i++) {
9195 if (e->
GetGRF() ==
nullptr) {
9198 e->info.
string_id = STR_NEWGRF_INVALID_ENGINE;
9231 default: NOT_REACHED();
9243 if (parent != e->
index)
continue;
9248 GrfMsg(1,
"FinaliseEngineArray: Variant of engine {:x} in '{}' loops back on itself", _engine_mngr[e->
index].internal_id, e->
GetGRF()->filename);
9264 switch (cs.label.base()) {
9266 case CT_MAIL.base(): cs.town_production_effect =
TPE_MAIL;
break;
9267 default: cs.town_production_effect =
TPE_NONE;
break;
9270 if (!cs.IsValid()) {
9271 cs.name = cs.name_single = cs.units_volume = STR_NEWGRF_INVALID_CARGO;
9272 cs.quantifier = STR_NEWGRF_INVALID_CARGO_QUANTITY;
9273 cs.abbrev = STR_NEWGRF_INVALID_CARGO_ABBREV;
9297 if (!filename.empty())
Debug(grf, 1,
"FinaliseHouseArray: {} defines house {} as multitile, but no suitable tiles follow. Disabling house.", filename, hs->
grf_prop.
local_id);
9307 if (!filename.empty())
Debug(grf, 1,
"FinaliseHouseArray: {} defines multitile house {} with non-zero population on additional tiles. Disabling house.", filename, hs->
grf_prop.
local_id);
9315 Debug(grf, 1,
"FinaliseHouseArray: {} defines house {} with different house size then it's substitute type. Disabling house.", filename, hs->
grf_prop.
local_id);
9322 if (!filename.empty())
Debug(grf, 1,
"FinaliseHouseArray: {} defines house {} without a size but marked it as available. Disabling house.", filename, hs->
grf_prop.
local_id);
9345 if (min_year == 0)
return;
9372 if (file->housespec.empty())
continue;
9374 size_t num_houses = file->housespec.size();
9375 for (
size_t i = 0; i < num_houses; i++) {
9376 HouseSpec *hs = file->housespec[i].get();
9378 if (hs ==
nullptr)
continue;
9380 const HouseSpec *next1 = (i + 1 < num_houses ? file->housespec[i + 1].get() :
nullptr);
9381 const HouseSpec *next2 = (i + 2 < num_houses ? file->housespec[i + 2].get() :
nullptr);
9382 const HouseSpec *next3 = (i + 3 < num_houses ? file->housespec[i + 3].get() :
nullptr);
9441 for (
const auto &indsp : file->industryspec) {
9442 if (indsp ==
nullptr || !indsp->
enabled)
continue;
9449 if (strid != STR_UNDEFINED) indsp->
name = strid;
9452 if (strid != STR_UNDEFINED) indsp->
closure_text = strid;
9467 if (strid != STR_UNDEFINED) indsp->
station_name = strid;
9473 for (
const auto &indtsp : file->indtspec) {
9474 if (indtsp !=
nullptr) {
9475 _industile_mngr.SetEntitySpec(indtsp.get());
9480 for (
auto &indsp : _industry_specs) {
9487 indsp.
name = STR_NEWGRF_INVALID_INDUSTRYTYPE;
9491 for (
size_t i = 0; i < std::size(indsp.produced_cargo); ++i) {
9494 for (
size_t i = 0; i < std::size(indsp.
accepts_cargo); ++i) {
9499 for (
auto &indtsp : _industry_tile_specs) {
9515 for (
auto &objectspec : file->objectspec) {
9516 if (objectspec !=
nullptr && objectspec->grf_prop.grffile !=
nullptr && objectspec->IsEnabled()) {
9533 for (
auto &as : file->airportspec) {
9534 if (as !=
nullptr && as->
enabled) {
9535 _airport_mngr.SetEntitySpec(as.get());
9539 for (
auto &ats : file->airtspec) {
9540 if (ats !=
nullptr && ats->enabled) {
9541 _airporttile_mngr.SetEntitySpec(ats.get());
9553 static void DecodeSpecialSprite(uint8_t *buf, uint num, GrfLoadingStage stage)
9567 static const SpecialSpriteHandler handlers[][GLS_END] = {
9568 {
nullptr, SafeChangeInfo,
nullptr,
nullptr, ReserveChangeInfo, FeatureChangeInfo, },
9569 { SkipAct1, SkipAct1, SkipAct1, SkipAct1, SkipAct1, NewSpriteSet, },
9570 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr, NewSpriteGroup, },
9571 {
nullptr,
GRFUnsafe,
nullptr,
nullptr,
nullptr, FeatureMapSpriteGroup, },
9572 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr, FeatureNewName, },
9573 { SkipAct5, SkipAct5, SkipAct5, SkipAct5, SkipAct5, GraphicsNew, },
9574 {
nullptr,
nullptr,
nullptr, CfgApply, CfgApply, CfgApply, },
9575 {
nullptr,
nullptr,
nullptr,
nullptr, SkipIf, SkipIf, },
9576 { ScanInfo,
nullptr,
nullptr, GRFInfo, GRFInfo, GRFInfo, },
9577 {
nullptr,
nullptr,
nullptr, SkipIf, SkipIf, SkipIf, },
9578 { SkipActA, SkipActA, SkipActA, SkipActA, SkipActA, SpriteReplace, },
9579 {
nullptr,
nullptr,
nullptr, GRFLoadError, GRFLoadError, GRFLoadError, },
9580 {
nullptr,
nullptr,
nullptr, GRFComment,
nullptr, GRFComment, },
9582 {
nullptr, SafeGRFInhibit,
nullptr, GRFInhibit, GRFInhibit, GRFInhibit, },
9585 { SkipAct11,
GRFUnsafe, SkipAct11, GRFSound, SkipAct11, GRFSound, },
9588 {
StaticGRFInfo,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr, },
9593 GRFLineToSpriteOverride::iterator it = _grf_line_to_action6_sprite_override.find(location);
9594 if (it == _grf_line_to_action6_sprite_override.end()) {
9600 buf = _grf_line_to_action6_sprite_override[location].data();
9601 GrfMsg(7,
"DecodeSpecialSprite: Using preloaded pseudo sprite data");
9610 uint8_t action = br.ReadByte();
9612 if (action == 0xFF) {
9613 GrfMsg(2,
"DecodeSpecialSprite: Unexpected data block, skipping");
9614 }
else if (action == 0xFE) {
9615 GrfMsg(2,
"DecodeSpecialSprite: Unexpected import block, skipping");
9616 }
else if (action >=
lengthof(handlers)) {
9617 GrfMsg(7,
"DecodeSpecialSprite: Skipping unknown action 0x{:02X}", action);
9618 }
else if (handlers[action][stage] ==
nullptr) {
9619 GrfMsg(7,
"DecodeSpecialSprite: Skipping action 0x{:02X} in stage {}", action, stage);
9621 GrfMsg(7,
"DecodeSpecialSprite: Handling action 0x{:02X} in stage {}", action, stage);
9622 handlers[action][stage](br);
9625 GrfMsg(1,
"DecodeSpecialSprite: Tried to read past end of pseudo-sprite data");
9641 Debug(grf, 2,
"LoadNewGRFFile: Reading NewGRF-file '{}'", config->
filename);
9644 if (grf_container_version == 0) {
9645 Debug(grf, 7,
"LoadNewGRFFile: Custom .grf has invalid format");
9649 if (stage == GLS_INIT || stage == GLS_ACTIVATION) {
9655 if (grf_container_version >= 2) file.
ReadDword();
9658 if (grf_container_version >= 2) {
9660 uint8_t compression = file.
ReadByte();
9661 if (compression != 0) {
9662 Debug(grf, 7,
"LoadNewGRFFile: Unsupported compression format");
9671 if (num == 4 && file.
ReadByte() == 0xFF) {
9674 Debug(grf, 7,
"LoadNewGRFFile: Custom .grf has invalid format");
9682 while ((num = (grf_container_version >= 2 ? file.
ReadDword() : file.
ReadWord())) != 0) {
9688 DecodeSpecialSprite(buf.
Allocate(num), num, stage);
9699 GrfMsg(0,
"LoadNewGRFFile: Unexpected sprite, disabling");
9700 DisableGrf(STR_NEWGRF_ERROR_UNEXPECTED_SPRITE);
9704 if (grf_container_version >= 2 && type == 0xFD) {
9727 const std::string &filename = config->
filename;
9738 if (stage != GLS_FILESCAN && stage != GLS_SAFETYSCAN && stage != GLS_LABELSCAN) {
9740 if (_cur.
grffile ==
nullptr) UserError(
"File '{}' lost in cache.\n", filename);
9747 SpriteFile temporarySpriteFile(filename, subdir, needs_palette_remap);
9801 DupSprite(SPR_ROAD_DEPOT + 0, SPR_TRAMWAY_DEPOT_NO_TRACK + 0);
9802 DupSprite(SPR_TRAMWAY_DEPOT_WITH_TRACK + 1, SPR_TRAMWAY_DEPOT_NO_TRACK + 1);
9803 DupSprite(SPR_ROAD_DEPOT + 2, SPR_TRAMWAY_DEPOT_NO_TRACK + 2);
9804 DupSprite(SPR_TRAMWAY_DEPOT_WITH_TRACK + 3, SPR_TRAMWAY_DEPOT_NO_TRACK + 3);
9805 DupSprite(SPR_TRAMWAY_DEPOT_WITH_TRACK + 4, SPR_TRAMWAY_DEPOT_NO_TRACK + 4);
9806 DupSprite(SPR_TRAMWAY_DEPOT_WITH_TRACK + 5, SPR_TRAMWAY_DEPOT_NO_TRACK + 5);
9817 static const uint32_t override_features = (1 << GSF_TRAINS) | (1 << GSF_ROADVEHICLES) | (1 << GSF_SHIPS) | (1 << GSF_AIRCRAFT);
9821 std::vector<int> grf_overrides(num_grfs, -1);
9822 for (
int i = 0; i < num_grfs; i++) {
9824 auto it = _grf_id_overrides.find(source->grfid);
9825 if (it == std::end(_grf_id_overrides))
continue;
9826 uint32_t
override = it->second;
9829 if (dest ==
nullptr)
continue;
9832 assert(grf_overrides[i] >= 0);
9836 for (
int i = 0; i < num_grfs; i++) {
9837 if (grf_overrides[i] < 0 || grf_overrides[i] >= i)
continue;
9845 for (
Price p = PR_BEGIN; p < PR_END; p++) {
9848 Debug(grf, 3,
"'{}' overrides price base multiplier {} of '{}'", source->filename, p, dest->filename);
9854 for (
int i = num_grfs - 1; i >= 0; i--) {
9855 if (grf_overrides[i] < 0 || grf_overrides[i] <= i)
continue;
9863 for (
Price p = PR_BEGIN; p < PR_END; p++) {
9866 Debug(grf, 3,
"Price base multiplier {} from '{}' propagated to '{}'", p, source->filename, dest->filename);
9872 for (
int i = 0; i < num_grfs; i++) {
9873 if (grf_overrides[i] < 0)
continue;
9881 for (
Price p = PR_BEGIN; p < PR_END; p++) {
9882 if (!
HasBit(features, _price_base_specs[p].grf_feature))
continue;
9884 Debug(grf, 3,
"Price base multiplier {} from '{}' propagated to '{}'", p, dest->filename, source->filename);
9892 if (file->grf_version >= 8)
continue;
9893 PriceMultipliers &price_base_multipliers = file->price_base_multipliers;
9894 for (
Price p = PR_BEGIN; p < PR_END; p++) {
9896 if (fallback_price != INVALID_PRICE && price_base_multipliers[p] == INVALID_PRICE_MODIFIER) {
9899 price_base_multipliers[p] = price_base_multipliers[fallback_price];
9906 PriceMultipliers &price_base_multipliers = file->price_base_multipliers;
9907 for (
Price p = PR_BEGIN; p < PR_END; p++) {
9908 if (price_base_multipliers[p] == INVALID_PRICE_MODIFIER) {
9910 price_base_multipliers[p] = 0;
9915 Debug(grf, 3,
"'{}' sets global price base multiplier {}", file->
filename, p);
9917 price_base_multipliers[p] = 0;
9919 Debug(grf, 3,
"'{}' sets local price base multiplier {}", file->
filename, p);
9937 _grf_line_to_action6_sprite_override.clear();
10000 if (file ==
nullptr ||
_gted[e->
index].roadtramtype == 0) {
10009 if (
_gted[e->
index].roadtramtype < list->size())
10011 RoadTypeLabel rtl = (*list)[
_gted[e->
index].roadtramtype];
10040 _grm_sprites.clear();
10098 for (GrfLoadingStage stage = GLS_LABELSCAN; stage <= GLS_ACTIVATION; stage++) {
10105 if (stage == GLS_RESERVE) {
10106 static const std::pair<uint32_t, uint32_t> default_grf_overrides[] = {
10111 for (
const auto &grf_override : default_grf_overrides) {
10117 uint num_non_static = 0;
10119 _cur.
stage = stage;
10126 Debug(grf, 0,
"NewGRF file is missing '{}'; disabling", c->
filename);
10135 Debug(grf, 0,
"'{}' is not loaded as the maximum number of non-static GRFs has been reached", c->
filename);
10137 c->
error = {STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED};
10146 if (stage == GLS_RESERVE) {
10148 }
else if (stage == GLS_ACTIVATION) {
10153 Debug(sprite, 2,
"LoadNewGRF: Currently {} sprites are loaded", _cur.
spriteid);