src/newgrf.cpp
changeset 6607 b4f6595855c4
parent 6606 2e867a1c6b7f
child 6608 80bc656cc6fb
equal deleted inserted replaced
6606:2e867a1c6b7f 6607:b4f6595855c4
    98 };
    98 };
    99 
    99 
   100 
   100 
   101 typedef void (*SpecialSpriteHandler)(byte *buf, int len);
   101 typedef void (*SpecialSpriteHandler)(byte *buf, int len);
   102 
   102 
   103 static const int _vehcounts[4] = {
   103 static const uint _vehcounts[4] = {
   104 	/* GSF_TRAIN */    NUM_TRAIN_ENGINES,
   104 	/* GSF_TRAIN */    NUM_TRAIN_ENGINES,
   105 	/* GSF_ROAD */     NUM_ROAD_ENGINES,
   105 	/* GSF_ROAD */     NUM_ROAD_ENGINES,
   106 	/* GSF_SHIP */     NUM_SHIP_ENGINES,
   106 	/* GSF_SHIP */     NUM_SHIP_ENGINES,
   107 	/* GSF_AIRCRAFT */ NUM_AIRCRAFT_ENGINES
   107 	/* GSF_AIRCRAFT */ NUM_AIRCRAFT_ENGINES
   108 };
   108 };
   109 
   109 
   110 static const int _vehshifts[4] = {
   110 static const uint _vehshifts[4] = {
   111 	/* GSF_TRAIN */    0,
   111 	/* GSF_TRAIN */    0,
   112 	/* GSF_ROAD */     ROAD_ENGINES_INDEX,
   112 	/* GSF_ROAD */     ROAD_ENGINES_INDEX,
   113 	/* GSF_SHIP */     SHIP_ENGINES_INDEX,
   113 	/* GSF_SHIP */     SHIP_ENGINES_INDEX,
   114 	/* GSF_AIRCRAFT */ AIRCRAFT_ENGINES_INDEX,
   114 	/* GSF_AIRCRAFT */ AIRCRAFT_ENGINES_INDEX,
   115 };
   115 };
   879 	}
   879 	}
   880 
   880 
   881 	switch (prop) {
   881 	switch (prop) {
   882 		case 0x08: /* Class ID */
   882 		case 0x08: /* Class ID */
   883 			FOR_EACH_OBJECT {
   883 			FOR_EACH_OBJECT {
   884 				uint32 classid;
       
   885 
       
   886 				/* Property 0x08 is special; it is where the station is allocated */
   884 				/* Property 0x08 is special; it is where the station is allocated */
   887 				if (statspec[i] == NULL) statspec[i] = CallocT<StationSpec>(1);
   885 				if (statspec[i] == NULL) statspec[i] = CallocT<StationSpec>(1);
   888 
   886 
   889 				/* Swap classid because we read it in BE meaning WAYP or DFLT */
   887 				/* Swap classid because we read it in BE meaning WAYP or DFLT */
   890 				classid = grf_load_dword(&buf);
   888 				uint32 classid = grf_load_dword(&buf);
   891 				statspec[i]->sclass = AllocateStationClass(BSWAP32(classid));
   889 				statspec[i]->sclass = AllocateStationClass(BSWAP32(classid));
   892 			}
   890 			}
   893 			break;
   891 			break;
   894 
   892 
   895 		case 0x09: /* Define sprite layout */
   893 		case 0x09: /* Define sprite layout */
   896 			FOR_EACH_OBJECT {
   894 			FOR_EACH_OBJECT {
   897 				StationSpec *statspec = _cur_grffile->stations[stid + i];
   895 				StationSpec *statspec = _cur_grffile->stations[stid + i];
   898 				uint t;
       
   899 
   896 
   900 				statspec->tiles = grf_load_extended(&buf);
   897 				statspec->tiles = grf_load_extended(&buf);
   901 				statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
   898 				statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
   902 				statspec->copied_renderdata = false;
   899 				statspec->copied_renderdata = false;
   903 
   900 
   904 				for (t = 0; t < statspec->tiles; t++) {
   901 				for (uint t = 0; t < statspec->tiles; t++) {
   905 					DrawTileSprites *dts = &statspec->renderdata[t];
   902 					DrawTileSprites *dts = &statspec->renderdata[t];
   906 					uint seq_count = 0;
   903 					uint seq_count = 0;
   907 
   904 
   908 					dts->seq = NULL;
   905 					dts->seq = NULL;
   909 					dts->ground_sprite = grf_load_word(&buf);
   906 					dts->ground_sprite = grf_load_word(&buf);
  1125 					/* Allocate memory for sprite table pointers and zero out */
  1122 					/* Allocate memory for sprite table pointers and zero out */
  1126 					bridge->sprite_table = CallocT<PalSpriteID*>(7);
  1123 					bridge->sprite_table = CallocT<PalSpriteID*>(7);
  1127 				}
  1124 				}
  1128 
  1125 
  1129 				for (; numtables-- != 0; tableid++) {
  1126 				for (; numtables-- != 0; tableid++) {
  1130 					byte sprite;
       
  1131 
       
  1132 					if (tableid >= 7) { // skip invalid data
  1127 					if (tableid >= 7) { // skip invalid data
  1133 						grfmsg(1, "BridgeChangeInfo: Table %d >= 7, skipping", tableid);
  1128 						grfmsg(1, "BridgeChangeInfo: Table %d >= 7, skipping", tableid);
  1134 						for (sprite = 0; sprite < 32; sprite++) grf_load_dword(&buf);
  1129 						for (byte sprite = 0; sprite < 32; sprite++) grf_load_dword(&buf);
  1135 						continue;
  1130 						continue;
  1136 					}
  1131 					}
  1137 
  1132 
  1138 					if (bridge->sprite_table[tableid] == NULL) {
  1133 					if (bridge->sprite_table[tableid] == NULL) {
  1139 						bridge->sprite_table[tableid] = MallocT<PalSpriteID>(32);
  1134 						bridge->sprite_table[tableid] = MallocT<PalSpriteID>(32);
  1140 					}
  1135 					}
  1141 
  1136 
  1142 					for (sprite = 0; sprite < 32; sprite++) {
  1137 					for (byte sprite = 0; sprite < 32; sprite++) {
  1143 						SpriteID image = grf_load_word(&buf);
  1138 						SpriteID image = grf_load_word(&buf);
  1144 						SpriteID pal   = grf_load_word(&buf);
  1139 						SpriteID pal   = grf_load_word(&buf);
  1145 
  1140 
  1146 						if (HASBIT(pal, 15)) {
  1141 						if (HASBIT(pal, 15)) {
  1147 							SETBIT(image, PALETTE_MODIFIER_TRANSPARENT);
  1142 							SETBIT(image, PALETTE_MODIFIER_TRANSPARENT);
  1386 		/* GSF_INDUSTRIES */   NULL,
  1381 		/* GSF_INDUSTRIES */   NULL,
  1387 		/* GSF_CARGOS */       NULL,
  1382 		/* GSF_CARGOS */       NULL,
  1388 		/* GSF_SOUNDFX */      SoundEffectChangeInfo,
  1383 		/* GSF_SOUNDFX */      SoundEffectChangeInfo,
  1389 	};
  1384 	};
  1390 
  1385 
  1391 	uint8 feature;
       
  1392 	uint8 numprops;
       
  1393 	uint8 numinfo;
       
  1394 	byte engine;
       
  1395 	EngineInfo *ei = NULL;
  1386 	EngineInfo *ei = NULL;
  1396 
  1387 
  1397 	if (len == 1) {
  1388 	if (len == 1) {
  1398 		grfmsg(8, "Silently ignoring one-byte special sprite 0x00");
  1389 		grfmsg(8, "Silently ignoring one-byte special sprite 0x00");
  1399 		return;
  1390 		return;
  1400 	}
  1391 	}
  1401 
  1392 
  1402 	if (!check_length(len, 6, "FeatureChangeInfo")) return;
  1393 	if (!check_length(len, 6, "FeatureChangeInfo")) return;
  1403 	buf++;
  1394 	buf++;
  1404 	feature  = grf_load_byte(&buf);
  1395 	uint8 feature  = grf_load_byte(&buf);
  1405 	numprops = grf_load_byte(&buf);
  1396 	uint8 numprops = grf_load_byte(&buf);
  1406 	numinfo  = grf_load_byte(&buf);
  1397 	uint8 numinfo  = grf_load_byte(&buf);
  1407 	engine   = grf_load_byte(&buf);
  1398 	uint8 engine   = grf_load_byte(&buf);
  1408 
  1399 
  1409 	grfmsg(6, "FeatureChangeInfo: feature %d, %d properties, to apply to %d+%d",
  1400 	grfmsg(6, "FeatureChangeInfo: feature %d, %d properties, to apply to %d+%d",
  1410 	               feature, numprops, engine, numinfo);
  1401 	               feature, numprops, engine, numinfo);
  1411 
  1402 
  1412 	if (feature >= lengthof(handler) || handler[feature] == NULL) {
  1403 	if (feature >= lengthof(handler) || handler[feature] == NULL) {
  1459 						 * credit ;-). --pasky */
  1450 						 * credit ;-). --pasky */
  1460 						FOR_EACH_OBJECT ei[i].load_amount = grf_load_byte(&buf);
  1451 						FOR_EACH_OBJECT ei[i].load_amount = grf_load_byte(&buf);
  1461 						break;
  1452 						break;
  1462 
  1453 
  1463 					default:
  1454 					default:
  1464 						if (handler[feature](engine, numinfo, prop, &buf, bufend - buf))
  1455 						if (handler[feature](engine, numinfo, prop, &buf, bufend - buf)) {
  1465 							ignoring = true;
  1456 							ignoring = true;
       
  1457 						}
  1466 						break;
  1458 						break;
  1467 				}
  1459 				}
  1468 				break;
  1460 				break;
  1469 
  1461 
  1470 			default:
  1462 			default:
  1471 				if (handler[feature](engine, numinfo, prop, &buf, bufend - buf))
  1463 				if (handler[feature](engine, numinfo, prop, &buf, bufend - buf)) {
  1472 					ignoring = true;
  1464 					ignoring = true;
       
  1465 				}
  1473 				break;
  1466 				break;
  1474 		}
  1467 		}
  1475 
  1468 
  1476 		if (ignoring) grfmsg(2, "FeatureChangeInfo: Ignoring property 0x%02X (not implemented)", prop);
  1469 		if (ignoring) grfmsg(2, "FeatureChangeInfo: Ignoring property 0x%02X (not implemented)", prop);
  1477 	}
  1470 	}
  1478 }
  1471 }
  1479 
  1472 
  1480 /* Action 0x00 (GLS_SAFETYSCAN) */
  1473 /* Action 0x00 (GLS_SAFETYSCAN) */
  1481 static void SafeChangeInfo(byte *buf, int len)
  1474 static void SafeChangeInfo(byte *buf, int len)
  1482 {
  1475 {
  1483 	uint8 feature;
       
  1484 	uint8 numprops;
       
  1485 	uint8 numinfo;
       
  1486 	uint8 index;
       
  1487 
       
  1488 	if (len == 1) {
  1476 	if (len == 1) {
  1489 		grfmsg(8, "Silently ignoring one-byte special sprite 0x00");
  1477 		grfmsg(8, "Silently ignoring one-byte special sprite 0x00");
  1490 		return;
  1478 		return;
  1491 	}
  1479 	}
  1492 
  1480 
  1493 	if (!check_length(len, 6, "SafeChangeInfo")) return;
  1481 	if (!check_length(len, 6, "SafeChangeInfo")) return;
  1494 	buf++;
  1482 	buf++;
  1495 	feature  = grf_load_byte(&buf);
  1483 	uint8 feature  = grf_load_byte(&buf);
  1496 	numprops = grf_load_byte(&buf);
  1484 	uint8 numprops = grf_load_byte(&buf);
  1497 	numinfo  = grf_load_byte(&buf);
  1485 	grf_load_byte(&buf);
  1498 	index    = grf_load_byte(&buf);
  1486 	grf_load_byte(&buf);
  1499 
  1487 
  1500 	if (feature == GSF_BRIDGE && numprops == 1) {
  1488 	if (feature == GSF_BRIDGE && numprops == 1) {
  1501 		uint8 prop = grf_load_byte(&buf);
  1489 		uint8 prop = grf_load_byte(&buf);
  1502 		/* Bridge property 0x0D is redefinition of sprite layout tables, which
  1490 		/* Bridge property 0x0D is redefinition of sprite layout tables, which
  1503 		 * is considered safe. */
  1491 		 * is considered safe. */
  1512 
  1500 
  1513 /* Action 0x00 (GLS_INIT) */
  1501 /* Action 0x00 (GLS_INIT) */
  1514 static void InitChangeInfo(byte *buf, int len)
  1502 static void InitChangeInfo(byte *buf, int len)
  1515 {
  1503 {
  1516 	byte *bufend = buf + len;
  1504 	byte *bufend = buf + len;
  1517 	uint8 feature;
       
  1518 	uint8 numprops;
       
  1519 	uint8 numinfo;
       
  1520 	uint8 index;
       
  1521 
  1505 
  1522 	if (len == 1) {
  1506 	if (len == 1) {
  1523 		grfmsg(8, "Silently ignoring one-byte special sprite 0x00");
  1507 		grfmsg(8, "Silently ignoring one-byte special sprite 0x00");
  1524 		return;
  1508 		return;
  1525 	}
  1509 	}
  1526 
  1510 
  1527 	if (!check_length(len, 6, "InitChangeInfo")) return;
  1511 	if (!check_length(len, 6, "InitChangeInfo")) return;
  1528 	buf++;
  1512 	buf++;
  1529 	feature  = grf_load_byte(&buf);
  1513 	uint8 feature  = grf_load_byte(&buf);
  1530 	numprops = grf_load_byte(&buf);
  1514 	uint8 numprops = grf_load_byte(&buf);
  1531 	numinfo  = grf_load_byte(&buf);
  1515 	uint8 numinfo  = grf_load_byte(&buf);
  1532 	index    = grf_load_byte(&buf);
  1516 	uint8 index    = grf_load_byte(&buf);
  1533 
  1517 
  1534 	while (numprops-- && buf < bufend) {
  1518 	while (numprops-- && buf < bufend) {
  1535 		uint8 prop = grf_load_byte(&buf);
  1519 		uint8 prop = grf_load_byte(&buf);
  1536 
  1520 
  1537 		switch (feature) {
  1521 		switch (feature) {
  1611 	 * E num-ent       how many entries per sprite set
  1595 	 * E num-ent       how many entries per sprite set
  1612 	 *                 For vehicles, this is the number of different
  1596 	 *                 For vehicles, this is the number of different
  1613 	 *                         vehicle directions in each sprite set
  1597 	 *                         vehicle directions in each sprite set
  1614 	 *                         Set num-dirs=8, unless your sprites are symmetric.
  1598 	 *                         Set num-dirs=8, unless your sprites are symmetric.
  1615 	 *                         In that case, use num-dirs=4.
  1599 	 *                         In that case, use num-dirs=4.
  1616 	 *                 For stations, must be 12 (hex) for the eighteen
  1600 	 */
  1617 	 *                         different sprites that make up a station */
       
  1618 	/* TODO: No stations support. */
       
  1619 	uint8 feature;
       
  1620 	uint num_sets;
       
  1621 	uint num_ents;
       
  1622 	uint i;
       
  1623 
  1601 
  1624 	if (!check_length(len, 4, "NewSpriteSet")) return;
  1602 	if (!check_length(len, 4, "NewSpriteSet")) return;
  1625 	buf++;
  1603 	buf++;
  1626 	feature  = grf_load_byte(&buf);
  1604 	uint8 feature   = grf_load_byte(&buf);
  1627 	num_sets = grf_load_byte(&buf);
  1605 	uint8 num_sets  = grf_load_byte(&buf);
  1628 	num_ents = grf_load_extended(&buf);
  1606 	uint16 num_ents = grf_load_extended(&buf);
  1629 
  1607 
  1630 	_cur_grffile->spriteset_start = _cur_spriteid;
  1608 	_cur_grffile->spriteset_start = _cur_spriteid;
  1631 	_cur_grffile->spriteset_feature = feature;
  1609 	_cur_grffile->spriteset_feature = feature;
  1632 	_cur_grffile->spriteset_numsets = num_sets;
  1610 	_cur_grffile->spriteset_numsets = num_sets;
  1633 	_cur_grffile->spriteset_numents = num_ents;
  1611 	_cur_grffile->spriteset_numents = num_ents;
  1634 
  1612 
  1635 	grfmsg(7, "New sprite set at %d of type %d, consisting of %d sets with %d views each (total %d)",
  1613 	grfmsg(7, "New sprite set at %d of type %d, consisting of %d sets with %d views each (total %d)",
  1636 		_cur_spriteid, feature, num_sets, num_ents, num_sets * num_ents
  1614 		_cur_spriteid, feature, num_sets, num_ents, num_sets * num_ents
  1637 	);
  1615 	);
  1638 
  1616 
  1639 	for (i = 0; i < num_sets * num_ents; i++) {
  1617 	for (uint i = 0; i < num_sets * num_ents; i++) {
  1640 		LoadNextSprite(_cur_spriteid++, _file_index);
  1618 		LoadNextSprite(_cur_spriteid++, _file_index);
  1641 		_nfo_line++;
  1619 		_nfo_line++;
  1642 	}
  1620 	}
  1643 }
  1621 }
  1644 
  1622 
  1697 	 *                 if 80 or greater, this is a randomized or variational
  1675 	 *                 if 80 or greater, this is a randomized or variational
  1698 	 *                 list definition, see below
  1676 	 *                 list definition, see below
  1699 	 *                 otherwise it specifies a number of entries, the exact
  1677 	 *                 otherwise it specifies a number of entries, the exact
  1700 	 *                 meaning depends on the feature
  1678 	 *                 meaning depends on the feature
  1701 	 * V feature-specific-data (huge mess, don't even look it up --pasky) */
  1679 	 * V feature-specific-data (huge mess, don't even look it up --pasky) */
  1702 	uint8 feature;
       
  1703 	uint8 setid;
       
  1704 	uint8 type;
       
  1705 	SpriteGroup *group = NULL;
  1680 	SpriteGroup *group = NULL;
  1706 	byte *bufend = buf + len;
  1681 	byte *bufend = buf + len;
  1707 
  1682 
  1708 	if (!check_length(len, 5, "NewSpriteGroup")) return;
  1683 	if (!check_length(len, 5, "NewSpriteGroup")) return;
  1709 	buf++;
  1684 	buf++;
  1710 
  1685 
  1711 	feature = grf_load_byte(&buf);
  1686 	uint8 feature = grf_load_byte(&buf);
  1712 	setid   = grf_load_byte(&buf);
  1687 	uint8 setid   = grf_load_byte(&buf);
  1713 	type    = grf_load_byte(&buf);
  1688 	uint8 type    = grf_load_byte(&buf);
  1714 
  1689 
  1715 	if (setid >= _cur_grffile->spritegroups_count) {
  1690 	if (setid >= _cur_grffile->spritegroups_count) {
  1716 		// Allocate memory for new sprite group references.
  1691 		// Allocate memory for new sprite group references.
  1717 		_cur_grffile->spritegroups = ReallocT(_cur_grffile->spritegroups, setid + 1);
  1692 		_cur_grffile->spritegroups = ReallocT(_cur_grffile->spritegroups, setid + 1);
  1718 		// Initialise new space to NULL
  1693 		// Initialise new space to NULL
  1729 		case 0x89: // Self scope, dword
  1704 		case 0x89: // Self scope, dword
  1730 		case 0x8A: // Parent scope, dword
  1705 		case 0x8A: // Parent scope, dword
  1731 		{
  1706 		{
  1732 			byte varadjust;
  1707 			byte varadjust;
  1733 			byte varsize;
  1708 			byte varsize;
  1734 			uint i;
       
  1735 
  1709 
  1736 			/* Check we can load the var size parameter */
  1710 			/* Check we can load the var size parameter */
  1737 			if (!check_length(bufend - buf, 1, "NewSpriteGroup (Deterministic) (1)")) return;
  1711 			if (!check_length(bufend - buf, 1, "NewSpriteGroup (Deterministic) (1)")) return;
  1738 
  1712 
  1739 			group = AllocateSpriteGroup();
  1713 			group = AllocateSpriteGroup();
  1792 			group->g.determ.num_ranges = grf_load_byte(&buf);
  1766 			group->g.determ.num_ranges = grf_load_byte(&buf);
  1793 			group->g.determ.ranges = CallocT<DeterministicSpriteGroupRange>(group->g.determ.num_ranges);
  1767 			group->g.determ.ranges = CallocT<DeterministicSpriteGroupRange>(group->g.determ.num_ranges);
  1794 
  1768 
  1795 			if (!check_length(bufend - buf, 2 + (2 + 2 * varsize) * group->g.determ.num_ranges, "NewSpriteGroup (Deterministic)")) return;
  1769 			if (!check_length(bufend - buf, 2 + (2 + 2 * varsize) * group->g.determ.num_ranges, "NewSpriteGroup (Deterministic)")) return;
  1796 
  1770 
  1797 			for (i = 0; i < group->g.determ.num_ranges; i++) {
  1771 			for (uint i = 0; i < group->g.determ.num_ranges; i++) {
  1798 				group->g.determ.ranges[i].group = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
  1772 				group->g.determ.ranges[i].group = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
  1799 				group->g.determ.ranges[i].low   = grf_load_var(varsize, &buf);
  1773 				group->g.determ.ranges[i].low   = grf_load_var(varsize, &buf);
  1800 				group->g.determ.ranges[i].high  = grf_load_var(varsize, &buf);
  1774 				group->g.determ.ranges[i].high  = grf_load_var(varsize, &buf);
  1801 			}
  1775 			}
  1802 
  1776 
  1806 
  1780 
  1807 		/* Randomized Sprite Group */
  1781 		/* Randomized Sprite Group */
  1808 		case 0x80: // Self scope
  1782 		case 0x80: // Self scope
  1809 		case 0x83: // Parent scope
  1783 		case 0x83: // Parent scope
  1810 		{
  1784 		{
  1811 			byte triggers;
       
  1812 			uint i;
       
  1813 
       
  1814 			if (!check_length(bufend - buf, 7, "NewSpriteGroup (Randomized) (1)")) return;
  1785 			if (!check_length(bufend - buf, 7, "NewSpriteGroup (Randomized) (1)")) return;
  1815 
  1786 
  1816 			group = AllocateSpriteGroup();
  1787 			group = AllocateSpriteGroup();
  1817 			group->type = SGT_RANDOMIZED;
  1788 			group->type = SGT_RANDOMIZED;
  1818 			group->g.random.var_scope = HASBIT(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
  1789 			group->g.random.var_scope = HASBIT(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
  1819 
  1790 
  1820 			triggers = grf_load_byte(&buf);
  1791 			uint8 triggers = grf_load_byte(&buf);
  1821 			group->g.random.triggers       = GB(triggers, 0, 7);
  1792 			group->g.random.triggers       = GB(triggers, 0, 7);
  1822 			group->g.random.cmp_mode       = HASBIT(triggers, 7) ? RSG_CMP_ALL : RSG_CMP_ANY;
  1793 			group->g.random.cmp_mode       = HASBIT(triggers, 7) ? RSG_CMP_ALL : RSG_CMP_ANY;
  1823 			group->g.random.lowest_randbit = grf_load_byte(&buf);
  1794 			group->g.random.lowest_randbit = grf_load_byte(&buf);
  1824 			group->g.random.num_groups     = grf_load_byte(&buf);
  1795 			group->g.random.num_groups     = grf_load_byte(&buf);
  1825 			group->g.random.groups = CallocT<const SpriteGroup*>(group->g.random.num_groups);
  1796 			group->g.random.groups = CallocT<const SpriteGroup*>(group->g.random.num_groups);
  1826 
  1797 
  1827 			if (!check_length(bufend - buf, 2 * group->g.random.num_groups, "NewSpriteGroup (Randomized) (2)")) return;
  1798 			if (!check_length(bufend - buf, 2 * group->g.random.num_groups, "NewSpriteGroup (Randomized) (2)")) return;
  1828 
  1799 
  1829 			for (i = 0; i < group->g.random.num_groups; i++) {
  1800 			for (uint i = 0; i < group->g.random.num_groups; i++) {
  1830 				group->g.random.groups[i] = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
  1801 				group->g.random.groups[i] = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
  1831 			}
  1802 			}
  1832 
  1803 
  1833 			break;
  1804 			break;
  1834 		}
  1805 		}
  1846 				case GSF_STATION:
  1817 				case GSF_STATION:
  1847 				{
  1818 				{
  1848 					byte sprites     = _cur_grffile->spriteset_numents;
  1819 					byte sprites     = _cur_grffile->spriteset_numents;
  1849 					byte num_loaded  = type;
  1820 					byte num_loaded  = type;
  1850 					byte num_loading = grf_load_byte(&buf);
  1821 					byte num_loading = grf_load_byte(&buf);
  1851 					uint i;
       
  1852 
  1822 
  1853 					if (_cur_grffile->spriteset_start == 0) {
  1823 					if (_cur_grffile->spriteset_start == 0) {
  1854 						grfmsg(0, "NewSpriteGroup: No sprite set to work on! Skipping");
  1824 						grfmsg(0, "NewSpriteGroup: No sprite set to work on! Skipping");
  1855 						return;
  1825 						return;
  1856 					}
  1826 					}
  1866 					if (num_loading > 0) group->g.real.loading = CallocT<const SpriteGroup*>(num_loading);
  1836 					if (num_loading > 0) group->g.real.loading = CallocT<const SpriteGroup*>(num_loading);
  1867 
  1837 
  1868 					grfmsg(6, "NewSpriteGroup: New SpriteGroup 0x%02X, %u views, %u loaded, %u loading",
  1838 					grfmsg(6, "NewSpriteGroup: New SpriteGroup 0x%02X, %u views, %u loaded, %u loading",
  1869 							setid, sprites, num_loaded, num_loading);
  1839 							setid, sprites, num_loaded, num_loading);
  1870 
  1840 
  1871 					for (i = 0; i < num_loaded; i++) {
  1841 					for (uint i = 0; i < num_loaded; i++) {
  1872 						uint16 spriteid = grf_load_word(&buf);
  1842 						uint16 spriteid = grf_load_word(&buf);
  1873 						group->g.real.loaded[i] = CreateGroupFromGroupID(feature, setid, type, spriteid, sprites);
  1843 						group->g.real.loaded[i] = CreateGroupFromGroupID(feature, setid, type, spriteid, sprites);
  1874 						grfmsg(8, "NewSpriteGroup: + rg->loaded[%i]  = subset %u", i, spriteid);
  1844 						grfmsg(8, "NewSpriteGroup: + rg->loaded[%i]  = subset %u", i, spriteid);
  1875 					}
  1845 					}
  1876 
  1846 
  1877 					for (i = 0; i < num_loading; i++) {
  1847 					for (uint i = 0; i < num_loading; i++) {
  1878 						uint16 spriteid = grf_load_word(&buf);
  1848 						uint16 spriteid = grf_load_word(&buf);
  1879 						group->g.real.loading[i] = CreateGroupFromGroupID(feature, setid, type, spriteid, sprites);
  1849 						group->g.real.loading[i] = CreateGroupFromGroupID(feature, setid, type, spriteid, sprites);
  1880 						grfmsg(8, "NewSpriteGroup: + rg->loading[%i] = subset %u", i, spriteid);
  1850 						grfmsg(8, "NewSpriteGroup: + rg->loading[%i] = subset %u", i, spriteid);
  1881 					}
  1851 					}
  1882 
  1852 
  1941 	/* TODO: Multiple cargo support could be useful even for trains/cars -
  1911 	/* TODO: Multiple cargo support could be useful even for trains/cars -
  1942 	 * cargo id 0xff is used for showing images in the build train list. */
  1912 	 * cargo id 0xff is used for showing images in the build train list. */
  1943 
  1913 
  1944 	static byte *last_engines;
  1914 	static byte *last_engines;
  1945 	static int last_engines_count;
  1915 	static int last_engines_count;
  1946 	uint8 feature;
       
  1947 	uint8 idcount;
       
  1948 	bool wagover;
       
  1949 	uint8 cidcount;
       
  1950 	int c, i;
       
  1951 
  1916 
  1952 	if (!check_length(len, 6, "FeatureMapSpriteGroup")) return;
  1917 	if (!check_length(len, 6, "FeatureMapSpriteGroup")) return;
  1953 	feature = buf[1];
  1918 
  1954 	idcount = buf[2] & 0x7F;
  1919 	uint8 feature = buf[1];
  1955 	wagover = (buf[2] & 0x80) == 0x80;
  1920 	uint8 idcount = buf[2] & 0x7F;
       
  1921 	bool wagover = (buf[2] & 0x80) == 0x80;
       
  1922 
  1956 	if (!check_length(len, 3 + idcount, "FeatureMapSpriteGroup")) return;
  1923 	if (!check_length(len, 3 + idcount, "FeatureMapSpriteGroup")) return;
  1957 
  1924 
  1958 	/* If ``n-id'' (or ``idcount'') is zero, this is a ``feature
  1925 	/* If idcount is zero, this is a feature callback */
  1959 	 * callback''. */
       
  1960 	if (idcount == 0) {
  1926 	if (idcount == 0) {
  1961 		grfmsg(2, "FeatureMapSpriteGroup: Feature callbacks not implemented yet");
  1927 		grfmsg(2, "FeatureMapSpriteGroup: Feature callbacks not implemented yet");
  1962 		return;
  1928 		return;
  1963 	}
  1929 	}
  1964 
  1930 
  1965 	cidcount = buf[3 + idcount];
  1931 	uint8 cidcount = buf[3 + idcount];
  1966 	if (!check_length(len, 4 + idcount + cidcount * 3, "FeatureMapSpriteGroup")) return;
  1932 	if (!check_length(len, 4 + idcount + cidcount * 3, "FeatureMapSpriteGroup")) return;
  1967 
  1933 
  1968 	grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids, %d cids, wagon override %d",
  1934 	grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids, %d cids, wagon override %d",
  1969 			feature, idcount, cidcount, wagover);
  1935 			feature, idcount, cidcount, wagover);
  1970 
  1936 
  1975 
  1941 
  1976 
  1942 
  1977 	if (feature == GSF_STATION) {
  1943 	if (feature == GSF_STATION) {
  1978 		// We do things differently for stations.
  1944 		// We do things differently for stations.
  1979 
  1945 
  1980 		for (i = 0; i < idcount; i++) {
  1946 		for (uint i = 0; i < idcount; i++) {
  1981 			uint8 stid = buf[3 + i];
  1947 			uint8 stid = buf[3 + i];
  1982 			StationSpec *statspec = _cur_grffile->stations[stid];
  1948 			StationSpec *statspec = _cur_grffile->stations[stid];
  1983 			byte *bp = &buf[4 + idcount];
  1949 			byte *bp = &buf[4 + idcount];
  1984 
  1950 
  1985 			for (c = 0; c < cidcount; c++) {
  1951 			for (uint c = 0; c < cidcount; c++) {
  1986 				uint8 ctype = grf_load_byte(&bp);
  1952 				uint8 ctype = grf_load_byte(&bp);
  1987 				uint16 groupid = grf_load_word(&bp);
  1953 				uint16 groupid = grf_load_word(&bp);
  1988 
  1954 
  1989 				if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  1955 				if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  1990 					grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  1956 					grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  2007 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  1973 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  2008 				       groupid, _cur_grffile->spritegroups_count);
  1974 				       groupid, _cur_grffile->spritegroups_count);
  2009 				return;
  1975 				return;
  2010 			}
  1976 			}
  2011 
  1977 
  2012 			for (i = 0; i < idcount; i++) {
  1978 			for (uint i = 0; i < idcount; i++) {
  2013 				uint8 stid = buf[3 + i];
  1979 				uint8 stid = buf[3 + i];
  2014 				StationSpec *statspec = _cur_grffile->stations[stid];
  1980 				StationSpec *statspec = _cur_grffile->stations[stid];
  2015 
  1981 
  2016 				statspec->spritegroup[CT_DEFAULT] = _cur_grffile->spritegroups[groupid];
  1982 				statspec->spritegroup[CT_DEFAULT] = _cur_grffile->spritegroups[groupid];
  2017 				statspec->grfid = _cur_grffile->grfid;
  1983 				statspec->grfid = _cur_grffile->grfid;
  2044 		grfmsg(6, "FeatureMapSpriteGroup: WagonOverride: %u engines, %u wagons",
  2010 		grfmsg(6, "FeatureMapSpriteGroup: WagonOverride: %u engines, %u wagons",
  2045 				last_engines_count, idcount);
  2011 				last_engines_count, idcount);
  2046 	}
  2012 	}
  2047 
  2013 
  2048 
  2014 
  2049 	for (i = 0; i < idcount; i++) {
  2015 	for (uint i = 0; i < idcount; i++) {
  2050 		uint8 engine_id = buf[3 + i];
  2016 		uint8 engine_id = buf[3 + i];
  2051 		uint8 engine = engine_id + _vehshifts[feature];
  2017 		uint8 engine = engine_id + _vehshifts[feature];
  2052 		byte *bp = &buf[4 + idcount];
  2018 		byte *bp = &buf[4 + idcount];
  2053 
  2019 
  2054 		if (engine_id > _vehcounts[feature]) {
  2020 		if (engine_id > _vehcounts[feature]) {
  2056 			return;
  2022 			return;
  2057 		}
  2023 		}
  2058 
  2024 
  2059 		grfmsg(7, "FeatureMapSpriteGroup: [%d] Engine %d...", i, engine);
  2025 		grfmsg(7, "FeatureMapSpriteGroup: [%d] Engine %d...", i, engine);
  2060 
  2026 
  2061 		for (c = 0; c < cidcount; c++) {
  2027 		for (uint c = 0; c < cidcount; c++) {
  2062 			uint8 ctype = grf_load_byte(&bp);
  2028 			uint8 ctype = grf_load_byte(&bp);
  2063 			uint16 groupid = grf_load_word(&bp);
  2029 			uint16 groupid = grf_load_word(&bp);
  2064 
  2030 
  2065 			grfmsg(8, "FeatureMapSpriteGroup: * [%d] Cargo type 0x%X, group id 0x%02X", c, ctype, groupid);
  2031 			grfmsg(8, "FeatureMapSpriteGroup: * [%d] Cargo type 0x%X, group id 0x%02X", c, ctype, groupid);
  2066 
  2032 
  2085 		byte *bp = buf + 4 + idcount + cidcount * 3;
  2051 		byte *bp = buf + 4 + idcount + cidcount * 3;
  2086 		uint16 groupid = grf_load_word(&bp);
  2052 		uint16 groupid = grf_load_word(&bp);
  2087 
  2053 
  2088 		grfmsg(8, "-- Default group id 0x%04X", groupid);
  2054 		grfmsg(8, "-- Default group id 0x%04X", groupid);
  2089 
  2055 
  2090 		for (i = 0; i < idcount; i++) {
  2056 		for (uint i = 0; i < idcount; i++) {
  2091 			uint8 engine = buf[3 + i] + _vehshifts[feature];
  2057 			uint8 engine = buf[3 + i] + _vehshifts[feature];
  2092 
  2058 
  2093 			// Don't tell me you don't love duplicated code!
  2059 			// Don't tell me you don't love duplicated code!
  2094 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2060 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2095 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping", groupid, _cur_grffile->spritegroups_count);
  2061 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping", groupid, _cur_grffile->spritegroups_count);
  2130 	 * B/W offset      number of the first vehicle that gets a new name
  2096 	 * B/W offset      number of the first vehicle that gets a new name
  2131 	 *                 Byte : ID of vehicle to change
  2097 	 *                 Byte : ID of vehicle to change
  2132 	 *                 Word : ID of string to change/add
  2098 	 *                 Word : ID of string to change/add
  2133 	 * S data          new texts, each of them zero-terminated, after
  2099 	 * S data          new texts, each of them zero-terminated, after
  2134 	 *                 which the next name begins. */
  2100 	 *                 which the next name begins. */
  2135 	/* TODO: No support for changing non-vehicle text. Perhaps we shouldn't
  2101 
  2136 	 * implement it at all, but it could be useful for some "modpacks"
       
  2137 	 * (completely new scenarios changing all graphics and logically also
       
  2138 	 * factory names etc). We should then also support all languages (by
       
  2139 	 * name), not only the original four ones. --pasky
       
  2140 	 * All of the above are coming.  In Time.  Some sooner than others :)*/
       
  2141 
       
  2142 	uint8 feature;
       
  2143 	uint8 lang;
       
  2144 	uint8 num;
       
  2145 	uint16 id;
       
  2146 	uint16 endid;
       
  2147 	bool new_scheme = _cur_grffile->grf_version >= 7;
  2102 	bool new_scheme = _cur_grffile->grf_version >= 7;
  2148 	bool generic;
       
  2149 
  2103 
  2150 	if (!check_length(len, 6, "FeatureNewName")) return;
  2104 	if (!check_length(len, 6, "FeatureNewName")) return;
  2151 	buf++;
  2105 	buf++;
  2152 	feature  = grf_load_byte(&buf);
  2106 	uint8 feature  = grf_load_byte(&buf);
  2153 	lang     = grf_load_byte(&buf);
  2107 	uint8 lang     = grf_load_byte(&buf);
  2154 	num      = grf_load_byte(&buf);
  2108 	uint8 num      = grf_load_byte(&buf);
  2155 	generic  = HASBIT(lang, 7);
  2109 	bool generic   = HASBIT(lang, 7);
  2156 	id       = generic ? grf_load_word(&buf) : grf_load_byte(&buf);
  2110 	uint16 id      = generic ? grf_load_word(&buf) : grf_load_byte(&buf);
  2157 
  2111 
  2158 	CLRBIT(lang, 7);
  2112 	CLRBIT(lang, 7);
  2159 
  2113 
  2160 	if (feature <= GSF_AIRCRAFT && id < _vehcounts[feature]) {
  2114 	if (feature <= GSF_AIRCRAFT && id < _vehcounts[feature]) {
  2161 		id += _vehshifts[feature];
  2115 		id += _vehshifts[feature];
  2162 	}
  2116 	}
  2163 	endid    = id + num;
  2117 	uint16 endid = id + num;
  2164 
  2118 
  2165 	grfmsg(6, "FeatureNewName: About to rename engines %d..%d (feature %d) in language 0x%02X",
  2119 	grfmsg(6, "FeatureNewName: About to rename engines %d..%d (feature %d) in language 0x%02X",
  2166 	               id, endid, feature, lang);
  2120 	               id, endid, feature, lang);
  2167 
  2121 
  2168 	len -= generic ? 6 : 5;
  2122 	len -= generic ? 6 : 5;
  2258 	 * B graphics-type What set of graphics the sprites define.
  2212 	 * B graphics-type What set of graphics the sprites define.
  2259 	 * E num-sprites   How many sprites are in this set?
  2213 	 * E num-sprites   How many sprites are in this set?
  2260 	 * V other data    Graphics type specific data.  Currently unused. */
  2214 	 * V other data    Graphics type specific data.  Currently unused. */
  2261 	/* TODO */
  2215 	/* TODO */
  2262 
  2216 
  2263 	uint8 type;
       
  2264 	uint16 num;
       
  2265 	SpriteID replace = 0;
  2217 	SpriteID replace = 0;
  2266 
  2218 
  2267 	if (!check_length(len, 2, "GraphicsNew")) return;
  2219 	if (!check_length(len, 2, "GraphicsNew")) return;
  2268 	buf++;
  2220 	buf++;
  2269 	type = grf_load_byte(&buf);
  2221 	uint8 type = grf_load_byte(&buf);
  2270 	num  = grf_load_extended(&buf);
  2222 	uint16 num = grf_load_extended(&buf);
  2271 
  2223 
  2272 	switch (type) {
  2224 	switch (type) {
  2273 		case 0x04: /* Signal graphics */
  2225 		case 0x04: /* Signal graphics */
  2274 			if (num != 112 && num != 240) {
  2226 			if (num != 112 && num != 240) {
  2275 				grfmsg(1, "GraphicsNews: Signal graphics sprite count must be 112 or 240, skipping");
  2227 				grfmsg(1, "GraphicsNews: Signal graphics sprite count must be 112 or 240, skipping");
  2482 	 * B param-size
  2434 	 * B param-size
  2483 	 * B condition-type
  2435 	 * B condition-type
  2484 	 * V value
  2436 	 * V value
  2485 	 * B num-sprites */
  2437 	 * B num-sprites */
  2486 	/* TODO: More params. More condition types. */
  2438 	/* TODO: More params. More condition types. */
  2487 	uint8 param;
       
  2488 	uint8 paramsize;
       
  2489 	uint8 condtype;
       
  2490 	uint8 numsprites;
       
  2491 	uint32 param_val = 0;
       
  2492 	uint32 cond_val = 0;
  2439 	uint32 cond_val = 0;
  2493 	uint32 mask = 0;
  2440 	uint32 mask = 0;
  2494 	bool result;
  2441 	bool result;
  2495 	GRFLabel *label;
       
  2496 	GRFLabel *choice = NULL;
       
  2497 
  2442 
  2498 	if (!check_length(len, 6, "SkipIf")) return;
  2443 	if (!check_length(len, 6, "SkipIf")) return;
  2499 	buf++;
  2444 	buf++;
  2500 	param     = grf_load_byte(&buf);
  2445 	uint8 param     = grf_load_byte(&buf);
  2501 	paramsize = grf_load_byte(&buf);
  2446 	uint8 paramsize = grf_load_byte(&buf);
  2502 	condtype  = grf_load_byte(&buf);
  2447 	uint8 condtype  = grf_load_byte(&buf);
  2503 
  2448 
  2504 	if (condtype < 2) {
  2449 	if (condtype < 2) {
  2505 		/* Always 1 for bit tests, the given value should be ignored. */
  2450 		/* Always 1 for bit tests, the given value should be ignored. */
  2506 		paramsize = 1;
  2451 		paramsize = 1;
  2507 	}
  2452 	}
  2516 	if (param < 0x80 && _cur_grffile->param_end <= param) {
  2461 	if (param < 0x80 && _cur_grffile->param_end <= param) {
  2517 		grfmsg(7, "Param %d undefined, skipping test", param);
  2462 		grfmsg(7, "Param %d undefined, skipping test", param);
  2518 		return;
  2463 		return;
  2519 	}
  2464 	}
  2520 
  2465 
  2521 	param_val = GetParamVal(param, &cond_val);
  2466 	uint32 param_val = GetParamVal(param, &cond_val);
  2522 
  2467 
  2523 	grfmsg(7, "Test condtype %d, param 0x%08X, condval 0x%08X", condtype, param_val, cond_val);
  2468 	grfmsg(7, "Test condtype %d, param 0x%08X, condval 0x%08X", condtype, param_val, cond_val);
  2524 
  2469 
  2525 	if (param == 0x88) {
  2470 	if (param == 0x88) {
  2526 		/* GRF ID checks */
  2471 		/* GRF ID checks */
  2580 	if (!result) {
  2525 	if (!result) {
  2581 		grfmsg(2, "Not skipping sprites, test was false");
  2526 		grfmsg(2, "Not skipping sprites, test was false");
  2582 		return;
  2527 		return;
  2583 	}
  2528 	}
  2584 
  2529 
  2585 	numsprites = grf_load_byte(&buf);
  2530 	uint8 numsprites = grf_load_byte(&buf);
  2586 
  2531 
  2587 	/* numsprites can be a GOTO label if it has been defined in the GRF
  2532 	/* numsprites can be a GOTO label if it has been defined in the GRF
  2588 	 * file. The jump will always be the first matching label that follows
  2533 	 * file. The jump will always be the first matching label that follows
  2589 	 * the current nfo_line. If no matching label is found, the first matching
  2534 	 * the current nfo_line. If no matching label is found, the first matching
  2590 	 * label in the file is used. */
  2535 	 * label in the file is used. */
  2591 	for (label = _cur_grffile->label; label != NULL; label = label->next) {
  2536 	GRFLabel *choice = NULL;
       
  2537 	for (GRFLabel *label = _cur_grffile->label; label != NULL; label = label->next) {
  2592 		if (label->label != numsprites) continue;
  2538 		if (label->label != numsprites) continue;
  2593 
  2539 
  2594 		/* Remember a goto before the current line */
  2540 		/* Remember a goto before the current line */
  2595 		if (choice == NULL) choice = label;
  2541 		if (choice == NULL) choice = label;
  2596 		/* If we find a label here, this is definitely good */
  2542 		/* If we find a label here, this is definitely good */
  2622 
  2568 
  2623 
  2569 
  2624 /* Action 0x08 (GLS_FILESCAN) */
  2570 /* Action 0x08 (GLS_FILESCAN) */
  2625 static void ScanInfo(byte *buf, int len)
  2571 static void ScanInfo(byte *buf, int len)
  2626 {
  2572 {
  2627 	uint8 version;
  2573 	if (!check_length(len, 8, "Info")) return;
  2628 	uint32 grfid;
  2574 	buf++;
  2629 	const char *name;
  2575 	grf_load_byte(&buf);
  2630 	const char *info;
  2576 	uint32 grfid  = grf_load_dword(&buf);
  2631 
       
  2632 	if (!check_length(len, 8, "Info")) return; buf++;
       
  2633 	version = grf_load_byte(&buf);
       
  2634 	grfid = grf_load_dword(&buf);
       
  2635 
  2577 
  2636 	_cur_grfconfig->grfid = grfid;
  2578 	_cur_grfconfig->grfid = grfid;
  2637 
  2579 
  2638 	/* GRF IDs starting with 0xFF are reserved for internal TTDPatch use */
  2580 	/* GRF IDs starting with 0xFF are reserved for internal TTDPatch use */
  2639 	if (GB(grfid, 24, 8) == 0xFF) SETBIT(_cur_grfconfig->flags, GCF_SYSTEM);
  2581 	if (GB(grfid, 24, 8) == 0xFF) SETBIT(_cur_grfconfig->flags, GCF_SYSTEM);
  2640 
  2582 
  2641 	len -= 6;
  2583 	len -= 6;
  2642 	name = grf_load_string(&buf, len);
  2584 	const char *name = grf_load_string(&buf, len);
  2643 	_cur_grfconfig->name = TranslateTTDPatchCodes(name);
  2585 	_cur_grfconfig->name = TranslateTTDPatchCodes(name);
  2644 
  2586 
  2645 	len -= strlen(name) + 1;
  2587 	len -= strlen(name) + 1;
  2646 	if (len > 0) {
  2588 	if (len > 0) {
  2647 		info = grf_load_string(&buf, len);
  2589 		const char *info = grf_load_string(&buf, len);
  2648 		_cur_grfconfig->info = TranslateTTDPatchCodes(info);
  2590 		_cur_grfconfig->info = TranslateTTDPatchCodes(info);
  2649 	}
  2591 	}
  2650 
  2592 
  2651 	/* GLS_INFOSCAN only looks for the action 8, so we can skip the rest of the file */
  2593 	/* GLS_INFOSCAN only looks for the action 8, so we can skip the rest of the file */
  2652 	_skip_sprites = -1;
  2594 	_skip_sprites = -1;
  2659 	 *
  2601 	 *
  2660 	 * B version       newgrf version, currently 06
  2602 	 * B version       newgrf version, currently 06
  2661 	 * 4*B grf-id      globally unique ID of this .grf file
  2603 	 * 4*B grf-id      globally unique ID of this .grf file
  2662 	 * S name          name of this .grf set
  2604 	 * S name          name of this .grf set
  2663 	 * S info          string describing the set, and e.g. author and copyright */
  2605 	 * S info          string describing the set, and e.g. author and copyright */
  2664 	/* TODO: Check version. (We should have own versioning done somehow.) */
  2606 
  2665 	uint8 version;
  2607 	if (!check_length(len, 8, "GRFInfo")) return;
  2666 	uint32 grfid;
  2608 	buf++;
  2667 	const char *name;
  2609 	uint8 version    = grf_load_byte(&buf);
  2668 
  2610 	uint32 grfid     = grf_load_dword(&buf);
  2669 	if (!check_length(len, 8, "GRFInfo")) return; buf++;
  2611 	const char *name = grf_load_string(&buf, len - 6);
  2670 	version = grf_load_byte(&buf);
       
  2671 	grfid = grf_load_dword(&buf);
       
  2672 	name = grf_load_string(&buf, len - 6);
       
  2673 
  2612 
  2674 	_cur_grffile->grfid = grfid;
  2613 	_cur_grffile->grfid = grfid;
  2675 	_cur_grffile->grf_version = version;
  2614 	_cur_grffile->grf_version = version;
  2676 	_cur_grfconfig->status = _cur_stage < GLS_ACTIVATION ? GCS_INITIALISED : GCS_ACTIVATED;
  2615 	_cur_grfconfig->status = _cur_stage < GLS_ACTIVATION ? GCS_INITIALISED : GCS_ACTIVATED;
  2677 
  2616 
  2687 	 *
  2626 	 *
  2688 	 * B num-sets      How many sets of sprites to replace.
  2627 	 * B num-sets      How many sets of sprites to replace.
  2689 	 * Each set:
  2628 	 * Each set:
  2690 	 * B num-sprites   How many sprites are in this set
  2629 	 * B num-sprites   How many sprites are in this set
  2691 	 * W first-sprite  First sprite number to replace */
  2630 	 * W first-sprite  First sprite number to replace */
  2692 	uint8 num_sets;
       
  2693 	uint i;
       
  2694 
  2631 
  2695 	buf++; /* skip action byte */
  2632 	buf++; /* skip action byte */
  2696 	num_sets = grf_load_byte(&buf);
  2633 	uint8 num_sets = grf_load_byte(&buf);
  2697 
  2634 
  2698 	for (i = 0; i < num_sets; i++) {
  2635 	for (uint i = 0; i < num_sets; i++) {
  2699 		uint8 num_sprites = grf_load_byte(&buf);
  2636 		uint8 num_sprites = grf_load_byte(&buf);
  2700 		uint16 first_sprite = grf_load_word(&buf);
  2637 		uint16 first_sprite = grf_load_word(&buf);
  2701 		uint j;
       
  2702 
  2638 
  2703 		grfmsg(2, "SpriteReplace: [Set %d] Changing %d sprites, beginning with %d",
  2639 		grfmsg(2, "SpriteReplace: [Set %d] Changing %d sprites, beginning with %d",
  2704 			i, num_sprites, first_sprite
  2640 			i, num_sprites, first_sprite
  2705 		);
  2641 		);
  2706 
  2642 
  2707 		for (j = 0; j < num_sprites; j++) {
  2643 		for (uint j = 0; j < num_sprites; j++) {
  2708 			LoadNextSprite(first_sprite + j, _file_index); // XXX
  2644 			LoadNextSprite(first_sprite + j, _file_index); // XXX
  2709 			_nfo_line++;
  2645 			_nfo_line++;
  2710 		}
  2646 		}
  2711 	}
  2647 	}
  2712 }
  2648 }
  2842 }
  2778 }
  2843 
  2779 
  2844 /* Action 0x0D (GLS_SAFETYSCAN) */
  2780 /* Action 0x0D (GLS_SAFETYSCAN) */
  2845 static void SafeParamSet(byte *buf, int len)
  2781 static void SafeParamSet(byte *buf, int len)
  2846 {
  2782 {
  2847 	uint8 target;
       
  2848 
       
  2849 	if (!check_length(len, 5, "SafeParamSet")) return;
  2783 	if (!check_length(len, 5, "SafeParamSet")) return;
  2850 	buf++;
  2784 	buf++;
  2851 	target = grf_load_byte(&buf);
  2785 	uint8 target = grf_load_byte(&buf);
  2852 
  2786 
  2853 	/* Only writing GRF parameters is considered safe */
  2787 	/* Only writing GRF parameters is considered safe */
  2854 	if (target < 0x80) return;
  2788 	if (target < 0x80) return;
  2855 
  2789 
  2856 	/* GRM could be unsafe, but as here it can only happen after other GRFs
  2790 	/* GRM could be unsafe, but as here it can only happen after other GRFs
  2887 	 *         negative, source1 is unsigned)
  2821 	 *         negative, source1 is unsigned)
  2888 	 * 06      Signed bit shift, source1 by source2
  2822 	 * 06      Signed bit shift, source1 by source2
  2889 	 *         (source2 like in 05, and source1 as well)
  2823 	 *         (source2 like in 05, and source1 as well)
  2890 	 */
  2824 	 */
  2891 
  2825 
  2892 	byte target;
       
  2893 	byte oper;
       
  2894 	uint32 src1;
       
  2895 	uint32 src2;
       
  2896 	uint32 data = 0;
       
  2897 	uint32 res;
       
  2898 
       
  2899 	if (!check_length(len, 5, "ParamSet")) return;
  2826 	if (!check_length(len, 5, "ParamSet")) return;
  2900 	buf++;
  2827 	buf++;
  2901 	target = grf_load_byte(&buf);
  2828 	uint8 target = grf_load_byte(&buf);
  2902 	oper = grf_load_byte(&buf);
  2829 	uint8 oper   = grf_load_byte(&buf);
  2903 	src1 = grf_load_byte(&buf);
  2830 	uint32 src1  = grf_load_byte(&buf);
  2904 	src2 = grf_load_byte(&buf);
  2831 	uint32 src2  = grf_load_byte(&buf);
  2905 
  2832 
       
  2833 	uint32 data = 0;
  2906 	if (len >= 8) data = grf_load_dword(&buf);
  2834 	if (len >= 8) data = grf_load_dword(&buf);
  2907 
  2835 
  2908 	/* You can add 80 to the operation to make it apply only if the target
  2836 	/* You can add 80 to the operation to make it apply only if the target
  2909 	 * is not defined yet.  In this respect, a parameter is taken to be
  2837 	 * is not defined yet.  In this respect, a parameter is taken to be
  2910 	 * defined if any of the following applies:
  2838 	 * defined if any of the following applies:
  2911 	 * - it has been set to any value in the newgrf(w).cfg parameter list
  2839 	 * - it has been set to any value in the newgrf(w).cfg parameter list
  2912 	 * - it OR A PARAMETER WITH HIGHER NUMBER has been set to any value by
  2840 	 * - it OR A PARAMETER WITH HIGHER NUMBER has been set to any value by
  2913 	 *   an earlier action D */
  2841 	 *   an earlier action D */
  2914 	if (oper & 0x80) {
  2842 	if (HASBIT(oper, 7)) {
  2915 		if (target < 0x80 && target < _cur_grffile->param_end) {
  2843 		if (target < 0x80 && target < _cur_grffile->param_end) {
  2916 			grfmsg(7, "Param %u already defined, skipping", target);
  2844 			grfmsg(7, "Param %u already defined, skipping", target);
  2917 			return;
  2845 			return;
  2918 		}
  2846 		}
  2919 
  2847 
  2920 		oper &= 0x7F;
  2848 		oper = GB(oper, 0, 7);
  2921 	}
  2849 	}
  2922 
  2850 
  2923 	if (src2 == 0xFE) {
  2851 	if (src2 == 0xFE) {
  2924 		if (GB(data, 0, 8) == 0xFF) {
  2852 		if (GB(data, 0, 8) == 0xFF) {
  2925 			if (data == 0x0000FFFF) {
  2853 			if (data == 0x0000FFFF) {
  2943 						case 0x03: /* Aircraft */
  2871 						case 0x03: /* Aircraft */
  2944 						{
  2872 						{
  2945 							uint start = 0;
  2873 							uint start = 0;
  2946 							uint size  = 0;
  2874 							uint size  = 0;
  2947 							uint shift = _vehshifts[feature];
  2875 							uint shift = _vehshifts[feature];
  2948 							int i;
       
  2949 
  2876 
  2950 							if (op == 6) {
  2877 							if (op == 6) {
  2951 								/* Return GRFID of set that reserved ID */
  2878 								/* Return GRFID of set that reserved ID */
  2952 								src1 = _grm_engines[shift + _cur_grffile->param[target]];
  2879 								src1 = _grm_engines[shift + _cur_grffile->param[target]];
  2953 								break;
  2880 								break;
  2954 							}
  2881 							}
  2955 
  2882 
  2956 							/* With an operation of 2 or 3, we want to reserve a specific block of IDs */
  2883 							/* With an operation of 2 or 3, we want to reserve a specific block of IDs */
  2957 							if (op == 2 || op == 3) start = _cur_grffile->param[target];
  2884 							if (op == 2 || op == 3) start = _cur_grffile->param[target];
  2958 
  2885 
  2959 							for (i = start; i < _vehcounts[feature]; i++) {
  2886 							for (uint i = start; i < _vehcounts[feature]; i++) {
  2960 								if (_grm_engines[shift + i] == 0) {
  2887 								if (_grm_engines[shift + i] == 0) {
  2961 									size++;
  2888 									size++;
  2962 								} else {
  2889 								} else {
  2963 									if (op == 2 || op == 3) break;
  2890 									if (op == 2 || op == 3) break;
  2964 									start = i + 1;
  2891 									start = i + 1;
  2970 
  2897 
  2971 							if (size == count) {
  2898 							if (size == count) {
  2972 								/* Got the slot... */
  2899 								/* Got the slot... */
  2973 								if (op == 0 || op == 3) {
  2900 								if (op == 0 || op == 3) {
  2974 									grfmsg(2, "GRM: Reserving %d vehicles at %d", count, start);
  2901 									grfmsg(2, "GRM: Reserving %d vehicles at %d", count, start);
  2975 									for (i = 0; i < count; i++) _grm_engines[shift + start + i] = _cur_grffile->grfid;
  2902 									for (uint i = 0; i < count; i++) _grm_engines[shift + start + i] = _cur_grffile->grfid;
  2976 								}
  2903 								}
  2977 								src1 = start;
  2904 								src1 = start;
  2978 							} else {
  2905 							} else {
  2979 								/* Unable to allocate */
  2906 								/* Unable to allocate */
  2980 								if (op != 4 && op != 5) {
  2907 								if (op != 4 && op != 5) {
  3046 	 * source2=FE, source1=the other GRF's parameter number and data=GRF
  2973 	 * source2=FE, source1=the other GRF's parameter number and data=GRF
  3047 	 * ID.  This is only valid with operation 00 (set).  If the GRF ID
  2974 	 * ID.  This is only valid with operation 00 (set).  If the GRF ID
  3048 	 * cannot be found, a value of 0 is used for the parameter value
  2975 	 * cannot be found, a value of 0 is used for the parameter value
  3049 	 * instead. */
  2976 	 * instead. */
  3050 
  2977 
       
  2978 	uint32 res;
  3051 	switch (oper) {
  2979 	switch (oper) {
  3052 		case 0x00:
  2980 		case 0x00:
  3053 			res = src1;
  2981 			res = src1;
  3054 			break;
  2982 			break;
  3055 
  2983 
  3167 	/* <0E> <num> <grfids...>
  3095 	/* <0E> <num> <grfids...>
  3168 	 *
  3096 	 *
  3169 	 * B num           Number of GRFIDs that follow
  3097 	 * B num           Number of GRFIDs that follow
  3170 	 * D grfids        GRFIDs of the files to deactivate */
  3098 	 * D grfids        GRFIDs of the files to deactivate */
  3171 
  3099 
  3172 	byte num;
  3100 	if (!check_length(len, 2, "GRFInhibit")) return;
  3173 	int i;
  3101 	buf++;
  3174 
  3102 	uint8 num = grf_load_byte(&buf);
  3175 	if (!check_length(len, 1, "GRFInhibit")) return;
  3103 	if (!check_length(len, 2 + 4 * num, "GRFInhibit")) return;
  3176 	buf++, len--;
  3104 
  3177 	num = grf_load_byte(&buf); len--;
  3105 	for (uint i = 0; i < num; i++) {
  3178 	if (!check_length(len, 4 * num, "GRFInhibit")) return;
       
  3179 
       
  3180 	for (i = 0; i < num; i++) {
       
  3181 		uint32 grfid = grf_load_dword(&buf);
  3106 		uint32 grfid = grf_load_dword(&buf);
  3182 
  3107 
  3183 		/* GRF is unsafe it if tries to deactivate other GRFs */
  3108 		/* GRF is unsafe it if tries to deactivate other GRFs */
  3184 		if (grfid != _cur_grfconfig->grfid) {
  3109 		if (grfid != _cur_grfconfig->grfid) {
  3185 			SETBIT(_cur_grfconfig->flags, GCF_UNSAFE);
  3110 			SETBIT(_cur_grfconfig->flags, GCF_UNSAFE);
  3198 	/* <0E> <num> <grfids...>
  3123 	/* <0E> <num> <grfids...>
  3199 	 *
  3124 	 *
  3200 	 * B num           Number of GRFIDs that follow
  3125 	 * B num           Number of GRFIDs that follow
  3201 	 * D grfids        GRFIDs of the files to deactivate */
  3126 	 * D grfids        GRFIDs of the files to deactivate */
  3202 
  3127 
  3203 	byte num;
  3128 	if (!check_length(len, 2, "GRFInhibit")) return;
  3204 	int i;
  3129 	buf++;
  3205 
  3130 	uint8 num = grf_load_byte(&buf);
  3206 	if (!check_length(len, 1, "GRFInhibit")) return;
  3131 	if (!check_length(len, 2 + 4 * num, "GRFInhibit")) return;
  3207 	buf++, len--;
  3132 
  3208 	num = grf_load_byte(&buf); len--;
  3133 	for (uint i = 0; i < num; i++) {
  3209 	if (!check_length(len, 4 * num, "GRFInhibit")) return;
       
  3210 
       
  3211 	for (i = 0; i < num; i++) {
       
  3212 		uint32 grfid = grf_load_dword(&buf);
  3134 		uint32 grfid = grf_load_dword(&buf);
  3213 		GRFConfig *file = GetGRFConfig(grfid);
  3135 		GRFConfig *file = GetGRFConfig(grfid);
  3214 
  3136 
  3215 		/* Unset activation flag */
  3137 		/* Unset activation flag */
  3216 		if (file != NULL && file != _cur_grfconfig) {
  3138 		if (file != NULL && file != _cur_grfconfig) {
  3226 	/* <10> <label> [<comment>]
  3148 	/* <10> <label> [<comment>]
  3227 	 *
  3149 	 *
  3228 	 * B label      The label to define
  3150 	 * B label      The label to define
  3229 	 * V comment    Optional comment - ignored */
  3151 	 * V comment    Optional comment - ignored */
  3230 
  3152 
  3231 	GRFLabel *label;
       
  3232 
       
  3233 	if (!check_length(len, 1, "DefineGotoLabel")) return;
  3153 	if (!check_length(len, 1, "DefineGotoLabel")) return;
  3234 	buf++; len--;
  3154 	buf++; len--;
  3235 
  3155 
  3236 	label = MallocT<GRFLabel>(1);
  3156 	GRFLabel *label = MallocT<GRFLabel>(1);
  3237 	label->label    = grf_load_byte(&buf);
  3157 	label->label    = grf_load_byte(&buf);
  3238 	label->nfo_line = _nfo_line;
  3158 	label->nfo_line = _nfo_line;
  3239 	label->pos      = FioGetPos();
  3159 	label->pos      = FioGetPos();
  3240 	label->next     = NULL;
  3160 	label->next     = NULL;
  3241 
  3161 
  3257 {
  3177 {
  3258 	/* <11> <num>
  3178 	/* <11> <num>
  3259 	 *
  3179 	 *
  3260 	 * W num      Number of sound files that follow */
  3180 	 * W num      Number of sound files that follow */
  3261 
  3181 
  3262 	uint16 num;
       
  3263 
       
  3264 	if (!check_length(len, 1, "GRFSound")) return;
  3182 	if (!check_length(len, 1, "GRFSound")) return;
  3265 	buf++;
  3183 	buf++;
  3266 	num = grf_load_word(&buf);
  3184 	uint16 num = grf_load_word(&buf);
  3267 
  3185 
  3268 	_grf_data_blocks = num;
  3186 	_grf_data_blocks = num;
  3269 	_grf_data_type   = GDT_SOUND;
  3187 	_grf_data_type   = GDT_SOUND;
  3270 
  3188 
  3271 	if (_cur_grffile->sound_offset == 0) _cur_grffile->sound_offset = GetNumSounds();
  3189 	if (_cur_grffile->sound_offset == 0) _cur_grffile->sound_offset = GetNumSounds();
  3323 }
  3241 }
  3324 
  3242 
  3325 static void LoadGRFSound(byte *buf, int len)
  3243 static void LoadGRFSound(byte *buf, int len)
  3326 {
  3244 {
  3327 	byte *buf_start = buf;
  3245 	byte *buf_start = buf;
  3328 	FileEntry *se;
       
  3329 
  3246 
  3330 	/* Allocate a sound entry. This is done even if the data is not loaded
  3247 	/* Allocate a sound entry. This is done even if the data is not loaded
  3331 	 * so that the indices used elsewhere are still correct. */
  3248 	 * so that the indices used elsewhere are still correct. */
  3332 	se = AllocateFileEntry();
  3249 	FileEntry *se = AllocateFileEntry();
  3333 
  3250 
  3334 	if (grf_load_dword(&buf) != BSWAP32('RIFF')) {
  3251 	if (grf_load_dword(&buf) != BSWAP32('RIFF')) {
  3335 		grfmsg(1, "LoadGRFSound: Missing RIFF header");
  3252 		grfmsg(1, "LoadGRFSound: Missing RIFF header");
  3336 		return;
  3253 		return;
  3337 	}
  3254 	}
  3392 	 * B num_def      Number of definitions
  3309 	 * B num_def      Number of definitions
  3393 	 * B font_size    Size of font (0 = normal, 1 = small, 2 = large)
  3310 	 * B font_size    Size of font (0 = normal, 1 = small, 2 = large)
  3394 	 * B num_char     Number of consecutive glyphs
  3311 	 * B num_char     Number of consecutive glyphs
  3395 	 * W base_char    First character index */
  3312 	 * W base_char    First character index */
  3396 
  3313 
  3397 	uint8 num_def;
       
  3398 	uint i;
       
  3399 
       
  3400 	buf++; len--;
  3314 	buf++; len--;
  3401 	if (!check_length(len, 1, "LoadFontGlyph")) return;
  3315 	if (!check_length(len, 1, "LoadFontGlyph")) return;
  3402 
  3316 
  3403 	num_def = grf_load_byte(&buf);
  3317 	uint8 num_def = grf_load_byte(&buf);
  3404 
  3318 
  3405 	if (!check_length(len, 1 + num_def * 4, "LoadFontGlyph")) return;
  3319 	if (!check_length(len, 1 + num_def * 4, "LoadFontGlyph")) return;
  3406 
  3320 
  3407 	for (i = 0; i < num_def; i++) {
  3321 	for (uint i = 0; i < num_def; i++) {
  3408 		FontSize size    = (FontSize)grf_load_byte(&buf);
  3322 		FontSize size    = (FontSize)grf_load_byte(&buf);
  3409 		uint8  num_char  = grf_load_byte(&buf);
  3323 		uint8  num_char  = grf_load_byte(&buf);
  3410 		uint16 base_char = grf_load_word(&buf);
  3324 		uint16 base_char = grf_load_word(&buf);
  3411 		uint c;
       
  3412 
  3325 
  3413 		grfmsg(7, "LoadFontGlyph: Loading %u glyph(s) at 0x%04X for size %u", num_char, base_char, size);
  3326 		grfmsg(7, "LoadFontGlyph: Loading %u glyph(s) at 0x%04X for size %u", num_char, base_char, size);
  3414 
  3327 
  3415 		for (c = 0; c < num_char; c++) {
  3328 		for (uint c = 0; c < num_char; c++) {
  3416 			SetUnicodeGlyph(size, base_char + c, _cur_spriteid);
  3329 			SetUnicodeGlyph(size, base_char + c, _cur_spriteid);
  3417 			LoadNextSprite(_cur_spriteid++, _file_index);
  3330 			LoadNextSprite(_cur_spriteid++, _file_index);
  3418 			_nfo_line++;
  3331 			_nfo_line++;
  3419 		}
  3332 		}
  3420 	}
  3333 	}
  3487 }
  3400 }
  3488 
  3401 
  3489 /* 'Action 0xFF' */
  3402 /* 'Action 0xFF' */
  3490 static void GRFDataBlock(byte *buf, int len)
  3403 static void GRFDataBlock(byte *buf, int len)
  3491 {
  3404 {
  3492 	byte name_len;
       
  3493 	const char *name;
       
  3494 
       
  3495 	if (_grf_data_blocks == 0) {
  3405 	if (_grf_data_blocks == 0) {
  3496 		grfmsg(2, "GRFDataBlock: unexpected data block, skipping");
  3406 		grfmsg(2, "GRFDataBlock: unexpected data block, skipping");
  3497 		return;
  3407 		return;
  3498 	}
  3408 	}
  3499 
  3409 
  3500 	buf++;
  3410 	buf++;
  3501 	name_len = grf_load_byte(&buf);
  3411 	uint8 name_len = grf_load_byte(&buf);
  3502 	name = (const char *)buf;
  3412 	const char *name = (const char *)buf;
  3503 	buf += name_len + 1;
  3413 	buf += name_len + 1;
  3504 
  3414 
  3505 	grfmsg(2, "GRFDataBlock: block name '%s'...", name);
  3415 	grfmsg(2, "GRFDataBlock: block name '%s'...", name);
  3506 
  3416 
  3507 	_grf_data_blocks--;
  3417 	_grf_data_blocks--;
  3602 	                   |                                        (0 << 0x17); // articulatedrvs
  3512 	                   |                                        (0 << 0x17); // articulatedrvs
  3603 }
  3513 }
  3604 
  3514 
  3605 static void ResetCustomStations()
  3515 static void ResetCustomStations()
  3606 {
  3516 {
  3607 	StationSpec *statspec;
  3517 	for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
  3608 	GRFFile *file;
       
  3609 	uint i;
       
  3610 	uint t;
       
  3611 
       
  3612 	for (file = _first_grffile; file != NULL; file = file->next) {
       
  3613 		if (file->stations == NULL) continue;
  3518 		if (file->stations == NULL) continue;
  3614 		for (i = 0; i < MAX_STATIONS; i++) {
  3519 		for (uint i = 0; i < MAX_STATIONS; i++) {
  3615 			if (file->stations[i] == NULL) continue;
  3520 			if (file->stations[i] == NULL) continue;
  3616 			statspec = file->stations[i];
  3521 			StationSpec *statspec = file->stations[i];
  3617 
  3522 
  3618 			/* Release renderdata, if it wasn't copied from another custom station spec  */
  3523 			/* Release renderdata, if it wasn't copied from another custom station spec  */
  3619 			if (!statspec->copied_renderdata) {
  3524 			if (!statspec->copied_renderdata) {
  3620 				for (t = 0; t < statspec->tiles; t++) {
  3525 				for (uint t = 0; t < statspec->tiles; t++) {
  3621 					free((void*)statspec->renderdata[t].seq);
  3526 					free((void*)statspec->renderdata[t].seq);
  3622 				}
  3527 				}
  3623 				free(statspec->renderdata);
  3528 				free(statspec->renderdata);
  3624 			}
  3529 			}
  3625 
  3530 
  3626 			/* Release platforms and layouts */
  3531 			/* Release platforms and layouts */
  3627 			if (!statspec->copied_layouts) {
  3532 			if (!statspec->copied_layouts) {
  3628 				uint l, p;
  3533 				for (uint l = 0; l < statspec->lengths; l++) {
  3629 				for (l = 0; l < statspec->lengths; l++) {
  3534 					for (uint p = 0; p < statspec->platforms[l]; p++) {
  3630 					for (p = 0; p < statspec->platforms[l]; p++) {
       
  3631 						free(statspec->layouts[l][p]);
  3535 						free(statspec->layouts[l][p]);
  3632 					}
  3536 					}
  3633 					free(statspec->layouts[l]);
  3537 					free(statspec->layouts[l]);
  3634 				}
  3538 				}
  3635 				free(statspec->layouts);
  3539 				free(statspec->layouts);
  3646 	}
  3550 	}
  3647 }
  3551 }
  3648 
  3552 
  3649 static void ResetNewGRF()
  3553 static void ResetNewGRF()
  3650 {
  3554 {
  3651 	GRFFile *f, *next;
  3555 	GRFFile *next;
  3652 
  3556 
  3653 	for (f = _first_grffile; f != NULL; f = next) {
  3557 	for (GRFFile *f = _first_grffile; f != NULL; f = next) {
  3654 		next = f->next;
  3558 		next = f->next;
  3655 
  3559 
  3656 		free(f->filename);
  3560 		free(f->filename);
  3657 		free(f);
  3561 		free(f);
  3658 	}
  3562 	}
  3665  * Reset all NewGRF loaded data
  3569  * Reset all NewGRF loaded data
  3666  * TODO
  3570  * TODO
  3667  */
  3571  */
  3668 static void ResetNewGRFData()
  3572 static void ResetNewGRFData()
  3669 {
  3573 {
  3670 	uint i;
       
  3671 
       
  3672 	CleanUpStrings();
  3574 	CleanUpStrings();
  3673 
  3575 
  3674 	// Copy/reset original engine info data
  3576 	// Copy/reset original engine info data
  3675 	memcpy(&_engine_info, &orig_engine_info, sizeof(orig_engine_info));
  3577 	memcpy(&_engine_info, &orig_engine_info, sizeof(orig_engine_info));
  3676 	memcpy(&_rail_vehicle_info, &orig_rail_vehicle_info, sizeof(orig_rail_vehicle_info));
  3578 	memcpy(&_rail_vehicle_info, &orig_rail_vehicle_info, sizeof(orig_rail_vehicle_info));
  3678 	memcpy(&_aircraft_vehicle_info, &orig_aircraft_vehicle_info, sizeof(orig_aircraft_vehicle_info));
  3580 	memcpy(&_aircraft_vehicle_info, &orig_aircraft_vehicle_info, sizeof(orig_aircraft_vehicle_info));
  3679 	memcpy(&_road_vehicle_info, &orig_road_vehicle_info, sizeof(orig_road_vehicle_info));
  3581 	memcpy(&_road_vehicle_info, &orig_road_vehicle_info, sizeof(orig_road_vehicle_info));
  3680 
  3582 
  3681 	// Copy/reset original bridge info data
  3583 	// Copy/reset original bridge info data
  3682 	// First, free sprite table data
  3584 	// First, free sprite table data
  3683 	for (i = 0; i < MAX_BRIDGES; i++) {
  3585 	for (uint i = 0; i < MAX_BRIDGES; i++) {
  3684 		if (_bridge[i].sprite_table != NULL) {
  3586 		if (_bridge[i].sprite_table != NULL) {
  3685 			uint j;
  3587 			for (uint j = 0; j < 7; j++) free(_bridge[i].sprite_table[j]);
  3686 
       
  3687 			for (j = 0; j < 7; j++) free(_bridge[i].sprite_table[j]);
       
  3688 			free(_bridge[i].sprite_table);
  3588 			free(_bridge[i].sprite_table);
  3689 		}
  3589 		}
  3690 	}
  3590 	}
  3691 	memcpy(&_bridge, &orig_bridge, sizeof(_bridge));
  3591 	memcpy(&_bridge, &orig_bridge, sizeof(_bridge));
  3692 
  3592 
  3744 
  3644 
  3745 /** Reset all NewGRFData that was used only while processing data */
  3645 /** Reset all NewGRFData that was used only while processing data */
  3746 static void ClearTemporaryNewGRFData()
  3646 static void ClearTemporaryNewGRFData()
  3747 {
  3647 {
  3748 	/* Clear the GOTO labels used for GRF processing */
  3648 	/* Clear the GOTO labels used for GRF processing */
  3749 	GRFLabel *l;
  3649 	for (GRFLabel *l = _cur_grffile->label; l != NULL;) {
  3750 	for (l = _cur_grffile->label; l != NULL;) {
       
  3751 		GRFLabel *l2 = l->next;
  3650 		GRFLabel *l2 = l->next;
  3752 		free(l);
  3651 		free(l);
  3753 		l = l2;
  3652 		l = l2;
  3754 	}
  3653 	}
  3755 	_cur_grffile->label = NULL;
  3654 	_cur_grffile->label = NULL;
  3783 	}
  3682 	}
  3784 }
  3683 }
  3785 
  3684 
  3786 static void InitNewGRFFile(const GRFConfig *config, int sprite_offset)
  3685 static void InitNewGRFFile(const GRFConfig *config, int sprite_offset)
  3787 {
  3686 {
  3788 	GRFFile *newfile;
  3687 	GRFFile *newfile = GetFileByFilename(config->filename);
  3789 
       
  3790 	newfile = GetFileByFilename(config->filename);
       
  3791 	if (newfile != NULL) {
  3688 	if (newfile != NULL) {
  3792 		/* We already loaded it once. */
  3689 		/* We already loaded it once. */
  3793 		newfile->sprite_offset = sprite_offset;
  3690 		newfile->sprite_offset = sprite_offset;
  3794 		_cur_grffile = newfile;
  3691 		_cur_grffile = newfile;
  3795 		return;
  3692 		return;
  3852 /**
  3749 /**
  3853  * Precalculate refit masks from cargo classes for all vehicles.
  3750  * Precalculate refit masks from cargo classes for all vehicles.
  3854  */
  3751  */
  3855 static void CalculateRefitMasks()
  3752 static void CalculateRefitMasks()
  3856 {
  3753 {
  3857 	EngineID engine;
  3754 	for (EngineID engine = 0; engine < TOTAL_NUM_ENGINES; engine++) {
  3858 
       
  3859 	for (engine = 0; engine < TOTAL_NUM_ENGINES; engine++) {
       
  3860 		uint32 mask = 0;
  3755 		uint32 mask = 0;
  3861 		uint32 not_mask = 0;
  3756 		uint32 not_mask = 0;
  3862 		uint32 xor_mask = _engine_info[engine].refit_mask;
  3757 		uint32 xor_mask = _engine_info[engine].refit_mask;
  3863 		byte i;
       
  3864 
  3758 
  3865 		if (cargo_allowed[engine] != 0) {
  3759 		if (cargo_allowed[engine] != 0) {
  3866 			// Build up the list of cargo types from the set cargo classes.
  3760 			// Build up the list of cargo types from the set cargo classes.
  3867 			for (i = 0; i < NUM_CARGO; i++) {
  3761 			for (CargoID i = 0; i < NUM_CARGO; i++) {
  3868 				const CargoSpec *cs = GetCargo(i);
  3762 				const CargoSpec *cs = GetCargo(i);
  3869 				if (cargo_allowed[engine]    & cs->classes) SETBIT(mask,     i);
  3763 				if (cargo_allowed[engine]    & cs->classes) SETBIT(mask,     i);
  3870 				if (cargo_disallowed[engine] & cs->classes) SETBIT(not_mask, i);
  3764 				if (cargo_disallowed[engine] & cs->classes) SETBIT(not_mask, i);
  3871 			}
  3765 			}
  3872 		} else {
  3766 		} else {
  3956 		/* 0x12 */ { NULL,     NULL,      NULL,            NULL,       LoadFontGlyph, },
  3850 		/* 0x12 */ { NULL,     NULL,      NULL,            NULL,       LoadFontGlyph, },
  3957 		/* 0x13 */ { NULL,     NULL,      NULL,            NULL,       TranslateGRFStrings, },
  3851 		/* 0x13 */ { NULL,     NULL,      NULL,            NULL,       TranslateGRFStrings, },
  3958 	};
  3852 	};
  3959 
  3853 
  3960 	byte* buf;
  3854 	byte* buf;
  3961 	byte action;
       
  3962 
  3855 
  3963 	if (_preload_sprite == NULL) {
  3856 	if (_preload_sprite == NULL) {
  3964 		/* No preloaded sprite to work with; allocate and read the
  3857 		/* No preloaded sprite to work with; allocate and read the
  3965 		 * pseudo sprite content. */
  3858 		 * pseudo sprite content. */
  3966 		buf = MallocT<byte>(num);
  3859 		buf = MallocT<byte>(num);
  3974 
  3867 
  3975 		/* Skip the real (original) content of this action. */
  3868 		/* Skip the real (original) content of this action. */
  3976 		FioSeekTo(num, SEEK_CUR);
  3869 		FioSeekTo(num, SEEK_CUR);
  3977 	}
  3870 	}
  3978 
  3871 
  3979 	action = buf[0];
  3872 	byte action = buf[0];
  3980 
  3873 
  3981 	if (action == 0xFF) {
  3874 	if (action == 0xFF) {
  3982 		grfmsg(7, "Handling data block in stage %d", stage);
  3875 		grfmsg(7, "Handling data block in stage %d", stage);
  3983 		GRFDataBlock(buf, num);
  3876 		GRFDataBlock(buf, num);
  3984 	} else if (action == 0xFE) {
  3877 	} else if (action == 0xFE) {
  4080 
  3973 
  4081 void InitDepotWindowBlockSizes();
  3974 void InitDepotWindowBlockSizes();
  4082 
  3975 
  4083 void LoadNewGRF(uint load_index, uint file_index)
  3976 void LoadNewGRF(uint load_index, uint file_index)
  4084 {
  3977 {
  4085 	GrfLoadingStage stage;
       
  4086 
       
  4087 	InitializeGRFSpecial();
  3978 	InitializeGRFSpecial();
  4088 
  3979 
  4089 	ResetNewGRFData();
  3980 	ResetNewGRFData();
  4090 
  3981 
  4091 	/* Load newgrf sprites
  3982 	/* Load newgrf sprites
  4092 	 * in each loading stage, (try to) open each file specified in the config
  3983 	 * in each loading stage, (try to) open each file specified in the config
  4093 	 * and load information from it. */
  3984 	 * and load information from it. */
  4094 	for (stage = GLS_LABELSCAN; stage <= GLS_ACTIVATION; stage++) {
  3985 	for (GrfLoadingStage stage = GLS_LABELSCAN; stage <= GLS_ACTIVATION; stage++) {
  4095 		uint slot = file_index;
  3986 		uint slot = file_index;
  4096 		GRFConfig *c;
       
  4097 
  3987 
  4098 		_cur_stage = stage;
  3988 		_cur_stage = stage;
  4099 		_cur_spriteid = load_index;
  3989 		_cur_spriteid = load_index;
  4100 		for (c = _grfconfig; c != NULL; c = c->next) {
  3990 		for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
  4101 			if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
  3991 			if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
  4102 
  3992 
  4103 			// TODO usererror()
  3993 			// TODO usererror()
  4104 			if (!FioCheckFileExists(c->filename)) error("NewGRF file is missing '%s'", c->filename);
  3994 			if (!FioCheckFileExists(c->filename)) error("NewGRF file is missing '%s'", c->filename);
  4105 
  3995 
  4117 	CalculateRefitMasks();
  4007 	CalculateRefitMasks();
  4118 
  4008 
  4119 	/* Set the block size in the depot windows based on vehicle sprite sizes */
  4009 	/* Set the block size in the depot windows based on vehicle sprite sizes */
  4120 	InitDepotWindowBlockSizes();
  4010 	InitDepotWindowBlockSizes();
  4121 }
  4011 }
  4122 
       
  4123 
       
  4124 
       
  4125 
       
  4126