28 #include "currency.h" |
28 #include "currency.h" |
29 #include "sound.h" |
29 #include "sound.h" |
30 #include "newgrf_config.h" |
30 #include "newgrf_config.h" |
31 #include "newgrf_sound.h" |
31 #include "newgrf_sound.h" |
32 #include "newgrf_spritegroup.h" |
32 #include "newgrf_spritegroup.h" |
|
33 #include "helpers.hpp" |
33 |
34 |
34 /* TTDPatch extended GRF format codec |
35 /* TTDPatch extended GRF format codec |
35 * (c) Petr Baudis 2004 (GPL'd) |
36 * (c) Petr Baudis 2004 (GPL'd) |
36 * Changes by Florian octo Forster are (c) by the OpenTTD development team. |
37 * Changes by Florian octo Forster are (c) by the OpenTTD development team. |
37 * |
38 * |
540 case 0x11: /* Cost factor */ |
541 case 0x11: /* Cost factor */ |
541 FOR_EACH_OBJECT rvi[i].base_cost = grf_load_byte(&buf); // ?? is it base_cost? |
542 FOR_EACH_OBJECT rvi[i].base_cost = grf_load_byte(&buf); // ?? is it base_cost? |
542 break; |
543 break; |
543 |
544 |
544 case 0x12: /* SFX */ |
545 case 0x12: /* SFX */ |
545 FOR_EACH_OBJECT rvi[i].sfx = grf_load_byte(&buf); |
546 FOR_EACH_OBJECT rvi[i].sfx = (SoundFx)grf_load_byte(&buf); |
546 break; |
547 break; |
547 |
548 |
548 case 0x13: /* Power in 10hp */ |
549 case 0x13: /* Power in 10hp */ |
549 case 0x14: /* Weight in 1/4 tons */ |
550 case 0x14: /* Weight in 1/4 tons */ |
550 case 0x15: /* Speed in mph*0.8 */ |
551 case 0x15: /* Speed in mph*0.8 */ |
839 grfmsg(1, "StationChangeInfo: Station %u is invalid, max %u, ignoring", stid + numinfo, MAX_STATIONS); |
840 grfmsg(1, "StationChangeInfo: Station %u is invalid, max %u, ignoring", stid + numinfo, MAX_STATIONS); |
840 return false; |
841 return false; |
841 } |
842 } |
842 |
843 |
843 /* Allocate station specs if necessary */ |
844 /* Allocate station specs if necessary */ |
844 if (_cur_grffile->stations == NULL) _cur_grffile->stations = calloc(MAX_STATIONS, sizeof(*_cur_grffile->stations)); |
845 if (_cur_grffile->stations == NULL) CallocT(&_cur_grffile->stations, MAX_STATIONS); |
845 |
846 |
846 statspec = &_cur_grffile->stations[stid]; |
847 statspec = &_cur_grffile->stations[stid]; |
847 |
848 |
848 if (prop != 0x08) { |
849 if (prop != 0x08) { |
849 /* Check that all stations we are modifying are defined. */ |
850 /* Check that all stations we are modifying are defined. */ |
859 case 0x08: /* Class ID */ |
860 case 0x08: /* Class ID */ |
860 FOR_EACH_OBJECT { |
861 FOR_EACH_OBJECT { |
861 uint32 classid; |
862 uint32 classid; |
862 |
863 |
863 /* Property 0x08 is special; it is where the station is allocated */ |
864 /* Property 0x08 is special; it is where the station is allocated */ |
864 if (statspec[i] == NULL) statspec[i] = calloc(1, sizeof(*statspec[i])); |
865 if (statspec[i] == NULL) CallocT(&statspec[i], 1); |
865 |
866 |
866 /* Swap classid because we read it in BE meaning WAYP or DFLT */ |
867 /* Swap classid because we read it in BE meaning WAYP or DFLT */ |
867 classid = grf_load_dword(&buf); |
868 classid = grf_load_dword(&buf); |
868 statspec[i]->sclass = AllocateStationClass(BSWAP32(classid)); |
869 statspec[i]->sclass = AllocateStationClass(BSWAP32(classid)); |
869 } |
870 } |
873 FOR_EACH_OBJECT { |
874 FOR_EACH_OBJECT { |
874 StationSpec *statspec = _cur_grffile->stations[stid + i]; |
875 StationSpec *statspec = _cur_grffile->stations[stid + i]; |
875 uint t; |
876 uint t; |
876 |
877 |
877 statspec->tiles = grf_load_extended(&buf); |
878 statspec->tiles = grf_load_extended(&buf); |
878 statspec->renderdata = calloc(statspec->tiles, sizeof(*statspec->renderdata)); |
879 CallocT(&statspec->renderdata, statspec->tiles); |
879 statspec->copied_renderdata = false; |
880 statspec->copied_renderdata = false; |
880 |
881 |
881 for (t = 0; t < statspec->tiles; t++) { |
882 for (t = 0; t < statspec->tiles; t++) { |
882 DrawTileSprites *dts = &statspec->renderdata[t]; |
883 DrawTileSprites *dts = &statspec->renderdata[t]; |
883 uint seq_count = 0; |
884 uint seq_count = 0; |
888 |
889 |
889 while (buf < *bufp + len) { |
890 while (buf < *bufp + len) { |
890 DrawTileSeqStruct *dtss; |
891 DrawTileSeqStruct *dtss; |
891 |
892 |
892 // no relative bounding box support |
893 // no relative bounding box support |
893 dts->seq = realloc((void*)dts->seq, ++seq_count * sizeof(DrawTileSeqStruct)); |
894 ReallocT((DrawTileSeqStruct**)&dts->seq, ++seq_count); |
894 dtss = (DrawTileSeqStruct*) &dts->seq[seq_count - 1]; |
895 dtss = (DrawTileSeqStruct*) &dts->seq[seq_count - 1]; |
895 |
896 |
896 dtss->delta_x = grf_load_byte(&buf); |
897 dtss->delta_x = grf_load_byte(&buf); |
897 if ((byte) dtss->delta_x == 0x80) break; |
898 if ((byte) dtss->delta_x == 0x80) break; |
898 dtss->delta_y = grf_load_byte(&buf); |
899 dtss->delta_y = grf_load_byte(&buf); |
954 |
955 |
955 if (length == 0 || number == 0) break; |
956 if (length == 0 || number == 0) break; |
956 |
957 |
957 //debug("l %d > %d ?", length, stat->lengths); |
958 //debug("l %d > %d ?", length, stat->lengths); |
958 if (length > statspec->lengths) { |
959 if (length > statspec->lengths) { |
959 statspec->platforms = realloc(statspec->platforms, length); |
960 ReallocT(&statspec->platforms, length); |
960 memset(statspec->platforms + statspec->lengths, 0, length - statspec->lengths); |
961 memset(statspec->platforms + statspec->lengths, 0, length - statspec->lengths); |
961 |
962 |
962 statspec->layouts = realloc(statspec->layouts, length * sizeof(*statspec->layouts)); |
963 ReallocT(&statspec->layouts, length); |
963 memset(statspec->layouts + statspec->lengths, 0, |
964 memset(statspec->layouts + statspec->lengths, 0, |
964 (length - statspec->lengths) * sizeof(*statspec->layouts)); |
965 (length - statspec->lengths) * sizeof(*statspec->layouts)); |
965 |
966 |
966 statspec->lengths = length; |
967 statspec->lengths = length; |
967 } |
968 } |
968 l = length - 1; // index is zero-based |
969 l = length - 1; // index is zero-based |
969 |
970 |
970 //debug("p %d > %d ?", number, stat->platforms[l]); |
971 //debug("p %d > %d ?", number, stat->platforms[l]); |
971 if (number > statspec->platforms[l]) { |
972 if (number > statspec->platforms[l]) { |
972 statspec->layouts[l] = realloc(statspec->layouts[l], |
973 ReallocT(&statspec->layouts[l], number); |
973 number * sizeof(**statspec->layouts)); |
|
974 // We expect NULL being 0 here, but C99 guarantees that. |
974 // We expect NULL being 0 here, but C99 guarantees that. |
975 memset(statspec->layouts[l] + statspec->platforms[l], 0, |
975 memset(statspec->layouts[l] + statspec->platforms[l], 0, |
976 (number - statspec->platforms[l]) * sizeof(**statspec->layouts)); |
976 (number - statspec->platforms[l]) * sizeof(**statspec->layouts)); |
977 |
977 |
978 statspec->platforms[l] = number; |
978 statspec->platforms[l] = number; |
979 } |
979 } |
980 |
980 |
981 p = 0; |
981 p = 0; |
982 layout = malloc(length * number); |
982 MallocT(&layout, length * number); |
983 for (l = 0; l < length; l++) { |
983 for (l = 0; l < length; l++) { |
984 for (p = 0; p < number; p++) { |
984 for (p = 0; p < number; p++) { |
985 layout[l * number + p] = grf_load_byte(&buf); |
985 layout[l * number + p] = grf_load_byte(&buf); |
986 } |
986 } |
987 } |
987 } |
1073 byte tableid = grf_load_byte(&buf); |
1073 byte tableid = grf_load_byte(&buf); |
1074 byte numtables = grf_load_byte(&buf); |
1074 byte numtables = grf_load_byte(&buf); |
1075 |
1075 |
1076 if (bridge->sprite_table == NULL) { |
1076 if (bridge->sprite_table == NULL) { |
1077 /* Allocate memory for sprite table pointers and zero out */ |
1077 /* Allocate memory for sprite table pointers and zero out */ |
1078 bridge->sprite_table = calloc(7, sizeof(*bridge->sprite_table)); |
1078 CallocT(bridge->sprite_table, 7); |
1079 } |
1079 } |
1080 |
1080 |
1081 for (; numtables-- != 0; tableid++) { |
1081 for (; numtables-- != 0; tableid++) { |
1082 byte sprite; |
1082 byte sprite; |
1083 |
1083 |
1593 setid = grf_load_byte(&buf); |
1593 setid = grf_load_byte(&buf); |
1594 type = grf_load_byte(&buf); |
1594 type = grf_load_byte(&buf); |
1595 |
1595 |
1596 if (setid >= _cur_grffile->spritegroups_count) { |
1596 if (setid >= _cur_grffile->spritegroups_count) { |
1597 // Allocate memory for new sprite group references. |
1597 // Allocate memory for new sprite group references. |
1598 _cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, (setid + 1) * sizeof(*_cur_grffile->spritegroups)); |
1598 ReallocT(&_cur_grffile->spritegroups, setid + 1); |
1599 // Initialise new space to NULL |
1599 // Initialise new space to NULL |
1600 for (; _cur_grffile->spritegroups_count < (setid + 1); _cur_grffile->spritegroups_count++) |
1600 for (; _cur_grffile->spritegroups_count < (setid + 1); _cur_grffile->spritegroups_count++) |
1601 _cur_grffile->spritegroups[_cur_grffile->spritegroups_count] = NULL; |
1601 _cur_grffile->spritegroups[_cur_grffile->spritegroups_count] = NULL; |
1602 } |
1602 } |
1603 |
1603 |
1638 if (group->g.determ.num_adjusts > 0) { |
1638 if (group->g.determ.num_adjusts > 0) { |
1639 check_length(bufend - buf, 2 + varsize + 3, "NewSpriteGroup (Deterministic) (3)"); |
1639 check_length(bufend - buf, 2 + varsize + 3, "NewSpriteGroup (Deterministic) (3)"); |
1640 } |
1640 } |
1641 |
1641 |
1642 group->g.determ.num_adjusts++; |
1642 group->g.determ.num_adjusts++; |
1643 group->g.determ.adjusts = realloc(group->g.determ.adjusts, group->g.determ.num_adjusts * sizeof(*group->g.determ.adjusts)); |
1643 ReallocT(&group->g.determ.adjusts, group->g.determ.num_adjusts); |
1644 |
1644 |
1645 adjust = &group->g.determ.adjusts[group->g.determ.num_adjusts - 1]; |
1645 adjust = &group->g.determ.adjusts[group->g.determ.num_adjusts - 1]; |
1646 |
1646 |
1647 /* The first var adjust doesn't have an operation specified, so we set it to add. */ |
1647 /* The first var adjust doesn't have an operation specified, so we set it to add. */ |
1648 adjust->operation = group->g.determ.num_adjusts == 1 ? DSGA_OP_ADD : grf_load_byte(&buf); |
1648 adjust->operation = group->g.determ.num_adjusts == 1 ? DSGA_OP_ADD : (DeterministicSpriteGroupAdjustOperation)grf_load_byte(&buf); |
1649 adjust->variable = grf_load_byte(&buf); |
1649 adjust->variable = grf_load_byte(&buf); |
1650 adjust->parameter = IS_BYTE_INSIDE(adjust->variable, 0x60, 0x80) ? grf_load_byte(&buf) : 0; |
1650 adjust->parameter = IS_BYTE_INSIDE(adjust->variable, 0x60, 0x80) ? grf_load_byte(&buf) : 0; |
1651 |
1651 |
1652 varadjust = grf_load_byte(&buf); |
1652 varadjust = grf_load_byte(&buf); |
1653 adjust->shift_num = GB(varadjust, 0, 5); |
1653 adjust->shift_num = GB(varadjust, 0, 5); |
1654 adjust->type = GB(varadjust, 6, 2); |
1654 adjust->type = (DeterministicSpriteGroupAdjustType)GB(varadjust, 6, 2); |
1655 adjust->and_mask = grf_load_var(varsize, &buf); |
1655 adjust->and_mask = grf_load_var(varsize, &buf); |
1656 |
1656 |
1657 if (adjust->type != DSGA_TYPE_NONE) { |
1657 if (adjust->type != DSGA_TYPE_NONE) { |
1658 adjust->add_val = grf_load_var(varsize, &buf); |
1658 adjust->add_val = grf_load_var(varsize, &buf); |
1659 adjust->divmod_val = grf_load_var(varsize, &buf); |
1659 adjust->divmod_val = grf_load_var(varsize, &buf); |
1664 |
1664 |
1665 /* Continue reading var adjusts while bit 5 is set. */ |
1665 /* Continue reading var adjusts while bit 5 is set. */ |
1666 } while (HASBIT(varadjust, 5)); |
1666 } while (HASBIT(varadjust, 5)); |
1667 |
1667 |
1668 group->g.determ.num_ranges = grf_load_byte(&buf); |
1668 group->g.determ.num_ranges = grf_load_byte(&buf); |
1669 group->g.determ.ranges = calloc(group->g.determ.num_ranges, sizeof(*group->g.determ.ranges)); |
1669 CallocT(&group->g.determ.ranges, group->g.determ.num_ranges); |
1670 |
1670 |
1671 check_length(bufend - buf, 2 + (2 + 2 * varsize) * group->g.determ.num_ranges, "NewSpriteGroup (Deterministic)"); |
1671 check_length(bufend - buf, 2 + (2 + 2 * varsize) * group->g.determ.num_ranges, "NewSpriteGroup (Deterministic)"); |
1672 |
1672 |
1673 for (i = 0; i < group->g.determ.num_ranges; i++) { |
1673 for (i = 0; i < group->g.determ.num_ranges; i++) { |
1674 group->g.determ.ranges[i].group = GetGroupFromGroupID(setid, type, grf_load_word(&buf)); |
1674 group->g.determ.ranges[i].group = GetGroupFromGroupID(setid, type, grf_load_word(&buf)); |
1696 triggers = grf_load_byte(&buf); |
1696 triggers = grf_load_byte(&buf); |
1697 group->g.random.triggers = GB(triggers, 0, 7); |
1697 group->g.random.triggers = GB(triggers, 0, 7); |
1698 group->g.random.cmp_mode = HASBIT(triggers, 7) ? RSG_CMP_ALL : RSG_CMP_ANY; |
1698 group->g.random.cmp_mode = HASBIT(triggers, 7) ? RSG_CMP_ALL : RSG_CMP_ANY; |
1699 group->g.random.lowest_randbit = grf_load_byte(&buf); |
1699 group->g.random.lowest_randbit = grf_load_byte(&buf); |
1700 group->g.random.num_groups = grf_load_byte(&buf); |
1700 group->g.random.num_groups = grf_load_byte(&buf); |
1701 group->g.random.groups = calloc(group->g.random.num_groups, sizeof(*group->g.random.groups)); |
1701 CallocT(&group->g.random.groups, group->g.random.num_groups); |
1702 |
1702 |
1703 check_length(bufend - buf, 2 * group->g.random.num_groups, "NewSpriteGroup (Randomized) (2)"); |
1703 check_length(bufend - buf, 2 * group->g.random.num_groups, "NewSpriteGroup (Randomized) (2)"); |
1704 |
1704 |
1705 for (i = 0; i < group->g.random.num_groups; i++) { |
1705 for (i = 0; i < group->g.random.num_groups; i++) { |
1706 group->g.random.groups[i] = GetGroupFromGroupID(setid, type, grf_load_word(&buf)); |
1706 group->g.random.groups[i] = GetGroupFromGroupID(setid, type, grf_load_word(&buf)); |
1736 group = AllocateSpriteGroup(); |
1736 group = AllocateSpriteGroup(); |
1737 group->type = SGT_REAL; |
1737 group->type = SGT_REAL; |
1738 |
1738 |
1739 group->g.real.num_loaded = num_loaded; |
1739 group->g.real.num_loaded = num_loaded; |
1740 group->g.real.num_loading = num_loading; |
1740 group->g.real.num_loading = num_loading; |
1741 if (num_loaded > 0) group->g.real.loaded = calloc(num_loaded, sizeof(*group->g.real.loaded)); |
1741 if (num_loaded > 0) CallocT(&group->g.real.loaded, num_loaded); |
1742 if (num_loading > 0) group->g.real.loading = calloc(num_loading, sizeof(*group->g.real.loading)); |
1742 if (num_loading > 0) CallocT(&group->g.real.loading, num_loading); |
1743 |
1743 |
1744 grfmsg(6, "NewSpriteGroup: New SpriteGroup 0x%02X, %u views, %u loaded, %u loading", |
1744 grfmsg(6, "NewSpriteGroup: New SpriteGroup 0x%02X, %u views, %u loaded, %u loading", |
1745 setid, sprites, num_loaded, num_loading); |
1745 setid, sprites, num_loaded, num_loading); |
1746 |
1746 |
1747 for (i = 0; i < num_loaded; i++) { |
1747 for (i = 0; i < num_loaded; i++) { |
2272 uint16 num = FioReadWord(); |
2272 uint16 num = FioReadWord(); |
2273 uint8 type = FioReadByte(); |
2273 uint8 type = FioReadByte(); |
2274 |
2274 |
2275 /* Check if the sprite is a pseudo sprite. We can't operate on real sprites. */ |
2275 /* Check if the sprite is a pseudo sprite. We can't operate on real sprites. */ |
2276 if (type == 0xFF) { |
2276 if (type == 0xFF) { |
2277 _preload_sprite = malloc(num); |
2277 MallocT(&_preload_sprite, num); |
2278 FioReadBlock(_preload_sprite, num); |
2278 FioReadBlock(_preload_sprite, num); |
2279 } |
2279 } |
2280 |
2280 |
2281 /* Reset the file position to the start of the next sprite */ |
2281 /* Reset the file position to the start of the next sprite */ |
2282 FioSeekTo(pos, SEEK_SET); |
2282 FioSeekTo(pos, SEEK_SET); |
3196 num_def = grf_load_byte(&buf); |
3196 num_def = grf_load_byte(&buf); |
3197 |
3197 |
3198 check_length(len, 1 + num_def * 4, "LoadFontGlyph"); |
3198 check_length(len, 1 + num_def * 4, "LoadFontGlyph"); |
3199 |
3199 |
3200 for (i = 0; i < num_def; i++) { |
3200 for (i = 0; i < num_def; i++) { |
3201 FontSize size = grf_load_byte(&buf); |
3201 FontSize size = (FontSize)grf_load_byte(&buf); |
3202 uint8 num_char = grf_load_byte(&buf); |
3202 uint8 num_char = grf_load_byte(&buf); |
3203 uint16 base_char = grf_load_word(&buf); |
3203 uint16 base_char = grf_load_word(&buf); |
3204 uint c; |
3204 uint c; |
3205 |
3205 |
3206 grfmsg(7, "LoadFontGlyph: Loading %u glyph(s) at 0x%04X for size %u", num_char, base_char, size); |
3206 grfmsg(7, "LoadFontGlyph: Loading %u glyph(s) at 0x%04X for size %u", num_char, base_char, size); |
3487 newfile->sprite_offset = sprite_offset; |
3487 newfile->sprite_offset = sprite_offset; |
3488 _cur_grffile = newfile; |
3488 _cur_grffile = newfile; |
3489 return; |
3489 return; |
3490 } |
3490 } |
3491 |
3491 |
3492 newfile = calloc(1, sizeof(*newfile)); |
3492 CallocT(&newfile, 1); |
3493 |
3493 |
3494 if (newfile == NULL) error ("Out of memory"); |
3494 if (newfile == NULL) error ("Out of memory"); |
3495 |
3495 |
3496 newfile->filename = strdup(config->filename); |
3496 newfile->filename = strdup(config->filename); |
3497 newfile->sprite_offset = sprite_offset; |
3497 newfile->sprite_offset = sprite_offset; |
3615 byte action; |
3615 byte action; |
3616 |
3616 |
3617 if (_preload_sprite == NULL) { |
3617 if (_preload_sprite == NULL) { |
3618 /* No preloaded sprite to work with; allocate and read the |
3618 /* No preloaded sprite to work with; allocate and read the |
3619 * pseudo sprite content. */ |
3619 * pseudo sprite content. */ |
3620 buf = malloc(num); |
3620 MallocT(&buf, num); |
3621 if (buf == NULL) error("DecodeSpecialSprite: Could not allocate memory"); |
3621 if (buf == NULL) error("DecodeSpecialSprite: Could not allocate memory"); |
3622 FioReadBlock(buf, num); |
3622 FioReadBlock(buf, num); |
3623 } else { |
3623 } else { |
3624 /* Use the preloaded sprite data. */ |
3624 /* Use the preloaded sprite data. */ |
3625 buf = _preload_sprite; |
3625 buf = _preload_sprite; |