src/newgrf.cpp
branchNewGRF_ports
changeset 6720 35756db7e577
parent 6719 4cc327ad39d5
child 6722 72f280229ee1
equal deleted inserted replaced
6719:4cc327ad39d5 6720:35756db7e577
    16 #include "station.h"
    16 #include "station.h"
    17 #include "sprite.h"
    17 #include "sprite.h"
    18 #include "newgrf.h"
    18 #include "newgrf.h"
    19 #include "variables.h"
    19 #include "variables.h"
    20 #include "string.h"
    20 #include "string.h"
       
    21 #include "strings.h"
    21 #include "table/strings.h"
    22 #include "table/strings.h"
    22 #include "bridge.h"
    23 #include "bridge.h"
    23 #include "town.h"
    24 #include "town.h"
    24 #include "economy.h"
    25 #include "economy.h"
    25 #include "newgrf_engine.h"
    26 #include "newgrf_engine.h"
    38 #include "helpers.hpp"
    39 #include "helpers.hpp"
    39 #include "table/town_land.h"
    40 #include "table/town_land.h"
    40 #include "cargotype.h"
    41 #include "cargotype.h"
    41 #include "industry.h"
    42 #include "industry.h"
    42 #include "newgrf_canal.h"
    43 #include "newgrf_canal.h"
       
    44 #include "table/build_industry.h"
    43 #include "newgrf_commons.h"
    45 #include "newgrf_commons.h"
       
    46 #include "newgrf_townname.h"
       
    47 #include "newgrf_industries.h"
    44 
    48 
    45 /* TTDPatch extended GRF format codec
    49 /* TTDPatch extended GRF format codec
    46  * (c) Petr Baudis 2004 (GPL'd)
    50  * (c) Petr Baudis 2004 (GPL'd)
    47  * Changes by Florian octo Forster are (c) by the OpenTTD development team.
    51  * Changes by Florian octo Forster are (c) by the OpenTTD development team.
    48  *
    52  *
    73 
    77 
    74 /* Used by Action 0x06 to preload a pseudo sprite and modify its content */
    78 /* Used by Action 0x06 to preload a pseudo sprite and modify its content */
    75 static byte *_preload_sprite = NULL;
    79 static byte *_preload_sprite = NULL;
    76 
    80 
    77 /* Indicates which are the newgrf features currently loaded ingame */
    81 /* Indicates which are the newgrf features currently loaded ingame */
    78 uint8 _loaded_newgrf_features;
    82 GRFLoadedFeatures _loaded_newgrf_features;
    79 
    83 
    80 enum GrfDataType {
    84 enum GrfDataType {
    81 	GDT_SOUND,
    85 	GDT_SOUND,
    82 };
    86 };
    83 
    87 
   281 }
   285 }
   282 
   286 
   283 
   287 
   284 typedef bool (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
   288 typedef bool (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
   285 
   289 
   286 #define FOR_EACH_OBJECT for (i = 0; i < numinfo; i++)
       
   287 
       
   288 static void dewagonize(int condition, int engine)
   290 static void dewagonize(int condition, int engine)
   289 {
   291 {
   290 	EngineInfo *ei = &_engine_info[engine];
   292 	EngineInfo *ei = &_engine_info[engine];
   291 	RailVehicleInfo *rvi = &_rail_vehicle_info[engine];
   293 	RailVehicleInfo *rvi = &_rail_vehicle_info[engine];
   292 
   294 
   300 	}
   302 	}
   301 }
   303 }
   302 
   304 
   303 static bool RailVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   305 static bool RailVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   304 {
   306 {
   305 	EngineInfo *ei = &_engine_info[engine];
       
   306 	RailVehicleInfo *rvi = &_rail_vehicle_info[engine];
       
   307 	byte *buf = *bufp;
   307 	byte *buf = *bufp;
   308 	int i;
       
   309 	bool ret = false;
   308 	bool ret = false;
   310 
   309 
   311 	switch (prop) {
   310 	for (int i = 0; i < numinfo; i++) {
   312 		case 0x05: // Track type
   311 		EngineInfo *ei       = &_engine_info[engine + i];
   313 			FOR_EACH_OBJECT {
   312 		RailVehicleInfo *rvi = &_rail_vehicle_info[engine + i];
       
   313 
       
   314 		switch (prop) {
       
   315 			case 0x05: { // Track type
   314 				uint8 tracktype = grf_load_byte(&buf);
   316 				uint8 tracktype = grf_load_byte(&buf);
   315 
   317 
   316 				switch (tracktype) {
   318 				switch (tracktype) {
   317 					case 0: rvi[i].railtype = rvi[i].engclass >= 2 ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL; break;
   319 					case 0: rvi->railtype = rvi->engclass >= 2 ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL; break;
   318 					case 1: rvi[i].railtype = RAILTYPE_MONO; break;
   320 					case 1: rvi->railtype = RAILTYPE_MONO; break;
   319 					case 2: rvi[i].railtype = RAILTYPE_MAGLEV; break;
   321 					case 2: rvi->railtype = RAILTYPE_MAGLEV; break;
   320 					default:
   322 					default:
   321 						grfmsg(1, "RailVehicleChangeInfo: Invalid track type %d specified, ignoring", tracktype);
   323 						grfmsg(1, "RailVehicleChangeInfo: Invalid track type %d specified, ignoring", tracktype);
   322 						break;
   324 						break;
   323 				}
   325 				}
   324 			}
   326 			} break;
   325 			break;
   327 
   326 
   328 			case 0x08: // AI passenger service
   327 		case 0x08: // AI passenger service
   329 				/** @todo Tells the AI that this engine is designed for
   328 			/* @todo missing feature */
   330 				 * passenger services and shouldn't be used for freight. */
   329 			FOR_EACH_OBJECT grf_load_byte(&buf);
   331 				grf_load_byte(&buf);
   330 			ret = true;
   332 				ret = true;
   331 			break;
   333 				break;
   332 
   334 
   333 		case 0x09: // Speed (1 unit is 1 kmh)
   335 			case 0x09: { // Speed (1 unit is 1 kmh)
   334 			FOR_EACH_OBJECT {
       
   335 				uint16 speed = grf_load_word(&buf);
   336 				uint16 speed = grf_load_word(&buf);
   336 				if (speed == 0xFFFF) speed = 0;
   337 				if (speed == 0xFFFF) speed = 0;
   337 
   338 
   338 				rvi[i].max_speed = speed;
   339 				rvi->max_speed = speed;
   339 			}
   340 			} break;
   340 			break;
   341 
   341 
   342 			case 0x0B: { // Power
   342 		case 0x0B: // Power
       
   343 			FOR_EACH_OBJECT {
       
   344 				uint16 power = grf_load_word(&buf);
   343 				uint16 power = grf_load_word(&buf);
   345 
   344 
   346 				if (rvi[i].railveh_type == RAILVEH_MULTIHEAD) power /= 2;
   345 				if (rvi->railveh_type == RAILVEH_MULTIHEAD) power /= 2;
   347 
   346 
   348 				rvi[i].power = power;
   347 				rvi->power = power;
   349 				dewagonize(power, engine + i);
   348 				dewagonize(power, engine + i);
   350 			}
   349 			} break;
   351 			break;
   350 
   352 
   351 			case 0x0D: { // Running cost factor
   353 		case 0x0D: // Running cost factor
       
   354 			FOR_EACH_OBJECT {
       
   355 				uint8 runcostfact = grf_load_byte(&buf);
   352 				uint8 runcostfact = grf_load_byte(&buf);
   356 
   353 
   357 				if (rvi[i].railveh_type == RAILVEH_MULTIHEAD) runcostfact /= 2;
   354 				if (rvi->railveh_type == RAILVEH_MULTIHEAD) runcostfact /= 2;
   358 
   355 
   359 				rvi[i].running_cost_base = runcostfact;
   356 				rvi->running_cost_base = runcostfact;
   360 			}
   357 			} break;
   361 			break;
   358 
   362 
   359 			case 0x0E: { // Running cost base
   363 		case 0x0E: // Running cost base
       
   364 			FOR_EACH_OBJECT {
       
   365 				uint32 base = grf_load_dword(&buf);
   360 				uint32 base = grf_load_dword(&buf);
   366 
   361 
   367 				switch (base) {
   362 				switch (base) {
   368 					case 0x4C30: rvi[i].running_cost_class = 0; break;
   363 					case 0x4C30: rvi->running_cost_class = 0; break;
   369 					case 0x4C36: rvi[i].running_cost_class = 1; break;
   364 					case 0x4C36: rvi->running_cost_class = 1; break;
   370 					case 0x4C3C: rvi[i].running_cost_class = 2; break;
   365 					case 0x4C3C: rvi->running_cost_class = 2; break;
   371 					case 0: break; // Used by wagons
   366 					case 0: break; // Used by wagons
   372 					default:
   367 					default:
   373 						grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
   368 						grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
   374 						break;
   369 						break;
   375 				}
   370 				}
   376 			}
   371 			} break;
   377 			break;
   372 
   378 
   373 			case 0x12: { // Sprite ID
   379 		case 0x12: // Sprite ID
       
   380 			FOR_EACH_OBJECT {
       
   381 				uint8 spriteid = grf_load_byte(&buf);
   374 				uint8 spriteid = grf_load_byte(&buf);
   382 
   375 
   383 				/* TTD sprite IDs point to a location in a 16bit array, but we use it
   376 				/* TTD sprite IDs point to a location in a 16bit array, but we use it
   384 				 * as an array index, so we need it to be half the original value. */
   377 				 * as an array index, so we need it to be half the original value. */
   385 				if (spriteid < 0xFD) spriteid >>= 1;
   378 				if (spriteid < 0xFD) spriteid >>= 1;
   386 
   379 
   387 				rvi[i].image_index = spriteid;
   380 				rvi->image_index = spriteid;
   388 			}
   381 			} break;
   389 			break;
   382 
   390 
   383 			case 0x13: { // Dual-headed
   391 		case 0x13: // Dual-headed
       
   392 			FOR_EACH_OBJECT {
       
   393 				uint8 dual = grf_load_byte(&buf);
   384 				uint8 dual = grf_load_byte(&buf);
   394 
   385 
   395 				if (dual != 0) {
   386 				if (dual != 0) {
   396 					if (rvi[i].railveh_type != RAILVEH_MULTIHEAD) {
   387 					if (rvi->railveh_type != RAILVEH_MULTIHEAD) {
   397 						// adjust power and running cost if needed
   388 						// adjust power and running cost if needed
   398 						rvi[i].power /= 2;
   389 						rvi->power /= 2;
   399 						rvi[i].running_cost_base /= 2;
   390 						rvi->running_cost_base /= 2;
   400 					}
   391 					}
   401 					rvi[i].railveh_type = RAILVEH_MULTIHEAD;
   392 					rvi->railveh_type = RAILVEH_MULTIHEAD;
   402 				} else {
   393 				} else {
   403 					if (rvi[i].railveh_type == RAILVEH_MULTIHEAD) {
   394 					if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
   404 						// adjust power and running cost if needed
   395 						// adjust power and running cost if needed
   405 						rvi[i].power *= 2;
   396 						rvi->power *= 2;
   406 						rvi[i].running_cost_base *= 2;
   397 						rvi->running_cost_base *= 2;
   407 					}
   398 					}
   408 					rvi[i].railveh_type = rvi[i].power == 0 ?
   399 					rvi->railveh_type = rvi->power == 0 ?
   409 						RAILVEH_WAGON : RAILVEH_SINGLEHEAD;
   400 						RAILVEH_WAGON : RAILVEH_SINGLEHEAD;
   410 				}
   401 				}
   411 			}
   402 			} break;
   412 			break;
   403 
   413 
   404 			case 0x14: // Cargo capacity
   414 		case 0x14: // Cargo capacity
   405 				rvi->capacity = grf_load_byte(&buf);
   415 			FOR_EACH_OBJECT rvi[i].capacity = grf_load_byte(&buf);
   406 				break;
   416 			break;
   407 
   417 
   408 			case 0x15: { // Cargo type
   418 		case 0x15: // Cargo type
       
   419 			FOR_EACH_OBJECT {
       
   420 				uint8 ctype = grf_load_byte(&buf);
   409 				uint8 ctype = grf_load_byte(&buf);
   421 
   410 
   422 				if (ctype < NUM_CARGO && HASBIT(_cargo_mask, ctype)) {
   411 				if (ctype < NUM_CARGO && HASBIT(_cargo_mask, ctype)) {
   423 					rvi[i].cargo_type = ctype;
   412 					rvi->cargo_type = ctype;
   424 				} else {
   413 				} else {
   425 					rvi[i].cargo_type = CT_INVALID;
   414 					rvi->cargo_type = CT_INVALID;
   426 					grfmsg(2, "RailVehicleChangeInfo: Invalid cargo type %d, using first refittable", ctype);
   415 					grfmsg(2, "RailVehicleChangeInfo: Invalid cargo type %d, using first refittable", ctype);
   427 				}
   416 				}
   428 			}
   417 			} break;
   429 			break;
   418 
   430 
   419 			case 0x16: // Weight
   431 		case 0x16: // Weight
   420 				SB(rvi->weight, 0, 8, grf_load_byte(&buf));
   432 			FOR_EACH_OBJECT SB(rvi[i].weight, 0, 8, grf_load_byte(&buf));
   421 				break;
   433 			break;
   422 
   434 
   423 			case 0x17: // Cost factor
   435 		case 0x17: // Cost factor
   424 				rvi->base_cost = grf_load_byte(&buf);
   436 			FOR_EACH_OBJECT rvi[i].base_cost = grf_load_byte(&buf);
   425 				break;
   437 			break;
   426 
   438 
   427 			case 0x18: // AI rank
   439 		case 0x18: // AI rank
   428 				rvi->ai_rank = grf_load_byte(&buf);
   440 			FOR_EACH_OBJECT rvi[i].ai_rank = grf_load_byte(&buf);
   429 				break;
   441 			break;
   430 
   442 
   431 			case 0x19: { // Engine traction type
   443 		case 0x19: // Engine traction type
   432 				/* What do the individual numbers mean?
   444 			/* What do the individual numbers mean?
   433 				 * 0x00 .. 0x07: Steam
   445 			 * 0x00 .. 0x07: Steam
   434 				 * 0x08 .. 0x27: Diesel
   446 			 * 0x08 .. 0x27: Diesel
   435 				 * 0x28 .. 0x31: Electric
   447 			 * 0x28 .. 0x31: Electric
   436 				 * 0x32 .. 0x37: Monorail
   448 			 * 0x32 .. 0x37: Monorail
   437 				 * 0x38 .. 0x41: Maglev
   449 			 * 0x38 .. 0x41: Maglev
   438 				 */
   450 			 */
       
   451 			FOR_EACH_OBJECT {
       
   452 				uint8 traction = grf_load_byte(&buf);
   439 				uint8 traction = grf_load_byte(&buf);
   453 				EngineClass engclass;
   440 				EngineClass engclass;
   454 
   441 
   455 				if (traction <= 0x07) {
   442 				if (traction <= 0x07) {
   456 					engclass = EC_STEAM;
   443 					engclass = EC_STEAM;
   463 				} else if (traction <= 0x41) {
   450 				} else if (traction <= 0x41) {
   464 					engclass = EC_MAGLEV;
   451 					engclass = EC_MAGLEV;
   465 				} else {
   452 				} else {
   466 					break;
   453 					break;
   467 				}
   454 				}
   468 				if (rvi[i].railtype == RAILTYPE_RAIL     && engclass >= EC_ELECTRIC) rvi[i].railtype = RAILTYPE_ELECTRIC;
   455 				if (rvi->railtype == RAILTYPE_RAIL     && engclass >= EC_ELECTRIC) rvi->railtype = RAILTYPE_ELECTRIC;
   469 				if (rvi[i].railtype == RAILTYPE_ELECTRIC && engclass  < EC_ELECTRIC) rvi[i].railtype = RAILTYPE_RAIL;
   456 				if (rvi->railtype == RAILTYPE_ELECTRIC && engclass  < EC_ELECTRIC) rvi->railtype = RAILTYPE_RAIL;
   470 
   457 
   471 				rvi[i].engclass = engclass;
   458 				rvi->engclass = engclass;
   472 			}
   459 			} break;
   473 			break;
   460 
   474 
   461 			case 0x1A: { // Alter purchase list sort order
   475 		case 0x1A: // Alter purchase list sort order
       
   476 			FOR_EACH_OBJECT {
       
   477 				EngineID pos = grf_load_byte(&buf);
   462 				EngineID pos = grf_load_byte(&buf);
   478 
   463 
   479 				if (pos < NUM_TRAIN_ENGINES) {
   464 				if (pos < NUM_TRAIN_ENGINES) {
   480 					AlterRailVehListOrder(engine + i, pos);
   465 					AlterRailVehListOrder(engine + i, pos);
   481 				} else {
   466 				} else {
   482 					grfmsg(2, "RailVehicleChangeInfo: Invalid train engine ID %d, ignoring", pos);
   467 					grfmsg(2, "RailVehicleChangeInfo: Invalid train engine ID %d, ignoring", pos);
   483 				}
   468 				}
   484 			}
   469 			} break;
   485 			break;
   470 
   486 
   471 			case 0x1B: // Powered wagons power bonus
   487 		case 0x1B: // Powered wagons power bonus
   472 				rvi->pow_wag_power = grf_load_word(&buf);
   488 			FOR_EACH_OBJECT rvi[i].pow_wag_power = grf_load_word(&buf);
   473 				break;
   489 			break;
   474 
   490 
   475 			case 0x1C: // Refit cost
   491 		case 0x1C: // Refit cost
   476 				ei->refit_cost = grf_load_byte(&buf);
   492 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   477 				break;
   493 			break;
   478 
   494 
   479 			case 0x1D: // Refit cargo
   495 		case 0x1D: // Refit cargo
   480 				ei->refit_mask = grf_load_dword(&buf);
   496 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   481 				break;
   497 			break;
   482 
   498 
   483 			case 0x1E: // Callback
   499 		case 0x1E: // Callback
   484 				ei->callbackmask = grf_load_byte(&buf);
   500 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   485 				break;
   501 			break;
   486 
   502 
   487 			case 0x1F: // Tractive effort coefficient
   503 		case 0x1F: // Tractive effort coefficient
   488 				rvi->tractive_effort = grf_load_byte(&buf);
   504 			FOR_EACH_OBJECT rvi[i].tractive_effort = grf_load_byte(&buf);
   489 				break;
   505 			break;
   490 
   506 
   491 			case 0x21: // Shorter vehicle
   507 		case 0x21: // Shorter vehicle
   492 				rvi->shorten_factor = grf_load_byte(&buf);
   508 			FOR_EACH_OBJECT rvi[i].shorten_factor = grf_load_byte(&buf);
   493 				break;
   509 			break;
   494 
   510 
   495 			case 0x22: // Visual effect
   511 		case 0x22: // Visual effect
   496 				/** @see note in engine.h about rvi->visual_effect */
   512 			/* see note in engine.h about rvi->visual_effect */
   497 				rvi->visual_effect = grf_load_byte(&buf);
   513 			FOR_EACH_OBJECT rvi[i].visual_effect = grf_load_byte(&buf);
   498 				break;
   514 			break;
   499 
   515 
   500 			case 0x23: // Powered wagons weight bonus
   516 		case 0x23: // Powered wagons weight bonus
   501 				rvi->pow_wag_weight = grf_load_byte(&buf);
   517 			FOR_EACH_OBJECT rvi[i].pow_wag_weight = grf_load_byte(&buf);
   502 				break;
   518 			break;
   503 
   519 
   504 			case 0x24: { // High byte of vehicle weight
   520 		case 0x24: // High byte of vehicle weight
       
   521 			FOR_EACH_OBJECT {
       
   522 				byte weight = grf_load_byte(&buf);
   505 				byte weight = grf_load_byte(&buf);
   523 
   506 
   524 				if (weight > 4) {
   507 				if (weight > 4) {
   525 					grfmsg(2, "RailVehicleChangeInfo: Nonsensical weight of %d tons, ignoring", weight << 8);
   508 					grfmsg(2, "RailVehicleChangeInfo: Nonsensical weight of %d tons, ignoring", weight << 8);
   526 				} else {
   509 				} else {
   527 					SB(rvi[i].weight, 8, 8, weight);
   510 					SB(rvi->weight, 8, 8, weight);
   528 				}
   511 				}
   529 			}
   512 			} break;
   530 			break;
   513 
   531 
   514 			case 0x25: // User-defined bit mask to set when checking veh. var. 42
   532 		case 0x25: // User-defined bit mask to set when checking veh. var. 42
   515 				rvi->user_def_data = grf_load_byte(&buf);
   533 			FOR_EACH_OBJECT rvi[i].user_def_data = grf_load_byte(&buf);
   516 				break;
   534 			break;
   517 
   535 
   518 			case 0x26: // Retire vehicle early
   536 		case 0x26: // Retire vehicle early
   519 				ei->retire_early = grf_load_byte(&buf);
   537 			FOR_EACH_OBJECT ei[i].retire_early = grf_load_byte(&buf);
   520 				break;
   538 			break;
   521 
   539 
   522 			case 0x27: // Miscellaneous flags
   540 		case 0x27: // Miscellaneous flags
   523 				ei->misc_flags = grf_load_byte(&buf);
   541 			FOR_EACH_OBJECT {
   524 				_loaded_newgrf_features.has_2CC |= HASBIT(ei->misc_flags, EF_USES_2CC);
   542 				ei[i].misc_flags = grf_load_byte(&buf);
   525 				break;
   543 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) SETBIT(_loaded_newgrf_features, GRFLOADED_2CC);
   526 
   544 			}
   527 			case 0x28: // Cargo classes allowed
   545 			break;
   528 				cargo_allowed[engine + i] = grf_load_word(&buf);
   546 
   529 				break;
   547 		case 0x28: // Cargo classes allowed
   530 
   548 			FOR_EACH_OBJECT cargo_allowed[engine + i] = grf_load_word(&buf);
   531 			case 0x29: // Cargo classes disallowed
   549 			break;
   532 				cargo_disallowed[engine + i] = grf_load_word(&buf);
   550 
   533 				break;
   551 		case 0x29: // Cargo classes disallowed
   534 
   552 			FOR_EACH_OBJECT cargo_disallowed[engine + i] = grf_load_word(&buf);
   535 			case 0x2A: // Long format introduction date (days since year 0)
   553 			break;
   536 				ei->base_intro = grf_load_dword(&buf);
   554 
   537 				break;
   555 		case 0x2A: // Long format introduction date (days since year 0)
   538 
   556 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   539 			case 0x20: // Air drag
   557 			break;
   540 				/** @todo Air drag for trains. */
   558 
   541 				grf_load_byte(&buf);
   559 		/* @todo air drag and retire vehicle early
   542 				ret = true;
   560 		 * Fall-through for unimplemented one byte long properties. */
   543 				break;
   561 		case 0x20: // Air drag
   544 
   562 			FOR_EACH_OBJECT grf_load_byte(&buf);
   545 			default:
   563 			ret = true;
   546 				ret = true;
   564 			break;
   547 				break;
   565 
   548 		}
   566 		default:
   549 	}
   567 			ret = true;
   550 
   568 			break;
       
   569 	}
       
   570 	*bufp = buf;
   551 	*bufp = buf;
   571 	return ret;
   552 	return ret;
   572 }
   553 }
   573 
   554 
   574 static bool RoadVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   555 static bool RoadVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   575 {
   556 {
   576 	EngineInfo *ei = &_engine_info[ROAD_ENGINES_INDEX + engine];
       
   577 	RoadVehicleInfo *rvi = &_road_vehicle_info[engine];
       
   578 	byte *buf = *bufp;
   557 	byte *buf = *bufp;
   579 	int i;
       
   580 	bool ret = false;
   558 	bool ret = false;
   581 
   559 
   582 	switch (prop) {
   560 	for (int i = 0; i < numinfo; i++) {
   583 		case 0x08: // Speed (1 unit is 0.5 kmh)
   561 		EngineInfo *ei       = &_engine_info[ROAD_ENGINES_INDEX + engine + i];
   584 			FOR_EACH_OBJECT rvi[i].max_speed = grf_load_byte(&buf);
   562 		RoadVehicleInfo *rvi = &_road_vehicle_info[engine + i];
   585 			break;
   563 
   586 
   564 		switch (prop) {
   587 		case 0x09: // Running cost factor
   565 			case 0x08: // Speed (1 unit is 0.5 kmh)
   588 			FOR_EACH_OBJECT rvi[i].running_cost = grf_load_byte(&buf);
   566 				rvi->max_speed = grf_load_byte(&buf);
   589 			break;
   567 				break;
   590 
   568 
   591 		case 0x0A: // Running cost base
   569 			case 0x09: // Running cost factor
   592 			/* @todo : I have no idea. --pasky
   570 				rvi->running_cost = grf_load_byte(&buf);
   593 			 * I THINK it is used for overriding the base cost of all road vehicle (_price.roadveh_base) --belugas */
   571 				break;
   594 			FOR_EACH_OBJECT grf_load_dword(&buf);
   572 
   595 			ret = true;
   573 			case 0x0A: // Running cost base
   596 			break;
   574 				/** @todo : I have no idea. --pasky
   597 
   575 				 * I THINK it is used for overriding the base cost of all road vehicle (_price.roadveh_base) --belugas */
   598 		case 0x0E: // Sprite ID
   576 				grf_load_dword(&buf);
   599 			FOR_EACH_OBJECT {
   577 				ret = true;
       
   578 				break;
       
   579 
       
   580 			case 0x0E: { // Sprite ID
   600 				uint8 spriteid = grf_load_byte(&buf);
   581 				uint8 spriteid = grf_load_byte(&buf);
   601 
   582 
   602 				/* cars have different custom id in the GRF file */
   583 				/* cars have different custom id in the GRF file */
   603 				if (spriteid == 0xFF) spriteid = 0xFD;
   584 				if (spriteid == 0xFF) spriteid = 0xFD;
   604 
   585 
   605 				if (spriteid < 0xFD) spriteid >>= 1;
   586 				if (spriteid < 0xFD) spriteid >>= 1;
   606 
   587 
   607 				rvi[i].image_index = spriteid;
   588 				rvi->image_index = spriteid;
   608 			}
   589 			} break;
   609 			break;
   590 
   610 
   591 			case 0x0F: // Cargo capacity
   611 		case 0x0F: // Cargo capacity
   592 				rvi->capacity = grf_load_byte(&buf);
   612 			FOR_EACH_OBJECT rvi[i].capacity = grf_load_byte(&buf);
   593 				break;
   613 			break;
   594 
   614 
   595 			case 0x10: { // Cargo type
   615 		case 0x10: // Cargo type
       
   616 			FOR_EACH_OBJECT {
       
   617 				uint8 cargo = grf_load_byte(&buf);
   596 				uint8 cargo = grf_load_byte(&buf);
   618 
   597 
   619 				if (cargo < NUM_CARGO && HASBIT(_cargo_mask, cargo)) {
   598 				if (cargo < NUM_CARGO && HASBIT(_cargo_mask, cargo)) {
   620 					rvi[i].cargo_type = cargo;
   599 					rvi->cargo_type = cargo;
   621 				} else {
   600 				} else {
   622 					rvi[i].cargo_type = CT_INVALID;
   601 					rvi->cargo_type = CT_INVALID;
   623 					grfmsg(2, "RoadVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   602 					grfmsg(2, "RoadVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   624 				}
   603 				}
   625 			}
   604 			} break;
   626 			break;
   605 
   627 
   606 			case 0x11: // Cost factor
   628 		case 0x11: // Cost factor
   607 				rvi->base_cost = grf_load_byte(&buf); // ?? is it base_cost?
   629 			FOR_EACH_OBJECT rvi[i].base_cost = grf_load_byte(&buf); // ?? is it base_cost?
   608 				break;
   630 			break;
   609 
   631 
   610 			case 0x12: // SFX
   632 		case 0x12: // SFX
   611 				rvi->sfx = (SoundFx)grf_load_byte(&buf);
   633 			FOR_EACH_OBJECT rvi[i].sfx = (SoundFx)grf_load_byte(&buf);
   612 				break;
   634 			break;
   613 
   635 
   614 			case 0x13: // Power in 10hp
   636 		case 0x13: // Power in 10hp
   615 			case 0x14: // Weight in 1/4 tons
   637 		case 0x14: // Weight in 1/4 tons
   616 			case 0x15: // Speed in mph*0.8
   638 		case 0x15: // Speed in mph*0.8
   617 				/** @todo Support for road vehicles realistic power
   639 			/* TODO: Support for road vehicles realistic power
   618 				 * computations (called rvpower in TTDPatch) is just
   640 			 * computations (called rvpower in TTDPatch) is just
   619 				 * missing in OTTD yet. --pasky */
   641 			 * missing in OTTD yet. --pasky */
   620 				grf_load_byte(&buf);
   642 			FOR_EACH_OBJECT grf_load_byte(&buf);
   621 				ret = true;
   643 			ret = true;
   622 				break;
   644 			break;
   623 
   645 
   624 			case 0x16: // Cargos available for refitting
   646 		case 0x16: // Cargos available for refitting
   625 				ei->refit_mask = grf_load_dword(&buf);
   647 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   626 				break;
   648 			break;
   627 
   649 
   628 			case 0x17: // Callback mask
   650 		case 0x17: // Callback mask
   629 				ei->callbackmask = grf_load_byte(&buf);
   651 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   630 				break;
   652 			break;
   631 
   653 
   632 			case 0x1A: // Refit cost
   654 		case 0x1A: // Refit cost
   633 				ei->refit_cost = grf_load_byte(&buf);
   655 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   634 				break;
   656 			break;
   635 
   657 
   636 			case 0x1B: // Retire vehicle early
   658 		case 0x1B: // Retire vehicle early
   637 				ei->retire_early = grf_load_byte(&buf);
   659 			FOR_EACH_OBJECT ei[i].retire_early = grf_load_byte(&buf);
   638 				break;
   660 			break;
   639 
   661 
   640 			case 0x1C: // Miscellaneous flags
   662 		case 0x1C: // Miscellaneous flags
   641 				ei->misc_flags = grf_load_byte(&buf);
   663 			FOR_EACH_OBJECT {
   642 				_loaded_newgrf_features.has_2CC |= HASBIT(ei->misc_flags, EF_USES_2CC);
   664 				ei[i].misc_flags = grf_load_byte(&buf);
   643 				break;
   665 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) SETBIT(_loaded_newgrf_features, GRFLOADED_2CC);
   644 
   666 			}
   645 			case 0x1D: // Cargo classes allowed
   667 			break;
   646 				cargo_allowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   668 
   647 				break;
   669 		case 0x1D: // Cargo classes allowed
   648 
   670 			FOR_EACH_OBJECT cargo_allowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   649 			case 0x1E: // Cargo classes disallowed
   671 			break;
   650 				cargo_disallowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   672 
   651 				break;
   673 		case 0x1E: // Cargo classes disallowed
   652 
   674 			FOR_EACH_OBJECT cargo_disallowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   653 			case 0x1F: // Long format introduction date (days since year 0)
   675 			break;
   654 				ei->base_intro = grf_load_dword(&buf);
   676 
   655 				break;
   677 		case 0x1F: // Long format introduction date (days since year 0)
   656 
   678 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   657 			case 0x18: // Tractive effort
   679 			break;
   658 			case 0x19: // Air drag
   680 
   659 				/** @todo Tractive effort and air drag for road vehicles. */
   681 		case 0x18: // Tractive effort
   660 				grf_load_byte(&buf);
   682 		case 0x19: // Air drag
   661 				ret = true;
   683 			/* @todo */
   662 				break;
   684 			FOR_EACH_OBJECT grf_load_byte(&buf);
   663 
   685 			ret = true;
   664 			default:
   686 			break;
   665 				ret = true;
   687 
   666 				break;
   688 		default:
   667 		}
   689 			ret = true;
       
   690 			break;
       
   691 	}
   668 	}
   692 
   669 
   693 	*bufp = buf;
   670 	*bufp = buf;
   694 	return ret;
   671 	return ret;
   695 }
   672 }
   696 
   673 
   697 static bool ShipVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   674 static bool ShipVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   698 {
   675 {
   699 	EngineInfo *ei = &_engine_info[SHIP_ENGINES_INDEX + engine];
       
   700 	ShipVehicleInfo *svi = &_ship_vehicle_info[engine];
       
   701 	byte *buf = *bufp;
   676 	byte *buf = *bufp;
   702 	int i;
       
   703 	bool ret = false;
   677 	bool ret = false;
   704 
   678 
   705 	//printf("e %x prop %x?\n", engine, prop);
   679 	for (int i = 0; i < numinfo; i++) {
   706 	switch (prop) {
   680 		EngineInfo *ei       = &_engine_info[SHIP_ENGINES_INDEX + engine + i];
   707 		case 0x08: // Sprite ID
   681 		ShipVehicleInfo *svi = &_ship_vehicle_info[engine + i];
   708 			FOR_EACH_OBJECT {
   682 
       
   683 		switch (prop) {
       
   684 			case 0x08: { // Sprite ID
   709 				uint8 spriteid = grf_load_byte(&buf);
   685 				uint8 spriteid = grf_load_byte(&buf);
   710 
   686 
   711 				/* ships have different custom id in the GRF file */
   687 				/* ships have different custom id in the GRF file */
   712 				if (spriteid == 0xFF) spriteid = 0xFD;
   688 				if (spriteid == 0xFF) spriteid = 0xFD;
   713 
   689 
   714 				if (spriteid < 0xFD) spriteid >>= 1;
   690 				if (spriteid < 0xFD) spriteid >>= 1;
   715 
   691 
   716 				svi[i].image_index = spriteid;
   692 				svi->image_index = spriteid;
   717 			}
   693 			} break;
   718 			break;
   694 
   719 
   695 			case 0x09: // Refittable
   720 		case 0x09: // Refittable
   696 				svi->refittable = (grf_load_byte(&buf) != 0);
   721 			FOR_EACH_OBJECT svi[i].refittable = (grf_load_byte(&buf) != 0);
   697 				break;
   722 			break;
   698 
   723 
   699 			case 0x0A: // Cost factor
   724 		case 0x0A: // Cost factor
   700 				svi->base_cost = grf_load_byte(&buf); // ?? is it base_cost?
   725 			FOR_EACH_OBJECT svi[i].base_cost = grf_load_byte(&buf); // ?? is it base_cost?
   701 				break;
   726 			break;
   702 
   727 
   703 			case 0x0B: // Speed (1 unit is 0.5 kmh)
   728 		case 0x0B: // Speed (1 unit is 0.5 kmh)
   704 				svi->max_speed = grf_load_byte(&buf);
   729 			FOR_EACH_OBJECT svi[i].max_speed = grf_load_byte(&buf);
   705 				break;
   730 			break;
   706 
   731 
   707 			case 0x0C: { // Cargo type
   732 		case 0x0C: // Cargo type
       
   733 			FOR_EACH_OBJECT {
       
   734 				uint8 cargo = grf_load_byte(&buf);
   708 				uint8 cargo = grf_load_byte(&buf);
   735 
   709 
   736 				if (cargo < NUM_CARGO && HASBIT(_cargo_mask, cargo)) {
   710 				if (cargo < NUM_CARGO && HASBIT(_cargo_mask, cargo)) {
   737 					svi[i].cargo_type = cargo;
   711 					svi->cargo_type = cargo;
   738 				} else {
   712 				} else {
   739 					svi[i].cargo_type = CT_INVALID;
   713 					svi->cargo_type = CT_INVALID;
   740 					grfmsg(2, "ShipVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   714 					grfmsg(2, "ShipVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   741 				}
   715 				}
   742 			}
   716 			} break;
   743 			break;
   717 
   744 
   718 			case 0x0D: // Cargo capacity
   745 		case 0x0D: // Cargo capacity
   719 				svi->capacity = grf_load_word(&buf);
   746 			FOR_EACH_OBJECT svi[i].capacity = grf_load_word(&buf);
   720 				break;
   747 			break;
   721 
   748 
   722 			case 0x0F: // Running cost factor
   749 		case 0x0F: // Running cost factor
   723 				svi->running_cost = grf_load_byte(&buf);
   750 			FOR_EACH_OBJECT svi[i].running_cost = grf_load_byte(&buf);
   724 				break;
   751 			break;
   725 
   752 
   726 			case 0x10: // SFX
   753 		case 0x10: // SFX
   727 				svi->sfx = (SoundFx)grf_load_byte(&buf);
   754 			FOR_EACH_OBJECT svi[i].sfx = (SoundFx)grf_load_byte(&buf);
   728 				break;
   755 			break;
   729 
   756 
   730 			case 0x11: // Cargos available for refitting
   757 		case 0x11: // Cargos available for refitting
   731 				ei->refit_mask = grf_load_dword(&buf);
   758 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   732 				break;
   759 			break;
   733 
   760 
   734 			case 0x12: // Callback mask
   761 		case 0x12: // Callback mask
   735 				ei->callbackmask = grf_load_byte(&buf);
   762 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   736 				break;
   763 			break;
   737 
   764 
   738 			case 0x13: // Refit cost
   765 		case 0x13: // Refit cost
   739 				ei->refit_cost = grf_load_byte(&buf);
   766 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   740 				break;
   767 			break;
   741 
   768 
   742 			case 0x16: // Retire vehicle early
   769 		case 0x16: // Retire vehicle early
   743 				ei->retire_early = grf_load_byte(&buf);
   770 			FOR_EACH_OBJECT ei[i].retire_early = grf_load_byte(&buf);
   744 				break;
   771 			break;
   745 
   772 
   746 			case 0x17: // Miscellaneous flags
   773 		case 0x17: // Miscellaneous flags
   747 				ei->misc_flags = grf_load_byte(&buf);
   774 			FOR_EACH_OBJECT {
   748 				_loaded_newgrf_features.has_2CC |= HASBIT(ei->misc_flags, EF_USES_2CC);
   775 				ei[i].misc_flags = grf_load_byte(&buf);
   749 				break;
   776 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) SETBIT(_loaded_newgrf_features, GRFLOADED_2CC);
   750 
   777 			}
   751 			case 0x18: // Cargo classes allowed
   778 			break;
   752 				cargo_allowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   779 
   753 				break;
   780 		case 0x18: // Cargo classes allowed
   754 
   781 			FOR_EACH_OBJECT cargo_allowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   755 			case 0x19: // Cargo classes disallowed
   782 			break;
   756 				cargo_disallowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   783 
   757 				break;
   784 		case 0x19: // Cargo classes disallowed
   758 
   785 			FOR_EACH_OBJECT cargo_disallowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   759 			case 0x1A: // Long format introduction date (days since year 0)
   786 			break;
   760 				ei->base_intro = grf_load_dword(&buf);
   787 
   761 				break;
   788 		case 0x1A: // Long format introduction date (days since year 0)
   762 
   789 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   763 			case 0x14: // Ocean speed fraction
   790 			break;
   764 			case 0x15: // Canal speed fraction
   791 
   765 				/** @todo Speed fractions for ships on oceans and canals */
   792 		case 0x14: // Ocean speed fraction
   766 				grf_load_byte(&buf);
   793 		case 0x15: // Canal speed fraction
   767 				ret = true;
   794 			/* @todo */
   768 				break;
   795 			FOR_EACH_OBJECT grf_load_byte(&buf);
   769 
   796 			ret = true;
   770 			default:
   797 			break;
   771 				ret = true;
   798 
   772 				break;
   799 		default:
   773 		}
   800 			ret = true;
       
   801 			break;
       
   802 	}
   774 	}
   803 
   775 
   804 	*bufp = buf;
   776 	*bufp = buf;
   805 	return ret;
   777 	return ret;
   806 }
   778 }
   807 
   779 
   808 static bool AircraftVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   780 static bool AircraftVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   809 {
   781 {
   810 	EngineInfo *ei = &_engine_info[AIRCRAFT_ENGINES_INDEX + engine];
       
   811 	AircraftVehicleInfo *avi = &_aircraft_vehicle_info[engine];
       
   812 	byte *buf = *bufp;
   782 	byte *buf = *bufp;
   813 	int i;
       
   814 	bool ret = false;
   783 	bool ret = false;
   815 
   784 
   816 	//printf("e %x prop %x?\n", engine, prop);
   785 	for (int i = 0; i < numinfo; i++) {
   817 	switch (prop) {
   786 		EngineInfo *ei           = &_engine_info[AIRCRAFT_ENGINES_INDEX + engine + i];
   818 		case 0x08: // Sprite ID
   787 		AircraftVehicleInfo *avi = &_aircraft_vehicle_info[engine + i];
   819 			FOR_EACH_OBJECT {
   788 
       
   789 		switch (prop) {
       
   790 			case 0x08: { // Sprite ID
   820 				uint8 spriteid = grf_load_byte(&buf);
   791 				uint8 spriteid = grf_load_byte(&buf);
   821 
   792 
   822 				/* aircraft have different custom id in the GRF file */
   793 				/* aircraft have different custom id in the GRF file */
   823 				if (spriteid == 0xFF) spriteid = 0xFD;
   794 				if (spriteid == 0xFF) spriteid = 0xFD;
   824 
   795 
   825 				if (spriteid < 0xFD) spriteid >>= 1;
   796 				if (spriteid < 0xFD) spriteid >>= 1;
   826 
   797 
   827 				avi[i].image_index = spriteid;
   798 				avi->image_index = spriteid;
   828 			}
   799 			} break;
   829 			break;
   800 
   830 
   801 			case 0x09: // Helicopter
   831 		case 0x09: // Helicopter
       
   832 			FOR_EACH_OBJECT {
       
   833 				if (grf_load_byte(&buf) == 0) {
   802 				if (grf_load_byte(&buf) == 0) {
   834 					avi[i].subtype = AIR_HELI;
   803 					avi->subtype = AIR_HELI;
   835 				} else {
   804 				} else {
   836 					SB(avi[i].subtype, 0, 1, 1); // AIR_CTOL
   805 					SB(avi->subtype, 0, 1, 1); // AIR_CTOL
   837 				}
   806 				}
   838 			}
   807 				break;
   839 			break;
   808 
   840 
   809 			case 0x0A: // Large
   841 		case 0x0A: // Large
   810 				SB(avi->subtype, 1, 1, (grf_load_byte(&buf) != 0 ? 1 : 0)); // AIR_FAST
   842 			FOR_EACH_OBJECT SB(avi[i].subtype, 1, 1, (grf_load_byte(&buf) != 0 ? 1 : 0)); // AIR_FAST
   811 				break;
   843 			break;
   812 
   844 
   813 			case 0x0B: // Cost factor
   845 		case 0x0B: // Cost factor
   814 				avi->base_cost = grf_load_byte(&buf); // ?? is it base_cost?
   846 			FOR_EACH_OBJECT avi[i].base_cost = grf_load_byte(&buf); // ?? is it base_cost?
   815 				break;
   847 			break;
   816 
   848 
   817 			case 0x0C: // Speed (1 unit is 8 mph, we translate to 1 unit is 1 km/h)
   849 		case 0x0C: // Speed (1 unit is 8 mph, we translate to 1 unit is 1 km/h)
   818 				avi->max_speed = (grf_load_byte(&buf) * 129) / 10;
   850 			FOR_EACH_OBJECT avi[i].max_speed = (grf_load_byte(&buf) * 129) / 10;
   819 				break;
   851 			break;
   820 
   852 
   821 			case 0x0D: // Acceleration
   853 		case 0x0D: // Acceleration
   822 				avi->acceleration = (grf_load_byte(&buf) * 129) / 10;
   854 			FOR_EACH_OBJECT avi[i].acceleration = (grf_load_byte(&buf) * 129) / 10;
   823 				break;
   855 			break;
   824 
   856 
   825 			case 0x0E: // Running cost factor
   857 		case 0x0E: // Running cost factor
   826 				avi->running_cost = grf_load_byte(&buf);
   858 			FOR_EACH_OBJECT avi[i].running_cost = grf_load_byte(&buf);
   827 				break;
   859 			break;
   828 
   860 
   829 			case 0x0F: // Passenger capacity
   861 		case 0x0F: // Passenger capacity
   830 				avi->passenger_capacity = grf_load_word(&buf);
   862 			FOR_EACH_OBJECT avi[i].passenger_capacity = grf_load_word(&buf);
   831 				break;
   863 			break;
   832 
   864 
   833 			case 0x11: // Mail capacity
   865 		case 0x11: // Mail capacity
   834 				avi->mail_capacity = grf_load_byte(&buf);
   866 			FOR_EACH_OBJECT avi[i].mail_capacity = grf_load_byte(&buf);
   835 				break;
   867 			break;
   836 
   868 
   837 			case 0x12: // SFX
   869 		case 0x12: // SFX
   838 				avi->sfx = (SoundFx)grf_load_byte(&buf);
   870 			FOR_EACH_OBJECT avi[i].sfx = (SoundFx)grf_load_byte(&buf);
   839 				break;
   871 			break;
   840 
   872 
   841 			case 0x13: // Cargos available for refitting
   873 		case 0x13: // Cargos available for refitting
   842 				ei->refit_mask = grf_load_dword(&buf);
   874 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   843 				break;
   875 			break;
   844 
   876 
   845 			case 0x14: // Callback mask
   877 		case 0x14: // Callback mask
   846 				ei->callbackmask = grf_load_byte(&buf);
   878 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   847 				break;
   879 			break;
   848 
   880 
   849 			case 0x15: // Refit cost
   881 		case 0x15: // Refit cost
   850 				ei->refit_cost = grf_load_byte(&buf);
   882 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   851 				break;
   883 			break;
   852 
   884 
   853 			case 0x16: // Retire vehicle early
   885 		case 0x16: // Retire vehicle early
   854 				ei->retire_early = grf_load_byte(&buf);
   886 			FOR_EACH_OBJECT ei[i].retire_early = grf_load_byte(&buf);
   855 				break;
   887 			break;
   856 
   888 
   857 			case 0x17: // Miscellaneous flags
   889 		case 0x17: // Miscellaneous flags
   858 				ei->misc_flags = grf_load_byte(&buf);
   890 			FOR_EACH_OBJECT {
   859 				_loaded_newgrf_features.has_2CC |= HASBIT(ei->misc_flags, EF_USES_2CC);
   891 				ei[i].misc_flags = grf_load_byte(&buf);
   860 				break;
   892 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) SETBIT(_loaded_newgrf_features, GRFLOADED_2CC);
   861 
   893 			}
   862 			case 0x18: // Cargo classes allowed
   894 			break;
   863 				cargo_allowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   895 
   864 				break;
   896 		case 0x18: // Cargo classes allowed
   865 
   897 			FOR_EACH_OBJECT cargo_allowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   866 			case 0x19: // Cargo classes disallowed
   898 			break;
   867 				cargo_disallowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   899 
   868 				break;
   900 		case 0x19: // Cargo classes disallowed
   869 
   901 			FOR_EACH_OBJECT cargo_disallowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   870 			case 0x1A: // Long format introduction date (days since year 0)
   902 			break;
   871 				ei->base_intro = grf_load_dword(&buf);
   903 
   872 				break;
   904 		case 0x1A: // Long format introduction date (days since year 0)
   873 
   905 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   874 			default:
   906 			break;
   875 				ret = true;
   907 
   876 				break;
   908 		default:
   877 		}
   909 			ret = true;
       
   910 			break;
       
   911 	}
   878 	}
   912 
   879 
   913 	*bufp = buf;
   880 	*bufp = buf;
   914 	return ret;
   881 	return ret;
   915 }
   882 }
   916 
   883 
   917 static bool StationChangeInfo(uint stid, int numinfo, int prop, byte **bufp, int len)
   884 static bool StationChangeInfo(uint stid, int numinfo, int prop, byte **bufp, int len)
   918 {
   885 {
   919 	StationSpec **statspec;
       
   920 	byte *buf = *bufp;
   886 	byte *buf = *bufp;
   921 	int i;
       
   922 	bool ret = false;
   887 	bool ret = false;
   923 
   888 
   924 	if (stid + numinfo > MAX_STATIONS) {
   889 	if (stid + numinfo > MAX_STATIONS) {
   925 		grfmsg(1, "StationChangeInfo: Station %u is invalid, max %u, ignoring", stid + numinfo, MAX_STATIONS);
   890 		grfmsg(1, "StationChangeInfo: Station %u is invalid, max %u, ignoring", stid + numinfo, MAX_STATIONS);
   926 		return false;
   891 		return false;
   927 	}
   892 	}
   928 
   893 
   929 	/* Allocate station specs if necessary */
   894 	/* Allocate station specs if necessary */
   930 	if (_cur_grffile->stations == NULL) _cur_grffile->stations = CallocT<StationSpec*>(MAX_STATIONS);
   895 	if (_cur_grffile->stations == NULL) _cur_grffile->stations = CallocT<StationSpec*>(MAX_STATIONS);
   931 
   896 
   932 	statspec = &_cur_grffile->stations[stid];
   897 	for (int i = 0; i < numinfo; i++) {
   933 
   898 		StationSpec *statspec = _cur_grffile->stations[stid + i];
   934 	if (prop != 0x08) {
   899 
   935 		/* Check that all stations we are modifying are defined. */
   900 		/* Check that the station we are modifying is defined. */
   936 		FOR_EACH_OBJECT {
   901 		if (statspec == NULL && prop != 0x08) {
   937 			if (statspec[i] == NULL) {
   902 			grfmsg(2, "StationChangeInfo: Attempt to modify undefined station %u, ignoring", stid + i);
   938 				grfmsg(2, "StationChangeInfo: Attempt to modify undefined station %u, ignoring", stid + i);
   903 			continue;
   939 				return false;
   904 		}
   940 			}
   905 
   941 		}
   906 		switch (prop) {
   942 	}
   907 			case 0x08: { // Class ID
   943 
   908 				StationSpec **spec = &_cur_grffile->stations[stid + i];
   944 	switch (prop) {
   909 
   945 		case 0x08: // Class ID
       
   946 			FOR_EACH_OBJECT {
       
   947 				/* Property 0x08 is special; it is where the station is allocated */
   910 				/* Property 0x08 is special; it is where the station is allocated */
   948 				if (statspec[i] == NULL) statspec[i] = CallocT<StationSpec>(1);
   911 				if (*spec == NULL) *spec = CallocT<StationSpec>(1);
   949 
   912 
   950 				/* Swap classid because we read it in BE meaning WAYP or DFLT */
   913 				/* Swap classid because we read it in BE meaning WAYP or DFLT */
   951 				uint32 classid = grf_load_dword(&buf);
   914 				uint32 classid = grf_load_dword(&buf);
   952 				statspec[i]->sclass = AllocateStationClass(BSWAP32(classid));
   915 				(*spec)->sclass = AllocateStationClass(BSWAP32(classid));
   953 			}
   916 			} break;
   954 			break;
   917 
   955 
   918 			case 0x09: // Define sprite layout
   956 		case 0x09: // Define sprite layout
       
   957 			FOR_EACH_OBJECT {
       
   958 				StationSpec *statspec = _cur_grffile->stations[stid + i];
       
   959 
       
   960 				statspec->tiles = grf_load_extended(&buf);
   919 				statspec->tiles = grf_load_extended(&buf);
   961 				statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
   920 				statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
   962 				statspec->copied_renderdata = false;
   921 				statspec->copied_renderdata = false;
   963 
   922 
   964 				for (uint t = 0; t < statspec->tiles; t++) {
   923 				for (uint t = 0; t < statspec->tiles; t++) {
  1005 							CLRBIT(dtss->image, 14);
   964 							CLRBIT(dtss->image, 14);
  1006 							SETBIT(dtss->image, PALETTE_MODIFIER_TRANSPARENT);
   965 							SETBIT(dtss->image, PALETTE_MODIFIER_TRANSPARENT);
  1007 						}
   966 						}
  1008 					}
   967 					}
  1009 				}
   968 				}
  1010 			}
   969 				break;
  1011 			break;
   970 
  1012 
   971 			case 0x0A: { // Copy sprite layout
  1013 		case 0x0A: // Copy sprite layout
       
  1014 			FOR_EACH_OBJECT {
       
  1015 				StationSpec *statspec = _cur_grffile->stations[stid + i];
       
  1016 				byte srcid = grf_load_byte(&buf);
   972 				byte srcid = grf_load_byte(&buf);
  1017 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
   973 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  1018 
   974 
  1019 				statspec->tiles = srcstatspec->tiles;
   975 				statspec->tiles = srcstatspec->tiles;
  1020 				statspec->renderdata = srcstatspec->renderdata;
   976 				statspec->renderdata = srcstatspec->renderdata;
  1021 				statspec->copied_renderdata = true;
   977 				statspec->copied_renderdata = true;
  1022 			}
   978 			} break;
  1023 			break;
   979 
  1024 
   980 			case 0x0B: // Callback mask
  1025 		case 0x0B: // Callback mask
   981 				statspec->callbackmask = grf_load_byte(&buf);
  1026 			FOR_EACH_OBJECT statspec[i]->callbackmask = grf_load_byte(&buf);
   982 				break;
  1027 			break;
   983 
  1028 
   984 			case 0x0C: // Disallowed number of platforms
  1029 		case 0x0C: // Disallowed number of platforms
   985 				statspec->disallowed_platforms = grf_load_byte(&buf);
  1030 			FOR_EACH_OBJECT statspec[i]->disallowed_platforms = grf_load_byte(&buf);
   986 				break;
  1031 			break;
   987 
  1032 
   988 			case 0x0D: // Disallowed platform lengths
  1033 		case 0x0D: // Disallowed platform lengths
   989 				statspec->disallowed_lengths = grf_load_byte(&buf);
  1034 			FOR_EACH_OBJECT statspec[i]->disallowed_lengths = grf_load_byte(&buf);
   990 				break;
  1035 			break;
   991 
  1036 
   992 			case 0x0E: // Define custom layout
  1037 		case 0x0E: // Define custom layout
       
  1038 			FOR_EACH_OBJECT {
       
  1039 				StationSpec *statspec = _cur_grffile->stations[stid + i];
       
  1040 
       
  1041 				statspec->copied_layouts = false;
   993 				statspec->copied_layouts = false;
  1042 
   994 
  1043 				while (buf < *bufp + len) {
   995 				while (buf < *bufp + len) {
  1044 					byte length = grf_load_byte(&buf);
   996 					byte length = grf_load_byte(&buf);
  1045 					byte number = grf_load_byte(&buf);
   997 					byte number = grf_load_byte(&buf);
  1082 					l--;
  1034 					l--;
  1083 					p--;
  1035 					p--;
  1084 					free(statspec->layouts[l][p]);
  1036 					free(statspec->layouts[l][p]);
  1085 					statspec->layouts[l][p] = layout;
  1037 					statspec->layouts[l][p] = layout;
  1086 				}
  1038 				}
  1087 			}
  1039 				break;
  1088 			break;
  1040 
  1089 
  1041 			case 0x0F: { // Copy custom layout
  1090 		case 0x0F: // Copy custom layout
       
  1091 			FOR_EACH_OBJECT {
       
  1092 				StationSpec *statspec = _cur_grffile->stations[stid + i];
       
  1093 				byte srcid = grf_load_byte(&buf);
  1042 				byte srcid = grf_load_byte(&buf);
  1094 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  1043 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  1095 
  1044 
  1096 				statspec->lengths   = srcstatspec->lengths;
  1045 				statspec->lengths   = srcstatspec->lengths;
  1097 				statspec->platforms = srcstatspec->platforms;
  1046 				statspec->platforms = srcstatspec->platforms;
  1098 				statspec->layouts   = srcstatspec->layouts;
  1047 				statspec->layouts   = srcstatspec->layouts;
  1099 				statspec->copied_layouts = true;
  1048 				statspec->copied_layouts = true;
  1100 			}
  1049 			} break;
  1101 			break;
  1050 
  1102 
  1051 			case 0x10: // Little/lots cargo threshold
  1103 		case 0x10: // Little/lots cargo threshold
  1052 				statspec->cargo_threshold = grf_load_word(&buf);
  1104 			FOR_EACH_OBJECT statspec[i]->cargo_threshold = grf_load_word(&buf);
  1053 				break;
  1105 			break;
  1054 
  1106 
  1055 			case 0x11: // Pylon placement
  1107 		case 0x11: // Pylon placement
  1056 				statspec->pylons = grf_load_byte(&buf);
  1108 			FOR_EACH_OBJECT statspec[i]->pylons = grf_load_byte(&buf);
  1057 				break;
  1109 			break;
  1058 
  1110 
  1059 			case 0x12: // Cargo types for random triggers
  1111 		case 0x12: // Cargo types for random triggers
  1060 				statspec->cargo_triggers = grf_load_dword(&buf);
  1112 			FOR_EACH_OBJECT statspec[i]->cargo_triggers = grf_load_dword(&buf);
  1061 				break;
  1113 			break;
  1062 
  1114 
  1063 			case 0x13: // General flags
  1115 		case 0x13: // General flags
  1064 				statspec->flags = grf_load_byte(&buf);
  1116 			FOR_EACH_OBJECT statspec[i]->flags = grf_load_byte(&buf);
  1065 				break;
  1117 			break;
  1066 
  1118 
  1067 			case 0x14: // Overhead wire placement
  1119 		case 0x14: // Overhead wire placement
  1068 				statspec->wires = grf_load_byte(&buf);
  1120 			FOR_EACH_OBJECT statspec[i]->wires = grf_load_byte(&buf);
  1069 				break;
  1121 			break;
  1070 
  1122 
  1071 			case 0x15: // Blocked tiles
  1123 		case 0x15: // Blocked tiles
  1072 				statspec->blocked = grf_load_byte(&buf);
  1124 			FOR_EACH_OBJECT statspec[i]->blocked = grf_load_byte(&buf);
  1073 				break;
  1125 			break;
  1074 
  1126 
  1075 			case 0x16: // @todo Animation info
  1127 		case 0x16: // @todo Animation info
  1076 				grf_load_word(&buf);
  1128 			FOR_EACH_OBJECT grf_load_word(&buf);
  1077 				ret = true;
  1129 			ret = true;
  1078 				break;
  1130 			break;
  1079 
  1131 
  1080 			case 0x17: // @todo Animation speed
  1132 		case 0x17: // @todo Animation speed
  1081 				grf_load_byte(&buf);
  1133 			FOR_EACH_OBJECT grf_load_byte(&buf);
  1082 				ret = true;
  1134 			ret = true;
  1083 				break;
  1135 			break;
  1084 
  1136 
  1085 			case 0x18: // @todo Animation triggers
  1137 		case 0x18: // @todo Animation triggers
  1086 				grf_load_word(&buf);
  1138 			FOR_EACH_OBJECT grf_load_word(&buf);
  1087 				ret = true;
  1139 			ret = true;
  1088 				break;
  1140 			break;
  1089 
  1141 
  1090 			default:
  1142 		default:
  1091 				ret = true;
  1143 			ret = true;
  1092 				break;
  1144 			break;
  1093 		}
  1145 	}
  1094 	}
  1146 
  1095 
  1147 	*bufp = buf;
  1096 	*bufp = buf;
  1148 	return ret;
  1097 	return ret;
  1149 }
  1098 }
  1150 
  1099 
  1151 static bool BridgeChangeInfo(uint brid, int numinfo, int prop, byte **bufp, int len)
  1100 static bool BridgeChangeInfo(uint brid, int numinfo, int prop, byte **bufp, int len)
  1152 {
  1101 {
  1153 	byte *buf = *bufp;
  1102 	byte *buf = *bufp;
  1154 	int i;
       
  1155 	bool ret = false;
  1103 	bool ret = false;
  1156 
  1104 
  1157 	switch (prop) {
  1105 	if (brid + numinfo > MAX_BRIDGES) {
  1158 		case 0x08: // Year of availability
  1106 		grfmsg(1, "BridgeChangeInfo: Bridge %u is invalid, max %u, ignoring", brid + numinfo, MAX_BRIDGES);
  1159 			FOR_EACH_OBJECT _bridge[brid + i].avail_year = ORIGINAL_BASE_YEAR + grf_load_byte(&buf);
  1107 		return false;
  1160 			break;
  1108 	}
  1161 
  1109 
  1162 		case 0x09: // Minimum length
  1110 	for (int i = 0; i < numinfo; i++) {
  1163 			FOR_EACH_OBJECT _bridge[brid + i].min_length = grf_load_byte(&buf);
  1111 		Bridge *bridge = &_bridge[brid + i];
  1164 			break;
  1112 
  1165 
  1113 		switch (prop) {
  1166 		case 0x0A: // Maximum length
  1114 			case 0x08: // Year of availability
  1167 			FOR_EACH_OBJECT _bridge[brid + i].max_length = grf_load_byte(&buf);
  1115 				bridge->avail_year = ORIGINAL_BASE_YEAR + grf_load_byte(&buf);
  1168 			break;
  1116 				break;
  1169 
  1117 
  1170 		case 0x0B: // Cost factor
  1118 			case 0x09: // Minimum length
  1171 			FOR_EACH_OBJECT _bridge[brid + i].price = grf_load_byte(&buf);
  1119 				bridge->min_length = grf_load_byte(&buf);
  1172 			break;
  1120 				break;
  1173 
  1121 
  1174 		case 0x0C: // Maximum speed
  1122 			case 0x0A: // Maximum length
  1175 			FOR_EACH_OBJECT _bridge[brid + i].speed = grf_load_word(&buf);
  1123 				bridge->max_length = grf_load_byte(&buf);
  1176 			break;
  1124 				break;
  1177 
  1125 
  1178 		case 0x0D: // Bridge sprite tables
  1126 			case 0x0B: // Cost factor
  1179 			FOR_EACH_OBJECT {
  1127 				bridge->price = grf_load_byte(&buf);
  1180 				Bridge *bridge = &_bridge[brid + i];
  1128 				break;
       
  1129 
       
  1130 			case 0x0C: // Maximum speed
       
  1131 				bridge->speed = grf_load_word(&buf);
       
  1132 				break;
       
  1133 
       
  1134 			case 0x0D: { // Bridge sprite tables
  1181 				byte tableid = grf_load_byte(&buf);
  1135 				byte tableid = grf_load_byte(&buf);
  1182 				byte numtables = grf_load_byte(&buf);
  1136 				byte numtables = grf_load_byte(&buf);
  1183 
  1137 
  1184 				if (bridge->sprite_table == NULL) {
  1138 				if (bridge->sprite_table == NULL) {
  1185 					/* Allocate memory for sprite table pointers and zero out */
  1139 					/* Allocate memory for sprite table pointers and zero out */
  1210 
  1164 
  1211 						bridge->sprite_table[tableid][sprite].sprite = image;
  1165 						bridge->sprite_table[tableid][sprite].sprite = image;
  1212 						bridge->sprite_table[tableid][sprite].pal    = pal;
  1166 						bridge->sprite_table[tableid][sprite].pal    = pal;
  1213 					}
  1167 					}
  1214 				}
  1168 				}
  1215 			}
  1169 			} break;
  1216 			break;
  1170 
  1217 
  1171 			case 0x0E: // Flags; bit 0 - disable far pillars
  1218 		case 0x0E: // Flags; bit 0 - disable far pillars
  1172 				bridge->flags = grf_load_byte(&buf);
  1219 			FOR_EACH_OBJECT _bridge[brid + i].flags = grf_load_byte(&buf);
  1173 				break;
  1220 			break;
  1174 
  1221 
  1175 			case 0x0F: // Long format year of availability (year since year 0)
  1222 		case 0x0F: // Long format year of availability (year since year 0)
  1176 				bridge->avail_year = clamp(grf_load_dword(&buf), MIN_YEAR, MAX_YEAR);
  1223 			FOR_EACH_OBJECT _bridge[brid + i].avail_year = clamp(grf_load_dword(&buf), MIN_YEAR, MAX_YEAR);
  1177 				break;
  1224 			break;
  1178 
  1225 
  1179 			default:
  1226 		default:
  1180 				ret = true;
  1227 			ret = true;
  1181 				break;
       
  1182 		}
  1228 	}
  1183 	}
  1229 
  1184 
  1230 	*bufp = buf;
  1185 	*bufp = buf;
  1231 	return ret;
  1186 	return ret;
  1232 }
  1187 }
  1233 
  1188 
  1234 static bool TownHouseChangeInfo(uint hid, int numinfo, int prop, byte **bufp, int len)
  1189 static bool TownHouseChangeInfo(uint hid, int numinfo, int prop, byte **bufp, int len)
  1235 {
  1190 {
  1236 	HouseSpec **housespec;
       
  1237 	byte *buf = *bufp;
  1191 	byte *buf = *bufp;
  1238 	int i;
       
  1239 	bool ret = false;
  1192 	bool ret = false;
  1240 
  1193 
  1241 	if (hid + numinfo >= HOUSE_MAX) {
  1194 	if (hid + numinfo > HOUSE_MAX) {
  1242 		grfmsg(1, "TownHouseChangeInfo: Too many houses loaded (%u), max (%u). Ignoring.", hid + numinfo, HOUSE_MAX-1);
  1195 		grfmsg(1, "TownHouseChangeInfo: Too many houses loaded (%u), max (%u). Ignoring.", hid + numinfo, HOUSE_MAX);
  1243 		return false;
  1196 		return false;
  1244 	}
  1197 	}
  1245 
  1198 
  1246 	/* Allocate house specs if they haven't been allocated already. */
  1199 	/* Allocate house specs if they haven't been allocated already. */
  1247 	if (_cur_grffile->housespec == NULL) {
  1200 	if (_cur_grffile->housespec == NULL) {
  1249 
  1202 
  1250 		/* Reset any overrides that have been set. */
  1203 		/* Reset any overrides that have been set. */
  1251 		_house_mngr.ResetOverride();
  1204 		_house_mngr.ResetOverride();
  1252 	}
  1205 	}
  1253 
  1206 
  1254 	housespec = &_cur_grffile->housespec[hid];
  1207 	for (int i = 0; i < numinfo; i++) {
  1255 
  1208 		HouseSpec *housespec = _cur_grffile->housespec[hid + i];
  1256 	if (prop != 0x08) {
  1209 
  1257 		/* Check that all the houses being modified have been defined. */
  1210 		if (prop != 0x08 && housespec == NULL) {
  1258 		FOR_EACH_OBJECT {
  1211 			grfmsg(2, "TownHouseChangeInfo: Attempt to modify undefined house %u. Ignoring.", hid + i);
  1259 			if (housespec[i] == NULL) {
  1212 			continue;
  1260 				grfmsg(2, "TownHouseChangeInfo: Attempt to modify undefined house %u. Ignoring.", hid + i);
  1213 		}
  1261 				return false;
  1214 
  1262 			}
  1215 		switch (prop) {
  1263 		}
  1216 			case 0x08: { // Substitute building type, and definition of a new house
  1264 	}
  1217 				HouseSpec **house = &_cur_grffile->housespec[hid + i];
  1265 
       
  1266 	switch (prop) {
       
  1267 		case 0x08: // Substitute building type, and definition of a new house
       
  1268 			FOR_EACH_OBJECT {
       
  1269 				byte subs_id = grf_load_byte(&buf);
  1218 				byte subs_id = grf_load_byte(&buf);
  1270 
  1219 
  1271 				if (subs_id == 0xFF) {
  1220 				if (subs_id == 0xFF) {
  1272 					/* Instead of defining a new house, a substitute house id
  1221 					/* Instead of defining a new house, a substitute house id
  1273 					 * of 0xFF disables the old house with the current id. */
  1222 					 * of 0xFF disables the old house with the current id. */
  1278 					grfmsg(2, "TownHouseChangeInfo: Attempt to use new house %u as substitute house for %u. Ignoring.", subs_id, hid + i);
  1227 					grfmsg(2, "TownHouseChangeInfo: Attempt to use new house %u as substitute house for %u. Ignoring.", subs_id, hid + i);
  1279 					return false;
  1228 					return false;
  1280 				}
  1229 				}
  1281 
  1230 
  1282 				/* Allocate space for this house. */
  1231 				/* Allocate space for this house. */
  1283 				if (housespec[i] == NULL) housespec[i] = CallocT<HouseSpec>(1);
  1232 				if (*house == NULL) *house = CallocT<HouseSpec>(1);
  1284 
  1233 
  1285 				memcpy(housespec[i], &_house_specs[subs_id], sizeof(_house_specs[subs_id]));
  1234 				housespec = *house;
  1286 
  1235 
  1287 				housespec[i]->enabled = true;
  1236 				memcpy(housespec, &_house_specs[subs_id], sizeof(_house_specs[subs_id]));
  1288 				housespec[i]->local_id = hid + i;
  1237 
  1289 				housespec[i]->substitute_id = subs_id;
  1238 				housespec->enabled = true;
  1290 				housespec[i]->grffile = _cur_grffile;
  1239 				housespec->local_id = hid + i;
  1291 				housespec[i]->random_colour[0] = 0x04;  // those 4 random colours are the base colour
  1240 				housespec->substitute_id = subs_id;
  1292 				housespec[i]->random_colour[1] = 0x08;  // for all new houses
  1241 				housespec->grffile = _cur_grffile;
  1293 				housespec[i]->random_colour[2] = 0x0C;  // they stand for red, blue, orange and green
  1242 				housespec->random_colour[0] = 0x04;  // those 4 random colours are the base colour
  1294 				housespec[i]->random_colour[3] = 0x06;
  1243 				housespec->random_colour[1] = 0x08;  // for all new houses
  1295 
  1244 				housespec->random_colour[2] = 0x0C;  // they stand for red, blue, orange and green
  1296 				/* New houses do not (currently) expect to have a default start
  1245 				housespec->random_colour[3] = 0x06;
  1297 				 * date before 1930, as this breaks the build date stuff. See
  1246 
  1298 				 * FinaliseHouseArray() for more details. */
  1247 				/**
  1299 				if (housespec[i]->min_date < 1930) housespec[i]->min_date = 1930;
  1248 				 * New houses do not (currently) expect to have a default start
  1300 			}
  1249 				 * date before 1930, as this breaks the build date stuff.
  1301 			SETBIT(_loaded_newgrf_features, GRFLOADED_NEWHOUSES);
  1250 				 * @see FinaliseHouseArray() for more details.
  1302 			break;
  1251 				 */
  1303 
  1252 				if (housespec->min_date < 1930) housespec->min_date = 1930;
  1304 		case 0x09: // Building flags
  1253 
  1305 			FOR_EACH_OBJECT {
  1254 				_loaded_newgrf_features.has_newhouses = true;
  1306 				byte state = grf_load_byte(&buf);
  1255 			} break;
  1307 				housespec[i]->building_flags = (BuildingFlags)state;
  1256 
  1308 			}
  1257 			case 0x09: // Building flags
  1309 			break;
  1258 				housespec->building_flags = (BuildingFlags)grf_load_byte(&buf);
  1310 
  1259 				break;
  1311 		case 0x0A: // Availability years
  1260 
  1312 			FOR_EACH_OBJECT {
  1261 			case 0x0A: { // Availability years
  1313 				uint16 years = grf_load_word(&buf);
  1262 				uint16 years = grf_load_word(&buf);
  1314 				housespec[i]->min_date = GB(years, 0, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 0, 8);
  1263 				housespec->min_date = GB(years, 0, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 0, 8);
  1315 				housespec[i]->max_date = GB(years, 8, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 8, 8);
  1264 				housespec->max_date = GB(years, 8, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 8, 8);
  1316 			}
  1265 			} break;
  1317 			break;
  1266 
  1318 
  1267 			case 0x0B: // Population
  1319 		case 0x0B: // Population
  1268 				housespec->population = grf_load_byte(&buf);
  1320 			FOR_EACH_OBJECT housespec[i]->population = grf_load_byte(&buf);
  1269 				break;
  1321 			break;
  1270 
  1322 
  1271 			case 0x0C: // Mail generation multiplier
  1323 		case 0x0C: // Mail generation multiplier
  1272 				housespec->mail_generation = grf_load_byte(&buf);
  1324 			FOR_EACH_OBJECT housespec[i]->mail_generation = grf_load_byte(&buf);
  1273 				break;
  1325 			break;
  1274 
  1326 
  1275 			case 0x0D: // Passenger acceptance
  1327 		case 0x0D: // Passenger acceptance
  1276 			case 0x0E: // Mail acceptance
  1328 		case 0x0E: // Mail acceptance
  1277 				housespec->cargo_acceptance[prop - 0x0D] = grf_load_byte(&buf);
  1329 			FOR_EACH_OBJECT housespec[i]->cargo_acceptance[prop - 0x0D] = grf_load_byte(&buf);
  1278 				break;
  1330 			break;
  1279 
  1331 		case 0x0F: // Goods/candy, food/fizzy drinks acceptance
  1280 			case 0x0F: { // Goods/candy, food/fizzy drinks acceptance
  1332 			FOR_EACH_OBJECT {
       
  1333 				int8 goods = grf_load_byte(&buf);
  1281 				int8 goods = grf_load_byte(&buf);
  1334 
  1282 
  1335 				/* If value of goods is negative, it means in fact food or, if in toyland, fizzy_drink acceptance.
  1283 				/* If value of goods is negative, it means in fact food or, if in toyland, fizzy_drink acceptance.
  1336 				 * Else, we have "standard" 3rd cargo type, goods or candy, for toyland once more */
  1284 				 * Else, we have "standard" 3rd cargo type, goods or candy, for toyland once more */
  1337 				housespec[i]->accepts_cargo[2] = (goods >= 0) ? ((_opt.landscape == LT_TOYLAND) ? CT_CANDY : CT_GOODS) :
  1285 				housespec->accepts_cargo[2] = (goods >= 0) ? ((_opt.landscape == LT_TOYLAND) ? CT_CANDY : CT_GOODS) :
  1338 						((_opt.landscape == LT_TOYLAND) ? CT_FIZZY_DRINKS : CT_FOOD);
  1286 						((_opt.landscape == LT_TOYLAND) ? CT_FIZZY_DRINKS : CT_FOOD);
  1339 
  1287 
  1340 				housespec[i]->cargo_acceptance[2] = abs(goods); // but we do need positive value here
  1288 				housespec->cargo_acceptance[2] = abs(goods); // but we do need positive value here
  1341 			}
  1289 			} break;
  1342 			break;
  1290 
  1343 
  1291 			case 0x10: // Local authority rating decrease on removal
  1344 		case 0x10: // Local authority rating decrease on removal
  1292 				housespec->remove_rating_decrease = grf_load_word(&buf);
  1345 			FOR_EACH_OBJECT housespec[i]->remove_rating_decrease = grf_load_word(&buf);
  1293 				break;
  1346 			break;
  1294 
  1347 
  1295 			case 0x11: // Removal cost multiplier
  1348 		case 0x11: // Removal cost multiplier
  1296 				housespec->removal_cost = grf_load_byte(&buf);
  1349 			FOR_EACH_OBJECT housespec[i]->removal_cost = grf_load_byte(&buf);
  1297 				break;
  1350 			break;
  1298 
  1351 
  1299 			case 0x12: // Building name ID
  1352 		case 0x12: // Building name ID
  1300 				housespec->building_name = MapGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
  1353 			FOR_EACH_OBJECT housespec[i]->building_name = MapGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
  1301 				break;
  1354 			break;
  1302 
  1355 
  1303 			case 0x13: // Building availability mask
  1356 		case 0x13: // Building availability mask
  1304 				housespec->building_availability = (HouseZones)grf_load_word(&buf);
  1357 			FOR_EACH_OBJECT {
  1305 				break;
  1358 				uint16 avail = grf_load_word(&buf);
  1306 
  1359 				housespec[i]->building_availability = (HouseZones)avail;
  1307 			case 0x14: // House callback flags
  1360 			}
  1308 				housespec->callback_mask = grf_load_byte(&buf);
  1361 			break;
  1309 				break;
  1362 
  1310 
  1363 		case 0x14: // House callback flags
  1311 			case 0x15: { // House override byte
  1364 			FOR_EACH_OBJECT housespec[i]->callback_mask = grf_load_byte(&buf);
       
  1365 			break;
       
  1366 
       
  1367 		case 0x15: // House override byte
       
  1368 			FOR_EACH_OBJECT {
       
  1369 				byte override = grf_load_byte(&buf);
  1312 				byte override = grf_load_byte(&buf);
  1370 
  1313 
  1371 				/* The house being overridden must be an original house. */
  1314 				/* The house being overridden must be an original house. */
  1372 				if (override >= NEW_HOUSE_OFFSET) {
  1315 				if (override >= NEW_HOUSE_OFFSET) {
  1373 					grfmsg(2, "TownHouseChangeInfo: Attempt to override new house %u with house id %u. Ignoring.", override, hid);
  1316 					grfmsg(2, "TownHouseChangeInfo: Attempt to override new house %u with house id %u. Ignoring.", override, hid + i);
  1374 					return false;
  1317 					continue;
  1375 				}
  1318 				}
  1376 
  1319 
  1377 				_house_mngr.Add(hid, override);
  1320 				_house_mngr.Add(hid + i, override);
  1378 			}
  1321 			} break;
  1379 			break;
  1322 
  1380 
  1323 			case 0x16: // Periodic refresh multiplier
  1381 		case 0x16: // Periodic refresh multiplier
  1324 				housespec->processing_time = grf_load_byte(&buf);
  1382 			FOR_EACH_OBJECT housespec[i]->processing_time = grf_load_byte(&buf);
  1325 				break;
  1383 			break;
  1326 
  1384 
  1327 			case 0x17: // Four random colours to use
  1385 		case 0x17: // Four random colours to use
  1328 				for (uint j = 0; j < 4; j++) housespec->random_colour[j] = grf_load_byte(&buf);
  1386 			FOR_EACH_OBJECT {
  1329 				break;
  1387 				uint j;
  1330 
  1388 				for (j = 0; j < 4; j++) housespec[i]->random_colour[j] = grf_load_byte(&buf);
  1331 			case 0x18: // Relative probability of appearing
  1389 			}
  1332 				housespec->probability = grf_load_byte(&buf);
  1390 			break;
  1333 				break;
  1391 
  1334 
  1392 		case 0x18: // Relative probability of appearing
  1335 			case 0x19: // Extra flags
  1393 			FOR_EACH_OBJECT housespec[i]->probability = grf_load_byte(&buf);
  1336 				housespec->extra_flags = (HouseExtraFlags)grf_load_byte(&buf);
  1394 			break;
  1337 				break;
  1395 
  1338 
  1396 		case 0x19: // Extra flags
  1339 			case 0x1A: // Animation frames
  1397 			FOR_EACH_OBJECT {
  1340 				housespec->animation_frames = grf_load_byte(&buf);
  1398 				byte flags = grf_load_byte(&buf);
  1341 				break;
  1399 				housespec[i]->extra_flags = (HouseExtraFlags)flags;
  1342 
  1400 			}
  1343 			case 0x1B: // Animation speed
  1401 			break;
  1344 				housespec->animation_speed = clamp(grf_load_byte(&buf), 2, 16);
  1402 
  1345 				break;
  1403 		case 0x1A: // Animation frames
  1346 
  1404 			FOR_EACH_OBJECT housespec[i]->animation_frames = grf_load_byte(&buf);
  1347 			case 0x1C: // Class of the building type
  1405 			break;
  1348 				housespec->class_id = AllocateHouseClassID(grf_load_byte(&buf), _cur_grffile->grfid);
  1406 
  1349 				break;
  1407 		case 0x1B: // Animation speed
  1350 
  1408 			FOR_EACH_OBJECT housespec[i]->animation_speed = clamp(grf_load_byte(&buf), 2, 16);
  1351 			case 0x1D: // Callback flags 2
  1409 			break;
  1352 				housespec->callback_mask |= (grf_load_byte(&buf) << 8);
  1410 
  1353 				break;
  1411 		case 0x1C: // Class of the building type
  1354 
  1412 			FOR_EACH_OBJECT housespec[i]->class_id = AllocateHouseClassID(grf_load_byte(&buf), _cur_grffile->grfid);
  1355 			case 0x1E: { // Accepted cargo types
  1413 			break;
       
  1414 
       
  1415 		case 0x1D: // Callback flags 2
       
  1416 			FOR_EACH_OBJECT housespec[i]->callback_mask |= (grf_load_byte(&buf) << 8);
       
  1417 			break;
       
  1418 
       
  1419 		case 0x1E: // Accepted cargo types
       
  1420 			FOR_EACH_OBJECT {
       
  1421 				uint32 cargotypes = grf_load_dword(&buf);
  1356 				uint32 cargotypes = grf_load_dword(&buf);
  1422 
  1357 
  1423 				/* Check if the cargo types should not be changed */
  1358 				/* Check if the cargo types should not be changed */
  1424 				if (cargotypes == 0xFFFFFFFF) break;
  1359 				if (cargotypes == 0xFFFFFFFF) break;
  1425 
  1360 
  1428 					uint8 cargo_part = GB(cargotypes, 8 * j, 8);
  1363 					uint8 cargo_part = GB(cargotypes, 8 * j, 8);
  1429 					CargoID cargo = GetCargoTranslation(cargo_part, _cur_grffile);
  1364 					CargoID cargo = GetCargoTranslation(cargo_part, _cur_grffile);
  1430 
  1365 
  1431 					if (cargo == CT_INVALID) {
  1366 					if (cargo == CT_INVALID) {
  1432 						/* Disable acceptance of invalid cargo type */
  1367 						/* Disable acceptance of invalid cargo type */
  1433 						housespec[i]->cargo_acceptance[j] = 0;
  1368 						housespec->cargo_acceptance[j] = 0;
  1434 					} else {
  1369 					} else {
  1435 						housespec[i]->accepts_cargo[j] = cargo;
  1370 						housespec->accepts_cargo[j] = cargo;
  1436 					}
  1371 					}
  1437 				}
  1372 				}
  1438 			}
  1373 			} break;
  1439 			break;
  1374 
  1440 
  1375 			case 0x1F: // Minimum life span
  1441 		case 0x1F: // Minimum life span
  1376 				housespec->minimum_life = grf_load_byte(&buf);
  1442 			FOR_EACH_OBJECT housespec[i]->minimum_life = grf_load_byte(&buf);
  1377 				break;
  1443 			break;
  1378 
  1444 
  1379 			default:
  1445 		default:
  1380 				ret = true;
  1446 			ret = true;
  1381 				break;
  1447 			break;
  1382 		}
  1448 	}
  1383 	}
  1449 
  1384 
  1450 	*bufp = buf;
  1385 	*bufp = buf;
  1451 	return ret;
  1386 	return ret;
  1452 }
  1387 }
  1453 
  1388 
  1454 static bool GlobalVarChangeInfo(uint gvid, int numinfo, int prop, byte **bufp, int len)
  1389 static bool GlobalVarChangeInfo(uint gvid, int numinfo, int prop, byte **bufp, int len)
  1455 {
  1390 {
  1456 	byte *buf = *bufp;
  1391 	byte *buf = *bufp;
  1457 	int i;
       
  1458 	bool ret = false;
  1392 	bool ret = false;
  1459 
  1393 
  1460 	switch (prop) {
  1394 	for (int i = 0; i < numinfo; i++) {
  1461 		case 0x08: /* Cost base factor */
  1395 		switch (prop) {
  1462 			FOR_EACH_OBJECT {
  1396 			case 0x08: { // Cost base factor
  1463 				byte factor = grf_load_byte(&buf);
  1397 				byte factor = grf_load_byte(&buf);
  1464 				uint price = gvid + i;
  1398 				uint price = gvid + i;
  1465 
  1399 
  1466 				if (price < NUM_PRICES) {
  1400 				if (price < NUM_PRICES) {
  1467 					SetPriceBaseMultiplier(price, factor);
  1401 					SetPriceBaseMultiplier(price, factor);
  1468 				} else {
  1402 				} else {
  1469 					grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
  1403 					grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
  1470 				}
  1404 				}
  1471 			}
  1405 			} break;
  1472 			break;
  1406 
  1473 
  1407 			case 0x09: // Cargo translation table
  1474 		case 0x09: /* Cargo translation table */
  1408 				/* This is loaded during the initialisation stage, so just skip it here. */
  1475 			/* This is loaded during the initialisation stage, so just skip it here. */
  1409 				/* Each entry is 4 bytes. */
  1476 			/* Each entry is 4 bytes. */
  1410 				buf += 4;
  1477 			buf += numinfo * 4;
  1411 				break;
  1478 			break;
  1412 
  1479 
  1413 			case 0x0A: { // Currency display names
  1480 		case 0x0A: // Currency display names
       
  1481 			FOR_EACH_OBJECT {
       
  1482 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1414 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1483 				StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
  1415 				StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
  1484 
  1416 
  1485 				if ((newone != STR_UNDEFINED) && (curidx < NUM_CURRENCY)) {
  1417 				if ((newone != STR_UNDEFINED) && (curidx < NUM_CURRENCY)) {
  1486 					_currency_specs[curidx].name = newone;
  1418 					_currency_specs[curidx].name = newone;
  1487 				}
  1419 				}
  1488 			}
  1420 			} break;
  1489 			break;
  1421 
  1490 
  1422 			case 0x0B: { // Currency multipliers
  1491 		case 0x0B: // Currency multipliers
       
  1492 			FOR_EACH_OBJECT {
       
  1493 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1423 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1494 				uint32 rate = grf_load_dword(&buf);
  1424 				uint32 rate = grf_load_dword(&buf);
  1495 
  1425 
  1496 				if (curidx < NUM_CURRENCY) {
  1426 				if (curidx < NUM_CURRENCY) {
  1497 					/* TTDPatch uses a multiple of 1000 for its conversion calculations,
  1427 					/* TTDPatch uses a multiple of 1000 for its conversion calculations,
  1499 					 * to be compatible */
  1429 					 * to be compatible */
  1500 					_currency_specs[curidx].rate = rate / 1000;
  1430 					_currency_specs[curidx].rate = rate / 1000;
  1501 				} else {
  1431 				} else {
  1502 					grfmsg(1, "GlobalVarChangeInfo: Currency multipliers %d out of range, ignoring", curidx);
  1432 					grfmsg(1, "GlobalVarChangeInfo: Currency multipliers %d out of range, ignoring", curidx);
  1503 				}
  1433 				}
  1504 			}
  1434 			} break;
  1505 			break;
  1435 
  1506 
  1436 			case 0x0C: { // Currency options
  1507 		case 0x0C: // Currency options
       
  1508 			FOR_EACH_OBJECT {
       
  1509 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1437 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1510 				uint16 options = grf_load_word(&buf);
  1438 				uint16 options = grf_load_word(&buf);
  1511 
  1439 
  1512 				if (curidx < NUM_CURRENCY) {
  1440 				if (curidx < NUM_CURRENCY) {
  1513 					_currency_specs[curidx].separator = GB(options, 0, 8);
  1441 					_currency_specs[curidx].separator = GB(options, 0, 8);
  1515 					 * since newgrf specs said that only 0 and 1 can be set for symbol_pos */
  1443 					 * since newgrf specs said that only 0 and 1 can be set for symbol_pos */
  1516 					_currency_specs[curidx].symbol_pos = GB(options, 8, 1);
  1444 					_currency_specs[curidx].symbol_pos = GB(options, 8, 1);
  1517 				} else {
  1445 				} else {
  1518 					grfmsg(1, "GlobalVarChangeInfo: Currency option %d out of range, ignoring", curidx);
  1446 					grfmsg(1, "GlobalVarChangeInfo: Currency option %d out of range, ignoring", curidx);
  1519 				}
  1447 				}
  1520 			}
  1448 			} break;
  1521 			break;
  1449 
  1522 
  1450 			case 0x0D: { // Currency prefix symbol
  1523 		case 0x0D: // Currency prefix symbol
       
  1524 			FOR_EACH_OBJECT {
       
  1525 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1451 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1526 				uint32 tempfix = grf_load_dword(&buf);
  1452 				uint32 tempfix = grf_load_dword(&buf);
  1527 
  1453 
  1528 				if (curidx < NUM_CURRENCY) {
  1454 				if (curidx < NUM_CURRENCY) {
  1529 					memcpy(_currency_specs[curidx].prefix,&tempfix,4);
  1455 					memcpy(_currency_specs[curidx].prefix, &tempfix, 4);
  1530 					_currency_specs[curidx].prefix[4] = 0;
  1456 					_currency_specs[curidx].prefix[4] = 0;
  1531 				} else {
  1457 				} else {
  1532 					grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
  1458 					grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
  1533 				}
  1459 				}
  1534 			}
  1460 			} break;
  1535 			break;
  1461 
  1536 
  1462 			case 0x0E: { // Currency suffix symbol
  1537 		case 0x0E: // Currency suffix symbol
       
  1538 			FOR_EACH_OBJECT {
       
  1539 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1463 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1540 				uint32 tempfix = grf_load_dword(&buf);
  1464 				uint32 tempfix = grf_load_dword(&buf);
  1541 
  1465 
  1542 				if (curidx < NUM_CURRENCY) {
  1466 				if (curidx < NUM_CURRENCY) {
  1543 					memcpy(&_currency_specs[curidx].suffix, &tempfix, 4);
  1467 					memcpy(&_currency_specs[curidx].suffix, &tempfix, 4);
  1544 					_currency_specs[curidx].suffix[4] = 0;
  1468 					_currency_specs[curidx].suffix[4] = 0;
  1545 				} else {
  1469 				} else {
  1546 					grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
  1470 					grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
  1547 				}
  1471 				}
  1548 			}
  1472 			} break;
  1549 			break;
  1473 
  1550 
  1474 			case 0x0F: { //  Euro introduction dates
  1551 		case 0x0F: //  Euro introduction dates
       
  1552 			FOR_EACH_OBJECT {
       
  1553 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1475 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1554 				Year year_euro = grf_load_word(&buf);
  1476 				Year year_euro = grf_load_word(&buf);
  1555 
  1477 
  1556 				if (curidx < NUM_CURRENCY) {
  1478 				if (curidx < NUM_CURRENCY) {
  1557 					_currency_specs[curidx].to_euro = year_euro;
  1479 					_currency_specs[curidx].to_euro = year_euro;
  1558 				} else {
  1480 				} else {
  1559 					grfmsg(1, "GlobalVarChangeInfo: Euro intro date %d out of range, ignoring", curidx);
  1481 					grfmsg(1, "GlobalVarChangeInfo: Euro intro date %d out of range, ignoring", curidx);
  1560 				}
  1482 				}
  1561 			}
  1483 			} break;
  1562 			break;
  1484 
  1563 
  1485 			case 0x10: // Snow line height table
  1564 		case 0x10: // 12 * 32 * B Snow line height table
  1486 				if (numinfo > 1 || IsSnowLineSet()) {
  1565 			if (numinfo > 1 || IsSnowLineSet()) {
  1487 					grfmsg(1, "GlobalVarChangeInfo: The snowline can only be set once (%d)", numinfo);
  1566 				grfmsg(1, "GlobalVarChangeInfo: The snowline can only be set once (%d)", numinfo);
  1488 				} else if (len < SNOW_LINE_MONTHS * SNOW_LINE_DAYS) {
  1567 			} else if (len < SNOW_LINE_MONTHS * SNOW_LINE_DAYS) {
  1489 					grfmsg(1, "GlobalVarChangeInfo: Not enough entries set in the snowline table (%d)", len);
  1568 				grfmsg(1, "GlobalVarChangeInfo: Not enough entries set in the snowline table (%d)", len);
  1490 				} else {
  1569 			} else {
  1491 					byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS];
  1570 				byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS];
  1492 
  1571 
  1493 					for (uint i = 0; i < SNOW_LINE_MONTHS; i++) {
  1572 				for (uint i = 0; i < SNOW_LINE_MONTHS; i++) {
  1494 						for (uint j = 0; j < SNOW_LINE_DAYS; j++) {
  1573 					for (uint j = 0; j < SNOW_LINE_DAYS; j++) {
  1495 							table[i][j] = grf_load_byte(&buf);
  1574 						table[i][j] = grf_load_byte(&buf);
  1496 						}
  1575 					}
  1497 					}
  1576 				}
  1498 					SetSnowLine(table);
  1577 				SetSnowLine(table);
  1499 				}
  1578 			}
  1500 				break;
  1579 			break;
  1501 
  1580 
  1502 			default:
  1581 		default:
  1503 				ret = true;
  1582 			ret = true;
  1504 				break;
       
  1505 		}
  1583 	}
  1506 	}
  1584 
  1507 
  1585 	*bufp = buf;
  1508 	*bufp = buf;
  1586 	return ret;
  1509 	return ret;
  1587 }
  1510 }
  1588 
  1511 
  1589 static bool CargoChangeInfo(uint cid, int numinfo, int prop, byte **bufp, int len)
  1512 static bool CargoChangeInfo(uint cid, int numinfo, int prop, byte **bufp, int len)
  1590 {
  1513 {
       
  1514 	byte *buf = *bufp;
       
  1515 	bool ret = false;
       
  1516 
  1591 	if (cid + numinfo > NUM_CARGO) {
  1517 	if (cid + numinfo > NUM_CARGO) {
  1592 		grfmsg(2, "CargoChangeInfo: Cargo type %d out of range (max %d)", cid + numinfo, NUM_CARGO - 1);
  1518 		grfmsg(2, "CargoChangeInfo: Cargo type %d out of range (max %d)", cid + numinfo, NUM_CARGO - 1);
  1593 		return false;
  1519 		return false;
  1594 	}
  1520 	}
  1595 
  1521 
  1596 	CargoSpec *cs = &_cargo[cid];
  1522 	for (int i = 0; i < numinfo; i++) {
  1597 	byte *buf = *bufp;
  1523 		CargoSpec *cs = &_cargo[cid + i];
  1598 	int i;
  1524 
  1599 	bool ret = false;
  1525 		switch (prop) {
  1600 
  1526 			case 0x08: /* Bit number of cargo */
  1601 	switch (prop) {
  1527 				cs->bitnum = grf_load_byte(&buf);
  1602 		case 0x08: /* Bit number of cargo */
  1528 				cs->grfid = _cur_grffile->grfid;
  1603 			FOR_EACH_OBJECT {
       
  1604 				cs[i].bitnum = grf_load_byte(&buf);
       
  1605 				cs[i].grfid = _cur_grffile->grfid;
       
  1606 				if (cs->IsValid()) {
  1529 				if (cs->IsValid()) {
  1607 					SETBIT(_cargo_mask, cid + i);
  1530 					SETBIT(_cargo_mask, cid + i);
  1608 				} else {
  1531 				} else {
  1609 					CLRBIT(_cargo_mask, cid + i);
  1532 					CLRBIT(_cargo_mask, cid + i);
  1610 				}
  1533 				}
  1611 			}
  1534 				break;
  1612 			break;
  1535 
  1613 
  1536 			case 0x09: /* String ID for cargo type name */
  1614 		case 0x09: /* String ID for cargo type name */
  1537 				cs->name = grf_load_word(&buf);
  1615 			FOR_EACH_OBJECT cs[i].name = grf_load_word(&buf);
  1538 				break;
  1616 			break;
  1539 
  1617 
  1540 			case 0x0A: /* String for cargo name, plural */
  1618 		case 0x0A: /* String for cargo name, plural */
  1541 				cs->name_plural = grf_load_word(&buf);
  1619 			FOR_EACH_OBJECT cs[i].name_plural = grf_load_word(&buf);
  1542 				break;
  1620 			break;
  1543 
  1621 
  1544 			case 0x0B:
  1622 		case 0x0B:
  1545 				/* String for units of cargo. This is different in OpenTTD to TTDPatch
  1623 			/* String for units of cargo. This is different in OpenTTD to TTDPatch
  1546 				 * (e.g. 10 tonnes of coal) */
  1624 			 * (e.g. 10 tonnes of coal) */
  1547 				cs->units_volume = grf_load_word(&buf);
  1625 			FOR_EACH_OBJECT cs[i].units_volume = grf_load_word(&buf);
  1548 				break;
  1626 			break;
  1549 
  1627 
  1550 			case 0x0C: /* String for quantity of cargo (e.g. 10 tonnes of coal) */
  1628 		case 0x0C: /* String for quantity of cargo (e.g. 10 tonnes of coal) */
  1551 				cs->quantifier = grf_load_word(&buf);
  1629 			FOR_EACH_OBJECT cs[i].quantifier = grf_load_word(&buf);
  1552 				break;
  1630 			break;
  1553 
  1631 
  1554 			case 0x0D: /* String for two letter cargo abbreviation */
  1632 		case 0x0D: /* String for two letter cargo abbreviation */
  1555 				cs->abbrev = grf_load_word(&buf);
  1633 			FOR_EACH_OBJECT cs[i].abbrev = grf_load_word(&buf);
  1556 				break;
  1634 			break;
  1557 
  1635 
  1558 			case 0x0E: /* Sprite ID for cargo icon */
  1636 		case 0x0E: /* Sprite ID for cargo icon */
  1559 				cs->sprite = grf_load_word(&buf);
  1637 			FOR_EACH_OBJECT cs[i].sprite = grf_load_word(&buf);
  1560 				break;
  1638 			break;
  1561 
  1639 
  1562 			case 0x0F: /* Weight of one unit of cargo */
  1640 		case 0x0F: /* Weight of one unit of cargo */
  1563 				cs->weight = grf_load_byte(&buf);
  1641 			FOR_EACH_OBJECT cs[i].weight = grf_load_byte(&buf);
  1564 				break;
  1642 			break;
  1565 
  1643 
  1566 			case 0x10: /* Used for payment calculation */
  1644 		case 0x10: /* Used for payment calculation */
  1567 				cs->transit_days[0] = grf_load_byte(&buf);
  1645 			FOR_EACH_OBJECT cs[i].transit_days[0] = grf_load_byte(&buf);
  1568 				break;
  1646 			break;
  1569 
  1647 
  1570 			case 0x11: /* Used for payment calculation */
  1648 		case 0x11: /* Used for payment calculation */
  1571 				cs->transit_days[1] = grf_load_byte(&buf);
  1649 			FOR_EACH_OBJECT cs[i].transit_days[1] = grf_load_byte(&buf);
  1572 				break;
  1650 			break;
  1573 
  1651 
  1574 			case 0x12: /* Base cargo price */
  1652 		case 0x12: /* Base cargo price */
  1575 				cs->initial_payment = grf_load_dword(&buf);
  1653 			FOR_EACH_OBJECT cs[i].initial_payment = grf_load_dword(&buf);
  1576 				break;
  1654 			break;
  1577 
  1655 
  1578 			case 0x13: /* Colour for station rating bars */
  1656 		case 0x13: /* Colour for station rating bars */
  1579 				cs->rating_colour = MapDOSColour(grf_load_byte(&buf));
  1657 			FOR_EACH_OBJECT cs[i].rating_colour = MapDOSColour(grf_load_byte(&buf));
  1580 				break;
  1658 			break;
  1581 
  1659 
  1582 			case 0x14: /* Colour for cargo graph */
  1660 		case 0x14: /* Colour for cargo graph */
  1583 				cs->legend_colour = MapDOSColour(grf_load_byte(&buf));
  1661 			FOR_EACH_OBJECT cs[i].legend_colour = MapDOSColour(grf_load_byte(&buf));
  1584 				break;
  1662 			break;
  1585 
  1663 
  1586 			case 0x15: /* Freight status */
  1664 		case 0x15: /* Freight status */
  1587 				cs->is_freight = (grf_load_byte(&buf) != 0);
  1665 			FOR_EACH_OBJECT cs[i].is_freight = grf_load_byte(&buf) != 0;
  1588 				break;
  1666 			break;
  1589 
  1667 
  1590 			case 0x16: /* Cargo classes */
  1668 		case 0x16: /* Cargo classes */
  1591 				cs->classes = grf_load_word(&buf);
  1669 			FOR_EACH_OBJECT cs[i].classes = grf_load_word(&buf);
  1592 				break;
  1670 			break;
  1593 
  1671 
  1594 			case 0x17: /* Cargo label */
  1672 		case 0x17: /* Cargo label */
  1595 				cs->label = grf_load_dword(&buf);
  1673 			FOR_EACH_OBJECT {
  1596 				cs->label = BSWAP32(cs->label);
  1674 				cs[i].label = grf_load_dword(&buf);
  1597 				break;
  1675 				cs[i].label = BSWAP32(cs[i].label);
  1598 
  1676 			}
  1599 			case 0x18: { /* Town growth substitute type */
  1677 			break;
       
  1678 
       
  1679 		case 0x18: /* Town growth substitute type */
       
  1680 			FOR_EACH_OBJECT {
       
  1681 				uint8 substitute_type = grf_load_byte(&buf);
  1600 				uint8 substitute_type = grf_load_byte(&buf);
       
  1601 
  1682 				switch (substitute_type) {
  1602 				switch (substitute_type) {
  1683 					case 0x00: cs[i].town_effect = TE_PASSENGERS; break;
  1603 					case 0x00: cs->town_effect = TE_PASSENGERS; break;
  1684 					case 0x02: cs[i].town_effect = TE_MAIL; break;
  1604 					case 0x02: cs->town_effect = TE_MAIL; break;
  1685 					case 0x05: cs[i].town_effect = TE_GOODS; break;
  1605 					case 0x05: cs->town_effect = TE_GOODS; break;
  1686 					case 0x09: cs[i].town_effect = TE_WATER; break;
  1606 					case 0x09: cs->town_effect = TE_WATER; break;
  1687 					case 0x0B: cs[i].town_effect = TE_FOOD; break;
  1607 					case 0x0B: cs->town_effect = TE_FOOD; break;
  1688 					default:
  1608 					default:
  1689 						grfmsg(1, "CargoChangeInfo: Unknown town growth substitute value %d, setting to none.", substitute_type);
  1609 						grfmsg(1, "CargoChangeInfo: Unknown town growth substitute value %d, setting to none.", substitute_type);
  1690 					case 0xFF: cs[i].town_effect = TE_NONE; break;
  1610 					case 0xFF: cs->town_effect = TE_NONE; break;
  1691 				}
  1611 				}
  1692 			}
  1612 			} break;
  1693 			break;
  1613 
  1694 
  1614 			case 0x19: /* Town growth coefficient */
  1695 		case 0x19: /* Town growth coefficient */
  1615 				cs->multipliertowngrowth = grf_load_word(&buf);
  1696 			FOR_EACH_OBJECT cs[i].multipliertowngrowth = grf_load_word(&buf);
  1616 				break;
  1697 			break;
  1617 
  1698 
  1618 			case 0x1A: /* Bitmask of callbacks to use */
  1699 		case 0x1A: /* Bitmask of callbacks to use */
  1619 				cs->callback_mask = grf_load_byte(&buf);
  1700 			FOR_EACH_OBJECT cs[i].callback_mask = grf_load_byte(&buf);
  1620 				break;
  1701 			break;
  1621 
  1702 
  1622 			default:
  1703 		default:
  1623 				ret = true;
  1704 			ret = true;
  1624 				break;
       
  1625 		}
  1705 	}
  1626 	}
  1706 
  1627 
  1707 	*bufp = buf;
  1628 	*bufp = buf;
  1708 	return ret;
  1629 	return ret;
  1709 }
  1630 }
  1710 
  1631 
  1711 
  1632 
  1712 static bool SoundEffectChangeInfo(uint sid, int numinfo, int prop, byte **bufp, int len)
  1633 static bool SoundEffectChangeInfo(uint sid, int numinfo, int prop, byte **bufp, int len)
  1713 {
  1634 {
  1714 	byte *buf = *bufp;
  1635 	byte *buf = *bufp;
  1715 	int i;
       
  1716 	bool ret = false;
  1636 	bool ret = false;
  1717 
  1637 
  1718 	if (_cur_grffile->sound_offset == 0) {
  1638 	if (_cur_grffile->sound_offset == 0) {
  1719 		grfmsg(1, "SoundEffectChangeInfo: No effects defined, skipping");
  1639 		grfmsg(1, "SoundEffectChangeInfo: No effects defined, skipping");
  1720 		return false;
  1640 		return false;
  1721 	}
  1641 	}
  1722 
  1642 
  1723 	switch (prop) {
  1643 	for (int i = 0; i < numinfo; i++) {
  1724 		case 0x08: // Relative volume
  1644 		uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
  1725 			FOR_EACH_OBJECT {
  1645 
  1726 				uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
  1646 		if (sound >= GetNumSounds()) {
  1727 
  1647 			grfmsg(1, "SoundEffectChangeInfo: Sound %d not defined (max %d)", sound, GetNumSounds());
  1728 				if (sound >= GetNumSounds()) {
  1648 			continue;
  1729 					grfmsg(1, "SoundEffectChangeInfo: Sound %d not defined (max %d)", sound, GetNumSounds());
  1649 		}
  1730 				} else {
  1650 
  1731 					GetSound(sound)->volume = grf_load_byte(&buf);
  1651 		switch (prop) {
  1732 				}
  1652 			case 0x08: // Relative volume
  1733 			}
  1653 				GetSound(sound)->volume = grf_load_byte(&buf);
  1734 			break;
  1654 				break;
  1735 
  1655 
  1736 		case 0x09: // Priority
  1656 			case 0x09: // Priority
  1737 			FOR_EACH_OBJECT {
  1657 				GetSound(sound)->priority = grf_load_byte(&buf);
  1738 				uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
  1658 				break;
  1739 
  1659 
  1740 				if (sound >= GetNumSounds()) {
  1660 			case 0x0A: { // Override old sound
  1741 					grfmsg(1, "SoundEffectChangeInfo: Sound %d not defined (max %d)", sound, GetNumSounds());
       
  1742 				} else {
       
  1743 					GetSound(sound)->priority = grf_load_byte(&buf);
       
  1744 				}
       
  1745 			}
       
  1746 			break;
       
  1747 
       
  1748 		case 0x0A: // Override old sound
       
  1749 			FOR_EACH_OBJECT {
       
  1750 				uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
       
  1751 				uint orig_sound = grf_load_byte(&buf);
  1661 				uint orig_sound = grf_load_byte(&buf);
  1752 
  1662 
  1753 				if (sound >= GetNumSounds() || orig_sound >= GetNumSounds()) {
  1663 				if (orig_sound >= GetNumSounds()) {
  1754 					grfmsg(1, "SoundEffectChangeInfo: Sound %d or %d not defined (max %d)", sound, orig_sound, GetNumSounds());
  1664 					grfmsg(1, "SoundEffectChangeInfo: Original sound %d not defined (max %d)", orig_sound, GetNumSounds());
  1755 				} else {
  1665 				} else {
  1756 					FileEntry *newfe = GetSound(sound);
  1666 					FileEntry *newfe = GetSound(sound);
  1757 					FileEntry *oldfe = GetSound(orig_sound);
  1667 					FileEntry *oldfe = GetSound(orig_sound);
  1758 
  1668 
  1759 					/* Literally copy the data of the new sound over the original */
  1669 					/* Literally copy the data of the new sound over the original */
  1760 					*oldfe = *newfe;
  1670 					*oldfe = *newfe;
  1761 				}
  1671 				}
  1762 			}
  1672 			} break;
  1763 			break;
  1673 
  1764 
  1674 			default:
  1765 		default:
  1675 				ret = true;
  1766 			ret = true;
  1676 				break;
       
  1677 		}
       
  1678 	}
       
  1679 
       
  1680 	*bufp = buf;
       
  1681 	return ret;
       
  1682 }
       
  1683 
       
  1684 static bool IndustrytilesChangeInfo(uint indtid, int numinfo, int prop, byte **bufp, int len)
       
  1685 {
       
  1686 	if (!HASBIT(_ttdpatch_flags[3], 0x07)) return true;
       
  1687 
       
  1688 	byte *buf = *bufp;
       
  1689 	bool ret = false;
       
  1690 
       
  1691 	if (indtid + numinfo > NUM_INDUSTRYTILES) {
       
  1692 		grfmsg(1, "IndustryTilesChangeInfo: Too many industry tiles loaded (%u), max (%u). Ignoring.", indtid + numinfo, NUM_INDUSTRYTILES);
       
  1693 		return false;
       
  1694 	}
       
  1695 
       
  1696 	/* Allocate industry tile specs if they haven't been allocated already. */
       
  1697 	if (_cur_grffile->indtspec == NULL) {
       
  1698 		_cur_grffile->indtspec = CallocT<IndustryTileSpec*>(NUM_INDUSTRYTILES);
       
  1699 
       
  1700 		/* Reset any overrides that have been set. */
       
  1701 		_industile_mngr.ResetOverride();
       
  1702 	}
       
  1703 
       
  1704 	for (int i = 0; i < numinfo; i++) {
       
  1705 		IndustryTileSpec *tsp = _cur_grffile->indtspec[indtid + i];
       
  1706 
       
  1707 		if (prop != 0x08 && tsp == NULL) {
       
  1708 			grfmsg(2, "IndustryTilesChangeInfo: Attempt to modify undefined industry tile %u. Ignoring.", indtid + i);
       
  1709 			continue;
       
  1710 		}
       
  1711 
       
  1712 		switch (prop) {
       
  1713 			case 0x08: { // Substitute industry tile type
       
  1714 				IndustryTileSpec **tilespec = &_cur_grffile->indtspec[indtid + i];
       
  1715 				byte subs_id = grf_load_byte(&buf);
       
  1716 
       
  1717 				if (subs_id == 0xFF) {
       
  1718 					/* Instead of defining a new industry, a substitute industry id
       
  1719 					 * of 0xFF disables the old industry with the current id. */
       
  1720 					tsp->enabled = false;
       
  1721 					continue;
       
  1722 				} else if (subs_id >= NEW_INDUSTRYTILEOFFSET) {
       
  1723 					/* The substitute id must be one of the original industry tile. */
       
  1724 					grfmsg(2, "IndustryTilesChangeInfo: Attempt to use new industry tile %u as substitute industry tile for %u. Ignoring.", subs_id, indtid + i);
       
  1725 					return false;
       
  1726 				}
       
  1727 
       
  1728 				/* Allocate space for this industry. */
       
  1729 				if (*tilespec == NULL) {
       
  1730 					int tempid;
       
  1731 					*tilespec = CallocT<IndustryTileSpec>(1);
       
  1732 					tsp = *tilespec;
       
  1733 
       
  1734 					memcpy(tsp, &_industry_tile_specs[subs_id], sizeof(_industry_tile_specs[subs_id]));
       
  1735 					tsp->enabled = true;
       
  1736 					tsp->grf_prop.local_id = indtid + i;
       
  1737 					tsp->grf_prop.subst_id = subs_id;
       
  1738 					tsp->grf_prop.grffile = _cur_grffile;
       
  1739 					tempid = _industile_mngr.AddEntityID(indtid + i, _cur_grffile->grfid, subs_id); // pre-reserve the tile slot
       
  1740 				}
       
  1741 			} break;
       
  1742 
       
  1743 			case 0x09: { // Industry tile override
       
  1744 				byte ovrid = grf_load_byte(&buf);
       
  1745 
       
  1746 				/* The industry being overridden must be an original industry. */
       
  1747 				if (ovrid >= NEW_INDUSTRYTILEOFFSET) {
       
  1748 					grfmsg(2, "IndustryTilesChangeInfo: Attempt to override new industry tile %u with industry tile id %u. Ignoring.", ovrid, indtid + i);
       
  1749 					return false;
       
  1750 				}
       
  1751 
       
  1752 				tsp->grf_prop.override = ovrid;
       
  1753 				_industile_mngr.Add(indtid + i, ovrid);
       
  1754 			} break;
       
  1755 
       
  1756 			case 0x0A: // Tile acceptance
       
  1757 			case 0x0B:
       
  1758 			case 0x0C: {
       
  1759 				uint16 acctp = grf_load_word(&buf);
       
  1760 				tsp->accepts_cargo[prop - 0x0A] = GetCargoTranslation(GB(acctp, 0, 8), _cur_grffile);
       
  1761 				tsp->acceptance[prop - 0x0A] = GetCargoTranslation(GB(acctp, 8, 8), _cur_grffile);
       
  1762 			} break;
       
  1763 
       
  1764 			case 0x0D: // Land shape flags
       
  1765 				tsp->slopes_refused = (Slope)grf_load_byte(&buf);
       
  1766 				break;
       
  1767 
       
  1768 			case 0x0E: // Callback flags
       
  1769 				tsp->callback_flags = grf_load_byte(&buf);
       
  1770 				break;
       
  1771 
       
  1772 			case 0x0F: // Animation information
       
  1773 				tsp->animation_info = grf_load_word(&buf);
       
  1774 				break;
       
  1775 
       
  1776 			case 0x10: // Animation speed
       
  1777 				tsp->animation_speed = grf_load_byte(&buf);
       
  1778 				break;
       
  1779 
       
  1780 			case 0x11: // Triggers for callback 25
       
  1781 				tsp->animation_triggers = grf_load_byte(&buf);
       
  1782 				break;
       
  1783 
       
  1784 			case 0x12: // Special flags
       
  1785 				tsp->animation_special_flags = grf_load_byte(&buf);
       
  1786 				break;
       
  1787 
       
  1788 			default:
       
  1789 				ret = true;
       
  1790 				break;
       
  1791 		}
       
  1792 	}
       
  1793 
       
  1794 	*bufp = buf;
       
  1795 	return ret;
       
  1796 }
       
  1797 
       
  1798 static bool IndustriesChangeInfo(uint indid, int numinfo, int prop, byte **bufp, int len)
       
  1799 {
       
  1800 	if (!HASBIT(_ttdpatch_flags[3], 0x07)) return true;
       
  1801 
       
  1802 	byte *buf = *bufp;
       
  1803 	bool ret = false;
       
  1804 
       
  1805 	if (indid + numinfo > NUM_INDUSTRYTYPES) {
       
  1806 		grfmsg(1, "IndustriesChangeInfo: Too many industries loaded (%u), max (%u). Ignoring.", indid + numinfo, NUM_INDUSTRYTYPES);
       
  1807 		return false;
       
  1808 	}
       
  1809 
       
  1810 	grfmsg(1, "IndustriesChangeInfo: newid %u", indid);
       
  1811 
       
  1812 	/* Allocate industry specs if they haven't been allocated already. */
       
  1813 	if (_cur_grffile->industryspec == NULL) {
       
  1814 		_cur_grffile->industryspec = CallocT<IndustrySpec*>(NUM_INDUSTRYTYPES);
       
  1815 
       
  1816 		/* Reset any overrides that have been set. */
       
  1817 		_industry_mngr.ResetOverride();
       
  1818 	}
       
  1819 
       
  1820 	for (int i = 0; i < numinfo; i++) {
       
  1821 		IndustrySpec *indsp = _cur_grffile->industryspec[indid + i];
       
  1822 
       
  1823 		if (prop != 0x08 && indsp == NULL) {
       
  1824 			grfmsg(2, "IndustriesChangeInfo: Attempt to modify undefined industry %u. Ignoring.", indid + i);
       
  1825 			continue;
       
  1826 		}
       
  1827 
       
  1828 		switch (prop) {
       
  1829 			case 0x08: { // Substitute industry type
       
  1830 				IndustrySpec **indspec = &_cur_grffile->industryspec[indid + i];
       
  1831 				byte subs_id = grf_load_byte(&buf);
       
  1832 
       
  1833 				if (subs_id == 0xFF) {
       
  1834 					/* Instead of defining a new industry, a substitute industry id
       
  1835 					 * of 0xFF disables the old industry with the current id. */
       
  1836 					_industry_specs[indid + i].enabled = false;
       
  1837 					continue;
       
  1838 				} else if (subs_id >= NEW_INDUSTRYOFFSET) {
       
  1839 					/* The substitute id must be one of the original industry. */
       
  1840 					grfmsg(2, "_industry_specs: Attempt to use new industry %u as substitute industry for %u. Ignoring.", subs_id, indid + i);
       
  1841 					return false;
       
  1842 				}
       
  1843 
       
  1844 				/* Allocate space for this industry.
       
  1845 				 * Only need to do it once. If ever it is called again, it should not
       
  1846 				 * do anything */
       
  1847 				if (*indspec == NULL) {
       
  1848 					*indspec = CallocT<IndustrySpec>(1);
       
  1849 					indsp = *indspec;
       
  1850 
       
  1851 					memcpy(indsp, &_origin_industry_specs[subs_id], sizeof(_industry_specs[subs_id]));
       
  1852 					indsp->enabled = true;
       
  1853 					indsp->grf_prop.local_id = indid + i;
       
  1854 					indsp->grf_prop.subst_id = subs_id;
       
  1855 					indsp->grf_prop.grffile = _cur_grffile;
       
  1856 				}
       
  1857 			} break;
       
  1858 
       
  1859 			case 0x09: { // Industry type override
       
  1860 				byte ovrid = grf_load_byte(&buf);
       
  1861 
       
  1862 				/* The industry being overridden must be an original industry. */
       
  1863 				if (ovrid >= NEW_INDUSTRYOFFSET) {
       
  1864 					grfmsg(2, "IndustriesChangeInfo: Attempt to override new industry %u with industry id %u. Ignoring.", ovrid, indid + i);
       
  1865 					return false;
       
  1866 				}
       
  1867 				indsp->grf_prop.override = ovrid;
       
  1868 				_industry_mngr.Add(indid + i, ovrid);
       
  1869 			} break;
       
  1870 
       
  1871 			case 0x0A: { // Set industry layout(s)
       
  1872 				indsp->num_table = grf_load_byte(&buf); // Number of layaouts
       
  1873 				uint32 defsize = grf_load_dword(&buf);  // Total size of the definition
       
  1874 				IndustryTileTable **tile_table = CallocT<IndustryTileTable*>(indsp->num_table); // Table with tiles to compose an industry
       
  1875 				IndustryTileTable *itt = CallocT<IndustryTileTable>(defsize); // Temporary array to read the tile layouts from the GRF
       
  1876 				int size;
       
  1877 				IndustryTileTable *copy_from;
       
  1878 
       
  1879 				for (byte j = 0; j < indsp->num_table; j++) {
       
  1880 					for (int k = 0;; k++) {
       
  1881 						itt[k].ti.x = grf_load_byte(&buf); // Offsets from northermost tile
       
  1882 
       
  1883 						if (itt[k].ti.x == 0xFE && k == 0) {
       
  1884 							/* This means we have to borrow the layout from an old industry */
       
  1885 							IndustryType type = grf_load_byte(&buf);  //industry holding required layout
       
  1886 							byte laynbr = grf_load_byte(&buf);        //layout number to borrow
       
  1887 
       
  1888 							copy_from = (IndustryTileTable*)_origin_industry_specs[type].table[laynbr];
       
  1889 							for (size = 1;; size++) {
       
  1890 								if (_origin_industry_specs[type].table[laynbr + (size - 1)]->ti.x == -0x80 &&
       
  1891 										_origin_industry_specs[type].table[laynbr + (size - 1)]->ti.y == 0) break;
       
  1892 							}
       
  1893 							break;
       
  1894 						}
       
  1895 
       
  1896 						itt[k].ti.y = grf_load_byte(&buf); // Or table definition finalisation
       
  1897 
       
  1898 						if (itt[k].ti.x == 0 && itt[k].ti.y == 0x80) {
       
  1899 							/*  Not the same terminator.  The one we are using is rather
       
  1900 							 x= -80, y = x .  So, adjust it. */
       
  1901 							itt[k].ti.x = -0x80;
       
  1902 							itt[k].ti.y =  0;
       
  1903 							itt[k].gfx  =  0;
       
  1904 
       
  1905 							size = k + 1;
       
  1906 							copy_from = itt;
       
  1907 							break;
       
  1908 						}
       
  1909 
       
  1910 						itt[k].gfx = grf_load_byte(&buf);
       
  1911 
       
  1912 						if (itt[k].gfx == 0xFE) {
       
  1913 							/* Use a new tile from this GRF */
       
  1914 							int local_tile_id = grf_load_word(&buf);
       
  1915 
       
  1916 							/* Read the ID from the _industile_mngr. */
       
  1917 							int tempid = _industile_mngr.GetID(local_tile_id, _cur_grffile->grfid);
       
  1918 
       
  1919 							if (tempid == INVALID_INDUSTRYTILE) {
       
  1920 								grfmsg(2, "IndustriesChangeInfo: Attempt to use industry tile %u with industry id %u, not yet defined. Ignoring.", local_tile_id, indid);
       
  1921 							} else {
       
  1922 								/* Declared as been valid, can be used */
       
  1923 								itt[k].gfx = tempid;
       
  1924 								size = k + 1;
       
  1925 								copy_from = itt;
       
  1926 							}
       
  1927 						}
       
  1928 					}
       
  1929 					tile_table[j] = CallocT<IndustryTileTable>(size);
       
  1930 					memcpy(tile_table[j], copy_from, sizeof(*copy_from) * size);
       
  1931 				}
       
  1932 				/* Install final layout construction in the industry spec */
       
  1933 				indsp->table = tile_table;
       
  1934 				SETBIT(indsp->cleanup_flag, 1);
       
  1935 				free(itt);
       
  1936 			} break;
       
  1937 
       
  1938 			case 0x0B: // Industry production flags
       
  1939 				indsp->life_type = (IndustryLifeType)grf_load_byte(&buf);
       
  1940 				break;
       
  1941 
       
  1942 			case 0x0C: // Industry closure message
       
  1943 				indsp->closure_text = MapGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
       
  1944 				break;
       
  1945 
       
  1946 			case 0x0D: // Production increase message
       
  1947 				indsp->production_up_text = MapGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
       
  1948 				break;
       
  1949 
       
  1950 			case 0x0E: // Production decrease message
       
  1951 				indsp->production_down_text = MapGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
       
  1952 				break;
       
  1953 
       
  1954 			case 0x0F: // Fund cost multiplier
       
  1955 				indsp->cost_multiplier = grf_load_byte(&buf);
       
  1956 				break;
       
  1957 
       
  1958 			case 0x10: // Production cargo types
       
  1959 				for (byte j = 0; j < 2; j++) {
       
  1960 					indsp->produced_cargo[j] = GetCargoTranslation(grf_load_byte(&buf), _cur_grffile);
       
  1961 				}
       
  1962 				break;
       
  1963 
       
  1964 			case 0x11: // Acceptance cargo types
       
  1965 				for (byte j = 0; j < 3; j++) {
       
  1966 					indsp->accepts_cargo[j] = GetCargoTranslation(grf_load_byte(&buf), _cur_grffile);
       
  1967 				}
       
  1968 				grf_load_byte(&buf); // Unnused, eat it up
       
  1969 				break;
       
  1970 
       
  1971 			case 0x12: // Production multipliers
       
  1972 			case 0x13:
       
  1973 				indsp->production_rate[prop - 0x12] = grf_load_byte(&buf);
       
  1974 				break;
       
  1975 
       
  1976 			case 0x14: // Minimal amount of cargo distributed
       
  1977 				indsp->minimal_cargo = grf_load_byte(&buf);
       
  1978 				break;
       
  1979 
       
  1980 			case 0x15: { // Random sound effects
       
  1981 				indsp->number_of_sounds = grf_load_byte(&buf);
       
  1982 				uint8 *sounds = MallocT<uint8>(indsp->number_of_sounds);
       
  1983 
       
  1984 				for (uint8 j = 0; j < indsp->number_of_sounds; j++) sounds[j] = grf_load_byte(&buf);
       
  1985 				indsp->random_sounds = sounds;
       
  1986 				SETBIT(indsp->cleanup_flag, 0);
       
  1987 			} break;
       
  1988 
       
  1989 			case 0x16: // Conflicting industry types
       
  1990 				for (byte j = 0; j < 3; j++) indsp->conflicting[j] = grf_load_byte(&buf);
       
  1991 				break;
       
  1992 
       
  1993 			case 0x17: // Probability in random game
       
  1994 				indsp->appear_ingame[_opt.landscape] = grf_load_byte(&buf);
       
  1995 				break;
       
  1996 
       
  1997 			case 0x18: // Probability during gameplay
       
  1998 				indsp->appear_creation[_opt.landscape] = grf_load_byte(&buf);
       
  1999 				break;
       
  2000 
       
  2001 			case 0x19: // Map color
       
  2002 				indsp->map_colour = MapDOSColour(grf_load_byte(&buf));
       
  2003 				break;
       
  2004 
       
  2005 			case 0x1A: // Special industry flags to define special behavior
       
  2006 				indsp->behaviour = (IndustyBehaviour)grf_load_dword(&buf);
       
  2007 				break;
       
  2008 
       
  2009 			case 0x1B: // New industry text ID
       
  2010 				indsp->new_industry_text = MapGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
       
  2011 				break;
       
  2012 
       
  2013 			case 0x1C: // Input cargo multipliers for the three input cargo types
       
  2014 			case 0x1D:
       
  2015 			case 0x1E: {
       
  2016 					uint32 multiples = grf_load_dword(&buf);
       
  2017 					indsp->input_cargo_multiplier[prop - 0x1C][0] = GB(multiples, 0,15);
       
  2018 					indsp->input_cargo_multiplier[prop - 0x1C][1] = GB(multiples, 15,15);
       
  2019 				} break;
       
  2020 
       
  2021 			case 0x1F: // Industry name
       
  2022 				indsp->name = MapGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
       
  2023 				break;
       
  2024 
       
  2025 			case 0x20: // Prospecting success chance
       
  2026 				indsp->prospecting_chance = grf_load_dword(&buf);
       
  2027 				break;
       
  2028 
       
  2029 			case 0x21:   // Callback flags
       
  2030 			case 0x22: { // Callback additional flags
       
  2031 				byte aflag = grf_load_byte(&buf);
       
  2032 				SB(indsp->callback_flags, (prop - 0x21) * 8, 8, aflag);
       
  2033 			} break;
       
  2034 
       
  2035 			default:
       
  2036 				ret = true;
       
  2037 				break;
       
  2038 		}
  1767 	}
  2039 	}
  1768 
  2040 
  1769 	*bufp = buf;
  2041 	*bufp = buf;
  1770 	return ret;
  2042 	return ret;
  1771 }
  2043 }
  1781 		grfmsg(1, "StationChangeInfo: Station %u is invalid, max %u, ignoring", stid + numinfo, MAX_FSM_PORTS);
  2053 		grfmsg(1, "StationChangeInfo: Station %u is invalid, max %u, ignoring", stid + numinfo, MAX_FSM_PORTS);
  1782 		return false;
  2054 		return false;
  1783 	}
  2055 	}
  1784 
  2056 
  1785 	/* Allocate station specs if necessary */
  2057 	/* Allocate station specs if necessary */
  1786 	if (_cur_grffile->stations == NULL) _cur_grffile->stations = CallocT<StationSpec*>(MAX_FSM_PORTS);
  2058 	if (_cur_grffile->stations == NULL) _cur_grffile->stations = CallocT<StationSpec*>(MAX_STATIONS);
  1787 
  2059 
  1788 	statspec = &_cur_grffile->stations[stid];
  2060 	for (int i = 0; i < numinfo; i++) {
  1789 
  2061 		StationSpec *statspec = _cur_grffile->stations[stid + i];
  1790 	if (prop != 0x08) {
  2062 
  1791 		/* Check that all stations we are modifying are defined. */
  2063 		/* Check that the station we are modifying is defined. */
  1792 		FOR_EACH_OBJECT {
  2064 		if (statspec == NULL && prop != 0x08) {
  1793 			if (statspec[i] == NULL) {
  2065 			grfmsg(2, "StationChangeInfo: Attempt to modify undefined station %u, ignoring", stid + i);
  1794 				grfmsg(2, "StationChangeInfo: Attempt to modify undefined station %u, ignoring", stid + i);
  2066 			continue;
  1795 				return false;
  2067 		}
  1796 			}
  2068 
  1797 		}
  2069 		switch (prop) {
  1798 	}
  2070 			case 0x08: { // Class ID
  1799 
  2071 				StationSpec **spec = &_cur_grffile->stations[stid + i];
  1800 	switch (prop) {
  2072 
  1801 		case 0x08: // Class ID
       
  1802 			FOR_EACH_OBJECT {
       
  1803 				/* Property 0x08 is special; it is where the station is allocated */
  2073 				/* Property 0x08 is special; it is where the station is allocated */
  1804 				if (statspec[i] == NULL) statspec[i] = CallocT<StationSpec>(1);
  2074 				if (*spec == NULL) *spec = CallocT<StationSpec>(1);
  1805 
  2075 
  1806 				/* Swap classid because we read it in BE meaning WAYP or DFLT */
  2076 				/* Swap classid because we read it in BE meaning WAYP or DFLT */
  1807 				uint32 classid = grf_load_dword(&buf);
  2077 				uint32 classid = grf_load_dword(&buf);
  1808 				statspec[i]->sclass = AllocateStationClass(BSWAP32(classid));
  2078 				(*spec)->sclass = AllocateStationClass(BSWAP32(classid));
  1809 			}
  2079 			} break;
  1810 			break;
  2080 
  1811 
  2081 			case 0x09: // Define sprite layout
  1812 		case 0x09: // Define sprite layout
       
  1813 			FOR_EACH_OBJECT {
       
  1814 				StationSpec *statspec = _cur_grffile->stations[stid + i];
       
  1815 
       
  1816 				statspec->tiles = grf_load_extended(&buf);
  2082 				statspec->tiles = grf_load_extended(&buf);
  1817 				statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
  2083 				statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
  1818 				statspec->copied_renderdata = false;
  2084 				statspec->copied_renderdata = false;
  1819 
  2085 
  1820 				for (uint t = 0; t < statspec->tiles; t++) {
  2086 				for (uint t = 0; t < statspec->tiles; t++) {
  1861 							CLRBIT(dtss->image, 14);
  2127 							CLRBIT(dtss->image, 14);
  1862 							SETBIT(dtss->image, PALETTE_MODIFIER_TRANSPARENT);
  2128 							SETBIT(dtss->image, PALETTE_MODIFIER_TRANSPARENT);
  1863 						}
  2129 						}
  1864 					}
  2130 					}
  1865 				}
  2131 				}
  1866 			}
  2132 				break;
  1867 			break;
  2133 
  1868 
  2134 			case 0x0A: { // Copy sprite layout
  1869 		case 0x0A: // Copy sprite layout
       
  1870 			FOR_EACH_OBJECT {
       
  1871 				StationSpec *statspec = _cur_grffile->stations[stid + i];
       
  1872 				byte srcid = grf_load_byte(&buf);
  2135 				byte srcid = grf_load_byte(&buf);
  1873 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  2136 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  1874 
  2137 
  1875 				statspec->tiles = srcstatspec->tiles;
  2138 				statspec->tiles = srcstatspec->tiles;
  1876 				statspec->renderdata = srcstatspec->renderdata;
  2139 				statspec->renderdata = srcstatspec->renderdata;
  1877 				statspec->copied_renderdata = true;
  2140 				statspec->copied_renderdata = true;
  1878 			}
  2141 			} break;
  1879 			break;
  2142 
  1880 
  2143 			case 0x0B: // Callback mask
  1881 		case 0x0B: // Callback mask
  2144 				statspec->callbackmask = grf_load_byte(&buf);
  1882 			FOR_EACH_OBJECT statspec[i]->callbackmask = grf_load_byte(&buf);
  2145 				break;
  1883 			break;
  2146 
  1884 
  2147 			case 0x0C: // Disallowed number of platforms
  1885 		case 0x0C: // Disallowed number of platforms
  2148 				statspec->disallowed_platforms = grf_load_byte(&buf);
  1886 			FOR_EACH_OBJECT statspec[i]->disallowed_platforms = grf_load_byte(&buf);
  2149 				break;
  1887 			break;
  2150 
  1888 
  2151 			case 0x0D: // Disallowed platform lengths
  1889 		case 0x0D: // Disallowed platform lengths
  2152 				statspec->disallowed_lengths = grf_load_byte(&buf);
  1890 			FOR_EACH_OBJECT statspec[i]->disallowed_lengths = grf_load_byte(&buf);
  2153 				break;
  1891 			break;
  2154 
  1892 
  2155 			case 0x0E: // Define custom layout
  1893 		case 0x0E: // Define custom layout
       
  1894 			FOR_EACH_OBJECT {
       
  1895 				StationSpec *statspec = _cur_grffile->stations[stid + i];
       
  1896 
       
  1897 				statspec->copied_layouts = false;
  2156 				statspec->copied_layouts = false;
  1898 
  2157 
  1899 				while (buf < *bufp + len) {
  2158 				while (buf < *bufp + len) {
  1900 					byte length = grf_load_byte(&buf);
  2159 					byte length = grf_load_byte(&buf);
  1901 					byte number = grf_load_byte(&buf);
  2160 					byte number = grf_load_byte(&buf);
  1938 					l--;
  2197 					l--;
  1939 					p--;
  2198 					p--;
  1940 					free(statspec->layouts[l][p]);
  2199 					free(statspec->layouts[l][p]);
  1941 					statspec->layouts[l][p] = layout;
  2200 					statspec->layouts[l][p] = layout;
  1942 				}
  2201 				}
  1943 			}
  2202 				break;
  1944 			break;
  2203 
  1945 
  2204 			case 0x0F: { // Copy custom layout
  1946 		case 0x0F: // Copy custom layout
       
  1947 			FOR_EACH_OBJECT {
       
  1948 				StationSpec *statspec = _cur_grffile->stations[stid + i];
       
  1949 				byte srcid = grf_load_byte(&buf);
  2205 				byte srcid = grf_load_byte(&buf);
  1950 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  2206 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  1951 
  2207 
  1952 				statspec->lengths   = srcstatspec->lengths;
  2208 				statspec->lengths   = srcstatspec->lengths;
  1953 				statspec->platforms = srcstatspec->platforms;
  2209 				statspec->platforms = srcstatspec->platforms;
  1954 				statspec->layouts   = srcstatspec->layouts;
  2210 				statspec->layouts   = srcstatspec->layouts;
  1955 				statspec->copied_layouts = true;
  2211 				statspec->copied_layouts = true;
  1956 			}
  2212 			} break;
  1957 			break;
  2213 
  1958 
  2214 			case 0x10: // Little/lots cargo threshold
  1959 		case 0x10: // Little/lots cargo threshold
  2215 				statspec->cargo_threshold = grf_load_word(&buf);
  1960 			FOR_EACH_OBJECT statspec[i]->cargo_threshold = grf_load_word(&buf);
  2216 				break;
  1961 			break;
  2217 
  1962 
  2218 			case 0x11: // Pylon placement
  1963 		case 0x11: // Pylon placement
  2219 				statspec->pylons = grf_load_byte(&buf);
  1964 			FOR_EACH_OBJECT statspec[i]->pylons = grf_load_byte(&buf);
  2220 				break;
  1965 			break;
  2221 
  1966 
  2222 			case 0x12: // Cargo types for random triggers
  1967 		case 0x12: // Cargo types for random triggers
  2223 				statspec->cargo_triggers = grf_load_dword(&buf);
  1968 			FOR_EACH_OBJECT statspec[i]->cargo_triggers = grf_load_dword(&buf);
  2224 				break;
  1969 			break;
  2225 
  1970 
  2226 			case 0x13: // General flags
  1971 		case 0x13: // General flags
  2227 				statspec->flags = grf_load_byte(&buf);
  1972 			FOR_EACH_OBJECT statspec[i]->flags = grf_load_byte(&buf);
  2228 				break;
  1973 			break;
  2229 
  1974 
  2230 			case 0x14: // Overhead wire placement
  1975 		case 0x14: // Overhead wire placement
  2231 				statspec->wires = grf_load_byte(&buf);
  1976 			FOR_EACH_OBJECT statspec[i]->wires = grf_load_byte(&buf);
  2232 				break;
  1977 			break;
  2233 
  1978 
  2234 			case 0x15: // Blocked tiles
  1979 		case 0x15: // Blocked tiles
  2235 				statspec->blocked = grf_load_byte(&buf);
  1980 			FOR_EACH_OBJECT statspec[i]->blocked = grf_load_byte(&buf);
  2236 				break;
  1981 			break;
  2237 
  1982 
  2238 			case 0x16: // @todo Animation info
  1983 		case 0x16: // @todo Animation info
  2239 				grf_load_word(&buf);
  1984 			FOR_EACH_OBJECT grf_load_word(&buf);
  2240 				ret = true;
  1985 			ret = true;
  2241 				break;
  1986 			break;
  2242 
  1987 
  2243 			case 0x17: // @todo Animation speed
  1988 		case 0x17: // @todo Animation speed
  2244 				grf_load_byte(&buf);
  1989 			FOR_EACH_OBJECT grf_load_byte(&buf);
  2245 				ret = true;
  1990 			ret = true;
  2246 				break;
  1991 			break;
  2247 
  1992 
  2248 			case 0x18: // @todo Animation triggers
  1993 		case 0x18: // @todo Animation triggers
  2249 				grf_load_word(&buf);
  1994 			FOR_EACH_OBJECT grf_load_word(&buf);
  2250 				ret = true;
  1995 			ret = true;
  2251 				break;
  1996 			break;
  2252 
  1997 
  2253 			default:
  1998 		default:
  2254 				ret = true;
  1999 			ret = true;
  2255 				break;
  2000 			break;
  2256 		}
  2001 	}
  2257 	}
  2002 
  2258 
  2003 	*bufp = buf;
  2259 	*bufp = buf;
  2004 	return ret;
  2260 	return ret;
  2005 }
  2261 }
  2006 
  2262 
  2007 /* Action 0x00 */
  2263 /* Action 0x00 */
  2008 static void FeatureChangeInfo(byte *buf, int len)
  2264 static void FeatureChangeInfo(byte *buf, int len)
  2009 {
  2265 {
  2010 	byte *bufend = buf + len;
  2266 	byte *bufend = buf + len;
  2011 	uint i;
       
  2012 
  2267 
  2013 	/* <00> <feature> <num-props> <num-info> <id> (<property <new-info>)...
  2268 	/* <00> <feature> <num-props> <num-info> <id> (<property <new-info>)...
  2014 	 *
  2269 	 *
  2015 	 * B feature       0, 1, 2 or 3 for trains, road vehicles, ships or planes
  2270 	 * B feature       0, 1, 2 or 3 for trains, road vehicles, ships or planes
  2016 	 *                 4 for defining new train station sets
  2271 	 *                 4 for defining new train station sets
  2031 		/* GSF_STATION */      StationChangeInfo,
  2286 		/* GSF_STATION */      StationChangeInfo,
  2032 		/* GSF_CANAL */        NULL,
  2287 		/* GSF_CANAL */        NULL,
  2033 		/* GSF_BRIDGE */       BridgeChangeInfo,
  2288 		/* GSF_BRIDGE */       BridgeChangeInfo,
  2034 		/* GSF_TOWNHOUSE */    TownHouseChangeInfo,
  2289 		/* GSF_TOWNHOUSE */    TownHouseChangeInfo,
  2035 		/* GSF_GLOBALVAR */    GlobalVarChangeInfo,
  2290 		/* GSF_GLOBALVAR */    GlobalVarChangeInfo,
  2036 		/* GSF_INDUSTRYTILES */NULL,
  2291 		/* GSF_INDUSTRYTILES */IndustrytilesChangeInfo,
  2037 		/* GSF_INDUSTRIES */   NULL,
  2292 		/* GSF_INDUSTRIES */   IndustriesChangeInfo,
  2038 		/* GSF_CARGOS */       NULL, /* Cargo is handled during reservation */
  2293 		/* GSF_CARGOS */       NULL, /* Cargo is handled during reservation */
  2039 		/* GSF_SOUNDFX */      SoundEffectChangeInfo,
  2294 		/* GSF_SOUNDFX */      SoundEffectChangeInfo,
  2040 		/* GSF_FSMPORTS */     FSMPortChangeInfo,
  2295 		/* GSF_FSMPORTS */     FSMPortChangeInfo,
  2041 	};
  2296 	};
  2042 
       
  2043 	EngineInfo *ei = NULL;
       
  2044 
  2297 
  2045 	if (!check_length(len, 6, "FeatureChangeInfo")) return;
  2298 	if (!check_length(len, 6, "FeatureChangeInfo")) return;
  2046 	buf++;
  2299 	buf++;
  2047 	uint8 feature  = grf_load_byte(&buf);
  2300 	uint8 feature  = grf_load_byte(&buf);
  2048 	uint8 numprops = grf_load_byte(&buf);
  2301 	uint8 numprops = grf_load_byte(&buf);
  2060 	if (feature <= GSF_AIRCRAFT) {
  2313 	if (feature <= GSF_AIRCRAFT) {
  2061 		if (engine + numinfo > _vehcounts[feature]) {
  2314 		if (engine + numinfo > _vehcounts[feature]) {
  2062 			grfmsg(0, "FeatureChangeInfo: Last engine ID %d out of bounds (max %d), skipping", engine + numinfo, _vehcounts[feature]);
  2315 			grfmsg(0, "FeatureChangeInfo: Last engine ID %d out of bounds (max %d), skipping", engine + numinfo, _vehcounts[feature]);
  2063 			return;
  2316 			return;
  2064 		}
  2317 		}
  2065 		ei = &_engine_info[engine + _vehshifts[feature]];
       
  2066 	}
  2318 	}
  2067 
  2319 
  2068 	while (numprops-- && buf < bufend) {
  2320 	while (numprops-- && buf < bufend) {
  2069 		uint8 prop = grf_load_byte(&buf);
  2321 		uint8 prop = grf_load_byte(&buf);
  2070 		bool ignoring = false;
  2322 		bool ignoring = false;
  2071 
  2323 
  2072 		switch (feature) {
  2324 		switch (feature) {
  2073 			case GSF_TRAIN:
  2325 			case GSF_TRAIN:
  2074 			case GSF_ROAD:
  2326 			case GSF_ROAD:
  2075 			case GSF_SHIP:
  2327 			case GSF_SHIP:
  2076 			case GSF_AIRCRAFT:
  2328 			case GSF_AIRCRAFT: {
  2077 				/* Common properties for vehicles */
  2329 				bool handled = true;
  2078 				switch (prop) {
  2330 
  2079 					case 0x00: // Introduction date
  2331 				for (uint i = 0; i < numinfo; i++) {
  2080 						FOR_EACH_OBJECT ei[i].base_intro = grf_load_word(&buf) + DAYS_TILL_ORIGINAL_BASE_YEAR;
  2332 					EngineInfo *ei = &_engine_info[engine + _vehshifts[feature] + i];
  2081 						break;
  2333 
  2082 
  2334 					/* Common properties for vehicles */
  2083 					case 0x02: // Decay speed
  2335 					switch (prop) {
  2084 						FOR_EACH_OBJECT SB(ei[i].unk2, 0, 7, grf_load_byte(&buf) & 0x7F);
  2336 						case 0x00: // Introduction date
  2085 						break;
  2337 							ei->base_intro = grf_load_word(&buf) + DAYS_TILL_ORIGINAL_BASE_YEAR;
  2086 
  2338 							break;
  2087 					case 0x03: // Vehicle life
  2339 
  2088 						FOR_EACH_OBJECT ei[i].lifelength = grf_load_byte(&buf);
  2340 						case 0x02: // Decay speed
  2089 						break;
  2341 							SB(ei->unk2, 0, 7, grf_load_byte(&buf) & 0x7F);
  2090 
  2342 							break;
  2091 					case 0x04: // Model life
  2343 
  2092 						FOR_EACH_OBJECT ei[i].base_life = grf_load_byte(&buf);
  2344 						case 0x03: // Vehicle life
  2093 						break;
  2345 							ei->lifelength = grf_load_byte(&buf);
  2094 
  2346 							break;
  2095 					case 0x06: // Climates available
  2347 
  2096 						FOR_EACH_OBJECT ei[i].climates = grf_load_byte(&buf);
  2348 						case 0x04: // Model life
  2097 						break;
  2349 							ei->base_life = grf_load_byte(&buf);
  2098 
  2350 							break;
  2099 					case 0x07: // Loading speed
  2351 
  2100 						/* Hyronymus explained me what does
  2352 						case 0x06: // Climates available
  2101 						 * this mean and insists on having a
  2353 							ei->climates = grf_load_byte(&buf);
  2102 						 * credit ;-). --pasky */
  2354 							break;
  2103 						FOR_EACH_OBJECT ei[i].load_amount = grf_load_byte(&buf);
  2355 
  2104 						break;
  2356 						case 0x07: // Loading speed
  2105 
  2357 							/* Hyronymus explained me what does
  2106 					default:
  2358 							 * this mean and insists on having a
  2107 						if (handler[feature](engine, numinfo, prop, &buf, bufend - buf)) {
  2359 							 * credit ;-). --pasky */
  2108 							ignoring = true;
  2360 							ei->load_amount = grf_load_byte(&buf);
  2109 						}
  2361 							break;
  2110 						break;
  2362 
  2111 				}
  2363 						default:
  2112 				break;
  2364 							handled = false;
       
  2365 							break;
       
  2366 					}
       
  2367 				}
       
  2368 
       
  2369 				if (handled) break;
       
  2370 			} /* FALL THROUGH */
  2113 
  2371 
  2114 			default:
  2372 			default:
  2115 				if (handler[feature](engine, numinfo, prop, &buf, bufend - buf)) {
  2373 				if (handler[feature](engine, numinfo, prop, &buf, bufend - buf)) {
  2116 					ignoring = true;
  2374 					ignoring = true;
  2117 				}
  2375 				}
  2171 
  2429 
  2172 						free(_cur_grffile->cargo_list);
  2430 						free(_cur_grffile->cargo_list);
  2173 						_cur_grffile->cargo_max = numinfo;
  2431 						_cur_grffile->cargo_max = numinfo;
  2174 						_cur_grffile->cargo_list = MallocT<CargoLabel>(numinfo);
  2432 						_cur_grffile->cargo_list = MallocT<CargoLabel>(numinfo);
  2175 
  2433 
  2176 						int i;
  2434 						for (uint i = 0; i < numinfo; i++) {
  2177 						FOR_EACH_OBJECT {
       
  2178 							CargoLabel cl = grf_load_dword(&buf);
  2435 							CargoLabel cl = grf_load_dword(&buf);
  2179 							_cur_grffile->cargo_list[i] = BSWAP32(cl);
  2436 							_cur_grffile->cargo_list[i] = BSWAP32(cl);
  2180 						}
  2437 						}
  2181 						break;
  2438 						break;
  2182 				}
  2439 				}
  2206 		if (CargoChangeInfo(index, numinfo, prop, &buf, bufend - buf)) {
  2463 		if (CargoChangeInfo(index, numinfo, prop, &buf, bufend - buf)) {
  2207 			grfmsg(2, "ReserveChangeInfo: Ignoring property 0x%02X (not implemented)", prop);
  2464 			grfmsg(2, "ReserveChangeInfo: Ignoring property 0x%02X (not implemented)", prop);
  2208 		}
  2465 		}
  2209 	}
  2466 	}
  2210 }
  2467 }
  2211 
       
  2212 #undef FOR_EACH_OBJECT
       
  2213 
  2468 
  2214 /**
  2469 /**
  2215  * Creates a spritegroup representing a callback result
  2470  * Creates a spritegroup representing a callback result
  2216  * @param value The value that was used to represent this callback result
  2471  * @param value The value that was used to represent this callback result
  2217  * @return A spritegroup representing that callback result
  2472  * @return A spritegroup representing that callback result
  2279 	grfmsg(7, "New sprite set at %d of type %d, consisting of %d sets with %d views each (total %d)",
  2534 	grfmsg(7, "New sprite set at %d of type %d, consisting of %d sets with %d views each (total %d)",
  2280 		_cur_spriteid, feature, num_sets, num_ents, num_sets * num_ents
  2535 		_cur_spriteid, feature, num_sets, num_ents, num_sets * num_ents
  2281 	);
  2536 	);
  2282 
  2537 
  2283 	for (uint16 i = 0; i < num_sets * num_ents; i++) {
  2538 	for (uint16 i = 0; i < num_sets * num_ents; i++) {
  2284 		LoadNextSprite(_cur_spriteid++, _file_index);
  2539 		LoadNextSprite(_cur_spriteid++, _file_index, _nfo_line);
  2285 		_nfo_line++;
  2540 		_nfo_line++;
  2286 	}
  2541 	}
  2287 }
  2542 }
  2288 
  2543 
  2289 /* Action 0x01 (SKIP) */
  2544 /* Action 0x01 (SKIP) */
  2335 		return NULL;
  2590 		return NULL;
  2336 	}
  2591 	}
  2337 
  2592 
  2338 	if (feature != _cur_grffile->spriteset_feature) {
  2593 	if (feature != _cur_grffile->spriteset_feature) {
  2339 		grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set feature 0x%02X does not match action feature 0x%02X, skipping",
  2594 		grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set feature 0x%02X does not match action feature 0x%02X, skipping",
       
  2595 				setid, type,
  2340 				_cur_grffile->spriteset_feature, feature);
  2596 				_cur_grffile->spriteset_feature, feature);
  2341 		return NULL;
  2597 		return NULL;
  2342 	}
  2598 	}
  2343 
  2599 
  2344 	return NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteid * num_sprites, num_sprites);
  2600 	return NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteid * num_sprites, num_sprites);
  2533 					}
  2789 					}
  2534 
  2790 
  2535 					break;
  2791 					break;
  2536 				}
  2792 				}
  2537 
  2793 
  2538 				case GSF_TOWNHOUSE: {
  2794 				case GSF_TOWNHOUSE:
       
  2795 				case GSF_INDUSTRYTILES: {
  2539 					byte sprites     = _cur_grffile->spriteset_numents;
  2796 					byte sprites     = _cur_grffile->spriteset_numents;
  2540 					byte num_sprites = max((uint8)1, type);
  2797 					byte num_sprites = max((uint8)1, type);
  2541 					uint i;
  2798 					uint i;
  2542 
  2799 
  2543 					group = AllocateSpriteGroup();
  2800 					group = AllocateSpriteGroup();
  2613 					((DrawTileSeqStruct*)group->g.layout.dts->seq)[i].delta_x = (byte)0x80;
  2870 					((DrawTileSeqStruct*)group->g.layout.dts->seq)[i].delta_x = (byte)0x80;
  2614 
  2871 
  2615 					break;
  2872 					break;
  2616 				}
  2873 				}
  2617 
  2874 
       
  2875 				case GSF_INDUSTRIES: {
       
  2876 					if (type > 1) {
       
  2877 						grfmsg(1, "NewSpriteGroup: Unsupported industry production version %d, skipping", type);
       
  2878 						break;
       
  2879 					}
       
  2880 
       
  2881 					group = AllocateSpriteGroup();
       
  2882 					group->type = SGT_INDUSTRY_PRODUCTION;
       
  2883 					group->g.indprod.version = type;
       
  2884 					if (type == 0) {
       
  2885 						for (uint i = 0; i < 3; i++) {
       
  2886 							group->g.indprod.substract_input[i] = grf_load_word(&buf);
       
  2887 						}
       
  2888 						for (uint i = 0; i < 2; i++) {
       
  2889 							group->g.indprod.add_output[i] = grf_load_word(&buf);
       
  2890 						}
       
  2891 						group->g.indprod.again = grf_load_byte(&buf);
       
  2892 					} else {
       
  2893 						for (uint i = 0; i < 3; i++) {
       
  2894 							group->g.indprod.substract_input[i] = grf_load_byte(&buf);
       
  2895 						}
       
  2896 						for (uint i = 0; i < 2; i++) {
       
  2897 							group->g.indprod.add_output[i] = grf_load_byte(&buf);
       
  2898 						}
       
  2899 						group->g.indprod.again = grf_load_byte(&buf);
       
  2900 					}
       
  2901 					break;
       
  2902 				}
       
  2903 
  2618 				/* Loading of Tile Layout and Production Callback groups would happen here */
  2904 				/* Loading of Tile Layout and Production Callback groups would happen here */
  2619 				default: grfmsg(1, "NewSpriteGroup: Unsupported feature %d, skipping", feature);
  2905 				default: grfmsg(1, "NewSpriteGroup: Unsupported feature %d, skipping", feature);
  2620 			}
  2906 			}
  2621 		}
  2907 		}
  2622 	}
  2908 	}
  2739 		for (uint i = 0; i < idcount; i++) {
  3025 		for (uint i = 0; i < idcount; i++) {
  2740 			uint8 engine = buf[3 + i] + _vehshifts[feature];
  3026 			uint8 engine = buf[3 + i] + _vehshifts[feature];
  2741 
  3027 
  2742 			/* Don't tell me you don't love duplicated code! */
  3028 			/* Don't tell me you don't love duplicated code! */
  2743 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  3029 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2744 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  3030 				grfmsg(1, "VehicleMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  2745 				       groupid, _cur_grffile->spritegroups_count);
  3031 				       groupid, _cur_grffile->spritegroups_count);
  2746 				continue;
  3032 				continue;
  2747 			}
  3033 			}
  2748 
  3034 
  2749 			if (wagover) {
  3035 			if (wagover) {
  2858 
  3144 
  2859 		hs->spritegroup = _cur_grffile->spritegroups[groupid];
  3145 		hs->spritegroup = _cur_grffile->spritegroups[groupid];
  2860 	}
  3146 	}
  2861 }
  3147 }
  2862 
  3148 
       
  3149 static void IndustryMapSpriteGroup(byte *buf, uint8 idcount, uint8 cidcount)
       
  3150 {
       
  3151 	if (!HASBIT(_ttdpatch_flags[3], 0x07)) return;
       
  3152 
       
  3153 	byte *bp = &buf[4 + idcount + cidcount * 3];
       
  3154 	uint16 groupid = grf_load_word(&bp);
       
  3155 
       
  3156 	if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
       
  3157 		grfmsg(1, "IndustryMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping.",
       
  3158 		       groupid, _cur_grffile->spritegroups_count);
       
  3159 		return;
       
  3160 	}
       
  3161 
       
  3162 	for (uint i = 0; i < idcount; i++) {
       
  3163 		uint8 id = buf[3 + i];
       
  3164 		IndustrySpec *indsp = _cur_grffile->industryspec[id];
       
  3165 
       
  3166 		if (indsp == NULL) {
       
  3167 			grfmsg(1, "IndustryMapSpriteGroup: Too many industries defined, skipping");
       
  3168 			return;
       
  3169 		}
       
  3170 
       
  3171 		indsp->grf_prop.spritegroup = _cur_grffile->spritegroups[groupid];
       
  3172 	}
       
  3173 }
       
  3174 
       
  3175 static void IndustrytileMapSpriteGroup(byte *buf, uint8 idcount, uint8 cidcount)
       
  3176 {
       
  3177 	if (!HASBIT(_ttdpatch_flags[3], 0x07)) return;
       
  3178 
       
  3179 	byte *bp = &buf[4 + idcount + cidcount * 3];
       
  3180 	uint16 groupid = grf_load_word(&bp);
       
  3181 
       
  3182 	if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
       
  3183 		grfmsg(1, "IndustrytileMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping.",
       
  3184 		       groupid, _cur_grffile->spritegroups_count);
       
  3185 		return;
       
  3186 	}
       
  3187 
       
  3188 	for (uint i = 0; i < idcount; i++) {
       
  3189 		uint8 id = buf[3 + i];
       
  3190 		IndustryTileSpec *indtsp = _cur_grffile->indtspec[id];
       
  3191 
       
  3192 		if (indtsp == NULL) {
       
  3193 			grfmsg(1, "IndustrytileMapSpriteGroup: Too many industry tiles defined, skipping");
       
  3194 			return;
       
  3195 		}
       
  3196 
       
  3197 		indtsp->grf_prop.spritegroup = _cur_grffile->spritegroups[groupid];
       
  3198 	}
       
  3199 }
  2863 
  3200 
  2864 static void CargoMapSpriteGroup(byte *buf, uint8 idcount, uint8 cidcount)
  3201 static void CargoMapSpriteGroup(byte *buf, uint8 idcount, uint8 cidcount)
  2865 {
  3202 {
  2866 	byte *bp = &buf[4 + idcount + cidcount * 3];
  3203 	byte *bp = &buf[4 + idcount + cidcount * 3];
  2867 	uint16 groupid = grf_load_word(&bp);
  3204 	uint16 groupid = grf_load_word(&bp);
  2945 			StationMapSpriteGroup(buf, idcount, cidcount);
  3282 			StationMapSpriteGroup(buf, idcount, cidcount);
  2946 			return;
  3283 			return;
  2947 
  3284 
  2948 		case GSF_TOWNHOUSE:
  3285 		case GSF_TOWNHOUSE:
  2949 			TownHouseMapSpriteGroup(buf, idcount, cidcount);
  3286 			TownHouseMapSpriteGroup(buf, idcount, cidcount);
       
  3287 			return;
       
  3288 
       
  3289 		case GSF_INDUSTRIES:
       
  3290 			IndustryMapSpriteGroup(buf, idcount, cidcount);
       
  3291 			return;
       
  3292 
       
  3293 		case GSF_INDUSTRYTILES:
       
  3294 			IndustrytileMapSpriteGroup(buf, idcount, cidcount);
  2950 			return;
  3295 			return;
  2951 
  3296 
  2952 		case GSF_CARGOS:
  3297 		case GSF_CARGOS:
  2953 			CargoMapSpriteGroup(buf, idcount, cidcount);
  3298 			CargoMapSpriteGroup(buf, idcount, cidcount);
  2954 			return;
  3299 			return;
  3019 				} else {
  3364 				} else {
  3020 					AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, id);
  3365 					AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, id);
  3021 				}
  3366 				}
  3022 				break;
  3367 				break;
  3023 
  3368 
       
  3369 			case GSF_INDUSTRIES: {
       
  3370 				AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
       
  3371 				break;
       
  3372 			}
       
  3373 
  3024 			case GSF_TOWNHOUSE:
  3374 			case GSF_TOWNHOUSE:
  3025 			default:
  3375 			default:
  3026 				switch (GB(id, 8, 8)) {
  3376 				switch (GB(id, 8, 8)) {
  3027 					case 0xC4: // Station class name
  3377 					case 0xC4: // Station class name
  3028 						if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
  3378 						if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
  3069 						default:
  3419 						default:
  3070 							grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
  3420 							grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
  3071 					}
  3421 					}
  3072 					break;
  3422 					break;
  3073 
  3423 
  3074 				case GSF_INDUSTRIES :
       
  3075 				case 0x48 :   // for generic strings
       
  3076 					AddGRFString(_cur_spriteid, id, lang, name);
       
  3077 					break;
       
  3078 				default :
  3424 				default :
  3079 					grfmsg(7, "FeatureNewName: Unsupported feature (0x%02X)", feature);
  3425 					grfmsg(7, "FeatureNewName: Unsupported feature (0x%02X)", feature);
  3080 					break;
  3426 					break;
  3081 #endif
  3427 #endif
  3082 		}
  3428 		}
  3149 			replace = SPR_2CCMAP_BASE;
  3495 			replace = SPR_2CCMAP_BASE;
  3150 			break;
  3496 			break;
  3151 
  3497 
  3152 		case 0x0B: // tramways
  3498 		case 0x0B: // tramways
  3153 			if (num != 113) {
  3499 			if (num != 113) {
  3154 				grfmsg(1, "GraphicsNews: Tramway graphics sprite count must be 113, skipping");
  3500 				grfmsg(1, "GraphicsNew: Tramway graphics sprite count must be 113, skipping");
  3155 				return;
  3501 				return;
  3156 			}
  3502 			}
  3157 			replace = SPR_TRAMWAY_BASE;
  3503 			replace = SPR_TRAMWAY_BASE;
  3158 			break;
  3504 			break;
  3159 
  3505 
  3182 			break;
  3528 			break;
  3183 
  3529 
  3184 		default:
  3530 		default:
  3185 			grfmsg(2, "GraphicsNew: Custom graphics (type 0x%02X) sprite block of length %u (unimplemented, ignoring)",
  3531 			grfmsg(2, "GraphicsNew: Custom graphics (type 0x%02X) sprite block of length %u (unimplemented, ignoring)",
  3186 					type, num);
  3532 					type, num);
       
  3533 			_skip_sprites = num;
  3187 			return;
  3534 			return;
  3188 	}
  3535 	}
  3189 
  3536 
  3190 	if (replace == 0) {
  3537 	if (replace == 0) {
  3191 		grfmsg(2, "GraphicsNew: Loading %u sprites of type 0x%02X at SpriteID 0x%04X", num, type, _cur_spriteid);
  3538 		grfmsg(2, "GraphicsNew: Loading %u sprites of type 0x%02X at SpriteID 0x%04X", num, type, _cur_spriteid);
  3192 	} else {
  3539 	} else {
  3193 		grfmsg(2, "GraphicsNew: Replacing %u sprites of type 0x%02X at SpriteID 0x%04X", num, type, replace);
  3540 		grfmsg(2, "GraphicsNew: Replacing %u sprites of type 0x%02X at SpriteID 0x%04X", num, type, replace);
  3194 	}
  3541 	}
  3195 
  3542 
  3196 	for (; num > 0; num--) {
  3543 	for (; num > 0; num--) {
  3197 		LoadNextSprite(replace == 0 ? _cur_spriteid++ : replace++, _file_index);
  3544 		LoadNextSprite(replace == 0 ? _cur_spriteid++ : replace++, _file_index, _nfo_line);
  3198 		_nfo_line++;
  3545 		_nfo_line++;
  3199 	}
  3546 	}
  3200 }
  3547 }
  3201 
  3548 
  3202 /* Action 0x05 (SKIP) */
  3549 /* Action 0x05 (SKIP) */
  3221 			return clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
  3568 			return clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
  3222 
  3569 
  3223 		case 0x83: // current climate, 0=temp, 1=arctic, 2=trop, 3=toyland
  3570 		case 0x83: // current climate, 0=temp, 1=arctic, 2=trop, 3=toyland
  3224 			return _opt.landscape;
  3571 			return _opt.landscape;
  3225 
  3572 
  3226 		case 0x84: // GRF loading stage
  3573 		case 0x84: { // GRF loading stage
  3227 			return (_cur_stage > GLS_INIT) | ((_cur_stage == GLS_ACTIVATION) << 9);
  3574 			uint32 res = 0;
       
  3575 
       
  3576 			if (_cur_stage > GLS_INIT) SETBIT(res, 0);
       
  3577 			if (_cur_stage == GLS_RESERVE) SETBIT(res, 8);
       
  3578 			if (_cur_stage == GLS_ACTIVATION) SETBIT(res, 9);
       
  3579 			return res;
       
  3580 		}
  3228 
  3581 
  3229 		case 0x85: // TTDPatch flags, only for bit tests
  3582 		case 0x85: // TTDPatch flags, only for bit tests
  3230 			if (cond_val == NULL) {
  3583 			if (cond_val == NULL) {
  3231 				/* Supported in Action 0x07 and 0x09, not 0x0D */
  3584 				/* Supported in Action 0x07 and 0x09, not 0x0D */
  3232 				return 0;
  3585 				return 0;
  3505 		 * we use -1 to indicate that all further
  3858 		 * we use -1 to indicate that all further
  3506 		 * sprites should be skipped. */
  3859 		 * sprites should be skipped. */
  3507 		_skip_sprites = -1;
  3860 		_skip_sprites = -1;
  3508 
  3861 
  3509 		/* If an action 8 hasn't been encountered yet, disable the grf. */
  3862 		/* If an action 8 hasn't been encountered yet, disable the grf. */
  3510 		if (_cur_stage != GLS_RESERVE && _cur_grfconfig->status != GCS_ACTIVATED) _cur_grfconfig->status = GCS_DISABLED;
  3863 		if (_cur_grfconfig->status != GCS_ACTIVATED && _cur_grfconfig->status != GCS_INITIALISED) {
       
  3864 			_cur_grfconfig->status = GCS_DISABLED;
       
  3865 		}
  3511 	}
  3866 	}
  3512 }
  3867 }
  3513 
  3868 
  3514 
  3869 
  3515 /* Action 0x08 (GLS_FILESCAN) */
  3870 /* Action 0x08 (GLS_FILESCAN) */
  3555 	uint32 grfid     = grf_load_dword(&buf);
  3910 	uint32 grfid     = grf_load_dword(&buf);
  3556 	const char *name = grf_load_string(&buf, len - 6);
  3911 	const char *name = grf_load_string(&buf, len - 6);
  3557 
  3912 
  3558 	_cur_grffile->grfid = grfid;
  3913 	_cur_grffile->grfid = grfid;
  3559 	_cur_grffile->grf_version = version;
  3914 	_cur_grffile->grf_version = version;
  3560 	_cur_grfconfig->status = _cur_stage < GLS_ACTIVATION ? GCS_INITIALISED : GCS_ACTIVATED;
  3915 	_cur_grfconfig->status = _cur_stage < GLS_RESERVE ? GCS_INITIALISED : GCS_ACTIVATED;
  3561 
  3916 
  3562 	/* Do swap the GRFID for displaying purposes since people expect that */
  3917 	/* Do swap the GRFID for displaying purposes since people expect that */
  3563 	DEBUG(grf, 1, "GRFInfo: Loaded GRFv%d set %08lX - %s", version, BSWAP32(grfid), name);
  3918 	DEBUG(grf, 1, "GRFInfo: Loaded GRFv%d set %08lX - %s", version, BSWAP32(grfid), name);
  3564 }
  3919 }
  3565 
  3920 
  3584 		grfmsg(2, "SpriteReplace: [Set %d] Changing %d sprites, beginning with %d",
  3939 		grfmsg(2, "SpriteReplace: [Set %d] Changing %d sprites, beginning with %d",
  3585 			i, num_sprites, first_sprite
  3940 			i, num_sprites, first_sprite
  3586 		);
  3941 		);
  3587 
  3942 
  3588 		for (uint j = 0; j < num_sprites; j++) {
  3943 		for (uint j = 0; j < num_sprites; j++) {
  3589 			LoadNextSprite(first_sprite + j, _file_index); // XXX
  3944 			LoadNextSprite(first_sprite + j, _file_index, _nfo_line); // XXX
  3590 			_nfo_line++;
  3945 			_nfo_line++;
  3591 		}
  3946 		}
  3592 	}
  3947 	}
  3593 }
  3948 }
  3594 
  3949 
  3640 		STR_NEWGRF_ERROR_MSG_WARNING,
  3995 		STR_NEWGRF_ERROR_MSG_WARNING,
  3641 		STR_NEWGRF_ERROR_MSG_ERROR,
  3996 		STR_NEWGRF_ERROR_MSG_ERROR,
  3642 		STR_NEWGRF_ERROR_MSG_FATAL
  3997 		STR_NEWGRF_ERROR_MSG_FATAL
  3643 	};
  3998 	};
  3644 
  3999 
  3645 	/* AddGRFString expects the string to be referred to by an id in the newgrf
       
  3646 	 * file. Errors messages are never referred to however, so invent ids that
       
  3647 	 * are unlikely to be reached in a newgrf file so they don't overwrite
       
  3648 	 * anything else. */
       
  3649 	enum {
       
  3650 		MESSAGE_STRING_ID = MAX_UVALUE(StringID) - 1,
       
  3651 		MESSAGE_DATA_ID   = MAX_UVALUE(StringID)
       
  3652 	};
       
  3653 
       
  3654 	if (!check_length(len, 6, "GRFLoadError")) return;
  4000 	if (!check_length(len, 6, "GRFLoadError")) return;
  3655 
  4001 
  3656 	/* For now we can only show one message per newgrf file. */
  4002 	/* For now we can only show one message per newgrf file. */
  3657 	if (_cur_grfconfig->error != NULL) return;
  4003 	if (_cur_grfconfig->error != NULL) return;
  3658 
  4004 
  3660 	byte severity   = grf_load_byte(&buf);
  4006 	byte severity   = grf_load_byte(&buf);
  3661 	byte lang       = grf_load_byte(&buf);
  4007 	byte lang       = grf_load_byte(&buf);
  3662 	byte message_id = grf_load_byte(&buf);
  4008 	byte message_id = grf_load_byte(&buf);
  3663 	len -= 4;
  4009 	len -= 4;
  3664 
  4010 
       
  4011 	/* Skip the error if it isn't valid for the current language. */
       
  4012 	if (!CheckGrfLangID(lang, _cur_grffile->grf_version)) return;
       
  4013 
  3665 	/* Skip the error until the activation stage unless bit 7 of the severity
  4014 	/* Skip the error until the activation stage unless bit 7 of the severity
  3666 	 * is set. */
  4015 	 * is set. */
  3667 	if (!HASBIT(severity, 7) && _cur_stage < GLS_ACTIVATION) {
  4016 	if (!HASBIT(severity, 7) && _cur_stage == GLS_INIT) {
  3668 		grfmsg(7, "GRFLoadError: Skipping non-fatal GRFLoadError in stage 1");
  4017 		grfmsg(7, "GRFLoadError: Skipping non-fatal GRFLoadError in stage %d", _cur_stage);
  3669 		return;
  4018 		return;
  3670 	}
  4019 	}
  3671 	CLRBIT(severity, 7);
  4020 	CLRBIT(severity, 7);
  3672 
  4021 
  3673 	if (severity >= lengthof(sevstr)) {
  4022 	if (severity >= lengthof(sevstr)) {
  3689 	if (len <= 1) {
  4038 	if (len <= 1) {
  3690 		grfmsg(7, "GRFLoadError: No message data supplied.");
  4039 		grfmsg(7, "GRFLoadError: No message data supplied.");
  3691 		return;
  4040 		return;
  3692 	}
  4041 	}
  3693 
  4042 
  3694 	bool new_scheme = _cur_grffile->grf_version >= 7;
       
  3695 	GRFError *error = CallocT<GRFError>(1);
  4043 	GRFError *error = CallocT<GRFError>(1);
  3696 
  4044 
  3697 	error->severity = sevstr[severity];
  4045 	error->severity = sevstr[severity];
  3698 
  4046 
  3699 	if (message_id == 0xFF) {
  4047 	if (message_id == 0xFF) {
  3700 		/* This is a custom error message. */
  4048 		/* This is a custom error message. */
  3701 		const char *message = grf_load_string(&buf, len);
  4049 		const char *message = grf_load_string(&buf, len);
  3702 		len -= (strlen(message) + 1);
  4050 		len -= (strlen(message) + 1);
  3703 
  4051 
  3704 		error->message = AddGRFString(_cur_grffile->grfid, MESSAGE_STRING_ID, lang, new_scheme, message, STR_UNDEFINED);
  4052 		error->custom_message = TranslateTTDPatchCodes(message);
  3705 	} else {
  4053 	} else {
  3706 		error->message = msgstr[message_id];
  4054 		error->message = msgstr[message_id];
  3707 	}
  4055 	}
  3708 
  4056 
  3709 	if (len > 0) {
  4057 	if (len > 0) {
  3710 		const char *data = grf_load_string(&buf, len);
  4058 		const char *data = grf_load_string(&buf, len);
  3711 		len -= (strlen(data) + 1);
  4059 		len -= (strlen(data) + 1);
  3712 
  4060 
  3713 		error->data = AddGRFString(_cur_grffile->grfid, MESSAGE_DATA_ID, lang, new_scheme, data, STR_UNDEFINED);
  4061 		error->data = TranslateTTDPatchCodes(data);
  3714 	}
  4062 	}
  3715 
  4063 
  3716 	/* Only two parameter numbers can be used in the string. */
  4064 	/* Only two parameter numbers can be used in the string. */
  3717 	uint i = 0;
  4065 	uint i = 0;
  3718 	for (; i < 2 && len > 0; i++) {
  4066 	for (; i < 2 && len > 0; i++) {
  4129 			file->status = GCS_DISABLED;
  4477 			file->status = GCS_DISABLED;
  4130 		}
  4478 		}
  4131 	}
  4479 	}
  4132 }
  4480 }
  4133 
  4481 
       
  4482 /* Action 0x0F */
       
  4483 static void FeatureTownName(byte *buf, int len)
       
  4484 {
       
  4485 	/* <0F> <id> <style-name> <num-parts> <parts>
       
  4486 	 *
       
  4487 	 * B id          ID of this definition in bottom 7 bits (final definition if bit 7 set)
       
  4488 	 * V style-name  Name of the style (only for final definition)
       
  4489 	 * B num-parts   Number of parts in this definition
       
  4490 	 * V parts       The parts */
       
  4491 
       
  4492 	if (!check_length(len, 1, "FeatureTownName: definition ID")) return;
       
  4493 	buf++; len--;
       
  4494 
       
  4495 	uint32 grfid = _cur_grffile->grfid;
       
  4496 
       
  4497 	GRFTownName *townname = AddGRFTownName(grfid);
       
  4498 
       
  4499 	byte id = grf_load_byte(&buf);
       
  4500 	len--;
       
  4501 	grfmsg(6, "FeatureTownName: definition 0x%02X", id & 0x7F);
       
  4502 
       
  4503 	if (HASBIT(id, 7)) {
       
  4504 		/* Final definition */
       
  4505 		CLRBIT(id, 7);
       
  4506 		bool new_scheme = _cur_grffile->grf_version >= 7;
       
  4507 
       
  4508 		if (!check_length(len, 1, "FeatureTownName: lang_id")) return;
       
  4509 		byte lang = grf_load_byte(&buf);
       
  4510 		len--;
       
  4511 
       
  4512 		byte nb_gen = townname->nb_gen;
       
  4513 		do {
       
  4514 			CLRBIT(lang, 7);
       
  4515 
       
  4516 			if (!check_length(len, 1, "FeatureTownName: style name")) return;
       
  4517 			const char *name = grf_load_string(&buf, len);
       
  4518 			len -= strlen(name) + 1;
       
  4519 			grfmsg(6, "FeatureTownName: lang 0x%X -> '%s'", lang, TranslateTTDPatchCodes(name));
       
  4520 
       
  4521 			townname->name[nb_gen] = AddGRFString(grfid, id, lang, new_scheme, name, STR_UNDEFINED);
       
  4522 
       
  4523 			if (!check_length(len, 1, "FeatureTownName: lang_id")) return;
       
  4524 			lang = grf_load_byte(&buf);
       
  4525 			len--;
       
  4526 		} while (lang != 0);
       
  4527 		townname->id[nb_gen] = id;
       
  4528 		townname->nb_gen++;
       
  4529 	}
       
  4530 
       
  4531 	if (!check_length(len, 1, "FeatureTownName: number of parts")) return;
       
  4532 	byte nb = grf_load_byte(&buf);
       
  4533 	len--;
       
  4534 	grfmsg(6, "FeatureTownName: %d parts", nb, nb);
       
  4535 
       
  4536 	townname->nbparts[id] = nb;
       
  4537 	townname->partlist[id] = CallocT<NamePartList>(nb);
       
  4538 
       
  4539 	for (int i = 0; i < nb; i++) {
       
  4540 		if (!check_length(len, 3, "FeatureTownName: parts header")) return;
       
  4541 		byte nbtext =  grf_load_byte(&buf);
       
  4542 		townname->partlist[id][i].bitstart  = grf_load_byte(&buf);
       
  4543 		townname->partlist[id][i].bitcount  = grf_load_byte(&buf);
       
  4544 		townname->partlist[id][i].maxprob   = 0;
       
  4545 		townname->partlist[id][i].partcount = nbtext;
       
  4546 		townname->partlist[id][i].parts     = CallocT<NamePart>(nbtext);
       
  4547 		len -= 3;
       
  4548 		grfmsg(6, "FeatureTownName: part %d contains %d texts and will use GB(seed, %d, %d)", i, nbtext, townname->partlist[id][i].bitstart, townname->partlist[id][i].bitcount);
       
  4549 
       
  4550 		for (int j = 0; j < nbtext; j++) {
       
  4551 			if (!check_length(len, 2, "FeatureTownName: part")) return;
       
  4552 			byte prob = grf_load_byte(&buf);
       
  4553 			len--;
       
  4554 
       
  4555 			if (HASBIT(prob, 7)) {
       
  4556 				byte ref_id = grf_load_byte(&buf);
       
  4557 				len--;
       
  4558 
       
  4559 				if (townname->nbparts[ref_id] == 0) {
       
  4560 					grfmsg(0, "FeatureTownName: definition 0x%02X doesn't exist, deactivating", ref_id);
       
  4561 					DelGRFTownName(grfid);
       
  4562 					_cur_grfconfig->status = GCS_DISABLED;
       
  4563 					_skip_sprites = -1;
       
  4564 					return;
       
  4565 				}
       
  4566 
       
  4567 				grfmsg(6, "FeatureTownName: part %d, text %d, uses intermediate definition 0x%02X (with probability %d)", i, j, ref_id, prob & 0x7F);
       
  4568 				townname->partlist[id][i].parts[j].data.id = ref_id;
       
  4569 			} else {
       
  4570 				const char *text = grf_load_string(&buf, len);
       
  4571 				len -= strlen(text) + 1;
       
  4572 				townname->partlist[id][i].parts[j].data.text = TranslateTTDPatchCodes(text);
       
  4573 				grfmsg(6, "FeatureTownName: part %d, text %d, '%s' (with probability %d)", i, j, townname->partlist[id][i].parts[j].data.text, prob);
       
  4574 			}
       
  4575 			townname->partlist[id][i].parts[j].prob = prob;
       
  4576 			townname->partlist[id][i].maxprob += GB(prob, 0, 7);
       
  4577 		}
       
  4578 		grfmsg(6, "FeatureTownName: part %d, total probability %d", i, townname->partlist[id][i].maxprob);
       
  4579 	}
       
  4580 }
       
  4581 
  4134 /* Action 0x10 */
  4582 /* Action 0x10 */
  4135 static void DefineGotoLabel(byte *buf, int len)
  4583 static void DefineGotoLabel(byte *buf, int len)
  4136 {
  4584 {
  4137 	/* <10> <label> [<comment>]
  4585 	/* <10> <label> [<comment>]
  4138 	 *
  4586 	 *
  4314 
  4762 
  4315 		grfmsg(7, "LoadFontGlyph: Loading %u glyph(s) at 0x%04X for size %u", num_char, base_char, size);
  4763 		grfmsg(7, "LoadFontGlyph: Loading %u glyph(s) at 0x%04X for size %u", num_char, base_char, size);
  4316 
  4764 
  4317 		for (uint c = 0; c < num_char; c++) {
  4765 		for (uint c = 0; c < num_char; c++) {
  4318 			SetUnicodeGlyph(size, base_char + c, _cur_spriteid);
  4766 			SetUnicodeGlyph(size, base_char + c, _cur_spriteid);
  4319 			LoadNextSprite(_cur_spriteid++, _file_index);
  4767 			LoadNextSprite(_cur_spriteid++, _file_index, _nfo_line);
  4320 			_nfo_line++;
  4768 			_nfo_line++;
  4321 		}
  4769 		}
  4322 	}
  4770 	}
  4323 }
  4771 }
  4324 
  4772 
  4344 
  4792 
  4345 	if (c->status == GCS_INITIALISED) {
  4793 	if (c->status == GCS_INITIALISED) {
  4346 		/* If the file is not active but will be activated later, give an error
  4794 		/* If the file is not active but will be activated later, give an error
  4347 		 * and disable this file. */
  4795 		 * and disable this file. */
  4348 		GRFError *error = CallocT<GRFError>(1);
  4796 		GRFError *error = CallocT<GRFError>(1);
       
  4797 
       
  4798 		char tmp[256];
       
  4799 		GetString(tmp, STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE, lastof(tmp));
       
  4800 		error->data = strdup(tmp);
       
  4801 
  4349 		error->message  = STR_NEWGRF_ERROR_LOAD_AFTER;
  4802 		error->message  = STR_NEWGRF_ERROR_LOAD_AFTER;
  4350 		error->data     = STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE;
       
  4351 		error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
  4803 		error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
  4352 
  4804 
  4353 		if (_cur_grfconfig->error != NULL) free(_cur_grfconfig->error);
  4805 		if (_cur_grfconfig->error != NULL) free(_cur_grfconfig->error);
  4354 		_cur_grfconfig->error = error;
  4806 		_cur_grfconfig->error = error;
  4355 
  4807 
  4456 	                   |                                        (1 << 0x03)  // semaphores
  4908 	                   |                                        (1 << 0x03)  // semaphores
  4457 	                   |                                        (0 << 0x0B)  // enhancedgui
  4909 	                   |                                        (0 << 0x0B)  // enhancedgui
  4458 	                   |                                        (0 << 0x0C)  // newagerating
  4910 	                   |                                        (0 << 0x0C)  // newagerating
  4459 	                   |       ((_patches.build_on_slopes ? 1 : 0) << 0x0D)  // buildonslopes
  4911 	                   |       ((_patches.build_on_slopes ? 1 : 0) << 0x0D)  // buildonslopes
  4460 	                   |         ((_patches.full_load_any ? 1 : 0) << 0x0E)  // fullloadany
  4912 	                   |         ((_patches.full_load_any ? 1 : 0) << 0x0E)  // fullloadany
  4461 	                   |                                        (0 << 0x0F)  // planespeed
  4913 	                   |                                        (1 << 0x0F)  // planespeed - TODO depends on patch when implemented
  4462 	                   |                                        (0 << 0x10)  // moreindustriesperclimate - obsolete
  4914 	                   |                                        (0 << 0x10)  // moreindustriesperclimate - obsolete
  4463 	                   |                                        (0 << 0x11)  // moretoylandfeatures
  4915 	                   |                                        (0 << 0x11)  // moretoylandfeatures
  4464 	                   |                                        (1 << 0x12)  // newstations
  4916 	                   |                                        (1 << 0x12)  // newstations
  4465 	                   |                                        (0 << 0x13)  // tracktypecostdiff
  4917 	                   |                                        (0 << 0x13)  // tracktypecostdiff
  4466 	                   |                                        (1 << 0x14)  // manualconvert
  4918 	                   |                                        (1 << 0x14)  // manualconvert
  4468 	                   |                                        (1 << 0x16)  // canals
  4920 	                   |                                        (1 << 0x16)  // canals
  4469 	                   |                                        (1 << 0x17)  // newstartyear
  4921 	                   |                                        (1 << 0x17)  // newstartyear
  4470 	                   |    ((_patches.freight_trains > 1 ? 1 : 0) << 0x18)  // freighttrains
  4922 	                   |    ((_patches.freight_trains > 1 ? 1 : 0) << 0x18)  // freighttrains
  4471 	                   |                                        (1 << 0x19)  // newhouses
  4923 	                   |                                        (1 << 0x19)  // newhouses
  4472 	                   |                                        (1 << 0x1A)  // newbridges
  4924 	                   |                                        (1 << 0x1A)  // newbridges
  4473 	                   |                                        (0 << 0x1B)  // newtownnames
  4925 	                   |                                        (1 << 0x1B)  // newtownnames
  4474 	                   |                                        (0 << 0x1C)  // moreanimations
  4926 	                   |                                        (0 << 0x1C)  // moreanimations
  4475 	                   |    ((_patches.wagon_speed_limits ? 1 : 0) << 0x1D)  // wagonspeedlimits
  4927 	                   |    ((_patches.wagon_speed_limits ? 1 : 0) << 0x1D)  // wagonspeedlimits
  4476 	                   |                                        (1 << 0x1E)  // newshistory
  4928 	                   |                                        (1 << 0x1E)  // newshistory
  4477 	                   |                                        (0 << 0x1F); // custombridgeheads
  4929 	                   |                                        (0 << 0x1F); // custombridgeheads
  4478 
  4930 
  4496 	                   |                                        (1 << 0x11)  // autoreplace
  4948 	                   |                                        (1 << 0x11)  // autoreplace
  4497 	                   |                                        (1 << 0x12)  // autoslope
  4949 	                   |                                        (1 << 0x12)  // autoslope
  4498 	                   |                                        (0 << 0x13)  // followvehicle
  4950 	                   |                                        (0 << 0x13)  // followvehicle
  4499 	                   |                                        (1 << 0x14)  // trams
  4951 	                   |                                        (1 << 0x14)  // trams
  4500 	                   |                                        (0 << 0x15)  // enhancetunnels
  4952 	                   |                                        (0 << 0x15)  // enhancetunnels
  4501 	                   |                                        (0 << 0x16)  // shortrvs
  4953 	                   |                                        (1 << 0x16)  // shortrvs
  4502 	                   |                                        (0 << 0x17)  // articulatedrvs
  4954 	                   |                                        (1 << 0x17)  // articulatedrvs
  4503 	                   |                                        (1 << 0x1E); // variablerunningcosts
  4955 	                   |                                        (1 << 0x1E); // variablerunningcosts
  4504 }
  4956 }
  4505 
  4957 
  4506 static void ResetCustomStations()
  4958 static void ResetCustomStations()
  4507 {
  4959 {
  4546 	GRFFile *file;
  4998 	GRFFile *file;
  4547 	uint i;
  4999 	uint i;
  4548 
  5000 
  4549 	for (file = _first_grffile; file != NULL; file = file->next) {
  5001 	for (file = _first_grffile; file != NULL; file = file->next) {
  4550 		if (file->housespec == NULL) continue;
  5002 		if (file->housespec == NULL) continue;
  4551 		for (i = 0; i < HOUSE_MAX; i++) free(file->housespec[i]);
  5003 		for (i = 0; i < HOUSE_MAX; i++) {
       
  5004 			free(file->housespec[i]);
       
  5005 		}
  4552 
  5006 
  4553 		free(file->housespec);
  5007 		free(file->housespec);
  4554 		file->housespec = NULL;
  5008 		file->housespec = NULL;
  4555 	}
  5009 	}
  4556 }
  5010 }
  4624 
  5078 
  4625 	_first_grffile = NULL;
  5079 	_first_grffile = NULL;
  4626 	_cur_grffile   = NULL;
  5080 	_cur_grffile   = NULL;
  4627 }
  5081 }
  4628 
  5082 
       
  5083 static void ResetNewGRFErrors()
       
  5084 {
       
  5085 	for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
       
  5086 		if (!HASBIT(c->flags, GCF_COPY) && c->error != NULL) {
       
  5087 			free(c->error->custom_message);
       
  5088 			free(c->error->data);
       
  5089 			free(c->error);
       
  5090 			c->error = NULL;
       
  5091 		}
       
  5092 	}
       
  5093 }
       
  5094 
  4629 /**
  5095 /**
  4630  * Reset all NewGRF loaded data
  5096  * Reset all NewGRF loaded data
  4631  * TODO
  5097  * TODO
  4632  */
  5098  */
  4633 static void ResetNewGRFData()
  5099 static void ResetNewGRFData()
  4634 {
  5100 {
  4635 	CleanUpStrings();
  5101 	CleanUpStrings();
       
  5102 	CleanUpGRFTownNames();
  4636 
  5103 
  4637 	/* Copy/reset original engine info data */
  5104 	/* Copy/reset original engine info data */
  4638 	memcpy(&_engine_info, &orig_engine_info, sizeof(orig_engine_info));
  5105 	memcpy(&_engine_info, &orig_engine_info, sizeof(orig_engine_info));
  4639 	memcpy(&_rail_vehicle_info, &orig_rail_vehicle_info, sizeof(orig_rail_vehicle_info));
  5106 	memcpy(&_rail_vehicle_info, &orig_rail_vehicle_info, sizeof(orig_rail_vehicle_info));
  4640 	memcpy(&_ship_vehicle_info, &orig_ship_vehicle_info, sizeof(orig_ship_vehicle_info));
  5107 	memcpy(&_ship_vehicle_info, &orig_ship_vehicle_info, sizeof(orig_ship_vehicle_info));
  4691 	ClearSnowLine();
  5158 	ClearSnowLine();
  4692 
  5159 
  4693 	/* Reset NewGRF files */
  5160 	/* Reset NewGRF files */
  4694 	ResetNewGRF();
  5161 	ResetNewGRF();
  4695 
  5162 
       
  5163 	/* Reset NewGRF errors. */
       
  5164 	ResetNewGRFErrors();
       
  5165 
  4696 	/* Add engine type to engine data. This is needed for the refit precalculation. */
  5166 	/* Add engine type to engine data. This is needed for the refit precalculation. */
  4697 	AddTypeToEngines();
  5167 	AddTypeToEngines();
  4698 
  5168 
  4699 	/* Set up the default cargo types */
  5169 	/* Set up the default cargo types */
  4700 	SetupCargoForClimate(_opt.landscape);
  5170 	SetupCargoForClimate(_opt.landscape);
  4702 	/* Reset misc GRF features and train list display variables */
  5172 	/* Reset misc GRF features and train list display variables */
  4703 	_misc_grf_features = 0;
  5173 	_misc_grf_features = 0;
  4704 	_traininfo_vehicle_pitch = 0;
  5174 	_traininfo_vehicle_pitch = 0;
  4705 	_traininfo_vehicle_width = 29;
  5175 	_traininfo_vehicle_width = 29;
  4706 
  5176 
  4707 	_loaded_newgrf_features = 0;
  5177 	_loaded_newgrf_features.has_2CC           = false;
       
  5178 	_loaded_newgrf_features.has_newhouses     = false;
       
  5179 	_loaded_newgrf_features.has_newindustries = false,
  4708 
  5180 
  4709 	_signal_base = 0;
  5181 	_signal_base = 0;
  4710 	_coast_base = 0;
  5182 	_coast_base = 0;
  4711 
  5183 
  4712 	InitializeSoundPool();
  5184 	InitializeSoundPool();
  4765 
  5237 
  4766 	newfile = CallocT<GRFFile>(1);
  5238 	newfile = CallocT<GRFFile>(1);
  4767 
  5239 
  4768 	if (newfile == NULL) error ("Out of memory");
  5240 	if (newfile == NULL) error ("Out of memory");
  4769 
  5241 
  4770 	newfile->filename = strdup(config->full_path);
  5242 	newfile->filename = strdup(config->filename);
  4771 	newfile->sprite_offset = sprite_offset;
  5243 	newfile->sprite_offset = sprite_offset;
  4772 
  5244 
  4773 	/* Copy the initial parameter list */
  5245 	/* Copy the initial parameter list */
  4774 	assert(lengthof(newfile->param) == lengthof(config->param) && lengthof(config->param) == 0x80);
  5246 	assert(lengthof(newfile->param) == lengthof(config->param) && lengthof(config->param) == 0x80);
  4775 	newfile->param_end = config->num_params;
  5247 	newfile->param_end = config->num_params;
  4939 			if (hs->enabled && hs->min_date == 1930) hs->min_date = 0;
  5411 			if (hs->enabled && hs->min_date == 1930) hs->min_date = 0;
  4940 		}
  5412 		}
  4941 	}
  5413 	}
  4942 }
  5414 }
  4943 
  5415 
       
  5416 /** Add all new industries to the industry array. Industry properties can be set at any
       
  5417  * time in the GRF file, so we can only add a industry spec to the industry array
       
  5418  * after the file has finished loading. */
       
  5419 static void FinaliseIndustriesArray()
       
  5420 {
       
  5421 	for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
       
  5422 		if (file->industryspec != NULL) {
       
  5423 			for (int i = 0; i < NUM_INDUSTRYTYPES; i++) {
       
  5424 				IndustrySpec *indsp = file->industryspec[i];
       
  5425 
       
  5426 				if (indsp != NULL && indsp->enabled) {
       
  5427 					StringID strid;
       
  5428 					/* process the conversion of text at the end, so to be sure everything will be fine
       
  5429 					 * and available.  Check if it does not return undefind marker, which is a very good sign of a
       
  5430 					 * substitute industry who has not changed the string been examined, thus using it as such */
       
  5431 					strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->name);
       
  5432 					if (strid != STR_UNDEFINED) indsp->name = strid;
       
  5433 
       
  5434 					strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->closure_text);
       
  5435 					if (strid != STR_UNDEFINED) indsp->closure_text = strid;
       
  5436 
       
  5437 					strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->production_up_text);
       
  5438 					if (strid != STR_UNDEFINED) indsp->production_up_text = strid;
       
  5439 
       
  5440 					strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->production_down_text);
       
  5441 					if (strid != STR_UNDEFINED) indsp->production_down_text = strid;
       
  5442 
       
  5443 					strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->new_industry_text);
       
  5444 					if (strid != STR_UNDEFINED) indsp->new_industry_text = strid;
       
  5445 
       
  5446 					_industry_mngr.SetEntitySpec(indsp);
       
  5447 					_loaded_newgrf_features.has_newindustries = true;
       
  5448 				}
       
  5449 			}
       
  5450 		}
       
  5451 
       
  5452 		if (file->indtspec != NULL) {
       
  5453 			for (int i = 0; i < NUM_INDUSTRYTILES; i++) {
       
  5454 				IndustryTileSpec *indtsp = file->indtspec[i];
       
  5455 				if (indtsp != NULL) {
       
  5456 					_industile_mngr.SetEntitySpec(indtsp);
       
  5457 				}
       
  5458 			}
       
  5459 		}
       
  5460 	}
       
  5461 
       
  5462 	for (uint j = 0; j < NUM_INDUSTRYTYPES; j++) {
       
  5463 		IndustrySpec *indsp = &_industry_specs[j];
       
  5464 		if (indsp->enabled && indsp->grf_prop.grffile != NULL) {
       
  5465 			for (uint i = 0; i < 3; i++) {
       
  5466 				indsp->conflicting[i] = MapNewGRFIndustryType(indsp->conflicting[i], indsp->grf_prop.grffile->grfid);
       
  5467 			}
       
  5468 		}
       
  5469 	}
       
  5470 }
  4944 
  5471 
  4945 /** Each cargo string needs to be mapped from TTDPatch to OpenTTD string IDs.
  5472 /** Each cargo string needs to be mapped from TTDPatch to OpenTTD string IDs.
  4946  * This is done after loading so that strings from Action 4 will be mapped
  5473  * This is done after loading so that strings from Action 4 will be mapped
  4947  * properly. */
  5474  * properly. */
  4948 static void MapNewCargoStrings()
  5475 static void MapNewCargoStrings()
  4988 		/* 0x03 */ { NULL,     GRFUnsafe, NULL,            NULL,           NULL,              FeatureMapSpriteGroup, },
  5515 		/* 0x03 */ { NULL,     GRFUnsafe, NULL,            NULL,           NULL,              FeatureMapSpriteGroup, },
  4989 		/* 0x04 */ { NULL,     NULL,      NULL,            NULL,           NULL,              FeatureNewName, },
  5516 		/* 0x04 */ { NULL,     NULL,      NULL,            NULL,           NULL,              FeatureNewName, },
  4990 		/* 0x05 */ { SkipAct5, SkipAct5,  SkipAct5,        SkipAct5,       SkipAct5,          GraphicsNew, },
  5517 		/* 0x05 */ { SkipAct5, SkipAct5,  SkipAct5,        SkipAct5,       SkipAct5,          GraphicsNew, },
  4991 		/* 0x06 */ { NULL,     NULL,      NULL,            CfgApply,       CfgApply,          CfgApply, },
  5518 		/* 0x06 */ { NULL,     NULL,      NULL,            CfgApply,       CfgApply,          CfgApply, },
  4992 		/* 0x07 */ { NULL,     NULL,      NULL,            NULL,           SkipIf,            SkipIf, },
  5519 		/* 0x07 */ { NULL,     NULL,      NULL,            NULL,           SkipIf,            SkipIf, },
  4993 		/* 0x08 */ { ScanInfo, NULL,      NULL,            GRFInfo,        NULL,              GRFInfo, },
  5520 		/* 0x08 */ { ScanInfo, NULL,      NULL,            GRFInfo,        GRFInfo,           GRFInfo, },
  4994 		/* 0x09 */ { NULL,     NULL,      NULL,            SkipIf,         SkipIf,            SkipIf, },
  5521 		/* 0x09 */ { NULL,     NULL,      NULL,            SkipIf,         SkipIf,            SkipIf, },
  4995 		/* 0x0A */ { SkipActA, SkipActA,  SkipActA,        SkipActA,       SkipActA,          SpriteReplace, },
  5522 		/* 0x0A */ { SkipActA, SkipActA,  SkipActA,        SkipActA,       SkipActA,          SpriteReplace, },
  4996 		/* 0x0B */ { NULL,     NULL,      NULL,            GRFLoadError,   GRFLoadError,      GRFLoadError, },
  5523 		/* 0x0B */ { NULL,     NULL,      NULL,            GRFLoadError,   GRFLoadError,      GRFLoadError, },
  4997 		/* 0x0C */ { NULL,     NULL,      NULL,            GRFComment,     NULL,              GRFComment, },
  5524 		/* 0x0C */ { NULL,     NULL,      NULL,            GRFComment,     NULL,              GRFComment, },
  4998 		/* 0x0D */ { NULL,     SafeParamSet, NULL,         ParamSet,       ParamSet,          ParamSet, },
  5525 		/* 0x0D */ { NULL,     SafeParamSet, NULL,         ParamSet,       ParamSet,          ParamSet, },
  4999 		/* 0x0E */ { NULL,     SafeGRFInhibit, NULL,       GRFInhibit,     GRFInhibit,        GRFInhibit, },
  5526 		/* 0x0E */ { NULL,     SafeGRFInhibit, NULL,       GRFInhibit,     GRFInhibit,        GRFInhibit, },
  5000 		/* 0x0F */ { NULL,     NULL,      NULL,            NULL,           NULL,              NULL, },
  5527 		/* 0x0F */ { NULL,     GRFUnsafe, NULL,            FeatureTownName, NULL,             NULL, },
  5001 		/* 0x10 */ { NULL,     NULL,      DefineGotoLabel, NULL,           NULL,              NULL, },
  5528 		/* 0x10 */ { NULL,     NULL,      DefineGotoLabel, NULL,           NULL,              NULL, },
  5002 		/* 0x11 */ { NULL,     GRFUnsafe, NULL,            NULL,           NULL,              GRFSound, },
  5529 		/* 0x11 */ { NULL,     GRFUnsafe, NULL,            NULL,           NULL,              GRFSound, },
  5003 		/* 0x12 */ { NULL,     NULL,      NULL,            NULL,           NULL,              LoadFontGlyph, },
  5530 		/* 0x12 */ { NULL,     NULL,      NULL,            NULL,           NULL,              LoadFontGlyph, },
  5004 		/* 0x13 */ { NULL,     NULL,      NULL,            NULL,           NULL,              TranslateGRFStrings, },
  5531 		/* 0x13 */ { NULL,     NULL,      NULL,            NULL,           NULL,              TranslateGRFStrings, },
  5005 	};
  5532 	};
  5042 }
  5569 }
  5043 
  5570 
  5044 
  5571 
  5045 void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage)
  5572 void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage)
  5046 {
  5573 {
  5047 	const char *filename = config->full_path;
  5574 	const char *filename = config->filename;
  5048 	uint16 num;
  5575 	uint16 num;
  5049 
  5576 
  5050 	/* A .grf file is activated only if it was active when the game was
  5577 	/* A .grf file is activated only if it was active when the game was
  5051 	 * started.  If a game is loaded, only its active .grfs will be
  5578 	 * started.  If a game is loaded, only its active .grfs will be
  5052 	 * reactivated, unless "loadallgraphics on" is used.  A .grf file is
  5579 	 * reactivated, unless "loadallgraphics on" is used.  A .grf file is
  5057 	 * carried out.  All others are ignored, because they only need to be
  5584 	 * carried out.  All others are ignored, because they only need to be
  5058 	 * processed once at initialization.  */
  5585 	 * processed once at initialization.  */
  5059 	if (stage != GLS_FILESCAN && stage != GLS_SAFETYSCAN && stage != GLS_LABELSCAN) {
  5586 	if (stage != GLS_FILESCAN && stage != GLS_SAFETYSCAN && stage != GLS_LABELSCAN) {
  5060 		_cur_grffile = GetFileByFilename(filename);
  5587 		_cur_grffile = GetFileByFilename(filename);
  5061 		if (_cur_grffile == NULL) error("File '%s' lost in cache.\n", filename);
  5588 		if (_cur_grffile == NULL) error("File '%s' lost in cache.\n", filename);
       
  5589 		if (stage == GLS_RESERVE && config->status != GCS_INITIALISED) return;
  5062 		if (stage == GLS_ACTIVATION && config->status != GCS_INITIALISED) return;
  5590 		if (stage == GLS_ACTIVATION && config->status != GCS_INITIALISED) return;
  5063 	}
  5591 	}
  5064 
  5592 
  5065 	FioOpenFile(file_index, filename);
  5593 	FioOpenFile(file_index, filename);
  5066 	_file_index = file_index; // XXX
  5594 	_file_index = file_index; // XXX
  5124 	}
  5652 	}
  5125 }
  5653 }
  5126 
  5654 
  5127 void InitDepotWindowBlockSizes();
  5655 void InitDepotWindowBlockSizes();
  5128 
  5656 
       
  5657 extern void SortTownGeneratorNames();
       
  5658 
  5129 static void AfterLoadGRFs()
  5659 static void AfterLoadGRFs()
  5130 {
  5660 {
  5131 	/* Update the bitmasks for the vehicle lists */
  5661 	/* Update the bitmasks for the vehicle lists */
  5132 	Player *p;
  5662 	Player *p;
  5133 	FOR_ALL_PLAYERS(p) {
  5663 	FOR_ALL_PLAYERS(p) {
  5142 	InitDepotWindowBlockSizes();
  5672 	InitDepotWindowBlockSizes();
  5143 
  5673 
  5144 	/* Add all new houses to the house array. */
  5674 	/* Add all new houses to the house array. */
  5145 	FinaliseHouseArray();
  5675 	FinaliseHouseArray();
  5146 
  5676 
       
  5677 	/* Add all new industries to the industry array. */
       
  5678 	FinaliseIndustriesArray();
       
  5679 
  5147 	/* Create dynamic list of industry legends for smallmap_gui.cpp */
  5680 	/* Create dynamic list of industry legends for smallmap_gui.cpp */
  5148 	BuildIndustriesLegend();
  5681 	BuildIndustriesLegend();
  5149 
  5682 
  5150 	/* Map cargo strings. This is a separate step because cargos are
  5683 	/* Map cargo strings. This is a separate step because cargos are
  5151 	 * loaded before strings... */
  5684 	 * loaded before strings... */
  5152 	MapNewCargoStrings();
  5685 	MapNewCargoStrings();
       
  5686 
       
  5687 	/* Update the townname generators list */
       
  5688 	SortTownGeneratorNames();
  5153 }
  5689 }
  5154 
  5690 
  5155 void LoadNewGRF(uint load_index, uint file_index)
  5691 void LoadNewGRF(uint load_index, uint file_index)
  5156 {
  5692 {
  5157 	InitializeGRFSpecial();
  5693 	InitializeGRFSpecial();
  5166 
  5702 
  5167 		_cur_stage = stage;
  5703 		_cur_stage = stage;
  5168 		_cur_spriteid = load_index;
  5704 		_cur_spriteid = load_index;
  5169 		for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
  5705 		for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
  5170 			if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
  5706 			if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
       
  5707 			if (stage > GLS_INIT && HASBIT(c->flags, GCF_INIT_ONLY)) continue;
  5171 
  5708 
  5172 			/* @todo usererror() */
  5709 			/* @todo usererror() */
  5173 			if (!FileExists(c->full_path)) error("NewGRF file is missing '%s'", c->filename);
  5710 			if (!FioCheckFileExists(c->filename)) error("NewGRF file is missing '%s'", c->filename);
  5174 
  5711 
  5175 			if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
  5712 			if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
  5176 			LoadNewGRFFile(c, slot++, stage);
  5713 			LoadNewGRFFile(c, slot++, stage);
  5177 			if (stage == GLS_ACTIVATION) {
  5714 			if (stage == GLS_RESERVE) {
       
  5715 				if (c->status == GCS_ACTIVATED) c->status = GCS_INITIALISED;
       
  5716 			} else if (stage == GLS_ACTIVATION) {
  5178 				ClearTemporaryNewGRFData();
  5717 				ClearTemporaryNewGRFData();
  5179 				BuildCargoTranslationMap();
  5718 				BuildCargoTranslationMap();
  5180 				DEBUG(sprite, 2, "LoadNewGRF: Currently %i sprites are loaded", _cur_spriteid);
  5719 				DEBUG(sprite, 2, "LoadNewGRF: Currently %i sprites are loaded", _cur_spriteid);
  5181 			}
  5720 			}
  5182 		}
  5721 		}