Author: dragonking
Date: Wed Mar 26 00:04:42 2008
New Revision: 25133

URL: http://svn.gna.org/viewcvs/wesnoth?rev=25133&view=rev
Log:
New formula function: nearest_keep

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=25133&r1=25132&r2=25133&view=diff
==============================================================================
--- trunk/src/formula_ai.cpp (original)
+++ trunk/src/formula_ai.cpp Wed Mar 26 00:04:42 2008
@@ -108,6 +108,39 @@
                }
 
                return variant(best);
+       }
+       
+       const formula_ai& ai_;
+};
+
+class nearest_keep_function : public function_expression {
+public:
+       nearest_keep_function(const args_list& args, const formula_ai& ai)
+         : function_expression("nearest_keep", args, 1, 1), ai_(ai) {
+       }
+
+private:
+       variant execute(const formula_callable& variables) const {
+               const gamemap::location loc = 
convert_variant<location_callable>(args()[0]->evaluate(variables))->loc();
+               int best = 1000000;
+               int best_i = -1;
+
+               ai_.get_keeps();
+               int size = ai_.get_keeps_cache().num_elements();
+
+               for( int i = 0 ; i < size; ++i) {
+                       int distance = distance_between(loc, 
convert_variant<location_callable>(ai_.get_keeps_cache()[i])->loc() );
+                       if(distance < best)
+                       {
+                                       best = distance;
+                                       best_i = i;
+                       }
+               }
+
+               if( best_i != -1)
+                       return variant(new 
location_callable(convert_variant<location_callable>(ai_.get_keeps_cache()[best_i])->loc()));
+               else
+                       return variant();
        }
        
        const formula_ai& ai_;
@@ -525,6 +558,8 @@
                        return expression_ptr(new 
max_possible_damage_function(args, ai_));
                } else if(fn == "distance_to_nearest_unowned_village") {
                        return expression_ptr(new 
distance_to_nearest_unowned_village_function(args, ai_));
+               } else if(fn == "nearest_keep") {
+                       return expression_ptr(new nearest_keep_function(args, 
ai_));
                } else if(fn == "distance_between") {
                        return expression_ptr(new 
distance_between_function(args));
                } else {

Modified: trunk/src/formula_ai.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_ai.hpp?rev=25133&r1=25132&r2=25133&view=diff
==============================================================================
--- trunk/src/formula_ai.hpp (original)
+++ trunk/src/formula_ai.hpp Wed Mar 26 00:04:42 2008
@@ -42,6 +42,10 @@
 
        void swap_move_map(move_map_backup& backup);
 
+       variant get_keeps() const;
+
+       const variant& get_keeps_cache() const { return keeps_cache_; }
+
 private:
        void do_recruitment();
        bool make_move(game_logic::const_formula_ptr formula_, const 
game_logic::formula_callable& variables);
@@ -59,8 +63,6 @@
        mutable variant attacks_cache_;
        mutable variant keeps_cache_;
 
-       variant get_keeps() const;
-
        game_logic::map_formula_callable vars_;
 };
 


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

Reply via email to