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

Tracking whether MAPBUFFER actually needs saving #32

Merged
merged 1 commit into from
Feb 2, 2013
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
10 changes: 5 additions & 5 deletions game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ void game::start_game()

// Put some NPCs in there!
create_starting_npcs();

MAPBUFFER.set_dirty();
}

void game::create_factions()
Expand Down Expand Up @@ -1504,6 +1504,7 @@ input_ret game::get_input(int timeout_ms)
save();
u.moves = 0;
uquit = QUIT_SAVED;
MAPBUFFER.make_volatile();
}
break;
} else {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
21 changes: 11 additions & 10 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
23 changes: 21 additions & 2 deletions mapbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
mapbuffer MAPBUFFER;

// g defaults to NULL
mapbuffer::mapbuffer(game *g)
mapbuffer::mapbuffer()
{
master_game = g;
dirty = false;
}

mapbuffer::~mapbuffer()
Expand All @@ -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 <<"])";
Expand Down Expand Up @@ -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<tripoint, submap*>::iterator it;
Expand Down
8 changes: 7 additions & 1 deletion mapbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -39,6 +44,7 @@ class mapbuffer
std::map<tripoint, submap*, pointcomp> submaps;
std::list<submap*> submap_list;
game *master_game;
bool dirty;
};

extern mapbuffer MAPBUFFER;