src/newgrf.cpp
branchcpp_gui
changeset 6308 646711c5feaa
parent 6307 f40e88cff863
equal deleted inserted replaced
6307:f40e88cff863 6308:646711c5feaa
   134  * addition of file:line information when parsing grf files.
   134  * addition of file:line information when parsing grf files.
   135  * NOTE: for the above reason(s) grfmsg() should ONLY be used for
   135  * NOTE: for the above reason(s) grfmsg() should ONLY be used for
   136  * loading/parsing grf files, not for runtime debug messages as there
   136  * loading/parsing grf files, not for runtime debug messages as there
   137  * is no file information available during that time.
   137  * is no file information available during that time.
   138  * @param severity debugging severity level, see debug.h
   138  * @param severity debugging severity level, see debug.h
   139  * @param debugging message in printf() format */
   139  * @param str message in printf() format */
   140 void CDECL grfmsg(int severity, const char *str, ...)
   140 void CDECL grfmsg(int severity, const char *str, ...)
   141 {
   141 {
   142 	char buf[1024];
   142 	char buf[1024];
   143 	va_list va;
   143 	va_list va;
   144 
   144 
   232 }
   232 }
   233 
   233 
   234 
   234 
   235 /** Used when setting an object's property to map to the GRF's strings
   235 /** Used when setting an object's property to map to the GRF's strings
   236  * while taking in consideration the "drift" between TTDPatch string system and OpenTTD's one
   236  * while taking in consideration the "drift" between TTDPatch string system and OpenTTD's one
       
   237  * @param grfid Id of the grf file
   237  * @param str StringID that we want to have the equivalent in OoenTTD
   238  * @param str StringID that we want to have the equivalent in OoenTTD
   238  * @return the properly adjusted StringID
   239  * @return the properly adjusted StringID
   239  */
   240  */
   240 static StringID MapGRFStringID(uint32 grfid, StringID str)
   241 static StringID MapGRFStringID(uint32 grfid, StringID str)
   241 {
   242 {
  1369 			break;
  1370 			break;
  1370 
  1371 
  1371 		case 0x17: // Four random colours to use
  1372 		case 0x17: // Four random colours to use
  1372 			FOR_EACH_OBJECT {
  1373 			FOR_EACH_OBJECT {
  1373 				uint j;
  1374 				uint j;
  1374 				for (j = 0; j < 4; j++)	housespec[i]->random_colour[j] = grf_load_byte(&buf);
  1375 				for (j = 0; j < 4; j++) housespec[i]->random_colour[j] = grf_load_byte(&buf);
  1375 			}
  1376 			}
  1376 			break;
  1377 			break;
  1377 
  1378 
  1378 		case 0x18: // Relative probability of appearing
  1379 		case 0x18: // Relative probability of appearing
  1379 			FOR_EACH_OBJECT housespec[i]->probability = grf_load_byte(&buf);
  1380 			FOR_EACH_OBJECT housespec[i]->probability = grf_load_byte(&buf);
  1520 			FOR_EACH_OBJECT {
  1521 			FOR_EACH_OBJECT {
  1521 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1522 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1522 				uint32 tempfix = grf_load_dword(&buf);
  1523 				uint32 tempfix = grf_load_dword(&buf);
  1523 
  1524 
  1524 				if (curidx < NUM_CURRENCY) {
  1525 				if (curidx < NUM_CURRENCY) {
  1525 					memcpy(&_currency_specs[curidx].suffix,&tempfix,4);
  1526 					memcpy(&_currency_specs[curidx].suffix, &tempfix, 4);
  1526 					_currency_specs[curidx].suffix[4] = 0;
  1527 					_currency_specs[curidx].suffix[4] = 0;
  1527 				} else {
  1528 				} else {
  1528 					grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
  1529 					grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
  1529 				}
  1530 				}
  1530 			}
  1531 			}
  1969 
  1970 
  1970 	while (numprops-- && buf < bufend) {
  1971 	while (numprops-- && buf < bufend) {
  1971 		uint8 prop = grf_load_byte(&buf);
  1972 		uint8 prop = grf_load_byte(&buf);
  1972 
  1973 
  1973 		if (CargoChangeInfo(index, numinfo, prop, &buf, bufend - buf)) {
  1974 		if (CargoChangeInfo(index, numinfo, prop, &buf, bufend - buf)) {
  1974 			grfmsg(2, "FeatureChangeInfo: Ignoring property 0x%02X (not implemented)", prop);
  1975 			grfmsg(2, "ReserveChangeInfo: Ignoring property 0x%02X (not implemented)", prop);
  1975 		}
  1976 		}
  1976 	}
  1977 	}
  1977 }
  1978 }
  1978 
  1979 
  1979 #undef FOR_EACH_OBJECT
  1980 #undef FOR_EACH_OBJECT
  2002 	return group;
  2003 	return group;
  2003 }
  2004 }
  2004 
  2005 
  2005 /**
  2006 /**
  2006  * Creates a spritegroup representing a sprite number result.
  2007  * Creates a spritegroup representing a sprite number result.
  2007  * @param value The sprite number.
  2008  * @param sprite The sprite number.
  2008  * @param sprites The number of sprites per set.
  2009  * @param num_sprites The number of sprites per set.
  2009  * @return A spritegroup representing the sprite number result.
  2010  * @return A spritegroup representing the sprite number result.
  2010  */
  2011  */
  2011 static const SpriteGroup* NewResultSpriteGroup(SpriteID sprite, byte num_sprites)
  2012 static const SpriteGroup* NewResultSpriteGroup(SpriteID sprite, byte num_sprites)
  2012 {
  2013 {
  2013 	SpriteGroup *group = AllocateSpriteGroup();
  2014 	SpriteGroup *group = AllocateSpriteGroup();
  2058 static const SpriteGroup* GetGroupFromGroupID(byte setid, byte type, uint16 groupid)
  2059 static const SpriteGroup* GetGroupFromGroupID(byte setid, byte type, uint16 groupid)
  2059 {
  2060 {
  2060 	if (HASBIT(groupid, 15)) return NewCallBackResultSpriteGroup(groupid);
  2061 	if (HASBIT(groupid, 15)) return NewCallBackResultSpriteGroup(groupid);
  2061 
  2062 
  2062 	if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2063 	if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2063 		grfmsg(1, "NewSpriteGroup(0x%02X:0x%02X): Groupid 0x%04X does not exist, leaving empty", setid, type, groupid);
  2064 		grfmsg(1, "GetGroupFromGroupID(0x%02X:0x%02X): Groupid 0x%04X does not exist, leaving empty", setid, type, groupid);
  2064 		return NULL;
  2065 		return NULL;
  2065 	}
  2066 	}
  2066 
  2067 
  2067 	return _cur_grffile->spritegroups[groupid];
  2068 	return _cur_grffile->spritegroups[groupid];
  2068 }
  2069 }
  2071 static const SpriteGroup* CreateGroupFromGroupID(byte feature, byte setid, byte type, uint16 spriteid, uint16 num_sprites)
  2072 static const SpriteGroup* CreateGroupFromGroupID(byte feature, byte setid, byte type, uint16 spriteid, uint16 num_sprites)
  2072 {
  2073 {
  2073 	if (HASBIT(spriteid, 15)) return NewCallBackResultSpriteGroup(spriteid);
  2074 	if (HASBIT(spriteid, 15)) return NewCallBackResultSpriteGroup(spriteid);
  2074 
  2075 
  2075 	if (spriteid >= _cur_grffile->spriteset_numsets) {
  2076 	if (spriteid >= _cur_grffile->spriteset_numsets) {
  2076 		grfmsg(1, "NewSpriteGroup(0x%02X:0x%02X): Sprite set %u invalid, max %u", setid, type, spriteid, _cur_grffile->spriteset_numsets);
  2077 		grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set %u invalid, max %u", setid, type, spriteid, _cur_grffile->spriteset_numsets);
  2077 		return NULL;
  2078 		return NULL;
  2078 	}
  2079 	}
  2079 
  2080 
  2080 	/* Check if the sprite is within range. This can fail if the Action 0x01
  2081 	/* Check if the sprite is within range. This can fail if the Action 0x01
  2081 	 * is skipped, as TTDPatch mandates that Action 0x02s must be processed.
  2082 	 * is skipped, as TTDPatch mandates that Action 0x02s must be processed.
  2082 	 * We don't have that rule, but must live by the Patch... */
  2083 	 * We don't have that rule, but must live by the Patch... */
  2083 	if (_cur_grffile->spriteset_start + spriteid * num_sprites + num_sprites > _cur_spriteid) {
  2084 	if (_cur_grffile->spriteset_start + spriteid * num_sprites + num_sprites > _cur_spriteid) {
  2084 		grfmsg(1, "NewSpriteGroup(0x%02X:0x%02X): Real Sprite IDs 0x%04X - 0x%04X do not (all) exist (max 0x%04X), leaving empty",
  2085 		grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Real Sprite IDs 0x%04X - 0x%04X do not (all) exist (max 0x%04X), leaving empty",
  2085 				setid, type,
  2086 				setid, type,
  2086 				_cur_grffile->spriteset_start + spriteid * num_sprites,
  2087 				_cur_grffile->spriteset_start + spriteid * num_sprites,
  2087 				_cur_grffile->spriteset_start + spriteid * num_sprites + num_sprites - 1, _cur_spriteid - 1);
  2088 				_cur_grffile->spriteset_start + spriteid * num_sprites + num_sprites - 1, _cur_spriteid - 1);
  2088 		return NULL;
  2089 		return NULL;
  2089 	}
  2090 	}
  2090 
  2091 
  2091 	if (feature != _cur_grffile->spriteset_feature) {
  2092 	if (feature != _cur_grffile->spriteset_feature) {
  2092 		grfmsg(1, "NewSpriteGroup(0x%02X:0x%02X): Sprite set feature 0x%02X does not match action feature 0x%02X, skipping",
  2093 		grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set feature 0x%02X does not match action feature 0x%02X, skipping",
  2093 				_cur_grffile->spriteset_feature, feature);
  2094 				_cur_grffile->spriteset_feature, feature);
  2094 		return NULL;
  2095 		return NULL;
  2095 	}
  2096 	}
  2096 
  2097 
  2097 	return NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteid * num_sprites, num_sprites);
  2098 	return NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteid * num_sprites, num_sprites);
  2383 	if (ctype == 0xFF) return CT_PURCHASE;
  2384 	if (ctype == 0xFF) return CT_PURCHASE;
  2384 
  2385 
  2385 	if (_cur_grffile->cargo_max == 0) {
  2386 	if (_cur_grffile->cargo_max == 0) {
  2386 		/* No cargo table, so use bitnum values */
  2387 		/* No cargo table, so use bitnum values */
  2387 		if (ctype >= 32) {
  2388 		if (ctype >= 32) {
  2388 			grfmsg(1, "FeatureMapSpriteGroup: Cargo bitnum %d out of range (max 31), skipping.", ctype);
  2389 			grfmsg(1, "TranslateCargo: Cargo bitnum %d out of range (max 31), skipping.", ctype);
  2389 			return CT_INVALID;
  2390 			return CT_INVALID;
  2390 		}
  2391 		}
  2391 
  2392 
  2392 		for (CargoID c = 0; c < NUM_CARGO; c++) {
  2393 		for (CargoID c = 0; c < NUM_CARGO; c++) {
  2393 			const CargoSpec *cs = GetCargo(c);
  2394 			const CargoSpec *cs = GetCargo(c);
  2394 			if (!cs->IsValid()) continue;
  2395 			if (!cs->IsValid()) continue;
  2395 
  2396 
  2396 			if (cs->bitnum == ctype) {
  2397 			if (cs->bitnum == ctype) {
  2397 				grfmsg(6, "FeatureMapSpriteGroup: Cargo bitnum %d mapped to cargo type %d.", ctype, c);
  2398 				grfmsg(6, "TranslateCargo: Cargo bitnum %d mapped to cargo type %d.", ctype, c);
  2398 				return c;
  2399 				return c;
  2399 			}
  2400 			}
  2400 		}
  2401 		}
  2401 
  2402 
  2402 		grfmsg(5, "FeatureMapSpriteGroup: Cargo bitnum %d not available in this climate, skipping.", ctype);
  2403 		grfmsg(5, "TranslateCargo: Cargo bitnum %d not available in this climate, skipping.", ctype);
  2403 		return CT_INVALID;
  2404 		return CT_INVALID;
  2404 	}
  2405 	}
  2405 
  2406 
  2406 	/* Check if the cargo type is out of bounds of the cargo translation table */
  2407 	/* Check if the cargo type is out of bounds of the cargo translation table */
  2407 	if (ctype >= _cur_grffile->cargo_max) {
  2408 	if (ctype >= _cur_grffile->cargo_max) {
  2408 		grfmsg(1, "FeatureMapSpriteGroup: Cargo type %d out of range (max %d), skipping.", ctype, _cur_grffile->cargo_max - 1);
  2409 		grfmsg(1, "TranslateCargo: Cargo type %d out of range (max %d), skipping.", ctype, _cur_grffile->cargo_max - 1);
  2409 		return CT_INVALID;
  2410 		return CT_INVALID;
  2410 	}
  2411 	}
  2411 
  2412 
  2412 	/* Look up the cargo label from the translation table */
  2413 	/* Look up the cargo label from the translation table */
  2413 	CargoLabel cl = _cur_grffile->cargo_list[ctype];
  2414 	CargoLabel cl = _cur_grffile->cargo_list[ctype];
  2414 	if (cl == 0) {
  2415 	if (cl == 0) {
  2415 		grfmsg(5, "FeatureMapSpriteGroup: Cargo type %d not available in this climate, skipping.", ctype);
  2416 		grfmsg(5, "TranslateCargo: Cargo type %d not available in this climate, skipping.", ctype);
  2416 		return CT_INVALID;
  2417 		return CT_INVALID;
  2417 	}
  2418 	}
  2418 
  2419 
  2419 	ctype = GetCargoIDByLabel(cl);
  2420 	ctype = GetCargoIDByLabel(cl);
  2420 	if (ctype == CT_INVALID) {
  2421 	if (ctype == CT_INVALID) {
  2421 		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));
  2422 		grfmsg(5, "TranslateCargo: Cargo '%c%c%c%c' unsupported, skipping.", GB(cl, 24, 8), GB(cl, 16, 8), GB(cl, 8, 8), GB(cl, 0, 8));
  2422 		return CT_INVALID;
  2423 		return CT_INVALID;
  2423 	}
  2424 	}
  2424 
  2425 
  2425 	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);
  2426 	grfmsg(6, "TranslateCargo: 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);
  2426 	return ctype;
  2427 	return ctype;
  2427 }
  2428 }
  2428 
  2429 
  2429 
  2430 
  2430 static void VehicleMapSpriteGroup(byte *buf, byte feature, uint8 idcount, uint8 cidcount, bool wagover)
  2431 static void VehicleMapSpriteGroup(byte *buf, byte feature, uint8 idcount, uint8 cidcount, bool wagover)
  2437 			last_engines = ReallocT(last_engines, idcount);
  2438 			last_engines = ReallocT(last_engines, idcount);
  2438 			last_engines_count = idcount;
  2439 			last_engines_count = idcount;
  2439 		}
  2440 		}
  2440 	} else {
  2441 	} else {
  2441 		if (last_engines_count == 0) {
  2442 		if (last_engines_count == 0) {
  2442 			grfmsg(0, "FeatureMapSpriteGroup: WagonOverride: No engine to do override with");
  2443 			grfmsg(0, "VehicleMapSpriteGroup: WagonOverride: No engine to do override with");
  2443 			return;
  2444 			return;
  2444 		}
  2445 		}
  2445 
  2446 
  2446 		grfmsg(6, "FeatureMapSpriteGroup: WagonOverride: %u engines, %u wagons",
  2447 		grfmsg(6, "VehicleMapSpriteGroup: WagonOverride: %u engines, %u wagons",
  2447 				last_engines_count, idcount);
  2448 				last_engines_count, idcount);
  2448 	}
  2449 	}
  2449 
  2450 
  2450 	for (uint i = 0; i < idcount; i++) {
  2451 	for (uint i = 0; i < idcount; i++) {
  2451 		uint8 engine_id = buf[3 + i];
  2452 		uint8 engine_id = buf[3 + i];
  2455 		if (engine_id > _vehcounts[feature]) {
  2456 		if (engine_id > _vehcounts[feature]) {
  2456 			grfmsg(0, "Id %u for feature 0x%02X is out of bounds", engine_id, feature);
  2457 			grfmsg(0, "Id %u for feature 0x%02X is out of bounds", engine_id, feature);
  2457 			return;
  2458 			return;
  2458 		}
  2459 		}
  2459 
  2460 
  2460 		grfmsg(7, "FeatureMapSpriteGroup: [%d] Engine %d...", i, engine);
  2461 		grfmsg(7, "VehicleMapSpriteGroup: [%d] Engine %d...", i, engine);
  2461 
  2462 
  2462 		for (uint c = 0; c < cidcount; c++) {
  2463 		for (uint c = 0; c < cidcount; c++) {
  2463 			uint8 ctype = grf_load_byte(&bp);
  2464 			uint8 ctype = grf_load_byte(&bp);
  2464 			uint16 groupid = grf_load_word(&bp);
  2465 			uint16 groupid = grf_load_word(&bp);
  2465 
  2466 
  2466 			grfmsg(8, "FeatureMapSpriteGroup: * [%d] Cargo type 0x%X, group id 0x%02X", c, ctype, groupid);
  2467 			grfmsg(8, "VehicleMapSpriteGroup: * [%d] Cargo type 0x%X, group id 0x%02X", c, ctype, groupid);
  2467 
  2468 
  2468 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2469 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2469 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping", groupid, _cur_grffile->spritegroups_count);
  2470 				grfmsg(1, "VehicleMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping", groupid, _cur_grffile->spritegroups_count);
  2470 				continue;
  2471 				continue;
  2471 			}
  2472 			}
  2472 
  2473 
  2473 			ctype = TranslateCargo(feature, ctype);
  2474 			ctype = TranslateCargo(feature, ctype);
  2474 			if (ctype == CT_INVALID) continue;
  2475 			if (ctype == CT_INVALID) continue;
  2527 		for (uint c = 0; c < cidcount; c++) {
  2528 		for (uint c = 0; c < cidcount; c++) {
  2528 			uint8 ctype = grf_load_byte(&bp);
  2529 			uint8 ctype = grf_load_byte(&bp);
  2529 			uint16 groupid = grf_load_word(&bp);
  2530 			uint16 groupid = grf_load_word(&bp);
  2530 
  2531 
  2531 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2532 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2532 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  2533 				grfmsg(1, "StationMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  2533 				       groupid, _cur_grffile->spritegroups_count);
  2534 				       groupid, _cur_grffile->spritegroups_count);
  2534 				continue;
  2535 				continue;
  2535 			}
  2536 			}
  2536 
  2537 
  2537 			ctype = TranslateCargo(GSF_STATION, ctype);
  2538 			ctype = TranslateCargo(GSF_STATION, ctype);
  2544 	{
  2545 	{
  2545 		byte *bp = &buf[4 + idcount + cidcount * 3];
  2546 		byte *bp = &buf[4 + idcount + cidcount * 3];
  2546 		uint16 groupid = grf_load_word(&bp);
  2547 		uint16 groupid = grf_load_word(&bp);
  2547 
  2548 
  2548 		if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2549 		if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2549 			grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  2550 			grfmsg(1, "StationMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  2550 			       groupid, _cur_grffile->spritegroups_count);
  2551 			       groupid, _cur_grffile->spritegroups_count);
  2551 			return;
  2552 			return;
  2552 		}
  2553 		}
  2553 
  2554 
  2554 		for (uint i = 0; i < idcount; i++) {
  2555 		for (uint i = 0; i < idcount; i++) {
  2568 {
  2569 {
  2569 	byte *bp = &buf[4 + idcount + cidcount * 3];
  2570 	byte *bp = &buf[4 + idcount + cidcount * 3];
  2570 	uint16 groupid = grf_load_word(&bp);
  2571 	uint16 groupid = grf_load_word(&bp);
  2571 
  2572 
  2572 	if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2573 	if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2573 		grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping.",
  2574 		grfmsg(1, "TownHouseMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping.",
  2574 		       groupid, _cur_grffile->spritegroups_count);
  2575 		       groupid, _cur_grffile->spritegroups_count);
  2575 		return;
  2576 		return;
  2576 	}
  2577 	}
  2577 
  2578 
  2578 	for (uint i = 0; i < idcount; i++) {
  2579 	for (uint i = 0; i < idcount; i++) {
  2579 		uint8 hid = buf[3 + i];
  2580 		uint8 hid = buf[3 + i];
  2580 		HouseSpec *hs = _cur_grffile->housespec[hid];
  2581 		HouseSpec *hs = _cur_grffile->housespec[hid];
  2581 
  2582 
  2582 		if (hs == NULL) {
  2583 		if (hs == NULL) {
  2583 			grfmsg(1, "FeatureMapSpriteGroup: Too many houses defined, skipping");
  2584 			grfmsg(1, "TownHouseMapSpriteGroup: Too many houses defined, skipping");
  2584 			return;
  2585 			return;
  2585 		}
  2586 		}
  2586 
  2587 
  2587 		hs->spritegroup = _cur_grffile->spritegroups[groupid];
  2588 		hs->spritegroup = _cur_grffile->spritegroups[groupid];
  2588 	}
  2589 	}
  2593 {
  2594 {
  2594 	byte *bp = &buf[4 + idcount + cidcount * 3];
  2595 	byte *bp = &buf[4 + idcount + cidcount * 3];
  2595 	uint16 groupid = grf_load_word(&bp);
  2596 	uint16 groupid = grf_load_word(&bp);
  2596 
  2597 
  2597 	if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2598 	if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2598 		grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping.",
  2599 		grfmsg(1, "CargoMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping.",
  2599 		       groupid, _cur_grffile->spritegroups_count);
  2600 		       groupid, _cur_grffile->spritegroups_count);
  2600 		return;
  2601 		return;
  2601 	}
  2602 	}
  2602 
  2603 
  2603 	for (uint i = 0; i < idcount; i++) {
  2604 	for (uint i = 0; i < idcount; i++) {
  2604 		CargoID cid = buf[3 + i];
  2605 		CargoID cid = buf[3 + i];
  2605 
  2606 
  2606 		if (cid >= NUM_CARGO) {
  2607 		if (cid >= NUM_CARGO) {
  2607 			grfmsg(1, "FeatureMapSpriteGroup: Cargo ID %d out of range, skipping");
  2608 			grfmsg(1, "CargoMapSpriteGroup: Cargo ID %d out of range, skipping");
  2608 			continue;
  2609 			continue;
  2609 		}
  2610 		}
  2610 
  2611 
  2611 		CargoSpec *cs = &_cargo[cid];
  2612 		CargoSpec *cs = &_cargo[cid];
  2612 		cs->grfid = _cur_grffile->grfid;
  2613 		cs->grfid = _cur_grffile->grfid;
  2793 
  2794 
  2794 #if 0
  2795 #if 0
  2795 				case GSF_CANAL :
  2796 				case GSF_CANAL :
  2796 				case GSF_BRIDGE :
  2797 				case GSF_BRIDGE :
  2797 					AddGRFString(_cur_spriteid, id, lang, name);
  2798 					AddGRFString(_cur_spriteid, id, lang, name);
  2798 					switch (GB(id, 8,8)) {
  2799 					switch (GB(id, 8, 8)) {
  2799 						case 0xC9: // House name
  2800 						case 0xC9: // House name
  2800 						default:
  2801 						default:
  2801 							grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
  2802 							grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
  2802 					}
  2803 					}
  2803 					break;
  2804 					break;
  2833 	uint16 num = grf_load_extended(&buf);
  2834 	uint16 num = grf_load_extended(&buf);
  2834 
  2835 
  2835 	switch (type) {
  2836 	switch (type) {
  2836 		case 0x04: // Signal graphics
  2837 		case 0x04: // Signal graphics
  2837 			if (num != 112 && num != 240) {
  2838 			if (num != 112 && num != 240) {
  2838 				grfmsg(1, "GraphicsNews: Signal graphics sprite count must be 112 or 240, skipping");
  2839 				grfmsg(1, "GraphicsNew: Signal graphics sprite count must be 112 or 240, skipping");
  2839 				return;
  2840 				return;
  2840 			}
  2841 			}
  2841 			_signal_base = _cur_spriteid;
  2842 			_signal_base = _cur_spriteid;
  2842 			break;
  2843 			break;
  2843 
  2844 
  2844 		case 0x05: // Catenary graphics
  2845 		case 0x05: // Catenary graphics
  2845 			if (num != 48) {
  2846 			if (num != 48) {
  2846 				grfmsg(1, "GraphicsNews: Catenary graphics sprite count must be 48, skipping");
  2847 				grfmsg(1, "GraphicsNew: Catenary graphics sprite count must be 48, skipping");
  2847 				return;
  2848 				return;
  2848 			}
  2849 			}
  2849 			replace = SPR_ELRAIL_BASE + 3;
  2850 			replace = SPR_ELRAIL_BASE + 3;
  2850 			break;
  2851 			break;
  2851 
  2852 
  2852 		case 0x06: // Foundations
  2853 		case 0x06: // Foundations
  2853 			if (num != 74) {
  2854 			if (num != 74) {
  2854 				grfmsg(1, "GraphicsNews: Foundation graphics sprite count must be 74, skipping");
  2855 				grfmsg(1, "GraphicsNew: Foundation graphics sprite count must be 74, skipping");
  2855 				return;
  2856 				return;
  2856 			}
  2857 			}
  2857 			replace = SPR_SLOPES_BASE;
  2858 			replace = SPR_SLOPES_BASE;
  2858 			break;
  2859 			break;
  2859 
  2860 
  2860 		case 0x08: // Canal graphics
  2861 		case 0x08: // Canal graphics
  2861 			if (num != 65) {
  2862 			if (num != 65) {
  2862 				grfmsg(1, "GraphicsNews: Canal graphics sprite count must be 65, skipping");
  2863 				grfmsg(1, "GraphicsNew: Canal graphics sprite count must be 65, skipping");
  2863 				return;
  2864 				return;
  2864 			}
  2865 			}
  2865 			replace = SPR_CANALS_BASE + 5;
  2866 			replace = SPR_CANALS_BASE + 5;
       
  2867 			break;
       
  2868 
       
  2869 		case 0x0A: // 2CC colour maps
       
  2870 			if (num != 256) {
       
  2871 				grfmsg(1, "GraphicsNew: 2CC colour maps sprite count must be 256, skipping");
       
  2872 				return;
       
  2873 			}
       
  2874 			replace = SPR_2CCMAP_BASE;
  2866 			break;
  2875 			break;
  2867 
  2876 
  2868 		case 0x0D: // Coast graphics
  2877 		case 0x0D: // Coast graphics
  2869 			if (num != 16) {
  2878 			if (num != 16) {
  2870 				grfmsg(1, "GraphicsNews: Coast graphics sprite count must be 16, skipping");
  2879 				grfmsg(1, "GraphicsNew: Coast graphics sprite count must be 16, skipping");
  2871 				return;
  2880 				return;
  2872 			}
  2881 			}
  2873 			_coast_base = _cur_spriteid;
  2882 			_coast_base = _cur_spriteid;
       
  2883 			break;
       
  2884 
       
  2885 		case 0x10: // New airport sprites
       
  2886 			if (num != 15) {
       
  2887 				grfmsg(1, "GraphicsNew: Airport graphics sprite count must be 15, skipping");
       
  2888 				return;
       
  2889 			}
       
  2890 			replace = SPR_AIRPORTX_BASE;
       
  2891 			break;
       
  2892 
       
  2893 		case 0x11: // Road stop sprites
       
  2894 			if (num != 8) {
       
  2895 				grfmsg(1, "GraphicsNew: Road stop graphics sprite count must be 8, skipping");
       
  2896 				return;
       
  2897 			}
       
  2898 			replace = SPR_ROADSTOP_BASE;
  2874 			break;
  2899 			break;
  2875 
  2900 
  2876 		default:
  2901 		default:
  2877 			grfmsg(2, "GraphicsNew: Custom graphics (type 0x%02X) sprite block of length %u (unimplemented, ignoring)",
  2902 			grfmsg(2, "GraphicsNew: Custom graphics (type 0x%02X) sprite block of length %u (unimplemented, ignoring)",
  2878 					type, num);
  2903 					type, num);
  3068 		case 1: cond_val = grf_load_byte(&buf);  mask = 0x000000FF; break;
  3093 		case 1: cond_val = grf_load_byte(&buf);  mask = 0x000000FF; break;
  3069 		default: break;
  3094 		default: break;
  3070 	}
  3095 	}
  3071 
  3096 
  3072 	if (param < 0x80 && _cur_grffile->param_end <= param) {
  3097 	if (param < 0x80 && _cur_grffile->param_end <= param) {
  3073 		grfmsg(7, "Param %d undefined, skipping test", param);
  3098 		grfmsg(7, "SkipIf: Param %d undefined, skipping test", param);
  3074 		return;
  3099 		return;
  3075 	}
  3100 	}
  3076 
  3101 
  3077 	uint32 param_val = GetParamVal(param, &cond_val);
  3102 	uint32 param_val = GetParamVal(param, &cond_val);
  3078 
  3103 
  3079 	grfmsg(7, "Test condtype %d, param 0x%08X, condval 0x%08X", condtype, param_val, cond_val);
  3104 	grfmsg(7, "SkipIf: Test condtype %d, param 0x%08X, condval 0x%08X", condtype, param_val, cond_val);
  3080 
  3105 
  3081 	if (param == 0x88) {
  3106 	if (param == 0x88) {
  3082 		/* GRF ID checks */
  3107 		/* GRF ID checks */
  3083 
  3108 
  3084 		const GRFConfig *c = GetGRFConfig(cond_val);
  3109 		const GRFConfig *c = GetGRFConfig(cond_val);
  3085 
  3110 
  3086 		if (condtype != 10 && c == NULL) {
  3111 		if (condtype != 10 && c == NULL) {
  3087 			grfmsg(7, "GRFID 0x%08X unknown, skipping test", BSWAP32(cond_val));
  3112 			grfmsg(7, "SkipIf: GRFID 0x%08X unknown, skipping test", BSWAP32(cond_val));
  3088 			return;
  3113 			return;
  3089 		}
  3114 		}
  3090 
  3115 
  3091 		switch (condtype) {
  3116 		switch (condtype) {
  3092 			/* Tests 6 to 10 are only for param 0x88, GRFID checks */
  3117 			/* Tests 6 to 10 are only for param 0x88, GRFID checks */
  3109 			case 10: // GRFID is not nor will be active
  3134 			case 10: // GRFID is not nor will be active
  3110 				/* This is the only condtype that doesn't get ignored if the GRFID is not found */
  3135 				/* This is the only condtype that doesn't get ignored if the GRFID is not found */
  3111 				result = c == NULL || c->flags == GCS_DISABLED || c->status == GCS_NOT_FOUND;
  3136 				result = c == NULL || c->flags == GCS_DISABLED || c->status == GCS_NOT_FOUND;
  3112 				break;
  3137 				break;
  3113 
  3138 
  3114 			default: grfmsg(1, "Unsupported GRF test %d. Ignoring", condtype); return;
  3139 			default: grfmsg(1, "SkipIf: Unsupported GRF test %d. Ignoring", condtype); return;
  3115 		}
  3140 		}
  3116 	} else {
  3141 	} else {
  3117 		/* Parameter or variable tests */
  3142 		/* Parameter or variable tests */
  3118 		switch (condtype) {
  3143 		switch (condtype) {
  3119 			case 0: result = !!(param_val & (1 << cond_val));
  3144 			case 0: result = !!(param_val & (1 << cond_val));
  3127 			case 4: result = (param_val & mask) < cond_val;
  3152 			case 4: result = (param_val & mask) < cond_val;
  3128 				break;
  3153 				break;
  3129 			case 5: result = (param_val & mask) > cond_val;
  3154 			case 5: result = (param_val & mask) > cond_val;
  3130 				break;
  3155 				break;
  3131 
  3156 
  3132 			default: grfmsg(1, "Unsupported test %d. Ignoring", condtype); return;
  3157 			default: grfmsg(1, "SkipIf: Unsupported test %d. Ignoring", condtype); return;
  3133 		}
  3158 		}
  3134 	}
  3159 	}
  3135 
  3160 
  3136 	if (!result) {
  3161 	if (!result) {
  3137 		grfmsg(2, "Not skipping sprites, test was false");
  3162 		grfmsg(2, "SkipIf: Not skipping sprites, test was false");
  3138 		return;
  3163 		return;
  3139 	}
  3164 	}
  3140 
  3165 
  3141 	uint8 numsprites = grf_load_byte(&buf);
  3166 	uint8 numsprites = grf_load_byte(&buf);
  3142 
  3167 
  3156 			break;
  3181 			break;
  3157 		}
  3182 		}
  3158 	}
  3183 	}
  3159 
  3184 
  3160 	if (choice != NULL) {
  3185 	if (choice != NULL) {
  3161 		grfmsg(2, "Jumping to label 0x%0X at line %d, test was true", choice->label, choice->nfo_line);
  3186 		grfmsg(2, "SkipIf: Jumping to label 0x%0X at line %d, test was true", choice->label, choice->nfo_line);
  3162 		FioSeekTo(choice->pos, SEEK_SET);
  3187 		FioSeekTo(choice->pos, SEEK_SET);
  3163 		_nfo_line = choice->nfo_line;
  3188 		_nfo_line = choice->nfo_line;
  3164 		return;
  3189 		return;
  3165 	}
  3190 	}
  3166 
  3191 
  3167 	grfmsg(2, "Skipping %d sprites, test was true", numsprites);
  3192 	grfmsg(2, "SkipIf: Skipping %d sprites, test was true", numsprites);
  3168 	_skip_sprites = numsprites;
  3193 	_skip_sprites = numsprites;
  3169 	if (_skip_sprites == 0) {
  3194 	if (_skip_sprites == 0) {
  3170 		/* Zero means there are no sprites to skip, so
  3195 		/* Zero means there are no sprites to skip, so
  3171 		 * we use -1 to indicate that all further
  3196 		 * we use -1 to indicate that all further
  3172 		 * sprites should be skipped. */
  3197 		 * sprites should be skipped. */
  3224 	_cur_grffile->grfid = grfid;
  3249 	_cur_grffile->grfid = grfid;
  3225 	_cur_grffile->grf_version = version;
  3250 	_cur_grffile->grf_version = version;
  3226 	_cur_grfconfig->status = _cur_stage < GLS_ACTIVATION ? GCS_INITIALISED : GCS_ACTIVATED;
  3251 	_cur_grfconfig->status = _cur_stage < GLS_ACTIVATION ? GCS_INITIALISED : GCS_ACTIVATED;
  3227 
  3252 
  3228 	/* Do swap the GRFID for displaying purposes since people expect that */
  3253 	/* Do swap the GRFID for displaying purposes since people expect that */
  3229 	DEBUG(grf, 1, "Loaded GRFv%d set %08lX - %s", version, BSWAP32(grfid), name);
  3254 	DEBUG(grf, 1, "GRFInfo: Loaded GRFv%d set %08lX - %s", version, BSWAP32(grfid), name);
  3230 }
  3255 }
  3231 
  3256 
  3232 /* Action 0x0A */
  3257 /* Action 0x0A */
  3233 static void SpriteReplace(byte *buf, int len)
  3258 static void SpriteReplace(byte *buf, int len)
  3234 {
  3259 {
  3313 	len -= 4;
  3338 	len -= 4;
  3314 
  3339 
  3315 	/* Skip the error until the activation stage unless bit 7 of the severity
  3340 	/* Skip the error until the activation stage unless bit 7 of the severity
  3316 	 * is set. */
  3341 	 * is set. */
  3317 	if (!HASBIT(severity, 7) && _cur_stage < GLS_ACTIVATION) {
  3342 	if (!HASBIT(severity, 7) && _cur_stage < GLS_ACTIVATION) {
  3318 		grfmsg(7, "Skipping non-fatal GRFLoadError in stage 1");
  3343 		grfmsg(7, "GRFLoadError: Skipping non-fatal GRFLoadError in stage 1");
  3319 		return;
  3344 		return;
  3320 	}
  3345 	}
  3321 	CLRBIT(severity, 7);
  3346 	CLRBIT(severity, 7);
  3322 
  3347 
  3323 	if (severity >= lengthof(sevstr)) {
  3348 	if (severity >= lengthof(sevstr)) {
  3406 	SETBIT(_cur_grfconfig->flags, GCF_UNSAFE);
  3431 	SETBIT(_cur_grfconfig->flags, GCF_UNSAFE);
  3407 
  3432 
  3408 	/* Skip remainder of GRF */
  3433 	/* Skip remainder of GRF */
  3409 	_skip_sprites = -1;
  3434 	_skip_sprites = -1;
  3410 }
  3435 }
       
  3436 
       
  3437 
       
  3438 static uint32 GetPatchVariable(uint8 param)
       
  3439 {
       
  3440 	switch (param) {
       
  3441 		/* start year - 1920 */
       
  3442 		case 0x0B: return _cur_year - ORIGINAL_BASE_YEAR;
       
  3443 		/* freight trains weight factor */
       
  3444 		case 0x0E: return _patches.freight_trains;
       
  3445 		/* empty wagon speed increase */
       
  3446 		case 0x0F: return 0;
       
  3447 		/* plane speed factor */
       
  3448 		case 0x10: return 4;
       
  3449 		/* 2CC colormap base sprite */
       
  3450 		case 0x11: return SPR_2CCMAP_BASE;
       
  3451 
       
  3452 		default:
       
  3453 			grfmsg(2, "ParamSet: Unknown Patch variable 0x%02X.", param);
       
  3454 			return 0;
       
  3455 	}
       
  3456 }
       
  3457 
  3411 
  3458 
  3412 /* Action 0x0D */
  3459 /* Action 0x0D */
  3413 static void ParamSet(byte *buf, int len)
  3460 static void ParamSet(byte *buf, int len)
  3414 {
  3461 {
  3415 	/* <0D> <target> <operation> <source1> <source2> [<data>]
  3462 	/* <0D> <target> <operation> <source1> <source2> [<data>]
  3450 	 * - it has been set to any value in the newgrf(w).cfg parameter list
  3497 	 * - it has been set to any value in the newgrf(w).cfg parameter list
  3451 	 * - it OR A PARAMETER WITH HIGHER NUMBER has been set to any value by
  3498 	 * - it OR A PARAMETER WITH HIGHER NUMBER has been set to any value by
  3452 	 *   an earlier action D */
  3499 	 *   an earlier action D */
  3453 	if (HASBIT(oper, 7)) {
  3500 	if (HASBIT(oper, 7)) {
  3454 		if (target < 0x80 && target < _cur_grffile->param_end) {
  3501 		if (target < 0x80 && target < _cur_grffile->param_end) {
  3455 			grfmsg(7, "Param %u already defined, skipping", target);
  3502 			grfmsg(7, "ParamSet: Param %u already defined, skipping", target);
  3456 			return;
  3503 			return;
  3457 		}
  3504 		}
  3458 
  3505 
  3459 		oper = GB(oper, 0, 7);
  3506 		oper = GB(oper, 0, 7);
  3460 	}
  3507 	}
  3461 
  3508 
  3462 	if (src2 == 0xFE) {
  3509 	if (src2 == 0xFE) {
  3463 		if (GB(data, 0, 8) == 0xFF) {
  3510 		if (GB(data, 0, 8) == 0xFF) {
  3464 			if (data == 0x0000FFFF) {
  3511 			if (data == 0x0000FFFF) {
  3465 				/* Patch variables */
  3512 				/* Patch variables */
  3466 				grfmsg(2, "ParamSet: Reading Patch variables unsupported");
  3513 				src1 = GetPatchVariable(src1);
  3467 				return;
       
  3468 			} else {
  3514 			} else {
  3469 				/* GRF Resource Management */
  3515 				/* GRF Resource Management */
  3470 				if (_cur_stage != GLS_ACTIVATION) {
  3516 				if (_cur_stage != GLS_ACTIVATION) {
  3471 					/* Ignore GRM during initialization */
  3517 					/* Ignore GRM during initialization */
  3472 					src1 = 0;
  3518 					src1 = 0;
  3507 							}
  3553 							}
  3508 
  3554 
  3509 							if (size == count) {
  3555 							if (size == count) {
  3510 								/* Got the slot... */
  3556 								/* Got the slot... */
  3511 								if (op == 0 || op == 3) {
  3557 								if (op == 0 || op == 3) {
  3512 									grfmsg(2, "GRM: Reserving %d vehicles at %d", count, start);
  3558 									grfmsg(2, "ParamSet: GRM: Reserving %d vehicles at %d", count, start);
  3513 									for (uint i = 0; i < count; i++) _grm_engines[shift + start + i] = _cur_grffile->grfid;
  3559 									for (uint i = 0; i < count; i++) _grm_engines[shift + start + i] = _cur_grffile->grfid;
  3514 								}
  3560 								}
  3515 								src1 = start;
  3561 								src1 = start;
  3516 							} else {
  3562 							} else {
  3517 								/* Unable to allocate */
  3563 								/* Unable to allocate */
  3518 								if (op != 4 && op != 5) {
  3564 								if (op != 4 && op != 5) {
  3519 									/* Deactivate GRF */
  3565 									/* Deactivate GRF */
  3520 									grfmsg(0, "GRM: Unable to allocate %d vehicles, deactivating", count);
  3566 									grfmsg(0, "ParamSet: GRM: Unable to allocate %d vehicles, deactivating", count);
  3521 									_cur_grfconfig->status = GCS_DISABLED;
  3567 									_cur_grfconfig->status = GCS_DISABLED;
  3522 
  3568 
  3523 									_skip_sprites = -1;
  3569 									_skip_sprites = -1;
  3524 									return;
  3570 									return;
  3525 								}
  3571 								}
  3526 
  3572 
  3527 								grfmsg(1, "GRM: Unable to allocate %d vehicles", count);
  3573 								grfmsg(1, "ParamSet: GRM: Unable to allocate %d vehicles", count);
  3528 								src1 = UINT_MAX;
  3574 								src1 = UINT_MAX;
  3529 							}
  3575 							}
  3530 							break;
  3576 							break;
  3531 						}
  3577 						}
  3532 
  3578 
  3533 						case 0x08: // General sprites
  3579 						case 0x08: // General sprites
  3534 							switch (op) {
  3580 							switch (op) {
  3535 								case 0:
  3581 								case 0:
  3536 									/* Check if the allocated sprites will fit below the original sprite limit */
  3582 									/* Check if the allocated sprites will fit below the original sprite limit */
  3537 									if (_cur_spriteid + count >= 16384) {
  3583 									if (_cur_spriteid + count >= 16384) {
  3538 										grfmsg(0, "GRM: Unable to allocate %d sprites; try changing NewGRF order", count);
  3584 										grfmsg(0, "ParamSet: GRM: Unable to allocate %d sprites; try changing NewGRF order", count);
  3539 										_cur_grfconfig->status = GCS_DISABLED;
  3585 										_cur_grfconfig->status = GCS_DISABLED;
  3540 
  3586 
  3541 										_skip_sprites = -1;
  3587 										_skip_sprites = -1;
  3542 										return;
  3588 										return;
  3543 									}
  3589 									}
  3550 								case 1:
  3596 								case 1:
  3551 									src1 = _cur_spriteid;
  3597 									src1 = _cur_spriteid;
  3552 									break;
  3598 									break;
  3553 
  3599 
  3554 								default:
  3600 								default:
  3555 									grfmsg(1, "GRM: Unsupported operation %d for general sprites", op);
  3601 									grfmsg(1, "ParamSet: GRM: Unsupported operation %d for general sprites", op);
  3556 									return;
  3602 									return;
  3557 							}
  3603 							}
  3558 							break;
  3604 							break;
  3559 
  3605 
  3560 						default: grfmsg(1, "GRM: Unsupported feature 0x%X", feature); return;
  3606 						default: grfmsg(1, "ParamSet: GRM: Unsupported feature 0x%X", feature); return;
  3561 					}
  3607 					}
  3562 				}
  3608 				}
  3563 			}
  3609 			}
  3564 		} else {
  3610 		} else {
  3565 			/* Read another GRF File's parameter */
  3611 			/* Read another GRF File's parameter */
  3958 	if (!check_length(len, 7, "TranslateGRFString")) return;
  4004 	if (!check_length(len, 7, "TranslateGRFString")) return;
  3959 
  4005 
  3960 	uint32 grfid = grf_load_dword(&buf);
  4006 	uint32 grfid = grf_load_dword(&buf);
  3961 	const GRFConfig *c = GetGRFConfig(grfid);
  4007 	const GRFConfig *c = GetGRFConfig(grfid);
  3962 	if (c == NULL || (c->status != GCS_INITIALISED && c->status != GCS_ACTIVATED)) {
  4008 	if (c == NULL || (c->status != GCS_INITIALISED && c->status != GCS_ACTIVATED)) {
  3963 		grfmsg(7, "GRFID 0x%08x unknown, skipping action 13", BSWAP32(grfid));
  4009 		grfmsg(7, "TranslateGRFStrings: GRFID 0x%08x unknown, skipping action 13", BSWAP32(grfid));
  3964 		return;
  4010 		return;
  3965 	}
  4011 	}
  3966 
  4012 
  3967 	if (c->status == GCS_INITIALISED) {
  4013 	if (c->status == GCS_INITIALISED) {
  3968 		/* If the file is not active but will be activated later, give an error
  4014 		/* If the file is not active but will be activated later, give an error
  3982 
  4028 
  3983 	byte num_strings = grf_load_byte(&buf);
  4029 	byte num_strings = grf_load_byte(&buf);
  3984 	uint16 first_id  = grf_load_word(&buf);
  4030 	uint16 first_id  = grf_load_word(&buf);
  3985 
  4031 
  3986 	if (!((first_id >= 0xD000 && first_id + num_strings <= 0xD3FF) || (first_id >= 0xDC00 && first_id + num_strings <= 0xDCFF))) {
  4032 	if (!((first_id >= 0xD000 && first_id + num_strings <= 0xD3FF) || (first_id >= 0xDC00 && first_id + num_strings <= 0xDCFF))) {
  3987 		grfmsg(7, "Attempting to set out-of-range string IDs in action 13 (first: 0x%4X, number: 0x%2X)", first_id, num_strings);
  4033 		grfmsg(7, "TranslateGRFStrings: Attempting to set out-of-range string IDs in action 13 (first: 0x%4X, number: 0x%2X)", first_id, num_strings);
  3988 		return;
  4034 		return;
  3989 	}
  4035 	}
  3990 
  4036 
  3991 	len -= 7;
  4037 	len -= 7;
  3992 
  4038 
  4077 	_ttdpatch_flags[2] =                                        (1 << 0x01)  // loadallgraphics - obsolote
  4123 	_ttdpatch_flags[2] =                                        (1 << 0x01)  // loadallgraphics - obsolote
  4078 	                   |                                        (1 << 0x03)  // semaphores
  4124 	                   |                                        (1 << 0x03)  // semaphores
  4079 	                   |                                        (0 << 0x0B)  // enhancedgui
  4125 	                   |                                        (0 << 0x0B)  // enhancedgui
  4080 	                   |                                        (0 << 0x0C)  // newagerating
  4126 	                   |                                        (0 << 0x0C)  // newagerating
  4081 	                   |       ((_patches.build_on_slopes ? 1 : 0) << 0x0D)  // buildonslopes
  4127 	                   |       ((_patches.build_on_slopes ? 1 : 0) << 0x0D)  // buildonslopes
       
  4128 	                   |         ((_patches.full_load_any ? 1 : 0) << 0x0E)  // fullloadany
  4082 	                   |                                        (0 << 0x0F)  // planespeed
  4129 	                   |                                        (0 << 0x0F)  // planespeed
  4083 	                   |                                        (0 << 0x10)  // moreindustriesperclimate - obsolete
  4130 	                   |                                        (0 << 0x10)  // moreindustriesperclimate - obsolete
  4084 	                   |                                        (0 << 0x11)  // moretoylandfeatures
  4131 	                   |                                        (0 << 0x11)  // moretoylandfeatures
  4085 	                   |                                        (1 << 0x12)  // newstations
  4132 	                   |                                        (1 << 0x12)  // newstations
  4086 	                   |                                        (0 << 0x13)  // tracktypecostdiff
  4133 	                   |                                        (0 << 0x13)  // tracktypecostdiff
  4087 	                   |                                        (0 << 0x14)  // manualconvert
  4134 	                   |                                        (1 << 0x14)  // manualconvert
  4088 	                   |       ((_patches.build_on_slopes ? 1 : 0) << 0x15)  // buildoncoasts
  4135 	                   |       ((_patches.build_on_slopes ? 1 : 0) << 0x15)  // buildoncoasts
  4089 	                   |                                        (1 << 0x16)  // canals
  4136 	                   |                                        (1 << 0x16)  // canals
  4090 	                   |                                        (1 << 0x17)  // newstartyear
  4137 	                   |                                        (1 << 0x17)  // newstartyear
  4091 	                   |                                        (0 << 0x18)  // freighttrains
  4138 	                   |    ((_patches.freight_trains > 1 ? 1 : 0) << 0x18)  // freighttrains
  4092 	                   |                                        (1 << 0x19)  // newhouses
  4139 	                   |                                        (1 << 0x19)  // newhouses
  4093 	                   |                                        (1 << 0x1A)  // newbridges
  4140 	                   |                                        (1 << 0x1A)  // newbridges
  4094 	                   |                                        (0 << 0x1B)  // newtownnames
  4141 	                   |                                        (0 << 0x1B)  // newtownnames
  4095 	                   |                                        (0 << 0x1C)  // moreanimations
  4142 	                   |                                        (0 << 0x1C)  // moreanimations
  4096 	                   |    ((_patches.wagon_speed_limits ? 1 : 0) << 0x1D)  // wagonspeedlimits
  4143 	                   |    ((_patches.wagon_speed_limits ? 1 : 0) << 0x1D)  // wagonspeedlimits
  4106 	                   |                                        (1 << 0x06)  // resolutionheight
  4153 	                   |                                        (1 << 0x06)  // resolutionheight
  4107 	                   |                                        (0 << 0x07)  // newindustries
  4154 	                   |                                        (0 << 0x07)  // newindustries
  4108 	                   |                                        (0 << 0x08)  // fifoloading
  4155 	                   |                                        (0 << 0x08)  // fifoloading
  4109 	                   |                                        (0 << 0x09)  // townroadbranchprob
  4156 	                   |                                        (0 << 0x09)  // townroadbranchprob
  4110 	                   |                                        (0 << 0x0A)  // tempsnowline
  4157 	                   |                                        (0 << 0x0A)  // tempsnowline
  4111 	                   |                                        (0 << 0x0B)  // newcargo
  4158 	                   |                                        (1 << 0x0B)  // newcargo
  4112 	                   |                                        (1 << 0x0C)  // enhancemultiplayer
  4159 	                   |                                        (1 << 0x0C)  // enhancemultiplayer
  4113 	                   |                                        (1 << 0x0D)  // onewayroads
  4160 	                   |                                        (1 << 0x0D)  // onewayroads
  4114 	                   |   ((_patches.nonuniform_stations ? 1 : 0) << 0x0E)  // irregularstations
  4161 	                   |   ((_patches.nonuniform_stations ? 1 : 0) << 0x0E)  // irregularstations
  4115 	                   |                                        (1 << 0x0F)  // statistics
  4162 	                   |                                        (1 << 0x0F)  // statistics
  4116 	                   |                                        (1 << 0x10)  // newsounds
  4163 	                   |                                        (1 << 0x10)  // newsounds
  4118 	                   |                                        (1 << 0x12)  // autoslope
  4165 	                   |                                        (1 << 0x12)  // autoslope
  4119 	                   |                                        (0 << 0x13)  // followvehicle
  4166 	                   |                                        (0 << 0x13)  // followvehicle
  4120 	                   |                                        (0 << 0x14)  // trams
  4167 	                   |                                        (0 << 0x14)  // trams
  4121 	                   |                                        (0 << 0x15)  // enhancetunnels
  4168 	                   |                                        (0 << 0x15)  // enhancetunnels
  4122 	                   |                                        (0 << 0x16)  // shortrvs
  4169 	                   |                                        (0 << 0x16)  // shortrvs
  4123 	                   |                                        (0 << 0x17); // articulatedrvs
  4170 	                   |                                        (0 << 0x17)  // articulatedrvs
       
  4171 	                   |                                        (1 << 0x1E); // variablerunningcosts
  4124 }
  4172 }
  4125 
  4173 
  4126 static void ResetCustomStations()
  4174 static void ResetCustomStations()
  4127 {
  4175 {
  4128 	for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
  4176 	for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
  4376  * Precalculate refit masks from cargo classes for all vehicles.
  4424  * Precalculate refit masks from cargo classes for all vehicles.
  4377  */
  4425  */
  4378 static void CalculateRefitMasks()
  4426 static void CalculateRefitMasks()
  4379 {
  4427 {
  4380 	for (EngineID engine = 0; engine < TOTAL_NUM_ENGINES; engine++) {
  4428 	for (EngineID engine = 0; engine < TOTAL_NUM_ENGINES; engine++) {
  4381 		/* Skip engine if not available in this climate */
       
  4382 		if (!HASBIT(_engine_info[engine].climates, _opt.landscape)) continue;
       
  4383 
       
  4384 		uint32 mask = 0;
  4429 		uint32 mask = 0;
  4385 		uint32 not_mask = 0;
  4430 		uint32 not_mask = 0;
  4386 		uint32 xor_mask = 0;
  4431 		uint32 xor_mask = 0;
  4387 
  4432 
  4388 		if (_engine_info[engine].refit_mask != 0) {
  4433 		if (_engine_info[engine].refit_mask != 0) {
  4579 	}
  4624 	}
  4580 
  4625 
  4581 	byte action = buf[0];
  4626 	byte action = buf[0];
  4582 
  4627 
  4583 	if (action == 0xFF) {
  4628 	if (action == 0xFF) {
  4584 		grfmsg(7, "Handling data block in stage %d", stage);
  4629 		grfmsg(7, "DecodeSpecialSprite: Handling data block in stage %d", stage);
  4585 		GRFDataBlock(buf, num);
  4630 		GRFDataBlock(buf, num);
  4586 	} else if (action == 0xFE) {
  4631 	} else if (action == 0xFE) {
  4587 		grfmsg(7, "Handling import block in stage %d", stage);
  4632 		grfmsg(7, "DecodeSpecialSprite: andling import block in stage %d", stage);
  4588 		GRFImportBlock(buf, num);
  4633 		GRFImportBlock(buf, num);
  4589 	} else if (action >= lengthof(handlers)) {
  4634 	} else if (action >= lengthof(handlers)) {
  4590 		grfmsg(7, "Skipping unknown action 0x%02X", action);
  4635 		grfmsg(7, "DecodeSpecialSprite: Skipping unknown action 0x%02X", action);
  4591 	} else if (handlers[action][stage] == NULL) {
  4636 	} else if (handlers[action][stage] == NULL) {
  4592 		grfmsg(7, "Skipping action 0x%02X in stage %d", action, stage);
  4637 		grfmsg(7, "DecodeSpecialSprite: Skipping action 0x%02X in stage %d", action, stage);
  4593 	} else {
  4638 	} else {
  4594 		grfmsg(7, "Handling action 0x%02X in stage %d", action, stage);
  4639 		grfmsg(7, "DecodeSpecialSprite: Handling action 0x%02X in stage %d", action, stage);
  4595 		handlers[action][stage](buf, num);
  4640 		handlers[action][stage](buf, num);
  4596 	}
  4641 	}
  4597 	free(buf);
  4642 	free(buf);
  4598 }
  4643 }
  4599 
  4644 
  4621 	FioOpenFile(file_index, filename);
  4666 	FioOpenFile(file_index, filename);
  4622 	_file_index = file_index; // XXX
  4667 	_file_index = file_index; // XXX
  4623 
  4668 
  4624 	_cur_grfconfig = config;
  4669 	_cur_grfconfig = config;
  4625 
  4670 
  4626 	DEBUG(grf, 2, "Reading NewGRF-file '%s'", filename);
  4671 	DEBUG(grf, 2, "LoadNewGRFFile: Reading NewGRF-file '%s'", filename);
  4627 
  4672 
  4628 	/* Skip the first sprite; we don't care about how many sprites this
  4673 	/* Skip the first sprite; we don't care about how many sprites this
  4629 	 * does contain; newest TTDPatches and George's longvehicles don't
  4674 	 * does contain; newest TTDPatches and George's longvehicles don't
  4630 	 * neither, apparently. */
  4675 	 * neither, apparently. */
  4631 	if (FioReadWord() == 4 && FioReadByte() == 0xFF) {
  4676 	if (FioReadWord() == 4 && FioReadByte() == 0xFF) {
  4632 		FioReadDword();
  4677 		FioReadDword();
  4633 	} else {
  4678 	} else {
  4634 		DEBUG(grf, 7, "Custom .grf has invalid format");
  4679 		DEBUG(grf, 7, "LoadNewGRFFile: Custom .grf has invalid format");
  4635 		return;
  4680 		return;
  4636 	}
  4681 	}
  4637 
  4682 
  4638 	_skip_sprites = 0; // XXX
  4683 	_skip_sprites = 0; // XXX
  4639 	_nfo_line = 0;
  4684 	_nfo_line = 0;
  4652 				continue;
  4697 				continue;
  4653 			} else {
  4698 			} else {
  4654 				FioSkipBytes(num);
  4699 				FioSkipBytes(num);
  4655 			}
  4700 			}
  4656 		} else {
  4701 		} else {
  4657 			if (_skip_sprites == 0) grfmsg(7, "Skipping unexpected sprite");
  4702 			if (_skip_sprites == 0) grfmsg(7, "LoadNewGRFFile: Skipping unexpected sprite");
  4658 
  4703 
  4659 			FioSkipBytes(7);
  4704 			FioSkipBytes(7);
  4660 			num -= 8;
  4705 			num -= 8;
  4661 
  4706 
  4662 			if (type & 2) {
  4707 			if (type & 2) {
  4721 			if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
  4766 			if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
  4722 			LoadNewGRFFile(c, slot++, stage);
  4767 			LoadNewGRFFile(c, slot++, stage);
  4723 			if (stage == GLS_ACTIVATION) {
  4768 			if (stage == GLS_ACTIVATION) {
  4724 				ClearTemporaryNewGRFData();
  4769 				ClearTemporaryNewGRFData();
  4725 				BuildCargoTranslationMap();
  4770 				BuildCargoTranslationMap();
  4726 				DEBUG(sprite, 2, "Currently %i sprites are loaded", _cur_spriteid);
  4771 				DEBUG(sprite, 2, "LoadNewGRF: Currently %i sprites are loaded", _cur_spriteid);
  4727 			}
  4772 			}
  4728 		}
  4773 		}
  4729 	}
  4774 	}
  4730 
  4775 
  4731 	/* Call any functions that should be run after GRFs have been loaded. */
  4776 	/* Call any functions that should be run after GRFs have been loaded. */