2009-08-21 20:21:05 +00:00
/*
* This file is part of OpenTTD .
* OpenTTD is free software ; you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation , version 2.
* OpenTTD is distributed in the hope that it will be useful , but WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE .
* See the GNU General Public License for more details . You should have received a copy of the GNU General Public License along with OpenTTD . If not , see < http : //www.gnu.org/licenses/>.
*/
2008-05-06 15:11:33 +00:00
/** @file openttd.cpp Functions related to starting OpenTTD. */
2007-03-21 15:19:33 +00:00
2004-08-09 17:04:08 +00:00
# include "stdafx.h"
2008-03-31 07:03:35 +00:00
# include "blitter/factory.hpp"
# include "sound/sound_driver.hpp"
# include "music/music_driver.hpp"
# include "video/video_driver.hpp"
# include "fontcache.h"
2011-12-10 13:54:10 +00:00
# include "error.h"
2004-08-09 17:04:08 +00:00
# include "gui.h"
2008-03-31 07:03:35 +00:00
2009-08-09 16:54:03 +00:00
# include "base_media_base.h"
2009-01-04 15:32:25 +00:00
# include "saveload/saveload.h"
2008-09-30 20:51:04 +00:00
# include "company_func.h"
2007-12-21 21:50:46 +00:00
# include "command_func.h"
2008-03-28 08:53:36 +00:00
# include "news_func.h"
2007-12-28 03:14:55 +00:00
# include "fios.h"
2007-03-02 12:01:24 +00:00
# include "aircraft.h"
2009-05-22 20:18:45 +00:00
# include "roadveh.h"
2009-05-22 22:22:46 +00:00
# include "train.h"
2010-11-27 21:11:37 +00:00
# include "ship.h"
2008-05-24 10:15:06 +00:00
# include "console_func.h"
2004-11-15 19:25:59 +00:00
# include "screenshot.h"
2007-01-02 17:34:03 +00:00
# include "network/network.h"
2008-05-30 18:20:26 +00:00
# include "network/network_func.h"
2009-01-12 17:11:45 +00:00
# include "ai/ai.hpp"
# include "ai/ai_config.hpp"
2008-01-07 00:19:09 +00:00
# include "settings_func.h"
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
# include "genworld.h"
2011-08-21 12:48:46 +00:00
# include "progress.h"
2007-12-21 19:49:27 +00:00
# include "strings_func.h"
2007-12-26 13:50:40 +00:00
# include "date_func.h"
2007-12-27 13:35:39 +00:00
# include "vehicle_func.h"
2008-06-03 18:35:58 +00:00
# include "gamelog.h"
2008-04-20 08:22:59 +00:00
# include "animated_tile_func.h"
2009-06-23 21:44:48 +00:00
# include "roadstop_base.h"
2008-05-08 16:48:29 +00:00
# include "elrail_func.h"
2008-05-04 21:53:36 +00:00
# include "rev.h"
2009-01-03 18:44:20 +00:00
# include "highscore.h"
2009-10-20 12:20:53 +00:00
# include "station_base.h"
2009-09-07 11:10:49 +00:00
# include "crashlog.h"
2010-01-15 16:41:15 +00:00
# include "engine_func.h"
# include "core/random_func.hpp"
2010-05-18 01:10:23 +00:00
# include "rail_gui.h"
2019-04-06 06:46:15 +00:00
# include "road_gui.h"
2010-05-31 20:22:57 +00:00
# include "core/backup_type.hpp"
2010-07-03 13:28:15 +00:00
# include "hotkeys.h"
2010-09-06 18:20:07 +00:00
# include "newgrf.h"
2011-02-18 20:14:30 +00:00
# include "misc/getoptdata.h"
2011-12-19 20:50:54 +00:00
# include "game/game.hpp"
2011-12-19 20:55:56 +00:00
# include "game/game_config.hpp"
2012-04-25 21:06:31 +00:00
# include "town.h"
2012-08-18 11:38:06 +00:00
# include "subsidy_func.h"
2013-07-06 19:00:33 +00:00
# include "gfx_layout.h"
2019-04-23 20:55:27 +00:00
# include "viewport_func.h"
2014-01-02 16:48:16 +00:00
# include "viewport_sprite_sorter.h"
2018-07-19 19:17:07 +00:00
# include "framerate_type.h"
2020-05-11 23:19:52 +00:00
# include "industry.h"
2006-12-27 12:38:02 +00:00
2013-06-09 12:58:37 +00:00
# include "linkgraph/linkgraphschedule.h"
2004-08-09 17:04:08 +00:00
# include <stdarg.h>
2019-03-10 23:45:39 +00:00
# include <system_error>
2004-08-09 17:04:08 +00:00
2014-04-23 20:13:33 +00:00
# include "safeguards.h"
2007-03-07 11:47:46 +00:00
void CallLandscapeTick ( ) ;
void IncreaseDate ( ) ;
void DoPaletteAnimations ( ) ;
void MusicLoop ( ) ;
void ResetMusic ( ) ;
2018-05-04 20:29:22 +00:00
void CallWindowGameTickEvent ( ) ;
2011-11-17 21:17:17 +00:00
bool HandleBootstrap ( ) ;
2004-08-09 17:04:08 +00:00
2009-08-28 15:23:11 +00:00
extern Company * DoStartupNewCompany ( bool is_ai , CompanyID company = INVALID_COMPANY ) ;
2008-06-05 20:54:52 +00:00
extern void ShowOSErrorBox ( const char * buf , bool system ) ;
2010-07-19 17:11:09 +00:00
extern char * _config_file ;
2004-08-09 17:04:08 +00:00
2008-04-29 18:19:29 +00:00
/**
2008-06-05 20:54:52 +00:00
* Error handling for fatal user errors .
* @ param s the string to print .
* @ note Does NEVER return .
*/
void CDECL usererror ( const char * s , . . . )
{
va_list va ;
char buf [ 512 ] ;
va_start ( va , s ) ;
2014-04-24 19:51:45 +00:00
vseprintf ( buf , lastof ( buf ) , s , va ) ;
2008-06-05 20:54:52 +00:00
va_end ( va ) ;
ShowOSErrorBox ( buf , false ) ;
2019-04-10 21:07:06 +00:00
if ( VideoDriver : : GetInstance ( ) ! = nullptr ) VideoDriver : : GetInstance ( ) - > Stop ( ) ;
2008-06-05 20:54:52 +00:00
exit ( 1 ) ;
}
/**
* Error handling for fatal non - user errors .
2008-04-29 18:19:29 +00:00
* @ param s the string to print .
* @ note Does NEVER return .
*/
2006-07-26 03:33:12 +00:00
void CDECL error ( const char * s , . . . )
2005-11-14 19:48:04 +00:00
{
2004-08-09 17:04:08 +00:00
va_list va ;
char buf [ 512 ] ;
2005-11-14 19:48:04 +00:00
2004-08-09 17:04:08 +00:00
va_start ( va , s ) ;
2014-04-24 19:51:45 +00:00
vseprintf ( buf , lastof ( buf ) , s , va ) ;
2004-08-09 17:04:08 +00:00
va_end ( va ) ;
2004-09-10 19:02:27 +00:00
2019-04-10 21:07:06 +00:00
if ( VideoDriver : : GetInstance ( ) = = nullptr | | VideoDriver : : GetInstance ( ) - > HasGUI ( ) ) {
2019-01-30 21:28:07 +00:00
ShowOSErrorBox ( buf , true ) ;
}
2004-08-09 17:04:08 +00:00
2009-09-07 11:10:49 +00:00
/* Set the error message for the crash log and then invoke it. */
CrashLog : : SetErrorMessage ( buf ) ;
abort ( ) ;
2004-08-09 17:04:08 +00:00
}
2008-04-29 18:19:29 +00:00
/**
* Shows some information on the console / a popup box depending on the OS .
* @ param str the text to show .
*/
2004-08-09 17:04:08 +00:00
void CDECL ShowInfoF ( const char * str , . . . )
{
va_list va ;
char buf [ 1024 ] ;
va_start ( va , str ) ;
2014-04-24 19:51:45 +00:00
vseprintf ( buf , lastof ( buf ) , str , va ) ;
2004-08-09 17:04:08 +00:00
va_end ( va ) ;
ShowInfo ( buf ) ;
}
2008-04-29 18:19:29 +00:00
/**
* Show the help message when someone passed a wrong parameter .
*/
static void ShowHelp ( )
{
2009-01-12 17:11:45 +00:00
char buf [ 8192 ] ;
2008-04-29 18:19:29 +00:00
char * p = buf ;
2004-08-09 17:04:08 +00:00
2008-10-28 14:42:31 +00:00
p + = seprintf ( p , lastof ( buf ) , " OpenTTD %s \n " , _openttd_revision ) ;
2006-11-28 20:55:16 +00:00
p = strecpy ( p ,
2006-01-06 22:52:31 +00:00
" \n "
" \n "
2004-08-09 17:04:08 +00:00
" Command line options: \n "
2004-12-04 17:54:56 +00:00
" -v drv = Set video driver (see below) \n "
2007-01-31 20:20:36 +00:00
" -s drv = Set sound driver (see below) (param bufsize,hz) \n "
2004-12-04 17:54:56 +00:00
" -m drv = Set music driver (see below) \n "
2007-06-11 11:50:49 +00:00
" -b drv = Set the blitter to use (see below) \n "
2004-12-04 17:54:56 +00:00
" -r res = Set resolution (for instance 800x600) \n "
" -h = Display this help text \n "
2006-08-15 14:52:17 +00:00
" -t year = Set starting year \n "
2005-03-06 16:08:19 +00:00
" -d [[fac=]lvl[,...]]= Debug mode \n "
2004-12-04 17:54:56 +00:00
" -e = Start Editor \n "
" -g [savegame] = Start new/save game immediately \n "
" -G seed = Set random seed \n "
2012-08-20 13:12:12 +00:00
" -n [ip:port#company]= Join network game \n "
2009-06-10 19:00:34 +00:00
" -p password = Password to join server \n "
" -P password = Password to join company \n "
2007-01-03 18:29:15 +00:00
" -D [ip][:port] = Start dedicated server \n "
2007-02-08 12:27:53 +00:00
" -l ip[:port] = Redirect DEBUG() \n "
2019-03-04 18:21:13 +00:00
# if !defined(_WIN32)
2004-12-18 14:19:21 +00:00
" -f = Fork into the background (dedicated only) \n "
2006-06-27 21:25:53 +00:00
# endif
2008-08-24 08:41:38 +00:00
" -I graphics_set = Force the graphics set (see below) \n "
2009-08-09 19:50:44 +00:00
" -S sounds_set = Force the sounds set (see below) \n "
2009-12-22 21:40:29 +00:00
" -M music_set = Force the music set (see below) \n "
2006-01-06 22:52:31 +00:00
" -c config_file = Use 'config_file' instead of 'openttd.cfg' \n "
2008-08-24 08:41:38 +00:00
" -x = Do not automatically save to config file on exit \n "
2011-10-30 13:47:45 +00:00
" -q savegame = Write some information about the savegame and exit \n "
2008-08-24 08:41:38 +00:00
" \n " ,
2006-11-28 20:55:16 +00:00
lastof ( buf )
2004-08-09 17:04:08 +00:00
) ;
2008-08-24 08:41:38 +00:00
/* List the graphics packs */
2009-08-09 16:54:03 +00:00
p = BaseGraphics : : GetSetsList ( p , lastof ( buf ) ) ;
2008-08-24 08:41:38 +00:00
2009-08-09 19:50:44 +00:00
/* List the sounds packs */
p = BaseSounds : : GetSetsList ( p , lastof ( buf ) ) ;
2009-12-22 21:40:29 +00:00
/* List the music packs */
p = BaseMusic : : GetSetsList ( p , lastof ( buf ) ) ;
2007-07-05 12:23:54 +00:00
/* List the drivers */
2013-11-25 14:26:46 +00:00
p = DriverFactoryBase : : GetDriversInfo ( p , lastof ( buf ) ) ;
2004-08-09 17:04:08 +00:00
2007-06-11 11:50:49 +00:00
/* List the blitters */
2014-01-02 22:41:58 +00:00
p = BlitterFactory : : GetBlittersInfo ( p , lastof ( buf ) ) ;
2007-06-11 11:50:49 +00:00
2012-04-07 20:55:55 +00:00
/* List the debug facilities. */
p = DumpDebugFacilityNames ( p , lastof ( buf ) ) ;
2009-01-12 17:11:45 +00:00
/* We need to initialize the AI, so it finds the AIs */
AI : : Initialize ( ) ;
2011-02-07 09:51:16 +00:00
p = AI : : GetConsoleList ( p , lastof ( buf ) , true ) ;
2009-01-12 17:11:45 +00:00
AI : : Uninitialize ( true ) ;
2011-12-19 20:55:56 +00:00
/* We need to initialize the GameScript, so it finds the GSs */
Game : : Initialize ( ) ;
p = Game : : GetConsoleList ( p , lastof ( buf ) , true ) ;
Game : : Uninitialize ( true ) ;
2007-01-18 14:08:56 +00:00
/* ShowInfo put output to stderr, but version information should go
* to stdout ; this is the only exception */
2018-12-09 01:28:14 +00:00
# if !defined(_WIN32)
2007-01-18 14:08:56 +00:00
printf ( " %s \n " , buf ) ;
# else
2004-08-09 17:04:08 +00:00
ShowInfo ( buf ) ;
2007-01-18 14:08:56 +00:00
# endif
2004-08-09 17:04:08 +00:00
}
2011-10-30 13:47:45 +00:00
static void WriteSavegameInfo ( const char * name )
{
2019-01-29 00:56:28 +00:00
extern SaveLoadVersion _sl_version ;
2011-10-30 13:47:45 +00:00
uint32 last_ottd_rev = 0 ;
byte ever_modified = 0 ;
bool removed_newgrfs = false ;
GamelogInfo ( _load_check_data . gamelog_action , _load_check_data . gamelog_actions , & last_ottd_rev , & ever_modified , & removed_newgrfs ) ;
char buf [ 8192 ] ;
char * p = buf ;
p + = seprintf ( p , lastof ( buf ) , " Name: %s \n " , name ) ;
p + = seprintf ( p , lastof ( buf ) , " Savegame ver: %d \n " , _sl_version ) ;
p + = seprintf ( p , lastof ( buf ) , " NewGRF ver: 0x%08X \n " , last_ottd_rev ) ;
p + = seprintf ( p , lastof ( buf ) , " Modified: %d \n " , ever_modified ) ;
if ( removed_newgrfs ) {
p + = seprintf ( p , lastof ( buf ) , " NewGRFs have been removed \n " ) ;
}
p = strecpy ( p , " NewGRFs: \n " , lastof ( buf ) ) ;
if ( _load_check_data . HasNewGrfs ( ) ) {
2019-04-10 21:07:06 +00:00
for ( GRFConfig * c = _load_check_data . grfconfig ; c ! = nullptr ; c = c - > next ) {
2011-10-30 13:47:45 +00:00
char md5sum [ 33 ] ;
md5sumToString ( md5sum , lastof ( md5sum ) , HasBit ( c - > flags , GCF_COMPATIBLE ) ? c - > original_md5sum : c - > ident . md5sum ) ;
p + = seprintf ( p , lastof ( buf ) , " %08X %s %s \n " , c - > ident . grfid , md5sum , c - > filename ) ;
}
}
/* ShowInfo put output to stderr, but version information should go
* to stdout ; this is the only exception */
2018-12-09 01:28:14 +00:00
# if !defined(_WIN32)
2011-10-30 13:47:45 +00:00
printf ( " %s \n " , buf ) ;
# else
ShowInfo ( buf ) ;
# endif
}
2004-08-09 17:04:08 +00:00
2008-04-29 18:19:29 +00:00
/**
* Extract the resolution from the given string and store
* it in the ' res ' parameter .
* @ param res variable to store the resolution in .
* @ param s the string to decompose .
*/
2008-06-16 19:38:41 +00:00
static void ParseResolution ( Dimension * res , const char * s )
2004-08-09 17:04:08 +00:00
{
2007-01-10 18:56:51 +00:00
const char * t = strchr ( s , ' x ' ) ;
2019-04-10 21:07:06 +00:00
if ( t = = nullptr ) {
2004-08-09 17:04:08 +00:00
ShowInfoF ( " Invalid resolution '%s' " , s ) ;
return ;
}
2019-04-10 21:07:06 +00:00
res - > width = max ( strtoul ( s , nullptr , 0 ) , 64UL ) ;
res - > height = max ( strtoul ( t + 1 , nullptr , 0 ) , 64UL ) ;
2004-09-10 19:02:27 +00:00
}
2004-08-09 17:04:08 +00:00
2007-01-14 17:17:30 +00:00
2010-08-01 19:22:34 +00:00
/**
2019-01-17 22:01:07 +00:00
* Uninitializes drivers , frees allocated memory , cleans pools , . . .
2008-05-08 23:26:17 +00:00
* Generally , prepares the game for shutting down
*/
static void ShutdownGame ( )
2005-01-06 22:31:58 +00:00
{
2008-05-08 23:26:17 +00:00
IConsoleFree ( ) ;
if ( _network_available ) NetworkShutDown ( ) ; // Shut down the network and close any open connections
DriverFactoryBase : : ShutdownDrivers ( ) ;
2007-01-14 17:17:30 +00:00
UnInitWindowSystem ( ) ;
2011-12-19 20:50:54 +00:00
/* stop the scripts */
2009-05-14 18:09:50 +00:00
AI : : Uninitialize ( false ) ;
2011-12-19 20:55:56 +00:00
Game : : Uninitialize ( false ) ;
2009-05-14 18:09:50 +00:00
2007-01-14 17:17:30 +00:00
/* Uninitialize variables that are allocated dynamically */
2008-06-03 18:35:58 +00:00
GamelogReset ( ) ;
2005-02-01 18:32:01 +00:00
2005-03-10 21:44:17 +00:00
free ( _config_file ) ;
2008-05-08 23:26:17 +00:00
2013-06-09 12:58:37 +00:00
LinkGraphSchedule : : Clear ( ) ;
2011-02-19 23:05:47 +00:00
PoolBase : : Clean ( PT_ALL ) ;
2011-02-19 18:25:24 +00:00
2011-11-17 21:17:17 +00:00
/* No NewGRFs were loaded when it was still bootstrapping. */
if ( _game_mode ! = GM_BOOTSTRAP ) ResetNewGRFData ( ) ;
2011-02-19 18:55:10 +00:00
2008-05-08 23:26:17 +00:00
/* Close all and any open filehandles */
FioCloseAll ( ) ;
2011-11-20 08:08:13 +00:00
UninitFreeType ( ) ;
2005-03-09 19:48:20 +00:00
}
2005-01-06 22:31:58 +00:00
2011-08-21 12:52:24 +00:00
/**
* Load the introduction game .
* @ param load_newgrfs Whether to load the NewGRFs or not .
*/
static void LoadIntroGame ( bool load_newgrfs = true )
2004-12-04 17:54:56 +00:00
{
_game_mode = GM_MENU ;
2007-04-05 07:49:04 +00:00
2011-08-21 12:52:24 +00:00
if ( load_newgrfs ) ResetGRFConfig ( false ) ;
2004-12-04 17:54:56 +00:00
2007-03-21 15:19:33 +00:00
/* Setup main window */
2005-03-09 19:48:20 +00:00
ResetWindowSystem ( ) ;
2009-02-09 02:57:15 +00:00
SetupColoursAndInitialWindow ( ) ;
2004-12-04 17:54:56 +00:00
2007-06-17 15:48:57 +00:00
/* Load the default opening screen savegame */
2016-09-04 16:06:50 +00:00
if ( SaveOrLoad ( " opntitle.dat " , SLO_LOAD , DFT_GAME_FILE , BASESET_DIR ) ! = SL_OK ) {
2010-01-17 22:59:24 +00:00
GenerateWorld ( GWM_EMPTY , 64 , 64 ) ; // if failed loading, make empty world.
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
WaitTillGeneratedWorld ( ) ;
2008-09-30 20:39:50 +00:00
SetLocalCompany ( COMPANY_SPECTATOR ) ;
2008-09-15 23:50:12 +00:00
} else {
2008-09-30 20:39:50 +00:00
SetLocalCompany ( COMPANY_FIRST ) ;
2005-01-07 08:07:24 +00:00
}
2004-12-04 17:54:56 +00:00
2019-04-23 20:55:27 +00:00
FixTitleGameZoom ( ) ;
2009-05-06 15:06:57 +00:00
_pause_mode = PM_UNPAUSED ;
2006-08-21 14:59:23 +00:00
_cursor . fix_at = false ;
2004-12-04 17:54:56 +00:00
2011-11-20 11:52:11 +00:00
CheckForMissingGlyphs ( ) ;
2007-12-16 18:38:19 +00:00
2018-06-24 18:06:05 +00:00
MusicLoop ( ) ; // ensure music is correct
2004-12-04 17:54:56 +00:00
}
2009-01-12 17:11:45 +00:00
void MakeNewgameSettingsLive ( )
{
for ( CompanyID c = COMPANY_FIRST ; c < MAX_COMPANIES ; c + + ) {
2019-04-10 21:07:06 +00:00
if ( _settings_game . ai_config [ c ] ! = nullptr ) {
2009-01-12 17:11:45 +00:00
delete _settings_game . ai_config [ c ] ;
}
}
2019-04-10 21:07:06 +00:00
if ( _settings_game . game_config ! = nullptr ) {
2011-12-19 20:55:56 +00:00
delete _settings_game . game_config ;
}
2009-01-12 17:11:45 +00:00
2011-07-03 10:59:25 +00:00
/* Copy newgame settings to active settings.
* Also initialise old settings needed for savegame conversion . */
2009-01-12 17:11:45 +00:00
_settings_game = _settings_newgame ;
2011-07-03 10:59:25 +00:00
_old_vds = _settings_client . company . vehicle ;
2009-01-12 17:11:45 +00:00
for ( CompanyID c = COMPANY_FIRST ; c < MAX_COMPANIES ; c + + ) {
2019-04-10 21:07:06 +00:00
_settings_game . ai_config [ c ] = nullptr ;
if ( _settings_newgame . ai_config [ c ] ! = nullptr ) {
2009-01-12 17:11:45 +00:00
_settings_game . ai_config [ c ] = new AIConfig ( _settings_newgame . ai_config [ c ] ) ;
2018-09-30 18:34:40 +00:00
if ( ! AIConfig : : GetConfig ( c , AIConfig : : SSS_FORCE_GAME ) - > HasScript ( ) ) {
2019-04-10 21:07:06 +00:00
AIConfig : : GetConfig ( c , AIConfig : : SSS_FORCE_GAME ) - > Change ( nullptr ) ;
2018-09-30 18:34:40 +00:00
}
2009-01-12 17:11:45 +00:00
}
}
2019-04-10 21:07:06 +00:00
_settings_game . game_config = nullptr ;
if ( _settings_newgame . game_config ! = nullptr ) {
2011-12-19 20:55:56 +00:00
_settings_game . game_config = new GameConfig ( _settings_newgame . game_config ) ;
}
2009-01-12 17:11:45 +00:00
}
2011-12-11 11:47:08 +00:00
void OpenBrowser ( const char * url )
{
/* Make sure we only accept urls that are sure to open a browser. */
if ( strstr ( url , " http:// " ) ! = url & & strstr ( url , " https:// " ) ! = url ) return ;
extern void OSOpenBrowser ( const char * url ) ;
OSOpenBrowser ( url ) ;
}
2011-08-21 12:50:47 +00:00
/** Callback structure of statements to be executed after the NewGRF scan. */
struct AfterNewGRFScan : NewGRFScanCallback {
2011-08-24 17:04:18 +00:00
Year startyear ; ///< The start year.
2018-10-10 23:33:37 +00:00
uint32 generation_seed ; ///< Seed for the new game.
2011-08-24 17:04:18 +00:00
char * dedicated_host ; ///< Hostname for the dedicated server.
uint16 dedicated_port ; ///< Port for the dedicated server.
2019-04-10 21:07:06 +00:00
char * network_conn ; ///< Information about the server to connect to, or nullptr.
2011-08-21 12:50:47 +00:00
const char * join_server_password ; ///< The password to join the server with.
const char * join_company_password ; ///< The password to join the company with.
2011-11-14 21:42:54 +00:00
bool * save_config_ptr ; ///< The pointer to the save config setting.
bool save_config ; ///< The save config setting.
2011-08-21 12:50:47 +00:00
2011-11-14 21:42:54 +00:00
/**
* Create a new callback .
* @ param save_config_ptr Pointer to the save_config local variable which
* decides whether to save of exit or not .
*/
AfterNewGRFScan ( bool * save_config_ptr ) :
2011-08-24 17:04:18 +00:00
startyear ( INVALID_YEAR ) , generation_seed ( GENERATE_NEW_SEED ) ,
2019-04-10 21:07:06 +00:00
dedicated_host ( nullptr ) , dedicated_port ( 0 ) , network_conn ( nullptr ) ,
join_server_password ( nullptr ) , join_company_password ( nullptr ) ,
2011-11-14 21:42:54 +00:00
save_config_ptr ( save_config_ptr ) , save_config ( true )
2011-08-21 12:50:47 +00:00
{
2018-10-27 06:17:40 +00:00
/* Visual C++ 2015 fails compiling this line (AfterNewGRFScan::generation_seed undefined symbol)
* if it ' s placed outside a member function , directly in the struct body . */
assert_compile ( sizeof ( generation_seed ) = = sizeof ( _settings_game . game_creation . generation_seed ) ) ;
2011-08-21 12:50:47 +00:00
}
virtual void OnNewGRFsScanned ( )
{
ResetGRFConfig ( false ) ;
2011-11-14 21:42:54 +00:00
TarScanner : : DoScan ( TarScanner : : SCENARIO ) ;
AI : : Initialize ( ) ;
2011-12-19 20:55:56 +00:00
Game : : Initialize ( ) ;
2011-11-14 21:42:54 +00:00
/* 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 ;
2011-12-04 11:18:43 +00:00
/* Since the default for the palette might have changed due to
* reading the configuration file , recalculate that now . */
UpdateNewGRFConfigPalette ( ) ;
2011-11-14 21:42:54 +00:00
2011-12-19 20:55:56 +00:00
Game : : Uninitialize ( true ) ;
2011-11-14 21:42:54 +00:00
AI : : Uninitialize ( true ) ;
2011-08-24 17:04:18 +00:00
LoadFromHighScore ( ) ;
LoadHotkeysFromConfig ( ) ;
2013-05-26 19:27:22 +00:00
WindowDesc : : LoadFromConfig ( ) ;
2011-08-24 17:04:18 +00:00
2011-11-14 21:42:54 +00:00
/* We have loaded the config, so we may possibly save it. */
* save_config_ptr = save_config ;
2012-04-19 20:08:51 +00:00
/* restore saved music volume */
2014-04-28 21:06:51 +00:00
MusicDriver : : GetInstance ( ) - > SetVolume ( _settings_client . music . music_vol ) ;
2011-11-14 21:42:54 +00:00
2011-08-24 17:04:18 +00:00
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 ;
2019-04-10 21:07:06 +00:00
if ( dedicated_host ! = nullptr ) {
2019-04-02 19:31:33 +00:00
_network_bind_list . clear ( ) ;
_network_bind_list . emplace_back ( dedicated_host ) ;
2011-08-24 17:04:18 +00:00
}
if ( dedicated_port ! = 0 ) _settings_client . network . server_port = dedicated_port ;
2011-08-21 12:50:47 +00:00
/* 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 ( ) ;
2019-04-10 21:07:06 +00:00
if ( _network_available & & network_conn ! = nullptr ) {
const char * port = nullptr ;
const char * company = nullptr ;
2011-08-21 12:50:47 +00:00
uint16 rport = NETWORK_DEFAULT_PORT ;
CompanyID join_as = COMPANY_NEW_COMPANY ;
ParseConnectionString ( & company , & port , network_conn ) ;
2019-04-10 21:07:06 +00:00
if ( company ! = nullptr ) {
2011-08-21 12:50:47 +00:00
join_as = ( CompanyID ) atoi ( company ) ;
if ( join_as ! = COMPANY_SPECTATOR ) {
join_as - - ;
if ( join_as > = MAX_COMPANIES ) {
delete this ;
return ;
}
}
}
2019-04-10 21:07:06 +00:00
if ( port ! = nullptr ) rport = atoi ( port ) ;
2011-08-21 12:50:47 +00:00
LoadIntroGame ( ) ;
_switch_mode = SM_NONE ;
NetworkClientConnectGame ( NetworkAddress ( network_conn , rport ) , join_as , join_server_password , join_company_password ) ;
}
/* After the scan we're not used anymore. */
delete this ;
}
} ;
2019-03-04 18:21:13 +00:00
# if defined(UNIX)
2007-03-07 11:47:46 +00:00
extern void DedicatedFork ( ) ;
2005-08-06 14:58:06 +00:00
# endif
2004-12-18 14:19:21 +00:00
2011-02-18 20:28:25 +00:00
/** Options of OpenTTD. */
static const OptionData _options [ ] = {
GETOPT_SHORT_VALUE ( ' I ' ) ,
GETOPT_SHORT_VALUE ( ' S ' ) ,
GETOPT_SHORT_VALUE ( ' M ' ) ,
GETOPT_SHORT_VALUE ( ' m ' ) ,
GETOPT_SHORT_VALUE ( ' s ' ) ,
GETOPT_SHORT_VALUE ( ' v ' ) ,
GETOPT_SHORT_VALUE ( ' b ' ) ,
GETOPT_SHORT_OPTVAL ( ' D ' ) ,
GETOPT_SHORT_OPTVAL ( ' n ' ) ,
GETOPT_SHORT_VALUE ( ' l ' ) ,
GETOPT_SHORT_VALUE ( ' p ' ) ,
GETOPT_SHORT_VALUE ( ' P ' ) ,
2019-03-04 18:21:13 +00:00
# if !defined(_WIN32)
2011-02-18 20:28:25 +00:00
GETOPT_SHORT_NOVAL ( ' f ' ) ,
# endif
GETOPT_SHORT_VALUE ( ' r ' ) ,
GETOPT_SHORT_VALUE ( ' t ' ) ,
GETOPT_SHORT_OPTVAL ( ' d ' ) ,
GETOPT_SHORT_NOVAL ( ' e ' ) ,
GETOPT_SHORT_OPTVAL ( ' g ' ) ,
GETOPT_SHORT_VALUE ( ' G ' ) ,
GETOPT_SHORT_VALUE ( ' c ' ) ,
GETOPT_SHORT_NOVAL ( ' x ' ) ,
2011-10-30 13:47:45 +00:00
GETOPT_SHORT_VALUE ( ' q ' ) ,
2011-02-18 20:28:25 +00:00
GETOPT_SHORT_NOVAL ( ' h ' ) ,
GETOPT_END ( )
} ;
2013-06-28 21:11:35 +00:00
/**
* Main entry point for this lovely game .
* @ param argc The number of arguments passed to this game .
* @ param argv The values of the arguments .
* @ return 0 when there is no error .
*/
int openttd_main ( int argc , char * argv [ ] )
2004-08-09 17:04:08 +00:00
{
2020-05-17 21:32:08 +00:00
std : : string musicdriver ;
std : : string sounddriver ;
std : : string videodriver ;
std : : string blitter ;
2020-05-17 21:32:06 +00:00
std : : string graphics_set ;
std : : string sounds_set ;
std : : string music_set ;
2008-06-16 19:38:41 +00:00
Dimension resolution = { 0 , 0 } ;
2011-11-14 21:42:54 +00:00
/* AfterNewGRFScan sets save_config to true after scanning completed. */
bool save_config = false ;
2020-05-17 21:32:10 +00:00
std : : unique_ptr < AfterNewGRFScan > scanner ( new AfterNewGRFScan ( & save_config ) ) ;
2006-02-21 17:29:53 +00:00
bool dedicated = false ;
2019-04-10 21:07:06 +00:00
char * debuglog_conn = nullptr ;
2010-07-19 17:46:53 +00:00
extern bool _dedicated_forks ;
_dedicated_forks = false ;
2005-02-23 09:13:12 +00:00
2019-03-10 23:45:39 +00:00
std : : unique_lock < std : : mutex > modal_work_lock ( _modal_progress_work_mutex , std : : defer_lock ) ;
std : : unique_lock < std : : mutex > modal_paint_lock ( _modal_progress_paint_mutex , std : : defer_lock ) ;
2004-08-09 17:04:08 +00:00
_game_mode = GM_MENU ;
_switch_mode = SM_MENU ;
2019-04-10 21:07:06 +00:00
_config_file = nullptr ;
2004-08-09 17:04:08 +00:00
2011-02-18 20:28:25 +00:00
GetOptData mgo ( argc - 1 , argv + 1 , _options ) ;
2013-11-24 15:21:38 +00:00
int ret = 0 ;
2007-01-10 18:56:51 +00:00
2011-02-18 20:28:25 +00:00
int i ;
2011-02-18 20:14:30 +00:00
while ( ( i = mgo . GetOpt ( ) ) ! = - 1 ) {
2006-02-01 06:32:03 +00:00
switch ( i ) {
2020-05-17 21:32:06 +00:00
case ' I ' : graphics_set = mgo . opt ; break ;
case ' S ' : sounds_set = mgo . opt ; break ;
case ' M ' : music_set = mgo . opt ; break ;
2020-05-17 21:32:08 +00:00
case ' m ' : musicdriver = mgo . opt ; break ;
case ' s ' : sounddriver = mgo . opt ; break ;
case ' v ' : videodriver = mgo . opt ; break ;
case ' b ' : blitter = mgo . opt ; break ;
2006-02-21 17:29:53 +00:00
case ' D ' :
2020-05-17 21:32:08 +00:00
musicdriver = " null " ;
sounddriver = " null " ;
videodriver = " dedicated " ;
blitter = " null " ;
2006-02-21 17:29:53 +00:00
dedicated = true ;
2009-02-21 13:27:09 +00:00
SetDebugString ( " net=6 " ) ;
2019-04-10 21:07:06 +00:00
if ( mgo . opt ! = nullptr ) {
2007-01-03 18:29:15 +00:00
/* Use the existing method for parsing (openttd -n).
2008-09-30 20:39:50 +00:00
* However , we do ignore the # company part . */
2019-04-10 21:07:06 +00:00
const char * temp = nullptr ;
const char * port = nullptr ;
2007-01-03 18:29:15 +00:00
ParseConnectionString ( & temp , & port , mgo . opt ) ;
2011-08-24 17:04:18 +00:00
if ( ! StrEmpty ( mgo . opt ) ) scanner - > dedicated_host = mgo . opt ;
2019-04-10 21:07:06 +00:00
if ( port ! = nullptr ) scanner - > dedicated_port = atoi ( port ) ;
2007-01-03 18:29:15 +00:00
}
2006-02-21 17:29:53 +00:00
break ;
case ' f ' : _dedicated_forks = true ; break ;
case ' n ' :
2019-04-10 21:07:06 +00:00
scanner - > network_conn = mgo . opt ; // optional IP parameter, nullptr if unset
2006-02-21 17:29:53 +00:00
break ;
2007-02-08 12:27:53 +00:00
case ' l ' :
debuglog_conn = mgo . opt ;
break ;
2009-06-10 19:00:34 +00:00
case ' p ' :
2011-08-24 17:04:18 +00:00
scanner - > join_server_password = mgo . opt ;
2009-06-10 19:00:34 +00:00
break ;
case ' P ' :
2011-08-24 17:04:18 +00:00
scanner - > join_company_password = mgo . opt ;
2009-06-10 19:00:34 +00:00
break ;
2008-06-16 19:38:41 +00:00
case ' r ' : ParseResolution ( & resolution , mgo . opt ) ; break ;
2011-08-24 17:04:18 +00:00
case ' t ' : scanner - > startyear = atoi ( mgo . opt ) ; break ;
2004-08-09 17:04:08 +00:00
case ' d ' : {
2018-12-09 01:28:14 +00:00
# if defined(_WIN32)
2004-08-09 17:04:08 +00:00
CreateConsole ( ) ;
# endif
2019-04-10 21:07:06 +00:00
if ( mgo . opt ! = nullptr ) SetDebugString ( mgo . opt ) ;
2010-08-01 18:53:30 +00:00
break ;
}
2012-01-22 18:37:01 +00:00
case ' e ' : _switch_mode = ( _switch_mode = = SM_LOAD_GAME | | _switch_mode = = SM_LOAD_SCENARIO ? SM_LOAD_SCENARIO : SM_EDITOR ) ; break ;
2004-09-10 19:02:27 +00:00
case ' g ' :
2019-04-10 21:07:06 +00:00
if ( mgo . opt ! = nullptr ) {
2016-09-04 12:58:04 +00:00
_file_to_saveload . SetName ( mgo . opt ) ;
2016-09-04 12:57:43 +00:00
bool is_scenario = _switch_mode = = SM_EDITOR | | _switch_mode = = SM_LOAD_SCENARIO ;
_switch_mode = is_scenario ? SM_LOAD_SCENARIO : SM_LOAD_GAME ;
2016-09-04 16:06:50 +00:00
_file_to_saveload . SetMode ( SLO_LOAD , is_scenario ? FT_SCENARIO : FT_SAVEGAME , DFT_GAME_FILE ) ;
2008-07-28 15:31:11 +00:00
/* 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 , ' . ' ) ;
2019-04-10 21:07:06 +00:00
if ( t ! = nullptr ) {
FiosType ft = FiosGetSavegameListCallback ( SLO_LOAD , _file_to_saveload . name , t , nullptr , nullptr ) ;
2016-09-04 12:47:07 +00:00
if ( ft ! = FIOS_TYPE_INVALID ) _file_to_saveload . SetMode ( ft ) ;
2008-07-28 15:31:11 +00:00
}
break ;
2006-06-27 21:25:53 +00:00
}
2008-07-28 15:31:11 +00:00
_switch_mode = SM_NEWGAME ;
2008-09-27 22:19:34 +00:00
/* Give a random map if no seed has been given */
2011-08-24 17:04:18 +00:00
if ( scanner - > generation_seed = = GENERATE_NEW_SEED ) {
scanner - > generation_seed = InteractiveRandom ( ) ;
2008-09-27 22:19:34 +00:00
}
2004-08-09 17:04:08 +00:00
break ;
2011-10-30 13:47:45 +00:00
case ' q ' : {
DeterminePaths ( argv [ 0 ] ) ;
2013-11-24 15:21:38 +00:00
if ( StrEmpty ( mgo . opt ) ) {
ret = 1 ;
2020-05-17 21:32:10 +00:00
return ret ;
2013-11-24 15:21:38 +00:00
}
2011-10-30 13:47:45 +00:00
char title [ 80 ] ;
title [ 0 ] = ' \0 ' ;
2016-09-04 16:06:50 +00:00
FiosGetSavegameListCallback ( SLO_LOAD , mgo . opt , strrchr ( mgo . opt , ' . ' ) , title , lastof ( title ) ) ;
2011-10-30 13:47:45 +00:00
_load_check_data . Clear ( ) ;
2016-09-04 16:06:50 +00:00
SaveOrLoadResult res = SaveOrLoad ( mgo . opt , SLO_CHECK , DFT_GAME_FILE , SAVE_DIR , false ) ;
2011-10-30 13:47:45 +00:00
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 ) ;
}
2020-05-17 21:32:10 +00:00
return ret ;
2011-10-30 13:47:45 +00:00
}
WriteSavegameInfo ( title ) ;
2020-05-17 21:32:10 +00:00
return ret ;
2011-10-30 13:47:45 +00:00
}
2019-04-10 21:07:06 +00:00
case ' G ' : scanner - > generation_seed = strtoul ( mgo . opt , nullptr , 10 ) ; break ;
2014-09-16 17:14:07 +00:00
case ' c ' : free ( _config_file ) ; _config_file = stredup ( mgo . opt ) ; break ;
2011-11-14 21:42:54 +00:00
case ' x ' : scanner - > save_config = false ; break ;
2005-07-08 22:25:24 +00:00
case ' h ' :
2011-02-18 20:28:25 +00:00
i = - 2 ; // Force printing of help.
break ;
2004-08-09 17:04:08 +00:00
}
2011-02-18 20:28:25 +00:00
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 ] ) ;
2011-11-14 21:40:39 +00:00
TarScanner : : DoScan ( TarScanner : : BASESET ) ;
2011-02-18 20:28:25 +00:00
BaseGraphics : : FindSets ( ) ;
BaseSounds : : FindSets ( ) ;
BaseMusic : : FindSets ( ) ;
ShowHelp ( ) ;
2020-05-17 21:32:10 +00:00
return ret ;
2004-08-09 17:04:08 +00:00
}
2007-03-17 11:36:04 +00:00
DeterminePaths ( argv [ 0 ] ) ;
2011-11-14 21:42:54 +00:00
TarScanner : : DoScan ( TarScanner : : BASESET ) ;
2004-12-18 14:19:21 +00:00
2011-08-24 17:04:18 +00:00
if ( dedicated ) DEBUG ( net , 0 , " Starting dedicated version %s " , _openttd_revision ) ;
if ( _dedicated_forks & & ! dedicated ) _dedicated_forks = false ;
2019-03-04 18:21:13 +00:00
# if defined(UNIX)
2007-03-21 15:19:33 +00:00
/* We must fork here, or we'll end up without some resources we need (like sockets) */
2010-07-19 15:44:49 +00:00
if ( _dedicated_forks ) DedicatedFork ( ) ;
2004-12-18 14:19:21 +00:00
# endif
2011-11-14 21:42:54 +00:00
LoadFromConfig ( true ) ;
2004-08-09 17:04:08 +00:00
2011-11-14 21:42:54 +00:00
if ( resolution . width ! = 0 ) _cur_resolution = resolution ;
2004-08-09 17:04:08 +00:00
2009-06-30 12:42:43 +00:00
/*
* The width and height must be at least 1 pixel and width times
2011-09-02 20:16:41 +00:00
* 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 .
2009-06-30 12:42:43 +00:00
*/
2011-09-02 20:16:41 +00:00
_cur_resolution . width = ClampU ( _cur_resolution . width , 1 , UINT16_MAX / 2 ) ;
_cur_resolution . height = ClampU ( _cur_resolution . height , 1 , UINT16_MAX / 2 ) ;
2007-06-13 14:57:42 +00:00
2011-11-06 09:37:26 +00:00
/* 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 ;
2007-03-21 15:19:33 +00:00
/* enumerate language files */
2004-08-09 17:04:08 +00:00
InitializeLanguagePacks ( ) ;
2011-11-20 11:59:36 +00:00
/* Initialize the regular font for FreeType */
InitFreeType ( false ) ;
2006-11-16 22:05:33 +00:00
2009-09-13 19:15:59 +00:00
/* This must be done early, since functions use the SetWindowDirty* calls */
2004-08-25 10:17:39 +00:00
InitWindowSystem ( ) ;
2004-08-09 17:04:08 +00:00
2011-11-17 21:21:45 +00:00
BaseGraphics : : FindSets ( ) ;
2020-05-17 21:32:06 +00:00
if ( graphics_set . empty ( ) & & ! BaseGraphics : : ini_set . empty ( ) ) graphics_set = BaseGraphics : : ini_set ;
2012-07-08 18:41:50 +00:00
if ( ! BaseGraphics : : SetSet ( graphics_set ) ) {
2020-05-17 21:32:06 +00:00
if ( ! graphics_set . empty ( ) ) {
BaseGraphics : : SetSet ( { } ) ;
2012-07-08 18:41:50 +00:00
ErrorMessageData msg ( STR_CONFIG_ERROR , STR_CONFIG_ERROR_INVALID_BASE_GRAPHICS_NOT_FOUND ) ;
2020-05-17 21:32:06 +00:00
msg . SetDParamStr ( 0 , graphics_set . c_str ( ) ) ;
2012-07-08 18:41:50 +00:00
ScheduleErrorMessage ( msg ) ;
}
2008-08-24 08:41:38 +00:00
}
2006-11-28 14:32:24 +00:00
/* Initialize game palette */
GfxInitPalettes ( ) ;
2007-09-13 12:28:53 +00:00
DEBUG ( misc , 1 , " Loading blitter... " ) ;
2020-05-17 21:32:08 +00:00
if ( blitter . empty ( ) & & ! _ini_blitter . empty ( ) ) blitter = _ini_blitter ;
_blitter_autodetected = blitter . empty ( ) ;
2014-04-27 12:15:14 +00:00
/* Activate the initial blitter.
* This is only some initial guess , after NewGRFs have been loaded SwitchNewGRFBlitter may switch to a different one .
* - Never guess anything , if the user specified a blitter . ( _blitter_autodetected )
* - Use 32 bpp blitter if baseset or 8 bpp - support settings says so .
* - Use 8 bpp blitter otherwise .
*/
if ( ! _blitter_autodetected | |
2019-04-10 21:07:06 +00:00
( _support8bpp ! = S8BPP_NONE & & ( BaseGraphics : : GetUsedSet ( ) = = nullptr | | BaseGraphics : : GetUsedSet ( ) - > blitter = = BLT_8BPP ) ) | |
BlitterFactory : : SelectBlitter ( " 32bpp-anim " ) = = nullptr ) {
if ( BlitterFactory : : SelectBlitter ( blitter ) = = nullptr ) {
2020-05-17 21:32:08 +00:00
blitter . empty ( ) ?
2011-10-04 21:35:47 +00:00
usererror ( " Failed to autoprobe blitter " ) :
2020-05-17 21:32:08 +00:00
usererror ( " Failed to select requested blitter '%s'; does it exist? " , blitter . c_str ( ) ) ;
2011-10-04 21:35:47 +00:00
}
2010-07-24 10:14:39 +00:00
}
2007-07-05 12:23:54 +00:00
2020-05-17 21:32:08 +00:00
if ( videodriver . empty ( ) & & ! _ini_videodriver . empty ( ) ) videodriver = _ini_videodriver ;
2014-04-28 21:06:51 +00:00
DriverFactoryBase : : SelectDriver ( videodriver , Driver : : DT_VIDEO ) ;
2007-07-05 12:23:54 +00:00
2014-01-02 16:48:16 +00:00
InitializeSpriteSorter ( ) ;
2007-05-19 23:52:04 +00:00
/* Initialize the zoom level of the screen to normal */
_screen . zoom = ZOOM_LVL_NORMAL ;
2004-08-09 17:04:08 +00:00
2011-11-17 21:17:17 +00:00
NetworkStartUp ( ) ; // initialize network-core
2019-04-10 21:07:06 +00:00
if ( debuglog_conn ! = nullptr & & _network_available ) {
const char * not_used = nullptr ;
const char * port = nullptr ;
2007-02-08 12:27:53 +00:00
uint16 rport ;
rport = NETWORK_DEFAULT_DEBUGLOG_PORT ;
ParseConnectionString ( & not_used , & port , debuglog_conn ) ;
2019-04-10 21:07:06 +00:00
if ( port ! = nullptr ) rport = atoi ( port ) ;
2007-02-08 12:27:53 +00:00
2009-01-20 01:32:06 +00:00
NetworkStartDebugLog ( NetworkAddress ( debuglog_conn , rport ) ) ;
2007-02-08 12:27:53 +00:00
}
2013-11-24 16:21:19 +00:00
if ( ! HandleBootstrap ( ) ) {
ShutdownGame ( ) ;
2020-05-17 21:32:10 +00:00
return ret ;
2013-11-24 16:21:19 +00:00
}
2011-11-17 21:21:45 +00:00
2014-04-28 21:06:51 +00:00
VideoDriver : : GetInstance ( ) - > ClaimMousePointer ( ) ;
2011-11-17 21:21:45 +00:00
/* initialize screenshot formats */
InitializeScreenshotFormats ( ) ;
BaseSounds : : FindSets ( ) ;
2020-05-17 21:32:06 +00:00
if ( sounds_set . empty ( ) & & ! BaseSounds : : ini_set . empty ( ) ) sounds_set = BaseSounds : : ini_set ;
2011-11-17 21:21:45 +00:00
if ( ! BaseSounds : : SetSet ( sounds_set ) ) {
2020-05-17 21:32:06 +00:00
if ( sounds_set . empty ( ) | | ! BaseSounds : : SetSet ( { } ) ) {
2019-12-26 18:36:53 +00:00
usererror ( " Failed to find a sounds set. Please acquire a sounds set for OpenTTD. See section 1.4 of README.md. " ) ;
2012-07-08 18:41:50 +00:00
} else {
ErrorMessageData msg ( STR_CONFIG_ERROR , STR_CONFIG_ERROR_INVALID_BASE_SOUNDS_NOT_FOUND ) ;
2020-05-17 21:32:06 +00:00
msg . SetDParamStr ( 0 , sounds_set . c_str ( ) ) ;
2012-07-08 18:41:50 +00:00
ScheduleErrorMessage ( msg ) ;
}
2011-11-17 21:21:45 +00:00
}
BaseMusic : : FindSets ( ) ;
2020-05-17 21:32:06 +00:00
if ( music_set . empty ( ) & & ! BaseMusic : : ini_set . empty ( ) ) music_set = BaseMusic : : ini_set ;
2011-11-17 21:21:45 +00:00
if ( ! BaseMusic : : SetSet ( music_set ) ) {
2020-05-17 21:32:06 +00:00
if ( music_set . empty ( ) | | ! BaseMusic : : SetSet ( { } ) ) {
2019-12-26 18:36:53 +00:00
usererror ( " Failed to find a music set. Please acquire a music set for OpenTTD. See section 1.4 of README.md. " ) ;
2012-07-08 18:41:50 +00:00
} else {
ErrorMessageData msg ( STR_CONFIG_ERROR , STR_CONFIG_ERROR_INVALID_BASE_MUSIC_NOT_FOUND ) ;
2020-05-17 21:32:06 +00:00
msg . SetDParamStr ( 0 , music_set . c_str ( ) ) ;
2012-07-08 18:41:50 +00:00
ScheduleErrorMessage ( msg ) ;
}
2011-11-17 21:21:45 +00:00
}
2020-05-17 21:32:08 +00:00
if ( sounddriver . empty ( ) & & ! _ini_sounddriver . empty ( ) ) sounddriver = _ini_sounddriver ;
2014-04-28 21:06:51 +00:00
DriverFactoryBase : : SelectDriver ( sounddriver , Driver : : DT_SOUND ) ;
2011-11-17 21:21:45 +00:00
2020-05-17 21:32:08 +00:00
if ( musicdriver . empty ( ) & & ! _ini_musicdriver . empty ( ) ) musicdriver = _ini_musicdriver ;
2014-04-28 21:06:51 +00:00
DriverFactoryBase : : SelectDriver ( musicdriver , Driver : : DT_MUSIC ) ;
2011-11-17 21:21:45 +00:00
2009-05-03 15:44:05 +00:00
/* Take our initial lock on whatever we might want to do! */
2019-03-10 23:45:39 +00:00
try {
modal_work_lock . lock ( ) ;
modal_paint_lock . lock ( ) ;
} catch ( const std : : system_error & ) {
/* If there is some error we assume that threads aren't usable on the system we run. */
extern bool _use_threaded_modal_progress ; // From progress.cpp
_use_threaded_modal_progress = false ;
}
2009-05-03 15:44:05 +00:00
2010-01-17 22:59:24 +00:00
GenerateWorld ( GWM_EMPTY , 64 , 64 ) ; // Make the viewport initialization happy
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
WaitTillGeneratedWorld ( ) ;
2005-02-02 18:28:08 +00:00
2011-08-21 12:52:24 +00:00
LoadIntroGame ( false ) ;
2011-11-20 11:52:11 +00:00
CheckForMissingGlyphs ( ) ;
2009-02-12 10:13:30 +00:00
2013-11-25 09:12:49 +00:00
/* ScanNewGRFFiles now has control over the scanner. */
2020-05-17 21:32:10 +00:00
ScanNewGRFFiles ( scanner . release ( ) ) ;
2004-12-04 17:54:56 +00:00
2014-04-28 21:06:51 +00:00
VideoDriver : : GetInstance ( ) - > MainLoop ( ) ;
2004-08-25 10:17:39 +00:00
2005-08-05 09:15:41 +00:00
WaitTillSaved ( ) ;
2019-03-17 00:59:46 +00:00
WaitTillGeneratedWorld ( ) ; // Make sure any generate world threads have been joined.
2004-08-09 17:04:08 +00:00
2007-01-03 18:06:50 +00:00
/* only save config if we have to */
if ( save_config ) {
SaveToConfig ( ) ;
2010-07-03 13:28:15 +00:00
SaveHotkeysToConfig ( ) ;
2013-05-26 19:27:22 +00:00
WindowDesc : : SaveToConfig ( ) ;
2007-01-03 18:06:50 +00:00
SaveToHighScore ( ) ;
}
2004-08-09 17:04:08 +00:00
2008-05-08 23:26:17 +00:00
/* Reset windowing system, stop drivers, free used memory, ... */
ShutdownGame ( ) ;
2013-11-24 15:21:38 +00:00
return ret ;
2004-08-09 17:04:08 +00:00
}
2007-03-07 11:47:46 +00:00
void HandleExitGameRequest ( )
2006-09-04 17:30:30 +00:00
{
2011-11-17 21:17:17 +00:00
if ( _game_mode = = GM_MENU | | _game_mode = = GM_BOOTSTRAP ) { // do not ask to quit on the main screen
2006-09-04 17:30:30 +00:00
_exit_game = true ;
2008-05-29 15:13:28 +00:00
} else if ( _settings_client . gui . autosave_on_exit ) {
2006-09-04 17:30:30 +00:00
DoExitSave ( ) ;
_exit_game = true ;
} else {
AskExitGame ( ) ;
}
}
2007-03-07 11:47:46 +00:00
static void MakeNewGameDone ( )
2004-08-09 17:04:08 +00:00
{
2008-07-03 18:00:36 +00:00
SettingsDisableElrail ( _settings_game . vehicle . disable_elrails ) ;
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
/* In a dedicated server, the server does not play */
2014-04-28 21:06:51 +00:00
if ( ! VideoDriver : : GetInstance ( ) - > HasGUI ( ) ) {
2008-09-30 20:39:50 +00:00
SetLocalCompany ( COMPANY_SPECTATOR ) ;
2013-02-05 21:41:32 +00:00
if ( _settings_client . gui . pause_on_newgame ) DoCommandP ( 0 , PM_PAUSED_NORMAL , 1 , CMD_PAUSE ) ;
2009-01-12 17:11:45 +00:00
IConsoleCmdExec ( " exec scripts/game_start.scr 0 " ) ;
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
return ;
}
2004-08-09 17:04:08 +00:00
2008-09-30 20:39:50 +00:00
/* Create a single company */
DoStartupNewCompany ( false ) ;
2004-08-09 17:04:08 +00:00
2009-05-16 23:34:14 +00:00
Company * c = Company : : Get ( COMPANY_FIRST ) ;
2009-05-15 23:55:06 +00:00
c - > settings = _settings_client . company ;
2009-05-12 22:32:22 +00:00
2019-01-10 09:45:38 +00:00
/* Overwrite color from settings if needed
* COLOUR_END corresponds to Random colour */
if ( _settings_client . gui . starting_colour ! = COLOUR_END ) {
c - > colour = _settings_client . gui . starting_colour ;
ResetCompanyLivery ( c ) ;
_company_colours [ c - > index ] = ( Colours ) c - > colour ;
}
2009-01-12 17:11:45 +00:00
IConsoleCmdExec ( " exec scripts/game_start.scr 0 " ) ;
2008-09-30 20:39:50 +00:00
SetLocalCompany ( COMPANY_FIRST ) ;
2004-08-09 17:04:08 +00:00
2008-04-03 21:54:31 +00:00
InitializeRailGUI ( ) ;
2019-04-06 06:46:15 +00:00
InitializeRoadGUI ( ) ;
2006-11-17 19:31:44 +00:00
2008-09-30 20:39:50 +00:00
/* We are the server, we start a new company (not dedicated),
2007-12-02 14:48:26 +00:00
* so set the default password * if * needed . */
2008-05-29 20:21:28 +00:00
if ( _network_server & & ! StrEmpty ( _settings_client . network . default_company_pass ) ) {
2011-01-19 16:47:40 +00:00
NetworkChangeCompanyPassword ( _local_company , _settings_client . network . default_company_pass ) ;
2007-12-02 14:48:26 +00:00
}
2010-06-05 15:22:46 +00:00
if ( _settings_client . gui . pause_on_newgame ) DoCommandP ( 0 , PM_PAUSED_NORMAL , 1 , CMD_PAUSE ) ;
2013-07-02 18:57:26 +00:00
CheckEngines ( ) ;
2014-08-13 19:31:45 +00:00
CheckIndustries ( ) ;
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
MarkWholeScreenDirty ( ) ;
}
2004-08-09 17:04:08 +00:00
2009-08-06 22:00:32 +00:00
static void MakeNewGame ( bool from_heightmap , bool reset_settings )
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
{
_game_mode = GM_NORMAL ;
2004-08-09 17:04:08 +00:00
2006-12-10 12:12:26 +00:00
ResetGRFConfig ( true ) ;
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
GenerateWorldSetCallback ( & MakeNewGameDone ) ;
2010-01-17 22:59:24 +00:00
GenerateWorld ( from_heightmap ? GWM_HEIGHTMAP : GWM_NEWGAME , 1 < < _settings_game . game_creation . map_x , 1 < < _settings_game . game_creation . map_y , reset_settings ) ;
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
}
2004-09-10 19:02:27 +00:00
2007-03-07 11:47:46 +00:00
static void MakeNewEditorWorldDone ( )
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
{
2008-09-30 20:39:50 +00:00
SetLocalCompany ( OWNER_NONE ) ;
2004-08-09 17:04:08 +00:00
}
2007-03-07 11:47:46 +00:00
static void MakeNewEditorWorld ( )
2004-08-09 17:04:08 +00:00
{
_game_mode = GM_EDITOR ;
2006-12-10 12:12:26 +00:00
ResetGRFConfig ( true ) ;
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
GenerateWorldSetCallback ( & MakeNewEditorWorldDone ) ;
2010-01-17 22:59:24 +00:00
GenerateWorld ( GWM_EMPTY , 1 < < _settings_game . game_creation . map_x , 1 < < _settings_game . game_creation . map_y ) ;
2004-08-09 17:04:08 +00:00
}
2010-06-30 20:20:15 +00:00
/**
* 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 .
2010-12-05 14:43:10 +00:00
* @ param filename file to be loaded
2018-10-28 02:17:36 +00:00
* @ param fop mode of loading , always SLO_LOAD
* @ param newgm switch to this mode of loading fails due to some unknown error
2010-06-30 20:20:15 +00:00
* @ param subdir default directory to look for filename , set to 0 if not needed
2019-04-10 21:07:06 +00:00
* @ param lf Load filter to use , if nullptr : use filename + subdir .
2010-06-30 20:20:15 +00:00
*/
2019-04-10 21:07:06 +00:00
bool SafeLoad ( const char * filename , SaveLoadOperation fop , DetailedFileType dft , GameMode newgm , Subdirectory subdir , struct LoadFilter * lf = nullptr )
2010-06-30 20:20:15 +00:00
{
2016-09-04 16:06:50 +00:00
assert ( fop = = SLO_LOAD ) ;
2019-04-10 21:07:06 +00:00
assert ( dft = = DFT_GAME_FILE | | ( lf = = nullptr & & dft = = DFT_OLD_GAME_FILE ) ) ;
2010-06-30 20:20:15 +00:00
GameMode ogm = _game_mode ;
_game_mode = newgm ;
2010-12-05 14:43:10 +00:00
2019-04-10 21:07:06 +00:00
switch ( lf = = nullptr ? SaveOrLoad ( filename , fop , dft , subdir ) : LoadWithFilter ( lf ) ) {
2010-06-30 20:20:15 +00:00
case SL_OK : return true ;
case SL_REINIT :
2010-06-30 21:38:51 +00:00
if ( _network_dedicated ) {
/*
* We need to reinit a network map . . .
* We can ' t simply load the intro game here as that game has many
* special cases which make clients desync immediately . So we fall
* back to just generating a new game with the current settings .
*/
DEBUG ( net , 0 , " Loading game failed, so a new (random) game will be started! " ) ;
MakeNewGame ( false , true ) ;
return false ;
}
2010-08-15 11:58:20 +00:00
if ( _network_server ) {
/* We can't load the intro game as server, so disconnect first. */
NetworkDisconnect ( ) ;
}
2010-06-30 21:38:51 +00:00
2010-06-30 20:20:15 +00:00
switch ( ogm ) {
default :
case GM_MENU : LoadIntroGame ( ) ; break ;
case GM_EDITOR : MakeNewEditorWorld ( ) ; break ;
}
return false ;
default :
_game_mode = ogm ;
return false ;
}
}
2009-02-25 00:45:52 +00:00
void SwitchToMode ( SwitchMode new_mode )
2004-08-09 17:04:08 +00:00
{
2007-03-21 15:19:33 +00:00
/* If we are saving something, the network stays in his current state */
2011-05-28 13:55:34 +00:00
if ( new_mode ! = SM_SAVE_GAME ) {
2007-03-21 15:19:33 +00:00
/* If the network is active, make it not-active */
2004-12-04 17:54:56 +00:00
if ( _networking ) {
2011-05-28 13:55:34 +00:00
if ( _network_server & & ( new_mode = = SM_LOAD_GAME | | new_mode = = SM_NEWGAME | | new_mode = = SM_RESTARTGAME ) ) {
2004-12-04 17:54:56 +00:00
NetworkReboot ( ) ;
} else {
NetworkDisconnect ( ) ;
}
}
2007-03-21 15:19:33 +00:00
/* If we are a server, we restart the server */
2004-12-04 17:54:56 +00:00
if ( _is_network_server ) {
2007-03-21 15:19:33 +00:00
/* But not if we are going to the menu */
2004-12-04 17:54:56 +00:00
if ( new_mode ! = SM_MENU ) {
2008-01-13 01:39:22 +00:00
/* check if we should reload the config */
2008-05-29 20:21:28 +00:00
if ( _settings_client . network . reload_cfg ) {
2008-01-13 01:39:22 +00:00
LoadFromConfig ( ) ;
2009-01-12 17:11:45 +00:00
MakeNewgameSettingsLive ( ) ;
2008-01-13 01:39:22 +00:00
ResetGRFConfig ( false ) ;
}
2004-12-04 17:54:56 +00:00
NetworkServerStart ( ) ;
} else {
2007-03-21 15:19:33 +00:00
/* This client no longer wants to be a network-server */
2004-12-04 17:54:56 +00:00
_is_network_server = false ;
}
}
}
Remove: ENABLE_NETWORK switch
This switch has been a pain for years. Often disabling broke
compilation, as no developer compiles OpenTTD without, neither do
any of our official binaries.
Additionaly, it has grown so hugely in our codebase, that it
clearly shows that the current solution was a poor one. 350+
instances of "#ifdef ENABLE_NETWORK" were in the code, of which
only ~30 in the networking code itself. The rest were all around
the code to do the right thing, from GUI to NewGRF.
A more proper solution would be to stub all the functions, and
make sure the rest of the code can simply assume network is
available. This was also partially done, and most variables were
correct if networking was disabled. Despite that, often the #ifdefs
were still used.
With the recent removal of DOS, there is also no platform anymore
which we support where networking isn't working out-of-the-box.
All in all, it is time to remove the ENABLE_NETWORK switch. No
replacement is planned, but if you feel we really need this option,
we welcome any Pull Request which implements this in a way that
doesn't crawl through the code like this diff shows we used to.
2019-03-20 16:01:13 +00:00
2013-01-08 22:46:42 +00:00
/* Make sure all AI controllers are gone at quitting game */
2011-05-28 13:55:34 +00:00
if ( new_mode ! = SM_SAVE_GAME ) AI : : KillAll ( ) ;
2004-12-04 17:54:56 +00:00
2005-03-12 21:21:47 +00:00
switch ( new_mode ) {
2009-03-15 00:32:18 +00:00
case SM_EDITOR : // Switch to scenario editor
2008-04-29 18:19:29 +00:00
MakeNewEditorWorld ( ) ;
break ;
2004-08-09 17:04:08 +00:00
2019-03-05 04:04:39 +00:00
case SM_RESTARTGAME : // Restart --> Current settings preserved
if ( _file_to_saveload . abstract_ftype = = FT_SAVEGAME | | _file_to_saveload . abstract_ftype = = FT_SCENARIO ) {
/* Restart current savegame/scenario */
_switch_mode = _game_mode = = GM_EDITOR ? SM_LOAD_SCENARIO : SM_LOAD_GAME ;
SwitchToMode ( _switch_mode ) ;
break ;
} else if ( _file_to_saveload . abstract_ftype = = FT_HEIGHTMAP ) {
/* Restart current heightmap */
_switch_mode = _game_mode = = GM_EDITOR ? SM_LOAD_HEIGHTMAP : SM_RESTART_HEIGHTMAP ;
SwitchToMode ( _switch_mode ) ;
break ;
}
/* No break here, to enter the next case:
* Restart - - > ' Random game ' with current settings */
FALLTHROUGH ;
2009-03-15 00:32:18 +00:00
case SM_NEWGAME : // New Game --> 'Random game'
2008-04-29 18:19:29 +00:00
if ( _network_server ) {
2014-04-23 21:12:09 +00:00
seprintf ( _network_game_info . map_name , lastof ( _network_game_info . map_name ) , " Random Map " ) ;
2008-04-29 18:19:29 +00:00
}
2009-08-06 22:00:32 +00:00
MakeNewGame ( false , new_mode = = SM_NEWGAME ) ;
2008-04-29 18:19:29 +00:00
break ;
2004-08-09 17:04:08 +00:00
2011-05-28 13:55:34 +00:00
case SM_LOAD_GAME : { // Load game, Play Scenario
2008-04-29 18:19:29 +00:00
ResetGRFConfig ( true ) ;
ResetWindowSystem ( ) ;
2004-08-09 17:04:08 +00:00
2016-09-04 12:57:43 +00:00
if ( ! SafeLoad ( _file_to_saveload . name , _file_to_saveload . file_op , _file_to_saveload . detail_ftype , GM_NORMAL , NO_DIRECTORY ) ) {
2008-12-26 18:24:05 +00:00
SetDParamStr ( 0 , GetSaveLoadErrorString ( ) ) ;
2010-02-24 14:46:15 +00:00
ShowErrorMessage ( STR_JUST_RAW_STRING , INVALID_STRING_ID , WL_ERROR ) ;
2008-04-29 18:19:29 +00:00
} else {
2016-09-04 12:57:43 +00:00
if ( _file_to_saveload . abstract_ftype = = FT_SCENARIO ) {
2012-11-12 20:13:51 +00:00
/* Reset engine pool to simplify changing engine NewGRFs in scenario editor. */
EngineOverrideManager : : ResetToCurrentNewGRFConfig ( ) ;
2008-05-04 22:38:18 +00:00
}
2008-09-30 20:39:50 +00:00
/* Update the local company for a loaded game. It is either always
2009-03-14 18:16:29 +00:00
* company # 1 ( eg 0 ) or in the case of a dedicated server a spectator */
2008-09-30 20:39:50 +00:00
SetLocalCompany ( _network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST ) ;
2009-01-12 17:11:45 +00:00
/* Execute the game-start script */
IConsoleCmdExec ( " exec scripts/game_start.scr 0 " ) ;
2008-04-29 18:19:29 +00:00
/* Decrease pause counter (was increased from opening load dialog) */
2009-05-06 15:06:57 +00:00
DoCommandP ( 0 , PM_PAUSED_SAVELOAD , 0 , CMD_PAUSE ) ;
2008-04-29 18:19:29 +00:00
if ( _network_server ) {
2014-04-23 21:12:09 +00:00
seprintf ( _network_game_info . map_name , lastof ( _network_game_info . map_name ) , " %s (Loaded game) " , _file_to_saveload . title ) ;
2008-04-29 18:19:29 +00:00
}
}
break ;
2004-08-09 17:04:08 +00:00
}
2019-03-05 04:04:39 +00:00
case SM_RESTART_HEIGHTMAP : // Load a heightmap and start a new game from it with current settings
2009-03-15 00:32:18 +00:00
case SM_START_HEIGHTMAP : // Load a heightmap and start a new game from it
2008-04-29 18:19:29 +00:00
if ( _network_server ) {
2014-04-23 21:12:09 +00:00
seprintf ( _network_game_info . map_name , lastof ( _network_game_info . map_name ) , " %s (Heightmap) " , _file_to_saveload . title ) ;
2008-04-29 18:19:29 +00:00
}
2019-03-05 04:04:39 +00:00
MakeNewGame ( true , new_mode = = SM_START_HEIGHTMAP ) ;
2008-04-29 18:19:29 +00:00
break ;
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
2009-03-15 00:32:18 +00:00
case SM_LOAD_HEIGHTMAP : // Load heightmap from scenario editor
2008-09-30 20:39:50 +00:00
SetLocalCompany ( OWNER_NONE ) ;
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
2010-01-17 22:59:24 +00:00
GenerateWorld ( GWM_HEIGHTMAP , 1 < < _settings_game . game_creation . map_x , 1 < < _settings_game . game_creation . map_y ) ;
2008-04-29 18:19:29 +00:00
MarkWholeScreenDirty ( ) ;
break ;
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
2009-03-15 00:32:18 +00:00
case SM_LOAD_SCENARIO : { // Load scenario from scenario editor
2016-09-04 12:57:43 +00:00
if ( SafeLoad ( _file_to_saveload . name , _file_to_saveload . file_op , _file_to_saveload . detail_ftype , GM_EDITOR , NO_DIRECTORY ) ) {
2008-09-30 20:39:50 +00:00
SetLocalCompany ( OWNER_NONE ) ;
2008-05-25 19:17:03 +00:00
_settings_newgame . game_creation . starting_year = _cur_year ;
2009-12-22 08:34:40 +00:00
/* Cancel the saveload pausing */
DoCommandP ( 0 , PM_PAUSED_SAVELOAD , 0 , CMD_PAUSE ) ;
2008-04-29 18:19:29 +00:00
} else {
2008-12-26 18:24:05 +00:00
SetDParamStr ( 0 , GetSaveLoadErrorString ( ) ) ;
2010-02-24 14:46:15 +00:00
ShowErrorMessage ( STR_JUST_RAW_STRING , INVALID_STRING_ID , WL_ERROR ) ;
2008-04-29 18:19:29 +00:00
}
break ;
2005-11-14 19:48:04 +00:00
}
2004-08-09 17:04:08 +00:00
2009-03-15 00:32:18 +00:00
case SM_MENU : // Switch to game intro menu
2008-04-29 18:19:29 +00:00
LoadIntroGame ( ) ;
2020-12-08 09:24:59 +00:00
if ( BaseSounds : : ini_set . empty ( ) & & BaseSounds : : GetUsedSet ( ) - > fallback & & SoundDriver : : GetInstance ( ) - > HasOutput ( ) ) {
2010-02-24 14:46:15 +00:00
ShowErrorMessage ( STR_WARNING_FALLBACK_SOUNDSET , INVALID_STRING_ID , WL_CRITICAL ) ;
2020-05-17 21:32:06 +00:00
BaseSounds : : ini_set = BaseSounds : : GetUsedSet ( ) - > name ;
2010-02-22 20:19:17 +00:00
}
2008-04-29 18:19:29 +00:00
break ;
2011-05-28 13:55:34 +00:00
case SM_SAVE_GAME : // Save game.
2008-04-29 18:19:29 +00:00
/* Make network saved games on pause compatible to singleplayer */
2016-09-04 16:06:50 +00:00
if ( SaveOrLoad ( _file_to_saveload . name , SLO_SAVE , DFT_GAME_FILE , NO_DIRECTORY ) ! = SL_OK ) {
2008-12-26 18:24:05 +00:00
SetDParamStr ( 0 , GetSaveLoadErrorString ( ) ) ;
2010-02-24 14:46:15 +00:00
ShowErrorMessage ( STR_JUST_RAW_STRING , INVALID_STRING_ID , WL_ERROR ) ;
2008-04-29 18:19:29 +00:00
} else {
DeleteWindowById ( WC_SAVELOAD , 0 ) ;
}
break ;
2011-05-28 13:55:34 +00:00
case SM_SAVE_HEIGHTMAP : // Save heightmap.
MakeHeightmapScreenshot ( _file_to_saveload . name ) ;
DeleteWindowById ( WC_SAVELOAD , 0 ) ;
break ;
2009-03-15 00:32:18 +00:00
case SM_GENRANDLAND : // Generate random land within scenario editor
2008-09-30 20:39:50 +00:00
SetLocalCompany ( OWNER_NONE ) ;
2010-01-17 22:59:24 +00:00
GenerateWorld ( GWM_RANDOM , 1 < < _settings_game . game_creation . map_x , 1 < < _settings_game . game_creation . map_y ) ;
2008-04-29 18:19:29 +00:00
/* XXX: set date */
MarkWholeScreenDirty ( ) ;
break ;
2009-02-25 00:45:52 +00:00
default : NOT_REACHED ( ) ;
2004-08-09 17:04:08 +00:00
}
}
2010-03-13 14:58:37 +00:00
/**
* 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 .
*/
static void CheckCaches ( )
{
2011-12-17 21:20:41 +00:00
/* Return here so it is easy to add checks that are run
* always to aid testing of caches . */
if ( _debug_desync_level < = 1 ) return ;
2012-04-25 21:06:31 +00:00
/* Check the town caches. */
2019-03-03 17:30:09 +00:00
std : : vector < TownCache > old_town_caches ;
2019-12-17 21:04:09 +00:00
for ( const Town * t : Town : : Iterate ( ) ) {
2019-02-18 22:39:06 +00:00
old_town_caches . push_back ( t - > cache ) ;
2012-04-25 21:06:31 +00:00
}
extern void RebuildTownCaches ( ) ;
RebuildTownCaches ( ) ;
2012-08-18 11:38:06 +00:00
RebuildSubsidisedSourceAndDestinationCache ( ) ;
2012-04-25 21:06:31 +00:00
uint i = 0 ;
2019-12-17 21:04:09 +00:00
for ( Town * t : Town : : Iterate ( ) ) {
2018-09-25 20:20:24 +00:00
if ( MemCmpT ( old_town_caches . data ( ) + i , & t - > cache ) ! = 0 ) {
2012-04-25 21:06:31 +00:00
DEBUG ( desync , 2 , " town cache mismatch: town %i " , ( int ) t - > index ) ;
}
i + + ;
}
2011-12-03 23:40:13 +00:00
/* Check company infrastructure cache. */
2019-03-03 17:30:09 +00:00
std : : vector < CompanyInfrastructure > old_infrastructure ;
2019-12-14 16:22:38 +00:00
for ( const Company * c : Company : : Iterate ( ) ) old_infrastructure . push_back ( c - > infrastructure ) ;
2011-12-03 23:40:13 +00:00
extern void AfterLoadCompanyStats ( ) ;
AfterLoadCompanyStats ( ) ;
2012-04-25 21:06:31 +00:00
i = 0 ;
2019-12-14 16:22:38 +00:00
for ( const Company * c : Company : : Iterate ( ) ) {
2018-09-25 20:20:24 +00:00
if ( MemCmpT ( old_infrastructure . data ( ) + i , & c - > infrastructure ) ! = 0 ) {
2011-12-03 23:47:42 +00:00
DEBUG ( desync , 2 , " infrastructure cache mismatch: company %i " , ( int ) c - > index ) ;
2011-12-03 23:40:13 +00:00
}
i + + ;
}
2010-03-13 14:58:37 +00:00
/* Strict checking of the road stop cache entries */
2019-12-17 18:11:24 +00:00
for ( const RoadStop * rs : RoadStop : : Iterate ( ) ) {
2010-03-13 14:58:37 +00:00
if ( IsStandardRoadStopTile ( rs - > xy ) ) continue ;
assert ( rs - > GetEntry ( DIAGDIR_NE ) ! = rs - > GetEntry ( DIAGDIR_NW ) ) ;
rs - > GetEntry ( DIAGDIR_NE ) - > CheckIntegrity ( rs ) ;
rs - > GetEntry ( DIAGDIR_NW ) - > CheckIntegrity ( rs ) ;
}
2019-12-17 02:37:43 +00:00
for ( Vehicle * v : Vehicle : : Iterate ( ) ) {
2010-11-27 21:09:41 +00:00
extern void FillNewGRFVehicleCache ( const Vehicle * v ) ;
2010-07-13 20:12:44 +00:00
if ( v ! = v - > First ( ) | | v - > vehstatus & VS_CRASHED | | ! v - > IsPrimaryVehicle ( ) ) continue ;
uint length = 0 ;
2019-04-10 21:07:06 +00:00
for ( const Vehicle * u = v ; u ! = nullptr ; u = u - > Next ( ) ) length + + ;
2010-07-13 20:12:44 +00:00
2010-12-14 21:28:45 +00:00
NewGRFCache * grf_cache = CallocT < NewGRFCache > ( length ) ;
VehicleCache * veh_cache = CallocT < VehicleCache > ( length ) ;
GroundVehicleCache * gro_cache = CallocT < GroundVehicleCache > ( length ) ;
TrainCache * tra_cache = CallocT < TrainCache > ( length ) ;
2010-07-13 20:12:44 +00:00
length = 0 ;
2019-04-10 21:07:06 +00:00
for ( const Vehicle * u = v ; u ! = nullptr ; u = u - > Next ( ) ) {
2010-11-27 21:09:41 +00:00
FillNewGRFVehicleCache ( u ) ;
2010-11-06 12:37:55 +00:00
grf_cache [ length ] = u - > grf_cache ;
2010-11-06 12:53:31 +00:00
veh_cache [ length ] = u - > vcache ;
2010-07-13 20:12:44 +00:00
switch ( u - > type ) {
case VEH_TRAIN :
2010-12-14 21:28:45 +00:00
gro_cache [ length ] = Train : : From ( u ) - > gcache ;
2010-07-13 20:12:44 +00:00
tra_cache [ length ] = Train : : From ( u ) - > tcache ;
break ;
case VEH_ROAD :
2010-12-14 21:28:45 +00:00
gro_cache [ length ] = RoadVehicle : : From ( u ) - > gcache ;
2010-07-13 20:12:44 +00:00
break ;
default :
break ;
}
length + + ;
}
2010-03-13 14:58:37 +00:00
switch ( v - > type ) {
2014-02-07 23:48:56 +00:00
case VEH_TRAIN : Train : : From ( v ) - > ConsistChanged ( CCF_TRACK ) ; break ;
2010-07-13 20:12:44 +00:00
case VEH_ROAD : RoadVehUpdateCache ( RoadVehicle : : From ( v ) ) ; break ;
case VEH_AIRCRAFT : UpdateAircraftCache ( Aircraft : : From ( v ) ) ; break ;
2010-11-27 21:11:37 +00:00
case VEH_SHIP : Ship : : From ( v ) - > UpdateCache ( ) ; break ;
2010-07-13 20:12:44 +00:00
default : break ;
}
2010-03-13 14:58:37 +00:00
2010-07-13 20:12:44 +00:00
length = 0 ;
2019-04-10 21:07:06 +00:00
for ( const Vehicle * u = v ; u ! = nullptr ; u = u - > Next ( ) ) {
2010-11-27 21:09:41 +00:00
FillNewGRFVehicleCache ( u ) ;
2010-11-06 12:37:55 +00:00
if ( memcmp ( & grf_cache [ length ] , & u - > grf_cache , sizeof ( NewGRFCache ) ) ! = 0 ) {
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 ) ;
2010-07-13 20:12:44 +00:00
}
2010-11-06 12:53:31 +00:00
if ( memcmp ( & veh_cache [ length ] , & u - > vcache , sizeof ( VehicleCache ) ) ! = 0 ) {
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 ) ;
}
2010-07-13 20:12:44 +00:00
switch ( u - > type ) {
case VEH_TRAIN :
2010-12-14 21:28:45 +00:00
if ( memcmp ( & gro_cache [ length ] , & Train : : From ( u ) - > gcache , sizeof ( GroundVehicleCache ) ) ! = 0 ) {
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 ) ;
2010-03-13 14:58:37 +00:00
}
2010-07-13 20:12:44 +00:00
if ( memcmp ( & tra_cache [ length ] , & Train : : From ( u ) - > tcache , sizeof ( TrainCache ) ) ! = 0 ) {
DEBUG ( desync , 2 , " train cache mismatch: vehicle %i, company %i, unit number %i, wagon %i " , v - > index , ( int ) v - > owner , v - > unitnumber , length ) ;
}
break ;
case VEH_ROAD :
2010-12-14 21:28:45 +00:00
if ( memcmp ( & gro_cache [ length ] , & RoadVehicle : : From ( u ) - > gcache , sizeof ( GroundVehicleCache ) ) ! = 0 ) {
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 ) ;
2010-07-13 20:12:44 +00:00
}
break ;
default :
break ;
}
length + + ;
2010-03-13 14:58:37 +00:00
}
2010-07-13 20:12:44 +00:00
2010-11-06 12:37:55 +00:00
free ( grf_cache ) ;
2010-11-06 12:53:31 +00:00
free ( veh_cache ) ;
2010-12-14 21:28:45 +00:00
free ( gro_cache ) ;
2010-07-13 20:12:44 +00:00
free ( tra_cache ) ;
2010-03-13 14:58:37 +00:00
}
/* Check whether the caches are still valid */
2019-12-17 02:37:43 +00:00
for ( Vehicle * v : Vehicle : : Iterate ( ) ) {
2010-03-13 14:58:37 +00:00
byte buff [ sizeof ( VehicleCargoList ) ] ;
memcpy ( buff , & v - > cargo , sizeof ( VehicleCargoList ) ) ;
v - > cargo . InvalidateCache ( ) ;
assert ( memcmp ( & v - > cargo , buff , sizeof ( VehicleCargoList ) ) = = 0 ) ;
}
2020-05-11 23:19:52 +00:00
/* Backup stations_near */
std : : vector < StationList > old_town_stations_near ;
for ( Town * t : Town : : Iterate ( ) ) old_town_stations_near . push_back ( t - > stations_near ) ;
std : : vector < StationList > old_industry_stations_near ;
for ( Industry * ind : Industry : : Iterate ( ) ) old_industry_stations_near . push_back ( ind - > stations_near ) ;
2019-12-15 04:55:59 +00:00
for ( Station * st : Station : : Iterate ( ) ) {
2010-03-13 14:58:37 +00:00
for ( CargoID c = 0 ; c < NUM_CARGO ; c + + ) {
byte buff [ sizeof ( StationCargoList ) ] ;
memcpy ( buff , & st - > goods [ c ] . cargo , sizeof ( StationCargoList ) ) ;
st - > goods [ c ] . cargo . InvalidateCache ( ) ;
assert ( memcmp ( & st - > goods [ c ] . cargo , buff , sizeof ( StationCargoList ) ) = = 0 ) ;
}
2020-05-06 23:14:22 +00:00
/* Check docking tiles */
TileArea ta ;
std : : map < TileIndex , bool > docking_tiles ;
TILE_AREA_LOOP ( tile , st - > docking_station ) {
ta . Add ( tile ) ;
docking_tiles [ tile ] = IsDockingTile ( tile ) ;
}
UpdateStationDockingTiles ( st ) ;
if ( ta . tile ! = st - > docking_station . tile | | ta . w ! = st - > docking_station . w | | ta . h ! = st - > docking_station . h ) {
DEBUG ( desync , 2 , " station docking mismatch: station %i, company %i " , st - > index , ( int ) st - > owner ) ;
}
TILE_AREA_LOOP ( tile , ta ) {
if ( docking_tiles [ tile ] ! = IsDockingTile ( tile ) ) {
DEBUG ( desync , 2 , " docking tile mismatch: tile %i " , ( int ) tile ) ;
}
}
2020-05-11 23:19:52 +00:00
/* Check industries_near */
IndustryList industries_near = st - > industries_near ;
st - > RecomputeCatchment ( ) ;
if ( st - > industries_near ! = industries_near ) {
DEBUG ( desync , 2 , " station industries near mismatch: station %i " , st - > index ) ;
}
}
/* Check stations_near */
i = 0 ;
for ( Town * t : Town : : Iterate ( ) ) {
if ( t - > stations_near ! = old_town_stations_near [ i ] ) {
DEBUG ( desync , 2 , " town stations near mismatch: town %i " , t - > index ) ;
}
i + + ;
}
i = 0 ;
for ( Industry * ind : Industry : : Iterate ( ) ) {
if ( ind - > stations_near ! = old_industry_stations_near [ i ] ) {
DEBUG ( desync , 2 , " industry stations near mismatch: industry %i " , ind - > index ) ;
}
i + + ;
2010-03-13 14:58:37 +00:00
}
}
2008-04-29 18:19:29 +00:00
/**
* State controlling game loop .
* The state must not be changed from anywhere but here .
* That check is enforced in DoCommand .
*/
2007-03-07 11:47:46 +00:00
void StateGameLoop ( )
2004-08-09 17:04:08 +00:00
{
2013-01-08 22:46:42 +00:00
/* don't execute the state loop during pause */
2009-05-06 15:06:57 +00:00
if ( _pause_mode ! = PM_UNPAUSED ) {
2018-07-19 19:17:07 +00:00
PerformanceMeasurer : : Paused ( PFE_GAMELOOP ) ;
PerformanceMeasurer : : Paused ( PFE_GL_ECONOMY ) ;
PerformanceMeasurer : : Paused ( PFE_GL_TRAINS ) ;
PerformanceMeasurer : : Paused ( PFE_GL_ROADVEHS ) ;
PerformanceMeasurer : : Paused ( PFE_GL_SHIPS ) ;
PerformanceMeasurer : : Paused ( PFE_GL_AIRCRAFT ) ;
PerformanceMeasurer : : Paused ( PFE_GL_LANDSCAPE ) ;
2011-01-04 22:50:09 +00:00
UpdateLandscapingLimits ( ) ;
2014-02-23 19:32:00 +00:00
# ifndef DEBUG_DUMP_COMMANDS
2011-12-19 20:50:54 +00:00
Game : : GameLoop ( ) ;
2014-02-23 19:32:00 +00:00
# endif
2008-01-01 22:34:00 +00:00
return ;
}
2018-07-19 19:17:07 +00:00
PerformanceMeasurer framerate ( PFE_GAMELOOP ) ;
PerformanceAccumulator : : Reset ( PFE_GL_LANDSCAPE ) ;
2011-08-21 12:46:46 +00:00
if ( HasModalProgress ( ) ) return ;
2004-09-12 15:29:37 +00:00
2013-07-06 19:00:33 +00:00
Layouter : : ReduceLineCache ( ) ;
2004-08-09 17:04:08 +00:00
if ( _game_mode = = GM_EDITOR ) {
2014-02-23 22:03:08 +00:00
BasePersistentStorageArray : : SwitchMode ( PSM_ENTER_GAMELOOP ) ;
2004-08-09 17:04:08 +00:00
RunTileLoop ( ) ;
CallVehicleTicks ( ) ;
CallLandscapeTick ( ) ;
2014-02-23 22:03:08 +00:00
BasePersistentStorageArray : : SwitchMode ( PSM_LEAVE_GAMELOOP ) ;
2011-01-04 22:50:09 +00:00
UpdateLandscapingLimits ( ) ;
2008-01-15 18:51:46 +00:00
2018-05-04 20:29:22 +00:00
CallWindowGameTickEvent ( ) ;
2004-08-09 17:04:08 +00:00
NewsLoop ( ) ;
} else {
2010-04-09 16:01:48 +00:00
if ( _debug_desync_level > 2 & & _date_fract = = 0 & & ( _date & 0x1F ) = = 0 ) {
/* Save the desync savegame if needed. */
char name [ MAX_PATH ] ;
2014-04-23 21:12:09 +00:00
seprintf ( name , lastof ( name ) , " dmp_cmds_%08x_%08x.sav " , _settings_game . game_creation . generation_seed , _date ) ;
2016-09-04 16:06:50 +00:00
SaveOrLoad ( name , SLO_SAVE , DFT_GAME_FILE , AUTOSAVE_DIR , false ) ;
2010-04-09 16:01:48 +00:00
}
2010-04-21 12:55:33 +00:00
CheckCaches ( ) ;
2007-03-21 15:19:33 +00:00
/* All these actions has to be done from OWNER_NONE
* for multiplayer compatibility */
2019-04-22 08:10:04 +00:00
Backup < CompanyID > cur_company ( _current_company , OWNER_NONE , FILE_LINE ) ;
2004-09-11 19:34:11 +00:00
2014-02-23 22:03:08 +00:00
BasePersistentStorageArray : : SwitchMode ( PSM_ENTER_GAMELOOP ) ;
2004-08-09 17:04:08 +00:00
AnimateAnimatedTiles ( ) ;
IncreaseDate ( ) ;
RunTileLoop ( ) ;
CallVehicleTicks ( ) ;
CallLandscapeTick ( ) ;
2014-02-23 22:03:08 +00:00
BasePersistentStorageArray : : SwitchMode ( PSM_LEAVE_GAMELOOP ) ;
2004-08-09 17:04:08 +00:00
2014-02-23 19:32:00 +00:00
# ifndef DEBUG_DUMP_COMMANDS
2019-02-04 00:26:55 +00:00
{
PerformanceMeasurer framerate ( PFE_ALLSCRIPTS ) ;
AI : : GameLoop ( ) ;
Game : : GameLoop ( ) ;
}
2014-02-23 19:32:00 +00:00
# endif
2011-01-04 22:50:09 +00:00
UpdateLandscapingLimits ( ) ;
2004-08-09 17:04:08 +00:00
2018-05-04 20:29:22 +00:00
CallWindowGameTickEvent ( ) ;
2004-08-09 17:04:08 +00:00
NewsLoop ( ) ;
2010-05-31 20:22:57 +00:00
cur_company . Restore ( ) ;
2004-08-09 17:04:08 +00:00
}
2010-06-05 13:32:42 +00:00
2010-07-31 21:43:07 +00:00
assert ( IsLocalCompany ( ) ) ;
2004-08-09 17:04:08 +00:00
}
2010-08-01 19:22:34 +00:00
/**
* Create an autosave . The default name is " autosave#.sav " . However with
2010-08-01 19:44:49 +00:00
* the setting ' keep_all_autosave ' the name defaults to company - name + date
*/
2007-03-07 11:47:46 +00:00
static void DoAutosave ( )
2004-08-09 17:04:08 +00:00
{
2007-06-17 15:48:57 +00:00
char buf [ MAX_PATH ] ;
2004-09-10 19:02:27 +00:00
2009-02-21 15:23:49 +00:00
if ( _settings_client . gui . keep_all_autosave ) {
2009-01-02 22:49:43 +00:00
GenerateDefaultSaveName ( buf , lastof ( buf ) ) ;
2008-11-02 11:20:15 +00:00
strecat ( buf , " .sav " , lastof ( buf ) ) ;
2007-06-17 15:48:57 +00:00
} else {
2009-04-09 01:22:33 +00:00
static int _autosave_ctr = 0 ;
2008-05-29 15:13:28 +00:00
/* generate a savegame name and number according to _settings_client.gui.max_num_autosaves */
2014-04-23 21:12:09 +00:00
seprintf ( buf , lastof ( buf ) , " autosave%d.sav " , _autosave_ctr ) ;
2007-06-17 15:48:57 +00:00
2008-05-29 15:13:28 +00:00
if ( + + _autosave_ctr > = _settings_client . gui . max_num_autosaves ) _autosave_ctr = 0 ;
2004-08-09 17:04:08 +00:00
}
2006-12-26 17:36:18 +00:00
DEBUG ( sl , 2 , " Autosaving to '%s' " , buf ) ;
2016-09-04 16:06:50 +00:00
if ( SaveOrLoad ( buf , SLO_SAVE , DFT_GAME_FILE , AUTOSAVE_DIR ) ! = SL_OK ) {
2010-02-24 14:46:15 +00:00
ShowErrorMessage ( STR_ERROR_AUTOSAVE_FAILED , INVALID_STRING_ID , WL_ERROR ) ;
2008-04-29 18:19:29 +00:00
}
2004-08-09 17:04:08 +00:00
}
2007-03-07 11:47:46 +00:00
void GameLoop ( )
2004-08-09 17:04:08 +00:00
{
2011-11-17 21:17:17 +00:00
if ( _game_mode = = GM_BOOTSTRAP ) {
/* Check for UDP stuff */
2012-01-04 22:08:43 +00:00
if ( _network_available ) NetworkBackgroundLoop ( ) ;
2011-11-17 21:17:17 +00:00
InputLoop ( ) ;
return ;
}
2008-04-19 10:18:38 +00:00
ProcessAsyncSaveFinish ( ) ;
2004-08-09 17:04:08 +00:00
2007-03-21 15:19:33 +00:00
/* autosave game? */
2004-08-09 17:04:08 +00:00
if ( _do_autosave ) {
DoAutosave ( ) ;
2014-02-09 20:37:36 +00:00
_do_autosave = false ;
2010-06-05 19:02:29 +00:00
SetWindowDirty ( WC_STATUS_BAR , 0 ) ;
2004-08-09 17:04:08 +00:00
}
2007-03-21 15:19:33 +00:00
/* switch game mode? */
2011-08-21 12:52:24 +00:00
if ( _switch_mode ! = SM_NONE & & ! HasModalProgress ( ) ) {
2009-02-25 00:45:52 +00:00
SwitchToMode ( _switch_mode ) ;
2004-08-09 17:04:08 +00:00
_switch_mode = SM_NONE ;
}
IncreaseSpriteLRU ( ) ;
InteractiveRandom ( ) ;
2007-03-21 15:19:33 +00:00
/* Check for UDP stuff */
2012-01-04 22:08:43 +00:00
if ( _network_available ) NetworkBackgroundLoop ( ) ;
2004-12-04 17:54:56 +00:00
2011-08-21 12:46:46 +00:00
if ( _networking & & ! HasModalProgress ( ) ) {
2007-03-21 15:19:33 +00:00
/* Multiplayer */
2004-12-04 17:54:56 +00:00
NetworkGameLoop ( ) ;
2004-08-09 17:04:08 +00:00
} else {
2004-12-04 17:54:56 +00:00
if ( _network_reconnect > 0 & & - - _network_reconnect = = 0 ) {
2007-03-21 15:19:33 +00:00
/* This means that we want to reconnect to the last host
* We do this here , because it means that the network is really closed */
2009-05-19 21:20:14 +00:00
NetworkClientConnectGame ( NetworkAddress ( _settings_client . network . last_host , _settings_client . network . last_port ) , COMPANY_SPECTATOR ) ;
2004-12-04 17:54:56 +00:00
}
2007-03-21 15:19:33 +00:00
/* Singleplayer */
2004-09-12 15:29:37 +00:00
StateGameLoop ( ) ;
2004-08-09 17:04:08 +00:00
}
2009-05-06 15:06:57 +00:00
if ( ! _pause_mode & & HasBit ( _display_opt , DO_FULL_ANIMATION ) ) DoPaletteAnimations ( ) ;
2004-08-09 17:04:08 +00:00
2005-03-26 04:16:39 +00:00
InputLoop ( ) ;
2004-08-09 17:04:08 +00:00
2014-04-28 21:06:51 +00:00
SoundDriver : : GetInstance ( ) - > MainLoop ( ) ;
2005-03-30 19:52:26 +00:00
MusicLoop ( ) ;
2004-08-09 17:04:08 +00:00
}