src/openttd.cpp
branchnoai
changeset 9631 8a2d1c2ceb88
parent 9629 66dde6412125
child 9686 d3c195c226f9
equal deleted inserted replaced
9630:550db5cefcc2 9631:8a2d1c2ceb88
    40 #include "news.h"
    40 #include "news.h"
    41 #include "engine.h"
    41 #include "engine.h"
    42 #include "sound.h"
    42 #include "sound.h"
    43 #include "economy.h"
    43 #include "economy.h"
    44 #include "fileio.h"
    44 #include "fileio.h"
    45 #include "hal.h"
       
    46 #include "airport.h"
    45 #include "airport.h"
    47 #include "aircraft.h"
    46 #include "aircraft.h"
    48 #include "console.h"
    47 #include "console.h"
    49 #include "screenshot.h"
    48 #include "screenshot.h"
    50 #include "network/network.h"
    49 #include "network/network.h"
    64 #include "newgrf_house.h"
    63 #include "newgrf_house.h"
    65 #include "newgrf_commons.h"
    64 #include "newgrf_commons.h"
    66 #include "player_face.h"
    65 #include "player_face.h"
    67 #include "group.h"
    66 #include "group.h"
    68 #include "blitter/factory.hpp"
    67 #include "blitter/factory.hpp"
       
    68 #include "sound/sound_driver.hpp"
       
    69 #include "music/music_driver.hpp"
       
    70 #include "video/video_driver.hpp"
    69 
    71 
    70 #include "bridge_map.h"
    72 #include "bridge_map.h"
    71 #include "clear_map.h"
    73 #include "clear_map.h"
    72 #include "rail_map.h"
    74 #include "rail_map.h"
    73 #include "road_map.h"
    75 #include "road_map.h"
   102 	va_start(va, s);
   104 	va_start(va, s);
   103 	vsnprintf(buf, lengthof(buf), s, va);
   105 	vsnprintf(buf, lengthof(buf), s, va);
   104 	va_end(va);
   106 	va_end(va);
   105 
   107 
   106 	ShowOSErrorBox(buf);
   108 	ShowOSErrorBox(buf);
   107 	if (_video_driver != NULL) _video_driver->stop();
   109 	if (_video_driver != NULL) _video_driver->Stop();
   108 
   110 
   109 	assert(0);
   111 	assert(0);
   110 	exit(1);
   112 	exit(1);
   111 }
   113 }
   112 
   114 
   186 		"  -c config_file      = Use 'config_file' instead of 'openttd.cfg'\n"
   188 		"  -c config_file      = Use 'config_file' instead of 'openttd.cfg'\n"
   187 		"  -x                  = Do not automatically save to config file on exit\n\n",
   189 		"  -x                  = Do not automatically save to config file on exit\n\n",
   188 		lastof(buf)
   190 		lastof(buf)
   189 	);
   191 	);
   190 
   192 
   191 	p = GetDriverList(p, lastof(buf));
   193 	/* List the drivers */
       
   194 	p = VideoDriverFactoryBase::GetDriversInfo(p, lastof(buf));
   192 
   195 
   193 	/* List the blitters */
   196 	/* List the blitters */
   194 	p = BlitterFactoryBase::GetBlittersInfo(p, lastof(buf));
   197 	p = BlitterFactoryBase::GetBlittersInfo(p, lastof(buf));
   195 
   198 
   196 	/* We need to initialize the AI, so it finds the AIs */
   199 	/* We need to initialize the AI, so it finds the AIs */
   291 static void InitializeDynamicVariables()
   294 static void InitializeDynamicVariables()
   292 {
   295 {
   293 	/* Dynamic stuff needs to be initialized somewhere... */
   296 	/* Dynamic stuff needs to be initialized somewhere... */
   294 	_town_sort     = NULL;
   297 	_town_sort     = NULL;
   295 	_industry_sort = NULL;
   298 	_industry_sort = NULL;
       
   299 	_industry_mngr.ResetMapping();
       
   300 	_industile_mngr.ResetMapping();
   296 }
   301 }
   297 
   302 
   298 
   303 
   299 static void UnInitializeGame()
   304 static void UnInitializeGame()
   300 {
   305 {
   345 	_scrolling_viewport = 0;
   350 	_scrolling_viewport = 0;
   346 	_cursor.fix_at = false;
   351 	_cursor.fix_at = false;
   347 	MarkWholeScreenDirty();
   352 	MarkWholeScreenDirty();
   348 
   353 
   349 	/* Play main theme */
   354 	/* Play main theme */
   350 	if (_music_driver->is_song_playing()) ResetMusic();
   355 	if (_music_driver->IsSongPlaying()) ResetMusic();
   351 }
   356 }
   352 
   357 
   353 #if defined(UNIX) && !defined(__MORPHOS__)
   358 #if defined(UNIX) && !defined(__MORPHOS__)
   354 extern void DedicatedFork();
   359 extern void DedicatedFork();
   355 #endif
   360 #endif
   519 	GfxInitPalettes();
   524 	GfxInitPalettes();
   520 
   525 
   521 	DEBUG(misc, 1, "Loading blitter '%s'...", blitter);
   526 	DEBUG(misc, 1, "Loading blitter '%s'...", blitter);
   522 	if (BlitterFactoryBase::SelectBlitter(blitter) == NULL)
   527 	if (BlitterFactoryBase::SelectBlitter(blitter) == NULL)
   523 		error("Failed to select requested blitter '%s'; does it exist?", blitter);
   528 		error("Failed to select requested blitter '%s'; does it exist?", blitter);
       
   529 
   524 	DEBUG(driver, 1, "Loading drivers...");
   530 	DEBUG(driver, 1, "Loading drivers...");
   525 	LoadDriver(SOUND_DRIVER, _ini_sounddriver);
   531 
   526 	LoadDriver(MUSIC_DRIVER, _ini_musicdriver);
   532 	_sound_driver = (SoundDriver*)SoundDriverFactoryBase::SelectDriver(_ini_sounddriver, Driver::DT_SOUND);
   527 	LoadDriver(VIDEO_DRIVER, _ini_videodriver); // load video last, to prevent an empty window while sound and music loads
   533 	if (_sound_driver == NULL) {
       
   534 		StrEmpty(_ini_sounddriver) ?
       
   535 			error("Failed to autoprobe sound driver") :
       
   536 			error("Failed to select requested sound driver '%s'", _ini_sounddriver);
       
   537 	}
       
   538 
       
   539 	_music_driver = (MusicDriver*)MusicDriverFactoryBase::SelectDriver(_ini_musicdriver, Driver::DT_MUSIC);
       
   540 	if (_music_driver == NULL) {
       
   541 		StrEmpty(_ini_musicdriver) ?
       
   542 			error("Failed to autoprobe music driver") :
       
   543 			error("Failed to select requested music driver '%s'", _ini_musicdriver);
       
   544 	}
       
   545 
       
   546 	_video_driver = (VideoDriver*)VideoDriverFactoryBase::SelectDriver(_ini_videodriver, Driver::DT_VIDEO);
       
   547 	if (_video_driver == NULL) {
       
   548 		StrEmpty(_ini_videodriver) ?
       
   549 			error("Failed to autoprobe video driver") :
       
   550 			error("Failed to select requested video driver '%s'", _ini_videodriver);
       
   551 	}
       
   552 
   528 	_savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
   553 	_savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
   529 	/* Initialize the zoom level of the screen to normal */
   554 	/* Initialize the zoom level of the screen to normal */
   530 	_screen.zoom = ZOOM_LVL_NORMAL;
   555 	_screen.zoom = ZOOM_LVL_NORMAL;
   531 
   556 
   532 	/* restore saved music volume */
   557 	/* restore saved music volume */
   533 	_music_driver->set_volume(msf.music_vol);
   558 	_music_driver->SetVolume(msf.music_vol);
   534 
   559 
   535 	NetworkStartUp(); // initialize network-core
   560 	NetworkStartUp(); // initialize network-core
   536 
   561 
   537 #if defined(ENABLE_NETWORK)
   562 #if defined(ENABLE_NETWORK)
   538 	if (debuglog_conn != NULL && _network_available) {
   563 	if (debuglog_conn != NULL && _network_available) {
   599 			NetworkClientConnectGame(network_conn, rport);
   624 			NetworkClientConnectGame(network_conn, rport);
   600 		}
   625 		}
   601 	}
   626 	}
   602 #endif /* ENABLE_NETWORK */
   627 #endif /* ENABLE_NETWORK */
   603 
   628 
   604 	_video_driver->main_loop();
   629 	_video_driver->MainLoop();
   605 
   630 
   606 	WaitTillSaved();
   631 	WaitTillSaved();
   607 	IConsoleFree();
   632 	IConsoleFree();
   608 
   633 
   609 	if (_network_available) NetworkShutDown(); // Shut down the network and close any open connections
   634 	if (_network_available) NetworkShutDown(); // Shut down the network and close any open connections
   610 
   635 
   611 	_video_driver->stop();
   636 	_video_driver->Stop();
   612 	_music_driver->stop();
   637 	_music_driver->Stop();
   613 	_sound_driver->stop();
   638 	_sound_driver->Stop();
   614 
   639 
   615 	/* only save config if we have to */
   640 	/* only save config if we have to */
   616 	if (save_config) {
   641 	if (save_config) {
   617 		SaveToConfig();
   642 		SaveToConfig();
   618 		SaveToHighScore();
   643 		SaveToHighScore();
   711 {
   736 {
   712 	_game_mode = GM_NORMAL;
   737 	_game_mode = GM_NORMAL;
   713 
   738 
   714 	ResetGRFConfig(true);
   739 	ResetGRFConfig(true);
   715 	_house_mngr.ResetMapping();
   740 	_house_mngr.ResetMapping();
       
   741 	_industile_mngr.ResetMapping();
       
   742 	_industry_mngr.ResetMapping();
   716 
   743 
   717 	GenerateWorldSetCallback(&MakeNewGameDone);
   744 	GenerateWorldSetCallback(&MakeNewGameDone);
   718 	GenerateWorld(from_heightmap ? GW_HEIGHTMAP : GW_NEWGAME, 1 << _patches.map_x, 1 << _patches.map_y);
   745 	GenerateWorld(from_heightmap ? GW_HEIGHTMAP : GW_NEWGAME, 1 << _patches.map_x, 1 << _patches.map_y);
   719 }
   746 }
   720 
   747 
   945 	if (_switch_mode_errorstr != INVALID_STRING_ID) {
   972 	if (_switch_mode_errorstr != INVALID_STRING_ID) {
   946 		ShowErrorMessage(INVALID_STRING_ID, _switch_mode_errorstr, 0, 0);
   973 		ShowErrorMessage(INVALID_STRING_ID, _switch_mode_errorstr, 0, 0);
   947 	}
   974 	}
   948 }
   975 }
   949 
   976 
   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);
       
   973 }
       
   974 
       
   975 
   977 
   976 /* State controlling game loop.
   978 /* State controlling game loop.
   977  * The state must not be changed from anywhere
   979  * The state must not be changed from anywhere
   978  * but here.
   980  * but here.
   979  * That check is enforced in DoCommand. */
   981  * That check is enforced in DoCommand. */
  1004 		AI_RunGameLoop();
  1006 		AI_RunGameLoop();
  1005 
  1007 
  1006 		CallWindowTickEvent();
  1008 		CallWindowTickEvent();
  1007 		NewsLoop();
  1009 		NewsLoop();
  1008 		_current_player = p;
  1010 		_current_player = p;
  1009 		CheckCargoPacketLeaks();
       
  1010 	}
  1011 	}
  1011 }
  1012 }
  1012 
  1013 
  1013 /** Create an autosave. The default name is "autosave#.sav". However with
  1014 /** Create an autosave. The default name is "autosave#.sav". However with
  1014  * the patch setting 'keep_all_autosave' the name defaults to company-name + date */
  1015  * the patch setting 'keep_all_autosave' the name defaults to company-name + date */
  2086 				SETBIT(v->u.road.state, RVS_IS_STOPPING);
  2087 				SETBIT(v->u.road.state, RVS_IS_STOPPING);
  2087 			}
  2088 			}
  2088 		}
  2089 		}
  2089 	}
  2090 	}
  2090 
  2091 
       
  2092 	if (CheckSavegameVersion(70)) {
       
  2093 		/* Added variables to support newindustries */
       
  2094 		Industry *i;
       
  2095 		FOR_ALL_INDUSTRIES(i) i->founder = OWNER_NONE;
       
  2096 	}
       
  2097 
  2091 	/* Recalculate */
  2098 	/* Recalculate */
  2092 	Group *g;
  2099 	Group *g;
  2093 	FOR_ALL_GROUPS(g) {
  2100 	FOR_ALL_GROUPS(g) {
  2094 		const Vehicle *v;
  2101 		const Vehicle *v;
  2095 		FOR_ALL_VEHICLES(v) {
  2102 		FOR_ALL_VEHICLES(v) {
  2126 	/* check that house ids are still valid */
  2133 	/* check that house ids are still valid */
  2127 	CheckHouseIDs();
  2134 	CheckHouseIDs();
  2128 	/* redraw the whole screen */
  2135 	/* redraw the whole screen */
  2129 	MarkWholeScreenDirty();
  2136 	MarkWholeScreenDirty();
  2130 }
  2137 }
  2131 
       
  2132 HalMusicDriver *_music_driver;
       
  2133 HalSoundDriver *_sound_driver;
       
  2134 HalVideoDriver *_video_driver;
       
  2135