File diff r9353:bc33456736f9 → r9354:358e967da2ca
src/openttd.cpp
Show inline comments
 
@@ -476,26 +476,26 @@ int ttd_main(int argc, char *argv[])
 
#endif
 

	
 
	LoadFromConfig();
 
	CheckConfig();
 
	LoadFromHighScore();
 

	
 
	/* override config? */
 
	if (!StrEmpty(musicdriver)) ttd_strlcpy(_ini_musicdriver, musicdriver, sizeof(_ini_musicdriver));
 
	if (!StrEmpty(sounddriver)) ttd_strlcpy(_ini_sounddriver, sounddriver, sizeof(_ini_sounddriver));
 
	if (!StrEmpty(videodriver)) ttd_strlcpy(_ini_videodriver, videodriver, sizeof(_ini_videodriver));
 
	if (!StrEmpty(blitter))     ttd_strlcpy(_ini_blitter, blitter, sizeof(_ini_blitter));
 
	if (resolution[0] != 0) { _cur_resolution[0] = resolution[0]; _cur_resolution[1] = resolution[1]; }
 
	if (startyear != INVALID_YEAR) _patches_newgame.starting_year = startyear;
 
	if (generation_seed != GENERATE_NEW_SEED) _patches_newgame.generation_seed = generation_seed;
 
	if (startyear != INVALID_YEAR) _settings_newgame.game_creation.starting_year = startyear;
 
	if (generation_seed != GENERATE_NEW_SEED) _settings_newgame.game_creation.generation_seed = generation_seed;
 

	
 
	/* The width and height must be at least 1 pixel, this
 
	 * way all internal drawing routines work correctly. */
 
	if (_cur_resolution[0] == 0) _cur_resolution[0] = 1;
 
	if (_cur_resolution[1] == 0) _cur_resolution[1] = 1;
 

	
 
#if defined(ENABLE_NETWORK)
 
	if (dedicated_host) snprintf(_network_server_bind_ip_host, NETWORK_HOSTNAME_LENGTH, "%s", dedicated_host);
 
	if (dedicated_port) _network_server_port = dedicated_port;
 
	if (_dedicated_forks && !dedicated) _dedicated_forks = false;
 
#endif /* ENABLE_NETWORK */
 

	
 
@@ -643,25 +643,25 @@ int ttd_main(int argc, char *argv[])
 
	}
 

	
 
	/* Reset windowing system, stop drivers, free used memory, ... */
 
	ShutdownGame();
 

	
 
	return 0;
 
}
 

	
 
void HandleExitGameRequest()
 
{
 
	if (_game_mode == GM_MENU) { // do not ask to quit on the main screen
 
		_exit_game = true;
 
	} else if (_patches.autosave_on_exit) {
 
	} else if (_settings.gui.autosave_on_exit) {
 
		DoExitSave();
 
		_exit_game = true;
 
	} else {
 
		AskExitGame();
 
	}
 
}
 

	
 
static void ShowScreenshotResult(bool b)
 
{
 
	if (b) {
 
		SetDParamStr(0, _screenshot_name);
 
		ShowErrorMessage(INVALID_STRING_ID, STR_031B_SCREENSHOT_SUCCESSFULLY, 0, 0);
 
@@ -675,69 +675,69 @@ static void MakeNewGameDone()
 
{
 
	/* In a dedicated server, the server does not play */
 
	if (_network_dedicated) {
 
		SetLocalPlayer(PLAYER_SPECTATOR);
 
		return;
 
	}
 

	
 
	/* Create a single player */
 
	DoStartupNewPlayer(false);
 

	
 
	SetLocalPlayer(PLAYER_FIRST);
 
	_current_player = _local_player;
 
	DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_SET_AUTOREPLACE);
 
	DoCommandP(0, (_settings.gui.autorenew << 15 ) | (_settings.gui.autorenew_months << 16) | 4, _settings.gui.autorenew_money, NULL, CMD_SET_AUTOREPLACE);
 

	
 
	SettingsDisableElrail(_patches.disable_elrails);
 
	SettingsDisableElrail(_settings.vehicle.disable_elrails);
 
	InitializeRailGUI();
 

	
 
#ifdef ENABLE_NETWORK
 
	/* We are the server, we start a new player (not dedicated),
 
	 * so set the default password *if* needed. */
 
	if (_network_server && !StrEmpty(_network_default_company_pass)) {
 
		char *password = _network_default_company_pass;
 
		NetworkChangeCompanyPassword(1, &password);
 
	}
 
#endif /* ENABLE_NETWORK */
 

	
 
	MarkWholeScreenDirty();
 
}
 

	
 
static void MakeNewGame(bool from_heightmap)
 
{
 
	_game_mode = GM_NORMAL;
 

	
 
	ResetGRFConfig(true);
 
	_house_mngr.ResetMapping();
 
	_industile_mngr.ResetMapping();
 
	_industry_mngr.ResetMapping();
 

	
 
	GenerateWorldSetCallback(&MakeNewGameDone);
 
	GenerateWorld(from_heightmap ? GW_HEIGHTMAP : GW_NEWGAME, 1 << _patches.map_x, 1 << _patches.map_y);
 
	GenerateWorld(from_heightmap ? GW_HEIGHTMAP : GW_NEWGAME, 1 << _settings.game_creation.map_x, 1 << _settings.game_creation.map_y);
 
}
 

	
 
static void MakeNewEditorWorldDone()
 
{
 
	SetLocalPlayer(OWNER_NONE);
 

	
 
	MarkWholeScreenDirty();
 
}
 

	
 
static void MakeNewEditorWorld()
 
{
 
	_game_mode = GM_EDITOR;
 

	
 
	ResetGRFConfig(true);
 

	
 
	GenerateWorldSetCallback(&MakeNewEditorWorldDone);
 
	GenerateWorld(GW_EMPTY, 1 << _patches.map_x, 1 << _patches.map_y);
 
	GenerateWorld(GW_EMPTY, 1 << _settings.game_creation.map_x, 1 << _settings.game_creation.map_y);
 
}
 

	
 
void StartupPlayers();
 
void StartupDisasters();
 
extern void StartupEconomy();
 

	
 
/**
 
 * Start Scenario starts a new game based on a scenario.
 
 * Eg 'New Game' --> select a preset scenario
 
 * This starts a scenario based on your current difficulty settings
 
 */
 
static void StartScenario()
 
@@ -769,25 +769,25 @@ static void StartScenario()
 

	
 
	_opt.diff = _opt_newgame.diff;
 
	_opt.diff_level = _opt_newgame.diff_level;
 

	
 
	/* Inititalize data */
 
	StartupEconomy();
 
	StartupPlayers();
 
	StartupEngines();
 
	StartupDisasters();
 

	
 
	SetLocalPlayer(PLAYER_FIRST);
 
	_current_player = _local_player;
 
	DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_SET_AUTOREPLACE);
 
	DoCommandP(0, (_settings.gui.autorenew << 15 ) | (_settings.gui.autorenew_months << 16) | 4, _settings.gui.autorenew_money, NULL, CMD_SET_AUTOREPLACE);
 

	
 
	MarkWholeScreenDirty();
 
}
 

	
 
/** Load the specified savegame but on error do different things.
 
 * If loading fails due to corrupt savegame, bad version, etc. go back to
 
 * a previous correct state. In the menu for example load the intro game again.
 
 * @param filename file to be loaded
 
 * @param mode mode of loading, either SL_LOAD or SL_OLD_LOAD
 
 * @param newgm switch to this mode of loading fails due to some unknown error
 
 * @param subdir default directory to look for filename, set to 0 if not needed
 
 */
 
@@ -828,25 +828,25 @@ void SwitchMode(int new_mode)
 
				NetworkDisconnect();
 
				NetworkUDPCloseAll();
 
			}
 
		}
 

	
 
		/* If we are a server, we restart the server */
 
		if (_is_network_server) {
 
			/* But not if we are going to the menu */
 
			if (new_mode != SM_MENU) {
 
				/* check if we should reload the config */
 
				if (_network_reload_cfg) {
 
					LoadFromConfig();
 
					_patches = _patches_newgame;
 
					_settings = _settings_newgame;
 
					_opt = _opt_newgame;
 
					ResetGRFConfig(false);
 
				}
 
				NetworkServerStart();
 
			} else {
 
				/* This client no longer wants to be a network-server */
 
				_is_network_server = false;
 
			}
 
		}
 
	}
 
#endif /* ENABLE_NETWORK */
 

	
 
@@ -902,32 +902,32 @@ void SwitchMode(int new_mode)
 
		case SM_START_HEIGHTMAP: /* Load a heightmap and start a new game from it */
 
#ifdef ENABLE_NETWORK
 
			if (_network_server) {
 
				snprintf(_network_game_info.map_name, lengthof(_network_game_info.map_name), "%s (Heightmap)", _file_to_saveload.title);
 
			}
 
#endif /* ENABLE_NETWORK */
 
			MakeNewGame(true);
 
			break;
 

	
 
		case SM_LOAD_HEIGHTMAP: /* Load heightmap from scenario editor */
 
			SetLocalPlayer(OWNER_NONE);
 

	
 
			GenerateWorld(GW_HEIGHTMAP, 1 << _patches.map_x, 1 << _patches.map_y);
 
			GenerateWorld(GW_HEIGHTMAP, 1 << _settings.game_creation.map_x, 1 << _settings.game_creation.map_y);
 
			MarkWholeScreenDirty();
 
			break;
 

	
 
		case SM_LOAD_SCENARIO: { /* Load scenario from scenario editor */
 
			if (SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_EDITOR, NO_DIRECTORY)) {
 
				SetLocalPlayer(OWNER_NONE);
 
				_patches_newgame.starting_year = _cur_year;
 
				_settings_newgame.game_creation.starting_year = _cur_year;
 
			} else {
 
				SetDParamStr(0, GetSaveLoadErrorString());
 
				ShowErrorMessage(INVALID_STRING_ID, STR_012D, 0, 0);
 
			}
 
			break;
 
		}
 

	
 
		case SM_MENU: /* Switch to game intro menu */
 
			LoadIntroGame();
 
			break;
 

	
 
		case SM_SAVE: /* Save game */
 
@@ -935,25 +935,25 @@ void SwitchMode(int new_mode)
 
			if (_networking && _pause_game == 1) _pause_game = 2;
 
			if (SaveOrLoad(_file_to_saveload.name, SL_SAVE, NO_DIRECTORY) != SL_OK) {
 
				SetDParamStr(0, GetSaveLoadErrorString());
 
				ShowErrorMessage(INVALID_STRING_ID, STR_012D, 0, 0);
 
			} else {
 
				DeleteWindowById(WC_SAVELOAD, 0);
 
			}
 
			if (_networking && _pause_game == 2) _pause_game = 1;
 
			break;
 

	
 
		case SM_GENRANDLAND: /* Generate random land within scenario editor */
 
			SetLocalPlayer(OWNER_NONE);
 
			GenerateWorld(GW_RANDOM, 1 << _patches.map_x, 1 << _patches.map_y);
 
			GenerateWorld(GW_RANDOM, 1 << _settings.game_creation.map_x, 1 << _settings.game_creation.map_y);
 
			/* XXX: set date */
 
			MarkWholeScreenDirty();
 
			break;
 
	}
 

	
 
	if (_switch_mode_errorstr != INVALID_STRING_ID) {
 
		ShowErrorMessage(INVALID_STRING_ID, _switch_mode_errorstr, 0, 0);
 
	}
 
}
 

	
 

	
 
/**
 
@@ -1051,34 +1051,34 @@ void StateGameLoop()
 

	
 
/** Create an autosave. The default name is "autosave#.sav". However with
 
 * the patch setting 'keep_all_autosave' the name defaults to company-name + date */
 
static void DoAutosave()
 
{
 
	char buf[MAX_PATH];
 

	
 
#if defined(PSP)
 
	/* Autosaving in networking is too time expensive for the PSP */
 
	if (_networking) return;
 
#endif /* PSP */
 

	
 
	if (_patches.keep_all_autosave && _local_player != PLAYER_SPECTATOR) {
 
	if (_settings.gui.keep_all_autosave && _local_player != PLAYER_SPECTATOR) {
 
		SetDParam(0, _local_player);
 
		SetDParam(1, _date);
 
		GetString(buf, STR_4004, lastof(buf));
 
		ttd_strlcat(buf, ".sav", lengthof(buf));
 
	} else {
 
		/* generate a savegame name and number according to _patches.max_num_autosaves */
 
		/* generate a savegame name and number according to _settings.gui.max_num_autosaves */
 
		snprintf(buf, sizeof(buf), "autosave%d.sav", _autosave_ctr);
 

	
 
		if (++_autosave_ctr >= _patches.max_num_autosaves) _autosave_ctr = 0;
 
		if (++_autosave_ctr >= _settings.gui.max_num_autosaves) _autosave_ctr = 0;
 
	}
 

	
 
	DEBUG(sl, 2, "Autosaving to '%s'", buf);
 
	if (SaveOrLoad(buf, SL_SAVE, AUTOSAVE_DIR) != SL_OK) {
 
		ShowErrorMessage(INVALID_STRING_ID, STR_AUTOSAVE_FAILED, 0, 0);
 
	}
 
}
 

	
 
static void ScrollMainViewport(int x, int y)
 
{
 
	if (_game_mode != GM_MENU) {
 
		Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 
@@ -1419,25 +1419,25 @@ bool AfterLoadGame()
 

	
 
	switch (gcf_res) {
 
		case GLC_COMPATIBLE: _switch_mode_errorstr = STR_NEWGRF_COMPATIBLE_LOAD_WARNING; break;
 
		case GLC_NOT_FOUND: _switch_mode_errorstr = STR_NEWGRF_DISABLED_WARNING; _pause_game = -1; break;
 
		default: break;
 
	}
 

	
 
	/* Update current year
 
	 * must be done before loading sprites as some newgrfs check it */
 
	SetDate(_date);
 

	
 
	/* Force dynamic engines off when loading older savegames */
 
	if (CheckSavegameVersion(95)) _patches.dynamic_engines = 0;
 
	if (CheckSavegameVersion(95)) _settings.vehicle.dynamic_engines = 0;
 

	
 
	/* Load the sprites */
 
	GfxLoadSprites();
 
	LoadStringWidthTable();
 

	
 
	/* Copy temporary data to Engine pool */
 
	CopyTempEngineData();
 

	
 
	/* Connect front and rear engines of multiheaded trains and converts
 
	 * subtype to the new format */
 
	if (CheckSavegameVersionOldStyle(17, 1)) ConvertOldMultiheadToNew();
 

	
 
@@ -1626,27 +1626,27 @@ bool AfterLoadGame()
 
			p->engine_renew_months = -6;
 
			p->engine_renew_money  = 100000;
 
		}
 

	
 
		/* When loading a game, _local_player is not yet set to the correct value.
 
		 * However, in a dedicated server we are a spectator, so nothing needs to
 
		 * happen. In case we are not a dedicated server, the local player always
 
		 * becomes player 0, unless we are in the scenario editor where all the
 
		 * players are 'invalid'.
 
		 */
 
		if (!_network_dedicated && IsValidPlayer(PLAYER_FIRST)) {
 
			p = GetPlayer(PLAYER_FIRST);
 
			p->engine_renew        = _patches.autorenew;
 
			p->engine_renew_months = _patches.autorenew_months;
 
			p->engine_renew_money  = _patches.autorenew_money;
 
			p->engine_renew        = _settings.gui.autorenew;
 
			p->engine_renew_months = _settings.gui.autorenew_months;
 
			p->engine_renew_money  = _settings.gui.autorenew_money;
 
		}
 
	}
 

	
 
	if (CheckSavegameVersion(48)) {
 
		for (TileIndex t = 0; t < map_size; t++) {
 
			switch (GetTileType(t)) {
 
				case MP_RAILWAY:
 
					if (IsPlainRailTile(t)) {
 
						/* Swap ground type and signal type for plain rail tiles, so the
 
						 * ground type uses the same bits as for depots and waypoints. */
 
						uint tmp = GB(_m[t].m4, 0, 4);
 
						SB(_m[t].m4, 0, 4, GB(_m[t].m2, 0, 4));
 
@@ -2013,27 +2013,27 @@ bool AfterLoadGame()
 

	
 
		FOR_ALL_ORDERS(order) {
 
			order->SetRefit(CT_NO_REFIT);
 
		}
 

	
 
		FOR_ALL_VEHICLES(v) {
 
			v->current_order.SetRefit(CT_NO_REFIT);
 
		}
 
	}
 

	
 
	/* from version 38 we have optional elrails, since we cannot know the
 
	 * preference of a user, let elrails enabled; it can be disabled manually */
 
	if (CheckSavegameVersion(38)) _patches.disable_elrails = false;
 
	if (CheckSavegameVersion(38)) _settings.vehicle.disable_elrails = false;
 
	/* do the same as when elrails were enabled/disabled manually just now */
 
	SettingsDisableElrail(_patches.disable_elrails);
 
	SettingsDisableElrail(_settings.vehicle.disable_elrails);
 
	InitializeRailGUI();
 

	
 
	/* From version 53, the map array was changed for house tiles to allow
 
	 * space for newhouses grf features. A new byte, m7, was also added. */
 
	if (CheckSavegameVersion(53)) {
 
		for (TileIndex t = 0; t < map_size; t++) {
 
			if (IsTileType(t, MP_HOUSE)) {
 
				if (GB(_m[t].m3, 6, 2) != TOWN_HOUSE_COMPLETED) {
 
					/* Move the construction stage from m3[7..6] to m5[5..4].
 
					 * The construction counter does not have to move. */
 
					SB(_m[t].m5, 3, 2, GB(_m[t].m3, 6, 2));
 
					SB(_m[t].m3, 6, 2, 0);
 
@@ -2180,25 +2180,25 @@ bool AfterLoadGame()
 
				_m[t].m2 = CalcClosestTownFromTile(t, (uint)-1)->index;
 
			}
 
		}
 
	}
 

	
 
	/* A patch option containing the proportion of towns that grow twice as
 
	 * fast was added in version 54. From version 56 this is now saved in the
 
	 * town as cities can be built specifically in the scenario editor. */
 
	if (CheckSavegameVersion(56)) {
 
		Town *t;
 

	
 
		FOR_ALL_TOWNS(t) {
 
			if (_patches.larger_towns != 0 && (t->index % _patches.larger_towns) == 0) {
 
			if (_settings.economy.larger_towns != 0 && (t->index % _settings.economy.larger_towns) == 0) {
 
				t->larger_town = true;
 
			}
 
		}
 
	}
 

	
 
	if (CheckSavegameVersion(57)) {
 
		Vehicle *v;
 
		/* Added a FIFO queue of vehicles loading at stations */
 
		FOR_ALL_VEHICLES(v) {
 
			if ((v->type != VEH_TRAIN || IsFrontEngine(v)) &&  // for all locs
 
					!(v->vehstatus & (VS_STOPPED | VS_CRASHED)) && // not stopped or crashed
 
					v->current_order.IsType(OT_LOADING)) {         // loading
 
@@ -2455,40 +2455,40 @@ bool AfterLoadGame()
 
				if (IsLevelCrossing(t)) {
 
					Owner o = GetTileOwner(t);
 
					if (!GetPlayer(o)->is_active) {
 
						/* remove leftover rail piece from crossing (from very old savegames) */
 
						_current_player = o;
 
						DoCommand(t, 0, GetCrossingRailTrack(t), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL);
 
					}
 
				}
 
			}
 
		}
 

	
 
		/* Convert old PF settings to new */
 
		if (_patches.yapf.rail_use_yapf || CheckSavegameVersion(28)) {
 
			_patches.pathfinder_for_trains = VPF_YAPF;
 
		if (_settings.pf.yapf.rail_use_yapf || CheckSavegameVersion(28)) {
 
			_settings.pf.pathfinder_for_trains = VPF_YAPF;
 
		} else {
 
			_patches.pathfinder_for_trains = (_patches.new_pathfinding_all ? VPF_NPF : VPF_NTP);
 
			_settings.pf.pathfinder_for_trains = (_settings.pf.new_pathfinding_all ? VPF_NPF : VPF_NTP);
 
		}
 

	
 
		if (_patches.yapf.road_use_yapf || CheckSavegameVersion(28)) {
 
			_patches.pathfinder_for_roadvehs = VPF_YAPF;
 
		if (_settings.pf.yapf.road_use_yapf || CheckSavegameVersion(28)) {
 
			_settings.pf.pathfinder_for_roadvehs = VPF_YAPF;
 
		} else {
 
			_patches.pathfinder_for_roadvehs = (_patches.new_pathfinding_all ? VPF_NPF : VPF_OPF);
 
			_settings.pf.pathfinder_for_roadvehs = (_settings.pf.new_pathfinding_all ? VPF_NPF : VPF_OPF);
 
		}
 

	
 
		if (_patches.yapf.ship_use_yapf) {
 
			_patches.pathfinder_for_ships = VPF_YAPF;
 
		if (_settings.pf.yapf.ship_use_yapf) {
 
			_settings.pf.pathfinder_for_ships = VPF_YAPF;
 
		} else {
 
			_patches.pathfinder_for_ships = (_patches.new_pathfinding_all ? VPF_NPF : VPF_OPF);
 
			_settings.pf.pathfinder_for_ships = (_settings.pf.new_pathfinding_all ? VPF_NPF : VPF_OPF);
 
		}
 
	}
 

	
 
	if (CheckSavegameVersion(88)) {
 
		/* Profits are now with 8 bit fract */
 
		Vehicle *v;
 
		FOR_ALL_VEHICLES(v) {
 
			v->profit_this_year <<= 8;
 
			v->profit_last_year <<= 8;
 
			v->running_ticks = 0;
 
		}
 
	}