00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013
00014 #include "blitter/factory.hpp"
00015 #include "sound/sound_driver.hpp"
00016 #include "music/music_driver.hpp"
00017 #include "video/video_driver.hpp"
00018
00019 #include "fontcache.h"
00020 #include "error.h"
00021 #include "gui.h"
00022
00023 #include "base_media_base.h"
00024 #include "saveload/saveload.h"
00025 #include "company_func.h"
00026 #include "command_func.h"
00027 #include "news_func.h"
00028 #include "fios.h"
00029 #include "aircraft.h"
00030 #include "roadveh.h"
00031 #include "train.h"
00032 #include "ship.h"
00033 #include "console_func.h"
00034 #include "screenshot.h"
00035 #include "network/network.h"
00036 #include "network/network_func.h"
00037 #include "ai/ai.hpp"
00038 #include "ai/ai_config.hpp"
00039 #include "settings_func.h"
00040 #include "genworld.h"
00041 #include "progress.h"
00042 #include "strings_func.h"
00043 #include "date_func.h"
00044 #include "vehicle_func.h"
00045 #include "gamelog.h"
00046 #include "animated_tile_func.h"
00047 #include "roadstop_base.h"
00048 #include "elrail_func.h"
00049 #include "rev.h"
00050 #include "highscore.h"
00051 #include "station_base.h"
00052 #include "crashlog.h"
00053 #include "engine_func.h"
00054 #include "core/random_func.hpp"
00055 #include "rail_gui.h"
00056 #include "core/backup_type.hpp"
00057 #include "hotkeys.h"
00058 #include "newgrf.h"
00059 #include "misc/getoptdata.h"
00060 #include "game/game.hpp"
00061 #include "game/game_config.hpp"
00062 #include "town.h"
00063
00064
00065
00066 #include "linkgraph/linkgraph.h"
00067
00068 #include <stdarg.h>
00069
00070
00071 void CallLandscapeTick();
00072 void IncreaseDate();
00073 void DoPaletteAnimations();
00074 void MusicLoop();
00075 void ResetMusic();
00076 void CallWindowTickEvent();
00077 bool HandleBootstrap();
00078
00079 extern void SetDifficultyLevel(int mode, DifficultySettings *gm_opt);
00080 extern Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY);
00081 extern void ShowOSErrorBox(const char *buf, bool system);
00082 extern char *_config_file;
00083
00089 void CDECL usererror(const char *s, ...)
00090 {
00091 va_list va;
00092 char buf[512];
00093
00094 va_start(va, s);
00095 vsnprintf(buf, lengthof(buf), s, va);
00096 va_end(va);
00097
00098 ShowOSErrorBox(buf, false);
00099 if (_video_driver != NULL) _video_driver->Stop();
00100
00101 exit(1);
00102 }
00103
00109 void CDECL error(const char *s, ...)
00110 {
00111 va_list va;
00112 char buf[512];
00113
00114 va_start(va, s);
00115 vsnprintf(buf, lengthof(buf), s, va);
00116 va_end(va);
00117
00118 ShowOSErrorBox(buf, true);
00119
00120
00121 CrashLog::SetErrorMessage(buf);
00122 abort();
00123 }
00124
00129 void CDECL ShowInfoF(const char *str, ...)
00130 {
00131 va_list va;
00132 char buf[1024];
00133 va_start(va, str);
00134 vsnprintf(buf, lengthof(buf), str, va);
00135 va_end(va);
00136 ShowInfo(buf);
00137 }
00138
00142 static void ShowHelp()
00143 {
00144 char buf[8192];
00145 char *p = buf;
00146
00147 p += seprintf(p, lastof(buf), "OpenTTD %s\n", _openttd_revision);
00148 p = strecpy(p,
00149 "\n"
00150 "\n"
00151 "Command line options:\n"
00152 " -v drv = Set video driver (see below)\n"
00153 " -s drv = Set sound driver (see below) (param bufsize,hz)\n"
00154 " -m drv = Set music driver (see below)\n"
00155 " -b drv = Set the blitter to use (see below)\n"
00156 " -r res = Set resolution (for instance 800x600)\n"
00157 " -h = Display this help text\n"
00158 " -t year = Set starting year\n"
00159 " -d [[fac=]lvl[,...]]= Debug mode\n"
00160 " -e = Start Editor\n"
00161 " -g [savegame] = Start new/save game immediately\n"
00162 " -G seed = Set random seed\n"
00163 #if defined(ENABLE_NETWORK)
00164 " -n [ip:port#company]= Start networkgame\n"
00165 " -p password = Password to join server\n"
00166 " -P password = Password to join company\n"
00167 " -D [ip][:port] = Start dedicated server\n"
00168 " -l ip[:port] = Redirect DEBUG()\n"
00169 #if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
00170 " -f = Fork into the background (dedicated only)\n"
00171 #endif
00172 #endif
00173 " -I graphics_set = Force the graphics set (see below)\n"
00174 " -S sounds_set = Force the sounds set (see below)\n"
00175 " -M music_set = Force the music set (see below)\n"
00176 " -c config_file = Use 'config_file' instead of 'openttd.cfg'\n"
00177 " -x = Do not automatically save to config file on exit\n"
00178 " -q savegame = Write some information about the savegame and exit\n"
00179 "\n",
00180 lastof(buf)
00181 );
00182
00183
00184 p = BaseGraphics::GetSetsList(p, lastof(buf));
00185
00186
00187 p = BaseSounds::GetSetsList(p, lastof(buf));
00188
00189
00190 p = BaseMusic::GetSetsList(p, lastof(buf));
00191
00192
00193 p = VideoDriverFactoryBase::GetDriversInfo(p, lastof(buf));
00194
00195
00196 p = BlitterFactoryBase::GetBlittersInfo(p, lastof(buf));
00197
00198
00199 p = DumpDebugFacilityNames(p, lastof(buf));
00200
00201
00202 AI::Initialize();
00203 p = AI::GetConsoleList(p, lastof(buf), true);
00204 AI::Uninitialize(true);
00205
00206
00207 Game::Initialize();
00208 p = Game::GetConsoleList(p, lastof(buf), true);
00209 Game::Uninitialize(true);
00210
00211
00212
00213 #if !defined(WIN32) && !defined(WIN64)
00214 printf("%s\n", buf);
00215 #else
00216 ShowInfo(buf);
00217 #endif
00218 }
00219
00220 static void WriteSavegameInfo(const char *name)
00221 {
00222 extern uint16 _sl_version;
00223 uint32 last_ottd_rev = 0;
00224 byte ever_modified = 0;
00225 bool removed_newgrfs = false;
00226
00227 GamelogInfo(_load_check_data.gamelog_action, _load_check_data.gamelog_actions, &last_ottd_rev, &ever_modified, &removed_newgrfs);
00228
00229 char buf[8192];
00230 char *p = buf;
00231 p += seprintf(p, lastof(buf), "Name: %s\n", name);
00232 p += seprintf(p, lastof(buf), "Savegame ver: %d\n", _sl_version);
00233 p += seprintf(p, lastof(buf), "NewGRF ver: 0x%08X\n", last_ottd_rev);
00234 p += seprintf(p, lastof(buf), "Modified: %d\n", ever_modified);
00235
00236 if (removed_newgrfs) {
00237 p += seprintf(p, lastof(buf), "NewGRFs have been removed\n");
00238 }
00239
00240 p = strecpy(p, "NewGRFs:\n", lastof(buf));
00241 if (_load_check_data.HasNewGrfs()) {
00242 for (GRFConfig *c = _load_check_data.grfconfig; c != NULL; c = c->next) {
00243 char md5sum[33];
00244 md5sumToString(md5sum, lastof(md5sum), HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum);
00245 p += seprintf(p, lastof(buf), "%08X %s %s\n", c->ident.grfid, md5sum, c->filename);
00246 }
00247 }
00248
00249
00250
00251 #if !defined(WIN32) && !defined(WIN64)
00252 printf("%s\n", buf);
00253 #else
00254 ShowInfo(buf);
00255 #endif
00256 }
00257
00258
00265 static void ParseResolution(Dimension *res, const char *s)
00266 {
00267 const char *t = strchr(s, 'x');
00268 if (t == NULL) {
00269 ShowInfoF("Invalid resolution '%s'", s);
00270 return;
00271 }
00272
00273 res->width = max(strtoul(s, NULL, 0), 64UL);
00274 res->height = max(strtoul(t + 1, NULL, 0), 64UL);
00275 }
00276
00277
00282 static void ShutdownGame()
00283 {
00284 IConsoleFree();
00285
00286 if (_network_available) NetworkShutDown();
00287
00288 DriverFactoryBase::ShutdownDrivers();
00289
00290 UnInitWindowSystem();
00291
00292
00293 AI::Uninitialize(false);
00294 Game::Uninitialize(false);
00295
00296
00297 GamelogReset();
00298
00299
00300
00301
00302
00303 InitializeLinkGraphs();
00304
00305 #ifdef ENABLE_NETWORK
00306 free(_config_file);
00307 #endif
00308
00309 PoolBase::Clean(PT_ALL);
00310
00311
00312 if (_game_mode != GM_BOOTSTRAP) ResetNewGRFData();
00313
00314
00315 FioCloseAll();
00316
00317 UninitFreeType();
00318 }
00319
00324 static void LoadIntroGame(bool load_newgrfs = true)
00325 {
00326 _game_mode = GM_MENU;
00327
00328 if (load_newgrfs) ResetGRFConfig(false);
00329
00330
00331 ResetWindowSystem();
00332 SetupColoursAndInitialWindow();
00333
00334
00335 if (SaveOrLoad("opntitle.dat", SL_LOAD, BASESET_DIR) != SL_OK) {
00336 GenerateWorld(GWM_EMPTY, 64, 64);
00337 WaitTillGeneratedWorld();
00338 SetLocalCompany(COMPANY_SPECTATOR);
00339 } else {
00340 SetLocalCompany(COMPANY_FIRST);
00341 }
00342
00343 _pause_mode = PM_UNPAUSED;
00344 _cursor.fix_at = false;
00345
00346 if (load_newgrfs) CheckForMissingSprites();
00347 CheckForMissingGlyphs();
00348
00349
00350 if (_music_driver->IsSongPlaying()) ResetMusic();
00351 }
00352
00353 void MakeNewgameSettingsLive()
00354 {
00355 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00356 if (_settings_game.ai_config[c] != NULL) {
00357 delete _settings_game.ai_config[c];
00358 }
00359 }
00360 if (_settings_game.game_config != NULL) {
00361 delete _settings_game.game_config;
00362 }
00363
00364
00365
00366 _settings_game = _settings_newgame;
00367 _old_vds = _settings_client.company.vehicle;
00368
00369 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00370 _settings_game.ai_config[c] = NULL;
00371 if (_settings_newgame.ai_config[c] != NULL) {
00372 _settings_game.ai_config[c] = new AIConfig(_settings_newgame.ai_config[c]);
00373 }
00374 }
00375 _settings_game.game_config = NULL;
00376 if (_settings_newgame.game_config != NULL) {
00377 _settings_game.game_config = new GameConfig(_settings_newgame.game_config);
00378 }
00379 }
00380
00381 void OpenBrowser(const char *url)
00382 {
00383
00384 if (strstr(url, "http://") != url && strstr(url, "https://") != url) return;
00385
00386 extern void OSOpenBrowser(const char *url);
00387 OSOpenBrowser(url);
00388 }
00389
00391 struct AfterNewGRFScan : NewGRFScanCallback {
00392 Year startyear;
00393 uint generation_seed;
00394 char *dedicated_host;
00395 uint16 dedicated_port;
00396 char *network_conn;
00397 const char *join_server_password;
00398 const char *join_company_password;
00399 bool *save_config_ptr;
00400 bool save_config;
00401
00407 AfterNewGRFScan(bool *save_config_ptr) :
00408 startyear(INVALID_YEAR), generation_seed(GENERATE_NEW_SEED),
00409 dedicated_host(NULL), dedicated_port(0), network_conn(NULL),
00410 join_server_password(NULL), join_company_password(NULL),
00411 save_config_ptr(save_config_ptr), save_config(true)
00412 {
00413 }
00414
00415 virtual void OnNewGRFsScanned()
00416 {
00417 ResetGRFConfig(false);
00418
00419 TarScanner::DoScan(TarScanner::SCENARIO);
00420
00421 AI::Initialize();
00422 Game::Initialize();
00423
00424
00425 uint last_newgrf_count = _settings_client.gui.last_newgrf_count;
00426 LoadFromConfig();
00427 _settings_client.gui.last_newgrf_count = last_newgrf_count;
00428
00429
00430 UpdateNewGRFConfigPalette();
00431
00432 Game::Uninitialize(true);
00433 AI::Uninitialize(true);
00434 CheckConfig();
00435 LoadFromHighScore();
00436 LoadHotkeysFromConfig();
00437
00438
00439 *save_config_ptr = save_config;
00440
00441
00442 _music_driver->SetVolume(_settings_client.music.music_vol);
00443
00444 if (startyear != INVALID_YEAR) _settings_newgame.game_creation.starting_year = startyear;
00445 if (generation_seed != GENERATE_NEW_SEED) _settings_newgame.game_creation.generation_seed = generation_seed;
00446
00447 #if defined(ENABLE_NETWORK)
00448 if (dedicated_host != NULL) {
00449 _network_bind_list.Clear();
00450 *_network_bind_list.Append() = strdup(dedicated_host);
00451 }
00452 if (dedicated_port != 0) _settings_client.network.server_port = dedicated_port;
00453 #endif
00454
00455
00456 IConsoleInit();
00457 InitializeGUI();
00458 IConsoleCmdExec("exec scripts/autoexec.scr 0");
00459
00460
00461 if (_switch_mode != SM_NONE) MakeNewgameSettingsLive();
00462
00463 #ifdef ENABLE_NETWORK
00464 if (_network_available && network_conn != NULL) {
00465 const char *port = NULL;
00466 const char *company = NULL;
00467 uint16 rport = NETWORK_DEFAULT_PORT;
00468 CompanyID join_as = COMPANY_NEW_COMPANY;
00469
00470 ParseConnectionString(&company, &port, network_conn);
00471
00472 if (company != NULL) {
00473 join_as = (CompanyID)atoi(company);
00474
00475 if (join_as != COMPANY_SPECTATOR) {
00476 join_as--;
00477 if (join_as >= MAX_COMPANIES) {
00478 delete this;
00479 return;
00480 }
00481 }
00482 }
00483 if (port != NULL) rport = atoi(port);
00484
00485 LoadIntroGame();
00486 _switch_mode = SM_NONE;
00487 NetworkClientConnectGame(NetworkAddress(network_conn, rport), join_as, join_server_password, join_company_password);
00488 }
00489 #endif
00490
00491
00492 delete this;
00493 }
00494 };
00495
00496 #if defined(UNIX) && !defined(__MORPHOS__)
00497 extern void DedicatedFork();
00498 #endif
00499
00501 static const OptionData _options[] = {
00502 GETOPT_SHORT_VALUE('I'),
00503 GETOPT_SHORT_VALUE('S'),
00504 GETOPT_SHORT_VALUE('M'),
00505 GETOPT_SHORT_VALUE('m'),
00506 GETOPT_SHORT_VALUE('s'),
00507 GETOPT_SHORT_VALUE('v'),
00508 GETOPT_SHORT_VALUE('b'),
00509 #if defined(ENABLE_NETWORK)
00510 GETOPT_SHORT_OPTVAL('D'),
00511 GETOPT_SHORT_OPTVAL('n'),
00512 GETOPT_SHORT_VALUE('l'),
00513 GETOPT_SHORT_VALUE('p'),
00514 GETOPT_SHORT_VALUE('P'),
00515 #if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
00516 GETOPT_SHORT_NOVAL('f'),
00517 #endif
00518 #endif
00519 GETOPT_SHORT_VALUE('r'),
00520 GETOPT_SHORT_VALUE('t'),
00521 GETOPT_SHORT_OPTVAL('d'),
00522 GETOPT_SHORT_NOVAL('e'),
00523 GETOPT_SHORT_OPTVAL('g'),
00524 GETOPT_SHORT_VALUE('G'),
00525 GETOPT_SHORT_VALUE('c'),
00526 GETOPT_SHORT_NOVAL('x'),
00527 GETOPT_SHORT_VALUE('q'),
00528 GETOPT_SHORT_NOVAL('h'),
00529 GETOPT_END()
00530 };
00531
00532
00533 int ttd_main(int argc, char *argv[])
00534 {
00535 char *musicdriver = NULL;
00536 char *sounddriver = NULL;
00537 char *videodriver = NULL;
00538 char *blitter = NULL;
00539 char *graphics_set = NULL;
00540 char *sounds_set = NULL;
00541 char *music_set = NULL;
00542 Dimension resolution = {0, 0};
00543
00544 bool save_config = false;
00545 AfterNewGRFScan *scanner = new AfterNewGRFScan(&save_config);
00546 #if defined(ENABLE_NETWORK)
00547 bool dedicated = false;
00548 char *debuglog_conn = NULL;
00549
00550 extern bool _dedicated_forks;
00551 _dedicated_forks = false;
00552 #endif
00553
00554 _game_mode = GM_MENU;
00555 _switch_mode = SM_MENU;
00556 _config_file = NULL;
00557
00558 GetOptData mgo(argc - 1, argv + 1, _options);
00559
00560 int i;
00561 while ((i = mgo.GetOpt()) != -1) {
00562 switch (i) {
00563 case 'I': free(graphics_set); graphics_set = strdup(mgo.opt); break;
00564 case 'S': free(sounds_set); sounds_set = strdup(mgo.opt); break;
00565 case 'M': free(music_set); music_set = strdup(mgo.opt); break;
00566 case 'm': free(musicdriver); musicdriver = strdup(mgo.opt); break;
00567 case 's': free(sounddriver); sounddriver = strdup(mgo.opt); break;
00568 case 'v': free(videodriver); videodriver = strdup(mgo.opt); break;
00569 case 'b': free(blitter); blitter = strdup(mgo.opt); break;
00570 #if defined(ENABLE_NETWORK)
00571 case 'D':
00572 free(musicdriver);
00573 free(sounddriver);
00574 free(videodriver);
00575 free(blitter);
00576 musicdriver = strdup("null");
00577 sounddriver = strdup("null");
00578 videodriver = strdup("dedicated");
00579 blitter = strdup("null");
00580 dedicated = true;
00581 SetDebugString("net=6");
00582 if (mgo.opt != NULL) {
00583
00584
00585 const char *temp = NULL;
00586 const char *port = NULL;
00587 ParseConnectionString(&temp, &port, mgo.opt);
00588 if (!StrEmpty(mgo.opt)) scanner->dedicated_host = mgo.opt;
00589 if (port != NULL) scanner->dedicated_port = atoi(port);
00590 }
00591 break;
00592 case 'f': _dedicated_forks = true; break;
00593 case 'n':
00594 scanner->network_conn = mgo.opt;
00595 break;
00596 case 'l':
00597 debuglog_conn = mgo.opt;
00598 break;
00599 case 'p':
00600 scanner->join_server_password = mgo.opt;
00601 break;
00602 case 'P':
00603 scanner->join_company_password = mgo.opt;
00604 break;
00605 #endif
00606 case 'r': ParseResolution(&resolution, mgo.opt); break;
00607 case 't': scanner->startyear = atoi(mgo.opt); break;
00608 case 'd': {
00609 #if defined(WIN32)
00610 CreateConsole();
00611 #endif
00612 if (mgo.opt != NULL) SetDebugString(mgo.opt);
00613 break;
00614 }
00615 case 'e': _switch_mode = (_switch_mode == SM_LOAD_GAME || _switch_mode == SM_LOAD_SCENARIO ? SM_LOAD_SCENARIO : SM_EDITOR); break;
00616 case 'g':
00617 if (mgo.opt != NULL) {
00618 strecpy(_file_to_saveload.name, mgo.opt, lastof(_file_to_saveload.name));
00619 _switch_mode = (_switch_mode == SM_EDITOR || _switch_mode == SM_LOAD_SCENARIO ? SM_LOAD_SCENARIO : SM_LOAD_GAME);
00620 _file_to_saveload.mode = SL_LOAD;
00621
00622
00623 const char *t = strrchr(_file_to_saveload.name, '.');
00624 if (t != NULL) {
00625 FiosType ft = FiosGetSavegameListCallback(SLD_LOAD_GAME, _file_to_saveload.name, t, NULL, NULL);
00626 if (ft != FIOS_TYPE_INVALID) SetFiosType(ft);
00627 }
00628
00629 break;
00630 }
00631
00632 _switch_mode = SM_NEWGAME;
00633
00634 if (scanner->generation_seed == GENERATE_NEW_SEED) {
00635 scanner->generation_seed = InteractiveRandom();
00636 }
00637 break;
00638 case 'q': {
00639 DeterminePaths(argv[0]);
00640 if (StrEmpty(mgo.opt)) return 1;
00641 char title[80];
00642 title[0] = '\0';
00643 FiosGetSavegameListCallback(SLD_LOAD_GAME, mgo.opt, strrchr(mgo.opt, '.'), title, lastof(title));
00644
00645 _load_check_data.Clear();
00646 SaveOrLoadResult res = SaveOrLoad(mgo.opt, SL_LOAD_CHECK, SAVE_DIR, false);
00647 if (res != SL_OK || _load_check_data.HasErrors()) {
00648 fprintf(stderr, "Failed to open savegame\n");
00649 if (_load_check_data.HasErrors()) {
00650 char buf[256];
00651 SetDParamStr(0, _load_check_data.error_data);
00652 GetString(buf, _load_check_data.error, lastof(buf));
00653 fprintf(stderr, "%s\n", buf);
00654 }
00655 return 1;
00656 }
00657
00658 WriteSavegameInfo(title);
00659
00660 return 0;
00661 }
00662 case 'G': scanner->generation_seed = atoi(mgo.opt); break;
00663 case 'c': _config_file = strdup(mgo.opt); break;
00664 case 'x': scanner->save_config = false; break;
00665 case 'h':
00666 i = -2;
00667 break;
00668 }
00669 if (i == -2) break;
00670 }
00671
00672 if (i == -2 || mgo.numleft > 0) {
00673
00674
00675
00676
00677
00678 DeterminePaths(argv[0]);
00679 TarScanner::DoScan(TarScanner::BASESET);
00680 BaseGraphics::FindSets();
00681 BaseSounds::FindSets();
00682 BaseMusic::FindSets();
00683 ShowHelp();
00684 delete scanner;
00685 return 0;
00686 }
00687
00688 #if defined(WINCE) && defined(_DEBUG)
00689
00690 SetDebugString("4");
00691 #endif
00692
00693 DeterminePaths(argv[0]);
00694 TarScanner::DoScan(TarScanner::BASESET);
00695
00696 #if defined(ENABLE_NETWORK)
00697 if (dedicated) DEBUG(net, 0, "Starting dedicated version %s", _openttd_revision);
00698 if (_dedicated_forks && !dedicated) _dedicated_forks = false;
00699
00700 #if defined(UNIX) && !defined(__MORPHOS__)
00701
00702 if (_dedicated_forks) DedicatedFork();
00703 #endif
00704 #endif
00705
00706 LoadFromConfig(true);
00707
00708 if (resolution.width != 0) _cur_resolution = resolution;
00709
00710
00711
00712
00713
00714
00715
00716 _cur_resolution.width = ClampU(_cur_resolution.width, 1, UINT16_MAX / 2);
00717 _cur_resolution.height = ClampU(_cur_resolution.height, 1, UINT16_MAX / 2);
00718
00719
00720
00721
00722
00723 _cursor.in_window = true;
00724
00725
00726 InitializeLanguagePacks();
00727
00728
00729 InitFreeType(false);
00730
00731
00732 InitWindowSystem();
00733
00734 BaseGraphics::FindSets();
00735 if (graphics_set == NULL && BaseGraphics::ini_set != NULL) graphics_set = strdup(BaseGraphics::ini_set);
00736 if (!BaseGraphics::SetSet(graphics_set) && !StrEmpty(graphics_set)) {
00737 usererror("Failed to select requested graphics set '%s'", graphics_set);
00738 }
00739 free(graphics_set);
00740
00741
00742 GfxInitPalettes();
00743
00744 DEBUG(misc, 1, "Loading blitter...");
00745 if (blitter == NULL && _ini_blitter != NULL) blitter = strdup(_ini_blitter);
00746 _blitter_autodetected = StrEmpty(blitter);
00747
00748 if (!_blitter_autodetected || BaseGraphics::GetUsedSet() == NULL || BaseGraphics::GetUsedSet()->blitter == BLT_8BPP || BlitterFactoryBase::SelectBlitter("32bpp-anim") == NULL) {
00749 if (BlitterFactoryBase::SelectBlitter(blitter) == NULL) {
00750 StrEmpty(blitter) ?
00751 usererror("Failed to autoprobe blitter") :
00752 usererror("Failed to select requested blitter '%s'; does it exist?", blitter);
00753 }
00754 }
00755 free(blitter);
00756
00757 if (videodriver == NULL && _ini_videodriver != NULL) videodriver = strdup(_ini_videodriver);
00758 _video_driver = (VideoDriver*)VideoDriverFactoryBase::SelectDriver(videodriver, Driver::DT_VIDEO);
00759 if (_video_driver == NULL) {
00760 StrEmpty(videodriver) ?
00761 usererror("Failed to autoprobe video driver") :
00762 usererror("Failed to select requested video driver '%s'", videodriver);
00763 }
00764 free(videodriver);
00765
00766
00767 _screen.zoom = ZOOM_LVL_NORMAL;
00768
00769 NetworkStartUp();
00770
00771 #if defined(ENABLE_NETWORK)
00772 if (debuglog_conn != NULL && _network_available) {
00773 const char *not_used = NULL;
00774 const char *port = NULL;
00775 uint16 rport;
00776
00777 rport = NETWORK_DEFAULT_DEBUGLOG_PORT;
00778
00779 ParseConnectionString(¬_used, &port, debuglog_conn);
00780 if (port != NULL) rport = atoi(port);
00781
00782 NetworkStartDebugLog(NetworkAddress(debuglog_conn, rport));
00783 }
00784 #endif
00785
00786 if (!HandleBootstrap()) goto exit;
00787
00788 _video_driver->ClaimMousePointer();
00789
00790
00791 InitializeScreenshotFormats();
00792
00793 BaseSounds::FindSets();
00794 if (sounds_set == NULL && BaseSounds::ini_set != NULL) sounds_set = strdup(BaseSounds::ini_set);
00795 if (!BaseSounds::SetSet(sounds_set)) {
00796 StrEmpty(sounds_set) ?
00797 usererror("Failed to find a sounds set. Please acquire a sounds set for OpenTTD. See section 4.1 of readme.txt.") :
00798 usererror("Failed to select requested sounds set '%s'", sounds_set);
00799 }
00800 free(sounds_set);
00801
00802 BaseMusic::FindSets();
00803 if (music_set == NULL && BaseMusic::ini_set != NULL) music_set = strdup(BaseMusic::ini_set);
00804 if (!BaseMusic::SetSet(music_set)) {
00805 StrEmpty(music_set) ?
00806 usererror("Failed to find a music set. Please acquire a music set for OpenTTD. See section 4.1 of readme.txt.") :
00807 usererror("Failed to select requested music set '%s'", music_set);
00808 }
00809 free(music_set);
00810
00811 if (sounddriver == NULL && _ini_sounddriver != NULL) sounddriver = strdup(_ini_sounddriver);
00812 _sound_driver = (SoundDriver*)SoundDriverFactoryBase::SelectDriver(sounddriver, Driver::DT_SOUND);
00813 if (_sound_driver == NULL) {
00814 StrEmpty(sounddriver) ?
00815 usererror("Failed to autoprobe sound driver") :
00816 usererror("Failed to select requested sound driver '%s'", sounddriver);
00817 }
00818 free(sounddriver);
00819
00820 if (musicdriver == NULL && _ini_musicdriver != NULL) musicdriver = strdup(_ini_musicdriver);
00821 _music_driver = (MusicDriver*)MusicDriverFactoryBase::SelectDriver(musicdriver, Driver::DT_MUSIC);
00822 if (_music_driver == NULL) {
00823 StrEmpty(musicdriver) ?
00824 usererror("Failed to autoprobe music driver") :
00825 usererror("Failed to select requested music driver '%s'", musicdriver);
00826 }
00827 free(musicdriver);
00828
00829
00830 _modal_progress_paint_mutex->BeginCritical();
00831 _modal_progress_work_mutex->BeginCritical();
00832
00833 GenerateWorld(GWM_EMPTY, 64, 64);
00834 WaitTillGeneratedWorld();
00835
00836 LoadIntroGame(false);
00837
00838 CheckForMissingGlyphs();
00839
00840 ScanNewGRFFiles(scanner);
00841
00842 _video_driver->MainLoop();
00843
00844 WaitTillSaved();
00845
00846
00847 if (save_config) {
00848 SaveToConfig();
00849 SaveHotkeysToConfig();
00850 SaveToHighScore();
00851 }
00852
00853 exit:
00854
00855 ShutdownGame();
00856
00857 free(BaseGraphics::ini_set);
00858 free(BaseSounds::ini_set);
00859 free(BaseMusic::ini_set);
00860 free(_ini_musicdriver);
00861 free(_ini_sounddriver);
00862 free(_ini_videodriver);
00863 free(_ini_blitter);
00864
00865 return 0;
00866 }
00867
00868 void HandleExitGameRequest()
00869 {
00870 if (_game_mode == GM_MENU || _game_mode == GM_BOOTSTRAP) {
00871 _exit_game = true;
00872 } else if (_settings_client.gui.autosave_on_exit) {
00873 DoExitSave();
00874 _exit_game = true;
00875 } else {
00876 AskExitGame();
00877 }
00878 }
00879
00880 static void MakeNewGameDone()
00881 {
00882 SettingsDisableElrail(_settings_game.vehicle.disable_elrails);
00883
00884
00885 if (!_video_driver->HasGUI()) {
00886 SetLocalCompany(COMPANY_SPECTATOR);
00887 IConsoleCmdExec("exec scripts/game_start.scr 0");
00888 return;
00889 }
00890
00891
00892 DoStartupNewCompany(false);
00893
00894 Company *c = Company::Get(COMPANY_FIRST);
00895 c->settings = _settings_client.company;
00896
00897 IConsoleCmdExec("exec scripts/game_start.scr 0");
00898
00899 SetLocalCompany(COMPANY_FIRST);
00900
00901 InitializeRailGUI();
00902
00903 #ifdef ENABLE_NETWORK
00904
00905
00906 if (_network_server && !StrEmpty(_settings_client.network.default_company_pass)) {
00907 NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass);
00908 }
00909 #endif
00910
00911 if (_settings_client.gui.pause_on_newgame) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
00912
00913 MarkWholeScreenDirty();
00914 }
00915
00916 static void MakeNewGame(bool from_heightmap, bool reset_settings)
00917 {
00918 _game_mode = GM_NORMAL;
00919
00920 ResetGRFConfig(true);
00921
00922 GenerateWorldSetCallback(&MakeNewGameDone);
00923 GenerateWorld(from_heightmap ? GWM_HEIGHTMAP : GWM_NEWGAME, 1 << _settings_game.game_creation.map_x, 1 << _settings_game.game_creation.map_y, reset_settings);
00924 }
00925
00926 static void MakeNewEditorWorldDone()
00927 {
00928 SetLocalCompany(OWNER_NONE);
00929 }
00930
00931 static void MakeNewEditorWorld()
00932 {
00933 _game_mode = GM_EDITOR;
00934
00935 ResetGRFConfig(true);
00936
00937 GenerateWorldSetCallback(&MakeNewEditorWorldDone);
00938 GenerateWorld(GWM_EMPTY, 1 << _settings_game.game_creation.map_x, 1 << _settings_game.game_creation.map_y);
00939 }
00940
00951 bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL)
00952 {
00953 assert(mode == SL_LOAD || (lf == NULL && mode == SL_OLD_LOAD));
00954 GameMode ogm = _game_mode;
00955
00956 _game_mode = newgm;
00957
00958 switch (lf == NULL ? SaveOrLoad(filename, mode, subdir) : LoadWithFilter(lf)) {
00959 case SL_OK: return true;
00960
00961 case SL_REINIT:
00962 #ifdef ENABLE_NETWORK
00963 if (_network_dedicated) {
00964
00965
00966
00967
00968
00969
00970 DEBUG(net, 0, "Loading game failed, so a new (random) game will be started!");
00971 MakeNewGame(false, true);
00972 return false;
00973 }
00974 if (_network_server) {
00975
00976 NetworkDisconnect();
00977 }
00978 #endif
00979
00980 switch (ogm) {
00981 default:
00982 case GM_MENU: LoadIntroGame(); break;
00983 case GM_EDITOR: MakeNewEditorWorld(); break;
00984 }
00985 return false;
00986
00987 default:
00988 _game_mode = ogm;
00989 return false;
00990 }
00991 }
00992
00993 void SwitchToMode(SwitchMode new_mode)
00994 {
00995 #ifdef ENABLE_NETWORK
00996
00997 if (new_mode != SM_SAVE_GAME) {
00998
00999 if (_networking) {
01000 if (_network_server && (new_mode == SM_LOAD_GAME || new_mode == SM_NEWGAME || new_mode == SM_RESTARTGAME)) {
01001 NetworkReboot();
01002 } else {
01003 NetworkDisconnect();
01004 }
01005 }
01006
01007
01008 if (_is_network_server) {
01009
01010 if (new_mode != SM_MENU) {
01011
01012 if (_settings_client.network.reload_cfg) {
01013 LoadFromConfig();
01014 MakeNewgameSettingsLive();
01015 ResetGRFConfig(false);
01016 }
01017 NetworkServerStart();
01018 } else {
01019
01020 _is_network_server = false;
01021 }
01022 }
01023 }
01024 #endif
01025
01026 if (new_mode != SM_SAVE_GAME) AI::KillAll();
01027
01028 switch (new_mode) {
01029 case SM_EDITOR:
01030 MakeNewEditorWorld();
01031 break;
01032
01033 case SM_RESTARTGAME:
01034 case SM_NEWGAME:
01035 #ifdef ENABLE_NETWORK
01036 if (_network_server) {
01037 snprintf(_network_game_info.map_name, lengthof(_network_game_info.map_name), "Random Map");
01038 }
01039 #endif
01040 MakeNewGame(false, new_mode == SM_NEWGAME);
01041 break;
01042
01043 case SM_LOAD_GAME: {
01044 ResetGRFConfig(true);
01045 ResetWindowSystem();
01046
01047 if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, NO_DIRECTORY)) {
01048 SetDParamStr(0, GetSaveLoadErrorString());
01049 ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
01050 } else {
01051 if (_saveload_mode == SLD_LOAD_SCENARIO) {
01052 StartupEngines();
01053 }
01054
01055
01056 SetLocalCompany(_network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST);
01057
01058 IConsoleCmdExec("exec scripts/game_start.scr 0");
01059
01060 DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE);
01061 #ifdef ENABLE_NETWORK
01062 if (_network_server) {
01063 snprintf(_network_game_info.map_name, lengthof(_network_game_info.map_name), "%s (Loaded game)", _file_to_saveload.title);
01064 }
01065 #endif
01066 }
01067 break;
01068 }
01069
01070 case SM_START_HEIGHTMAP:
01071 #ifdef ENABLE_NETWORK
01072 if (_network_server) {
01073 snprintf(_network_game_info.map_name, lengthof(_network_game_info.map_name), "%s (Heightmap)", _file_to_saveload.title);
01074 }
01075 #endif
01076 MakeNewGame(true, true);
01077 break;
01078
01079 case SM_LOAD_HEIGHTMAP:
01080 SetLocalCompany(OWNER_NONE);
01081
01082 GenerateWorld(GWM_HEIGHTMAP, 1 << _settings_game.game_creation.map_x, 1 << _settings_game.game_creation.map_y);
01083 MarkWholeScreenDirty();
01084 break;
01085
01086 case SM_LOAD_SCENARIO: {
01087 if (SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_EDITOR, NO_DIRECTORY)) {
01088 SetLocalCompany(OWNER_NONE);
01089 _settings_newgame.game_creation.starting_year = _cur_year;
01090
01091 DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE);
01092 } else {
01093 SetDParamStr(0, GetSaveLoadErrorString());
01094 ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
01095 }
01096 break;
01097 }
01098
01099 case SM_MENU:
01100 LoadIntroGame();
01101 if (BaseSounds::ini_set == NULL && BaseSounds::GetUsedSet()->fallback) {
01102 ShowErrorMessage(STR_WARNING_FALLBACK_SOUNDSET, INVALID_STRING_ID, WL_CRITICAL);
01103 BaseSounds::ini_set = strdup(BaseSounds::GetUsedSet()->name);
01104 }
01105 break;
01106
01107 case SM_SAVE_GAME:
01108
01109 if (SaveOrLoad(_file_to_saveload.name, SL_SAVE, NO_DIRECTORY) != SL_OK) {
01110 SetDParamStr(0, GetSaveLoadErrorString());
01111 ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
01112 } else {
01113 DeleteWindowById(WC_SAVELOAD, 0);
01114 }
01115 break;
01116
01117 case SM_SAVE_HEIGHTMAP:
01118 MakeHeightmapScreenshot(_file_to_saveload.name);
01119 DeleteWindowById(WC_SAVELOAD, 0);
01120 break;
01121
01122 case SM_GENRANDLAND:
01123 SetLocalCompany(OWNER_NONE);
01124 GenerateWorld(GWM_RANDOM, 1 << _settings_game.game_creation.map_x, 1 << _settings_game.game_creation.map_y);
01125
01126 MarkWholeScreenDirty();
01127 break;
01128
01129 default: NOT_REACHED();
01130 }
01131 }
01132
01133
01140 static void CheckCaches()
01141 {
01142
01143
01144 if (_debug_desync_level <= 1) return;
01145
01146
01147 SmallVector<TownCache, 4> old_town_caches;
01148 Town *t;
01149 FOR_ALL_TOWNS(t) {
01150 MemCpyT(old_town_caches.Append(), &t->cache);
01151 }
01152
01153 extern void RebuildTownCaches();
01154 RebuildTownCaches();
01155
01156 uint i = 0;
01157 FOR_ALL_TOWNS(t) {
01158 if (MemCmpT(old_town_caches.Get(i), &t->cache) != 0) {
01159 DEBUG(desync, 2, "town cache mismatch: town %i", (int)t->index);
01160 }
01161 i++;
01162 }
01163
01164
01165 SmallVector<CompanyInfrastructure, 4> old_infrastructure;
01166 Company *c;
01167 FOR_ALL_COMPANIES(c) MemCpyT(old_infrastructure.Append(), &c->infrastructure);
01168
01169 extern void AfterLoadCompanyStats();
01170 AfterLoadCompanyStats();
01171
01172 i = 0;
01173 FOR_ALL_COMPANIES(c) {
01174 if (MemCmpT(old_infrastructure.Get(i), &c->infrastructure) != 0) {
01175 DEBUG(desync, 2, "infrastructure cache mismatch: company %i", (int)c->index);
01176 }
01177 i++;
01178 }
01179
01180
01181 const RoadStop *rs;
01182 FOR_ALL_ROADSTOPS(rs) {
01183 if (IsStandardRoadStopTile(rs->xy)) continue;
01184
01185 assert(rs->GetEntry(DIAGDIR_NE) != rs->GetEntry(DIAGDIR_NW));
01186 rs->GetEntry(DIAGDIR_NE)->CheckIntegrity(rs);
01187 rs->GetEntry(DIAGDIR_NW)->CheckIntegrity(rs);
01188 }
01189
01190 Vehicle *v;
01191 FOR_ALL_VEHICLES(v) {
01192 extern void FillNewGRFVehicleCache(const Vehicle *v);
01193 if (v != v->First() || v->vehstatus & VS_CRASHED || !v->IsPrimaryVehicle()) continue;
01194
01195 uint length = 0;
01196 for (const Vehicle *u = v; u != NULL; u = u->Next()) length++;
01197
01198 NewGRFCache *grf_cache = CallocT<NewGRFCache>(length);
01199 VehicleCache *veh_cache = CallocT<VehicleCache>(length);
01200 GroundVehicleCache *gro_cache = CallocT<GroundVehicleCache>(length);
01201 TrainCache *tra_cache = CallocT<TrainCache>(length);
01202
01203 length = 0;
01204 for (const Vehicle *u = v; u != NULL; u = u->Next()) {
01205 FillNewGRFVehicleCache(u);
01206 grf_cache[length] = u->grf_cache;
01207 veh_cache[length] = u->vcache;
01208 switch (u->type) {
01209 case VEH_TRAIN:
01210 gro_cache[length] = Train::From(u)->gcache;
01211 tra_cache[length] = Train::From(u)->tcache;
01212 break;
01213 case VEH_ROAD:
01214 gro_cache[length] = RoadVehicle::From(u)->gcache;
01215 break;
01216 default:
01217 break;
01218 }
01219 length++;
01220 }
01221
01222 switch (v->type) {
01223 case VEH_TRAIN: Train::From(v)->ConsistChanged(true); break;
01224 case VEH_ROAD: RoadVehUpdateCache(RoadVehicle::From(v)); break;
01225 case VEH_AIRCRAFT: UpdateAircraftCache(Aircraft::From(v)); break;
01226 case VEH_SHIP: Ship::From(v)->UpdateCache(); break;
01227 default: break;
01228 }
01229
01230 length = 0;
01231 for (const Vehicle *u = v; u != NULL; u = u->Next()) {
01232 FillNewGRFVehicleCache(u);
01233 if (memcmp(&grf_cache[length], &u->grf_cache, sizeof(NewGRFCache)) != 0) {
01234 DEBUG(desync, 2, "newgrf cache mismatch: type %i, vehicle %i, company %i, unit number %i, wagon %i", (int)v->type, v->index, (int)v->owner, v->unitnumber, length);
01235 }
01236 if (memcmp(&veh_cache[length], &u->vcache, sizeof(VehicleCache)) != 0) {
01237 DEBUG(desync, 2, "vehicle cache mismatch: type %i, vehicle %i, company %i, unit number %i, wagon %i", (int)v->type, v->index, (int)v->owner, v->unitnumber, length);
01238 }
01239 switch (u->type) {
01240 case VEH_TRAIN:
01241 if (memcmp(&gro_cache[length], &Train::From(u)->gcache, sizeof(GroundVehicleCache)) != 0) {
01242 DEBUG(desync, 2, "train ground vehicle cache mismatch: vehicle %i, company %i, unit number %i, wagon %i", v->index, (int)v->owner, v->unitnumber, length);
01243 }
01244 if (memcmp(&tra_cache[length], &Train::From(u)->tcache, sizeof(TrainCache)) != 0) {
01245 DEBUG(desync, 2, "train cache mismatch: vehicle %i, company %i, unit number %i, wagon %i", v->index, (int)v->owner, v->unitnumber, length);
01246 }
01247 break;
01248 case VEH_ROAD:
01249 if (memcmp(&gro_cache[length], &RoadVehicle::From(u)->gcache, sizeof(GroundVehicleCache)) != 0) {
01250 DEBUG(desync, 2, "road vehicle ground vehicle cache mismatch: vehicle %i, company %i, unit number %i, wagon %i", v->index, (int)v->owner, v->unitnumber, length);
01251 }
01252 break;
01253 default:
01254 break;
01255 }
01256 length++;
01257 }
01258
01259 free(grf_cache);
01260 free(veh_cache);
01261 free(gro_cache);
01262 free(tra_cache);
01263 }
01264
01265
01266 FOR_ALL_VEHICLES(v) {
01267 byte buff[sizeof(VehicleCargoList)];
01268 memcpy(buff, &v->cargo, sizeof(VehicleCargoList));
01269 v->cargo.InvalidateCache();
01270 assert(memcmp(&v->cargo, buff, sizeof(VehicleCargoList)) == 0);
01271 }
01272
01273 Station *st;
01274 FOR_ALL_STATIONS(st) {
01275 for (CargoID c = 0; c < NUM_CARGO; c++) {
01276 byte buff[sizeof(StationCargoList)];
01277 memcpy(buff, &st->goods[c].cargo, sizeof(StationCargoList));
01278 st->goods[c].cargo.InvalidateCache();
01279 assert(memcmp(&st->goods[c].cargo, buff, sizeof(StationCargoList)) == 0);
01280 }
01281 }
01282 }
01283
01289 void StateGameLoop()
01290 {
01291
01292 if (_pause_mode != PM_UNPAUSED) {
01293 UpdateLandscapingLimits();
01294 Game::GameLoop();
01295 CallWindowTickEvent();
01296 return;
01297 }
01298 if (HasModalProgress()) return;
01299
01300 ClearStorageChanges(false);
01301
01302 if (_game_mode == GM_EDITOR) {
01303 RunTileLoop();
01304 CallVehicleTicks();
01305 CallLandscapeTick();
01306 ClearStorageChanges(true);
01307 UpdateLandscapingLimits();
01308
01309 CallWindowTickEvent();
01310 NewsLoop();
01311 } else {
01312 if (_debug_desync_level > 2 && _date_fract == 0 && (_date & 0x1F) == 0) {
01313
01314 char name[MAX_PATH];
01315 snprintf(name, lengthof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
01316 SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false);
01317 }
01318
01319 CheckCaches();
01320
01321
01322
01323 Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
01324
01325 AnimateAnimatedTiles();
01326 IncreaseDate();
01327 RunTileLoop();
01328 CallVehicleTicks();
01329 CallLandscapeTick();
01330 ClearStorageChanges(true);
01331
01332 AI::GameLoop();
01333 Game::GameLoop();
01334 UpdateLandscapingLimits();
01335
01336 CallWindowTickEvent();
01337 NewsLoop();
01338 cur_company.Restore();
01339 }
01340
01341 assert(IsLocalCompany());
01342 }
01343
01348 static void DoAutosave()
01349 {
01350 char buf[MAX_PATH];
01351
01352 #if defined(PSP)
01353
01354 if (_networking) return;
01355 #endif
01356
01357 if (_settings_client.gui.keep_all_autosave) {
01358 GenerateDefaultSaveName(buf, lastof(buf));
01359 strecat(buf, ".sav", lastof(buf));
01360 } else {
01361 static int _autosave_ctr = 0;
01362
01363
01364 snprintf(buf, sizeof(buf), "autosave%d.sav", _autosave_ctr);
01365
01366 if (++_autosave_ctr >= _settings_client.gui.max_num_autosaves) _autosave_ctr = 0;
01367 }
01368
01369 DEBUG(sl, 2, "Autosaving to '%s'", buf);
01370 if (SaveOrLoad(buf, SL_SAVE, AUTOSAVE_DIR) != SL_OK) {
01371 ShowErrorMessage(STR_ERROR_AUTOSAVE_FAILED, INVALID_STRING_ID, WL_ERROR);
01372 }
01373 }
01374
01375 void GameLoop()
01376 {
01377 if (_game_mode == GM_BOOTSTRAP) {
01378 #ifdef ENABLE_NETWORK
01379
01380 if (_network_available) NetworkBackgroundLoop();
01381 #endif
01382 InputLoop();
01383 return;
01384 }
01385
01386 ProcessAsyncSaveFinish();
01387
01388
01389 if (_do_autosave) {
01390 _do_autosave = false;
01391 DoAutosave();
01392 SetWindowDirty(WC_STATUS_BAR, 0);
01393 }
01394
01395
01396 if (_switch_mode != SM_NONE && !HasModalProgress()) {
01397 SwitchToMode(_switch_mode);
01398 _switch_mode = SM_NONE;
01399 }
01400
01401 IncreaseSpriteLRU();
01402 InteractiveRandom();
01403
01404 extern int _caret_timer;
01405 _caret_timer += 3;
01406 CursorTick();
01407
01408 #ifdef ENABLE_NETWORK
01409
01410 if (_network_available) NetworkBackgroundLoop();
01411
01412 if (_networking && !HasModalProgress()) {
01413
01414 NetworkGameLoop();
01415 } else {
01416 if (_network_reconnect > 0 && --_network_reconnect == 0) {
01417
01418
01419 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_SPECTATOR);
01420 }
01421
01422 StateGameLoop();
01423 }
01424
01425
01426 static uint check_message = 0;
01427 if (++check_message > 1000 / MILLISECONDS_PER_TICK) {
01428 check_message = 0;
01429 NetworkChatMessageLoop();
01430 }
01431 #else
01432 StateGameLoop();
01433 #endif
01434
01435 if (!_pause_mode && HasBit(_display_opt, DO_FULL_ANIMATION)) DoPaletteAnimations();
01436
01437 if (!_pause_mode || _game_mode == GM_EDITOR || _settings_game.construction.command_pause_level > CMDPL_NO_CONSTRUCTION) MoveAllTextEffects();
01438
01439 InputLoop();
01440
01441 _sound_driver->MainLoop();
01442 MusicLoop();
01443 }