From 1f8cd852cefec92a6d4964c8a2535d5be58471be Mon Sep 17 00:00:00 2001 From: Gabriel Morin Date: Sat, 7 Aug 2010 09:20:24 +0000 Subject: [PATCH] Split a part of menu_events::recall()... ...into a new do_recall() method, so the whiteboard can call do_recall() directly on planned recall execution. --- src/menu_events.cpp | 60 ++++++++++++++++++++++++++++++++------------- src/menu_events.hpp | 1 + 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/menu_events.cpp b/src/menu_events.cpp index ec265432756..8a21b2d57e4 100644 --- a/src/menu_events.cpp +++ b/src/menu_events.cpp @@ -969,26 +969,52 @@ void menu_handler::recall(int side_num, const map_location &last_hex) } unit un = recall_list_team[res]; if (!resources::whiteboard->save_recall(un, side_num, loc)) { - recall_list_team.erase(recall_list_team.begin() + res); - recorder.add_recall(un.id(), loc); - place_recruit(un, loc, true, true); - statistics::recall_unit(un); - current_team.spend_gold(current_team.recall_cost()); - - bool shroud_cleared = clear_shroud(side_num); - if (shroud_cleared) { - clear_undo_stack(side_num); - } else { - resources::undo_stack->push_back(undo_action(un, loc, undo_action::RECALL)); - } - - resources::redo_stack->clear(); - gui_->invalidate_game_status(); - gui_->invalidate_all(); - recorder.add_checksum_check(loc); + do_recall(un, side_num, loc); } } +/// Predicate that compares the id() of two units. Used for the search in do_recall below +struct unit_comparator_predicate { + unit_comparator_predicate(const unit& unit) : unit_(unit) {} + bool operator()(const unit& unit) { return unit_.id() == unit.id(); } +private: + const unit& unit_; +}; + +void menu_handler::do_recall(const unit& un, int side_num, const map_location& recall_location) +{ + team ¤t_team = teams_[side_num - 1]; + std::vector& recall_list_team = current_team.recall_list(); + + unit_comparator_predicate comparator(un); + + std::vector::iterator it = std::find_if(recall_list_team.begin(), + recall_list_team.end(), comparator); + if (it == recall_list_team.end()) + { + ERR_NG << "menu_handler::do_recall(): Unit doesn't exist in recall list.\n"; + return; + } + + recall_list_team.erase(it); + recorder.add_recall(un.id(), recall_location); + place_recruit(un, recall_location, true, true); + statistics::recall_unit(un); + current_team.spend_gold(current_team.recall_cost()); + + bool shroud_cleared = clear_shroud(side_num); + if (shroud_cleared) { + clear_undo_stack(side_num); + } else { + resources::undo_stack->push_back(undo_action(un, recall_location, undo_action::RECALL)); + } + + resources::redo_stack->clear(); + gui_->invalidate_game_status(); + gui_->invalidate_all(); + recorder.add_checksum_check(recall_location); +} + void menu_handler::undo(int side_num) { if(resources::undo_stack->empty()) diff --git a/src/menu_events.hpp b/src/menu_events.hpp index c3732a764dd..a071059c3da 100644 --- a/src/menu_events.hpp +++ b/src/menu_events.hpp @@ -95,6 +95,7 @@ public: void move_unit_to_loc(const unit_map::const_iterator& ui, const map_location& target, bool continue_move, int side_num, mouse_handler &mousehandler); void do_recruit(const std::string& name, int side_num, const map_location& last_hex); + void do_recall(const unit& un, int side_num, const map_location& recall_location); void do_speak(); void do_search(const std::string& new_search); void do_command(const std::string &str);