team.hpp

Go to the documentation of this file.
00001 /* $Id: team.hpp 26055 2008-04-24 01:11:10Z alink $ */
00002 /*
00003    Copyright (C) 2003 - 2008 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 version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 #ifndef TEAM_H_INCLUDED
00015 #define TEAM_H_INCLUDED
00016 
00017 #include "config.hpp"
00018 #include "color_range.hpp"
00019 #include "game_config.hpp"
00020 #include "map.hpp"
00021 
00022 struct time_of_day;
00023 
00024 #include <set>
00025 #include <string>
00026 #include <vector>
00027 
00028 #include "SDL.h"
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 class team : public viewpoint
00033 {
00034     class shroud_map {
00035     public:
00036         shroud_map() : enabled_(false), data_() {}
00037 
00038         void place(size_t x, size_t y);
00039         bool clear(size_t x, size_t y);
00040         void reset();
00041 
00042         bool value(size_t x, size_t y) const;
00043         bool shared_value(const std::vector<const shroud_map*>& maps, size_t x, size_t y) const;
00044 
00045         bool copy_from(const std::vector<const shroud_map*>& maps);
00046 
00047         std::string write() const;
00048         void read(const std::string& shroud_data);
00049         void merge(const std::string& shroud_data);
00050 
00051         bool enabled() const { return enabled_; }
00052         void set_enabled(bool enabled) { enabled_ = enabled; }
00053     private:
00054         bool enabled_;
00055         std::vector<std::vector<bool> > data_;
00056     };
00057 public:
00058 
00059     struct target {
00060         explicit target(const config& cfg);
00061         void write(config& cfg) const;
00062         config criteria;
00063         double value;
00064     };
00065 
00066     struct team_info
00067     {
00068         team_info(const config& cfg);
00069         void write(config& cfg) const;
00070         std::string name;
00071         std::string gold;
00072         std::string start_gold;
00073         std::string income;
00074         int income_per_village;
00075         std::set<std::string> can_recruit;
00076         std::vector<std::string> global_recruitment_pattern;
00077         std::vector<std::string> recruitment_pattern;
00078         std::vector<int> enemies;
00079         std::string team_name;
00080         std::string user_team_name;
00081         std::string save_id;
00082         // 'id' of the current player (not necessarily unique)
00083         std::string current_player;
00084         std::string countdown_time;
00085         int action_bonus_count;
00086 
00087         std::string flag;
00088         std::string flag_icon;
00089 
00090         std::string description;
00091 
00092         t_string objectives; /** < Team's objectives for the current level. */
00093 
00094         /** Set to true when the objectives for this time changes.
00095          * Reset to false when the objectives for this team have been
00096          * displayed to the user. */
00097         bool objectives_changed;
00098 
00099         enum CONTROLLER { HUMAN, AI, NETWORK, EMPTY };
00100         CONTROLLER controller;
00101         bool persistent;
00102         std::string ai_algorithm;
00103 
00104         std::vector<config> ai_params;
00105         config ai_memory_;
00106 
00107         int villages_per_scout;
00108         double leader_value, village_value;
00109 
00110         std::vector<target> targets;
00111 
00112         bool share_maps, share_view;
00113         bool disallow_observers;
00114         bool allow_player;
00115         bool no_leader;
00116 
00117         std::string music;
00118 
00119         std::string colour;
00120     };
00121 
00122     static std::map<int, color_range> team_color_range_;
00123     static const int default_team_gold;
00124     team(const config& cfg, int gold=default_team_gold);
00125 
00126     ~team() {};
00127 
00128     void write(config& cfg) const;
00129 
00130     bool get_village(const gamemap::location&);
00131     void lose_village(const gamemap::location&);
00132     void clear_villages() { villages_.clear(); }
00133     const std::set<gamemap::location>& villages() const { return villages_; }
00134     bool owns_village(const gamemap::location& loc) const
00135         { return villages_.count(loc) > 0; }
00136 
00137     int gold() const { return gold_; }
00138     std::string start_gold() const { return info_.start_gold; }
00139     int base_income() const
00140         { return atoi(info_.income.c_str()) + game_config::base_income; }
00141     int village_gold() const { return info_.income_per_village; }
00142     void set_village_gold(int income) { info_.income_per_village = income; }
00143     int income() const
00144         { return atoi(info_.income.c_str()) + villages_.size()*info_.income_per_village+game_config::base_income; }
00145     void new_turn() { gold_ += income(); }
00146     void set_time_of_day(int turn, const struct time_of_day& tod);
00147     void get_shared_maps();
00148     void spend_gold(const int amount) { gold_ -= amount; }
00149     void set_income(const int amount)
00150         { info_.income = lexical_cast<std::string>(amount); }
00151     int countdown_time() const {  return countdown_time_; }
00152     void set_countdown_time(const int amount)
00153         { countdown_time_ = amount; }
00154     int action_bonus_count() const { return action_bonus_count_; }
00155     void set_action_bonus_count(const int count) { action_bonus_count_ = count; }
00156     void set_current_player(const std::string player)
00157         { info_.current_player = player; }
00158 
00159     const std::set<std::string>& recruits() const
00160         { return info_.can_recruit; }
00161     std::set<std::string>& recruits() { return info_.can_recruit; }
00162     const std::vector<std::string>& recruitment_pattern() const
00163         { return info_.recruitment_pattern; }
00164     const std::string& name() const
00165         { return info_.name; }
00166     const std::string& save_id() const { return info_.save_id; }
00167     const std::string& current_player() const { return info_.current_player; }
00168 
00169     void set_objectives(const t_string& new_objectives, bool silently=false);
00170     void reset_objectives_changed() { info_.objectives_changed = false; }
00171 
00172     const t_string& objectives() const { return info_.objectives; }
00173     bool objectives_changed() const { return info_.objectives_changed; }
00174 
00175     bool is_enemy(int n) const {
00176         const size_t index = size_t(n-1);
00177         if(index < enemies_.size()) {
00178             return enemies_[index];
00179         } else {
00180             return calculate_enemies(index);
00181         }
00182     }
00183 
00184     bool has_seen(unsigned int index) const {
00185         if(!uses_shroud() && !uses_fog()) return true;
00186         if(index < seen_.size()) {
00187             return seen_[index];
00188         } else {
00189             return false;
00190         }
00191     }
00192     void see(unsigned int index) {
00193         if(index >= seen_.size()) {
00194             seen_.resize(index+1);
00195         }
00196         seen_[index] = true;
00197     }
00198 
00199     double aggression() const { return aggression_; }
00200     double caution() const { return caution_; }
00201 
00202     team_info::CONTROLLER controller() const { return info_.controller; }
00203     bool is_human() const { return info_.controller == team_info::HUMAN; }
00204     bool is_network() const { return info_.controller == team_info::NETWORK; }
00205     bool is_ai() const { return info_.controller == team_info::AI; }
00206     bool is_empty() const { return info_.controller == team_info::EMPTY; }
00207 
00208     bool is_persistent() const { return info_.persistent; }
00209 
00210     void make_human() { info_.controller = team_info::HUMAN; }
00211     void make_network() { info_.controller = team_info::NETWORK; }
00212     void make_ai() { info_.controller = team_info::AI; }
00213     // Should make the above make_*() functions obsolete, as it accepts controller
00214     // by lexical or numerical id
00215     void change_controller(team_info::CONTROLLER controller) { info_.controller = controller; }
00216     void change_controller(const std::string& controller);
00217 
00218     const std::string& team_name() const { return info_.team_name; }
00219     const std::string& user_team_name() const { return info_.user_team_name; }
00220     void change_team(const std::string& name,
00221                      const std::string& user_name);
00222 
00223     const std::string& flag() const { return info_.flag; }
00224     const std::string& flag_icon() const { return info_.flag_icon; }
00225 
00226     const std::string& ai_algorithm() const { return info_.ai_algorithm; }
00227     const config& ai_parameters() const { return aiparams_; }
00228     const config& ai_memory() const { return info_.ai_memory_; }
00229     void set_ai_memory(const config& ai_mem);
00230     void set_ai_parameters(const config::child_list& ai_parameters);
00231 
00232     double leader_value() const { return info_.leader_value; }
00233     void set_leader_value(double value) { info_.leader_value = value; }
00234     double village_value() const { return info_.village_value; }
00235     void set_village_value(double value) { info_.village_value = value; }
00236 
00237     int villages_per_scout() const { return info_.villages_per_scout; }
00238 
00239     std::vector<target>& targets() { return info_.targets; }
00240 
00241     //Returns true if the hex is shrouded/fogged for this side, or
00242     //any other ally with shared vision.
00243     bool shrouded(const gamemap::location& loc) const;
00244     bool fogged(const gamemap::location& loc) const;
00245 
00246     bool uses_shroud() const { return shroud_.enabled(); }
00247     bool uses_fog() const { return fog_.enabled(); }
00248     bool fog_or_shroud() const { return uses_shroud() || uses_fog(); }
00249     bool clear_shroud(const gamemap::location& loc) { return shroud_.clear(loc.x+1,loc.y+1); }
00250     void place_shroud(const gamemap::location& loc) { shroud_.place(loc.x+1,loc.y+1); }
00251     bool clear_fog(const gamemap::location& loc) { return fog_.clear(loc.x+1,loc.y+1); }
00252     void refog() { fog_.reset(); }
00253     void set_shroud(bool shroud) { shroud_.set_enabled(shroud); }
00254     void set_fog(bool fog) { fog_.set_enabled(fog); }
00255 
00256     //! Merge a WML shroud map with the shroud data of this player.
00257     void merge_shroud_map_data(const std::string& shroud_data);
00258 
00259     bool knows_about_team(size_t index) const;
00260     bool copy_ally_shroud();
00261 
00262     bool auto_shroud_updates() const { return auto_shroud_updates_; }
00263     void set_auto_shroud_updates(bool value) { auto_shroud_updates_ = value; }
00264     bool get_disallow_observers() {return info_.disallow_observers; };
00265     std::string map_colour_to() const { return info_.colour; };
00266     bool& no_leader()  { return info_.no_leader; }
00267 
00268     static int nteams();
00269 
00270     //function which, when given a 1-based side will return the colour used by that side.
00271     static const color_range get_side_color_range(int side);
00272     static Uint32 get_side_rgb(int side) { return(get_side_color_range(side).mid()); }
00273     static Uint32 get_side_rgb_max(int side) { return(get_side_color_range(side).max()); }
00274     static Uint32 get_side_rgb_min(int side) { return(get_side_color_range(side).min()); }
00275     static const SDL_Color get_minimap_colour(int side);
00276     static std::string get_side_colour_index(int side);
00277     static std::string get_side_highlight(int side);
00278 
00279     void log_recruitable();
00280 
00281 private:
00282     //Make these public if you need them, but look at knows_about_team(...) first.
00283     bool share_maps() const { return info_.share_maps; }
00284     bool share_view() const { return info_.share_view; }
00285 
00286     const std::vector<const shroud_map*>& ally_shroud(const std::vector<team>& teams) const;
00287     const std::vector<const shroud_map*>& ally_fog(const std::vector<team>& teams) const;
00288 
00289     int gold_;
00290     std::set<gamemap::location> villages_;
00291 
00292     shroud_map shroud_, fog_;
00293 
00294     bool auto_shroud_updates_;
00295 
00296     team_info info_;
00297 
00298     int countdown_time_;
00299     int action_bonus_count_;
00300 
00301     config aiparams_;
00302 
00303     //cached values for ai parameters
00304     double aggression_, caution_;
00305 
00306     bool calculate_enemies(size_t index) const;
00307     bool calculate_is_enemy(size_t index) const;
00308     mutable std::vector<bool> enemies_;
00309 
00310     mutable std::vector<bool> seen_;
00311 
00312     mutable std::vector<const shroud_map*> ally_shroud_, ally_fog_;
00313 };
00314 
00315 struct teams_manager {
00316     teams_manager(std::vector<team>& teams);
00317     ~teams_manager();
00318 
00319     bool is_observer();
00320 };
00321 
00322 namespace player_teams {
00323     int village_owner(const gamemap::location& loc);
00324 }
00325 
00326 bool is_observer();
00327 
00328 //function which will validate a side. Trows game::game_error
00329 //if the side is invalid
00330 void validate_side(int side); //throw game::game_error
00331 
00332 #endif
00333 

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs