mail[Wesnoth-commits] r28362 - 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:42:
Author: dhains
Date: Thu Aug  7 06:53:11 2008
New Revision: 28362

URL: http://svn.gna.org/viewcvs/wesnoth?rev=28362&view=rev
Log:
Added can_attack function to formula AI, returns true if a unit can reach 
attacking range of enemy unit
Added functionality to eval attack moves to candiate_move::evaluate_move

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=28362&r1=28361&r2=28362&view=diff
==============================================================================
--- trunk/src/formula_ai.cpp (original)
+++ trunk/src/formula_ai.cpp Thu Aug  7 06:53:11 2008
@@ -1809,3 +1809,17 @@
 
        return keeps_cache_;
 }
+
+bool formula_ai::can_attack(const gamemap::location unit_loc, const 
gamemap::location enemy_loc) {
+       move_map::iterator i;
+       std::pair<move_map::iterator, 
+                         move_map::iterator> unit_moves;
+       unit_moves = srcdst_.equal_range(unit_loc);
+       for(i = unit_moves.first; i != unit_moves.second; ++i) {
+               gamemap::location 
diff(((*i).second).vector_difference(enemy_loc));
+               if((abs(diff.x) <= 1) && (abs(diff.y) <= 1)) {
+                       return true;
+               }
+       }
+       return false;
+}

Modified: trunk/src/formula_ai.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_ai.hpp?rev=28362&r1=28361&r2=28362&view=diff
==============================================================================
--- trunk/src/formula_ai.hpp (original)
+++ trunk/src/formula_ai.hpp Thu Aug  7 06:53:11 2008
@@ -41,17 +41,40 @@
 
        void evaluate_move(const formula_callable* ai, unit_map& units, int 
team_num) {
                score_ = -1000;
-               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;
+               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;
+                                       }
                                }
                        }
                }
@@ -157,6 +180,9 @@
 
        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);
+
 private:
        void do_recruitment();
        bool make_move(game_logic::const_formula_ptr formula_, const 
game_logic::formula_callable& variables);




Related Messages


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