From 77c94414ece6023d006a8118807ae138ecdfd8e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Hinrichs?= Date: Sat, 5 Sep 2009 06:48:20 +0000 Subject: [PATCH] Cleaning up the undo code a little. --- src/actions.hpp | 24 +++++++++++------------- src/menu_events.cpp | 10 +++++----- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/actions.hpp b/src/actions.hpp index 28a466b4a1f..c54022b8776 100644 --- a/src/actions.hpp +++ b/src/actions.hpp @@ -36,8 +36,6 @@ class unit; #include #include -#define RECRUIT_POS -2 - bool can_recruit_on(const gamemap& map, const map_location& leader, const map_location loc); struct end_level_exception; @@ -344,6 +342,8 @@ int combat_modifier(const unit_map &units, const map_location &loc, /** Records information to be able to undo a movement. */ struct undo_action { + enum ACTION_TYPE { NONE, RECRUIT, RECALL, DISMISS}; + undo_action(const unit& u, const std::vector& rt, int sm, int timebonus = 0, int orig = -1) : @@ -351,33 +351,31 @@ struct undo_action { starting_moves(sm), original_village_owner(orig), recall_loc(), - recall_pos(-1), + type(NONE), affected_unit(u), - countdown_time_bonus(timebonus), - is_dismiss(false) + countdown_time_bonus(timebonus) {} - undo_action(const unit& u, const map_location& loc, const int pos, const bool dismiss=false) : + undo_action(const unit& u, const map_location& loc, const ACTION_TYPE action_type=NONE) : route(), starting_moves(), original_village_owner(), recall_loc(loc), - recall_pos(pos), + type(action_type), affected_unit(u), - countdown_time_bonus(1), - is_dismiss(dismiss) + countdown_time_bonus(1) {} std::vector route; int starting_moves; int original_village_owner; map_location recall_loc; - int recall_pos; // set to RECRUIT_POS for an undo-able recruit + ACTION_TYPE type; unit affected_unit; int countdown_time_bonus; - bool is_dismiss; //set to true if the action is a dismissal of a recallable unit - bool is_recall() const { return recall_pos >= 0; } - bool is_recruit() const { return recall_pos == RECRUIT_POS; } + bool is_dismiss() const { return type == DISMISS; } + bool is_recall() const { return type == RECALL; } + bool is_recruit() const { return type == RECRUIT; } }; typedef std::deque undo_list; diff --git a/src/menu_events.cpp b/src/menu_events.cpp index 68d041660ae..e163dfbc851 100644 --- a/src/menu_events.cpp +++ b/src/menu_events.cpp @@ -104,7 +104,7 @@ namespace events{ // Remove the item from filter_textbox memory filter_.delete_item(menu_selection); //add dismissal to the undo stack - undo_stack_.push_back(undo_action(u, map_location(), static_cast(index), true)); + undo_stack_.push_back(undo_action(u, map_location(), undo_action::DISMISS)); //remove the unit from the recall list std::vector::iterator dismissed_unit = std::find_if(units_.begin(), units_.end(), boost::bind(&unit::matches_id, _1, u.id())); @@ -803,7 +803,7 @@ private: || new_unit.type()->has_random_traits()) { clear_undo_stack(side_num); } else { - undo_stack_.push_back(undo_action(new_unit, loc, RECRUIT_POS)); + undo_stack_.push_back(undo_action(new_unit, loc, undo_action::RECRUIT)); } gui_->recalculate_minimap(); @@ -953,7 +953,7 @@ private: if (shroud_cleared) { clear_undo_stack(side_num); } else { - undo_stack_.push_back(undo_action(un, loc, res)); + undo_stack_.push_back(undo_action(un, loc, undo_action::RECALL)); } redo_stack_.clear(); @@ -973,7 +973,7 @@ private: team ¤t_team = teams_[side_num - 1]; undo_action& action = undo_stack_.back(); - if (action.is_dismiss) { + if (action.is_dismiss()) { //undo a dismissal if(!current_team.persistent()) { @@ -1088,7 +1088,7 @@ private: team ¤t_team = teams_[side_num - 1]; undo_action& action = redo_stack_.back(); - if (action.is_dismiss) { + if (action.is_dismiss()) { if(!current_team.persistent()) { ERR_NG << "trying to redo a dismiss for side " << side_num << ", which has no recall list!\n";