game.hpp

Go to the documentation of this file.
00001 /* $Id: game.hpp 25613 2008-04-06 08:00:37Z martinxyz $ */
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 
00015 #ifndef GAME_HPP_INCLUDED
00016 #define GAME_HPP_INCLUDED
00017 
00018 #include "../network.hpp"
00019 #include "player.hpp"
00020 
00021 #include "simple_wml.hpp"
00022 
00023 #include <map>
00024 #include <vector>
00025 
00026 //class player;
00027 
00028 typedef std::map<network::connection,player> player_map;
00029 typedef std::vector<network::connection> user_vector;
00030 typedef std::vector<network::connection> side_vector;
00031 
00032 class game
00033 {
00034 public:
00035     game(player_map& players, const network::connection host=0, const std::string name="");
00036     ~game();
00037 
00038     int id() const { return id_; }
00039     const std::string& name() const { return name_; }
00040 
00041     bool is_owner(const network::connection player) const { return (player == owner_); }
00042     bool is_member(const network::connection player) const
00043     { return is_player(player) || is_observer(player); }
00044     bool allow_observers() const;
00045     bool is_observer(const network::connection player) const;
00046     bool is_player(const network::connection player) const;
00047     bool player_is_banned(const network::connection player) const;
00048     bool level_init() const { return level_.child("side") != NULL; }
00049     bool started() const { return started_; }
00050 
00051     size_t nplayers() const { return players_.size(); }
00052     size_t nobservers() const { return observers_.size(); }
00053     size_t current_turn() const { return (nsides_ ? end_turn_ / nsides_ + 1 : 0); }
00054 
00055     void mute_all_observers();
00056     //! Mute an observer by name.
00057     void mute_observer(const simple_wml::node& mute, const player_map::const_iterator muter);
00058     //! Kick a member by name.
00059     network::connection kick_member(const simple_wml::node& kick, const player_map::const_iterator kicker);
00060     //! Ban and kick a user by name. He doesn't need to be in this game.
00061     network::connection ban_user(const simple_wml::node& ban, const player_map::const_iterator banner);
00062 
00063     void add_player(const network::connection player, bool observer = false);
00064     bool remove_player(const network::connection player, const bool disconnect=false);
00065 
00066     //! Adds players and observers into one vector and returns that.
00067     const user_vector all_game_users() const;
00068     //! Adds players from one game to another. This is used to add players and
00069     //! observers from a game to the lobby (which is also implemented as a game),
00070     //! if that game ends. The second parameter controls, wether the players are
00071     //! added to the players_ or observers_ vector (default observers_).
00072     void add_players(const game& other_game, const bool observer = true);
00073 
00074     void start_game(const player_map::const_iterator starter);
00075     //! A user (player only?) asks for the next scenario to advance to.
00076     void load_next_scenario(const player_map::const_iterator user) const;
00077 
00078     //! Resets the side configuration according to the scenario data.
00079     void update_side_data();
00080     //! Let's a player owning a side give it to another player or observer.
00081     void transfer_side_control(const network::connection sock, const simple_wml::node& cfg);
00082 
00083     void process_message(simple_wml::document& data, const player_map::iterator user);
00084     //! Filters and processes (some) commands.
00085     //! Returns true iff the turn ended.
00086     bool process_turn(simple_wml::document& data, const player_map::const_iterator user);
00087     //! Set the description to the number of available slots.
00088     //! Returns true iff the number of slots has changed.
00089     bool describe_slots();
00090 
00091     void send_server_message_to_all(const char* message, network::connection exclude=0) const;
00092     void send_server_message(const char* message, network::connection sock=0, simple_wml::document* doc=NULL) const;
00093     //! Send data to all players in this game except 'exclude'.
00094     void send_and_record_server_message(const char* message,
00095             const network::connection exclude=0);
00096     void send_data(simple_wml::document& data, const network::connection exclude=0) const;
00097     void send_to_one(simple_wml::document& data, const network::connection sock) const;
00098 
00099     void record_data(simple_wml::document* data);
00100 
00101     //! The full scenario data.
00102     simple_wml::document& level() { return level_; }
00103 
00104     //! Functions to set/get the address of the game's summary description as
00105     //! sent to players in the lobby.
00106     void set_description(simple_wml::node* desc);
00107     simple_wml::node* description() const { return description_; }
00108 
00109     void set_password(const std::string& passwd) { password_ = passwd; }
00110     bool password_matches(const std::string& passwd) const {
00111         return password_.empty() || passwd == password_;
00112     }
00113 
00114     const std::string& termination_reason() const {
00115         static const std::string aborted = "aborted";
00116         static const std::string not_started = "not started";
00117         return started_ ? (termination_.empty() ? aborted : termination_) : not_started;
00118     }
00119 
00120     void set_termination_reason(const std::string& reason);
00121 
00122 private:
00123     //forbidden operations
00124     game(const game&);
00125     void operator=(const game&);
00126 
00127     size_t current_side() const { return (nsides_ ? end_turn_ % nsides_ : 0); }
00128     network::connection current_player() const
00129     { return (nsides_ ? sides_[current_side()] : 0); }
00130     bool is_current_player(const network::connection player) const
00131     { return (current_player() == player); }
00132     bool is_muted_observer(const network::connection player) const;
00133     bool all_observers_muted() const { return all_observers_muted_; }
00134 
00135     //! Figures out which side to take and tells that side to the game owner.
00136     bool take_side(const player_map::const_iterator user);
00137     //! Send [change_controller] message to tell all clients the new controller's name.
00138     void send_change_controller(const size_t side_num,
00139             const player_map::const_iterator newplayer,
00140             const bool player_left=true);
00141     void transfer_ai_sides();
00142     void send_data_team(simple_wml::document& data, const simple_wml::string_span& team,
00143             const network::connection exclude=0) const;
00144     void send_data_observers(simple_wml::document& data, const network::connection exclude=0) const;
00145     //! Send [observer] tags of all the observers in the game to the user or
00146     //! everyone if none given.
00147     void send_observerjoins(const network::connection sock=0) const;
00148     void send_observerquit(const player_map::const_iterator observer) const;
00149     void send_history(const network::connection sock) const;
00150     //! In case of a host transfer, notify the new host about its status.
00151     void notify_new_host();
00152     //! Convenience function for finding a user by name.
00153     player_map::const_iterator find_user(const simple_wml::string_span& name) const;
00154 
00155     bool observers_can_label() const { return false; }
00156     bool observers_can_chat() const { return true; }
00157     bool is_legal_command(const simple_wml::node& command, bool is_player);
00158     //! Function which returns true iff 'player' is on 'team'.
00159     bool is_on_team(const simple_wml::string_span& team, const network::connection player) const;
00160 
00161     //! Function which should be called every time a player ends their turn
00162     //! (i.e. [end_turn] received). This will update the 'turn' attribute for
00163     //! the game's description when appropriate. Will return true iff there has
00164     //! been a change.
00165     bool end_turn();
00166 
00167     //! Function to send a list of users to all clients.
00168     //! Only sends data if the game is initialized but not yet started.
00169     void send_user_list(const network::connection exclude=0) const;
00170 
00171     //! Helps debugging player and observer lists.
00172     std::string debug_player_info() const;
00173 
00174     player_map* player_info_;
00175 
00176     static int id_num;
00177     int id_;
00178     //! The name of the game.
00179     std::string name_;
00180     std::string password_;
00181     //! The game host or later owner (if the host left).
00182     network::connection owner_;
00183     //! A vector of players (members owning a side).
00184     user_vector players_;
00185     //! A vector of observers (members not owning a side).
00186     user_vector observers_;
00187     user_vector muted_observers_;
00188     //! A vector of side owners.
00189     side_vector sides_;
00190     //! A vector indicating what sides are actually taken. (Really needed?)
00191     std::vector<bool> sides_taken_;
00192     //! A vector of controller strings indicating the type.
00193     //! "network" - a side controlled by some member of the game (not the owner)
00194     //! "human"   - a side controlled by the owner
00195     //! "ai"      - a side of the owner controlled by an AI
00196     //! "null"    - an empty side
00197     std::vector<std::string> side_controllers_;
00198     
00199     //! Number of sides in the current scenario.
00200     int nsides_;
00201     bool started_;
00202 
00203     //! The current scenario data.
00204     simple_wml::document level_;
00205     //! Replay data.
00206     mutable std::vector<simple_wml::document*> history_;
00207     //! Pointer to the game's description in the games_and_users_list_.
00208     simple_wml::node* description_;
00209 
00210     int end_turn_;
00211 
00212     bool all_observers_muted_;
00213 
00214     std::vector<std::string> bans_;
00215 
00216     std::string termination_;
00217 };
00218 
00219 struct game_id_matches {
00220     game_id_matches(int id) : id_(id) {}
00221     bool operator()(const game* g) const { return g->id() == id_; }
00222 
00223 private:
00224     int id_;
00225 };
00226 
00227 #endif

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