# openttd.cpp File Reference

`#include "stdafx.h"`
`#include "debug.h"`
`#include "driver.h"`
`#include "saveload.h"`
`#include "openttd.h"`
`#include "bridge_map.h"`
`#include "mixer.h"`
`#include "spritecache.h"`
`#include "gfxinit.h"`
`#include "gui.h"`
`#include "landscape.h"`
`#include "station.h"`
`#include "station_map.h"`
`#include "town_map.h"`
`#include "tunnel_map.h"`
`#include "viewport_func.h"`
`#include "gfx_func.h"`
`#include "window_func.h"`
`#include "window_gui.h"`
`#include "player_func.h"`
`#include "player_base.h"`
`#include "command_func.h"`
`#include "town.h"`
`#include "industry.h"`
`#include "news.h"`
`#include "engine.h"`
`#include "fileio.h"`
`#include "fios.h"`
`#include "airport.h"`
`#include "aircraft.h"`
`#include "console.h"`
`#include "screenshot.h"`
`#include "network/network.h"`
`#include "network/network_internal.h"`
`#include "signs.h"`
`#include "depot.h"`
`#include "waypoint.h"`
`#include "ai/ai.h"`
`#include "train.h"`
`#include "yapf/yapf.h"`
`#include "settings_func.h"`
`#include "genworld.h"`
`#include "clear_map.h"`
`#include "fontcache.h"`
`#include "newgrf.h"`
`#include "newgrf_config.h"`
`#include "newgrf_house.h"`
`#include "newgrf_commons.h"`
`#include "newgrf_storage.h"`
`#include "player_face.h"`
`#include "group.h"`
`#include "blitter/factory.hpp"`
`#include "sound/sound_driver.hpp"`
`#include "music/music_driver.hpp"`
`#include "video/video_driver.hpp"`
`#include "strings_func.h"`
`#include "zoom_func.h"`
`#include "date_func.h"`
`#include "vehicle_func.h"`
`#include "sound_func.h"`
`#include "variables.h"`
`#include "road_func.h"`
`#include "rail_map.h"`
`#include "road_map.h"`
`#include "water_map.h"`
`#include "industry_map.h"`
`#include "unmovable_map.h"`
`#include "tree_map.h"`
`#include "tunnelbridge_map.h"`
`#include "void_map.h"`
`#include "water.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 ()
void | DoPaletteAnimations ()
void | MusicLoop ()
void | ResetMusic ()
void | SetDifficultyLevel (int mode, GameOptions *gm_opt)
Player * | DoStartupNewPlayer (bool is_ai)
| Create a new player and sets all player variables default values.
void | ShowOSErrorBox (const char *buf)
void | SetDefaultRailGui ()
void CDECL | error (const char *s,...)
void CDECL | ShowInfoF (const char *str,...)
static void | showhelp ()
static int | MyGetOpt (MyGetOptData *md)
static void | ParseResolution (int res[2], const char *s)
static void | InitializeDynamicVariables ()
static void | UnInitializeGame ()
static void | LoadIntroGame ()
int | ttd_main (int argc, char *argv[])
void | HandleExitGameRequest ()
static void | OTTD_ReleaseMutex ()
static ThreadMsg | OTTD_PollThreadEvent ()
void | OTTD_SendThreadMessage (ThreadMsg msg)
| Called by running thread to execute some action in the main game.
static void | ProcessSentMessage (ThreadMsg message)
| Handle the user-messages sent to us.
static void | ShowScreenshotResult (bool b)
static void | MakeNewGameDone ()
static void | MakeNewGame (bool from_heightmap)
static void | MakeNewEditorWorldDone ()
static void | MakeNewEditorWorld ()
void | StartupPlayers ()
void | StartupDisasters ()
void | StartupEconomy ()
static void | StartScenario ()
| Start Scenario starts a new game based on a scenario.
bool | SafeSaveOrLoad (const char *filename, int mode, int newgm, Subdirectory subdir)
| Load the specified savegame but on error do different things.
void | SwitchMode (int new_mode)
void | StateGameLoop ()
static void | DoAutosave ()
| Create an autosave.
static void | ScrollMainViewport (int x, int y)
static void | HandleKeyScrolling ()
void | GameLoop ()
void | BeforeSaveGame ()
static void | ConvertTownOwner ()
static void | CheckIsPlayerActive ()
static void | UpdateExclusiveRights ()
static void | UpdateCurrencies ()
static void | UpdateVoidTiles ()
static void | UpdateSignOwner ()
void | UpdateOldAircraft ()
| need to be called to load aircraft from old version
static RailType | UpdateRailType (RailType rt, RailType min)
static bool | InitializeWindowsAndCaches ()
| Initialization of the windows and several kinds of caches.
bool | AfterLoadGame ()
void | ReloadNewGRFData ()
| Reload all NewGRF files during a running game.

## Variables

const char | _openttd_revision []
byte | _no_scroll
byte | _savegame_sort_order
static ThreadMsg | _message = MSG_OTTD_NO_MESSAGE
| Mutex so that only one thread can communicate with the main program at any given time.
static const int8 | scrollamt [16][2]
| Describes all the different arrow key combinations the game allows when it is in scrolling mode.
static const byte | convert_currency []

## Detailed Description

Definition in file openttd.cpp.

## Function Documentation

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

Create an autosave.

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

Definition at line 1033 of file openttd.cpp.

References _networking, AUTOSAVE_DIR, PLAYER_SPECTATOR, SaveOrLoad(), SL_OK, and ttd_strlcat().

Player* DoStartupNewPlayer | ( | bool | is_ai | ) |

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

Parameters:

  | is_ai | is a ai player?

Returns:
  the player struct

Definition at line 484 of file players.cpp.

References _networking, _patches_newgame, GetPlayerRailtypes(), PLAYER_SPECTATOR, RandomPlayerFaceBits(), and ResetPlayerLivery().

Referenced by CmdPlayerCtrl().

static bool InitializeWindowsAndCaches | ( |  | ) | `[static]`

Initialization of the windows and several kinds of caches.

This is not done directly in AfterLoadGame because these functions require that all saveload conversions have been done. As people tend to add savegame conversion stuff after the intialization of the windows and caches quite some bugs had been made. Moving this out of there is both cleaner and less bug-prone.

Returns:
  true if everything went according to plan, otherwise false.

Definition at line 1285 of file openttd.cpp.

References CheckTrainsLengths(), FT_SCENARIO, IsEngineCountable(), MarkWholeScreenDirty(), MAX_PLAYERS, min(), PLAYER_FIRST, UpdateAllSignVirtCoords(), UpdateAllStationVirtCoord(), UpdateAllTownVirtCoords(), and UpdateAllWaypointSigns().

void OTTD_SendThreadMessage | ( | ThreadMsg | msg | ) |

Called by running thread to execute some action in the main game.

It will stall as long as the mutex is not freed (handled) by the game

Definition at line 664 of file openttd.cpp.

Referenced by SaveFileToDisk().

static void ProcessSentMessage | ( | ThreadMsg | message | ) | `[static]`

Handle the user-messages sent to us.

Parameters:

  | message | message sent

Definition at line 676 of file openttd.cpp.

References SaveFileDone(), and SaveFileError().

void ReloadNewGRFData | ( |  | ) |

Reload all NewGRF files during a running game.

This is a cut-down version of AfterLoadGame(). XXX - We need to reset the vehicle position hash because with a non-empty hash AfterLoadVehicles() will loop infinitely. We need AfterLoadVehicles() to recalculate vehicle data as some NewGRF vehicle sets could have been removed or added and changed statistics

Definition at line 2498 of file openttd.cpp.

References AfterLoadVehicles(), AfterLoadWaypoints(), CheckTrainsLengths(), MarkWholeScreenDirty(), and UpdateHousesAndTowns().

Referenced by ClickChangeClimateCheat(), and NewGRFConfirmationCallback().

bool SafeSaveOrLoad | ( | const char * | filename,
|  | int | mode,
|  | int | 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 819 of file openttd.cpp.

References SaveOrLoad(), SL_OK, and SL_REINIT.

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 768 of file openttd.cpp.

References CMD_SET_AUTOREPLACE, DoCommandP(), GetSaveLoadErrorString(), MarkWholeScreenDirty(), PLAYER_FIRST, SaveOrLoad(), SCENARIO_DIR, SetDParamStr(), SetLocalPlayer(), and SL_OK.

## Variable Documentation

const byte convert_currency[]`[static]`

Initial value:

     {
       0,  1, 12,  8,  3,
      10, 14, 19,  4,  5,
       9, 11, 13,  6, 17,
      16, 22, 21,  7, 15,
      18,  2, 20, }

Definition at line 1234 of file openttd.cpp.

const int8 scrollamt[16][2]`[static]`

Initial value:

     {
      { 0,  0},
      {-2,  0},
      { 0, -2},
      {-2, -1},
      { 2,  0},
      { 0,  0},
      { 2, -1},
      { 0, -2},
      { 0  ,2},
      {-2  ,1},
      { 0,  0},
      {-2,  0},
      { 2,  1},
      { 0,  2},
      { 2,  0},
      { 0,  0},
    }

Describes all the different arrow key combinations the game allows when it is in scrolling mode.

The real arrow keys are bitwise numbered as 1 = left 2 = up 4 = right 8 = down

Definition at line 1079 of file openttd.cpp.