Author: crab
Date: Thu May 21 02:18:37 2009
New Revision: 35778

URL: http://svn.gna.org/viewcvs/wesnoth?rev=35778&view=rev
Log:
Delayed initialization of formula AI - moved it from constructor to on_create 
method. This is needed to simplify the initiazization process (which formerly 
called virtual methods of a not-yet-fully-created class)

Modified:
    trunk/src/ai/ai_interface.hpp
    trunk/src/ai/ai_manager.cpp
    trunk/src/ai/formula_ai.cpp
    trunk/src/ai/formula_ai.hpp
    trunk/src/ai/game_info.hpp

Modified: trunk/src/ai/ai_interface.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/ai_interface.hpp?rev=35778&r1=35777&r2=35778&view=diff
==============================================================================
--- trunk/src/ai/ai_interface.hpp (original)
+++ trunk/src/ai/ai_interface.hpp Thu May 21 02:18:37 2009
@@ -45,6 +45,13 @@
        virtual void new_turn() {
        }
 
+       /**
+        * Function called after the new ai is created
+        *
+        */
+       virtual void on_create() {
+       }
+
        virtual void switch_side(ai::side_number side) = 0;
 
         /** Evaluate */

Modified: trunk/src/ai/ai_manager.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/ai_manager.cpp?rev=35778&r1=35777&r2=35778&view=diff
==============================================================================
--- trunk/src/ai/ai_manager.cpp (original)
+++ trunk/src/ai/ai_manager.cpp Thu May 21 02:18:37 2009
@@ -551,33 +551,45 @@
        //: To add an AI of your own, put
        //      if(ai_algorithm_type == "my_ai") {
        //              LOG_AI_MANAGER << "Creating new AI of type [" << 
"my_ai" << "]"<< std::endl;
-       //              return new my_ai(rw_context);
+       //              ai_interface *a = new my_ai(*rw_context);
+       //              a->on_create();
+       //              return a;
        //      }
        // at the top of this function
 
        //if(ai_algorithm_type == ai_manager::AI_TYPE_SAMPLE_AI) {
        //  LOG_AI_MANAGER << "Creating new AI of type [" << 
ai_manager::AI_TYPE_IDLE_AI << "]"<< std::endl;
-       //      return new sample_ai(*rw_context);
+       //      ai_interface *a = new sample_ai(*rw_context);
+       //      a->on_create();
+       //      return a;
        //}
 
        if(ai_algorithm_type == ai_manager::AI_TYPE_IDLE_AI) {
                LOG_AI_MANAGER << "Creating new AI of type [" << 
ai_manager::AI_TYPE_IDLE_AI << "]"<< std::endl;
-               return new idle_ai(*rw_context);
+               ai_interface *a = new idle_ai(*rw_context);
+               a->on_create();
+               return a;
        }
 
        if(ai_algorithm_type == ai_manager::AI_TYPE_FORMULA_AI) {
                LOG_AI_MANAGER << "Creating new AI of type [" << 
ai_manager::AI_TYPE_FORMULA_AI << "]"<< std::endl;
-               return new formula_ai(*rw_context);
+               ai_interface *a = new formula_ai(*rw_context);
+               a->on_create();
+               return a;
        }
 
        if(ai_algorithm_type == ai_manager::AI_TYPE_DFOOL_AI) {
                LOG_AI_MANAGER << "Creating new AI of type [" << 
ai_manager::AI_TYPE_DFOOL_AI << "]"<< std::endl;
-               return new dfool::dfool_ai(*rw_context);
+               ai_interface *a = new dfool::dfool_ai(*rw_context);
+               a->on_create();
+               return a;
        }
 
        if(ai_algorithm_type == ai_manager::AI_TYPE_AI2) {
                LOG_AI_MANAGER << "Creating new AI of type [" << 
ai_manager::AI_TYPE_AI2 << "]"<< std::endl;
-               return new ai2(*rw_context);
+               ai_interface *a = new ai2(*rw_context);
+               a->on_create();
+               return a;
        }
 
        if (!ai_algorithm_type.empty() && ai_algorithm_type != 
ai_manager::AI_TYPE_DEFAULT) {
@@ -585,7 +597,9 @@
        }
 
        LOG_AI_MANAGER  << "Creating new AI of type [" << 
ai_manager::AI_TYPE_DEFAULT << "]"<< std::endl;
-       return new ai_default(*rw_context);
+       ai_interface *a = new ai_default(*rw_context);
+       a->on_create();
+       return a;
 }
 
 std::vector<std::string> ai_manager::get_available_ais()

Modified: trunk/src/ai/formula_ai.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/formula_ai.cpp?rev=35778&r1=35777&r2=35778&view=diff
==============================================================================
--- trunk/src/ai/formula_ai.cpp (original)
+++ trunk/src/ai/formula_ai.cpp Thu May 21 02:18:37 2009
@@ -1623,49 +1623,6 @@
        function_table(*this),
        candidate_action_manager_()
 {
-       //make sure we don't run out of refcount
-       vars_.add_ref();
-       const config& ai_param = current_team().ai_parameters();
-
-       // load candidate actions from config
-       candidate_action_manager_.load_config(ai_param, this, &function_table);
-
-       foreach (const config &func, ai_param.child_range("function"))
-       {
-               const t_string &name = func["name"];
-               const t_string &inputs = func["inputs"];
-               const t_string &formula_str = func["formula"];
-
-               std::vector<std::string> args = utils::split(inputs);
-
-               try {
-                       function_table.add_formula_function(name,
-                               game_logic::const_formula_ptr(new 
game_logic::formula(formula_str, &function_table)),
-                               
game_logic::formula::create_optional_formula(func["precondition"], 
&function_table),
-                               args);
-                       }
-                       catch(formula_error& e) {
-                               handle_exception(e, "Error while registering 
function '" + name + "'");
-                       }
-               }
-
-
-        try{
-                recruit_formula_ = 
game_logic::formula::create_optional_formula(current_team().ai_parameters()["recruitment"],
 &function_table);
-        }
-        catch(formula_error& e) {
-                handle_exception(e);
-                recruit_formula_ = game_logic::formula_ptr();
-        }
-
-        try{
-                move_formula_ = 
game_logic::formula::create_optional_formula(current_team().ai_parameters()["move"],
 &function_table);
-        }
-        catch(formula_error& e) {
-                handle_exception(e);
-                move_formula_ = game_logic::formula_ptr();
-        }
-
 }
 
 void formula_ai::handle_exception(game_logic::formula_error& e) const
@@ -2575,3 +2532,49 @@
        }
        return false;
 }
+
+void formula_ai::on_create(){
+       //make sure we don't run out of refcount
+       vars_.add_ref();
+       const config& ai_param = current_team().ai_parameters();
+
+       // load candidate actions from config
+       candidate_action_manager_.load_config(ai_param, this, &function_table);
+
+       foreach (const config &func, ai_param.child_range("function"))
+       {
+               const t_string &name = func["name"];
+               const t_string &inputs = func["inputs"];
+               const t_string &formula_str = func["formula"];
+
+               std::vector<std::string> args = utils::split(inputs);
+
+               try {
+                       function_table.add_formula_function(name,
+                               game_logic::const_formula_ptr(new 
game_logic::formula(formula_str, &function_table)),
+                               
game_logic::formula::create_optional_formula(func["precondition"], 
&function_table),
+                               args);
+                       }
+                       catch(formula_error& e) {
+                               handle_exception(e, "Error while registering 
function '" + name + "'");
+                       }
+               }
+
+
+        try{
+                recruit_formula_ = 
game_logic::formula::create_optional_formula(current_team().ai_parameters()["recruitment"],
 &function_table);
+        }
+        catch(formula_error& e) {
+                handle_exception(e);
+                recruit_formula_ = game_logic::formula_ptr();
+        }
+
+        try{
+                move_formula_ = 
game_logic::formula::create_optional_formula(current_team().ai_parameters()["move"],
 &function_table);
+        }
+        catch(formula_error& e) {
+                handle_exception(e);
+                move_formula_ = game_logic::formula_ptr();
+        }
+
+}

Modified: trunk/src/ai/formula_ai.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/formula_ai.hpp?rev=35778&r1=35777&r2=35778&view=diff
==============================================================================
--- trunk/src/ai/formula_ai.hpp (original)
+++ trunk/src/ai/formula_ai.hpp Thu May 21 02:18:37 2009
@@ -99,6 +99,8 @@
 
        variant get_keeps() const;
 
+       void on_create();
+
        int get_recursion_count() const;
 
        const variant& get_keeps_cache() const { return keeps_cache_; }

Modified: trunk/src/ai/game_info.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/game_info.hpp?rev=35778&r1=35777&r2=35778&view=diff
==============================================================================
--- trunk/src/ai/game_info.hpp (original)
+++ trunk/src/ai/game_info.hpp Thu May 21 02:18:37 2009
@@ -32,7 +32,7 @@
  * decisions.
  */
 namespace ai {
-typedef unsigned int side_number;
+typedef int side_number;
 
 /** The standard way in which a map of possible moves is recorded. */
 typedef std::multimap<map_location,map_location> move_map;


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

Reply via email to