src/newgrf.cpp
branchcpp_gui
changeset 6268 4b5241e5dd10
parent 6254 abc6ad7c035c
child 6285 187e3ef04cc9
equal deleted inserted replaced
6267:7c8ec33959b1 6268:4b5241e5dd10
    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 #include "helpers.hpp"
       
    34 #include "cargotype.h"
    34 
    35 
    35 /* TTDPatch extended GRF format codec
    36 /* TTDPatch extended GRF format codec
    36  * (c) Petr Baudis 2004 (GPL'd)
    37  * (c) Petr Baudis 2004 (GPL'd)
    37  * Changes by Florian octo Forster are (c) by the OpenTTD development team.
    38  * Changes by Florian octo Forster are (c) by the OpenTTD development team.
    38  *
    39  *
    64 /* Used by Action 0x06 to preload a pseudo sprite and modify its content */
    65 /* Used by Action 0x06 to preload a pseudo sprite and modify its content */
    65 static byte *_preload_sprite = NULL;
    66 static byte *_preload_sprite = NULL;
    66 
    67 
    67 /* Set if any vehicle is loaded which uses 2cc (two company colours) */
    68 /* Set if any vehicle is loaded which uses 2cc (two company colours) */
    68 bool _have_2cc = false;
    69 bool _have_2cc = false;
       
    70 
       
    71 /* Default cargo translation table. By default there are 27 possible cargo types */
       
    72 static const uint _default_cargo_max = 27;
       
    73 static CargoLabel _default_cargo_list[_default_cargo_max];
    69 
    74 
    70 
    75 
    71 typedef enum GrfDataType {
    76 typedef enum GrfDataType {
    72 	GDT_SOUND,
    77 	GDT_SOUND,
    73 } GrfDataType;
    78 } GrfDataType;
   179 		case 4: return grf_load_dword(buf);
   184 		case 4: return grf_load_dword(buf);
   180 		default:
   185 		default:
   181 			NOT_REACHED();
   186 			NOT_REACHED();
   182 			return 0;
   187 			return 0;
   183 	}
   188 	}
       
   189 }
       
   190 
       
   191 static const char *grf_load_string(byte **buf, size_t max_len)
       
   192 {
       
   193 	const char *string   = *(const char **)buf;
       
   194 	size_t string_length = ttd_strnlen(string, max_len);
       
   195 
       
   196 	if (string_length == max_len) {
       
   197 		/* String was not NUL terminated, so make sure it is now. */
       
   198 		(*buf)[string_length - 1] = '\0';
       
   199 		grfmsg(7, "String was not terminated with a zero byte.");
       
   200 	} else {
       
   201 		/* Increase the string length to include the NUL byte. */
       
   202 		string_length++;
       
   203 	}
       
   204 	*buf += string_length;
       
   205 
       
   206 	return string;
   184 }
   207 }
   185 
   208 
   186 static GRFFile *GetFileByGRFID(uint32 grfid)
   209 static GRFFile *GetFileByGRFID(uint32 grfid)
   187 {
   210 {
   188 	GRFFile *file;
   211 	GRFFile *file;
   343 				uint8 ctype = grf_load_byte(&buf);
   366 				uint8 ctype = grf_load_byte(&buf);
   344 
   367 
   345 				if (ctype < NUM_CARGO) {
   368 				if (ctype < NUM_CARGO) {
   346 					rvi[i].cargo_type = ctype;
   369 					rvi[i].cargo_type = ctype;
   347 				} else {
   370 				} else {
   348 					grfmsg(2, "RailVehicleChangeInfo: Invalid cargo type %d, ignoring", ctype);
   371 					rvi[i].cargo_type = CT_INVALID;
       
   372 					grfmsg(2, "RailVehicleChangeInfo: Invalid cargo type %d, using first refittable", ctype);
   349 				}
   373 				}
   350 			}
   374 			}
   351 			break;
   375 			break;
   352 
   376 
   353 		case 0x16: /* Weight */
   377 		case 0x16: /* Weight */
   534 				uint8 cargo = grf_load_byte(&buf);
   558 				uint8 cargo = grf_load_byte(&buf);
   535 
   559 
   536 				if (cargo < NUM_CARGO) {
   560 				if (cargo < NUM_CARGO) {
   537 					rvi[i].cargo_type = cargo;
   561 					rvi[i].cargo_type = cargo;
   538 				} else {
   562 				} else {
   539 					grfmsg(2, "RoadVehicleChangeInfo: Invalid cargo type %d, ignoring", cargo);
   563 					rvi[i].cargo_type = CT_INVALID;
       
   564 					grfmsg(2, "RoadVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   540 				}
   565 				}
   541 			}
   566 			}
   542 			break;
   567 			break;
   543 
   568 
   544 		case 0x11: /* Cost factor */
   569 		case 0x11: /* Cost factor */
   644 
   669 
   645 		case 0x0C: /* Cargo type */
   670 		case 0x0C: /* Cargo type */
   646 			FOR_EACH_OBJECT {
   671 			FOR_EACH_OBJECT {
   647 				uint8 cargo = grf_load_byte(&buf);
   672 				uint8 cargo = grf_load_byte(&buf);
   648 
   673 
   649 				// XXX: Need to consult this with patchman yet.
       
   650 #if 0
       
   651 				// Documentation claims this is already the
       
   652 				// per-landscape cargo type id, but newships.grf
       
   653 				// assume otherwise.
       
   654 				cargo = local_cargo_id_ctype[cargo];
       
   655 #endif
       
   656 				if (cargo < NUM_CARGO) {
   674 				if (cargo < NUM_CARGO) {
   657 					svi[i].cargo_type = cargo;
   675 					svi[i].cargo_type = cargo;
   658 				} else {
   676 				} else {
   659 					grfmsg(2, "ShipVehicleChangeInfo: Invalid cargo type %d, ignoring", cargo);
   677 					svi[i].cargo_type = CT_INVALID;
       
   678 					grfmsg(2, "ShipVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   660 				}
   679 				}
   661 			}
   680 			}
   662 			break;
   681 			break;
   663 
   682 
   664 		case 0x0D: /* Cargo capacity */
   683 		case 0x0D: /* Cargo capacity */
  1172 					grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
  1191 					grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
  1173 				}
  1192 				}
  1174 			}
  1193 			}
  1175 			break;
  1194 			break;
  1176 
  1195 
       
  1196 		case 0x09: /* Cargo translation table */
       
  1197 			/* This is loaded during the initialisation stage, so just skip it here. */
       
  1198 			/* Each entry is 4 bytes. */
       
  1199 			buf += numinfo * 4;
       
  1200 			break;
       
  1201 
  1177 		case 0x0A: // Currency display names
  1202 		case 0x0A: // Currency display names
  1178 			FOR_EACH_OBJECT {
  1203 			FOR_EACH_OBJECT {
  1179 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1204 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1180 				StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
  1205 				StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
  1181 
  1206 
  1256 					grfmsg(1, "GlobalVarChangeInfo: Euro intro date %d out of range, ignoring", curidx);
  1281 					grfmsg(1, "GlobalVarChangeInfo: Euro intro date %d out of range, ignoring", curidx);
  1257 				}
  1282 				}
  1258 			}
  1283 			}
  1259 			break;
  1284 			break;
  1260 
  1285 
  1261 		case 0x09: // Cargo translation table
       
  1262 		case 0x10: // 12 * 32 * B Snow line height table
  1286 		case 0x10: // 12 * 32 * B Snow line height table
  1263 		default:
  1287 		default:
  1264 			ret = true;
  1288 			ret = true;
  1265 	}
  1289 	}
  1266 
  1290 
  1459 	uint8 feature;
  1483 	uint8 feature;
  1460 	uint8 numprops;
  1484 	uint8 numprops;
  1461 	uint8 numinfo;
  1485 	uint8 numinfo;
  1462 	uint8 index;
  1486 	uint8 index;
  1463 
  1487 
       
  1488 	if (len == 1) {
       
  1489 		grfmsg(8, "Silently ignoring one-byte special sprite 0x00");
       
  1490 		return;
       
  1491 	}
       
  1492 
  1464 	if (!check_length(len, 6, "SafeChangeInfo")) return;
  1493 	if (!check_length(len, 6, "SafeChangeInfo")) return;
  1465 	buf++;
  1494 	buf++;
  1466 	feature  = grf_load_byte(&buf);
  1495 	feature  = grf_load_byte(&buf);
  1467 	numprops = grf_load_byte(&buf);
  1496 	numprops = grf_load_byte(&buf);
  1468 	numinfo  = grf_load_byte(&buf);
  1497 	numinfo  = grf_load_byte(&buf);
  1477 
  1506 
  1478 	SETBIT(_cur_grfconfig->flags, GCF_UNSAFE);
  1507 	SETBIT(_cur_grfconfig->flags, GCF_UNSAFE);
  1479 
  1508 
  1480 	/* Skip remainder of GRF */
  1509 	/* Skip remainder of GRF */
  1481 	_skip_sprites = -1;
  1510 	_skip_sprites = -1;
       
  1511 }
       
  1512 
       
  1513 /* Action 0x00 (GLS_INIT) */
       
  1514 static void InitChangeInfo(byte *buf, int len)
       
  1515 {
       
  1516 	byte *bufend = buf + len;
       
  1517 	uint8 feature;
       
  1518 	uint8 numprops;
       
  1519 	uint8 numinfo;
       
  1520 	uint8 index;
       
  1521 
       
  1522 	if (len == 1) {
       
  1523 		grfmsg(8, "Silently ignoring one-byte special sprite 0x00");
       
  1524 		return;
       
  1525 	}
       
  1526 
       
  1527 	if (!check_length(len, 6, "InitChangeInfo")) return;
       
  1528 	buf++;
       
  1529 	feature  = grf_load_byte(&buf);
       
  1530 	numprops = grf_load_byte(&buf);
       
  1531 	numinfo  = grf_load_byte(&buf);
       
  1532 	index    = grf_load_byte(&buf);
       
  1533 
       
  1534 	while (numprops-- && buf < bufend) {
       
  1535 		uint8 prop = grf_load_byte(&buf);
       
  1536 
       
  1537 		switch (feature) {
       
  1538 			case GSF_GLOBALVAR:
       
  1539 				switch (prop) {
       
  1540 					case 0x09: /* Cargo Translation Table */
       
  1541 						if (index != 0) {
       
  1542 							grfmsg(1, "InitChangeInfo: Cargo translation table must start at zero");
       
  1543 							return;
       
  1544 						}
       
  1545 
       
  1546 						free(_cur_grffile->cargo_list);
       
  1547 						_cur_grffile->cargo_max = numinfo;
       
  1548 						_cur_grffile->cargo_list = MallocT<CargoLabel>(numinfo);
       
  1549 
       
  1550 						int i;
       
  1551 						FOR_EACH_OBJECT {
       
  1552 							CargoLabel cl = grf_load_dword(&buf);
       
  1553 							_cur_grffile->cargo_list[i] = BSWAP32(cl);
       
  1554 						}
       
  1555 						break;
       
  1556 				}
       
  1557 				break;
       
  1558 		}
       
  1559 	}
  1482 }
  1560 }
  1483 
  1561 
  1484 #undef FOR_EACH_OBJECT
  1562 #undef FOR_EACH_OBJECT
  1485 
  1563 
  1486 /**
  1564 /**
  1812 	}
  1890 	}
  1813 
  1891 
  1814 	_cur_grffile->spritegroups[setid] = group;
  1892 	_cur_grffile->spritegroups[setid] = group;
  1815 }
  1893 }
  1816 
  1894 
       
  1895 static CargoID TranslateCargo(uint8 feature, uint8 ctype)
       
  1896 {
       
  1897 	/* Special cargo types for purchase list and stations */
       
  1898 	if (feature == GSF_STATION && ctype == 0xFE) return CT_DEFAULT_NA;
       
  1899 	if (ctype == 0xFF) return CT_PURCHASE;
       
  1900 
       
  1901 	/* Check if the cargo type is out of bounds of the cargo translation table */
       
  1902 	if (ctype >= (_cur_grffile->cargo_max == 0 ? _default_cargo_max : _cur_grffile->cargo_max)) {
       
  1903 		grfmsg(1, "FeatureMapSpriteGroup: Cargo type %d out of range (max %d), skipping.", ctype, (_cur_grffile->cargo_max == 0 ? _default_cargo_max : _cur_grffile->cargo_max) - 1);
       
  1904 		return CT_INVALID;
       
  1905 	}
       
  1906 
       
  1907 	/* Look up the cargo label from the translation table */
       
  1908 	CargoLabel cl = _cur_grffile->cargo_max == 0 ? _default_cargo_list[ctype] : _cur_grffile->cargo_list[ctype];
       
  1909 	if (cl == 0) {
       
  1910 		grfmsg(5, "FeatureMapSpriteGroup: Cargo type %d not available in this climate, skipping.", ctype);
       
  1911 		return CT_INVALID;
       
  1912 	}
       
  1913 
       
  1914 	ctype = GetCargoIDByLabel(cl);
       
  1915 	if (ctype == CT_INVALID) {
       
  1916 		grfmsg(5, "FeatureMapSpriteGroup: Cargo '%c%c%c%c' unsupported, skipping.", GB(cl, 24, 8), GB(cl, 16, 8), GB(cl, 8, 8), GB(cl, 0, 8));
       
  1917 		return CT_INVALID;
       
  1918 	}
       
  1919 
       
  1920 	grfmsg(6, "FeatureMapSpriteGroup: Cargo '%c%c%c%c' mapped to cargo type %d.", GB(cl, 24, 8), GB(cl, 16, 8), GB(cl, 8, 8), GB(cl, 0, 8), ctype);
       
  1921 	return ctype;
       
  1922 }
       
  1923 
  1817 /* Action 0x03 */
  1924 /* Action 0x03 */
  1818 static void FeatureMapSpriteGroup(byte *buf, int len)
  1925 static void FeatureMapSpriteGroup(byte *buf, int len)
  1819 {
  1926 {
  1820 	/* <03> <feature> <n-id> <ids>... <num-cid> [<cargo-type> <cid>]... <def-cid>
  1927 	/* <03> <feature> <n-id> <ids>... <num-cid> [<cargo-type> <cid>]... <def-cid>
  1821 	 * id-list    := [<id>] [id-list]
  1928 	 * id-list    := [<id>] [id-list]
  1883 					grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  1990 					grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  1884 					       groupid, _cur_grffile->spritegroups_count);
  1991 					       groupid, _cur_grffile->spritegroups_count);
  1885 					return;
  1992 					return;
  1886 				}
  1993 				}
  1887 
  1994 
  1888 				if (ctype == 0xFE) ctype = GC_DEFAULT_NA;
  1995 				ctype = TranslateCargo(feature, ctype);
  1889 				if (ctype == 0xFF) ctype = GC_PURCHASE;
  1996 				if (ctype == CT_INVALID) continue;
  1890 
       
  1891 				if (ctype >= NUM_GLOBAL_CID) {
       
  1892 					grfmsg(1, "FeatureMapSpriteGroup: Cargo type %d out of range, skipping.", ctype);
       
  1893 					continue;
       
  1894 				}
       
  1895 
  1997 
  1896 				statspec->spritegroup[ctype] = _cur_grffile->spritegroups[groupid];
  1998 				statspec->spritegroup[ctype] = _cur_grffile->spritegroups[groupid];
  1897 			}
  1999 			}
  1898 		}
  2000 		}
  1899 
  2001 
  1909 
  2011 
  1910 			for (i = 0; i < idcount; i++) {
  2012 			for (i = 0; i < idcount; i++) {
  1911 				uint8 stid = buf[3 + i];
  2013 				uint8 stid = buf[3 + i];
  1912 				StationSpec *statspec = _cur_grffile->stations[stid];
  2014 				StationSpec *statspec = _cur_grffile->stations[stid];
  1913 
  2015 
  1914 				statspec->spritegroup[GC_DEFAULT] = _cur_grffile->spritegroups[groupid];
  2016 				statspec->spritegroup[CT_DEFAULT] = _cur_grffile->spritegroups[groupid];
  1915 				statspec->grfid = _cur_grffile->grfid;
  2017 				statspec->grfid = _cur_grffile->grfid;
  1916 				statspec->localidx = stid;
  2018 				statspec->localidx = stid;
  1917 				SetCustomStationSpec(statspec);
  2019 				SetCustomStationSpec(statspec);
  1918 			}
  2020 			}
  1919 		}
  2021 		}
  1965 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2067 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  1966 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping", groupid, _cur_grffile->spritegroups_count);
  2068 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping", groupid, _cur_grffile->spritegroups_count);
  1967 				return;
  2069 				return;
  1968 			}
  2070 			}
  1969 
  2071 
  1970 			if (ctype == GC_INVALID) ctype = GC_PURCHASE;
  2072 			ctype = TranslateCargo(feature, ctype);
  1971 
  2073 			if (ctype == CT_INVALID) continue;
  1972 			if (ctype >= NUM_GLOBAL_CID) {
       
  1973 				grfmsg(1, "FeatureMapSpriteGroup: Cargo type %d out of range, skipping.", ctype);
       
  1974 				continue;
       
  1975 			}
       
  1976 
  2074 
  1977 			if (wagover) {
  2075 			if (wagover) {
  1978 				SetWagonOverrideSprites(engine, ctype, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
  2076 				SetWagonOverrideSprites(engine, ctype, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
  1979 			} else {
  2077 			} else {
  1980 				SetCustomEngineSprites(engine, ctype, _cur_grffile->spritegroups[groupid]);
  2078 				SetCustomEngineSprites(engine, ctype, _cur_grffile->spritegroups[groupid]);
  2003 				 * this indicates we have a helicopter rotor override. */
  2101 				 * this indicates we have a helicopter rotor override. */
  2004 				if (feature == GSF_AIRCRAFT && engine == last_engines[i]) {
  2102 				if (feature == GSF_AIRCRAFT && engine == last_engines[i]) {
  2005 					SetRotorOverrideSprites(engine, _cur_grffile->spritegroups[groupid]);
  2103 					SetRotorOverrideSprites(engine, _cur_grffile->spritegroups[groupid]);
  2006 				} else {
  2104 				} else {
  2007 					// TODO: No multiple cargo types per vehicle yet. --pasky
  2105 					// TODO: No multiple cargo types per vehicle yet. --pasky
  2008 					SetWagonOverrideSprites(engine, GC_DEFAULT, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
  2106 					SetWagonOverrideSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
  2009 				}
  2107 				}
  2010 			} else {
  2108 			} else {
  2011 				SetCustomEngineSprites(engine, GC_DEFAULT, _cur_grffile->spritegroups[groupid]);
  2109 				SetCustomEngineSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid]);
  2012 				SetEngineGRF(engine, _cur_grffile);
  2110 				SetEngineGRF(engine, _cur_grffile);
  2013 				last_engines[i] = engine;
  2111 				last_engines[i] = engine;
  2014 			}
  2112 			}
  2015 		}
  2113 		}
  2016 	}
  2114 	}
  2044 	uint8 feature;
  2142 	uint8 feature;
  2045 	uint8 lang;
  2143 	uint8 lang;
  2046 	uint8 num;
  2144 	uint8 num;
  2047 	uint16 id;
  2145 	uint16 id;
  2048 	uint16 endid;
  2146 	uint16 endid;
  2049 	const char* name;
       
  2050 	bool new_scheme = _cur_grffile->grf_version >= 7;
  2147 	bool new_scheme = _cur_grffile->grf_version >= 7;
  2051 	bool generic;
  2148 	bool generic;
  2052 
  2149 
  2053 	if (!check_length(len, 6, "FeatureNewName")) return;
  2150 	if (!check_length(len, 6, "FeatureNewName")) return;
  2054 	buf++;
  2151 	buf++;
  2066 	endid    = id + num;
  2163 	endid    = id + num;
  2067 
  2164 
  2068 	grfmsg(6, "FeatureNewName: About to rename engines %d..%d (feature %d) in language 0x%02X",
  2165 	grfmsg(6, "FeatureNewName: About to rename engines %d..%d (feature %d) in language 0x%02X",
  2069 	               id, endid, feature, lang);
  2166 	               id, endid, feature, lang);
  2070 
  2167 
  2071 	name = (const char*)buf; /*transfer read value*/
       
  2072 	len -= generic ? 6 : 5;
  2168 	len -= generic ? 6 : 5;
  2073 
  2169 
  2074 	for (; id < endid && len > 0; id++) {
  2170 	for (; id < endid && len > 0; id++) {
  2075 		size_t ofs = strlen(name) + 1;
  2171 		const char *name   = grf_load_string(&buf, len);
  2076 
  2172 		size_t name_length = strlen(name) + 1;
  2077 		if (ofs < 128) {
  2173 
       
  2174 		len -= (int)name_length;
       
  2175 
       
  2176 		if (name_length == 1) {
       
  2177 			grfmsg(7, "FeatureNewName: Can't add empty name");
       
  2178 		} else if (name_length > 127) {
       
  2179 			grfmsg(7, "FeatureNewName: Too long a name (%d)", name_length);
       
  2180 		} else {
  2078 			grfmsg(8, "FeatureNewName: %d <- %s", id, name);
  2181 			grfmsg(8, "FeatureNewName: %d <- %s", id, name);
  2079 
  2182 
  2080 			switch (feature) {
  2183 			switch (feature) {
  2081 				case GSF_TRAIN:
  2184 				case GSF_TRAIN:
  2082 				case GSF_ROAD:
  2185 				case GSF_ROAD:
  2141 				default :
  2244 				default :
  2142 					grfmsg(7, "FeatureNewName: Unsupported feature (0x%02X)", feature);
  2245 					grfmsg(7, "FeatureNewName: Unsupported feature (0x%02X)", feature);
  2143 					break;
  2246 					break;
  2144 #endif
  2247 #endif
  2145 			}
  2248 			}
  2146 		} else {
       
  2147 			/* ofs is the string length + 1, so if the string is empty, ofs
       
  2148 			 * is 1 */
       
  2149 			if (ofs == 1) {
       
  2150 				grfmsg(7, "FeatureNewName: Can't add empty name");
       
  2151 			} else {
       
  2152 				grfmsg(7, "FeatureNewName: Too long a name (%d)", ofs);
       
  2153 			}
       
  2154 		}
  2249 		}
  2155 		name += ofs;
       
  2156 		len -= (int)ofs;
       
  2157 	}
  2250 	}
  2158 }
  2251 }
  2159 
  2252 
  2160 /* Action 0x05 */
  2253 /* Action 0x05 */
  2161 static void GraphicsNew(byte *buf, int len)
  2254 static void GraphicsNew(byte *buf, int len)
  2519 	if (_skip_sprites == 0) {
  2612 	if (_skip_sprites == 0) {
  2520 		/* Zero means there are no sprites to skip, so
  2613 		/* Zero means there are no sprites to skip, so
  2521 		 * we use -1 to indicate that all further
  2614 		 * we use -1 to indicate that all further
  2522 		 * sprites should be skipped. */
  2615 		 * sprites should be skipped. */
  2523 		_skip_sprites = -1;
  2616 		_skip_sprites = -1;
       
  2617 
       
  2618 		/* If an action 8 hasn't been encountered yet, disable the grf. */
       
  2619 		if (!HASBIT(_cur_grfconfig->flags, GCF_ACTIVATED)) SETBIT(_cur_grfconfig->flags, GCF_DISABLED);
  2524 	}
  2620 	}
  2525 }
  2621 }
  2526 
  2622 
  2527 
  2623 
  2528 /* Action 0x08 (GLS_FILESCAN) */
  2624 /* Action 0x08 (GLS_FILESCAN) */
  2577 	const char *name;
  2673 	const char *name;
  2578 
  2674 
  2579 	if (!check_length(len, 8, "GRFInfo")) return; buf++;
  2675 	if (!check_length(len, 8, "GRFInfo")) return; buf++;
  2580 	version = grf_load_byte(&buf);
  2676 	version = grf_load_byte(&buf);
  2581 	grfid = grf_load_dword(&buf);
  2677 	grfid = grf_load_dword(&buf);
  2582 	name = (const char*)buf;
  2678 	name = grf_load_string(&buf, len - 6);
  2583 
  2679 
  2584 	_cur_grffile->grfid = grfid;
  2680 	_cur_grffile->grfid = grfid;
  2585 	_cur_grffile->grf_version = version;
  2681 	_cur_grffile->grf_version = version;
  2586 	SETBIT(_cur_grfconfig->flags, GCF_ACTIVATED);
  2682 	SETBIT(_cur_grfconfig->flags, GCF_ACTIVATED);
  2587 
  2683 
  2620 		}
  2716 		}
  2621 	}
  2717 	}
  2622 }
  2718 }
  2623 
  2719 
  2624 /* Action 0x0B */
  2720 /* Action 0x0B */
  2625 static void GRFError(byte *buf, int len)
  2721 static void GRFLoadError(byte *buf, int len)
  2626 {
  2722 {
  2627 	/* <0B> <severity> <language-id> <message-id> [<message...> 00] [<data...>] 00 [<parnum>]
  2723 	/* <0B> <severity> <language-id> <message-id> [<message...> 00] [<data...>] 00 [<parnum>]
  2628 	 *
  2724 	 *
  2629 	 * B severity      00: notice, contine loading grf file
  2725 	 * B severity      00: notice, contine loading grf file
  2630 	 *                 01: warning, continue loading grf file
  2726 	 *                 01: warning, continue loading grf file
  2635 	 * B language-id   see action 4, use 1F for built-in error messages
  2731 	 * B language-id   see action 4, use 1F for built-in error messages
  2636 	 * B message-id    message to show, see below
  2732 	 * B message-id    message to show, see below
  2637 	 * S message       for custom messages (message-id FF), text of the message
  2733 	 * S message       for custom messages (message-id FF), text of the message
  2638 	 *                 not present for built-in messages.
  2734 	 *                 not present for built-in messages.
  2639 	 * V data          additional data for built-in (or custom) messages
  2735 	 * V data          additional data for built-in (or custom) messages
  2640 	 * B parnum        see action 6, only used with built-in message 03 */
  2736 	 * B parnum        parameter numbers to be shown in the message (maximum of 2) */
  2641 	/* TODO: For now we just show the message, sometimes incomplete and never translated. */
  2737 
  2642 
  2738 	static const StringID msgstr[] = {
  2643 	static const char *const msgstr[] = {
  2739 		STR_NEWGRF_ERROR_VERSION_NUMBER,
  2644 		"%sRequires at least pseudo-TTDPatch version %s",
  2740 		STR_NEWGRF_ERROR_DOS_OR_WINDOWS,
  2645 		"%sThis file is for %s version of TTD",
  2741 		STR_NEWGRF_ERROR_UNSET_SWITCH,
  2646 		"%sDesigned to be used with %s",
  2742 		STR_NEWGRF_ERROR_INVALID_PARAMETER,
  2647 		"%sInvalid parameter %s",
  2743 		STR_NEWGRF_ERROR_LOAD_BEFORE,
  2648 		"%sMust be loaded before %s",
  2744 		STR_NEWGRF_ERROR_LOAD_AFTER
  2649 		"%sMust be loaded after %s",
       
  2650 		"%s%s"
       
  2651 	};
  2745 	};
  2652 
  2746 
  2653 	static const char *const sevstr[] = {
  2747 	static const StringID sevstr[] = {
  2654 		"",
  2748 		STR_NEWGRF_ERROR_MSG_INFO,
  2655 		"Warning: ",
  2749 		STR_NEWGRF_ERROR_MSG_WARNING,
  2656 		"Error: ",
  2750 		STR_NEWGRF_ERROR_MSG_ERROR,
  2657 		"Fatal: ",
  2751 		STR_NEWGRF_ERROR_MSG_FATAL
  2658 	};
  2752 	};
  2659 	uint8 sevid;
  2753 
  2660 	uint8 msgid;
  2754 	/* AddGRFString expects the string to be referred to by an id in the newgrf
  2661 
  2755 	 * file. Errors messages are never referred to however, so invent ids that
  2662 	if (!check_length(len, 6, "GRFError")) return;
  2756 	 * are unlikely to be reached in a newgrf file so they don't overwrite
  2663 	sevid = buf[1];
  2757 	 * anything else. */
  2664 	msgid = buf[3];
  2758 	enum {
  2665 
  2759 		MESSAGE_STRING_ID = MAX_UVALUE(StringID) - 1,
  2666 	// Undocumented TTDPatch feature.
  2760 		MESSAGE_DATA_ID   = MAX_UVALUE(StringID)
  2667 	if (!HASBIT(sevid, 7) && _cur_stage < GLS_ACTIVATION) {
  2761 	};
  2668 		grfmsg(7, "Skipping non-fatal GRFError in stage 1");
  2762 
       
  2763 	if (!check_length(len, 6, "GRFLoadError")) return;
       
  2764 
       
  2765 	/* For now we can only show one message per newgrf file. */
       
  2766 	if (_cur_grfconfig->error != NULL) return;
       
  2767 
       
  2768 	buf++; /* Skip the action byte. */
       
  2769 	byte severity   = grf_load_byte(&buf);
       
  2770 	byte lang       = grf_load_byte(&buf);
       
  2771 	byte message_id = grf_load_byte(&buf);
       
  2772 	len -= 4;
       
  2773 
       
  2774 	/* Skip the error until the activation stage unless bit 7 of the severity
       
  2775 	 * is set. */
       
  2776 	if (!HASBIT(severity, 7) && _cur_stage < GLS_ACTIVATION) {
       
  2777 		grfmsg(7, "Skipping non-fatal GRFLoadError in stage 1");
  2669 		return;
  2778 		return;
  2670 	}
  2779 	}
  2671 
  2780 	CLRBIT(severity, 7);
  2672 	sevid = GB(sevid, 0, 2);
  2781 
  2673 	grfmsg(0,  msgstr[(msgid == 0xFF) ? lengthof(msgstr) - 1 : msgid], sevstr[sevid], &buf[4]);
  2782 	if (severity >= lengthof(sevstr)) {
       
  2783 		grfmsg(7, "GRFLoadError: Invalid severity id %d. Setting to 2 (non-fatal error).", severity);
       
  2784 		severity = 2;
       
  2785 	} else if (severity == 3) {
       
  2786 		/* This is a fatal error, so make sure the GRF is deactivated and no
       
  2787 		 * more of it gets loaded. */
       
  2788 		SETBIT(_cur_grfconfig->flags, GCF_DISABLED);
       
  2789 		CLRBIT(_cur_grfconfig->flags, GCF_ACTIVATED);
       
  2790 
       
  2791 		_skip_sprites = -1;
       
  2792 	}
       
  2793 
       
  2794 	if (message_id >= lengthof(msgstr) && message_id != 0xFF) {
       
  2795 		grfmsg(7, "GRFLoadError: Invalid message id.");
       
  2796 		return;
       
  2797 	}
       
  2798 
       
  2799 	if (len <= 1) {
       
  2800 		grfmsg(7, "GRFLoadError: No message data supplied.");
       
  2801 		return;
       
  2802 	}
       
  2803 
       
  2804 	bool new_scheme = _cur_grffile->grf_version >= 7;
       
  2805 	GRFError *error = CallocT<GRFError>(1);
       
  2806 
       
  2807 	error->severity = sevstr[severity];
       
  2808 
       
  2809 	if (message_id == 0xFF) {
       
  2810 		/* This is a custom error message. */
       
  2811 		const char *message = grf_load_string(&buf, len);
       
  2812 		len -= (strlen(message) + 1);
       
  2813 
       
  2814 		error->message = AddGRFString(_cur_grffile->grfid, MESSAGE_STRING_ID, lang, new_scheme, message, STR_UNDEFINED);
       
  2815 	} else {
       
  2816 		error->message = msgstr[message_id];
       
  2817 	}
       
  2818 
       
  2819 	if (len > 0) {
       
  2820 		const char *data = grf_load_string(&buf, len);
       
  2821 		len -= (strlen(data) + 1);
       
  2822 
       
  2823 		error->data = AddGRFString(_cur_grffile->grfid, MESSAGE_DATA_ID, lang, new_scheme, data, STR_UNDEFINED);
       
  2824 	}
       
  2825 
       
  2826 	/* Only two parameter numbers can be used in the string. */
       
  2827 	uint i = 0;
       
  2828 	for (; i < 2 && len > 0; i++) {
       
  2829 		error->param_number[i] = grf_load_byte(&buf);
       
  2830 		len--;
       
  2831 	}
       
  2832 	error->num_params = i;
       
  2833 
       
  2834 	_cur_grfconfig->error = error;
  2674 }
  2835 }
  2675 
  2836 
  2676 /* Action 0x0C */
  2837 /* Action 0x0C */
  2677 static void GRFComment(byte *buf, int len)
  2838 static void GRFComment(byte *buf, int len)
  2678 {
  2839 {
  2679 	/* <0C> [<ignored...>]
  2840 	/* <0C> [<ignored...>]
  2680 	 *
  2841 	 *
  2681 	 * V ignored       Anything following the 0C is ignored */
  2842 	 * V ignored       Anything following the 0C is ignored */
  2682 
  2843 
  2683 	static char comment[256];
       
  2684 	if (len == 1) return;
  2844 	if (len == 1) return;
  2685 
  2845 
  2686 	ttd_strlcpy(comment, (char*)(buf + 1), minu(sizeof(comment), len));
  2846 	int text_len = len - 1;
  2687 	grfmsg(2, "GRFComment: %s", comment);
  2847 	const char *text = (const char*)(buf + 1);
       
  2848 	grfmsg(2, "GRFComment: %.*s", text_len, text);
  2688 }
  2849 }
  2689 
  2850 
  2690 /* Action 0x0D (GLS_SAFETYSCAN) */
  2851 /* Action 0x0D (GLS_SAFETYSCAN) */
  2691 static void SafeParamSet(byte *buf, int len)
  2852 static void SafeParamSet(byte *buf, int len)
  2692 {
  2853 {
  3501 	ResetNewGRF();
  3662 	ResetNewGRF();
  3502 
  3663 
  3503 	// Add engine type to engine data. This is needed for the refit precalculation.
  3664 	// Add engine type to engine data. This is needed for the refit precalculation.
  3504 	AddTypeToEngines();
  3665 	AddTypeToEngines();
  3505 
  3666 
       
  3667 	/* Set up the default cargo types */
       
  3668 	SetupCargoForClimate(_opt.landscape);
       
  3669 
       
  3670 	/* Generate default cargo translation table */
       
  3671 	memset(_default_cargo_list, 0, sizeof(_default_cargo_list));
       
  3672 	for (CargoID c = 0; c != NUM_CARGO; c++) {
       
  3673 		const CargoSpec *cs = GetCargo(c);
       
  3674 		if (cs->IsValid()) _default_cargo_list[cs->bitnum] = cs->label;
       
  3675 	}
       
  3676 
  3506 	/* Reset misc GRF features and train list display variables */
  3677 	/* Reset misc GRF features and train list display variables */
  3507 	_misc_grf_features = 0;
  3678 	_misc_grf_features = 0;
  3508 	_traininfo_vehicle_pitch = 0;
  3679 	_traininfo_vehicle_pitch = 0;
  3509 	_traininfo_vehicle_width = 29;
  3680 	_traininfo_vehicle_width = 29;
  3510 	_have_2cc = false;
  3681 	_have_2cc = false;
  3531 	free(_cur_grffile->spritegroups);
  3702 	free(_cur_grffile->spritegroups);
  3532 	_cur_grffile->spritegroups = NULL;
  3703 	_cur_grffile->spritegroups = NULL;
  3533 	_cur_grffile->spritegroups_count = 0;
  3704 	_cur_grffile->spritegroups_count = 0;
  3534 }
  3705 }
  3535 
  3706 
       
  3707 static void BuildCargoTranslationMap()
       
  3708 {
       
  3709 	memset(_cur_grffile->cargo_map, 0xFF, sizeof(_cur_grffile->cargo_map));
       
  3710 
       
  3711 	for (CargoID c = 0; c < NUM_CARGO; c++) {
       
  3712 		const CargoSpec *cs = GetCargo(c);
       
  3713 		if (!cs->IsValid()) continue;
       
  3714 
       
  3715 		if (_cur_grffile->cargo_max == 0) {
       
  3716 			/* Default translation table, so just a straight mapping to bitnum */
       
  3717 			_cur_grffile->cargo_map[c] = cs->bitnum;
       
  3718 		} else {
       
  3719 			/* Check the translation table for this cargo's label */
       
  3720 			for (uint i = 0; i < _cur_grffile->cargo_max; i++) {
       
  3721 				if (cs->label == _cur_grffile->cargo_list[i]) {
       
  3722 					_cur_grffile->cargo_map[c] = i;
       
  3723 					break;
       
  3724 				}
       
  3725 			}
       
  3726 		}
       
  3727 	}
       
  3728 }
       
  3729 
  3536 static void InitNewGRFFile(const GRFConfig *config, int sprite_offset)
  3730 static void InitNewGRFFile(const GRFConfig *config, int sprite_offset)
  3537 {
  3731 {
  3538 	GRFFile *newfile;
  3732 	GRFFile *newfile;
  3539 
  3733 
  3540 	newfile = GetFileByFilename(config->filename);
  3734 	newfile = GetFileByFilename(config->filename);
  3565 		_cur_grffile = newfile;
  3759 		_cur_grffile = newfile;
  3566 	}
  3760 	}
  3567 }
  3761 }
  3568 
  3762 
  3569 
  3763 
  3570 /** Bitmasked values of what type of cargo is refittable for the given vehicle-type.
  3764 /** List of what cargo labels are refittable for the given the vehicle-type.
  3571  * This coupled with the landscape information (_landscape_global_cargo_mask) gives
  3765  * Only currently active labels are applied. */
  3572  * us exactly what is refittable and what is not */
  3766 static const CargoLabel _default_refitmasks_rail[] = {
  3573 #define MC(cargo) (1 << cargo)
  3767 	'PASS', 'COAL', 'MAIL', 'LVST', 'GOOD', 'GRAI', 'WHEA', 'MAIZ', 'WOOD',
  3574 static const uint32 _default_refitmasks[NUM_VEHICLE_TYPES] = {
  3768 	'IORE', 'STEL', 'VALU', 'GOLD', 'DIAM', 'PAPR', 'FOOD', 'FRUT', 'CORE',
  3575 	/* Trains */
  3769 	'WATR', 'SUGR', 'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL',
  3576 	MC(GC_PASSENGERS) | MC(GC_COAL)      | MC(GC_MAIL)   | MC(GC_LIVESTOCK) | MC(GC_GOODS)        | MC(GC_GRAIN)      | MC(GC_WOOD)    | MC(GC_IRON_ORE)    |
  3770 	'PLST', 'FZDR',
  3577 	MC(GC_STEEL)      | MC(GC_VALUABLES) | MC(GC_PAPER)  | MC(GC_FOOD)      | MC(GC_FRUIT)        | MC(GC_COPPER_ORE) | MC(GC_WATER)   | MC(GC_SUGAR)       |
  3771 	0 };
  3578 	MC(GC_TOYS)       | MC(GC_CANDY)     | MC(GC_TOFFEE) | MC(GC_COLA)      | MC(GC_COTTON_CANDY) | MC(GC_BUBBLES)    | MC(GC_PLASTIC) | MC(GC_FIZZY_DRINKS),
  3772 
  3579 	/* Road vehicles (not refittable by default) */
  3773 static const CargoLabel _default_refitmasks_road[] = {
  3580 	0,
  3774 	0 };
  3581 	/* Ships */
  3775 
  3582 	MC(GC_COAL)  | MC(GC_MAIL)   | MC(GC_LIVESTOCK) | MC(GC_GOODS)        | MC(GC_GRAIN)   | MC(GC_WOOD)    | MC(GC_IRON_ORE) | MC(GC_STEEL) | MC(GC_VALUABLES) |
  3776 static const CargoLabel _default_refitmasks_ships[] = {
  3583 	MC(GC_PAPER) | MC(GC_FOOD)   | MC(GC_FRUIT)     | MC(GC_COPPER_ORE)   | MC(GC_WATER)   | MC(GC_RUBBER)  | MC(GC_SUGAR)    | MC(GC_TOYS)  | MC(GC_BATTERIES) |
  3777 	'COAL', 'MAIL', 'LVST', 'GOOD', 'GRAI', 'WHEA', 'MAIZ', 'WOOD', 'IORE',
  3584 	MC(GC_CANDY) | MC(GC_TOFFEE) | MC(GC_COLA)      | MC(GC_COTTON_CANDY) | MC(GC_BUBBLES) | MC(GC_PLASTIC) | MC(GC_FIZZY_DRINKS),
  3778 	'STEL', 'VALU', 'GOLD', 'DIAM', 'PAPR', 'FOOD', 'FRUT', 'CORE', 'WATR',
  3585 	/* Aircraft */
  3779 	'RUBR', 'SUGR', 'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL',
  3586 	MC(GC_PASSENGERS) | MC(GC_MAIL)  | MC(GC_GOODS)  | MC(GC_VALUABLES) | MC(GC_FOOD)         | MC(GC_FRUIT)   | MC(GC_SUGAR)   | MC(GC_TOYS) |
  3780 	'PLST', 'FZDR',
  3587 	MC(GC_BATTERIES)  | MC(GC_CANDY) | MC(GC_TOFFEE) | MC(GC_COLA)      | MC(GC_COTTON_CANDY) | MC(GC_BUBBLES) | MC(GC_PLASTIC) | MC(GC_FIZZY_DRINKS),
  3781 	0 };
  3588 	/* Special/Disaster */
  3782 
  3589 	0,0
  3783 static const CargoLabel _default_refitmasks_aircraft[] = {
       
  3784 	'PASS', 'MAIL', 'GOOD', 'VALU', 'GOLD', 'DIAM', 'FOOD', 'FRUT', 'SUGR',
       
  3785 	'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL', 'PLST', 'FZDR',
       
  3786 	0 };
       
  3787 
       
  3788 static const CargoLabel *_default_refitmasks[] = {
       
  3789 	_default_refitmasks_rail,
       
  3790 	_default_refitmasks_road,
       
  3791 	_default_refitmasks_ships,
       
  3792 	_default_refitmasks_aircraft,
  3590 };
  3793 };
  3591 #undef MC
       
  3592 
  3794 
  3593 
  3795 
  3594 /**
  3796 /**
  3595  * Precalculate refit masks from cargo classes for all vehicles.
  3797  * Precalculate refit masks from cargo classes for all vehicles.
  3596  */
  3798  */
  3604 		uint32 xor_mask = _engine_info[engine].refit_mask;
  3806 		uint32 xor_mask = _engine_info[engine].refit_mask;
  3605 		byte i;
  3807 		byte i;
  3606 
  3808 
  3607 		if (cargo_allowed[engine] != 0) {
  3809 		if (cargo_allowed[engine] != 0) {
  3608 			// Build up the list of cargo types from the set cargo classes.
  3810 			// Build up the list of cargo types from the set cargo classes.
  3609 			for (i = 0; i < lengthof(cargo_classes); i++) {
  3811 			for (i = 0; i < NUM_CARGO; i++) {
  3610 				if (HASBIT(cargo_allowed[engine], i)) mask |= cargo_classes[i];
  3812 				const CargoSpec *cs = GetCargo(i);
  3611 				if (HASBIT(cargo_disallowed[engine], i)) not_mask |= cargo_classes[i];
  3813 				if (cargo_allowed[engine]    & cs->classes) SETBIT(mask,     i);
       
  3814 				if (cargo_disallowed[engine] & cs->classes) SETBIT(not_mask, i);
  3612 			}
  3815 			}
  3613 		} else {
  3816 		} else {
  3614 			// Don't apply default refit mask to wagons or engines with no capacity
  3817 			// Don't apply default refit mask to wagons or engines with no capacity
  3615 			if (xor_mask == 0 && (
  3818 			if (xor_mask == 0 && (
  3616 						GetEngine(engine)->type != VEH_Train || (
  3819 						GetEngine(engine)->type != VEH_Train || (
  3617 							RailVehInfo(engine)->capacity != 0 &&
  3820 							RailVehInfo(engine)->capacity != 0 &&
  3618 							RailVehInfo(engine)->railveh_type != RAILVEH_WAGON
  3821 							RailVehInfo(engine)->railveh_type != RAILVEH_WAGON
  3619 						)
  3822 						)
  3620 					)) {
  3823 					)) {
  3621 				xor_mask = _default_refitmasks[GetEngine(engine)->type];
  3824 				const CargoLabel *cl = _default_refitmasks[GetEngine(engine)->type];
       
  3825 				for (uint i = 0;; i++) {
       
  3826 					if (cl[i] == 0) break;
       
  3827 
       
  3828 					CargoID cargo = GetCargoIDByLabel(cl[i]);
       
  3829 					if (cargo == CT_INVALID) continue;
       
  3830 
       
  3831 					SETBIT(xor_mask, cargo);
       
  3832 				}
  3622 			}
  3833 			}
  3623 		}
  3834 		}
  3624 		_engine_info[engine].refit_mask = ((mask & ~not_mask) ^ xor_mask) & _landscape_global_cargo_mask[_opt.landscape];
  3835 		_engine_info[engine].refit_mask = ((mask & ~not_mask) ^ xor_mask) & _cargo_mask;
       
  3836 
       
  3837 		if (_engine_info[engine].refit_mask == 0) continue;
       
  3838 
       
  3839 		/* Check if this engine's cargo type is valid. If not, set to the first refittable
       
  3840 		 * cargo type. Apparently cargo_type isn't a common property... */
       
  3841 		switch (GetEngine(engine)->type) {
       
  3842 			case VEH_Train: {
       
  3843 				RailVehicleInfo *rvi = &_rail_vehicle_info[engine];
       
  3844 				if (rvi->cargo_type == CT_INVALID) rvi->cargo_type = FindFirstRefittableCargo(engine);
       
  3845 				break;
       
  3846 			}
       
  3847 			case VEH_Road: {
       
  3848 				RoadVehicleInfo *rvi = &_road_vehicle_info[engine - ROAD_ENGINES_INDEX];
       
  3849 				if (rvi->cargo_type == CT_INVALID) rvi->cargo_type = FindFirstRefittableCargo(engine);
       
  3850 				break;
       
  3851 			}
       
  3852 			case VEH_Ship: {
       
  3853 				ShipVehicleInfo *svi = &_ship_vehicle_info[engine - SHIP_ENGINES_INDEX];
       
  3854 				if (svi->cargo_type == CT_INVALID) svi->cargo_type = FindFirstRefittableCargo(engine);
       
  3855 				break;
       
  3856 			}
       
  3857 		}
  3625 	}
  3858 	}
  3626 }
  3859 }
  3627 
  3860 
  3628 /* Here we perform initial decoding of some special sprites (as are they
  3861 /* Here we perform initial decoding of some special sprites (as are they
  3629  * described at http://www.ttdpatch.net/src/newgrf.txt, but this is only a very
  3862  * described at http://www.ttdpatch.net/src/newgrf.txt, but this is only a very
  3644 	 * --pasky */
  3877 	 * --pasky */
  3645 	/* We need a pre-stage to set up GOTO labels of Action 0x10 because the grf
  3878 	/* We need a pre-stage to set up GOTO labels of Action 0x10 because the grf
  3646 	 * is not in memory and scanning the file every time would be too expensive.
  3879 	 * is not in memory and scanning the file every time would be too expensive.
  3647 	 * In other stages we skip action 0x10 since it's already dealt with. */
  3880 	 * In other stages we skip action 0x10 since it's already dealt with. */
  3648 	static const SpecialSpriteHandler handlers[][GLS_END] = {
  3881 	static const SpecialSpriteHandler handlers[][GLS_END] = {
  3649 		/* 0x00 */ { NULL,     SafeChangeInfo, NULL,       NULL,       FeatureChangeInfo, },
  3882 		/* 0x00 */ { NULL,     SafeChangeInfo, NULL,       InitChangeInfo, FeatureChangeInfo, },
  3650 		/* 0x01 */ { NULL,     GRFUnsafe, NULL,            NULL,       NewSpriteSet, },
  3883 		/* 0x01 */ { NULL,     GRFUnsafe, NULL,            NULL,       NewSpriteSet, },
  3651 		/* 0x02 */ { NULL,     GRFUnsafe, NULL,            NULL,       NewSpriteGroup, },
  3884 		/* 0x02 */ { NULL,     GRFUnsafe, NULL,            NULL,       NewSpriteGroup, },
  3652 		/* 0x03 */ { NULL,     GRFUnsafe, NULL,            NULL,       FeatureMapSpriteGroup, },
  3885 		/* 0x03 */ { NULL,     GRFUnsafe, NULL,            NULL,       FeatureMapSpriteGroup, },
  3653 		/* 0x04 */ { NULL,     NULL,      NULL,            NULL,       FeatureNewName, },
  3886 		/* 0x04 */ { NULL,     NULL,      NULL,            NULL,       FeatureNewName, },
  3654 		/* 0x05 */ { NULL,     NULL,      NULL,            NULL,       GraphicsNew, },
  3887 		/* 0x05 */ { NULL,     NULL,      NULL,            NULL,       GraphicsNew, },
  3655 		/* 0x06 */ { NULL,     NULL,      NULL,            CfgApply,   CfgApply, },
  3888 		/* 0x06 */ { NULL,     NULL,      NULL,            CfgApply,   CfgApply, },
  3656 		/* 0x07 */ { NULL,     NULL,      NULL,            NULL,       SkipIf, },
  3889 		/* 0x07 */ { NULL,     NULL,      NULL,            NULL,       SkipIf, },
  3657 		/* 0x08 */ { ScanInfo, NULL,      NULL,            GRFInfo,    GRFInfo, },
  3890 		/* 0x08 */ { ScanInfo, NULL,      NULL,            GRFInfo,    GRFInfo, },
  3658 		/* 0x09 */ { NULL,     NULL,      NULL,            SkipIf,     SkipIf, },
  3891 		/* 0x09 */ { NULL,     NULL,      NULL,            SkipIf,     SkipIf, },
  3659 		/* 0x0A */ { NULL,     NULL,      NULL,            NULL,       SpriteReplace, },
  3892 		/* 0x0A */ { NULL,     NULL,      NULL,            NULL,       SpriteReplace, },
  3660 		/* 0x0B */ { NULL,     NULL,      NULL,            GRFError,   GRFError, },
  3893 		/* 0x0B */ { NULL,     NULL,      NULL,            GRFLoadError, GRFLoadError, },
  3661 		/* 0x0C */ { NULL,     NULL,      NULL,            GRFComment, GRFComment, },
  3894 		/* 0x0C */ { NULL,     NULL,      NULL,            GRFComment, GRFComment, },
  3662 		/* 0x0D */ { NULL,     SafeParamSet, NULL,         ParamSet,   ParamSet, },
  3895 		/* 0x0D */ { NULL,     SafeParamSet, NULL,         ParamSet,   ParamSet, },
  3663 		/* 0x0E */ { NULL,     SafeGRFInhibit, NULL,       GRFInhibit, GRFInhibit, },
  3896 		/* 0x0E */ { NULL,     SafeGRFInhibit, NULL,       GRFInhibit, GRFInhibit, },
  3664 		/* 0x0F */ { NULL,     NULL,      NULL,            NULL,       NULL, },
  3897 		/* 0x0F */ { NULL,     NULL,      NULL,            NULL,       NULL, },
  3665 		/* 0x10 */ { NULL,     NULL,      DefineGotoLabel, NULL,       NULL, },
  3898 		/* 0x10 */ { NULL,     NULL,      DefineGotoLabel, NULL,       NULL, },
  3723 	if (stage != GLS_FILESCAN && stage != GLS_SAFETYSCAN && stage != GLS_LABELSCAN) {
  3956 	if (stage != GLS_FILESCAN && stage != GLS_SAFETYSCAN && stage != GLS_LABELSCAN) {
  3724 		_cur_grffile = GetFileByFilename(filename);
  3957 		_cur_grffile = GetFileByFilename(filename);
  3725 		if (_cur_grffile == NULL) error("File '%s' lost in cache.\n", filename);
  3958 		if (_cur_grffile == NULL) error("File '%s' lost in cache.\n", filename);
  3726 		if (stage == GLS_ACTIVATION && !HASBIT(config->flags, GCF_ACTIVATED)) return;
  3959 		if (stage == GLS_ACTIVATION && !HASBIT(config->flags, GCF_ACTIVATED)) return;
  3727 	}
  3960 	}
       
  3961 
       
  3962 	if (stage == GLS_ACTIVATION) CLRBIT(config->flags, GCF_ACTIVATED);
  3728 
  3963 
  3729 	FioOpenFile(file_index, filename);
  3964 	FioOpenFile(file_index, filename);
  3730 	_file_index = file_index; // XXX
  3965 	_file_index = file_index; // XXX
  3731 
  3966 
  3732 	_cur_grfconfig = config;
  3967 	_cur_grfconfig = config;
  3815 
  4050 
  3816 			if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
  4051 			if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
  3817 			LoadNewGRFFile(c, slot++, stage);
  4052 			LoadNewGRFFile(c, slot++, stage);
  3818 			if (stage == GLS_ACTIVATION) {
  4053 			if (stage == GLS_ACTIVATION) {
  3819 				ClearTemporaryNewGRFData();
  4054 				ClearTemporaryNewGRFData();
       
  4055 				BuildCargoTranslationMap();
  3820 				DEBUG(sprite, 2, "Currently %i sprites are loaded", _cur_spriteid);
  4056 				DEBUG(sprite, 2, "Currently %i sprites are loaded", _cur_spriteid);
  3821 			}
  4057 			}
  3822 		}
  4058 		}
  3823 	}
  4059 	}
  3824 
  4060