Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize weather into the main game save. #35661

Merged
merged 6 commits into from
Nov 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2669,8 +2669,6 @@ void game::load( const save_t &name )
u.deserialize_map_memory( jsin );
} );

read_from_file_optional( worldpath + name.base_path() + SAVE_EXTENSION_WEATHER,
std::bind( &game::load_weather, this, _1 ) );
weather.nextweather = calendar::turn;

read_from_file_optional( worldpath + name.base_path() + SAVE_EXTENSION_LOG,
Expand Down Expand Up @@ -2880,10 +2878,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 + SAVE_EXTENSION_WEATHER, [&](
std::ostream & fout ) {
save_weather( fout );
}, _( "weather state" ) );
const bool saved_log = write_to_file( playerfile + SAVE_EXTENSION_MAP_MEMORY, [&](
std::ostream & fout ) {
fout << memorial().dump();
Expand All @@ -2895,7 +2889,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
Expand Down
2 changes: 0 additions & 2 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,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
Expand All @@ -732,7 +731,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
Expand Down
71 changes: 13 additions & 58 deletions src/savegame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +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;
}
}
std::string linebuf;

chkversion( fin );
int tmpturn = 0;
int tmpcalstart = 0;
int tmprun = 0;
Expand Down Expand Up @@ -198,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 {
Expand Down Expand Up @@ -263,53 +250,10 @@ 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 )
{
std::string linebuf;
std::stringstream linein;

JsonIn jsin( fin );
try {
JsonObject data = jsin.get_object();
Expand Down Expand Up @@ -1620,6 +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 );
} else {
// silently ignore anything else
jsin.skip_value();
Expand Down Expand Up @@ -1653,6 +1602,12 @@ 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.end_object();

json.end_object();
} catch( const JsonError &e ) {
Expand Down