Author: oracle
Date: Wed Jul  9 00:15:38 2008
New Revision: 27859

URL: http://svn.gna.org/viewcvs/wesnoth?rev=27859&view=rev
Log:
Fixed key-collision in ai_manager which would cause an AI to
be incorrectly reused by another team. Added a method to allow
the ai_manager to detect if an AI requests management by the 
ai_manager. Lastly, added a hook to allow the ai_manager to
clean up AI resources prior to playsingle_controller destruction.
Formula_ai now has method to inform ai_manager it wishes to be
managed.


Modified:
    trunk/src/ai.cpp
    trunk/src/ai.hpp
    trunk/src/ai_interface.hpp
    trunk/src/formula_ai.hpp
    trunk/src/playsingle_controller.cpp
    trunk/src/playsingle_controller.hpp

Modified: trunk/src/ai.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai.cpp?rev=27859&r1=27858&r2=27859&view=diff
==============================================================================
--- trunk/src/ai.cpp (original)
+++ trunk/src/ai.cpp Wed Jul  9 00:15:38 2008
@@ -2308,17 +2308,40 @@
        inputs->push_back(formula_input("is_surrounded", FORMULA_READ_ONLY));
 }
 
-ai_manager::ai_map ai_manager::ais = ai_manager::ai_map();
-
-boost::intrusive_ptr<ai_interface> ai_manager::get_ai(ai_interface::info& 
ai_info, std::string ai_key, std::string ai_algo) {
-       ai_map::const_iterator itor = ais.find(ai_key);
+ai_manager::AINameMap ai_manager::ais = ai_manager::AINameMap();
+
+boost::intrusive_ptr<ai_interface> ai_manager::get_ai( std::string ai_algo,
+                                                      ai_interface::info& 
ai_info )
+{
+        int ai_key = ai_info.team_num - 1 ;
+       AINameMap::const_iterator itor = ais.find(ai_key);
+
+       //      std::cout << "Looking at AI: " << ai_key << std::endl ;
        if(itor == ais.end()) {
+         //      std::cout << "ai_manager was not able to locate this ai - 
saving..." << ai_key << std::endl ;
                boost::intrusive_ptr<ai_interface> new_ai(create_ai(ai_algo, 
ai_info));
-               ai_map::value_type new_ai_pair(ai_key, new_ai);
+               AINameMap::value_type new_ai_pair(ai_key, new_ai);
                itor = ais.insert(new_ai_pair).first;
        } else {
-               itor->second->new_turn();
+         //      std::cout << "ai_manager located existing AI. Calling ai's 
new_turn method." << std::endl ;
+         //            itor->second->new_turn();
        }
        return itor->second;
 }
 
+
+int ai_manager::reap_ais()
+{
+  int counter = 0 ;
+  for( AINameMap::iterator itor = ais.begin() ; itor != ais.end() ; ++itor )
+    {
+      //  std::cout << "Should reap ai:" << itor->first << ", " << 
itor->second->manager_reap_ai() << std::endl ;
+      if( itor->second->manager_reap_ai() )
+       {
+         //      std::cout << "reaping ai: " << itor->first << std::endl ;
+         ++counter ;
+       }
+    }
+
+  return counter ;
+}

Modified: trunk/src/ai.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai.hpp?rev=27859&r1=27858&r2=27859&view=diff
==============================================================================
--- trunk/src/ai.hpp (original)
+++ trunk/src/ai.hpp Wed Jul  9 00:15:38 2008
@@ -376,14 +376,15 @@
 };
 
 class ai_manager {
-
-       typedef std::map<std::string, boost::intrusive_ptr<ai_interface> > 
ai_map;
-
-       public: 
-               static boost::intrusive_ptr<ai_interface> 
get_ai(ai_interface::info&, std::string, std::string);
-
-       private:
-               static ai_map ais;
+public: 
+  static boost::intrusive_ptr<ai_interface> get_ai( std::string, 
ai_interface::info& );
+  static int reap_ais() ;
+
+private:
+  typedef std::map< int, boost::intrusive_ptr<ai_interface> > AINameMap ;
+
+  static AINameMap ais ;
 };
 
+
 #endif

Modified: trunk/src/ai_interface.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai_interface.hpp?rev=27859&r1=27858&r2=27859&view=diff
==============================================================================
--- trunk/src/ai_interface.hpp (original)
+++ trunk/src/ai_interface.hpp Wed Jul  9 00:15:38 2008
@@ -122,6 +122,13 @@
        events::generic_event& unit_recruited() { return unit_recruited_; }
        events::generic_event& unit_moved()     { return unit_moved_; }
        events::generic_event& enemy_attacked() { return enemy_attacked_; }
+
+        // If the AI manager should manage the AI once constructed.
+        virtual const bool manager_manage_ai() const { return false ; } ;
+
+        // If an AI manager should reap the AI at end of game
+        // Older AIs should use the default - false.
+        virtual const bool manager_reap_ai() { return false ; } ;
 
 protected:
        /**

Modified: trunk/src/formula_ai.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_ai.hpp?rev=27859&r1=27858&r2=27859&view=diff
==============================================================================
--- trunk/src/formula_ai.hpp (original)
+++ trunk/src/formula_ai.hpp Wed Jul  9 00:15:38 2008
@@ -55,6 +55,7 @@
 class formula_ai : public ai {
 public:
        explicit formula_ai(info& i);
+        virtual ~formula_ai() {} ;
        virtual void play_turn();
        virtual void new_turn();
 
@@ -83,6 +84,9 @@
                move_map srcdst, dstsrc, full_srcdst, full_dstsrc, 
enemy_srcdst, enemy_dstsrc;
                variant attacks_cache;
        };
+
+        // If the AI manager should manager the AI once constructed.
+        virtual const bool manager_manage_ai() const { return true ; } ;
 
        void swap_move_map(move_map_backup& backup);
 

Modified: trunk/src/playsingle_controller.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/playsingle_controller.cpp?rev=27859&r1=27858&r2=27859&view=diff
==============================================================================
--- trunk/src/playsingle_controller.cpp (original)
+++ trunk/src/playsingle_controller.cpp Wed Jul  9 00:15:38 2008
@@ -52,6 +52,13 @@
                browse_ = linger_ = true;
        }
 }
+
+
+playsingle_controller::~playsingle_controller()
+{
+  ai_manager::reap_ais() ;
+}
+
 
 void playsingle_controller::init_gui(){
        LOG_NG << "Initializing GUI... " << (SDL_GetTicks() - ticks_) << "\n";
@@ -721,9 +728,9 @@
        boost::intrusive_ptr<ai_interface> ai_obj;
 
        if(ai_algorithm == "formula_ai") {
-               ai_obj = ai_manager::get_ai(ai_info,current_team().name(), 
ai_algorithm);
+               ai_obj = ai_manager::get_ai( ai_algorithm, ai_info ) ;
        } else {
-               ai_obj = create_ai(ai_algorithm, ai_info);
+               ai_obj = create_ai( ai_algorithm, ai_info ) ;
        }
 
        ai_obj->user_interact().attach_handler(this);

Modified: trunk/src/playsingle_controller.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/playsingle_controller.hpp?rev=27859&r1=27858&r2=27859&view=diff
==============================================================================
--- trunk/src/playsingle_controller.hpp (original)
+++ trunk/src/playsingle_controller.hpp Wed Jul  9 00:15:38 2008
@@ -31,7 +31,8 @@
 public:
        playsingle_controller(const config& level, game_state& state_of_game,
                const int ticks, const int num_turns, const config& 
game_config, CVideo& video, bool skip_replay);
-
+        virtual ~playsingle_controller() ;
+ 
        LEVEL_RESULT play_scenario(const std::vector<config*>& story, 
upload_log& log, bool skip_replay);
 
        virtual void handle_generic_event(const std::string& name);


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

Reply via email to