00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef AI_HPP_INCLUDED
00019 #define AI_HPP_INCLUDED
00020
00021 #include "global.hpp"
00022
00023 #include "actions.hpp"
00024 #include "ai_interface.hpp"
00025 #include "formula_callable.hpp"
00026
00027 class ai : public ai_interface {
00028 public:
00029
00030 ai(ai_interface::info& info);
00031 virtual ~ai() {}
00032
00033 virtual void play_turn();
00034
00035 struct target {
00036 enum TYPE { VILLAGE, LEADER, EXPLICIT, THREAT, BATTLE_AID, MASS, SUPPORT };
00037
00038 target(const location& pos, double val, TYPE target_type=VILLAGE) : loc(pos), value(val), type(target_type)
00039 {}
00040 location loc;
00041 double value;
00042
00043 TYPE type;
00044 };
00045
00046 struct defensive_position {
00047 defensive_position() :
00048 loc(),
00049 chance_to_hit(0),
00050 vulnerability(0.0),
00051 support(0.0)
00052 {}
00053
00054 location loc;
00055 int chance_to_hit;
00056 double vulnerability, support;
00057 };
00058
00059 defensive_position const& best_defensive_position(const location& unit,
00060 const move_map& dstsrc, const move_map& srcdst, const move_map& enemy_dstsrc);
00061
00062 void invalidate_defensive_position_cache() { defensive_position_cache_.clear(); }
00063
00064 bool leader_can_reach_keep();
00065
00066
00067 bool attack_close(const location& loc) const;
00068
00069 protected:
00070
00071 std::map<location,defensive_position> defensive_position_cache_;
00072
00073 virtual void do_move();
00074
00075 virtual bool do_combat(std::map<gamemap::location,paths>& possible_moves,
00076 const move_map& srcdst, const move_map& dstsrc,
00077 const move_map& enemy_srcdst, const move_map& enemy_dstsrc);
00078 virtual bool get_villages(std::map<gamemap::location,paths>& possible_moves,
00079 const move_map& dstsrc, const move_map& enemy_dstsrc,
00080 unit_map::iterator &leader);
00081 virtual bool get_healing(std::map<gamemap::location,paths>& possible_moves,
00082 const move_map& srcdst, const move_map& enemy_dstsrc);
00083 virtual bool retreat_units(std::map<gamemap::location,paths>& possible_moves,
00084 const move_map& srcdst, const move_map& dstsrc,
00085 const move_map& enemy_dstsrc, unit_map::const_iterator leader);
00086 virtual bool move_to_targets(std::map<gamemap::location,paths>& possible_moves,
00087 move_map& srcdst, move_map& dstsrc, const move_map& enemy_dstsrc,
00088 unit_map::const_iterator leader);
00089
00090 virtual bool should_retreat(const gamemap::location& loc,
00091 const unit_map::const_iterator un, const move_map& srcdst,
00092 const move_map& dstsrc, const move_map& enemy_dstsrc, double caution);
00093
00094 virtual void do_recruitment();
00095
00096 virtual void move_leader_to_keep(const move_map& enemy_dstsrc);
00097 virtual void move_leader_after_recruit(const move_map& srcdst,
00098 const move_map& dstsrc, const move_map& enemy_dstsrc);
00099
00100 virtual void move_leader_to_goals(const move_map& enemy_dstsrc);
00101
00102
00103 virtual bool recruit_usage(const std::string& usage);
00104
00105 virtual bool desperate_attack(const gamemap::location &loc);
00106
00107 void remove_unit_from_moves(const gamemap::location& u, move_map& srcdst, move_map& dstsrc);
00108
00109
00110 void find_threats();
00111
00112 bool threats_found_;
00113
00114
00115
00116 location move_unit(location from, location to, std::map<location,paths>& possible_moves);
00117
00118
00119 void attack_enemy(const location& attacking_unit, const location& target,
00120 int att_weapon, int def_weapon);
00121
00122 std::set<location> attacks_;
00123
00124
00125 bool multistep_move_possible(const location& from,
00126 const location& to, const location& via,
00127 const std::map<location,paths>& possible_moves) const;
00128
00129 public:
00130 struct attack_analysis : public game_logic::formula_callable
00131 {
00132 void analyze(const gamemap& map, unit_map& units,
00133 const std::vector<team>& teams,
00134 const gamestatus& status,
00135 class ai& ai_obj,
00136 const move_map& dstsrc, const move_map& srcdst,
00137 const move_map& enemy_dstsrc, double aggression);
00138
00139 double rating(double aggression, class ai& ai_obj) const;
00140 variant get_value(const std::string& key) const;
00141 void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00142
00143 gamemap::location target;
00144 std::vector<std::pair<gamemap::location,gamemap::location> > movements;
00145
00146
00147 double target_value;
00148
00149
00150 double avg_losses;
00151
00152
00153 double chance_to_kill;
00154
00155
00156 double avg_damage_inflicted;
00157
00158 int target_starting_damage;
00159
00160
00161 double avg_damage_taken;
00162
00163
00164 double resources_used;
00165
00166
00167 double terrain_quality;
00168
00169
00170
00171
00172 double alternative_terrain_quality;
00173
00174
00175
00176 double vulnerability, support;
00177
00178
00179 bool leader_threat;
00180
00181
00182 bool uses_leader;
00183
00184
00185 bool is_surrounded;
00186 };
00187
00188 protected:
00189
00190 virtual void do_attack_analysis(
00191 const location& loc,
00192 const move_map& srcdst, const move_map& dstsrc,
00193 const move_map& fullmove_srcdst, const move_map& fullmove_dstsrc,
00194 const move_map& enemy_srcdst, const move_map& enemy_dstsrc,
00195 const location* tiles, bool* used_locations,
00196 std::vector<location>& units,
00197 std::vector<attack_analysis>& result,
00198 attack_analysis& cur_analysis
00199 );
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 virtual double power_projection(const gamemap::location& loc, const move_map& dstsrc,
00212 bool use_terrain=true) const;
00213
00214 virtual std::vector<attack_analysis> analyze_targets(
00215 const move_map& srcdst, const move_map& dstsrc,
00216 const move_map& enemy_srcdst, const move_map& enemy_dstsrc
00217 );
00218
00219 bool is_accessible(const location& loc, const move_map& dstsrc) const;
00220
00221 virtual std::vector<target> find_targets(unit_map::const_iterator leader,
00222 const move_map& enemy_dstsrc);
00223
00224
00225
00226
00227 virtual location form_group(const std::vector<location>& route,
00228 const move_map& dstsrc, std::set<location>& units);
00229
00230
00231 virtual void enemies_along_path(const std::vector<location>& route,
00232 const move_map& dstsrc, std::set<location>& units);
00233
00234 virtual bool move_group(const location& dst, const std::vector<location>& route,
00235 const std::set<location>& units);
00236
00237 virtual double rate_group(const std::set<location>& group,
00238 const std::vector<location>& battlefield) const;
00239
00240 virtual double compare_groups(const std::set<location>& our_group,
00241 const std::set<location>& enemy_groups,
00242 const std::vector<location>& battlefield) const;
00243
00244 virtual std::pair<location,location> choose_move(std::vector<target>& targets,
00245 const move_map& srcdst, const move_map& dstsrc, const move_map& enemy_dstsrc);
00246
00247
00248 virtual int rate_terrain(const unit& u, const location& loc);
00249
00250 game_display& disp_;
00251 const gamemap& map_;
00252 unit_map& units_;
00253 std::vector<team>& teams_;
00254 unsigned int team_num_;
00255 const gamestatus& state_;
00256 bool consider_combat_;
00257 std::vector<target> additional_targets_;
00258
00259 void add_target(const target& tgt) { additional_targets_.push_back(tgt); }
00260
00261
00262
00263
00264
00265
00266 virtual void analyze_potential_recruit_movements();
00267
00268 std::map<std::string,int> unit_movement_scores_;
00269 std::set<std::string> not_recommended_units_;
00270
00271
00272
00273
00274
00275
00276
00277 virtual void analyze_potential_recruit_combat();
00278
00279 std::map<std::string,int> unit_combat_scores_;
00280
00281
00282
00283
00284
00285 virtual int compare_unit_types(const unit_type& a, const unit_type& b) const;
00286
00287
00288
00289 virtual int average_resistance_against(const unit_type& a, const unit_type& b) const;
00290
00291
00292 const std::set<location>& keeps();
00293 const location& nearest_keep(const location& loc);
00294
00295 std::set<location> keeps_;
00296
00297
00298
00299
00300
00301 void access_points(const move_map& srcdst, const location& u,
00302 const location& dst, std::vector<location>& out);
00303
00304
00305
00306 const std::set<location>& avoided_locations();
00307
00308 std::set<location> avoid_;
00309
00310
00311 std::map<std::pair<location,const unit_type *>,
00312 std::pair<battle_context::unit_stats,battle_context::unit_stats> > unit_stats_cache_;
00313
00314 int attack_depth();
00315 int attack_depth_;
00316 friend struct attack_analysis;
00317
00318 private:
00319 void find_villages(
00320
00321 std::map<gamemap::location , std::vector<gamemap::location /* villages we can reach*/> >& reachmap,
00322 std::vector<std::pair<gamemap::location,gamemap::location> >& moves,
00323 const std::multimap<gamemap::location,gamemap::location>& dstsrc,
00324 const std::map<gamemap::location,paths>& possible_moves,
00325 const std::multimap<gamemap::location,gamemap::location>& enemy_dstsrc) const;
00326 };
00327
00328 #endif