ai Class Reference

#include <ai.hpp>

Inheritance diagram for ai:

Inheritance graph
[legend]
Collaboration diagram for ai:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 ai (ai_interface::info &info)
virtual ~ai ()
virtual void play_turn ()
 Function that is called when the AI must play its turn.
defensive_position const & best_defensive_position (const location &unit, const move_map &dstsrc, const move_map &srcdst, const move_map &enemy_dstsrc)
void invalidate_defensive_position_cache ()
bool leader_can_reach_keep ()
bool attack_close (const location &loc) const
 Return true iff there has been another attack this turn 'close' to this one.

Protected Member Functions

virtual void do_move ()
virtual bool do_combat (std::map< gamemap::location, paths > &possible_moves, const move_map &srcdst, const move_map &dstsrc, const move_map &enemy_srcdst, const move_map &enemy_dstsrc)
virtual bool get_villages (std::map< gamemap::location, paths > &possible_moves, const move_map &dstsrc, const move_map &enemy_dstsrc, unit_map::iterator &leader)
virtual bool get_healing (std::map< gamemap::location, paths > &possible_moves, const move_map &srcdst, const move_map &enemy_dstsrc)
virtual bool retreat_units (std::map< gamemap::location, paths > &possible_moves, const move_map &srcdst, const move_map &dstsrc, const move_map &enemy_dstsrc, unit_map::const_iterator leader)
virtual bool move_to_targets (std::map< gamemap::location, paths > &possible_moves, move_map &srcdst, move_map &dstsrc, const move_map &enemy_dstsrc, unit_map::const_iterator leader)
virtual bool should_retreat (const gamemap::location &loc, const unit_map::const_iterator un, const move_map &srcdst, const move_map &dstsrc, const move_map &enemy_dstsrc, double caution)
virtual void do_recruitment ()
virtual void move_leader_to_keep (const move_map &enemy_dstsrc)
virtual void move_leader_after_recruit (const move_map &srcdst, const move_map &dstsrc, const move_map &enemy_dstsrc)
virtual void move_leader_to_goals (const move_map &enemy_dstsrc)
virtual bool recruit_usage (const std::string &usage)
virtual bool desperate_attack (const gamemap::location &loc)
 There is no real hope for us: we should try to do some damage to the enemy.
void remove_unit_from_moves (const gamemap::location &u, move_map &srcdst, move_map &dstsrc)
void find_threats ()
 Find enemy units that threaten our valuable assets.
location move_unit (location from, location to, std::map< location, paths > &possible_moves)
 Our own version of 'move_unit'.
void attack_enemy (const location &attacking_unit, const location &target, int att_weapon, int def_weapon)
 Our own version of 'attack_enemy'. We record all attacks to support group attacking.
bool multistep_move_possible (const location &from, const location &to, const location &via, const std::map< location, paths > &possible_moves) const
 Sees if it's possible for a unit to move 'from' -> 'via' -> 'to' all in one turn.
virtual void do_attack_analysis (const location &loc, const move_map &srcdst, const move_map &dstsrc, const move_map &fullmove_srcdst, const move_map &fullmove_dstsrc, const move_map &enemy_srcdst, const move_map &enemy_dstsrc, const location *tiles, bool *used_locations, std::vector< location > &units, std::vector< attack_analysis > &result, attack_analysis &cur_analysis)
 Analyze possibility of attacking target on 'loc'.
virtual double power_projection (const gamemap::location &loc, const move_map &dstsrc, bool use_terrain=true) const
 Function which finds how much 'power' a side can attack a certain location with.
virtual std::vector
< attack_analysis
analyze_targets (const move_map &srcdst, const move_map &dstsrc, const move_map &enemy_srcdst, const move_map &enemy_dstsrc)
bool is_accessible (const location &loc, const move_map &dstsrc) const
virtual std::vector< targetfind_targets (unit_map::const_iterator leader, const move_map &enemy_dstsrc)
virtual location form_group (const std::vector< location > &route, const move_map &dstsrc, std::set< location > &units)
 Function to form a group of units suitable for moving along the route, 'route'.
virtual void enemies_along_path (const std::vector< location > &route, const move_map &dstsrc, std::set< location > &units)
 Return the group of enemies that threaten a certain path.
virtual bool move_group (const location &dst, const std::vector< location > &route, const std::set< location > &units)
virtual double rate_group (const std::set< location > &group, const std::vector< location > &battlefield) const
virtual double compare_groups (const std::set< location > &our_group, const std::set< location > &enemy_groups, const std::vector< location > &battlefield) const
virtual std::pair< location,
location
choose_move (std::vector< target > &targets, const move_map &srcdst, const move_map &dstsrc, const move_map &enemy_dstsrc)
virtual int rate_terrain (const unit &u, const location &loc)
 Rates the value of moving onto certain terrain for a unit.
void add_target (const target &tgt)
virtual void analyze_potential_recruit_movements ()
 Analyze all the units that this side can recruit and rate their movement types.
virtual void analyze_potential_recruit_combat ()
 Analyze all the units that this side can recruit and rate their fighting suitability against enemy units.
virtual int compare_unit_types (const unit_type &a, const unit_type &b) const
 Rates two unit types for their suitability against each other.
virtual int average_resistance_against (const unit_type &a, const unit_type &b) const
 calculates the average resistance unit type a has against the attacks of unit type b.
const std::set< location > & keeps ()
 Functions to deal with keeps.
const locationnearest_keep (const location &loc)
void access_points (const move_map &srcdst, const location &u, const location &dst, std::vector< location > &out)
 Function which, given a unit position, and a position the unit wants to get to in two turns, will return all possible positions the unit can move to, that will make the destination position accessible next turn.
const std::set< location > & avoided_locations ()
 Function which gets the areas of the map that this AI has been instructed to avoid.
int attack_depth ()

Protected Attributes

std::map< location,
defensive_position
defensive_position_cache_
bool threats_found_
std::set< locationattacks_
game_displaydisp_
const gamemapmap_
unit_mapunits_
std::vector< team > & teams_
unsigned int team_num_
const gamestatusstate_
bool consider_combat_
std::vector< targetadditional_targets_
std::map< std::string, int > unit_movement_scores_
std::set< std::string > not_recommended_units_
std::map< std::string, int > unit_combat_scores_
std::set< locationkeeps_
std::set< locationavoid_
std::map< std::pair< location,
const unit_type * >, std::pair
< battle_context::unit_stats,
battle_context::unit_stats > > 
unit_stats_cache_
 Weapon choice cache, to speed simulations.
int attack_depth_

Private Member Functions

void find_villages (std::map< gamemap::location, std::vector< gamemap::location > > &reachmap, std::vector< std::pair< gamemap::location, gamemap::location > > &moves, const std::multimap< gamemap::location, gamemap::location > &dstsrc, const std::map< gamemap::location, paths > &possible_moves, const std::multimap< gamemap::location, gamemap::location > &enemy_dstsrc) const

Friends

struct attack_analysis

Classes

struct  attack_analysis
struct  defensive_position
struct  target


Detailed Description

Definition at line 27 of file ai.hpp.


Constructor & Destructor Documentation

ai::ai ( ai_interface::info info  ) 

Definition at line 233 of file ai.cpp.

virtual ai::~ai (  )  [inline, virtual]

Definition at line 31 of file ai.hpp.


Member Function Documentation

void ai::play_turn (  )  [virtual]

Function that is called when the AI must play its turn.

Derived classes should implement their AI algorithm in this function.

Implements ai_interface.

Reimplemented in formula_ai.

Definition at line 872 of file ai.cpp.

References consider_combat_, do_move(), game_events::fire(), and lg::wml_error.

const ai::defensive_position & ai::best_defensive_position ( const location unit,
const move_map dstsrc,
const move_map srcdst,
const move_map enemy_dstsrc 
)

void ai::invalidate_defensive_position_cache (  )  [inline]

Definition at line 62 of file ai.hpp.

References defensive_position_cache_.

Referenced by do_move().

bool ai::leader_can_reach_keep (  ) 

bool ai::attack_close ( const location loc  )  const

Return true iff there has been another attack this turn 'close' to this one.

Definition at line 682 of file ai.cpp.

References attacks_, and distance_between().

Referenced by ai::attack_analysis::rating().

void ai::do_move (  )  [protected, virtual]

bool ai::do_combat ( std::map< gamemap::location, paths > &  possible_moves,
const move_map srcdst,
const move_map dstsrc,
const move_map enemy_srcdst,
const move_map enemy_dstsrc 
) [protected, virtual]

bool ai::get_villages ( std::map< gamemap::location, paths > &  possible_moves,
const move_map dstsrc,
const move_map enemy_dstsrc,
unit_map::iterator leader 
) [protected, virtual]

bool ai::get_healing ( std::map< gamemap::location, paths > &  possible_moves,
const move_map srcdst,
const move_map enemy_dstsrc 
) [protected, virtual]

bool ai::retreat_units ( std::map< gamemap::location, paths > &  possible_moves,
const move_map srcdst,
const move_map dstsrc,
const move_map enemy_dstsrc,
unit_map::const_iterator  leader 
) [protected, virtual]

bool ai::move_to_targets ( std::map< gamemap::location, paths > &  possible_moves,
move_map srcdst,
move_map dstsrc,
const move_map enemy_dstsrc,
unit_map::const_iterator  leader 
) [protected, virtual]

bool ai::should_retreat ( const gamemap::location loc,
const unit_map::const_iterator  un,
const move_map srcdst,
const move_map dstsrc,
const move_map enemy_dstsrc,
double  caution 
) [protected, virtual]

void ai::do_recruitment (  )  [protected, virtual]

void ai::move_leader_to_keep ( const move_map enemy_dstsrc  )  [protected, virtual]

void ai::move_leader_after_recruit ( const move_map srcdst,
const move_map dstsrc,
const move_map enemy_dstsrc 
) [protected, virtual]

void ai::move_leader_to_goals ( const move_map enemy_dstsrc  )  [protected, virtual]

bool ai::recruit_usage ( const std::string &  usage  )  [protected, virtual]

bool ai::desperate_attack ( const gamemap::location loc  )  [protected, virtual]

There is no real hope for us: we should try to do some damage to the enemy.

We can spend some cycles here, since it's rare.

Definition at line 706 of file ai_attack.cpp.

References attack_enemy(), unit::attacks(), ai_interface::current_team(), unit_map::end(), unit_map::find(), find_visible_unit(), get_adjacent_tiles(), battle_context::get_attacker_stats(), battle_context::get_defender_stats(), unit::hitpoints(), team::is_enemy(), LOG_AI, map_, state_, teams_, unit::type_id(), and units_.

Referenced by move_leader_after_recruit(), and retreat_units().

void ai::remove_unit_from_moves ( const gamemap::location u,
move_map srcdst,
move_map dstsrc 
) [protected]

Definition at line 786 of file ai.cpp.

Referenced by do_move().

void ai::find_threats (  )  [protected]

gamemap::location ai::move_unit ( location  from,
location  to,
std::map< location, paths > &  possible_moves 
) [protected]

void ai::attack_enemy ( const location attacking_unit,
const location target,
int  att_weapon,
int  def_weapon 
) [protected]

Our own version of 'attack_enemy'. We record all attacks to support group attacking.

Definition at line 693 of file ai.cpp.

References ai_interface::attack_enemy(), and attacks_.

Referenced by desperate_attack(), do_combat(), formula_ai::execute_variant(), move_to_targets(), and move_unit().

bool ai::multistep_move_possible ( const location from,
const location to,
const location via,
const std::map< location, paths > &  possible_moves 
) const [protected]

Sees if it's possible for a unit to move 'from' -> 'via' -> 'to' all in one turn.

Definition at line 581 of file ai.cpp.

References unit_map::count(), ai_interface::current_team(), unit_map::end(), unit_map::find(), LOG_AI, map_, paths::routes, unit::set_movement(), teams_, and units_.

Referenced by move_unit().

void ai::do_attack_analysis ( const location loc,
const move_map srcdst,
const move_map dstsrc,
const move_map fullmove_srcdst,
const move_map fullmove_dstsrc,
const move_map enemy_srcdst,
const move_map enemy_dstsrc,
const location tiles,
bool *  used_locations,
std::vector< location > &  units,
std::vector< attack_analysis > &  result,
attack_analysis cur_analysis 
) [protected, virtual]

double ai::power_projection ( const gamemap::location loc,
const move_map dstsrc,
bool  use_terrain = true 
) const [protected, virtual]

Function which finds how much 'power' a side can attack a certain location with.

This is basically the maximum hp of damage that can be inflicted upon a unit on loc by full-health units, multiplied by the defense these units will have. (if 'use_terrain' is false, then it will be multiplied by 0.5)

Definition at line 603 of file ai_attack.cpp.

References unit::alignment(), unit::attacks(), unit_type::CHAOTIC, unit::defense_modifier(), unit_map::end(), unit_map::find(), get_adjacent_tiles(), gamestatus::get_time_of_day(), unit::hitpoints(), gamemap::is_village(), unit_type::LAWFUL, time_of_day::lawful_bonus, map_, unit::max_hitpoints(), gamemap::on_board(), pos, state_, units_, and gamemap::location::valid().

Referenced by best_defensive_position(), choose_move(), do_attack_analysis(), find_targets(), get_healing(), get_villages(), move_leader_to_goals(), retreat_units(), and should_retreat().

std::vector< ai::attack_analysis > ai::analyze_targets ( const move_map srcdst,
const move_map dstsrc,
const move_map enemy_srcdst,
const move_map enemy_dstsrc 
) [protected, virtual]

bool ai::is_accessible ( const location loc,
const move_map dstsrc 
) const [protected]

Definition at line 2111 of file ai.cpp.

References get_adjacent_tiles().

Referenced by move_leader_after_recruit().

std::vector< ai::target > ai::find_targets ( unit_map::const_iterator  leader,
const move_map enemy_dstsrc 
) [protected, virtual]

gamemap::location ai::form_group ( const std::vector< location > &  route,
const move_map dstsrc,
std::set< location > &  units 
) [protected, virtual]

Function to form a group of units suitable for moving along the route, 'route'.

Returns the location which the group may reach this turn. Stores the locations of the units in the group in 'units'

Definition at line 198 of file ai_move.cpp.

References unit_map::count(), unit_map::end(), unit_map::find(), and units_.

Referenced by choose_move().

void ai::enemies_along_path ( const std::vector< location > &  route,
const move_map dstsrc,
std::set< location > &  units 
) [protected, virtual]

Return the group of enemies that threaten a certain path.

Definition at line 239 of file ai_move.cpp.

References get_adjacent_tiles().

Referenced by choose_move().

bool ai::move_group ( const location dst,
const std::vector< location > &  route,
const std::set< location > &  units 
) [protected, virtual]

double ai::rate_group ( const std::set< location > &  group,
const std::vector< location > &  battlefield 
) const [protected, virtual]

double ai::compare_groups ( const std::set< location > &  our_group,
const std::set< location > &  enemy_groups,
const std::vector< location > &  battlefield 
) const [protected, virtual]

Definition at line 382 of file ai_move.cpp.

References a, b, and rate_group().

Referenced by choose_move().

std::pair< gamemap::location, gamemap::location > ai::choose_move ( std::vector< target > &  targets,
const move_map srcdst,
const move_map dstsrc,
const move_map enemy_dstsrc 
) [protected, virtual]

int ai::rate_terrain ( const unit u,
const location loc 
) [protected, virtual]

Rates the value of moving onto certain terrain for a unit.

Definition at line 2035 of file ai.cpp.

References unit::defense_modifier(), unit::get_ability_bool(), gamemap::get_terrain(), gamemap::gives_healing(), gamemap::is_village(), map_, team_num_, teams_, and village_owner().

Referenced by do_attack_analysis().

void ai::add_target ( const target tgt  )  [inline, protected]

Definition at line 259 of file ai.hpp.

References additional_targets_.

Referenced by do_combat(), find_threats(), and get_villages().

void ai::analyze_potential_recruit_movements (  )  [protected, virtual]

Analyze all the units that this side can recruit and rate their movement types.

Ratings will be placed in 'unit_movement_scores_', with lower scores being better, and the lowest possible rating being '10'.

Definition at line 1603 of file ai.cpp.

References a_star_search(), ai_interface::current_team(), boost::foreach_detail_::end(), unit_map::end(), find_leader(), find_targets(), ai_interface::get_info(), gamemap::h(), LOG_AI, log_scope2, ai_interface::info::map, map, map_, paths::route::move_left, nearest_keep(), not_recommended_units_, gamemap::on_board(), team::recruits(), paths::route::steps, utils::string_bool(), team_num_, teams, teams_, unit_type_data::types(), unit_movement_scores_, units_, unit::usage(), and gamemap::w().

Referenced by do_recruitment().

void ai::analyze_potential_recruit_combat (  )  [protected, virtual]

Analyze all the units that this side can recruit and rate their fighting suitability against enemy units.

Ratings will be placed in 'unit_combat_scores_', with a '0' rating indicating that the unit is 'average' against enemy units, negative ratings meaning they are poorly suited, and positive ratings meaning they are well suited.

Definition at line 1522 of file ai.cpp.

References _, unit_map::begin(), compare_unit_types(), unit::cost(), ai_interface::current_team(), unit_map::end(), boost::foreach_detail_::end(), unit::hitpoints(), team::is_enemy(), LOG_AI, log_scope2, unit::max_hitpoints(), not_recommended_units_, team::recruits(), utils::string_bool(), unit::type_id(), unit_type_data::types(), unit_combat_scores_, units_, and VALIDATE.

Referenced by do_recruitment().

int ai::compare_unit_types ( const unit_type a,
const unit_type b 
) const [protected, virtual]

Rates two unit types for their suitability against each other.

Definition at line 1511 of file ai.cpp.

References average_resistance_against(), unit_type::id(), and LOG_AI.

Referenced by analyze_potential_recruit_combat().

int ai::average_resistance_against ( const unit_type a,
const unit_type b 
) const [protected, virtual]

const std::set< gamemap::location > & ai::keeps (  )  [protected]

Functions to deal with keeps.

Definition at line 2125 of file ai.cpp.

References get_adjacent_tiles(), gamemap::h(), gamemap::is_castle(), gamemap::is_keep(), keeps_, loc, map_, and gamemap::w().

Referenced by nearest_keep().

const gamemap::location & ai::nearest_keep ( const location loc  )  [protected]

void ai::access_points ( const move_map srcdst,
const location u,
const location dst,
std::vector< location > &  out 
) [protected]

Function which, given a unit position, and a position the unit wants to get to in two turns, will return all possible positions the unit can move to, that will make the destination position accessible next turn.

Definition at line 774 of file ai_move.cpp.

References a_star_search(), ai_interface::current_team(), distance_between(), unit_map::end(), unit_map::find(), gamemap::h(), loc, map_, paths::route::steps, teams_, units_, and gamemap::w().

Referenced by choose_move().

const std::set< gamemap::location > & ai::avoided_locations (  )  [protected]

Function which gets the areas of the map that this AI has been instructed to avoid.

Definition at line 2171 of file ai.cpp.

References a, team::ai_parameters(), avoid_, unit_map::begin(), ai_interface::current_team(), unit_map::end(), config::get_children(), and parse_location_range().

Referenced by choose_move(), do_move(), move_leader_after_recruit(), and retreat_units().

int ai::attack_depth (  )  [protected]

Definition at line 2191 of file ai.cpp.

References team::ai_parameters(), attack_depth_, and ai_interface::current_team().

Referenced by do_attack_analysis().

void ai::find_villages ( std::map< gamemap::location, std::vector< gamemap::location > > &  reachmap,
std::vector< std::pair< gamemap::location, gamemap::location > > &  moves,
const std::multimap< gamemap::location, gamemap::location > &  dstsrc,
const std::map< gamemap::location, paths > &  possible_moves,
const std::multimap< gamemap::location, gamemap::location > &  enemy_dstsrc 
) const [private]

Referenced by get_villages().


Friends And Related Function Documentation

friend struct attack_analysis [friend]

Definition at line 316 of file ai.hpp.

Referenced by formula_ai::get_value().


Member Data Documentation

Definition at line 71 of file ai.hpp.

Referenced by best_defensive_position(), and invalidate_defensive_position_cache().

bool ai::threats_found_ [protected]

Definition at line 112 of file ai.hpp.

Referenced by find_threats().

std::set<location> ai::attacks_ [protected]

Definition at line 122 of file ai.hpp.

Referenced by attack_close(), and attack_enemy().

game_display& ai::disp_ [protected]

Definition at line 250 of file ai.hpp.

const gamemap& ai::map_ [protected]

unit_map& ai::units_ [protected]

std::vector<team>& ai::teams_ [protected]

unsigned int ai::team_num_ [protected]

const gamestatus& ai::state_ [protected]

bool ai::consider_combat_ [protected]

Definition at line 256 of file ai.hpp.

Referenced by do_move(), and play_turn().

std::vector<target> ai::additional_targets_ [protected]

Definition at line 257 of file ai.hpp.

Referenced by add_target(), and move_to_targets().

std::map<std::string,int> ai::unit_movement_scores_ [protected]

Definition at line 268 of file ai.hpp.

Referenced by analyze_potential_recruit_movements().

std::set<std::string> ai::not_recommended_units_ [protected]

std::map<std::string,int> ai::unit_combat_scores_ [protected]

Definition at line 279 of file ai.hpp.

Referenced by analyze_potential_recruit_combat().

std::set<location> ai::keeps_ [protected]

Definition at line 295 of file ai.hpp.

Referenced by keeps().

std::set<location> ai::avoid_ [protected]

Definition at line 308 of file ai.hpp.

Referenced by avoided_locations().

Weapon choice cache, to speed simulations.

Definition at line 312 of file ai.hpp.

Referenced by ai::attack_analysis::analyze(), and analyze_targets().

int ai::attack_depth_ [protected]

Definition at line 315 of file ai.hpp.

Referenced by attack_depth().


The documentation for this class was generated from the following files:

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs