Author: crab
Date: Tue Apr 28 20:01:21 2009
New Revision: 35302

URL: http://svn.gna.org/viewcvs/wesnoth?rev=35302&view=rev
Log:
Implement FR in Bug #13397 : new fai function suitable_keep which takes a 
location on which a unit is standing, and returns, in order of decreasing 
preference: nearest (by pathfinding) empty keep reachable by that unit within 1 
turn, nearest (by pathfinding) occupied keep reachable within 1 turn, result of 
old nearest_keep() implementation, null location.

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

Modified: trunk/changelog
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/changelog?rev=35302&r1=35301&r2=35302&view=diff
==============================================================================
--- trunk/changelog (original)
+++ trunk/changelog Tue Apr 28 20:01:21 2009
@@ -49,6 +49,7 @@
    * Added attacks_left attribute to unit callable
    * New variable: my_attacks with all possible attacks 
    * Added a new type of candidate move : strategic
+   * Added suitable_keep FormulaAI function to allow easier selection of 
suitable keep for leader
  * Graphics:
    * New type of animation : "recruiting" used by leaders when recruiting
      units

Modified: trunk/src/ai/ai.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/ai.hpp?rev=35302&r1=35301&r2=35302&view=diff
==============================================================================
--- trunk/src/ai/ai.hpp (original)
+++ trunk/src/ai/ai.hpp Tue Apr 28 20:01:21 2009
@@ -76,6 +76,9 @@
 
        /** Return true iff there has been another attack this turn 'close' to 
this one. */
        bool attack_close(const location& loc) const;
+
+       /** get most suitable keep for leader - nearest free that can be 
reached in 1 turn, if none - return nearest occupied that can be reached in 1 
turn, if none - return nearest keep, if none - return null_location */
+       const map_location& suitable_keep( const location& leader_location, 
const paths& leader_paths );
 
 protected:
 
@@ -348,8 +351,6 @@
        /** Functions to deal with keeps. */
        const std::set<location>& keeps();
        const location& nearest_keep(const location& loc);
-       /** get most suitable keep for leader - nearest free that can be 
reached in 1 turn, if none - return nearest occupied that can be reached in 1 
turn, if none - return nearest keep, if none - return null_location */
-       const map_location& suitable_keep( const location& leader_location, 
const paths& leader_paths );
        int count_free_hexes_in_castle(const map_location& loc, 
std::set<map_location>&);
 
        void evaluate_recruiting_value(unit_map::iterator leader);

Modified: trunk/src/ai/formula_ai.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/formula_ai.cpp?rev=35302&r1=35301&r2=35302&view=diff
==============================================================================
--- trunk/src/ai/formula_ai.cpp (original)
+++ trunk/src/ai/formula_ai.cpp Tue Apr 28 20:01:21 2009
@@ -356,6 +356,29 @@
        }
 
        const formula_ai& ai_;
+};
+
+/**
+* Find suitable keep for unit at location
+* arguments[0] - location for unit on which the suitable keep is to be found
+*/
+class suitable_keep_function : public function_expression {
+public:
+       suitable_keep_function(const args_list& args, formula_ai& ai)
+         : function_expression("suitable_keep", args, 1, 1), ai_(ai) {
+       }
+
+private:
+       variant execute(const formula_callable& variables) const {
+               const map_location loc = 
convert_variant<location_callable>(args()[0]->evaluate(variables))->loc();
+               if (ai_.get_info().units.find(loc)==ai_.get_info().units.end()){
+                       return variant();
+               }
+               const paths unit_paths(ai_.get_info().map, 
ai_.get_info().units, loc ,ai_.get_info().teams, false, false, 
ai_.current_team());
+               return variant(new 
location_callable(ai_.suitable_keep(loc,unit_paths)));
+       }
+
+       formula_ai& ai_;
 };
 
 class find_shroud_function : public function_expression {
@@ -1552,6 +1575,8 @@
                return expression_ptr(new simplest_path_function(args, ai_));
        } else if(fn == "nearest_keep") {
                return expression_ptr(new nearest_keep_function(args, ai_));
+       } else if(fn == "suitable_keep") {
+               return expression_ptr(new suitable_keep_function(args, ai_));
        } else if(fn == "nearest_loc") {
                return expression_ptr(new nearest_loc_function(args, ai_));
        } else if(fn == "find_shroud") {


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

Reply via email to