src/newgrf.cpp
branchcpp_gui
changeset 6303 84c215fc8eb8
parent 6298 c30fe89622df
child 6307 f40e88cff863
equal deleted inserted replaced
6302:bd80897189ba 6303:84c215fc8eb8
     1 /* $Id$ */
     1 /* $Id$ */
       
     2 
       
     3 /** @file newgrf.cpp */
     2 
     4 
     3 #include "stdafx.h"
     5 #include "stdafx.h"
     4 
     6 
     5 #include <stdarg.h>
     7 #include <stdarg.h>
     6 
     8 
    16 #include "newgrf.h"
    18 #include "newgrf.h"
    17 #include "variables.h"
    19 #include "variables.h"
    18 #include "string.h"
    20 #include "string.h"
    19 #include "table/strings.h"
    21 #include "table/strings.h"
    20 #include "bridge.h"
    22 #include "bridge.h"
       
    23 #include "town.h"
    21 #include "economy.h"
    24 #include "economy.h"
    22 #include "newgrf_engine.h"
    25 #include "newgrf_engine.h"
    23 #include "vehicle.h"
    26 #include "vehicle.h"
    24 #include "newgrf_text.h"
    27 #include "newgrf_text.h"
    25 #include "table/sprites.h"
    28 #include "table/sprites.h"
    26 #include "fontcache.h"
    29 #include "fontcache.h"
    27 #include "date.h"
    30 #include "date.h"
    28 #include "currency.h"
    31 #include "currency.h"
       
    32 #include "landscape.h"
    29 #include "sound.h"
    33 #include "sound.h"
    30 #include "newgrf_config.h"
    34 #include "newgrf_config.h"
       
    35 #include "newgrf_house.h"
    31 #include "newgrf_sound.h"
    36 #include "newgrf_sound.h"
    32 #include "newgrf_spritegroup.h"
    37 #include "newgrf_spritegroup.h"
    33 #include "helpers.hpp"
    38 #include "helpers.hpp"
       
    39 #include "table/town_land.h"
    34 #include "cargotype.h"
    40 #include "cargotype.h"
    35 
    41 
    36 /* TTDPatch extended GRF format codec
    42 /* TTDPatch extended GRF format codec
    37  * (c) Petr Baudis 2004 (GPL'd)
    43  * (c) Petr Baudis 2004 (GPL'd)
    38  * Changes by Florian octo Forster are (c) by the OpenTTD development team.
    44  * Changes by Florian octo Forster are (c) by the OpenTTD development team.
    66 static byte *_preload_sprite = NULL;
    72 static byte *_preload_sprite = NULL;
    67 
    73 
    68 /* Set if any vehicle is loaded which uses 2cc (two company colours) */
    74 /* Set if any vehicle is loaded which uses 2cc (two company colours) */
    69 bool _have_2cc = false;
    75 bool _have_2cc = false;
    70 
    76 
    71 /* Default cargo translation table. By default there are 27 possible cargo types */
    77 /* Set if there are any newhouses loaded. */
    72 static const uint _default_cargo_max = 27;
    78 bool _have_newhouses = false;
    73 static CargoLabel _default_cargo_list[_default_cargo_max];
       
    74 
    79 
    75 
    80 
    76 enum GrfDataType {
    81 enum GrfDataType {
    77 	GDT_SOUND,
    82 	GDT_SOUND,
    78 };
    83 };
   225 	}
   230 	}
   226 	return file;
   231 	return file;
   227 }
   232 }
   228 
   233 
   229 
   234 
       
   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
       
   237  * @param str StringID that we want to have the equivalent in OoenTTD
       
   238  * @return the properly adjusted StringID
       
   239  */
       
   240 static StringID MapGRFStringID(uint32 grfid, StringID str)
       
   241 {
       
   242 	/* 0xD0 and 0xDC stand for all the TextIDs in the range
       
   243 	 * of 0xD000 (misc graphics texts) and 0xDC00 (misc persistent texts).
       
   244 	 * These strings are unique to each grf file, and thus require to be used with the
       
   245 	 * grfid in which they are declared */
       
   246 	if (GB(str, 8, 8) == 0xD0 || GB(str, 8, 8) == 0xDC) {
       
   247 		return GetGRFStringID(grfid, str);
       
   248 	}
       
   249 
       
   250 	/* We have some changes in our cargo strings, resulting in some missing. */
       
   251 	if (str >= 0x006E && str <= 0x008D) return str - 0x20;
       
   252 	if (str >= 0x008E && str <= 0x00AD) return str - 0x20;
       
   253 
       
   254 	/* Map building names according to our lang file changes
       
   255 	 * 0x200F = Tall Office Block, first house name in the original data, the one that TTDPatch stil uses
       
   256 	 * 0x201F = Old houses is the last house name.
       
   257 	 * OpenTTD does not have exactly the same order aymore, so, the code below allows
       
   258 	 * to compensate for the difference */
       
   259 	if (str >= 0x200F && str <= 0x201F) return str + (STR_200F_TALL_OFFICE_BLOCK - 0x200F);
       
   260 
       
   261 	return str;
       
   262 }
       
   263 
       
   264 static uint8 MapDOSColour(uint8 colour)
       
   265 {
       
   266 	if (_use_dos_palette) return colour;
       
   267 
       
   268 	if (colour < 10) {
       
   269 		static uint8 dos_to_win_colour_map[] = { 0, 215, 216, 136, 88, 106, 32, 33, 40, 245 };
       
   270 		return dos_to_win_colour_map[colour];
       
   271 	}
       
   272 
       
   273 	if (colour >= 245 && colour < 254) return colour - 28;
       
   274 
       
   275 	return colour;
       
   276 }
       
   277 
       
   278 
   230 typedef bool (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
   279 typedef bool (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
   231 
   280 
   232 #define FOR_EACH_OBJECT for (i = 0; i < numinfo; i++)
   281 #define FOR_EACH_OBJECT for (i = 0; i < numinfo; i++)
   233 
   282 
   234 static void dewagonize(int condition, int engine)
   283 static void dewagonize(int condition, int engine)
   253 	byte *buf = *bufp;
   302 	byte *buf = *bufp;
   254 	int i;
   303 	int i;
   255 	bool ret = false;
   304 	bool ret = false;
   256 
   305 
   257 	switch (prop) {
   306 	switch (prop) {
   258 		case 0x05: /* Track type */
   307 		case 0x05: // Track type
   259 			FOR_EACH_OBJECT {
   308 			FOR_EACH_OBJECT {
   260 				uint8 tracktype = grf_load_byte(&buf);
   309 				uint8 tracktype = grf_load_byte(&buf);
   261 
   310 
   262 				switch (tracktype) {
   311 				switch (tracktype) {
   263 					case 0: rvi[i].railtype = rvi[i].engclass == 2 ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL; break;
   312 					case 0: rvi[i].railtype = rvi[i].engclass == 2 ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL; break;
   268 						break;
   317 						break;
   269 				}
   318 				}
   270 			}
   319 			}
   271 			break;
   320 			break;
   272 
   321 
   273 		case 0x08: /* AI passenger service */
   322 		case 0x08: // AI passenger service
   274 			/* TODO */
   323 			/* @todo missing feature */
   275 			FOR_EACH_OBJECT grf_load_byte(&buf);
   324 			FOR_EACH_OBJECT grf_load_byte(&buf);
   276 			ret = true;
   325 			ret = true;
   277 			break;
   326 			break;
   278 
   327 
   279 		case 0x09: /* Speed (1 unit is 1 kmh) */
   328 		case 0x09: // Speed (1 unit is 1 kmh)
   280 			FOR_EACH_OBJECT {
   329 			FOR_EACH_OBJECT {
   281 				uint16 speed = grf_load_word(&buf);
   330 				uint16 speed = grf_load_word(&buf);
   282 				if (speed == 0xFFFF) speed = 0;
   331 				if (speed == 0xFFFF) speed = 0;
   283 
   332 
   284 				rvi[i].max_speed = speed;
   333 				rvi[i].max_speed = speed;
   285 			}
   334 			}
   286 			break;
   335 			break;
   287 
   336 
   288 		case 0x0B: /* Power */
   337 		case 0x0B: // Power
   289 			FOR_EACH_OBJECT {
   338 			FOR_EACH_OBJECT {
   290 				uint16 power = grf_load_word(&buf);
   339 				uint16 power = grf_load_word(&buf);
   291 
   340 
   292 				if (rvi[i].railveh_type == RAILVEH_MULTIHEAD) power /= 2;
   341 				if (rvi[i].railveh_type == RAILVEH_MULTIHEAD) power /= 2;
   293 
   342 
   294 				rvi[i].power = power;
   343 				rvi[i].power = power;
   295 				dewagonize(power, engine + i);
   344 				dewagonize(power, engine + i);
   296 			}
   345 			}
   297 			break;
   346 			break;
   298 
   347 
   299 		case 0x0D: /* Running cost factor */
   348 		case 0x0D: // Running cost factor
   300 			FOR_EACH_OBJECT {
   349 			FOR_EACH_OBJECT {
   301 				uint8 runcostfact = grf_load_byte(&buf);
   350 				uint8 runcostfact = grf_load_byte(&buf);
   302 
   351 
   303 				if (rvi[i].railveh_type == RAILVEH_MULTIHEAD) runcostfact /= 2;
   352 				if (rvi[i].railveh_type == RAILVEH_MULTIHEAD) runcostfact /= 2;
   304 
   353 
   305 				rvi[i].running_cost_base = runcostfact;
   354 				rvi[i].running_cost_base = runcostfact;
   306 			}
   355 			}
   307 			break;
   356 			break;
   308 
   357 
   309 		case 0x0E: /* Running cost base */
   358 		case 0x0E: // Running cost base
   310 			FOR_EACH_OBJECT {
   359 			FOR_EACH_OBJECT {
   311 				uint32 base = grf_load_dword(&buf);
   360 				uint32 base = grf_load_dword(&buf);
   312 
   361 
   313 				switch (base) {
   362 				switch (base) {
   314 					case 0x4C30: rvi[i].running_cost_class = 0; break;
   363 					case 0x4C30: rvi[i].running_cost_class = 0; break;
   315 					case 0x4C36: rvi[i].running_cost_class = 1; break;
   364 					case 0x4C36: rvi[i].running_cost_class = 1; break;
   316 					case 0x4C3C: rvi[i].running_cost_class = 2; break;
   365 					case 0x4C3C: rvi[i].running_cost_class = 2; break;
   317 					case 0: break; /* Used by wagons */
   366 					case 0: break; // Used by wagons
   318 					default:
   367 					default:
   319 						grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
   368 						grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
   320 						break;
   369 						break;
   321 				}
   370 				}
   322 			}
   371 			}
   323 			break;
   372 			break;
   324 
   373 
   325 		case 0x12: /* Sprite ID */
   374 		case 0x12: // Sprite ID
   326 			FOR_EACH_OBJECT {
   375 			FOR_EACH_OBJECT {
   327 				uint8 spriteid = grf_load_byte(&buf);
   376 				uint8 spriteid = grf_load_byte(&buf);
   328 
   377 
   329 				/* TTD sprite IDs point to a location in a 16bit array, but we use it
   378 				/* TTD sprite IDs point to a location in a 16bit array, but we use it
   330 				 * as an array index, so we need it to be half the original value. */
   379 				 * as an array index, so we need it to be half the original value. */
   332 
   381 
   333 				rvi[i].image_index = spriteid;
   382 				rvi[i].image_index = spriteid;
   334 			}
   383 			}
   335 			break;
   384 			break;
   336 
   385 
   337 		case 0x13: /* Dual-headed */
   386 		case 0x13: // Dual-headed
   338 			FOR_EACH_OBJECT {
   387 			FOR_EACH_OBJECT {
   339 				uint8 dual = grf_load_byte(&buf);
   388 				uint8 dual = grf_load_byte(&buf);
   340 
   389 
   341 				if (dual != 0) {
   390 				if (dual != 0) {
   342 					if (rvi[i].railveh_type != RAILVEH_MULTIHEAD) {
   391 					if (rvi[i].railveh_type != RAILVEH_MULTIHEAD) {
   355 						RAILVEH_WAGON : RAILVEH_SINGLEHEAD;
   404 						RAILVEH_WAGON : RAILVEH_SINGLEHEAD;
   356 				}
   405 				}
   357 			}
   406 			}
   358 			break;
   407 			break;
   359 
   408 
   360 		case 0x14: /* Cargo capacity */
   409 		case 0x14: // Cargo capacity
   361 			FOR_EACH_OBJECT rvi[i].capacity = grf_load_byte(&buf);
   410 			FOR_EACH_OBJECT rvi[i].capacity = grf_load_byte(&buf);
   362 			break;
   411 			break;
   363 
   412 
   364 		case 0x15: /* Cargo type */
   413 		case 0x15: // Cargo type
   365 			FOR_EACH_OBJECT {
   414 			FOR_EACH_OBJECT {
   366 				uint8 ctype = grf_load_byte(&buf);
   415 				uint8 ctype = grf_load_byte(&buf);
   367 
   416 
   368 				if (ctype < NUM_CARGO && HASBIT(_cargo_mask, ctype)) {
   417 				if (ctype < NUM_CARGO && HASBIT(_cargo_mask, ctype)) {
   369 					rvi[i].cargo_type = ctype;
   418 					rvi[i].cargo_type = ctype;
   372 					grfmsg(2, "RailVehicleChangeInfo: Invalid cargo type %d, using first refittable", ctype);
   421 					grfmsg(2, "RailVehicleChangeInfo: Invalid cargo type %d, using first refittable", ctype);
   373 				}
   422 				}
   374 			}
   423 			}
   375 			break;
   424 			break;
   376 
   425 
   377 		case 0x16: /* Weight */
   426 		case 0x16: // Weight
   378 			FOR_EACH_OBJECT SB(rvi[i].weight, 0, 8, grf_load_byte(&buf));
   427 			FOR_EACH_OBJECT SB(rvi[i].weight, 0, 8, grf_load_byte(&buf));
   379 			break;
   428 			break;
   380 
   429 
   381 		case 0x17: /* Cost factor */
   430 		case 0x17: // Cost factor
   382 			FOR_EACH_OBJECT rvi[i].base_cost = grf_load_byte(&buf);
   431 			FOR_EACH_OBJECT rvi[i].base_cost = grf_load_byte(&buf);
   383 			break;
   432 			break;
   384 
   433 
   385 		case 0x18: /* AI rank */
   434 		case 0x18: // AI rank
   386 			FOR_EACH_OBJECT rvi[i].ai_rank = grf_load_byte(&buf);
   435 			FOR_EACH_OBJECT rvi[i].ai_rank = grf_load_byte(&buf);
   387 			break;
   436 			break;
   388 
   437 
   389 		case 0x19: /* Engine traction type */
   438 		case 0x19: // Engine traction type
   390 			/* What do the individual numbers mean?
   439 			/* What do the individual numbers mean?
   391 			 * 0x00 .. 0x07: Steam
   440 			 * 0x00 .. 0x07: Steam
   392 			 * 0x08 .. 0x27: Diesel
   441 			 * 0x08 .. 0x27: Diesel
   393 			 * 0x28 .. 0x31: Electric
   442 			 * 0x28 .. 0x31: Electric
   394 			 * 0x32 .. 0x37: Monorail
   443 			 * 0x32 .. 0x37: Monorail
   414 
   463 
   415 				rvi[i].engclass = engclass;
   464 				rvi[i].engclass = engclass;
   416 			}
   465 			}
   417 			break;
   466 			break;
   418 
   467 
   419 		case 0x1A: /* Alter purchase list sort order */
   468 		case 0x1A: // Alter purchase list sort order
   420 			FOR_EACH_OBJECT {
   469 			FOR_EACH_OBJECT {
   421 				EngineID pos = grf_load_byte(&buf);
   470 				EngineID pos = grf_load_byte(&buf);
   422 
   471 
   423 				if (pos < NUM_TRAIN_ENGINES) {
   472 				if (pos < NUM_TRAIN_ENGINES) {
   424 					AlterRailVehListOrder(engine + i, pos);
   473 					AlterRailVehListOrder(engine + i, pos);
   426 					grfmsg(2, "RailVehicleChangeInfo: Invalid train engine ID %d, ignoring", pos);
   475 					grfmsg(2, "RailVehicleChangeInfo: Invalid train engine ID %d, ignoring", pos);
   427 				}
   476 				}
   428 			}
   477 			}
   429 			break;
   478 			break;
   430 
   479 
   431 		case 0x1B: /* Powered wagons power bonus */
   480 		case 0x1B: // Powered wagons power bonus
   432 			FOR_EACH_OBJECT rvi[i].pow_wag_power = grf_load_word(&buf);
   481 			FOR_EACH_OBJECT rvi[i].pow_wag_power = grf_load_word(&buf);
   433 			break;
   482 			break;
   434 
   483 
   435 		case 0x1C: /* Refit cost */
   484 		case 0x1C: // Refit cost
   436 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   485 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   437 			break;
   486 			break;
   438 
   487 
   439 		case 0x1D: /* Refit cargo */
   488 		case 0x1D: // Refit cargo
   440 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   489 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   441 			break;
   490 			break;
   442 
   491 
   443 		case 0x1E: /* Callback */
   492 		case 0x1E: // Callback
   444 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   493 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   445 			break;
   494 			break;
   446 
   495 
   447 		case 0x1F: /* Tractive effort coefficient */
   496 		case 0x1F: // Tractive effort coefficient
   448 			FOR_EACH_OBJECT rvi[i].tractive_effort = grf_load_byte(&buf);
   497 			FOR_EACH_OBJECT rvi[i].tractive_effort = grf_load_byte(&buf);
   449 			break;
   498 			break;
   450 
   499 
   451 		case 0x21: /* Shorter vehicle */
   500 		case 0x21: // Shorter vehicle
   452 			FOR_EACH_OBJECT rvi[i].shorten_factor = grf_load_byte(&buf);
   501 			FOR_EACH_OBJECT rvi[i].shorten_factor = grf_load_byte(&buf);
   453 			break;
   502 			break;
   454 
   503 
   455 		case 0x22: /* Visual effect */
   504 		case 0x22: // Visual effect
   456 			// see note in engine.h about rvi->visual_effect
   505 			/* see note in engine.h about rvi->visual_effect */
   457 			FOR_EACH_OBJECT rvi[i].visual_effect = grf_load_byte(&buf);
   506 			FOR_EACH_OBJECT rvi[i].visual_effect = grf_load_byte(&buf);
   458 			break;
   507 			break;
   459 
   508 
   460 		case 0x23: /* Powered wagons weight bonus */
   509 		case 0x23: // Powered wagons weight bonus
   461 			FOR_EACH_OBJECT rvi[i].pow_wag_weight = grf_load_byte(&buf);
   510 			FOR_EACH_OBJECT rvi[i].pow_wag_weight = grf_load_byte(&buf);
   462 			break;
   511 			break;
   463 
   512 
   464 		case 0x24: /* High byte of vehicle weight */
   513 		case 0x24: // High byte of vehicle weight
   465 			FOR_EACH_OBJECT {
   514 			FOR_EACH_OBJECT {
   466 				byte weight = grf_load_byte(&buf);
   515 				byte weight = grf_load_byte(&buf);
   467 
   516 
   468 				if (weight > 4) {
   517 				if (weight > 4) {
   469 					grfmsg(2, "RailVehicleChangeInfo: Nonsensical weight of %d tons, ignoring", weight << 8);
   518 					grfmsg(2, "RailVehicleChangeInfo: Nonsensical weight of %d tons, ignoring", weight << 8);
   471 					SB(rvi[i].weight, 8, 8, weight);
   520 					SB(rvi[i].weight, 8, 8, weight);
   472 				}
   521 				}
   473 			}
   522 			}
   474 			break;
   523 			break;
   475 
   524 
   476 		case 0x25: /* User-defined bit mask to set when checking veh. var. 42 */
   525 		case 0x25: // User-defined bit mask to set when checking veh. var. 42
   477 			FOR_EACH_OBJECT rvi[i].user_def_data = grf_load_byte(&buf);
   526 			FOR_EACH_OBJECT rvi[i].user_def_data = grf_load_byte(&buf);
   478 			break;
   527 			break;
   479 
   528 
   480 		case 0x27: /* Miscellaneous flags */
   529 		case 0x27: // Miscellaneous flags
   481 			FOR_EACH_OBJECT {
   530 			FOR_EACH_OBJECT {
   482 				ei[i].misc_flags = grf_load_byte(&buf);
   531 				ei[i].misc_flags = grf_load_byte(&buf);
   483 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) _have_2cc = true;
   532 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) _have_2cc = true;
   484 			}
   533 			}
   485 			break;
   534 			break;
   486 
   535 
   487 		case 0x28: /* Cargo classes allowed */
   536 		case 0x28: // Cargo classes allowed
   488 			FOR_EACH_OBJECT cargo_allowed[engine + i] = grf_load_word(&buf);
   537 			FOR_EACH_OBJECT cargo_allowed[engine + i] = grf_load_word(&buf);
   489 			break;
   538 			break;
   490 
   539 
   491 		case 0x29: /* Cargo classes disallowed */
   540 		case 0x29: // Cargo classes disallowed
   492 			FOR_EACH_OBJECT cargo_disallowed[engine + i] = grf_load_word(&buf);
   541 			FOR_EACH_OBJECT cargo_disallowed[engine + i] = grf_load_word(&buf);
   493 			break;
   542 			break;
   494 
   543 
   495 		case 0x2A: /* Long format introduction date (days since year 0) */
   544 		case 0x2A: // Long format introduction date (days since year 0)
   496 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   545 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   497 			break;
   546 			break;
   498 
   547 
   499 		/* TODO */
   548 		/* @todo air drag and retire vehicle early
   500 		/* Fall-through for unimplemented one byte long properties. */
   549 		 * Fall-through for unimplemented one byte long properties. */
   501 		case 0x20: /* Air drag */
   550 		case 0x20: // Air drag
   502 		case 0x26: /* Retire vehicle early */
   551 		case 0x26: // Retire vehicle early
   503 			/* TODO */
       
   504 			FOR_EACH_OBJECT grf_load_byte(&buf);
   552 			FOR_EACH_OBJECT grf_load_byte(&buf);
   505 			ret = true;
   553 			ret = true;
   506 			break;
   554 			break;
   507 
   555 
   508 		default:
   556 		default:
   520 	byte *buf = *bufp;
   568 	byte *buf = *bufp;
   521 	int i;
   569 	int i;
   522 	bool ret = false;
   570 	bool ret = false;
   523 
   571 
   524 	switch (prop) {
   572 	switch (prop) {
   525 		case 0x08: /* Speed (1 unit is 0.5 kmh) */
   573 		case 0x08: // Speed (1 unit is 0.5 kmh)
   526 			FOR_EACH_OBJECT rvi[i].max_speed = grf_load_byte(&buf);
   574 			FOR_EACH_OBJECT rvi[i].max_speed = grf_load_byte(&buf);
   527 			break;
   575 			break;
   528 
   576 
   529 		case 0x09: /* Running cost factor */
   577 		case 0x09: // Running cost factor
   530 			FOR_EACH_OBJECT rvi[i].running_cost = grf_load_byte(&buf);
   578 			FOR_EACH_OBJECT rvi[i].running_cost = grf_load_byte(&buf);
   531 			break;
   579 			break;
   532 
   580 
   533 		case 0x0A: /* Running cost base */
   581 		case 0x0A: // Running cost base
   534 			/* TODO: I have no idea. --pasky */
   582 			/* @todo : I have no idea. --pasky
       
   583 			 * I THINK it is used for overriding the base cost of all road vehicle (_price.roadveh_base) --belugas */
   535 			FOR_EACH_OBJECT grf_load_dword(&buf);
   584 			FOR_EACH_OBJECT grf_load_dword(&buf);
   536 			ret = true;
   585 			ret = true;
   537 			break;
   586 			break;
   538 
   587 
   539 		case 0x0E: /* Sprite ID */
   588 		case 0x0E: // Sprite ID
   540 			FOR_EACH_OBJECT {
   589 			FOR_EACH_OBJECT {
   541 				uint8 spriteid = grf_load_byte(&buf);
   590 				uint8 spriteid = grf_load_byte(&buf);
   542 
   591 
   543 				// cars have different custom id in the GRF file
   592 				/* cars have different custom id in the GRF file */
   544 				if (spriteid == 0xFF) spriteid = 0xFD;
   593 				if (spriteid == 0xFF) spriteid = 0xFD;
   545 
   594 
   546 				if (spriteid < 0xFD) spriteid >>= 1;
   595 				if (spriteid < 0xFD) spriteid >>= 1;
   547 
   596 
   548 				rvi[i].image_index = spriteid;
   597 				rvi[i].image_index = spriteid;
   549 			}
   598 			}
   550 			break;
   599 			break;
   551 
   600 
   552 		case 0x0F: /* Cargo capacity */
   601 		case 0x0F: // Cargo capacity
   553 			FOR_EACH_OBJECT rvi[i].capacity = grf_load_byte(&buf);
   602 			FOR_EACH_OBJECT rvi[i].capacity = grf_load_byte(&buf);
   554 			break;
   603 			break;
   555 
   604 
   556 		case 0x10: /* Cargo type */
   605 		case 0x10: // Cargo type
   557 			FOR_EACH_OBJECT {
   606 			FOR_EACH_OBJECT {
   558 				uint8 cargo = grf_load_byte(&buf);
   607 				uint8 cargo = grf_load_byte(&buf);
   559 
   608 
   560 				if (cargo < NUM_CARGO && HASBIT(_cargo_mask, cargo)) {
   609 				if (cargo < NUM_CARGO && HASBIT(_cargo_mask, cargo)) {
   561 					rvi[i].cargo_type = cargo;
   610 					rvi[i].cargo_type = cargo;
   564 					grfmsg(2, "RoadVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   613 					grfmsg(2, "RoadVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   565 				}
   614 				}
   566 			}
   615 			}
   567 			break;
   616 			break;
   568 
   617 
   569 		case 0x11: /* Cost factor */
   618 		case 0x11: // Cost factor
   570 			FOR_EACH_OBJECT rvi[i].base_cost = grf_load_byte(&buf); // ?? is it base_cost?
   619 			FOR_EACH_OBJECT rvi[i].base_cost = grf_load_byte(&buf); // ?? is it base_cost?
   571 			break;
   620 			break;
   572 
   621 
   573 		case 0x12: /* SFX */
   622 		case 0x12: // SFX
   574 			FOR_EACH_OBJECT rvi[i].sfx = (SoundFx)grf_load_byte(&buf);
   623 			FOR_EACH_OBJECT rvi[i].sfx = (SoundFx)grf_load_byte(&buf);
   575 			break;
   624 			break;
   576 
   625 
   577 		case 0x13: /* Power in 10hp */
   626 		case 0x13: // Power in 10hp
   578 		case 0x14: /* Weight in 1/4 tons */
   627 		case 0x14: // Weight in 1/4 tons
   579 		case 0x15: /* Speed in mph*0.8 */
   628 		case 0x15: // Speed in mph*0.8
   580 			/* TODO: Support for road vehicles realistic power
   629 			/* TODO: Support for road vehicles realistic power
   581 			 * computations (called rvpower in TTDPatch) is just
   630 			 * computations (called rvpower in TTDPatch) is just
   582 			 * missing in OTTD yet. --pasky */
   631 			 * missing in OTTD yet. --pasky */
   583 			FOR_EACH_OBJECT grf_load_byte(&buf);
   632 			FOR_EACH_OBJECT grf_load_byte(&buf);
   584 			ret = true;
   633 			ret = true;
   585 			break;
   634 			break;
   586 
   635 
   587 		case 0x16: /* Cargos available for refitting */
   636 		case 0x16: // Cargos available for refitting
   588 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   637 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   589 			break;
   638 			break;
   590 
   639 
   591 		case 0x17: /* Callback mask */
   640 		case 0x17: // Callback mask
   592 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   641 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   593 			break;
   642 			break;
   594 
   643 
   595 		case 0x1A: /* Refit cost */
   644 		case 0x1A: // Refit cost
   596 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   645 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   597 			break;
   646 			break;
   598 
   647 
   599 		case 0x1C: /* Miscellaneous flags */
   648 		case 0x1C: // Miscellaneous flags
   600 			FOR_EACH_OBJECT {
   649 			FOR_EACH_OBJECT {
   601 				ei[i].misc_flags = grf_load_byte(&buf);
   650 				ei[i].misc_flags = grf_load_byte(&buf);
   602 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) _have_2cc = true;
   651 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) _have_2cc = true;
   603 			}
   652 			}
   604 			break;
   653 			break;
   605 
   654 
   606 		case 0x1D: /* Cargo classes allowed */
   655 		case 0x1D: // Cargo classes allowed
   607 			FOR_EACH_OBJECT cargo_allowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   656 			FOR_EACH_OBJECT cargo_allowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   608 			break;
   657 			break;
   609 
   658 
   610 		case 0x1E: /* Cargo classes disallowed */
   659 		case 0x1E: // Cargo classes disallowed
   611 			FOR_EACH_OBJECT cargo_disallowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   660 			FOR_EACH_OBJECT cargo_disallowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   612 			break;
   661 			break;
   613 
   662 
   614 		case 0x1F: /* Long format introduction date (days since year 0) */
   663 		case 0x1F: // Long format introduction date (days since year 0)
   615 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   664 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   616 			break;
   665 			break;
   617 
   666 
   618 		case 0x18: /* Tractive effort */
   667 		case 0x18: // Tractive effort
   619 		case 0x19: /* Air drag */
   668 		case 0x19: // Air drag
   620 		case 0x1B: /* Retire vehicle early */
   669 		case 0x1B: // Retire vehicle early
   621 			/* TODO */
   670 			/* @todo */
   622 			FOR_EACH_OBJECT grf_load_byte(&buf);
   671 			FOR_EACH_OBJECT grf_load_byte(&buf);
   623 			ret = true;
   672 			ret = true;
   624 			break;
   673 			break;
   625 
   674 
   626 		default:
   675 		default:
   640 	int i;
   689 	int i;
   641 	bool ret = false;
   690 	bool ret = false;
   642 
   691 
   643 	//printf("e %x prop %x?\n", engine, prop);
   692 	//printf("e %x prop %x?\n", engine, prop);
   644 	switch (prop) {
   693 	switch (prop) {
   645 		case 0x08: /* Sprite ID */
   694 		case 0x08: // Sprite ID
   646 			FOR_EACH_OBJECT {
   695 			FOR_EACH_OBJECT {
   647 				uint8 spriteid = grf_load_byte(&buf);
   696 				uint8 spriteid = grf_load_byte(&buf);
   648 
   697 
   649 				// ships have different custom id in the GRF file
   698 				/* ships have different custom id in the GRF file */
   650 				if (spriteid == 0xFF) spriteid = 0xFD;
   699 				if (spriteid == 0xFF) spriteid = 0xFD;
   651 
   700 
   652 				if (spriteid < 0xFD) spriteid >>= 1;
   701 				if (spriteid < 0xFD) spriteid >>= 1;
   653 
   702 
   654 				svi[i].image_index = spriteid;
   703 				svi[i].image_index = spriteid;
   655 			}
   704 			}
   656 			break;
   705 			break;
   657 
   706 
   658 		case 0x09: /* Refittable */
   707 		case 0x09: // Refittable
   659 			FOR_EACH_OBJECT svi[i].refittable = (grf_load_byte(&buf) != 0);
   708 			FOR_EACH_OBJECT svi[i].refittable = (grf_load_byte(&buf) != 0);
   660 			break;
   709 			break;
   661 
   710 
   662 		case 0x0A: /* Cost factor */
   711 		case 0x0A: // Cost factor
   663 			FOR_EACH_OBJECT svi[i].base_cost = grf_load_byte(&buf); // ?? is it base_cost?
   712 			FOR_EACH_OBJECT svi[i].base_cost = grf_load_byte(&buf); // ?? is it base_cost?
   664 			break;
   713 			break;
   665 
   714 
   666 		case 0x0B: /* Speed (1 unit is 0.5 kmh) */
   715 		case 0x0B: // Speed (1 unit is 0.5 kmh)
   667 			FOR_EACH_OBJECT svi[i].max_speed = grf_load_byte(&buf);
   716 			FOR_EACH_OBJECT svi[i].max_speed = grf_load_byte(&buf);
   668 			break;
   717 			break;
   669 
   718 
   670 		case 0x0C: /* Cargo type */
   719 		case 0x0C: // Cargo type
   671 			FOR_EACH_OBJECT {
   720 			FOR_EACH_OBJECT {
   672 				uint8 cargo = grf_load_byte(&buf);
   721 				uint8 cargo = grf_load_byte(&buf);
   673 
   722 
   674 				if (cargo < NUM_CARGO && HASBIT(_cargo_mask, cargo)) {
   723 				if (cargo < NUM_CARGO && HASBIT(_cargo_mask, cargo)) {
   675 					svi[i].cargo_type = cargo;
   724 					svi[i].cargo_type = cargo;
   678 					grfmsg(2, "ShipVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   727 					grfmsg(2, "ShipVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   679 				}
   728 				}
   680 			}
   729 			}
   681 			break;
   730 			break;
   682 
   731 
   683 		case 0x0D: /* Cargo capacity */
   732 		case 0x0D: // Cargo capacity
   684 			FOR_EACH_OBJECT svi[i].capacity = grf_load_word(&buf);
   733 			FOR_EACH_OBJECT svi[i].capacity = grf_load_word(&buf);
   685 			break;
   734 			break;
   686 
   735 
   687 		case 0x0F: /* Running cost factor */
   736 		case 0x0F: // Running cost factor
   688 			FOR_EACH_OBJECT svi[i].running_cost = grf_load_byte(&buf);
   737 			FOR_EACH_OBJECT svi[i].running_cost = grf_load_byte(&buf);
   689 			break;
   738 			break;
   690 
   739 
   691 		case 0x10: /* SFX */
   740 		case 0x10: // SFX
   692 			FOR_EACH_OBJECT svi[i].sfx = (SoundFx)grf_load_byte(&buf);
   741 			FOR_EACH_OBJECT svi[i].sfx = (SoundFx)grf_load_byte(&buf);
   693 			break;
   742 			break;
   694 
   743 
   695 		case 0x11: /* Cargos available for refitting */
   744 		case 0x11: // Cargos available for refitting
   696 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   745 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   697 			break;
   746 			break;
   698 
   747 
   699 		case 0x12: /* Callback mask */
   748 		case 0x12: // Callback mask
   700 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   749 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   701 			break;
   750 			break;
   702 
   751 
   703 		case 0x13: /* Refit cost */
   752 		case 0x13: // Refit cost
   704 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   753 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   705 			break;
   754 			break;
   706 
   755 
   707 		case 0x17: /* Miscellaneous flags */
   756 		case 0x17: // Miscellaneous flags
   708 			FOR_EACH_OBJECT {
   757 			FOR_EACH_OBJECT {
   709 				ei[i].misc_flags = grf_load_byte(&buf);
   758 				ei[i].misc_flags = grf_load_byte(&buf);
   710 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) _have_2cc = true;
   759 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) _have_2cc = true;
   711 			}
   760 			}
   712 			break;
   761 			break;
   713 
   762 
   714 		case 0x18: /* Cargo classes allowed */
   763 		case 0x18: // Cargo classes allowed
   715 			FOR_EACH_OBJECT cargo_allowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   764 			FOR_EACH_OBJECT cargo_allowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   716 			break;
   765 			break;
   717 
   766 
   718 		case 0x19: /* Cargo classes disallowed */
   767 		case 0x19: // Cargo classes disallowed
   719 			FOR_EACH_OBJECT cargo_disallowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   768 			FOR_EACH_OBJECT cargo_disallowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   720 			break;
   769 			break;
   721 
   770 
   722 		case 0x1A: /* Long format introduction date (days since year 0) */
   771 		case 0x1A: // Long format introduction date (days since year 0)
   723 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   772 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   724 			break;
   773 			break;
   725 
   774 
   726 		case 0x14: /* Ocean speed fraction */
   775 		case 0x14: // Ocean speed fraction
   727 		case 0x15: /* Canal speed fraction */
   776 		case 0x15: // Canal speed fraction
   728 		case 0x16: /* Retire vehicle early */
   777 		case 0x16: // Retire vehicle early
   729 			/* TODO */
   778 			/* @todo */
   730 			FOR_EACH_OBJECT grf_load_byte(&buf);
   779 			FOR_EACH_OBJECT grf_load_byte(&buf);
   731 			ret = true;
   780 			ret = true;
   732 			break;
   781 			break;
   733 
   782 
   734 		default:
   783 		default:
   748 	int i;
   797 	int i;
   749 	bool ret = false;
   798 	bool ret = false;
   750 
   799 
   751 	//printf("e %x prop %x?\n", engine, prop);
   800 	//printf("e %x prop %x?\n", engine, prop);
   752 	switch (prop) {
   801 	switch (prop) {
   753 		case 0x08: /* Sprite ID */
   802 		case 0x08: // Sprite ID
   754 			FOR_EACH_OBJECT {
   803 			FOR_EACH_OBJECT {
   755 				uint8 spriteid = grf_load_byte(&buf);
   804 				uint8 spriteid = grf_load_byte(&buf);
   756 
   805 
   757 				// aircraft have different custom id in the GRF file
   806 				/* aircraft have different custom id in the GRF file */
   758 				if (spriteid == 0xFF) spriteid = 0xFD;
   807 				if (spriteid == 0xFF) spriteid = 0xFD;
   759 
   808 
   760 				if (spriteid < 0xFD) spriteid >>= 1;
   809 				if (spriteid < 0xFD) spriteid >>= 1;
   761 
   810 
   762 				avi[i].image_index = spriteid;
   811 				avi[i].image_index = spriteid;
   763 			}
   812 			}
   764 			break;
   813 			break;
   765 
   814 
   766 		case 0x09: /* Helicopter */
   815 		case 0x09: // Helicopter
   767 			FOR_EACH_OBJECT {
   816 			FOR_EACH_OBJECT {
   768 				if (grf_load_byte(&buf) == 0) {
   817 				if (grf_load_byte(&buf) == 0) {
   769 					avi[i].subtype = AIR_HELI;
   818 					avi[i].subtype = AIR_HELI;
   770 				} else {
   819 				} else {
   771 					SB(avi[i].subtype, 0, 1, 1); // AIR_CTOL
   820 					SB(avi[i].subtype, 0, 1, 1); // AIR_CTOL
   772 				}
   821 				}
   773 			}
   822 			}
   774 			break;
   823 			break;
   775 
   824 
   776 		case 0x0A: /* Large */
   825 		case 0x0A: // Large
   777 			FOR_EACH_OBJECT SB(avi[i].subtype, 1, 1, (grf_load_byte(&buf) != 0 ? 1 : 0)); // AIR_FAST
   826 			FOR_EACH_OBJECT SB(avi[i].subtype, 1, 1, (grf_load_byte(&buf) != 0 ? 1 : 0)); // AIR_FAST
   778 			break;
   827 			break;
   779 
   828 
   780 		case 0x0B: /* Cost factor */
   829 		case 0x0B: // Cost factor
   781 			FOR_EACH_OBJECT avi[i].base_cost = grf_load_byte(&buf); // ?? is it base_cost?
   830 			FOR_EACH_OBJECT avi[i].base_cost = grf_load_byte(&buf); // ?? is it base_cost?
   782 			break;
   831 			break;
   783 
   832 
   784 		case 0x0C: /* Speed (1 unit is 8 mph, we translate to 1 unit is 1 km/h) */
   833 		case 0x0C: // Speed (1 unit is 8 mph, we translate to 1 unit is 1 km/h)
   785 			FOR_EACH_OBJECT avi[i].max_speed = (grf_load_byte(&buf) * 129) / 10;
   834 			FOR_EACH_OBJECT avi[i].max_speed = (grf_load_byte(&buf) * 129) / 10;
   786 			break;
   835 			break;
   787 
   836 
   788 		case 0x0D: /* Acceleration */
   837 		case 0x0D: // Acceleration
   789 			FOR_EACH_OBJECT avi[i].acceleration = (grf_load_byte(&buf) * 129) / 10;
   838 			FOR_EACH_OBJECT avi[i].acceleration = (grf_load_byte(&buf) * 129) / 10;
   790 			break;
   839 			break;
   791 
   840 
   792 		case 0x0E: /* Running cost factor */
   841 		case 0x0E: // Running cost factor
   793 			FOR_EACH_OBJECT avi[i].running_cost = grf_load_byte(&buf);
   842 			FOR_EACH_OBJECT avi[i].running_cost = grf_load_byte(&buf);
   794 			break;
   843 			break;
   795 
   844 
   796 		case 0x0F: /* Passenger capacity */
   845 		case 0x0F: // Passenger capacity
   797 			FOR_EACH_OBJECT avi[i].passenger_capacity = grf_load_word(&buf);
   846 			FOR_EACH_OBJECT avi[i].passenger_capacity = grf_load_word(&buf);
   798 			break;
   847 			break;
   799 
   848 
   800 		case 0x11: /* Mail capacity */
   849 		case 0x11: // Mail capacity
   801 			FOR_EACH_OBJECT avi[i].mail_capacity = grf_load_byte(&buf);
   850 			FOR_EACH_OBJECT avi[i].mail_capacity = grf_load_byte(&buf);
   802 			break;
   851 			break;
   803 
   852 
   804 		case 0x12: /* SFX */
   853 		case 0x12: // SFX
   805 			FOR_EACH_OBJECT avi[i].sfx = (SoundFx)grf_load_byte(&buf);
   854 			FOR_EACH_OBJECT avi[i].sfx = (SoundFx)grf_load_byte(&buf);
   806 			break;
   855 			break;
   807 
   856 
   808 		case 0x13: /* Cargos available for refitting */
   857 		case 0x13: // Cargos available for refitting
   809 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   858 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   810 			break;
   859 			break;
   811 
   860 
   812 		case 0x14: /* Callback mask */
   861 		case 0x14: // Callback mask
   813 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   862 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   814 			break;
   863 			break;
   815 
   864 
   816 		case 0x15: /* Refit cost */
   865 		case 0x15: // Refit cost
   817 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   866 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   818 			break;
   867 			break;
   819 
   868 
   820 		case 0x17: /* Miscellaneous flags */
   869 		case 0x17: // Miscellaneous flags
   821 			FOR_EACH_OBJECT {
   870 			FOR_EACH_OBJECT {
   822 				ei[i].misc_flags = grf_load_byte(&buf);
   871 				ei[i].misc_flags = grf_load_byte(&buf);
   823 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) _have_2cc = true;
   872 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) _have_2cc = true;
   824 			}
   873 			}
   825 			break;
   874 			break;
   826 
   875 
   827 		case 0x18: /* Cargo classes allowed */
   876 		case 0x18: // Cargo classes allowed
   828 			FOR_EACH_OBJECT cargo_allowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   877 			FOR_EACH_OBJECT cargo_allowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   829 			break;
   878 			break;
   830 
   879 
   831 		case 0x19: /* Cargo classes disallowed */
   880 		case 0x19: // Cargo classes disallowed
   832 			FOR_EACH_OBJECT cargo_disallowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   881 			FOR_EACH_OBJECT cargo_disallowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   833 			break;
   882 			break;
   834 
   883 
   835 		case 0x1A: /* Long format introduction date (days since year 0) */
   884 		case 0x1A: // Long format introduction date (days since year 0)
   836 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   885 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   837 			break;
   886 			break;
   838 
   887 
   839 		case 0x16: /* Retire vehicle early */
   888 		case 0x16: // Retire vehicle early
   840 			/* TODO */
   889 			/* @todo */
   841 			FOR_EACH_OBJECT grf_load_byte(&buf);
   890 			FOR_EACH_OBJECT grf_load_byte(&buf);
   842 			ret = true;
   891 			ret = true;
   843 			break;
   892 			break;
   844 
   893 
   845 		default:
   894 		default:
   877 			}
   926 			}
   878 		}
   927 		}
   879 	}
   928 	}
   880 
   929 
   881 	switch (prop) {
   930 	switch (prop) {
   882 		case 0x08: /* Class ID */
   931 		case 0x08: // Class ID
   883 			FOR_EACH_OBJECT {
   932 			FOR_EACH_OBJECT {
   884 				/* Property 0x08 is special; it is where the station is allocated */
   933 				/* Property 0x08 is special; it is where the station is allocated */
   885 				if (statspec[i] == NULL) statspec[i] = CallocT<StationSpec>(1);
   934 				if (statspec[i] == NULL) statspec[i] = CallocT<StationSpec>(1);
   886 
   935 
   887 				/* Swap classid because we read it in BE meaning WAYP or DFLT */
   936 				/* Swap classid because we read it in BE meaning WAYP or DFLT */
   888 				uint32 classid = grf_load_dword(&buf);
   937 				uint32 classid = grf_load_dword(&buf);
   889 				statspec[i]->sclass = AllocateStationClass(BSWAP32(classid));
   938 				statspec[i]->sclass = AllocateStationClass(BSWAP32(classid));
   890 			}
   939 			}
   891 			break;
   940 			break;
   892 
   941 
   893 		case 0x09: /* Define sprite layout */
   942 		case 0x09: // Define sprite layout
   894 			FOR_EACH_OBJECT {
   943 			FOR_EACH_OBJECT {
   895 				StationSpec *statspec = _cur_grffile->stations[stid + i];
   944 				StationSpec *statspec = _cur_grffile->stations[stid + i];
   896 
   945 
   897 				statspec->tiles = grf_load_extended(&buf);
   946 				statspec->tiles = grf_load_extended(&buf);
   898 				statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
   947 				statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
   912 					}
   961 					}
   913 
   962 
   914 					while (buf < *bufp + len) {
   963 					while (buf < *bufp + len) {
   915 						DrawTileSeqStruct *dtss;
   964 						DrawTileSeqStruct *dtss;
   916 
   965 
   917 						// no relative bounding box support
   966 						/* no relative bounding box support */
   918 						dts->seq = ReallocT((DrawTileSeqStruct*)dts->seq, ++seq_count);
   967 						dts->seq = ReallocT((DrawTileSeqStruct*)dts->seq, ++seq_count);
   919 						dtss = (DrawTileSeqStruct*) &dts->seq[seq_count - 1];
   968 						dtss = (DrawTileSeqStruct*) &dts->seq[seq_count - 1];
   920 
   969 
   921 						dtss->delta_x = grf_load_byte(&buf);
   970 						dtss->delta_x = grf_load_byte(&buf);
   922 						if ((byte) dtss->delta_x == 0x80) break;
   971 						if ((byte) dtss->delta_x == 0x80) break;
   945 					}
   994 					}
   946 				}
   995 				}
   947 			}
   996 			}
   948 			break;
   997 			break;
   949 
   998 
   950 		case 0x0A: /* Copy sprite layout */
   999 		case 0x0A: // Copy sprite layout
   951 			FOR_EACH_OBJECT {
  1000 			FOR_EACH_OBJECT {
   952 				StationSpec *statspec = _cur_grffile->stations[stid + i];
  1001 				StationSpec *statspec = _cur_grffile->stations[stid + i];
   953 				byte srcid = grf_load_byte(&buf);
  1002 				byte srcid = grf_load_byte(&buf);
   954 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  1003 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
   955 
  1004 
   957 				statspec->renderdata = srcstatspec->renderdata;
  1006 				statspec->renderdata = srcstatspec->renderdata;
   958 				statspec->copied_renderdata = true;
  1007 				statspec->copied_renderdata = true;
   959 			}
  1008 			}
   960 			break;
  1009 			break;
   961 
  1010 
   962 		case 0x0B: /* Callback mask */
  1011 		case 0x0B: // Callback mask
   963 			FOR_EACH_OBJECT statspec[i]->callbackmask = grf_load_byte(&buf);
  1012 			FOR_EACH_OBJECT statspec[i]->callbackmask = grf_load_byte(&buf);
   964 			break;
  1013 			break;
   965 
  1014 
   966 		case 0x0C: /* Disallowed number of platforms */
  1015 		case 0x0C: // Disallowed number of platforms
   967 			FOR_EACH_OBJECT statspec[i]->disallowed_platforms = grf_load_byte(&buf);
  1016 			FOR_EACH_OBJECT statspec[i]->disallowed_platforms = grf_load_byte(&buf);
   968 			break;
  1017 			break;
   969 
  1018 
   970 		case 0x0D: /* Disallowed platform lengths */
  1019 		case 0x0D: // Disallowed platform lengths
   971 			FOR_EACH_OBJECT statspec[i]->disallowed_lengths = grf_load_byte(&buf);
  1020 			FOR_EACH_OBJECT statspec[i]->disallowed_lengths = grf_load_byte(&buf);
   972 			break;
  1021 			break;
   973 
  1022 
   974 		case 0x0E: /* Define custom layout */
  1023 		case 0x0E: // Define custom layout
   975 			FOR_EACH_OBJECT {
  1024 			FOR_EACH_OBJECT {
   976 				StationSpec *statspec = _cur_grffile->stations[stid + i];
  1025 				StationSpec *statspec = _cur_grffile->stations[stid + i];
   977 
  1026 
   978 				statspec->copied_layouts = false;
  1027 				statspec->copied_layouts = false;
   979 
  1028 
   999 					l = length - 1; // index is zero-based
  1048 					l = length - 1; // index is zero-based
  1000 
  1049 
  1001 					//debug("p %d > %d ?", number, stat->platforms[l]);
  1050 					//debug("p %d > %d ?", number, stat->platforms[l]);
  1002 					if (number > statspec->platforms[l]) {
  1051 					if (number > statspec->platforms[l]) {
  1003 						statspec->layouts[l] = ReallocT(statspec->layouts[l], number);
  1052 						statspec->layouts[l] = ReallocT(statspec->layouts[l], number);
  1004 						// We expect NULL being 0 here, but C99 guarantees that.
  1053 						/* We expect NULL being 0 here, but C99 guarantees that. */
  1005 						memset(statspec->layouts[l] + statspec->platforms[l], 0,
  1054 						memset(statspec->layouts[l] + statspec->platforms[l], 0,
  1006 						       (number - statspec->platforms[l]) * sizeof(**statspec->layouts));
  1055 						       (number - statspec->platforms[l]) * sizeof(**statspec->layouts));
  1007 
  1056 
  1008 						statspec->platforms[l] = number;
  1057 						statspec->platforms[l] = number;
  1009 					}
  1058 					}
  1022 					statspec->layouts[l][p] = layout;
  1071 					statspec->layouts[l][p] = layout;
  1023 				}
  1072 				}
  1024 			}
  1073 			}
  1025 			break;
  1074 			break;
  1026 
  1075 
  1027 		case 0x0F: /* Copy custom layout */
  1076 		case 0x0F: // Copy custom layout
  1028 			FOR_EACH_OBJECT {
  1077 			FOR_EACH_OBJECT {
  1029 				StationSpec *statspec = _cur_grffile->stations[stid + i];
  1078 				StationSpec *statspec = _cur_grffile->stations[stid + i];
  1030 				byte srcid = grf_load_byte(&buf);
  1079 				byte srcid = grf_load_byte(&buf);
  1031 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  1080 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  1032 
  1081 
  1035 				statspec->layouts   = srcstatspec->layouts;
  1084 				statspec->layouts   = srcstatspec->layouts;
  1036 				statspec->copied_layouts = true;
  1085 				statspec->copied_layouts = true;
  1037 			}
  1086 			}
  1038 			break;
  1087 			break;
  1039 
  1088 
  1040 		case 0x10: /* Little/lots cargo threshold */
  1089 		case 0x10: // Little/lots cargo threshold
  1041 			FOR_EACH_OBJECT statspec[i]->cargo_threshold = grf_load_word(&buf);
  1090 			FOR_EACH_OBJECT statspec[i]->cargo_threshold = grf_load_word(&buf);
  1042 			break;
  1091 			break;
  1043 
  1092 
  1044 		case 0x11: /* Pylon placement */
  1093 		case 0x11: // Pylon placement
  1045 			FOR_EACH_OBJECT statspec[i]->pylons = grf_load_byte(&buf);
  1094 			FOR_EACH_OBJECT statspec[i]->pylons = grf_load_byte(&buf);
  1046 			break;
  1095 			break;
  1047 
  1096 
  1048 		case 0x12: /* Cargo types for random triggers */
  1097 		case 0x12: // Cargo types for random triggers
  1049 			FOR_EACH_OBJECT statspec[i]->cargo_triggers = grf_load_dword(&buf);
  1098 			FOR_EACH_OBJECT statspec[i]->cargo_triggers = grf_load_dword(&buf);
  1050 			break;
  1099 			break;
  1051 
  1100 
  1052 		case 0x13: /* General flags */
  1101 		case 0x13: // General flags
  1053 			FOR_EACH_OBJECT statspec[i]->flags = grf_load_byte(&buf);
  1102 			FOR_EACH_OBJECT statspec[i]->flags = grf_load_byte(&buf);
  1054 			break;
  1103 			break;
  1055 
  1104 
  1056 		case 0x14: /* Overhead wire placement */
  1105 		case 0x14: // Overhead wire placement
  1057 			FOR_EACH_OBJECT statspec[i]->wires = grf_load_byte(&buf);
  1106 			FOR_EACH_OBJECT statspec[i]->wires = grf_load_byte(&buf);
  1058 			break;
  1107 			break;
  1059 
  1108 
  1060 		case 0x15: /* Blocked tiles */
  1109 		case 0x15: // Blocked tiles
  1061 			FOR_EACH_OBJECT statspec[i]->blocked = grf_load_byte(&buf);
  1110 			FOR_EACH_OBJECT statspec[i]->blocked = grf_load_byte(&buf);
  1062 			break;
  1111 			break;
  1063 
  1112 
  1064 		case 0x16: /* TODO Animation info */
  1113 		case 0x16: // @todo Animation info
  1065 			FOR_EACH_OBJECT grf_load_word(&buf);
  1114 			FOR_EACH_OBJECT grf_load_word(&buf);
  1066 			ret = true;
  1115 			ret = true;
  1067 			break;
  1116 			break;
  1068 
  1117 
  1069 		case 0x17: /* TODO Animation speed */
  1118 		case 0x17: // @todo Animation speed
  1070 			FOR_EACH_OBJECT grf_load_byte(&buf);
  1119 			FOR_EACH_OBJECT grf_load_byte(&buf);
  1071 			ret = true;
  1120 			ret = true;
  1072 			break;
  1121 			break;
  1073 
  1122 
  1074 		case 0x18: /* TODO Animation triggers */
  1123 		case 0x18: // @todo Animation triggers
  1075 			FOR_EACH_OBJECT grf_load_word(&buf);
  1124 			FOR_EACH_OBJECT grf_load_word(&buf);
  1076 			ret = true;
  1125 			ret = true;
  1077 			break;
  1126 			break;
  1078 
  1127 
  1079 		default:
  1128 		default:
  1090 	byte *buf = *bufp;
  1139 	byte *buf = *bufp;
  1091 	int i;
  1140 	int i;
  1092 	bool ret = false;
  1141 	bool ret = false;
  1093 
  1142 
  1094 	switch (prop) {
  1143 	switch (prop) {
  1095 		case 0x08: /* Year of availability */
  1144 		case 0x08: // Year of availability
  1096 			FOR_EACH_OBJECT _bridge[brid + i].avail_year = ORIGINAL_BASE_YEAR + grf_load_byte(&buf);
  1145 			FOR_EACH_OBJECT _bridge[brid + i].avail_year = ORIGINAL_BASE_YEAR + grf_load_byte(&buf);
  1097 			break;
  1146 			break;
  1098 
  1147 
  1099 		case 0x09: /* Minimum length */
  1148 		case 0x09: // Minimum length
  1100 			FOR_EACH_OBJECT _bridge[brid + i].min_length = grf_load_byte(&buf);
  1149 			FOR_EACH_OBJECT _bridge[brid + i].min_length = grf_load_byte(&buf);
  1101 			break;
  1150 			break;
  1102 
  1151 
  1103 		case 0x0A: /* Maximum length */
  1152 		case 0x0A: // Maximum length
  1104 			FOR_EACH_OBJECT _bridge[brid + i].max_length = grf_load_byte(&buf);
  1153 			FOR_EACH_OBJECT _bridge[brid + i].max_length = grf_load_byte(&buf);
  1105 			break;
  1154 			break;
  1106 
  1155 
  1107 		case 0x0B: /* Cost factor */
  1156 		case 0x0B: // Cost factor
  1108 			FOR_EACH_OBJECT _bridge[brid + i].price = grf_load_byte(&buf);
  1157 			FOR_EACH_OBJECT _bridge[brid + i].price = grf_load_byte(&buf);
  1109 			break;
  1158 			break;
  1110 
  1159 
  1111 		case 0x0C: /* Maximum speed */
  1160 		case 0x0C: // Maximum speed
  1112 			FOR_EACH_OBJECT _bridge[brid + i].speed = grf_load_word(&buf);
  1161 			FOR_EACH_OBJECT _bridge[brid + i].speed = grf_load_word(&buf);
  1113 			break;
  1162 			break;
  1114 
  1163 
  1115 		case 0x0D: /* Bridge sprite tables */
  1164 		case 0x0D: // Bridge sprite tables
  1116 			FOR_EACH_OBJECT {
  1165 			FOR_EACH_OBJECT {
  1117 				Bridge *bridge = &_bridge[brid + i];
  1166 				Bridge *bridge = &_bridge[brid + i];
  1118 				byte tableid = grf_load_byte(&buf);
  1167 				byte tableid = grf_load_byte(&buf);
  1119 				byte numtables = grf_load_byte(&buf);
  1168 				byte numtables = grf_load_byte(&buf);
  1120 
  1169 
  1150 					}
  1199 					}
  1151 				}
  1200 				}
  1152 			}
  1201 			}
  1153 			break;
  1202 			break;
  1154 
  1203 
  1155 		case 0x0E: /* Flags; bit 0 - disable far pillars */
  1204 		case 0x0E: // Flags; bit 0 - disable far pillars
  1156 			FOR_EACH_OBJECT _bridge[brid + i].flags = grf_load_byte(&buf);
  1205 			FOR_EACH_OBJECT _bridge[brid + i].flags = grf_load_byte(&buf);
  1157 			break;
  1206 			break;
  1158 
  1207 
  1159 		case 0x0F: /* Long format year of availability (year since year 0) */
  1208 		case 0x0F: // Long format year of availability (year since year 0)
  1160 			FOR_EACH_OBJECT _bridge[brid + i].avail_year = clamp(grf_load_dword(&buf), MIN_YEAR, MAX_YEAR);
  1209 			FOR_EACH_OBJECT _bridge[brid + i].avail_year = clamp(grf_load_dword(&buf), MIN_YEAR, MAX_YEAR);
  1161 			break;
  1210 			break;
  1162 
  1211 
  1163 		default:
  1212 		default:
  1164 			ret = true;
  1213 			ret = true;
       
  1214 	}
       
  1215 
       
  1216 	*bufp = buf;
       
  1217 	return ret;
       
  1218 }
       
  1219 
       
  1220 static bool TownHouseChangeInfo(uint hid, int numinfo, int prop, byte **bufp, int len)
       
  1221 {
       
  1222 	HouseSpec **housespec;
       
  1223 	byte *buf = *bufp;
       
  1224 	int i;
       
  1225 	bool ret = false;
       
  1226 
       
  1227 	if (hid + numinfo >= HOUSE_MAX) {
       
  1228 		grfmsg(1, "TownHouseChangeInfo: Too many houses loaded (%u), max (%u). Ignoring.", hid + numinfo, HOUSE_MAX-1);
       
  1229 		return false;
       
  1230 	}
       
  1231 
       
  1232 	/* Allocate house specs if they haven't been allocated already. */
       
  1233 	if (_cur_grffile->housespec == NULL) {
       
  1234 		_cur_grffile->housespec = CallocT<HouseSpec*>(HOUSE_MAX);
       
  1235 
       
  1236 		/* Reset any overrides that have been set. */
       
  1237 		ResetHouseOverrides();
       
  1238 	}
       
  1239 
       
  1240 	housespec = &_cur_grffile->housespec[hid];
       
  1241 
       
  1242 	if (prop != 0x08) {
       
  1243 		/* Check that all the houses being modified have been defined. */
       
  1244 		FOR_EACH_OBJECT {
       
  1245 			if (housespec[i] == NULL) {
       
  1246 				grfmsg(2, "TownHouseChangeInfo: Attempt to modify undefined house %u. Ignoring.", hid + i);
       
  1247 				return false;
       
  1248 			}
       
  1249 		}
       
  1250 	}
       
  1251 
       
  1252 	switch (prop) {
       
  1253 		case 0x08: // Substitute building type, and definition of a new house
       
  1254 			FOR_EACH_OBJECT {
       
  1255 				byte subs_id = grf_load_byte(&buf);
       
  1256 
       
  1257 				if (subs_id == 0xFF) {
       
  1258 					/* Instead of defining a new house, a substitute house id
       
  1259 					 * of 0xFF disables the old house with the current id. */
       
  1260 					_house_specs[hid + i].enabled = false;
       
  1261 					continue;
       
  1262 				} else if (subs_id >= NEW_HOUSE_OFFSET) {
       
  1263 					/* The substitute id must be one of the original houses. */
       
  1264 					grfmsg(2, "TownHouseChangeInfo: Attempt to use new house %u as substitute house for %u. Ignoring.", subs_id, hid + i);
       
  1265 					return false;
       
  1266 				}
       
  1267 
       
  1268 				/* Allocate space for this house. */
       
  1269 				if (housespec[i] == NULL) housespec[i] = CallocT<HouseSpec>(1);
       
  1270 
       
  1271 				memcpy(housespec[i], &_house_specs[subs_id], sizeof(_house_specs[subs_id]));
       
  1272 
       
  1273 				housespec[i]->enabled = true;
       
  1274 				housespec[i]->local_id = hid + i;
       
  1275 				housespec[i]->substitute_id = subs_id;
       
  1276 				housespec[i]->grffile = _cur_grffile;
       
  1277 				housespec[i]->random_colour[0] = 0x04;  // those 4 random colours are the base colour
       
  1278 				housespec[i]->random_colour[1] = 0x08;  // for all new houses
       
  1279 				housespec[i]->random_colour[2] = 0x0C;  // they stand for red, blue, orange and green
       
  1280 				housespec[i]->random_colour[3] = 0x06;
       
  1281 
       
  1282 				/* New houses do not (currently) expect to have a default start
       
  1283 				 * date before 1930, as this breaks the build date stuff. See
       
  1284 				 * FinaliseHouseArray() for more details. */
       
  1285 				if (housespec[i]->min_date < 1930) housespec[i]->min_date = 1930;
       
  1286 			}
       
  1287 			_have_newhouses = true;
       
  1288 			break;
       
  1289 
       
  1290 		case 0x09: // Building flags
       
  1291 			FOR_EACH_OBJECT {
       
  1292 				byte state = grf_load_byte(&buf);
       
  1293 				housespec[i]->building_flags = (BuildingFlags)state;
       
  1294 			}
       
  1295 			break;
       
  1296 
       
  1297 		case 0x0A: // Availability years
       
  1298 			FOR_EACH_OBJECT {
       
  1299 				uint16 years = grf_load_word(&buf);
       
  1300 				housespec[i]->min_date = GB(years, 0, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 0, 8);
       
  1301 				housespec[i]->max_date = GB(years, 8, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 8, 8);
       
  1302 			}
       
  1303 			break;
       
  1304 
       
  1305 		case 0x0B: // Population
       
  1306 			FOR_EACH_OBJECT housespec[i]->population = grf_load_byte(&buf);
       
  1307 			break;
       
  1308 
       
  1309 		case 0x0C: // Mail generation multiplier
       
  1310 			FOR_EACH_OBJECT housespec[i]->mail_generation = grf_load_byte(&buf);
       
  1311 			break;
       
  1312 
       
  1313 		case 0x0D: // Passenger acceptance
       
  1314 		case 0x0E: // Mail acceptance
       
  1315 			FOR_EACH_OBJECT housespec[i]->cargo_acceptance[prop - 0x0D] = grf_load_byte(&buf);
       
  1316 			break;
       
  1317 		case 0x0F: // Goods/candy, food/fizzy drinks acceptance
       
  1318 			FOR_EACH_OBJECT {
       
  1319 				int8 goods = grf_load_byte(&buf);
       
  1320 
       
  1321 				/* If value of goods is negative, it means in fact food or, if in toyland, fizzy_drink acceptance.
       
  1322 				 * Else, we have "standard" 3rd cargo type, goods or candy, for toyland once more */
       
  1323 				housespec[i]->accepts_cargo[2] = (goods >= 0) ? ((_opt.landscape == LT_TOYLAND) ? CT_CANDY : CT_GOODS) :
       
  1324 						((_opt.landscape == LT_TOYLAND) ? CT_FIZZY_DRINKS : CT_FOOD);
       
  1325 
       
  1326 				housespec[i]->cargo_acceptance[2] = abs(goods); // but we do need positive value here
       
  1327 			}
       
  1328 			break;
       
  1329 
       
  1330 		case 0x10: // Local authority rating decrease on removal
       
  1331 			FOR_EACH_OBJECT housespec[i]->remove_rating_decrease = grf_load_word(&buf);
       
  1332 			break;
       
  1333 
       
  1334 		case 0x11: // Removal cost multiplier
       
  1335 			FOR_EACH_OBJECT housespec[i]->removal_cost = grf_load_byte(&buf);
       
  1336 			break;
       
  1337 
       
  1338 		case 0x12: // Building name ID
       
  1339 			FOR_EACH_OBJECT housespec[i]->building_name = MapGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
       
  1340 			break;
       
  1341 
       
  1342 		case 0x13: // Building availability mask
       
  1343 			FOR_EACH_OBJECT {
       
  1344 				uint16 avail = grf_load_word(&buf);
       
  1345 				housespec[i]->building_availability = (HouseZones)avail;
       
  1346 			}
       
  1347 			break;
       
  1348 
       
  1349 		case 0x14: // House callback flags
       
  1350 			FOR_EACH_OBJECT housespec[i]->callback_mask = grf_load_byte(&buf);
       
  1351 			break;
       
  1352 
       
  1353 		case 0x15: // House override byte
       
  1354 			FOR_EACH_OBJECT {
       
  1355 				byte override = grf_load_byte(&buf);
       
  1356 
       
  1357 				/* The house being overridden must be an original house. */
       
  1358 				if (override >= NEW_HOUSE_OFFSET) {
       
  1359 					grfmsg(2, "TownHouseChangeInfo: Attempt to override new house %u with house id %u. Ignoring.", override, hid);
       
  1360 					return false;
       
  1361 				}
       
  1362 
       
  1363 				AddHouseOverride(hid, override);
       
  1364 			}
       
  1365 			break;
       
  1366 
       
  1367 		case 0x16: // Periodic refresh multiplier
       
  1368 			FOR_EACH_OBJECT housespec[i]->processing_time = grf_load_byte(&buf);
       
  1369 			break;
       
  1370 
       
  1371 		case 0x17: // Four random colours to use
       
  1372 			FOR_EACH_OBJECT {
       
  1373 				uint j;
       
  1374 				for (j = 0; j < 4; j++)	housespec[i]->random_colour[j] = grf_load_byte(&buf);
       
  1375 			}
       
  1376 			break;
       
  1377 
       
  1378 		case 0x18: // Relative probability of appearing
       
  1379 			FOR_EACH_OBJECT housespec[i]->probability = grf_load_byte(&buf);
       
  1380 			break;
       
  1381 
       
  1382 		case 0x19: // Extra flags
       
  1383 			FOR_EACH_OBJECT {
       
  1384 				byte flags = grf_load_byte(&buf);
       
  1385 				housespec[i]->extra_flags = (HouseExtraFlags)flags;
       
  1386 			}
       
  1387 			break;
       
  1388 
       
  1389 		case 0x1A: // Animation frames
       
  1390 			FOR_EACH_OBJECT housespec[i]->animation_frames = grf_load_byte(&buf);
       
  1391 			break;
       
  1392 
       
  1393 		case 0x1B: // Animation speed
       
  1394 			FOR_EACH_OBJECT housespec[i]->animation_speed = clamp(grf_load_byte(&buf), 2, 16);
       
  1395 			break;
       
  1396 
       
  1397 		case 0x1C: // Class of the building type
       
  1398 			FOR_EACH_OBJECT housespec[i]->class_id = AllocateHouseClassID(grf_load_byte(&buf), _cur_grffile->grfid);
       
  1399 			break;
       
  1400 
       
  1401 		case 0x1D: // Callback flags 2
       
  1402 			FOR_EACH_OBJECT housespec[i]->callback_mask |= (grf_load_byte(&buf) << 8);
       
  1403 			break;
       
  1404 
       
  1405 		case 0x1E: // Accepted cargo types
       
  1406 			FOR_EACH_OBJECT grf_load_dword(&buf);
       
  1407 			ret = true;
       
  1408 			break;
       
  1409 
       
  1410 		default:
       
  1411 			ret = true;
       
  1412 			break;
  1165 	}
  1413 	}
  1166 
  1414 
  1167 	*bufp = buf;
  1415 	*bufp = buf;
  1168 	return ret;
  1416 	return ret;
  1169 }
  1417 }
  1277 				}
  1525 				}
  1278 			}
  1526 			}
  1279 			break;
  1527 			break;
  1280 
  1528 
  1281 		case 0x10: // 12 * 32 * B Snow line height table
  1529 		case 0x10: // 12 * 32 * B Snow line height table
       
  1530 			if (numinfo > 1 || IsSnowLineSet()) {
       
  1531 				grfmsg(1, "GlobalVarChangeInfo: The snowline can only be set once (%d)", numinfo);
       
  1532 			} else if (len < SNOW_LINE_MONTHS * SNOW_LINE_DAYS) {
       
  1533 				grfmsg(1, "GlobalVarChangeInfo: Not enough entries set in the snowline table (%d)", len);
       
  1534 			} else {
       
  1535 				byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS];
       
  1536 
       
  1537 				for (uint i = 0; i < SNOW_LINE_MONTHS; i++) {
       
  1538 					for (uint j = 0; j < SNOW_LINE_DAYS; j++) {
       
  1539 						table[i][j] = grf_load_byte(&buf);
       
  1540 					}
       
  1541 				}
       
  1542 				SetSnowLine(table);
       
  1543 			}
       
  1544 			break;
       
  1545 
  1282 		default:
  1546 		default:
  1283 			ret = true;
  1547 			ret = true;
  1284 	}
  1548 	}
  1285 
  1549 
  1286 	*bufp = buf;
  1550 	*bufp = buf;
  1287 	return ret;
  1551 	return ret;
  1288 }
  1552 }
  1289 
  1553 
  1290 static bool SoundEffectChangeInfo(uint sid, int numinfo, int prop, byte **bufp, int len)
  1554 static bool CargoChangeInfo(uint cid, int numinfo, int prop, byte **bufp, int len)
  1291 {
  1555 {
       
  1556 	if (cid + numinfo > NUM_CARGO) {
       
  1557 		grfmsg(2, "CargoChangeInfo: Cargo type %d out of range (max %d)", cid + numinfo, NUM_CARGO - 1);
       
  1558 		return false;
       
  1559 	}
       
  1560 
       
  1561 	CargoSpec *cs = &_cargo[cid];
  1292 	byte *buf = *bufp;
  1562 	byte *buf = *bufp;
  1293 	int i;
  1563 	int i;
  1294 	bool ret = false;
  1564 	bool ret = false;
  1295 
  1565 
       
  1566 	switch (prop) {
       
  1567 		case 0x08: /* Bit number of cargo */
       
  1568 			FOR_EACH_OBJECT {
       
  1569 				cs[i].bitnum = grf_load_byte(&buf);
       
  1570 				cs[i].grfid = _cur_grffile->grfid;
       
  1571 				if (cs->IsValid()) {
       
  1572 					SETBIT(_cargo_mask, cid + i);
       
  1573 				} else {
       
  1574 					CLRBIT(_cargo_mask, cid + i);
       
  1575 				}
       
  1576 			}
       
  1577 			break;
       
  1578 
       
  1579 		case 0x09: /* String ID for cargo type name */
       
  1580 			FOR_EACH_OBJECT cs[i].name = grf_load_word(&buf);
       
  1581 			break;
       
  1582 
       
  1583 		case 0x0A: /* String for cargo name, plural */
       
  1584 			FOR_EACH_OBJECT cs[i].name_plural = grf_load_word(&buf);
       
  1585 			break;
       
  1586 
       
  1587 		case 0x0B:
       
  1588 			/* String for units of cargo. This is different in OpenTTD to TTDPatch
       
  1589 			 * (e.g. 10 tonnes of coal) */
       
  1590 			FOR_EACH_OBJECT cs[i].units_volume = grf_load_word(&buf);
       
  1591 			break;
       
  1592 
       
  1593 		case 0x0C: /* String for quantity of cargo (e.g. 10 tonnes of coal) */
       
  1594 			FOR_EACH_OBJECT cs[i].quantifier = grf_load_word(&buf);
       
  1595 			break;
       
  1596 
       
  1597 		case 0x0D: /* String for two letter cargo abbreviation */
       
  1598 			FOR_EACH_OBJECT cs[i].abbrev = grf_load_word(&buf);
       
  1599 			break;
       
  1600 
       
  1601 		case 0x0E: /* Sprite ID for cargo icon */
       
  1602 			FOR_EACH_OBJECT cs[i].sprite = grf_load_word(&buf);
       
  1603 			break;
       
  1604 
       
  1605 		case 0x0F: /* Weight of one unit of cargo */
       
  1606 			FOR_EACH_OBJECT cs[i].weight = grf_load_byte(&buf);
       
  1607 			break;
       
  1608 
       
  1609 		case 0x10: /* Used for payment calculation */
       
  1610 			FOR_EACH_OBJECT cs[i].transit_days[0] = grf_load_byte(&buf);
       
  1611 			break;
       
  1612 
       
  1613 		case 0x11: /* Used for payment calculation */
       
  1614 			FOR_EACH_OBJECT cs[i].transit_days[1] = grf_load_byte(&buf);
       
  1615 			break;
       
  1616 
       
  1617 		case 0x12: /* Base cargo price */
       
  1618 			FOR_EACH_OBJECT cs[i].initial_payment = grf_load_dword(&buf);
       
  1619 			break;
       
  1620 
       
  1621 		case 0x13: /* Colour for station rating bars */
       
  1622 			FOR_EACH_OBJECT cs[i].rating_colour = MapDOSColour(grf_load_byte(&buf));
       
  1623 			break;
       
  1624 
       
  1625 		case 0x14: /* Colour for cargo graph */
       
  1626 			FOR_EACH_OBJECT cs[i].legend_colour = MapDOSColour(grf_load_byte(&buf));
       
  1627 			break;
       
  1628 
       
  1629 		case 0x15: /* Freight status */
       
  1630 			FOR_EACH_OBJECT cs[i].is_freight = grf_load_byte(&buf) != 0;
       
  1631 			break;
       
  1632 
       
  1633 		case 0x16: /* Cargo classes */
       
  1634 			FOR_EACH_OBJECT cs[i].classes = grf_load_word(&buf);
       
  1635 			break;
       
  1636 
       
  1637 		case 0x17: /* Cargo label */
       
  1638 			FOR_EACH_OBJECT {
       
  1639 				cs[i].label = grf_load_dword(&buf);
       
  1640 				cs[i].label = BSWAP32(cs[i].label);
       
  1641 			}
       
  1642 			break;
       
  1643 
       
  1644 		case 0x18: /* Town growth substitute type */
       
  1645 			FOR_EACH_OBJECT {
       
  1646 				uint8 substitute_type = grf_load_byte(&buf);
       
  1647 				switch (substitute_type) {
       
  1648 					case 0x00: cs[i].town_effect = TE_PASSENGERS; break;
       
  1649 					case 0x02: cs[i].town_effect = TE_MAIL; break;
       
  1650 					case 0x05: cs[i].town_effect = TE_GOODS; break;
       
  1651 					case 0x09: cs[i].town_effect = TE_WATER; break;
       
  1652 					case 0x0B: cs[i].town_effect = TE_FOOD; break;
       
  1653 					default:
       
  1654 						grfmsg(1, "CargoChangeInfo: Unknown town growth substitute value %d, setting to none.", substitute_type);
       
  1655 					case 0xFF: cs[i].town_effect = TE_NONE; break;
       
  1656 				}
       
  1657 			}
       
  1658 			break;
       
  1659 
       
  1660 		case 0x19: /* Town growth coefficient */
       
  1661 			FOR_EACH_OBJECT cs[i].multipliertowngrowth = grf_load_word(&buf);
       
  1662 			break;
       
  1663 
       
  1664 		case 0x1A: /* Bitmask of callbacks to use */
       
  1665 			FOR_EACH_OBJECT cs[i].callback_mask = grf_load_byte(&buf);
       
  1666 			break;
       
  1667 
       
  1668 		default:
       
  1669 			ret = true;
       
  1670 	}
       
  1671 
       
  1672 	*bufp = buf;
       
  1673 	return ret;
       
  1674 }
       
  1675 
       
  1676 
       
  1677 static bool SoundEffectChangeInfo(uint sid, int numinfo, int prop, byte **bufp, int len)
       
  1678 {
       
  1679 	byte *buf = *bufp;
       
  1680 	int i;
       
  1681 	bool ret = false;
       
  1682 
  1296 	if (_cur_grffile->sound_offset == 0) {
  1683 	if (_cur_grffile->sound_offset == 0) {
  1297 		grfmsg(1, "SoundEffectChangeInfo: No effects defined, skipping");
  1684 		grfmsg(1, "SoundEffectChangeInfo: No effects defined, skipping");
  1298 		return false;
  1685 		return false;
  1299 	}
  1686 	}
  1300 
  1687 
  1301 	switch (prop) {
  1688 	switch (prop) {
  1302 		case 0x08: /* Relative volume */
  1689 		case 0x08: // Relative volume
  1303 			FOR_EACH_OBJECT {
  1690 			FOR_EACH_OBJECT {
  1304 				uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
  1691 				uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
  1305 
  1692 
  1306 				if (sound >= GetNumSounds()) {
  1693 				if (sound >= GetNumSounds()) {
  1307 					grfmsg(1, "SoundEffectChangeInfo: Sound %d not defined (max %d)", sound, GetNumSounds());
  1694 					grfmsg(1, "SoundEffectChangeInfo: Sound %d not defined (max %d)", sound, GetNumSounds());
  1309 					GetSound(sound)->volume = grf_load_byte(&buf);
  1696 					GetSound(sound)->volume = grf_load_byte(&buf);
  1310 				}
  1697 				}
  1311 			}
  1698 			}
  1312 			break;
  1699 			break;
  1313 
  1700 
  1314 		case 0x09: /* Priority */
  1701 		case 0x09: // Priority
  1315 			FOR_EACH_OBJECT {
  1702 			FOR_EACH_OBJECT {
  1316 				uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
  1703 				uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
  1317 
  1704 
  1318 				if (sound >= GetNumSounds()) {
  1705 				if (sound >= GetNumSounds()) {
  1319 					grfmsg(1, "SoundEffectChangeInfo: Sound %d not defined (max %d)", sound, GetNumSounds());
  1706 					grfmsg(1, "SoundEffectChangeInfo: Sound %d not defined (max %d)", sound, GetNumSounds());
  1321 					GetSound(sound)->priority = grf_load_byte(&buf);
  1708 					GetSound(sound)->priority = grf_load_byte(&buf);
  1322 				}
  1709 				}
  1323 			}
  1710 			}
  1324 			break;
  1711 			break;
  1325 
  1712 
  1326 		case 0x0A: /* Override old sound */
  1713 		case 0x0A: // Override old sound
  1327 			FOR_EACH_OBJECT {
  1714 			FOR_EACH_OBJECT {
  1328 				uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
  1715 				uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
  1329 				uint orig_sound = grf_load_byte(&buf);
  1716 				uint orig_sound = grf_load_byte(&buf);
  1330 
  1717 
  1331 				if (sound >= GetNumSounds() || orig_sound >= GetNumSounds()) {
  1718 				if (sound >= GetNumSounds() || orig_sound >= GetNumSounds()) {
  1350 
  1737 
  1351 /* Action 0x00 */
  1738 /* Action 0x00 */
  1352 static void FeatureChangeInfo(byte *buf, int len)
  1739 static void FeatureChangeInfo(byte *buf, int len)
  1353 {
  1740 {
  1354 	byte *bufend = buf + len;
  1741 	byte *bufend = buf + len;
  1355 	int i;
  1742 	uint i;
  1356 
  1743 
  1357 	/* <00> <feature> <num-props> <num-info> <id> (<property <new-info>)...
  1744 	/* <00> <feature> <num-props> <num-info> <id> (<property <new-info>)...
  1358 	 *
  1745 	 *
  1359 	 * B feature       0, 1, 2 or 3 for trains, road vehicles, ships or planes
  1746 	 * B feature       0, 1, 2 or 3 for trains, road vehicles, ships or planes
  1360 	 *                 4 for defining new train station sets
  1747 	 *                 4 for defining new train station sets
  1373 		/* GSF_SHIP */         ShipVehicleChangeInfo,
  1760 		/* GSF_SHIP */         ShipVehicleChangeInfo,
  1374 		/* GSF_AIRCRAFT */     AircraftVehicleChangeInfo,
  1761 		/* GSF_AIRCRAFT */     AircraftVehicleChangeInfo,
  1375 		/* GSF_STATION */      StationChangeInfo,
  1762 		/* GSF_STATION */      StationChangeInfo,
  1376 		/* GSF_CANAL */        NULL,
  1763 		/* GSF_CANAL */        NULL,
  1377 		/* GSF_BRIDGE */       BridgeChangeInfo,
  1764 		/* GSF_BRIDGE */       BridgeChangeInfo,
  1378 		/* GSF_TOWNHOUSE */    NULL,
  1765 		/* GSF_TOWNHOUSE */    TownHouseChangeInfo,
  1379 		/* GSF_GLOBALVAR */    GlobalVarChangeInfo,
  1766 		/* GSF_GLOBALVAR */    GlobalVarChangeInfo,
  1380 		/* GSF_INDUSTRYTILES */NULL,
  1767 		/* GSF_INDUSTRYTILES */NULL,
  1381 		/* GSF_INDUSTRIES */   NULL,
  1768 		/* GSF_INDUSTRIES */   NULL,
  1382 		/* GSF_CARGOS */       NULL,
  1769 		/* GSF_CARGOS */       NULL, /* Cargo is handled during reservation */
  1383 		/* GSF_SOUNDFX */      SoundEffectChangeInfo,
  1770 		/* GSF_SOUNDFX */      SoundEffectChangeInfo,
  1384 	};
  1771 	};
  1385 
  1772 
  1386 	EngineInfo *ei = NULL;
  1773 	EngineInfo *ei = NULL;
  1387 
  1774 
  1392 
  1779 
  1393 	if (!check_length(len, 6, "FeatureChangeInfo")) return;
  1780 	if (!check_length(len, 6, "FeatureChangeInfo")) return;
  1394 	buf++;
  1781 	buf++;
  1395 	uint8 feature  = grf_load_byte(&buf);
  1782 	uint8 feature  = grf_load_byte(&buf);
  1396 	uint8 numprops = grf_load_byte(&buf);
  1783 	uint8 numprops = grf_load_byte(&buf);
  1397 	uint8 numinfo  = grf_load_byte(&buf);
  1784 	uint numinfo  = grf_load_byte(&buf);
  1398 	uint8 engine   = grf_load_byte(&buf);
  1785 	uint engine   = grf_load_byte(&buf);
  1399 
  1786 
  1400 	grfmsg(6, "FeatureChangeInfo: feature %d, %d properties, to apply to %d+%d",
  1787 	grfmsg(6, "FeatureChangeInfo: feature %d, %d properties, to apply to %d+%d",
  1401 	               feature, numprops, engine, numinfo);
  1788 	               feature, numprops, engine, numinfo);
  1402 
  1789 
  1403 	if (feature >= lengthof(handler) || handler[feature] == NULL) {
  1790 	if (feature >= lengthof(handler) || handler[feature] == NULL) {
  1422 			case GSF_ROAD:
  1809 			case GSF_ROAD:
  1423 			case GSF_SHIP:
  1810 			case GSF_SHIP:
  1424 			case GSF_AIRCRAFT:
  1811 			case GSF_AIRCRAFT:
  1425 				/* Common properties for vehicles */
  1812 				/* Common properties for vehicles */
  1426 				switch (prop) {
  1813 				switch (prop) {
  1427 					case 0x00: /* Introduction date */
  1814 					case 0x00: // Introduction date
  1428 						FOR_EACH_OBJECT ei[i].base_intro = grf_load_word(&buf) + DAYS_TILL_ORIGINAL_BASE_YEAR;
  1815 						FOR_EACH_OBJECT ei[i].base_intro = grf_load_word(&buf) + DAYS_TILL_ORIGINAL_BASE_YEAR;
  1429 						break;
  1816 						break;
  1430 
  1817 
  1431 					case 0x02: /* Decay speed */
  1818 					case 0x02: // Decay speed
  1432 						FOR_EACH_OBJECT SB(ei[i].unk2, 0, 7, grf_load_byte(&buf) & 0x7F);
  1819 						FOR_EACH_OBJECT SB(ei[i].unk2, 0, 7, grf_load_byte(&buf) & 0x7F);
  1433 						break;
  1820 						break;
  1434 
  1821 
  1435 					case 0x03: /* Vehicle life */
  1822 					case 0x03: // Vehicle life
  1436 						FOR_EACH_OBJECT ei[i].lifelength = grf_load_byte(&buf);
  1823 						FOR_EACH_OBJECT ei[i].lifelength = grf_load_byte(&buf);
  1437 						break;
  1824 						break;
  1438 
  1825 
  1439 					case 0x04: /* Model life */
  1826 					case 0x04: // Model life
  1440 						FOR_EACH_OBJECT ei[i].base_life = grf_load_byte(&buf);
  1827 						FOR_EACH_OBJECT ei[i].base_life = grf_load_byte(&buf);
  1441 						break;
  1828 						break;
  1442 
  1829 
  1443 					case 0x06: /* Climates available */
  1830 					case 0x06: // Climates available
  1444 						FOR_EACH_OBJECT ei[i].climates = grf_load_byte(&buf);
  1831 						FOR_EACH_OBJECT ei[i].climates = grf_load_byte(&buf);
  1445 						break;
  1832 						break;
  1446 
  1833 
  1447 					case 0x07: /* Loading speed */
  1834 					case 0x07: // Loading speed
  1448 						/* Hyronymus explained me what does
  1835 						/* Hyronymus explained me what does
  1449 						 * this mean and insists on having a
  1836 						 * this mean and insists on having a
  1450 						 * credit ;-). --pasky */
  1837 						 * credit ;-). --pasky */
  1451 						FOR_EACH_OBJECT ei[i].load_amount = grf_load_byte(&buf);
  1838 						FOR_EACH_OBJECT ei[i].load_amount = grf_load_byte(&buf);
  1452 						break;
  1839 						break;
  1519 		uint8 prop = grf_load_byte(&buf);
  1906 		uint8 prop = grf_load_byte(&buf);
  1520 
  1907 
  1521 		switch (feature) {
  1908 		switch (feature) {
  1522 			case GSF_GLOBALVAR:
  1909 			case GSF_GLOBALVAR:
  1523 				switch (prop) {
  1910 				switch (prop) {
  1524 					case 0x09: /* Cargo Translation Table */
  1911 					case 0x09: // Cargo Translation Table
  1525 						if (index != 0) {
  1912 						if (index != 0) {
  1526 							grfmsg(1, "InitChangeInfo: Cargo translation table must start at zero");
  1913 							grfmsg(1, "InitChangeInfo: Cargo translation table must start at zero");
  1527 							return;
  1914 							return;
  1528 						}
  1915 						}
  1529 
  1916 
  1541 				break;
  1928 				break;
  1542 		}
  1929 		}
  1543 	}
  1930 	}
  1544 }
  1931 }
  1545 
  1932 
       
  1933 /* Action 0x00 (GLS_RESERVE) */
       
  1934 static void ReserveChangeInfo(byte *buf, int len)
       
  1935 {
       
  1936 	byte *bufend = buf + len;
       
  1937 
       
  1938 	if (len == 1) {
       
  1939 		grfmsg(8, "Silently ignoring one-byte special sprite 0x00");
       
  1940 		return;
       
  1941 	}
       
  1942 
       
  1943 	if (!check_length(len, 6, "InitChangeInfo")) return;
       
  1944 	buf++;
       
  1945 	uint8 feature  = grf_load_byte(&buf);
       
  1946 
       
  1947 	if (feature != GSF_CARGOS) return;
       
  1948 
       
  1949 	uint8 numprops = grf_load_byte(&buf);
       
  1950 	uint8 numinfo  = grf_load_byte(&buf);
       
  1951 	uint8 index    = grf_load_byte(&buf);
       
  1952 
       
  1953 	while (numprops-- && buf < bufend) {
       
  1954 		uint8 prop = grf_load_byte(&buf);
       
  1955 
       
  1956 		if (CargoChangeInfo(index, numinfo, prop, &buf, bufend - buf)) {
       
  1957 			grfmsg(2, "FeatureChangeInfo: Ignoring property 0x%02X (not implemented)", prop);
       
  1958 		}
       
  1959 	}
       
  1960 }
       
  1961 
  1546 #undef FOR_EACH_OBJECT
  1962 #undef FOR_EACH_OBJECT
  1547 
  1963 
  1548 /**
  1964 /**
  1549  * Creates a spritegroup representing a callback result
  1965  * Creates a spritegroup representing a callback result
  1550  * @param value The value that was used to represent this callback result
  1966  * @param value The value that was used to represent this callback result
  1554 {
  1970 {
  1555 	SpriteGroup *group = AllocateSpriteGroup();
  1971 	SpriteGroup *group = AllocateSpriteGroup();
  1556 
  1972 
  1557 	group->type = SGT_CALLBACK;
  1973 	group->type = SGT_CALLBACK;
  1558 
  1974 
  1559 	// Old style callback results have the highest byte 0xFF so signify it is a callback result
  1975 	/* Old style callback results have the highest byte 0xFF so signify it is a callback result
  1560 	// New style ones only have the highest bit set (allows 15-bit results, instead of just 8)
  1976 	 * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
  1561 	if ((value >> 8) == 0xFF) {
  1977 	if ((value >> 8) == 0xFF) {
  1562 		value &= ~0xFF00;
  1978 		value &= ~0xFF00;
  1563 	} else {
  1979 	} else {
  1564 		value &= ~0x8000;
  1980 		value &= ~0x8000;
  1565 	}
  1981 	}
  1612 
  2028 
  1613 	grfmsg(7, "New sprite set at %d of type %d, consisting of %d sets with %d views each (total %d)",
  2029 	grfmsg(7, "New sprite set at %d of type %d, consisting of %d sets with %d views each (total %d)",
  1614 		_cur_spriteid, feature, num_sets, num_ents, num_sets * num_ents
  2030 		_cur_spriteid, feature, num_sets, num_ents, num_sets * num_ents
  1615 	);
  2031 	);
  1616 
  2032 
  1617 	for (uint i = 0; i < num_sets * num_ents; i++) {
  2033 	for (uint16 i = 0; i < num_sets * num_ents; i++) {
  1618 		LoadNextSprite(_cur_spriteid++, _file_index);
  2034 		LoadNextSprite(_cur_spriteid++, _file_index);
  1619 		_nfo_line++;
  2035 		_nfo_line++;
  1620 	}
  2036 	}
  1621 }
  2037 }
  1622 
  2038 
  1686 	uint8 feature = grf_load_byte(&buf);
  2102 	uint8 feature = grf_load_byte(&buf);
  1687 	uint8 setid   = grf_load_byte(&buf);
  2103 	uint8 setid   = grf_load_byte(&buf);
  1688 	uint8 type    = grf_load_byte(&buf);
  2104 	uint8 type    = grf_load_byte(&buf);
  1689 
  2105 
  1690 	if (setid >= _cur_grffile->spritegroups_count) {
  2106 	if (setid >= _cur_grffile->spritegroups_count) {
  1691 		// Allocate memory for new sprite group references.
  2107 		/* Allocate memory for new sprite group references. */
  1692 		_cur_grffile->spritegroups = ReallocT(_cur_grffile->spritegroups, setid + 1);
  2108 		_cur_grffile->spritegroups = ReallocT(_cur_grffile->spritegroups, setid + 1);
  1693 		// Initialise new space to NULL
  2109 		/* Initialise new space to NULL */
  1694 		for (; _cur_grffile->spritegroups_count < (setid + 1); _cur_grffile->spritegroups_count++)
  2110 		for (; _cur_grffile->spritegroups_count < (setid + 1); _cur_grffile->spritegroups_count++)
  1695 			_cur_grffile->spritegroups[_cur_grffile->spritegroups_count] = NULL;
  2111 			_cur_grffile->spritegroups[_cur_grffile->spritegroups_count] = NULL;
  1696 	}
  2112 	}
  1697 
  2113 
  1698 	switch (type) {
  2114 	switch (type) {
  1813 				case GSF_TRAIN:
  2229 				case GSF_TRAIN:
  1814 				case GSF_ROAD:
  2230 				case GSF_ROAD:
  1815 				case GSF_SHIP:
  2231 				case GSF_SHIP:
  1816 				case GSF_AIRCRAFT:
  2232 				case GSF_AIRCRAFT:
  1817 				case GSF_STATION:
  2233 				case GSF_STATION:
       
  2234 				case GSF_CARGOS:
  1818 				{
  2235 				{
  1819 					byte sprites     = _cur_grffile->spriteset_numents;
  2236 					byte sprites     = _cur_grffile->spriteset_numents;
  1820 					byte num_loaded  = type;
  2237 					byte num_loaded  = type;
  1821 					byte num_loading = grf_load_byte(&buf);
  2238 					byte num_loading = grf_load_byte(&buf);
  1822 
  2239 
  1851 					}
  2268 					}
  1852 
  2269 
  1853 					break;
  2270 					break;
  1854 				}
  2271 				}
  1855 
  2272 
       
  2273 				case GSF_TOWNHOUSE: {
       
  2274 					byte sprites     = _cur_grffile->spriteset_numents;
       
  2275 					byte num_sprites = max((uint8)1, type);
       
  2276 					uint i;
       
  2277 
       
  2278 					group = AllocateSpriteGroup();
       
  2279 					group->type = SGT_TILELAYOUT;
       
  2280 					group->g.layout.num_sprites = sprites;
       
  2281 					group->g.layout.dts = CallocT<DrawTileSprites>(1);
       
  2282 
       
  2283 					/* Groundsprite */
       
  2284 					group->g.layout.dts->ground_sprite = grf_load_word(&buf);
       
  2285 					group->g.layout.dts->ground_pal    = grf_load_word(&buf);
       
  2286 					/* Remap transparent/colour modifier bits */
       
  2287 					if (HASBIT(group->g.layout.dts->ground_sprite, 14)) {
       
  2288 						CLRBIT(group->g.layout.dts->ground_sprite, 14);
       
  2289 						SETBIT(group->g.layout.dts->ground_sprite, PALETTE_MODIFIER_TRANSPARENT);
       
  2290 					}
       
  2291 					if (HASBIT(group->g.layout.dts->ground_sprite, 15)) {
       
  2292 						CLRBIT(group->g.layout.dts->ground_sprite, 15);
       
  2293 						SETBIT(group->g.layout.dts->ground_sprite, PALETTE_MODIFIER_COLOR);
       
  2294 					}
       
  2295 					if (HASBIT(group->g.layout.dts->ground_pal, 14)) {
       
  2296 						CLRBIT(group->g.layout.dts->ground_pal, 14);
       
  2297 						SETBIT(group->g.layout.dts->ground_sprite, SPRITE_MODIFIER_OPAQUE);
       
  2298 					}
       
  2299 					if (HASBIT(group->g.layout.dts->ground_pal, 15)) {
       
  2300 						/* Bit 31 set means this is a custom sprite, so rewrite it to the
       
  2301 						 * last spriteset defined. */
       
  2302 						SpriteID sprite = _cur_grffile->spriteset_start + GB(group->g.layout.dts->ground_sprite, 0, 14) * sprites;
       
  2303 						SB(group->g.layout.dts->ground_sprite, 0, SPRITE_WIDTH, sprite);
       
  2304 						CLRBIT(group->g.layout.dts->ground_pal, 15);
       
  2305 					}
       
  2306 
       
  2307 					group->g.layout.dts->seq = CallocT<DrawTileSeqStruct>(num_sprites + 1);
       
  2308 
       
  2309 					for (i = 0; i < num_sprites; i++) {
       
  2310 						DrawTileSeqStruct *seq = (DrawTileSeqStruct*)&group->g.layout.dts->seq[i];
       
  2311 
       
  2312 						seq->image = grf_load_word(&buf);
       
  2313 						seq->pal   = grf_load_word(&buf);
       
  2314 						seq->delta_x = grf_load_byte(&buf);
       
  2315 						seq->delta_y = grf_load_byte(&buf);
       
  2316 
       
  2317 						if (HASBIT(seq->image, 14)) {
       
  2318 							CLRBIT(seq->image, 14);
       
  2319 							SETBIT(seq->image, PALETTE_MODIFIER_TRANSPARENT);
       
  2320 						}
       
  2321 						if (HASBIT(seq->image, 15)) {
       
  2322 							CLRBIT(seq->image, 15);
       
  2323 							SETBIT(seq->image, PALETTE_MODIFIER_COLOR);
       
  2324 						}
       
  2325 						if (HASBIT(seq->pal, 14)) {
       
  2326 							CLRBIT(seq->pal, 14);
       
  2327 							SETBIT(seq->image, SPRITE_MODIFIER_OPAQUE);
       
  2328 						}
       
  2329 						if (HASBIT(seq->pal, 15)) {
       
  2330 							/* Bit 31 set means this is a custom sprite, so rewrite it to the
       
  2331 							 * last spriteset defined. */
       
  2332 							SpriteID sprite = _cur_grffile->spriteset_start + GB(seq->image, 0, 14) * sprites;
       
  2333 							SB(seq->image, 0, SPRITE_WIDTH, sprite);
       
  2334 							CLRBIT(seq->pal, 15);
       
  2335 						}
       
  2336 
       
  2337 						if (type > 0) {
       
  2338 							seq->delta_z = grf_load_byte(&buf);
       
  2339 							if ((byte)seq->delta_z == 0x80) continue;
       
  2340 						}
       
  2341 
       
  2342 						seq->size_x = grf_load_byte(&buf);
       
  2343 						seq->size_y = grf_load_byte(&buf);
       
  2344 						seq->size_z = grf_load_byte(&buf);
       
  2345 					}
       
  2346 
       
  2347 					/* Set the terminator value. */
       
  2348 					((DrawTileSeqStruct*)group->g.layout.dts->seq)[i].delta_x = (byte)0x80;
       
  2349 
       
  2350 					break;
       
  2351 				}
       
  2352 
  1856 				/* Loading of Tile Layout and Production Callback groups would happen here */
  2353 				/* Loading of Tile Layout and Production Callback groups would happen here */
  1857 				default: grfmsg(1, "NewSpriteGroup: Unsupported feature %d, skipping", feature);
  2354 				default: grfmsg(1, "NewSpriteGroup: Unsupported feature %d, skipping", feature);
  1858 			}
  2355 			}
  1859 		}
  2356 		}
  1860 	}
  2357 	}
  1866 {
  2363 {
  1867 	/* Special cargo types for purchase list and stations */
  2364 	/* Special cargo types for purchase list and stations */
  1868 	if (feature == GSF_STATION && ctype == 0xFE) return CT_DEFAULT_NA;
  2365 	if (feature == GSF_STATION && ctype == 0xFE) return CT_DEFAULT_NA;
  1869 	if (ctype == 0xFF) return CT_PURCHASE;
  2366 	if (ctype == 0xFF) return CT_PURCHASE;
  1870 
  2367 
       
  2368 	if (_cur_grffile->cargo_max == 0) {
       
  2369 		/* No cargo table, so use bitnum values */
       
  2370 		if (ctype >= 32) {
       
  2371 			grfmsg(1, "FeatureMapSpriteGroup: Cargo bitnum %d out of range (max 31), skipping.", ctype);
       
  2372 			return CT_INVALID;
       
  2373 		}
       
  2374 
       
  2375 		for (CargoID c = 0; c < NUM_CARGO; c++) {
       
  2376 			const CargoSpec *cs = GetCargo(c);
       
  2377 			if (!cs->IsValid()) continue;
       
  2378 
       
  2379 			if (cs->bitnum == ctype) {
       
  2380 				grfmsg(6, "FeatureMapSpriteGroup: Cargo bitnum %d mapped to cargo type %d.", ctype, c);
       
  2381 				return c;
       
  2382 			}
       
  2383 		}
       
  2384 
       
  2385 		grfmsg(5, "FeatureMapSpriteGroup: Cargo bitnum %d not available in this climate, skipping.", ctype);
       
  2386 		return CT_INVALID;
       
  2387 	}
       
  2388 
  1871 	/* Check if the cargo type is out of bounds of the cargo translation table */
  2389 	/* Check if the cargo type is out of bounds of the cargo translation table */
  1872 	if (ctype >= (_cur_grffile->cargo_max == 0 ? _default_cargo_max : _cur_grffile->cargo_max)) {
  2390 	if (ctype >= _cur_grffile->cargo_max) {
  1873 		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);
  2391 		grfmsg(1, "FeatureMapSpriteGroup: Cargo type %d out of range (max %d), skipping.", ctype, _cur_grffile->cargo_max - 1);
  1874 		return CT_INVALID;
  2392 		return CT_INVALID;
  1875 	}
  2393 	}
  1876 
  2394 
  1877 	/* Look up the cargo label from the translation table */
  2395 	/* Look up the cargo label from the translation table */
  1878 	CargoLabel cl = _cur_grffile->cargo_max == 0 ? _default_cargo_list[ctype] : _cur_grffile->cargo_list[ctype];
  2396 	CargoLabel cl = _cur_grffile->cargo_list[ctype];
  1879 	if (cl == 0) {
  2397 	if (cl == 0) {
  1880 		grfmsg(5, "FeatureMapSpriteGroup: Cargo type %d not available in this climate, skipping.", ctype);
  2398 		grfmsg(5, "FeatureMapSpriteGroup: Cargo type %d not available in this climate, skipping.", ctype);
  1881 		return CT_INVALID;
  2399 		return CT_INVALID;
  1882 	}
  2400 	}
  1883 
  2401 
  1888 	}
  2406 	}
  1889 
  2407 
  1890 	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);
  2408 	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);
  1891 	return ctype;
  2409 	return ctype;
  1892 }
  2410 }
       
  2411 
       
  2412 
       
  2413 static void VehicleMapSpriteGroup(byte *buf, byte feature, uint8 idcount, uint8 cidcount, bool wagover)
       
  2414 {
       
  2415 	static byte *last_engines;
       
  2416 	static int last_engines_count;
       
  2417 
       
  2418 	if (!wagover) {
       
  2419 		if (last_engines_count != idcount) {
       
  2420 			last_engines = ReallocT(last_engines, idcount);
       
  2421 			last_engines_count = idcount;
       
  2422 		}
       
  2423 	} else {
       
  2424 		if (last_engines_count == 0) {
       
  2425 			grfmsg(0, "FeatureMapSpriteGroup: WagonOverride: No engine to do override with");
       
  2426 			return;
       
  2427 		}
       
  2428 
       
  2429 		grfmsg(6, "FeatureMapSpriteGroup: WagonOverride: %u engines, %u wagons",
       
  2430 				last_engines_count, idcount);
       
  2431 	}
       
  2432 
       
  2433 	for (uint i = 0; i < idcount; i++) {
       
  2434 		uint8 engine_id = buf[3 + i];
       
  2435 		uint8 engine = engine_id + _vehshifts[feature];
       
  2436 		byte *bp = &buf[4 + idcount];
       
  2437 
       
  2438 		if (engine_id > _vehcounts[feature]) {
       
  2439 			grfmsg(0, "Id %u for feature 0x%02X is out of bounds", engine_id, feature);
       
  2440 			return;
       
  2441 		}
       
  2442 
       
  2443 		grfmsg(7, "FeatureMapSpriteGroup: [%d] Engine %d...", i, engine);
       
  2444 
       
  2445 		for (uint c = 0; c < cidcount; c++) {
       
  2446 			uint8 ctype = grf_load_byte(&bp);
       
  2447 			uint16 groupid = grf_load_word(&bp);
       
  2448 
       
  2449 			grfmsg(8, "FeatureMapSpriteGroup: * [%d] Cargo type 0x%X, group id 0x%02X", c, ctype, groupid);
       
  2450 
       
  2451 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
       
  2452 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping", groupid, _cur_grffile->spritegroups_count);
       
  2453 				continue;
       
  2454 			}
       
  2455 
       
  2456 			ctype = TranslateCargo(feature, ctype);
       
  2457 			if (ctype == CT_INVALID) continue;
       
  2458 
       
  2459 			if (wagover) {
       
  2460 				SetWagonOverrideSprites(engine, ctype, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
       
  2461 			} else {
       
  2462 				SetCustomEngineSprites(engine, ctype, _cur_grffile->spritegroups[groupid]);
       
  2463 				last_engines[i] = engine;
       
  2464 			}
       
  2465 		}
       
  2466 	}
       
  2467 
       
  2468 	{
       
  2469 		byte *bp = &buf[4 + idcount + cidcount * 3];
       
  2470 		uint16 groupid = grf_load_word(&bp);
       
  2471 
       
  2472 		grfmsg(8, "-- Default group id 0x%04X", groupid);
       
  2473 
       
  2474 		for (uint i = 0; i < idcount; i++) {
       
  2475 			uint8 engine = buf[3 + i] + _vehshifts[feature];
       
  2476 
       
  2477 			/* Don't tell me you don't love duplicated code! */
       
  2478 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
       
  2479 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
       
  2480 				       groupid, _cur_grffile->spritegroups_count);
       
  2481 				continue;
       
  2482 			}
       
  2483 
       
  2484 			if (wagover) {
       
  2485 				/* If the ID for this action 3 is the same as the vehicle ID,
       
  2486  * this indicates we have a helicopter rotor override. */
       
  2487 				if (feature == GSF_AIRCRAFT && engine == last_engines[i]) {
       
  2488 					SetRotorOverrideSprites(engine, _cur_grffile->spritegroups[groupid]);
       
  2489 				} else {
       
  2490 					/* TODO: No multiple cargo types per vehicle yet. --pasky */
       
  2491 					SetWagonOverrideSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
       
  2492 				}
       
  2493 			} else {
       
  2494 				SetCustomEngineSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid]);
       
  2495 				SetEngineGRF(engine, _cur_grffile);
       
  2496 				last_engines[i] = engine;
       
  2497 			}
       
  2498 		}
       
  2499 	}
       
  2500 }
       
  2501 
       
  2502 
       
  2503 static void StationMapSpriteGroup(byte *buf, uint8 idcount, uint8 cidcount)
       
  2504 {
       
  2505 	for (uint i = 0; i < idcount; i++) {
       
  2506 		uint8 stid = buf[3 + i];
       
  2507 		StationSpec *statspec = _cur_grffile->stations[stid];
       
  2508 		byte *bp = &buf[4 + idcount];
       
  2509 
       
  2510 		for (uint c = 0; c < cidcount; c++) {
       
  2511 			uint8 ctype = grf_load_byte(&bp);
       
  2512 			uint16 groupid = grf_load_word(&bp);
       
  2513 
       
  2514 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
       
  2515 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
       
  2516 				       groupid, _cur_grffile->spritegroups_count);
       
  2517 				continue;
       
  2518 			}
       
  2519 
       
  2520 			ctype = TranslateCargo(GSF_STATION, ctype);
       
  2521 			if (ctype == CT_INVALID) continue;
       
  2522 
       
  2523 			statspec->spritegroup[ctype] = _cur_grffile->spritegroups[groupid];
       
  2524 		}
       
  2525 	}
       
  2526 
       
  2527 	{
       
  2528 		byte *bp = &buf[4 + idcount + cidcount * 3];
       
  2529 		uint16 groupid = grf_load_word(&bp);
       
  2530 
       
  2531 		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 			       groupid, _cur_grffile->spritegroups_count);
       
  2534 			return;
       
  2535 		}
       
  2536 
       
  2537 		for (uint i = 0; i < idcount; i++) {
       
  2538 			uint8 stid = buf[3 + i];
       
  2539 			StationSpec *statspec = _cur_grffile->stations[stid];
       
  2540 
       
  2541 			statspec->spritegroup[CT_DEFAULT] = _cur_grffile->spritegroups[groupid];
       
  2542 			statspec->grfid = _cur_grffile->grfid;
       
  2543 			statspec->localidx = stid;
       
  2544 			SetCustomStationSpec(statspec);
       
  2545 		}
       
  2546 	}
       
  2547 }
       
  2548 
       
  2549 
       
  2550 static void TownHouseMapSpriteGroup(byte *buf, uint8 idcount, uint8 cidcount)
       
  2551 {
       
  2552 	byte *bp = &buf[4 + idcount + cidcount * 3];
       
  2553 	uint16 groupid = grf_load_word(&bp);
       
  2554 
       
  2555 	if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
       
  2556 		grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping.",
       
  2557 		       groupid, _cur_grffile->spritegroups_count);
       
  2558 		return;
       
  2559 	}
       
  2560 
       
  2561 	for (uint i = 0; i < idcount; i++) {
       
  2562 		uint8 hid = buf[3 + i];
       
  2563 		HouseSpec *hs = _cur_grffile->housespec[hid];
       
  2564 
       
  2565 		if (hs == NULL) {
       
  2566 			grfmsg(1, "FeatureMapSpriteGroup: Too many houses defined, skipping");
       
  2567 			return;
       
  2568 		}
       
  2569 
       
  2570 		hs->spritegroup = _cur_grffile->spritegroups[groupid];
       
  2571 	}
       
  2572 }
       
  2573 
       
  2574 
       
  2575 static void CargoMapSpriteGroup(byte *buf, uint8 idcount, uint8 cidcount)
       
  2576 {
       
  2577 	byte *bp = &buf[4 + idcount + cidcount * 3];
       
  2578 	uint16 groupid = grf_load_word(&bp);
       
  2579 
       
  2580 	if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
       
  2581 		grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping.",
       
  2582 		       groupid, _cur_grffile->spritegroups_count);
       
  2583 		return;
       
  2584 	}
       
  2585 
       
  2586 	for (uint i = 0; i < idcount; i++) {
       
  2587 		CargoID cid = buf[3 + i];
       
  2588 
       
  2589 		if (cid >= NUM_CARGO) {
       
  2590 			grfmsg(1, "FeatureMapSpriteGroup: Cargo ID %d out of range, skipping");
       
  2591 			continue;
       
  2592 		}
       
  2593 
       
  2594 		CargoSpec *cs = &_cargo[cid];
       
  2595 		cs->grfid = _cur_grffile->grfid;
       
  2596 		cs->group = _cur_grffile->spritegroups[groupid];
       
  2597 	}
       
  2598 }
       
  2599 
  1893 
  2600 
  1894 /* Action 0x03 */
  2601 /* Action 0x03 */
  1895 static void FeatureMapSpriteGroup(byte *buf, int len)
  2602 static void FeatureMapSpriteGroup(byte *buf, int len)
  1896 {
  2603 {
  1897 	/* <03> <feature> <n-id> <ids>... <num-cid> [<cargo-type> <cid>]... <def-cid>
  2604 	/* <03> <feature> <n-id> <ids>... <num-cid> [<cargo-type> <cid>]... <def-cid>
  1905 	 * B num-cid       number of cargo IDs (sprite group IDs) in this definition
  2612 	 * B num-cid       number of cargo IDs (sprite group IDs) in this definition
  1906 	 *                 can be zero, in that case the def-cid is used always
  2613 	 *                 can be zero, in that case the def-cid is used always
  1907 	 * B cargo-type    type of this cargo type (e.g. mail=2, wood=7, see below)
  2614 	 * B cargo-type    type of this cargo type (e.g. mail=2, wood=7, see below)
  1908 	 * W cid           cargo ID (sprite group ID) for this type of cargo
  2615 	 * W cid           cargo ID (sprite group ID) for this type of cargo
  1909 	 * W def-cid       default cargo ID (sprite group ID) */
  2616 	 * W def-cid       default cargo ID (sprite group ID) */
  1910 	/* TODO: Bridges, town houses. */
       
  1911 	/* TODO: Multiple cargo support could be useful even for trains/cars -
       
  1912 	 * cargo id 0xff is used for showing images in the build train list. */
       
  1913 
       
  1914 	static byte *last_engines;
       
  1915 	static int last_engines_count;
       
  1916 
  2617 
  1917 	if (!check_length(len, 6, "FeatureMapSpriteGroup")) return;
  2618 	if (!check_length(len, 6, "FeatureMapSpriteGroup")) return;
  1918 
  2619 
  1919 	uint8 feature = buf[1];
  2620 	uint8 feature = buf[1];
  1920 	uint8 idcount = buf[2] & 0x7F;
  2621 	uint8 idcount = buf[2] & 0x7F;
  1932 	if (!check_length(len, 4 + idcount + cidcount * 3, "FeatureMapSpriteGroup")) return;
  2633 	if (!check_length(len, 4 + idcount + cidcount * 3, "FeatureMapSpriteGroup")) return;
  1933 
  2634 
  1934 	grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids, %d cids, wagon override %d",
  2635 	grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids, %d cids, wagon override %d",
  1935 			feature, idcount, cidcount, wagover);
  2636 			feature, idcount, cidcount, wagover);
  1936 
  2637 
  1937 	if (feature > GSF_STATION) {
       
  1938 		grfmsg(1, "FeatureMapSpriteGroup: Unsupported feature %d, skipping", feature);
       
  1939 		return;
       
  1940 	}
       
  1941 
       
  1942 
       
  1943 	if (feature == GSF_STATION) {
       
  1944 		// We do things differently for stations.
       
  1945 
       
  1946 		for (uint i = 0; i < idcount; i++) {
       
  1947 			uint8 stid = buf[3 + i];
       
  1948 			StationSpec *statspec = _cur_grffile->stations[stid];
       
  1949 			byte *bp = &buf[4 + idcount];
       
  1950 
       
  1951 			for (uint c = 0; c < cidcount; c++) {
       
  1952 				uint8 ctype = grf_load_byte(&bp);
       
  1953 				uint16 groupid = grf_load_word(&bp);
       
  1954 
       
  1955 				if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
       
  1956 					grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
       
  1957 					       groupid, _cur_grffile->spritegroups_count);
       
  1958 					return;
       
  1959 				}
       
  1960 
       
  1961 				ctype = TranslateCargo(feature, ctype);
       
  1962 				if (ctype == CT_INVALID) continue;
       
  1963 
       
  1964 				statspec->spritegroup[ctype] = _cur_grffile->spritegroups[groupid];
       
  1965 			}
       
  1966 		}
       
  1967 
       
  1968 		{
       
  1969 			byte *bp = buf + 4 + idcount + cidcount * 3;
       
  1970 			uint16 groupid = grf_load_word(&bp);
       
  1971 
       
  1972 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
       
  1973 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
       
  1974 				       groupid, _cur_grffile->spritegroups_count);
       
  1975 				return;
       
  1976 			}
       
  1977 
       
  1978 			for (uint i = 0; i < idcount; i++) {
       
  1979 				uint8 stid = buf[3 + i];
       
  1980 				StationSpec *statspec = _cur_grffile->stations[stid];
       
  1981 
       
  1982 				statspec->spritegroup[CT_DEFAULT] = _cur_grffile->spritegroups[groupid];
       
  1983 				statspec->grfid = _cur_grffile->grfid;
       
  1984 				statspec->localidx = stid;
       
  1985 				SetCustomStationSpec(statspec);
       
  1986 			}
       
  1987 		}
       
  1988 		return;
       
  1989 	}
       
  1990 
       
  1991 	// FIXME: Tropicset contains things like:
       
  1992 	// 03 00 01 19 01 00 00 00 00 - this is missing one 00 at the end,
       
  1993 	// what should we exactly do with that? --pasky
       
  1994 
       
  1995 	if (_cur_grffile->spriteset_start == 0 || _cur_grffile->spritegroups == 0) {
  2638 	if (_cur_grffile->spriteset_start == 0 || _cur_grffile->spritegroups == 0) {
  1996 		grfmsg(1, "FeatureMapSpriteGroup: No sprite set to work on! Skipping");
  2639 		grfmsg(1, "FeatureMapSpriteGroup: No sprite set to work on! Skipping");
  1997 		return;
  2640 		return;
  1998 	}
  2641 	}
  1999 
  2642 
  2000 	if (!wagover && last_engines_count != idcount) {
  2643 	switch (feature) {
  2001 		last_engines = ReallocT(last_engines, idcount);
  2644 		case GSF_TRAIN:
  2002 		last_engines_count = idcount;
  2645 		case GSF_ROAD:
  2003 	}
  2646 		case GSF_SHIP:
  2004 
  2647 		case GSF_AIRCRAFT:
  2005 	if (wagover) {
  2648 			VehicleMapSpriteGroup(buf, feature, idcount, cidcount, wagover);
  2006 		if (last_engines_count == 0) {
       
  2007 			grfmsg(0, "FeatureMapSpriteGroup: WagonOverride: No engine to do override with");
       
  2008 			return;
  2649 			return;
  2009 		}
  2650 
  2010 		grfmsg(6, "FeatureMapSpriteGroup: WagonOverride: %u engines, %u wagons",
  2651 		case GSF_STATION:
  2011 				last_engines_count, idcount);
  2652 			StationMapSpriteGroup(buf, idcount, cidcount);
  2012 	}
       
  2013 
       
  2014 
       
  2015 	for (uint i = 0; i < idcount; i++) {
       
  2016 		uint8 engine_id = buf[3 + i];
       
  2017 		uint8 engine = engine_id + _vehshifts[feature];
       
  2018 		byte *bp = &buf[4 + idcount];
       
  2019 
       
  2020 		if (engine_id > _vehcounts[feature]) {
       
  2021 			grfmsg(0, "Id %u for feature 0x%02X is out of bounds", engine_id, feature);
       
  2022 			return;
  2653 			return;
  2023 		}
  2654 
  2024 
  2655 		case GSF_TOWNHOUSE:
  2025 		grfmsg(7, "FeatureMapSpriteGroup: [%d] Engine %d...", i, engine);
  2656 			TownHouseMapSpriteGroup(buf, idcount, cidcount);
  2026 
  2657 			return;
  2027 		for (uint c = 0; c < cidcount; c++) {
  2658 
  2028 			uint8 ctype = grf_load_byte(&bp);
  2659 		case GSF_CARGOS:
  2029 			uint16 groupid = grf_load_word(&bp);
  2660 			CargoMapSpriteGroup(buf, idcount, cidcount);
  2030 
  2661 			return;
  2031 			grfmsg(8, "FeatureMapSpriteGroup: * [%d] Cargo type 0x%X, group id 0x%02X", c, ctype, groupid);
  2662 
  2032 
  2663 		default:
  2033 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2664 			grfmsg(1, "FeatureMapSpriteGroup: Unsupported feature %d, skipping", feature);
  2034 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping", groupid, _cur_grffile->spritegroups_count);
  2665 			return;
  2035 				return;
       
  2036 			}
       
  2037 
       
  2038 			ctype = TranslateCargo(feature, ctype);
       
  2039 			if (ctype == CT_INVALID) continue;
       
  2040 
       
  2041 			if (wagover) {
       
  2042 				SetWagonOverrideSprites(engine, ctype, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
       
  2043 			} else {
       
  2044 				SetCustomEngineSprites(engine, ctype, _cur_grffile->spritegroups[groupid]);
       
  2045 				last_engines[i] = engine;
       
  2046 			}
       
  2047 		}
       
  2048 	}
       
  2049 
       
  2050 	{
       
  2051 		byte *bp = buf + 4 + idcount + cidcount * 3;
       
  2052 		uint16 groupid = grf_load_word(&bp);
       
  2053 
       
  2054 		grfmsg(8, "-- Default group id 0x%04X", groupid);
       
  2055 
       
  2056 		for (uint i = 0; i < idcount; i++) {
       
  2057 			uint8 engine = buf[3 + i] + _vehshifts[feature];
       
  2058 
       
  2059 			// Don't tell me you don't love duplicated code!
       
  2060 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
       
  2061 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping", groupid, _cur_grffile->spritegroups_count);
       
  2062 				return;
       
  2063 			}
       
  2064 
       
  2065 			if (wagover) {
       
  2066 				/* If the ID for this action 3 is the same as the vehicle ID,
       
  2067 				 * this indicates we have a helicopter rotor override. */
       
  2068 				if (feature == GSF_AIRCRAFT && engine == last_engines[i]) {
       
  2069 					SetRotorOverrideSprites(engine, _cur_grffile->spritegroups[groupid]);
       
  2070 				} else {
       
  2071 					// TODO: No multiple cargo types per vehicle yet. --pasky
       
  2072 					SetWagonOverrideSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
       
  2073 				}
       
  2074 			} else {
       
  2075 				SetCustomEngineSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid]);
       
  2076 				SetEngineGRF(engine, _cur_grffile);
       
  2077 				last_engines[i] = engine;
       
  2078 			}
       
  2079 		}
       
  2080 	}
  2666 	}
  2081 }
  2667 }
  2082 
  2668 
  2083 /* Action 0x04 */
  2669 /* Action 0x04 */
  2084 static void FeatureNewName(byte *buf, int len)
  2670 static void FeatureNewName(byte *buf, int len)
  2146 						AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, id);
  2732 						AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, id);
  2147 					}
  2733 					}
  2148 					break;
  2734 					break;
  2149 				}
  2735 				}
  2150 
  2736 
       
  2737 				case GSF_TOWNHOUSE:
  2151 				default:
  2738 				default:
  2152 					switch (GB(id, 8, 8)) {
  2739 					switch (GB(id, 8, 8)) {
  2153 						case 0xC4: /* Station class name */
  2740 						case 0xC4: // Station class name
  2154 							if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
  2741 							if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
  2155 								grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8));
  2742 								grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8));
  2156 							} else {
  2743 							} else {
  2157 								StationClassID sclass = _cur_grffile->stations[GB(id, 0, 8)]->sclass;
  2744 								StationClassID sclass = _cur_grffile->stations[GB(id, 0, 8)]->sclass;
  2158 								SetStationClassName(sclass, AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED));
  2745 								SetStationClassName(sclass, AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED));
  2159 							}
  2746 							}
  2160 							break;
  2747 							break;
  2161 
  2748 
  2162 						case 0xC5: /* Station name */
  2749 						case 0xC5: // Station name
  2163 							if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
  2750 							if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
  2164 								grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8));
  2751 								grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8));
  2165 							} else {
  2752 							} else {
  2166 								_cur_grffile->stations[GB(id, 0, 8)]->name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
  2753 								_cur_grffile->stations[GB(id, 0, 8)]->name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
  2167 							}
  2754 							}
  2168 							break;
  2755 							break;
  2169 
  2756 
  2170 						case 0xC9:
  2757 						case 0xC9: { // House name
       
  2758 							if (_cur_grffile->housespec == NULL || _cur_grffile->housespec[GB(id, 0, 8)] == NULL) {
       
  2759 								grfmsg(1, "FeatureNewName: Attempt to name undefined house 0x%X, ignoring.", GB(id, 0, 8));
       
  2760 							} else {
       
  2761 								_cur_grffile->housespec[GB(id, 0, 8)]->building_name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
       
  2762 							}
       
  2763 							break;
       
  2764 						}
       
  2765 
  2171 						case 0xD0:
  2766 						case 0xD0:
  2172 						case 0xDC:
  2767 						case 0xDC:
  2173 							AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
  2768 							AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
  2174 							break;
  2769 							break;
  2175 
  2770 
  2180 					break;
  2775 					break;
  2181 
  2776 
  2182 #if 0
  2777 #if 0
  2183 				case GSF_CANAL :
  2778 				case GSF_CANAL :
  2184 				case GSF_BRIDGE :
  2779 				case GSF_BRIDGE :
  2185 				case GSF_TOWNHOUSE :
       
  2186 					AddGRFString(_cur_spriteid, id, lang, name);
  2780 					AddGRFString(_cur_spriteid, id, lang, name);
  2187 					switch (GB(id, 8,8)) {
  2781 					switch (GB(id, 8,8)) {
  2188 						case 0xC9: /* House name */
  2782 						case 0xC9: // House name
  2189 						default:
  2783 						default:
  2190 							grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
  2784 							grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
  2191 					}
  2785 					}
  2192 					break;
  2786 					break;
  2193 
  2787 
  2194 				case GSF_INDUSTRIES :
  2788 				case GSF_INDUSTRIES :
  2195 				case 0x48 :   /* for generic strings */
  2789 				case 0x48 :   // for generic strings
  2196 					AddGRFString(_cur_spriteid, id, lang, name);
  2790 					AddGRFString(_cur_spriteid, id, lang, name);
  2197 					break;
  2791 					break;
  2198 				default :
  2792 				default :
  2199 					grfmsg(7, "FeatureNewName: Unsupported feature (0x%02X)", feature);
  2793 					grfmsg(7, "FeatureNewName: Unsupported feature (0x%02X)", feature);
  2200 					break;
  2794 					break;
  2220 	buf++;
  2814 	buf++;
  2221 	uint8 type = grf_load_byte(&buf);
  2815 	uint8 type = grf_load_byte(&buf);
  2222 	uint16 num = grf_load_extended(&buf);
  2816 	uint16 num = grf_load_extended(&buf);
  2223 
  2817 
  2224 	switch (type) {
  2818 	switch (type) {
  2225 		case 0x04: /* Signal graphics */
  2819 		case 0x04: // Signal graphics
  2226 			if (num != 112 && num != 240) {
  2820 			if (num != 112 && num != 240) {
  2227 				grfmsg(1, "GraphicsNews: Signal graphics sprite count must be 112 or 240, skipping");
  2821 				grfmsg(1, "GraphicsNews: Signal graphics sprite count must be 112 or 240, skipping");
  2228 				return;
  2822 				return;
  2229 			}
  2823 			}
  2230 			_signal_base = _cur_spriteid;
  2824 			_signal_base = _cur_spriteid;
  2231 			break;
  2825 			break;
  2232 
  2826 
  2233 		case 0x05: /* Catenary graphics */
  2827 		case 0x05: // Catenary graphics
  2234 			if (num != 48) {
  2828 			if (num != 48) {
  2235 				grfmsg(1, "GraphicsNews: Catenary graphics sprite count must be 48, skipping");
  2829 				grfmsg(1, "GraphicsNews: Catenary graphics sprite count must be 48, skipping");
  2236 				return;
  2830 				return;
  2237 			}
  2831 			}
  2238 			replace = SPR_ELRAIL_BASE + 3;
  2832 			replace = SPR_ELRAIL_BASE + 3;
  2239 			break;
  2833 			break;
  2240 
  2834 
  2241 		case 0x06: /* Foundations */
  2835 		case 0x06: // Foundations
  2242 			if (num != 74) {
  2836 			if (num != 74) {
  2243 				grfmsg(1, "GraphicsNews: Foundation graphics sprite count must be 74, skipping");
  2837 				grfmsg(1, "GraphicsNews: Foundation graphics sprite count must be 74, skipping");
  2244 				return;
  2838 				return;
  2245 			}
  2839 			}
  2246 			replace = SPR_SLOPES_BASE;
  2840 			replace = SPR_SLOPES_BASE;
  2247 			break;
  2841 			break;
  2248 
  2842 
  2249 		case 0x08: /* Canal graphics */
  2843 		case 0x08: // Canal graphics
  2250 			if (num != 65) {
  2844 			if (num != 65) {
  2251 				grfmsg(1, "GraphicsNews: Canal graphics sprite count must be 65, skipping");
  2845 				grfmsg(1, "GraphicsNews: Canal graphics sprite count must be 65, skipping");
  2252 				return;
  2846 				return;
  2253 			}
  2847 			}
  2254 			replace = SPR_CANALS_BASE + 5;
  2848 			replace = SPR_CANALS_BASE + 5;
  2255 			break;
  2849 			break;
  2256 
  2850 
  2257 		case 0x0D: /* Coast graphics */
  2851 		case 0x0D: // Coast graphics
  2258 			if (num != 16) {
  2852 			if (num != 16) {
  2259 				grfmsg(1, "GraphicsNews: Coast graphics sprite count must be 16, skipping");
  2853 				grfmsg(1, "GraphicsNews: Coast graphics sprite count must be 16, skipping");
  2260 				return;
  2854 				return;
  2261 			}
  2855 			}
  2262 			_coast_base = _cur_spriteid;
  2856 			_coast_base = _cur_spriteid;
  2281 }
  2875 }
  2282 
  2876 
  2283 static uint32 GetParamVal(byte param, uint32 *cond_val)
  2877 static uint32 GetParamVal(byte param, uint32 *cond_val)
  2284 {
  2878 {
  2285 	switch (param) {
  2879 	switch (param) {
  2286 		case 0x81: /* current year */
  2880 		case 0x81: // current year
  2287 			return clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
  2881 			return clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
  2288 
  2882 
  2289 		case 0x83: /* current climate, 0=temp, 1=arctic, 2=trop, 3=toyland */
  2883 		case 0x83: // current climate, 0=temp, 1=arctic, 2=trop, 3=toyland
  2290 			return _opt.landscape;
  2884 			return _opt.landscape;
  2291 
  2885 
  2292 		case 0x84: /* GRF loading stage */
  2886 		case 0x84: // GRF loading stage
  2293 			return (_cur_stage > GLS_INIT) | ((_cur_stage == GLS_ACTIVATION) << 9);
  2887 			return (_cur_stage > GLS_INIT) | ((_cur_stage == GLS_ACTIVATION) << 9);
  2294 
  2888 
  2295 		case 0x85: /* TTDPatch flags, only for bit tests */
  2889 		case 0x85: // TTDPatch flags, only for bit tests
  2296 			if (cond_val == NULL) {
  2890 			if (cond_val == NULL) {
  2297 				/* Supported in Action 0x07 and 0x09, not 0x0D */
  2891 				/* Supported in Action 0x07 and 0x09, not 0x0D */
  2298 				return 0;
  2892 				return 0;
  2299 			} else {
  2893 			} else {
  2300 				uint32 param_val = _ttdpatch_flags[*cond_val / 0x20];
  2894 				uint32 param_val = _ttdpatch_flags[*cond_val / 0x20];
  2301 				*cond_val %= 0x20;
  2895 				*cond_val %= 0x20;
  2302 				return param_val;
  2896 				return param_val;
  2303 			}
  2897 			}
  2304 
  2898 
  2305 		case 0x86: /* road traffic side, bit 4 clear=left, set=right */
  2899 		case 0x86: // road traffic side, bit 4 clear=left, set=right
  2306 			return _opt.road_side << 4;
  2900 			return _opt.road_side << 4;
  2307 
  2901 
  2308 		case 0x88: /* GRF ID check */
  2902 		case 0x88: // GRF ID check
  2309 			return 0;
  2903 			return 0;
  2310 
  2904 
  2311 		case 0x8B: { /* TTDPatch version */
  2905 		case 0x8B: { // TTDPatch version
  2312 			uint major    = 2;
  2906 			uint major    = 2;
  2313 			uint minor    = 6;
  2907 			uint minor    = 6;
  2314 			uint revision = 0; // special case: 2.0.1 is 2.0.10
  2908 			uint revision = 0; // special case: 2.0.1 is 2.0.10
  2315 			uint build    = 1168;
  2909 			uint build    = 1168;
  2316 			return (major << 24) | (minor << 20) | (revision << 16) | build;
  2910 			return (major << 24) | (minor << 20) | (revision << 16) | build;
  2317 		}
  2911 		}
  2318 
  2912 
  2319 		case 0x8D: /* TTD Version, 00=DOS, 01=Windows */
  2913 		case 0x8D: // TTD Version, 00=DOS, 01=Windows
  2320 			return !_use_dos_palette;
  2914 			return !_use_dos_palette;
  2321 
  2915 
  2322 		case 0x8E: /* Y-offset for train sprites */
  2916 		case 0x8E: // Y-offset for train sprites
  2323 			return _traininfo_vehicle_pitch;
  2917 			return _traininfo_vehicle_pitch;
  2324 
  2918 
  2325 		case 0x92: /* Game mode */
  2919 		case 0x92: // Game mode
  2326 			return _game_mode;
  2920 			return _game_mode;
  2327 
  2921 
  2328 		case 0x9A: /* Always -1 */
  2922 		case 0x9A: // Always -1
  2329 			return UINT_MAX;
  2923 			return UINT_MAX;
  2330 
  2924 
  2331 		case 0x9D: /* TTD Platform, 00=TTDPatch, 01=OpenTTD */
  2925 		case 0x9D: // TTD Platform, 00=TTDPatch, 01=OpenTTD
  2332 			return 1;
  2926 			return 1;
  2333 
  2927 
  2334 		case 0x9E: /* Miscellaneous GRF features */
  2928 		case 0x9E: // Miscellaneous GRF features
  2335 			return _misc_grf_features;
  2929 			return _misc_grf_features;
  2336 
  2930 
  2337 		default:
  2931 		default:
  2338 			/* GRF Parameter */
  2932 			/* GRF Parameter */
  2339 			if (param < 0x80) return _cur_grffile->param[param];
  2933 			if (param < 0x80) return _cur_grffile->param[param];
  2477 			return;
  3071 			return;
  2478 		}
  3072 		}
  2479 
  3073 
  2480 		switch (condtype) {
  3074 		switch (condtype) {
  2481 			/* Tests 6 to 10 are only for param 0x88, GRFID checks */
  3075 			/* Tests 6 to 10 are only for param 0x88, GRFID checks */
  2482 			case 6: /* Is GRFID active? */
  3076 			case 6: // Is GRFID active?
  2483 				result = c->status == GCS_ACTIVATED;
  3077 				result = c->status == GCS_ACTIVATED;
  2484 				break;
  3078 				break;
  2485 
  3079 
  2486 			case 7: /* Is GRFID non-active? */
  3080 			case 7: // Is GRFID non-active?
  2487 				result = c->status != GCS_ACTIVATED;
  3081 				result = c->status != GCS_ACTIVATED;
  2488 				break;
  3082 				break;
  2489 
  3083 
  2490 			case 8: /* GRFID is not but will be active? */
  3084 			case 8: // GRFID is not but will be active?
  2491 				result = c->status == GCS_INITIALISED;
  3085 				result = c->status == GCS_INITIALISED;
  2492 				break;
  3086 				break;
  2493 
  3087 
  2494 			case 9: /* GRFID is or will be active? */
  3088 			case 9: // GRFID is or will be active?
  2495 				result = c->status == GCS_ACTIVATED || c->status == GCS_INITIALISED;
  3089 				result = c->status == GCS_ACTIVATED || c->status == GCS_INITIALISED;
  2496 				break;
  3090 				break;
  2497 
  3091 
  2498 			case 10: /* GRFID is not nor will be active */
  3092 			case 10: // GRFID is not nor will be active
  2499 				/* This is the only condtype that doesn't get ignored if the GRFID is not found */
  3093 				/* This is the only condtype that doesn't get ignored if the GRFID is not found */
  2500 				result = c == NULL || c->flags == GCS_DISABLED || c->status == GCS_NOT_FOUND;
  3094 				result = c == NULL || c->flags == GCS_DISABLED || c->status == GCS_NOT_FOUND;
  2501 				break;
  3095 				break;
  2502 
  3096 
  2503 			default: grfmsg(1, "Unsupported GRF test %d. Ignoring", condtype); return;
  3097 			default: grfmsg(1, "Unsupported GRF test %d. Ignoring", condtype); return;
  2560 		 * we use -1 to indicate that all further
  3154 		 * we use -1 to indicate that all further
  2561 		 * sprites should be skipped. */
  3155 		 * sprites should be skipped. */
  2562 		_skip_sprites = -1;
  3156 		_skip_sprites = -1;
  2563 
  3157 
  2564 		/* If an action 8 hasn't been encountered yet, disable the grf. */
  3158 		/* If an action 8 hasn't been encountered yet, disable the grf. */
  2565 		if (_cur_grfconfig->status != GCS_ACTIVATED) _cur_grfconfig->status = GCS_DISABLED;
  3159 		if (_cur_stage != GLS_RESERVE && _cur_grfconfig->status != GCS_ACTIVATED) _cur_grfconfig->status = GCS_DISABLED;
  2566 	}
  3160 	}
  2567 }
  3161 }
  2568 
  3162 
  2569 
  3163 
  2570 /* Action 0x08 (GLS_FILESCAN) */
  3164 /* Action 0x08 (GLS_FILESCAN) */
  2627 	 * B num-sets      How many sets of sprites to replace.
  3221 	 * B num-sets      How many sets of sprites to replace.
  2628 	 * Each set:
  3222 	 * Each set:
  2629 	 * B num-sprites   How many sprites are in this set
  3223 	 * B num-sprites   How many sprites are in this set
  2630 	 * W first-sprite  First sprite number to replace */
  3224 	 * W first-sprite  First sprite number to replace */
  2631 
  3225 
  2632 	buf++; /* skip action byte */
  3226 	buf++; // skip action byte
  2633 	uint8 num_sets = grf_load_byte(&buf);
  3227 	uint8 num_sets = grf_load_byte(&buf);
  2634 
  3228 
  2635 	for (uint i = 0; i < num_sets; i++) {
  3229 	for (uint i = 0; i < num_sets; i++) {
  2636 		uint8 num_sprites = grf_load_byte(&buf);
  3230 		uint8 num_sprites = grf_load_byte(&buf);
  2637 		uint16 first_sprite = grf_load_word(&buf);
  3231 		uint16 first_sprite = grf_load_word(&buf);
  2693 	if (!check_length(len, 6, "GRFLoadError")) return;
  3287 	if (!check_length(len, 6, "GRFLoadError")) return;
  2694 
  3288 
  2695 	/* For now we can only show one message per newgrf file. */
  3289 	/* For now we can only show one message per newgrf file. */
  2696 	if (_cur_grfconfig->error != NULL) return;
  3290 	if (_cur_grfconfig->error != NULL) return;
  2697 
  3291 
  2698 	buf++; /* Skip the action byte. */
  3292 	buf++; // Skip the action byte.
  2699 	byte severity   = grf_load_byte(&buf);
  3293 	byte severity   = grf_load_byte(&buf);
  2700 	byte lang       = grf_load_byte(&buf);
  3294 	byte lang       = grf_load_byte(&buf);
  2701 	byte message_id = grf_load_byte(&buf);
  3295 	byte message_id = grf_load_byte(&buf);
  2702 	len -= 4;
  3296 	len -= 4;
  2703 
  3297 
  2863 					uint8  op      = src1;
  3457 					uint8  op      = src1;
  2864 					uint8  feature = GB(data, 8, 8);
  3458 					uint8  feature = GB(data, 8, 8);
  2865 					uint16 count   = GB(data, 16, 16);
  3459 					uint16 count   = GB(data, 16, 16);
  2866 
  3460 
  2867 					switch (feature) {
  3461 					switch (feature) {
  2868 						case 0x00: /* Trains */
  3462 						case 0x00: // Trains
  2869 						case 0x01: /* Road Vehicles */
  3463 						case 0x01: // Road Vehicles
  2870 						case 0x02: /* Ships */
  3464 						case 0x02: // Ships
  2871 						case 0x03: /* Aircraft */
  3465 						case 0x03: // Aircraft
  2872 						{
  3466 						{
  2873 							uint start = 0;
  3467 							uint start = 0;
  2874 							uint size  = 0;
  3468 							uint size  = 0;
  2875 							uint shift = _vehshifts[feature];
  3469 							uint shift = _vehshifts[feature];
  2876 
  3470 
  2917 								src1 = UINT_MAX;
  3511 								src1 = UINT_MAX;
  2918 							}
  3512 							}
  2919 							break;
  3513 							break;
  2920 						}
  3514 						}
  2921 
  3515 
  2922 						case 0x08: /* General sprites */
  3516 						case 0x08: // General sprites
  2923 							switch (op) {
  3517 							switch (op) {
  2924 								case 0:
  3518 								case 0:
  2925 									/* Check if the allocated sprites will fit below the original sprite limit */
  3519 									/* Check if the allocated sprites will fit below the original sprite limit */
  2926 									if (_cur_spriteid + count >= 16384) {
  3520 									if (_cur_spriteid + count >= 16384) {
  2927 										grfmsg(0, "GRM: Unable to allocate %d sprites; try changing NewGRF order", count);
  3521 										grfmsg(0, "GRM: Unable to allocate %d sprites; try changing NewGRF order", count);
  3011 			} else {
  3605 			} else {
  3012 				res = (int32)src1 << src2;
  3606 				res = (int32)src1 << src2;
  3013 			}
  3607 			}
  3014 			break;
  3608 			break;
  3015 
  3609 
  3016 		case 0x07: /* Bitwise AND */
  3610 		case 0x07: // Bitwise AND
  3017 			res = src1 & src2;
  3611 			res = src1 & src2;
  3018 			break;
  3612 			break;
  3019 
  3613 
  3020 		case 0x08: /* Bitwise OR */
  3614 		case 0x08: // Bitwise OR
  3021 			res = src1 | src2;
  3615 			res = src1 | src2;
  3022 			break;
  3616 			break;
  3023 
  3617 
  3024 		case 0x09: /* Unsigned division */
  3618 		case 0x09: // Unsigned division
  3025 			if (src2 == 0) {
  3619 			if (src2 == 0) {
  3026 				res = src1;
  3620 				res = src1;
  3027 			} else {
  3621 			} else {
  3028 				res = src1 / src2;
  3622 				res = src1 / src2;
  3029 			}
  3623 			}
  3030 			break;
  3624 			break;
  3031 
  3625 
  3032 		case 0x0A: /* Signed divison */
  3626 		case 0x0A: // Signed divison
  3033 			if (src2 == 0) {
  3627 			if (src2 == 0) {
  3034 				res = src1;
  3628 				res = src1;
  3035 			} else {
  3629 			} else {
  3036 				res = (int32)src1 / (int32)src2;
  3630 				res = (int32)src1 / (int32)src2;
  3037 			}
  3631 			}
  3038 			break;
  3632 			break;
  3039 
  3633 
  3040 		case 0x0B: /* Unsigned modulo */
  3634 		case 0x0B: // Unsigned modulo
  3041 			if (src2 == 0) {
  3635 			if (src2 == 0) {
  3042 				res = src1;
  3636 				res = src1;
  3043 			} else {
  3637 			} else {
  3044 				res = src1 % src2;
  3638 				res = src1 % src2;
  3045 			}
  3639 			}
  3046 			break;
  3640 			break;
  3047 
  3641 
  3048 		case 0x0C: /* Signed modulo */
  3642 		case 0x0C: // Signed modulo
  3049 			if (src2 == 0) {
  3643 			if (src2 == 0) {
  3050 				res = src1;
  3644 				res = src1;
  3051 			} else {
  3645 			} else {
  3052 				res = (int32)src1 % (int32)src2;
  3646 				res = (int32)src1 % (int32)src2;
  3053 			}
  3647 			}
  3059 	switch (target) {
  3653 	switch (target) {
  3060 		case 0x8E: // Y-Offset for train sprites
  3654 		case 0x8E: // Y-Offset for train sprites
  3061 			_traininfo_vehicle_pitch = res;
  3655 			_traininfo_vehicle_pitch = res;
  3062 			break;
  3656 			break;
  3063 
  3657 
  3064 		// TODO implement
  3658 		/* @todo implement */
  3065 		case 0x8F: // Rail track type cost factors
  3659 		case 0x8F: // Rail track type cost factors
  3066 		case 0x93: // Tile refresh offset to left
  3660 		case 0x93: // Tile refresh offset to left
  3067 		case 0x94: // Tile refresh offset to right
  3661 		case 0x94: // Tile refresh offset to right
  3068 		case 0x95: // Tile refresh offset upwards
  3662 		case 0x95: // Tile refresh offset upwards
  3069 		case 0x96: // Tile refresh offset downwards
  3663 		case 0x96: // Tile refresh offset downwards
  3070 		case 0x97: // Snow line height
  3664 		case 0x97: // Snow line height
  3071 		case 0x99: // Global ID offset
  3665 		case 0x99: // Global ID offset
  3072 			grfmsg(7, "ParamSet: Skipping unimplemented target 0x%02X", target);
  3666 			grfmsg(7, "ParamSet: Skipping unimplemented target 0x%02X", target);
  3073 			break;
  3667 			break;
  3074 
  3668 
  3075 		case 0x9E: /* Miscellaneous GRF features */
  3669 		case 0x9E: // Miscellaneous GRF features
  3076 			_misc_grf_features = res;
  3670 			_misc_grf_features = res;
  3077 			/* Set train list engine width */
  3671 			/* Set train list engine width */
  3078 			_traininfo_vehicle_width = HASBIT(res, 3) ? 32 : 29;
  3672 			_traininfo_vehicle_width = HASBIT(res, 3) ? 32 : 29;
  3079 			break;
  3673 			break;
  3080 
  3674 
  3264 	for (;;) {
  3858 	for (;;) {
  3265 		uint32 tag  = grf_load_dword(&buf);
  3859 		uint32 tag  = grf_load_dword(&buf);
  3266 		uint32 size = grf_load_dword(&buf);
  3860 		uint32 size = grf_load_dword(&buf);
  3267 
  3861 
  3268 		switch (tag) {
  3862 		switch (tag) {
  3269 			case ' tmf': /* 'fmt ' */
  3863 			case ' tmf': // 'fmt '
  3270 				/* Audio format, must be 1 (PCM) */
  3864 				/* Audio format, must be 1 (PCM) */
  3271 				if (grf_load_word(&buf) != 1) {
  3865 				if (grf_load_word(&buf) != 1) {
  3272 					grfmsg(1, "LoadGRFSound: Invalid audio format");
  3866 					grfmsg(1, "LoadGRFSound: Invalid audio format");
  3273 					return;
  3867 					return;
  3274 				}
  3868 				}
  3280 
  3874 
  3281 				/* Consume any extra bytes */
  3875 				/* Consume any extra bytes */
  3282 				for (; size > 16; size--) grf_load_byte(&buf);
  3876 				for (; size > 16; size--) grf_load_byte(&buf);
  3283 				break;
  3877 				break;
  3284 
  3878 
  3285 			case 'atad': /* 'data' */
  3879 			case 'atad': // 'data'
  3286 				se->file_size    = size;
  3880 				se->file_size    = size;
  3287 				se->file_offset  = FioGetPos() - (len - (buf - buf_start)) + 1;
  3881 				se->file_offset  = FioGetPos() - (len - (buf - buf_start)) + 1;
  3288 				se->file_offset |= _file_index << 24;
  3882 				se->file_offset |= _file_index << 24;
  3289 
  3883 
  3290 				/* Set default volume and priority */
  3884 				/* Set default volume and priority */
  3476 	                   |                                        (0 << 0x14)  // manualconvert
  4070 	                   |                                        (0 << 0x14)  // manualconvert
  3477 	                   |       ((_patches.build_on_slopes ? 1 : 0) << 0x15)  // buildoncoasts
  4071 	                   |       ((_patches.build_on_slopes ? 1 : 0) << 0x15)  // buildoncoasts
  3478 	                   |                                        (1 << 0x16)  // canals
  4072 	                   |                                        (1 << 0x16)  // canals
  3479 	                   |                                        (1 << 0x17)  // newstartyear
  4073 	                   |                                        (1 << 0x17)  // newstartyear
  3480 	                   |                                        (0 << 0x18)  // freighttrains
  4074 	                   |                                        (0 << 0x18)  // freighttrains
  3481 	                   |                                        (0 << 0x19)  // newhouses
  4075 	                   |                                        (1 << 0x19)  // newhouses
  3482 	                   |                                        (1 << 0x1A)  // newbridges
  4076 	                   |                                        (1 << 0x1A)  // newbridges
  3483 	                   |                                        (0 << 0x1B)  // newtownnames
  4077 	                   |                                        (0 << 0x1B)  // newtownnames
  3484 	                   |                                        (0 << 0x1C)  // moreanimations
  4078 	                   |                                        (0 << 0x1C)  // moreanimations
  3485 	                   |    ((_patches.wagon_speed_limits ? 1 : 0) << 0x1D)  // wagonspeedlimits
  4079 	                   |    ((_patches.wagon_speed_limits ? 1 : 0) << 0x1D)  // wagonspeedlimits
  3486 	                   |                                        (1 << 0x1E)  // newshistory
  4080 	                   |                                        (1 << 0x1E)  // newshistory
  3548 		free(file->stations);
  4142 		free(file->stations);
  3549 		file->stations = NULL;
  4143 		file->stations = NULL;
  3550 	}
  4144 	}
  3551 }
  4145 }
  3552 
  4146 
       
  4147 static void ResetCustomHouses()
       
  4148 {
       
  4149 	GRFFile *file;
       
  4150 	uint i;
       
  4151 
       
  4152 	for (file = _first_grffile; file != NULL; file = file->next) {
       
  4153 		if (file->housespec == NULL) continue;
       
  4154 		for (i = 0; i < HOUSE_MAX; i++) free(file->housespec[i]);
       
  4155 
       
  4156 		free(file->housespec);
       
  4157 		file->housespec = NULL;
       
  4158 	}
       
  4159 }
       
  4160 
  3553 static void ResetNewGRF()
  4161 static void ResetNewGRF()
  3554 {
  4162 {
  3555 	GRFFile *next;
  4163 	GRFFile *next;
  3556 
  4164 
  3557 	for (GRFFile *f = _first_grffile; f != NULL; f = next) {
  4165 	for (GRFFile *f = _first_grffile; f != NULL; f = next) {
  3571  */
  4179  */
  3572 static void ResetNewGRFData()
  4180 static void ResetNewGRFData()
  3573 {
  4181 {
  3574 	CleanUpStrings();
  4182 	CleanUpStrings();
  3575 
  4183 
  3576 	// Copy/reset original engine info data
  4184 	/* Copy/reset original engine info data */
  3577 	memcpy(&_engine_info, &orig_engine_info, sizeof(orig_engine_info));
  4185 	memcpy(&_engine_info, &orig_engine_info, sizeof(orig_engine_info));
  3578 	memcpy(&_rail_vehicle_info, &orig_rail_vehicle_info, sizeof(orig_rail_vehicle_info));
  4186 	memcpy(&_rail_vehicle_info, &orig_rail_vehicle_info, sizeof(orig_rail_vehicle_info));
  3579 	memcpy(&_ship_vehicle_info, &orig_ship_vehicle_info, sizeof(orig_ship_vehicle_info));
  4187 	memcpy(&_ship_vehicle_info, &orig_ship_vehicle_info, sizeof(orig_ship_vehicle_info));
  3580 	memcpy(&_aircraft_vehicle_info, &orig_aircraft_vehicle_info, sizeof(orig_aircraft_vehicle_info));
  4188 	memcpy(&_aircraft_vehicle_info, &orig_aircraft_vehicle_info, sizeof(orig_aircraft_vehicle_info));
  3581 	memcpy(&_road_vehicle_info, &orig_road_vehicle_info, sizeof(orig_road_vehicle_info));
  4189 	memcpy(&_road_vehicle_info, &orig_road_vehicle_info, sizeof(orig_road_vehicle_info));
  3582 
  4190 
  3583 	// Copy/reset original bridge info data
  4191 	/* Copy/reset original bridge info data
  3584 	// First, free sprite table data
  4192 	 * First, free sprite table data */
  3585 	for (uint i = 0; i < MAX_BRIDGES; i++) {
  4193 	for (uint i = 0; i < MAX_BRIDGES; i++) {
  3586 		if (_bridge[i].sprite_table != NULL) {
  4194 		if (_bridge[i].sprite_table != NULL) {
  3587 			for (uint j = 0; j < 7; j++) free(_bridge[i].sprite_table[j]);
  4195 			for (uint j = 0; j < 7; j++) free(_bridge[i].sprite_table[j]);
  3588 			free(_bridge[i].sprite_table);
  4196 			free(_bridge[i].sprite_table);
  3589 		}
  4197 		}
  3590 	}
  4198 	}
  3591 	memcpy(&_bridge, &orig_bridge, sizeof(_bridge));
  4199 	memcpy(&_bridge, &orig_bridge, sizeof(_bridge));
  3592 
  4200 
  3593 	// Reset refit/cargo class data
  4201 	/* Reset refit/cargo class data */
  3594 	memset(&cargo_allowed, 0, sizeof(cargo_allowed));
  4202 	memset(&cargo_allowed, 0, sizeof(cargo_allowed));
  3595 	memset(&cargo_disallowed, 0, sizeof(cargo_disallowed));
  4203 	memset(&cargo_disallowed, 0, sizeof(cargo_disallowed));
  3596 
  4204 
  3597 	// Reset GRM reservations
  4205 	/* Reset GRM reservations */
  3598 	memset(&_grm_engines, 0, sizeof(_grm_engines));
  4206 	memset(&_grm_engines, 0, sizeof(_grm_engines));
  3599 
  4207 
  3600 	// Unload sprite group data
  4208 	/* Unload sprite group data */
  3601 	UnloadWagonOverrides();
  4209 	UnloadWagonOverrides();
  3602 	UnloadRotorOverrideSprites();
  4210 	UnloadRotorOverrideSprites();
  3603 	UnloadCustomEngineSprites();
  4211 	UnloadCustomEngineSprites();
  3604 	UnloadCustomEngineNames();
  4212 	UnloadCustomEngineNames();
  3605 	ResetEngineListOrder();
  4213 	ResetEngineListOrder();
  3606 
  4214 
  3607 	// Reset price base data
  4215 	/* Reset price base data */
  3608 	ResetPriceBaseMultipliers();
  4216 	ResetPriceBaseMultipliers();
  3609 
  4217 
  3610 	/* Reset the curencies array */
  4218 	/* Reset the curencies array */
  3611 	ResetCurrencies();
  4219 	ResetCurrencies();
  3612 
  4220 
  3613 	// Reset station classes
  4221 	/* Reset the house array */
       
  4222 	ResetCustomHouses();
       
  4223 	ResetHouses();
       
  4224 
       
  4225 	/* Reset station classes */
  3614 	ResetStationClasses();
  4226 	ResetStationClasses();
  3615 	ResetCustomStations();
  4227 	ResetCustomStations();
  3616 
  4228 
       
  4229 	/* Reset the snowline table. */
       
  4230 	ClearSnowLine();
       
  4231 
  3617 	/* Reset NewGRF files */
  4232 	/* Reset NewGRF files */
  3618 	ResetNewGRF();
  4233 	ResetNewGRF();
  3619 
  4234 
  3620 	// Add engine type to engine data. This is needed for the refit precalculation.
  4235 	/* Add engine type to engine data. This is needed for the refit precalculation. */
  3621 	AddTypeToEngines();
  4236 	AddTypeToEngines();
  3622 
  4237 
  3623 	/* Set up the default cargo types */
  4238 	/* Set up the default cargo types */
  3624 	SetupCargoForClimate(_opt.landscape);
  4239 	SetupCargoForClimate(_opt.landscape);
  3625 
       
  3626 	/* Generate default cargo translation table */
       
  3627 	memset(_default_cargo_list, 0, sizeof(_default_cargo_list));
       
  3628 	for (CargoID c = 0; c != NUM_CARGO; c++) {
       
  3629 		const CargoSpec *cs = GetCargo(c);
       
  3630 		if (cs->IsValid()) _default_cargo_list[cs->bitnum] = cs->label;
       
  3631 	}
       
  3632 
  4240 
  3633 	/* Reset misc GRF features and train list display variables */
  4241 	/* Reset misc GRF features and train list display variables */
  3634 	_misc_grf_features = 0;
  4242 	_misc_grf_features = 0;
  3635 	_traininfo_vehicle_pitch = 0;
  4243 	_traininfo_vehicle_pitch = 0;
  3636 	_traininfo_vehicle_width = 29;
  4244 	_traininfo_vehicle_width = 29;
  3637 	_have_2cc = false;
  4245 	_have_2cc = false;
       
  4246 	_have_newhouses = false;
  3638 	_signal_base = 0;
  4247 	_signal_base = 0;
  3639 	_coast_base = 0;
  4248 	_coast_base = 0;
  3640 
  4249 
  3641 	InitializeSoundPool();
  4250 	InitializeSoundPool();
  3642 	InitializeSpriteGroupPool();
  4251 	InitializeSpriteGroupPool();
  3755 		/* Skip engine if not available in this climate */
  4364 		/* Skip engine if not available in this climate */
  3756 		if (!HASBIT(_engine_info[engine].climates, _opt.landscape)) continue;
  4365 		if (!HASBIT(_engine_info[engine].climates, _opt.landscape)) continue;
  3757 
  4366 
  3758 		uint32 mask = 0;
  4367 		uint32 mask = 0;
  3759 		uint32 not_mask = 0;
  4368 		uint32 not_mask = 0;
  3760 		uint32 xor_mask = _engine_info[engine].refit_mask;
  4369 		uint32 xor_mask = 0;
       
  4370 
       
  4371 		if (_engine_info[engine].refit_mask != 0) {
       
  4372 			const GRFFile *file = GetEngineGRF(engine);
       
  4373 			if (file != NULL && file->cargo_max != 0) {
       
  4374 				/* Apply cargo translation table to the refit mask */
       
  4375 				uint num_cargo = min(32, file->cargo_max);
       
  4376 				for (uint i = 0; i < num_cargo; i++) {
       
  4377 					if (!HASBIT(_engine_info[engine].refit_mask, i)) continue;
       
  4378 
       
  4379 					CargoID c = GetCargoIDByLabel(file->cargo_list[i]);
       
  4380 					if (c == CT_INVALID) continue;
       
  4381 
       
  4382 					SETBIT(xor_mask, c);
       
  4383 				}
       
  4384 			} else {
       
  4385 				/* No cargo table, so use the cargo bitnum values */
       
  4386 				for (CargoID c = 0; c < NUM_CARGO; c++) {
       
  4387 					const CargoSpec *cs = GetCargo(c);
       
  4388 					if (!cs->IsValid()) continue;
       
  4389 
       
  4390 					if (HASBIT(_engine_info[engine].refit_mask, cs->bitnum)) SETBIT(xor_mask, c);
       
  4391 				}
       
  4392 			}
       
  4393 		}
  3761 
  4394 
  3762 		if (cargo_allowed[engine] != 0) {
  4395 		if (cargo_allowed[engine] != 0) {
  3763 			// Build up the list of cargo types from the set cargo classes.
  4396 			/* Build up the list of cargo types from the set cargo classes. */
  3764 			for (CargoID i = 0; i < NUM_CARGO; i++) {
  4397 			for (CargoID i = 0; i < NUM_CARGO; i++) {
  3765 				const CargoSpec *cs = GetCargo(i);
  4398 				const CargoSpec *cs = GetCargo(i);
  3766 				if (cargo_allowed[engine]    & cs->classes) SETBIT(mask,     i);
  4399 				if (cargo_allowed[engine]    & cs->classes) SETBIT(mask,     i);
  3767 				if (cargo_disallowed[engine] & cs->classes) SETBIT(not_mask, i);
  4400 				if (cargo_disallowed[engine] & cs->classes) SETBIT(not_mask, i);
  3768 			}
  4401 			}
  3769 		} else {
  4402 		} else {
  3770 			// Don't apply default refit mask to wagons or engines with no capacity
  4403 			/* Don't apply default refit mask to wagons or engines with no capacity */
  3771 			if (xor_mask == 0 && (
  4404 			if (xor_mask == 0 && (
  3772 						GetEngine(engine)->type != VEH_TRAIN || (
  4405 						GetEngine(engine)->type != VEH_TRAIN || (
  3773 							RailVehInfo(engine)->capacity != 0 &&
  4406 							RailVehInfo(engine)->capacity != 0 &&
  3774 							RailVehInfo(engine)->railveh_type != RAILVEH_WAGON
  4407 							RailVehInfo(engine)->railveh_type != RAILVEH_WAGON
  3775 						)
  4408 						)
  3785 				}
  4418 				}
  3786 			}
  4419 			}
  3787 		}
  4420 		}
  3788 		_engine_info[engine].refit_mask = ((mask & ~not_mask) ^ xor_mask) & _cargo_mask;
  4421 		_engine_info[engine].refit_mask = ((mask & ~not_mask) ^ xor_mask) & _cargo_mask;
  3789 
  4422 
  3790 		if (_engine_info[engine].refit_mask == 0) continue;
       
  3791 
       
  3792 		/* Check if this engine's cargo type is valid. If not, set to the first refittable
  4423 		/* Check if this engine's cargo type is valid. If not, set to the first refittable
  3793 		 * cargo type. Apparently cargo_type isn't a common property... */
  4424 		 * cargo type. Apparently cargo_type isn't a common property... */
  3794 		switch (GetEngine(engine)->type) {
  4425 		switch (GetEngine(engine)->type) {
  3795 			case VEH_TRAIN: {
  4426 			case VEH_TRAIN: {
  3796 				RailVehicleInfo *rvi = &_rail_vehicle_info[engine];
  4427 				RailVehicleInfo *rvi = &_rail_vehicle_info[engine];
  3811 				break;
  4442 				break;
  3812 			}
  4443 			}
  3813 		}
  4444 		}
  3814 	}
  4445 	}
  3815 }
  4446 }
       
  4447 
       
  4448 /** Add all new houses to the house array. House properties can be set at any
       
  4449  * time in the GRF file, so we can only add a house spec to the house array
       
  4450  * after the file has finished loading. We also need to check the dates, due to
       
  4451  * the TTDPatch behaviour described below that we need to emulate. */
       
  4452 static void FinaliseHouseArray()
       
  4453 {
       
  4454 	/* If there are no houses with start dates before 1930, then all houses
       
  4455 	 * with start dates of 1930 have them reset to 0. This is in order to be
       
  4456 	 * compatible with TTDPatch, where if no houses have start dates before
       
  4457 	 * 1930 and the date is before 1930, the game pretends that this is 1930.
       
  4458 	 * If there have been any houses defined with start dates before 1930 then
       
  4459 	 * the dates are left alone. */
       
  4460 	bool reset_dates = true;
       
  4461 
       
  4462 	for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
       
  4463 		if (file->housespec == NULL) continue;
       
  4464 
       
  4465 		for (int i = 0; i < HOUSE_MAX; i++) {
       
  4466 			HouseSpec *hs = file->housespec[i];
       
  4467 			if (hs != NULL) {
       
  4468 				SetHouseSpec(hs);
       
  4469 				if (hs->min_date < 1930) reset_dates = false;
       
  4470 			}
       
  4471 		}
       
  4472 	}
       
  4473 
       
  4474 	if (reset_dates) {
       
  4475 		for (int i = NEW_HOUSE_OFFSET; i < HOUSE_MAX; i++) {
       
  4476 			HouseSpec *hs = GetHouseSpecs(i);
       
  4477 
       
  4478 			if (hs->enabled && hs->min_date == 1930) hs->min_date = 0;
       
  4479 		}
       
  4480 	}
       
  4481 }
       
  4482 
       
  4483 
       
  4484 /** Each cargo string needs to be mapped from TTDPatch to OpenTTD string IDs.
       
  4485  * This is done after loading so that strings from Action 4 will be mapped
       
  4486  * properly. */
       
  4487 static void MapNewCargoStrings()
       
  4488 {
       
  4489 	for (CargoID c = 0; c < NUM_CARGO; c++) {
       
  4490 		CargoSpec *cs = &_cargo[c];
       
  4491 		/* Don't map if the cargo is unavailable or not from NewGRF */
       
  4492 		if (!cs->IsValid() || cs->grfid == 0) continue;
       
  4493 
       
  4494 		cs->name         = MapGRFStringID(cs->grfid, cs->name);
       
  4495 		cs->name_plural  = MapGRFStringID(cs->grfid, cs->name_plural);
       
  4496 		cs->units_volume = MapGRFStringID(cs->grfid, cs->units_volume);
       
  4497 		cs->quantifier   = MapGRFStringID(cs->grfid, cs->quantifier);
       
  4498 		cs->abbrev       = MapGRFStringID(cs->grfid, cs->abbrev);
       
  4499 	}
       
  4500 }
       
  4501 
  3816 
  4502 
  3817 /* Here we perform initial decoding of some special sprites (as are they
  4503 /* Here we perform initial decoding of some special sprites (as are they
  3818  * described at http://www.ttdpatch.net/src/newgrf.txt, but this is only a very
  4504  * described at http://www.ttdpatch.net/src/newgrf.txt, but this is only a very
  3819  * partial implementation yet). */
  4505  * partial implementation yet). */
  3820 /* XXX: We consider GRF files trusted. It would be trivial to exploit OTTD by
  4506 /* XXX: We consider GRF files trusted. It would be trivial to exploit OTTD by
  3833 	 * --pasky */
  4519 	 * --pasky */
  3834 	/* We need a pre-stage to set up GOTO labels of Action 0x10 because the grf
  4520 	/* We need a pre-stage to set up GOTO labels of Action 0x10 because the grf
  3835 	 * is not in memory and scanning the file every time would be too expensive.
  4521 	 * is not in memory and scanning the file every time would be too expensive.
  3836 	 * In other stages we skip action 0x10 since it's already dealt with. */
  4522 	 * In other stages we skip action 0x10 since it's already dealt with. */
  3837 	static const SpecialSpriteHandler handlers[][GLS_END] = {
  4523 	static const SpecialSpriteHandler handlers[][GLS_END] = {
  3838 		/* 0x00 */ { NULL,     SafeChangeInfo, NULL,       InitChangeInfo, FeatureChangeInfo, },
  4524 		/* 0x00 */ { NULL,     SafeChangeInfo, NULL,       InitChangeInfo, ReserveChangeInfo, FeatureChangeInfo, },
  3839 		/* 0x01 */ { NULL,     GRFUnsafe, NULL,            NULL,       NewSpriteSet, },
  4525 		/* 0x01 */ { NULL,     GRFUnsafe, NULL,            NULL,           NULL,              NewSpriteSet, },
  3840 		/* 0x02 */ { NULL,     GRFUnsafe, NULL,            NULL,       NewSpriteGroup, },
  4526 		/* 0x02 */ { NULL,     GRFUnsafe, NULL,            NULL,           NULL,              NewSpriteGroup, },
  3841 		/* 0x03 */ { NULL,     GRFUnsafe, NULL,            NULL,       FeatureMapSpriteGroup, },
  4527 		/* 0x03 */ { NULL,     GRFUnsafe, NULL,            NULL,           NULL,              FeatureMapSpriteGroup, },
  3842 		/* 0x04 */ { NULL,     NULL,      NULL,            NULL,       FeatureNewName, },
  4528 		/* 0x04 */ { NULL,     NULL,      NULL,            NULL,           NULL,              FeatureNewName, },
  3843 		/* 0x05 */ { NULL,     NULL,      NULL,            NULL,       GraphicsNew, },
  4529 		/* 0x05 */ { NULL,     NULL,      NULL,            NULL,           NULL,              GraphicsNew, },
  3844 		/* 0x06 */ { NULL,     NULL,      NULL,            CfgApply,   CfgApply, },
  4530 		/* 0x06 */ { NULL,     NULL,      NULL,            CfgApply,       CfgApply,          CfgApply, },
  3845 		/* 0x07 */ { NULL,     NULL,      NULL,            NULL,       SkipIf, },
  4531 		/* 0x07 */ { NULL,     NULL,      NULL,            NULL,           SkipIf,            SkipIf, },
  3846 		/* 0x08 */ { ScanInfo, NULL,      NULL,            GRFInfo,    GRFInfo, },
  4532 		/* 0x08 */ { ScanInfo, NULL,      NULL,            GRFInfo,        NULL,              GRFInfo, },
  3847 		/* 0x09 */ { NULL,     NULL,      NULL,            SkipIf,     SkipIf, },
  4533 		/* 0x09 */ { NULL,     NULL,      NULL,            SkipIf,         SkipIf,            SkipIf, },
  3848 		/* 0x0A */ { NULL,     NULL,      NULL,            NULL,       SpriteReplace, },
  4534 		/* 0x0A */ { NULL,     NULL,      NULL,            NULL,           NULL,              SpriteReplace, },
  3849 		/* 0x0B */ { NULL,     NULL,      NULL,            GRFLoadError, GRFLoadError, },
  4535 		/* 0x0B */ { NULL,     NULL,      NULL,            GRFLoadError,   GRFLoadError,      GRFLoadError, },
  3850 		/* 0x0C */ { NULL,     NULL,      NULL,            GRFComment, GRFComment, },
  4536 		/* 0x0C */ { NULL,     NULL,      NULL,            GRFComment,     NULL,              GRFComment, },
  3851 		/* 0x0D */ { NULL,     SafeParamSet, NULL,         ParamSet,   ParamSet, },
  4537 		/* 0x0D */ { NULL,     SafeParamSet, NULL,         ParamSet,       ParamSet,          ParamSet, },
  3852 		/* 0x0E */ { NULL,     SafeGRFInhibit, NULL,       GRFInhibit, GRFInhibit, },
  4538 		/* 0x0E */ { NULL,     SafeGRFInhibit, NULL,       GRFInhibit,     GRFInhibit,        GRFInhibit, },
  3853 		/* 0x0F */ { NULL,     NULL,      NULL,            NULL,       NULL, },
  4539 		/* 0x0F */ { NULL,     NULL,      NULL,            NULL,           NULL,              NULL, },
  3854 		/* 0x10 */ { NULL,     NULL,      DefineGotoLabel, NULL,       NULL, },
  4540 		/* 0x10 */ { NULL,     NULL,      DefineGotoLabel, NULL,           NULL,              NULL, },
  3855 		/* 0x11 */ { NULL,     GRFUnsafe, NULL,            NULL,       GRFSound, },
  4541 		/* 0x11 */ { NULL,     GRFUnsafe, NULL,            NULL,           NULL,              GRFSound, },
  3856 		/* 0x12 */ { NULL,     NULL,      NULL,            NULL,       LoadFontGlyph, },
  4542 		/* 0x12 */ { NULL,     NULL,      NULL,            NULL,           NULL,              LoadFontGlyph, },
  3857 		/* 0x13 */ { NULL,     NULL,      NULL,            NULL,       TranslateGRFStrings, },
  4543 		/* 0x13 */ { NULL,     NULL,      NULL,            NULL,           NULL,              TranslateGRFStrings, },
  3858 	};
  4544 	};
  3859 
  4545 
  3860 	byte* buf;
  4546 	byte* buf;
  3861 
  4547 
  3862 	if (_preload_sprite == NULL) {
  4548 	if (_preload_sprite == NULL) {
  3977 	}
  4663 	}
  3978 }
  4664 }
  3979 
  4665 
  3980 void InitDepotWindowBlockSizes();
  4666 void InitDepotWindowBlockSizes();
  3981 
  4667 
       
  4668 static void AfterLoadGRFs()
       
  4669 {
       
  4670 	/* Pre-calculate all refit masks after loading GRF files. */
       
  4671 	CalculateRefitMasks();
       
  4672 
       
  4673 	/* Set the block size in the depot windows based on vehicle sprite sizes */
       
  4674 	InitDepotWindowBlockSizes();
       
  4675 
       
  4676 	/* Add all new houses to the house array. */
       
  4677 	FinaliseHouseArray();
       
  4678 
       
  4679 	/* Map cargo strings. This is a separate step because cargos are
       
  4680 	 * loaded before strings... */
       
  4681 	MapNewCargoStrings();
       
  4682 }
       
  4683 
  3982 void LoadNewGRF(uint load_index, uint file_index)
  4684 void LoadNewGRF(uint load_index, uint file_index)
  3983 {
  4685 {
  3984 	InitializeGRFSpecial();
  4686 	InitializeGRFSpecial();
  3985 
  4687 
  3986 	ResetNewGRFData();
  4688 	ResetNewGRFData();
  3994 		_cur_stage = stage;
  4696 		_cur_stage = stage;
  3995 		_cur_spriteid = load_index;
  4697 		_cur_spriteid = load_index;
  3996 		for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
  4698 		for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
  3997 			if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
  4699 			if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
  3998 
  4700 
  3999 			// TODO usererror()
  4701 			/* @todo usererror() */
  4000 			if (!FioCheckFileExists(c->filename)) error("NewGRF file is missing '%s'", c->filename);
  4702 			if (!FioCheckFileExists(c->filename)) error("NewGRF file is missing '%s'", c->filename);
  4001 
  4703 
  4002 			if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
  4704 			if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
  4003 			LoadNewGRFFile(c, slot++, stage);
  4705 			LoadNewGRFFile(c, slot++, stage);
  4004 			if (stage == GLS_ACTIVATION) {
  4706 			if (stage == GLS_ACTIVATION) {
  4007 				DEBUG(sprite, 2, "Currently %i sprites are loaded", _cur_spriteid);
  4709 				DEBUG(sprite, 2, "Currently %i sprites are loaded", _cur_spriteid);
  4008 			}
  4710 			}
  4009 		}
  4711 		}
  4010 	}
  4712 	}
  4011 
  4713 
  4012 	// Pre-calculate all refit masks after loading GRF files
  4714 	/* Call any functions that should be run after GRFs have been loaded. */
  4013 	CalculateRefitMasks();
  4715 	AfterLoadGRFs();
  4014 
  4716 }
  4015 	/* Set the block size in the depot windows based on vehicle sprite sizes */
       
  4016 	InitDepotWindowBlockSizes();
       
  4017 }