Skip to content

Commit

Permalink
make the unit id manager a member of game_board
Browse files Browse the repository at this point in the history
previously it was a singleton

the next unit id counter is part of the gamestate so it should be part of the game_state object.
  • Loading branch information
gfgtdf committed Sep 9, 2015
1 parent 4afe65b commit 12b9019
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 56 deletions.
10 changes: 5 additions & 5 deletions src/actions/undo.cpp
Expand Up @@ -373,14 +373,14 @@ void undo_list::undo()
action_list::auto_type action = undos_.pop_back();
if (undo_action* undoable_action = dynamic_cast<undo_action*>(action.ptr()))
{
int last_unit_id = n_unit::id_manager::instance().get_save_id();
int last_unit_id = resources::gameboard->unit_id_manager().get_save_id();
if ( !undoable_action->undo(side_) ) {
return;
}
if(last_unit_id - undoable_action->unit_id_diff < 0) {
ERR_NG << "Next unit id is below 0 after undoing" << std::endl;
}
n_unit::id_manager::instance().set_save_id(last_unit_id - undoable_action->unit_id_diff);
resources::gameboard->unit_id_manager().set_save_id(last_unit_id - undoable_action->unit_id_diff);

// Bookkeeping.
resources::recorder->undo_cut(undoable_action->replay_data);
Expand Down Expand Up @@ -422,14 +422,14 @@ void undo_list::redo()
// Get the action to redo. (This will be placed on the undo stack, but
// only if the redo is successful.)
redos_list::auto_type action = redos_.pop_back();
int last_unit_id = n_unit::id_manager::instance().get_save_id();
int last_unit_id = resources::gameboard->unit_id_manager().get_save_id();
if ( !action->redo(side_) ) {
return;
}
if(last_unit_id + action->unit_id_diff < static_cast<int>(n_unit::id_manager::instance().get_save_id())) {
if(last_unit_id + action->unit_id_diff < static_cast<int>(resources::gameboard->unit_id_manager().get_save_id())) {
ERR_NG << "Too many units were generated during redoing." << std::endl;
}
n_unit::id_manager::instance().set_save_id(last_unit_id + action->unit_id_diff);
resources::gameboard->unit_id_manager().set_save_id(last_unit_id + action->unit_id_diff);

// Bookkeeping.
undos_.push_back(action.release());
Expand Down
19 changes: 16 additions & 3 deletions src/game_board.cpp
Expand Up @@ -36,9 +36,19 @@ static lg::log_domain log_engine("enginerefac");
static lg::log_domain log_engine_enemies("engine/enemies");
#define DBG_EE LOG_STREAM(debug, log_engine_enemies)

game_board::game_board(const tdata_cache & tdata, const config & level) : teams_(), map_(new gamemap(tdata, level)), units_() {}
game_board::game_board(const tdata_cache & tdata, const config & level)
: teams_()
, map_(new gamemap(tdata, level))
, unit_id_manager_(level["next_underlying_unit_id"])
, units_()
{
}

game_board::game_board(const game_board & other) : teams_(other.teams_), map_(new gamemap(*(other.map_))), units_(other.units_) {}
game_board::game_board(const game_board & other)
: teams_(other.teams_)
, map_(new gamemap(*(other.map_)))
, unit_id_manager_(other.unit_id_manager_)
, units_(other.units_) {}

game_board::~game_board() {}

Expand All @@ -49,6 +59,7 @@ game_board::~game_board() {}
void swap(game_board & one, game_board & other) {
std::swap(one.teams_, other.teams_);
std::swap(one.units_, other.units_);
std::swap(one.unit_id_manager_, other.unit_id_manager_);
one.map_.swap(other.map_);
}

Expand Down Expand Up @@ -328,7 +339,9 @@ bool game_board::change_terrain(const map_location &loc, const std::string &t_st
return true;
}

void game_board::write_config(config & cfg) const {
void game_board::write_config(config & cfg) const
{
cfg["next_underlying_unit_id"] = unit_id_manager_.get_save_id();
for(std::vector<team>::const_iterator t = teams_.begin(); t != teams_.end(); ++t) {
int side_num = t - teams_.begin() + 1;

Expand Down
9 changes: 6 additions & 3 deletions src/game_board.hpp
Expand Up @@ -21,6 +21,7 @@
#include "team.hpp"
#include "terrain_type_data.hpp"
#include "unit_map.hpp"
#include "unit_id.hpp"

#include <boost/optional.hpp>
#include <boost/scoped_ptr.hpp>
Expand Down Expand Up @@ -51,12 +52,14 @@ namespace events {
*
**/

class game_board : public display_context {
class game_board : public display_context
{

std::vector<team> teams_;
std::vector<std::string> labels_;

boost::scoped_ptr<gamemap> map_;
n_unit::id_manager unit_id_manager_;
unit_map units_;

//TODO: Remove these when we have refactored enough to make it possible.
Expand Down Expand Up @@ -84,8 +87,8 @@ class game_board : public display_context {
friend struct temporary_unit_mover;
friend struct temporary_unit_remover;

public:

public:
n_unit::id_manager& unit_id_manager() { return unit_id_manager_; }
// Constructors, trivial dtor, and const accessors

game_board(const tdata_cache & tdata, const config & level);
Expand Down
1 change: 0 additions & 1 deletion src/game_initialization/multiplayer.cpp
Expand Up @@ -452,7 +452,6 @@ static void enter_wait_mode(game_display& disp, const config& game_config,

gamelist.clear();
statistics::fresh_stats();
n_unit::id_manager::instance().clear(); /* reset the unit underlying_id counter back to zero */

{
mp::wait ui(disp, game_config, state, gamechat, gamelist);
Expand Down
8 changes: 8 additions & 0 deletions src/game_state.cpp
Expand Up @@ -133,6 +133,7 @@ static void no_op() {}

void game_state::init(const int ticks, play_controller & pc)
{
int ticks1 = SDL_GetTicks();
if (level_["modify_placing"].to_bool()) {
LOG_NG << "modifying placing..." << std::endl;
place_sides_in_preferred_locations();
Expand Down Expand Up @@ -183,6 +184,8 @@ void game_state::init(const int ticks, play_controller & pc)
}
}
}

std::cerr << "Initilizing teams took " << SDL_GetTicks() - ticks1 << " ticks.\n";

pathfind_manager_.reset(new pathfind::manager(level_));

Expand All @@ -191,6 +194,10 @@ void game_state::init(const int ticks, play_controller & pc)
game_events_resources_ = boost::make_shared<game_events::t_context>(lua_kernel_.get(), this, static_cast<game_display*>(NULL), &gamedata_, &board_.units_, &no_op, boost::bind(&play_controller::current_side, &pc));

events_manager_.reset(new game_events::manager(level_, game_events_resources_));

std::cerr << "Initilizing total took " << SDL_GetTicks() - ticks1 << " ticks.\n";


}

void game_state::bind(wb::manager * whiteboard, game_display * gd)
Expand Down Expand Up @@ -228,6 +235,7 @@ void game_state::write(config& cfg) const

//Write the game data, including wml vars
gamedata_.write_snapshot(cfg);

}

namespace {
Expand Down
1 change: 1 addition & 0 deletions src/game_state.hpp
Expand Up @@ -21,6 +21,7 @@ class config;
#include "game_board.hpp"
#include "game_data.hpp"
#include "tod_manager.hpp"
#include "unit_id.hpp"

#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
Expand Down
5 changes: 1 addition & 4 deletions src/play_controller.cpp
Expand Up @@ -172,8 +172,6 @@ play_controller::play_controller(const config& level, saved_game& state_of_game,
set_end_level_data(el_data);
}

n_unit::id_manager::instance().set_save_id(level_["next_underlying_unit_id"]);

// Setup victory and defeat music
set_victory_music_list(level_["victory_music"]);
set_defeat_music_list(level_["defeat_music"]);
Expand Down Expand Up @@ -477,7 +475,6 @@ config play_controller::to_config() const
sound::write_music_play_list(cfg);
}

cfg["next_underlying_unit_id"] = str_cast(n_unit::id_manager::instance().get_save_id());
return cfg;
}

Expand Down Expand Up @@ -508,7 +505,7 @@ void play_controller::finish_side_turn()
}

mouse_handler_.deselect_hex();
n_unit::id_manager::instance().reset_fake();
resources::gameboard->unit_id_manager().reset_fake();
init_side_done_ = false;
}

Expand Down
1 change: 0 additions & 1 deletion src/replay_controller.cpp
Expand Up @@ -338,7 +338,6 @@ void replay_controller::reset_replay()
gui_->labels().read(level_);

*resources::gamedata = game_data(level_);
n_unit::id_manager::instance().set_save_id(level_["next_underlying_unit_id"]);
statistics::fresh_stats();

gui_->needs_rebuild(true);
Expand Down
7 changes: 4 additions & 3 deletions src/synced_context.cpp
Expand Up @@ -27,6 +27,7 @@
#include "resources.hpp"
#include "synced_checkup.hpp"
#include "game_data.hpp"
#include "game_board.hpp"
#include "network.hpp"
#include "log.hpp"
#include "lua_jailbreak_exception.hpp"
Expand Down Expand Up @@ -239,7 +240,7 @@ int synced_context::get_unit_id_diff()
{
//this method only works in a synced context.
assert(is_synced());
return n_unit::id_manager::instance().get_save_id() - last_unit_id_;
return resources::gameboard->unit_id_manager().get_save_id() - last_unit_id_;
}

namespace
Expand Down Expand Up @@ -396,7 +397,7 @@ set_scontext_synced_base::set_scontext_synced_base()
assert(synced_context::get_synced_state() == synced_context::UNSYNCED);
synced_context::set_synced_state(synced_context::SYNCED);
synced_context::reset_is_simultaneously();
synced_context::set_last_unit_id(n_unit::id_manager::instance().get_save_id());
synced_context::set_last_unit_id(resources::gameboard->unit_id_manager().get_save_id());
old_rng_ = random_new::generator;
random_new::generator = new_rng_.get();
}
Expand Down Expand Up @@ -452,7 +453,7 @@ void set_scontext_synced::do_final_checkup(bool dont_throw)
config co;
config cn = config_of
("random_calls", new_rng_->get_random_calls())
("next_unit_id", n_unit::id_manager::instance().get_save_id() + 1);
("next_unit_id", resources::gameboard->unit_id_manager().get_save_id() + 1);
if(checkup_instance->local_checkup(cn, co))
{
return;
Expand Down
1 change: 0 additions & 1 deletion src/tests/test_unit_map.cpp
Expand Up @@ -108,7 +108,6 @@ BOOST_AUTO_TEST_CASE( test_1 ) {
// unit_map.add(map_location(2,guard), orc1_side0_real);
// };

// n_unit::id_manager::instance().clear();
// std::cerr<<"BREAK\n;";
// unit_map.add(map_location(1,3), orc2_side0_fake);
// unit_map.add(map_location(1,4), orc2_side0_fake);
Expand Down
12 changes: 6 additions & 6 deletions src/unit.cpp
Expand Up @@ -577,7 +577,7 @@ unit::unit(const unit_type &u_type, int side, bool real_unit,
, race_(&unit_race::null_race)
, id_()
, name_()
, underlying_id_(real_unit? n_unit::unit_id(0) : n_unit::id_manager::instance().next_fake_id())
, underlying_id_(real_unit? n_unit::unit_id(0) : resources::gameboard->unit_id_manager().next_fake_id())
, undead_variation_()
, variation_(type_->default_variation())
, hit_points_(0)
Expand Down Expand Up @@ -2272,10 +2272,10 @@ bool unit::is_visible_to_team(team const& team, gamemap const& map, bool const s
void unit::set_underlying_id() {
if(underlying_id_.value == 0) {
if(synced_context::is_synced() || !resources::gamedata || resources::gamedata->phase() == game_data::INITIAL) {
underlying_id_ = n_unit::id_manager::instance().next_id();
underlying_id_ = resources::gameboard->unit_id_manager().next_id();
}
else {
underlying_id_ = n_unit::id_manager::instance().next_fake_id();
underlying_id_ = resources::gameboard->unit_id_manager().next_fake_id();
}
}
if (id_.empty() /*&& !underlying_id_.is_fake()*/) {
Expand All @@ -2288,13 +2288,13 @@ void unit::set_underlying_id() {
unit& unit::clone(bool is_temporary)
{
if(is_temporary) {
underlying_id_ = n_unit::id_manager::instance().next_fake_id();
underlying_id_ = resources::gameboard->unit_id_manager().next_fake_id();
} else {
if(synced_context::is_synced() || !resources::gamedata || resources::gamedata->phase() == game_data::INITIAL) {
underlying_id_ = n_unit::id_manager::instance().next_id();
underlying_id_ = resources::gameboard->unit_id_manager().next_id();
}
else {
underlying_id_ = n_unit::id_manager::instance().next_fake_id();
underlying_id_ = resources::gameboard->unit_id_manager().next_fake_id();
}
std::string::size_type pos = id_.find_last_of('-');
if(pos != std::string::npos && pos+1 < id_.size()
Expand Down
10 changes: 3 additions & 7 deletions src/unit_id.cpp
Expand Up @@ -20,17 +20,13 @@
static lg::log_domain log_unit("unit");
#define DBG_UT LOG_STREAM(debug, log_unit)

namespace n_unit {
namespace n_unit
{
id_manager id_manager::manager_;

id_manager::id_manager() : next_id_(0), fake_id_(0)
{}

id_manager& id_manager::instance()
{
return manager_;
}

unit_id id_manager::next_id()
{
assert(next_id_ < unit_id::highest_bit);
Expand All @@ -45,7 +41,7 @@ namespace n_unit {
return unit_id::create_fake(++fake_id_);
}

size_t id_manager::get_save_id()
size_t id_manager::get_save_id() const
{
return next_id_;
}
Expand Down
39 changes: 20 additions & 19 deletions src/unit_id.hpp
Expand Up @@ -42,25 +42,26 @@ namespace n_unit {
friend bool operator >(unit_id a, unit_id b) { return a > b; }
};

class id_manager : private boost::noncopyable {
private:
size_t next_id_;
size_t fake_id_;
static id_manager manager_;
id_manager();
public:
static id_manager& instance();
/** returns id for unit that is created */
unit_id next_id();

unit_id next_fake_id();

/** Used for saving id to savegame */
size_t get_save_id();
void set_save_id(size_t);
/** Clears id counter after game */
void clear();
void reset_fake();
class id_manager //: private boost::noncopyable
{
private:
size_t next_id_;
size_t fake_id_;
static id_manager manager_;
id_manager();
public:
id_manager(size_t next_id) : next_id_(next_id) , fake_id_(0) {}
/** returns id for unit that is created */
unit_id next_id();

unit_id next_fake_id();

/** Used for saving id to savegame */
size_t get_save_id() const;
void set_save_id(size_t);
/** Clears id counter after game */
void clear();
void reset_fake();
};

}
Expand Down
8 changes: 5 additions & 3 deletions src/unit_map.cpp
Expand Up @@ -159,11 +159,13 @@ std::pair<unit_map::unit_iterator, bool> unit_map::insert(unit_ptr p) {
<< " (" << loc << ") over " << q->name()
<< " - " << q->id() << " - " << q->underlying_id()
<< " (" << q->get_location()
<< "). The new unit will be assigned underlying_id="
<< (1 + n_unit::id_manager::instance().get_save_id())
<< " to prevent duplicate id conflicts.\n";
<< ").";

p->clone(false);
ERR_NG << "The new unit was assigned underlying_id="
<< p->underlying_id()
<< " to prevent duplicate id conflicts.\n";

uinsert = umap_.insert(std::make_pair(p->underlying_id(), upod ));
int guard(0);
while (!uinsert.second && (++guard < 1e6) ) {
Expand Down

0 comments on commit 12b9019

Please sign in to comment.