Cleaned usage of unit_map::add.

This commit is contained in:
Guillaume Melquiond 2009-03-13 14:13:30 +00:00
parent 3301a19e7d
commit 2f2a238fae
12 changed files with 78 additions and 69 deletions

View File

@ -177,7 +177,7 @@ std::string recruit_unit(const gamemap& map, const int side, unit_map& units,
new_unit.heal_all();
new_unit.set_hidden(true);
units.add(new std::pair<map_location,unit>(recruit_location,new_unit));
units.add(recruit_location, new_unit);
if (is_recall)
{
@ -1206,7 +1206,8 @@ attack::attack(game_display& gui, const gamemap& map,
reanimitor = unit_type_data::types().find_unit_type(a_stats_->plague_type);
LOG_NG << "found unit type:" << reanimitor->second.id() << std::endl;
if(reanimitor != unit_type_data::types().end()) {
unit newunit = unit(&units_, &map_, &state_, &teams_, &reanimitor->second, a_.get_unit().side(), true, true);
unit newunit(&units_, &map_, &state_, &teams_, &reanimitor->second,
a_.get_unit().side(), true, true);
newunit.set_attacks(0);
// Apply variation
if(strcmp(undead_variation.c_str(), "null")) {
@ -1217,7 +1218,7 @@ attack::attack(game_display& gui, const gamemap& map,
newunit.add_modification("variation",mod);
newunit.heal_all();
}
units_.add(new std::pair<map_location,unit>(death_loc, newunit));
units_.add(death_loc, newunit);
preferences::encountered_units().insert(newunit.type_id());
if (update_display_){
gui_.invalidate(death_loc);
@ -1476,7 +1477,8 @@ attack::attack(game_display& gui, const gamemap& map,
reanimitor = unit_type_data::types().find_unit_type(d_stats_->plague_type);
LOG_NG << "found unit type:" << reanimitor->second.id() << std::endl;
if(reanimitor != unit_type_data::types().end()) {
unit newunit = unit(&units_, &map_, &state_, &teams_, &reanimitor->second,d_.get_unit().side(), true, true);
unit newunit(&units_, &map_, &state_, &teams_, &reanimitor->second,
d_.get_unit().side(), true, true);
// Apply variation
if(strcmp(undead_variation.c_str(),"null")){
config mod;
@ -1485,7 +1487,7 @@ attack::attack(game_display& gui, const gamemap& map,
variation["name"]=undead_variation;
newunit.add_modification("variation",mod);
}
units_.add(new std::pair<map_location,unit>(death_loc,newunit));
units_.add(death_loc, newunit);
preferences::encountered_units().insert(newunit.type_id());
if (update_display_){
gui_.invalidate(death_loc);
@ -1892,7 +1894,7 @@ void advance_unit(unit_map& units,
preferences::encountered_units().insert(new_unit.type_id());
LOG_STREAM(info, config) << "Added '" << new_unit.type_id() << "' to encountered units\n";
units.replace(new std::pair<map_location,unit>(loc,new_unit));
units.replace(loc, new_unit);
LOG_NG << "firing post_advance event at " << loc << "\n";
game_events::fire("post_advance",loc);
}
@ -2359,10 +2361,8 @@ size_t move_unit(game_display* disp,
ui->second.set_movement(moves_left);
std::pair<map_location,unit> *p = units.extract(ui->first);
p->first = steps.back();
units.add(p);
ui = units.find(p->first);
units.move(ui->first, steps.back());
ui = units.find(steps.back());
unit::clear_status_caches();
if(move_recorder != NULL) {

View File

@ -593,7 +593,7 @@ map_location ai_interface::move_unit_partial(location from, location to,
std::pair<map_location,unit> *p = info_.units.extract(u_it->first);
p->first = to;
info_.units.add(p);
info_.units.insert(p);
p->second.set_standing(p->first);
if(info_.map.is_village(to)) {
// If a new village is captured, disallow any future movement.
@ -2088,8 +2088,10 @@ void ai::move_leader_after_recruit(const move_map& /*srcdst*/,
if(current_loc.valid()) {
LOG_AI << "considering movement to " << str_cast(current_loc.x + 1)
<< "," << str_cast(current_loc.y+1);
unit_map temp_units(current_loc,leader->second);
const paths p(map_,temp_units,current_loc,teams_,false,false,current_team());
unit_map temp_units;
temp_units.add(current_loc, leader->second);
const paths p(map_, temp_units, current_loc, teams_, false,
false, current_team());
if(p.routes.count(i->first)) {
move_unit(leader->first,current_loc,possible_moves);
@ -2125,7 +2127,7 @@ void ai::move_leader_after_recruit(const move_map& /*srcdst*/,
}
}
units_.add(temp_leader);
units_.insert(temp_leader);
if(friend_can_reach_keep) {
// Find a location for our leader to vacate the keep to

View File

@ -357,7 +357,7 @@ void ai::attack_analysis::analyze(const gamemap& map, unit_map& units,
// We fix up units map to reflect what this would look like.
std::pair<map_location,unit> *up = units.extract(m->first);
up->first = m->second;
units.add(up);
units.insert(up);
if (up->second.can_recruit()) {
uses_leader = true;
@ -504,9 +504,7 @@ void ai::attack_analysis::analyze(const gamemap& map, unit_map& units,
// Restore the units to their original positions.
for (m = movements.begin(); m != movements.end(); ++m) {
std::pair<map_location,unit> *up = units.extract(m->second);
up->first = m->first;
units.add(up);
units.move(m->second, m->first);
}
}

View File

@ -396,11 +396,11 @@ namespace dfool {
// std::cout<<"turn: "<< turns_[i] <<"\n";
turn_used[l] = t;
if(t != 0){
u.replace(new std::pair<map_location,unit>(l,units_[i]));
u.replace(l, units_[i]);
}else{
std::cout<<"id: "<< ids_[i] <<"\n";
u.add(new std::pair<map_location,unit>(l,units_[i]));
u.add(l, units_[i]);
}
}
}

View File

@ -156,7 +156,7 @@ void get_player_info(const config& cfg, game_state& gamestate,
VALIDATE(units.count(start_pos) == 0,
t_string(vgettext("Duplicate side definition for side '$side|' found.", symbols)));
units.add(new std::pair<map_location,unit>(map.starting_position(new_unit.side()), new_unit));
units.add(map.starting_position(new_unit.side()), new_unit);
LOG_NG << "initializing side '" << cfg["side"] << "' at "
<< start_pos << '\n';
}
@ -212,7 +212,7 @@ void get_player_info(const config& cfg, game_state& gamestate,
if (units.find(loc) != units.end()) {
ERR_NG << "[unit] trying to overwrite existing unit at " << loc << "\n";
} else {
units.add(new std::pair<map_location,unit>(loc,new_unit));
units.add(loc, new_unit);
LOG_NG << "inserting unit for side " << new_unit.side() << "\n";
}
}

View File

@ -190,7 +190,7 @@ bool animate_unit_advancement(unit_map& units, map_location loc, game_display& g
amla_unit.get_experience(-amla_unit.max_experience()); // subtract xp required
amla_unit.add_modification("advance",mod_option);
units.replace(new std::pair<map_location,unit>(loc,amla_unit));
units.replace(loc, amla_unit);
LOG_NG << "firing post_advance event (AMLA)\n";
game_events::fire("post_advance",loc);

View File

@ -15,6 +15,7 @@
#include <boost/lexical_cast.hpp>
#include <vector>
#include "foreach.hpp"
#include "unit.hpp"
#include "menu_events.hpp"
@ -369,10 +370,9 @@ private:
variant attack = args()[0]->evaluate(variables);
ai::attack_analysis* analysis = convert_variant<ai::attack_analysis>(attack);
unit_map units_with_moves(ai_.get_info().units);
for(size_t n = 0; n != analysis->movements.size(); ++n) {
std::pair<map_location,unit>* pair = units_with_moves.extract(analysis->movements[n].first);
pair->first = analysis->movements[n].second;
units_with_moves.add(pair);
typedef std::pair<map_location, map_location> mv;
foreach (const mv &m, analysis->movements) {
units_with_moves.move(m.first, m.second);
}
std::vector<variant> vars;

View File

@ -549,9 +549,7 @@ namespace {
const int side = u->second.side();
const map_location src_loc = u->first;
std::pair<map_location,unit> *up = units->extract(u->first);
up->first = vacant_dst;
units->add(up);
units->move(src_loc, vacant_dst);
unit_mutations++;
if(game_map->is_village(vacant_dst)) {
get_village(vacant_dst, *screen,*teams,side-1,*units);
@ -1860,7 +1858,7 @@ namespace {
(*teams)[new_unit.side() - 1].set_current_player(new_unit.name());
units->erase(loc);
units->add(new std::pair<map_location,unit>(loc,new_unit));
units->add(loc, new_unit);
unit_mutations++;
if(game_map->is_village(loc)) {
get_village(loc,*screen,*teams,new_unit.side()-1,*units);
@ -2346,7 +2344,7 @@ namespace {
}
units->erase(loc);
units->add(new std::pair<map_location,unit>(loc,u));
units->add(loc, u);
unit_mutations++;
std::string text = cfg["text"];

View File

@ -1207,7 +1207,7 @@ private:
up->second.set_goto(map_location());
up->second.set_movement(starting_moves);
up->first = route.back();
units_.add(up);
units_.insert(up);
unit::clear_status_caches();
up->second.set_standing(up->first);
gui_->invalidate(route.back());
@ -1324,7 +1324,7 @@ private:
up->second.set_goto(map_location());
up->second.set_movement(starting_moves);
up->first = route.back();
units_.add(up);
units_.insert(up);
unit::clear_status_caches();
up->second.set_standing(up->first);
@ -1569,11 +1569,9 @@ private:
const unit_race::GENDER gender =
(!genders.empty() ? genders[gamestate_.rng().get_random() % genders.size()] : unit_race::MALE);
units_.erase(mousehandler.get_last_hex());
unit chosen(&units_,&map_,&status_,&teams_,unit_choices[choice],1,false,false,gender,"",random_gender);
chosen.new_turn();
units_.add(new std::pair<map_location,unit>(mousehandler.get_last_hex(),chosen));
units_.replace(mousehandler.get_last_hex(), chosen);
gui_->invalidate(mousehandler.get_last_hex());
gui_->invalidate_unit();
@ -3009,18 +3007,19 @@ private:
}
}
void console_handler::do_create() {
if (menu_handler_.map_.on_board(mouse_handler_.get_last_hex())) {
const map_location &loc = mouse_handler_.get_last_hex();
if (menu_handler_.map_.on_board(loc)) {
const unit_type_data::unit_type_map::const_iterator i = unit_type_data::types().find_unit_type(get_data());
if(i == unit_type_data::types().end()) {
command_failed("Invalid unit type");
return;
}
menu_handler_.units_.erase(mouse_handler_.get_last_hex());
menu_handler_.units_.add(new std::pair<map_location,unit>(
mouse_handler_.get_last_hex(),
unit(&menu_handler_.units_,&menu_handler_.map_,&menu_handler_.status_,&menu_handler_.teams_,&i->second,1,false)));
menu_handler_.gui_->invalidate(mouse_handler_.get_last_hex());
menu_handler_.units_.erase(loc);
menu_handler_.units_.add(loc,
unit(&menu_handler_.units_, &menu_handler_.map_, &menu_handler_.status_,
&menu_handler_.teams_, &i->second, 1, false));
menu_handler_.gui_->invalidate(loc);
menu_handler_.gui_->invalidate_unit();
} else {
command_failed("Invalid location");

View File

@ -3065,14 +3065,14 @@ temporary_unit_placer::temporary_unit_placer(unit_map& m, const map_location& lo
: m_(m), loc_(loc), temp_(m.extract(loc))
{
u.clone();
m.add(new std::pair<map_location,unit>(loc,u));
m.add(loc, u);
}
temporary_unit_placer::~temporary_unit_placer()
{
m_.erase(loc_);
if(temp_) {
m_.add(temp_);
m_.insert(temp_);
}
}

View File

@ -26,15 +26,6 @@
typedef std::pair<std::string, std::pair<bool, std::pair<map_location, unit>*> > umap_pair;
unit_map::unit_map(const map_location &loc, const unit &u) :
map_(),
lmap_(),
num_iters_(0),
num_invalid_(0)
{
add(new std::pair<map_location,unit>(loc, u));
}
unit_map::unit_map(const unit_map &that) :
/* Initialize to silence compiler warnings. */
map_(),
@ -52,7 +43,7 @@ unit_map &unit_map::operator=(const unit_map &that)
num_invalid_ = 0;
for (umap::const_iterator i = that.map_.begin(); i != that.map_.end(); i++) {
if (i->second.first) {
add(new std::pair<map_location,unit>(i->second.second->first, i->second.second->second));
add(i->second.second->first, i->second.second->second);
}
}
return *this;
@ -468,8 +459,19 @@ unit_map::unit_iterator unit_map::begin() {
return unit_iterator(i, this);
}
void unit_map::add(const map_location &l, const unit &u)
{
insert(new std::pair<map_location,unit>(l, u));
}
void unit_map::add(std::pair<map_location,unit> *p)
void unit_map::move(const map_location &src, const map_location &dst)
{
std::pair<map_location,unit> *p = extract(src);
p->first = dst;
insert(p);
}
void unit_map::insert(std::pair<map_location,unit> *p)
{
size_t unit_id = p->second.underlying_id();
umap::iterator iter = map_.find(unit_id);
@ -480,7 +482,8 @@ void unit_map::add(std::pair<map_location,unit> *p)
iter->second.second = p;
validate(iter);
} else if(iter->second.second->first == p->first) {
replace(p);
erase(p->first);
insert(p);
return;
} else {
ERR_NG << "Trying to add " << p->second.name() <<
@ -495,7 +498,7 @@ void unit_map::add(std::pair<map_location,unit> *p)
<< (1 + n_unit::id_manager::instance().get_save_id())
<< " to prevent duplicate id conflicts.\n";
p->second.clone(false);
add(p);
insert(p);
return;
}
@ -506,12 +509,10 @@ void unit_map::add(std::pair<map_location,unit> *p)
assert(res.second);
}
void unit_map::replace(std::pair<map_location,unit> *p)
void unit_map::replace(const map_location &l, const unit &u)
{
if (erase(p->first) != 1)
assert(0);
DBG_NG << "Replace unit " << p->second.underlying_id() << "\n";
add(p);
erase(l);
add(l, u);
}
void unit_map::delete_all()

View File

@ -75,8 +75,6 @@ public:
unit_map() : map_(), lmap_(), num_iters_(0), num_invalid_(0) { };
unit_map(const unit_map &that);
unit_map &operator =(const unit_map &that);
/** A unit map with a copy of a single unit in it. */
explicit unit_map(const map_location &loc, const unit &u);
~unit_map();
/**
@ -415,13 +413,26 @@ public:
std::pair<map_location,unit> *extract(const map_location &loc);
/**
* Map owns pointer after this. Loc must be currently empty. unit's
* underlying_id should not be present in the map already
* Adds a copy of unit @a u at location @a l of the map.
*/
void add(std::pair<map_location,unit> *p);
void add(const map_location &l, const unit &u);
/**
* Adds the pair location/unit to the map.
* @pre The location is empty.
* @pre The unit::underlying_id should not be used by the map already.
* @note The map takes ownership of the pointed object.
* @note This function should be used in conjunction with #extract only.
*/
void insert(std::pair<map_location,unit> *p);
/**
* Moves a unit from location @a src to location @a dst.
*/
void move(const map_location &src, const map_location &dst);
/** Like add, but loc must be occupied (implicitly erased). */
void replace(std::pair<map_location,unit> *p);
void replace(const map_location &l, const unit &u);
void erase(xy_accessor pos);
size_t erase(const map_location &loc);