src/newgrf.cpp
branchgamebalance
changeset 9912 1ac8aac92385
parent 9911 0b8b245a2391
child 9913 e79cd19772dd
equal deleted inserted replaced
9911:0b8b245a2391 9912:1ac8aac92385
    72 static uint32 _ttdpatch_flags[8];
    72 static uint32 _ttdpatch_flags[8];
    73 
    73 
    74 /* Used by Action 0x06 to preload a pseudo sprite and modify its content */
    74 /* Used by Action 0x06 to preload a pseudo sprite and modify its content */
    75 static byte *_preload_sprite = NULL;
    75 static byte *_preload_sprite = NULL;
    76 
    76 
    77 /* Set if any vehicle is loaded which uses 2cc (two company colours) */
    77 /* Indicates which are the newgrf features currently loaded ingame */
    78 bool _have_2cc = false;
    78 uint8 _loaded_newgrf_features;
    79 
       
    80 /* Set if there are any newhouses loaded. */
       
    81 bool _have_newhouses = false;
       
    82 
       
    83 
    79 
    84 enum GrfDataType {
    80 enum GrfDataType {
    85 	GDT_SOUND,
    81 	GDT_SOUND,
    86 };
    82 };
    87 
    83 
   130 static uint16 cargo_disallowed[TOTAL_NUM_ENGINES];
   126 static uint16 cargo_disallowed[TOTAL_NUM_ENGINES];
   131 
   127 
   132 /* Contains the GRF ID of the owner of a vehicle if it has been reserved */
   128 /* Contains the GRF ID of the owner of a vehicle if it has been reserved */
   133 static uint32 _grm_engines[TOTAL_NUM_ENGINES];
   129 static uint32 _grm_engines[TOTAL_NUM_ENGINES];
   134 
   130 
       
   131 /* Contains the GRF ID of the owner of a cargo if it has been reserved */
       
   132 static uint32 _grm_cargos[NUM_CARGO];
       
   133 
   135 /** DEBUG() function dedicated to newGRF debugging messages
   134 /** DEBUG() function dedicated to newGRF debugging messages
   136  * Function is essentialy the same as DEBUG(grf, severity, ...) with the
   135  * Function is essentialy the same as DEBUG(grf, severity, ...) with the
   137  * addition of file:line information when parsing grf files.
   136  * addition of file:line information when parsing grf files.
   138  * NOTE: for the above reason(s) grfmsg() should ONLY be used for
   137  * NOTE: for the above reason(s) grfmsg() should ONLY be used for
   139  * loading/parsing grf files, not for runtime debug messages as there
   138  * loading/parsing grf files, not for runtime debug messages as there
   537 			break;
   536 			break;
   538 
   537 
   539 		case 0x27: // Miscellaneous flags
   538 		case 0x27: // Miscellaneous flags
   540 			FOR_EACH_OBJECT {
   539 			FOR_EACH_OBJECT {
   541 				ei[i].misc_flags = grf_load_byte(&buf);
   540 				ei[i].misc_flags = grf_load_byte(&buf);
   542 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) _have_2cc = true;
   541 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) SETBIT(_loaded_newgrf_features, GRFLOADED_2CC);
   543 			}
   542 			}
   544 			break;
   543 			break;
   545 
   544 
   546 		case 0x28: // Cargo classes allowed
   545 		case 0x28: // Cargo classes allowed
   547 			FOR_EACH_OBJECT cargo_allowed[engine + i] = grf_load_word(&buf);
   546 			FOR_EACH_OBJECT cargo_allowed[engine + i] = grf_load_word(&buf);
   659 			break;
   658 			break;
   660 
   659 
   661 		case 0x1C: // Miscellaneous flags
   660 		case 0x1C: // Miscellaneous flags
   662 			FOR_EACH_OBJECT {
   661 			FOR_EACH_OBJECT {
   663 				ei[i].misc_flags = grf_load_byte(&buf);
   662 				ei[i].misc_flags = grf_load_byte(&buf);
   664 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) _have_2cc = true;
   663 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) SETBIT(_loaded_newgrf_features, GRFLOADED_2CC);
   665 			}
   664 			}
   666 			break;
   665 			break;
   667 
   666 
   668 		case 0x1D: // Cargo classes allowed
   667 		case 0x1D: // Cargo classes allowed
   669 			FOR_EACH_OBJECT cargo_allowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   668 			FOR_EACH_OBJECT cargo_allowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   770 			break;
   769 			break;
   771 
   770 
   772 		case 0x17: // Miscellaneous flags
   771 		case 0x17: // Miscellaneous flags
   773 			FOR_EACH_OBJECT {
   772 			FOR_EACH_OBJECT {
   774 				ei[i].misc_flags = grf_load_byte(&buf);
   773 				ei[i].misc_flags = grf_load_byte(&buf);
   775 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) _have_2cc = true;
   774 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) SETBIT(_loaded_newgrf_features, GRFLOADED_2CC);
   776 			}
   775 			}
   777 			break;
   776 			break;
   778 
   777 
   779 		case 0x18: // Cargo classes allowed
   778 		case 0x18: // Cargo classes allowed
   780 			FOR_EACH_OBJECT cargo_allowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   779 			FOR_EACH_OBJECT cargo_allowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   886 			break;
   885 			break;
   887 
   886 
   888 		case 0x17: // Miscellaneous flags
   887 		case 0x17: // Miscellaneous flags
   889 			FOR_EACH_OBJECT {
   888 			FOR_EACH_OBJECT {
   890 				ei[i].misc_flags = grf_load_byte(&buf);
   889 				ei[i].misc_flags = grf_load_byte(&buf);
   891 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) _have_2cc = true;
   890 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) SETBIT(_loaded_newgrf_features, GRFLOADED_2CC);
   892 			}
   891 			}
   893 			break;
   892 			break;
   894 
   893 
   895 		case 0x18: // Cargo classes allowed
   894 		case 0x18: // Cargo classes allowed
   896 			FOR_EACH_OBJECT cargo_allowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   895 			FOR_EACH_OBJECT cargo_allowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
  1295 				/* New houses do not (currently) expect to have a default start
  1294 				/* New houses do not (currently) expect to have a default start
  1296 				 * date before 1930, as this breaks the build date stuff. See
  1295 				 * date before 1930, as this breaks the build date stuff. See
  1297 				 * FinaliseHouseArray() for more details. */
  1296 				 * FinaliseHouseArray() for more details. */
  1298 				if (housespec[i]->min_date < 1930) housespec[i]->min_date = 1930;
  1297 				if (housespec[i]->min_date < 1930) housespec[i]->min_date = 1930;
  1299 			}
  1298 			}
  1300 			_have_newhouses = true;
  1299 			SETBIT(_loaded_newgrf_features, GRFLOADED_NEWHOUSES);
  1301 			break;
  1300 			break;
  1302 
  1301 
  1303 		case 0x09: // Building flags
  1302 		case 0x09: // Building flags
  1304 			FOR_EACH_OBJECT {
  1303 			FOR_EACH_OBJECT {
  1305 				byte state = grf_load_byte(&buf);
  1304 				byte state = grf_load_byte(&buf);
  2503 		for (uint i = 0; i < idcount; i++) {
  2502 		for (uint i = 0; i < idcount; i++) {
  2504 			uint8 engine = buf[3 + i] + _vehshifts[feature];
  2503 			uint8 engine = buf[3 + i] + _vehshifts[feature];
  2505 
  2504 
  2506 			/* Don't tell me you don't love duplicated code! */
  2505 			/* Don't tell me you don't love duplicated code! */
  2507 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2506 			if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
  2508 				grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  2507 				grfmsg(1, "VehicleMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping",
  2509 				       groupid, _cur_grffile->spritegroups_count);
  2508 				       groupid, _cur_grffile->spritegroups_count);
  2510 				continue;
  2509 				continue;
  2511 			}
  2510 			}
  2512 
  2511 
  2513 			if (wagover) {
  2512 			if (wagover) {
  2895 				return;
  2894 				return;
  2896 			}
  2895 			}
  2897 			replace = SPR_CANALS_BASE + 5;
  2896 			replace = SPR_CANALS_BASE + 5;
  2898 			break;
  2897 			break;
  2899 
  2898 
       
  2899 		case 0x09: // One way graphics
       
  2900 			if (num != 6) {
       
  2901 				grfmsg(1, "GraphicsNew: One way road graphics sprite count must be 6, skipping");
       
  2902 				return;
       
  2903 			}
       
  2904 			replace = SPR_ONEWAY_BASE;
       
  2905 			break;
       
  2906 
  2900 		case 0x0A: // 2CC colour maps
  2907 		case 0x0A: // 2CC colour maps
  2901 			if (num != 256) {
  2908 			if (num != 256) {
  2902 				grfmsg(1, "GraphicsNew: 2CC colour maps sprite count must be 256, skipping");
  2909 				grfmsg(1, "GraphicsNew: 2CC colour maps sprite count must be 256, skipping");
  2903 				return;
  2910 				return;
  2904 			}
  2911 			}
  2905 			replace = SPR_2CCMAP_BASE;
  2912 			replace = SPR_2CCMAP_BASE;
  2906 			break;
  2913 			break;
  2907 
  2914 
       
  2915 		case 0x0B: // tramways
       
  2916 			if (num != 113) {
       
  2917 				grfmsg(1, "GraphicsNew: Tramway graphics sprite count must be 113, skipping");
       
  2918 				return;
       
  2919 			}
       
  2920 			replace = SPR_TRAMWAY_BASE;
       
  2921 			break;
       
  2922 
  2908 		case 0x0D: // Coast graphics
  2923 		case 0x0D: // Coast graphics
  2909 			if (num != 16) {
  2924 			if (num != 16) {
  2910 				grfmsg(1, "GraphicsNew: Coast graphics sprite count must be 16, skipping");
  2925 				grfmsg(1, "GraphicsNew: Coast graphics sprite count must be 16, skipping");
  2911 				return;
  2926 				return;
  2912 			}
  2927 			}
  2930 			break;
  2945 			break;
  2931 
  2946 
  2932 		default:
  2947 		default:
  2933 			grfmsg(2, "GraphicsNew: Custom graphics (type 0x%02X) sprite block of length %u (unimplemented, ignoring)",
  2948 			grfmsg(2, "GraphicsNew: Custom graphics (type 0x%02X) sprite block of length %u (unimplemented, ignoring)",
  2934 					type, num);
  2949 					type, num);
       
  2950 			_skip_sprites = num;
  2935 			return;
  2951 			return;
  2936 	}
  2952 	}
  2937 
  2953 
  2938 	if (replace == 0) {
  2954 	if (replace == 0) {
  2939 		grfmsg(2, "GraphicsNew: Loading %u sprites of type 0x%02X at SpriteID 0x%04X", num, type, _cur_spriteid);
  2955 		grfmsg(2, "GraphicsNew: Loading %u sprites of type 0x%02X at SpriteID 0x%04X", num, type, _cur_spriteid);
  2969 			return clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
  2985 			return clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
  2970 
  2986 
  2971 		case 0x83: // current climate, 0=temp, 1=arctic, 2=trop, 3=toyland
  2987 		case 0x83: // current climate, 0=temp, 1=arctic, 2=trop, 3=toyland
  2972 			return _opt.landscape;
  2988 			return _opt.landscape;
  2973 
  2989 
  2974 		case 0x84: // GRF loading stage
  2990 		case 0x84: { // GRF loading stage
  2975 			return (_cur_stage > GLS_INIT) | ((_cur_stage == GLS_ACTIVATION) << 9);
  2991 			uint32 res = 0;
       
  2992 
       
  2993 			if (_cur_stage > GLS_INIT) SETBIT(res, 0);
       
  2994 			if (_cur_stage == GLS_RESERVE) SETBIT(res, 8);
       
  2995 			if (_cur_stage == GLS_ACTIVATION) SETBIT(res, 9);
       
  2996 			return res;
       
  2997 		}
  2976 
  2998 
  2977 		case 0x85: // TTDPatch flags, only for bit tests
  2999 		case 0x85: // TTDPatch flags, only for bit tests
  2978 			if (cond_val == NULL) {
  3000 			if (cond_val == NULL) {
  2979 				/* Supported in Action 0x07 and 0x09, not 0x0D */
  3001 				/* Supported in Action 0x07 and 0x09, not 0x0D */
  2980 				return 0;
  3002 				return 0;
  3253 		 * we use -1 to indicate that all further
  3275 		 * we use -1 to indicate that all further
  3254 		 * sprites should be skipped. */
  3276 		 * sprites should be skipped. */
  3255 		_skip_sprites = -1;
  3277 		_skip_sprites = -1;
  3256 
  3278 
  3257 		/* If an action 8 hasn't been encountered yet, disable the grf. */
  3279 		/* If an action 8 hasn't been encountered yet, disable the grf. */
  3258 		if (_cur_stage != GLS_RESERVE && _cur_grfconfig->status != GCS_ACTIVATED) _cur_grfconfig->status = GCS_DISABLED;
  3280 		if (_cur_grfconfig->status != GCS_ACTIVATED) _cur_grfconfig->status = GCS_DISABLED;
  3259 	}
  3281 	}
  3260 }
  3282 }
  3261 
  3283 
  3262 
  3284 
  3263 /* Action 0x08 (GLS_FILESCAN) */
  3285 /* Action 0x08 (GLS_FILESCAN) */
  3303 	uint32 grfid     = grf_load_dword(&buf);
  3325 	uint32 grfid     = grf_load_dword(&buf);
  3304 	const char *name = grf_load_string(&buf, len - 6);
  3326 	const char *name = grf_load_string(&buf, len - 6);
  3305 
  3327 
  3306 	_cur_grffile->grfid = grfid;
  3328 	_cur_grffile->grfid = grfid;
  3307 	_cur_grffile->grf_version = version;
  3329 	_cur_grffile->grf_version = version;
  3308 	_cur_grfconfig->status = _cur_stage < GLS_ACTIVATION ? GCS_INITIALISED : GCS_ACTIVATED;
  3330 	_cur_grfconfig->status = _cur_stage < GLS_RESERVE ? GCS_INITIALISED : GCS_ACTIVATED;
  3309 
  3331 
  3310 	/* Do swap the GRFID for displaying purposes since people expect that */
  3332 	/* Do swap the GRFID for displaying purposes since people expect that */
  3311 	DEBUG(grf, 1, "GRFInfo: Loaded GRFv%d set %08lX - %s", version, BSWAP32(grfid), name);
  3333 	DEBUG(grf, 1, "GRFInfo: Loaded GRFv%d set %08lX - %s", version, BSWAP32(grfid), name);
  3312 }
  3334 }
  3313 
  3335 
  3410 	byte message_id = grf_load_byte(&buf);
  3432 	byte message_id = grf_load_byte(&buf);
  3411 	len -= 4;
  3433 	len -= 4;
  3412 
  3434 
  3413 	/* Skip the error until the activation stage unless bit 7 of the severity
  3435 	/* Skip the error until the activation stage unless bit 7 of the severity
  3414 	 * is set. */
  3436 	 * is set. */
  3415 	if (!HASBIT(severity, 7) && _cur_stage < GLS_ACTIVATION) {
  3437 	if (!HASBIT(severity, 7) && _cur_stage == GLS_INIT) {
  3416 		grfmsg(7, "GRFLoadError: Skipping non-fatal GRFLoadError in stage 1");
  3438 		grfmsg(7, "GRFLoadError: Skipping non-fatal GRFLoadError in stage %d", _cur_stage);
  3417 		return;
  3439 		return;
  3418 	}
  3440 	}
  3419 	CLRBIT(severity, 7);
  3441 	CLRBIT(severity, 7);
  3420 
  3442 
  3421 	if (severity >= lengthof(sevstr)) {
  3443 	if (severity >= lengthof(sevstr)) {
  3524 
  3546 
  3525 		default:
  3547 		default:
  3526 			grfmsg(2, "ParamSet: Unknown Patch variable 0x%02X.", param);
  3548 			grfmsg(2, "ParamSet: Unknown Patch variable 0x%02X.", param);
  3527 			return 0;
  3549 			return 0;
  3528 	}
  3550 	}
       
  3551 }
       
  3552 
       
  3553 
       
  3554 static uint32 PerformGRM(uint32 *grm, uint16 num_ids, uint16 count, uint8 op, uint8 target, const char *type)
       
  3555 {
       
  3556 	uint start = 0;
       
  3557 	uint size  = 0;
       
  3558 
       
  3559 	if (op == 6) {
       
  3560 		/* Return GRFID of set that reserved ID */
       
  3561 		return grm[_cur_grffile->param[target]];
       
  3562 	}
       
  3563 
       
  3564 	/* With an operation of 2 or 3, we want to reserve a specific block of IDs */
       
  3565 	if (op == 2 || op == 3) start = _cur_grffile->param[target];
       
  3566 
       
  3567 	for (uint i = start; i < num_ids; i++) {
       
  3568 		if (grm[i] == 0) {
       
  3569 			size++;
       
  3570 		} else {
       
  3571 			if (op == 2 || op == 3) break;
       
  3572 			start = i + 1;
       
  3573 			size = 0;
       
  3574 		}
       
  3575 
       
  3576 		if (size == count) break;
       
  3577 	}
       
  3578 
       
  3579 	if (size == count) {
       
  3580 		/* Got the slot... */
       
  3581 		if (op == 0 || op == 3) {
       
  3582 			grfmsg(2, "ParamSet: GRM: Reserving %d %s at %d", count, type, start);
       
  3583 			for (uint i = 0; i < count; i++) grm[start + i] = _cur_grffile->grfid;
       
  3584 		}
       
  3585 		return start;
       
  3586 	}
       
  3587 
       
  3588 	/* Unable to allocate */
       
  3589 	if (op != 4 && op != 5) {
       
  3590 		/* Deactivate GRF */
       
  3591 		grfmsg(0, "ParamSet: GRM: Unable to allocate %d %s, deactivating", count, type);
       
  3592 		_cur_grfconfig->status = GCS_DISABLED;
       
  3593 		_skip_sprites = -1;
       
  3594 		return UINT_MAX;
       
  3595 	}
       
  3596 
       
  3597 	grfmsg(1, "ParamSet: GRM: Unable to allocate %d %s", count, type);
       
  3598 	return UINT_MAX;
  3529 }
  3599 }
  3530 
  3600 
  3531 
  3601 
  3532 /* Action 0x0D */
  3602 /* Action 0x0D */
  3533 static void ParamSet(byte *buf, int len)
  3603 static void ParamSet(byte *buf, int len)
  3597 					switch (feature) {
  3667 					switch (feature) {
  3598 						case 0x00: // Trains
  3668 						case 0x00: // Trains
  3599 						case 0x01: // Road Vehicles
  3669 						case 0x01: // Road Vehicles
  3600 						case 0x02: // Ships
  3670 						case 0x02: // Ships
  3601 						case 0x03: // Aircraft
  3671 						case 0x03: // Aircraft
  3602 						{
  3672 							src1 = PerformGRM(&_grm_engines[_vehshifts[feature]], _vehcounts[feature], count, op, target, "vehicles");
  3603 							uint start = 0;
  3673 							if (_skip_sprites == -1) return;
  3604 							uint size  = 0;
       
  3605 							uint shift = _vehshifts[feature];
       
  3606 
       
  3607 							if (op == 6) {
       
  3608 								/* Return GRFID of set that reserved ID */
       
  3609 								src1 = _grm_engines[shift + _cur_grffile->param[target]];
       
  3610 								break;
       
  3611 							}
       
  3612 
       
  3613 							/* With an operation of 2 or 3, we want to reserve a specific block of IDs */
       
  3614 							if (op == 2 || op == 3) start = _cur_grffile->param[target];
       
  3615 
       
  3616 							for (uint i = start; i < _vehcounts[feature]; i++) {
       
  3617 								if (_grm_engines[shift + i] == 0) {
       
  3618 									size++;
       
  3619 								} else {
       
  3620 									if (op == 2 || op == 3) break;
       
  3621 									start = i + 1;
       
  3622 									size = 0;
       
  3623 								}
       
  3624 
       
  3625 								if (size == count) break;
       
  3626 							}
       
  3627 
       
  3628 							if (size == count) {
       
  3629 								/* Got the slot... */
       
  3630 								if (op == 0 || op == 3) {
       
  3631 									grfmsg(2, "ParamSet: GRM: Reserving %d vehicles at %d", count, start);
       
  3632 									for (uint i = 0; i < count; i++) _grm_engines[shift + start + i] = _cur_grffile->grfid;
       
  3633 								}
       
  3634 								src1 = start;
       
  3635 							} else {
       
  3636 								/* Unable to allocate */
       
  3637 								if (op != 4 && op != 5) {
       
  3638 									/* Deactivate GRF */
       
  3639 									grfmsg(0, "ParamSet: GRM: Unable to allocate %d vehicles, deactivating", count);
       
  3640 									_cur_grfconfig->status = GCS_DISABLED;
       
  3641 
       
  3642 									_skip_sprites = -1;
       
  3643 									return;
       
  3644 								}
       
  3645 
       
  3646 								grfmsg(1, "ParamSet: GRM: Unable to allocate %d vehicles", count);
       
  3647 								src1 = UINT_MAX;
       
  3648 							}
       
  3649 							break;
  3674 							break;
  3650 						}
       
  3651 
  3675 
  3652 						case 0x08: // General sprites
  3676 						case 0x08: // General sprites
  3653 							switch (op) {
  3677 							switch (op) {
  3654 								case 0:
  3678 								case 0:
  3655 									/* Check if the allocated sprites will fit below the original sprite limit */
  3679 									/* Check if the allocated sprites will fit below the original sprite limit */
  3672 
  3696 
  3673 								default:
  3697 								default:
  3674 									grfmsg(1, "ParamSet: GRM: Unsupported operation %d for general sprites", op);
  3698 									grfmsg(1, "ParamSet: GRM: Unsupported operation %d for general sprites", op);
  3675 									return;
  3699 									return;
  3676 							}
  3700 							}
       
  3701 							break;
       
  3702 
       
  3703 						case 0x0B: // Cargo
       
  3704 							src1 = PerformGRM(_grm_cargos, NUM_CARGO, count, op, target, "cargos");
       
  3705 							if (_skip_sprites == -1) return;
  3677 							break;
  3706 							break;
  3678 
  3707 
  3679 						default: grfmsg(1, "ParamSet: GRM: Unsupported feature 0x%X", feature); return;
  3708 						default: grfmsg(1, "ParamSet: GRM: Unsupported feature 0x%X", feature); return;
  3680 					}
  3709 					}
  3681 				}
  3710 				}
  4235 	                   |                                        (1 << 0x0F)  // statistics
  4264 	                   |                                        (1 << 0x0F)  // statistics
  4236 	                   |                                        (1 << 0x10)  // newsounds
  4265 	                   |                                        (1 << 0x10)  // newsounds
  4237 	                   |                                        (1 << 0x11)  // autoreplace
  4266 	                   |                                        (1 << 0x11)  // autoreplace
  4238 	                   |                                        (1 << 0x12)  // autoslope
  4267 	                   |                                        (1 << 0x12)  // autoslope
  4239 	                   |                                        (0 << 0x13)  // followvehicle
  4268 	                   |                                        (0 << 0x13)  // followvehicle
  4240 	                   |                                        (0 << 0x14)  // trams
  4269 	                   |                                        (1 << 0x14)  // trams
  4241 	                   |                                        (0 << 0x15)  // enhancetunnels
  4270 	                   |                                        (0 << 0x15)  // enhancetunnels
  4242 	                   |                                        (0 << 0x16)  // shortrvs
  4271 	                   |                                        (1 << 0x16)  // shortrvs
  4243 	                   |                                        (0 << 0x17)  // articulatedrvs
  4272 	                   |                                        (1 << 0x17)  // articulatedrvs
  4244 	                   |                                        (1 << 0x1E); // variablerunningcosts
  4273 	                   |                                        (1 << 0x1E); // variablerunningcosts
  4245 }
  4274 }
  4246 
  4275 
  4247 static void ResetCustomStations()
  4276 static void ResetCustomStations()
  4248 {
  4277 {
  4287 	GRFFile *file;
  4316 	GRFFile *file;
  4288 	uint i;
  4317 	uint i;
  4289 
  4318 
  4290 	for (file = _first_grffile; file != NULL; file = file->next) {
  4319 	for (file = _first_grffile; file != NULL; file = file->next) {
  4291 		if (file->housespec == NULL) continue;
  4320 		if (file->housespec == NULL) continue;
  4292 		for (i = 0; i < HOUSE_MAX; i++) free(file->housespec[i]);
  4321 		for (i = 0; i < HOUSE_MAX; i++) {
       
  4322 			free(file->housespec[i]);
       
  4323 		}
  4293 
  4324 
  4294 		free(file->housespec);
  4325 		free(file->housespec);
  4295 		file->housespec = NULL;
  4326 		file->housespec = NULL;
  4296 	}
  4327 	}
  4297 }
  4328 }
  4298 
  4329 
       
  4330 static void ResetCustomIndustries()
       
  4331 {
       
  4332 	GRFFile *file;
       
  4333 
       
  4334 	for (file = _first_grffile; file != NULL; file = file->next) {
       
  4335 		uint i;
       
  4336 		/* We are verifiying both tiles and industries specs loaded from the grf file
       
  4337 		 * First, let's deal with industryspec */
       
  4338 		if (file->industryspec != NULL) {
       
  4339 
       
  4340 			for (i = 0; i < NUM_INDUSTRYTYPES; i++) {
       
  4341 				IndustrySpec *ind = file->industryspec[i];
       
  4342 
       
  4343 				if (ind != NULL) {
       
  4344 					/* We need to remove the sounds array */
       
  4345 					if (HASBIT(ind->cleanup_flag, CLEAN_RANDOMSOUNDS)) {
       
  4346 						free((void*)ind->random_sounds);
       
  4347 					}
       
  4348 
       
  4349 					/* We need to remove the tiles layouts */
       
  4350 					if (HASBIT(ind->cleanup_flag, CLEAN_TILELSAYOUT) && ind->table != NULL) {
       
  4351 						for (int j = 0; j < ind->num_table; j++) {
       
  4352 							/* remove the individual layouts */
       
  4353 							if (ind->table[j] != NULL) {
       
  4354 								free((IndustryTileTable*)ind->table[j]);
       
  4355 							}
       
  4356 						}
       
  4357 						/* remove the layouts pointers */
       
  4358 						free((IndustryTileTable**)ind->table);
       
  4359 						ind->table = NULL;
       
  4360 					}
       
  4361 
       
  4362 					free(ind);
       
  4363 					ind = NULL;
       
  4364 				}
       
  4365 			}
       
  4366 
       
  4367 			free(file->industryspec);
       
  4368 			file->industryspec = NULL;
       
  4369 		}
       
  4370 
       
  4371 		if (file->indtspec != NULL) {
       
  4372 			for (i = 0; i < NUM_INDUSTRYTILES; i++) {
       
  4373 				if (file->indtspec[i] != NULL) {
       
  4374 					free(file->indtspec[i]);
       
  4375 					file->indtspec[i] = NULL;
       
  4376 				}
       
  4377 			}
       
  4378 
       
  4379 			free(file->indtspec);
       
  4380 			file->indtspec = NULL;
       
  4381 		}
       
  4382 	}
       
  4383 }
       
  4384 
  4299 static void ResetNewGRF()
  4385 static void ResetNewGRF()
  4300 {
  4386 {
  4301 	GRFFile *next;
  4387 	GRFFile *next;
  4302 
  4388 
  4303 	for (GRFFile *f = _first_grffile; f != NULL; f = next) {
  4389 	for (GRFFile *f = _first_grffile; f != NULL; f = next) {
  4304 		next = f->next;
  4390 		next = f->next;
  4305 
  4391 
  4306 		free(f->filename);
  4392 		free(f->filename);
       
  4393 		free(f->cargo_list);
  4307 		free(f);
  4394 		free(f);
  4308 	}
  4395 	}
  4309 
  4396 
  4310 	_first_grffile = NULL;
  4397 	_first_grffile = NULL;
  4311 	_cur_grffile   = NULL;
  4398 	_cur_grffile   = NULL;
  4340 	memset(&cargo_allowed, 0, sizeof(cargo_allowed));
  4427 	memset(&cargo_allowed, 0, sizeof(cargo_allowed));
  4341 	memset(&cargo_disallowed, 0, sizeof(cargo_disallowed));
  4428 	memset(&cargo_disallowed, 0, sizeof(cargo_disallowed));
  4342 
  4429 
  4343 	/* Reset GRM reservations */
  4430 	/* Reset GRM reservations */
  4344 	memset(&_grm_engines, 0, sizeof(_grm_engines));
  4431 	memset(&_grm_engines, 0, sizeof(_grm_engines));
       
  4432 	memset(&_grm_cargos, 0, sizeof(_grm_cargos));
  4345 
  4433 
  4346 	/* Unload sprite group data */
  4434 	/* Unload sprite group data */
  4347 	UnloadWagonOverrides();
  4435 	UnloadWagonOverrides();
  4348 	UnloadRotorOverrideSprites();
  4436 	UnloadRotorOverrideSprites();
  4349 	UnloadCustomEngineSprites();
  4437 	UnloadCustomEngineSprites();
  4358 
  4446 
  4359 	/* Reset the house array */
  4447 	/* Reset the house array */
  4360 	ResetCustomHouses();
  4448 	ResetCustomHouses();
  4361 	ResetHouses();
  4449 	ResetHouses();
  4362 
  4450 
       
  4451 	/* Reset the industries structures*/
       
  4452 	ResetCustomIndustries();
       
  4453 	ResetIndustries();
       
  4454 
  4363 	/* Reset station classes */
  4455 	/* Reset station classes */
  4364 	ResetStationClasses();
  4456 	ResetStationClasses();
  4365 	ResetCustomStations();
  4457 	ResetCustomStations();
  4366 
  4458 
  4367 	/* Reset canal sprite groups */
  4459 	/* Reset canal sprite groups */
  4381 
  4473 
  4382 	/* Reset misc GRF features and train list display variables */
  4474 	/* Reset misc GRF features and train list display variables */
  4383 	_misc_grf_features = 0;
  4475 	_misc_grf_features = 0;
  4384 	_traininfo_vehicle_pitch = 0;
  4476 	_traininfo_vehicle_pitch = 0;
  4385 	_traininfo_vehicle_width = 29;
  4477 	_traininfo_vehicle_width = 29;
  4386 	_have_2cc = false;
  4478 
  4387 	_have_newhouses = false;
  4479 	_loaded_newgrf_features = 0;
       
  4480 
  4388 	_signal_base = 0;
  4481 	_signal_base = 0;
  4389 	_coast_base = 0;
  4482 	_coast_base = 0;
  4390 
  4483 
  4391 	InitializeSoundPool();
  4484 	InitializeSoundPool();
  4392 	InitializeSpriteGroupPool();
  4485 	InitializeSpriteGroupPool();
  4667 		/* 0x03 */ { NULL,     GRFUnsafe, NULL,            NULL,           NULL,              FeatureMapSpriteGroup, },
  4760 		/* 0x03 */ { NULL,     GRFUnsafe, NULL,            NULL,           NULL,              FeatureMapSpriteGroup, },
  4668 		/* 0x04 */ { NULL,     NULL,      NULL,            NULL,           NULL,              FeatureNewName, },
  4761 		/* 0x04 */ { NULL,     NULL,      NULL,            NULL,           NULL,              FeatureNewName, },
  4669 		/* 0x05 */ { SkipAct5, SkipAct5,  SkipAct5,        SkipAct5,       SkipAct5,          GraphicsNew, },
  4762 		/* 0x05 */ { SkipAct5, SkipAct5,  SkipAct5,        SkipAct5,       SkipAct5,          GraphicsNew, },
  4670 		/* 0x06 */ { NULL,     NULL,      NULL,            CfgApply,       CfgApply,          CfgApply, },
  4763 		/* 0x06 */ { NULL,     NULL,      NULL,            CfgApply,       CfgApply,          CfgApply, },
  4671 		/* 0x07 */ { NULL,     NULL,      NULL,            NULL,           SkipIf,            SkipIf, },
  4764 		/* 0x07 */ { NULL,     NULL,      NULL,            NULL,           SkipIf,            SkipIf, },
  4672 		/* 0x08 */ { ScanInfo, NULL,      NULL,            GRFInfo,        NULL,              GRFInfo, },
  4765 		/* 0x08 */ { ScanInfo, NULL,      NULL,            GRFInfo,        GRFInfo,           GRFInfo, },
  4673 		/* 0x09 */ { NULL,     NULL,      NULL,            SkipIf,         SkipIf,            SkipIf, },
  4766 		/* 0x09 */ { NULL,     NULL,      NULL,            SkipIf,         SkipIf,            SkipIf, },
  4674 		/* 0x0A */ { SkipActA, SkipActA,  SkipActA,        SkipActA,       SkipActA,          SpriteReplace, },
  4767 		/* 0x0A */ { SkipActA, SkipActA,  SkipActA,        SkipActA,       SkipActA,          SpriteReplace, },
  4675 		/* 0x0B */ { NULL,     NULL,      NULL,            GRFLoadError,   GRFLoadError,      GRFLoadError, },
  4768 		/* 0x0B */ { NULL,     NULL,      NULL,            GRFLoadError,   GRFLoadError,      GRFLoadError, },
  4676 		/* 0x0C */ { NULL,     NULL,      NULL,            GRFComment,     NULL,              GRFComment, },
  4769 		/* 0x0C */ { NULL,     NULL,      NULL,            GRFComment,     NULL,              GRFComment, },
  4677 		/* 0x0D */ { NULL,     SafeParamSet, NULL,         ParamSet,       ParamSet,          ParamSet, },
  4770 		/* 0x0D */ { NULL,     SafeParamSet, NULL,         ParamSet,       ParamSet,          ParamSet, },
  4736 	 * carried out.  All others are ignored, because they only need to be
  4829 	 * carried out.  All others are ignored, because they only need to be
  4737 	 * processed once at initialization.  */
  4830 	 * processed once at initialization.  */
  4738 	if (stage != GLS_FILESCAN && stage != GLS_SAFETYSCAN && stage != GLS_LABELSCAN) {
  4831 	if (stage != GLS_FILESCAN && stage != GLS_SAFETYSCAN && stage != GLS_LABELSCAN) {
  4739 		_cur_grffile = GetFileByFilename(filename);
  4832 		_cur_grffile = GetFileByFilename(filename);
  4740 		if (_cur_grffile == NULL) error("File '%s' lost in cache.\n", filename);
  4833 		if (_cur_grffile == NULL) error("File '%s' lost in cache.\n", filename);
       
  4834 		if (stage == GLS_RESERVE && config->status != GCS_INITIALISED) return;
  4741 		if (stage == GLS_ACTIVATION && config->status != GCS_INITIALISED) return;
  4835 		if (stage == GLS_ACTIVATION && config->status != GCS_INITIALISED) return;
  4742 	}
  4836 	}
  4743 
  4837 
  4744 	FioOpenFile(file_index, filename);
  4838 	FioOpenFile(file_index, filename);
  4745 	_file_index = file_index; // XXX
  4839 	_file_index = file_index; // XXX
  4805 
  4899 
  4806 void InitDepotWindowBlockSizes();
  4900 void InitDepotWindowBlockSizes();
  4807 
  4901 
  4808 static void AfterLoadGRFs()
  4902 static void AfterLoadGRFs()
  4809 {
  4903 {
       
  4904 	/* Update the bitmasks for the vehicle lists */
       
  4905 	Player *p;
       
  4906 	FOR_ALL_PLAYERS(p) {
       
  4907 		p->avail_railtypes = GetPlayerRailtypes(p->index);
       
  4908 		p->avail_roadtypes = GetPlayerRoadtypes(p->index);
       
  4909 	}
       
  4910 
  4810 	/* Pre-calculate all refit masks after loading GRF files. */
  4911 	/* Pre-calculate all refit masks after loading GRF files. */
  4811 	CalculateRefitMasks();
  4912 	CalculateRefitMasks();
  4812 
  4913 
  4813 	/* Set the block size in the depot windows based on vehicle sprite sizes */
  4914 	/* Set the block size in the depot windows based on vehicle sprite sizes */
  4814 	InitDepotWindowBlockSizes();
  4915 	InitDepotWindowBlockSizes();
  4840 		_cur_spriteid = load_index;
  4941 		_cur_spriteid = load_index;
  4841 		for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
  4942 		for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
  4842 			if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
  4943 			if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
  4843 
  4944 
  4844 			/* @todo usererror() */
  4945 			/* @todo usererror() */
  4845 			if (!FileExists(c->full_path)) error("NewGRF file is missing '%s'", c->filename);
  4946 			if (!FioCheckFileExists(c->full_path)) error("NewGRF file is missing '%s'", c->filename);
  4846 
  4947 
  4847 			if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
  4948 			if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
  4848 			LoadNewGRFFile(c, slot++, stage);
  4949 			LoadNewGRFFile(c, slot++, stage);
  4849 			if (stage == GLS_ACTIVATION) {
  4950 			if (stage == GLS_RESERVE) {
       
  4951 				if (c->status == GCS_ACTIVATED) c->status = GCS_INITIALISED;
       
  4952 			} else if (stage == GLS_ACTIVATION) {
  4850 				ClearTemporaryNewGRFData();
  4953 				ClearTemporaryNewGRFData();
  4851 				BuildCargoTranslationMap();
  4954 				BuildCargoTranslationMap();
  4852 				DEBUG(sprite, 2, "LoadNewGRF: Currently %i sprites are loaded", _cur_spriteid);
  4955 				DEBUG(sprite, 2, "LoadNewGRF: Currently %i sprites are loaded", _cur_spriteid);
  4853 			}
  4956 			}
  4854 		}
  4957 		}