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"
2022-11-07 21:26:03 +00:00
# include "mixer.h"
2008-03-31 07:03:35 +00:00
# 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"
2023-06-07 22:29:52 +00:00
# include "sl/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"
2023-08-11 19:00:13 +00:00
# include "command_log.h"
2008-03-28 08:53:36 +00:00
# include "news_func.h"
2007-12-28 03:14:55 +00:00
# include "fios.h"
2023-07-03 21:42:28 +00:00
# include "load_check.h"
2007-03-02 12:01:24 +00:00
# include "aircraft.h"
2009-05-22 20:18:45 +00:00
# include "roadveh.h"
2009-05-22 22:22:46 +00:00
# include "train.h"
2010-11-27 21:11:37 +00:00
# include "ship.h"
2008-05-24 10:15:06 +00:00
# include "console_func.h"
2004-11-15 19:25:59 +00:00
# include "screenshot.h"
2007-01-02 17:34:03 +00:00
# include "network/network.h"
2008-05-30 18:20:26 +00:00
# include "network/network_func.h"
2009-01-12 17:11:45 +00:00
# include "ai/ai.hpp"
# include "ai/ai_config.hpp"
2008-01-07 00:19:09 +00:00
# include "settings_func.h"
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
# include "genworld.h"
2011-08-21 12:48:46 +00:00
# include "progress.h"
2007-12-21 19:49:27 +00:00
# include "strings_func.h"
2007-12-26 13:50:40 +00:00
# include "date_func.h"
2007-12-27 13:35:39 +00:00
# include "vehicle_func.h"
2008-06-03 18:35:58 +00:00
# include "gamelog.h"
2008-04-20 08:22:59 +00:00
# include "animated_tile_func.h"
2009-06-23 21:44:48 +00:00
# include "roadstop_base.h"
2008-05-08 16:48:29 +00:00
# include "elrail_func.h"
2008-05-04 21:53:36 +00:00
# include "rev.h"
2009-01-03 18:44:20 +00:00
# include "highscore.h"
2009-10-20 12:20:53 +00:00
# include "station_base.h"
2009-09-07 11:10:49 +00:00
# include "crashlog.h"
2010-01-15 16:41:15 +00:00
# include "engine_func.h"
# include "core/random_func.hpp"
2010-05-18 01:10:23 +00:00
# include "rail_gui.h"
2019-04-06 06:46:15 +00:00
# include "road_gui.h"
2010-05-31 20:22:57 +00:00
# include "core/backup_type.hpp"
2010-07-03 13:28:15 +00:00
# include "hotkeys.h"
2010-09-06 18:20:07 +00:00
# include "newgrf.h"
2015-08-02 21:16:49 +00:00
# include "newgrf_commons.h"
2011-02-18 20:14:30 +00:00
# include "misc/getoptdata.h"
2011-12-19 20:50:54 +00:00
# include "game/game.hpp"
2011-12-19 20:55:56 +00:00
# include "game/game_config.hpp"
2012-04-25 21:06:31 +00:00
# include "town.h"
2012-08-18 11:38:06 +00:00
# include "subsidy_func.h"
2013-07-06 19:00:33 +00:00
# include "gfx_layout.h"
2019-04-23 20:55:27 +00:00
# include "viewport_func.h"
2014-01-02 16:48:16 +00:00
# include "viewport_sprite_sorter.h"
2018-07-19 19:17:07 +00:00
# include "framerate_type.h"
2014-01-14 20:32:07 +00:00
# include "programmable_signals.h"
2015-08-02 18:37:42 +00:00
# include "smallmap_gui.h"
2015-11-11 19:18:32 +00:00
# include "viewport_func.h"
2019-04-09 18:21:39 +00:00
# include "thread.h"
2016-09-18 18:48:52 +00:00
# include "bridge_signal_map.h"
2018-01-22 17:49:53 +00:00
# include "zoning.h"
2018-01-26 18:28:57 +00:00
# include "cargopacket.h"
2019-05-16 19:05:22 +00:00
# include "tbtr_template_vehicle.h"
2019-05-18 17:31:08 +00:00
# include "string_func_extra.h"
2019-05-22 17:33:30 +00:00
# include "industry.h"
2021-08-14 22:17:10 +00:00
# include "network/network_gui.h"
2019-06-08 11:52:34 +00:00
# include "cargopacket.h"
2019-08-05 18:16:51 +00:00
# include "core/checksum_func.hpp"
2020-12-26 09:37:26 +00:00
# include "tbtr_template_vehicle_func.h"
2021-10-03 00:58:54 +00:00
# include "debug_settings.h"
2021-10-03 22:03:26 +00:00
# include "debug_desync.h"
2021-10-04 19:29:02 +00:00
# include "event_logs.h"
2021-12-06 01:58:03 +00:00
# include "tunnelbridge.h"
2022-11-09 21:57:05 +00:00
# include "worker_thread.h"
2023-04-23 10:41:38 +00:00
# include "scope_info.h"
2023-04-25 17:43:45 +00:00
# include "network/network_survey.h"
2023-04-13 15:16:48 +00:00
# include "timer/timer.h"
2023-04-27 15:21:29 +00:00
# include "timer/timer_game_realtime.h"
2023-04-16 18:14:22 +00:00
# include "timer/timer_game_tick.h"
2023-08-28 14:17:53 +00:00
# include "network/network_sync.h"
2006-12-27 12:38:02 +00:00
2013-06-09 12:58:37 +00:00
# include "linkgraph/linkgraphschedule.h"
2015-07-21 23:28:53 +00:00
# include "tracerestrict.h"
2013-06-09 12:58:37 +00:00
2023-06-01 16:41:34 +00:00
# include "3rdparty/cpp-btree/btree_set.h"
2023-08-17 19:21:22 +00:00
# include <atomic>
2021-04-05 19:25:45 +00:00
# include <mutex>
# if defined(__MINGW32__)
2021-04-06 17:42:18 +00:00
# include "3rdparty/mingw-std-threads/mingw.mutex.h"
2021-04-05 19:25:45 +00:00
# endif
2004-08-09 17:04:08 +00:00
# include <stdarg.h>
2019-03-10 23:45:39 +00:00
# include <system_error>
2004-08-09 17:04:08 +00:00
2014-04-23 20:13:33 +00:00
# include "safeguards.h"
2020-12-05 20:57:47 +00:00
# ifdef __EMSCRIPTEN__
# include <emscripten.h>
# include <emscripten / html5.h>
# endif
2007-03-07 11:47:46 +00:00
void CallLandscapeTick ( ) ;
void IncreaseDate ( ) ;
2021-04-05 18:55:53 +00:00
void DoPaletteAnimations ( ) ;
2007-03-07 11:47:46 +00:00
void MusicLoop ( ) ;
2018-05-04 20:29:22 +00:00
void CallWindowGameTickEvent ( ) ;
2011-11-17 21:17:17 +00:00
bool HandleBootstrap ( ) ;
2022-09-02 17:14:35 +00:00
void OnTick_Companies ( bool main_tick ) ;
2004-08-09 17:04:08 +00:00
2023-01-29 08:28:24 +00:00
extern void AfterLoadCompanyStats ( ) ;
2009-08-28 15:23:11 +00:00
extern Company * DoStartupNewCompany ( bool is_ai , CompanyID company = INVALID_COMPANY ) ;
2023-01-29 08:28:24 +00:00
extern void OSOpenBrowser ( const char * url ) ;
2023-02-11 22:11:21 +00:00
extern void RebuildTownCaches ( bool cargo_update_required , bool old_map_position ) ;
2008-06-05 20:54:52 +00:00
extern void ShowOSErrorBox ( const char * buf , bool system ) ;
2023-06-11 15:46:12 +00:00
extern void NORETURN DoOSAbort ( ) ;
2020-12-06 20:11:42 +00:00
extern std : : string _config_file ;
2023-09-30 11:19:05 +00:00
extern uint64 _station_tile_cache_hash ;
2004-08-09 17:04:08 +00:00
2020-12-13 15:56:33 +00:00
bool _save_config = false ;
2021-03-09 13:53:51 +00:00
bool _request_newgrf_scan = false ;
NewGRFScanCallback * _request_newgrf_scan_callback = nullptr ;
2004-08-09 17:04:08 +00:00
2019-08-05 18:16:51 +00:00
SimpleChecksum64 _state_checksum ;
2023-08-17 19:21:22 +00:00
std : : mutex _music_driver_mutex ;
static std : : string _music_driver_params ;
static std : : atomic < bool > _music_inited ;
2008-04-29 18:19:29 +00:00
/**
2008-06-05 20:54:52 +00:00
* Error handling for fatal user errors .
* @ param s the string to print .
* @ note Does NEVER return .
*/
void CDECL usererror ( const char * s , . . . )
{
va_list va ;
char buf [ 512 ] ;
va_start ( va , s ) ;
2014-04-24 19:51:45 +00:00
vseprintf ( buf , lastof ( buf ) , s , va ) ;
2008-06-05 20:54:52 +00:00
va_end ( va ) ;
ShowOSErrorBox ( buf , false ) ;
2019-04-10 21:07:06 +00:00
if ( VideoDriver : : GetInstance ( ) ! = nullptr ) VideoDriver : : GetInstance ( ) - > Stop ( ) ;
2008-06-05 20:54:52 +00:00
2020-12-05 20:57:47 +00:00
# ifdef __EMSCRIPTEN__
emscripten_exit_pointerlock ( ) ;
/* In effect, the game ends here. As emscripten_set_main_loop() caused
* the stack to be unwound , the code after MainLoop ( ) in
* openttd_main ( ) is never executed . */
EM_ASM ( if ( window [ " openttd_abort " ] ) openttd_abort ( ) ) ;
# endif
2023-10-28 15:08:08 +00:00
_exit ( 1 ) ;
2008-06-05 20:54:52 +00:00
}
/**
* 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
{
2023-06-12 20:26:43 +00:00
if ( CrashLog : : HaveAlreadyCrashed ( ) ) DoOSAbort ( ) ;
2004-08-09 17:04:08 +00:00
va_list va ;
2016-09-05 00:18:09 +00:00
char buf [ 2048 ] ;
2005-11-14 19:48:04 +00:00
2004-08-09 17:04:08 +00:00
va_start ( va , s ) ;
2014-04-24 19:51:45 +00:00
vseprintf ( buf , lastof ( buf ) , s , va ) ;
2004-08-09 17:04:08 +00:00
va_end ( va ) ;
2004-09-10 19:02:27 +00:00
2019-04-10 21:07:06 +00:00
if ( VideoDriver : : GetInstance ( ) = = nullptr | | VideoDriver : : GetInstance ( ) - > HasGUI ( ) ) {
2019-01-30 21:28:07 +00:00
ShowOSErrorBox ( buf , true ) ;
}
2004-08-09 17:04:08 +00:00
2009-09-07 11:10:49 +00:00
/* Set the error message for the crash log and then invoke it. */
CrashLog : : SetErrorMessage ( buf ) ;
2023-06-11 15:46:12 +00:00
DoOSAbort ( ) ;
2004-08-09 17:04:08 +00:00
}
2018-07-26 17:48:53 +00:00
void CDECL assert_msg_error ( int line , const char * file , const char * expr , const char * extra , const char * str , . . . )
2016-02-16 18:27:21 +00:00
{
2023-06-12 20:26:43 +00:00
if ( CrashLog : : HaveAlreadyCrashed ( ) ) DoOSAbort ( ) ;
2016-02-16 18:27:21 +00:00
va_list va ;
char buf [ 2048 ] ;
char * b = buf ;
b + = seprintf ( b , lastof ( buf ) , " Assertion failed at line %i of %s: %s \n \t " , line , file , expr ) ;
2018-07-26 17:48:53 +00:00
if ( extra ! = nullptr ) {
b + = seprintf ( b , lastof ( buf ) , " %s \n \t " , extra ) ;
}
2016-02-16 18:27:21 +00:00
va_start ( va , str ) ;
vseprintf ( b , lastof ( buf ) , str , va ) ;
va_end ( va ) ;
2023-06-12 20:26:43 +00:00
if ( VideoDriver : : GetInstance ( ) = = nullptr | | VideoDriver : : GetInstance ( ) - > HasGUI ( ) ) {
ShowOSErrorBox ( buf , true ) ;
}
2016-02-16 18:27:21 +00:00
/* Set the error message for the crash log and then invoke it. */
CrashLog : : SetErrorMessage ( buf ) ;
2023-06-11 15:46:12 +00:00
DoOSAbort ( ) ;
2016-02-16 18:27:21 +00:00
}
2018-07-26 17:48:53 +00:00
const char * assert_tile_info ( uint32 tile ) {
static char buffer [ 128 ] ;
DumpTileInfo ( buffer , lastof ( buffer ) , tile ) ;
return buffer ;
}
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 "
2022-11-05 14:42:20 +00:00
" -s drv = Set sound driver (see below) \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 "
2023-07-25 20:27:23 +00:00
" -n host[: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 "
2023-07-25 20:27:23 +00:00
" -D [host][:port] = Start dedicated server \n "
2023-11-13 19:08:22 +00:00
" -l host[:port] = Redirect DEBUG() \n "
2019-03-04 18:21:13 +00:00
# if !defined(_WIN32)
2004-12-18 14:19:21 +00:00
" -f = Fork into the background (dedicated only) \n "
2006-06-27 21:25:53 +00:00
# endif
2008-08-24 08:41:38 +00:00
" -I graphics_set = Force the graphics set (see below) \n "
2009-08-09 19:50:44 +00:00
" -S sounds_set = Force the sounds set (see below) \n "
2009-12-22 21:40:29 +00:00
" -M music_set = Force the music set (see below) \n "
2006-01-06 22:52:31 +00:00
" -c config_file = Use 'config_file' instead of 'openttd.cfg' \n "
2020-12-13 15:28:06 +00:00
" -x = Never save configuration changes to disk \n "
2021-05-27 16:56:39 +00:00
" -X = Don't use global folders to search for files \n "
2011-10-30 13:47:45 +00:00
" -q savegame = Write some information about the savegame and exit \n "
2022-05-06 08:35:47 +00:00
" -Q = Don't scan for/load NewGRF files on startup \n "
" -QQ = Disable NewGRF scanning/loading entirely \n "
2019-10-07 23:05:31 +00:00
" -Z = Write detailed version information 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 ( ) ;
2022-10-10 11:19:58 +00:00
const std : : string ai_list = AI : : GetConsoleList ( true ) ;
p = strecpy ( p , ai_list . c_str ( ) , lastof ( buf ) ) ;
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 ( ) ;
2022-10-10 11:19:58 +00:00
const std : : string game_list = Game : : GetConsoleList ( true ) ;
p = strecpy ( p , game_list . c_str ( ) , lastof ( buf ) ) ;
2011-12-19 20:55:56 +00:00
Game : : Uninitialize ( true ) ;
2007-01-18 14:08:56 +00:00
/* ShowInfo put output to stderr, but version information should go
* to stdout ; this is the only exception */
2018-12-09 01:28:14 +00:00
# if !defined(_WIN32)
2007-01-18 14:08:56 +00:00
printf ( " %s \n " , buf ) ;
# else
2004-08-09 17:04:08 +00:00
ShowInfo ( buf ) ;
2007-01-18 14:08:56 +00:00
# endif
2004-08-09 17:04:08 +00:00
}
2011-10-30 13:47:45 +00:00
static void WriteSavegameInfo ( const char * name )
{
2019-01-29 00:56:28 +00:00
extern SaveLoadVersion _sl_version ;
2021-08-30 10:21:14 +00:00
extern std : : string _sl_xv_version_label ;
2023-03-10 22:40:31 +00:00
extern SaveLoadVersion _sl_xv_upstream_version ;
2011-10-30 13:47:45 +00:00
uint32 last_ottd_rev = 0 ;
byte ever_modified = 0 ;
bool removed_newgrfs = false ;
2023-07-03 21:42:28 +00:00
GamelogInfo ( _load_check_data . gamelog_actions , & last_ottd_rev , & ever_modified , & removed_newgrfs ) ;
2011-10-30 13:47:45 +00:00
2019-09-14 20:04:21 +00:00
char buf [ 65536 ] ;
2011-10-30 13:47:45 +00:00
char * p = buf ;
p + = seprintf ( p , lastof ( buf ) , " Name: %s \n " , name ) ;
2016-09-13 18:14:19 +00:00
const char * type = " " ;
extern bool _sl_is_faked_ext ;
extern bool _sl_is_ext_version ;
if ( _sl_is_faked_ext ) {
type = " (fake extended) " ;
} else if ( _sl_is_ext_version ) {
type = " (extended) " ;
}
p + = seprintf ( p , lastof ( buf ) , " Savegame ver: %d%s \n " , _sl_version , type ) ;
2021-08-30 10:21:14 +00:00
if ( ! _sl_xv_version_label . empty ( ) ) {
p + = seprintf ( p , lastof ( buf ) , " Version label: %s \n " , _sl_xv_version_label . c_str ( ) ) ;
}
2023-03-07 18:40:53 +00:00
if ( _sl_xv_upstream_version ! = SL_MIN_VERSION ) {
p + = seprintf ( p , lastof ( buf ) , " Upstream version: %u \n " , _sl_xv_upstream_version ) ;
}
2016-09-13 18:14:19 +00:00
for ( size_t i = 0 ; i < XSLFI_SIZE ; i + + ) {
if ( _sl_xv_feature_versions [ i ] > 0 ) {
p + = seprintf ( p , lastof ( buf ) , " Feature: %s = %d \n " , SlXvGetFeatureName ( ( SlXvFeatureIndex ) i ) , _sl_xv_feature_versions [ i ] ) ;
}
}
2011-10-30 13:47:45 +00:00
p + = seprintf ( p , lastof ( buf ) , " NewGRF ver: 0x%08X \n " , last_ottd_rev ) ;
p + = seprintf ( p , lastof ( buf ) , " Modified: %d \n " , ever_modified ) ;
if ( removed_newgrfs ) {
p + = seprintf ( p , lastof ( buf ) , " NewGRFs have been removed \n " ) ;
}
p = strecpy ( p , " NewGRFs: \n " , lastof ( buf ) ) ;
if ( _load_check_data . HasNewGrfs ( ) ) {
2019-04-10 21:07:06 +00:00
for ( GRFConfig * c = _load_check_data . grfconfig ; c ! = nullptr ; c = c - > next ) {
2011-10-30 13:47:45 +00:00
char md5sum [ 33 ] ;
md5sumToString ( md5sum , lastof ( md5sum ) , HasBit ( c - > flags , GCF_COMPATIBLE ) ? c - > original_md5sum : c - > ident . md5sum ) ;
2023-05-31 22:11:44 +00:00
p + = seprintf ( p , lastof ( buf ) , " %08X %s %s \n " , c - > ident . grfid , md5sum , c - > filename . c_str ( ) ) ;
2011-10-30 13:47:45 +00:00
}
}
/* ShowInfo put output to stderr, but version information should go
* to stdout ; this is the only exception */
2018-12-09 01:28:14 +00:00
# if !defined(_WIN32)
2011-10-30 13:47:45 +00:00
printf ( " %s \n " , buf ) ;
# else
ShowInfo ( buf ) ;
# endif
}
2019-09-14 21:00:13 +00:00
static void WriteSavegameDebugData ( const char * name )
{
char * buf = MallocT < char > ( 4096 ) ;
char * buflast = buf + 4095 ;
char * p = buf ;
auto bump_size = [ & ] ( ) {
size_t offset = p - buf ;
size_t new_size = buflast - buf + 1 + 4096 ;
buf = ReallocT < char > ( buf , new_size ) ;
buflast = buf + new_size - 1 ;
p = buf + offset ;
} ;
p + = seprintf ( p , buflast , " Name: %s \n " , name ) ;
if ( _load_check_data . debug_log_data . size ( ) ) {
2020-04-21 17:17:13 +00:00
p + = seprintf ( p , buflast , " %u bytes of debug log data in savegame \n " , ( uint ) _load_check_data . debug_log_data . size ( ) ) ;
2019-09-14 21:00:13 +00:00
std : : string buffer = _load_check_data . debug_log_data ;
2021-03-11 01:08:33 +00:00
ProcessLineByLine ( buffer . data ( ) , [ & ] ( const char * line ) {
2019-09-14 21:00:13 +00:00
if ( buflast - p < = 1024 ) bump_size ( ) ;
p + = seprintf ( p , buflast , " > %s \n " , line ) ;
} ) ;
} else {
2020-04-21 17:17:13 +00:00
p + = seprintf ( p , buflast , " No debug log data in savegame \n " ) ;
}
if ( _load_check_data . debug_config_data . size ( ) ) {
p + = seprintf ( p , buflast , " %u bytes of debug config data in savegame \n " , ( uint ) _load_check_data . debug_config_data . size ( ) ) ;
std : : string buffer = _load_check_data . debug_config_data ;
2021-03-11 01:08:33 +00:00
ProcessLineByLine ( buffer . data ( ) , [ & ] ( const char * line ) {
2020-04-21 17:17:13 +00:00
if ( buflast - p < = 1024 ) bump_size ( ) ;
p + = seprintf ( p , buflast , " > %s \n " , line ) ;
} ) ;
} else {
p + = seprintf ( p , buflast , " No debug config data in savegame \n " ) ;
2019-09-14 21:00:13 +00:00
}
/* ShowInfo put output to stderr, but version information should go
* to stdout ; this is the only exception */
# if !defined(_WIN32)
printf ( " %s \n " , buf ) ;
# else
ShowInfo ( buf ) ;
# endif
free ( buf ) ;
}
2004-08-09 17:04:08 +00:00
2008-04-29 18:19:29 +00:00
/**
* Extract the resolution from the given string and store
* it in the ' res ' parameter .
* @ param res variable to store the resolution in .
* @ param s the string to decompose .
*/
2008-06-16 19:38:41 +00:00
static void ParseResolution ( Dimension * res , const char * s )
2004-08-09 17:04:08 +00:00
{
2007-01-10 18:56:51 +00:00
const char * t = strchr ( s , ' x ' ) ;
2019-04-10 21:07:06 +00:00
if ( t = = nullptr ) {
2004-08-09 17:04:08 +00:00
ShowInfoF ( " Invalid resolution '%s' " , s ) ;
return ;
}
2023-04-26 11:56:14 +00:00
res - > width = std : : max ( std : : strtoul ( s , nullptr , 0 ) , 64UL ) ;
res - > height = std : : max ( std : : strtoul ( t + 1 , nullptr , 0 ) , 64UL ) ;
2004-09-10 19:02:27 +00:00
}
2004-08-09 17:04:08 +00:00
2007-01-14 17:17:30 +00:00
2010-08-01 19:22:34 +00:00
/**
2019-01-17 22:01:07 +00:00
* Uninitializes drivers , frees allocated memory , cleans pools , . . .
2008-05-08 23:26:17 +00:00
* Generally , prepares the game for shutting down
*/
static void ShutdownGame ( )
2005-01-06 22:31:58 +00:00
{
2008-05-08 23:26:17 +00:00
IConsoleFree ( ) ;
if ( _network_available ) NetworkShutDown ( ) ; // Shut down the network and close any open connections
DriverFactoryBase : : ShutdownDrivers ( ) ;
2007-01-14 17:17:30 +00:00
UnInitWindowSystem ( ) ;
2011-12-19 20:50:54 +00:00
/* stop the scripts */
2009-05-14 18:09:50 +00:00
AI : : Uninitialize ( false ) ;
2011-12-19 20:55:56 +00:00
Game : : Uninitialize ( false ) ;
2009-05-14 18:09:50 +00:00
2007-01-14 17:17:30 +00:00
/* Uninitialize variables that are allocated dynamically */
2008-06-03 18:35:58 +00:00
GamelogReset ( ) ;
2005-02-01 18:32:01 +00:00
2013-06-09 12:58:37 +00:00
LinkGraphSchedule : : Clear ( ) ;
2015-07-21 23:28:53 +00:00
ClearTraceRestrictMapping ( ) ;
2016-09-18 18:48:52 +00:00
ClearBridgeSimulatedSignalMapping ( ) ;
2022-06-18 22:37:35 +00:00
ClearBridgeSignalStyleMapping ( ) ;
2018-01-26 18:28:57 +00:00
ClearCargoPacketDeferredPayments ( ) ;
2011-02-19 23:05:47 +00:00
PoolBase : : Clean ( PT_ALL ) ;
2011-02-19 18:25:24 +00:00
2014-01-14 20:32:07 +00:00
FreeSignalPrograms ( ) ;
FreeSignalDependencies ( ) ;
2022-06-25 13:33:47 +00:00
extern void ClearNewSignalStyleMapping ( ) ;
ClearNewSignalStyleMapping ( ) ;
2021-06-27 02:18:38 +00:00
extern void ClearAllSignalSpeedRestrictions ( ) ;
ClearAllSignalSpeedRestrictions ( ) ;
2018-01-22 17:49:53 +00:00
ClearZoningCaches ( ) ;
2019-05-12 03:24:10 +00:00
ClearOrderDestinationRefcountMap ( ) ;
2018-01-22 17:49:53 +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
2022-09-15 17:21:27 +00:00
UninitFontCache ( ) ;
2015-11-11 19:18:32 +00:00
ViewportMapClearTunnelCache ( ) ;
2019-01-25 20:17:15 +00:00
InvalidateVehicleTickCaches ( ) ;
ClearVehicleTickCaches ( ) ;
2020-12-26 09:37:26 +00:00
InvalidateTemplateReplacementImages ( ) ;
2017-02-21 21:04:28 +00:00
ClearCommandLog ( ) ;
2022-09-14 20:55:28 +00:00
ClearCommandQueue ( ) ;
2021-10-04 19:54:12 +00:00
ClearSpecialEventsLog ( ) ;
2019-05-22 18:57:16 +00:00
ClearDesyncMsgLog ( ) ;
2019-05-16 19:15:44 +00:00
2022-11-17 00:40:12 +00:00
extern void UninitializeCompanies ( ) ;
UninitializeCompanies ( ) ;
2020-09-20 16:36:36 +00:00
_loaded_local_company = COMPANY_SPECTATOR ;
2019-05-16 19:15:44 +00:00
_game_events_since_load = ( GameEventFlags ) 0 ;
_game_events_overall = ( GameEventFlags ) 0 ;
2019-06-14 20:43:50 +00:00
_game_load_cur_date_ymd = { 0 , 0 , 0 } ;
_game_load_date_fract = 0 ;
_game_load_tick_skip_counter = 0 ;
_game_load_time = 0 ;
2021-08-28 11:51:06 +00:00
_extra_aspects = 0 ;
2022-06-19 00:55:38 +00:00
_aspect_cfg_hash = 0 ;
2023-09-30 11:19:05 +00:00
_station_tile_cache_hash = 0 ;
2022-12-12 21:40:46 +00:00
InitGRFGlobalVars ( ) ;
2019-05-21 00:49:40 +00:00
_loadgame_DBGL_data . clear ( ) ;
2020-04-21 17:17:13 +00:00
_loadgame_DBGC_data . clear ( ) ;
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
{
2019-08-22 01:28:29 +00:00
UnshowCriticalError ( ) ;
2023-09-18 17:34:50 +00:00
for ( Window * w : Window : : Iterate ( ) ) {
2023-09-15 22:56:33 +00:00
w - > Close ( ) ;
2021-04-28 19:24:24 +00:00
}
2019-07-11 17:21:52 +00:00
2004-12-04 17:54:56 +00:00
_game_mode = GM_MENU ;
2007-04-05 07:49:04 +00:00
2011-08-21 12:52:24 +00:00
if ( load_newgrfs ) ResetGRFConfig ( false ) ;
2004-12-04 17:54:56 +00:00
2007-03-21 15:19:33 +00:00
/* Setup main window */
2005-03-09 19:48:20 +00:00
ResetWindowSystem ( ) ;
2009-02-09 02:57:15 +00:00
SetupColoursAndInitialWindow ( ) ;
2004-12-04 17:54:56 +00:00
2007-06-17 15:48:57 +00:00
/* Load the default opening screen savegame */
2016-09-04 16:06:50 +00:00
if ( SaveOrLoad ( " opntitle.dat " , SLO_LOAD , DFT_GAME_FILE , BASESET_DIR ) ! = SL_OK ) {
2010-01-17 22:59:24 +00:00
GenerateWorld ( GWM_EMPTY , 64 , 64 ) ; // if failed loading, make empty world.
2008-09-30 20:39:50 +00:00
SetLocalCompany ( COMPANY_SPECTATOR ) ;
2008-09-15 23:50:12 +00:00
} else {
2008-09-30 20:39:50 +00:00
SetLocalCompany ( COMPANY_FIRST ) ;
2005-01-07 08:07:24 +00:00
}
2004-12-04 17:54:56 +00:00
2019-04-23 20:55:27 +00:00
FixTitleGameZoom ( ) ;
2009-05-06 15:06:57 +00:00
_pause_mode = PM_UNPAUSED ;
2020-06-05 16:11:09 +00:00
_pause_countdown = 0 ;
2006-08-21 14:59:23 +00:00
_cursor . fix_at = false ;
2004-12-04 17:54:56 +00:00
2011-11-20 11:52:11 +00:00
CheckForMissingGlyphs ( ) ;
2007-12-16 18:38:19 +00:00
2018-06-24 18:06:05 +00:00
MusicLoop ( ) ; // ensure music is correct
2004-12-04 17:54:56 +00:00
}
2009-01-12 17:11:45 +00:00
void MakeNewgameSettingsLive ( )
{
for ( CompanyID c = COMPANY_FIRST ; c < MAX_COMPANIES ; c + + ) {
2019-04-10 21:07:06 +00:00
if ( _settings_game . ai_config [ c ] ! = nullptr ) {
2009-01-12 17:11:45 +00:00
delete _settings_game . ai_config [ c ] ;
}
}
2019-04-10 21:07:06 +00:00
if ( _settings_game . game_config ! = nullptr ) {
2011-12-19 20:55:56 +00:00
delete _settings_game . game_config ;
}
2009-01-12 17:11:45 +00:00
2011-07-03 10:59:25 +00:00
/* Copy newgame settings to active settings.
* Also initialise old settings needed for savegame conversion . */
2009-01-12 17:11:45 +00:00
_settings_game = _settings_newgame ;
2023-02-15 23:25:23 +00:00
_settings_time = _settings_game . game_time = ( TimeSettings ) _settings_client . gui ;
2011-07-03 10:59:25 +00:00
_old_vds = _settings_client . company . vehicle ;
2009-01-12 17:11:45 +00:00
for ( CompanyID c = COMPANY_FIRST ; c < MAX_COMPANIES ; c + + ) {
2019-04-10 21:07:06 +00:00
_settings_game . ai_config [ c ] = nullptr ;
if ( _settings_newgame . ai_config [ c ] ! = nullptr ) {
2009-01-12 17:11:45 +00:00
_settings_game . ai_config [ c ] = new AIConfig ( _settings_newgame . ai_config [ c ] ) ;
2018-09-30 18:34:40 +00:00
if ( ! AIConfig : : GetConfig ( c , AIConfig : : SSS_FORCE_GAME ) - > HasScript ( ) ) {
2023-05-05 19:59:50 +00:00
AIConfig : : GetConfig ( c , AIConfig : : SSS_FORCE_GAME ) - > Change ( std : : nullopt ) ;
2018-09-30 18:34:40 +00:00
}
2009-01-12 17:11:45 +00:00
}
}
2019-04-10 21:07:06 +00:00
_settings_game . game_config = nullptr ;
if ( _settings_newgame . game_config ! = nullptr ) {
2011-12-19 20:55:56 +00:00
_settings_game . game_config = new GameConfig ( _settings_newgame . game_config ) ;
}
2023-05-22 17:40:46 +00:00
SetupTickRate ( ) ;
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. */
2023-04-28 16:25:35 +00:00
if ( StrStartsWith ( url , " http:// " ) | | StrStartsWith ( url , " https:// " ) ) {
OSOpenBrowser ( url ) ;
}
2011-12-11 11:47:08 +00:00
}
2011-08-21 12:50:47 +00:00
/** Callback structure of statements to be executed after the NewGRF scan. */
struct AfterNewGRFScan : NewGRFScanCallback {
2021-05-02 07:18:56 +00:00
Year startyear = INVALID_YEAR ; ///< The start year.
uint32 generation_seed = GENERATE_NEW_SEED ; ///< Seed for the new game.
std : : string dedicated_host ; ///< Hostname for the dedicated server.
uint16 dedicated_port = 0 ; ///< Port for the dedicated server.
std : : string connection_string ; ///< Information about the server to connect to
std : : string join_server_password ; ///< The password to join the server with.
std : : string join_company_password ; ///< The password to join the company with.
bool save_config = true ; ///< The save config setting.
2011-08-21 12:50:47 +00:00
2011-11-14 21:42:54 +00:00
/**
* Create a new callback .
*/
2021-05-02 07:18:56 +00:00
AfterNewGRFScan ( )
2011-08-21 12:50:47 +00:00
{
2018-10-27 06:17:40 +00:00
/* Visual C++ 2015 fails compiling this line (AfterNewGRFScan::generation_seed undefined symbol)
* if it ' s placed outside a member function , directly in the struct body . */
2020-12-27 10:44:22 +00:00
static_assert ( sizeof ( generation_seed ) = = sizeof ( _settings_game . game_creation . generation_seed ) ) ;
2011-08-21 12:50:47 +00:00
}
2023-04-13 06:23:18 +00:00
void OnNewGRFsScanned ( ) override
2011-08-21 12:50:47 +00:00
{
ResetGRFConfig ( false ) ;
2011-11-14 21:42:54 +00:00
TarScanner : : DoScan ( TarScanner : : SCENARIO ) ;
AI : : Initialize ( ) ;
2011-12-19 20:55:56 +00:00
Game : : Initialize ( ) ;
2011-11-14 21:42:54 +00:00
/* We want the new (correct) NewGRF count to survive the loading. */
uint last_newgrf_count = _settings_client . gui . last_newgrf_count ;
LoadFromConfig ( ) ;
_settings_client . gui . last_newgrf_count = last_newgrf_count ;
2011-12-04 11:18:43 +00:00
/* Since the default for the palette might have changed due to
* reading the configuration file , recalculate that now . */
UpdateNewGRFConfigPalette ( ) ;
2011-11-14 21:42:54 +00:00
2011-12-19 20:55:56 +00:00
Game : : Uninitialize ( true ) ;
2011-11-14 21:42:54 +00:00
AI : : Uninitialize ( true ) ;
2011-08-24 17:04:18 +00:00
LoadFromHighScore ( ) ;
LoadHotkeysFromConfig ( ) ;
2013-05-26 19:27:22 +00:00
WindowDesc : : LoadFromConfig ( ) ;
2011-08-24 17:04:18 +00:00
2011-11-14 21:42:54 +00:00
/* We have loaded the config, so we may possibly save it. */
2020-12-13 15:28:06 +00:00
_save_config = save_config ;
2011-11-14 21:42:54 +00:00
2022-11-07 21:26:03 +00:00
/* restore saved music and effects volumes */
2014-04-28 21:06:51 +00:00
MusicDriver : : GetInstance ( ) - > SetVolume ( _settings_client . music . music_vol ) ;
2022-11-07 21:26:03 +00:00
SetEffectVolume ( _settings_client . music . effect_vol ) ;
2011-11-14 21:42:54 +00:00
2021-04-11 11:52:55 +00:00
if ( startyear ! = INVALID_YEAR ) IConsoleSetSetting ( " game_creation.starting_year " , startyear ) ;
2011-08-24 17:04:18 +00:00
if ( generation_seed ! = GENERATE_NEW_SEED ) _settings_newgame . game_creation . generation_seed = generation_seed ;
2021-05-04 17:32:37 +00:00
if ( ! dedicated_host . empty ( ) ) {
2019-04-02 19:31:33 +00:00
_network_bind_list . clear ( ) ;
_network_bind_list . emplace_back ( dedicated_host ) ;
2011-08-24 17:04:18 +00:00
}
if ( dedicated_port ! = 0 ) _settings_client . network . server_port = dedicated_port ;
2011-08-21 12:50:47 +00:00
/* initialize the ingame console */
IConsoleInit ( ) ;
InitializeGUI ( ) ;
IConsoleCmdExec ( " exec scripts/autoexec.scr 0 " ) ;
/* Make sure _settings is filled with _settings_newgame if we switch to a game directly */
if ( _switch_mode ! = SM_NONE ) MakeNewgameSettingsLive ( ) ;
2021-05-02 07:18:56 +00:00
if ( _network_available & & ! connection_string . empty ( ) ) {
2011-08-21 12:50:47 +00:00
LoadIntroGame ( ) ;
_switch_mode = SM_NONE ;
2021-04-29 10:09:03 +00:00
2021-05-02 07:18:56 +00:00
NetworkClientConnectGame ( connection_string , COMPANY_NEW_COMPANY , join_server_password , join_company_password ) ;
2011-08-21 12:50:47 +00:00
}
/* After the scan we're not used anymore. */
delete this ;
}
} ;
2023-08-31 09:10:11 +00:00
void PostMainLoop ( )
{
WaitTillSaved ( ) ;
/* only save config if we have to */
if ( _save_config ) {
2023-11-17 19:16:55 +00:00
SaveToConfig ( STCF_ALL ) ;
2023-08-31 09:10:11 +00:00
SaveHotkeysToConfig ( ) ;
WindowDesc : : SaveToConfig ( ) ;
SaveToHighScore ( ) ;
}
/* Reset windowing system, stop drivers, free used memory, ... */
ShutdownGame ( ) ;
}
2019-03-04 18:21:13 +00:00
# if defined(UNIX)
2007-03-07 11:47:46 +00:00
extern void DedicatedFork ( ) ;
2005-08-06 14:58:06 +00:00
# endif
2004-12-18 14:19:21 +00:00
2011-02-18 20:28:25 +00:00
/** Options of OpenTTD. */
static const OptionData _options [ ] = {
GETOPT_SHORT_VALUE ( ' I ' ) ,
GETOPT_SHORT_VALUE ( ' S ' ) ,
GETOPT_SHORT_VALUE ( ' M ' ) ,
GETOPT_SHORT_VALUE ( ' m ' ) ,
GETOPT_SHORT_VALUE ( ' s ' ) ,
GETOPT_SHORT_VALUE ( ' v ' ) ,
GETOPT_SHORT_VALUE ( ' b ' ) ,
GETOPT_SHORT_OPTVAL ( ' D ' ) ,
2023-07-25 20:11:21 +00:00
GETOPT_SHORT_VALUE ( ' n ' ) ,
2011-02-18 20:28:25 +00:00
GETOPT_SHORT_VALUE ( ' l ' ) ,
GETOPT_SHORT_VALUE ( ' p ' ) ,
GETOPT_SHORT_VALUE ( ' P ' ) ,
2019-03-04 18:21:13 +00:00
# if !defined(_WIN32)
2011-02-18 20:28:25 +00:00
GETOPT_SHORT_NOVAL ( ' f ' ) ,
# endif
GETOPT_SHORT_VALUE ( ' r ' ) ,
GETOPT_SHORT_VALUE ( ' t ' ) ,
GETOPT_SHORT_OPTVAL ( ' d ' ) ,
GETOPT_SHORT_NOVAL ( ' e ' ) ,
GETOPT_SHORT_OPTVAL ( ' g ' ) ,
GETOPT_SHORT_VALUE ( ' G ' ) ,
GETOPT_SHORT_VALUE ( ' c ' ) ,
GETOPT_SHORT_NOVAL ( ' x ' ) ,
2021-05-27 16:56:39 +00:00
GETOPT_SHORT_NOVAL ( ' X ' ) ,
2011-10-30 13:47:45 +00:00
GETOPT_SHORT_VALUE ( ' q ' ) ,
2019-09-14 21:00:13 +00:00
GETOPT_SHORT_VALUE ( ' K ' ) ,
2011-02-18 20:28:25 +00:00
GETOPT_SHORT_NOVAL ( ' h ' ) ,
2022-05-06 08:35:47 +00:00
GETOPT_SHORT_NOVAL ( ' Q ' ) ,
2018-01-15 21:47:10 +00:00
GETOPT_SHORT_VALUE ( ' J ' ) ,
2019-10-07 23:05:31 +00:00
GETOPT_SHORT_NOVAL ( ' Z ' ) ,
2011-02-18 20:28:25 +00:00
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
{
2016-02-16 19:53:22 +00:00
SetSelfAsMainThread ( ) ;
2021-03-12 17:30:40 +00:00
PerThreadSetup ( ) ;
2022-02-04 21:29:25 +00:00
SlXvSetStaticCurrentVersions ( ) ;
2020-05-17 21:32:08 +00:00
std : : string musicdriver ;
std : : string sounddriver ;
std : : string videodriver ;
std : : string blitter ;
2020-05-17 21:32:06 +00:00
std : : string graphics_set ;
std : : string sounds_set ;
std : : string music_set ;
2008-06-16 19:38:41 +00:00
Dimension resolution = { 0 , 0 } ;
2020-12-13 15:28:06 +00:00
std : : unique_ptr < AfterNewGRFScan > scanner ( new AfterNewGRFScan ( ) ) ;
2006-02-21 17:29:53 +00:00
bool dedicated = false ;
2019-04-10 21:07:06 +00:00
char * debuglog_conn = nullptr ;
2021-05-27 16:56:39 +00:00
bool only_local_path = false ;
2010-07-19 17:46:53 +00:00
extern bool _dedicated_forks ;
_dedicated_forks = false ;
2005-02-23 09:13:12 +00:00
2004-08-09 17:04:08 +00:00
_game_mode = GM_MENU ;
_switch_mode = SM_MENU ;
2011-02-18 20:28:25 +00:00
GetOptData mgo ( argc - 1 , argv + 1 , _options ) ;
2013-11-24 15:21:38 +00:00
int ret = 0 ;
2007-01-10 18:56:51 +00:00
2011-02-18 20:28:25 +00:00
int i ;
2011-02-18 20:14:30 +00:00
while ( ( i = mgo . GetOpt ( ) ) ! = - 1 ) {
2006-02-01 06:32:03 +00:00
switch ( i ) {
2020-05-17 21:32:06 +00:00
case ' I ' : graphics_set = mgo . opt ; break ;
case ' S ' : sounds_set = mgo . opt ; break ;
case ' M ' : music_set = mgo . opt ; break ;
2020-05-17 21:32:08 +00:00
case ' m ' : musicdriver = mgo . opt ; break ;
case ' s ' : sounddriver = mgo . opt ; break ;
case ' v ' : videodriver = mgo . opt ; break ;
case ' b ' : blitter = mgo . opt ; break ;
2006-02-21 17:29:53 +00:00
case ' D ' :
2020-05-17 21:32:08 +00:00
musicdriver = " null " ;
sounddriver = " null " ;
videodriver = " dedicated " ;
blitter = " null " ;
2006-02-21 17:29:53 +00:00
dedicated = true ;
2022-08-27 14:50:50 +00:00
SetDebugString ( " net=3 " , ShowInfo ) ;
2019-04-10 21:07:06 +00:00
if ( mgo . opt ! = nullptr ) {
2021-05-04 17:32:37 +00:00
scanner - > dedicated_host = ParseFullConnectionString ( mgo . opt , scanner - > dedicated_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 ' :
2023-07-25 20:27:23 +00:00
scanner - > connection_string = mgo . opt ; // host:port#company parameter
2006-02-21 17:29:53 +00:00
break ;
2007-02-08 12:27:53 +00:00
case ' l ' :
debuglog_conn = mgo . opt ;
break ;
2009-06-10 19:00:34 +00:00
case ' p ' :
2011-08-24 17:04:18 +00:00
scanner - > join_server_password = mgo . opt ;
2009-06-10 19:00:34 +00:00
break ;
case ' P ' :
2011-08-24 17:04:18 +00:00
scanner - > join_company_password = mgo . opt ;
2009-06-10 19:00:34 +00:00
break ;
2008-06-16 19:38:41 +00:00
case ' r ' : ParseResolution ( & resolution , mgo . opt ) ; break ;
2011-08-24 17:04:18 +00:00
case ' t ' : scanner - > startyear = atoi ( mgo . opt ) ; break ;
2004-08-09 17:04:08 +00:00
case ' d ' : {
2018-12-09 01:28:14 +00:00
# if defined(_WIN32)
2004-08-09 17:04:08 +00:00
CreateConsole ( ) ;
# endif
2022-08-26 10:08:47 +00:00
if ( mgo . opt ! = nullptr ) SetDebugString ( mgo . opt , ShowInfo ) ;
2010-08-01 18:53:30 +00:00
break ;
}
2012-01-22 18:37:01 +00:00
case ' e ' : _switch_mode = ( _switch_mode = = SM_LOAD_GAME | | _switch_mode = = SM_LOAD_SCENARIO ? SM_LOAD_SCENARIO : SM_EDITOR ) ; break ;
2004-09-10 19:02:27 +00:00
case ' g ' :
2019-04-10 21:07:06 +00:00
if ( mgo . opt ! = nullptr ) {
2023-04-16 19:44:53 +00:00
_file_to_saveload . name = mgo . opt ;
2016-09-04 12:57:43 +00:00
bool is_scenario = _switch_mode = = SM_EDITOR | | _switch_mode = = SM_LOAD_SCENARIO ;
_switch_mode = is_scenario ? SM_LOAD_SCENARIO : SM_LOAD_GAME ;
2016-09-04 16:06:50 +00:00
_file_to_saveload . SetMode ( SLO_LOAD , is_scenario ? FT_SCENARIO : FT_SAVEGAME , DFT_GAME_FILE ) ;
2008-07-28 15:31:11 +00:00
/* if the file doesn't exist or it is not a valid savegame, let the saveload code show an error */
2020-12-06 20:11:45 +00:00
auto t = _file_to_saveload . name . find_last_of ( ' . ' ) ;
if ( t ! = std : : string : : npos ) {
2020-12-06 20:11:50 +00:00
FiosType ft = FiosGetSavegameListCallback ( SLO_LOAD , _file_to_saveload . name , _file_to_saveload . name . substr ( t ) . c_str ( ) , nullptr , nullptr ) ;
2016-09-04 12:47:07 +00:00
if ( ft ! = FIOS_TYPE_INVALID ) _file_to_saveload . SetMode ( ft ) ;
2008-07-28 15:31:11 +00:00
}
break ;
2006-06-27 21:25:53 +00:00
}
2008-07-28 15:31:11 +00:00
_switch_mode = SM_NEWGAME ;
2008-09-27 22:19:34 +00:00
/* Give a random map if no seed has been given */
2011-08-24 17:04:18 +00:00
if ( scanner - > generation_seed = = GENERATE_NEW_SEED ) {
scanner - > generation_seed = InteractiveRandom ( ) ;
2008-09-27 22:19:34 +00:00
}
2004-08-09 17:04:08 +00:00
break ;
2019-09-14 21:00:13 +00:00
case ' q ' :
case ' K ' : {
2021-05-27 16:56:39 +00:00
DeterminePaths ( argv [ 0 ] , only_local_path ) ;
2013-11-24 15:21:38 +00:00
if ( StrEmpty ( mgo . opt ) ) {
ret = 1 ;
2020-05-17 21:32:10 +00:00
return ret ;
2013-11-24 15:21:38 +00:00
}
2011-10-30 13:47:45 +00:00
char title [ 80 ] ;
title [ 0 ] = ' \0 ' ;
2016-09-04 16:06:50 +00:00
FiosGetSavegameListCallback ( SLO_LOAD , mgo . opt , strrchr ( mgo . opt , ' . ' ) , title , lastof ( title ) ) ;
2011-10-30 13:47:45 +00:00
_load_check_data . Clear ( ) ;
2020-04-21 17:17:13 +00:00
if ( i = = ' K ' ) _load_check_data . want_debug_data = true ;
2020-08-27 18:25:07 +00:00
_load_check_data . want_grf_compatibility = false ;
2016-09-04 16:06:50 +00:00
SaveOrLoadResult res = SaveOrLoad ( mgo . opt , SLO_CHECK , DFT_GAME_FILE , SAVE_DIR , false ) ;
2011-10-30 13:47:45 +00:00
if ( res ! = SL_OK | | _load_check_data . HasErrors ( ) ) {
fprintf ( stderr , " Failed to open savegame \n " ) ;
if ( _load_check_data . HasErrors ( ) ) {
2021-07-16 16:10:43 +00:00
InitializeLanguagePacks ( ) ; // A language pack is needed for GetString()
2011-10-30 13:47:45 +00:00
char buf [ 256 ] ;
2023-04-30 19:46:49 +00:00
SetDParamStr ( 0 , _load_check_data . error_msg ) ;
2011-10-30 13:47:45 +00:00
GetString ( buf , _load_check_data . error , lastof ( buf ) ) ;
fprintf ( stderr , " %s \n " , buf ) ;
}
2020-05-17 21:32:10 +00:00
return ret ;
2011-10-30 13:47:45 +00:00
}
2019-09-14 21:00:13 +00:00
if ( i = = ' q ' ) {
WriteSavegameInfo ( title ) ;
} else {
WriteSavegameDebugData ( title ) ;
}
2020-05-17 21:32:10 +00:00
return ret ;
2011-10-30 13:47:45 +00:00
}
2022-05-06 08:35:47 +00:00
case ' Q ' : {
extern int _skip_all_newgrf_scanning ;
_skip_all_newgrf_scanning + = 1 ;
break ;
}
2023-04-26 11:56:14 +00:00
case ' G ' : scanner - > generation_seed = std : : strtoul ( mgo . opt , nullptr , 10 ) ; break ;
2020-12-06 20:11:42 +00:00
case ' c ' : _config_file = mgo . opt ; break ;
2011-11-14 21:42:54 +00:00
case ' x ' : scanner - > save_config = false ; break ;
2018-01-15 21:47:10 +00:00
case ' J ' : _quit_after_days = Clamp ( atoi ( mgo . opt ) , 0 , INT_MAX ) ; break ;
2019-10-07 23:05:31 +00:00
case ' Z ' : {
CrashLog : : VersionInfoLog ( ) ;
2020-05-21 19:19:57 +00:00
return ret ;
2019-10-07 23:05:31 +00:00
}
2021-05-27 16:56:39 +00:00
case ' X ' : only_local_path = true ; 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 ) {
2021-05-08 10:02:30 +00:00
/* Either the user typed '-h', they made an error, or they added unrecognized command line arguments.
2011-02-18 20:28:25 +00:00
* 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 . */
2021-05-27 16:56:39 +00:00
DeterminePaths ( argv [ 0 ] , only_local_path ) ;
2011-11-14 21:40:39 +00:00
TarScanner : : DoScan ( TarScanner : : BASESET ) ;
2011-02-18 20:28:25 +00:00
BaseGraphics : : FindSets ( ) ;
BaseSounds : : FindSets ( ) ;
BaseMusic : : FindSets ( ) ;
ShowHelp ( ) ;
2020-05-17 21:32:10 +00:00
return ret ;
2004-08-09 17:04:08 +00:00
}
2021-05-27 16:56:39 +00:00
DeterminePaths ( argv [ 0 ] , only_local_path ) ;
2011-11-14 21:42:54 +00:00
TarScanner : : DoScan ( TarScanner : : BASESET ) ;
2004-12-18 14:19:21 +00:00
2021-05-12 14:34:02 +00:00
if ( dedicated ) DEBUG ( net , 3 , " Starting dedicated server, version %s " , _openttd_revision ) ;
2011-08-24 17:04:18 +00:00
if ( _dedicated_forks & & ! dedicated ) _dedicated_forks = false ;
2019-03-04 18:21:13 +00:00
# if defined(UNIX)
2007-03-21 15:19:33 +00:00
/* We must fork here, or we'll end up without some resources we need (like sockets) */
2010-07-19 15:44:49 +00:00
if ( _dedicated_forks ) DedicatedFork ( ) ;
2004-12-18 14:19:21 +00:00
# endif
2011-11-14 21:42:54 +00:00
LoadFromConfig ( true ) ;
2004-08-09 17:04:08 +00:00
2011-11-14 21:42:54 +00:00
if ( resolution . width ! = 0 ) _cur_resolution = resolution ;
2004-08-09 17:04:08 +00:00
2021-01-14 20:53:06 +00:00
/* Limit width times height times bytes per pixel to fit a 32 bit
* integer , This way all internal drawing routines work correctly .
* A resolution that has one component as 0 is treated as a marker to
* auto - detect a good window size . */
_cur_resolution . width = std : : min ( _cur_resolution . width , UINT16_MAX / 2u ) ;
_cur_resolution . height = std : : min ( _cur_resolution . height , UINT16_MAX / 2u ) ;
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 ( ) ;
2022-09-15 17:21:27 +00:00
/* Initialize the font cache */
InitFontCache ( 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 ( ) ;
2023-10-02 11:29:32 +00:00
bool valid_graphics_set ;
if ( ! graphics_set . empty ( ) ) {
2023-10-02 12:17:32 +00:00
valid_graphics_set = BaseGraphics : : SetSetByName ( graphics_set ) ;
} else if ( BaseGraphics : : ini_data . shortname ! = 0 ) {
graphics_set = BaseGraphics : : ini_data . name ;
valid_graphics_set = BaseGraphics : : SetSetByShortname ( BaseGraphics : : ini_data . shortname ) ;
2023-10-02 12:37:43 +00:00
if ( valid_graphics_set & & ! BaseGraphics : : ini_data . extra_params . empty ( ) ) {
GRFConfig & extra_cfg = BaseGraphics : : GetUsedSet ( ) - > GetOrCreateExtraConfig ( ) ;
if ( extra_cfg . IsCompatible ( BaseGraphics : : ini_data . extra_version ) ) {
extra_cfg . SetParams ( BaseGraphics : : ini_data . extra_params ) ;
}
2012-07-08 18:41:50 +00:00
}
2023-10-02 11:29:32 +00:00
} else if ( ! BaseGraphics : : ini_data . name . empty ( ) ) {
graphics_set = BaseGraphics : : ini_data . name ;
2023-10-02 12:17:32 +00:00
valid_graphics_set = BaseGraphics : : SetSetByName ( BaseGraphics : : ini_data . name ) ;
2023-10-02 11:29:32 +00:00
} else {
valid_graphics_set = true ;
BaseGraphics : : SetSet ( nullptr ) ; // ignore error, continue to bootstrap GUI
}
if ( ! valid_graphics_set ) {
BaseGraphics : : SetSet ( nullptr ) ;
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
}
2006-11-28 14:32:24 +00:00
/* Initialize game palette */
GfxInitPalettes ( ) ;
2007-09-13 12:28:53 +00:00
DEBUG ( misc , 1 , " Loading blitter... " ) ;
2020-05-17 21:32:08 +00:00
if ( blitter . empty ( ) & & ! _ini_blitter . empty ( ) ) blitter = _ini_blitter ;
_blitter_autodetected = blitter . empty ( ) ;
2014-04-27 12:15:14 +00:00
/* Activate the initial blitter.
* This is only some initial guess , after NewGRFs have been loaded SwitchNewGRFBlitter may switch to a different one .
* - Never guess anything , if the user specified a blitter . ( _blitter_autodetected )
* - Use 32 bpp blitter if baseset or 8 bpp - support settings says so .
* - Use 8 bpp blitter otherwise .
*/
if ( ! _blitter_autodetected | |
2019-04-10 21:07:06 +00:00
( _support8bpp ! = S8BPP_NONE & & ( BaseGraphics : : GetUsedSet ( ) = = nullptr | | BaseGraphics : : GetUsedSet ( ) - > blitter = = BLT_8BPP ) ) | |
BlitterFactory : : SelectBlitter ( " 32bpp-anim " ) = = nullptr ) {
if ( BlitterFactory : : SelectBlitter ( blitter ) = = nullptr ) {
2020-05-17 21:32:08 +00:00
blitter . empty ( ) ?
2011-10-04 21:35:47 +00:00
usererror ( " Failed to autoprobe blitter " ) :
2020-05-17 21:32:08 +00:00
usererror ( " Failed to select requested blitter '%s'; does it exist? " , blitter . c_str ( ) ) ;
2011-10-04 21:35:47 +00:00
}
2010-07-24 10:14:39 +00:00
}
2007-07-05 12:23:54 +00:00
2020-05-17 21:32:08 +00:00
if ( videodriver . empty ( ) & & ! _ini_videodriver . empty ( ) ) videodriver = _ini_videodriver ;
2014-04-28 21:06:51 +00:00
DriverFactoryBase : : SelectDriver ( videodriver , Driver : : DT_VIDEO ) ;
2007-07-05 12:23:54 +00:00
2014-01-02 16:48:16 +00:00
InitializeSpriteSorter ( ) ;
2007-05-19 23:52:04 +00:00
/* Initialize the zoom level of the screen to normal */
_screen . zoom = ZOOM_LVL_NORMAL ;
2021-04-20 10:49:20 +00:00
/* The video driver is now selected, now initialise GUI zoom */
2022-12-06 18:29:55 +00:00
AdjustGUIZoom ( AGZM_STARTUP ) ;
2004-08-09 17:04:08 +00:00
2011-11-17 21:17:17 +00:00
NetworkStartUp ( ) ; // initialize network-core
2019-04-10 21:07:06 +00:00
if ( debuglog_conn ! = nullptr & & _network_available ) {
2021-04-29 14:43:13 +00:00
NetworkStartDebugLog ( debuglog_conn ) ;
2007-02-08 12:27:53 +00:00
}
2013-11-24 16:21:19 +00:00
if ( ! HandleBootstrap ( ) ) {
ShutdownGame ( ) ;
2020-05-17 21:32:10 +00:00
return ret ;
2013-11-24 16:21:19 +00:00
}
2011-11-17 21:21:45 +00:00
2014-04-28 21:06:51 +00:00
VideoDriver : : GetInstance ( ) - > ClaimMousePointer ( ) ;
2011-11-17 21:21:45 +00:00
/* initialize screenshot formats */
InitializeScreenshotFormats ( ) ;
BaseSounds : : FindSets ( ) ;
2020-05-17 21:32:06 +00:00
if ( sounds_set . empty ( ) & & ! BaseSounds : : ini_set . empty ( ) ) sounds_set = BaseSounds : : ini_set ;
2023-10-02 12:17:32 +00:00
if ( ! BaseSounds : : SetSetByName ( sounds_set ) ) {
2020-05-17 21:32:06 +00:00
if ( sounds_set . empty ( ) | | ! BaseSounds : : SetSet ( { } ) ) {
2019-12-26 18:36:53 +00:00
usererror ( " Failed to find a sounds set. Please acquire a sounds set for OpenTTD. See section 1.4 of README.md. " ) ;
2012-07-08 18:41:50 +00:00
} else {
ErrorMessageData msg ( STR_CONFIG_ERROR , STR_CONFIG_ERROR_INVALID_BASE_SOUNDS_NOT_FOUND ) ;
2021-05-30 09:40:54 +00:00
msg . SetDParamStr ( 0 , sounds_set ) ;
2012-07-08 18:41:50 +00:00
ScheduleErrorMessage ( msg ) ;
}
2011-11-17 21:21:45 +00:00
}
BaseMusic : : FindSets ( ) ;
2020-05-17 21:32:06 +00:00
if ( music_set . empty ( ) & & ! BaseMusic : : ini_set . empty ( ) ) music_set = BaseMusic : : ini_set ;
2023-10-02 12:17:32 +00:00
if ( ! BaseMusic : : SetSetByName ( music_set ) ) {
2020-05-17 21:32:06 +00:00
if ( music_set . empty ( ) | | ! BaseMusic : : SetSet ( { } ) ) {
2019-12-26 18:36:53 +00:00
usererror ( " Failed to find a music set. Please acquire a music set for OpenTTD. See section 1.4 of README.md. " ) ;
2012-07-08 18:41:50 +00:00
} else {
ErrorMessageData msg ( STR_CONFIG_ERROR , STR_CONFIG_ERROR_INVALID_BASE_MUSIC_NOT_FOUND ) ;
2021-05-30 09:40:54 +00:00
msg . SetDParamStr ( 0 , music_set ) ;
2012-07-08 18:41:50 +00:00
ScheduleErrorMessage ( msg ) ;
}
2011-11-17 21:21:45 +00:00
}
2020-05-17 21:32:08 +00:00
if ( sounddriver . empty ( ) & & ! _ini_sounddriver . empty ( ) ) sounddriver = _ini_sounddriver ;
2014-04-28 21:06:51 +00:00
DriverFactoryBase : : SelectDriver ( sounddriver , Driver : : DT_SOUND ) ;
2011-11-17 21:21:45 +00:00
2020-05-17 21:32:08 +00:00
if ( musicdriver . empty ( ) & & ! _ini_musicdriver . empty ( ) ) musicdriver = _ini_musicdriver ;
2023-08-17 19:21:22 +00:00
_music_driver_params = std : : move ( musicdriver ) ;
if ( _music_driver_params . empty ( ) & & BaseMusic : : GetUsedSet ( ) - > name = = " NoMusic " ) {
DEBUG ( driver , 1 , " Deferring loading of music driver until a music set is loaded " ) ;
DriverFactoryBase : : SelectDriver ( " null " , Driver : : DT_MUSIC ) ;
} else {
InitMusicDriver ( false ) ;
}
2011-11-17 21:21:45 +00:00
2010-01-17 22:59:24 +00:00
GenerateWorld ( GWM_EMPTY , 64 , 64 ) ; // Make the viewport initialization happy
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. */
2021-03-09 13:53:51 +00:00
RequestNewGRFScan ( scanner . release ( ) ) ;
2004-12-04 17:54:56 +00:00
2022-11-09 21:57:05 +00:00
_general_worker_pool . Start ( " ottd:worker " , 8 ) ;
2014-04-28 21:06:51 +00:00
VideoDriver : : GetInstance ( ) - > MainLoop ( ) ;
2004-08-25 10:17:39 +00:00
2022-11-09 21:57:05 +00:00
_general_worker_pool . Stop ( ) ;
2023-08-31 09:10:11 +00:00
PostMainLoop ( ) ;
2013-11-24 15:21:38 +00:00
return ret ;
2004-08-09 17:04:08 +00:00
}
2023-08-17 19:21:22 +00:00
void InitMusicDriver ( bool init_volume )
{
if ( _music_inited . exchange ( true ) ) return ;
{
std : : unique_lock < std : : mutex > lock ( _music_driver_mutex ) ;
static std : : unique_ptr < MusicDriver > old_driver ;
2023-08-23 18:45:44 +00:00
old_driver = MusicDriver : : ExtractDriver ( ) ;
2023-08-17 19:21:22 +00:00
DriverFactoryBase : : SelectDriver ( _music_driver_params , Driver : : DT_MUSIC ) ;
}
if ( init_volume ) MusicDriver : : GetInstance ( ) - > SetVolume ( _settings_client . music . music_vol ) ;
}
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 ( ) ;
2023-04-25 17:43:45 +00:00
_survey . Transmit ( NetworkSurveyHandler : : Reason : : EXIT , true ) ;
2006-09-04 17:30:30 +00:00
_exit_game = true ;
} else {
AskExitGame ( ) ;
}
}
2022-06-09 19:32:31 +00:00
/**
* Triggers everything required to set up a saved scenario for a new game .
*/
static void OnStartScenario ( )
{
/* Reset engine pool to simplify changing engine NewGRFs in scenario editor. */
EngineOverrideManager : : ResetToCurrentNewGRFConfig ( ) ;
2022-06-09 19:36:55 +00:00
/* Make sure all industries were built "this year", to avoid too early closures. (#9918) */
for ( Industry * i : Industry : : Iterate ( ) ) {
i - > last_prod_year = _cur_year ;
}
2022-06-09 19:32:31 +00:00
}
2021-05-05 17:21:12 +00:00
/**
* Triggers everything that should be triggered when starting a game .
* @ param dedicated_server Whether this is a dedicated server or not .
*/
static void OnStartGame ( bool dedicated_server )
{
/* Update the local company for a loaded game. It is either always
2021-09-18 21:10:04 +00:00
* a company or in the case of a dedicated server a spectator */
if ( _network_server & & ! dedicated_server ) {
NetworkServerDoMove ( CLIENT_ID_SERVER , GetDefaultLocalCompany ( ) ) ;
} else {
SetLocalCompany ( dedicated_server ? COMPANY_SPECTATOR : GetDefaultLocalCompany ( ) ) ;
}
if ( _ctrl_pressed & & ! dedicated_server ) {
DoCommandP ( 0 , PM_PAUSED_NORMAL , 1 , CMD_PAUSE ) ;
}
2021-05-05 17:21:12 +00:00
/* Update the static game info to set the values from the new game. */
NetworkServerUpdateGameInfo ( ) ;
/* Execute the game-start script */
IConsoleCmdExec ( " exec scripts/game_start.scr 0 " ) ;
}
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 ) ;
2016-12-08 23:48:53 +00:00
extern void PostCheckNewGRFLoadWarnings ( ) ;
PostCheckNewGRFLoadWarnings ( ) ;
(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 ( ) ) {
2021-05-05 17:21:12 +00:00
OnStartGame ( true ) ;
2013-02-05 21:41:32 +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
return ;
}
2004-08-09 17:04:08 +00:00
2008-09-30 20:39:50 +00:00
/* Create a single company */
2020-12-29 20:52:53 +00:00
DoStartupNewCompany ( DSNC_NONE ) ;
2004-08-09 17:04:08 +00:00
2009-05-16 23:34:14 +00:00
Company * c = Company : : Get ( COMPANY_FIRST ) ;
2009-05-15 23:55:06 +00:00
c - > settings = _settings_client . company ;
2009-05-12 22:32:22 +00:00
2019-01-10 09:45:38 +00:00
/* Overwrite color from settings if needed
* COLOUR_END corresponds to Random colour */
2023-06-24 20:50:04 +00:00
2019-01-10 09:45:38 +00:00
if ( _settings_client . gui . starting_colour ! = COLOUR_END ) {
c - > colour = _settings_client . gui . starting_colour ;
ResetCompanyLivery ( c ) ;
_company_colours [ c - > index ] = ( Colours ) c - > colour ;
2021-06-12 01:26:34 +00:00
BuildOwnerLegend ( ) ;
2019-01-10 09:45:38 +00:00
}
2023-06-24 20:50:04 +00:00
if ( _settings_client . gui . starting_colour_secondary ! = COLOUR_END & & HasBit ( _loaded_newgrf_features . used_liveries , LS_DEFAULT ) ) {
2023-11-12 14:14:56 +00:00
DoCommandP ( 0 , LS_DEFAULT | 1 < < 8 , _settings_client . gui . starting_colour_secondary , CMD_SET_COMPANY_COLOUR ) ;
2023-06-24 20:50:04 +00:00
}
2021-05-05 17:21:12 +00:00
OnStartGame ( false ) ;
2004-08-09 17:04:08 +00:00
2008-04-03 21:54:31 +00:00
InitializeRailGUI ( ) ;
2019-04-06 06:46:15 +00:00
InitializeRoadGUI ( ) ;
2006-11-17 19:31:44 +00:00
2008-09-30 20:39:50 +00:00
/* We are the server, we start a new company (not dedicated),
2007-12-02 14:48:26 +00:00
* so set the default password * if * needed . */
2021-04-27 18:26:56 +00:00
if ( _network_server & & ! _settings_client . network . default_company_pass . empty ( ) ) {
2011-01-19 16:47:40 +00:00
NetworkChangeCompanyPassword ( _local_company , _settings_client . network . default_company_pass ) ;
2007-12-02 14:48:26 +00:00
}
2010-06-05 15:22:46 +00:00
if ( _settings_client . gui . pause_on_newgame ) DoCommandP ( 0 , PM_PAUSED_NORMAL , 1 , CMD_PAUSE ) ;
2013-07-02 18:57:26 +00:00
CheckEngines ( ) ;
2014-08-13 19:31:45 +00:00
CheckIndustries ( ) ;
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
MarkWholeScreenDirty ( ) ;
2021-08-14 22:17:10 +00:00
if ( _network_server & & ! _network_dedicated ) ShowClientList ( ) ;
(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-08-09 17:04:08 +00:00
2015-09-11 18:53:02 +00:00
/*
* Too large size may be stored in settings ( especially if switching between between OpenTTD
* versions with different map size limits ) , we have to check if it is valid before generating world .
* Simple separate checking of X and Y map sizes is not enough , as their sum is what counts for the limit .
* Check the size and decrease the larger of the sizes till the size is in limit .
*/
static void FixConfigMapSize ( )
{
while ( _settings_game . game_creation . map_x + _settings_game . game_creation . map_y > MAX_MAP_TILES_BITS ) {
/* Repeat reducing larger of X/Y dimensions until the map size is within allowable limits */
if ( _settings_game . game_creation . map_x > _settings_game . game_creation . map_y ) {
_settings_game . game_creation . map_x - - ;
} else {
_settings_game . game_creation . map_y - - ;
}
}
}
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 ;
2021-01-08 15:21:54 +00:00
if ( ! from_heightmap ) {
/* "reload" command needs to know what mode we were in. */
_file_to_saveload . SetMode ( SLO_INVALID , FT_INVALID , DFT_INVALID ) ;
}
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 ) ;
2015-09-11 18:53:02 +00:00
FixConfigMapSize ( ) ;
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 ) ;
2016-12-08 23:48:53 +00:00
extern void PostCheckNewGRFLoadWarnings ( ) ;
PostCheckNewGRFLoadWarnings ( ) ;
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 ;
2021-01-08 15:21:54 +00:00
/* "reload" command needs to know what mode we were in. */
_file_to_saveload . SetMode ( SLO_INVALID , FT_INVALID , DFT_INVALID ) ;
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 ( & MakeNewEditorWorldDone ) ;
2015-09-11 18:53:02 +00:00
FixConfigMapSize ( ) ;
2010-01-17 22:59:24 +00:00
GenerateWorld ( GWM_EMPTY , 1 < < _settings_game . game_creation . map_x , 1 < < _settings_game . game_creation . map_y ) ;
2004-08-09 17:04:08 +00:00
}
2010-06-30 20:20:15 +00:00
/**
* Load the specified savegame but on error do different things .
* If loading fails due to corrupt savegame , bad version , etc . go back to
* a previous correct state . In the menu for example load the intro game again .
2010-12-05 14:43:10 +00:00
* @ param filename file to be loaded
2018-10-28 02:17:36 +00:00
* @ param fop mode of loading , always SLO_LOAD
* @ param newgm switch to this mode of loading fails due to some unknown error
2010-06-30 20:20:15 +00:00
* @ param subdir default directory to look for filename , set to 0 if not needed
2019-04-10 21:07:06 +00:00
* @ param lf Load filter to use , if nullptr : use filename + subdir .
2022-08-27 21:34:28 +00:00
* @ param error_detail Optional string to fill with detaied error information .
2010-06-30 20:20:15 +00:00
*/
2022-08-27 21:34:28 +00:00
bool SafeLoad ( const std : : string & filename , SaveLoadOperation fop , DetailedFileType dft , GameMode newgm , Subdirectory subdir ,
struct LoadFilter * lf = nullptr , std : : string * error_detail = nullptr )
2010-06-30 20:20:15 +00:00
{
2016-09-04 16:06:50 +00:00
assert ( fop = = SLO_LOAD ) ;
2019-04-10 21:07:06 +00:00
assert ( dft = = DFT_GAME_FILE | | ( lf = = nullptr & & dft = = DFT_OLD_GAME_FILE ) ) ;
2010-06-30 20:20:15 +00:00
GameMode ogm = _game_mode ;
_game_mode = newgm ;
2010-12-05 14:43:10 +00:00
2023-03-15 19:59:58 +00:00
SaveOrLoadResult result = ( lf = = nullptr ) ? SaveOrLoad ( filename , fop , dft , subdir ) : LoadWithFilter ( lf ) ;
if ( result = = SL_OK ) return true ;
2023-03-16 20:43:11 +00:00
if ( error_detail ! = nullptr ) * error_detail = GetSaveLoadErrorString ( ) ;
2023-03-15 19:59:58 +00:00
if ( _network_dedicated & & ogm = = GM_MENU ) {
/*
* If we are a dedicated server * and * we just were in the menu , then we
* are loading the first savegame . If that fails , not starting the
* server is a better reaction than starting the server with a newly
* generated map as it is quite likely to be started from a script .
*/
2023-03-16 20:43:11 +00:00
DEBUG ( net , 0 , " Loading requested map failed; closing server. " ) ;
2023-03-15 19:59:58 +00:00
_exit_game = true ;
return false ;
}
2010-06-30 21:38:51 +00:00
2023-03-15 19:59:58 +00:00
if ( result ! = SL_REINIT ) {
_game_mode = ogm ;
return false ;
}
if ( _network_dedicated ) {
/*
* If we are a dedicated server , have already loaded / started a game ,
* and then loading the savegame fails in a manner that we need to
* reinitialize everything . We must not fall back into the menu mode
* with the intro game , as that is unjoinable by clients . So there is
* nothing else to do than start a new game , as it might have failed
* trying to reload the originally loaded savegame / scenario .
*/
2023-03-16 20:43:11 +00:00
DEBUG ( net , 0 , " Loading game failed, so a new (random) game will be started " ) ;
2023-03-15 19:59:58 +00:00
MakeNewGame ( false , true ) ;
return false ;
}
if ( _network_server ) {
/* We can't load the intro game as server, so disconnect first. */
NetworkDisconnect ( ) ;
}
2010-06-30 20:20:15 +00:00
2023-03-15 19:59:58 +00:00
switch ( ogm ) {
2010-06-30 20:20:15 +00:00
default :
2023-03-15 19:59:58 +00:00
case GM_MENU : LoadIntroGame ( ) ; break ;
case GM_EDITOR : MakeNewEditorWorld ( ) ; break ;
2010-06-30 20:20:15 +00:00
}
2023-03-15 19:59:58 +00:00
return false ;
2010-06-30 20:20:15 +00:00
}
2009-02-25 00:45:52 +00:00
void SwitchToMode ( SwitchMode new_mode )
2004-08-09 17:04:08 +00:00
{
2021-05-09 17:02:17 +00:00
/* If we are saving something, the network stays in its current state */
2011-05-28 13:55:34 +00:00
if ( new_mode ! = SM_SAVE_GAME ) {
2007-03-21 15:19:33 +00:00
/* If the network is active, make it not-active */
2004-12-04 17:54:56 +00:00
if ( _networking ) {
2011-05-28 13:55:34 +00:00
if ( _network_server & & ( new_mode = = SM_LOAD_GAME | | new_mode = = SM_NEWGAME | | new_mode = = SM_RESTARTGAME ) ) {
2004-12-04 17:54:56 +00:00
NetworkReboot ( ) ;
} else {
NetworkDisconnect ( ) ;
}
}
2007-03-21 15:19:33 +00:00
/* If we are a server, we restart the server */
2004-12-04 17:54:56 +00:00
if ( _is_network_server ) {
2007-03-21 15:19:33 +00:00
/* But not if we are going to the menu */
2004-12-04 17:54:56 +00:00
if ( new_mode ! = SM_MENU ) {
2008-01-13 01:39:22 +00:00
/* check if we should reload the config */
2008-05-29 20:21:28 +00:00
if ( _settings_client . network . reload_cfg ) {
2008-01-13 01:39:22 +00:00
LoadFromConfig ( ) ;
2009-01-12 17:11:45 +00:00
MakeNewgameSettingsLive ( ) ;
2008-01-13 01:39:22 +00:00
ResetGRFConfig ( false ) ;
}
2004-12-04 17:54:56 +00:00
NetworkServerStart ( ) ;
} else {
2007-03-21 15:19:33 +00:00
/* This client no longer wants to be a network-server */
2004-12-04 17:54:56 +00:00
_is_network_server = false ;
}
}
}
Remove: ENABLE_NETWORK switch
This switch has been a pain for years. Often disabling broke
compilation, as no developer compiles OpenTTD without, neither do
any of our official binaries.
Additionaly, it has grown so hugely in our codebase, that it
clearly shows that the current solution was a poor one. 350+
instances of "#ifdef ENABLE_NETWORK" were in the code, of which
only ~30 in the networking code itself. The rest were all around
the code to do the right thing, from GUI to NewGRF.
A more proper solution would be to stub all the functions, and
make sure the rest of the code can simply assume network is
available. This was also partially done, and most variables were
correct if networking was disabled. Despite that, often the #ifdefs
were still used.
With the recent removal of DOS, there is also no platform anymore
which we support where networking isn't working out-of-the-box.
All in all, it is time to remove the ENABLE_NETWORK switch. No
replacement is planned, but if you feel we really need this option,
we welcome any Pull Request which implements this in a way that
doesn't crawl through the code like this diff shows we used to.
2019-03-20 16:01:13 +00:00
2013-01-08 22:46:42 +00:00
/* Make sure all AI controllers are gone at quitting game */
2011-05-28 13:55:34 +00:00
if ( new_mode ! = SM_SAVE_GAME ) AI : : KillAll ( ) ;
2004-12-04 17:54:56 +00:00
2023-04-27 15:21:29 +00:00
/* When we change mode, reset the autosave. */
if ( new_mode ! = SM_SAVE_GAME ) ChangeAutosaveFrequency ( true ) ;
2023-04-25 17:43:45 +00:00
/* Transmit the survey if we were in normal-mode and not saving. It always means we leaving the current game. */
if ( _game_mode = = GM_NORMAL & & new_mode ! = SM_SAVE_GAME ) _survey . Transmit ( NetworkSurveyHandler : : Reason : : LEAVE ) ;
/* Keep track when we last switch mode. Used for survey, to know how long someone was in a game. */
if ( new_mode ! = SM_SAVE_GAME ) _switch_mode_time = std : : chrono : : steady_clock : : now ( ) ;
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 ( ) ;
2023-04-25 17:43:45 +00:00
GenerateSavegameId ( ) ;
2008-04-29 18:19:29 +00:00
break ;
2004-08-09 17:04:08 +00:00
2021-01-08 15:23:07 +00:00
case SM_RELOADGAME : // Reload with what-ever started the game
2019-03-05 04:04:39 +00:00
if ( _file_to_saveload . abstract_ftype = = FT_SAVEGAME | | _file_to_saveload . abstract_ftype = = FT_SCENARIO ) {
2021-01-08 15:23:07 +00:00
/* Reload current savegame/scenario */
2019-03-05 04:04:39 +00:00
_switch_mode = _game_mode = = GM_EDITOR ? SM_LOAD_SCENARIO : SM_LOAD_GAME ;
SwitchToMode ( _switch_mode ) ;
break ;
} else if ( _file_to_saveload . abstract_ftype = = FT_HEIGHTMAP ) {
/* Restart current heightmap */
_switch_mode = _game_mode = = GM_EDITOR ? SM_LOAD_HEIGHTMAP : SM_RESTART_HEIGHTMAP ;
SwitchToMode ( _switch_mode ) ;
break ;
}
2021-01-08 15:21:54 +00:00
MakeNewGame ( false , new_mode = = SM_NEWGAME ) ;
2023-04-25 17:43:45 +00:00
GenerateSavegameId ( ) ;
2021-01-08 15:21:54 +00:00
break ;
2019-03-05 04:04:39 +00:00
2021-01-08 15:23:07 +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'
2009-08-06 22:00:32 +00:00
MakeNewGame ( false , new_mode = = SM_NEWGAME ) ;
2023-04-25 17:43:45 +00:00
GenerateSavegameId ( ) ;
2008-04-29 18:19:29 +00:00
break ;
2004-08-09 17:04:08 +00:00
2011-05-28 13:55:34 +00:00
case SM_LOAD_GAME : { // Load game, Play Scenario
2008-04-29 18:19:29 +00:00
ResetGRFConfig ( true ) ;
ResetWindowSystem ( ) ;
2004-08-09 17:04:08 +00:00
2016-09-04 12:57:43 +00:00
if ( ! SafeLoad ( _file_to_saveload . name , _file_to_saveload . file_op , _file_to_saveload . detail_ftype , GM_NORMAL , NO_DIRECTORY ) ) {
2008-12-26 18:24:05 +00:00
SetDParamStr ( 0 , GetSaveLoadErrorString ( ) ) ;
2023-04-20 16:04:07 +00:00
ShowErrorMessage ( STR_JUST_RAW_STRING , INVALID_STRING_ID , WL_CRITICAL ) ;
2008-04-29 18:19:29 +00:00
} else {
2016-09-04 12:57:43 +00:00
if ( _file_to_saveload . abstract_ftype = = FT_SCENARIO ) {
2022-06-09 19:32:31 +00:00
OnStartScenario ( ) ;
2008-05-04 22:38:18 +00:00
}
2021-05-05 17:21:12 +00:00
OnStartGame ( _network_dedicated ) ;
2008-04-29 18:19:29 +00:00
/* Decrease pause counter (was increased from opening load dialog) */
2009-05-06 15:06:57 +00:00
DoCommandP ( 0 , PM_PAUSED_SAVELOAD , 0 , CMD_PAUSE ) ;
2008-04-29 18:19:29 +00:00
}
break ;
2004-08-09 17:04:08 +00:00
}
2019-03-05 04:04:39 +00:00
case SM_RESTART_HEIGHTMAP : // Load a heightmap and start a new game from it with current settings
2009-03-15 00:32:18 +00:00
case SM_START_HEIGHTMAP : // Load a heightmap and start a new game from it
2019-03-05 04:04:39 +00:00
MakeNewGame ( true , new_mode = = SM_START_HEIGHTMAP ) ;
2023-04-25 17:43:45 +00:00
GenerateSavegameId ( ) ;
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
2015-09-11 18:53:02 +00:00
FixConfigMapSize ( ) ;
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 ) ;
2023-04-25 17:43:45 +00:00
GenerateSavegameId ( ) ;
2008-04-29 18:19:29 +00:00
MarkWholeScreenDirty ( ) ;
break ;
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
2009-03-15 00:32:18 +00:00
case SM_LOAD_SCENARIO : { // Load scenario from scenario editor
2016-09-04 12:57:43 +00:00
if ( SafeLoad ( _file_to_saveload . name , _file_to_saveload . file_op , _file_to_saveload . detail_ftype , GM_EDITOR , NO_DIRECTORY ) ) {
2008-09-30 20:39:50 +00:00
SetLocalCompany ( OWNER_NONE ) ;
2023-04-25 17:43:45 +00:00
GenerateSavegameId ( ) ;
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 ( ) ) ;
2023-04-20 16:04:07 +00:00
ShowErrorMessage ( STR_JUST_RAW_STRING , INVALID_STRING_ID , WL_CRITICAL ) ;
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
2021-05-01 12:41:25 +00:00
case SM_JOIN_GAME : // Join a multiplayer game
LoadIntroGame ( ) ;
NetworkClientJoinGame ( ) ;
break ;
2009-03-15 00:32:18 +00:00
case SM_MENU : // Switch to game intro menu
2008-04-29 18:19:29 +00:00
LoadIntroGame ( ) ;
2020-12-08 09:24:59 +00:00
if ( BaseSounds : : ini_set . empty ( ) & & BaseSounds : : GetUsedSet ( ) - > fallback & & SoundDriver : : GetInstance ( ) - > HasOutput ( ) ) {
2010-02-24 14:46:15 +00:00
ShowErrorMessage ( STR_WARNING_FALLBACK_SOUNDSET , INVALID_STRING_ID , WL_CRITICAL ) ;
2020-05-17 21:32:06 +00:00
BaseSounds : : ini_set = BaseSounds : : GetUsedSet ( ) - > name ;
2010-02-22 20:19:17 +00:00
}
2023-04-25 17:43:45 +00:00
if ( _settings_client . network . participate_survey = = PS_ASK ) {
/* No matter how often you go back to the main menu, only ask the first time. */
static bool asked_once = false ;
if ( ! asked_once ) {
asked_once = true ;
ShowNetworkAskSurvey ( ) ;
}
}
2008-04-29 18:19:29 +00:00
break ;
2023-03-21 18:00:30 +00:00
case SM_SAVE_GAME : { // Save game.
2021-01-22 15:24:29 +00:00
/* Make network saved games on pause compatible to singleplayer mode */
2023-03-21 18:00:30 +00:00
SaveModeFlags flags = SMF_NONE ;
if ( _game_mode = = GM_EDITOR ) flags | = SMF_SCENARIO ;
if ( SaveOrLoad ( _file_to_saveload . name , SLO_SAVE , DFT_GAME_FILE , NO_DIRECTORY , true , flags ) ! = 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 {
2023-09-15 19:59:08 +00:00
CloseWindowById ( WC_SAVELOAD , 0 ) ;
2008-04-29 18:19:29 +00:00
}
break ;
2023-03-21 18:00:30 +00:00
}
2008-04-29 18:19:29 +00:00
2011-05-28 13:55:34 +00:00
case SM_SAVE_HEIGHTMAP : // Save heightmap.
2020-12-06 20:11:45 +00:00
MakeHeightmapScreenshot ( _file_to_saveload . name . c_str ( ) ) ;
2023-09-15 19:59:08 +00:00
CloseWindowById ( WC_SAVELOAD , 0 ) ;
2011-05-28 13:55:34 +00:00
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 ) ;
2015-09-11 18:53:02 +00:00
FixConfigMapSize ( ) ;
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
}
2015-08-02 18:37:42 +00:00
SmallMapWindow : : RebuildColourIndexIfNecessary ( ) ;
2004-08-09 17:04:08 +00:00
}
2020-11-22 11:41:50 +00:00
void WriteVehicleInfo ( char * & p , const char * last , const Vehicle * u , const Vehicle * v , uint length )
{
p + = seprintf ( p , last , " : type %i, vehicle %i (%i), company %i, unit number %i, wagon %i, engine: " ,
( int ) u - > type , u - > index , v - > index , ( int ) u - > owner , v - > unitnumber , length ) ;
SetDParam ( 0 , u - > engine_type ) ;
p = GetString ( p , STR_ENGINE_NAME , last ) ;
uint32 grfid = u - > GetGRFID ( ) ;
if ( grfid ) {
p + = seprintf ( p , last , " , GRF: %08X " , BSWAP32 ( grfid ) ) ;
GRFConfig * grfconfig = GetGRFConfig ( grfid ) ;
if ( grfconfig ) {
2023-05-31 22:11:44 +00:00
p + = seprintf ( p , last , " , %s, %s " , grfconfig - > GetName ( ) , grfconfig - > filename . c_str ( ) ) ;
2020-11-22 11:41:50 +00:00
}
}
}
2021-12-06 01:58:03 +00:00
static bool SignalInfraTotalMatches ( )
{
std : : array < int , MAX_COMPANIES > old_signal_totals = { } ;
for ( const Company * c : Company : : Iterate ( ) ) {
old_signal_totals [ c - > index ] = c - > infrastructure . signal ;
}
std : : array < int , MAX_COMPANIES > new_signal_totals = { } ;
for ( TileIndex tile = 0 ; tile < MapSize ( ) ; tile + + ) {
switch ( GetTileType ( tile ) ) {
case MP_RAILWAY :
if ( HasSignals ( tile ) ) {
const Company * c = Company : : GetIfValid ( GetTileOwner ( tile ) ) ;
if ( c ! = nullptr ) new_signal_totals [ c - > index ] + = CountBits ( GetPresentSignals ( tile ) ) ;
}
break ;
case MP_TUNNELBRIDGE : {
/* Only count the tunnel/bridge if we're on the northern end tile. */
DiagDirection dir = GetTunnelBridgeDirection ( tile ) ;
if ( dir = = DIAGDIR_NE | | dir = = DIAGDIR_NW ) break ;
if ( IsTunnelBridgeWithSignalSimulation ( tile ) ) {
const Company * c = Company : : GetIfValid ( GetTileOwner ( tile ) ) ;
if ( c ! = nullptr ) new_signal_totals [ c - > index ] + = GetTunnelBridgeSignalSimulationSignalCount ( tile , GetOtherTunnelBridgeEnd ( tile ) ) ;
}
break ;
}
default :
break ;
}
}
return old_signal_totals = = new_signal_totals ;
}
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 .
*/
2021-10-03 22:03:26 +00:00
void CheckCaches ( bool force_check , std : : function < void ( const char * ) > log , CheckCachesFlags flags )
2010-03-13 14:58:37 +00:00
{
2015-11-23 19:47:59 +00:00
if ( ! force_check ) {
2021-10-03 00:58:54 +00:00
int desync_level = _debug_desync_level ;
2021-10-03 22:58:35 +00:00
if ( unlikely ( HasChickenBit ( DCBF_DESYNC_CHECK_PERIODIC ) ) & & desync_level < 1 ) {
desync_level = 1 ;
if ( HasChickenBit ( DCBF_DESYNC_CHECK_NO_GENERAL ) ) flags & = ~ CHECK_CACHE_GENERAL ;
}
2021-12-06 01:58:03 +00:00
if ( unlikely ( HasChickenBit ( DCBF_DESYNC_CHECK_PERIODIC_SIGNALS ) ) & & desync_level < 2 & & _scaled_date_ticks % 256 = = 0 ) {
if ( ! SignalInfraTotalMatches ( ) ) desync_level = 2 ;
}
2021-10-03 00:58:54 +00:00
2015-11-23 19:47:59 +00:00
/* Return here so it is easy to add checks that are run
* always to aid testing of caches . */
2021-10-03 00:58:54 +00:00
if ( desync_level < 1 ) return ;
2015-11-23 19:17:49 +00:00
2021-10-03 00:58:54 +00:00
if ( desync_level = = 1 & & _scaled_date_ticks % 500 ! = 0 ) return ;
2015-11-23 19:47:59 +00:00
}
2011-12-17 21:20:41 +00:00
2023-04-23 10:41:38 +00:00
SCOPE_INFO_FMT ( [ flags ] , " CheckCaches: %X " , flags ) ;
2021-10-03 22:57:27 +00:00
std : : vector < std : : string > saved_messages ;
2023-03-24 22:27:26 +00:00
std : : function < void ( const char * ) > log_orig ;
2021-10-03 22:57:27 +00:00
if ( flags & CHECK_CACHE_EMIT_LOG ) {
2023-03-24 22:27:26 +00:00
log_orig = std : : move ( log ) ;
log = [ & saved_messages , & log_orig ] ( const char * str ) {
if ( log_orig ) log_orig ( str ) ;
2021-10-03 22:57:27 +00:00
saved_messages . emplace_back ( str ) ;
} ;
2015-11-23 19:47:59 +00:00
}
2011-12-17 21:20:41 +00:00
2019-05-18 17:31:08 +00:00
char cclog_buffer [ 1024 ] ;
2023-04-22 21:43:10 +00:00
auto cclog_common = [ & ] ( ) {
DEBUG ( desync , 0 , " %s " , cclog_buffer ) ;
if ( log ) {
log ( cclog_buffer ) ;
} else {
LogDesyncMsg ( cclog_buffer ) ;
}
} ;
2019-05-18 17:31:08 +00:00
# define CCLOG(...) { \
seprintf ( cclog_buffer , lastof ( cclog_buffer ) , __VA_ARGS__ ) ; \
2023-04-22 21:43:10 +00:00
cclog_common ( ) ; \
2019-05-18 17:31:08 +00:00
}
2019-09-17 23:46:37 +00:00
auto output_veh_info = [ & ] ( char * & p , const Vehicle * u , const Vehicle * v , uint length ) {
2020-11-22 11:41:50 +00:00
WriteVehicleInfo ( p , lastof ( cclog_buffer ) , u , v , length ) ;
2019-09-17 23:46:37 +00:00
} ;
2023-04-22 21:50:18 +00:00
auto output_veh_info_single = [ & ] ( char * & p , const Vehicle * v ) {
uint length = 0 ;
for ( const Vehicle * u = v - > First ( ) ; u ! = v ; u = u - > Next ( ) ) {
length + + ;
}
WriteVehicleInfo ( p , lastof ( cclog_buffer ) , v , v - > First ( ) , length ) ;
} ;
2019-09-17 23:46:37 +00:00
# define CCLOGV(...) { \
char * p = cclog_buffer + seprintf ( cclog_buffer , lastof ( cclog_buffer ) , __VA_ARGS__ ) ; \
output_veh_info ( p , u , v , length ) ; \
2023-04-22 21:43:10 +00:00
cclog_common ( ) ; \
2019-09-17 23:46:37 +00:00
}
2023-04-22 21:50:18 +00:00
# define CCLOGV1(...) { \
char * p = cclog_buffer + seprintf ( cclog_buffer , lastof ( cclog_buffer ) , __VA_ARGS__ ) ; \
output_veh_info_single ( p , v ) ; \
cclog_common ( ) ; \
}
2021-10-03 22:03:26 +00:00
if ( flags & CHECK_CACHE_GENERAL ) {
/* Check the town caches. */
std : : vector < TownCache > old_town_caches ;
std : : vector < StationList > old_town_stations_nears ;
for ( const Town * t : Town : : Iterate ( ) ) {
old_town_caches . push_back ( t - > cache ) ;
old_town_stations_nears . push_back ( t - > stations_near ) ;
}
2019-05-22 17:33:30 +00:00
2021-10-03 22:03:26 +00:00
std : : vector < IndustryList > old_station_industries_nears ;
std : : vector < BitmapTileArea > old_station_catchment_tiles ;
std : : vector < uint > old_station_tiles ;
for ( Station * st : Station : : Iterate ( ) ) {
old_station_industries_nears . push_back ( st - > industries_near ) ;
old_station_catchment_tiles . push_back ( st - > catchment_tiles ) ;
old_station_tiles . push_back ( st - > station_tiles ) ;
}
2019-05-22 17:33:30 +00:00
2021-10-03 22:03:26 +00:00
std : : vector < StationList > old_industry_stations_nears ;
for ( Industry * ind : Industry : : Iterate ( ) ) {
old_industry_stations_nears . push_back ( ind - > stations_near ) ;
}
2012-04-25 21:06:31 +00:00
2021-10-03 22:03:26 +00:00
RebuildTownCaches ( false , false ) ;
RebuildSubsidisedSourceAndDestinationCache ( ) ;
2012-04-25 21:06:31 +00:00
2021-10-03 22:03:26 +00:00
Station : : RecomputeCatchmentForAll ( ) ;
2019-05-22 17:33:30 +00:00
2021-10-03 22:03:26 +00:00
uint i = 0 ;
for ( Town * t : Town : : Iterate ( ) ) {
2022-10-15 16:02:30 +00:00
if ( old_town_caches [ i ] . num_houses ! = t - > cache . num_houses ) {
CCLOG ( " town cache num_houses mismatch: town %i, (old size: %u, new size: %u) " , ( int ) t - > index , old_town_caches [ i ] . num_houses , t - > cache . num_houses ) ;
}
if ( old_town_caches [ i ] . population ! = t - > cache . population ) {
CCLOG ( " town cache population mismatch: town %i, (old size: %u, new size: %u) " , ( int ) t - > index , old_town_caches [ i ] . population , t - > cache . population ) ;
}
if ( old_town_caches [ i ] . part_of_subsidy ! = t - > cache . part_of_subsidy ) {
CCLOG ( " town cache population mismatch: town %i, (old size: %u, new size: %u) " , ( int ) t - > index , old_town_caches [ i ] . part_of_subsidy , t - > cache . part_of_subsidy ) ;
}
if ( MemCmpT ( old_town_caches [ i ] . squared_town_zone_radius , t - > cache . squared_town_zone_radius , lengthof ( t - > cache . squared_town_zone_radius ) ) ! = 0 ) {
CCLOG ( " town cache squared_town_zone_radius mismatch: town %i " , ( int ) t - > index ) ;
}
if ( MemCmpT ( & old_town_caches [ i ] . building_counts , & t - > cache . building_counts ) ! = 0 ) {
CCLOG ( " town cache building_counts mismatch: town %i " , ( int ) t - > index ) ;
2021-10-03 22:03:26 +00:00
}
if ( old_town_stations_nears [ i ] ! = t - > stations_near ) {
CCLOG ( " town stations_near mismatch: town %i, (old size: %u, new size: %u) " , ( int ) t - > index , ( uint ) old_town_stations_nears [ i ] . size ( ) , ( uint ) t - > stations_near . size ( ) ) ;
}
i + + ;
2020-06-07 13:37:59 +00:00
}
2021-10-03 22:03:26 +00:00
i = 0 ;
for ( Station * st : Station : : Iterate ( ) ) {
if ( old_station_industries_nears [ i ] ! = st - > industries_near ) {
CCLOG ( " station industries_near mismatch: st %i, (old size: %u, new size: %u) " , ( int ) st - > index , ( uint ) old_station_industries_nears [ i ] . size ( ) , ( uint ) st - > industries_near . size ( ) ) ;
}
if ( ! ( old_station_catchment_tiles [ i ] = = st - > catchment_tiles ) ) {
CCLOG ( " station catchment_tiles mismatch: st %i " , ( int ) st - > index ) ;
}
if ( ! ( old_station_tiles [ i ] = = st - > station_tiles ) ) {
CCLOG ( " station station_tiles mismatch: st %i, (old: %u, new: %u) " , ( int ) st - > index , old_station_tiles [ i ] , st - > station_tiles ) ;
}
i + + ;
2019-05-22 17:33:30 +00:00
}
2021-10-03 22:03:26 +00:00
i = 0 ;
for ( Industry * ind : Industry : : Iterate ( ) ) {
if ( old_industry_stations_nears [ i ] ! = ind - > stations_near ) {
CCLOG ( " industry stations_near mismatch: ind %i, (old size: %u, new size: %u) " , ( int ) ind - > index , ( uint ) old_industry_stations_nears [ i ] . size ( ) , ( uint ) ind - > stations_near . size ( ) ) ;
2019-06-17 01:37:24 +00:00
}
2021-10-03 22:03:26 +00:00
StationList stlist ;
if ( ind - > neutral_station ! = nullptr & & ! _settings_game . station . serve_neutral_industries ) {
stlist . insert ( ind - > neutral_station ) ;
if ( ind - > stations_near ! = stlist ) {
CCLOG ( " industry neutral station stations_near mismatch: ind %i, (recalc size: %u, neutral size: %u) " , ( int ) ind - > index , ( uint ) ind - > stations_near . size ( ) , ( uint ) stlist . size ( ) ) ;
}
} else {
ForAllStationsAroundTiles ( ind - > location , [ ind , & stlist ] ( Station * st , TileIndex tile ) {
if ( ! IsTileType ( tile , MP_INDUSTRY ) | | GetIndustryIndex ( tile ) ! = ind - > index ) return false ;
stlist . insert ( st ) ;
return true ;
} ) ;
if ( ind - > stations_near ! = stlist ) {
CCLOG ( " industry FindStationsAroundTiles mismatch: ind %i, (recalc size: %u, find size: %u) " , ( int ) ind - > index , ( uint ) ind - > stations_near . size ( ) , ( uint ) stlist . size ( ) ) ;
}
2019-06-17 01:37:24 +00:00
}
2021-10-03 22:03:26 +00:00
i + + ;
2019-06-17 00:34:34 +00:00
}
2012-04-25 21:06:31 +00:00
}
2021-10-03 22:03:26 +00:00
if ( flags & CHECK_CACHE_INFRA_TOTALS ) {
/* Check company infrastructure cache. */
std : : vector < CompanyInfrastructure > old_infrastructure ;
for ( const Company * c : Company : : Iterate ( ) ) old_infrastructure . push_back ( c - > infrastructure ) ;
AfterLoadCompanyStats ( ) ;
uint i = 0 ;
for ( const Company * c : Company : : Iterate ( ) ) {
if ( MemCmpT ( old_infrastructure . data ( ) + i , & c - > infrastructure ) ! = 0 ) {
CCLOG ( " infrastructure cache mismatch: company %i " , ( int ) c - > index ) ;
char buffer [ 4096 ] ;
old_infrastructure [ i ] . Dump ( buffer , lastof ( buffer ) ) ;
CCLOG ( " Previous: " ) ;
ProcessLineByLine ( buffer , [ & ] ( const char * line ) {
CCLOG ( " %s " , line ) ;
} ) ;
c - > infrastructure . Dump ( buffer , lastof ( buffer ) ) ;
CCLOG ( " Recalculated: " ) ;
ProcessLineByLine ( buffer , [ & ] ( const char * line ) {
CCLOG ( " %s " , line ) ;
} ) ;
2021-12-06 01:58:03 +00:00
if ( old_infrastructure [ i ] . signal ! = c - > infrastructure . signal & & _network_server & & ! HasChickenBit ( DCBF_DESYNC_CHECK_PERIODIC_SIGNALS ) ) {
DoCommandP ( 0 , 0 , _settings_game . debug . chicken_bits | ( 1 < < DCBF_DESYNC_CHECK_PERIODIC_SIGNALS ) , CMD_CHANGE_SETTING , nullptr , " debug.chicken_bits " ) ;
}
2021-10-03 22:03:26 +00:00
}
i + + ;
2011-12-03 23:40:13 +00:00
}
}
2021-10-03 22:03:26 +00:00
if ( flags & CHECK_CACHE_GENERAL ) {
/* Strict checking of the road stop cache entries */
for ( const RoadStop * rs : RoadStop : : Iterate ( ) ) {
if ( IsStandardRoadStopTile ( rs - > xy ) ) continue ;
2010-03-13 14:58:37 +00:00
2021-10-03 22:03:26 +00:00
assert ( rs - > GetEntry ( DIAGDIR_NE ) ! = rs - > GetEntry ( DIAGDIR_NW ) ) ;
rs - > GetEntry ( DIAGDIR_NE ) - > CheckIntegrity ( rs ) ;
rs - > GetEntry ( DIAGDIR_NW ) - > CheckIntegrity ( rs ) ;
2019-05-27 14:58:30 +00:00
}
2021-10-03 22:03:26 +00:00
for ( Vehicle * v : Vehicle : : Iterate ( ) ) {
extern bool ValidateVehicleTileHash ( const Vehicle * v ) ;
if ( ! ValidateVehicleTileHash ( v ) ) {
CCLOG ( " vehicle tile hash mismatch: type %i, vehicle %i, company %i, unit number %i " , ( int ) v - > type , v - > index , ( int ) v - > owner , v - > unitnumber ) ;
}
extern void FillNewGRFVehicleCache ( const Vehicle * v ) ;
if ( v ! = v - > First ( ) | | v - > vehstatus & VS_CRASHED | | ! v - > IsPrimaryVehicle ( ) ) continue ;
2010-07-13 20:12:44 +00:00
2021-10-03 22:03:26 +00:00
uint length = 0 ;
for ( const Vehicle * u = v ; u ! = nullptr ; u = u - > Next ( ) ) {
if ( u - > IsGroundVehicle ( ) & & ( HasBit ( u - > GetGroundVehicleFlags ( ) , GVF_GOINGUP_BIT ) | | HasBit ( u - > GetGroundVehicleFlags ( ) , GVF_GOINGDOWN_BIT ) ) & & u - > GetGroundVehicleCache ( ) - > cached_slope_resistance & & HasBit ( v - > vcache . cached_veh_flags , VCF_GV_ZERO_SLOPE_RESIST ) ) {
CCLOGV ( " VCF_GV_ZERO_SLOPE_RESIST set incorrectly (1) " ) ;
}
2022-09-16 19:30:14 +00:00
if ( u - > type = = VEH_TRAIN & & u - > breakdown_ctr ! = 0 & & ! HasBit ( Train : : From ( v ) - > flags , VRF_CONSIST_BREAKDOWN ) & & ( Train : : From ( u ) - > IsEngine ( ) | | Train : : From ( u ) - > IsMultiheaded ( ) ) ) {
2021-10-03 22:03:26 +00:00
CCLOGV ( " VRF_CONSIST_BREAKDOWN incorrectly not set " ) ;
}
if ( u - > type = = VEH_TRAIN & & ( ( Train : : From ( u ) - > track & TRACK_BIT_WORMHOLE & & ! ( Train : : From ( u ) - > vehstatus & VS_HIDDEN ) ) | | Train : : From ( u ) - > track = = TRACK_BIT_DEPOT ) & & ! HasBit ( Train : : From ( v ) - > flags , VRF_CONSIST_SPEED_REDUCTION ) ) {
CCLOGV ( " VRF_CONSIST_SPEED_REDUCTION incorrectly not set " ) ;
}
length + + ;
2019-05-27 20:07:40 +00:00
}
2021-10-03 22:03:26 +00:00
NewGRFCache * grf_cache = CallocT < NewGRFCache > ( length ) ;
VehicleCache * veh_cache = CallocT < VehicleCache > ( length ) ;
GroundVehicleCache * gro_cache = CallocT < GroundVehicleCache > ( length ) ;
AircraftCache * air_cache = CallocT < AircraftCache > ( length ) ;
TrainCache * tra_cache = CallocT < TrainCache > ( length ) ;
Vehicle * * veh_old = CallocT < Vehicle * > ( length ) ;
length = 0 ;
for ( const Vehicle * u = v ; u ! = nullptr ; u = u - > Next ( ) ) {
FillNewGRFVehicleCache ( u ) ;
grf_cache [ length ] = u - > grf_cache ;
veh_cache [ length ] = u - > vcache ;
switch ( u - > type ) {
case VEH_TRAIN :
gro_cache [ length ] = Train : : From ( u ) - > gcache ;
tra_cache [ length ] = Train : : From ( u ) - > tcache ;
veh_old [ length ] = CallocT < Train > ( 1 ) ;
memcpy ( ( void * ) veh_old [ length ] , ( const void * ) Train : : From ( u ) , sizeof ( Train ) ) ;
break ;
case VEH_ROAD :
gro_cache [ length ] = RoadVehicle : : From ( u ) - > gcache ;
veh_old [ length ] = CallocT < RoadVehicle > ( 1 ) ;
memcpy ( ( void * ) veh_old [ length ] , ( const void * ) RoadVehicle : : From ( u ) , sizeof ( RoadVehicle ) ) ;
break ;
case VEH_AIRCRAFT :
air_cache [ length ] = Aircraft : : From ( u ) - > acache ;
veh_old [ length ] = CallocT < Aircraft > ( 1 ) ;
memcpy ( ( void * ) veh_old [ length ] , ( const void * ) Aircraft : : From ( u ) , sizeof ( Aircraft ) ) ;
break ;
default :
veh_old [ length ] = CallocT < Vehicle > ( 1 ) ;
memcpy ( ( void * ) veh_old [ length ] , ( const void * ) u , sizeof ( Vehicle ) ) ;
break ;
}
length + + ;
2019-05-27 20:07:40 +00:00
}
2021-10-03 22:03:26 +00:00
switch ( v - > type ) {
case VEH_TRAIN : Train : : From ( v ) - > ConsistChanged ( CCF_TRACK ) ; break ;
case VEH_ROAD : RoadVehUpdateCache ( RoadVehicle : : From ( v ) ) ; break ;
case VEH_AIRCRAFT : UpdateAircraftCache ( Aircraft : : From ( v ) ) ; break ;
case VEH_SHIP : Ship : : From ( v ) - > UpdateCache ( ) ; break ;
default : break ;
2019-05-27 20:07:40 +00:00
}
2010-07-13 20:12:44 +00:00
2021-10-03 22:03:26 +00:00
length = 0 ;
for ( const Vehicle * u = v ; u ! = nullptr ; u = u - > Next ( ) ) {
FillNewGRFVehicleCache ( u ) ;
if ( memcmp ( & grf_cache [ length ] , & u - > grf_cache , sizeof ( NewGRFCache ) ) ! = 0 ) {
CCLOGV ( " newgrf cache mismatch " ) ;
}
if ( veh_cache [ length ] . cached_max_speed ! = u - > vcache . cached_max_speed | | veh_cache [ length ] . cached_cargo_age_period ! = u - > vcache . cached_cargo_age_period | |
veh_cache [ length ] . cached_vis_effect ! = u - > vcache . cached_vis_effect | | HasBit ( veh_cache [ length ] . cached_veh_flags ^ u - > vcache . cached_veh_flags , VCF_LAST_VISUAL_EFFECT ) ) {
CCLOGV ( " vehicle cache mismatch: %c%c%c%c " ,
veh_cache [ length ] . cached_max_speed ! = u - > vcache . cached_max_speed ? ' m ' : ' - ' ,
veh_cache [ length ] . cached_cargo_age_period ! = u - > vcache . cached_cargo_age_period ? ' c ' : ' - ' ,
veh_cache [ length ] . cached_vis_effect ! = u - > vcache . cached_vis_effect ? ' v ' : ' - ' ,
HasBit ( veh_cache [ length ] . cached_veh_flags ^ u - > vcache . cached_veh_flags , VCF_LAST_VISUAL_EFFECT ) ? ' l ' : ' - ' ) ;
}
if ( u - > IsGroundVehicle ( ) & & ( HasBit ( u - > GetGroundVehicleFlags ( ) , GVF_GOINGUP_BIT ) | | HasBit ( u - > GetGroundVehicleFlags ( ) , GVF_GOINGDOWN_BIT ) ) & & u - > GetGroundVehicleCache ( ) - > cached_slope_resistance & & HasBit ( v - > vcache . cached_veh_flags , VCF_GV_ZERO_SLOPE_RESIST ) ) {
CCLOGV ( " VCF_GV_ZERO_SLOPE_RESIST set incorrectly (2) " ) ;
}
if ( veh_old [ length ] - > acceleration ! = u - > acceleration ) {
CCLOGV ( " acceleration mismatch " ) ;
}
if ( veh_old [ length ] - > breakdown_chance ! = u - > breakdown_chance ) {
CCLOGV ( " breakdown_chance mismatch " ) ;
}
if ( veh_old [ length ] - > breakdown_ctr ! = u - > breakdown_ctr ) {
CCLOGV ( " breakdown_ctr mismatch " ) ;
}
if ( veh_old [ length ] - > breakdown_delay ! = u - > breakdown_delay ) {
CCLOGV ( " breakdown_delay mismatch " ) ;
}
if ( veh_old [ length ] - > breakdowns_since_last_service ! = u - > breakdowns_since_last_service ) {
CCLOGV ( " breakdowns_since_last_service mismatch " ) ;
}
if ( veh_old [ length ] - > breakdown_severity ! = u - > breakdown_severity ) {
CCLOGV ( " breakdown_severity mismatch " ) ;
}
if ( veh_old [ length ] - > breakdown_type ! = u - > breakdown_type ) {
CCLOGV ( " breakdown_type mismatch " ) ;
}
if ( veh_old [ length ] - > vehicle_flags ! = u - > vehicle_flags ) {
CCLOGV ( " vehicle_flags mismatch " ) ;
}
auto print_gv_cache_diff = [ & ] ( const char * vtype , const GroundVehicleCache & a , const GroundVehicleCache & b ) {
CCLOGV ( " %s ground vehicle cache mismatch: %c%c%c%c%c%c%c%c%c%c " ,
vtype ,
a . cached_weight ! = b . cached_weight ? ' w ' : ' - ' ,
a . cached_slope_resistance ! = b . cached_slope_resistance ? ' r ' : ' - ' ,
a . cached_max_te ! = b . cached_max_te ? ' t ' : ' - ' ,
a . cached_axle_resistance ! = b . cached_axle_resistance ? ' a ' : ' - ' ,
a . cached_max_track_speed ! = b . cached_max_track_speed ? ' s ' : ' - ' ,
a . cached_power ! = b . cached_power ? ' p ' : ' - ' ,
a . cached_air_drag ! = b . cached_air_drag ? ' d ' : ' - ' ,
a . cached_total_length ! = b . cached_total_length ? ' l ' : ' - ' ,
a . first_engine ! = b . first_engine ? ' e ' : ' - ' ,
a . cached_veh_length ! = b . cached_veh_length ? ' L ' : ' - ' ) ;
} ;
switch ( u - > type ) {
case VEH_TRAIN :
if ( memcmp ( & gro_cache [ length ] , & Train : : From ( u ) - > gcache , sizeof ( GroundVehicleCache ) ) ! = 0 ) {
print_gv_cache_diff ( " train " , gro_cache [ length ] , Train : : From ( u ) - > gcache ) ;
}
if ( memcmp ( & tra_cache [ length ] , & Train : : From ( u ) - > tcache , sizeof ( TrainCache ) ) ! = 0 ) {
2022-07-01 22:39:02 +00:00
CCLOGV ( " train cache mismatch: %c%c%c%c%c%c%c%c%c%c%c " ,
2021-10-03 22:03:26 +00:00
tra_cache [ length ] . cached_override ! = Train : : From ( u ) - > tcache . cached_override ? ' o ' : ' - ' ,
2021-11-02 22:37:34 +00:00
tra_cache [ length ] . cached_curve_speed_mod ! = Train : : From ( u ) - > tcache . cached_curve_speed_mod ? ' C ' : ' - ' ,
2021-10-03 22:03:26 +00:00
tra_cache [ length ] . cached_tflags ! = Train : : From ( u ) - > tcache . cached_tflags ? ' f ' : ' - ' ,
tra_cache [ length ] . cached_num_engines ! = Train : : From ( u ) - > tcache . cached_num_engines ? ' e ' : ' - ' ,
tra_cache [ length ] . cached_centre_mass ! = Train : : From ( u ) - > tcache . cached_centre_mass ? ' m ' : ' - ' ,
2022-07-01 22:39:02 +00:00
tra_cache [ length ] . cached_braking_length ! = Train : : From ( u ) - > tcache . cached_braking_length ? ' b ' : ' - ' ,
2021-10-03 22:03:26 +00:00
tra_cache [ length ] . cached_veh_weight ! = Train : : From ( u ) - > tcache . cached_veh_weight ? ' w ' : ' - ' ,
tra_cache [ length ] . cached_uncapped_decel ! = Train : : From ( u ) - > tcache . cached_uncapped_decel ? ' D ' : ' - ' ,
tra_cache [ length ] . cached_deceleration ! = Train : : From ( u ) - > tcache . cached_deceleration ? ' d ' : ' - ' ,
tra_cache [ length ] . user_def_data ! = Train : : From ( u ) - > tcache . user_def_data ? ' u ' : ' - ' ,
tra_cache [ length ] . cached_max_curve_speed ! = Train : : From ( u ) - > tcache . cached_max_curve_speed ? ' c ' : ' - ' ) ;
}
if ( Train : : From ( veh_old [ length ] ) - > railtype ! = Train : : From ( u ) - > railtype ) {
CCLOGV ( " railtype mismatch " ) ;
}
if ( Train : : From ( veh_old [ length ] ) - > compatible_railtypes ! = Train : : From ( u ) - > compatible_railtypes ) {
CCLOGV ( " compatible_railtypes mismatch " ) ;
}
if ( Train : : From ( veh_old [ length ] ) - > flags ! = Train : : From ( u ) - > flags ) {
CCLOGV ( " train flags mismatch " ) ;
}
break ;
case VEH_ROAD :
if ( memcmp ( & gro_cache [ length ] , & RoadVehicle : : From ( u ) - > gcache , sizeof ( GroundVehicleCache ) ) ! = 0 ) {
print_gv_cache_diff ( " road vehicle " , gro_cache [ length ] , Train : : From ( u ) - > gcache ) ;
}
break ;
case VEH_AIRCRAFT :
if ( memcmp ( & air_cache [ length ] , & Aircraft : : From ( u ) - > acache , sizeof ( AircraftCache ) ) ! = 0 ) {
CCLOGV ( " Aircraft vehicle cache mismatch: %c%c " ,
air_cache [ length ] . cached_max_range ! = Aircraft : : From ( u ) - > acache . cached_max_range ? ' r ' : ' - ' ,
air_cache [ length ] . cached_max_range_sqr ! = Aircraft : : From ( u ) - > acache . cached_max_range_sqr ? ' s ' : ' - ' ) ;
}
break ;
default :
break ;
}
free ( veh_old [ length ] ) ;
length + + ;
2010-07-13 20:12:44 +00:00
}
2021-10-03 22:03:26 +00:00
free ( grf_cache ) ;
free ( veh_cache ) ;
free ( gro_cache ) ;
free ( air_cache ) ;
free ( tra_cache ) ;
free ( veh_old ) ;
2010-07-13 20:12:44 +00:00
}
2010-03-13 14:58:37 +00:00
2021-10-03 22:03:26 +00:00
/* Check whether the caches are still valid */
for ( Vehicle * v : Vehicle : : Iterate ( ) ) {
2023-09-09 21:21:21 +00:00
Money old_feeder_share = v - > cargo . GetFeederShare ( ) ;
2023-04-22 21:50:18 +00:00
uint old_count = v - > cargo . TotalCount ( ) ;
2023-09-09 21:21:21 +00:00
uint64 old_cargo_periods_in_transit = v - > cargo . CargoPeriodsInTransit ( ) ;
2023-04-22 21:50:18 +00:00
2021-10-03 22:03:26 +00:00
v - > cargo . InvalidateCache ( ) ;
2023-04-22 21:50:18 +00:00
uint changed = 0 ;
2023-09-09 21:21:21 +00:00
if ( v - > cargo . GetFeederShare ( ) ! = old_feeder_share ) SetBit ( changed , 0 ) ;
2023-04-22 21:50:18 +00:00
if ( v - > cargo . TotalCount ( ) ! = old_count ) SetBit ( changed , 1 ) ;
2023-09-09 21:21:21 +00:00
if ( v - > cargo . CargoPeriodsInTransit ( ) ! = old_cargo_periods_in_transit ) SetBit ( changed , 2 ) ;
2023-04-22 21:50:18 +00:00
if ( changed ! = 0 ) {
CCLOGV1 ( " vehicle cargo cache mismatch: %c%c%c " ,
HasBit ( changed , 0 ) ? ' f ' : ' - ' ,
HasBit ( changed , 1 ) ? ' t ' : ' - ' ,
2023-09-09 21:21:21 +00:00
HasBit ( changed , 2 ) ? ' p ' : ' - ' ) ;
2023-04-22 21:50:18 +00:00
}
2010-07-13 20:12:44 +00:00
}
2010-03-13 14:58:37 +00:00
2021-10-03 22:03:26 +00:00
for ( Station * st : Station : : Iterate ( ) ) {
for ( CargoID c = 0 ; c < NUM_CARGO ; c + + ) {
2023-08-19 20:21:31 +00:00
if ( st - > goods [ c ] . data = = nullptr ) continue ;
2023-04-22 21:50:18 +00:00
2023-08-19 20:21:31 +00:00
uint old_count = st - > goods [ c ] . data - > cargo . TotalCount ( ) ;
2023-09-09 21:21:21 +00:00
uint64 old_cargo_periods_in_transit = st - > goods [ c ] . data - > cargo . CargoPeriodsInTransit ( ) ;
2023-08-19 20:21:31 +00:00
st - > goods [ c ] . data - > cargo . InvalidateCache ( ) ;
2023-04-22 21:50:18 +00:00
uint changed = 0 ;
2023-08-19 20:21:31 +00:00
if ( st - > goods [ c ] . data - > cargo . TotalCount ( ) ! = old_count ) SetBit ( changed , 0 ) ;
2023-09-09 21:21:21 +00:00
if ( st - > goods [ c ] . data - > cargo . CargoPeriodsInTransit ( ) ! = old_cargo_periods_in_transit ) SetBit ( changed , 1 ) ;
2023-04-22 21:50:18 +00:00
if ( changed ! = 0 ) {
CCLOG ( " station cargo cache mismatch: station %i, company %i, cargo %u: %c%c " ,
st - > index , ( int ) st - > owner , c ,
HasBit ( changed , 0 ) ? ' t ' : ' - ' ,
HasBit ( changed , 1 ) ? ' d ' : ' - ' ) ;
}
2010-11-06 12:53:31 +00:00
}
2021-10-03 22:03:26 +00:00
/* Check docking tiles */
TileArea ta ;
2023-06-01 16:41:34 +00:00
btree : : btree_set < TileIndex > docking_tiles ;
2021-10-08 17:12:04 +00:00
for ( TileIndex tile : st - > docking_station ) {
2021-10-03 22:03:26 +00:00
ta . Add ( tile ) ;
2023-06-01 16:41:34 +00:00
if ( IsDockingTile ( tile ) ) docking_tiles . insert ( tile ) ;
2015-09-27 22:05:18 +00:00
}
2021-10-03 22:03:26 +00:00
UpdateStationDockingTiles ( st ) ;
if ( ta . tile ! = st - > docking_station . tile | | ta . w ! = st - > docking_station . w | | ta . h ! = st - > docking_station . h ) {
CCLOG ( " station docking mismatch: station %i, company %i, prev: (%X, %u, %u), recalc: (%X, %u, %u) " ,
st - > index , ( int ) st - > owner , ta . tile , ta . w , ta . h , st - > docking_station . tile , st - > docking_station . w , st - > docking_station . h ) ;
2015-09-27 22:05:18 +00:00
}
2021-10-08 17:12:04 +00:00
for ( TileIndex tile : ta ) {
2023-06-01 16:41:34 +00:00
if ( ( docking_tiles . find ( tile ) ! = docking_tiles . end ( ) ) ! = IsDockingTile ( tile ) ) {
2021-10-03 22:03:26 +00:00
CCLOG ( " docking tile mismatch: tile %i " , ( int ) tile ) ;
}
2010-07-13 20:12:44 +00:00
}
2010-03-13 14:58:37 +00:00
}
2010-07-13 20:12:44 +00:00
2021-10-03 22:03:26 +00:00
for ( OrderList * order_list : OrderList : : Iterate ( ) ) {
order_list - > DebugCheckSanity ( ) ;
2010-03-13 14:58:37 +00:00
}
2020-05-06 23:14:22 +00:00
2021-10-03 22:03:26 +00:00
extern void ValidateVehicleTickCaches ( ) ;
ValidateVehicleTickCaches ( ) ;
for ( Vehicle * v : Vehicle : : Iterate ( ) ) {
if ( v - > Previous ( ) ) assert_msg ( v - > Previous ( ) - > Next ( ) = = v , " %u " , v - > index ) ;
if ( v - > Next ( ) ) assert_msg ( v - > Next ( ) - > Previous ( ) = = v , " %u " , v - > index ) ;
2020-05-06 23:14:22 +00:00
}
2021-10-03 22:03:26 +00:00
for ( const TemplateVehicle * tv : TemplateVehicle : : Iterate ( ) ) {
if ( tv - > Prev ( ) ) assert_msg ( tv - > Prev ( ) - > Next ( ) = = tv , " %u " , tv - > index ) ;
if ( tv - > Next ( ) ) assert_msg ( tv - > Next ( ) - > Prev ( ) = = tv , " %u " , tv - > index ) ;
2020-05-06 23:14:22 +00:00
}
2019-05-02 02:05:07 +00:00
2023-03-24 22:14:22 +00:00
{
extern std : : string ValidateTemplateReplacementCaches ( ) ;
std : : string template_validation_result = ValidateTemplateReplacementCaches ( ) ;
if ( ! template_validation_result . empty ( ) ) {
CCLOG ( " Template replacement cache validation failed: %s " , template_validation_result . c_str ( ) ) ;
}
}
2021-10-03 22:03:26 +00:00
if ( ! TraceRestrictSlot : : ValidateVehicleIndex ( ) ) CCLOG ( " Trace restrict slot vehicle index validation failed " ) ;
TraceRestrictSlot : : ValidateSlotOccupants ( log ) ;
2019-05-02 18:20:47 +00:00
2021-10-03 22:03:26 +00:00
if ( ! CargoPacket : : ValidateDeferredCargoPayments ( ) ) CCLOG ( " Cargo packets deferred payments validation failed " ) ;
2019-05-16 19:05:22 +00:00
2021-10-03 22:03:26 +00:00
if ( _order_destination_refcount_map_valid ) {
btree : : btree_map < uint32 , uint32 > saved_order_destination_refcount_map = std : : move ( _order_destination_refcount_map ) ;
for ( auto iter = saved_order_destination_refcount_map . begin ( ) ; iter ! = saved_order_destination_refcount_map . end ( ) ; ) {
if ( iter - > second = = 0 ) {
iter = saved_order_destination_refcount_map . erase ( iter ) ;
} else {
+ + iter ;
}
2019-09-15 10:35:49 +00:00
}
2021-10-03 22:03:26 +00:00
IntialiseOrderDestinationRefcountMap ( ) ;
if ( saved_order_destination_refcount_map ! = _order_destination_refcount_map ) CCLOG ( " Order destination refcount map mismatch " ) ;
} else {
CCLOG ( " Order destination refcount map not valid " ) ;
2019-09-15 10:35:49 +00:00
}
2019-05-16 19:05:22 +00:00
}
2019-05-18 17:31:08 +00:00
2021-10-03 22:57:27 +00:00
if ( ( flags & CHECK_CACHE_EMIT_LOG ) & & ! saved_messages . empty ( ) ) {
InconsistencyExtraInfo info ;
info . check_caches_result = std : : move ( saved_messages ) ;
CrashLog : : InconsistencyLog ( info ) ;
for ( std : : string & str : info . check_caches_result ) {
LogDesyncMsg ( std : : move ( str ) ) ;
2019-09-15 10:35:49 +00:00
}
2019-07-30 18:09:22 +00:00
}
2019-05-18 17:31:08 +00:00
# undef CCLOG
2023-04-22 21:50:18 +00:00
# undef CCLOGV
# undef CCLOGV1
2010-03-13 14:58:37 +00:00
}
2015-11-23 19:47:59 +00:00
/**
* Network - safe forced desync check .
* @ param tile unused
* @ param flags operation to perform
* @ param p1 unused
* @ param p2 unused
* @ param text unused
* @ return the cost of this operation or an error
*/
CommandCost CmdDesyncCheck ( TileIndex tile , DoCommandFlag flags , uint32 p1 , uint32 p2 , const char * text )
{
if ( flags & DC_EXEC ) {
2021-10-03 22:57:27 +00:00
CheckCaches ( true , nullptr , CHECK_CACHE_ALL | CHECK_CACHE_EMIT_LOG ) ;
2015-11-23 19:47:59 +00:00
}
return CommandCost ( ) ;
}
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
{
2015-09-03 17:36:30 +00:00
if ( ! _networking | | _network_server ) {
StateGameLoop_LinkGraphPauseControl ( ) ;
}
2021-03-09 19:26:01 +00:00
/* Don't execute the state loop during pause or when modal windows are open. */
if ( _pause_mode ! = PM_UNPAUSED | | HasModalProgress ( ) ) {
2018-07-19 19:17:07 +00:00
PerformanceMeasurer : : Paused ( PFE_GAMELOOP ) ;
PerformanceMeasurer : : Paused ( PFE_GL_ECONOMY ) ;
PerformanceMeasurer : : Paused ( PFE_GL_TRAINS ) ;
PerformanceMeasurer : : Paused ( PFE_GL_ROADVEHS ) ;
PerformanceMeasurer : : Paused ( PFE_GL_SHIPS ) ;
PerformanceMeasurer : : Paused ( PFE_GL_AIRCRAFT ) ;
PerformanceMeasurer : : Paused ( PFE_GL_LANDSCAPE ) ;
2021-03-09 19:26:01 +00:00
if ( ! HasModalProgress ( ) ) UpdateLandscapingLimits ( ) ;
2014-02-23 19:32:00 +00:00
# ifndef DEBUG_DUMP_COMMANDS
2011-12-19 20:50:54 +00:00
Game : : GameLoop ( ) ;
2014-02-23 19:32:00 +00:00
# endif
2008-01-01 22:34:00 +00:00
return ;
}
2018-07-19 19:17:07 +00:00
PerformanceMeasurer framerate ( PFE_GAMELOOP ) ;
PerformanceAccumulator : : Reset ( PFE_GL_LANDSCAPE ) ;
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 ( ) ;
2023-05-26 18:36:48 +00:00
TimerManager < TimerGameTick > : : Elapsed ( 1 ) ;
2014-02-23 22:03:08 +00:00
BasePersistentStorageArray : : SwitchMode ( PSM_LEAVE_GAMELOOP ) ;
2011-01-04 22:50:09 +00:00
UpdateLandscapingLimits ( ) ;
2008-01-15 18:51:46 +00:00
2018-05-04 20:29:22 +00:00
CallWindowGameTickEvent ( ) ;
2004-08-09 17:04:08 +00:00
NewsLoop ( ) ;
} else {
2015-09-27 22:12:00 +00:00
if ( _debug_desync_level > 2 & & _tick_skip_counter = = 0 & & _date_fract = = 0 & & ( _date & 0x1F ) = = 0 ) {
2010-04-09 16:01:48 +00:00
/* Save the desync savegame if needed. */
char name [ MAX_PATH ] ;
2014-04-23 21:12:09 +00:00
seprintf ( name , lastof ( name ) , " dmp_cmds_%08x_%08x.sav " , _settings_game . game_creation . generation_seed , _date ) ;
2016-09-04 16:06:50 +00:00
SaveOrLoad ( name , SLO_SAVE , DFT_GAME_FILE , AUTOSAVE_DIR , false ) ;
2010-04-09 16:01:48 +00:00
}
2021-10-03 22:57:27 +00:00
CheckCaches ( false , nullptr , CHECK_CACHE_ALL | CHECK_CACHE_EMIT_LOG ) ;
2010-04-21 12:55:33 +00:00
2007-03-21 15:19:33 +00:00
/* All these actions has to be done from OWNER_NONE
* for multiplayer compatibility */
2019-04-22 08:10:04 +00:00
Backup < CompanyID > cur_company ( _current_company , OWNER_NONE , FILE_LINE ) ;
2004-09-11 19:34:11 +00:00
2014-02-23 22:03:08 +00:00
BasePersistentStorageArray : : SwitchMode ( PSM_ENTER_GAMELOOP ) ;
2015-08-09 10:23:07 +00:00
_tick_skip_counter + + ;
2022-12-15 22:54:42 +00:00
_scaled_tick_counter + + ;
2023-07-05 22:55:10 +00:00
if ( _game_mode ! = GM_MENU & & _game_mode ! = GM_BOOTSTRAP ) {
_scaled_date_ticks + + ; // This must update in lock-step with _tick_skip_counter, such that it always matches what SetScaledTickVariables would return.
}
2021-12-14 18:48:57 +00:00
2023-11-13 18:45:18 +00:00
if ( ! ( _game_mode = = GM_MENU | | _game_mode = = GM_BOOTSTRAP ) & & ! _settings_client . gui . autosave_realtime & &
2023-11-12 13:17:33 +00:00
( _scaled_date_ticks % ( _settings_client . gui . autosave_interval * ( _settings_game . economy . tick_rate = = TRM_MODERN ? ( 60000 / 27 ) : ( 60000 / 30 ) ) ) ) = = 0 ) {
2021-12-14 18:48:57 +00:00
_do_autosave = true ;
_check_special_modes = true ;
SetWindowDirty ( WC_STATUS_BAR , 0 ) ;
}
2023-02-09 21:39:04 +00:00
RunAuxiliaryTileLoop ( ) ;
2015-08-09 10:23:07 +00:00
if ( _tick_skip_counter < _settings_game . economy . day_length_factor ) {
2016-03-02 18:36:53 +00:00
AnimateAnimatedTiles ( ) ;
2023-06-17 18:32:33 +00:00
RunTileLoop ( true ) ;
2015-08-09 10:23:07 +00:00
CallVehicleTicks ( ) ;
2022-09-02 17:14:35 +00:00
OnTick_Companies ( false ) ;
2015-08-09 10:23:07 +00:00
} else {
_tick_skip_counter = 0 ;
IncreaseDate ( ) ;
2016-09-09 23:32:00 +00:00
AnimateAnimatedTiles ( ) ;
2023-06-17 18:32:33 +00:00
RunTileLoop ( true ) ;
2015-08-09 10:23:07 +00:00
CallVehicleTicks ( ) ;
CallLandscapeTick ( ) ;
2022-09-02 17:14:35 +00:00
OnTick_Companies ( true ) ;
2015-08-09 10:23:07 +00:00
}
2023-04-16 18:14:22 +00:00
TimerManager < TimerGameTick > : : Elapsed ( 1 ) ;
2014-02-23 22:03:08 +00:00
BasePersistentStorageArray : : SwitchMode ( PSM_LEAVE_GAMELOOP ) ;
2004-08-09 17:04:08 +00:00
2014-02-23 19:32:00 +00:00
# ifndef DEBUG_DUMP_COMMANDS
2019-02-04 00:26:55 +00:00
{
2023-01-28 19:06:51 +00:00
PerformanceMeasurer script_framerate ( PFE_ALLSCRIPTS ) ;
2019-02-04 00:26:55 +00:00
AI : : GameLoop ( ) ;
Game : : GameLoop ( ) ;
}
2014-02-23 19:32:00 +00:00
# endif
2011-01-04 22:50:09 +00:00
UpdateLandscapingLimits ( ) ;
2004-08-09 17:04:08 +00:00
2018-05-04 20:29:22 +00:00
CallWindowGameTickEvent ( ) ;
2004-08-09 17:04:08 +00:00
NewsLoop ( ) ;
2019-08-05 18:16:51 +00:00
2022-11-13 11:53:16 +00:00
if ( _networking ) {
2023-08-28 14:17:53 +00:00
RecordSyncEvent ( NSRE_PRE_COMPANY_STATE ) ;
2022-11-13 11:53:16 +00:00
for ( Company * c : Company : : Iterate ( ) ) {
DEBUG_UPDATESTATECHECKSUM ( " Company: %u, Money: " OTTD_PRINTF64 , c - > index , ( int64 ) c - > money ) ;
UpdateStateChecksum ( c - > money ) ;
for ( uint i = 0 ; i < ROADTYPE_END ; i + + ) {
DEBUG_UPDATESTATECHECKSUM ( " Company: %u, road[%u]: %u " , c - > index , i , c - > infrastructure . road [ i ] ) ;
UpdateStateChecksum ( c - > infrastructure . road [ i ] ) ;
}
for ( uint i = 0 ; i < RAILTYPE_END ; i + + ) {
DEBUG_UPDATESTATECHECKSUM ( " Company: %u, rail[%u]: %u " , c - > index , i , c - > infrastructure . rail [ i ] ) ;
UpdateStateChecksum ( c - > infrastructure . rail [ i ] ) ;
}
DEBUG_UPDATESTATECHECKSUM ( " Company: %u, signal: %u, water: %u, station: %u, airport: %u " ,
c - > index , c - > infrastructure . signal , c - > infrastructure . water , c - > infrastructure . station , c - > infrastructure . airport ) ;
UpdateStateChecksum ( c - > infrastructure . signal ) ;
UpdateStateChecksum ( c - > infrastructure . water ) ;
UpdateStateChecksum ( c - > infrastructure . station ) ;
UpdateStateChecksum ( c - > infrastructure . airport ) ;
}
2019-08-05 18:16:51 +00:00
}
2021-10-18 23:39:41 +00:00
cur_company . Restore ( ) ;
2004-08-09 17:04:08 +00:00
}
2021-08-28 11:51:06 +00:00
if ( _extra_aspects > 0 ) FlushDeferredAspectUpdates ( ) ;
2010-06-05 13:32:42 +00:00
2020-06-05 16:11:09 +00:00
if ( _pause_countdown > 0 & & - - _pause_countdown = = 0 ) {
_pause_mode = PM_PAUSED_NORMAL ;
SetWindowDirty ( WC_MAIN_TOOLBAR , 0 ) ;
}
2022-10-22 11:34:54 +00:00
dbg_assert ( IsLocalCompany ( ) ) ;
2004-08-09 17:04:08 +00:00
}
2021-12-09 17:40:15 +00:00
FiosNumberedSaveName & GetAutoSaveFiosNumberedSaveName ( )
{
static FiosNumberedSaveName _autosave_ctr ( " autosave " ) ;
return _autosave_ctr ;
}
2023-07-09 08:44:09 +00:00
FiosNumberedSaveName & GetLongTermAutoSaveFiosNumberedSaveName ( )
{
static FiosNumberedSaveName _autosave_lt_ctr ( " ltautosave " ) ;
return _autosave_lt_ctr ;
}
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
{
2023-07-09 08:44:09 +00:00
FiosNumberedSaveName * lt_counter = nullptr ;
if ( _settings_client . gui . max_num_autosaves > 0 ) {
lt_counter = & GetLongTermAutoSaveFiosNumberedSaveName ( ) ;
}
DoAutoOrNetsave ( GetAutoSaveFiosNumberedSaveName ( ) , true , lt_counter ) ;
2004-08-09 17:04:08 +00:00
}
2023-04-27 15:21:29 +00:00
/** Interval for regular autosaves. Initialized at zero to disable till settings are loaded. */
static IntervalTimer < TimerGameRealtime > _autosave_interval ( { std : : chrono : : milliseconds : : zero ( ) , TimerGameRealtime : : AUTOSAVE } , [ ] ( auto )
{
/* We reset the command-during-pause mode here, so we don't continue
* to make auto - saves when nothing more is changing . */
_pause_mode & = ~ PM_COMMAND_DURING_PAUSE ;
_do_autosave = true ;
DoAutosave ( ) ;
_do_autosave = false ;
SetWindowDirty ( WC_STATUS_BAR , 0 ) ;
} ) ;
/**
* Reset the interval of the autosave .
*
* If reset is not set , this does not set the elapsed time on the timer ,
* so if the interval is smaller , it might result in an autosave being done
* immediately .
*
* @ param reset Whether to reset the timer back to zero , or to continue .
*/
void ChangeAutosaveFrequency ( bool reset )
{
2023-11-13 18:45:18 +00:00
std : : chrono : : minutes interval = _settings_client . gui . autosave_realtime ? std : : chrono : : minutes ( _settings_client . gui . autosave_interval ) : std : : chrono : : minutes : : zero ( ) ;
_autosave_interval . SetInterval ( { interval , TimerGameRealtime : : AUTOSAVE } , reset ) ;
2023-04-27 15:21:29 +00:00
}
2021-03-09 13:53:51 +00:00
/**
* Request a new NewGRF scan . This will be executed on the next game - tick .
* This is mostly needed to ensure NewGRF scans ( which are blocking ) are
* done in the game - thread , and not in the draw - thread ( which most often
* triggers this request ) .
* @ param callback Optional callback to call when NewGRF scan is completed .
2021-04-12 18:53:04 +00:00
* @ return True when the NewGRF scan was actually requested , false when the scan was already running .
2021-03-09 13:53:51 +00:00
*/
2021-04-12 18:53:04 +00:00
bool RequestNewGRFScan ( NewGRFScanCallback * callback )
2021-03-09 13:53:51 +00:00
{
2021-04-12 18:53:04 +00:00
if ( _request_newgrf_scan ) return false ;
2021-03-09 13:53:51 +00:00
_request_newgrf_scan = true ;
_request_newgrf_scan_callback = callback ;
2021-04-12 18:53:04 +00:00
return true ;
2021-03-09 13:53:51 +00:00
}
2020-12-26 10:54:01 +00:00
void GameLoopSpecial ( )
{
/* autosave game? */
if ( _do_autosave ) {
DoAutosave ( ) ;
_do_autosave = false ;
SetWindowDirty ( WC_STATUS_BAR , 0 ) ;
}
extern std : : string _switch_baseset ;
if ( ! _switch_baseset . empty ( ) ) {
if ( BaseGraphics : : GetUsedSet ( ) - > name ! = _switch_baseset ) {
2023-11-29 20:32:54 +00:00
BaseGraphics : : SetSetByName ( _switch_baseset ) ;
2020-12-26 10:54:01 +00:00
ReloadNewGRFData ( ) ;
}
_switch_baseset . clear ( ) ;
}
_check_special_modes = false ;
}
2007-03-07 11:47:46 +00:00
void GameLoop ( )
2004-08-09 17:04:08 +00:00
{
2011-11-17 21:17:17 +00:00
if ( _game_mode = = GM_BOOTSTRAP ) {
/* Check for UDP stuff */
2012-01-04 22:08:43 +00:00
if ( _network_available ) NetworkBackgroundLoop ( ) ;
2011-11-17 21:17:17 +00:00
return ;
}
2021-03-09 13:53:51 +00:00
if ( _request_newgrf_scan ) {
ScanNewGRFFiles ( _request_newgrf_scan_callback ) ;
_request_newgrf_scan = false ;
_request_newgrf_scan_callback = nullptr ;
2021-03-09 14:21:57 +00:00
/* In case someone closed the game during our scan, don't do anything else. */
if ( _exit_game ) return ;
2021-03-09 13:53:51 +00:00
}
2008-04-19 10:18:38 +00:00
ProcessAsyncSaveFinish ( ) ;
2004-08-09 17:04:08 +00:00
2020-12-26 10:54:01 +00:00
if ( unlikely ( _check_special_modes ) ) GameLoopSpecial ( ) ;
2004-08-09 17:04:08 +00:00
2023-04-27 15:21:29 +00:00
if ( _game_mode = = GM_NORMAL ) {
static auto last_time = std : : chrono : : steady_clock : : now ( ) ;
auto now = std : : chrono : : steady_clock : : now ( ) ;
auto delta_ms = std : : chrono : : duration_cast < std : : chrono : : milliseconds > ( now - last_time ) ;
if ( delta_ms . count ( ) ! = 0 ) {
TimerManager < TimerGameRealtime > : : Elapsed ( delta_ms ) ;
last_time = now ;
}
}
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 ;
2023-06-16 16:39:31 +00:00
if ( _exit_game ) return ;
2004-08-09 17:04:08 +00:00
}
IncreaseSpriteLRU ( ) ;
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
2021-07-23 20:36:17 +00:00
DebugSendRemoteMessages ( ) ;
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 */
2021-04-29 14:43:13 +00:00
NetworkClientConnectGame ( _settings_client . network . last_joined , 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
}
2022-09-14 20:55:28 +00:00
ExecuteCommandQueue ( ) ;
2004-08-09 17:04:08 +00:00
2021-04-05 19:25:45 +00:00
if ( ! _pause_mode & & HasBit ( _display_opt , DO_FULL_ANIMATION ) ) {
extern std : : mutex _cur_palette_mutex ;
std : : lock_guard < std : : mutex > lock_state ( _cur_palette_mutex ) ;
DoPaletteAnimations ( ) ;
}
2021-04-05 18:55:53 +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
}