multiplayer_connect.hpp

Go to the documentation of this file.
00001 /* $Id: multiplayer_connect.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2007 - 2012
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 /** @file */
00017 
00018 #ifndef MULTIPLAYER_CONNECT_H_INCLUDED
00019 #define MULTIPLAYER_CONNECT_H_INCLUDED
00020 
00021 #include "gamestatus.hpp"
00022 #include "leader_list.hpp"
00023 #include "multiplayer_ui.hpp"
00024 #include "widgets/scrollpane.hpp"
00025 #include "widgets/slider.hpp"
00026 #include "widgets/combo_drag.hpp"
00027 
00028 namespace ai {
00029     struct description;
00030 }
00031 
00032 namespace mp {
00033 
00034 class connect : public mp::ui
00035 {
00036 public:
00037     struct connected_user {
00038         connected_user(const std::string& name, mp::controller controller,
00039                 network::connection connection) :
00040             name(name), controller(controller), connection(connection)
00041         {};
00042         std::string name;
00043         mp::controller controller;
00044         network::connection connection;
00045         operator std::string() const
00046         {
00047             return name;
00048         }
00049     };
00050 
00051     typedef std::vector<connected_user> connected_user_list;
00052 
00053     class side {
00054     public:
00055         side(connect& parent, const config& cfg, int index);
00056 
00057         side(const side& a);
00058 
00059         void add_widgets_to_scrollpane(gui::scrollpane& pane, int pos);
00060 
00061         void process_event();
00062 
00063         /** Returns true if this side changed since last call to changed(). */
00064         bool changed();
00065 
00066         /**
00067          * Gets a config object representing this side.
00068          *
00069          * If include_leader is set to true, the config objects include the
00070          * "type=" defining the leader type, else it does not.
00071          */
00072         config get_config() const;
00073 
00074         /**
00075          * Returns true if this side is waiting for a network player and
00076          * players allowed.
00077          */
00078         bool available(const std::string& name = "") const;
00079 
00080         /** Returns true, if the player has chosen his leader and this side is ready for the game to start */
00081         bool ready_for_start() const;
00082 
00083         /** Return true if players are allowed to take this side. */
00084         bool allow_player() const;
00085 
00086         /** Sets the controller of a side. */
00087         void set_controller(mp::controller controller);
00088         mp::controller get_controller() const;
00089 
00090         /** Adds an user to the user list combo. */
00091         void update_user_list();
00092 
00093         /** Returns the username of this side. */
00094         const std::string& get_current_player() const
00095             { return current_player_; }
00096 
00097         int get_index();
00098 
00099         void set_index(int index);
00100 
00101         const std::string& get_player_id() const;
00102 
00103         /** Sets the username of this side. */
00104         void set_player_id(const std::string& player_id);
00105 
00106         /** Sets if the joining player has chosen his leader. */
00107         void set_ready_for_start(bool ready_for_start);
00108 
00109         int get_team();
00110 
00111         void set_team(int team);
00112 
00113         std::vector<std::string> get_children_to_swap();
00114 
00115         std::map<std::string, config> get_side_children();
00116 
00117         void set_side_children(std::map<std::string, config> children);
00118 
00119         /**
00120          * Imports data from the network into this side, and updates the UI
00121          * accordingly.
00122          */
00123         void import_network_user(const config& data);
00124 
00125         /** Resets this side to its default state, and updates the UI accordingly. */
00126         void reset(mp::controller controller);
00127 
00128         /** Resolves the random leader / factions. */
00129         void resolve_random();
00130         void hide_ai_algorithm_combo(bool invis);
00131     private:
00132         void init_ai_algorithm_combo();
00133         void update_ai_algorithm_combo() {hide_ai_algorithm_combo(parent_->hidden());}
00134 
00135         /** Fill or refresh the faction combo using the proper team color. */
00136         void update_faction_combo();
00137         void update_controller_ui();
00138         void update_ui();
00139 
00140         /**
00141          * The mp::connect widget owning this mp::connect::side.
00142          *
00143          * Used in the constructor, must be first.
00144          */
00145         connect* parent_;
00146 
00147         /**
00148          * A non-const config. Be careful not to insert keys when only reading.
00149          *
00150          * (Use cfg_.get_attribute().)
00151          */
00152         config cfg_;
00153 
00154         // Configurable variables
00155         int index_;
00156         std::string id_;
00157         std::string player_id_;
00158         std::string save_id_;
00159         std::string current_player_;
00160         mp::controller controller_;
00161         int faction_;
00162         int team_;
00163         int color_;
00164         int gold_;
00165         int income_;
00166         std::string leader_;
00167         std::string gender_;
00168         std::string ai_algorithm_;
00169         bool ready_for_start_;
00170 
00171         // Flags for controlling the dialog widgets of the game lobby
00172         bool gold_lock_;
00173         bool income_lock_;
00174         bool team_lock_;
00175         bool color_lock_;
00176 
00177         // Widgets for this side
00178         gui::label player_number_;
00179         gui::combo_drag_ptr combo_controller_;
00180         gui::label orig_controller_;
00181         gui::combo combo_ai_algorithm_;
00182         gui::combo combo_faction_;
00183         gui::combo combo_leader_;
00184         gui::combo combo_gender_;
00185         gui::combo combo_team_;
00186         gui::combo combo_color_;
00187         gui::slider slider_gold_;
00188         gui::slider slider_income_;
00189         gui::label label_gold_;
00190         gui::label label_income_;
00191 
00192         bool allow_player_;
00193         bool allow_changes_;
00194         bool enabled_;
00195         bool changed_;
00196         leader_list_manager llm_;
00197     };
00198 
00199     friend class side;
00200 
00201     typedef std::vector<side> side_list;
00202 
00203     /**
00204      * Pointer to the display
00205      */
00206     game_display* disp_;
00207 
00208     connect(game_display& disp, const config& game_config, chat& c,
00209             config& gamelist, const mp_game_settings& params, const int num_turns,
00210             mp::controller default_controller, bool local_players_only = false);
00211 
00212     virtual void process_event();
00213 
00214     void take_reserved_side(connect::side& side, const config& data);
00215 
00216     /**
00217      * Returns the game state, which contains all information about the current
00218      * scenario.
00219      */
00220     const game_state& get_state();
00221 
00222     /**
00223      * Updates the current game state, resolves random factions, and sends a
00224      * "start game" message to the network.
00225      */
00226     void start_game();
00227 
00228 protected:
00229     virtual void layout_children(const SDL_Rect& rect);
00230 
00231     virtual void process_network_data(const config& data, const network::connection sock);
00232     virtual void process_network_error(network::error& error);
00233     virtual bool accept_connections();
00234     virtual void process_network_connection(const network::connection sock);
00235 
00236     virtual void hide_children(bool hide=true);
00237 
00238 private:
00239     // Those 2 functions are actually the steps of the (complex)
00240     // construction of this class.
00241 
00242     /**
00243      * Called by the constructor to initialize the game from a
00244      * create::parameters structure.
00245      */
00246     void load_game();
00247     void lists_init();
00248 
00249     /** Convenience function. */
00250     config* current_config();
00251 
00252     /** Updates the level_ variable to reflect the sides in the sides_ vector. */
00253     void update_level();
00254 
00255     /** Updates the level, and send a diff to the clients. */
00256     void update_and_send_diff(bool update_time_of_day = false);
00257 
00258     /** Returns true if there still are sides available for this game. */
00259     bool sides_available() const;
00260 
00261     /** Returns true if all sides are ready to start the game. */
00262     bool sides_ready() const;
00263 
00264     /**
00265      * Validates whether the game can be started.
00266      *
00267      * returns                       Can the game be started?
00268      */
00269     bool can_start_game() const;
00270 
00271     /**
00272      * Updates the state of the player list, the launch button and of the start
00273      * game label, to reflect the actual state.
00274      */
00275     void update_playerlist_state(bool silent=true);
00276 
00277     /** Returns the index of a player, from its id, or -1 if the player was not found. */
00278     connected_user_list::iterator find_player(const std::string& id);
00279 
00280     /** Returns the side which is taken by a given player, or -1 if none was found. */
00281     int find_player_side(const std::string& id) const;
00282 
00283     /** Adds a player. */
00284     void update_user_combos();
00285 
00286     bool local_only_;
00287 
00288     config level_;
00289 
00290     /** This is the "game state" object which is created by this dialog. */
00291     game_state state_;
00292 
00293     mp_game_settings params_;
00294     int num_turns_;
00295 
00296     /** The list of available sides for the current era. */
00297     std::vector<const config *> era_sides_;
00298 
00299     // Lists used for combos
00300     std::vector<std::string> player_types_;
00301     std::vector<std::string> player_factions_;
00302     std::vector<std::string> player_teams_;
00303     std::vector<std::string> player_colors_;
00304     std::vector<ai::description*> ai_algorithms_;
00305 
00306     // team_name list and "Team" prefix
00307     std::vector<std::string> team_names_;
00308     std::vector<std::string> user_team_names_;
00309     const std::string team_prefix_;
00310 
00311     side_list sides_;
00312     connected_user_list users_;
00313 
00314     gui::label waiting_label_;
00315     bool message_full_;
00316 
00317     controller default_controller_;
00318 
00319     // Widgets
00320     gui::scrollpane scroll_pane_;
00321 
00322     gui::label type_title_label_;
00323     gui::label faction_title_label_;
00324     gui::label team_title_label_;
00325     gui::label color_title_label_;
00326     gui::label gold_title_label_;
00327     gui::label income_title_label_;
00328 
00329     gui::button launch_;
00330     gui::button cancel_;
00331     gui::button add_local_player_;
00332 
00333     gui::drop_group_manager_ptr combo_control_group_;
00334 
00335 }; // end class connect
00336 
00337 } // end namespace mp
00338 
00339 #endif
00340 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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