ai/testing/ca.hpp

Go to the documentation of this file.
00001 /* $Id: ca.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 
00003 /*
00004    Copyright (C) 2009 - 2012 by Yurii Chernyi <terraninfo@terraninfo.net>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY.
00013 
00014    See the COPYING file for more details.
00015 */
00016 
00017 /**
00018  * Default AI (Testing)
00019  * @file
00020  */
00021 
00022 #ifndef AI_TESTING_CA_HPP_INCLUDED
00023 #define AI_TESTING_CA_HPP_INCLUDED
00024 
00025 #include "../../unit_map.hpp"
00026 
00027 #include "../composite/rca.hpp"
00028 
00029 
00030 #ifdef _MSC_VER
00031 #pragma warning(push)
00032 //silence "inherits via dominance" warnings
00033 #pragma warning(disable:4250)
00034 #endif
00035 
00036 namespace ai {
00037 
00038 namespace testing_ai_default {
00039 
00040 //============================================================================
00041 
00042 class goto_phase : public candidate_action {
00043 public:
00044 
00045     goto_phase( rca_context &context, const config &cfg );
00046 
00047     virtual ~goto_phase();
00048 
00049     virtual double evaluate();
00050 
00051     virtual void execute();
00052 private:
00053     move_result_ptr move_;
00054 };
00055 
00056 
00057 //============================================================================
00058 class aspect_recruitment_phase : public candidate_action {
00059 public:
00060 
00061     aspect_recruitment_phase( rca_context &context, const config &cfg );
00062 
00063     virtual ~aspect_recruitment_phase();
00064 
00065     virtual double evaluate();
00066 
00067     virtual void execute();
00068 };
00069 
00070 //============================================================================
00071 
00072 class recruitment_phase : public candidate_action {
00073 public:
00074 
00075     recruitment_phase( rca_context &context, const config &cfg );
00076 
00077     virtual ~recruitment_phase();
00078 
00079     virtual double evaluate();
00080 
00081     virtual void execute();
00082 
00083 private:
00084 
00085     bool recruit_usage(const std::string& usage);
00086 
00087     std::map<std::string,int> unit_movement_scores_;
00088     std::set<std::string> not_recommended_units_;
00089 
00090     /**
00091      * Analyze all the units that this side can recruit
00092      * and rate their fighting suitability against enemy units.
00093      * Ratings will be placed in 'unit_combat_scores_',
00094      * with a '0' rating indicating that the unit is 'average' against enemy units,
00095      * negative ratings meaning they are poorly suited,
00096      * and positive ratings meaning they are well suited.
00097      */
00098     void analyze_potential_recruit_combat();
00099 
00100     std::map<std::string,int> unit_combat_scores_;
00101 
00102     /**
00103      * Rates two unit types for their suitability against each other.
00104      * Returns 0 if the units are equally matched,
00105      * a positive number if a is suited against b,
00106      * and a negative number if b is suited against a.
00107      */
00108     int compare_unit_types(const unit_type& a, const unit_type& b) const;
00109 
00110     /**
00111      * calculates the average resistance unit type a has against the attacks of
00112      * unit type b.
00113      */
00114     int average_resistance_against(const unit_type& a, const unit_type& b) const;
00115 };
00116 
00117 //============================================================================
00118 
00119 class combat_phase : public candidate_action {
00120 public:
00121 
00122     combat_phase( rca_context &context, const config &cfg );
00123 
00124     virtual ~combat_phase();
00125 
00126     virtual double evaluate();
00127 
00128     virtual void execute();
00129 private:
00130     attack_analysis best_analysis_;
00131     double choice_rating_;
00132 
00133 };
00134 
00135 //============================================================================
00136 
00137 class move_leader_to_goals_phase : public candidate_action {
00138 public:
00139 
00140     move_leader_to_goals_phase( rca_context &context, const config &cfg );
00141 
00142     virtual ~move_leader_to_goals_phase();
00143 
00144     virtual double evaluate();
00145 
00146     virtual void execute();
00147 private:
00148 
00149     void remove_goal(const std::string &id);
00150 
00151     bool auto_remove_;
00152     map_location dst_;
00153     std::string id_;
00154     move_result_ptr move_;
00155 };
00156 
00157 //============================================================================
00158 
00159 class move_leader_to_keep_phase : public candidate_action {
00160 public:
00161 
00162     move_leader_to_keep_phase( rca_context &context, const config &cfg );
00163 
00164     virtual ~move_leader_to_keep_phase();
00165 
00166     virtual double evaluate();
00167 
00168     virtual void execute();
00169 
00170 private:
00171     move_result_ptr move_;
00172 };
00173 
00174 //============================================================================
00175 
00176 class get_villages_phase : public candidate_action {
00177 public:
00178 
00179     get_villages_phase( rca_context &context, const config& cfg );
00180 
00181     virtual ~get_villages_phase();
00182 
00183     virtual double evaluate();
00184 
00185     virtual void execute();
00186 private:
00187     /** Location of the keep the closest to our leader. */
00188     map_location keep_loc_;
00189 
00190     /** Locaton of our leader. */
00191     map_location leader_loc_;
00192 
00193     /** The best possible location for our leader if it can't reach a village. */
00194     map_location best_leader_loc_;
00195 
00196     /** debug log level for AI enabled? */
00197     bool debug_;
00198 
00199     typedef std::map<map_location /* unit location */,
00200         std::vector<map_location /* villages we can reach */> > treachmap;
00201 
00202     typedef std::vector<std::pair<map_location /* destination */,
00203         map_location /* start */ > > tmoves;
00204 
00205 
00206     // The list of moves we want to make
00207     tmoves moves_;
00208 
00209 
00210     /** Dispatches all units to their best location. */
00211     void dispatch(treachmap& reachmap, tmoves& moves);
00212 
00213 
00214     /**
00215      * Dispatches all units who can reach one village.
00216      * Returns true if it modified reachmap isn't empty.
00217      */
00218     bool dispatch_unit_simple(treachmap& reachmap, tmoves& moves);
00219 
00220 
00221     /*
00222      * Dispatches units to villages which can only be reached by one unit.
00223      * Returns true if modified reachmap and reachmap isn't empty.
00224      */
00225     bool dispatch_village_simple(
00226         treachmap& reachmap, tmoves& moves, size_t& village_count);
00227 
00228 
00229     /** Removes a village for all units, returns true if anything is deleted. */
00230     bool remove_village(
00231         treachmap& reachmap, tmoves& moves, const map_location& village);
00232 
00233 
00234     /** Removes a unit which can't reach any village anymore. */
00235     treachmap::iterator remove_unit(
00236         treachmap& reachmap, tmoves& moves, treachmap::iterator unit);
00237 
00238 
00239     /** Dispatches the units to a village after the simple dispatching failed. */
00240     void dispatch_complex(
00241         treachmap& reachmap, tmoves& moves, const size_t village_count);
00242 
00243 
00244     /** Dispatches all units to a village, every unit can reach every village. */
00245     void full_dispatch(treachmap& reachmap, tmoves& moves);
00246 
00247 
00248     /** Shows which villages every unit can reach (debug function). */
00249     void dump_reachmap(treachmap& reachmap);
00250 
00251 
00252     void get_villages(
00253         const move_map &dstsrc, const move_map &enemy_dstsrc,
00254         unit_map::const_iterator &leader);
00255 
00256 
00257     void find_villages(
00258         treachmap& reachmap,
00259         tmoves& moves,
00260         const std::multimap<map_location,map_location>& dstsrc,
00261         const std::multimap<map_location,map_location>& enemy_dstsrc);
00262 
00263 };
00264 
00265 //============================================================================
00266 
00267 class get_healing_phase : public candidate_action {
00268 public:
00269 
00270     get_healing_phase( rca_context &context, const config& cfg );
00271 
00272     virtual ~get_healing_phase();
00273 
00274     virtual double evaluate();
00275 
00276     virtual void execute();
00277 private:
00278 
00279     move_result_ptr move_;
00280 };
00281 
00282 //============================================================================
00283 
00284 class retreat_phase : public candidate_action {
00285 public:
00286 
00287     retreat_phase( rca_context &context, const config &cfg );
00288 
00289     virtual ~retreat_phase();
00290 
00291     virtual double evaluate();
00292 
00293     virtual void execute();
00294 private:
00295 
00296     bool should_retreat(const map_location& loc, const unit_map::const_iterator& un, const move_map &srcdst, const move_map &dstsrc, double caution);
00297 
00298     move_result_ptr move_;
00299 
00300 };
00301 
00302 //============================================================================
00303 
00304 class simple_move_and_targeting_phase : public candidate_action {
00305 public:
00306 
00307     simple_move_and_targeting_phase( rca_context &context, const config &cfg );
00308 
00309     virtual ~simple_move_and_targeting_phase();
00310 
00311     virtual double evaluate();
00312 
00313     virtual void execute();
00314 
00315 private:
00316 
00317     move_result_ptr move_;
00318 };
00319 
00320 
00321 //============================================================================
00322 
00323 class leader_control_phase : public candidate_action {
00324 public:
00325 
00326     leader_control_phase( rca_context &context, const config &cfg );
00327 
00328     virtual ~leader_control_phase();
00329 
00330     virtual double evaluate();
00331 
00332     virtual void execute();
00333 };
00334 
00335 
00336 //============================================================================
00337 class passive_leader_shares_keep_phase : public candidate_action {
00338 public:
00339 
00340     passive_leader_shares_keep_phase( rca_context &context, const config &cfg );
00341 
00342     virtual ~passive_leader_shares_keep_phase();
00343 
00344     virtual double evaluate();
00345 
00346     virtual void execute();
00347 };
00348 //============================================================================
00349 
00350 } // end of namespace testing_ai_default
00351 
00352 } // end of namespace ai
00353 
00354 #ifdef _MSC_VER
00355 #pragma warning(pop)
00356 #endif
00357 
00358 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Wed May 23 2012 01:02:32 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs