src/newgrf.cpp
branchgamebalance
changeset 9913 e79cd19772dd
parent 9912 1ac8aac92385
equal deleted inserted replaced
9912:1ac8aac92385 9913:e79cd19772dd
    16 #include "station.h"
    16 #include "station.h"
    17 #include "sprite.h"
    17 #include "sprite.h"
    18 #include "newgrf.h"
    18 #include "newgrf.h"
    19 #include "variables.h"
    19 #include "variables.h"
    20 #include "string.h"
    20 #include "string.h"
       
    21 #include "strings.h"
    21 #include "table/strings.h"
    22 #include "table/strings.h"
    22 #include "bridge.h"
    23 #include "bridge.h"
    23 #include "town.h"
    24 #include "town.h"
    24 #include "economy.h"
    25 #include "economy.h"
    25 #include "newgrf_engine.h"
    26 #include "newgrf_engine.h"
    73 
    74 
    74 /* Used by Action 0x06 to preload a pseudo sprite and modify its content */
    75 /* Used by Action 0x06 to preload a pseudo sprite and modify its content */
    75 static byte *_preload_sprite = NULL;
    76 static byte *_preload_sprite = NULL;
    76 
    77 
    77 /* Indicates which are the newgrf features currently loaded ingame */
    78 /* Indicates which are the newgrf features currently loaded ingame */
    78 uint8 _loaded_newgrf_features;
    79 GRFLoadedFeatures _loaded_newgrf_features;
    79 
    80 
    80 enum GrfDataType {
    81 enum GrfDataType {
    81 	GDT_SOUND,
    82 	GDT_SOUND,
    82 };
    83 };
    83 
    84 
   536 			break;
   537 			break;
   537 
   538 
   538 		case 0x27: // Miscellaneous flags
   539 		case 0x27: // Miscellaneous flags
   539 			FOR_EACH_OBJECT {
   540 			FOR_EACH_OBJECT {
   540 				ei[i].misc_flags = grf_load_byte(&buf);
   541 				ei[i].misc_flags = grf_load_byte(&buf);
   541 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) SETBIT(_loaded_newgrf_features, GRFLOADED_2CC);
   542 				_loaded_newgrf_features.has_2CC |= HASBIT(ei[i].misc_flags, EF_USES_2CC);
   542 			}
   543 			}
   543 			break;
   544 			break;
   544 
   545 
   545 		case 0x28: // Cargo classes allowed
   546 		case 0x28: // Cargo classes allowed
   546 			FOR_EACH_OBJECT cargo_allowed[engine + i] = grf_load_word(&buf);
   547 			FOR_EACH_OBJECT cargo_allowed[engine + i] = grf_load_word(&buf);
   658 			break;
   659 			break;
   659 
   660 
   660 		case 0x1C: // Miscellaneous flags
   661 		case 0x1C: // Miscellaneous flags
   661 			FOR_EACH_OBJECT {
   662 			FOR_EACH_OBJECT {
   662 				ei[i].misc_flags = grf_load_byte(&buf);
   663 				ei[i].misc_flags = grf_load_byte(&buf);
   663 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) SETBIT(_loaded_newgrf_features, GRFLOADED_2CC);
   664 				_loaded_newgrf_features.has_2CC |= HASBIT(ei[i].misc_flags, EF_USES_2CC);
   664 			}
   665 			}
   665 			break;
   666 			break;
   666 
   667 
   667 		case 0x1D: // Cargo classes allowed
   668 		case 0x1D: // Cargo classes allowed
   668 			FOR_EACH_OBJECT cargo_allowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   669 			FOR_EACH_OBJECT cargo_allowed[ROAD_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   769 			break;
   770 			break;
   770 
   771 
   771 		case 0x17: // Miscellaneous flags
   772 		case 0x17: // Miscellaneous flags
   772 			FOR_EACH_OBJECT {
   773 			FOR_EACH_OBJECT {
   773 				ei[i].misc_flags = grf_load_byte(&buf);
   774 				ei[i].misc_flags = grf_load_byte(&buf);
   774 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) SETBIT(_loaded_newgrf_features, GRFLOADED_2CC);
   775 				_loaded_newgrf_features.has_2CC |= HASBIT(ei[i].misc_flags, EF_USES_2CC);
   775 			}
   776 			}
   776 			break;
   777 			break;
   777 
   778 
   778 		case 0x18: // Cargo classes allowed
   779 		case 0x18: // Cargo classes allowed
   779 			FOR_EACH_OBJECT cargo_allowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   780 			FOR_EACH_OBJECT cargo_allowed[SHIP_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   885 			break;
   886 			break;
   886 
   887 
   887 		case 0x17: // Miscellaneous flags
   888 		case 0x17: // Miscellaneous flags
   888 			FOR_EACH_OBJECT {
   889 			FOR_EACH_OBJECT {
   889 				ei[i].misc_flags = grf_load_byte(&buf);
   890 				ei[i].misc_flags = grf_load_byte(&buf);
   890 				if (HASBIT(ei[i].misc_flags, EF_USES_2CC)) SETBIT(_loaded_newgrf_features, GRFLOADED_2CC);
   891 				_loaded_newgrf_features.has_2CC |= HASBIT(ei[i].misc_flags, EF_USES_2CC);
   891 			}
   892 			}
   892 			break;
   893 			break;
   893 
   894 
   894 		case 0x18: // Cargo classes allowed
   895 		case 0x18: // Cargo classes allowed
   895 			FOR_EACH_OBJECT cargo_allowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
   896 			FOR_EACH_OBJECT cargo_allowed[AIRCRAFT_ENGINES_INDEX + engine + i] = grf_load_word(&buf);
  1294 				/* New houses do not (currently) expect to have a default start
  1295 				/* New houses do not (currently) expect to have a default start
  1295 				 * date before 1930, as this breaks the build date stuff. See
  1296 				 * date before 1930, as this breaks the build date stuff. See
  1296 				 * FinaliseHouseArray() for more details. */
  1297 				 * FinaliseHouseArray() for more details. */
  1297 				if (housespec[i]->min_date < 1930) housespec[i]->min_date = 1930;
  1298 				if (housespec[i]->min_date < 1930) housespec[i]->min_date = 1930;
  1298 			}
  1299 			}
  1299 			SETBIT(_loaded_newgrf_features, GRFLOADED_NEWHOUSES);
  1300 			_loaded_newgrf_features.has_newhouses = true;
  1300 			break;
  1301 			break;
  1301 
  1302 
  1302 		case 0x09: // Building flags
  1303 		case 0x09: // Building flags
  1303 			FOR_EACH_OBJECT {
  1304 			FOR_EACH_OBJECT {
  1304 				byte state = grf_load_byte(&buf);
  1305 				byte state = grf_load_byte(&buf);
  2042 	grfmsg(7, "New sprite set at %d of type %d, consisting of %d sets with %d views each (total %d)",
  2043 	grfmsg(7, "New sprite set at %d of type %d, consisting of %d sets with %d views each (total %d)",
  2043 		_cur_spriteid, feature, num_sets, num_ents, num_sets * num_ents
  2044 		_cur_spriteid, feature, num_sets, num_ents, num_sets * num_ents
  2044 	);
  2045 	);
  2045 
  2046 
  2046 	for (uint16 i = 0; i < num_sets * num_ents; i++) {
  2047 	for (uint16 i = 0; i < num_sets * num_ents; i++) {
  2047 		LoadNextSprite(_cur_spriteid++, _file_index);
  2048 		LoadNextSprite(_cur_spriteid++, _file_index, _nfo_line);
  2048 		_nfo_line++;
  2049 		_nfo_line++;
  2049 	}
  2050 	}
  2050 }
  2051 }
  2051 
  2052 
  2052 /* Action 0x01 (SKIP) */
  2053 /* Action 0x01 (SKIP) */
  2098 		return NULL;
  2099 		return NULL;
  2099 	}
  2100 	}
  2100 
  2101 
  2101 	if (feature != _cur_grffile->spriteset_feature) {
  2102 	if (feature != _cur_grffile->spriteset_feature) {
  2102 		grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set feature 0x%02X does not match action feature 0x%02X, skipping",
  2103 		grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set feature 0x%02X does not match action feature 0x%02X, skipping",
       
  2104 				setid, type,
  2103 				_cur_grffile->spriteset_feature, feature);
  2105 				_cur_grffile->spriteset_feature, feature);
  2104 		return NULL;
  2106 		return NULL;
  2105 	}
  2107 	}
  2106 
  2108 
  2107 	return NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteid * num_sprites, num_sprites);
  2109 	return NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteid * num_sprites, num_sprites);
  2956 	} else {
  2958 	} else {
  2957 		grfmsg(2, "GraphicsNew: Replacing %u sprites of type 0x%02X at SpriteID 0x%04X", num, type, replace);
  2959 		grfmsg(2, "GraphicsNew: Replacing %u sprites of type 0x%02X at SpriteID 0x%04X", num, type, replace);
  2958 	}
  2960 	}
  2959 
  2961 
  2960 	for (; num > 0; num--) {
  2962 	for (; num > 0; num--) {
  2961 		LoadNextSprite(replace == 0 ? _cur_spriteid++ : replace++, _file_index);
  2963 		LoadNextSprite(replace == 0 ? _cur_spriteid++ : replace++, _file_index, _nfo_line);
  2962 		_nfo_line++;
  2964 		_nfo_line++;
  2963 	}
  2965 	}
  2964 }
  2966 }
  2965 
  2967 
  2966 /* Action 0x05 (SKIP) */
  2968 /* Action 0x05 (SKIP) */
  3275 		 * we use -1 to indicate that all further
  3277 		 * we use -1 to indicate that all further
  3276 		 * sprites should be skipped. */
  3278 		 * sprites should be skipped. */
  3277 		_skip_sprites = -1;
  3279 		_skip_sprites = -1;
  3278 
  3280 
  3279 		/* If an action 8 hasn't been encountered yet, disable the grf. */
  3281 		/* If an action 8 hasn't been encountered yet, disable the grf. */
  3280 		if (_cur_grfconfig->status != GCS_ACTIVATED) _cur_grfconfig->status = GCS_DISABLED;
  3282 		if (_cur_grfconfig->status != GCS_ACTIVATED && _cur_grfconfig->status != GCS_INITIALISED) {
       
  3283 			_cur_grfconfig->status = GCS_DISABLED;
       
  3284 		}
  3281 	}
  3285 	}
  3282 }
  3286 }
  3283 
  3287 
  3284 
  3288 
  3285 /* Action 0x08 (GLS_FILESCAN) */
  3289 /* Action 0x08 (GLS_FILESCAN) */
  3354 		grfmsg(2, "SpriteReplace: [Set %d] Changing %d sprites, beginning with %d",
  3358 		grfmsg(2, "SpriteReplace: [Set %d] Changing %d sprites, beginning with %d",
  3355 			i, num_sprites, first_sprite
  3359 			i, num_sprites, first_sprite
  3356 		);
  3360 		);
  3357 
  3361 
  3358 		for (uint j = 0; j < num_sprites; j++) {
  3362 		for (uint j = 0; j < num_sprites; j++) {
  3359 			LoadNextSprite(first_sprite + j, _file_index); // XXX
  3363 			LoadNextSprite(first_sprite + j, _file_index, _nfo_line); // XXX
  3360 			_nfo_line++;
  3364 			_nfo_line++;
  3361 		}
  3365 		}
  3362 	}
  3366 	}
  3363 }
  3367 }
  3364 
  3368 
  3410 		STR_NEWGRF_ERROR_MSG_WARNING,
  3414 		STR_NEWGRF_ERROR_MSG_WARNING,
  3411 		STR_NEWGRF_ERROR_MSG_ERROR,
  3415 		STR_NEWGRF_ERROR_MSG_ERROR,
  3412 		STR_NEWGRF_ERROR_MSG_FATAL
  3416 		STR_NEWGRF_ERROR_MSG_FATAL
  3413 	};
  3417 	};
  3414 
  3418 
  3415 	/* AddGRFString expects the string to be referred to by an id in the newgrf
       
  3416 	 * file. Errors messages are never referred to however, so invent ids that
       
  3417 	 * are unlikely to be reached in a newgrf file so they don't overwrite
       
  3418 	 * anything else. */
       
  3419 	enum {
       
  3420 		MESSAGE_STRING_ID = MAX_UVALUE(StringID) - 1,
       
  3421 		MESSAGE_DATA_ID   = MAX_UVALUE(StringID)
       
  3422 	};
       
  3423 
       
  3424 	if (!check_length(len, 6, "GRFLoadError")) return;
  3419 	if (!check_length(len, 6, "GRFLoadError")) return;
  3425 
  3420 
  3426 	/* For now we can only show one message per newgrf file. */
  3421 	/* For now we can only show one message per newgrf file. */
  3427 	if (_cur_grfconfig->error != NULL) return;
  3422 	if (_cur_grfconfig->error != NULL) return;
  3428 
  3423 
  3429 	buf++; // Skip the action byte.
  3424 	buf++; // Skip the action byte.
  3430 	byte severity   = grf_load_byte(&buf);
  3425 	byte severity   = grf_load_byte(&buf);
  3431 	byte lang       = grf_load_byte(&buf);
  3426 	byte lang       = grf_load_byte(&buf);
  3432 	byte message_id = grf_load_byte(&buf);
  3427 	byte message_id = grf_load_byte(&buf);
  3433 	len -= 4;
  3428 	len -= 4;
       
  3429 
       
  3430 	/* Skip the error if it isn't valid for the current language. */
       
  3431 	if (!CheckGrfLangID(lang, _cur_grffile->grf_version)) return;
  3434 
  3432 
  3435 	/* Skip the error until the activation stage unless bit 7 of the severity
  3433 	/* Skip the error until the activation stage unless bit 7 of the severity
  3436 	 * is set. */
  3434 	 * is set. */
  3437 	if (!HASBIT(severity, 7) && _cur_stage == GLS_INIT) {
  3435 	if (!HASBIT(severity, 7) && _cur_stage == GLS_INIT) {
  3438 		grfmsg(7, "GRFLoadError: Skipping non-fatal GRFLoadError in stage %d", _cur_stage);
  3436 		grfmsg(7, "GRFLoadError: Skipping non-fatal GRFLoadError in stage %d", _cur_stage);
  3459 	if (len <= 1) {
  3457 	if (len <= 1) {
  3460 		grfmsg(7, "GRFLoadError: No message data supplied.");
  3458 		grfmsg(7, "GRFLoadError: No message data supplied.");
  3461 		return;
  3459 		return;
  3462 	}
  3460 	}
  3463 
  3461 
  3464 	bool new_scheme = _cur_grffile->grf_version >= 7;
       
  3465 	GRFError *error = CallocT<GRFError>(1);
  3462 	GRFError *error = CallocT<GRFError>(1);
  3466 
  3463 
  3467 	error->severity = sevstr[severity];
  3464 	error->severity = sevstr[severity];
  3468 
  3465 
  3469 	if (message_id == 0xFF) {
  3466 	if (message_id == 0xFF) {
  3470 		/* This is a custom error message. */
  3467 		/* This is a custom error message. */
  3471 		const char *message = grf_load_string(&buf, len);
  3468 		const char *message = grf_load_string(&buf, len);
  3472 		len -= (strlen(message) + 1);
  3469 		len -= (strlen(message) + 1);
  3473 
  3470 
  3474 		error->message = AddGRFString(_cur_grffile->grfid, MESSAGE_STRING_ID, lang, new_scheme, message, STR_UNDEFINED);
  3471 		error->custom_message = TranslateTTDPatchCodes(message);
  3475 	} else {
  3472 	} else {
  3476 		error->message = msgstr[message_id];
  3473 		error->message = msgstr[message_id];
  3477 	}
  3474 	}
  3478 
  3475 
  3479 	if (len > 0) {
  3476 	if (len > 0) {
  3480 		const char *data = grf_load_string(&buf, len);
  3477 		const char *data = grf_load_string(&buf, len);
  3481 		len -= (strlen(data) + 1);
  3478 		len -= (strlen(data) + 1);
  3482 
  3479 
  3483 		error->data = AddGRFString(_cur_grffile->grfid, MESSAGE_DATA_ID, lang, new_scheme, data, STR_UNDEFINED);
  3480 		error->data = TranslateTTDPatchCodes(data);
  3484 	}
  3481 	}
  3485 
  3482 
  3486 	/* Only two parameter numbers can be used in the string. */
  3483 	/* Only two parameter numbers can be used in the string. */
  3487 	uint i = 0;
  3484 	uint i = 0;
  3488 	for (; i < 2 && len > 0; i++) {
  3485 	for (; i < 2 && len > 0; i++) {
  4084 
  4081 
  4085 		grfmsg(7, "LoadFontGlyph: Loading %u glyph(s) at 0x%04X for size %u", num_char, base_char, size);
  4082 		grfmsg(7, "LoadFontGlyph: Loading %u glyph(s) at 0x%04X for size %u", num_char, base_char, size);
  4086 
  4083 
  4087 		for (uint c = 0; c < num_char; c++) {
  4084 		for (uint c = 0; c < num_char; c++) {
  4088 			SetUnicodeGlyph(size, base_char + c, _cur_spriteid);
  4085 			SetUnicodeGlyph(size, base_char + c, _cur_spriteid);
  4089 			LoadNextSprite(_cur_spriteid++, _file_index);
  4086 			LoadNextSprite(_cur_spriteid++, _file_index, _nfo_line);
  4090 			_nfo_line++;
  4087 			_nfo_line++;
  4091 		}
  4088 		}
  4092 	}
  4089 	}
  4093 }
  4090 }
  4094 
  4091 
  4114 
  4111 
  4115 	if (c->status == GCS_INITIALISED) {
  4112 	if (c->status == GCS_INITIALISED) {
  4116 		/* If the file is not active but will be activated later, give an error
  4113 		/* If the file is not active but will be activated later, give an error
  4117 		 * and disable this file. */
  4114 		 * and disable this file. */
  4118 		GRFError *error = CallocT<GRFError>(1);
  4115 		GRFError *error = CallocT<GRFError>(1);
       
  4116 
       
  4117 		char tmp[256];
       
  4118 		GetString(tmp, STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE, lastof(tmp));
       
  4119 		error->data = strdup(tmp);
       
  4120 
  4119 		error->message  = STR_NEWGRF_ERROR_LOAD_AFTER;
  4121 		error->message  = STR_NEWGRF_ERROR_LOAD_AFTER;
  4120 		error->data     = STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE;
       
  4121 		error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
  4122 		error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
  4122 
  4123 
  4123 		if (_cur_grfconfig->error != NULL) free(_cur_grfconfig->error);
  4124 		if (_cur_grfconfig->error != NULL) free(_cur_grfconfig->error);
  4124 		_cur_grfconfig->error = error;
  4125 		_cur_grfconfig->error = error;
  4125 
  4126 
  4226 	                   |                                        (1 << 0x03)  // semaphores
  4227 	                   |                                        (1 << 0x03)  // semaphores
  4227 	                   |                                        (0 << 0x0B)  // enhancedgui
  4228 	                   |                                        (0 << 0x0B)  // enhancedgui
  4228 	                   |                                        (0 << 0x0C)  // newagerating
  4229 	                   |                                        (0 << 0x0C)  // newagerating
  4229 	                   |       ((_patches.build_on_slopes ? 1 : 0) << 0x0D)  // buildonslopes
  4230 	                   |       ((_patches.build_on_slopes ? 1 : 0) << 0x0D)  // buildonslopes
  4230 	                   |         ((_patches.full_load_any ? 1 : 0) << 0x0E)  // fullloadany
  4231 	                   |         ((_patches.full_load_any ? 1 : 0) << 0x0E)  // fullloadany
  4231 	                   |                                        (0 << 0x0F)  // planespeed
  4232 	                   |                                        (1 << 0x0F)  // planespeed - TODO depends on patch when implemented
  4232 	                   |                                        (0 << 0x10)  // moreindustriesperclimate - obsolete
  4233 	                   |                                        (0 << 0x10)  // moreindustriesperclimate - obsolete
  4233 	                   |                                        (0 << 0x11)  // moretoylandfeatures
  4234 	                   |                                        (0 << 0x11)  // moretoylandfeatures
  4234 	                   |                                        (1 << 0x12)  // newstations
  4235 	                   |                                        (1 << 0x12)  // newstations
  4235 	                   |                                        (0 << 0x13)  // tracktypecostdiff
  4236 	                   |                                        (0 << 0x13)  // tracktypecostdiff
  4236 	                   |                                        (1 << 0x14)  // manualconvert
  4237 	                   |                                        (1 << 0x14)  // manualconvert
  4396 
  4397 
  4397 	_first_grffile = NULL;
  4398 	_first_grffile = NULL;
  4398 	_cur_grffile   = NULL;
  4399 	_cur_grffile   = NULL;
  4399 }
  4400 }
  4400 
  4401 
       
  4402 static void ResetNewGRFErrors()
       
  4403 {
       
  4404 	for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
       
  4405 		if (!HASBIT(c->flags, GCF_COPY) && c->error != NULL) {
       
  4406 			free(c->error->custom_message);
       
  4407 			free(c->error->data);
       
  4408 			free(c->error);
       
  4409 			c->error = NULL;
       
  4410 		}
       
  4411 	}
       
  4412 }
       
  4413 
  4401 /**
  4414 /**
  4402  * Reset all NewGRF loaded data
  4415  * Reset all NewGRF loaded data
  4403  * TODO
  4416  * TODO
  4404  */
  4417  */
  4405 static void ResetNewGRFData()
  4418 static void ResetNewGRFData()
  4463 	ClearSnowLine();
  4476 	ClearSnowLine();
  4464 
  4477 
  4465 	/* Reset NewGRF files */
  4478 	/* Reset NewGRF files */
  4466 	ResetNewGRF();
  4479 	ResetNewGRF();
  4467 
  4480 
       
  4481 	/* Reset NewGRF errors. */
       
  4482 	ResetNewGRFErrors();
       
  4483 
  4468 	/* Add engine type to engine data. This is needed for the refit precalculation. */
  4484 	/* Add engine type to engine data. This is needed for the refit precalculation. */
  4469 	AddTypeToEngines();
  4485 	AddTypeToEngines();
  4470 
  4486 
  4471 	/* Set up the default cargo types */
  4487 	/* Set up the default cargo types */
  4472 	SetupCargoForClimate(_opt.landscape);
  4488 	SetupCargoForClimate(_opt.landscape);
  4474 	/* Reset misc GRF features and train list display variables */
  4490 	/* Reset misc GRF features and train list display variables */
  4475 	_misc_grf_features = 0;
  4491 	_misc_grf_features = 0;
  4476 	_traininfo_vehicle_pitch = 0;
  4492 	_traininfo_vehicle_pitch = 0;
  4477 	_traininfo_vehicle_width = 29;
  4493 	_traininfo_vehicle_width = 29;
  4478 
  4494 
  4479 	_loaded_newgrf_features = 0;
  4495 	_loaded_newgrf_features.has_2CC           = false;
       
  4496 	_loaded_newgrf_features.has_newhouses     = false;
       
  4497 	_loaded_newgrf_features.has_newindustries = false,
  4480 
  4498 
  4481 	_signal_base = 0;
  4499 	_signal_base = 0;
  4482 	_coast_base = 0;
  4500 	_coast_base = 0;
  4483 
  4501 
  4484 	InitializeSoundPool();
  4502 	InitializeSoundPool();