Author: esr
Date: Thu Oct 23 04:20:22 2008
New Revision: 30323

URL: http://svn.gna.org/viewcvs/wesnoth?rev=30323&view=rev
Log:
Merged formula ai to default ai (formula makes moves first and handles 
recruiting if scenario includes formulas).

This is ported forward from suokko's commit 29484, which had previously
been reverted as an emergency fix. 

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

Modified: trunk/src/ai.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai.cpp?rev=30323&r1=30322&r2=30323&view=diff
==============================================================================
--- trunk/src/ai.cpp (original)
+++ trunk/src/ai.cpp Thu Oct 23 04:20:22 2008
@@ -47,6 +47,7 @@
 #include <cassert>
 #include <fstream>
 
+#include <boost/scoped_ptr.hpp>
 
 #define DBG_AI LOG_STREAM(debug, ai)
 #define LOG_AI LOG_STREAM(info, ai)
@@ -291,6 +292,7 @@
        state_(info.state),
        consider_combat_(true),
        additional_targets_(),
+       info_(info),
        unit_movement_scores_(),
        not_recommended_units_(),
        unit_combat_scores_(),
@@ -298,8 +300,11 @@
        avoid_(),
        unit_stats_cache_(),
        attack_depth_(0),
-       recruiting_preferred_(0)
-{}
+       recruiting_preferred_(0),
+       master_(info_.master)
+{
+       info_.master = false;
+}
 
 void ai::new_turn()
 {
@@ -315,6 +320,7 @@
        avoid_.clear();
        unit_stats_cache_.clear();
        attack_depth_ = 0;
+       ai_manager::get_ai("formula_ai", info_)->new_turn();
        ai_interface::new_turn();
 }
 
@@ -1008,6 +1014,11 @@
 
        raise_user_interact();
 
+       // Formula AI is first going to move everything that it can
+
+//     if (master_)
+//             static_cast<formula_ai*>(ai_manager::get_ai("formula_ai", 
info_).get())->play_turn();
+
        typedef paths::route route;
 
        typedef std::map<location,paths> moves_map;
@@ -1872,6 +1883,16 @@
        const unit_map::const_iterator leader = find_leader(units_,team_num_);
        if(leader == units_.end()) {
                return false;
+       }
+
+       raise_user_interact();
+       // Let formula ai to do recruiting first
+       if (master_)
+       {
+               
if(static_cast<formula_ai*>(ai_manager::get_ai("formula_ai",info_).get())->do_recruitment())
 {
+                       LOG_AI << "Recruitment done by formula_ai\n";
+                       return true;
+               }
        }
 
        const location& start_pos = nearest_keep(leader->first);

Modified: trunk/src/ai.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai.hpp?rev=30323&r1=30322&r2=30323&view=diff
==============================================================================
--- trunk/src/ai.hpp (original)
+++ trunk/src/ai.hpp Thu Oct 23 04:20:22 2008
@@ -295,6 +295,8 @@
        bool consider_combat_;
        std::vector<target> additional_targets_;
 
+ai_interface::info info_;
+
        void add_target(const target& tgt) { 
additional_targets_.push_back(tgt); }
 
        /**
@@ -379,6 +381,8 @@
        
        int recruiting_preferred_;
        static const int min_recruiting_value_to_force_recruit = 28;
+protected:
+       bool master_;
 };
 
 class ai_manager {

Modified: trunk/src/ai_interface.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai_interface.hpp?rev=30323&r1=30322&r2=30323&view=diff
==============================================================================
--- trunk/src/ai_interface.hpp (original)
+++ trunk/src/ai_interface.hpp Thu Oct 23 04:20:22 2008
@@ -47,7 +47,8 @@
                info(game_display& disp, gamemap& map, unit_map& units,
                        std::vector<team>& teams, unsigned int team_num, 
gamestatus& state, class turn_info& turn_data, class game_state& game_state)
                        : disp(disp), map(map), units(units), teams(teams),
-                         team_num(team_num), state(state), 
turn_data_(turn_data), game_state_(game_state)              {}
+                         team_num(team_num), state(state), 
turn_data_(turn_data), game_state_(game_state), master(true)
+               {}
 
                /** The display object, used to draw the moves the AI makes. */
                game_display& disp;
@@ -80,6 +81,7 @@
 
                /** The global game state, because we may set the completion 
field. */
                class game_state& game_state_;
+               bool master;
        };
 
        /**

Modified: trunk/src/formula_ai.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_ai.cpp?rev=30323&r1=30322&r2=30323&view=diff
==============================================================================
--- trunk/src/formula_ai.cpp (original)
+++ trunk/src/formula_ai.cpp Thu Oct 23 04:20:22 2008
@@ -1449,9 +1449,12 @@
                int best_score = (*best_move)->get_score();
                // If no evals > 0, fallback
                if(best_score < 0) {
-                       ai_interface* fallback = create_ai("", get_info());
-                       fallback->play_turn();
-                       return;
+                       if (master_)
+                       {
+                               ai_interface* fallback = create_ai("", 
get_info());
+                               fallback->play_turn();
+                               return;
+                       }
                }
                // Otherwise, make the best scoring move
                game_logic::map_formula_callable callable(this);
@@ -1470,9 +1473,11 @@
        }
 
        // After all candidate moves have been exhausted, fallback
-       ai_interface* fallback = create_ai("", get_info());
-       fallback->play_turn();
-
+       if (master_)
+       {
+               ai_interface* fallback = create_ai("", get_info());
+               fallback->play_turn();
+       }
 }
 
 
@@ -1550,8 +1555,10 @@
 bool formula_ai::make_move(game_logic::const_formula_ptr formula_, const 
game_logic::formula_callable& variables)
 {
        if(!formula_) {
-               ai_interface* fallback = create_ai("", get_info());
-               fallback->play_turn();
+               if(master_) {
+                       ai_interface* fallback = create_ai("", get_info());
+                       fallback->play_turn();
+               }
                return false;
        }
 
@@ -1744,15 +1751,18 @@
                } else if(i->is_string() && i->as_string() == "end_turn") {
                        return false;
                } else if(fallback_command) {
-                       if(fallback_command->key() == "human")
+                       if (master_)
                        {
-                               //we want give control of the side to human for 
the rest of this turn
-                               throw fallback_ai_to_human_exception();
-                       } else
-                       {
-                               ai_interface* fallback = 
create_ai(fallback_command->key(), get_info());
-                               if(fallback) {
-                                       fallback->play_turn();
+                               if(fallback_command->key() == "human")
+                               {
+                                       //we want give control of the side to 
human for the rest of this turn
+                                       throw fallback_ai_to_human_exception();
+                               } else
+                               {
+                                       ai_interface* fallback = 
create_ai(fallback_command->key(), get_info());
+                                       if(fallback) {
+                                               fallback->play_turn();
+                                       }
                                }
                        }
                        return false;

Modified: trunk/src/formula_ai.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_ai.hpp?rev=30323&r1=30322&r2=30323&view=diff
==============================================================================
--- trunk/src/formula_ai.hpp (original)
+++ trunk/src/formula_ai.hpp Thu Oct 23 04:20:22 2008
@@ -176,6 +176,7 @@
 
        game_logic::candidate_move_set candidate_moves_;
        bool use_eval_lists_;
+       friend class ai;
 };
 
 #endif


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

Reply via email to