Skip to content

Commit

Permalink
Merge pull request CleverRaven#54541 from kevingranade/fix-zones-save…
Browse files Browse the repository at this point in the history
…-load

Fix zones save load
  • Loading branch information
Rivet-the-Zombie authored Jan 21, 2022
2 parents 2b15889 + dfa2bfa commit a3d494f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
59 changes: 55 additions & 4 deletions src/clzones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#include "visitable.h"
#include "vpart_position.h"

static const faction_id faction_your_followers( "your_followers" );

static const item_category_id item_category_food( "food" );

static const itype_id itype_disassembly( "disassembly" );
Expand Down Expand Up @@ -1049,6 +1051,12 @@ void zone_manager::add( const std::string &name, const zone_type_id &type, const

//Create a regular zone
zones.push_back( new_zone );

// personal/faction zones are saved when the zone manager exits,
// but other-faction zones are not, so save them here.
if( fac != faction_your_followers ) {
save_world_zones();
}
if( personal ) {
num_personal_zones++;
}
Expand Down Expand Up @@ -1203,15 +1211,19 @@ void zone_manager::serialize( JsonOut &json ) const
void zone_manager::deserialize( const JsonValue &jv )
{
jv.read( zones );
for( auto it = zones.begin(); it != zones.end(); ++it ) {
for( auto it = zones.begin(); it != zones.end(); ) {
// need to keep track of number of personal zones on reload
if( it->get_is_personal() ) {
num_personal_zones++;
}
const zone_type_id zone_type = it->get_type();
if( !has_type( zone_type ) ) {
zones.erase( it );
it = zones.erase( it );
debugmsg( "Invalid zone type: %s", zone_type.c_str() );
} else if( it->get_faction() != faction_your_followers ) {
it = zones.erase( it );
} else {
it++;
}
}
}
Expand Down Expand Up @@ -1296,10 +1308,15 @@ void zone_manager::load_zones()
{
std::string savefile = PATH_INFO::player_base_save_path() + ".zones.json";

read_from_file_optional( savefile, [&]( std::istream & fin ) {
const auto reader = [this]( std::istream & fin ) {
JsonIn jsin( fin );
deserialize( jsin.get_value() );
} );
};
if( !read_from_file_optional( savefile, reader ) ) {
// If no such file or failed to load, clear zones.
zones.clear();
}
load_world_zones();
revert_vzones();
added_vzones.clear();
changed_vzones.clear();
Expand All @@ -1309,6 +1326,40 @@ void zone_manager::load_zones()
cache_vzones();
}

bool zone_manager::save_world_zones()
{
std::string savefile = PATH_INFO::world_base_save_path() + "/zones.json";
std::vector<zone_data> tmp;
std::copy_if( zones.begin(), zones.end(), std::back_inserter( tmp ), []( zone_data z ) {
return z.get_faction() != faction_your_followers;
} );
return write_to_file( savefile, [&]( std::ostream & fout ) {
JsonOut jsout( fout );
jsout.write( tmp );
}, _( "zones date" ) );
}

void zone_manager::load_world_zones()
{
std::string savefile = PATH_INFO::world_base_save_path() + "/zones.json";
std::vector<zone_data> tmp;
read_from_file_optional( savefile, [&]( std::istream & fin ) {
JsonIn jsin( fin );
jsin.read( tmp );
for( auto it = tmp.begin(); it != tmp.end(); ++it ) {
const zone_type_id zone_type = it->get_type();
if( !has_type( zone_type ) ) {
tmp.erase( it );
debugmsg( "Invalid zone type: %s", zone_type.c_str() );
}
if( it->get_faction() == faction_your_followers ) {
tmp.erase( it );
}
}
std::copy( tmp.begin(), tmp.end(), std::back_inserter( zones ) );
} );
}

void zone_manager::zone_edited( zone_data &zone )
{
if( zone.get_is_vehicle() ) {
Expand Down
3 changes: 2 additions & 1 deletion src/clzones.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ class zone_manager
const faction_id &fac = your_fac ) const;
std::unordered_set<tripoint_abs_ms> get_vzone_set( const zone_type_id &type,
const faction_id &fac = your_fac ) const;

public:
zone_manager();
~zone_manager() = default;
Expand Down Expand Up @@ -485,7 +484,9 @@ class zone_manager
bool has_personal_zones() const;

bool save_zones();
bool save_world_zones();
void load_zones();
void load_world_zones();
void zone_edited( zone_data &zone );
void revert_vzones();
void serialize( JsonOut &json ) const;
Expand Down
1 change: 1 addition & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2928,6 +2928,7 @@ bool game::save()
JsonOut jsout( fout );
uistate.serialize( jsout );
}, _( "uistate data" ) ) ) {
debugmsg( "game not saved" );
return false;
} else {
world_generator->active_world->add_save( save_t::from_save_id( u.get_save_id() ) );
Expand Down

0 comments on commit a3d494f

Please sign in to comment.