From 46e390cbb75dc8d6266cdb13ccd707b08a7e0a7a Mon Sep 17 00:00:00 2001 From: Zach Morgan Date: Sat, 2 Feb 2013 02:21:53 -0500 Subject: [PATCH] Tracking whether MAPBUFFER actually needs saving with a 'dirty bit'. I imagine that overmaps and nonants could use the same principle to be loaded on demand andn saved as needed. --- game.cpp | 10 +++++----- main.cpp | 21 +++++++++++---------- mapbuffer.cpp | 23 +++++++++++++++++++++-- mapbuffer.h | 8 +++++++- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/game.cpp b/game.cpp index d06d98183d915..3fa91d7703125 100644 --- a/game.cpp +++ b/game.cpp @@ -517,7 +517,7 @@ void game::start_game() // Put some NPCs in there! create_starting_npcs(); - + MAPBUFFER.set_dirty(); } void game::create_factions() @@ -1504,6 +1504,7 @@ input_ret game::get_input(int timeout_ms) save(); u.moves = 0; uquit = QUIT_SAVED; + MAPBUFFER.make_volatile(); } break; } else { @@ -1639,8 +1640,6 @@ bool game::is_game_over() m.add_item(u.posx, u.posy, your_body); for (int i = 0; i < tmp.size(); i++) m.add_item(u.posx, u.posy, tmp[i]); - //m.save(&cur_om, turn, levx, levy); - //MAPBUFFER.save(); std::stringstream playerfile; playerfile << "save/" << u.name << ".sav"; unlink(playerfile.str().c_str()); @@ -1781,10 +1780,10 @@ void game::load(std::string name) turn = tmpturn; nextspawn = tmpspawn; nextweather = tmpnextweather; + cur_om = overmap(this, comx, comy, levz); -// m = map(&itypes, &mapitems, &traps); // Init the root map with our vectors - //MAPBUFFER.load(); m.load(this, levx, levy); + run_mode = tmprun; if (OPTIONS[OPT_SAFEMODE] && run_mode == 0) run_mode = 1; @@ -1851,6 +1850,7 @@ void game::load(std::string name) // Now load up the master game data; factions (and more?) load_master(); set_adjacent_overmaps(true); + MAPBUFFER.set_dirty(); draw(); } diff --git a/main.cpp b/main.cpp index 7e340b52762bc..3ac2bcf1c3e78 100644 --- a/main.cpp +++ b/main.cpp @@ -39,7 +39,7 @@ int main(int argc, char *argv[]) bool quit_game = false; bool delete_world = false; game *g = new game; - MAPBUFFER = mapbuffer(g); + MAPBUFFER.set_game(g); MAPBUFFER.load(); load_options(); do { @@ -50,16 +50,17 @@ int main(int argc, char *argv[]) if (g->game_quit()) quit_game = true; } while (!quit_game); - MAPBUFFER.save(); - if (delete_world && (remove("save/") != 0)) - { - #if (defined _WIN32 || defined __WIN32__) - system("rmdir /s /q save"); - #else - system("rm -rf save/*"); - #endif - } + if (delete_world && (remove("save/") != 0)) + { + #if (defined _WIN32 || defined __WIN32__) + system("rmdir /s /q save"); + #else + system("rm -rf save/*"); + #endif + } else { + MAPBUFFER.save_if_dirty(); + } erase(); // Clear screen endwin(); // End ncurses diff --git a/mapbuffer.cpp b/mapbuffer.cpp index 98bcf489da2d6..def75dd79a5d1 100644 --- a/mapbuffer.cpp +++ b/mapbuffer.cpp @@ -9,9 +9,9 @@ mapbuffer MAPBUFFER; // g defaults to NULL -mapbuffer::mapbuffer(game *g) +mapbuffer::mapbuffer() { - master_game = g; + dirty = false; } mapbuffer::~mapbuffer() @@ -22,11 +22,24 @@ mapbuffer::~mapbuffer() delete *it; } +// game g's existance does not imply that it has been identified, started, or loaded. void mapbuffer::set_game(game *g) { master_game = g; } +// set to dirty right before the game starts & the player starts changing stuff. +void mapbuffer::set_dirty() +{ + dirty = true; +} +// initial state; no need to synchronize. +// make volatile after game has ended. +void mapbuffer::make_volatile() +{ + dirty = false; +} + bool mapbuffer::add_submap(int x, int y, int z, submap *sm) { dbg(D_INFO) << "mapbuffer::add_submap( x["<< x <<"], y["<< y <<"], z["<< z <<"], submap["<< sm <<"])"; @@ -57,6 +70,12 @@ submap* mapbuffer::lookup_submap(int x, int y, int z) return submaps[p]; } +void mapbuffer::save_if_dirty() +{ + if(dirty) + save(); +} + void mapbuffer::save() { std::map::iterator it; diff --git a/mapbuffer.h b/mapbuffer.h index 9bed45e9f58b2..3c44ec0f743f1 100644 --- a/mapbuffer.h +++ b/mapbuffer.h @@ -22,13 +22,18 @@ struct pointcomp class mapbuffer { public: - mapbuffer(game *g = NULL); + mapbuffer(); ~mapbuffer(); void set_game(game *g); + //help save_if_dirty() know to save as many times as it's supposed to. + void set_dirty(); + void make_volatile(); + void load(); void save(); + void save_if_dirty(); bool add_submap(int x, int y, int z, submap *sm); submap* lookup_submap(int x, int y, int z); @@ -39,6 +44,7 @@ class mapbuffer std::map submaps; std::list submap_list; game *master_game; + bool dirty; }; extern mapbuffer MAPBUFFER;