ai/default/contexts.hpp

Go to the documentation of this file.
00001 /* $Id: contexts.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2009 - 2012 by Yurii Chernyi <terraninfo@terraninfo.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /**
00017  * @file
00018  * Default AI contexts
00019  */
00020 
00021 #ifndef AI_DEFAULT_CONTEXTS_HPP_INCLUDED
00022 #define AI_DEFAULT_CONTEXTS_HPP_INCLUDED
00023 
00024 #include "../contexts.hpp"
00025 #include "formula_callable.hpp"
00026 
00027 #ifdef _MSC_VER
00028 #pragma warning(push)
00029 //silence "inherits via dominance" warnings
00030 #pragma warning(disable:4250)
00031 #endif
00032 
00033 //============================================================================
00034 namespace ai {
00035 
00036 
00037 struct target {
00038     enum TYPE { VILLAGE, LEADER, EXPLICIT, THREAT, BATTLE_AID, MASS, SUPPORT };
00039 
00040     target(const map_location& pos, double val, TYPE target_type=VILLAGE) : loc(pos), value(val), type(target_type)
00041     {}
00042     map_location loc;
00043     double value;
00044 
00045     TYPE type;
00046     ///@todo 1.7: ai goals: this is a 'target' marker class which should be expanded with additional information which is generic enough to apply to all targets.
00047 };
00048 
00049 
00050 class attack_analysis : public game_logic::formula_callable
00051 {
00052 public:
00053     attack_analysis() :
00054         game_logic::formula_callable(),
00055         target(),
00056         movements(),
00057         target_value(0.0),
00058         avg_losses(0.0),
00059         chance_to_kill(0.0),
00060         avg_damage_inflicted(0.0),
00061         target_starting_damage(0),
00062         avg_damage_taken(0.0),
00063         resources_used(0.0),
00064         terrain_quality(0.0),
00065         alternative_terrain_quality(0.0),
00066         vulnerability(0.0),
00067         support(0.0),
00068         leader_threat(false),
00069         uses_leader(false),
00070         is_surrounded(false)
00071     {
00072     }
00073 
00074     void analyze(const gamemap& map, unit_map& units,
00075                  const readonly_context& ai_obj,
00076                  const move_map& dstsrc, const move_map& srcdst,
00077                  const move_map& enemy_dstsrc, double aggression);
00078 
00079     double rating(double aggression, const readonly_context& ai_obj) const;
00080     variant get_value(const std::string& key) const;
00081     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00082 
00083     bool attack_close(const map_location& loc) const;
00084 
00085     map_location target;
00086     std::vector<std::pair<map_location,map_location> > movements;
00087 
00088     /** The value of the unit being targeted. */
00089     double target_value;
00090 
00091     /** The value on average, of units lost in the combat. */
00092     double avg_losses;
00093 
00094     /** Estimated % chance to kill the unit. */
00095     double chance_to_kill;
00096 
00097     /** The average hitpoints damage inflicted. */
00098     double avg_damage_inflicted;
00099 
00100     int target_starting_damage;
00101 
00102     /** The average hitpoints damage taken. */
00103     double avg_damage_taken;
00104 
00105     /** The sum of the values of units used in the attack. */
00106     double resources_used;
00107 
00108     /** The weighted average of the % chance to hit each attacking unit. */
00109     double terrain_quality;
00110 
00111     /**
00112      * The weighted average of the % defense of the best possible terrain
00113      * that the attacking units could reach this turn, without attacking
00114      * (good for comparison to see just how good/bad 'terrain_quality' is).
00115      */
00116     double alternative_terrain_quality;
00117 
00118     /**
00119      * The vulnerability is the power projection of enemy units onto the hex
00120      * we're standing on. support is the power projection of friendly units.
00121      */
00122     double vulnerability, support;
00123 
00124     /** Is true if the unit is a threat to our leader. */
00125     bool leader_threat;
00126 
00127     /** Is true if this attack sequence makes use of the leader. */
00128     bool uses_leader;
00129 
00130     /** Is true if the units involved in this attack sequence are surrounded. */
00131     bool is_surrounded;
00132 
00133 };
00134 
00135 
00136 class default_ai_context;
00137 class default_ai_context : public virtual readwrite_context{
00138 public:
00139 
00140     virtual int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes) = 0;
00141 
00142 
00143     /** Constructor */
00144     default_ai_context();
00145 
00146 
00147     /** Destructor */
00148     virtual ~default_ai_context();
00149 
00150 
00151     virtual const std::vector<target>& additional_targets() const = 0;
00152 
00153 
00154     virtual void add_target(const target& t) const = 0;
00155 
00156 
00157     virtual void clear_additional_targets() const = 0;
00158 
00159 
00160     virtual default_ai_context& get_default_ai_context() = 0;
00161 
00162 
00163     virtual std::vector<target> find_targets(const move_map& enemy_dstsrc) = 0;
00164 
00165 
00166     virtual int rate_terrain(const unit& u, const map_location& loc) const = 0;
00167 
00168 
00169     virtual config to_default_ai_context_config() const = 0;
00170 
00171 
00172 };
00173 
00174 
00175 // proxies
00176 class default_ai_context_proxy : public virtual default_ai_context, public virtual readwrite_context_proxy {
00177 public:
00178 
00179     int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes)
00180     {
00181         return target_->count_free_hexes_in_castle(loc, checked_hexes);
00182     }
00183 
00184 
00185     default_ai_context_proxy()
00186         : target_(NULL)
00187     {
00188     }
00189 
00190 
00191     virtual ~default_ai_context_proxy();
00192 
00193 
00194     virtual const std::vector<target>& additional_targets() const
00195     {
00196         return target_->additional_targets();
00197     }
00198 
00199 
00200     virtual void add_target(const target& t) const
00201     {
00202         target_->add_target(t);
00203     }
00204 
00205 
00206     virtual void clear_additional_targets() const
00207     {
00208         target_->clear_additional_targets();
00209     }
00210 
00211 
00212     virtual default_ai_context& get_default_ai_context()
00213     {
00214         return target_->get_default_ai_context();
00215     }
00216 
00217 
00218     virtual std::vector<target> find_targets(const move_map& enemy_dstsrc)
00219     {
00220         return target_->find_targets(enemy_dstsrc);
00221     }
00222 
00223 
00224     void init_default_ai_context_proxy(default_ai_context &target);
00225 
00226 
00227     virtual int rate_terrain(const unit& u, const map_location& loc) const
00228     {
00229         return target_->rate_terrain(u,loc);
00230     }
00231 
00232 
00233     virtual config to_default_ai_context_config() const
00234     {
00235         return target_->to_default_ai_context_config();
00236     }
00237 
00238 private:
00239     default_ai_context *target_;
00240 };
00241 
00242 
00243 class default_ai_context_impl : public virtual readwrite_context_proxy, public default_ai_context {
00244 public:
00245 
00246     int count_free_hexes_in_castle(const map_location& loc, std::set<map_location> &checked_hexes);
00247 
00248 
00249     default_ai_context_impl(readwrite_context &context, const config &/*cfg*/)
00250         : recursion_counter_(context.get_recursion_count()),additional_targets_()
00251     {
00252         init_readwrite_context_proxy(context);
00253     }
00254 
00255 
00256     virtual ~default_ai_context_impl();
00257 
00258 
00259     virtual default_ai_context& get_default_ai_context();
00260 
00261 
00262     virtual const std::vector<target>& additional_targets() const;
00263 
00264 
00265     virtual void add_target(const target& t) const;
00266 
00267 
00268     virtual void clear_additional_targets() const;
00269 
00270 
00271     int get_recursion_count() const
00272     {
00273         return recursion_counter_.get_count();
00274     }
00275 
00276 
00277     virtual std::vector<target> find_targets(const move_map& enemy_dstsrc);
00278 
00279 
00280     virtual int rate_terrain(const unit& u, const map_location& loc) const;
00281 
00282 
00283     virtual config to_default_ai_context_config() const;
00284 
00285 private:
00286     recursion_counter recursion_counter_;
00287     mutable std::vector<target> additional_targets_;///@todo 1.9 refactor this
00288 
00289 
00290 };
00291 
00292 } //end of namespace ai
00293 
00294 #ifdef _MSC_VER
00295 #pragma warning(pop)
00296 #endif
00297 
00298 
00299 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:28 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs