2005-07-24 14:12:37 +00:00
/* $Id$ */
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"
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"
2014-01-02 16:48:16 +00:00
# include "viewport_sprite_sorter.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>
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 ( ) ;
2008-05-09 09:29:27 +00:00
void CallWindowTickEvent ( ) ;
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 ) ;
2014-04-28 21:06:51 +00:00
if ( VideoDriver : : GetInstance ( ) ! = NULL ) 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
2008-06-05 20:54:52 +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 "
2007-01-16 15:20:22 +00:00
# if defined(ENABLE_NETWORK)
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 "
2006-06-27 21:25:53 +00:00
# if !defined(__MORPHOS__) && !defined(__AMIGA__) && !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
2007-01-16 15:20:22 +00:00
# endif /* ENABLE_NETWORK */
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 */
# if !defined(WIN32) && !defined(WIN64)
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 )
{
extern uint16 _sl_version ;
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 ( ) ) {
for ( GRFConfig * c = _load_check_data . grfconfig ; c ! = NULL ; c = c - > next ) {
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 */
# if !defined(WIN32) && !defined(WIN64)
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 ' ) ;
2004-08-09 17:04:08 +00:00
if ( t = = NULL ) {
ShowInfoF ( " Invalid resolution '%s' " , s ) ;
return ;
}
2008-06-16 19:38:41 +00:00
res - > width = max ( strtoul ( s , NULL , 0 ) , 64UL ) ;
res - > height = max ( strtoul ( t + 1 , NULL , 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
/**
* Unitializes 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
2010-07-19 17:46:53 +00:00
# ifdef ENABLE_NETWORK
2005-03-10 21:44:17 +00:00
free ( _config_file ) ;
2010-07-19 17:46:53 +00:00
# endif
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 */
2011-08-24 13:33:49 +00:00
if ( SaveOrLoad ( " opntitle.dat " , SL_LOAD , 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
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
2012-02-18 17:42:27 +00:00
if ( load_newgrfs ) CheckForMissingSprites ( ) ;
2011-11-20 11:52:11 +00:00
CheckForMissingGlyphs ( ) ;
2007-12-16 18:38:19 +00:00
2007-03-21 15:19:33 +00:00
/* Play main theme */
2014-04-28 21:06:51 +00:00
if ( MusicDriver : : GetInstance ( ) - > IsSongPlaying ( ) ) ResetMusic ( ) ;
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 + + ) {
if ( _settings_game . ai_config [ c ] ! = NULL ) {
delete _settings_game . ai_config [ c ] ;
}
}
2011-12-19 20:55:56 +00:00
if ( _settings_game . game_config ! = NULL ) {
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 + + ) {
_settings_game . ai_config [ c ] = NULL ;
if ( _settings_newgame . ai_config [ c ] ! = NULL ) {
_settings_game . ai_config [ c ] = new AIConfig ( _settings_newgame . ai_config [ c ] ) ;
}
}
2011-12-19 20:55:56 +00:00
_settings_game . game_config = NULL ;
if ( _settings_newgame . game_config ! = NULL ) {
_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.
uint generation_seed ; ///< Seed for the new game.
char * dedicated_host ; ///< Hostname for the dedicated server.
uint16 dedicated_port ; ///< Port for the dedicated server.
2011-08-21 12:50:47 +00:00
char * network_conn ; ///< Information about the server to connect to, or NULL.
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 ) ,
dedicated_host ( NULL ) , dedicated_port ( 0 ) , network_conn ( NULL ) ,
2011-11-14 21:42:54 +00:00
join_server_password ( NULL ) , join_company_password ( NULL ) ,
save_config_ptr ( save_config_ptr ) , save_config ( true )
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
CheckConfig ( ) ;
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 ;
# if defined(ENABLE_NETWORK)
if ( dedicated_host ! = NULL ) {
_network_bind_list . Clear ( ) ;
2014-04-25 15:40:32 +00:00
* _network_bind_list . Append ( ) = stredup ( dedicated_host ) ;
2011-08-24 17:04:18 +00:00
}
if ( dedicated_port ! = 0 ) _settings_client . network . server_port = dedicated_port ;
# endif /* ENABLE_NETWORK */
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 ( ) ;
# ifdef ENABLE_NETWORK
if ( _network_available & & network_conn ! = NULL ) {
const char * port = NULL ;
const char * company = NULL ;
uint16 rport = NETWORK_DEFAULT_PORT ;
CompanyID join_as = COMPANY_NEW_COMPANY ;
ParseConnectionString ( & company , & port , network_conn ) ;
if ( company ! = NULL ) {
join_as = ( CompanyID ) atoi ( company ) ;
if ( join_as ! = COMPANY_SPECTATOR ) {
join_as - - ;
if ( join_as > = MAX_COMPANIES ) {
delete this ;
return ;
}
}
}
if ( port ! = NULL ) rport = atoi ( port ) ;
LoadIntroGame ( ) ;
_switch_mode = SM_NONE ;
NetworkClientConnectGame ( NetworkAddress ( network_conn , rport ) , join_as , join_server_password , join_company_password ) ;
}
# endif /* ENABLE_NETWORK */
/* After the scan we're not used anymore. */
delete this ;
}
} ;
2005-08-06 14:58:06 +00:00
# if defined(UNIX) && !defined(__MORPHOS__)
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 ' ) ,
# if defined(ENABLE_NETWORK)
GETOPT_SHORT_OPTVAL ( ' D ' ) ,
GETOPT_SHORT_OPTVAL ( ' n ' ) ,
GETOPT_SHORT_VALUE ( ' l ' ) ,
GETOPT_SHORT_VALUE ( ' p ' ) ,
GETOPT_SHORT_VALUE ( ' P ' ) ,
# if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
GETOPT_SHORT_NOVAL ( ' f ' ) ,
# endif
# endif /* ENABLE_NETWORK */
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
{
2009-01-08 11:06:07 +00:00
char * musicdriver = NULL ;
char * sounddriver = NULL ;
char * videodriver = NULL ;
char * blitter = NULL ;
char * graphics_set = NULL ;
2009-08-09 19:50:44 +00:00
char * sounds_set = NULL ;
2009-12-22 21:40:29 +00:00
char * music_set = NULL ;
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 ;
AfterNewGRFScan * scanner = new AfterNewGRFScan ( & save_config ) ;
2007-01-16 15:20:22 +00:00
# if defined(ENABLE_NETWORK)
2006-02-21 17:29:53 +00:00
bool dedicated = false ;
2007-02-08 12:27:53 +00:00
char * debuglog_conn = NULL ;
2010-07-19 17:46:53 +00:00
extern bool _dedicated_forks ;
_dedicated_forks = false ;
2007-01-16 15:20:22 +00:00
# endif /* ENABLE_NETWORK */
2005-02-23 09:13:12 +00:00
2004-08-09 17:04:08 +00:00
_game_mode = GM_MENU ;
_switch_mode = SM_MENU ;
2005-03-10 21:44:17 +00:00
_config_file = NULL ;
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 ) {
2014-04-25 15:40:32 +00:00
case ' I ' : free ( graphics_set ) ; graphics_set = stredup ( mgo . opt ) ; break ;
case ' S ' : free ( sounds_set ) ; sounds_set = stredup ( mgo . opt ) ; break ;
case ' M ' : free ( music_set ) ; music_set = stredup ( mgo . opt ) ; break ;
case ' m ' : free ( musicdriver ) ; musicdriver = stredup ( mgo . opt ) ; break ;
case ' s ' : free ( sounddriver ) ; sounddriver = stredup ( mgo . opt ) ; break ;
case ' v ' : free ( videodriver ) ; videodriver = stredup ( mgo . opt ) ; break ;
case ' b ' : free ( blitter ) ; blitter = stredup ( mgo . opt ) ; break ;
2007-01-16 15:20:22 +00:00
# if defined(ENABLE_NETWORK)
2006-02-21 17:29:53 +00:00
case ' D ' :
2009-01-08 14:10:39 +00:00
free ( musicdriver ) ;
free ( sounddriver ) ;
free ( videodriver ) ;
free ( blitter ) ;
2014-04-25 15:40:32 +00:00
musicdriver = stredup ( " null " ) ;
sounddriver = stredup ( " null " ) ;
videodriver = stredup ( " dedicated " ) ;
blitter = stredup ( " null " ) ;
2006-02-21 17:29:53 +00:00
dedicated = true ;
2009-02-21 13:27:09 +00:00
SetDebugString ( " net=6 " ) ;
2007-01-31 20:20:36 +00:00
if ( mgo . opt ! = NULL ) {
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 . */
2007-01-03 18:29:15 +00:00
const char * temp = NULL ;
const char * port = NULL ;
ParseConnectionString ( & temp , & port , mgo . opt ) ;
2011-08-24 17:04:18 +00:00
if ( ! StrEmpty ( mgo . opt ) ) scanner - > dedicated_host = mgo . opt ;
if ( port ! = NULL ) 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 ' :
2011-08-24 17:04:18 +00:00
scanner - > network_conn = mgo . opt ; // optional IP parameter, NULL 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 ;
2007-01-16 15:20:22 +00:00
# endif /* ENABLE_NETWORK */
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 ' : {
# if defined(WIN32)
CreateConsole ( ) ;
# endif
2005-11-14 19:48:04 +00:00
if ( mgo . opt ! = NULL ) 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 ' :
2005-11-14 19:48:04 +00:00
if ( mgo . opt ! = NULL ) {
2008-11-02 11:20:15 +00:00
strecpy ( _file_to_saveload . name , mgo . opt , lastof ( _file_to_saveload . name ) ) ;
2012-01-22 18:37:01 +00:00
_switch_mode = ( _switch_mode = = SM_EDITOR | | _switch_mode = = SM_LOAD_SCENARIO ? SM_LOAD_SCENARIO : SM_LOAD_GAME ) ;
2008-07-28 15:31:11 +00:00
_file_to_saveload . mode = SL_LOAD ;
/* if the file doesn't exist or it is not a valid savegame, let the saveload code show an error */
const char * t = strrchr ( _file_to_saveload . name , ' . ' ) ;
if ( t ! = NULL ) {
2009-01-18 22:44:53 +00:00
FiosType ft = FiosGetSavegameListCallback ( SLD_LOAD_GAME , _file_to_saveload . name , t , NULL , NULL ) ;
2008-07-28 15:31:11 +00:00
if ( ft ! = FIOS_TYPE_INVALID ) SetFiosType ( ft ) ;
}
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 ;
goto exit_noshutdown ;
}
2011-10-30 13:47:45 +00:00
char title [ 80 ] ;
title [ 0 ] = ' \0 ' ;
FiosGetSavegameListCallback ( SLD_LOAD_GAME , mgo . opt , strrchr ( mgo . opt , ' . ' ) , title , lastof ( title ) ) ;
_load_check_data . Clear ( ) ;
SaveOrLoadResult res = SaveOrLoad ( mgo . opt , SL_LOAD_CHECK , SAVE_DIR , false ) ;
if ( res ! = SL_OK | | _load_check_data . HasErrors ( ) ) {
fprintf ( stderr , " Failed to open savegame \n " ) ;
if ( _load_check_data . HasErrors ( ) ) {
char buf [ 256 ] ;
SetDParamStr ( 0 , _load_check_data . error_data ) ;
GetString ( buf , _load_check_data . error , lastof ( buf ) ) ;
fprintf ( stderr , " %s \n " , buf ) ;
}
2013-11-24 15:21:38 +00:00
goto exit_noshutdown ;
2011-10-30 13:47:45 +00:00
}
WriteSavegameInfo ( title ) ;
2013-11-24 15:21:38 +00:00
goto exit_noshutdown ;
2011-10-30 13:47:45 +00:00
}
2011-08-24 17:04:18 +00:00
case ' G ' : scanner - > generation_seed = atoi ( mgo . opt ) ; 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 ( ) ;
2013-11-24 15:21:38 +00:00
goto exit_noshutdown ;
2004-08-09 17:04:08 +00:00
}
2007-08-04 12:53:41 +00:00
# if defined(WINCE) && defined(_DEBUG)
/* Switch on debug lvl 4 for WinCE if Debug release, as you can't give params, and you most likely do want this information */
SetDebugString ( " 4 " ) ;
# endif
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 defined(ENABLE_NETWORK)
if ( dedicated ) DEBUG ( net , 0 , " Starting dedicated version %s " , _openttd_revision ) ;
if ( _dedicated_forks & & ! dedicated ) _dedicated_forks = false ;
# if defined(UNIX) && !defined(__MORPHOS__)
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 ( ) ;
2011-08-24 17:04:18 +00:00
# endif
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 ( ) ;
2014-04-25 15:40:32 +00:00
if ( graphics_set = = NULL & & BaseGraphics : : ini_set ! = NULL ) graphics_set = stredup ( BaseGraphics : : ini_set ) ;
2012-07-08 18:41:50 +00:00
if ( ! BaseGraphics : : SetSet ( graphics_set ) ) {
if ( ! StrEmpty ( graphics_set ) ) {
BaseGraphics : : SetSet ( NULL ) ;
ErrorMessageData msg ( STR_CONFIG_ERROR , STR_CONFIG_ERROR_INVALID_BASE_GRAPHICS_NOT_FOUND ) ;
msg . SetDParamStr ( 0 , graphics_set ) ;
ScheduleErrorMessage ( msg ) ;
}
2008-08-24 08:41:38 +00:00
}
2009-05-17 19:14:22 +00:00
free ( graphics_set ) ;
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... " ) ;
2014-04-25 15:40:32 +00:00
if ( blitter = = NULL & & _ini_blitter ! = NULL ) blitter = stredup ( _ini_blitter ) ;
2011-10-04 21:35:47 +00:00
_blitter_autodetected = StrEmpty ( blitter ) ;
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 | |
( _support8bpp ! = S8BPP_NONE & & ( BaseGraphics : : GetUsedSet ( ) = = NULL | | BaseGraphics : : GetUsedSet ( ) - > blitter = = BLT_8BPP ) ) | |
BlitterFactory : : SelectBlitter ( " 32bpp-anim " ) = = NULL ) {
2014-01-02 22:41:58 +00:00
if ( BlitterFactory : : SelectBlitter ( blitter ) = = NULL ) {
2011-10-04 21:35:47 +00:00
StrEmpty ( blitter ) ?
usererror ( " Failed to autoprobe blitter " ) :
usererror ( " Failed to select requested blitter '%s'; does it exist? " , blitter ) ;
}
2010-07-24 10:14:39 +00:00
}
2009-05-17 19:14:22 +00:00
free ( blitter ) ;
2007-07-05 12:23:54 +00:00
2014-04-25 15:40:32 +00:00
if ( videodriver = = NULL & & _ini_videodriver ! = NULL ) videodriver = stredup ( _ini_videodriver ) ;
2014-04-28 21:06:51 +00:00
DriverFactoryBase : : SelectDriver ( videodriver , Driver : : DT_VIDEO ) ;
2009-05-17 19:14:22 +00:00
free ( videodriver ) ;
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
2007-02-08 12:27:53 +00:00
# if defined(ENABLE_NETWORK)
if ( debuglog_conn ! = NULL & & _network_available ) {
const char * not_used = NULL ;
const char * port = NULL ;
uint16 rport ;
rport = NETWORK_DEFAULT_DEBUGLOG_PORT ;
ParseConnectionString ( & not_used , & port , debuglog_conn ) ;
if ( port ! = NULL ) rport = atoi ( port ) ;
2009-01-20 01:32:06 +00:00
NetworkStartDebugLog ( NetworkAddress ( debuglog_conn , rport ) ) ;
2007-02-08 12:27:53 +00:00
}
# endif /* ENABLE_NETWORK */
2013-11-24 16:21:19 +00:00
if ( ! HandleBootstrap ( ) ) {
ShutdownGame ( ) ;
2013-11-25 09:12:49 +00:00
2013-11-24 16:21:19 +00:00
goto exit_bootstrap ;
}
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 ( ) ;
2014-04-25 15:40:32 +00:00
if ( sounds_set = = NULL & & BaseSounds : : ini_set ! = NULL ) sounds_set = stredup ( BaseSounds : : ini_set ) ;
2011-11-17 21:21:45 +00:00
if ( ! BaseSounds : : SetSet ( sounds_set ) ) {
2012-07-08 18:41:50 +00:00
if ( StrEmpty ( sounds_set ) | | ! BaseSounds : : SetSet ( NULL ) ) {
usererror ( " Failed to find a sounds set. Please acquire a sounds set for OpenTTD. See section 4.1 of readme.txt. " ) ;
} else {
ErrorMessageData msg ( STR_CONFIG_ERROR , STR_CONFIG_ERROR_INVALID_BASE_SOUNDS_NOT_FOUND ) ;
msg . SetDParamStr ( 0 , sounds_set ) ;
ScheduleErrorMessage ( msg ) ;
}
2011-11-17 21:21:45 +00:00
}
free ( sounds_set ) ;
BaseMusic : : FindSets ( ) ;
2014-04-25 15:40:32 +00:00
if ( music_set = = NULL & & BaseMusic : : ini_set ! = NULL ) music_set = stredup ( BaseMusic : : ini_set ) ;
2011-11-17 21:21:45 +00:00
if ( ! BaseMusic : : SetSet ( music_set ) ) {
2012-07-08 18:41:50 +00:00
if ( StrEmpty ( music_set ) | | ! BaseMusic : : SetSet ( NULL ) ) {
usererror ( " Failed to find a music set. Please acquire a music set for OpenTTD. See section 4.1 of readme.txt. " ) ;
} else {
ErrorMessageData msg ( STR_CONFIG_ERROR , STR_CONFIG_ERROR_INVALID_BASE_MUSIC_NOT_FOUND ) ;
msg . SetDParamStr ( 0 , music_set ) ;
ScheduleErrorMessage ( msg ) ;
}
2011-11-17 21:21:45 +00:00
}
free ( music_set ) ;
2014-04-25 15:40:32 +00:00
if ( sounddriver = = NULL & & _ini_sounddriver ! = NULL ) sounddriver = stredup ( _ini_sounddriver ) ;
2014-04-28 21:06:51 +00:00
DriverFactoryBase : : SelectDriver ( sounddriver , Driver : : DT_SOUND ) ;
2011-11-17 21:21:45 +00:00
free ( sounddriver ) ;
2014-04-25 15:40:32 +00:00
if ( musicdriver = = NULL & & _ini_musicdriver ! = NULL ) musicdriver = stredup ( _ini_musicdriver ) ;
2014-04-28 21:06:51 +00:00
DriverFactoryBase : : SelectDriver ( musicdriver , Driver : : DT_MUSIC ) ;
2011-11-17 21:21:45 +00:00
free ( musicdriver ) ;
2009-05-03 15:44:05 +00:00
/* Take our initial lock on whatever we might want to do! */
2011-08-21 12:47:24 +00:00
_modal_progress_paint_mutex - > BeginCritical ( ) ;
_modal_progress_work_mutex - > BeginCritical ( ) ;
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. */
2011-08-24 17:04:18 +00:00
ScanNewGRFFiles ( scanner ) ;
2013-11-25 09:22:56 +00:00
scanner = NULL ;
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 ( ) ;
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 16:21:19 +00:00
goto exit_normal ;
2005-01-16 12:14:52 +00:00
2013-11-24 15:21:38 +00:00
exit_noshutdown :
2013-11-24 16:21:19 +00:00
/* These three are normally freed before bootstrap. */
2013-11-24 15:21:38 +00:00
free ( graphics_set ) ;
2013-11-24 16:21:19 +00:00
free ( videodriver ) ;
free ( blitter ) ;
exit_bootstrap :
/* These are normally freed before exit, but after bootstrap. */
2013-11-24 15:21:38 +00:00
free ( sounds_set ) ;
free ( music_set ) ;
2013-11-24 16:21:19 +00:00
free ( musicdriver ) ;
free ( sounddriver ) ;
exit_normal :
free ( BaseGraphics : : ini_set ) ;
free ( BaseSounds : : ini_set ) ;
free ( BaseMusic : : ini_set ) ;
2013-11-24 15:21:38 +00:00
2009-05-17 19:14:22 +00:00
free ( _ini_musicdriver ) ;
free ( _ini_sounddriver ) ;
free ( _ini_videodriver ) ;
free ( _ini_blitter ) ;
2009-04-15 20:49:26 +00:00
2013-11-25 09:22:56 +00:00
delete scanner ;
2013-11-25 09:20:35 +00:00
# ifdef ENABLE_NETWORK
extern FILE * _log_fd ;
if ( _log_fd ! = NULL ) {
fclose ( _log_fd ) ;
}
# endif /* ENABLE_NETWORK */
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
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 ( ) ;
2006-11-17 19:31:44 +00:00
2008-01-11 00:30:32 +00:00
# ifdef ENABLE_NETWORK
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
}
2008-01-11 00:30:32 +00:00
# endif /* ENABLE_NETWORK */
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 .
* @ param mode mode of loading , either SL_LOAD or SL_OLD_LOAD
* @ param newgm switch to this mode of loading fails due to some unknown error
2010-12-05 14:43:10 +00:00
* @ param filename file to be loaded
2010-06-30 20:20:15 +00:00
* @ param subdir default directory to look for filename , set to 0 if not needed
2010-12-05 14:43:10 +00:00
* @ param lf Load filter to use , if NULL : use filename + subdir .
2010-06-30 20:20:15 +00:00
*/
2010-12-05 14:43:10 +00:00
bool SafeLoad ( const char * filename , int mode , GameMode newgm , Subdirectory subdir , struct LoadFilter * lf = NULL )
2010-06-30 20:20:15 +00:00
{
2010-12-05 14:43:10 +00:00
assert ( mode = = SL_LOAD | | ( lf = = NULL & & mode = = SL_OLD_LOAD ) ) ;
2010-06-30 20:20:15 +00:00
GameMode ogm = _game_mode ;
_game_mode = newgm ;
2010-12-05 14:43:10 +00:00
switch ( lf = = NULL ? SaveOrLoad ( filename , mode , subdir ) : LoadWithFilter ( lf ) ) {
2010-06-30 20:20:15 +00:00
case SL_OK : return true ;
case SL_REINIT :
2010-08-15 11:58:20 +00:00
# ifdef ENABLE_NETWORK
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 ( ) ;
}
# endif /* ENABLE_NETWORK */
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
{
2004-12-04 17:54:56 +00:00
# ifdef ENABLE_NETWORK
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 ;
}
}
}
# endif /* ENABLE_NETWORK */
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
2009-08-06 22:00:32 +00:00
case SM_RESTARTGAME : // Restart --> 'Random game' with current settings
2009-03-15 00:32:18 +00:00
case SM_NEWGAME : // New Game --> 'Random game'
2004-12-13 17:58:53 +00:00
# ifdef ENABLE_NETWORK
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
}
2004-12-13 17:58:53 +00:00
# endif /* ENABLE_NETWORK */
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
2010-12-05 14:43:10 +00:00
if ( ! SafeLoad ( _file_to_saveload . name , _file_to_saveload . mode , 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 {
2008-05-04 22:38:18 +00:00
if ( _saveload_mode = = SLD_LOAD_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 ) ;
2004-12-13 17:58:53 +00:00
# ifdef ENABLE_NETWORK
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
}
2004-12-13 17:58:53 +00:00
# endif /* ENABLE_NETWORK */
2008-04-29 18:19:29 +00:00
}
break ;
2004-08-09 17:04:08 +00:00
}
2009-03-15 00:32:18 +00:00
case SM_START_HEIGHTMAP : // Load a heightmap and start a new game from it
(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
# ifdef ENABLE_NETWORK
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
}
(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
# endif /* ENABLE_NETWORK */
2009-08-06 22:00:32 +00:00
MakeNewGame ( true , true ) ;
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
2010-12-05 14:43:10 +00:00
if ( SafeLoad ( _file_to_saveload . name , _file_to_saveload . mode , 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 ( ) ;
2010-02-22 20:19:17 +00:00
if ( BaseSounds : : ini_set = = NULL & & BaseSounds : : GetUsedSet ( ) - > fallback ) {
2010-02-24 14:46:15 +00:00
ShowErrorMessage ( STR_WARNING_FALLBACK_SOUNDSET , INVALID_STRING_ID , WL_CRITICAL ) ;
2014-04-25 15:40:32 +00:00
BaseSounds : : ini_set = stredup ( 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 */
if ( SaveOrLoad ( _file_to_saveload . name , SL_SAVE , 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. */
SmallVector < TownCache , 4 > old_town_caches ;
Town * t ;
FOR_ALL_TOWNS ( t ) {
MemCpyT ( old_town_caches . Append ( ) , & t - > cache ) ;
}
extern void RebuildTownCaches ( ) ;
RebuildTownCaches ( ) ;
2012-08-18 11:38:06 +00:00
RebuildSubsidisedSourceAndDestinationCache ( ) ;
2012-04-25 21:06:31 +00:00
uint i = 0 ;
FOR_ALL_TOWNS ( t ) {
if ( MemCmpT ( old_town_caches . Get ( i ) , & t - > cache ) ! = 0 ) {
DEBUG ( desync , 2 , " town cache mismatch: town %i " , ( int ) t - > index ) ;
}
i + + ;
}
2011-12-03 23:40:13 +00:00
/* Check company infrastructure cache. */
SmallVector < CompanyInfrastructure , 4 > old_infrastructure ;
Company * c ;
FOR_ALL_COMPANIES ( c ) MemCpyT ( old_infrastructure . Append ( ) , & c - > infrastructure ) ;
extern void AfterLoadCompanyStats ( ) ;
AfterLoadCompanyStats ( ) ;
2012-04-25 21:06:31 +00:00
i = 0 ;
2011-12-03 23:40:13 +00:00
FOR_ALL_COMPANIES ( c ) {
if ( MemCmpT ( old_infrastructure . Get ( 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 */
const RoadStop * rs ;
FOR_ALL_ROADSTOPS ( rs ) {
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 ) ;
}
Vehicle * v ;
FOR_ALL_VEHICLES ( v ) {
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 ;
for ( const Vehicle * u = v ; u ! = NULL ; u = u - > Next ( ) ) length + + ;
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 ;
for ( const Vehicle * u = v ; u ! = NULL ; 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 ;
for ( const Vehicle * u = v ; u ! = NULL ; 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 */
FOR_ALL_VEHICLES ( v ) {
byte buff [ sizeof ( VehicleCargoList ) ] ;
memcpy ( buff , & v - > cargo , sizeof ( VehicleCargoList ) ) ;
v - > cargo . InvalidateCache ( ) ;
assert ( memcmp ( & v - > cargo , buff , sizeof ( VehicleCargoList ) ) = = 0 ) ;
}
Station * st ;
FOR_ALL_STATIONS ( st ) {
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 ) ;
}
}
}
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 ) {
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
CallWindowTickEvent ( ) ;
return ;
}
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
2004-08-09 17:04:08 +00:00
CallWindowTickEvent ( ) ;
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 ) ;
2011-01-18 18:14:14 +00:00
SaveOrLoad ( name , SL_SAVE , 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 */
2010-06-05 12:16:12 +00:00
Backup < CompanyByte > 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
2009-01-12 17:11:45 +00:00
AI : : GameLoop ( ) ;
2011-12-19 20:50:54 +00:00
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
CallWindowTickEvent ( ) ;
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
2007-02-11 14:04:40 +00:00
# if defined(PSP)
/* Autosaving in networking is too time expensive for the PSP */
2008-04-29 18:19:29 +00:00
if ( _networking ) return ;
2007-02-11 14:04:40 +00:00
# endif /* PSP */
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 ) ;
2008-04-29 18:19:29 +00:00
if ( SaveOrLoad ( buf , SL_SAVE , 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 ) {
# ifdef ENABLE_NETWORK
/* Check for UDP stuff */
2012-01-04 22:08:43 +00:00
if ( _network_available ) NetworkBackgroundLoop ( ) ;
2011-11-17 21:17:17 +00:00
# endif
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 ( ) ;
2009-02-25 21:45:14 +00:00
extern int _caret_timer ;
2004-08-09 17:04:08 +00:00
_caret_timer + = 3 ;
CursorTick ( ) ;
2004-12-04 17:54:56 +00:00
# ifdef ENABLE_NETWORK
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
}
2010-12-14 14:57:51 +00:00
/* Check chat messages roughly once a second. */
static uint check_message = 0 ;
if ( + + check_message > 1000 / MILLISECONDS_PER_TICK ) {
check_message = 0 ;
NetworkChatMessageLoop ( ) ;
}
2004-12-04 17:54:56 +00:00
# else
StateGameLoop ( ) ;
# endif /* ENABLE_NETWORK */
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
2010-12-07 21:13:00 +00:00
if ( ! _pause_mode | | _game_mode = = GM_EDITOR | | _settings_game . construction . command_pause_level > CMDPL_NO_CONSTRUCTION ) MoveAllTextEffects ( ) ;
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
}