team.hpp

Go to the documentation of this file.
00001 /* $Id: team.hpp 54051 2012-04-30 21:52:38Z brilliand $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
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 #ifndef TEAM_H_INCLUDED
00016 #define TEAM_H_INCLUDED
00017 
00018 #include "color_range.hpp"
00019 #include "game_config.hpp"
00020 #include "savegame_config.hpp"
00021 #include "unit.hpp"
00022 
00023 class gamemap;
00024 
00025 namespace wb {
00026     class side_actions;
00027 }
00028 
00029 /**
00030  * This class stores all the data for a single 'side' (in game nomenclature).
00031  * E.g., there is only one leader unit per team.
00032  */
00033 class team : public savegame::savegame_config
00034 {
00035     class shroud_map {
00036     public:
00037         shroud_map() : enabled_(false), data_() {}
00038 
00039         void place(int x, int y);
00040         bool clear(int x, int y);
00041         void reset();
00042 
00043         bool value(int x, int y) const;
00044         bool shared_value(const std::vector<const shroud_map*>& maps, int x, int y) const;
00045 
00046         bool copy_from(const std::vector<const shroud_map*>& maps);
00047 
00048         std::string write() const;
00049         void read(const std::string& shroud_data);
00050         void merge(const std::string& shroud_data);
00051 
00052         bool enabled() const { return enabled_; }
00053         void set_enabled(bool enabled) { enabled_ = enabled; }
00054     private:
00055         bool enabled_;
00056         std::vector<std::vector<bool> > data_;
00057     };
00058 public:
00059 
00060     struct team_info
00061     {
00062         team_info();
00063         void read(const config &cfg);
00064         void write(config& cfg) const;
00065         std::string name;
00066         int gold;
00067         int start_gold;
00068         bool gold_add;
00069         int income;
00070         int income_per_village;
00071         int support_per_village;
00072         mutable int minimum_recruit_price;
00073         int recall_cost;
00074         std::set<std::string> can_recruit;
00075         std::string team_name;
00076         t_string user_team_name;
00077         std::string save_id;
00078         // 'id' of the current player (not necessarily unique)
00079         std::string current_player;
00080         std::string countdown_time;
00081         int action_bonus_count;
00082 
00083         std::string flag;
00084         std::string flag_icon;
00085 
00086         std::string description;
00087 
00088         bool scroll_to_leader;
00089 
00090         t_string objectives; /** < Team's objectives for the current level. */
00091 
00092         /** Set to true when the objectives for this time changes.
00093          * Reset to false when the objectives for this team have been
00094          * displayed to the user. */
00095         bool objectives_changed;
00096 
00097         enum CONTROLLER { HUMAN, HUMAN_AI, AI, NETWORK, NETWORK_AI, EMPTY };
00098         CONTROLLER controller;
00099         char const *controller_string() const;
00100 
00101         bool share_maps, share_view;
00102         bool disallow_observers;
00103         bool allow_player;
00104         bool no_leader;
00105         bool hidden;
00106 
00107         std::string music;
00108 
00109         std::string color;
00110 
00111         int side;
00112         bool persistent;
00113     };
00114 
00115     static std::map<int, color_range> team_color_range_;
00116     static const int default_team_gold;
00117 
00118     team();
00119     virtual ~team();
00120 
00121     void build(const config &cfg, const gamemap &map, int gold = default_team_gold);
00122 
00123     void write(config& cfg) const;
00124 
00125     bool get_village(const map_location&, const int owner_side, const bool fire_event = true);
00126     void lose_village(const map_location&);
00127     void clear_villages() { villages_.clear(); }
00128     const std::set<map_location>& villages() const { return villages_; }
00129     bool owns_village(const map_location& loc) const
00130         { return villages_.count(loc) > 0; }
00131 
00132     int side() const { return info_.side; }
00133     int gold() const { return gold_; }
00134     int start_gold() const { return info_.start_gold; }
00135     bool gold_add() const { return info_.gold_add; }
00136     int base_income() const { return info_.income + game_config::base_income; }
00137     int village_gold() const { return info_.income_per_village; }
00138     int recall_cost() const { return info_.recall_cost; }
00139     void set_village_gold(int income) { info_.income_per_village = income; }
00140     void set_recall_cost(int cost) { info_.recall_cost = cost; }
00141     int total_income() const { return base_income() + villages_.size() * info_.income_per_village; }
00142     /** @return The number of unit levels each village can support,
00143         i.e. how much upkeep each village can bear. */
00144     int village_support() const { return info_.support_per_village; }
00145     /** @param support The number of unit levels each village can support */
00146     void set_village_support(int support) { info_.support_per_village = support; }
00147     /** Calculate total support capacity, based on support_per_village. */
00148     int support() const { return villages_.size()*village_support(); }
00149     void new_turn() { gold_ += total_income(); }
00150     void get_shared_maps();
00151     void set_gold(int amount) { gold_ = amount; }
00152     void spend_gold(const int amount) { gold_ -= amount; }
00153     void set_gold_add(bool b) {info_.gold_add = b; }
00154     void set_base_income(int amount) { info_.income = amount - game_config::base_income; }
00155     int countdown_time() const {  return countdown_time_; }
00156     void set_countdown_time(const int amount)
00157         { countdown_time_ = amount; }
00158     int action_bonus_count() const { return action_bonus_count_; }
00159     void set_action_bonus_count(const int count) { action_bonus_count_ = count; }
00160     std::vector<unit>& recall_list() {return recall_list_;}
00161     const std::vector<unit>& recall_list() const {return recall_list_;}
00162     void set_current_player(const std::string& player)
00163         { info_.current_player = player; }
00164 
00165     bool get_scroll_to_leader() const {return info_.scroll_to_leader;}
00166 
00167     const std::set<std::string>& recruits() const
00168         { return info_.can_recruit; }
00169     void add_recruit(const std::string &);
00170     void set_recruits(const std::set<std::string>& recruits);
00171     int minimum_recruit_price() const;
00172     const std::string& name() const
00173         { return info_.name; }
00174     const std::string& save_id() const { return info_.save_id; }
00175     const std::string& current_player() const { return info_.current_player; }
00176 
00177     void set_objectives(const t_string& new_objectives, bool silently=false);
00178     void set_objectives_changed(bool c = true) { info_.objectives_changed = c; }
00179     void reset_objectives_changed() { info_.objectives_changed = false; }
00180 
00181     const t_string& objectives() const { return info_.objectives; }
00182     bool objectives_changed() const { return info_.objectives_changed; }
00183 
00184     bool is_enemy(int n) const {
00185         const size_t index = size_t(n-1);
00186         if(index < enemies_.size()) {
00187             return enemies_[index];
00188         } else {
00189             return calculate_enemies(index);
00190         }
00191     }
00192 
00193     team_info::CONTROLLER controller() const { return info_.controller; }
00194     char const *controller_string() const { return info_.controller_string(); }
00195     const std::string& color() const { return info_.color; }
00196     void set_color(const std::string& color) { info_.color = color; }
00197     bool is_human() const { return info_.controller == team_info::HUMAN; }
00198     bool is_human_ai() const { return info_.controller == team_info::HUMAN_AI; }
00199     bool is_network_human() const { return info_.controller == team_info::NETWORK; }
00200     bool is_network_ai() const { return info_.controller == team_info::NETWORK_AI; }
00201     bool is_ai() const { return info_.controller == team_info::AI || is_human_ai(); }
00202     bool is_empty() const { return info_.controller == team_info::EMPTY; }
00203 
00204     bool is_local() const { return is_human() || is_ai(); }
00205     bool is_network() const { return is_network_human() || is_network_ai(); }
00206 
00207     void make_human() { info_.controller = team_info::HUMAN; }
00208     void make_human_ai() { info_.controller = team_info::HUMAN_AI; }
00209     void make_network() { info_.controller = team_info::NETWORK; }
00210     void make_network_ai() { info_.controller = team_info::NETWORK_AI; }
00211     void make_ai() { info_.controller = team_info::AI; }
00212     // Should make the above make_*() functions obsolete, as it accepts controller
00213     // by lexical or numerical id
00214     void change_controller(team_info::CONTROLLER controller) { info_.controller = controller; }
00215     void change_controller(const std::string& controller);
00216 
00217     const std::string& team_name() const { return info_.team_name; }
00218     const t_string &user_team_name() const { return info_.user_team_name; }
00219     void change_team(const std::string &name, const t_string &user_name);
00220 
00221     const std::string& flag() const { return info_.flag; }
00222     const std::string& flag_icon() const { return info_.flag_icon; }
00223 
00224     //Returns true if the hex is shrouded/fogged for this side, or
00225     //any other ally with shared vision.
00226     bool shrouded(const map_location& loc) const;
00227     bool fogged(const map_location& loc) const;
00228 
00229     bool uses_shroud() const { return shroud_.enabled(); }
00230     bool uses_fog() const { return fog_.enabled(); }
00231     bool fog_or_shroud() const { return uses_shroud() || uses_fog(); }
00232     bool clear_shroud(const map_location& loc) { return shroud_.clear(loc.x+1,loc.y+1); }
00233     void place_shroud(const map_location& loc) { shroud_.place(loc.x+1,loc.y+1); }
00234     bool clear_fog(const map_location& loc) { return fog_.clear(loc.x+1,loc.y+1); }
00235     void reshroud() { shroud_.reset(); }
00236     void refog() { fog_.reset(); }
00237     void set_shroud(bool shroud) { shroud_.set_enabled(shroud); }
00238     void set_fog(bool fog) { fog_.set_enabled(fog); }
00239 
00240     /** Merge a WML shroud map with the shroud data of this player. */
00241     void merge_shroud_map_data(const std::string& shroud_data) { shroud_.merge(shroud_data); }
00242 
00243     bool knows_about_team(size_t index, bool is_multiplayer) const;
00244     bool copy_ally_shroud();
00245     /// Records hexes that were cleared of fog via WML.
00246     void add_fog_override(const std::set<map_location> &hexes) { fog_clearer_.insert(hexes.begin(), hexes.end()); }
00247     /// Removes the record of hexes that were cleared of fog via WML.
00248     void remove_fog_override(const std::set<map_location> &hexes);
00249 
00250     bool auto_shroud_updates() const { return auto_shroud_updates_; }
00251     void set_auto_shroud_updates(bool value) { auto_shroud_updates_ = value; }
00252     bool get_disallow_observers() const {return info_.disallow_observers; };
00253     bool no_leader() const { return info_.no_leader; }
00254     void have_leader(bool value=true) { info_.no_leader = !value; }
00255     bool hidden() const { return info_.hidden; }
00256     void set_hidden(bool value) { info_.hidden=value; }
00257     bool persistent() const {return info_.persistent;}
00258 
00259     static int nteams();
00260 
00261     //function which, when given a 1-based side will return the color used by that side.
00262     static const color_range get_side_color_range(int side);
00263     static Uint32 get_side_rgb(int side) { return(get_side_color_range(side).mid()); }
00264     static Uint32 get_side_rgb_max(int side) { return(get_side_color_range(side).max()); }
00265     static Uint32 get_side_rgb_min(int side) { return(get_side_color_range(side).min()); }
00266     static SDL_Color get_side_color(int side);
00267     static SDL_Color get_minimap_color(int side);
00268     static std::string get_side_color_index(int side);
00269     static std::string get_side_highlight(int side);
00270     static std::string get_side_highlight_pango(int side);
00271 
00272     void log_recruitable();
00273 
00274     /**set the share maps attribute */
00275     void set_share_maps( bool share_maps );
00276     /**set the share view attribute */
00277     void set_share_view( bool share_view );
00278 
00279     /** clear the shroud, fog, and enemies cache for all teams*/
00280     static void clear_caches();
00281 
00282     /** get the whiteboard planned actions for this team */
00283     boost::shared_ptr<wb::side_actions> get_side_actions() { return planned_actions_; }
00284 
00285     config to_config() const;
00286 
00287     bool share_maps() const { return info_.share_maps; }
00288     bool share_view() const { return info_.share_view; }
00289 private:
00290 
00291     const std::vector<const shroud_map*>& ally_shroud(const std::vector<team>& teams) const;
00292     const std::vector<const shroud_map*>& ally_fog(const std::vector<team>& teams) const;
00293 
00294     int gold_;
00295     std::set<map_location> villages_;
00296 
00297     shroud_map shroud_, fog_;
00298     /// Stores hexes that have been cleared of fog via WML.
00299     std::set<map_location> fog_clearer_;
00300 
00301     bool auto_shroud_updates_;
00302 
00303     team_info info_;
00304 
00305     int countdown_time_;
00306     int action_bonus_count_;
00307 
00308     std::vector<unit> recall_list_;
00309 
00310     bool calculate_enemies(size_t index) const;
00311     bool calculate_is_enemy(size_t index) const;
00312     mutable std::vector<bool> enemies_;
00313 
00314     mutable std::vector<const shroud_map*> ally_shroud_, ally_fog_;
00315 
00316     /**
00317      * Whiteboard planned actions for this team.
00318      */
00319     boost::shared_ptr<wb::side_actions> planned_actions_;
00320 };
00321 
00322 namespace teams_manager {
00323     const std::vector<team> &get_teams();
00324 }
00325 
00326 namespace player_teams {
00327     int village_owner(const map_location& loc);
00328 }
00329 
00330 //FIXME: this global method really needs to be moved into play_controller,
00331 //or somewhere else that makes sense.
00332 bool is_observer();
00333 
00334 //function which will validate a side. Throws game::game_error
00335 //if the side is invalid
00336 void validate_side(int side); //throw game::game_error
00337 
00338 #endif
00339 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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