60 #define DBG_AI_ACTIONS LOG_STREAM(debug, log_ai_actions)
61 #define LOG_AI_ACTIONS LOG_STREAM(info, log_ai_actions)
62 #define WRN_AI_ACTIONS LOG_STREAM(warn, log_ai_actions)
63 #define ERR_AI_ACTIONS LOG_STREAM(err, log_ai_actions)
69 : return_value_checked_(true),side_(side),status_(AI_ACTION_SUCCESS),is_execution_(false),is_gamestate_changed_(false)
76 ERR_AI_ACTIONS <<
"Return value of AI ACTION was not checked. This may cause bugs! " << std::endl;
98 }
catch (return_to_play_side_exception&) {
99 if (!
is_ok()) {
DBG_AI_ACTIONS <<
"Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; }
174 :
action_result(side), attacker_loc_(attacker_loc), defender_loc_(defender_loc), attacker_weapon_(attacker_weapon), aggression_(aggression), advancements_(advancements){
197 if(attacker->incapacitated()) {
198 LOG_AI_ACTIONS <<
"attempt to attack with unit that is petrified\n";
203 if(defender->incapacitated()) {
209 if(!attacker->attacks_left()) {
249 s <<
"attack by side ";
271 if(attacker_weapon < 0) {
315 if (!
is_ok()) {
DBG_AI_ACTIONS <<
"Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; }
327 const map_location& to,
bool remove_movement,
bool unreach_is_ok)
331 , remove_movement_(remove_movement)
333 , unit_location_(from)
334 , unreach_is_ok_(unreach_is_ok)
335 , has_ambusher_(false)
336 , has_interrupted_teleport_(false)
347 const unit *u = &*un;
387 if (
route_->steps.empty()) {
433 s <<
"full move by side ";
435 s <<
"partial move by side ";
438 s <<
" from location "<<
from_;
439 s <<
" to location "<<
to_;
450 bool gamestate_changed =
false;
452 int step =
route_->steps.size();
480 if ( num_steps > 0 ) {
496 if (!stopunit_res->is_ok()) {
499 if (stopunit_res->is_gamestate_changed()) {
514 if (!
is_ok()) {
DBG_AI_ACTIONS <<
"Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; }
531 , recall_location_(where)
533 , location_checked_(false)
590 if ( location_specified ) {
621 s <<
"recall by side ";
625 s <<
"] on location "<<
where_;
627 s <<
"] on any suitable location";
635 LOG_AI_ACTIONS <<
"start of execution of: " << *
this << std::endl;
666 if (!
is_ok()) {
DBG_AI_ACTIONS <<
"Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; }
680 , unit_name_(unit_name)
682 , recruit_location_(where)
683 , recruit_from_(from)
684 , location_checked_(false)
742 if ( location_specified ) {
773 s <<
"recruitment by side ";
777 s <<
"] on location "<<
where_;
779 s <<
"] on any suitable location";
787 LOG_AI_ACTIONS <<
"start of execution of: " << *
this << std::endl;
815 if (!
is_ok()) {
DBG_AI_ACTIONS <<
"Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; }
827 :
action_result(side), unit_location_(unit_location), remove_movement_(remove_movement), remove_attacks_(remove_attacks)
838 const unit *u = &*un;
879 s <<
" stopunit by side ";
882 s <<
" : remove movement ";
888 s <<
" remove attacks ";
897 LOG_AI_ACTIONS <<
"start of execution of: " << *
this << std::endl;
911 un->remove_movement_ai();
916 un->remove_attacks_ai();
921 if (!
is_ok()) {
DBG_AI_ACTIONS <<
"Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; }
933 :
action_result(side), lua_code_(lua_code), location_(location)
949 s <<
" synced_command by side ";
965 LOG_AI_ACTIONS <<
"start of execution of: " << *
this << std::endl;
980 if (!
is_ok()) {
DBG_AI_ACTIONS <<
"Return value of AI ACTION was not checked. This may cause bugs! " << std::endl; }
1003 execute ? action->execute() : action->check_before();
1011 bool remove_movement,
1015 execute ? action->execute() : action->check_before();
1026 execute ? action->execute() : action->check_before();
1037 execute ? action->execute() : action->check_before();
1044 bool remove_movement,
1045 bool remove_attacks)
1048 execute ? action->execute() : action->check_before();
1058 execute ? action->execute() : action->check_before();
1108 ERR_AI_ACTIONS <<
"error name not available for error #"<<error_code << std::endl;
1118 if(gamestate_changed){
static config get_lua_ai(const std::string &lua_code)
play_controller * controller
map_location recruit_location_
map_location recruit_from_
virtual void do_init_for_execution()
plain_route a_star_search(const map_location &src, const map_location &dst, double stop_at, const cost_calculator *calc, const size_t width, const size_t height, const teleport_map *teleports, bool border)
virtual void do_check_after()
bool simulated_synced_command()
::tod_manager * tod_manager
const map_location & get_location() const
virtual void do_execute()
map_location recall_from_
unit_const_ptr get_recall_unit(const team &my_team)
size_t move_unit_and_record(const std::vector< map_location > &steps, undo_list *undo_stack, bool continued_move, bool show_move, bool *interrupted, move_unit_spectator *move_spectator)
Moves a unit across the board.
static attack_result_ptr execute_attack_action(side_number side, bool execute, const map_location &attacker_loc, const map_location &defender_loc, int attacker_weapon, double aggression, const unit_advancements_aspect &advancements=unit_advancements_aspect())
Ask the game to attack an enemy defender using our unit attacker from attackers current location...
virtual void do_check_before()
static std::map< int, std::string > error_names_
team & get_my_team() const
void do_final_checkup(bool dont_throw=false)
virtual void do_execute()=0
static void raise_gamestate_changed()
Notifies all observers of 'ai_gamestate_changed' event.
map_location unit_location_
virtual void do_init_for_execution()
Various functions that implement attacks and attack calculations.
static config get_recall(const std::string &unit_id, const map_location &loc, const map_location &from)
GLuint GLuint GLsizei GLenum type
const combatant & get_attacker_combatant(const combatant *prev_def=nullptr)
Get the simulation results.
virtual void do_check_after()
bool test_route(const unit &un)
Managing the AI-Game interaction - AI actions and their results.
map_location recall_location_
static game_info & get_active_ai_info_for_side(side_number side)
Gets AI info for active AI of the given side.
const time_of_day & get_time_of_day(int for_turn=0) const
Returns global time of day for the passed turn.
virtual void do_init_for_execution()
const map_location & where_
const std::string & unit_id_
const unit_type * get_unit_type_known(const std::string &recruit)
virtual std::string do_describe() const
virtual void do_init_for_execution()
virtual std::string do_describe() const
virtual std::string do_describe() const
virtual void do_check_before()
unit_type_data unit_types
Recruitment OK, but not at the specified location.
const unit_advancements_aspect & advancements_
virtual void do_execute()
void check_victory()
Checks to see if a side has won.
static config unit_name(const unit *u)
action_result(side_number side)
bool simulated_attack(const map_location &attacker_loc, const map_location &defender_loc, double attacker_hp, double defender_hp)
virtual void do_check_after()=0
const std::string & lua_code_
void init_for_execution()
void add_synced_command(const std::string &name, const config &command)
bool has_interrupted_teleport_
const unit_map::const_iterator & get_failed_teleport() const
get the location of a failed teleport
const combatant & get_defender_combatant(const combatant *prev_def=nullptr)
This class stores all the data for a single 'side' (in game nomenclature).
A small explanation about what's going on here: Each action has access to two game_info objects First...
virtual void do_init_for_execution()
bool incapacitated() const
static recall_result_ptr execute_recall_action(side_number side, bool execute, const std::string &unit_id, const map_location &where, const map_location &from)
Ask the game to recall a unit for us on specified location.
bool simulated_recall(int side, const std::string &unit_id, const map_location &recall_location)
std::ostream & operator<<(std::ostream &s, ai::attack_result const &r)
unit_ptr find_if_matches_id(const std::string &unit_id)
Find a unit by id. Null pointer if not found.
static lg::log_domain log_ai_actions("ai/actions")
bool tiles_adjacent(const map_location &a, const map_location &b)
Function which tells if two locations are adjacent.
virtual void do_check_after()
std::vector< team > * teams
Structure which holds a single route between one location and another.
bool return_value_checked_
bool is_execution() const
static void ignore_error_function(const std::string &message, bool heavy)
a function to be passed to run_in_synced_context to ignore the error.
virtual void do_check_after()
int w() const
Effective map width.
virtual void do_execute()
const battle_context_unit_stats & get_defender_stats() const
This method returns the statistics of the defender.
bool test_enough_gold(const team &my_team, const unit_type &type)
Computes the statistics of a battle between an attacker and a defender unit.
synced_command_result(side_number side, const std::string &lua_code, const map_location &location)
Managing the AIs lifecycle - headers.
int attack_num
Index into unit->attacks() or -1 for none.
virtual void do_check_after()
virtual void do_execute()
static const map_location & null_location()
const bool remove_movement_
friend void sim_gamestate_changed(action_result *result, bool gamestate_changed)
No vacant castle tiles around a leader on a keep.
virtual void do_check_before()=0
int movement_left() const
Returns how far a unit can move this turn (zero if incapacitated).
virtual void do_init_for_execution()=0
attack_result(side_number side, const map_location &attacker_loc, const map_location &defender_loc, int attacker_weapon, double aggression, const unit_advancements_aspect &advancements=unit_advancements_aspect())
const std::string & unit_name_
virtual std::string do_describe() const
void sim_gamestate_changed(action_result *result, bool gamestate_changed)
virtual void do_check_before()
Encapsulates the map of the game.
const bool remove_attacks_
virtual void do_check_before()
Various functions related to the creation of units (recruits, recalls, and placed units)...
void maybe_throw_return_to_play_side()
const map_location & defender_loc_
RECRUIT_CHECK check_recall_location(const int side, map_location &recall_location, map_location &recall_from, const unit &unit_recall)
Checks if there is a location on which to recall unit_recall.
const unit_map::const_iterator & get_ambusher() const
get the location of an ambusher
int h() const
Effective map height.
No able leaders are on a keep.
virtual std::string do_describe() const
bool simulated_stopunit(const map_location &unit_location, bool remove_movement, bool remove_attacks)
virtual const map_location & get_unit_location() const
bool test_enough_gold(const team &my_team)
std::set< map_location > recent_attacks
hack.
void set_gamestate_changed()
virtual void do_check_before()
static recruit_result_ptr execute_recruit_action(side_number side, bool execute, const std::string &unit_name, const map_location &where, const map_location &from)
Ask the game to recruit a unit for us on specified location.
const unit_map::const_iterator & get_unit() const
get new location of moved unit
stopunit_result(side_number side, const map_location &unit_location, bool remove_movement, bool remove_attacks)
static synced_command_result_ptr execute_synced_command_action(side_number side, bool execute, const std::string &lua_code, const map_location &location)
Ask the game to run Lua code.
bool is_gamestate_changed_
RECRUIT_CHECK check_recruit_location(const int side, map_location &recruit_location, map_location &recruited_from, const std::string &unit_type)
Checks if there is a location on which to place a recruited unit.
virtual std::string do_describe() const
virtual void do_check_before()
GLdouble GLdouble GLdouble r
const map_location & unit_location_
static stopunit_result_ptr execute_stopunit_action(side_number side, bool execute, const map_location &unit_location, bool remove_movement, bool remove_attacks)
Ask the game to remove unit movements and/or attack.
void set_error(int error_code, bool log_as_error=true)
boost::shared_ptr< pathfind::plain_route > route_
virtual const gamemap & map() const
void set_unit(const unit_map::const_iterator &u)
set the iterator to moved unit
bool is_gamestate_changed() const
static bool run_in_synced_context_if_not_already(const std::string &commandname, const config &data, bool use_undo=true, bool show=true, synced_command::error_handler_function error_handler=default_error_function)
checks whether we are currently running in a synced context, and if not we enters it...
bool find(E event, F functor)
Tests whether an event handler is available.
bool simulated_move(int side, const map_location &from, const map_location &to, int steps, map_location &unit_location)
bool simulated_recruit(int side, const unit_type *u, const map_location &recruit_location)
Standard logging facilities (interface).
recall_list_manager & recall_list()
virtual void do_execute()
virtual std::string do_describe() const =0
const unit_type * find(const std::string &key, unit_type::BUILD_STATUS status=unit_type::FULL) const
Finds a unit_type by its id() and makes sure it is built to the specified level.
double average_hp(unsigned int healing=0) const
What's the average hp (weighted average of hp_dist).
static config get_recruit(const std::string &type_id, const map_location &loc, const map_location &from)
const battle_context_unit_stats & get_attacker_stats() const
This method returns the statistics of the attacker.
unit_iterator find(size_t id)
const teleport_map get_teleport_locations(const unit &u, const team &viewing_team, bool see_all, bool ignore_units)
recall_result(side_number side, const std::string &unit_id, const map_location &where, const map_location &from)
const map_location where_
Ensures that the real unit map is active for the duration of the struct's life.
static config get_attack(const map_location &a, const map_location &b, int att_weapon, int def_weapon, const std::string &attacker_type_id, const std::string &defender_type_id, int attacker_lvl, int defender_lvl, const size_t turn, const time_of_day &t)
const map_location & attacker_loc_
move_result(side_number side, const map_location &from, const map_location &to, bool remove_movement, bool unreach_is_ok)
GLsizei const GLcharARB ** string
virtual void do_check_after()
recruit_result(side_number side, const std::string &unit_name, const map_location &where, const map_location &from)
static move_result_ptr execute_move_action(side_number side, bool execute, const map_location &from, const map_location &to, bool remove_movement, bool unreach_is_ok=false)
Ask the game to move our unit from location 'from' to location 'to', optionally - doing a partial mov...
virtual void do_init_for_execution()
void attack_unit_and_advance(const map_location &attacker, const map_location &defender, int attack_with, int defend_with, bool update_display, const ai::unit_advancements_aspect &ai_advancement)
Performs an attack, and advanced the units afterwards.
const std::string & id() const
The id for this unit_type.
No leaders able to recall/recruit the given unit/type.
static const std::string & get_error_name(int error_code)
get human-readable name of the error by code.
const std::vector< map_location > & route_
const map_location & location_
virtual void do_execute()
game_info & get_info() const
Implement simulated actions.