src/newgrf.cpp
changeset 6469 05a2e97144ec
parent 6468 29a9bd549d0b
child 6471 d0e79064c324
equal deleted inserted replaced
6468:29a9bd549d0b 6469:05a2e97144ec
    66 static byte *_preload_sprite = NULL;
    66 static byte *_preload_sprite = NULL;
    67 
    67 
    68 /* 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) */
    69 bool _have_2cc = false;
    69 bool _have_2cc = false;
    70 
    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];
       
    74 
    71 
    75 
    72 typedef enum GrfDataType {
    76 typedef enum GrfDataType {
    73 	GDT_SOUND,
    77 	GDT_SOUND,
    74 } GrfDataType;
    78 } GrfDataType;
    75 
    79 
  1187 					grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
  1191 					grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
  1188 				}
  1192 				}
  1189 			}
  1193 			}
  1190 			break;
  1194 			break;
  1191 
  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 
  1192 		case 0x0A: // Currency display names
  1202 		case 0x0A: // Currency display names
  1193 			FOR_EACH_OBJECT {
  1203 			FOR_EACH_OBJECT {
  1194 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1204 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1195 				StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
  1205 				StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
  1196 
  1206 
  1271 					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);
  1272 				}
  1282 				}
  1273 			}
  1283 			}
  1274 			break;
  1284 			break;
  1275 
  1285 
  1276 		case 0x09: // Cargo translation table
       
  1277 		case 0x10: // 12 * 32 * B Snow line height table
  1286 		case 0x10: // 12 * 32 * B Snow line height table
  1278 		default:
  1287 		default:
  1279 			ret = true;
  1288 			ret = true;
  1280 	}
  1289 	}
  1281 
  1290 
  1497 
  1506 
  1498 	SETBIT(_cur_grfconfig->flags, GCF_UNSAFE);
  1507 	SETBIT(_cur_grfconfig->flags, GCF_UNSAFE);
  1499 
  1508 
  1500 	/* Skip remainder of GRF */
  1509 	/* Skip remainder of GRF */
  1501 	_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 	}
  1502 }
  1560 }
  1503 
  1561 
  1504 #undef FOR_EACH_OBJECT
  1562 #undef FOR_EACH_OBJECT
  1505 
  1563 
  1506 /**
  1564 /**
  1832 	}
  1890 	}
  1833 
  1891 
  1834 	_cur_grffile->spritegroups[setid] = group;
  1892 	_cur_grffile->spritegroups[setid] = group;
  1835 }
  1893 }
  1836 
  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 GC_DEFAULT_NA;
       
  1899 	if (ctype == 0xFF) return GC_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 	/* Remap back to global cargo */
       
  1921 	ctype = GetCargo(ctype)->bitnum;
       
  1922 
       
  1923 	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);
       
  1924 	return ctype;
       
  1925 }
       
  1926 
  1837 /* Action 0x03 */
  1927 /* Action 0x03 */
  1838 static void FeatureMapSpriteGroup(byte *buf, int len)
  1928 static void FeatureMapSpriteGroup(byte *buf, int len)
  1839 {
  1929 {
  1840 	/* <03> <feature> <n-id> <ids>... <num-cid> [<cargo-type> <cid>]... <def-cid>
  1930 	/* <03> <feature> <n-id> <ids>... <num-cid> [<cargo-type> <cid>]... <def-cid>
  1841 	 * id-list    := [<id>] [id-list]
  1931 	 * id-list    := [<id>] [id-list]
  1903 					grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  1993 					grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  1904 					       groupid, _cur_grffile->spritegroups_count);
  1994 					       groupid, _cur_grffile->spritegroups_count);
  1905 					return;
  1995 					return;
  1906 				}
  1996 				}
  1907 
  1997 
  1908 				if (ctype == 0xFE) ctype = GC_DEFAULT_NA;
  1998 				ctype = TranslateCargo(feature, ctype);
  1909 				if (ctype == 0xFF) ctype = GC_PURCHASE;
  1999 				if (ctype == CT_INVALID) continue;
  1910 
       
  1911 				if (ctype >= NUM_GLOBAL_CID) {
       
  1912 					grfmsg(1, "FeatureMapSpriteGroup: Cargo type %d out of range, skipping.", ctype);
       
  1913 					continue;
       
  1914 				}
       
  1915 
  2000 
  1916 				statspec->spritegroup[ctype] = _cur_grffile->spritegroups[groupid];
  2001 				statspec->spritegroup[ctype] = _cur_grffile->spritegroups[groupid];
  1917 			}
  2002 			}
  1918 		}
  2003 		}
  1919 
  2004 
  1985 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2070 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  1986 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping", groupid, _cur_grffile->spritegroups_count);
  2071 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping", groupid, _cur_grffile->spritegroups_count);
  1987 				return;
  2072 				return;
  1988 			}
  2073 			}
  1989 
  2074 
  1990 			if (ctype == GC_INVALID) ctype = GC_PURCHASE;
  2075 			ctype = TranslateCargo(feature, ctype);
  1991 
  2076 			if (ctype == CT_INVALID) continue;
  1992 			if (ctype >= NUM_GLOBAL_CID) {
       
  1993 				grfmsg(1, "FeatureMapSpriteGroup: Cargo type %d out of range, skipping.", ctype);
       
  1994 				continue;
       
  1995 			}
       
  1996 
  2077 
  1997 			if (wagover) {
  2078 			if (wagover) {
  1998 				SetWagonOverrideSprites(engine, ctype, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
  2079 				SetWagonOverrideSprites(engine, ctype, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
  1999 			} else {
  2080 			} else {
  2000 				SetCustomEngineSprites(engine, ctype, _cur_grffile->spritegroups[groupid]);
  2081 				SetCustomEngineSprites(engine, ctype, _cur_grffile->spritegroups[groupid]);
  3587 	AddTypeToEngines();
  3668 	AddTypeToEngines();
  3588 
  3669 
  3589 	/* Set up the default cargo types */
  3670 	/* Set up the default cargo types */
  3590 	SetupCargoForClimate(_opt.landscape);
  3671 	SetupCargoForClimate(_opt.landscape);
  3591 
  3672 
       
  3673 	/* Generate default cargo translation table */
       
  3674 	memset(_default_cargo_list, 0, sizeof(_default_cargo_list));
       
  3675 	for (CargoID c = 0; c != NUM_CARGO; c++) {
       
  3676 		const CargoSpec *cs = GetCargo(c);
       
  3677 		if (cs->IsValid()) _default_cargo_list[cs->bitnum] = cs->label;
       
  3678 	}
       
  3679 
  3592 	/* Reset misc GRF features and train list display variables */
  3680 	/* Reset misc GRF features and train list display variables */
  3593 	_misc_grf_features = 0;
  3681 	_misc_grf_features = 0;
  3594 	_traininfo_vehicle_pitch = 0;
  3682 	_traininfo_vehicle_pitch = 0;
  3595 	_traininfo_vehicle_width = 29;
  3683 	_traininfo_vehicle_width = 29;
  3596 	_have_2cc = false;
  3684 	_have_2cc = false;
  3753 	 * --pasky */
  3841 	 * --pasky */
  3754 	/* We need a pre-stage to set up GOTO labels of Action 0x10 because the grf
  3842 	/* We need a pre-stage to set up GOTO labels of Action 0x10 because the grf
  3755 	 * is not in memory and scanning the file every time would be too expensive.
  3843 	 * is not in memory and scanning the file every time would be too expensive.
  3756 	 * In other stages we skip action 0x10 since it's already dealt with. */
  3844 	 * In other stages we skip action 0x10 since it's already dealt with. */
  3757 	static const SpecialSpriteHandler handlers[][GLS_END] = {
  3845 	static const SpecialSpriteHandler handlers[][GLS_END] = {
  3758 		/* 0x00 */ { NULL,     SafeChangeInfo, NULL,       NULL,       FeatureChangeInfo, },
  3846 		/* 0x00 */ { NULL,     SafeChangeInfo, NULL,       InitChangeInfo, FeatureChangeInfo, },
  3759 		/* 0x01 */ { NULL,     GRFUnsafe, NULL,            NULL,       NewSpriteSet, },
  3847 		/* 0x01 */ { NULL,     GRFUnsafe, NULL,            NULL,       NewSpriteSet, },
  3760 		/* 0x02 */ { NULL,     GRFUnsafe, NULL,            NULL,       NewSpriteGroup, },
  3848 		/* 0x02 */ { NULL,     GRFUnsafe, NULL,            NULL,       NewSpriteGroup, },
  3761 		/* 0x03 */ { NULL,     GRFUnsafe, NULL,            NULL,       FeatureMapSpriteGroup, },
  3849 		/* 0x03 */ { NULL,     GRFUnsafe, NULL,            NULL,       FeatureMapSpriteGroup, },
  3762 		/* 0x04 */ { NULL,     NULL,      NULL,            NULL,       FeatureNewName, },
  3850 		/* 0x04 */ { NULL,     NULL,      NULL,            NULL,       FeatureNewName, },
  3763 		/* 0x05 */ { NULL,     NULL,      NULL,            NULL,       GraphicsNew, },
  3851 		/* 0x05 */ { NULL,     NULL,      NULL,            NULL,       GraphicsNew, },