Author: crab
Date: Thu Apr  2 21:14:29 2009
New Revision: 34407

URL: http://svn.gna.org/viewcvs/wesnoth?rev=34407&view=rev
Log:
Encapsulated AI inside ai_manager to prevent the need to get
a non-const reference to private member when asking the AI
to play its turn.

Modified:
    trunk/src/ai_manager.cpp
    trunk/src/ai_manager.hpp
    trunk/src/playsingle_controller.cpp

Modified: trunk/src/ai_manager.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai_manager.cpp?rev=34407&r1=34406&r2=34407&view=diff
==============================================================================
--- trunk/src/ai_manager.cpp (original)
+++ trunk/src/ai_manager.cpp Thu Apr  2 21:14:29 2009
@@ -536,12 +536,6 @@
 // GET active AI parameters
 // =======================================================================
 
-ai_interface& ai_manager::get_active_ai_for_team( int team, 
ai_interface::info& i )
-{
-       return get_active_ai_holder_for_team(team).get_ai_ref(i);
-}
-
-
 const std::vector<config>& ai_manager::get_active_ai_parameters_for_team( int 
team )
 {
        return get_active_ai_holder_for_team(team).get_ai_parameters();
@@ -603,6 +597,20 @@
 void ai_manager::set_active_ai_algorithm_type_for_team( int team, const 
std::string& ai_algorithm_type )
 {
        
get_active_ai_holder_for_team(team).set_ai_algorithm_type(ai_algorithm_type);
+}
+
+
+// =======================================================================
+// PROXY
+// =======================================================================
+
+void ai_manager::play_turn( int team, ai_interface::info& i, events::observer* 
event_observer ){
+       ai_interface& ai_obj = get_active_ai_for_team(team, i);
+       ai_obj.user_interact().attach_handler(event_observer);
+       ai_obj.unit_recruited().attach_handler(event_observer);
+       ai_obj.unit_moved().attach_handler(event_observer);
+       ai_obj.enemy_attacked().attach_handler(event_observer);
+       ai_obj.play_turn();
 }
 
 
@@ -669,6 +677,12 @@
 // AI POINTERS
 // =======================================================================
 
+ai_interface& ai_manager::get_active_ai_for_team( int team, 
ai_interface::info& i )
+{
+       return get_active_ai_holder_for_team(team).get_ai_ref(i);
+}
+
+
 ai_interface& ai_manager::get_or_create_active_ai_for_team_without_fallback( 
int team, ai_interface::info& i, const std::string& ai_algorithm_type )
 {
        ai_holder& ai_holder = 
get_or_create_active_ai_holder_for_team_without_fallback(team,ai_algorithm_type);

Modified: trunk/src/ai_manager.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai_manager.hpp?rev=34407&r1=34406&r2=34407&view=diff
==============================================================================
--- trunk/src/ai_manager.hpp (original)
+++ trunk/src/ai_manager.hpp Thu Apr  2 21:14:29 2009
@@ -201,7 +201,7 @@
        /**
         * Return a list of available AIs
         * @return the list of available AIs
-        * @todo tet this list from a list of files in specified directory
+        * @todo get this list from a list of files in specified directory
         */
        static std::vector<std::string> get_available_ais();
 
@@ -238,16 +238,6 @@
        // 
=======================================================================
        // GET active AI parameters
        // 
=======================================================================
-
-       /**
-        * Get active AI for specified team.
-        * @note: Running this command may invalidate references previously 
returned by AI Manager
-        * @return a reference to the active ai
-        * @note this reference may become invalid after specific ai_manager 
operations.
-        * @param team side number (1-based, as in ai_interface::info)
-        * @param i game information for the AI. is used only if AI is to be 
created.
-        */
-       static ai_interface& get_active_ai_for_team( int team, 
ai_interface::info& i );
 
 
        /**
@@ -350,6 +340,19 @@
         * @param ai_algorithm_type ai algorithm type to be set
         */
        static void set_active_ai_algorithm_type_for_team( int team, const 
std::string& ai_algorithm_type );
+
+
+       // 
=======================================================================
+       // PROXY
+       // 
=======================================================================
+
+       /**
+        * Play a turn for the specified team using its active AI
+        * @param team side number (1-based, as in ai_interface::info)
+        * @param i game information for the AI
+        * @param event_observer controller which will observe events which are 
produced by the AI
+        */
+       static void play_turn(int team, ai_interface::info& i, 
events::observer* event_observer);
 
 
 private:
@@ -358,6 +361,7 @@
        static AI_map_of_stacks ai_map_;
        static std::deque< ai_command_history_item > history_;
        static long history_item_counter;
+
 
        // 
=======================================================================
        // EVALUATION
@@ -421,6 +425,17 @@
        // 
=======================================================================
 
        /**
+        * Get active AI for specified team.
+        * @note: Running this command may invalidate references previously 
returned by AI Manager
+        * @return a reference to the active ai
+        * @note this reference may become invalid after specific ai_manager 
operations.
+        * @param team side number (1-based, as in ai_interface::info)
+        * @param i game information for the AI. is used only if AI is to be 
created.
+        */
+       static ai_interface& get_active_ai_for_team( int team, 
ai_interface::info& i );
+
+
+       /**
         * Get the command AI for the specified team
         */
        static ai_interface& get_command_ai(int team, ai_interface::info& i);

Modified: trunk/src/playsingle_controller.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/playsingle_controller.cpp?rev=34407&r1=34406&r2=34407&view=diff
==============================================================================
--- trunk/src/playsingle_controller.cpp (original)
+++ trunk/src/playsingle_controller.cpp Thu Apr  2 21:14:29 2009
@@ -823,14 +823,8 @@
 
        ai_interface::info 
ai_info(*gui_,map_,units_,teams_,player_number_,status_, turn_data, gamestate_);
 
-       ai_interface& ai_obj = 
ai_manager::get_active_ai_for_team(player_number_ , ai_info ) ;
-
-       ai_obj.user_interact().attach_handler(this);
-       ai_obj.unit_recruited().attach_handler(this);
-       ai_obj.unit_moved().attach_handler(this);
-       ai_obj.enemy_attacked().attach_handler(this);
        try {
-               ai_obj.play_turn();
+               ai_manager::play_turn(player_number_, ai_info, this);
        } catch (end_turn_exception) {
        }
        recorder.end_turn();


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to