Author: oracle
Date: Wed Jul 9 01:10:17 2008
New Revision: 27864
URL: http://svn.gna.org/viewcvs/wesnoth?rev=27864&view=rev
Log:
Further refinement of ai_manager. It is now called for all
AIs and the AI in turn informs the manager if it should be
managed. This decouples the manager from specific AI implementation
knowledge.
Modified:
trunk/src/ai.cpp
trunk/src/playsingle_controller.cpp
Modified: trunk/src/ai.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai.cpp?rev=27864&r1=27863&r2=27864&view=diff
==============================================================================
--- trunk/src/ai.cpp (original)
+++ trunk/src/ai.cpp Wed Jul 9 01:10:17 2008
@@ -2313,20 +2313,40 @@
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));
- AINameMap::value_type new_ai_pair(ai_key, new_ai);
- itor = ais.insert(new_ai_pair).first;
- } else {
- // std::cout << "ai_manager located existing AI. Calling ai's
new_turn method." << std::endl ;
- // itor->second->new_turn();
- }
- return itor->second;
+ int ai_key = ai_info.team_num - 1 ;
+ boost::intrusive_ptr<ai_interface> ai_obj;
+
+ // If we are dealing with an AI - try to find it
+ if( ai_algo.size() )
+ {
+ AINameMap::const_iterator itor = ais.find(ai_key);
+ std::cout << "ai_manager::get_ai() for algorithm: " << ai_algo <<
std::endl ;
+ if(itor == ais.end())
+ {
+ std::cout << "manager did not find AI - creating..." << std::endl ;
+ ai_obj = create_ai(ai_algo, ai_info) ;
+ std::cout << "Asking newly created AI if it wants to be managed." <<
std::endl ;
+ if( ai_obj->manager_manage_ai() )
+ {
+ std::cout << "AI has requested itself be managed: " << ai_algo <<
std::endl ;
+ AINameMap::value_type new_ai_pair( ai_key, ai_obj ) ;
+ itor = ais.insert(new_ai_pair).first ;
+ }
+ }
+ else
+ {
+ // AI was found - so return it
+ std::cout << "Reusing managed AI" << std::endl ;
+ ai_obj = itor->second ;
+ }
+ }
+ else
+ {
+ // No AI algorithm - simply create the instance
+ ai_obj = create_ai(ai_algo, ai_info) ;
+ }
+
+ return ai_obj ;
}
Modified: trunk/src/playsingle_controller.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/playsingle_controller.cpp?rev=27864&r1=27863&r2=27864&view=diff
==============================================================================
--- trunk/src/playsingle_controller.cpp (original)
+++ trunk/src/playsingle_controller.cpp Wed Jul 9 01:10:17 2008
@@ -725,13 +725,8 @@
ai_interface::info
ai_info(*gui_,map_,units_,teams_,player_number_,status_, turn_data, gamestate_);
std::string ai_algorithm = current_team().ai_algorithm();
- boost::intrusive_ptr<ai_interface> ai_obj;
-
- if(ai_algorithm == "formula_ai") {
- ai_obj = ai_manager::get_ai( ai_algorithm, ai_info ) ;
- } else {
- ai_obj = create_ai( ai_algorithm, ai_info ) ;
- }
+ boost::intrusive_ptr<ai_interface> ai_obj ;
+ ai_obj = ai_manager::get_ai( ai_algorithm, ai_info ) ;
ai_obj->user_interact().attach_handler(this);
ai_obj->unit_recruited().attach_handler(this);
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits