multiplayer_connect.hpp

Go to the documentation of this file.
00001 /* $Id: multiplayer_connect.hpp 25333 2008-03-30 13:49:03Z jhinrichs $ */
00002 /*
00003    Copyright (C) 2007 - 2008
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 //! @file multiplayer_connect.hpp
00016 //!
00017 
00018 #ifndef MULTIPLAYER_CONNECT_H_INCLUDED
00019 #define MULTIPLAYER_CONNECT_H_INCLUDED
00020 
00021 #include "config.hpp"
00022 #include "gamestatus.hpp"
00023 #include "leader_list.hpp"
00024 #include "multiplayer_ui.hpp"
00025 #include "multiplayer_create.hpp"
00026 #include "network.hpp"
00027 #include "widgets/scrollpane.hpp"
00028 
00029 #include <string>
00030 
00031 namespace mp {
00032 
00033 class connect : public mp::ui
00034 {
00035 public:
00036     struct connected_user {
00037         connected_user(const std::string& name, mp::controller controller,
00038                 network::connection connection) :
00039             name(name), controller(controller), connection(connection)
00040         {};
00041         std::string name;
00042         mp::controller controller;
00043         network::connection connection;
00044     };
00045 
00046     typedef std::vector<connected_user> connected_user_list;
00047 
00048     class side {
00049     public:
00050         side(connect& parent, const config& cfg, int index);
00051 
00052         side(const side& a);
00053 
00054         void add_widgets_to_scrollpane(gui::scrollpane& pane, int pos);
00055 
00056         void process_event();
00057 
00058         //! Returns true if this side changed since last call to changed()
00059         bool changed();
00060 
00061         //! Gets a config object representing this side.
00062         //! If include_leader is set to true, the config objects include
00063         //! the "type=" defining the leader type, else it does not.
00064         config get_config() const;
00065 
00066         //! Returns true if this side is waiting for a network player
00067         //! and players allowed
00068         bool available() const;
00069 
00070         //! Return true if players are allowed to take this side
00071         bool allow_player() const;
00072 
00073         //! Sets the controller of a side.
00074         void set_controller(mp::controller controller);
00075         mp::controller get_controller() const;
00076 
00077         //! Adds an user to the user list combo
00078         void update_user_list();
00079 
00080         //! Returns the username of this side
00081         const std::string& get_id() const;
00082         //! Sets the username of this side
00083         void set_id(const std::string& id);
00084 
00085         const std::string& get_save_id() const;
00086 
00087         //! Imports data from the network into this side,
00088         //! and updates the UI accordingly.
00089         void import_network_user(const config& data);
00090 
00091         //! Resets this side to its default state, and updates the UI accordingly.
00092         void reset(mp::controller controller);
00093 
00094         //! Resolves the random leader / factions.
00095         void resolve_random();
00096         void hide_ai_algorithm_combo(bool invis);
00097     private:
00098         void init_ai_algorithm_combo();
00099         void update_ai_algorithm_combo() {hide_ai_algorithm_combo(parent_->hidden());}
00100         void update_controller_ui();
00101         void update_ui();
00102 
00103         //! The mp::connect widget owning this mp::connect::side.
00104         //! Used in the constructor, must be first.
00105         connect* parent_;
00106 
00107         //! A non-const config. Be careful not to insert keys when only reading.
00108         //! (Use cfg_.get_attribute().)
00109         config cfg_;
00110 
00111         // Configurable variables
00112         int index_;
00113         std::string id_;
00114         std::string save_id_;
00115         mp::controller controller_;
00116         int faction_;
00117         int team_;
00118         int colour_;
00119         int gold_;
00120         int income_;
00121         std::string leader_;
00122         std::string gender_;
00123         std::string ai_algorithm_;
00124         //bool taken_;
00125 
00126         // Widgets for this side
00127         gui::label player_number_;
00128         gui::combo combo_controller_;
00129         gui::label orig_controller_;
00130         gui::combo combo_ai_algorithm_;
00131         gui::combo combo_faction_;
00132         gui::combo combo_leader_;
00133         gui::combo combo_gender_;
00134         gui::combo combo_team_;
00135         gui::combo combo_colour_;
00136         gui::slider slider_gold_;
00137         gui::slider slider_income_;
00138         gui::label label_gold_;
00139         gui::label label_income_;
00140 
00141         bool allow_player_;
00142         bool enabled_;
00143         bool changed_;
00144         leader_list_manager llm_;
00145     };
00146 
00147     friend class side;
00148 
00149     typedef std::vector<side> side_list;
00150 
00151 
00152     connect(game_display& disp, const config& game_config,
00153             chat& c, config& gamelist, const create::parameters& params,
00154             mp::controller default_controller);
00155 
00156     virtual void process_event();
00157 
00158     /** Returns the game state, which contains all information
00159      * about the current scenario.
00160      */
00161     const game_state& get_state();
00162 
00163     /** Updates the current game state, resolves random factions,
00164      * and sends a "start game" message to the network.
00165      */
00166     void start_game();
00167 
00168 protected:
00169     virtual void layout_children(const SDL_Rect& rect);
00170 
00171     virtual void process_network_data(const config& data, const network::connection sock);
00172     virtual void process_network_error(network::error& error);
00173     virtual bool accept_connections();
00174     virtual void process_network_connection(const network::connection sock);
00175 
00176     virtual void hide_children(bool hide=true);
00177 
00178 private:
00179     // Those 2 functions are actually the steps of the (complex)
00180     // construction of this class.
00181     void load_game();
00182     void lists_init();
00183 
00184     // Convenience function
00185     config* current_config();
00186 
00187     //! Updates the level_ variable to reflect the sides in the sides_ vector
00188     void update_level();
00189 
00190     //! Updates the level, and send a diff to the clients.
00191     void update_and_send_diff(bool update_time_of_day = false);
00192 
00193     //! Returns true if there still are sides available for this game.
00194     bool sides_available();
00195 
00196     //! Updates the state of the player list,
00197     //! the launch button and of the start game label,
00198     //! to reflect the actual state.
00199     void update_playerlist_state(bool silent=true);
00200 
00201     //! Returns the index of a player, from its id, or -1 if the player was not found.
00202     connected_user_list::iterator find_player(const std::string& id);
00203 
00204     //! Returns the side which is taken by a given player, or -1 if none was found.
00205     int find_player_side(const std::string& id) const;
00206 
00207     //! Adds a player.
00208     void update_user_combos();
00209 
00210     //! Removes a player and kicks it from the game.
00211     void kick_player(const std::string& name);
00212 
00213     config level_;
00214 
00215     //! This is the "game state" object which is created by this dialog.
00216     game_state state_;
00217 
00218     create::parameters params_;
00219 
00220     //! The list of available sides for the current era.
00221     config::child_list era_sides_;
00222 
00223     // Lists used for combos
00224     std::vector<std::string> player_types_;
00225     std::vector<std::string> player_factions_;
00226     std::vector<std::string> player_teams_;
00227     std::vector<std::string> player_colours_;
00228     std::vector<std::string> ai_algorithms_;
00229 
00230     // team_name list and "Team" prefix
00231     std::vector<std::string> team_names_;
00232     std::vector<std::string> user_team_names_;
00233     const std::string team_prefix_;
00234 
00235     side_list sides_;
00236     connected_user_list users_;
00237 
00238     gui::label waiting_label_;
00239     bool message_full_;
00240 
00241     controller default_controller_;
00242 
00243     // Widgets
00244     gui::scrollpane scroll_pane_;
00245 
00246     gui::label type_title_label_;
00247     gui::label faction_title_label_;
00248     gui::label team_title_label_;
00249     gui::label colour_title_label_;
00250     gui::label gold_title_label_;
00251     gui::label income_title_label_;
00252 
00253     gui::button launch_;
00254     gui::button cancel_;
00255 
00256 }; // end class connect
00257 
00258 } // end namespace mp
00259 
00260 #endif
00261 

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