Erase unit's previous move when defining a new move

This commit is contained in:
Gabriel Morin 2010-06-15 20:37:28 +00:00
parent 3c81bdbe54
commit d3da33b68b
5 changed files with 55 additions and 3 deletions

View file

@ -20,6 +20,7 @@
#include "action.hpp"
#include "mapbuilder_visitor.hpp"
#include "find_visitor.hpp"
#include "arrow.hpp"
#include "foreach.hpp"
@ -65,6 +66,7 @@ side_actions& get_current_side_actions()
void manager::apply_temp_modifiers()
{
mapbuilder_.reset(new mapbuilder_visitor(*resources::units));
mapbuilder_->exclude(*selected_unit_);
const action_set& actions = get_current_side_actions().actions();
foreach (const action_ptr &action, actions)
{
@ -82,17 +84,19 @@ void manager::remove_temp_modifiers()
void manager::select_unit(unit& unit)
{
selected_unit_ = &unit;
DBG_WB << "Selected unit " << selected_unit_->name() << " [" << selected_unit_->id() << "]\n";
}
void manager::deselect_unit()
{
DBG_WB << "Deselecting unit " << selected_unit_->name() << " [" << selected_unit_->id() << "]\n";
selected_unit_ = NULL;
}
void manager::create_temp_move(const std::vector<map_location> &steps)
{
route_ = steps;
if (route_.size() > 1)
if (route_.size() > 1 && selected_unit_ != NULL)
{
bool show_ghosted_unit_bars = false;
@ -132,6 +136,20 @@ void manager::erase_temp_move()
void manager::save_temp_move()
{
//If selected unit already has a move defined, erase it first
// TODO: implement a find_and_erase method in find_visitor to avoid iterating twice over actions
{ // scope-limiting block
find_visitor finder;
action_ptr action = finder.find_first_action_of(*selected_unit_, get_current_side_actions().actions());
if (action)
{
LOG_WB << "Previous action found for unit " << selected_unit_->name() << " [" << selected_unit_->id() << "]"
<< ", erasing action.\n";
get_current_side_actions().remove_action(action);
}
} // end scope-limiting block
//Define the new move
LOG_WB << "Creating move for unit " << selected_unit_->name() << " [" << selected_unit_->id() << "]"
<< " from " << selected_unit_->get_location()
<< " to " << route_.back() << "\n";
@ -141,7 +159,6 @@ void manager::save_temp_move()
get_current_side_actions().queue_move(*selected_unit_, route_.back(),
*(move_arrow_.release()) /* ownership of the arrow transferred to the new move action */,
*(fake_unit_.release()) /* ownership of the fake unit transferred to the new move action */);
}
} // end namespace wb

View file

@ -18,6 +18,8 @@
#include "mapbuilder_visitor.hpp"
#include "move.hpp"
#include "unit.hpp"
#include "unit_map.hpp"
namespace wb
@ -26,6 +28,7 @@ namespace wb
mapbuilder_visitor::mapbuilder_visitor(unit_map& unit_map)
: unit_map_(unit_map)
, modifiers_()
, excluded_units_()
{
}
@ -40,7 +43,10 @@ mapbuilder_visitor::~mapbuilder_visitor()
void mapbuilder_visitor::visit_move(move& move)
{
modifiers_.push(move.apply_temp_modifier(unit_map_));
if (excluded_units_.count(&move.get_unit()) == 0)
{
modifiers_.push(move.apply_temp_modifier(unit_map_));
}
}
}

View file

@ -22,10 +22,12 @@
#include "visitor.hpp"
#include "action.hpp"
#include <set>
#include <stack>
#include <boost/shared_ptr.hpp>
class unit;
class unit_map;
namespace wb
@ -44,10 +46,15 @@ public:
virtual void visit_move(move& move);
// Any actions associated with this unit will be ignored when modifying the unit map
virtual void exclude(const unit& unit) { excluded_units_.insert(&unit); }
private:
unit_map& unit_map_;
std::stack<modifier_ptr> modifiers_;
std::set<unit const*> excluded_units_;
};
}

View file

@ -20,6 +20,8 @@
#include "action.hpp"
#include "move.hpp"
#include "foreach.hpp"
namespace wb
{
@ -71,6 +73,21 @@ void side_actions::remove_action(size_t index)
}
}
void side_actions::remove_action(action_ptr action)
{
assert(!actions_.empty());
action_set::iterator position;
for ( position = actions_.begin(); position != actions_.end(); ++position)
{
if (*position == action)
{
actions_.erase(position);
break;
}
}
}
/*
* Utility function to move actions around the queue.
* Positive increment = move toward back of the queue and later execution.

View file

@ -87,6 +87,11 @@ public:
*/
void remove_action(size_t index);
/**
* Deletes the specified action. If the action doesn't exist, the function does nothing.
*/
void remove_action(action_ptr action);
private:
/**
* Utility function to move actions around the queue.