src/newgrf.cpp
changeset 7159 9030d8ca51f8
parent 7034 e2d80a01d9c1
child 7160 131be0f3e0ef
equal deleted inserted replaced
7158:ca0084d96dbd 7159:9030d8ca51f8
   281 }
   281 }
   282 
   282 
   283 
   283 
   284 typedef bool (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
   284 typedef bool (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
   285 
   285 
   286 #define FOR_EACH_OBJECT for (i = 0; i < numinfo; i++)
       
   287 
       
   288 static void dewagonize(int condition, int engine)
   286 static void dewagonize(int condition, int engine)
   289 {
   287 {
   290 	EngineInfo *ei = &_engine_info[engine];
   288 	EngineInfo *ei = &_engine_info[engine];
   291 	RailVehicleInfo *rvi = &_rail_vehicle_info[engine];
   289 	RailVehicleInfo *rvi = &_rail_vehicle_info[engine];
   292 
   290 
   300 	}
   298 	}
   301 }
   299 }
   302 
   300 
   303 static bool RailVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   301 static bool RailVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   304 {
   302 {
   305 	EngineInfo *ei = &_engine_info[engine];
       
   306 	RailVehicleInfo *rvi = &_rail_vehicle_info[engine];
       
   307 	byte *buf = *bufp;
   303 	byte *buf = *bufp;
   308 	int i;
       
   309 	bool ret = false;
   304 	bool ret = false;
   310 
   305 
   311 	switch (prop) {
   306 	for (int i = 0; i < numinfo; i++) {
   312 		case 0x05: // Track type
   307 		EngineInfo *ei       = &_engine_info[engine + i];
   313 			FOR_EACH_OBJECT {
   308 		RailVehicleInfo *rvi = &_rail_vehicle_info[engine + i];
       
   309 
       
   310 		switch (prop) {
       
   311 			case 0x05: { // Track type
   314 				uint8 tracktype = grf_load_byte(&buf);
   312 				uint8 tracktype = grf_load_byte(&buf);
   315 
   313 
   316 				switch (tracktype) {
   314 				switch (tracktype) {
   317 					case 0: rvi[i].railtype = rvi[i].engclass >= 2 ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL; break;
   315 					case 0: rvi->railtype = rvi->engclass >= 2 ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL; break;
   318 					case 1: rvi[i].railtype = RAILTYPE_MONO; break;
   316 					case 1: rvi->railtype = RAILTYPE_MONO; break;
   319 					case 2: rvi[i].railtype = RAILTYPE_MAGLEV; break;
   317 					case 2: rvi->railtype = RAILTYPE_MAGLEV; break;
   320 					default:
   318 					default:
   321 						grfmsg(1, "RailVehicleChangeInfo: Invalid track type %d specified, ignoring", tracktype);
   319 						grfmsg(1, "RailVehicleChangeInfo: Invalid track type %d specified, ignoring", tracktype);
   322 						break;
   320 						break;
   323 				}
   321 				}
   324 			}
   322 			} break;
   325 			break;
   323 
   326 
   324 			case 0x08: // AI passenger service
   327 		case 0x08: // AI passenger service
   325 				/* @todo missing feature */
   328 			/* @todo missing feature */
   326 				grf_load_byte(&buf);
   329 			FOR_EACH_OBJECT grf_load_byte(&buf);
   327 				ret = true;
   330 			ret = true;
   328 				break;
   331 			break;
   329 
   332 
   330 			case 0x09: { // Speed (1 unit is 1 kmh)
   333 		case 0x09: // Speed (1 unit is 1 kmh)
       
   334 			FOR_EACH_OBJECT {
       
   335 				uint16 speed = grf_load_word(&buf);
   331 				uint16 speed = grf_load_word(&buf);
   336 				if (speed == 0xFFFF) speed = 0;
   332 				if (speed == 0xFFFF) speed = 0;
   337 
   333 
   338 				rvi[i].max_speed = speed;
   334 				rvi->max_speed = speed;
   339 			}
   335 			} break;
   340 			break;
   336 
   341 
   337 			case 0x0B: { // Power
   342 		case 0x0B: // Power
       
   343 			FOR_EACH_OBJECT {
       
   344 				uint16 power = grf_load_word(&buf);
   338 				uint16 power = grf_load_word(&buf);
   345 
   339 
   346 				if (rvi[i].railveh_type == RAILVEH_MULTIHEAD) power /= 2;
   340 				if (rvi->railveh_type == RAILVEH_MULTIHEAD) power /= 2;
   347 
   341 
   348 				rvi[i].power = power;
   342 				rvi->power = power;
   349 				dewagonize(power, engine + i);
   343 				dewagonize(power, engine + i);
   350 			}
   344 			} break;
   351 			break;
   345 
   352 
   346 			case 0x0D: { // Running cost factor
   353 		case 0x0D: // Running cost factor
       
   354 			FOR_EACH_OBJECT {
       
   355 				uint8 runcostfact = grf_load_byte(&buf);
   347 				uint8 runcostfact = grf_load_byte(&buf);
   356 
   348 
   357 				if (rvi[i].railveh_type == RAILVEH_MULTIHEAD) runcostfact /= 2;
   349 				if (rvi->railveh_type == RAILVEH_MULTIHEAD) runcostfact /= 2;
   358 
   350 
   359 				rvi[i].running_cost_base = runcostfact;
   351 				rvi->running_cost_base = runcostfact;
   360 			}
   352 			} break;
   361 			break;
   353 
   362 
   354 			case 0x0E: { // Running cost base
   363 		case 0x0E: // Running cost base
       
   364 			FOR_EACH_OBJECT {
       
   365 				uint32 base = grf_load_dword(&buf);
   355 				uint32 base = grf_load_dword(&buf);
   366 
   356 
   367 				switch (base) {
   357 				switch (base) {
   368 					case 0x4C30: rvi[i].running_cost_class = 0; break;
   358 					case 0x4C30: rvi->running_cost_class = 0; break;
   369 					case 0x4C36: rvi[i].running_cost_class = 1; break;
   359 					case 0x4C36: rvi->running_cost_class = 1; break;
   370 					case 0x4C3C: rvi[i].running_cost_class = 2; break;
   360 					case 0x4C3C: rvi->running_cost_class = 2; break;
   371 					case 0: break; // Used by wagons
   361 					case 0: break; // Used by wagons
   372 					default:
   362 					default:
   373 						grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
   363 						grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
   374 						break;
   364 						break;
   375 				}
   365 				}
   376 			}
   366 			} break;
   377 			break;
   367 
   378 
   368 			case 0x12: { // Sprite ID
   379 		case 0x12: // Sprite ID
       
   380 			FOR_EACH_OBJECT {
       
   381 				uint8 spriteid = grf_load_byte(&buf);
   369 				uint8 spriteid = grf_load_byte(&buf);
   382 
   370 
   383 				/* TTD sprite IDs point to a location in a 16bit array, but we use it
   371 				/* 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. */
   372 				 * as an array index, so we need it to be half the original value. */
   385 				if (spriteid < 0xFD) spriteid >>= 1;
   373 				if (spriteid < 0xFD) spriteid >>= 1;
   386 
   374 
   387 				rvi[i].image_index = spriteid;
   375 				rvi->image_index = spriteid;
   388 			}
   376 			} break;
   389 			break;
   377 
   390 
   378 			case 0x13: { // Dual-headed
   391 		case 0x13: // Dual-headed
       
   392 			FOR_EACH_OBJECT {
       
   393 				uint8 dual = grf_load_byte(&buf);
   379 				uint8 dual = grf_load_byte(&buf);
   394 
   380 
   395 				if (dual != 0) {
   381 				if (dual != 0) {
   396 					if (rvi[i].railveh_type != RAILVEH_MULTIHEAD) {
   382 					if (rvi->railveh_type != RAILVEH_MULTIHEAD) {
   397 						// adjust power and running cost if needed
   383 						// adjust power and running cost if needed
   398 						rvi[i].power /= 2;
   384 						rvi->power /= 2;
   399 						rvi[i].running_cost_base /= 2;
   385 						rvi->running_cost_base /= 2;
   400 					}
   386 					}
   401 					rvi[i].railveh_type = RAILVEH_MULTIHEAD;
   387 					rvi->railveh_type = RAILVEH_MULTIHEAD;
   402 				} else {
   388 				} else {
   403 					if (rvi[i].railveh_type == RAILVEH_MULTIHEAD) {
   389 					if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
   404 						// adjust power and running cost if needed
   390 						// adjust power and running cost if needed
   405 						rvi[i].power *= 2;
   391 						rvi->power *= 2;
   406 						rvi[i].running_cost_base *= 2;
   392 						rvi->running_cost_base *= 2;
   407 					}
   393 					}
   408 					rvi[i].railveh_type = rvi[i].power == 0 ?
   394 					rvi->railveh_type = rvi->power == 0 ?
   409 						RAILVEH_WAGON : RAILVEH_SINGLEHEAD;
   395 						RAILVEH_WAGON : RAILVEH_SINGLEHEAD;
   410 				}
   396 				}
   411 			}
   397 			} break;
   412 			break;
   398 
   413 
   399 			case 0x14: // Cargo capacity
   414 		case 0x14: // Cargo capacity
   400 				rvi->capacity = grf_load_byte(&buf);
   415 			FOR_EACH_OBJECT rvi[i].capacity = grf_load_byte(&buf);
   401 				break;
   416 			break;
   402 
   417 
   403 			case 0x15: { // Cargo type
   418 		case 0x15: // Cargo type
       
   419 			FOR_EACH_OBJECT {
       
   420 				uint8 ctype = grf_load_byte(&buf);
   404 				uint8 ctype = grf_load_byte(&buf);
   421 
   405 
   422 				if (ctype < NUM_CARGO && HASBIT(_cargo_mask, ctype)) {
   406 				if (ctype < NUM_CARGO && HASBIT(_cargo_mask, ctype)) {
   423 					rvi[i].cargo_type = ctype;
   407 					rvi->cargo_type = ctype;
   424 				} else {
   408 				} else {
   425 					rvi[i].cargo_type = CT_INVALID;
   409 					rvi->cargo_type = CT_INVALID;
   426 					grfmsg(2, "RailVehicleChangeInfo: Invalid cargo type %d, using first refittable", ctype);
   410 					grfmsg(2, "RailVehicleChangeInfo: Invalid cargo type %d, using first refittable", ctype);
   427 				}
   411 				}
   428 			}
   412 			} break;
   429 			break;
   413 
   430 
   414 			case 0x16: // Weight
   431 		case 0x16: // Weight
   415 				SB(rvi->weight, 0, 8, grf_load_byte(&buf));
   432 			FOR_EACH_OBJECT SB(rvi[i].weight, 0, 8, grf_load_byte(&buf));
   416 				break;
   433 			break;
   417 
   434 
   418 			case 0x17: // Cost factor
   435 		case 0x17: // Cost factor
   419 				rvi->base_cost = grf_load_byte(&buf);
   436 			FOR_EACH_OBJECT rvi[i].base_cost = grf_load_byte(&buf);
   420 				break;
   437 			break;
   421 
   438 
   422 			case 0x18: // AI rank
   439 		case 0x18: // AI rank
   423 				rvi->ai_rank = grf_load_byte(&buf);
   440 			FOR_EACH_OBJECT rvi[i].ai_rank = grf_load_byte(&buf);
   424 				break;
   441 			break;
   425 
   442 
   426 			case 0x19: { // Engine traction type
   443 		case 0x19: // Engine traction type
   427 				/* What do the individual numbers mean?
   444 			/* What do the individual numbers mean?
   428 				 * 0x00 .. 0x07: Steam
   445 			 * 0x00 .. 0x07: Steam
   429 				 * 0x08 .. 0x27: Diesel
   446 			 * 0x08 .. 0x27: Diesel
   430 				 * 0x28 .. 0x31: Electric
   447 			 * 0x28 .. 0x31: Electric
   431 				 * 0x32 .. 0x37: Monorail
   448 			 * 0x32 .. 0x37: Monorail
   432 				 * 0x38 .. 0x41: Maglev
   449 			 * 0x38 .. 0x41: Maglev
   433 				 */
   450 			 */
       
   451 			FOR_EACH_OBJECT {
       
   452 				uint8 traction = grf_load_byte(&buf);
   434 				uint8 traction = grf_load_byte(&buf);
   453 				EngineClass engclass;
   435 				EngineClass engclass;
   454 
   436 
   455 				if (traction <= 0x07) {
   437 				if (traction <= 0x07) {
   456 					engclass = EC_STEAM;
   438 					engclass = EC_STEAM;
   463 				} else if (traction <= 0x41) {
   445 				} else if (traction <= 0x41) {
   464 					engclass = EC_MAGLEV;
   446 					engclass = EC_MAGLEV;
   465 				} else {
   447 				} else {
   466 					break;
   448 					break;
   467 				}
   449 				}
   468 				if (rvi[i].railtype == RAILTYPE_RAIL     && engclass >= EC_ELECTRIC) rvi[i].railtype = RAILTYPE_ELECTRIC;
   450 				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;
   451 				if (rvi->railtype == RAILTYPE_ELECTRIC && engclass  < EC_ELECTRIC) rvi->railtype = RAILTYPE_RAIL;
   470 
   452 
   471 				rvi[i].engclass = engclass;
   453 				rvi->engclass = engclass;
   472 			}
   454 			} break;
   473 			break;
   455 
   474 
   456 			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);
   457 				EngineID pos = grf_load_byte(&buf);
   478 
   458 
   479 				if (pos < NUM_TRAIN_ENGINES) {
   459 				if (pos < NUM_TRAIN_ENGINES) {
   480 					AlterRailVehListOrder(engine + i, pos);
   460 					AlterRailVehListOrder(engine + i, pos);
   481 				} else {
   461 				} else {
   482 					grfmsg(2, "RailVehicleChangeInfo: Invalid train engine ID %d, ignoring", pos);
   462 					grfmsg(2, "RailVehicleChangeInfo: Invalid train engine ID %d, ignoring", pos);
   483 				}
   463 				}
   484 			}
   464 			} break;
   485 			break;
   465 
   486 
   466 			case 0x1B: // Powered wagons power bonus
   487 		case 0x1B: // Powered wagons power bonus
   467 				rvi->pow_wag_power = grf_load_word(&buf);
   488 			FOR_EACH_OBJECT rvi[i].pow_wag_power = grf_load_word(&buf);
   468 				break;
   489 			break;
   469 
   490 
   470 			case 0x1C: // Refit cost
   491 		case 0x1C: // Refit cost
   471 				ei->refit_cost = grf_load_byte(&buf);
   492 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   472 				break;
   493 			break;
   473 
   494 
   474 			case 0x1D: // Refit cargo
   495 		case 0x1D: // Refit cargo
   475 				ei->refit_mask = grf_load_dword(&buf);
   496 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   476 				break;
   497 			break;
   477 
   498 
   478 			case 0x1E: // Callback
   499 		case 0x1E: // Callback
   479 				ei->callbackmask = grf_load_byte(&buf);
   500 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   480 				break;
   501 			break;
   481 
   502 
   482 			case 0x1F: // Tractive effort coefficient
   503 		case 0x1F: // Tractive effort coefficient
   483 				rvi->tractive_effort = grf_load_byte(&buf);
   504 			FOR_EACH_OBJECT rvi[i].tractive_effort = grf_load_byte(&buf);
   484 				break;
   505 			break;
   485 
   506 
   486 			case 0x21: // Shorter vehicle
   507 		case 0x21: // Shorter vehicle
   487 				rvi->shorten_factor = grf_load_byte(&buf);
   508 			FOR_EACH_OBJECT rvi[i].shorten_factor = grf_load_byte(&buf);
   488 				break;
   509 			break;
   489 
   510 
   490 			case 0x22: // Visual effect
   511 		case 0x22: // Visual effect
   491 				/* see note in engine.h about rvi->visual_effect */
   512 			/* see note in engine.h about rvi->visual_effect */
   492 				rvi->visual_effect = grf_load_byte(&buf);
   513 			FOR_EACH_OBJECT rvi[i].visual_effect = grf_load_byte(&buf);
   493 				break;
   514 			break;
   494 
   515 
   495 			case 0x23: // Powered wagons weight bonus
   516 		case 0x23: // Powered wagons weight bonus
   496 				rvi->pow_wag_weight = grf_load_byte(&buf);
   517 			FOR_EACH_OBJECT rvi[i].pow_wag_weight = grf_load_byte(&buf);
   497 				break;
   518 			break;
   498 
   519 
   499 			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);
   500 				byte weight = grf_load_byte(&buf);
   523 
   501 
   524 				if (weight > 4) {
   502 				if (weight > 4) {
   525 					grfmsg(2, "RailVehicleChangeInfo: Nonsensical weight of %d tons, ignoring", weight << 8);
   503 					grfmsg(2, "RailVehicleChangeInfo: Nonsensical weight of %d tons, ignoring", weight << 8);
   526 				} else {
   504 				} else {
   527 					SB(rvi[i].weight, 8, 8, weight);
   505 					SB(rvi->weight, 8, 8, weight);
   528 				}
   506 				}
   529 			}
   507 			} break;
   530 			break;
   508 
   531 
   509 			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
   510 				rvi->user_def_data = grf_load_byte(&buf);
   533 			FOR_EACH_OBJECT rvi[i].user_def_data = grf_load_byte(&buf);
   511 				break;
   534 			break;
   512 
   535 
   513 			case 0x26: // Retire vehicle early
   536 		case 0x26: // Retire vehicle early
   514 				ei->retire_early = grf_load_byte(&buf);
   537 			FOR_EACH_OBJECT ei[i].retire_early = grf_load_byte(&buf);
   515 				break;
   538 			break;
   516 
   539 
   517 			case 0x27: // Miscellaneous flags
   540 		case 0x27: // Miscellaneous flags
   518 				ei->misc_flags = grf_load_byte(&buf);
   541 			FOR_EACH_OBJECT {
   519 				_loaded_newgrf_features.has_2CC |= HASBIT(ei->misc_flags, EF_USES_2CC);
   542 				ei[i].misc_flags = grf_load_byte(&buf);
   520 				break;
   543 				_loaded_newgrf_features.has_2CC |= HASBIT(ei[i].misc_flags, EF_USES_2CC);
   521 
   544 			}
   522 			case 0x28: // Cargo classes allowed
   545 			break;
   523 				cargo_allowed[engine + i] = grf_load_word(&buf);
   546 
   524 				break;
   547 		case 0x28: // Cargo classes allowed
   525 
   548 			FOR_EACH_OBJECT cargo_allowed[engine + i] = grf_load_word(&buf);
   526 			case 0x29: // Cargo classes disallowed
   549 			break;
   527 				cargo_disallowed[engine + i] = grf_load_word(&buf);
   550 
   528 				break;
   551 		case 0x29: // Cargo classes disallowed
   529 
   552 			FOR_EACH_OBJECT cargo_disallowed[engine + i] = grf_load_word(&buf);
   530 			case 0x2A: // Long format introduction date (days since year 0)
   553 			break;
   531 				ei->base_intro = grf_load_dword(&buf);
   554 
   532 				break;
   555 		case 0x2A: // Long format introduction date (days since year 0)
   533 
   556 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   534 			/* @todo air drag and retire vehicle early
   557 			break;
   535 			 * Fall-through for unimplemented one byte long properties. */
   558 
   536 			case 0x20: // Air drag
   559 		/* @todo air drag and retire vehicle early
   537 				grf_load_byte(&buf);
   560 		 * Fall-through for unimplemented one byte long properties. */
   538 				ret = true;
   561 		case 0x20: // Air drag
   539 				break;
   562 			FOR_EACH_OBJECT grf_load_byte(&buf);
   540 
   563 			ret = true;
   541 			default:
   564 			break;
   542 				ret = true;
   565 
   543 				break;
   566 		default:
   544 		}
   567 			ret = true;
   545 	}
   568 			break;
   546 
   569 	}
       
   570 	*bufp = buf;
   547 	*bufp = buf;
   571 	return ret;
   548 	return ret;
   572 }
   549 }
   573 
   550 
   574 static bool RoadVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   551 static bool RoadVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   575 {
   552 {
   576 	EngineInfo *ei = &_engine_info[ROAD_ENGINES_INDEX + engine];
       
   577 	RoadVehicleInfo *rvi = &_road_vehicle_info[engine];
       
   578 	byte *buf = *bufp;
   553 	byte *buf = *bufp;
   579 	int i;
       
   580 	bool ret = false;
   554 	bool ret = false;
   581 
   555 
   582 	switch (prop) {
   556 	for (int i = 0; i < numinfo; i++) {
   583 		case 0x08: // Speed (1 unit is 0.5 kmh)
   557 		EngineInfo *ei       = &_engine_info[ROAD_ENGINES_INDEX + engine + i];
   584 			FOR_EACH_OBJECT rvi[i].max_speed = grf_load_byte(&buf);
   558 		RoadVehicleInfo *rvi = &_road_vehicle_info[engine + i];
   585 			break;
   559 
   586 
   560 		switch (prop) {
   587 		case 0x09: // Running cost factor
   561 			case 0x08: // Speed (1 unit is 0.5 kmh)
   588 			FOR_EACH_OBJECT rvi[i].running_cost = grf_load_byte(&buf);
   562 				rvi->max_speed = grf_load_byte(&buf);
   589 			break;
   563 				break;
   590 
   564 
   591 		case 0x0A: // Running cost base
   565 			case 0x09: // Running cost factor
   592 			/* @todo : I have no idea. --pasky
   566 				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 */
   567 				break;
   594 			FOR_EACH_OBJECT grf_load_dword(&buf);
   568 
   595 			ret = true;
   569 			case 0x0A: // Running cost base
   596 			break;
   570 				/* @todo : I have no idea. --pasky
   597 
   571 				 * I THINK it is used for overriding the base cost of all road vehicle (_price.roadveh_base) --belugas */
   598 		case 0x0E: // Sprite ID
   572 				grf_load_dword(&buf);
   599 			FOR_EACH_OBJECT {
   573 				ret = true;
       
   574 				break;
       
   575 
       
   576 			case 0x0E: { // Sprite ID
   600 				uint8 spriteid = grf_load_byte(&buf);
   577 				uint8 spriteid = grf_load_byte(&buf);
   601 
   578 
   602 				/* cars have different custom id in the GRF file */
   579 				/* cars have different custom id in the GRF file */
   603 				if (spriteid == 0xFF) spriteid = 0xFD;
   580 				if (spriteid == 0xFF) spriteid = 0xFD;
   604 
   581 
   605 				if (spriteid < 0xFD) spriteid >>= 1;
   582 				if (spriteid < 0xFD) spriteid >>= 1;
   606 
   583 
   607 				rvi[i].image_index = spriteid;
   584 				rvi->image_index = spriteid;
   608 			}
   585 			} break;
   609 			break;
   586 
   610 
   587 			case 0x0F: // Cargo capacity
   611 		case 0x0F: // Cargo capacity
   588 				rvi->capacity = grf_load_byte(&buf);
   612 			FOR_EACH_OBJECT rvi[i].capacity = grf_load_byte(&buf);
   589 				break;
   613 			break;
   590 
   614 
   591 			case 0x10: { // Cargo type
   615 		case 0x10: // Cargo type
       
   616 			FOR_EACH_OBJECT {
       
   617 				uint8 cargo = grf_load_byte(&buf);
   592 				uint8 cargo = grf_load_byte(&buf);
   618 
   593 
   619 				if (cargo < NUM_CARGO && HASBIT(_cargo_mask, cargo)) {
   594 				if (cargo < NUM_CARGO && HASBIT(_cargo_mask, cargo)) {
   620 					rvi[i].cargo_type = cargo;
   595 					rvi->cargo_type = cargo;
   621 				} else {
   596 				} else {
   622 					rvi[i].cargo_type = CT_INVALID;
   597 					rvi->cargo_type = CT_INVALID;
   623 					grfmsg(2, "RoadVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   598 					grfmsg(2, "RoadVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   624 				}
   599 				}
   625 			}
   600 			} break;
   626 			break;
   601 
   627 
   602 			case 0x11: // Cost factor
   628 		case 0x11: // Cost factor
   603 				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?
   604 				break;
   630 			break;
   605 
   631 
   606 			case 0x12: // SFX
   632 		case 0x12: // SFX
   607 				rvi->sfx = (SoundFx)grf_load_byte(&buf);
   633 			FOR_EACH_OBJECT rvi[i].sfx = (SoundFx)grf_load_byte(&buf);
   608 				break;
   634 			break;
   609 
   635 
   610 			case 0x13: // Power in 10hp
   636 		case 0x13: // Power in 10hp
   611 			case 0x14: // Weight in 1/4 tons
   637 		case 0x14: // Weight in 1/4 tons
   612 			case 0x15: // Speed in mph*0.8
   638 		case 0x15: // Speed in mph*0.8
   613 				/* TODO: Support for road vehicles realistic power
   639 			/* TODO: Support for road vehicles realistic power
   614 				 * computations (called rvpower in TTDPatch) is just
   640 			 * computations (called rvpower in TTDPatch) is just
   615 				 * missing in OTTD yet. --pasky */
   641 			 * missing in OTTD yet. --pasky */
   616 				grf_load_byte(&buf);
   642 			FOR_EACH_OBJECT grf_load_byte(&buf);
   617 				ret = true;
   643 			ret = true;
   618 				break;
   644 			break;
   619 
   645 
   620 			case 0x16: // Cargos available for refitting
   646 		case 0x16: // Cargos available for refitting
   621 				ei->refit_mask = grf_load_dword(&buf);
   647 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   622 				break;
   648 			break;
   623 
   649 
   624 			case 0x17: // Callback mask
   650 		case 0x17: // Callback mask
   625 				ei->callbackmask = grf_load_byte(&buf);
   651 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   626 				break;
   652 			break;
   627 
   653 
   628 			case 0x1A: // Refit cost
   654 		case 0x1A: // Refit cost
   629 				ei->refit_cost = grf_load_byte(&buf);
   655 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   630 				break;
   656 			break;
   631 
   657 
   632 			case 0x1B: // Retire vehicle early
   658 		case 0x1B: // Retire vehicle early
   633 				ei->retire_early = grf_load_byte(&buf);
   659 			FOR_EACH_OBJECT ei[i].retire_early = grf_load_byte(&buf);
   634 				break;
   660 			break;
   635 
   661 
   636 			case 0x1C: // Miscellaneous flags
   662 		case 0x1C: // Miscellaneous flags
   637 				ei->misc_flags = grf_load_byte(&buf);
   663 			FOR_EACH_OBJECT {
   638 				_loaded_newgrf_features.has_2CC |= HASBIT(ei->misc_flags, EF_USES_2CC);
   664 				ei[i].misc_flags = grf_load_byte(&buf);
   639 				break;
   665 				_loaded_newgrf_features.has_2CC |= HASBIT(ei[i].misc_flags, EF_USES_2CC);
   640 
   666 			}
   641 			case 0x1D: // Cargo classes allowed
   667 			break;
   642 				cargo_allowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   668 
   643 				break;
   669 		case 0x1D: // Cargo classes allowed
   644 
   670 			FOR_EACH_OBJECT cargo_allowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   645 			case 0x1E: // Cargo classes disallowed
   671 			break;
   646 				cargo_disallowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   672 
   647 				break;
   673 		case 0x1E: // Cargo classes disallowed
   648 
   674 			FOR_EACH_OBJECT cargo_disallowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   649 			case 0x1F: // Long format introduction date (days since year 0)
   675 			break;
   650 				ei->base_intro = grf_load_dword(&buf);
   676 
   651 				break;
   677 		case 0x1F: // Long format introduction date (days since year 0)
   652 
   678 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   653 			case 0x18: // Tractive effort
   679 			break;
   654 			case 0x19: // Air drag
   680 
   655 				/* @todo */
   681 		case 0x18: // Tractive effort
   656 				grf_load_byte(&buf);
   682 		case 0x19: // Air drag
   657 				ret = true;
   683 			/* @todo */
   658 				break;
   684 			FOR_EACH_OBJECT grf_load_byte(&buf);
   659 
   685 			ret = true;
   660 			default:
   686 			break;
   661 				ret = true;
   687 
   662 				break;
   688 		default:
   663 		}
   689 			ret = true;
       
   690 			break;
       
   691 	}
   664 	}
   692 
   665 
   693 	*bufp = buf;
   666 	*bufp = buf;
   694 	return ret;
   667 	return ret;
   695 }
   668 }
   696 
   669 
   697 static bool ShipVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   670 static bool ShipVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   698 {
   671 {
   699 	EngineInfo *ei = &_engine_info[SHIP_ENGINES_INDEX + engine];
       
   700 	ShipVehicleInfo *svi = &_ship_vehicle_info[engine];
       
   701 	byte *buf = *bufp;
   672 	byte *buf = *bufp;
   702 	int i;
       
   703 	bool ret = false;
   673 	bool ret = false;
   704 
   674 
   705 	//printf("e %x prop %x?\n", engine, prop);
   675 	for (int i = 0; i < numinfo; i++) {
   706 	switch (prop) {
   676 		EngineInfo *ei       = &_engine_info[SHIP_ENGINES_INDEX + engine + i];
   707 		case 0x08: // Sprite ID
   677 		ShipVehicleInfo *svi = &_ship_vehicle_info[engine + i];
   708 			FOR_EACH_OBJECT {
   678 
       
   679 		switch (prop) {
       
   680 			case 0x08: { // Sprite ID
   709 				uint8 spriteid = grf_load_byte(&buf);
   681 				uint8 spriteid = grf_load_byte(&buf);
   710 
   682 
   711 				/* ships have different custom id in the GRF file */
   683 				/* ships have different custom id in the GRF file */
   712 				if (spriteid == 0xFF) spriteid = 0xFD;
   684 				if (spriteid == 0xFF) spriteid = 0xFD;
   713 
   685 
   714 				if (spriteid < 0xFD) spriteid >>= 1;
   686 				if (spriteid < 0xFD) spriteid >>= 1;
   715 
   687 
   716 				svi[i].image_index = spriteid;
   688 				svi->image_index = spriteid;
   717 			}
   689 			} break;
   718 			break;
   690 
   719 
   691 			case 0x09: // Refittable
   720 		case 0x09: // Refittable
   692 				svi->refittable = (grf_load_byte(&buf) != 0);
   721 			FOR_EACH_OBJECT svi[i].refittable = (grf_load_byte(&buf) != 0);
   693 				break;
   722 			break;
   694 
   723 
   695 			case 0x0A: // Cost factor
   724 		case 0x0A: // Cost factor
   696 				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?
   697 				break;
   726 			break;
   698 
   727 
   699 			case 0x0B: // Speed (1 unit is 0.5 kmh)
   728 		case 0x0B: // Speed (1 unit is 0.5 kmh)
   700 				svi->max_speed = grf_load_byte(&buf);
   729 			FOR_EACH_OBJECT svi[i].max_speed = grf_load_byte(&buf);
   701 				break;
   730 			break;
   702 
   731 
   703 			case 0x0C: { // Cargo type
   732 		case 0x0C: // Cargo type
       
   733 			FOR_EACH_OBJECT {
       
   734 				uint8 cargo = grf_load_byte(&buf);
   704 				uint8 cargo = grf_load_byte(&buf);
   735 
   705 
   736 				if (cargo < NUM_CARGO && HASBIT(_cargo_mask, cargo)) {
   706 				if (cargo < NUM_CARGO && HASBIT(_cargo_mask, cargo)) {
   737 					svi[i].cargo_type = cargo;
   707 					svi->cargo_type = cargo;
   738 				} else {
   708 				} else {
   739 					svi[i].cargo_type = CT_INVALID;
   709 					svi->cargo_type = CT_INVALID;
   740 					grfmsg(2, "ShipVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   710 					grfmsg(2, "ShipVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
   741 				}
   711 				}
   742 			}
   712 			} break;
   743 			break;
   713 
   744 
   714 			case 0x0D: // Cargo capacity
   745 		case 0x0D: // Cargo capacity
   715 				svi->capacity = grf_load_word(&buf);
   746 			FOR_EACH_OBJECT svi[i].capacity = grf_load_word(&buf);
   716 				break;
   747 			break;
   717 
   748 
   718 			case 0x0F: // Running cost factor
   749 		case 0x0F: // Running cost factor
   719 				svi->running_cost = grf_load_byte(&buf);
   750 			FOR_EACH_OBJECT svi[i].running_cost = grf_load_byte(&buf);
   720 				break;
   751 			break;
   721 
   752 
   722 			case 0x10: // SFX
   753 		case 0x10: // SFX
   723 				svi->sfx = (SoundFx)grf_load_byte(&buf);
   754 			FOR_EACH_OBJECT svi[i].sfx = (SoundFx)grf_load_byte(&buf);
   724 				break;
   755 			break;
   725 
   756 
   726 			case 0x11: // Cargos available for refitting
   757 		case 0x11: // Cargos available for refitting
   727 				ei->refit_mask = grf_load_dword(&buf);
   758 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   728 				break;
   759 			break;
   729 
   760 
   730 			case 0x12: // Callback mask
   761 		case 0x12: // Callback mask
   731 				ei->callbackmask = grf_load_byte(&buf);
   762 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   732 				break;
   763 			break;
   733 
   764 
   734 			case 0x13: // Refit cost
   765 		case 0x13: // Refit cost
   735 				ei->refit_cost = grf_load_byte(&buf);
   766 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   736 				break;
   767 			break;
   737 
   768 
   738 			case 0x16: // Retire vehicle early
   769 		case 0x16: // Retire vehicle early
   739 				ei->retire_early = grf_load_byte(&buf);
   770 			FOR_EACH_OBJECT ei[i].retire_early = grf_load_byte(&buf);
   740 				break;
   771 			break;
   741 
   772 
   742 			case 0x17: // Miscellaneous flags
   773 		case 0x17: // Miscellaneous flags
   743 				ei->misc_flags = grf_load_byte(&buf);
   774 			FOR_EACH_OBJECT {
   744 				_loaded_newgrf_features.has_2CC |= HASBIT(ei->misc_flags, EF_USES_2CC);
   775 				ei[i].misc_flags = grf_load_byte(&buf);
   745 				break;
   776 				_loaded_newgrf_features.has_2CC |= HASBIT(ei[i].misc_flags, EF_USES_2CC);
   746 
   777 			}
   747 			case 0x18: // Cargo classes allowed
   778 			break;
   748 				cargo_allowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   779 
   749 				break;
   780 		case 0x18: // Cargo classes allowed
   750 
   781 			FOR_EACH_OBJECT cargo_allowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   751 			case 0x19: // Cargo classes disallowed
   782 			break;
   752 				cargo_disallowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   783 
   753 				break;
   784 		case 0x19: // Cargo classes disallowed
   754 
   785 			FOR_EACH_OBJECT cargo_disallowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   755 			case 0x1A: // Long format introduction date (days since year 0)
   786 			break;
   756 				ei->base_intro = grf_load_dword(&buf);
   787 
   757 				break;
   788 		case 0x1A: // Long format introduction date (days since year 0)
   758 
   789 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   759 			case 0x14: // Ocean speed fraction
   790 			break;
   760 			case 0x15: // Canal speed fraction
   791 
   761 				/* @todo */
   792 		case 0x14: // Ocean speed fraction
   762 				grf_load_byte(&buf);
   793 		case 0x15: // Canal speed fraction
   763 				ret = true;
   794 			/* @todo */
   764 				break;
   795 			FOR_EACH_OBJECT grf_load_byte(&buf);
   765 
   796 			ret = true;
   766 			default:
   797 			break;
   767 				ret = true;
   798 
   768 				break;
   799 		default:
   769 		}
   800 			ret = true;
       
   801 			break;
       
   802 	}
   770 	}
   803 
   771 
   804 	*bufp = buf;
   772 	*bufp = buf;
   805 	return ret;
   773 	return ret;
   806 }
   774 }
   807 
   775 
   808 static bool AircraftVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   776 static bool AircraftVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
   809 {
   777 {
   810 	EngineInfo *ei = &_engine_info[AIRCRAFT_ENGINES_INDEX + engine];
       
   811 	AircraftVehicleInfo *avi = &_aircraft_vehicle_info[engine];
       
   812 	byte *buf = *bufp;
   778 	byte *buf = *bufp;
   813 	int i;
       
   814 	bool ret = false;
   779 	bool ret = false;
   815 
   780 
   816 	//printf("e %x prop %x?\n", engine, prop);
   781 	for (int i = 0; i < numinfo; i++) {
   817 	switch (prop) {
   782 		EngineInfo *ei           = &_engine_info[AIRCRAFT_ENGINES_INDEX + engine + i];
   818 		case 0x08: // Sprite ID
   783 		AircraftVehicleInfo *avi = &_aircraft_vehicle_info[engine + i];
   819 			FOR_EACH_OBJECT {
   784 
       
   785 		switch (prop) {
       
   786 			case 0x08: { // Sprite ID
   820 				uint8 spriteid = grf_load_byte(&buf);
   787 				uint8 spriteid = grf_load_byte(&buf);
   821 
   788 
   822 				/* aircraft have different custom id in the GRF file */
   789 				/* aircraft have different custom id in the GRF file */
   823 				if (spriteid == 0xFF) spriteid = 0xFD;
   790 				if (spriteid == 0xFF) spriteid = 0xFD;
   824 
   791 
   825 				if (spriteid < 0xFD) spriteid >>= 1;
   792 				if (spriteid < 0xFD) spriteid >>= 1;
   826 
   793 
   827 				avi[i].image_index = spriteid;
   794 				avi->image_index = spriteid;
   828 			}
   795 			} break;
   829 			break;
   796 
   830 
   797 			case 0x09: // Helicopter
   831 		case 0x09: // Helicopter
       
   832 			FOR_EACH_OBJECT {
       
   833 				if (grf_load_byte(&buf) == 0) {
   798 				if (grf_load_byte(&buf) == 0) {
   834 					avi[i].subtype = AIR_HELI;
   799 					avi->subtype = AIR_HELI;
   835 				} else {
   800 				} else {
   836 					SB(avi[i].subtype, 0, 1, 1); // AIR_CTOL
   801 					SB(avi->subtype, 0, 1, 1); // AIR_CTOL
   837 				}
   802 				}
   838 			}
   803 				break;
   839 			break;
   804 
   840 
   805 			case 0x0A: // Large
   841 		case 0x0A: // Large
   806 				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
   807 				break;
   843 			break;
   808 
   844 
   809 			case 0x0B: // Cost factor
   845 		case 0x0B: // Cost factor
   810 				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?
   811 				break;
   847 			break;
   812 
   848 
   813 			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)
   814 				avi->max_speed = (grf_load_byte(&buf) * 129) / 10;
   850 			FOR_EACH_OBJECT avi[i].max_speed = (grf_load_byte(&buf) * 129) / 10;
   815 				break;
   851 			break;
   816 
   852 
   817 			case 0x0D: // Acceleration
   853 		case 0x0D: // Acceleration
   818 				avi->acceleration = (grf_load_byte(&buf) * 129) / 10;
   854 			FOR_EACH_OBJECT avi[i].acceleration = (grf_load_byte(&buf) * 129) / 10;
   819 				break;
   855 			break;
   820 
   856 
   821 			case 0x0E: // Running cost factor
   857 		case 0x0E: // Running cost factor
   822 				avi->running_cost = grf_load_byte(&buf);
   858 			FOR_EACH_OBJECT avi[i].running_cost = grf_load_byte(&buf);
   823 				break;
   859 			break;
   824 
   860 
   825 			case 0x0F: // Passenger capacity
   861 		case 0x0F: // Passenger capacity
   826 				avi->passenger_capacity = grf_load_word(&buf);
   862 			FOR_EACH_OBJECT avi[i].passenger_capacity = grf_load_word(&buf);
   827 				break;
   863 			break;
   828 
   864 
   829 			case 0x11: // Mail capacity
   865 		case 0x11: // Mail capacity
   830 				avi->mail_capacity = grf_load_byte(&buf);
   866 			FOR_EACH_OBJECT avi[i].mail_capacity = grf_load_byte(&buf);
   831 				break;
   867 			break;
   832 
   868 
   833 			case 0x12: // SFX
   869 		case 0x12: // SFX
   834 				avi->sfx = (SoundFx)grf_load_byte(&buf);
   870 			FOR_EACH_OBJECT avi[i].sfx = (SoundFx)grf_load_byte(&buf);
   835 				break;
   871 			break;
   836 
   872 
   837 			case 0x13: // Cargos available for refitting
   873 		case 0x13: // Cargos available for refitting
   838 				ei->refit_mask = grf_load_dword(&buf);
   874 			FOR_EACH_OBJECT ei[i].refit_mask = grf_load_dword(&buf);
   839 				break;
   875 			break;
   840 
   876 
   841 			case 0x14: // Callback mask
   877 		case 0x14: // Callback mask
   842 				ei->callbackmask = grf_load_byte(&buf);
   878 			FOR_EACH_OBJECT ei[i].callbackmask = grf_load_byte(&buf);
   843 				break;
   879 			break;
   844 
   880 
   845 			case 0x15: // Refit cost
   881 		case 0x15: // Refit cost
   846 				ei->refit_cost = grf_load_byte(&buf);
   882 			FOR_EACH_OBJECT ei[i].refit_cost = grf_load_byte(&buf);
   847 				break;
   883 			break;
   848 
   884 
   849 			case 0x16: // Retire vehicle early
   885 		case 0x16: // Retire vehicle early
   850 				ei->retire_early = grf_load_byte(&buf);
   886 			FOR_EACH_OBJECT ei[i].retire_early = grf_load_byte(&buf);
   851 				break;
   887 			break;
   852 
   888 
   853 			case 0x17: // Miscellaneous flags
   889 		case 0x17: // Miscellaneous flags
   854 				ei->misc_flags = grf_load_byte(&buf);
   890 			FOR_EACH_OBJECT {
   855 				_loaded_newgrf_features.has_2CC |= HASBIT(ei->misc_flags, EF_USES_2CC);
   891 				ei[i].misc_flags = grf_load_byte(&buf);
   856 				break;
   892 				_loaded_newgrf_features.has_2CC |= HASBIT(ei[i].misc_flags, EF_USES_2CC);
   857 
   893 			}
   858 			case 0x18: // Cargo classes allowed
   894 			break;
   859 				cargo_allowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   895 
   860 				break;
   896 		case 0x18: // Cargo classes allowed
   861 
   897 			FOR_EACH_OBJECT cargo_allowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   862 			case 0x19: // Cargo classes disallowed
   898 			break;
   863 				cargo_disallowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   899 
   864 				break;
   900 		case 0x19: // Cargo classes disallowed
   865 
   901 			FOR_EACH_OBJECT cargo_disallowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   866 			case 0x1A: // Long format introduction date (days since year 0)
   902 			break;
   867 				ei->base_intro = grf_load_dword(&buf);
   903 
   868 				break;
   904 		case 0x1A: // Long format introduction date (days since year 0)
   869 
   905 			FOR_EACH_OBJECT ei[i].base_intro = grf_load_dword(&buf);
   870 			default:
   906 			break;
   871 				ret = true;
   907 
   872 				break;
   908 		default:
   873 		}
   909 			ret = true;
       
   910 			break;
       
   911 	}
   874 	}
   912 
   875 
   913 	*bufp = buf;
   876 	*bufp = buf;
   914 	return ret;
   877 	return ret;
   915 }
   878 }
   916 
   879 
   917 static bool StationChangeInfo(uint stid, int numinfo, int prop, byte **bufp, int len)
   880 static bool StationChangeInfo(uint stid, int numinfo, int prop, byte **bufp, int len)
   918 {
   881 {
   919 	StationSpec **statspec;
       
   920 	byte *buf = *bufp;
   882 	byte *buf = *bufp;
   921 	int i;
       
   922 	bool ret = false;
   883 	bool ret = false;
   923 
   884 
   924 	if (stid + numinfo > MAX_STATIONS) {
   885 	if (stid + numinfo > MAX_STATIONS) {
   925 		grfmsg(1, "StationChangeInfo: Station %u is invalid, max %u, ignoring", stid + numinfo, MAX_STATIONS);
   886 		grfmsg(1, "StationChangeInfo: Station %u is invalid, max %u, ignoring", stid + numinfo, MAX_STATIONS);
   926 		return false;
   887 		return false;
   927 	}
   888 	}
   928 
   889 
   929 	/* Allocate station specs if necessary */
   890 	/* Allocate station specs if necessary */
   930 	if (_cur_grffile->stations == NULL) _cur_grffile->stations = CallocT<StationSpec*>(MAX_STATIONS);
   891 	if (_cur_grffile->stations == NULL) _cur_grffile->stations = CallocT<StationSpec*>(MAX_STATIONS);
   931 
   892 
   932 	statspec = &_cur_grffile->stations[stid];
   893 	for (int i = 0; i < numinfo; i++) {
   933 
   894 		StationSpec *statspec = _cur_grffile->stations[stid + i];
   934 	if (prop != 0x08) {
   895 
   935 		/* Check that all stations we are modifying are defined. */
   896 		/* Check that the station we are modifying is defined. */
   936 		FOR_EACH_OBJECT {
   897 		if (statspec == NULL && prop != 0x08) {
   937 			if (statspec[i] == NULL) {
   898 			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);
   899 			continue;
   939 				return false;
   900 		}
   940 			}
   901 
   941 		}
   902 		switch (prop) {
   942 	}
   903 			case 0x08: { // Class ID
   943 
   904 				StationSpec **spec = &_cur_grffile->stations[stid + i];
   944 	switch (prop) {
   905 
   945 		case 0x08: // Class ID
       
   946 			FOR_EACH_OBJECT {
       
   947 				/* Property 0x08 is special; it is where the station is allocated */
   906 				/* Property 0x08 is special; it is where the station is allocated */
   948 				if (statspec[i] == NULL) statspec[i] = CallocT<StationSpec>(1);
   907 				if (*spec == NULL) *spec = CallocT<StationSpec>(1);
   949 
   908 
   950 				/* Swap classid because we read it in BE meaning WAYP or DFLT */
   909 				/* Swap classid because we read it in BE meaning WAYP or DFLT */
   951 				uint32 classid = grf_load_dword(&buf);
   910 				uint32 classid = grf_load_dword(&buf);
   952 				statspec[i]->sclass = AllocateStationClass(BSWAP32(classid));
   911 				(*spec)->sclass = AllocateStationClass(BSWAP32(classid));
   953 			}
   912 			} break;
   954 			break;
   913 
   955 
   914 			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);
   915 				statspec->tiles = grf_load_extended(&buf);
   961 				statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
   916 				statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
   962 				statspec->copied_renderdata = false;
   917 				statspec->copied_renderdata = false;
   963 
   918 
   964 				for (uint t = 0; t < statspec->tiles; t++) {
   919 				for (uint t = 0; t < statspec->tiles; t++) {
  1005 							CLRBIT(dtss->image, 14);
   960 							CLRBIT(dtss->image, 14);
  1006 							SETBIT(dtss->image, PALETTE_MODIFIER_TRANSPARENT);
   961 							SETBIT(dtss->image, PALETTE_MODIFIER_TRANSPARENT);
  1007 						}
   962 						}
  1008 					}
   963 					}
  1009 				}
   964 				}
  1010 			}
   965 				break;
  1011 			break;
   966 
  1012 
   967 			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);
   968 				byte srcid = grf_load_byte(&buf);
  1017 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
   969 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  1018 
   970 
  1019 				statspec->tiles = srcstatspec->tiles;
   971 				statspec->tiles = srcstatspec->tiles;
  1020 				statspec->renderdata = srcstatspec->renderdata;
   972 				statspec->renderdata = srcstatspec->renderdata;
  1021 				statspec->copied_renderdata = true;
   973 				statspec->copied_renderdata = true;
  1022 			}
   974 			} break;
  1023 			break;
   975 
  1024 
   976 			case 0x0B: // Callback mask
  1025 		case 0x0B: // Callback mask
   977 				statspec->callbackmask = grf_load_byte(&buf);
  1026 			FOR_EACH_OBJECT statspec[i]->callbackmask = grf_load_byte(&buf);
   978 				break;
  1027 			break;
   979 
  1028 
   980 			case 0x0C: // Disallowed number of platforms
  1029 		case 0x0C: // Disallowed number of platforms
   981 				statspec->disallowed_platforms = grf_load_byte(&buf);
  1030 			FOR_EACH_OBJECT statspec[i]->disallowed_platforms = grf_load_byte(&buf);
   982 				break;
  1031 			break;
   983 
  1032 
   984 			case 0x0D: // Disallowed platform lengths
  1033 		case 0x0D: // Disallowed platform lengths
   985 				statspec->disallowed_lengths = grf_load_byte(&buf);
  1034 			FOR_EACH_OBJECT statspec[i]->disallowed_lengths = grf_load_byte(&buf);
   986 				break;
  1035 			break;
   987 
  1036 
   988 			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;
   989 				statspec->copied_layouts = false;
  1042 
   990 
  1043 				while (buf < *bufp + len) {
   991 				while (buf < *bufp + len) {
  1044 					byte length = grf_load_byte(&buf);
   992 					byte length = grf_load_byte(&buf);
  1045 					byte number = grf_load_byte(&buf);
   993 					byte number = grf_load_byte(&buf);
  1082 					l--;
  1030 					l--;
  1083 					p--;
  1031 					p--;
  1084 					free(statspec->layouts[l][p]);
  1032 					free(statspec->layouts[l][p]);
  1085 					statspec->layouts[l][p] = layout;
  1033 					statspec->layouts[l][p] = layout;
  1086 				}
  1034 				}
  1087 			}
  1035 				break;
  1088 			break;
  1036 
  1089 
  1037 			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);
  1038 				byte srcid = grf_load_byte(&buf);
  1094 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  1039 				const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
  1095 
  1040 
  1096 				statspec->lengths   = srcstatspec->lengths;
  1041 				statspec->lengths   = srcstatspec->lengths;
  1097 				statspec->platforms = srcstatspec->platforms;
  1042 				statspec->platforms = srcstatspec->platforms;
  1098 				statspec->layouts   = srcstatspec->layouts;
  1043 				statspec->layouts   = srcstatspec->layouts;
  1099 				statspec->copied_layouts = true;
  1044 				statspec->copied_layouts = true;
  1100 			}
  1045 			} break;
  1101 			break;
  1046 
  1102 
  1047 			case 0x10: // Little/lots cargo threshold
  1103 		case 0x10: // Little/lots cargo threshold
  1048 				statspec->cargo_threshold = grf_load_word(&buf);
  1104 			FOR_EACH_OBJECT statspec[i]->cargo_threshold = grf_load_word(&buf);
  1049 				break;
  1105 			break;
  1050 
  1106 
  1051 			case 0x11: // Pylon placement
  1107 		case 0x11: // Pylon placement
  1052 				statspec->pylons = grf_load_byte(&buf);
  1108 			FOR_EACH_OBJECT statspec[i]->pylons = grf_load_byte(&buf);
  1053 				break;
  1109 			break;
  1054 
  1110 
  1055 			case 0x12: // Cargo types for random triggers
  1111 		case 0x12: // Cargo types for random triggers
  1056 				statspec->cargo_triggers = grf_load_dword(&buf);
  1112 			FOR_EACH_OBJECT statspec[i]->cargo_triggers = grf_load_dword(&buf);
  1057 				break;
  1113 			break;
  1058 
  1114 
  1059 			case 0x13: // General flags
  1115 		case 0x13: // General flags
  1060 				statspec->flags = grf_load_byte(&buf);
  1116 			FOR_EACH_OBJECT statspec[i]->flags = grf_load_byte(&buf);
  1061 				break;
  1117 			break;
  1062 
  1118 
  1063 			case 0x14: // Overhead wire placement
  1119 		case 0x14: // Overhead wire placement
  1064 				statspec->wires = grf_load_byte(&buf);
  1120 			FOR_EACH_OBJECT statspec[i]->wires = grf_load_byte(&buf);
  1065 				break;
  1121 			break;
  1066 
  1122 
  1067 			case 0x15: // Blocked tiles
  1123 		case 0x15: // Blocked tiles
  1068 				statspec->blocked = grf_load_byte(&buf);
  1124 			FOR_EACH_OBJECT statspec[i]->blocked = grf_load_byte(&buf);
  1069 				break;
  1125 			break;
  1070 
  1126 
  1071 			case 0x16: // @todo Animation info
  1127 		case 0x16: // @todo Animation info
  1072 				grf_load_word(&buf);
  1128 			FOR_EACH_OBJECT grf_load_word(&buf);
  1073 				ret = true;
  1129 			ret = true;
  1074 				break;
  1130 			break;
  1075 
  1131 
  1076 			case 0x17: // @todo Animation speed
  1132 		case 0x17: // @todo Animation speed
  1077 				grf_load_byte(&buf);
  1133 			FOR_EACH_OBJECT grf_load_byte(&buf);
  1078 				ret = true;
  1134 			ret = true;
  1079 				break;
  1135 			break;
  1080 
  1136 
  1081 			case 0x18: // @todo Animation triggers
  1137 		case 0x18: // @todo Animation triggers
  1082 				grf_load_word(&buf);
  1138 			FOR_EACH_OBJECT grf_load_word(&buf);
  1083 				ret = true;
  1139 			ret = true;
  1084 				break;
  1140 			break;
  1085 
  1141 
  1086 			default:
  1142 		default:
  1087 				ret = true;
  1143 			ret = true;
  1088 				break;
  1144 			break;
  1089 		}
  1145 	}
  1090 	}
  1146 
  1091 
  1147 	*bufp = buf;
  1092 	*bufp = buf;
  1148 	return ret;
  1093 	return ret;
  1149 }
  1094 }
  1150 
  1095 
  1151 static bool BridgeChangeInfo(uint brid, int numinfo, int prop, byte **bufp, int len)
  1096 static bool BridgeChangeInfo(uint brid, int numinfo, int prop, byte **bufp, int len)
  1152 {
  1097 {
  1153 	byte *buf = *bufp;
  1098 	byte *buf = *bufp;
  1154 	int i;
       
  1155 	bool ret = false;
  1099 	bool ret = false;
  1156 
  1100 
  1157 	switch (prop) {
  1101 	if (brid + numinfo > MAX_BRIDGES) {
  1158 		case 0x08: // Year of availability
  1102 		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);
  1103 		return false;
  1160 			break;
  1104 	}
  1161 
  1105 
  1162 		case 0x09: // Minimum length
  1106 	for (int i = 0; i < numinfo; i++) {
  1163 			FOR_EACH_OBJECT _bridge[brid + i].min_length = grf_load_byte(&buf);
  1107 		Bridge *bridge = &_bridge[brid + i];
  1164 			break;
  1108 
  1165 
  1109 		switch (prop) {
  1166 		case 0x0A: // Maximum length
  1110 			case 0x08: // Year of availability
  1167 			FOR_EACH_OBJECT _bridge[brid + i].max_length = grf_load_byte(&buf);
  1111 				bridge->avail_year = ORIGINAL_BASE_YEAR + grf_load_byte(&buf);
  1168 			break;
  1112 				break;
  1169 
  1113 
  1170 		case 0x0B: // Cost factor
  1114 			case 0x09: // Minimum length
  1171 			FOR_EACH_OBJECT _bridge[brid + i].price = grf_load_byte(&buf);
  1115 				bridge->min_length = grf_load_byte(&buf);
  1172 			break;
  1116 				break;
  1173 
  1117 
  1174 		case 0x0C: // Maximum speed
  1118 			case 0x0A: // Maximum length
  1175 			FOR_EACH_OBJECT _bridge[brid + i].speed = grf_load_word(&buf);
  1119 				bridge->max_length = grf_load_byte(&buf);
  1176 			break;
  1120 				break;
  1177 
  1121 
  1178 		case 0x0D: // Bridge sprite tables
  1122 			case 0x0B: // Cost factor
  1179 			FOR_EACH_OBJECT {
  1123 				bridge->price = grf_load_byte(&buf);
  1180 				Bridge *bridge = &_bridge[brid + i];
  1124 				break;
       
  1125 
       
  1126 			case 0x0C: // Maximum speed
       
  1127 				bridge->speed = grf_load_word(&buf);
       
  1128 				break;
       
  1129 
       
  1130 			case 0x0D: { // Bridge sprite tables
  1181 				byte tableid = grf_load_byte(&buf);
  1131 				byte tableid = grf_load_byte(&buf);
  1182 				byte numtables = grf_load_byte(&buf);
  1132 				byte numtables = grf_load_byte(&buf);
  1183 
  1133 
  1184 				if (bridge->sprite_table == NULL) {
  1134 				if (bridge->sprite_table == NULL) {
  1185 					/* Allocate memory for sprite table pointers and zero out */
  1135 					/* Allocate memory for sprite table pointers and zero out */
  1210 
  1160 
  1211 						bridge->sprite_table[tableid][sprite].sprite = image;
  1161 						bridge->sprite_table[tableid][sprite].sprite = image;
  1212 						bridge->sprite_table[tableid][sprite].pal    = pal;
  1162 						bridge->sprite_table[tableid][sprite].pal    = pal;
  1213 					}
  1163 					}
  1214 				}
  1164 				}
  1215 			}
  1165 			} break;
  1216 			break;
  1166 
  1217 
  1167 			case 0x0E: // Flags; bit 0 - disable far pillars
  1218 		case 0x0E: // Flags; bit 0 - disable far pillars
  1168 				bridge->flags = grf_load_byte(&buf);
  1219 			FOR_EACH_OBJECT _bridge[brid + i].flags = grf_load_byte(&buf);
  1169 				break;
  1220 			break;
  1170 
  1221 
  1171 			case 0x0F: // Long format year of availability (year since year 0)
  1222 		case 0x0F: // Long format year of availability (year since year 0)
  1172 				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);
  1173 				break;
  1224 			break;
  1174 
  1225 
  1175 			default:
  1226 		default:
  1176 				ret = true;
  1227 			ret = true;
  1177 				break;
       
  1178 		}
  1228 	}
  1179 	}
  1229 
  1180 
  1230 	*bufp = buf;
  1181 	*bufp = buf;
  1231 	return ret;
  1182 	return ret;
  1232 }
  1183 }
  1233 
  1184 
  1234 static bool TownHouseChangeInfo(uint hid, int numinfo, int prop, byte **bufp, int len)
  1185 static bool TownHouseChangeInfo(uint hid, int numinfo, int prop, byte **bufp, int len)
  1235 {
  1186 {
  1236 	HouseSpec **housespec;
       
  1237 	byte *buf = *bufp;
  1187 	byte *buf = *bufp;
  1238 	int i;
       
  1239 	bool ret = false;
  1188 	bool ret = false;
  1240 
  1189 
  1241 	if (hid + numinfo >= HOUSE_MAX) {
  1190 	if (hid + numinfo >= HOUSE_MAX) {
  1242 		grfmsg(1, "TownHouseChangeInfo: Too many houses loaded (%u), max (%u). Ignoring.", hid + numinfo, HOUSE_MAX-1);
  1191 		grfmsg(1, "TownHouseChangeInfo: Too many houses loaded (%u), max (%u). Ignoring.", hid + numinfo, HOUSE_MAX-1);
  1243 		return false;
  1192 		return false;
  1249 
  1198 
  1250 		/* Reset any overrides that have been set. */
  1199 		/* Reset any overrides that have been set. */
  1251 		_house_mngr.ResetOverride();
  1200 		_house_mngr.ResetOverride();
  1252 	}
  1201 	}
  1253 
  1202 
  1254 	housespec = &_cur_grffile->housespec[hid];
  1203 	for (int i = 0; i < numinfo; i++) {
  1255 
  1204 		HouseSpec *housespec = _cur_grffile->housespec[hid + i];
  1256 	if (prop != 0x08) {
  1205 
  1257 		/* Check that all the houses being modified have been defined. */
  1206 		if (prop != 0x08 && housespec == NULL) {
  1258 		FOR_EACH_OBJECT {
  1207 			grfmsg(2, "TownHouseChangeInfo: Attempt to modify undefined house %u. Ignoring.", hid + i);
  1259 			if (housespec[i] == NULL) {
  1208 			continue;
  1260 				grfmsg(2, "TownHouseChangeInfo: Attempt to modify undefined house %u. Ignoring.", hid + i);
  1209 		}
  1261 				return false;
  1210 
  1262 			}
  1211 		switch (prop) {
  1263 		}
  1212 			case 0x08: { // Substitute building type, and definition of a new house
  1264 	}
  1213 				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);
  1214 				byte subs_id = grf_load_byte(&buf);
  1270 
  1215 
  1271 				if (subs_id == 0xFF) {
  1216 				if (subs_id == 0xFF) {
  1272 					/* Instead of defining a new house, a substitute house id
  1217 					/* Instead of defining a new house, a substitute house id
  1273 					 * of 0xFF disables the old house with the current id. */
  1218 					 * 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);
  1223 					grfmsg(2, "TownHouseChangeInfo: Attempt to use new house %u as substitute house for %u. Ignoring.", subs_id, hid + i);
  1279 					return false;
  1224 					return false;
  1280 				}
  1225 				}
  1281 
  1226 
  1282 				/* Allocate space for this house. */
  1227 				/* Allocate space for this house. */
  1283 				if (housespec[i] == NULL) housespec[i] = CallocT<HouseSpec>(1);
  1228 				if (*house == NULL) *house = CallocT<HouseSpec>(1);
  1284 
  1229 
  1285 				memcpy(housespec[i], &_house_specs[subs_id], sizeof(_house_specs[subs_id]));
  1230 				housespec = *house;
  1286 
  1231 
  1287 				housespec[i]->enabled = true;
  1232 				memcpy(housespec, &_house_specs[subs_id], sizeof(_house_specs[subs_id]));
  1288 				housespec[i]->local_id = hid + i;
  1233 
  1289 				housespec[i]->substitute_id = subs_id;
  1234 				housespec->enabled = true;
  1290 				housespec[i]->grffile = _cur_grffile;
  1235 				housespec->local_id = hid + i;
  1291 				housespec[i]->random_colour[0] = 0x04;  // those 4 random colours are the base colour
  1236 				housespec->substitute_id = subs_id;
  1292 				housespec[i]->random_colour[1] = 0x08;  // for all new houses
  1237 				housespec->grffile = _cur_grffile;
  1293 				housespec[i]->random_colour[2] = 0x0C;  // they stand for red, blue, orange and green
  1238 				housespec->random_colour[0] = 0x04;  // those 4 random colours are the base colour
  1294 				housespec[i]->random_colour[3] = 0x06;
  1239 				housespec->random_colour[1] = 0x08;  // for all new houses
       
  1240 				housespec->random_colour[2] = 0x0C;  // they stand for red, blue, orange and green
       
  1241 				housespec->random_colour[3] = 0x06;
  1295 
  1242 
  1296 				/* New houses do not (currently) expect to have a default start
  1243 				/* New houses do not (currently) expect to have a default start
  1297 				 * date before 1930, as this breaks the build date stuff. See
  1244 				 * date before 1930, as this breaks the build date stuff. See
  1298 				 * FinaliseHouseArray() for more details. */
  1245 				 * FinaliseHouseArray() for more details. */
  1299 				if (housespec[i]->min_date < 1930) housespec[i]->min_date = 1930;
  1246 				if (housespec->min_date < 1930) housespec->min_date = 1930;
  1300 			}
  1247 
  1301 			_loaded_newgrf_features.has_newhouses = true;
  1248 				_loaded_newgrf_features.has_newhouses = true;
  1302 			break;
  1249 			} break;
  1303 
  1250 
  1304 		case 0x09: // Building flags
  1251 			case 0x09: // Building flags
  1305 			FOR_EACH_OBJECT {
  1252 				housespec->building_flags = (BuildingFlags)grf_load_byte(&buf);
  1306 				byte state = grf_load_byte(&buf);
  1253 				break;
  1307 				housespec[i]->building_flags = (BuildingFlags)state;
  1254 
  1308 			}
  1255 			case 0x0A: { // Availability years
  1309 			break;
       
  1310 
       
  1311 		case 0x0A: // Availability years
       
  1312 			FOR_EACH_OBJECT {
       
  1313 				uint16 years = grf_load_word(&buf);
  1256 				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);
  1257 				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);
  1258 				housespec->max_date = GB(years, 8, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 8, 8);
  1316 			}
  1259 			} break;
  1317 			break;
  1260 
  1318 
  1261 			case 0x0B: // Population
  1319 		case 0x0B: // Population
  1262 				housespec->population = grf_load_byte(&buf);
  1320 			FOR_EACH_OBJECT housespec[i]->population = grf_load_byte(&buf);
  1263 				break;
  1321 			break;
  1264 
  1322 
  1265 			case 0x0C: // Mail generation multiplier
  1323 		case 0x0C: // Mail generation multiplier
  1266 				housespec->mail_generation = grf_load_byte(&buf);
  1324 			FOR_EACH_OBJECT housespec[i]->mail_generation = grf_load_byte(&buf);
  1267 				break;
  1325 			break;
  1268 
  1326 
  1269 			case 0x0D: // Passenger acceptance
  1327 		case 0x0D: // Passenger acceptance
  1270 			case 0x0E: // Mail acceptance
  1328 		case 0x0E: // Mail acceptance
  1271 				housespec->cargo_acceptance[prop - 0x0D] = grf_load_byte(&buf);
  1329 			FOR_EACH_OBJECT housespec[i]->cargo_acceptance[prop - 0x0D] = grf_load_byte(&buf);
  1272 				break;
  1330 			break;
  1273 
  1331 		case 0x0F: // Goods/candy, food/fizzy drinks acceptance
  1274 			case 0x0F: { // Goods/candy, food/fizzy drinks acceptance
  1332 			FOR_EACH_OBJECT {
       
  1333 				int8 goods = grf_load_byte(&buf);
  1275 				int8 goods = grf_load_byte(&buf);
  1334 
  1276 
  1335 				/* If value of goods is negative, it means in fact food or, if in toyland, fizzy_drink acceptance.
  1277 				/* 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 */
  1278 				 * 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) :
  1279 				housespec->accepts_cargo[2] = (goods >= 0) ? ((_opt.landscape == LT_TOYLAND) ? CT_CANDY : CT_GOODS) :
  1338 						((_opt.landscape == LT_TOYLAND) ? CT_FIZZY_DRINKS : CT_FOOD);
  1280 						((_opt.landscape == LT_TOYLAND) ? CT_FIZZY_DRINKS : CT_FOOD);
  1339 
  1281 
  1340 				housespec[i]->cargo_acceptance[2] = abs(goods); // but we do need positive value here
  1282 				housespec->cargo_acceptance[2] = abs(goods); // but we do need positive value here
  1341 			}
  1283 			} break;
  1342 			break;
  1284 
  1343 
  1285 			case 0x10: // Local authority rating decrease on removal
  1344 		case 0x10: // Local authority rating decrease on removal
  1286 				housespec->remove_rating_decrease = grf_load_word(&buf);
  1345 			FOR_EACH_OBJECT housespec[i]->remove_rating_decrease = grf_load_word(&buf);
  1287 				break;
  1346 			break;
  1288 
  1347 
  1289 			case 0x11: // Removal cost multiplier
  1348 		case 0x11: // Removal cost multiplier
  1290 				housespec->removal_cost = grf_load_byte(&buf);
  1349 			FOR_EACH_OBJECT housespec[i]->removal_cost = grf_load_byte(&buf);
  1291 				break;
  1350 			break;
  1292 
  1351 
  1293 			case 0x12: // Building name ID
  1352 		case 0x12: // Building name ID
  1294 				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));
  1295 				break;
  1354 			break;
  1296 
  1355 
  1297 			case 0x13: // Building availability mask
  1356 		case 0x13: // Building availability mask
  1298 				housespec->building_availability = (HouseZones)grf_load_word(&buf);
  1357 			FOR_EACH_OBJECT {
  1299 				break;
  1358 				uint16 avail = grf_load_word(&buf);
  1300 
  1359 				housespec[i]->building_availability = (HouseZones)avail;
  1301 			case 0x14: // House callback flags
  1360 			}
  1302 				housespec->callback_mask = grf_load_byte(&buf);
  1361 			break;
  1303 				break;
  1362 
  1304 
  1363 		case 0x14: // House callback flags
  1305 			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);
  1306 				byte override = grf_load_byte(&buf);
  1370 
  1307 
  1371 				/* The house being overridden must be an original house. */
  1308 				/* The house being overridden must be an original house. */
  1372 				if (override >= NEW_HOUSE_OFFSET) {
  1309 				if (override >= NEW_HOUSE_OFFSET) {
  1373 					grfmsg(2, "TownHouseChangeInfo: Attempt to override new house %u with house id %u. Ignoring.", override, hid);
  1310 					grfmsg(2, "TownHouseChangeInfo: Attempt to override new house %u with house id %u. Ignoring.", override, hid);
  1374 					return false;
  1311 					continue;
  1375 				}
  1312 				}
  1376 
  1313 
  1377 				_house_mngr.Add(hid, override);
  1314 				_house_mngr.Add(hid, override);
  1378 			}
  1315 			} break;
  1379 			break;
  1316 
  1380 
  1317 			case 0x16: // Periodic refresh multiplier
  1381 		case 0x16: // Periodic refresh multiplier
  1318 				housespec->processing_time = grf_load_byte(&buf);
  1382 			FOR_EACH_OBJECT housespec[i]->processing_time = grf_load_byte(&buf);
  1319 				break;
  1383 			break;
  1320 
  1384 
  1321 			case 0x17: // Four random colours to use
  1385 		case 0x17: // Four random colours to use
  1322 				for (uint j = 0; j < 4; j++) housespec->random_colour[j] = grf_load_byte(&buf);
  1386 			FOR_EACH_OBJECT {
  1323 				break;
  1387 				uint j;
  1324 
  1388 				for (j = 0; j < 4; j++) housespec[i]->random_colour[j] = grf_load_byte(&buf);
  1325 			case 0x18: // Relative probability of appearing
  1389 			}
  1326 				housespec->probability = grf_load_byte(&buf);
  1390 			break;
  1327 				break;
  1391 
  1328 
  1392 		case 0x18: // Relative probability of appearing
  1329 			case 0x19: // Extra flags
  1393 			FOR_EACH_OBJECT housespec[i]->probability = grf_load_byte(&buf);
  1330 				housespec->extra_flags = (HouseExtraFlags)grf_load_byte(&buf);
  1394 			break;
  1331 				break;
  1395 
  1332 
  1396 		case 0x19: // Extra flags
  1333 			case 0x1A: // Animation frames
  1397 			FOR_EACH_OBJECT {
  1334 				housespec->animation_frames = grf_load_byte(&buf);
  1398 				byte flags = grf_load_byte(&buf);
  1335 				break;
  1399 				housespec[i]->extra_flags = (HouseExtraFlags)flags;
  1336 
  1400 			}
  1337 			case 0x1B: // Animation speed
  1401 			break;
  1338 				housespec->animation_speed = clamp(grf_load_byte(&buf), 2, 16);
  1402 
  1339 				break;
  1403 		case 0x1A: // Animation frames
  1340 
  1404 			FOR_EACH_OBJECT housespec[i]->animation_frames = grf_load_byte(&buf);
  1341 			case 0x1C: // Class of the building type
  1405 			break;
  1342 				housespec->class_id = AllocateHouseClassID(grf_load_byte(&buf), _cur_grffile->grfid);
  1406 
  1343 				break;
  1407 		case 0x1B: // Animation speed
  1344 
  1408 			FOR_EACH_OBJECT housespec[i]->animation_speed = clamp(grf_load_byte(&buf), 2, 16);
  1345 			case 0x1D: // Callback flags 2
  1409 			break;
  1346 				housespec->callback_mask |= (grf_load_byte(&buf) << 8);
  1410 
  1347 				break;
  1411 		case 0x1C: // Class of the building type
  1348 
  1412 			FOR_EACH_OBJECT housespec[i]->class_id = AllocateHouseClassID(grf_load_byte(&buf), _cur_grffile->grfid);
  1349 			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);
  1350 				uint32 cargotypes = grf_load_dword(&buf);
  1422 
  1351 
  1423 				/* Check if the cargo types should not be changed */
  1352 				/* Check if the cargo types should not be changed */
  1424 				if (cargotypes == 0xFFFFFFFF) break;
  1353 				if (cargotypes == 0xFFFFFFFF) break;
  1425 
  1354 
  1428 					uint8 cargo_part = GB(cargotypes, 8 * j, 8);
  1357 					uint8 cargo_part = GB(cargotypes, 8 * j, 8);
  1429 					CargoID cargo = GetCargoTranslation(cargo_part, _cur_grffile);
  1358 					CargoID cargo = GetCargoTranslation(cargo_part, _cur_grffile);
  1430 
  1359 
  1431 					if (cargo == CT_INVALID) {
  1360 					if (cargo == CT_INVALID) {
  1432 						/* Disable acceptance of invalid cargo type */
  1361 						/* Disable acceptance of invalid cargo type */
  1433 						housespec[i]->cargo_acceptance[j] = 0;
  1362 						housespec->cargo_acceptance[j] = 0;
  1434 					} else {
  1363 					} else {
  1435 						housespec[i]->accepts_cargo[j] = cargo;
  1364 						housespec->accepts_cargo[j] = cargo;
  1436 					}
  1365 					}
  1437 				}
  1366 				}
  1438 			}
  1367 			} break;
  1439 			break;
  1368 
  1440 
  1369 			case 0x1F: // Minimum life span
  1441 		case 0x1F: // Minimum life span
  1370 				housespec->minimum_life = grf_load_byte(&buf);
  1442 			FOR_EACH_OBJECT housespec[i]->minimum_life = grf_load_byte(&buf);
  1371 				break;
  1443 			break;
  1372 
  1444 
  1373 			default:
  1445 		default:
  1374 				ret = true;
  1446 			ret = true;
  1375 				break;
  1447 			break;
  1376 		}
  1448 	}
  1377 	}
  1449 
  1378 
  1450 	*bufp = buf;
  1379 	*bufp = buf;
  1451 	return ret;
  1380 	return ret;
  1452 }
  1381 }
  1453 
  1382 
  1454 static bool GlobalVarChangeInfo(uint gvid, int numinfo, int prop, byte **bufp, int len)
  1383 static bool GlobalVarChangeInfo(uint gvid, int numinfo, int prop, byte **bufp, int len)
  1455 {
  1384 {
  1456 	byte *buf = *bufp;
  1385 	byte *buf = *bufp;
  1457 	int i;
       
  1458 	bool ret = false;
  1386 	bool ret = false;
  1459 
  1387 
  1460 	switch (prop) {
  1388 	for (int i = 0; i < numinfo; i++) {
  1461 		case 0x08: /* Cost base factor */
  1389 		switch (prop) {
  1462 			FOR_EACH_OBJECT {
  1390 			case 0x08: { /* Cost base factor */
  1463 				byte factor = grf_load_byte(&buf);
  1391 				byte factor = grf_load_byte(&buf);
  1464 				uint price = gvid + i;
  1392 				uint price = gvid + i;
  1465 
  1393 
  1466 				if (price < NUM_PRICES) {
  1394 				if (price < NUM_PRICES) {
  1467 					SetPriceBaseMultiplier(price, factor);
  1395 					SetPriceBaseMultiplier(price, factor);
  1468 				} else {
  1396 				} else {
  1469 					grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
  1397 					grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
  1470 				}
  1398 				}
  1471 			}
  1399 			} break;
  1472 			break;
  1400 
  1473 
  1401 			case 0x09: /* Cargo translation table */
  1474 		case 0x09: /* Cargo translation table */
  1402 				/* 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. */
  1403 				/* Each entry is 4 bytes. */
  1476 			/* Each entry is 4 bytes. */
  1404 				buf += 4;
  1477 			buf += numinfo * 4;
  1405 				break;
  1478 			break;
  1406 
  1479 
  1407 			case 0x0A: { // Currency display names
  1480 		case 0x0A: // Currency display names
       
  1481 			FOR_EACH_OBJECT {
       
  1482 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1408 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1483 				StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
  1409 				StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
  1484 
  1410 
  1485 				if ((newone != STR_UNDEFINED) && (curidx < NUM_CURRENCY)) {
  1411 				if ((newone != STR_UNDEFINED) && (curidx < NUM_CURRENCY)) {
  1486 					_currency_specs[curidx].name = newone;
  1412 					_currency_specs[curidx].name = newone;
  1487 				}
  1413 				}
  1488 			}
  1414 			} break;
  1489 			break;
  1415 
  1490 
  1416 			case 0x0B: { // Currency multipliers
  1491 		case 0x0B: // Currency multipliers
       
  1492 			FOR_EACH_OBJECT {
       
  1493 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1417 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1494 				uint32 rate = grf_load_dword(&buf);
  1418 				uint32 rate = grf_load_dword(&buf);
  1495 
  1419 
  1496 				if (curidx < NUM_CURRENCY) {
  1420 				if (curidx < NUM_CURRENCY) {
  1497 					/* TTDPatch uses a multiple of 1000 for its conversion calculations,
  1421 					/* TTDPatch uses a multiple of 1000 for its conversion calculations,
  1499 					 * to be compatible */
  1423 					 * to be compatible */
  1500 					_currency_specs[curidx].rate = rate / 1000;
  1424 					_currency_specs[curidx].rate = rate / 1000;
  1501 				} else {
  1425 				} else {
  1502 					grfmsg(1, "GlobalVarChangeInfo: Currency multipliers %d out of range, ignoring", curidx);
  1426 					grfmsg(1, "GlobalVarChangeInfo: Currency multipliers %d out of range, ignoring", curidx);
  1503 				}
  1427 				}
  1504 			}
  1428 			} break;
  1505 			break;
  1429 
  1506 
  1430 			case 0x0C: { // Currency options
  1507 		case 0x0C: // Currency options
       
  1508 			FOR_EACH_OBJECT {
       
  1509 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1431 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1510 				uint16 options = grf_load_word(&buf);
  1432 				uint16 options = grf_load_word(&buf);
  1511 
  1433 
  1512 				if (curidx < NUM_CURRENCY) {
  1434 				if (curidx < NUM_CURRENCY) {
  1513 					_currency_specs[curidx].separator = GB(options, 0, 8);
  1435 					_currency_specs[curidx].separator = GB(options, 0, 8);
  1515 					 * since newgrf specs said that only 0 and 1 can be set for symbol_pos */
  1437 					 * 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);
  1438 					_currency_specs[curidx].symbol_pos = GB(options, 8, 1);
  1517 				} else {
  1439 				} else {
  1518 					grfmsg(1, "GlobalVarChangeInfo: Currency option %d out of range, ignoring", curidx);
  1440 					grfmsg(1, "GlobalVarChangeInfo: Currency option %d out of range, ignoring", curidx);
  1519 				}
  1441 				}
  1520 			}
  1442 			} break;
  1521 			break;
  1443 
  1522 
  1444 			case 0x0D: { // Currency prefix symbol
  1523 		case 0x0D: // Currency prefix symbol
       
  1524 			FOR_EACH_OBJECT {
       
  1525 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1445 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1526 				uint32 tempfix = grf_load_dword(&buf);
  1446 				uint32 tempfix = grf_load_dword(&buf);
  1527 
  1447 
  1528 				if (curidx < NUM_CURRENCY) {
  1448 				if (curidx < NUM_CURRENCY) {
  1529 					memcpy(_currency_specs[curidx].prefix,&tempfix,4);
  1449 					memcpy(_currency_specs[curidx].prefix,&tempfix,4);
  1530 					_currency_specs[curidx].prefix[4] = 0;
  1450 					_currency_specs[curidx].prefix[4] = 0;
  1531 				} else {
  1451 				} else {
  1532 					grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
  1452 					grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
  1533 				}
  1453 				}
  1534 			}
  1454 			} break;
  1535 			break;
  1455 
  1536 
  1456 			case 0x0E: { // Currency suffix symbol
  1537 		case 0x0E: // Currency suffix symbol
       
  1538 			FOR_EACH_OBJECT {
       
  1539 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1457 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1540 				uint32 tempfix = grf_load_dword(&buf);
  1458 				uint32 tempfix = grf_load_dword(&buf);
  1541 
  1459 
  1542 				if (curidx < NUM_CURRENCY) {
  1460 				if (curidx < NUM_CURRENCY) {
  1543 					memcpy(&_currency_specs[curidx].suffix, &tempfix, 4);
  1461 					memcpy(&_currency_specs[curidx].suffix, &tempfix, 4);
  1544 					_currency_specs[curidx].suffix[4] = 0;
  1462 					_currency_specs[curidx].suffix[4] = 0;
  1545 				} else {
  1463 				} else {
  1546 					grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
  1464 					grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
  1547 				}
  1465 				}
  1548 			}
  1466 			} break;
  1549 			break;
  1467 
  1550 
  1468 			case 0x0F: { //  Euro introduction dates
  1551 		case 0x0F: //  Euro introduction dates
       
  1552 			FOR_EACH_OBJECT {
       
  1553 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1469 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1554 				Year year_euro = grf_load_word(&buf);
  1470 				Year year_euro = grf_load_word(&buf);
  1555 
  1471 
  1556 				if (curidx < NUM_CURRENCY) {
  1472 				if (curidx < NUM_CURRENCY) {
  1557 					_currency_specs[curidx].to_euro = year_euro;
  1473 					_currency_specs[curidx].to_euro = year_euro;
  1558 				} else {
  1474 				} else {
  1559 					grfmsg(1, "GlobalVarChangeInfo: Euro intro date %d out of range, ignoring", curidx);
  1475 					grfmsg(1, "GlobalVarChangeInfo: Euro intro date %d out of range, ignoring", curidx);
  1560 				}
  1476 				}
  1561 			}
  1477 			} break;
  1562 			break;
  1478 
  1563 
  1479 			case 0x10: // 12 * 32 * B Snow line height table
  1564 		case 0x10: // 12 * 32 * B Snow line height table
  1480 				if (numinfo > 1 || IsSnowLineSet()) {
  1565 			if (numinfo > 1 || IsSnowLineSet()) {
  1481 					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);
  1482 				} else if (len < SNOW_LINE_MONTHS * SNOW_LINE_DAYS) {
  1567 			} else if (len < SNOW_LINE_MONTHS * SNOW_LINE_DAYS) {
  1483 					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);
  1484 				} else {
  1569 			} else {
  1485 					byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS];
  1570 				byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS];
  1486 
  1571 
  1487 					for (uint i = 0; i < SNOW_LINE_MONTHS; i++) {
  1572 				for (uint i = 0; i < SNOW_LINE_MONTHS; i++) {
  1488 						for (uint j = 0; j < SNOW_LINE_DAYS; j++) {
  1573 					for (uint j = 0; j < SNOW_LINE_DAYS; j++) {
  1489 							table[i][j] = grf_load_byte(&buf);
  1574 						table[i][j] = grf_load_byte(&buf);
  1490 						}
  1575 					}
  1491 					}
  1576 				}
  1492 					SetSnowLine(table);
  1577 				SetSnowLine(table);
  1493 				}
  1578 			}
  1494 				break;
  1579 			break;
  1495 
  1580 
  1496 			default:
  1581 		default:
  1497 				ret = true;
  1582 			ret = true;
  1498 				break;
       
  1499 		}
  1583 	}
  1500 	}
  1584 
  1501 
  1585 	*bufp = buf;
  1502 	*bufp = buf;
  1586 	return ret;
  1503 	return ret;
  1587 }
  1504 }
  1588 
  1505 
  1589 static bool CargoChangeInfo(uint cid, int numinfo, int prop, byte **bufp, int len)
  1506 static bool CargoChangeInfo(uint cid, int numinfo, int prop, byte **bufp, int len)
  1590 {
  1507 {
       
  1508 	byte *buf = *bufp;
       
  1509 	bool ret = false;
       
  1510 
  1591 	if (cid + numinfo > NUM_CARGO) {
  1511 	if (cid + numinfo > NUM_CARGO) {
  1592 		grfmsg(2, "CargoChangeInfo: Cargo type %d out of range (max %d)", cid + numinfo, NUM_CARGO - 1);
  1512 		grfmsg(2, "CargoChangeInfo: Cargo type %d out of range (max %d)", cid + numinfo, NUM_CARGO - 1);
  1593 		return false;
  1513 		return false;
  1594 	}
  1514 	}
  1595 
  1515 
  1596 	CargoSpec *cs = &_cargo[cid];
  1516 	for (int i = 0; i < numinfo; i++) {
  1597 	byte *buf = *bufp;
  1517 		CargoSpec *cs = &_cargo[cid + i];
  1598 	int i;
  1518 
  1599 	bool ret = false;
  1519 		switch (prop) {
  1600 
  1520 			case 0x08: /* Bit number of cargo */
  1601 	switch (prop) {
  1521 				cs->bitnum = grf_load_byte(&buf);
  1602 		case 0x08: /* Bit number of cargo */
  1522 				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()) {
  1523 				if (cs->IsValid()) {
  1607 					SETBIT(_cargo_mask, cid + i);
  1524 					SETBIT(_cargo_mask, cid + i);
  1608 				} else {
  1525 				} else {
  1609 					CLRBIT(_cargo_mask, cid + i);
  1526 					CLRBIT(_cargo_mask, cid + i);
  1610 				}
  1527 				}
  1611 			}
  1528 				break;
  1612 			break;
  1529 
  1613 
  1530 			case 0x09: /* String ID for cargo type name */
  1614 		case 0x09: /* String ID for cargo type name */
  1531 				cs->name = grf_load_word(&buf);
  1615 			FOR_EACH_OBJECT cs[i].name = grf_load_word(&buf);
  1532 				break;
  1616 			break;
  1533 
  1617 
  1534 			case 0x0A: /* String for cargo name, plural */
  1618 		case 0x0A: /* String for cargo name, plural */
  1535 				cs->name_plural = grf_load_word(&buf);
  1619 			FOR_EACH_OBJECT cs[i].name_plural = grf_load_word(&buf);
  1536 				break;
  1620 			break;
  1537 
  1621 
  1538 			case 0x0B:
  1622 		case 0x0B:
  1539 				/* 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
  1540 				 * (e.g. 10 tonnes of coal) */
  1624 			 * (e.g. 10 tonnes of coal) */
  1541 				cs->units_volume = grf_load_word(&buf);
  1625 			FOR_EACH_OBJECT cs[i].units_volume = grf_load_word(&buf);
  1542 				break;
  1626 			break;
  1543 
  1627 
  1544 			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) */
  1545 				cs->quantifier = grf_load_word(&buf);
  1629 			FOR_EACH_OBJECT cs[i].quantifier = grf_load_word(&buf);
  1546 				break;
  1630 			break;
  1547 
  1631 
  1548 			case 0x0D: /* String for two letter cargo abbreviation */
  1632 		case 0x0D: /* String for two letter cargo abbreviation */
  1549 				cs->abbrev = grf_load_word(&buf);
  1633 			FOR_EACH_OBJECT cs[i].abbrev = grf_load_word(&buf);
  1550 				break;
  1634 			break;
  1551 
  1635 
  1552 			case 0x0E: /* Sprite ID for cargo icon */
  1636 		case 0x0E: /* Sprite ID for cargo icon */
  1553 				cs->sprite = grf_load_word(&buf);
  1637 			FOR_EACH_OBJECT cs[i].sprite = grf_load_word(&buf);
  1554 				break;
  1638 			break;
  1555 
  1639 
  1556 			case 0x0F: /* Weight of one unit of cargo */
  1640 		case 0x0F: /* Weight of one unit of cargo */
  1557 				cs->weight = grf_load_byte(&buf);
  1641 			FOR_EACH_OBJECT cs[i].weight = grf_load_byte(&buf);
  1558 				break;
  1642 			break;
  1559 
  1643 
  1560 			case 0x10: /* Used for payment calculation */
  1644 		case 0x10: /* Used for payment calculation */
  1561 				cs->transit_days[0] = grf_load_byte(&buf);
  1645 			FOR_EACH_OBJECT cs[i].transit_days[0] = grf_load_byte(&buf);
  1562 				break;
  1646 			break;
  1563 
  1647 
  1564 			case 0x11: /* Used for payment calculation */
  1648 		case 0x11: /* Used for payment calculation */
  1565 				cs->transit_days[1] = grf_load_byte(&buf);
  1649 			FOR_EACH_OBJECT cs[i].transit_days[1] = grf_load_byte(&buf);
  1566 				break;
  1650 			break;
  1567 
  1651 
  1568 			case 0x12: /* Base cargo price */
  1652 		case 0x12: /* Base cargo price */
  1569 				cs->initial_payment = grf_load_dword(&buf);
  1653 			FOR_EACH_OBJECT cs[i].initial_payment = grf_load_dword(&buf);
  1570 				break;
  1654 			break;
  1571 
  1655 
  1572 			case 0x13: /* Colour for station rating bars */
  1656 		case 0x13: /* Colour for station rating bars */
  1573 				cs->rating_colour = MapDOSColour(grf_load_byte(&buf));
  1657 			FOR_EACH_OBJECT cs[i].rating_colour = MapDOSColour(grf_load_byte(&buf));
  1574 				break;
  1658 			break;
  1575 
  1659 
  1576 			case 0x14: /* Colour for cargo graph */
  1660 		case 0x14: /* Colour for cargo graph */
  1577 				cs->legend_colour = MapDOSColour(grf_load_byte(&buf));
  1661 			FOR_EACH_OBJECT cs[i].legend_colour = MapDOSColour(grf_load_byte(&buf));
  1578 				break;
  1662 			break;
  1579 
  1663 
  1580 			case 0x15: /* Freight status */
  1664 		case 0x15: /* Freight status */
  1581 				cs->is_freight = grf_load_byte(&buf) != 0;
  1665 			FOR_EACH_OBJECT cs[i].is_freight = grf_load_byte(&buf) != 0;
  1582 				break;
  1666 			break;
  1583 
  1667 
  1584 			case 0x16: /* Cargo classes */
  1668 		case 0x16: /* Cargo classes */
  1585 				cs->classes = grf_load_word(&buf);
  1669 			FOR_EACH_OBJECT cs[i].classes = grf_load_word(&buf);
  1586 				break;
  1670 			break;
  1587 
  1671 
  1588 			case 0x17: /* Cargo label */
  1672 		case 0x17: /* Cargo label */
  1589 				cs->label = grf_load_dword(&buf);
  1673 			FOR_EACH_OBJECT {
  1590 				cs->label = BSWAP32(cs->label);
  1674 				cs[i].label = grf_load_dword(&buf);
  1591 				break;
  1675 				cs[i].label = BSWAP32(cs[i].label);
  1592 
  1676 			}
  1593 			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);
  1594 				uint8 substitute_type = grf_load_byte(&buf);
       
  1595 
  1682 				switch (substitute_type) {
  1596 				switch (substitute_type) {
  1683 					case 0x00: cs[i].town_effect = TE_PASSENGERS; break;
  1597 					case 0x00: cs->town_effect = TE_PASSENGERS; break;
  1684 					case 0x02: cs[i].town_effect = TE_MAIL; break;
  1598 					case 0x02: cs->town_effect = TE_MAIL; break;
  1685 					case 0x05: cs[i].town_effect = TE_GOODS; break;
  1599 					case 0x05: cs->town_effect = TE_GOODS; break;
  1686 					case 0x09: cs[i].town_effect = TE_WATER; break;
  1600 					case 0x09: cs->town_effect = TE_WATER; break;
  1687 					case 0x0B: cs[i].town_effect = TE_FOOD; break;
  1601 					case 0x0B: cs->town_effect = TE_FOOD; break;
  1688 					default:
  1602 					default:
  1689 						grfmsg(1, "CargoChangeInfo: Unknown town growth substitute value %d, setting to none.", substitute_type);
  1603 						grfmsg(1, "CargoChangeInfo: Unknown town growth substitute value %d, setting to none.", substitute_type);
  1690 					case 0xFF: cs[i].town_effect = TE_NONE; break;
  1604 					case 0xFF: cs->town_effect = TE_NONE; break;
  1691 				}
  1605 				}
  1692 			}
  1606 			} break;
  1693 			break;
  1607 
  1694 
  1608 			case 0x19: /* Town growth coefficient */
  1695 		case 0x19: /* Town growth coefficient */
  1609 				cs->multipliertowngrowth = grf_load_word(&buf);
  1696 			FOR_EACH_OBJECT cs[i].multipliertowngrowth = grf_load_word(&buf);
  1610 				break;
  1697 			break;
  1611 
  1698 
  1612 			case 0x1A: /* Bitmask of callbacks to use */
  1699 		case 0x1A: /* Bitmask of callbacks to use */
  1613 				cs->callback_mask = grf_load_byte(&buf);
  1700 			FOR_EACH_OBJECT cs[i].callback_mask = grf_load_byte(&buf);
  1614 				break;
  1701 			break;
  1615 
  1702 
  1616 			default:
  1703 		default:
  1617 				ret = true;
  1704 			ret = true;
  1618 				break;
       
  1619 		}
  1705 	}
  1620 	}
  1706 
  1621 
  1707 	*bufp = buf;
  1622 	*bufp = buf;
  1708 	return ret;
  1623 	return ret;
  1709 }
  1624 }
  1710 
  1625 
  1711 
  1626 
  1712 static bool SoundEffectChangeInfo(uint sid, int numinfo, int prop, byte **bufp, int len)
  1627 static bool SoundEffectChangeInfo(uint sid, int numinfo, int prop, byte **bufp, int len)
  1713 {
  1628 {
  1714 	byte *buf = *bufp;
  1629 	byte *buf = *bufp;
  1715 	int i;
       
  1716 	bool ret = false;
  1630 	bool ret = false;
  1717 
  1631 
  1718 	if (_cur_grffile->sound_offset == 0) {
  1632 	if (_cur_grffile->sound_offset == 0) {
  1719 		grfmsg(1, "SoundEffectChangeInfo: No effects defined, skipping");
  1633 		grfmsg(1, "SoundEffectChangeInfo: No effects defined, skipping");
  1720 		return false;
  1634 		return false;
  1721 	}
  1635 	}
  1722 
  1636 
  1723 	switch (prop) {
  1637 	for (int i = 0; i < numinfo; i++) {
  1724 		case 0x08: // Relative volume
  1638 		uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
  1725 			FOR_EACH_OBJECT {
  1639 
  1726 				uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
  1640 		if (sound >= GetNumSounds()) {
  1727 
  1641 			grfmsg(1, "SoundEffectChangeInfo: Sound %d not defined (max %d)", sound, GetNumSounds());
  1728 				if (sound >= GetNumSounds()) {
  1642 			continue;
  1729 					grfmsg(1, "SoundEffectChangeInfo: Sound %d not defined (max %d)", sound, GetNumSounds());
  1643 		}
  1730 				} else {
  1644 
  1731 					GetSound(sound)->volume = grf_load_byte(&buf);
  1645 		switch (prop) {
  1732 				}
  1646 			case 0x08: // Relative volume
  1733 			}
  1647 				GetSound(sound)->volume = grf_load_byte(&buf);
  1734 			break;
  1648 				break;
  1735 
  1649 
  1736 		case 0x09: // Priority
  1650 			case 0x09: // Priority
  1737 			FOR_EACH_OBJECT {
  1651 				GetSound(sound)->priority = grf_load_byte(&buf);
  1738 				uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
  1652 				break;
  1739 
  1653 
  1740 				if (sound >= GetNumSounds()) {
  1654 			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);
  1655 				uint orig_sound = grf_load_byte(&buf);
  1752 
  1656 
  1753 				if (sound >= GetNumSounds() || orig_sound >= GetNumSounds()) {
  1657 				if (orig_sound >= GetNumSounds()) {
  1754 					grfmsg(1, "SoundEffectChangeInfo: Sound %d or %d not defined (max %d)", sound, orig_sound, GetNumSounds());
  1658 					grfmsg(1, "SoundEffectChangeInfo: Original sound %d not defined (max %d)", orig_sound, GetNumSounds());
  1755 				} else {
  1659 				} else {
  1756 					FileEntry *newfe = GetSound(sound);
  1660 					FileEntry *newfe = GetSound(sound);
  1757 					FileEntry *oldfe = GetSound(orig_sound);
  1661 					FileEntry *oldfe = GetSound(orig_sound);
  1758 
  1662 
  1759 					/* Literally copy the data of the new sound over the original */
  1663 					/* Literally copy the data of the new sound over the original */
  1760 					*oldfe = *newfe;
  1664 					*oldfe = *newfe;
  1761 				}
  1665 				}
  1762 			}
  1666 			} break;
  1763 			break;
  1667 
  1764 
  1668 			default:
  1765 		default:
  1669 				ret = true;
  1766 			ret = true;
  1670 				break;
       
  1671 		}
  1767 	}
  1672 	}
  1768 
  1673 
  1769 	*bufp = buf;
  1674 	*bufp = buf;
  1770 	return ret;
  1675 	return ret;
  1771 }
  1676 }
  1772 
  1677 
  1773 /* Action 0x00 */
  1678 /* Action 0x00 */
  1774 static void FeatureChangeInfo(byte *buf, int len)
  1679 static void FeatureChangeInfo(byte *buf, int len)
  1775 {
  1680 {
  1776 	byte *bufend = buf + len;
  1681 	byte *bufend = buf + len;
  1777 	uint i;
       
  1778 
  1682 
  1779 	/* <00> <feature> <num-props> <num-info> <id> (<property <new-info>)...
  1683 	/* <00> <feature> <num-props> <num-info> <id> (<property <new-info>)...
  1780 	 *
  1684 	 *
  1781 	 * B feature       0, 1, 2 or 3 for trains, road vehicles, ships or planes
  1685 	 * B feature       0, 1, 2 or 3 for trains, road vehicles, ships or planes
  1782 	 *                 4 for defining new train station sets
  1686 	 *                 4 for defining new train station sets
  1803 		/* GSF_INDUSTRIES */   NULL,
  1707 		/* GSF_INDUSTRIES */   NULL,
  1804 		/* GSF_CARGOS */       NULL, /* Cargo is handled during reservation */
  1708 		/* GSF_CARGOS */       NULL, /* Cargo is handled during reservation */
  1805 		/* GSF_SOUNDFX */      SoundEffectChangeInfo,
  1709 		/* GSF_SOUNDFX */      SoundEffectChangeInfo,
  1806 	};
  1710 	};
  1807 
  1711 
  1808 	EngineInfo *ei = NULL;
       
  1809 
       
  1810 	if (!check_length(len, 6, "FeatureChangeInfo")) return;
  1712 	if (!check_length(len, 6, "FeatureChangeInfo")) return;
  1811 	buf++;
  1713 	buf++;
  1812 	uint8 feature  = grf_load_byte(&buf);
  1714 	uint8 feature  = grf_load_byte(&buf);
  1813 	uint8 numprops = grf_load_byte(&buf);
  1715 	uint8 numprops = grf_load_byte(&buf);
  1814 	uint numinfo  = grf_load_byte(&buf);
  1716 	uint numinfo  = grf_load_byte(&buf);
  1825 	if (feature <= GSF_AIRCRAFT) {
  1727 	if (feature <= GSF_AIRCRAFT) {
  1826 		if (engine + numinfo > _vehcounts[feature]) {
  1728 		if (engine + numinfo > _vehcounts[feature]) {
  1827 			grfmsg(0, "FeatureChangeInfo: Last engine ID %d out of bounds (max %d), skipping", engine + numinfo, _vehcounts[feature]);
  1729 			grfmsg(0, "FeatureChangeInfo: Last engine ID %d out of bounds (max %d), skipping", engine + numinfo, _vehcounts[feature]);
  1828 			return;
  1730 			return;
  1829 		}
  1731 		}
  1830 		ei = &_engine_info[engine + _vehshifts[feature]];
       
  1831 	}
  1732 	}
  1832 
  1733 
  1833 	while (numprops-- && buf < bufend) {
  1734 	while (numprops-- && buf < bufend) {
  1834 		uint8 prop = grf_load_byte(&buf);
  1735 		uint8 prop = grf_load_byte(&buf);
  1835 		bool ignoring = false;
  1736 		bool ignoring = false;
  1836 
  1737 
  1837 		switch (feature) {
  1738 		switch (feature) {
  1838 			case GSF_TRAIN:
  1739 			case GSF_TRAIN:
  1839 			case GSF_ROAD:
  1740 			case GSF_ROAD:
  1840 			case GSF_SHIP:
  1741 			case GSF_SHIP:
  1841 			case GSF_AIRCRAFT:
  1742 			case GSF_AIRCRAFT: {
  1842 				/* Common properties for vehicles */
  1743 				bool handled = true;
  1843 				switch (prop) {
  1744 
  1844 					case 0x00: // Introduction date
  1745 				for (uint i = 0; i < numinfo; i++) {
  1845 						FOR_EACH_OBJECT ei[i].base_intro = grf_load_word(&buf) + DAYS_TILL_ORIGINAL_BASE_YEAR;
  1746 					EngineInfo *ei = &_engine_info[engine + _vehshifts[feature] + i];
  1846 						break;
  1747 
  1847 
  1748 					/* Common properties for vehicles */
  1848 					case 0x02: // Decay speed
  1749 					switch (prop) {
  1849 						FOR_EACH_OBJECT SB(ei[i].unk2, 0, 7, grf_load_byte(&buf) & 0x7F);
  1750 						case 0x00: // Introduction date
  1850 						break;
  1751 							ei->base_intro = grf_load_word(&buf) + DAYS_TILL_ORIGINAL_BASE_YEAR;
  1851 
  1752 							break;
  1852 					case 0x03: // Vehicle life
  1753 
  1853 						FOR_EACH_OBJECT ei[i].lifelength = grf_load_byte(&buf);
  1754 						case 0x02: // Decay speed
  1854 						break;
  1755 							SB(ei->unk2, 0, 7, grf_load_byte(&buf) & 0x7F);
  1855 
  1756 							break;
  1856 					case 0x04: // Model life
  1757 
  1857 						FOR_EACH_OBJECT ei[i].base_life = grf_load_byte(&buf);
  1758 						case 0x03: // Vehicle life
  1858 						break;
  1759 							ei->lifelength = grf_load_byte(&buf);
  1859 
  1760 							break;
  1860 					case 0x06: // Climates available
  1761 
  1861 						FOR_EACH_OBJECT ei[i].climates = grf_load_byte(&buf);
  1762 						case 0x04: // Model life
  1862 						break;
  1763 							ei->base_life = grf_load_byte(&buf);
  1863 
  1764 							break;
  1864 					case 0x07: // Loading speed
  1765 
  1865 						/* Hyronymus explained me what does
  1766 						case 0x06: // Climates available
  1866 						 * this mean and insists on having a
  1767 							ei->climates = grf_load_byte(&buf);
  1867 						 * credit ;-). --pasky */
  1768 							break;
  1868 						FOR_EACH_OBJECT ei[i].load_amount = grf_load_byte(&buf);
  1769 
  1869 						break;
  1770 						case 0x07: // Loading speed
  1870 
  1771 							/* Hyronymus explained me what does
  1871 					default:
  1772 							 * this mean and insists on having a
  1872 						if (handler[feature](engine, numinfo, prop, &buf, bufend - buf)) {
  1773 							 * credit ;-). --pasky */
  1873 							ignoring = true;
  1774 							ei->load_amount = grf_load_byte(&buf);
  1874 						}
  1775 							break;
  1875 						break;
  1776 
  1876 				}
  1777 						default:
  1877 				break;
  1778 							handled = false;
       
  1779 							break;
       
  1780 					}
       
  1781 				}
       
  1782 
       
  1783 				if (handled) break;
       
  1784 			} /* FALL THROUGH */
  1878 
  1785 
  1879 			default:
  1786 			default:
  1880 				if (handler[feature](engine, numinfo, prop, &buf, bufend - buf)) {
  1787 				if (handler[feature](engine, numinfo, prop, &buf, bufend - buf)) {
  1881 					ignoring = true;
  1788 					ignoring = true;
  1882 				}
  1789 				}
  1936 
  1843 
  1937 						free(_cur_grffile->cargo_list);
  1844 						free(_cur_grffile->cargo_list);
  1938 						_cur_grffile->cargo_max = numinfo;
  1845 						_cur_grffile->cargo_max = numinfo;
  1939 						_cur_grffile->cargo_list = MallocT<CargoLabel>(numinfo);
  1846 						_cur_grffile->cargo_list = MallocT<CargoLabel>(numinfo);
  1940 
  1847 
  1941 						int i;
  1848 						for (uint i = 0; i < numinfo; i++) {
  1942 						FOR_EACH_OBJECT {
       
  1943 							CargoLabel cl = grf_load_dword(&buf);
  1849 							CargoLabel cl = grf_load_dword(&buf);
  1944 							_cur_grffile->cargo_list[i] = BSWAP32(cl);
  1850 							_cur_grffile->cargo_list[i] = BSWAP32(cl);
  1945 						}
  1851 						}
  1946 						break;
  1852 						break;
  1947 				}
  1853 				}