whiteboard/manager.hpp

Go to the documentation of this file.
00001 /* $Id: manager.hpp 53957 2012-04-22 04:28:48Z gabba $ */
00002 /*
00003  Copyright (C) 2010 - 2012 by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
00004  Part of the Battle for Wesnoth Project http://www.wesnoth.org
00005 
00006  This program is free software; you can redistribute it and/or modify
00007  it under the terms of the GNU General Public License as published by
00008  the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010  This program is distributed in the hope that it will be useful,
00011  but WITHOUT ANY WARRANTY.
00012 
00013  See the COPYING file for more details.
00014  */
00015 
00016 /**
00017  * @file
00018  */
00019 
00020 #ifndef WB_MANAGER_HPP_
00021 #define WB_MANAGER_HPP_
00022 
00023 #include "side_actions.hpp"
00024 
00025 #include <boost/noncopyable.hpp>
00026 
00027 class CKey;
00028 class team;
00029 
00030 namespace pathfind {
00031     struct marked_route;
00032 }
00033 
00034 namespace wb {
00035 
00036 class mapbuilder;
00037 class highlight_visitor;
00038 
00039 /**
00040  * This class is the frontend of the whiteboard framework for the rest of the Wesnoth code.
00041  */
00042 class manager : private boost::noncopyable
00043 {
00044     friend struct future_map;
00045     friend struct future_map_if_active;
00046     friend struct real_map;
00047 
00048 public:
00049 
00050     manager();
00051     ~manager();
00052 
00053     void print_help_once();
00054 
00055     /** Determine whether the game is initialized and the current side has control of the game
00056      *  i.e. the whiteboard can take over
00057      */
00058     bool can_modify_game_state() const;
00059     /** Determine whether the whiteboard can be activated safely */
00060     bool can_activate() const;
00061     /** Determine whether the whiteboard is activated. */
00062     bool is_active() const { return active_; }
00063     /** Activates/Deactivates the whiteboard*/
00064     void set_active(bool active);
00065     /** Called by the key that temporarily toggles the activated state when held */
00066     void set_invert_behavior(bool invert);
00067     /** Prevents the whiteboard from changing its activation state, as long as the returned reference is held */
00068     whiteboard_lock get_activation_state_lock() { return activation_state_lock_; }
00069 
00070     /** Is the whiteboard in the process of executing an action? */
00071     bool is_executing_actions() const { return executing_actions_; }
00072 
00073     /** Used to ask the whiteboard if its action execution hotkeys should be available to the user */
00074     bool can_enable_execution_hotkeys() const;
00075     /** Used to ask the whiteboard if hotkeys affecting the action queue should be available to the user */
00076     bool can_enable_modifier_hotkeys() const;
00077     /** Used to ask the whiteboard if its action reordering hotkeys should be available to the user */
00078     bool can_enable_reorder_hotkeys() const;
00079     /** Used to ask permission to the wb to move a leader, to avoid invalidating planned recruits */
00080     bool allow_leader_to_move(unit const& leader) const;
00081     /** @ return true if the whiteboard is ready to end turn. Triggers the execution of remaining planned actions. */
00082     bool allow_end_turn();
00083     /**
00084      * The on_* methods below inform the whiteboard of specific events
00085      */
00086     void on_init_side();
00087     void on_finish_side_turn(int side);
00088     void on_mouseover_change(const map_location& hex);
00089     void on_deselect_hex(){ erase_temp_move();}
00090     void on_gamestate_change();
00091     void on_viewer_change(size_t team_index);
00092     void on_change_controller(int side, team& t);
00093     /** Handles various cleanup right before removing an action from the queue */
00094     void pre_delete_action(action_ptr action);
00095     /** Handles various cleanup right after removing an action from the queue */
00096     void post_delete_action(action_ptr action);
00097 
00098     /** Called by replay_network_sender to add whiteboard data to the outgoing network packets */
00099     void send_network_data();
00100     /** Called by turn_info::process_network_data() when network data needs to be processed */
00101     void process_network_data(config const&);
00102     /** Adds a side_actions::net_cmd to net_buffer_[team_index], whereupon it will (later) be sent to all allies */
00103     void queue_net_cmd(size_t team_index, side_actions::net_cmd const&);
00104 
00105     /** Whether the current side has actions in the first turn of its planned actions queue */
00106     static bool current_side_has_actions();
00107 
00108     /** Validates all actions of the current viewing side */
00109     void validate_viewer_actions();
00110 
00111     /** Whether the planned unit map is currently applied */
00112     bool has_planned_unit_map() const { return planned_unit_map_active_; }
00113 
00114 
00115     /**
00116      * Called from the display before drawing.
00117      */
00118     void pre_draw();
00119     /**
00120      * Called from the display after drawing.
00121      */
00122     void post_draw();
00123     /**
00124      * Called from the display when drawing hexes, to allow the whiteboard to
00125      * add visual elements. Some visual elements such as arrows and fake units
00126      * are not handled through this function, but separately registered with the display.
00127      */
00128     void draw_hex(const map_location& hex);
00129 
00130     /** Creates a temporary visual arrow, that follows the cursor, for move creation purposes */
00131     void create_temp_move();
00132     /** Informs whether an arrow is being displayed for move creation purposes */
00133     bool has_temp_move() const { return route_ && !fake_units_.empty() && !move_arrows_.empty(); }
00134     /** Erase the temporary arrow */
00135     void erase_temp_move();
00136     /** Creates a move action for the current side, and erases the temp move.
00137      *  The move action is inserted at the end of the queue, to be executed last. */
00138     void save_temp_move();
00139     /** @return an iterator to the unit that owns the temp move, resources::units->end() if there's none. */
00140     unit_map::iterator get_temp_move_unit() const;
00141 
00142     /** Creates an attack or attack-move action for the current side */
00143     void save_temp_attack(const map_location& attacker_loc, const map_location& defender_loc, int weapon_choice);
00144 
00145     /** Creates a recruit action for the current side
00146      *  @return true if manager has saved a planned recruit */
00147     bool save_recruit(const std::string& name, int side_num, const map_location& recruit_hex);
00148 
00149     /** Creates a recall action for the current side
00150      *  @return true if manager has saved a planned recall */
00151     bool save_recall(const unit& unit, int side_num, const map_location& recall_hex);
00152 
00153     /** Creates a suppose-dead action for the current side */
00154     void save_suppose_dead(unit& curr_unit, map_location const& loc);
00155 
00156     /** Executes first action in the queue for current side */
00157     void contextual_execute();
00158     /** Executes all actions for the current turn in sequence
00159      *  @return true if the there are no more actions left for this turn when the method returns */
00160     bool execute_all_actions();
00161     /** Called by the game controller to let the whiteboard continue executing all actions
00162      *  if it stopped to wait for an attack to complete on reception of its random seed from server */
00163     void continue_execute_all();
00164     /** Deletes last action in the queue for current side */
00165     void contextual_delete();
00166     /** Moves the action determined by the UI toward the beginning of the queue  */
00167     void contextual_bump_up_action();
00168     /** Moves the action determined by the UI toward the beginning of the queue  */
00169     void contextual_bump_down_action();
00170 
00171     /** Get the highlight visitor instance in use by the manager */
00172     boost::weak_ptr<highlight_visitor> get_highlighter() { return highlighter_; }
00173 
00174     /** Checks whether the whiteboard has any planned action on any team */
00175     bool has_actions() const;
00176     /** Checks whether the specified unit has at least one planned action */
00177     bool unit_has_actions(unit const* unit) const;
00178 
00179     /** Used to track gold spending per-side when building the planned unit map
00180      *  Is referenced by the top bar gold display */
00181     int get_spent_gold_for(int side);
00182 
00183     /** Determines whether or not the undo_stack should be cleared.
00184      *  @todo Only when there are networked allies and we have set a preferences option */
00185     bool should_clear_undo() const {return true;}
00186     /** Updates shroud and clears the undo_stack and redo_stack. */
00187     void clear_undo();
00188 
00189     /** Displays the whiteboard options dialog. */
00190     void options_dlg();
00191 
00192 private:
00193     /** Transforms the unit map so that it now reflects the future state of things,
00194      *  i.e. when all planned actions will have been executed */
00195     void set_planned_unit_map();
00196     /** Restore the regular unit map */
00197     void set_real_unit_map();
00198 
00199     void validate_actions_if_needed();
00200     /** Called by all of the save_***() methods after they have added their action to the queue */
00201     void on_save_action(unit const* unit_with_plan) const;
00202     void update_plan_hiding(size_t viewing_team);
00203     void update_plan_hiding(); //same as above, but uses wb::viewer_team() as default argument
00204 
00205     /** Tracks whether the whiteboard is active. */
00206     bool active_;
00207     bool inverted_behavior_;
00208     bool self_activate_once_;
00209     bool print_help_once_;
00210     bool wait_for_side_init_;
00211     bool planned_unit_map_active_;
00212     /** Track whenever we're modifying actions, to avoid dual execution etc. */
00213     bool executing_actions_;
00214     /** Track whether we're in the process of executing all actions */
00215     bool executing_all_actions_;
00216     /** true if we're in the process of executing all action and should end turn once finished. */
00217     bool preparing_to_end_turn_;
00218     /** Track whether the gamestate changed and we need to validate actions. */
00219     bool gamestate_mutated_;
00220 
00221     /** Reference counted "lock" to allow preventing whiteboard activation state changes */
00222     whiteboard_lock activation_state_lock_;
00223     /** Reference counted "lock" to prevent the building of the unit map at certain times */
00224     whiteboard_lock unit_map_lock_;
00225 
00226 
00227     boost::scoped_ptr<mapbuilder> mapbuilder_;
00228     boost::shared_ptr<highlight_visitor> highlighter_;
00229 
00230     boost::scoped_ptr<pathfind::marked_route> route_;
00231 
00232     std::vector<arrow_ptr> move_arrows_;
00233     std::vector<fake_unit_ptr> fake_units_;
00234     size_t temp_move_unit_underlying_id_;
00235 
00236     boost::scoped_ptr<CKey> key_poller_;
00237 
00238     std::vector<map_location> hidden_unit_hexes_;
00239 
00240     ///net_buffer_[i] = whiteboard network data to be sent "from" teams[i].
00241     std::vector<config> net_buffer_;
00242 
00243     ///team_plans_hidden_[i] = whether or not to hide actions from teams[i].
00244     std::vector<bool> team_plans_hidden_;
00245 
00246     ///used to keep track of units owning planned moves for visual ghosting/unghosting
00247     std::set<size_t> units_owning_moves_;
00248 };
00249 
00250 /** Applies the planned unit map for the duration of the struct's life.
00251  *  Reverts to real unit map on destruction, unless planned unit map was already applied when the struct was created. */
00252 struct future_map
00253 {
00254     future_map();
00255     ~future_map();
00256     bool initial_planned_unit_map_;
00257 };
00258 
00259 /** ONLY IF whiteboard is currently active, applies the planned unit map for the duration of the struct's life.
00260  *  Reverts to real unit map on destruction, unless planned unit map was already applied when the struct was created. */
00261 struct future_map_if_active
00262 {
00263     future_map_if_active();
00264     ~future_map_if_active();
00265     bool initial_planned_unit_map_;
00266     bool whiteboard_active_;
00267 };
00268 
00269 /** Ensures that the real unit map is active for the duration of the struct's life.
00270  *  On destruction reverts to planned unit map if it was active when the struct was created. */
00271 struct real_map
00272 {
00273     real_map();
00274     ~real_map();
00275     bool initial_planned_unit_map_;
00276     whiteboard_lock unit_map_lock_;
00277 };
00278 
00279 } // end namespace wb
00280 
00281 #endif /* WB_MANAGER_HPP_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:39 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs