# openttd.cpp File Reference

Functions related to starting OpenTTD. More...

`#include "stdafx.h"`
`#include "variables.h"`
`#include "blitter/factory.hpp"`
`#include "sound/sound_driver.hpp"`
`#include "music/music_driver.hpp"`
`#include "video/video_driver.hpp"`
`#include "fontcache.h"`
`#include "gui.h"`
`#include "sound_func.h"`
`#include "window_func.h"`
`#include "base_media_base.h"`
`#include "saveload/saveload.h"`
`#include "landscape.h"`
`#include "company_func.h"`
`#include "command_func.h"`
`#include "news_func.h"`
`#include "fios.h"`
`#include "aircraft.h"`
`#include "roadveh.h"`
`#include "train.h"`
`#include "console_func.h"`
`#include "screenshot.h"`
`#include "network/network.h"`
`#include "network/network_func.h"`
`#include "signs_base.h"`
`#include "ai/ai.hpp"`
`#include "ai/ai_config.hpp"`
`#include "settings_func.h"`
`#include "genworld.h"`
`#include "group.h"`
`#include "strings_func.h"`
`#include "date_func.h"`
`#include "vehicle_func.h"`
`#include "gamelog.h"`
`#include "cheat_type.h"`
`#include "animated_tile_func.h"`
`#include "roadstop_base.h"`
`#include "functions.h"`
`#include "elrail_func.h"`
`#include "rev.h"`
`#include "highscore.h"`
`#include "thread/thread.h"`
`#include "station_base.h"`
`#include "crashlog.h"`
`#include "company_base.h"`
`#include "engine_base.h"`
`#include "engine_func.h"`
`#include "core/random_func.hpp"`
`#include "newgrf_commons.h"`
`#include "town.h"`
`#include "industry.h"`
`#include <stdarg.h>`
`#include "table/strings.h"`

Go to the source code of this file.

## Data Structures
struct | MyGetOptData
## Functions
void | CallLandscapeTick ()
void | IncreaseDate ()
| Increases the tick counter, increases date and possibly calls procedures that have to be called daily, monthly or yearly.
void | DoPaletteAnimations ()
void | MusicLoop ()
void | ResetMusic ()
void | ProcessAsyncSaveFinish ()
| Handle async save finishes.
void | CallWindowTickEvent ()
| Dispatch WE_TICK event over all windows.
void | SetDifficultyLevel (int mode, DifficultySettings *gm_opt)
Company * | DoStartupNewCompany (bool is_ai, CompanyID company=INVALID_COMPANY)
| Create a new company and sets all company variables default values.
void | ShowOSErrorBox (const char *buf, bool system)
void | InitializeRailGUI ()
| Resets the rail GUI - sets default railtype to build and resets the signal GUI.
void CDECL | usererror (const char *s,...)
| Error handling for fatal user errors.
void CDECL | error (const char *s,...)
| Error handling for fatal non-user errors.
void CDECL | ShowInfoF (const char *str,...)
| Shows some information on the console/a popup box depending on the OS.
static void | ShowHelp ()
| Show the help message when someone passed a wrong parameter.
static int | MyGetOpt (MyGetOptData *md)
static void | ParseResolution (Dimension *res, const char *s)
| Extract the resolution from the given string and store it in the 'res' parameter.
static void | InitializeDynamicVariables ()
static void | ShutdownGame ()
| Unitializes drivers, frees allocated memory, cleans pools, .
static void | LoadIntroGame ()
void | MakeNewgameSettingsLive ()
int | ttd_main (int argc, char *argv[])
void | HandleExitGameRequest ()
static void | MakeNewGameDone ()
static void | MakeNewGame (bool from_heightmap, bool reset_settings)
static void | MakeNewEditorWorldDone ()
static void | MakeNewEditorWorld ()
void | StartupCompanies ()
void | StartupDisasters ()
void | StartupEconomy ()
bool | SafeSaveOrLoad (const char *filename, int mode, GameMode newgm, Subdirectory subdir)
| Load the specified savegame but on error do different things.
static void | StartScenario ()
| Start Scenario starts a new game based on a scenario.
void | SwitchToMode (SwitchMode new_mode)
static void | CheckCaches ()
| Check the validity of some of the caches.
void | StateGameLoop ()
| State controlling game loop.
static void | DoAutosave ()
| Create an autosave.
void | GameLoop ()
## Variables
StringID | _switch_mode_errorstr
byte | _savegame_sort_order

## Detailed Description

Functions related to starting OpenTTD.

Definition in file openttd.cpp.

## Function Documentation

static void CheckCaches | ( |  | ) | `[static]`

Check the validity of some of the caches.

Especially in the sense of desyncs between the cached value and what the value would be when calculated from the 'base' data.

Definition at line 1116 of file openttd.cpp.

References Aircraft::acache, GoodsEntry::cargo, Vehicle::cargo, Train::ConsistChanged(), DIAGDIR_NE, DIAGDIR_NW, Vehicle::First(), SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::From(), SpecializedVehicle< RoadVehicle, VEH_ROAD >::From(), SpecializedVehicle< Train, VEH_TRAIN >::From(), RoadStop::GetEntry(), Station::goods, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, CargoList< Tinst >::InvalidateCache(), VehicleCargoList::InvalidateCache(), Vehicle::IsPrimaryVehicle(), IsStandardRoadStopTile(), Vehicle::Next(), NUM_CARGO, RoadVehicle::rcache, BaseVehicle::type, UpdateAircraftCache(), VEH_AIRCRAFT, VEH_ROAD, VEH_TRAIN, Vehicle::vehstatus, and RoadStop::xy.

Referenced by StateGameLoop().

static void DoAutosave | ( |  | ) | `[static]`

Create an autosave.

The default name is "autosave#.sav". However with the setting 'keep_all_autosave' the name defaults to company-name + date

Definition at line 1277 of file openttd.cpp.

References _networking, _settings_client, AUTOSAVE_DIR, GenerateDefaultSaveName(), ClientSettings::gui, INVALID_STRING_ID, GUISettings::keep_all_autosave, lastof, GUISettings::max_num_autosaves, SaveOrLoad(), ShowErrorMessage(), SL_OK, and strecat().

Company* DoStartupNewCompany | ( | bool | is_ai,
|  | CompanyID | company = `INVALID_COMPANY` |
| ) |  |  |

Create a new company and sets all company variables default values.

Parameters:

  | is_ai | is a ai company?
  | company | CompanyID to use for the new company

Returns:
  the company struct

Definition at line 464 of file company_cmd.cpp.

void CDECL error | ( | const char * | s,
|  |  | ... |
| ) |  |  |

Error handling for fatal non-user errors.

Parameters:

  | s | the string to print.

Note:
  Does NEVER return.

Definition at line 117 of file openttd.cpp.

References lengthof, and CrashLog::SetErrorMessage().

Referenced by AIInfo::AddLabels(), AIInfo::AddSetting(), AllocateMap(), ScriptFileInfo::CheckMethod(), CheckTrainAttachment(), CmdStartStopVehicle(), DoCommand(), BaseMedia< Tbase_set >::GetSet(), GetStringWithArgs(), AIScanner::ImportLibrary(), LoadFreeTypeFont(), MallocError(), NetworkExecuteLocalCommandQueue(), ReallocError(), and strecpy().

static void ParseResolution | ( | Dimension * | res,
|  | const char * | s |
| ) |  |  | `[static]`

Extract the resolution from the given string and store it in the 'res' parameter.

Parameters:

  | res | variable to store the resolution in.
  | s | the string to decompose.

Definition at line 297 of file openttd.cpp.

References max(), and ShowInfoF().

bool SafeSaveOrLoad | ( | const char * | filename,
|  | int | mode,
|  | GameMode | newgm,
|  | Subdirectory | subdir |
| ) |  |  |

Load the specified savegame but on error do different things.

If loading fails due to corrupt savegame, bad version, etc. go back to a previous correct state. In the menu for example load the intro game again.

Parameters:

  | filename | file to be loaded
  | mode | mode of loading, either SL_LOAD or SL_OLD_LOAD
  | newgm | switch to this mode of loading fails due to some unknown error
  | subdir | default directory to look for filename, set to 0 if not needed

Definition at line 872 of file openttd.cpp.

References _network_dedicated, _network_server, NetworkDisconnect(), SaveOrLoad(), SL_OK, and SL_REINIT.

Referenced by StartScenario().

void CDECL ShowInfoF | ( | const char * | str,
|  |  | ... |
| ) |  |  |

Shows some information on the console/a popup box depending on the OS.

Parameters:

  | str | the text to show.

Definition at line 137 of file openttd.cpp.

References lengthof.

Referenced by CheckExternalFiles(), GetFontByFaceName(), GetKeyboardLayout(), GetSavegameFormat(), ini_load_settings(), LoadFreeTypeFont(), IniFile::LoadFromDisk(), ParseResolution(), and string_to_val().

static void ShutdownGame | ( |  | ) | `[static]`

Unitializes drivers, frees allocated memory, cleans pools, .

.. Generally, prepares the game for shutting down

Definition at line 322 of file openttd.cpp.

References _cargopacket_pool, _network_available, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::CleanPool(), GamelogReset(), NetworkShutDown(), DriverFactoryBase::ShutdownDrivers(), AI::Uninitialize(), and UnInitWindowSystem().

static void StartScenario | ( |  | ) | `[static]`

Start Scenario starts a new game based on a scenario.

Eg 'New Game' --> select a preset scenario This starts a scenario based on your current difficulty settings

Definition at line 918 of file openttd.cpp.

References _settings_client, _settings_game, _settings_newgame, ClientSettings::company, COMPANY_FIRST, GameSettings::difficulty, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), GetSaveLoadErrorString(), INVALID_STRING_ID, MarkWholeScreenDirty(), SmallFiosItem::mode, SmallFiosItem::name, ResetWindowSystem(), SafeSaveOrLoad(), SCENARIO_DIR, SetDParamStr(), SetLocalCompany(), Company::settings, and ShowErrorMessage().

void StateGameLoop | ( |  | ) |

State controlling game loop.

The state must not be changed from anywhere but here. That check is enforced in DoCommand.

Definition at line 1226 of file openttd.cpp.

References _date, _pause_mode, _settings_game, AnimateAnimatedTiles(), AUTOSAVE_DIR, CallWindowTickEvent(), CheckCaches(), ClearStorageChanges(), GameSettings::game_creation, AI::GameLoop(), GameCreationSettings::generation_seed, IncreaseDate(), IsGeneratingWorld(), lengthof, OWNER_NONE, PM_UNPAUSED, and SaveOrLoad().

void CDECL usererror | ( | const char * | s,
|  |  | ... |
| ) |  |  |

Error handling for fatal user errors.

Parameters:

  | s | the string to print.

Note:
  Does NEVER return.

Definition at line 97 of file openttd.cpp.

References lengthof.

Referenced by HandleInvalidSpriteRequest(), InitializeLanguagePacks(), and DriverFactoryBase::SelectDriver().