From 3c8d8376b62447ee94a0ebb2649b5fc4baf6dd42 Mon Sep 17 00:00:00 2001 From: BevapDin Date: Fri, 22 Nov 2019 23:34:03 +0100 Subject: [PATCH 1/5] Save weather data as part of the master game file This ensures each character in the world has the same weather. And save it as JSON data. --- src/game.cpp | 7 +------ src/game.h | 2 -- src/savegame.cpp | 50 ++++++++++-------------------------------------- 3 files changed, 11 insertions(+), 48 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 18c4d7cab864f..ed1890308dc9d 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2668,8 +2668,6 @@ void game::load( const save_t &name ) u.deserialize_map_memory( jsin ); } ); - read_from_file_optional( worldpath + name.base_path() + ".weather", std::bind( &game::load_weather, - this, _1 ) ); weather.nextweather = calendar::turn; read_from_file_optional( worldpath + name.base_path() + ".log", @@ -2878,9 +2876,6 @@ bool game::save_player_data() JsonOut jsout( fout ); u.serialize_map_memory( jsout ); }, _( "player map memory" ) ); - const bool saved_weather = write_to_file( playerfile + ".weather", [&]( std::ostream & fout ) { - save_weather( fout ); - }, _( "weather state" ) ); const bool saved_log = write_to_file( playerfile + ".log", [&]( std::ostream & fout ) { fout << memorial().dump(); }, _( "player memorial" ) ); @@ -2890,7 +2885,7 @@ bool game::save_player_data() }, _( "quick shortcuts" ) ); #endif - return saved_data && saved_map_memory && saved_weather && saved_log + return saved_data && saved_map_memory && saved_log #if defined(__ANDROID__) && saved_shortcuts #endif diff --git a/src/game.h b/src/game.h index 78128a7be54ba..baa9a2a2b898e 100644 --- a/src/game.h +++ b/src/game.h @@ -709,7 +709,6 @@ class game // Game-start procedures void load( const save_t &name ); // Load a player-specific save file void load_master(); // Load the master data file, with factions &c - void load_weather( std::istream &fin ); #if defined(__ANDROID__) void load_shortcuts( std::istream &fin ); #endif @@ -724,7 +723,6 @@ class game bool save_artifacts(); // returns false if saving failed for whatever reason bool save_maps(); - void save_weather( std::ostream &fout ); #if defined(__ANDROID__) void save_shortcuts( std::ostream &fout ); #endif diff --git a/src/savegame.cpp b/src/savegame.cpp index c57e1bbede964..282ee845dcfc7 100644 --- a/src/savegame.cpp +++ b/src/savegame.cpp @@ -263,46 +263,6 @@ void scent_map::deserialize( const std::string &data ) } } -///// weather -void game::load_weather( std::istream &fin ) -{ - if( fin.peek() == '#' ) { - std::string vline; - getline( fin, vline ); - std::string tmphash; - std::string tmpver; - int savedver = -1; - std::stringstream vliness( vline ); - vliness >> tmphash >> tmpver >> savedver; - if( tmpver == "version" && savedver != -1 ) { - savegame_loading_version = savedver; - } - } - - //Check for "lightning:" marker - if absent, ignore - if( fin.peek() == 'l' ) { - std::string line; - getline( fin, line ); - weather.lightning_active = line == "lightning: 1"; - } else { - weather.lightning_active = false; - } - if( fin.peek() == 's' ) { - std::string line; - std::string label; - getline( fin, line ); - std::stringstream liness( line ); - liness >> label >> seed; - } -} - -void game::save_weather( std::ostream &fout ) -{ - fout << "# version " << savegame_version << std::endl; - fout << "lightning: " << ( weather.lightning_active ? "1" : "0" ) << std::endl; - fout << "seed: " << seed; -} - #if defined(__ANDROID__) ///// quick shortcuts void game::load_shortcuts( std::istream &fin ) @@ -1620,6 +1580,10 @@ void game::unserialize_master( std::istream &fin ) mission::unserialize_all( jsin ); } else if( name == "factions" ) { jsin.read( *faction_manager_ptr ); + } else if( name == "weather" ) { + JsonObject w = jsin.get_object(); + w.read( "lightning", weather.lightning_active ); + w.read( "seed", seed ); } else { // silently ignore anything else jsin.skip_value(); @@ -1654,6 +1618,12 @@ void game::serialize_master( std::ostream &fout ) json.member( "factions", *faction_manager_ptr ); + json.member( "weather" ); + json.start_object(); + json.member( "lightning", weather.lightning_active ); + json.member( "seed", seed ); + json.end_object(); + json.end_object(); } catch( const JsonError &e ) { debugmsg( "error saving to master.gsav: %s", e.c_str() ); From 8c01ea4d4238988ff3973db8ca0ca349e67d0746 Mon Sep 17 00:00:00 2001 From: BevapDin Date: Fri, 22 Nov 2019 23:37:01 +0100 Subject: [PATCH 2/5] Deduplicate code and call the proper function instead. The function contains character-by-character the same. --- src/savegame.cpp | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/savegame.cpp b/src/savegame.cpp index 282ee845dcfc7..9f9fd9a0ef8c6 100644 --- a/src/savegame.cpp +++ b/src/savegame.cpp @@ -149,18 +149,7 @@ static void chkversion( std::istream &fin ) */ void game::unserialize( std::istream &fin ) { - if( fin.peek() == '#' ) { - std::string vline; - getline( fin, vline ); - std::string tmphash; - std::string tmpver; - int savedver = -1; - std::stringstream vliness( vline ); - vliness >> tmphash >> tmpver >> savedver; - if( tmpver == "version" && savedver != -1 ) { - savegame_loading_version = savedver; - } - } + chkversion( fin ); std::string linebuf; int tmpturn = 0; From 20e917de8f4dc2cfcff227f81d2527037b33a767 Mon Sep 17 00:00:00 2001 From: BevapDin Date: Fri, 22 Nov 2019 23:37:29 +0100 Subject: [PATCH 3/5] Remove unused variables. --- src/savegame.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/savegame.cpp b/src/savegame.cpp index 9f9fd9a0ef8c6..fd91576eec21c 100644 --- a/src/savegame.cpp +++ b/src/savegame.cpp @@ -256,9 +256,6 @@ void scent_map::deserialize( const std::string &data ) ///// quick shortcuts void game::load_shortcuts( std::istream &fin ) { - std::string linebuf; - std::stringstream linein; - JsonIn jsin( fin ); try { JsonObject data = jsin.get_object(); From ed9f1ab3c00e17557fe636adde066d88fbd83341 Mon Sep 17 00:00:00 2001 From: BevapDin Date: Fri, 22 Nov 2019 23:38:03 +0100 Subject: [PATCH 4/5] Move variable into loop so it does not have to be cleared there. --- src/savegame.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/savegame.cpp b/src/savegame.cpp index fd91576eec21c..cb7e482fe8e24 100644 --- a/src/savegame.cpp +++ b/src/savegame.cpp @@ -150,8 +150,6 @@ static void chkversion( std::istream &fin ) void game::unserialize( std::istream &fin ) { chkversion( fin ); - std::string linebuf; - int tmpturn = 0; int tmpcalstart = 0; int tmprun = 0; @@ -187,7 +185,7 @@ void game::unserialize( std::istream &fin ) safe_mode = SAFE_MODE_ON; } - linebuf.clear(); + std::string linebuf; if( data.read( "grscent", linebuf ) ) { scent.deserialize( linebuf ); } else { From 8094c2a4a965de7bd20db1f8c56a1a307e279fcf Mon Sep 17 00:00:00 2001 From: BevapDin Date: Sat, 23 Nov 2019 00:22:34 +0100 Subject: [PATCH 5/5] Serialize game::seed into the main JSON object, not into the weather sub-object --- src/savegame.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/savegame.cpp b/src/savegame.cpp index cb7e482fe8e24..cfec2c14b6d2b 100644 --- a/src/savegame.cpp +++ b/src/savegame.cpp @@ -1564,10 +1564,11 @@ void game::unserialize_master( std::istream &fin ) mission::unserialize_all( jsin ); } else if( name == "factions" ) { jsin.read( *faction_manager_ptr ); + } else if( name == "seed" ) { + jsin.read( seed ); } else if( name == "weather" ) { JsonObject w = jsin.get_object(); w.read( "lightning", weather.lightning_active ); - w.read( "seed", seed ); } else { // silently ignore anything else jsin.skip_value(); @@ -1601,11 +1602,11 @@ void game::serialize_master( std::ostream &fout ) mission::serialize_all( json ); json.member( "factions", *faction_manager_ptr ); + json.member( "seed", seed ); json.member( "weather" ); json.start_object(); json.member( "lightning", weather.lightning_active ); - json.member( "seed", seed ); json.end_object(); json.end_object();