src/openttd.cpp
branchnoai
changeset 9629 66dde6412125
parent 9628 b5c2449616b5
child 9631 8a2d1c2ceb88
equal deleted inserted replaced
9628:b5c2449616b5 9629:66dde6412125
    84 void ResetMusic();
    84 void ResetMusic();
    85 
    85 
    86 extern void SetDifficultyLevel(int mode, GameOptions *gm_opt);
    86 extern void SetDifficultyLevel(int mode, GameOptions *gm_opt);
    87 extern Player* DoStartupNewPlayer(bool is_ai);
    87 extern Player* DoStartupNewPlayer(bool is_ai);
    88 extern void ShowOSErrorBox(const char *buf);
    88 extern void ShowOSErrorBox(const char *buf);
       
    89 extern void SetDefaultRailGui();
    89 
    90 
    90 const char *_default_blitter = "8bpp-optimized";
    91 const char *_default_blitter = "8bpp-optimized";
    91 
    92 
    92 /* TODO: usrerror() for errors which are not of an internal nature but
    93 /* TODO: usrerror() for errors which are not of an internal nature but
    93  * caused by the user, i.e. missing files or fatal configuration errors.
    94  * caused by the user, i.e. missing files or fatal configuration errors.
   308 	CleanPool(&_Station_pool);
   309 	CleanPool(&_Station_pool);
   309 	CleanPool(&_Vehicle_pool);
   310 	CleanPool(&_Vehicle_pool);
   310 	CleanPool(&_Sign_pool);
   311 	CleanPool(&_Sign_pool);
   311 	CleanPool(&_Order_pool);
   312 	CleanPool(&_Order_pool);
   312 	CleanPool(&_Group_pool);
   313 	CleanPool(&_Group_pool);
       
   314 	CleanPool(&_CargoPacket_pool);
   313 
   315 
   314 	free((void*)_town_sort);
   316 	free((void*)_town_sort);
   315 	free((void*)_industry_sort);
   317 	free((void*)_industry_sort);
   316 
   318 
   317 	free(_config_file);
   319 	free(_config_file);
   698 	SetLocalPlayer(PLAYER_FIRST);
   700 	SetLocalPlayer(PLAYER_FIRST);
   699 	_current_player = _local_player;
   701 	_current_player = _local_player;
   700 	DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_SET_AUTOREPLACE);
   702 	DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_SET_AUTOREPLACE);
   701 
   703 
   702 	SettingsDisableElrail(_patches.disable_elrails);
   704 	SettingsDisableElrail(_patches.disable_elrails);
       
   705 	SetDefaultRailGui();
   703 
   706 
   704 	MarkWholeScreenDirty();
   707 	MarkWholeScreenDirty();
   705 }
   708 }
   706 
   709 
   707 static void MakeNewGame(bool from_heightmap)
   710 static void MakeNewGame(bool from_heightmap)
   746 	_game_mode = GM_NORMAL;
   749 	_game_mode = GM_NORMAL;
   747 
   750 
   748 	/* invalid type */
   751 	/* invalid type */
   749 	if (_file_to_saveload.mode == SL_INVALID) {
   752 	if (_file_to_saveload.mode == SL_INVALID) {
   750 		DEBUG(sl, 0, "Savegame is obsolete or invalid format: '%s'", _file_to_saveload.name);
   753 		DEBUG(sl, 0, "Savegame is obsolete or invalid format: '%s'", _file_to_saveload.name);
   751 		ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
   754 		SetDParamStr(0, GetSaveLoadErrorString());
       
   755 		ShowErrorMessage(INVALID_STRING_ID, STR_012D, 0, 0);
   752 		_game_mode = GM_MENU;
   756 		_game_mode = GM_MENU;
   753 		return;
   757 		return;
   754 	}
   758 	}
   755 
   759 
   756 	/* Reinitialize windows */
   760 	/* Reinitialize windows */
   761 	ResetGRFConfig(true);
   765 	ResetGRFConfig(true);
   762 
   766 
   763 	/* Load game */
   767 	/* Load game */
   764 	if (SaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, SCENARIO_DIR) != SL_OK) {
   768 	if (SaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, SCENARIO_DIR) != SL_OK) {
   765 		LoadIntroGame();
   769 		LoadIntroGame();
   766 		ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
   770 		SetDParamStr(0, GetSaveLoadErrorString());
       
   771 		ShowErrorMessage(INVALID_STRING_ID, STR_012D, 0, 0);
   767 	}
   772 	}
   768 
   773 
   769 	_opt_ptr = &_opt;
   774 	_opt_ptr = &_opt;
   770 	_opt_ptr->diff = _opt_newgame.diff;
   775 	_opt_ptr->diff = _opt_newgame.diff;
   771 	_opt.diff_level = _opt_newgame.diff_level;
   776 	_opt.diff_level = _opt_newgame.diff_level;
   869 		_opt_ptr = &_opt;
   874 		_opt_ptr = &_opt;
   870 		ResetGRFConfig(true);
   875 		ResetGRFConfig(true);
   871 
   876 
   872 		if (!SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, NO_DIRECTORY)) {
   877 		if (!SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, NO_DIRECTORY)) {
   873 			LoadIntroGame();
   878 			LoadIntroGame();
   874 			ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
   879 			SetDParamStr(0, GetSaveLoadErrorString());
       
   880 			ShowErrorMessage(INVALID_STRING_ID, STR_012D, 0, 0);
   875 		} else {
   881 		} else {
   876 			/* Update the local player for a loaded game. It is either always
   882 			/* Update the local player for a loaded game. It is either always
   877 			 * player #1 (eg 0) or in the case of a dedicated server a spectator */
   883 			 * player #1 (eg 0) or in the case of a dedicated server a spectator */
   878 			SetLocalPlayer(_network_dedicated ? PLAYER_SPECTATOR : PLAYER_FIRST);
   884 			SetLocalPlayer(_network_dedicated ? PLAYER_SPECTATOR : PLAYER_FIRST);
   879 			DoCommandP(0, 0, 0, NULL, CMD_PAUSE); // decrease pause counter (was increased from opening load dialog)
   885 			DoCommandP(0, 0, 0, NULL, CMD_PAUSE); // decrease pause counter (was increased from opening load dialog)
   907 			_opt_ptr = &_opt;
   913 			_opt_ptr = &_opt;
   908 
   914 
   909 			SetLocalPlayer(OWNER_NONE);
   915 			SetLocalPlayer(OWNER_NONE);
   910 			_patches_newgame.starting_year = _cur_year;
   916 			_patches_newgame.starting_year = _cur_year;
   911 		} else {
   917 		} else {
   912 			ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
   918 			SetDParamStr(0, GetSaveLoadErrorString());
       
   919 			ShowErrorMessage(INVALID_STRING_ID, STR_012D, 0, 0);
   913 		}
   920 		}
   914 		break;
   921 		break;
   915 	}
   922 	}
   916 
   923 
   917 	case SM_MENU: /* Switch to game intro menu */
   924 	case SM_MENU: /* Switch to game intro menu */
   918 		LoadIntroGame();
   925 		LoadIntroGame();
   919 		break;
   926 		break;
   920 
   927 
   921 	case SM_SAVE: /* Save game */
   928 	case SM_SAVE: /* Save game */
   922 		if (SaveOrLoad(_file_to_saveload.name, SL_SAVE, NO_DIRECTORY) != SL_OK) {
   929 		if (SaveOrLoad(_file_to_saveload.name, SL_SAVE, NO_DIRECTORY) != SL_OK) {
   923 			ShowErrorMessage(INVALID_STRING_ID, STR_4007_GAME_SAVE_FAILED, 0, 0);
   930 			SetDParamStr(0, GetSaveLoadErrorString());
       
   931 			ShowErrorMessage(INVALID_STRING_ID, STR_012D, 0, 0);
   924 		} else {
   932 		} else {
   925 			DeleteWindowById(WC_SAVELOAD, 0);
   933 			DeleteWindowById(WC_SAVELOAD, 0);
   926 		}
   934 		}
   927 		break;
   935 		break;
   928 
   936 
   935 	}
   943 	}
   936 
   944 
   937 	if (_switch_mode_errorstr != INVALID_STRING_ID) {
   945 	if (_switch_mode_errorstr != INVALID_STRING_ID) {
   938 		ShowErrorMessage(INVALID_STRING_ID, _switch_mode_errorstr, 0, 0);
   946 		ShowErrorMessage(INVALID_STRING_ID, _switch_mode_errorstr, 0, 0);
   939 	}
   947 	}
       
   948 }
       
   949 
       
   950 #include "cargopacket.h"
       
   951 void CheckCargoPacketLeaks()
       
   952 {
       
   953 	CargoPacket *cp;
       
   954 	FOR_ALL_CARGOPACKETS(cp) cp->touched = false;
       
   955 
       
   956 	Vehicle *v;
       
   957 	FOR_ALL_VEHICLES(v) {
       
   958 		const CargoList::List *packets = v->cargo.Packets();
       
   959 		for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) (*it)->touched = true;
       
   960 	}
       
   961 
       
   962 	Station *st;
       
   963 	FOR_ALL_STATIONS(st) {
       
   964 		for (CargoID c = 0; c < NUM_CARGO; c++) {
       
   965 			GoodsEntry *ge = &st->goods[c];
       
   966 
       
   967 			const CargoList::List *packets = ge->cargo.Packets();
       
   968 			for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) (*it)->touched = true;
       
   969 		}
       
   970 	}
       
   971 
       
   972 	FOR_ALL_CARGOPACKETS(cp) assert(cp->touched);
   940 }
   973 }
   941 
   974 
   942 
   975 
   943 /* State controlling game loop.
   976 /* State controlling game loop.
   944  * The state must not be changed from anywhere
   977  * The state must not be changed from anywhere
   971 		AI_RunGameLoop();
  1004 		AI_RunGameLoop();
   972 
  1005 
   973 		CallWindowTickEvent();
  1006 		CallWindowTickEvent();
   974 		NewsLoop();
  1007 		NewsLoop();
   975 		_current_player = p;
  1008 		_current_player = p;
       
  1009 		CheckCargoPacketLeaks();
   976 	}
  1010 	}
   977 }
  1011 }
   978 
  1012 
   979 /** Create an autosave. The default name is "autosave#.sav". However with
  1013 /** Create an autosave. The default name is "autosave#.sav". However with
   980  * the patch setting 'keep_all_autosave' the name defaults to company-name + date */
  1014  * the patch setting 'keep_all_autosave' the name defaults to company-name + date */
   987 	if (_networking)
  1021 	if (_networking)
   988 		return;
  1022 		return;
   989 #endif /* PSP */
  1023 #endif /* PSP */
   990 
  1024 
   991 	if (_patches.keep_all_autosave && _local_player != PLAYER_SPECTATOR) {
  1025 	if (_patches.keep_all_autosave && _local_player != PLAYER_SPECTATOR) {
   992 		const Player *p = GetPlayer(_local_player);
  1026 		SetDParam(0, _local_player);
   993 
  1027 		SetDParam(1, _date);
   994 		SetDParam(0, p->name_1);
       
   995 		SetDParam(1, p->name_2);
       
   996 		SetDParam(2, _date);
       
   997 		GetString(buf, STR_4004, lastof(buf));
  1028 		GetString(buf, STR_4004, lastof(buf));
   998 		ttd_strlcpy(buf, ".sav", sizeof(buf));
  1029 		ttd_strlcpy(buf, ".sav", sizeof(buf));
   999 	} else {
  1030 	} else {
  1000 		/* generate a savegame name and number according to _patches.max_num_autosaves */
  1031 		/* generate a savegame name and number according to _patches.max_num_autosaves */
  1001 		snprintf(buf, sizeof(buf), "autosave%d.sav", _autosave_ctr);
  1032 		snprintf(buf, sizeof(buf), "autosave%d.sav", _autosave_ctr);
  1079 	} else {
  1110 	} else {
  1080 		_scroller_click_timeout = 0;
  1111 		_scroller_click_timeout = 0;
  1081 	}
  1112 	}
  1082 
  1113 
  1083 	_caret_timer += 3;
  1114 	_caret_timer += 3;
  1084 	_timer_counter += 8;
  1115 	_palette_animation_counter += 8;
  1085 	CursorTick();
  1116 	CursorTick();
  1086 
  1117 
  1087 #ifdef ENABLE_NETWORK
  1118 #ifdef ENABLE_NETWORK
  1088 	/* Check for UDP stuff */
  1119 	/* Check for UDP stuff */
  1089 	if (_network_available) NetworkUDPGameLoop();
  1120 	if (_network_available) NetworkUDPGameLoop();
  1248 	/* convert road side to my format. */
  1279 	/* convert road side to my format. */
  1249 	if (_opt.road_side) _opt.road_side = 1;
  1280 	if (_opt.road_side) _opt.road_side = 1;
  1250 
  1281 
  1251 	/* Check if all NewGRFs are present, we are very strict in MP mode */
  1282 	/* Check if all NewGRFs are present, we are very strict in MP mode */
  1252 	GRFListCompatibility gcf_res = IsGoodGRFConfigList();
  1283 	GRFListCompatibility gcf_res = IsGoodGRFConfigList();
  1253 	if (_networking && gcf_res != GLC_ALL_GOOD) return false;
  1284 	if (_networking && gcf_res != GLC_ALL_GOOD) {
       
  1285 		SetSaveLoadError(STR_NETWORK_ERR_CLIENT_NEWGRF_MISMATCH);
       
  1286 		return false;
       
  1287 	}
  1254 
  1288 
  1255 	switch (gcf_res) {
  1289 	switch (gcf_res) {
  1256 		case GLC_COMPATIBLE: _switch_mode_errorstr = STR_NEWGRF_COMPATIBLE_LOAD_WARNING; break;
  1290 		case GLC_COMPATIBLE: _switch_mode_errorstr = STR_NEWGRF_COMPATIBLE_LOAD_WARNING; break;
  1257 		case GLC_NOT_FOUND: _switch_mode_errorstr = STR_NEWGRF_DISABLED_WARNING; break;
  1291 		case GLC_NOT_FOUND: _switch_mode_errorstr = STR_NEWGRF_DISABLED_WARNING; break;
  1258 		default: break;
  1292 		default: break;
  1293 	AfterLoadTown();
  1327 	AfterLoadTown();
  1294 	UpdateAllSignVirtCoords();
  1328 	UpdateAllSignVirtCoords();
  1295 
  1329 
  1296 	/* make sure there is a town in the game */
  1330 	/* make sure there is a town in the game */
  1297 	if (_game_mode == GM_NORMAL && !ClosestTownFromTile(0, (uint)-1)) {
  1331 	if (_game_mode == GM_NORMAL && !ClosestTownFromTile(0, (uint)-1)) {
  1298 		_error_message = STR_NO_TOWN_IN_SCENARIO;
  1332 		SetSaveLoadError(STR_NO_TOWN_IN_SCENARIO);
  1299 		return false;
  1333 		return false;
  1300 	}
  1334 	}
  1301 
  1335 
  1302 	/* Initialize windows */
  1336 	/* Initialize windows */
  1303 	ResetWindowSystem();
  1337 	ResetWindowSystem();
  1833 	/* from version 38 we have optional elrails, since we cannot know the
  1867 	/* from version 38 we have optional elrails, since we cannot know the
  1834 	 * preference of a user, let elrails enabled; it can be disabled manually */
  1868 	 * preference of a user, let elrails enabled; it can be disabled manually */
  1835 	if (CheckSavegameVersion(38)) _patches.disable_elrails = false;
  1869 	if (CheckSavegameVersion(38)) _patches.disable_elrails = false;
  1836 	/* do the same as when elrails were enabled/disabled manually just now */
  1870 	/* do the same as when elrails were enabled/disabled manually just now */
  1837 	SettingsDisableElrail(_patches.disable_elrails);
  1871 	SettingsDisableElrail(_patches.disable_elrails);
       
  1872 	SetDefaultRailGui();
  1838 
  1873 
  1839 	/* From version 53, the map array was changed for house tiles to allow
  1874 	/* From version 53, the map array was changed for house tiles to allow
  1840 	 * space for newhouses grf features. A new byte, m7, was also added. */
  1875 	 * space for newhouses grf features. A new byte, m7, was also added. */
  1841 	if (CheckSavegameVersion(53)) {
  1876 	if (CheckSavegameVersion(53)) {
  1842 		for (TileIndex t = 0; t < map_size; t++) {
  1877 		for (TileIndex t = 0; t < map_size; t++) {
  1903 	}
  1938 	}
  1904 
  1939 
  1905 	if (CheckSavegameVersion(44)) {
  1940 	if (CheckSavegameVersion(44)) {
  1906 		Vehicle *v;
  1941 		Vehicle *v;
  1907 		/* If we remove a station while cargo from it is still enroute, payment calculation will assume
  1942 		/* If we remove a station while cargo from it is still enroute, payment calculation will assume
  1908 		 * 0, 0 to be the origin of the cargo, resulting in very high payments usually. v->cargo_source_xy
  1943 		 * 0, 0 to be the source of the cargo, resulting in very high payments usually. v->source_xy
  1909 		 * stores the coordinates, preserving them even if the station is removed. However, if a game is loaded
  1944 		 * stores the coordinates, preserving them even if the station is removed. However, if a game is loaded
  1910 		 * where this situation exists, the cargo-source information is lost. in this case, we set the origin
  1945 		 * where this situation exists, the cargo-source information is lost. in this case, we set the source
  1911 		 * to the current tile of the vehicle to prevent excessive profits
  1946 		 * to the current tile of the vehicle to prevent excessive profits
  1912 		 */
  1947 		 */
  1913 		FOR_ALL_VEHICLES(v) {
  1948 		FOR_ALL_VEHICLES(v) {
  1914 			v->cargo_source_xy = IsValidStationID(v->cargo_source) ? GetStation(v->cargo_source)->xy : v->tile;
  1949 			const CargoList::List *packets = v->cargo.Packets();
       
  1950 			for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
       
  1951 				CargoPacket *cp = *it;
       
  1952 				cp->source_xy = IsValidStationID(cp->source) ? GetStation(cp->source)->xy : v->tile;
       
  1953 				cp->loaded_at_xy = cp->source_xy;
       
  1954 			}
       
  1955 			v->cargo.InvalidateCache();
  1915 		}
  1956 		}
  1916 
  1957 
  1917 		/* Store position of the station where the goods come from, so there
  1958 		/* Store position of the station where the goods come from, so there
  1918 		 * are no very high payments when stations get removed. However, if the
  1959 		 * are no very high payments when stations get removed. However, if the
  1919 		 * station where the goods came from is already removed, the source
  1960 		 * station where the goods came from is already removed, the source
  1922 		Station *st;
  1963 		Station *st;
  1923 		FOR_ALL_STATIONS(st) {
  1964 		FOR_ALL_STATIONS(st) {
  1924 			for (CargoID c = 0; c < NUM_CARGO; c++) {
  1965 			for (CargoID c = 0; c < NUM_CARGO; c++) {
  1925 				GoodsEntry *ge = &st->goods[c];
  1966 				GoodsEntry *ge = &st->goods[c];
  1926 
  1967 
  1927 				/* In old versions, enroute_from used 0xFF as INVALID_STATION */
  1968 				const CargoList::List *packets = ge->cargo.Packets();
  1928 				if (CheckSavegameVersion(7) && ge->enroute_from == 0xFF) {
  1969 				for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
  1929 					ge->enroute_from = INVALID_STATION;
  1970 					CargoPacket *cp = *it;
       
  1971 					cp->source_xy = IsValidStationID(cp->source) ? GetStation(cp->source)->xy : st->xy;
       
  1972 					cp->loaded_at_xy = cp->source_xy;
  1930 				}
  1973 				}
  1931 
       
  1932 				ge->enroute_from_xy = IsValidStationID(ge->enroute_from) ? GetStation(ge->enroute_from)->xy : st->xy;
       
  1933 			}
  1974 			}
  1934 		}
  1975 		}
  1935 	}
  1976 	}
  1936 
  1977 
  1937 	if (CheckSavegameVersion(45)) {
  1978 	if (CheckSavegameVersion(45)) {
  1940 		 * stored to stop people cheating and cashing in several times. This
  1981 		 * stored to stop people cheating and cashing in several times. This
  1941 		 * wasn't enough though as it was cleared when the vehicle started
  1982 		 * wasn't enough though as it was cleared when the vehicle started
  1942 		 * loading again, even if it didn't actually load anything, so now the
  1983 		 * loading again, even if it didn't actually load anything, so now the
  1943 		 * amount of cargo that has been paid for is stored. */
  1984 		 * amount of cargo that has been paid for is stored. */
  1944 		FOR_ALL_VEHICLES(v) {
  1985 		FOR_ALL_VEHICLES(v) {
  1945 			if (HASBIT(v->vehicle_flags, 2)) {
  1986 			const CargoList::List *packets = v->cargo.Packets();
  1946 				v->cargo_paid_for = v->cargo_count;
  1987 			for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
  1947 				CLRBIT(v->vehicle_flags, 2);
  1988 				CargoPacket *cp = *it;
  1948 			} else {
  1989 				cp->paid_for = HASBIT(v->vehicle_flags, 2);
  1949 				v->cargo_paid_for = 0;
  1990 			}
  1950 			}
  1991 			CLRBIT(v->vehicle_flags, 2);
       
  1992 			v->cargo.InvalidateCache();
  1951 		}
  1993 		}
  1952 	}
  1994 	}
  1953 
  1995 
  1954 	/* Buoys do now store the owner of the previous water tile, which can never
  1996 	/* Buoys do now store the owner of the previous water tile, which can never
  1955 	 * be OWNER_NONE. So replace OWNER_NONE with OWNER_WATER. */
  1997 	 * be OWNER_NONE. So replace OWNER_NONE with OWNER_WATER. */
  2030 			if (IsTileType(t, MP_RAILWAY) && HasSignals(t)) {
  2072 			if (IsTileType(t, MP_RAILWAY) && HasSignals(t)) {
  2031 				SetSignalStates(t, GB(_m[t].m2, 4, 4));
  2073 				SetSignalStates(t, GB(_m[t].m2, 4, 4));
  2032 				SetSignalVariant(t, INVALID_TRACK, GetSignalVariant(t, TRACK_X));
  2074 				SetSignalVariant(t, INVALID_TRACK, GetSignalVariant(t, TRACK_X));
  2033 				SetSignalType(t, INVALID_TRACK, GetSignalType(t, TRACK_X));
  2075 				SetSignalType(t, INVALID_TRACK, GetSignalType(t, TRACK_X));
  2034 				CLRBIT(_m[t].m2, 7);
  2076 				CLRBIT(_m[t].m2, 7);
       
  2077 			}
       
  2078 		}
       
  2079 	}
       
  2080 
       
  2081 	if (CheckSavegameVersion(69)) {
       
  2082 		/* In some old savegames a bit was cleared when it should not be cleared */
       
  2083 		Vehicle *v;
       
  2084 		FOR_ALL_VEHICLES(v) {
       
  2085 			if (v->type == VEH_ROAD && (v->u.road.state == 250 || v->u.road.state == 251)) {
       
  2086 				SETBIT(v->u.road.state, RVS_IS_STOPPING);
  2035 			}
  2087 			}
  2036 		}
  2088 		}
  2037 	}
  2089 	}
  2038 
  2090 
  2039 	/* Recalculate */
  2091 	/* Recalculate */