static void MakeNewGameDone() { SettingsDisableElrail(_settings_game.vehicle.disable_elrails); /* In a dedicated server, the server does not play */ if (_network_dedicated || BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0) { SetLocalCompany(COMPANY_SPECTATOR); IConsoleCmdExec("exec scripts/game_start.scr 0"); return; } /* Create a single company */ DoStartupNewCompany(false); Company *c = Company::Get(COMPANY_FIRST); c->settings = _settings_client.company; IConsoleCmdExec("exec scripts/game_start.scr 0"); SetLocalCompany(COMPANY_FIRST); InitializeRailGUI(); #ifdef ENABLE_NETWORK /* We are the server, we start a new company (not dedicated), * so set the default password *if* needed. */ if (_network_server && !StrEmpty(_settings_client.network.default_company_pass)) { NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass); } #endif /* ENABLE_NETWORK */ if (_settings_client.gui.pause_on_newgame) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE); MarkWholeScreenDirty(); }
static void DedicatedHandleKeyInput() { static char input_line[1024] = ""; if (!InputWaiting()) return; if (_exit_game) return; #if defined(UNIX) || defined(__OS2__) || defined(PSP) if (fgets(input_line, lengthof(input_line), stdin) == NULL) return; #else /* Handle console input, and singal console thread, it can accept input again */ assert_compile(lengthof(_win_console_thread_buffer) <= lengthof(input_line)); strecpy(input_line, _win_console_thread_buffer, lastof(input_line)); SetEvent(_hWaitForInputHandling); #endif /* strtok() does not 'forget' \r\n if the string starts with it, * so we have to manually remove that! */ strtok(input_line, "\r\n"); for (char *c = input_line; *c != '\0'; c++) { if (*c == '\n' || *c == '\r' || c == lastof(input_line)) { *c = '\0'; break; } } str_validate(input_line, lastof(input_line)); IConsoleCmdExec(input_line); // execute command }
static void DedicatedHandleKeyInput() { static char input_line[1024] = ""; if (!InputWaiting()) return; if (_exit_game) return; #if defined(UNIX) || defined(__OS2__) || defined(PSP) if (fgets(input_line, lengthof(input_line), stdin) == NULL) return; #else /* Handle console input, and signal console thread, it can accept input again */ assert_compile(lengthof(_win_console_thread_buffer) <= lengthof(input_line)); strecpy(input_line, _win_console_thread_buffer, lastof(input_line)); SetEvent(_hWaitForInputHandling); #endif /* Remove trailing \r or \n */ for (char *c = input_line; *c != '\0'; c++) { if (*c == '\n' || *c == '\r' || c == lastof(input_line)) { *c = '\0'; break; } } str_validate(input_line, lastof(input_line)); IConsoleCmdExec(input_line); // execute command }
NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_RCON(Packet *p) { if (this->status == ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED); char command[NETWORK_RCONCOMMAND_LENGTH]; p->Recv_string(command, sizeof(command)); DEBUG(net, 2, "[admin] Rcon command from '%s' (%s): '%s'", this->admin_name, this->admin_version, command); _redirect_console_to_admin = this->index; IConsoleCmdExec(command); _redirect_console_to_admin = INVALID_ADMIN_ID; return NETWORK_RECV_STATUS_OKAY; }
virtual EventState OnKeyPress(uint16 key, uint16 keycode) { if (_focused_window != this) return ES_NOT_HANDLED; const int scroll_height = (this->height / this->line_height) - 1; switch (keycode) { case WKC_UP: IConsoleHistoryNavigate(1); this->SetDirty(); break; case WKC_DOWN: IConsoleHistoryNavigate(-1); this->SetDirty(); break; case WKC_SHIFT | WKC_PAGEDOWN: this->Scroll(-scroll_height); break; case WKC_SHIFT | WKC_PAGEUP: this->Scroll(scroll_height); break; case WKC_SHIFT | WKC_DOWN: this->Scroll(-1); break; case WKC_SHIFT | WKC_UP: this->Scroll(1); break; case WKC_BACKQUOTE: IConsoleSwitch(); break; case WKC_RETURN: case WKC_NUM_ENTER: { /* We always want the ] at the left side; we always force these strings to be left * aligned anyway. So enforce this in all cases by addding a left-to-right marker, * otherwise it will be drawn at the wrong side with right-to-left texts. */ IConsolePrintF(CC_COMMAND, LRM "] %s", _iconsole_cmdline.buf); const char *cmd = IConsoleHistoryAdd(_iconsole_cmdline.buf); IConsoleClearCommand(); if (cmd != NULL) IConsoleCmdExec(cmd); break; } case WKC_CTRL | WKC_RETURN: _iconsole_mode = (_iconsole_mode == ICONSOLE_FULL) ? ICONSOLE_OPENED : ICONSOLE_FULL; IConsoleResize(this); MarkWholeScreenDirty(); break; #ifdef WITH_COCOA case (WKC_META | 'V'): #endif case (WKC_CTRL | 'V'): if (InsertTextBufferClipboard(&_iconsole_cmdline)) { IConsoleResetHistoryPos(); this->SetDirty(); } break; case (WKC_CTRL | 'L'): IConsoleCmdExec("clear"); break; #ifdef WITH_COCOA case (WKC_META | 'U'): #endif case (WKC_CTRL | 'U'): DeleteTextBufferAll(&_iconsole_cmdline); this->SetDirty(); break; case WKC_BACKSPACE: case WKC_DELETE: if (DeleteTextBufferChar(&_iconsole_cmdline, keycode)) { IConsoleResetHistoryPos(); this->SetDirty(); } break; case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME: if (MoveTextBufferPos(&_iconsole_cmdline, keycode)) { IConsoleResetHistoryPos(); this->SetDirty(); } break; default: if (IsValidChar(key, CS_ALPHANUMERAL)) { IConsoleWindow::scroll = 0; InsertTextBufferChar(&_iconsole_cmdline, key); IConsoleResetHistoryPos(); this->SetDirty(); } else { return ES_NOT_HANDLED; } break; } return ES_HANDLED; }
virtual void OnNewGRFsScanned() { ResetGRFConfig(false); TarScanner::DoScan(TarScanner::SCENARIO); AI::Initialize(); Game::Initialize(); /* We want the new (correct) NewGRF count to survive the loading. */ uint last_newgrf_count = _settings_client.gui.last_newgrf_count; LoadFromConfig(); _settings_client.gui.last_newgrf_count = last_newgrf_count; /* Since the default for the palette might have changed due to * reading the configuration file, recalculate that now. */ UpdateNewGRFConfigPalette(); Game::Uninitialize(true); AI::Uninitialize(true); CheckConfig(); LoadFromHighScore(); LoadHotkeysFromConfig(); WindowDesc::LoadFromConfig(); /* We have loaded the config, so we may possibly save it. */ *save_config_ptr = save_config; /* restore saved music volume */ MusicDriver::GetInstance()->SetVolume(_settings_client.music.music_vol); 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; #if defined(ENABLE_NETWORK) if (dedicated_host != NULL) { _network_bind_list.Clear(); *_network_bind_list.Append() = stredup(dedicated_host); } if (dedicated_port != 0) _settings_client.network.server_port = dedicated_port; #endif /* ENABLE_NETWORK */ /* initialize the ingame console */ IConsoleInit(); InitializeGUI(); IConsoleCmdExec("exec scripts/autoexec.scr 0"); /* Make sure _settings is filled with _settings_newgame if we switch to a game directly */ if (_switch_mode != SM_NONE) MakeNewgameSettingsLive(); #ifdef ENABLE_NETWORK if (_network_available && network_conn != NULL) { const char *port = NULL; const char *company = NULL; uint16 rport = NETWORK_DEFAULT_PORT; CompanyID join_as = COMPANY_NEW_COMPANY; ParseConnectionString(&company, &port, network_conn); if (company != NULL) { join_as = (CompanyID)atoi(company); if (join_as != COMPANY_SPECTATOR) { join_as--; if (join_as >= MAX_COMPANIES) { delete this; return; } } } if (port != NULL) rport = atoi(port); LoadIntroGame(); _switch_mode = SM_NONE; NetworkClientConnectGame(NetworkAddress(network_conn, rport), join_as, join_server_password, join_company_password); } #endif /* ENABLE_NETWORK */ /* After the scan we're not used anymore. */ delete this; }
void SwitchToMode(SwitchMode new_mode) { #ifdef ENABLE_NETWORK /* If we are saving something, the network stays in his current state */ if (new_mode != SM_SAVE_GAME) { /* If the network is active, make it not-active */ if (_networking) { if (_network_server && (new_mode == SM_LOAD_GAME || new_mode == SM_NEWGAME || new_mode == SM_RESTARTGAME)) { NetworkReboot(); } else { NetworkDisconnect(); } } /* 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 (_settings_client.network.reload_cfg) { LoadFromConfig(); MakeNewgameSettingsLive(); ResetGRFConfig(false); } NetworkServerStart(); } else { /* This client no longer wants to be a network-server */ _is_network_server = false; } } } #endif /* ENABLE_NETWORK */ /* Make sure all AI controllers are gone at quitting game */ if (new_mode != SM_SAVE_GAME) AI::KillAll(); switch (new_mode) { case SM_EDITOR: // Switch to scenario editor MakeNewEditorWorld(); break; case SM_RESTARTGAME: // Restart --> 'Random game' with current settings case SM_NEWGAME: // New Game --> 'Random game' #ifdef ENABLE_NETWORK if (_network_server) { seprintf(_network_game_info.map_name, lastof(_network_game_info.map_name), "Random Map"); } #endif /* ENABLE_NETWORK */ MakeNewGame(false, new_mode == SM_NEWGAME); break; case SM_LOAD_GAME: { // Load game, Play Scenario ResetGRFConfig(true); ResetWindowSystem(); if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.file_op, _file_to_saveload.detail_ftype, GM_NORMAL, NO_DIRECTORY)) { SetDParamStr(0, GetSaveLoadErrorString()); ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR); } else { if (_file_to_saveload.abstract_ftype == FT_SCENARIO) { /* Reset engine pool to simplify changing engine NewGRFs in scenario editor. */ EngineOverrideManager::ResetToCurrentNewGRFConfig(); } /* Update the local company for a loaded game. It is either always * company #1 (eg 0) or in the case of a dedicated server a spectator */ SetLocalCompany(_network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST); /* Execute the game-start script */ IConsoleCmdExec("exec scripts/game_start.scr 0"); /* Decrease pause counter (was increased from opening load dialog) */ DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE); #ifdef ENABLE_NETWORK if (_network_server) { seprintf(_network_game_info.map_name, lastof(_network_game_info.map_name), "%s (Loaded game)", _file_to_saveload.title); } #endif /* ENABLE_NETWORK */ } break; } case SM_START_HEIGHTMAP: // Load a heightmap and start a new game from it #ifdef ENABLE_NETWORK if (_network_server) { seprintf(_network_game_info.map_name, lastof(_network_game_info.map_name), "%s (Heightmap)", _file_to_saveload.title); } #endif /* ENABLE_NETWORK */ MakeNewGame(true, true); break; case SM_LOAD_HEIGHTMAP: // Load heightmap from scenario editor SetLocalCompany(OWNER_NONE); GenerateWorld(GWM_HEIGHTMAP, 1 << _settings_game.game_creation.map_x, 1 << _settings_game.game_creation.map_y); MarkWholeScreenDirty(); break; case SM_LOAD_SCENARIO: { // Load scenario from scenario editor if (SafeLoad(_file_to_saveload.name, _file_to_saveload.file_op, _file_to_saveload.detail_ftype, GM_EDITOR, NO_DIRECTORY)) { SetLocalCompany(OWNER_NONE); _settings_newgame.game_creation.starting_year = _cur_year; /* Cancel the saveload pausing */ DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE); } else { SetDParamStr(0, GetSaveLoadErrorString()); ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR); } break; } case SM_MENU: // Switch to game intro menu LoadIntroGame(); if (BaseSounds::ini_set == NULL && BaseSounds::GetUsedSet()->fallback) { ShowErrorMessage(STR_WARNING_FALLBACK_SOUNDSET, INVALID_STRING_ID, WL_CRITICAL); BaseSounds::ini_set = stredup(BaseSounds::GetUsedSet()->name); } break; case SM_SAVE_GAME: // Save game. /* Make network saved games on pause compatible to singleplayer */ if (SaveOrLoad(_file_to_saveload.name, SLO_SAVE, DFT_GAME_FILE, NO_DIRECTORY) != SL_OK) { SetDParamStr(0, GetSaveLoadErrorString()); ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR); } else { DeleteWindowById(WC_SAVELOAD, 0); } break; case SM_SAVE_HEIGHTMAP: // Save heightmap. MakeHeightmapScreenshot(_file_to_saveload.name); DeleteWindowById(WC_SAVELOAD, 0); break; case SM_GENRANDLAND: // Generate random land within scenario editor SetLocalCompany(OWNER_NONE); GenerateWorld(GWM_RANDOM, 1 << _settings_game.game_creation.map_x, 1 << _settings_game.game_creation.map_y); /* XXX: set date */ MarkWholeScreenDirty(); break; default: NOT_REACHED(); } }
virtual EventState OnKeyPress(WChar key, uint16 keycode) { if (_focused_window != this) return ES_NOT_HANDLED; const int scroll_height = (this->height / this->line_height) - 1; switch (keycode) { case WKC_UP: IConsoleHistoryNavigate(1); this->SetDirty(); break; case WKC_DOWN: IConsoleHistoryNavigate(-1); this->SetDirty(); break; case WKC_SHIFT | WKC_PAGEDOWN: this->Scroll(-scroll_height); break; case WKC_SHIFT | WKC_PAGEUP: this->Scroll(scroll_height); break; case WKC_SHIFT | WKC_DOWN: this->Scroll(-1); break; case WKC_SHIFT | WKC_UP: this->Scroll(1); break; case WKC_BACKQUOTE: IConsoleSwitch(); break; case WKC_RETURN: case WKC_NUM_ENTER: { /* We always want the ] at the left side; we always force these strings to be left * aligned anyway. So enforce this in all cases by addding a left-to-right marker, * otherwise it will be drawn at the wrong side with right-to-left texts. */ IConsolePrintF(CC_COMMAND, LRM "] %s", _iconsole_cmdline.buf); const char *cmd = IConsoleHistoryAdd(_iconsole_cmdline.buf); IConsoleClearCommand(); if (cmd != NULL) IConsoleCmdExec(cmd); break; } case WKC_CTRL | WKC_RETURN: _iconsole_mode = (_iconsole_mode == ICONSOLE_FULL) ? ICONSOLE_OPENED : ICONSOLE_FULL; IConsoleResize(this); MarkWholeScreenDirty(); break; case (WKC_CTRL | 'L'): IConsoleCmdExec("clear"); break; default: if (_iconsole_cmdline.HandleKeyPress(key, keycode) != HKPR_NOT_HANDLED) { IConsoleWindow::scroll = 0; IConsoleResetHistoryPos(); this->SetDirty(); } else { return ES_NOT_HANDLED; } break; } return ES_HANDLED; }
int ttd_main(int argc, char *argv[]) { int i; const char *optformat; char *musicdriver = NULL; char *sounddriver = NULL; char *videodriver = NULL; char *blitter = NULL; char *graphics_set = NULL; char *sounds_set = NULL; char *music_set = NULL; Dimension resolution = {0, 0}; Year startyear = INVALID_YEAR; uint generation_seed = GENERATE_NEW_SEED; bool save_config = true; #if defined(ENABLE_NETWORK) bool dedicated = false; bool network = false; char *network_conn = NULL; char *debuglog_conn = NULL; char *dedicated_host = NULL; uint16 dedicated_port = 0; char *join_server_password = NULL; char *join_company_password = NULL; extern bool _dedicated_forks; _dedicated_forks = false; #endif /* ENABLE_NETWORK */ _game_mode = GM_MENU; _switch_mode = SM_MENU; _switch_mode_errorstr = INVALID_STRING_ID; _config_file = NULL; /* The last param of the following function means this: * a letter means: it accepts that param (e.g.: -h) * a ':' behind it means: it need a param (e.g.: -m<driver>) * a '::' behind it means: it can optional have a param (e.g.: -d<debug>) */ optformat = "m:s:v:b:hD::n::ei::I:S:M:t:d::r:g::G:c:xl:p:P:" #if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32) "f" #endif ; MyGetOptData mgo(argc - 1, argv + 1, optformat); while ((i = MyGetOpt(&mgo)) != -1) { switch (i) { case 'I': free(graphics_set); graphics_set = strdup(mgo.opt); break; case 'S': free(sounds_set); sounds_set = strdup(mgo.opt); break; case 'M': free(music_set); music_set = strdup(mgo.opt); break; case 'm': free(musicdriver); musicdriver = strdup(mgo.opt); break; case 's': free(sounddriver); sounddriver = strdup(mgo.opt); break; case 'v': free(videodriver); videodriver = strdup(mgo.opt); break; case 'b': free(blitter); blitter = strdup(mgo.opt); break; #if defined(ENABLE_NETWORK) case 'D': free(musicdriver); free(sounddriver); free(videodriver); free(blitter); musicdriver = strdup("null"); sounddriver = strdup("null"); videodriver = strdup("dedicated"); blitter = strdup("null"); dedicated = true; SetDebugString("net=6"); if (mgo.opt != NULL) { /* Use the existing method for parsing (openttd -n). * However, we do ignore the #company part. */ const char *temp = NULL; const char *port = NULL; ParseConnectionString(&temp, &port, mgo.opt); if (!StrEmpty(mgo.opt)) dedicated_host = mgo.opt; if (port != NULL) dedicated_port = atoi(port); } break; case 'f': _dedicated_forks = true; break; case 'n': network = true; network_conn = mgo.opt; // optional IP parameter, NULL if unset break; case 'l': debuglog_conn = mgo.opt; break; case 'p': join_server_password = mgo.opt; break; case 'P': join_company_password = mgo.opt; break; #endif /* ENABLE_NETWORK */ case 'r': ParseResolution(&resolution, mgo.opt); break; case 't': startyear = atoi(mgo.opt); break; case 'd': { #if defined(WIN32) CreateConsole(); #endif if (mgo.opt != NULL) SetDebugString(mgo.opt); break; } case 'e': _switch_mode = SM_EDITOR; break; case 'i': /* there is an argument, it is not empty, and it is exactly 1 char long */ if (!StrEmpty(mgo.opt) && mgo.opt[1] == '\0') { _use_palette = (PaletteType)(mgo.opt[0] - '0'); if (_use_palette <= MAX_PAL) break; } usererror("Valid value for '-i' is 0, 1 or 2"); case 'g': if (mgo.opt != NULL) { strecpy(_file_to_saveload.name, mgo.opt, lastof(_file_to_saveload.name)); _switch_mode = SM_LOAD; _file_to_saveload.mode = SL_LOAD; /* if the file doesn't exist or it is not a valid savegame, let the saveload code show an error */ const char *t = strrchr(_file_to_saveload.name, '.'); if (t != NULL) { FiosType ft = FiosGetSavegameListCallback(SLD_LOAD_GAME, _file_to_saveload.name, t, NULL, NULL); if (ft != FIOS_TYPE_INVALID) SetFiosType(ft); } break; } _switch_mode = SM_NEWGAME; /* Give a random map if no seed has been given */ if (generation_seed == GENERATE_NEW_SEED) { generation_seed = InteractiveRandom(); } break; case 'G': generation_seed = atoi(mgo.opt); break; case 'c': _config_file = strdup(mgo.opt); break; case 'x': save_config = false; break; case -2: case 'h': /* The next two functions are needed to list the graphics sets. * We can't do them earlier because then we can't show it on * the debug console as that hasn't been configured yet. */ DeterminePaths(argv[0]); BaseGraphics::FindSets(); BaseSounds::FindSets(); BaseMusic::FindSets(); ShowHelp(); return 0; } } #if defined(WINCE) && defined(_DEBUG) /* Switch on debug lvl 4 for WinCE if Debug release, as you can't give params, and you most likely do want this information */ SetDebugString("4"); #endif DeterminePaths(argv[0]); BaseGraphics::FindSets(); BaseSounds::FindSets(); BaseMusic::FindSets(); #if defined(ENABLE_NETWORK) && defined(UNIX) && !defined(__MORPHOS__) /* We must fork here, or we'll end up without some resources we need (like sockets) */ if (_dedicated_forks) DedicatedFork(); #endif TarScanner::DoScan(); AI::Initialize(); LoadFromConfig(); AI::Uninitialize(true); CheckConfig(); LoadFromHighScore(); LoadHotkeysFromConfig(); if (resolution.width != 0) { _cur_resolution = resolution; } 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 and width times * height times bytes per pixel must still fit within a 32 bits * integer, even for 32 bpp video modes. This way all internal * drawing routines work correctly. */ _cur_resolution.width = ClampU(_cur_resolution.width, 1, UINT16_MAX / 2); _cur_resolution.height = ClampU(_cur_resolution.height, 1, UINT16_MAX / 2); #if defined(ENABLE_NETWORK) if (dedicated) DEBUG(net, 0, "Starting dedicated version %s", _openttd_revision); if (dedicated_host) { _network_bind_list.Clear(); *_network_bind_list.Append() = strdup(dedicated_host); } if (dedicated_port) _settings_client.network.server_port = dedicated_port; if (_dedicated_forks && !dedicated) _dedicated_forks = false; #endif /* ENABLE_NETWORK */ /* enumerate language files */ InitializeLanguagePacks(); /* initialize screenshot formats */ InitializeScreenshotFormats(); /* Initialize FreeType */ InitFreeType(); /* This must be done early, since functions use the SetWindowDirty* calls */ InitWindowSystem(); /* Look for the sounds before the graphics. Otherwise none would be set and * the first initialisation of the video happens on the wrong data. Now it * can do the first initialisation right. */ if (sounds_set == NULL && BaseSounds::ini_set != NULL) sounds_set = strdup(BaseSounds::ini_set); if (!BaseSounds::SetSet(sounds_set)) { StrEmpty(sounds_set) ? usererror("Failed to find a sounds set. Please acquire a sounds set for OpenTTD. See section 4.1 of readme.txt.") : usererror("Failed to select requested sounds set '%s'", sounds_set); } free(sounds_set); if (graphics_set == NULL && BaseGraphics::ini_set != NULL) graphics_set = strdup(BaseGraphics::ini_set); if (!BaseGraphics::SetSet(graphics_set)) { StrEmpty(graphics_set) ? usererror("Failed to find a graphics set. Please acquire a graphics set for OpenTTD. See section 4.1 of readme.txt.") : usererror("Failed to select requested graphics set '%s'", graphics_set); } free(graphics_set); if (music_set == NULL && BaseMusic::ini_set != NULL) music_set = strdup(BaseMusic::ini_set); if (!BaseMusic::SetSet(music_set)) { StrEmpty(music_set) ? usererror("Failed to find a music set. Please acquire a music set for OpenTTD. See section 4.1 of readme.txt.") : usererror("Failed to select requested music set '%s'", music_set); } free(music_set); /* Initialize game palette */ GfxInitPalettes(); DEBUG(misc, 1, "Loading blitter..."); if (blitter == NULL && _ini_blitter != NULL) blitter = strdup(_ini_blitter); if (BlitterFactoryBase::SelectBlitter(blitter) == NULL) { StrEmpty(blitter) ? usererror("Failed to autoprobe blitter") : usererror("Failed to select requested blitter '%s'; does it exist?", blitter); } free(blitter); DEBUG(driver, 1, "Loading drivers..."); if (sounddriver == NULL && _ini_sounddriver != NULL) sounddriver = strdup(_ini_sounddriver); _sound_driver = (SoundDriver*)SoundDriverFactoryBase::SelectDriver(sounddriver, Driver::DT_SOUND); if (_sound_driver == NULL) { StrEmpty(sounddriver) ? usererror("Failed to autoprobe sound driver") : usererror("Failed to select requested sound driver '%s'", sounddriver); } free(sounddriver); if (videodriver == NULL && _ini_videodriver != NULL) videodriver = strdup(_ini_videodriver); _video_driver = (VideoDriver*)VideoDriverFactoryBase::SelectDriver(videodriver, Driver::DT_VIDEO); if (_video_driver == NULL) { StrEmpty(videodriver) ? usererror("Failed to autoprobe video driver") : usererror("Failed to select requested video driver '%s'", videodriver); } free(videodriver); if (musicdriver == NULL && _ini_musicdriver != NULL) musicdriver = strdup(_ini_musicdriver); _music_driver = (MusicDriver*)MusicDriverFactoryBase::SelectDriver(musicdriver, Driver::DT_MUSIC); if (_music_driver == NULL) { StrEmpty(musicdriver) ? usererror("Failed to autoprobe music driver") : usererror("Failed to select requested music driver '%s'", musicdriver); } free(musicdriver); /* Initialize the zoom level of the screen to normal */ _screen.zoom = ZOOM_LVL_NORMAL; /* restore saved music volume */ _music_driver->SetVolume(_msf.music_vol); NetworkStartUp(); // initialize network-core #if defined(ENABLE_NETWORK) if (debuglog_conn != NULL && _network_available) { const char *not_used = NULL; const char *port = NULL; uint16 rport; rport = NETWORK_DEFAULT_DEBUGLOG_PORT; ParseConnectionString(¬_used, &port, debuglog_conn); if (port != NULL) rport = atoi(port); NetworkStartDebugLog(NetworkAddress(debuglog_conn, rport)); } #endif /* ENABLE_NETWORK */ ScanNewGRFFiles(); ResetGRFConfig(false); /* Make sure _settings is filled with _settings_newgame if we switch to a game directly */ if (_switch_mode != SM_NONE) MakeNewgameSettingsLive(); /* initialize the ingame console */ IConsoleInit(); _cursor.in_window = true; InitializeGUI(); IConsoleCmdExec("exec scripts/autoexec.scr 0"); /* Take our initial lock on whatever we might want to do! */ _genworld_paint_mutex->BeginCritical(); _genworld_mapgen_mutex->BeginCritical(); GenerateWorld(GWM_EMPTY, 64, 64); // Make the viewport initialization happy WaitTillGeneratedWorld(); CheckForMissingGlyphsInLoadedLanguagePack(); #ifdef ENABLE_NETWORK if (network && _network_available) { if (network_conn != NULL) { const char *port = NULL; const char *company = NULL; uint16 rport = NETWORK_DEFAULT_PORT; CompanyID join_as = COMPANY_NEW_COMPANY; ParseConnectionString(&company, &port, network_conn); if (company != NULL) { join_as = (CompanyID)atoi(company); if (join_as != COMPANY_SPECTATOR) { join_as--; if (join_as >= MAX_COMPANIES) return false; } } if (port != NULL) rport = atoi(port); LoadIntroGame(); _switch_mode = SM_NONE; NetworkClientConnectGame(NetworkAddress(network_conn, rport), join_as, join_server_password, join_company_password); } } #endif /* ENABLE_NETWORK */ _video_driver->MainLoop(); WaitTillSaved(); /* only save config if we have to */ if (save_config) { SaveToConfig(); SaveHotkeysToConfig(); SaveToHighScore(); } /* Reset windowing system, stop drivers, free used memory, ... */ ShutdownGame(); free(const_cast<char *>(BaseGraphics::ini_set)); free(const_cast<char *>(BaseSounds::ini_set)); free(const_cast<char *>(BaseMusic::ini_set)); free(_ini_musicdriver); free(_ini_sounddriver); free(_ini_videodriver); free(_ini_blitter); return 0; }
/** * An alias is just another name for a command, or for more commands * Execute it as well. * @param *alias is the alias of the command * @param tokencount the number of parameters passed * @param *tokens are the parameters given to the original command (0 is the first param) */ static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char *tokens[ICON_TOKEN_COUNT]) { char alias_buffer[ICON_MAX_STREAMSIZE] = { '\0' }; char *alias_stream = alias_buffer; DEBUG(console, 6, "Requested command is an alias; parsing..."); for (const char *cmdptr = alias->cmdline; *cmdptr != '\0'; cmdptr++) { switch (*cmdptr) { case '\'': // ' will double for "" alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer)); break; case ';': // Cmd separator; execute previous and start new command IConsoleCmdExec(alias_buffer); alias_stream = alias_buffer; *alias_stream = '\0'; // Make sure the new command is terminated. cmdptr++; break; case '%': // Some or all parameters cmdptr++; switch (*cmdptr) { case '+': { // All parameters separated: "[param 1]" "[param 2]" for (uint i = 0; i != tokencount; i++) { if (i != 0) alias_stream = strecpy(alias_stream, " ", lastof(alias_buffer)); alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer)); alias_stream = strecpy(alias_stream, tokens[i], lastof(alias_buffer)); alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer)); } break; } case '!': { // Merge the parameters to one: "[param 1] [param 2] [param 3...]" alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer)); for (uint i = 0; i != tokencount; i++) { if (i != 0) alias_stream = strecpy(alias_stream, " ", lastof(alias_buffer)); alias_stream = strecpy(alias_stream, tokens[i], lastof(alias_buffer)); } alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer)); break; } default: { // One specific parameter: %A = [param 1] %B = [param 2] ... int param = *cmdptr - 'A'; if (param < 0 || param >= tokencount) { IConsoleError("too many or wrong amount of parameters passed to alias, aborting"); IConsolePrintF(CC_WARNING, "Usage of alias '%s': %s", alias->name, alias->cmdline); return; } alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer)); alias_stream = strecpy(alias_stream, tokens[param], lastof(alias_buffer)); alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer)); break; } } break; default: *alias_stream++ = *cmdptr; *alias_stream = '\0'; break; } if (alias_stream >= lastof(alias_buffer) - 1) { IConsoleError("Requested alias execution would overflow execution buffer"); return; } } IConsoleCmdExec(alias_buffer); }
/** * An alias is just another name for a command, or for more commands * Execute it as well. * @param *alias is the alias of the command * @param tokencount the number of parameters passed * @param *tokens are the parameters given to the original command (0 is the first param) */ static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char *tokens[ICON_TOKEN_COUNT]) { const char *cmdptr; char *aliases[ICON_MAX_ALIAS_LINES], aliasstream[ICON_MAX_STREAMSIZE]; uint i; uint a_index, astream_i; memset(&aliases, 0, sizeof(aliases)); memset(&aliasstream, 0, sizeof(aliasstream)); DEBUG(console, 6, "Requested command is an alias; parsing..."); aliases[0] = aliasstream; for (cmdptr = alias->cmdline, a_index = 0, astream_i = 0; *cmdptr != '\0'; cmdptr++) { if (a_index >= lengthof(aliases) || astream_i >= lengthof(aliasstream)) break; switch (*cmdptr) { case '\'': // ' will double for "" aliasstream[astream_i++] = '"'; break; case ';': // Cmd seperator, start new command aliasstream[astream_i] = '\0'; aliases[++a_index] = &aliasstream[++astream_i]; cmdptr++; break; case '%': // Some or all parameters cmdptr++; switch (*cmdptr) { case '+': { // All parameters seperated: "[param 1]" "[param 2]" for (i = 0; i != tokencount; i++) { aliasstream[astream_i++] = '"'; astream_i += IConsoleCopyInParams(&aliasstream[astream_i], tokens[i], astream_i); aliasstream[astream_i++] = '"'; aliasstream[astream_i++] = ' '; } } break; case '!': { // Merge the parameters to one: "[param 1] [param 2] [param 3...]" aliasstream[astream_i++] = '"'; for (i = 0; i != tokencount; i++) { astream_i += IConsoleCopyInParams(&aliasstream[astream_i], tokens[i], astream_i); aliasstream[astream_i++] = ' '; } aliasstream[astream_i++] = '"'; } break; default: { // One specific parameter: %A = [param 1] %B = [param 2] ... int param = *cmdptr - 'A'; if (param < 0 || param >= tokencount) { IConsoleError("too many or wrong amount of parameters passed to alias, aborting"); IConsolePrintF(CC_WARNING, "Usage of alias '%s': %s", alias->name, alias->cmdline); return; } aliasstream[astream_i++] = '"'; astream_i += IConsoleCopyInParams(&aliasstream[astream_i], tokens[param], astream_i); aliasstream[astream_i++] = '"'; } break; } break; default: aliasstream[astream_i++] = *cmdptr; break; } } for (i = 0; i <= a_index; i++) IConsoleCmdExec(aliases[i]); // execute each alias in turn }
int ttd_main(int argc, char *argv[]) { #ifdef EMSCRIPTEN char* home = "/home/caiiiycuk/play-ttd/etc/preload/"; argv[0] = home; #endif char *musicdriver = NULL; char *sounddriver = NULL; char *videodriver = NULL; char *blitter = NULL; char *graphics_set = NULL; char *sounds_set = NULL; char *music_set = NULL; Dimension resolution = {0, 0}; /* AfterNewGRFScan sets save_config to true after scanning completed. */ bool save_config = false; AfterNewGRFScan *scanner = new AfterNewGRFScan(&save_config); #if defined(ENABLE_NETWORK) bool dedicated = false; char *debuglog_conn = NULL; extern bool _dedicated_forks; _dedicated_forks = false; #endif /* ENABLE_NETWORK */ _game_mode = GM_MENU; _switch_mode = SM_MENU; _config_file = NULL; GetOptData mgo(argc - 1, argv + 1, _options); int i; while ((i = mgo.GetOpt()) != -1) { switch (i) { case 'I': free(graphics_set); graphics_set = strdup(mgo.opt); break; case 'S': free(sounds_set); sounds_set = strdup(mgo.opt); break; case 'M': free(music_set); music_set = strdup(mgo.opt); break; case 'm': free(musicdriver); musicdriver = strdup(mgo.opt); break; case 's': free(sounddriver); sounddriver = strdup(mgo.opt); break; case 'v': free(videodriver); videodriver = strdup(mgo.opt); break; case 'b': free(blitter); blitter = strdup(mgo.opt); break; #if defined(ENABLE_NETWORK) case 'D': free(musicdriver); free(sounddriver); free(videodriver); free(blitter); musicdriver = strdup("null"); sounddriver = strdup("null"); videodriver = strdup("dedicated"); blitter = strdup("null"); dedicated = true; SetDebugString("net=6"); if (mgo.opt != NULL) { /* Use the existing method for parsing (openttd -n). * However, we do ignore the #company part. */ const char *temp = NULL; const char *port = NULL; ParseConnectionString(&temp, &port, mgo.opt); if (!StrEmpty(mgo.opt)) scanner->dedicated_host = mgo.opt; if (port != NULL) scanner->dedicated_port = atoi(port); } break; case 'f': _dedicated_forks = true; break; case 'n': scanner->network_conn = mgo.opt; // optional IP parameter, NULL if unset break; case 'l': debuglog_conn = mgo.opt; break; case 'p': scanner->join_server_password = mgo.opt; break; case 'P': scanner->join_company_password = mgo.opt; break; #endif /* ENABLE_NETWORK */ case 'r': ParseResolution(&resolution, mgo.opt); break; case 't': scanner->startyear = atoi(mgo.opt); break; case 'd': { #if defined(WIN32) CreateConsole(); #endif if (mgo.opt != NULL) SetDebugString(mgo.opt); break; } case 'e': _switch_mode = (_switch_mode == SM_LOAD_GAME || _switch_mode == SM_LOAD_SCENARIO ? SM_LOAD_SCENARIO : SM_EDITOR); break; case 'g': if (mgo.opt != NULL) { strecpy(_file_to_saveload.name, mgo.opt, lastof(_file_to_saveload.name)); _switch_mode = (_switch_mode == SM_EDITOR || _switch_mode == SM_LOAD_SCENARIO ? SM_LOAD_SCENARIO : SM_LOAD_GAME); _file_to_saveload.mode = SL_LOAD; /* if the file doesn't exist or it is not a valid savegame, let the saveload code show an error */ const char *t = strrchr(_file_to_saveload.name, '.'); if (t != NULL) { FiosType ft = FiosGetSavegameListCallback(SLD_LOAD_GAME, _file_to_saveload.name, t, NULL, NULL); if (ft != FIOS_TYPE_INVALID) SetFiosType(ft); } break; } _switch_mode = SM_NEWGAME; /* Give a random map if no seed has been given */ if (scanner->generation_seed == GENERATE_NEW_SEED) { scanner->generation_seed = InteractiveRandom(); } break; case 'q': { DeterminePaths(argv[0]); if (StrEmpty(mgo.opt)) return 1; char title[80]; title[0] = '\0'; FiosGetSavegameListCallback(SLD_LOAD_GAME, mgo.opt, strrchr(mgo.opt, '.'), title, lastof(title)); _load_check_data.Clear(); SaveOrLoadResult res = SaveOrLoad(mgo.opt, SL_LOAD_CHECK, SAVE_DIR, false); if (res != SL_OK || _load_check_data.HasErrors()) { fprintf(stderr, "Failed to open savegame\n"); if (_load_check_data.HasErrors()) { char buf[256]; SetDParamStr(0, _load_check_data.error_data); GetString(buf, _load_check_data.error, lastof(buf)); fprintf(stderr, "%s\n", buf); } return 1; } WriteSavegameInfo(title); return 0; } case 'G': scanner->generation_seed = atoi(mgo.opt); break; case 'c': _config_file = strdup(mgo.opt); break; case 'x': scanner->save_config = false; break; case 'h': i = -2; // Force printing of help. break; } if (i == -2) break; } if (i == -2 || mgo.numleft > 0) { /* Either the user typed '-h', he made an error, or he added unrecognized command line arguments. * In all cases, print the help, and exit. * * The next two functions are needed to list the graphics sets. We can't do them earlier * because then we cannot show it on the debug console as that hasn't been configured yet. */ DeterminePaths(argv[0]); TarScanner::DoScan(TarScanner::BASESET); BaseGraphics::FindSets(); BaseSounds::FindSets(); BaseMusic::FindSets(); ShowHelp(); delete scanner; return 0; } #if defined(WINCE) && defined(_DEBUG) /* Switch on debug lvl 4 for WinCE if Debug release, as you can't give params, and you most likely do want this information */ SetDebugString("4"); #endif DeterminePaths(argv[0]); TarScanner::DoScan(TarScanner::BASESET); #if defined(ENABLE_NETWORK) if (dedicated) DEBUG(net, 0, "Starting dedicated version %s", _openttd_revision); if (_dedicated_forks && !dedicated) _dedicated_forks = false; #if defined(UNIX) && !defined(__MORPHOS__) /* We must fork here, or we'll end up without some resources we need (like sockets) */ if (_dedicated_forks) DedicatedFork(); #endif #endif LoadFromConfig(true); if (resolution.width != 0) _cur_resolution = resolution; /* * The width and height must be at least 1 pixel and width times * height times bytes per pixel must still fit within a 32 bits * integer, even for 32 bpp video modes. This way all internal * drawing routines work correctly. */ _cur_resolution.width = ClampU(_cur_resolution.width, 1, UINT16_MAX / 2); _cur_resolution.height = ClampU(_cur_resolution.height, 1, UINT16_MAX / 2); /* Assume the cursor starts within the game as not all video drivers * get an event that the cursor is within the window when it is opened. * Saying the cursor is there makes no visible difference as it would * just be out of the bounds of the window. */ _cursor.in_window = true; /* enumerate language files */ InitializeLanguagePacks(); /* Initialize the regular font for FreeType */ InitFreeType(false); /* This must be done early, since functions use the SetWindowDirty* calls */ InitWindowSystem(); BaseGraphics::FindSets(); if (graphics_set == NULL && BaseGraphics::ini_set != NULL) graphics_set = strdup(BaseGraphics::ini_set); if (!BaseGraphics::SetSet(graphics_set)) { if (!StrEmpty(graphics_set)) { BaseGraphics::SetSet(NULL); ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_BASE_GRAPHICS_NOT_FOUND); msg.SetDParamStr(0, graphics_set); ScheduleErrorMessage(msg); } } free(graphics_set); /* Initialize game palette */ GfxInitPalettes(); DEBUG(misc, 1, "Loading blitter..."); if (blitter == NULL && _ini_blitter != NULL) blitter = strdup(_ini_blitter); _blitter_autodetected = StrEmpty(blitter); /* If we have a 32 bpp base set, try to select the 32 bpp blitter first, but only if we autoprobe the blitter. */ if (!_blitter_autodetected || BaseGraphics::GetUsedSet() == NULL || BaseGraphics::GetUsedSet()->blitter == BLT_8BPP || BlitterFactoryBase::SelectBlitter("32bpp-anim") == NULL) { if (BlitterFactoryBase::SelectBlitter(blitter) == NULL) { StrEmpty(blitter) ? usererror("Failed to autoprobe blitter") : usererror("Failed to select requested blitter '%s'; does it exist?", blitter); } } free(blitter); if (videodriver == NULL && _ini_videodriver != NULL) videodriver = strdup(_ini_videodriver); _video_driver = (VideoDriver*)VideoDriverFactoryBase::SelectDriver(videodriver, Driver::DT_VIDEO); if (_video_driver == NULL) { StrEmpty(videodriver) ? usererror("Failed to autoprobe video driver") : usererror("Failed to select requested video driver '%s'", videodriver); } free(videodriver); /* Initialize the zoom level of the screen to normal */ _screen.zoom = ZOOM_LVL_NORMAL; NetworkStartUp(); // initialize network-core #if defined(ENABLE_NETWORK) if (debuglog_conn != NULL && _network_available) { const char *not_used = NULL; const char *port = NULL; uint16 rport; rport = NETWORK_DEFAULT_DEBUGLOG_PORT; ParseConnectionString(¬_used, &port, debuglog_conn); if (port != NULL) rport = atoi(port); NetworkStartDebugLog(NetworkAddress(debuglog_conn, rport)); } #endif /* ENABLE_NETWORK */ if (!HandleBootstrap()) { abort(); } _video_driver->ClaimMousePointer(); /* initialize screenshot formats */ InitializeScreenshotFormats(); BaseSounds::FindSets(); if (sounds_set == NULL && BaseSounds::ini_set != NULL) sounds_set = strdup(BaseSounds::ini_set); if (!BaseSounds::SetSet(sounds_set)) { if (StrEmpty(sounds_set) || !BaseSounds::SetSet(NULL)) { usererror("Failed to find a sounds set. Please acquire a sounds set for OpenTTD. See section 4.1 of readme.txt."); } else { ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_BASE_SOUNDS_NOT_FOUND); msg.SetDParamStr(0, sounds_set); ScheduleErrorMessage(msg); } } free(sounds_set); BaseMusic::FindSets(); if (music_set == NULL && BaseMusic::ini_set != NULL) music_set = strdup(BaseMusic::ini_set); if (!BaseMusic::SetSet(music_set)) { if (StrEmpty(music_set) || !BaseMusic::SetSet(NULL)) { usererror("Failed to find a music set. Please acquire a music set for OpenTTD. See section 4.1 of readme.txt."); } else { ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_BASE_MUSIC_NOT_FOUND); msg.SetDParamStr(0, music_set); ScheduleErrorMessage(msg); } } free(music_set); if (sounddriver == NULL && _ini_sounddriver != NULL) sounddriver = strdup(_ini_sounddriver); _sound_driver = (SoundDriver*)SoundDriverFactoryBase::SelectDriver(sounddriver, Driver::DT_SOUND); if (_sound_driver == NULL) { StrEmpty(sounddriver) ? usererror("Failed to autoprobe sound driver") : usererror("Failed to select requested sound driver '%s'", sounddriver); } free(sounddriver); if (musicdriver == NULL && _ini_musicdriver != NULL) musicdriver = strdup(_ini_musicdriver); _music_driver = (MusicDriver*)MusicDriverFactoryBase::SelectDriver(musicdriver, Driver::DT_MUSIC); if (_music_driver == NULL) { StrEmpty(musicdriver) ? usererror("Failed to autoprobe music driver") : usererror("Failed to select requested music driver '%s'", musicdriver); } free(musicdriver); /* Take our initial lock on whatever we might want to do! */ _modal_progress_paint_mutex->BeginCritical(); _modal_progress_work_mutex->BeginCritical(); GenerateWorld(GWM_EMPTY, 64, 64); // Make the viewport initialization happy WaitTillGeneratedWorld(); LoadIntroGame(false); CheckForMissingGlyphs(); ScanNewGRFFiles(scanner); const char *script = getStartupScript(); IConsoleCmdExec(script); _video_driver->MainLoop(); // WaitTillSaved(); // // /* only save config if we have to */ // if (save_config) { // SaveToConfig(); // SaveHotkeysToConfig(); // SaveToHighScore(); // } // exit: // /* Reset windowing system, stop drivers, free used memory, ... */ // ShutdownGame(); // // free(BaseGraphics::ini_set); // free(BaseSounds::ini_set); // free(BaseMusic::ini_set); // free(_ini_musicdriver); // free(_ini_sounddriver); // free(_ini_videodriver); // free(_ini_blitter); // return 0; }