openttd.c
changeset 4300 c7e43c47a2b9
parent 4293 4b7006c1b5eb
child 4323 ae32c5fab67b
equal deleted inserted replaced
4299:91f5d2bedcff 4300:c7e43c47a2b9
    46 #include "waypoint.h"
    46 #include "waypoint.h"
    47 #include "ai/ai.h"
    47 #include "ai/ai.h"
    48 #include "train.h"
    48 #include "train.h"
    49 #include "yapf/yapf.h"
    49 #include "yapf/yapf.h"
    50 #include "settings.h"
    50 #include "settings.h"
       
    51 #include "genworld.h"
    51 #include "date.h"
    52 #include "date.h"
    52 
    53 
    53 #include <stdarg.h>
    54 #include <stdarg.h>
    54 
    55 
    55 void GenerateWorld(int mode, uint size_x, uint size_y);
       
    56 void CallLandscapeTick(void);
    56 void CallLandscapeTick(void);
    57 void IncreaseDate(void);
    57 void IncreaseDate(void);
    58 void DoPaletteAnimations(void);
    58 void DoPaletteAnimations(void);
    59 void MusicLoop(void);
    59 void MusicLoop(void);
    60 void ResetMusic(void);
    60 void ResetMusic(void);
   293 		sprintf(filename, "%sopntitle.dat",  _path.second_data_dir);
   293 		sprintf(filename, "%sopntitle.dat",  _path.second_data_dir);
   294 	}
   294 	}
   295 #endif
   295 #endif
   296 	if (SaveOrLoad(filename, SL_LOAD) != SL_OK) {
   296 	if (SaveOrLoad(filename, SL_LOAD) != SL_OK) {
   297 		GenerateWorld(GW_EMPTY, 64, 64); // if failed loading, make empty world.
   297 		GenerateWorld(GW_EMPTY, 64, 64); // if failed loading, make empty world.
       
   298 		WaitTillGeneratedWorld();
   298 	}
   299 	}
   299 
   300 
   300 	_pause = 0;
   301 	_pause = 0;
   301 	_local_player = 0;
   302 	_local_player = 0;
   302 	MarkWholeScreenDirty();
   303 	MarkWholeScreenDirty();
   315 	int i;
   316 	int i;
   316 	const char *optformat;
   317 	const char *optformat;
   317 	char musicdriver[16], sounddriver[16], videodriver[16];
   318 	char musicdriver[16], sounddriver[16], videodriver[16];
   318 	int resolution[2] = {0,0};
   319 	int resolution[2] = {0,0};
   319 	Year startyear = INVALID_YEAR;
   320 	Year startyear = INVALID_YEAR;
   320 
   321 	uint generation_seed = GENERATE_NEW_SEED;
   321 	bool dedicated = false;
   322 	bool dedicated = false;
   322 	bool network   = false;
   323 	bool network   = false;
   323 	char *network_conn = NULL;
   324 	char *network_conn = NULL;
   324 
   325 
   325 	musicdriver[0] = sounddriver[0] = videodriver[0] = 0;
   326 	musicdriver[0] = sounddriver[0] = videodriver[0] = 0;
   374 				_switch_mode = SM_LOAD;
   375 				_switch_mode = SM_LOAD;
   375 			} else {
   376 			} else {
   376 				_switch_mode = SM_NEWGAME;
   377 				_switch_mode = SM_NEWGAME;
   377 			}
   378 			}
   378 			break;
   379 			break;
   379 		case 'G': _random_seeds[0][0] = atoi(mgo.opt); break;
   380 		case 'G': generation_seed = atoi(mgo.opt); break;
   380 		case 'c': _config_file = strdup(mgo.opt); break;
   381 		case 'c': _config_file = strdup(mgo.opt); break;
   381 		case -2:
   382 		case -2:
   382 		case 'h':
   383 		case 'h':
   383 			showhelp();
   384 			showhelp();
   384 			return 0;
   385 			return 0;
   407 	if (musicdriver[0]) ttd_strlcpy(_ini_musicdriver, musicdriver, sizeof(_ini_musicdriver));
   408 	if (musicdriver[0]) ttd_strlcpy(_ini_musicdriver, musicdriver, sizeof(_ini_musicdriver));
   408 	if (sounddriver[0]) ttd_strlcpy(_ini_sounddriver, sounddriver, sizeof(_ini_sounddriver));
   409 	if (sounddriver[0]) ttd_strlcpy(_ini_sounddriver, sounddriver, sizeof(_ini_sounddriver));
   409 	if (videodriver[0]) ttd_strlcpy(_ini_videodriver, videodriver, sizeof(_ini_videodriver));
   410 	if (videodriver[0]) ttd_strlcpy(_ini_videodriver, videodriver, sizeof(_ini_videodriver));
   410 	if (resolution[0]) { _cur_resolution[0] = resolution[0]; _cur_resolution[1] = resolution[1]; }
   411 	if (resolution[0]) { _cur_resolution[0] = resolution[0]; _cur_resolution[1] = resolution[1]; }
   411 	if (startyear != INVALID_YEAR) _patches_newgame.starting_year = startyear;
   412 	if (startyear != INVALID_YEAR) _patches_newgame.starting_year = startyear;
       
   413 	if (generation_seed != GENERATE_NEW_SEED) _patches_newgame.generation_seed = generation_seed;
   412 
   414 
   413 	if (_dedicated_forks && !dedicated) _dedicated_forks = false;
   415 	if (_dedicated_forks && !dedicated) _dedicated_forks = false;
   414 
   416 
   415 	// enumerate language files
   417 	// enumerate language files
   416 	InitializeLanguagePacks();
   418 	InitializeLanguagePacks();
   455 	_opt_ptr = &_opt_newgame;
   457 	_opt_ptr = &_opt_newgame;
   456 
   458 
   457 	/* XXX - ugly hack, if diff_level is 9, it means we got no setting from the config file */
   459 	/* XXX - ugly hack, if diff_level is 9, it means we got no setting from the config file */
   458 	if (_opt_newgame.diff_level == 9) SetDifficultyLevel(0, &_opt_newgame);
   460 	if (_opt_newgame.diff_level == 9) SetDifficultyLevel(0, &_opt_newgame);
   459 
   461 
       
   462 	/* Make sure _patches is filled with _patches_newgame if we switch to a game directly */
       
   463 	if (_switch_mode != SM_NONE) {
       
   464 		memcpy(&_opt, &_opt_newgame, sizeof(_opt));
       
   465 		GfxLoadSprites();
       
   466 		UpdatePatches();
       
   467 	}
       
   468 
   460 	// initialize the ingame console
   469 	// initialize the ingame console
   461 	IConsoleInit();
   470 	IConsoleInit();
   462 	_cursor.in_window = true;
   471 	_cursor.in_window = true;
   463 	InitializeGUI();
   472 	InitializeGUI();
   464 	IConsoleCmdExec("exec scripts/autoexec.scr 0");
   473 	IConsoleCmdExec("exec scripts/autoexec.scr 0");
   465 
   474 
   466 	GenerateWorld(GW_EMPTY, 64, 64); // Make the viewport initialization happy
   475 	GenerateWorld(GW_EMPTY, 64, 64); // Make the viewport initialization happy
       
   476 	WaitTillGeneratedWorld();
   467 
   477 
   468 #ifdef ENABLE_NETWORK
   478 #ifdef ENABLE_NETWORK
   469 	if (network && _network_available) {
   479 	if (network && _network_available) {
   470 		if (network_conn != NULL) {
   480 		if (network_conn != NULL) {
   471 			const char *port = NULL;
   481 			const char *port = NULL;
   565 		ShowErrorMessage(INVALID_STRING_ID, STR_031C_SCREENSHOT_FAILED, 0, 0);
   575 		ShowErrorMessage(INVALID_STRING_ID, STR_031C_SCREENSHOT_FAILED, 0, 0);
   566 	}
   576 	}
   567 
   577 
   568 }
   578 }
   569 
   579 
   570 static void MakeNewGame(void)
   580 static void MakeNewGameDone(void)
   571 {
   581 {
   572 	_game_mode = GM_NORMAL;
   582 	/* In a dedicated server, the server does not play */
   573 
       
   574 	// Copy in game options
       
   575 	_opt_ptr = &_opt;
       
   576 	memcpy(_opt_ptr, &_opt_newgame, sizeof(*_opt_ptr));
       
   577 
       
   578 	GfxLoadSprites();
       
   579 
       
   580 	// Reinitialize windows
       
   581 	ResetWindowSystem();
       
   582 	LoadStringWidthTable();
       
   583 
       
   584 	SetupColorsAndInitialWindow();
       
   585 
       
   586 	// Randomize world
       
   587 	GenerateWorld(GW_NEWGAME, 1<<_patches.map_x, 1<<_patches.map_y);
       
   588 
       
   589 	// In a dedicated server, the server does not play
       
   590 	if (_network_dedicated) {
   583 	if (_network_dedicated) {
   591 		_local_player = OWNER_SPECTATOR;
   584 		_local_player = OWNER_SPECTATOR;
   592 	} else {
   585 		return;
   593 		// Create a single player
   586 	}
   594 		DoStartupNewPlayer(false);
   587 
   595 
   588 	/* Create a single player */
   596 		_local_player = 0;
   589 	DoStartupNewPlayer(false);
   597 		_current_player = _local_player;
   590 
   598 		DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_REPLACE_VEHICLE);
   591 	_local_player = 0;
   599 	}
   592 	_current_player = _local_player;
       
   593 	DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_REPLACE_VEHICLE);
   600 
   594 
   601 	MarkWholeScreenDirty();
   595 	MarkWholeScreenDirty();
   602 }
   596 }
   603 
   597 
       
   598 static void MakeNewGame(bool from_heightmap)
       
   599 {
       
   600 	_game_mode = GM_NORMAL;
       
   601 
       
   602 	GenerateWorldSetCallback(&MakeNewGameDone);
       
   603 	GenerateWorld(from_heightmap ? GW_HEIGHTMAP : GW_NEWGAME, 1 << _patches.map_x, 1 << _patches.map_y);
       
   604 }
       
   605 
       
   606 static void MakeNewEditorWorldDone(void)
       
   607 {
       
   608 	_local_player = OWNER_NONE;
       
   609 
       
   610 	MarkWholeScreenDirty();
       
   611 }
       
   612 
   604 static void MakeNewEditorWorld(void)
   613 static void MakeNewEditorWorld(void)
   605 {
   614 {
   606 	_game_mode = GM_EDITOR;
   615 	_game_mode = GM_EDITOR;
   607 
   616 
   608 	// Copy in game options
   617 	GenerateWorldSetCallback(&MakeNewEditorWorldDone);
   609 	_opt_ptr = &_opt;
       
   610 	memcpy(_opt_ptr, &_opt_newgame, sizeof(GameOptions));
       
   611 
       
   612 	GfxLoadSprites();
       
   613 
       
   614 	// Re-init the windowing system
       
   615 	ResetWindowSystem();
       
   616 
       
   617 	// Create toolbars
       
   618 	SetupColorsAndInitialWindow();
       
   619 
       
   620 	// Startup the game system
       
   621 	GenerateWorld(GW_EMPTY, 1 << _patches.map_x, 1 << _patches.map_y);
   618 	GenerateWorld(GW_EMPTY, 1 << _patches.map_x, 1 << _patches.map_y);
   622 
       
   623 	_local_player = OWNER_NONE;
       
   624 	MarkWholeScreenDirty();
       
   625 }
   619 }
   626 
   620 
   627 void StartupPlayers(void);
   621 void StartupPlayers(void);
   628 void StartupDisasters(void);
   622 void StartupDisasters(void);
   629 extern void StartupEconomy(void);
   623 extern void StartupEconomy(void);
   686 
   680 
   687 		case SL_REINIT:
   681 		case SL_REINIT:
   688 			switch (ogm) {
   682 			switch (ogm) {
   689 				case GM_MENU:   LoadIntroGame();      break;
   683 				case GM_MENU:   LoadIntroGame();      break;
   690 				case GM_EDITOR: MakeNewEditorWorld(); break;
   684 				case GM_EDITOR: MakeNewEditorWorld(); break;
   691 				default:        MakeNewGame();        break;
   685 				default:        MakeNewGame(false);   break;
   692 			}
   686 			}
   693 			return false;
   687 			return false;
   694 
   688 
   695 		default:
   689 		default:
   696 			_game_mode = ogm;
   690 			_game_mode = ogm;
   736 #ifdef ENABLE_NETWORK
   730 #ifdef ENABLE_NETWORK
   737 		if (_network_server) {
   731 		if (_network_server) {
   738 			snprintf(_network_game_info.map_name, lengthof(_network_game_info.map_name), "Random Map");
   732 			snprintf(_network_game_info.map_name, lengthof(_network_game_info.map_name), "Random Map");
   739 		}
   733 		}
   740 #endif /* ENABLE_NETWORK */
   734 #endif /* ENABLE_NETWORK */
   741 		MakeNewGame();
   735 		MakeNewGame(false);
   742 		break;
   736 		break;
   743 
   737 
   744 	case SM_START_SCENARIO: /* New Game --> Choose one of the preset scenarios */
   738 	case SM_START_SCENARIO: /* New Game --> Choose one of the preset scenarios */
   745 #ifdef ENABLE_NETWORK
   739 #ifdef ENABLE_NETWORK
   746 		if (_network_server) {
   740 		if (_network_server) {
   766 #endif /* ENABLE_NETWORK */
   760 #endif /* ENABLE_NETWORK */
   767 		}
   761 		}
   768 		break;
   762 		break;
   769 	}
   763 	}
   770 
   764 
       
   765 	case SM_START_HEIGHTMAP: /* Load a heightmap and start a new game from it */
       
   766 #ifdef ENABLE_NETWORK
       
   767 		if (_network_server) {
       
   768 			snprintf(_network_game_info.map_name, lengthof(_network_game_info.map_name), "%s (Heightmap)", _file_to_saveload.title);
       
   769 		}
       
   770 #endif /* ENABLE_NETWORK */
       
   771 		MakeNewGame(true);
       
   772 		break;
       
   773 
       
   774 	case SM_LOAD_HEIGHTMAP: /* Load heightmap from scenario editor */
       
   775 		_local_player = OWNER_NONE;
       
   776 
       
   777 		GenerateWorld(GW_HEIGHTMAP, 1 << _patches.map_x, 1 << _patches.map_y);
       
   778 		MarkWholeScreenDirty();
       
   779 		break;
       
   780 
   771 	case SM_LOAD_SCENARIO: { /* Load scenario from scenario editor */
   781 	case SM_LOAD_SCENARIO: { /* Load scenario from scenario editor */
   772 		if (SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_EDITOR)) {
   782 		if (SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_EDITOR)) {
   773 			Player *p;
   783 			Player *p;
   774 
   784 
   775 			_opt_ptr = &_opt;
   785 			_opt_ptr = &_opt;
   782 					ChangeOwnershipOfPlayerItems(p->index, OWNER_SPECTATOR);
   792 					ChangeOwnershipOfPlayerItems(p->index, OWNER_SPECTATOR);
   783 					p->is_active = false;
   793 					p->is_active = false;
   784 				}
   794 				}
   785 			}
   795 			}
   786 			_generating_world = false;
   796 			_generating_world = false;
       
   797 			_patches_newgame.starting_year = BASE_YEAR + _cur_year;
   787 			// delete all stations owned by a player
   798 			// delete all stations owned by a player
   788 			DeleteAllPlayerStations();
   799 			DeleteAllPlayerStations();
   789 		} else {
   800 		} else {
   790 			ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
   801 			ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
   791 		}
   802 		}
   803 			DeleteWindowById(WC_SAVELOAD, 0);
   814 			DeleteWindowById(WC_SAVELOAD, 0);
   804 		}
   815 		}
   805 		break;
   816 		break;
   806 
   817 
   807 	case SM_GENRANDLAND: /* Generate random land within scenario editor */
   818 	case SM_GENRANDLAND: /* Generate random land within scenario editor */
       
   819 		_local_player = OWNER_NONE;
   808 		GenerateWorld(GW_RANDOM, 1 << _patches.map_x, 1 << _patches.map_y);
   820 		GenerateWorld(GW_RANDOM, 1 << _patches.map_x, 1 << _patches.map_y);
   809 		// XXX: set date
   821 		// XXX: set date
   810 		_local_player = OWNER_NONE;
       
   811 		MarkWholeScreenDirty();
   822 		MarkWholeScreenDirty();
   812 		break;
   823 		break;
   813 	}
   824 	}
   814 
   825 
   815 	if (_switch_mode_errorstr != INVALID_STRING_ID) {
   826 	if (_switch_mode_errorstr != INVALID_STRING_ID) {
   824 // That check is enforced in DoCommand.
   835 // That check is enforced in DoCommand.
   825 void StateGameLoop(void)
   836 void StateGameLoop(void)
   826 {
   837 {
   827 	// dont execute the state loop during pause
   838 	// dont execute the state loop during pause
   828 	if (_pause) return;
   839 	if (_pause) return;
       
   840 	if (IsGeneratingWorld()) return;
   829 
   841 
   830 	if (_game_mode == GM_EDITOR) {
   842 	if (_game_mode == GM_EDITOR) {
   831 		RunTileLoop();
   843 		RunTileLoop();
   832 		CallVehicleTicks();
   844 		CallVehicleTicks();
   833 		CallLandscapeTick();
   845 		CallLandscapeTick();
   882 		ShowErrorMessage(INVALID_STRING_ID, STR_AUTOSAVE_FAILED, 0, 0);
   894 		ShowErrorMessage(INVALID_STRING_ID, STR_AUTOSAVE_FAILED, 0, 0);
   883 }
   895 }
   884 
   896 
   885 static void ScrollMainViewport(int x, int y)
   897 static void ScrollMainViewport(int x, int y)
   886 {
   898 {
   887 	if (_game_mode != GM_MENU) {
   899 	if (_game_mode != GM_MENU && !IsGeneratingWorld()) {
   888 		Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
   900 		Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
   889 		assert(w);
   901 		assert(w);
   890 
   902 
   891 		WP(w,vp_d).scrollpos_x += x << w->viewport->zoom;
   903 		WP(w,vp_d).scrollpos_x += x << w->viewport->zoom;
   892 		WP(w,vp_d).scrollpos_y += y << w->viewport->zoom;
   904 		WP(w,vp_d).scrollpos_y += y << w->viewport->zoom;
   961 #ifdef ENABLE_NETWORK
   973 #ifdef ENABLE_NETWORK
   962 	// Check for UDP stuff
   974 	// Check for UDP stuff
   963 	if (_network_available)
   975 	if (_network_available)
   964 		NetworkUDPGameLoop();
   976 		NetworkUDPGameLoop();
   965 
   977 
   966 	if (_networking) {
   978 	if (_networking && !IsGeneratingWorld()) {
   967 		// Multiplayer
   979 		// Multiplayer
   968 		NetworkGameLoop();
   980 		NetworkGameLoop();
   969 	} else {
   981 	} else {
   970 		if (_network_reconnect > 0 && --_network_reconnect == 0) {
   982 		if (_network_reconnect > 0 && --_network_reconnect == 0) {
   971 			// This means that we want to reconnect to the last host
   983 			// This means that we want to reconnect to the last host