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"
2004-08-09 17:04:08 +00:00
# include "gui.h"
2008-03-31 07:03:35 +00:00
# include "sound_func.h"
2007-12-19 20:45:46 +00:00
# include "window_func.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"
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"
2008-03-31 07:25:49 +00:00
# include "signs_base.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"
2007-05-19 09:40:18 +00:00
# include "group.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"
2009-01-31 20:16:06 +00:00
# include "cheat_type.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-04-20 08:43:31 +00:00
# include "functions.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-09-01 10:07:22 +00:00
# include "thread/thread.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"
2008-03-31 07:03:35 +00:00
2004-08-09 17:04:08 +00:00
2009-01-31 20:16:06 +00:00
# include "town.h"
# include "industry.h"
2006-12-27 12:38:02 +00:00
2004-08-09 17:04:08 +00:00
# include <stdarg.h>
2008-01-13 01:21:35 +00:00
# include "table/strings.h"
2008-04-18 10:16:51 +00:00
StringID _switch_mode_errorstr ;
2007-03-07 11:47:46 +00:00
void CallLandscapeTick ( ) ;
void IncreaseDate ( ) ;
void DoPaletteAnimations ( ) ;
void MusicLoop ( ) ;
void ResetMusic ( ) ;
2008-04-19 10:18:38 +00:00
void ProcessAsyncSaveFinish ( ) ;
2008-05-09 09:29:27 +00:00
void CallWindowTickEvent ( ) ;
2004-08-09 17:04:08 +00:00
2008-05-25 22:36:44 +00:00
extern void SetDifficultyLevel ( int mode , DifficultySettings * gm_opt ) ;
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 ) ;
vsnprintf ( buf , lengthof ( buf ) , s , va ) ;
va_end ( va ) ;
ShowOSErrorBox ( buf , false ) ;
if ( _video_driver ! = NULL ) _video_driver - > Stop ( ) ;
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 ) ;
2006-11-28 20:55:16 +00:00
vsnprintf ( buf , lengthof ( 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 ) ;
2006-11-28 20:55:16 +00:00
vsnprintf ( buf , lengthof ( 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)
2008-09-30 20:39:50 +00:00
" -n [ip:port#company]= Start networkgame \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-10-25 20:16:07 +00:00
" -i palette = Force to use the DOS (0) or Windows (1) palette \n "
" (defines default setting when adding newgrfs) \n "
2008-11-23 16:01:24 +00:00
" Default value (2) lets OpenTTD use the palette \n "
" specified in graphics set file (see below) \n "
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 "
" \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 */
p = VideoDriverFactoryBase : : GetDriversInfo ( p , lastof ( buf ) ) ;
2004-08-09 17:04:08 +00:00
2007-06-11 11:50:49 +00:00
/* List the blitters */
p = BlitterFactoryBase : : GetBlittersInfo ( p , lastof ( buf ) ) ;
2009-01-12 17:11:45 +00:00
/* We need to initialize the AI, so it finds the AIs */
2010-11-18 22:27:15 +00:00
TarScanner : : DoScan ( ) ;
2009-01-12 17:11:45 +00:00
AI : : Initialize ( ) ;
p = AI : : GetConsoleList ( p , lastof ( buf ) ) ;
AI : : 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
}
2007-01-10 18:56:51 +00:00
struct MyGetOptData {
2004-08-09 17:04:08 +00:00
char * opt ;
int numleft ;
char * * argv ;
const char * options ;
2009-05-24 20:29:04 +00:00
char * cont ;
2004-08-09 17:04:08 +00:00
2007-01-10 18:56:51 +00:00
MyGetOptData ( int argc , char * * argv , const char * options )
{
opt = NULL ;
numleft = argc ;
this - > argv = argv ;
this - > options = options ;
cont = NULL ;
}
} ;
2004-08-09 17:04:08 +00:00
static int MyGetOpt ( MyGetOptData * md )
{
2009-05-24 20:29:04 +00:00
char * s = md - > cont ;
2010-07-24 10:14:39 +00:00
if ( s ! = NULL ) {
2004-08-09 17:04:08 +00:00
goto md_continue_here ;
2010-07-24 10:14:39 +00:00
}
2004-08-09 17:04:08 +00:00
2005-11-14 19:48:04 +00:00
for ( ; ; ) {
if ( - - md - > numleft < 0 ) return - 1 ;
2004-08-09 17:04:08 +00:00
s = * md - > argv + + ;
if ( * s = = ' - ' ) {
md_continue_here : ;
s + + ;
if ( * s ! = 0 ) {
2009-05-24 20:29:04 +00:00
const char * r ;
2007-03-21 15:19:33 +00:00
/* Found argument, try to locate it in options. */
2004-08-09 17:04:08 +00:00
if ( * s = = ' : ' | | ( r = strchr ( md - > options , * s ) ) = = NULL ) {
2007-03-21 15:19:33 +00:00
/* ERROR! */
2004-08-09 17:04:08 +00:00
return - 2 ;
}
if ( r [ 1 ] = = ' : ' ) {
2009-05-24 20:29:04 +00:00
char * t ;
2007-03-21 15:19:33 +00:00
/* Item wants an argument. Check if the argument follows, or if it comes as a separate arg. */
2004-08-09 17:04:08 +00:00
if ( ! * ( t = s + 1 ) ) {
2007-03-21 15:19:33 +00:00
/* It comes as a separate arg. Check if out of args? */
2004-08-09 17:04:08 +00:00
if ( - - md - > numleft < 0 | | * ( t = * md - > argv ) = = ' - ' ) {
2007-03-21 15:19:33 +00:00
/* Check if item is optional? */
2010-07-24 10:14:39 +00:00
if ( r [ 2 ] ! = ' : ' ) return - 2 ;
2004-08-09 17:04:08 +00:00
md - > numleft + + ;
t = NULL ;
} else {
md - > argv + + ;
}
}
2009-05-24 20:29:04 +00:00
md - > opt = t ;
2004-08-09 17:04:08 +00:00
md - > cont = NULL ;
return * s ;
}
md - > opt = NULL ;
md - > cont = s ;
return * s ;
}
} else {
2007-03-21 15:19:33 +00:00
/* This is currently not supported. */
2004-08-09 17:04:08 +00:00
return - 2 ;
}
}
}
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-03-07 11:47:46 +00:00
static void InitializeDynamicVariables ( )
2005-01-06 22:31:58 +00:00
{
/* Dynamic stuff needs to be initialized somewhere... */
2009-03-08 16:51:08 +00:00
_engine_mngr . ResetToDefaultMapping ( ) ;
_house_mngr . ResetMapping ( ) ;
2007-07-04 01:34:28 +00:00
_industry_mngr . ResetMapping ( ) ;
_industile_mngr . ResetMapping ( ) ;
2010-03-18 23:12:38 +00:00
_airport_mngr . ResetMapping ( ) ;
2010-03-06 01:58:55 +00:00
_airporttile_mngr . ResetMapping ( ) ;
2005-01-06 22:31:58 +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 ( ) ;
2009-05-14 18:09:50 +00:00
/* stop the AI */
AI : : Uninitialize ( false ) ;
2007-01-14 17:17:30 +00:00
/* Uninitialize variables that are allocated dynamically */
2008-06-03 18:35:58 +00:00
GamelogReset ( ) ;
2009-05-22 15:13:50 +00:00
_town_pool . CleanPool ( ) ;
_industry_pool . CleanPool ( ) ;
_station_pool . CleanPool ( ) ;
_roadstop_pool . CleanPool ( ) ;
_vehicle_pool . CleanPool ( ) ;
_sign_pool . CleanPool ( ) ;
_order_pool . CleanPool ( ) ;
_group_pool . CleanPool ( ) ;
_cargopacket_pool . CleanPool ( ) ;
_engine_pool . CleanPool ( ) ;
_company_pool . CleanPool ( ) ;
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
2010-09-06 18:20:07 +00:00
ResetNewGRFData ( ) ;
2008-05-08 23:26:17 +00:00
/* Close all and any open filehandles */
FioCloseAll ( ) ;
2005-03-09 19:48:20 +00:00
}
2005-01-06 22:31:58 +00:00
2007-03-07 11:47:46 +00:00
static void LoadIntroGame ( )
2004-12-04 17:54:56 +00:00
{
_game_mode = GM_MENU ;
2007-04-05 07:49:04 +00:00
2006-12-04 08:30:04 +00:00
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 */
if ( SaveOrLoad ( " opntitle.dat " , SL_LOAD , DATA_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
2010-08-19 19:23:38 +00:00
CheckForMissingSprites ( ) ;
2007-12-16 18:38:19 +00:00
CheckForMissingGlyphsInLoadedLanguagePack ( ) ;
2007-03-21 15:19:33 +00:00
/* Play main theme */
2007-07-05 12:23:54 +00:00
if ( _music_driver - > IsSongPlaying ( ) ) ResetMusic ( ) ;
2004-12-04 17:54:56 +00:00
}
2009-01-12 17:11:45 +00:00
void MakeNewgameSettingsLive ( )
{
2010-02-10 16:24:05 +00:00
# ifdef ENABLE_AI
2009-01-12 17:11:45 +00:00
for ( CompanyID c = COMPANY_FIRST ; c < MAX_COMPANIES ; c + + ) {
if ( _settings_game . ai_config [ c ] ! = NULL ) {
delete _settings_game . ai_config [ c ] ;
}
}
2010-02-10 16:24:05 +00:00
# endif /* ENABLE_AI */
2009-01-12 17:11:45 +00:00
_settings_game = _settings_newgame ;
2010-02-10 16:24:05 +00:00
# ifdef ENABLE_AI
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 ] ) ;
}
}
2010-02-10 16:24:05 +00:00
# endif /* ENABLE_AI */
2009-01-12 17:11:45 +00:00
}
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
2006-07-26 03:33:12 +00:00
int ttd_main ( int argc , char * argv [ ] )
2004-08-09 17:04:08 +00:00
{
int i ;
2005-01-14 19:44:42 +00:00
const char * optformat ;
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 } ;
2006-08-16 11:39:55 +00:00
Year startyear = INVALID_YEAR ;
(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
uint generation_seed = GENERATE_NEW_SEED ;
2007-01-16 15:20:22 +00:00
bool save_config = true ;
# if defined(ENABLE_NETWORK)
2006-02-21 17:29:53 +00:00
bool dedicated = false ;
bool network = false ;
char * network_conn = NULL ;
2007-02-08 12:27:53 +00:00
char * debuglog_conn = NULL ;
2007-01-03 18:29:15 +00:00
char * dedicated_host = NULL ;
uint16 dedicated_port = 0 ;
2009-06-10 19:00:34 +00:00
char * join_server_password = NULL ;
char * join_company_password = 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 ;
2004-09-06 22:46:02 +00:00
_switch_mode_errorstr = INVALID_STRING_ID ;
2005-03-10 21:44:17 +00:00
_config_file = NULL ;
2004-08-09 17:04:08 +00:00
2007-03-21 15:19:33 +00:00
/* The last param of the following function means this:
* a letter means : it accepts that param ( e . g . : - h )
* a ' : ' behind it means : it need a param ( e . g . : - m < driver > )
* a ' : : ' behind it means : it can optional have a param ( e . g . : - d < debug > ) */
2010-02-24 22:41:51 +00:00
optformat = " m:s:v:b:hD::n::ei::I:S:M:t:d::r:g::G:c:xl:p:P: "
2006-06-27 21:25:53 +00:00
# if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
" f "
# endif
;
2004-12-22 21:12:36 +00:00
2007-04-18 22:10:36 +00:00
MyGetOptData mgo ( argc - 1 , argv + 1 , optformat ) ;
2007-01-10 18:56:51 +00:00
2004-08-09 17:04:08 +00:00
while ( ( i = MyGetOpt ( & mgo ) ) ! = - 1 ) {
2006-02-01 06:32:03 +00:00
switch ( i ) {
2009-01-08 11:06:07 +00:00
case ' I ' : free ( graphics_set ) ; graphics_set = strdup ( mgo . opt ) ; break ;
2009-08-09 19:50:44 +00:00
case ' S ' : free ( sounds_set ) ; sounds_set = strdup ( mgo . opt ) ; break ;
2009-12-22 21:40:29 +00:00
case ' M ' : free ( music_set ) ; music_set = strdup ( mgo . opt ) ; break ;
2009-01-08 11:06:07 +00:00
case ' m ' : free ( musicdriver ) ; musicdriver = strdup ( mgo . opt ) ; break ;
case ' s ' : free ( sounddriver ) ; sounddriver = strdup ( mgo . opt ) ; break ;
case ' v ' : free ( videodriver ) ; videodriver = strdup ( mgo . opt ) ; break ;
case ' b ' : free ( blitter ) ; blitter = strdup ( mgo . opt ) ; break ;
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 ) ;
musicdriver = strdup ( " null " ) ;
sounddriver = strdup ( " null " ) ;
videodriver = strdup ( " dedicated " ) ;
blitter = strdup ( " 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 ) ;
2007-01-31 20:20:36 +00:00
if ( ! StrEmpty ( mgo . opt ) ) dedicated_host = mgo . opt ;
2007-01-03 18:29:15 +00:00
if ( port ! = NULL ) dedicated_port = atoi ( port ) ;
}
2006-02-21 17:29:53 +00:00
break ;
case ' f ' : _dedicated_forks = true ; break ;
case ' n ' :
network = true ;
2006-02-21 17:48:40 +00:00
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 ' :
join_server_password = mgo . opt ;
break ;
case ' P ' :
join_company_password = mgo . opt ;
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 ;
2006-08-15 14:52:17 +00:00
case ' t ' : 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 ;
}
2004-08-09 17:04:08 +00:00
case ' e ' : _switch_mode = SM_EDITOR ; break ;
2008-11-23 16:01:24 +00:00
case ' i ' :
/* there is an argument, it is not empty, and it is exactly 1 char long */
if ( ! StrEmpty ( mgo . opt ) & & mgo . opt [ 1 ] = = ' \0 ' ) {
_use_palette = ( PaletteType ) ( mgo . opt [ 0 ] - ' 0 ' ) ;
if ( _use_palette < = MAX_PAL ) break ;
}
usererror ( " Valid value for '-i' is 0, 1 or 2 " ) ;
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 ) ) ;
2004-08-09 17:04:08 +00:00
_switch_mode = SM_LOAD ;
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 */
if ( generation_seed = = GENERATE_NEW_SEED ) {
generation_seed = InteractiveRandom ( ) ;
}
2004-08-09 17:04:08 +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
case ' G ' : generation_seed = atoi ( mgo . opt ) ; break ;
2006-02-21 17:29:53 +00:00
case ' c ' : _config_file = strdup ( mgo . opt ) ; break ;
2007-01-03 18:06:50 +00:00
case ' x ' : save_config = false ; break ;
2004-08-09 17:04:08 +00:00
case - 2 :
2005-07-08 22:25:24 +00:00
case ' h ' :
2008-08-24 08:41:38 +00:00
/* The next two functions are needed to list the graphics sets.
* We can ' t do them earlier because then we can ' t show it on
* the debug console as that hasn ' t been configured yet . */
DeterminePaths ( argv [ 0 ] ) ;
2009-08-09 16:54:03 +00:00
BaseGraphics : : FindSets ( ) ;
2009-08-09 19:50:44 +00:00
BaseSounds : : FindSets ( ) ;
2009-12-22 21:40:29 +00:00
BaseMusic : : FindSets ( ) ;
2008-04-29 18:19:29 +00:00
ShowHelp ( ) ;
2004-08-09 17:04:08 +00:00
return 0 ;
}
}
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 ] ) ;
2009-08-09 16:54:03 +00:00
BaseGraphics : : FindSets ( ) ;
2009-08-09 19:50:44 +00:00
BaseSounds : : FindSets ( ) ;
2009-12-22 21:40:29 +00:00
BaseMusic : : FindSets ( ) ;
2004-12-18 14:19:21 +00:00
2010-07-19 17:46:53 +00:00
# if defined(ENABLE_NETWORK) && 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 ( ) ;
2004-12-18 14:19:21 +00:00
# endif
2010-11-18 22:27:15 +00:00
TarScanner : : DoScan ( ) ;
2009-01-12 17:11:45 +00:00
AI : : Initialize ( ) ;
2004-08-09 17:04:08 +00:00
LoadFromConfig ( ) ;
2009-01-12 17:11:45 +00:00
AI : : Uninitialize ( true ) ;
2005-04-13 23:03:31 +00:00
CheckConfig ( ) ;
2005-01-11 00:54:06 +00:00
LoadFromHighScore ( ) ;
2010-07-03 13:28:15 +00:00
LoadHotkeysFromConfig ( ) ;
2004-08-09 17:04:08 +00:00
2008-06-16 19:38:41 +00:00
if ( resolution . width ! = 0 ) { _cur_resolution = resolution ; }
2008-05-25 19:17:03 +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 ;
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
* height must still fit within a 32 bits integer , this way all
* internal drawing routines work correctly .
*/
_cur_resolution . width = ClampU ( _cur_resolution . width , 1 , UINT16_MAX ) ;
_cur_resolution . height = ClampU ( _cur_resolution . height , 1 , UINT16_MAX ) ;
2007-06-13 14:57:42 +00:00
2007-01-16 15:20:22 +00:00
# if defined(ENABLE_NETWORK)
2010-11-19 10:58:38 +00:00
if ( dedicated ) DEBUG ( net , 0 , " Starting dedicated version %s " , _openttd_revision ) ;
2009-04-10 12:56:55 +00:00
if ( dedicated_host ) {
_network_bind_list . Clear ( ) ;
* _network_bind_list . Append ( ) = strdup ( dedicated_host ) ;
}
2008-05-29 20:21:28 +00:00
if ( dedicated_port ) _settings_client . network . server_port = dedicated_port ;
2006-02-01 06:32:03 +00:00
if ( _dedicated_forks & & ! dedicated ) _dedicated_forks = false ;
2007-01-16 15:20:22 +00:00
# endif /* ENABLE_NETWORK */
2004-12-18 14:19:21 +00:00
2007-03-21 15:19:33 +00:00
/* enumerate language files */
2004-08-09 17:04:08 +00:00
InitializeLanguagePacks ( ) ;
2007-03-21 15:19:33 +00:00
/* initialize screenshot formats */
2004-08-09 17:04:08 +00:00
InitializeScreenshotFormats ( ) ;
2005-01-06 22:31:58 +00:00
/* initialize all variables that are allocated dynamically */
InitializeDynamicVariables ( ) ;
2006-11-16 22:05:33 +00:00
/* Initialize FreeType */
InitFreeType ( ) ;
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
2009-08-09 19:50:44 +00:00
/* Look for the sounds before the graphics. Otherwise none would be set and
* the first initialisation of the video happens on the wrong data . Now it
* can do the first initialisation right . */
if ( sounds_set = = NULL & & BaseSounds : : ini_set ! = NULL ) sounds_set = strdup ( BaseSounds : : ini_set ) ;
if ( ! BaseSounds : : SetSet ( sounds_set ) ) {
StrEmpty ( sounds_set ) ?
2009-12-13 14:23:45 +00:00
usererror ( " Failed to find a sounds set. Please acquire a sounds set for OpenTTD. See section 4.1 of readme.txt. " ) :
2009-08-09 19:50:44 +00:00
usererror ( " Failed to select requested sounds set '%s' " , sounds_set ) ;
}
free ( sounds_set ) ;
2009-08-09 16:54:03 +00:00
if ( graphics_set = = NULL & & BaseGraphics : : ini_set ! = NULL ) graphics_set = strdup ( BaseGraphics : : ini_set ) ;
if ( ! BaseGraphics : : SetSet ( graphics_set ) ) {
2009-02-07 00:33:35 +00:00
StrEmpty ( graphics_set ) ?
2009-12-13 14:23:45 +00:00
usererror ( " Failed to find a graphics set. Please acquire a graphics set for OpenTTD. See section 4.1 of readme.txt. " ) :
2009-02-07 00:33:35 +00:00
usererror ( " Failed to select requested graphics set '%s' " , graphics_set ) ;
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
2009-12-22 21:40:29 +00:00
if ( music_set = = NULL & & BaseMusic : : ini_set ! = NULL ) music_set = strdup ( BaseMusic : : ini_set ) ;
if ( ! BaseMusic : : SetSet ( music_set ) ) {
StrEmpty ( music_set ) ?
usererror ( " Failed to find a music set. Please acquire a music set for OpenTTD. See section 4.1 of readme.txt. " ) :
usererror ( " Failed to select requested music set '%s' " , music_set ) ;
}
free ( music_set ) ;
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... " ) ;
2009-05-19 17:04:42 +00:00
if ( blitter = = NULL & & _ini_blitter ! = NULL ) blitter = strdup ( _ini_blitter ) ;
2010-07-24 10:14:39 +00:00
if ( BlitterFactoryBase : : SelectBlitter ( blitter ) = = NULL ) {
2009-02-07 00:33:35 +00:00
StrEmpty ( blitter ) ?
2008-06-05 20:54:52 +00:00
usererror ( " Failed to autoprobe blitter " ) :
2009-02-07 00:33:35 +00:00
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
2006-12-26 17:36:18 +00:00
DEBUG ( driver , 1 , " Loading drivers... " ) ;
2007-07-05 12:23:54 +00:00
2009-05-19 17:04:42 +00:00
if ( sounddriver = = NULL & & _ini_sounddriver ! = NULL ) sounddriver = strdup ( _ini_sounddriver ) ;
2009-02-07 00:33:35 +00:00
_sound_driver = ( SoundDriver * ) SoundDriverFactoryBase : : SelectDriver ( sounddriver , Driver : : DT_SOUND ) ;
2007-07-05 12:23:54 +00:00
if ( _sound_driver = = NULL ) {
2009-02-07 00:33:35 +00:00
StrEmpty ( sounddriver ) ?
2008-06-05 20:54:52 +00:00
usererror ( " Failed to autoprobe sound driver " ) :
2009-02-07 00:33:35 +00:00
usererror ( " Failed to select requested sound driver '%s' " , sounddriver ) ;
2007-07-05 12:23:54 +00:00
}
2009-05-17 19:14:22 +00:00
free ( sounddriver ) ;
2007-07-05 12:23:54 +00:00
2009-05-19 17:04:42 +00:00
if ( videodriver = = NULL & & _ini_videodriver ! = NULL ) videodriver = strdup ( _ini_videodriver ) ;
2009-02-07 00:33:35 +00:00
_video_driver = ( VideoDriver * ) VideoDriverFactoryBase : : SelectDriver ( videodriver , Driver : : DT_VIDEO ) ;
2007-07-05 12:23:54 +00:00
if ( _video_driver = = NULL ) {
2009-02-07 00:33:35 +00:00
StrEmpty ( videodriver ) ?
2008-06-05 20:54:52 +00:00
usererror ( " Failed to autoprobe video driver " ) :
2009-02-07 00:33:35 +00:00
usererror ( " Failed to select requested video driver '%s' " , videodriver ) ;
2007-07-05 12:23:54 +00:00
}
2009-05-17 19:14:22 +00:00
free ( videodriver ) ;
2007-07-05 12:23:54 +00:00
2009-12-16 23:49:21 +00:00
if ( musicdriver = = NULL & & _ini_musicdriver ! = NULL ) musicdriver = strdup ( _ini_musicdriver ) ;
_music_driver = ( MusicDriver * ) MusicDriverFactoryBase : : SelectDriver ( musicdriver , Driver : : DT_MUSIC ) ;
if ( _music_driver = = NULL ) {
StrEmpty ( musicdriver ) ?
usererror ( " Failed to autoprobe music driver " ) :
usererror ( " Failed to select requested music driver '%s' " , musicdriver ) ;
}
free ( musicdriver ) ;
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
2007-03-21 15:19:33 +00:00
/* restore saved music volume */
2007-07-05 12:23:54 +00:00
_music_driver - > SetVolume ( msf . music_vol ) ;
2006-04-27 21:05:32 +00:00
2006-10-12 14:59:27 +00:00
NetworkStartUp ( ) ; // initialize network-core
2004-12-04 17:54:56 +00:00
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 */
2006-12-04 08:30:04 +00:00
ScanNewGRFFiles ( ) ;
ResetGRFConfig ( false ) ;
2004-09-10 19:02:27 +00:00
2009-02-14 23:17:32 +00:00
/* Make sure _settings is filled with _settings_newgame if we switch to a game directly */
if ( _switch_mode ! = SM_NONE ) MakeNewgameSettingsLive ( ) ;
(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
2007-03-21 15:19:33 +00:00
/* initialize the ingame console */
2004-08-25 10:17:39 +00:00
IConsoleInit ( ) ;
2006-03-24 00:42:35 +00:00
_cursor . in_window = true ;
2005-01-31 11:03:23 +00:00
InitializeGUI ( ) ;
2004-12-13 22:13:02 +00:00
IConsoleCmdExec ( " exec scripts/autoexec.scr 0 " ) ;
2004-12-14 20:27:00 +00:00
2009-05-03 15:44:05 +00:00
/* Take our initial lock on whatever we might want to do! */
_genworld_paint_mutex - > BeginCritical ( ) ;
_genworld_mapgen_mutex - > BeginCritical ( ) ;
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
2009-02-12 10:13:30 +00:00
CheckForMissingGlyphsInLoadedLanguagePack ( ) ;
2004-12-04 17:54:56 +00:00
# ifdef ENABLE_NETWORK
2006-02-01 06:32:03 +00:00
if ( network & & _network_available ) {
2004-12-04 17:54:56 +00:00
if ( network_conn ! = NULL ) {
2005-02-06 22:25:27 +00:00
const char * port = NULL ;
2008-09-30 20:39:50 +00:00
const char * company = NULL ;
2009-05-19 21:20:14 +00:00
uint16 rport = NETWORK_DEFAULT_PORT ;
CompanyID join_as = COMPANY_NEW_COMPANY ;
2004-12-04 17:54:56 +00:00
2008-09-30 20:39:50 +00:00
ParseConnectionString ( & company , & port , network_conn ) ;
2004-12-04 17:54:56 +00:00
2008-09-30 20:39:50 +00:00
if ( company ! = NULL ) {
2009-05-19 21:20:14 +00:00
join_as = ( CompanyID ) atoi ( company ) ;
2006-10-17 22:16:46 +00:00
2009-05-19 21:20:14 +00:00
if ( join_as ! = COMPANY_SPECTATOR ) {
join_as - - ;
if ( join_as > = MAX_COMPANIES ) return false ;
2006-10-17 22:16:46 +00:00
}
2006-10-15 23:48:34 +00:00
}
2004-12-04 17:54:56 +00:00
if ( port ! = NULL ) rport = atoi ( port ) ;
LoadIntroGame ( ) ;
_switch_mode = SM_NONE ;
2009-06-10 19:00:34 +00:00
NetworkClientConnectGame ( NetworkAddress ( network_conn , rport ) , join_as , join_server_password , join_company_password ) ;
2005-01-23 13:09:35 +00:00
}
2004-12-04 17:54:56 +00:00
}
# endif /* ENABLE_NETWORK */
2007-07-05 12:23:54 +00:00
_video_driver - > 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 ( ) ;
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 ( ) ;
2005-01-16 12:14:52 +00:00
2009-08-09 16:54:03 +00:00
free ( const_cast < char * > ( BaseGraphics : : ini_set ) ) ;
2009-08-09 19:50:44 +00:00
free ( const_cast < char * > ( BaseSounds : : ini_set ) ) ;
2009-12-22 21:40:29 +00:00
free ( const_cast < char * > ( BaseMusic : : ini_set ) ) ;
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
2004-08-09 17:04:08 +00:00
return 0 ;
}
2007-03-07 11:47:46 +00:00
void HandleExitGameRequest ( )
2006-09-04 17:30:30 +00:00
{
if ( _game_mode = = GM_MENU ) { // do not ask to quit on the main screen
_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 */
2010-04-17 22:19:14 +00:00
if ( _network_dedicated | | BlitterFactoryBase : : GetCurrentBlitter ( ) - > GetScreenDepth ( ) = = 0 ) {
2008-09-30 20:39:50 +00:00
SetLocalCompany ( COMPANY_SPECTATOR ) ;
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 ) ) {
2010-02-10 15:17:27 +00:00
NetworkChangeCompanyPassword ( _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 ) ;
(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 ) ;
2010-01-15 16:37:19 +00:00
InitializeDynamicVariables ( ) ;
2006-12-10 12:12:26 +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
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
}
2008-09-30 20:39:50 +00:00
void StartupCompanies ( ) ;
2007-03-07 11:47:46 +00:00
void StartupDisasters ( ) ;
extern void StartupEconomy ( ) ;
2004-08-23 10:59:03 +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 filename file to be loaded
* @ 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
* @ param subdir default directory to look for filename , set to 0 if not needed
*/
bool SafeSaveOrLoad ( const char * filename , int mode , GameMode newgm , Subdirectory subdir )
{
GameMode ogm = _game_mode ;
_game_mode = newgm ;
assert ( mode = = SL_LOAD | | mode = = SL_OLD_LOAD ) ;
switch ( SaveOrLoad ( filename , mode , subdir ) ) {
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 ;
}
}
2005-03-12 21:21:47 +00:00
/**
* Start Scenario starts a new game based on a scenario .
* Eg ' New Game ' - - > select a preset scenario
2005-03-13 11:47:04 +00:00
* This starts a scenario based on your current difficulty settings
2005-03-12 21:21:47 +00:00
*/
2007-03-07 11:47:46 +00:00
static void StartScenario ( )
2004-08-09 17:04:08 +00:00
{
_game_mode = GM_NORMAL ;
2007-03-21 15:19:33 +00:00
/* invalid type */
2004-08-09 17:04:08 +00:00
if ( _file_to_saveload . mode = = SL_INVALID ) {
2006-12-26 17:36:18 +00:00
DEBUG ( sl , 0 , " Savegame is obsolete or invalid format: '%s' " , _file_to_saveload . name ) ;
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 ) ;
2004-08-09 17:04:08 +00:00
_game_mode = GM_MENU ;
return ;
}
2007-03-21 15:19:33 +00:00
/* Reinitialize windows */
2005-03-09 19:48:20 +00:00
ResetWindowSystem ( ) ;
2004-08-09 17:04:08 +00:00
2009-02-09 02:57:15 +00:00
SetupColoursAndInitialWindow ( ) ;
2004-08-09 17:04:08 +00:00
2006-12-10 12:12:26 +00:00
ResetGRFConfig ( true ) ;
2007-03-21 15:19:33 +00:00
/* Load game */
2010-06-30 21:38:51 +00:00
if ( ! SafeSaveOrLoad ( _file_to_saveload . name , _file_to_saveload . mode , GM_NORMAL , SCENARIO_DIR ) ) {
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 ) ;
2010-06-30 21:38:51 +00:00
return ;
2004-08-09 17:04:08 +00:00
}
2008-05-29 15:13:28 +00:00
_settings_game . difficulty = _settings_newgame . difficulty ;
2005-03-12 21:21:47 +00:00
2007-03-21 15:19:33 +00:00
/* Inititalize data */
2006-05-22 16:44:16 +00:00
StartupEconomy ( ) ;
2008-09-30 20:39:50 +00:00
StartupCompanies ( ) ;
2004-08-23 08:59:36 +00:00
StartupEngines ( ) ;
StartupDisasters ( ) ;
2008-09-30 20:39:50 +00:00
SetLocalCompany ( COMPANY_FIRST ) ;
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 ;
2004-08-09 17:04:08 +00:00
MarkWholeScreenDirty ( ) ;
}
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 */
2004-12-04 17:54:56 +00:00
if ( new_mode ! = SM_SAVE ) {
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 ) {
2009-08-06 22:00:32 +00:00
if ( _network_server & & ( new_mode = = SM_LOAD | | 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 */
2009-01-12 17:11:45 +00:00
/* Make sure all AI controllers are gone at quiting game */
if ( new_mode ! = SM_SAVE ) 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 ) {
snprintf ( _network_game_info . map_name , lengthof ( _network_game_info . map_name ) , " Random Map " ) ;
}
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
2009-03-15 00:32:18 +00:00
case SM_START_SCENARIO : // New Game --> Choose one of the preset scenarios
2006-02-01 06:32:03 +00:00
# ifdef ENABLE_NETWORK
2008-04-29 18:19:29 +00:00
if ( _network_server ) {
snprintf ( _network_game_info . map_name , lengthof ( _network_game_info . map_name ) , " %s (Loaded scenario) " , _file_to_saveload . title ) ;
}
2006-02-01 06:32:03 +00:00
# endif /* ENABLE_NETWORK */
2008-04-29 18:19:29 +00:00
StartScenario ( ) ;
break ;
2004-12-04 17:54:56 +00:00
2009-03-15 00:32:18 +00:00
case SM_LOAD : { // Load game, Play Scenario
2008-04-29 18:19:29 +00:00
ResetGRFConfig ( true ) ;
ResetWindowSystem ( ) ;
2004-08-09 17:04:08 +00:00
2008-04-29 18:19:29 +00:00
if ( ! SafeSaveOrLoad ( _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 ) {
StartupEngines ( ) ;
}
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 ) {
snprintf ( _network_game_info . map_name , lengthof ( _network_game_info . map_name ) , " %s (Loaded game) " , _file_to_saveload . title ) ;
}
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 ) {
snprintf ( _network_game_info . map_name , lengthof ( _network_game_info . map_name ) , " %s (Heightmap) " , _file_to_saveload . title ) ;
}
(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
2008-04-29 18:19:29 +00:00
if ( SafeSaveOrLoad ( _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 ) ;
2010-02-22 20:19:17 +00:00
BaseSounds : : ini_set = strdup ( BaseSounds : : GetUsedSet ( ) - > name ) ;
}
2008-04-29 18:19:29 +00:00
break ;
2009-03-15 00:32:18 +00:00
case SM_SAVE : // 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 ;
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
}
2004-09-10 19:02:27 +00:00
2006-02-01 06:32:03 +00:00
if ( _switch_mode_errorstr ! = INVALID_STRING_ID ) {
2010-02-24 14:46:15 +00:00
ShowErrorMessage ( _switch_mode_errorstr , INVALID_STRING_ID , WL_CRITICAL ) ;
2010-11-19 13:24:32 +00:00
_switch_mode_errorstr = INVALID_STRING_ID ;
2006-02-01 06:32:03 +00:00
}
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 ( )
{
/* Return here so it is easy to add checks that are run
* always to aid testing of caches . */
2010-03-13 15:55:04 +00:00
if ( _debug_desync_level < = 1 ) return ;
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-11-06 12:37:55 +00:00
NewGRFCache * grf_cache = CallocT < NewGRFCache > ( length ) ;
2010-11-06 12:53:31 +00:00
VehicleCache * veh_cache = CallocT < VehicleCache > ( length ) ;
2010-07-13 20:12:44 +00:00
AccelerationCache * acc_cache = CallocT < AccelerationCache > ( length ) ;
TrainCache * tra_cache = CallocT < TrainCache > ( length ) ;
RoadVehicleCache * roa_cache = CallocT < RoadVehicleCache > ( length ) ;
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 :
acc_cache [ length ] = Train : : From ( u ) - > acc_cache ;
tra_cache [ length ] = Train : : From ( u ) - > tcache ;
break ;
case VEH_ROAD :
acc_cache [ length ] = RoadVehicle : : From ( u ) - > acc_cache ;
roa_cache [ length ] = RoadVehicle : : From ( u ) - > rcache ;
break ;
default :
break ;
}
length + + ;
}
2010-03-13 14:58:37 +00:00
switch ( v - > type ) {
2010-07-13 20:12:44 +00:00
case VEH_TRAIN : Train : : From ( v ) - > ConsistChanged ( true ) ; break ;
case VEH_ROAD : RoadVehUpdateCache ( RoadVehicle : : From ( v ) ) ; break ;
case VEH_AIRCRAFT : UpdateAircraftCache ( Aircraft : : From ( v ) ) ; break ;
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 :
if ( memcmp ( & acc_cache [ length ] , & Train : : From ( u ) - > acc_cache , sizeof ( AccelerationCache ) ) ! = 0 ) {
DEBUG ( desync , 2 , " train acceleration 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 :
if ( memcmp ( & acc_cache [ length ] , & RoadVehicle : : From ( u ) - > acc_cache , sizeof ( AccelerationCache ) ) ! = 0 ) {
DEBUG ( desync , 2 , " road vehicle acceleration cache mismatch: vehicle %i, company %i, unit number %i, wagon %i " , v - > index , ( int ) v - > owner , v - > unitnumber , length ) ;
}
2010-08-29 09:41:50 +00:00
if ( memcmp ( & roa_cache [ length ] , & RoadVehicle : : From ( u ) - > rcache , sizeof ( RoadVehicleCache ) ) ! = 0 ) {
2010-07-13 20:12:44 +00:00
DEBUG ( desync , 2 , " road vehicle cache mismatch: vehicle %i, company %i, unit number %i, wagon %i " , v - > index , ( int ) v - > owner , v - > unitnumber , length ) ;
}
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-07-13 20:12:44 +00:00
free ( acc_cache ) ;
free ( tra_cache ) ;
free ( roa_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
{
2007-03-21 15:19:33 +00:00
/* dont execute the state loop during pause */
2009-05-06 15:06:57 +00:00
if ( _pause_mode ! = PM_UNPAUSED ) {
2008-01-01 22:34:00 +00:00
CallWindowTickEvent ( ) ;
return ;
}
(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
if ( IsGeneratingWorld ( ) ) return ;
2004-09-12 15:29:37 +00:00
2008-01-15 18:51:46 +00:00
ClearStorageChanges ( false ) ;
2004-08-09 17:04:08 +00:00
if ( _game_mode = = GM_EDITOR ) {
RunTileLoop ( ) ;
CallVehicleTicks ( ) ;
CallLandscapeTick ( ) ;
2008-01-15 18:51:46 +00:00
ClearStorageChanges ( true ) ;
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 ] ;
snprintf ( name , lengthof ( name ) , " dmp_cmds_%08x_%08x.sav " , _settings_game . game_creation . generation_seed , _date ) ;
SaveOrLoad ( name , SL_SAVE , AUTOSAVE_DIR ) ;
}
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
2004-08-09 17:04:08 +00:00
AnimateAnimatedTiles ( ) ;
IncreaseDate ( ) ;
RunTileLoop ( ) ;
CallVehicleTicks ( ) ;
CallLandscapeTick ( ) ;
2008-01-15 18:51:46 +00:00
ClearStorageChanges ( true ) ;
2004-08-09 17:04:08 +00:00
2009-01-12 17:11:45 +00:00
AI : : GameLoop ( ) ;
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 */
2007-06-17 15:48:57 +00:00
snprintf ( buf , sizeof ( buf ) , " autosave%d.sav " , _autosave_ctr ) ;
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
{
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 ) {
_do_autosave = false ;
DoAutosave ( ) ;
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? */
2006-02-14 07:32:45 +00:00
if ( _switch_mode ! = SM_NONE ) {
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 */
2006-10-12 14:59:27 +00:00
if ( _network_available ) NetworkUDPGameLoop ( ) ;
2004-12-04 17:54:56 +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
if ( _networking & & ! IsGeneratingWorld ( ) ) {
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
}
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
2009-05-06 15:06:57 +00:00
if ( ! _pause_mode | | _cheats . build_in_pause . value ) 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
2008-11-25 21:09:00 +00:00
_sound_driver - > MainLoop ( ) ;
2005-03-30 19:52:26 +00:00
MusicLoop ( ) ;
2004-08-09 17:04:08 +00:00
}