mail[Wesnoth-commits] r28366 - in /trunk/src: formula_ai.cpp formula_ai.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by dhains on August 07, 2008 - 07:41:
Author: dhains
Date: Thu Aug  7 07:19:47 2008
New Revision: 28366

URL: http://svn.gna.org/viewcvs/wesnoth?rev=28366&view=rev
Log:
Added can_reach call to attack move evals to reduce amount of required evals.
Added target (enemy_unit in candidate_move class) as callable object to eval 
formula.

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

Modified: trunk/src/formula_ai.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_ai.cpp?rev=28366&r1=28365&r2=28366&view=diff
==============================================================================
--- trunk/src/formula_ai.cpp (original)
+++ trunk/src/formula_ai.cpp Thu Aug  7 07:19:47 2008
@@ -1810,7 +1810,8 @@
        return keeps_cache_;
 }
 
-bool formula_ai::can_attack(const gamemap::location unit_loc, const 
gamemap::location enemy_loc) {
+bool formula_ai::can_attack(const gamemap::location unit_loc, 
+               const gamemap::location enemy_loc) const {
        move_map::iterator i;
        std::pair<move_map::iterator, 
                          move_map::iterator> unit_moves;
@@ -1823,3 +1824,47 @@
        }
        return false;
 }
+
+
+void candidate_move::evaluate_move(const formula_ai* ai, unit_map& units, 
+               int team_num) {
+       score_ = -1000;
+       if(type_ == "attack") {
+               for(unit_map::unit_iterator me = units.begin() ; me != 
units.end() ; ++me)
+               {
+                       if( (me->second.side() == team_num) && 
+                                       (me->second.has_moved() == false) ) {
+                               for(unit_map::unit_iterator target = 
units.begin() ; target != units.end() ; ++target) {
+                                       if( (target->second.side() != 
team_num) &&
+                                                       
(ai->can_attack(me->first, target->first)) ) {
+                                               
game_logic::map_formula_callable callable((formula_callable*) ai);
+                                               callable.add_ref();
+                                               callable.add("me", 
variant(new unit_callable(*me)));
+                                               callable.add("target", 
variant(new unit_callable(*target)));
+                                               int res = 
(formula::evaluate(eval_, callable)).as_int();
+                                               if(res > score_) {
+                                                       score_ = res;
+                                                       action_unit_ = me;
+                                                       enemy_unit_ = target;
+                                               }
+                                       }
+                               }
+                       }
+               }
+       } else {
+               for(unit_map::unit_iterator i = units.begin() ; i != 
units.end() ; ++i)
+               {
+                       if( (i->second.side() == team_num) && 
+                                       (i->second.has_moved() == false) ) {
+                               game_logic::map_formula_callable 
callable((formula_callable*) ai);
+                               callable.add_ref();
+                               callable.add("me", variant(new 
unit_callable(*i)));
+                               int res = (formula::evaluate(eval_, 
callable)).as_int();
+                               if(res > score_) {
+                                       score_ = res;
+                                       action_unit_ = i;
+                               }
+                       }
+               }
+       }
+}

Modified: trunk/src/formula_ai.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_ai.hpp?rev=28366&r1=28365&r2=28366&view=diff
==============================================================================
--- trunk/src/formula_ai.hpp (original)
+++ trunk/src/formula_ai.hpp Thu Aug  7 07:19:47 2008
@@ -36,49 +36,11 @@
                eval_(eval),
                move_(move),
                score_(-1),
-               action_unit_()
+               action_unit_(),
+               enemy_unit_()
        {};
 
-       void evaluate_move(const formula_callable* ai, unit_map& units, int 
team_num) {
-               score_ = -1000;
-               if(type_ == "attack") {
-                       for(unit_map::unit_iterator me = units.begin() ; me 
!= units.end() ; ++me)
-                       {
-                               if( (me->second.side() == team_num) && 
-                                               (me->second.has_moved() == 
false) ) {
-                                       for(unit_map::unit_iterator target = 
units.begin() ; target != units.end() ; ++target) {
-                                               if(target->second.side() != 
team_num) {
-                                                       
game_logic::map_formula_callable callable(ai);
-                                                       callable.add_ref();
-                                                       callable.add("me", 
variant(new unit_callable(*me)));
-                                                       
callable.add("target", variant(new unit_callable(*target)));
-                                                       int res = 
(formula::evaluate(eval_, callable)).as_int();
-                                                       if(res > score_) {
-                                                               score_ = res;
-                                                               action_unit_ 
= me;
-                                       //                      enemy_unit_ = 
target;
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-               } else {
-                       for(unit_map::unit_iterator i = units.begin() ; i != 
units.end() ; ++i)
-                       {
-                               if( (i->second.side() == team_num) && 
-                                               (i->second.has_moved() == 
false) ) {
-                                       game_logic::map_formula_callable 
callable(ai);
-                                       callable.add_ref();
-                                       callable.add("me", variant(new 
unit_callable(*i)));
-                                       int res = (formula::evaluate(eval_, 
callable)).as_int();
-                                       if(res > score_) {
-                                               score_ = res;
-                                               action_unit_ = i;
-                                       }
-                               }
-                       }
-               }
-       }
+       void evaluate_move(const formula_ai* ai, unit_map& units, int 
team_num); 
 
        int get_score() const {return score_;}
        std::string get_type() const {return type_;}
@@ -101,6 +63,7 @@
        const_formula_ptr move_;
        int score_;
        unit_map::unit_iterator action_unit_;
+       unit_map::unit_iterator enemy_unit_;
 };
 
 
@@ -181,7 +144,7 @@
        const variant& get_keeps_cache() const { return keeps_cache_; }
 
        // Check if given unit loc can reach attack range of enemy loc 
-       bool can_attack (const gamemap::location, const gamemap::location);
+       bool can_attack (const gamemap::location, const gamemap::location) 
const;
 
 private:
        void do_recruitment();




Related Messages


Powered by MHonArc, Updated Thu Aug 07 08:41:00 2008