Update doxygen comment style.

This commit is contained in:
Mark de Wever 2008-06-23 16:24:55 +00:00
parent d3091952d8
commit fd802000dd
3 changed files with 232 additions and 151 deletions

View file

@ -13,9 +13,10 @@
See the COPYING file for more details.
*/
//! @file ai.cpp
//! Artificial intelligence - The computer commands the enemy.
/**
* @file ai.cpp
* Artificial intelligence - The computer commands the enemy.
*/
#include "ai.hpp"
#include "ai2.hpp"
@ -47,7 +48,7 @@
#define WRN_AI LOG_STREAM(warn, ai)
#define ERR_AI LOG_STREAM(err, ai)
//! A trivial ai that sits around doing absolutely nothing.
/** A trivial ai that sits around doing absolutely nothing. */
class idle_ai : public ai_interface {
public:
idle_ai(info& i) : ai_interface(i) {}
@ -56,7 +57,7 @@ public:
}
};
//! Sample ai, with simple strategy.
/** Sample ai, with simple strategy. */
class sample_ai : public ai_interface {
public:
sample_ai(info& i) : ai_interface(i) {}
@ -814,7 +815,7 @@ void ai::remove_unit_from_moves(const gamemap::location& loc, move_map& srcdst,
namespace {
//! A structure for storing an item we're trying to protect.
/** A structure for storing an item we're trying to protect. */
struct protected_item {
protected_item(double value, int radius, const gamemap::location& loc) :
value(value), radius(radius), loc(loc) {}
@ -889,9 +890,11 @@ void ai::find_threats()
void ai::play_turn()
{
// Protect against a memory over commitment:
//! @todo Not in the mood to figure out the exact cause:
// For some reason -1 hitpoints cause a segmentation fault.
// If -1 hitpoints are sent, we crash :/
/**
* @todo Not in the mood to figure out the exact cause:
* For some reason -1 hitpoints cause a segmentation fault.
* If -1 hitpoints are sent, we crash :/
*/
try {
consider_combat_ = true;
game_events::fire("ai turn");
@ -1121,17 +1124,6 @@ bool ai::do_combat(std::map<gamemap::location,paths>& possible_moves, const move
}
}
//! This function should be called to attack an enemy.
//!
//! @param u The location of the attacking unit. (Note this shouldn't
//! be a reference since attack::attack() can invalidate the
//! unit_map and references to the map are also invalid then.)
//! @param target The location of the target unit. This unit must be in
//! range of the attacking unit's weapon. (See note at param u.)
//! @param weapon The number of the weapon (0-based) which should be used
//! by the attacker. (It must be a valid weapon of the attacker.)
//! @param def_weapon The number of the weapon (0-based) which should be used
//! by the defender. (It must be a valid weapon of the defender.)
void ai_interface::attack_enemy(const location u,
const location target, int weapon, int def_weapon)
{

View file

@ -12,8 +12,7 @@
See the COPYING file for more details.
*/
//! @file ai.hpp
//!
/** @file ai.hpp */
#ifndef AI_HPP_INCLUDED
#define AI_HPP_INCLUDED
@ -64,7 +63,7 @@ public:
bool leader_can_reach_keep();
//! Return true iff there has been another attack this turn 'close' to this one.
/** Return true iff there has been another attack this turn 'close' to this one. */
bool attack_close(const location& loc) const;
protected:
@ -107,22 +106,30 @@ protected:
void remove_unit_from_moves(const gamemap::location& u, move_map& srcdst, move_map& dstsrc);
//! Find enemy units that threaten our valuable assets.
/** Find enemy units that threaten our valuable assets. */
void find_threats();
bool threats_found_;
//! Our own version of 'move_unit'. It is like the version in ai_interface,
//! however if it is the leader moving, it will first attempt recruitment.
/**
* Our own version of 'move_unit'. It is like the version in ai_interface,
* however if it is the leader moving, it will first attempt recruitment.
*/
location move_unit(location from, location to, std::map<location,paths>& possible_moves);
//! Our own version of 'attack_enemy'. We record all attacks to support group attacking.
/**
* Our own version of 'attack_enemy'. We record all attacks to support
* group attacking.
*/
void attack_enemy(const location& attacking_unit, const location& target,
int att_weapon, int def_weapon);
std::set<location> attacks_;
//! Sees if it's possible for a unit to move 'from' -> 'via' -> 'to' all in one turn.
/**
* Sees if it's possible for a unit to move 'from' -> 'via' -> 'to' all in
* one turn.
*/
bool multistep_move_possible(const location& from,
const location& to, const location& via,
const std::map<location,paths>& possible_moves) const;
@ -165,45 +172,49 @@ public:
gamemap::location target;
std::vector<std::pair<gamemap::location,gamemap::location> > movements;
//! The value of the unit being targeted.
/** The value of the unit being targeted. */
double target_value;
//! The value on average, of units lost in the combat
/** The value on average, of units lost in the combat. */
double avg_losses;
//! Estimated % chance to kill the unit
/** Estimated % chance to kill the unit. */
double chance_to_kill;
//! The average hitpoints damage inflicted
/** The average hitpoints damage inflicted. */
double avg_damage_inflicted;
int target_starting_damage;
//! The average hitpoints damage taken
/** The average hitpoints damage taken. */
double avg_damage_taken;
//! The sum of the values of units used in the attack
/** The sum of the values of units used in the attack. */
double resources_used;
//! The weighted average of the % chance to hit each attacking unit
/** The weighted average of the % chance to hit each attacking unit. */
double terrain_quality;
//! The weighted average of the % defense of the best possible terrain
//! that the attacking units could reach this turn, without attacking
//! (good for comparison to see just how good/bad 'terrain_quality' is).
/**
* The weighted average of the % defense of the best possible terrain
* that the attacking units could reach this turn, without attacking
* (good for comparison to see just how good/bad 'terrain_quality' is).
*/
double alternative_terrain_quality;
//! The vulnerability is the power projection of enemy units onto the hex
//! we're standing on. support is the power projection of friendly units.
/**
* The vulnerability is the power projection of enemy units onto the hex
* we're standing on. support is the power projection of friendly units.
*/
double vulnerability, support;
//! Is true if the unit is a threat to our leader.
/** Is true if the unit is a threat to our leader. */
bool leader_threat;
//! Is true if this attack sequence makes use of the leader.
/** Is true if this attack sequence makes use of the leader. */
bool uses_leader;
//! Is true if the units involved in this attack sequence are surrounded.
/** Is true if the units involved in this attack sequence are surrounded. */
bool is_surrounded;
};
@ -221,15 +232,17 @@ protected:
);
//! 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)
//
// Example: 'loc' can be reached by two units, one of whom has a 10-3 attack
// and has 48/48 hp, and can defend at 40% on the adjacent grassland.
// The other has a 8-2 attack, and has 30/40 hp, and can defend at 60% on the adjacent mountain.
// The rating will be 10*3*1.0*0.4 + 8*2*0.75*0.6 = 19.2
/**
* 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)
*
* Example: 'loc' can be reached by two units, one of whom has a 10-3 attack
* and has 48/48 hp, and can defend at 40% on the adjacent grassland.
* The other has a 8-2 attack, and has 30/40 hp, and can defend at 60% on the adjacent mountain.
* The rating will be 10*3*1.0*0.4 + 8*2*0.75*0.6 = 19.2
*/
virtual double power_projection(const gamemap::location& loc, const move_map& dstsrc,
bool use_terrain=true) const;
@ -243,13 +256,17 @@ protected:
virtual std::vector<target> find_targets(unit_map::const_iterator leader,
const move_map& enemy_dstsrc);
//! 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'
/**
* 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'.
*/
virtual location form_group(const std::vector<location>& route,
const move_map& dstsrc, std::set<location>& units);
//! Return the group of enemies that threaten a certain path.
/** Return the group of enemies that threaten a certain path. */
virtual void enemies_along_path(const std::vector<location>& route,
const move_map& dstsrc, std::set<location>& units);
@ -266,7 +283,7 @@ protected:
virtual std::pair<location,location> choose_move(std::vector<target>& targets,
const move_map& srcdst, const move_map& dstsrc, const move_map& enemy_dstsrc);
//! Rates the value of moving onto certain terrain for a unit.
/** Rates the value of moving onto certain terrain for a unit. */
virtual int rate_terrain(const unit& u, const location& loc);
game_display& disp_;
@ -280,56 +297,67 @@ protected:
void add_target(const target& tgt) { additional_targets_.push_back(tgt); }
//! 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'.
/**
* 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'.
*/
virtual void analyze_potential_recruit_movements();
std::map<std::string,int> unit_movement_scores_;
std::set<std::string> not_recommended_units_;
//! 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.
/**
* 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.
*/
virtual void analyze_potential_recruit_combat();
std::map<std::string,int> unit_combat_scores_;
//! Rates two unit types for their suitability against each other.
// Returns 0 if the units are equally matched,
// a positive number if a is suited against b,
// and a negative number if b is suited against a.
/**
* Rates two unit types for their suitability against each other.
* Returns 0 if the units are equally matched,
* a positive number if a is suited against b,
* and a negative number if b is suited against a.
*/
virtual int compare_unit_types(const unit_type& a, const unit_type& b) const;
//! calculates the average resistance unit type a has
//! against the attacks of unit type b.
/**
* calculates the average resistance unit type a has against the attacks of
* unit type b.
*/
virtual int average_resistance_against(const unit_type& a, const unit_type& b) const;
//! Functions to deal with keeps.
/** Functions to deal with keeps. */
const std::set<location>& keeps();
const location& nearest_keep(const location& loc);
std::set<location> keeps_;
//! 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.
/**
* 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.
*/
void access_points(const move_map& srcdst, const location& u,
const location& dst, std::vector<location>& out);
//! Function which gets the areas of the map
//! that this AI has been instructed to avoid.
/**
* Function which gets the areas of the map that this AI has been
* instructed to avoid.
*/
const std::set<location>& avoided_locations();
std::set<location> avoid_;
//! Weapon choice cache, to speed simulations.
/** Weapon choice cache, to speed simulations. */
std::map<std::pair<location,const unit_type *>,
std::pair<battle_context::unit_stats,battle_context::unit_stats> > unit_stats_cache_;

View file

@ -12,8 +12,10 @@
See the COPYING file for more details.
*/
//! @file ai_interface.hpp
//! Interface to the AI.
/**
* @file ai_interface.hpp
* Interface to the AI.
*/
#ifndef AI_INTERFACE_HPP_INCLUDED
#define AI_INTERFACE_HPP_INCLUDED
@ -30,74 +32,89 @@ class gamemap;
class ai_interface : public game_logic::formula_callable {
public:
//! A convenient typedef for the often used 'location' object
/** A convenient typedef for the often used 'location' object. */
typedef gamemap::location location;
//! The standard way in which a map of possible moves is recorded
/** The standard way in which a map of possible moves is recorded. */
typedef std::multimap<location,location> move_map;
//! info is structure which holds references to all the important objects
//! that an AI might need access to, in order to make and implement its decisions.
/**
* info is structure which holds references to all the important objects
* that an AI might need access to, in order to make and implement its
* decisions.
*/
struct info {
info(game_display& disp, gamemap& map, unit_map& units,
std::vector<team>& teams, unsigned int team_num, gamestatus& state, class turn_info& turn_data, class game_state& game_state)
: disp(disp), map(map), units(units), teams(teams),
team_num(team_num), state(state), turn_data_(turn_data), game_state_(game_state) {}
//! The display object, used to draw the moves the AI makes.
/** The display object, used to draw the moves the AI makes. */
game_display& disp;
//! The map of the game -- use this object to find the terrain at any location.
/** The map of the game -- use this object to find the terrain at any location. */
gamemap& map;
//! The map of units. It maps locations -> units.
/** The map of units. It maps locations -> units. */
unit_map& units;
//! A list of the teams in the game.
/** A list of the teams in the game. */
std::vector<team>& teams;
//! The number of the team the AI is.
//! Note: this number is 1-based, so 1 must be subtracted
//! for using it as index of 'teams'.
/**
* The number of the team the AI is.
*
* Note: this number is 1-based, so 1 must be subtracted for using it
* as index of 'teams'.
*/
unsigned int team_num;
//! Information about what turn it is, and what time of day.
/** Information about what turn it is, and what time of day. */
gamestatus& state;
//! The object that allows the player to interact with the game.
//! Should not be used outside of ai_interface.
/**
* The object that allows the player to interact with the game. Should
* not be used outside of ai_interface.
*/
class turn_info& turn_data_;
//! The global game state, because we may set the completion field.
/** The global game state, because we may set the completion field. */
class game_state& game_state_;
};
//! The constructor.
//! All derived classes should take an argument of type info&
//! which they should pass to this constructor.
/**
* The constructor.
*
* All derived classes should take an argument of type info& which they
* should pass to this constructor.
*/
ai_interface(info& arg) : info_(arg), last_interact_(0), user_interact_("ai_user_interact"),
unit_recruited_("ai_unit_recruited"), unit_moved_("ai_unit_moved"),
enemy_attacked_("ai_enemy_attacked") {}
virtual ~ai_interface() {}
//! Function that is called when the AI must play its turn.
//! Derived classes should implement their AI algorithm in this function.
/**
* Function that is called when the AI must play its turn.
* Derived classes should implement their AI algorithm in this function.
*/
virtual void play_turn() = 0;
//! Function called when a a new turn is played
//! Derived persistant AIs should call this function each turn (expect first)
/**
* Function called when a a new turn is played
* Derived persistant AIs should call this function each turn (expect first)
*/
virtual void new_turn() {
last_interact_ = 0;
}
//! Return a reference to the 'team' object for the AI.
/** Return a reference to the 'team' object for the AI. */
team& current_team() { return info_.teams[info_.team_num-1]; }
const team& current_team() const { return info_.teams[info_.team_num-1]; }
//! Show a diagnostic message on the screen.
/** Show a diagnostic message on the screen. */
void diagnostic(const std::string& msg);
//! Display a debug message as a chat message.
/** Display a debug message as a chat message. */
void log_message(const std::string& msg);
// Exposes events to allow for attaching/detaching handlers.
@ -107,61 +124,105 @@ public:
events::generic_event& enemy_attacked() { return enemy_attacked_; }
protected:
//! This function should be called to attack an enemy.
/**
* This function should be called to attack an enemy.
*
* @param u The location of the attacking unit. (Note this shouldn't
* be a reference since attack::attack() can invalidate the
* unit_map and references to the map are also invalid then.)
* @param target The location of the target unit. This unit must be in
* range of the attacking unit's weapon. (See note at param u.)
* @param weapon The number of the weapon (0-based) which should be used
* by the attacker. (It must be a valid weapon of the attacker.)
* @param def_weapon The number of the weapon (0-based) which should be used
* by the defender. (It must be a valid weapon of the defender.)
*/
void attack_enemy(const location u, const location target, int att_weapon, int def_weapon);
//! This function should be called to move a unit.
//! Once the unit has been moved, its movement allowance is set to 0.
//! 'from': the location of the unit being moved.
//! 'to': the location to be moved to. This must be a valid move for the unit.
//! 'possible_moves': the map of possible moves, as obtained from 'calculate_possible_moves'.
/**
* This function should be called to move a unit.
*
* Once the unit has been moved, its movement allowance is set to 0.
* @param from The location of the unit being moved.
* @param to The location to be moved to. This must be a
* valid move for the unit.
* @param possible_moves The map of possible moves, as obtained from
* 'calculate_possible_moves'.
*/
location move_unit(location from, location to, std::map<location,paths>& possible_moves);
//! Identical to 'move_unit', except that the unit's movement
//! isn't set to 0 after the move is complete.
/**
* Identical to 'move_unit', except that the unit's movement
* isn't set to 0 after the move is complete.
*/
location move_unit_partial(location from, location t, std::map<location,paths>& possible_moves);
//! Calculate the moves units may possibly make.
//! 'possible_moves': a map which will be filled with the paths each unit can take
//! to get to every possible destination.
//! You probably don't want to use this object at all, except to pass to 'move_unit'.
//! 'srcdst': a map of units to all their possible destinations
//! 'dstsrc': a map of destinations to all the units that can move to that destination
//! 'enemy': if true, a map of possible moves for enemies will be calculated.
//! If false, a map of possible moves for units on the AI's side will be calculated.
//! The AI's own leader will not be included in this map.
//! 'assume_full_movement': if true, the function will operate on the assumption
//! that all units can move their full movement allotment.
//! 'remove_destinations': a pointer to a set of possible destinations to omit.
void calculate_possible_moves(std::map<location,paths>& possible_moves, move_map& srcdst, move_map& dstsrc, bool enemy, bool assume_full_movement=false,
const std::set<location>* remove_destinations=NULL) const;
/**
* Calculate the moves units may possibly make.
*
* @param possible_moves A map which will be filled with the paths
* each unit can take to get to every possible
* destination. You probably don't want to use
* this object at all, except to pass to
* 'move_unit'.
* @param srcdst A map of units to all their possible
* destinations.
* @param dstsrc A map of destinations to all the units that
* can move to that destination.
* @param enemy if true, a map of possible moves for enemies
* will be calculated. If false, a map of
* possible moves for units on the AI's side
* will be calculated. The AI's own leader will
* not be included in this map.
* @param assume_full_movement
* If true, the function will operate on the
* assumption that all units can move their full
* movement allotment.
* @param remove_destinations a pointer to a set of possible destinations
* to omit.
*/
void calculate_possible_moves(std::map<location,paths>& possible_moves,
move_map& srcdst, move_map& dstsrc, bool enemy,
bool assume_full_movement=false,
const std::set<location>* remove_destinations=NULL) const;
//! A more fundamental version of calculate_possible_moves
//! which allows the use of a speculative unit map.
void calculate_moves(const unit_map& units, std::map<location,paths>& possible_moves, move_map& srcdst, move_map& dstsrc, bool enemy, bool assume_full_movement=false,
const std::set<location>* remove_destinations=NULL, bool see_all=false) const;
/**
* A more fundamental version of calculate_possible_moves which allows the
* use of a speculative unit map.
*/
void calculate_moves(const unit_map& units,
std::map<location,paths>& possible_moves, move_map& srcdst,
move_map& dstsrc, bool enemy, bool assume_full_movement=false,
const std::set<location>* remove_destinations=NULL,
bool see_all=false) const;
//! Recruit a unit. It will recruit the unit with the given name,
//! at the given location, or at an available location to recruit units
//! if 'loc' is not a valid recruiting location.
//!
//! @retval false if recruitment cannot be performed,
//! because there are no available tiles,
//! or not enough money.
/**
* Recruit a unit. It will recruit the unit with the given name,
* at the given location, or at an available location to recruit units
* if 'loc' is not a valid recruiting location.
*
* @retval false If recruitment cannot be performed, because
* there are no available tiles, or not enough
* money.
*/
bool recruit(const std::string& unit_name, location loc=location());
//! functions to retrieve the 'info' object.
//! Used by derived classes to discover all necessary game information.
/**
* Functions to retrieve the 'info' object.
* Used by derived classes to discover all necessary game information.
*/
info& get_info() { return info_; }
const info& get_info() const { return info_; }
//! Function which should be called frequently to allow the user
//! to interact with the interface.
//! This function will make sure that interaction doesn't occur too often,
//! so there is no problem with calling it very regularly.
/**
* Function which should be called frequently to allow the user to interact
* with the interface. This function will make sure that interaction
* doesn't occur too often, so there is no problem with calling it very
* regularly.
*/
void raise_user_interact();
//! Notifies all interested observers of the event respectively.
/** Notifies all interested observers of the event respectively. */
void raise_unit_recruited() { unit_recruited_.notify_observers(); }
void raise_unit_moved() { unit_moved_.notify_observers(); }
void raise_enemy_attacked() { enemy_attacked_.notify_observers(); }
@ -177,9 +238,9 @@ private:
events::generic_event enemy_attacked_;
};
//! Returns all currently available AIs.
/** Returns all currently available AIs. */
std::vector<std::string> get_available_ais();
//! Create a new AI object with the specified algorithm name.
/** Create a new AI object with the specified algorithm name. */
ai_interface* create_ai(const std::string& algorithm_name, ai_interface::info& info);
#endif