The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Static Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Static Private Member Functions | Static Private Attributes | List of all members
ai::manager Class Reference

Class that manages AIs for all sides and manages AI redeployment. More...

#include <manager.hpp>

Static Public Member Functions

static void set_ai_info (const game_info &info)
 Sets AI information. More...
 
static void clear_ai_info ()
 Clears AI information. More...
 
static void add_observer (events::observer *event_observer)
 Adds observer of game events. More...
 
static void remove_observer (events::observer *event_observer)
 Removes an observer of game events. More...
 
static void add_gamestate_observer (events::observer *event_observer)
 Adds observer of game events except ai_user_interact event and ai_sync_network event. More...
 
static void remove_gamestate_observer (events::observer *event_observer)
 Removes an observer of game events except ai_user_interact event and ai_sync_network event. More...
 
static void raise_user_interact ()
 Notifies all observers of 'ai_user_interact' event. More...
 
static void raise_sync_network ()
 Notifies all observers of 'ai_sync_network' event. More...
 
static void raise_gamestate_changed ()
 Notifies all observers of 'ai_gamestate_changed' event. More...
 
static void raise_tod_changed ()
 Notifies all observers of 'ai_tod_changed' event. More...
 
static void raise_recruit_list_changed ()
 Notifies all observers of 'ai_recruit_list_changed' event. More...
 
static void raise_turn_started ()
 Notifies all observers of 'ai_turn_started' event. More...
 
static void raise_map_changed ()
 Notifies all observers of 'ai_map_changed' event. More...
 
static void add_map_changed_observer (events::observer *event_observer)
 Adds an observer of 'ai_map_changed' event. More...
 
static void add_recruit_list_changed_observer (events::observer *event_observer)
 Adds an observer of 'ai_recruit_list_changed' event. More...
 
static void add_turn_started_observer (events::observer *event_observer)
 Adds an observer of 'ai_turn_started' event. More...
 
static void add_tod_changed_observer (events::observer *event_observer)
 Adds an observer of 'ai_tod_changed' event. More...
 
static void remove_map_changed_observer (events::observer *event_observer)
 Deletes an observer of 'ai_map_changed' event. More...
 
static void remove_recruit_list_changed_observer (events::observer *event_observer)
 Deletes an observer of 'ai_recruit_list_changed' event. More...
 
static void remove_turn_started_observer (events::observer *event_observer)
 Deletes an observer of 'ai_turn_started' event. More...
 
static void remove_tod_changed_observer (events::observer *event_observer)
 Deletes an observer of 'ai_tod_changed' event. More...
 
static const std::string evaluate_command (side_number side, const std::string &str)
 Evaluates a string command using command AI. More...
 
static bool add_ai_for_side_from_file (side_number side, const std::string &file, bool replace=true)
 Adds active AI for specified side from file. More...
 
static bool add_ai_for_side_from_config (side_number side, const config &cfg, bool replace=true)
 Adds active AI for specified side from cfg. More...
 
static bool add_ai_for_side (side_number side, const std::string &ai_algorithm_type, bool replace=true)
 Adds active AI for specified side from parameters. More...
 
static void remove_ai_for_side (side_number side)
 Removes top-level AI from side. More...
 
static void remove_all_ais_for_side (side_number side)
 Removes all AIs from side. More...
 
static void clear_ais ()
 Clears all the AIs. More...
 
static game_infoget_active_ai_info_for_side (side_number side)
 Gets AI info for active AI of the given side. More...
 
static std::string get_active_ai_overview_for_side (side_number side)
 Gets AI Overview for active AI of the given side. More...
 
static std::string get_active_ai_structure_for_side (side_number side)
 Gets AI Structure for active AI of the given side. More...
 
static std::string get_active_ai_identifier_for_side (side_number side)
 Gets AI algorithm identifier for active AI of the given side. More...
 
static ai::holderget_active_ai_holder_for_side_dbg (side_number side)
 Gets the active AI holder for debug purposes. More...
 
static config to_config (side_number side)
 Gets AI config for active AI of the given side. More...
 
static game_infoget_ai_info ()
 Gets global AI-game info. More...
 
static void modify_active_ai_config_old_for_side (side_number side, const config::const_child_itors &ai_parameters)
 Modifies AI parameters for active AI of the given side. More...
 
static void modify_active_ai_for_side (ai::side_number side, const config &cfg)
 Modifies AI parameters for active AI of the given side. More...
 
static void play_turn (side_number side)
 Plays a turn for the specified side using its active AI. More...
 

Static Public Attributes

static const size_t MAX_HISTORY_SIZE = 200
 
static const std::string AI_TYPE_COMPOSITE_AI = "composite_ai"
 
static const std::string AI_TYPE_SAMPLE_AI = "sample_ai"
 
static const std::string AI_TYPE_IDLE_AI = "idle_ai"
 
static const std::string AI_TYPE_FORMULA_AI = "formula_ai"
 
static const std::string AI_TYPE_DFOOL_AI
 
static const std::string AI_TYPE_AI2
 
static const std::string AI_TYPE_DEFAULT = "default"
 

Private Types

typedef std::map< side_number,
std::stack< holder > > 
AI_map_of_stacks
 

Private Member Functions

 manager ()
 

Static Private Member Functions

static const std::string internal_evaluate_command (side_number side, const std::string &str)
 Evaluates an internal manager command. More...
 
static bool should_intercept (const std::string &str)
 Determines if the command should be intercepted and evaluated as internal command. More...
 
static std::stack< holder > & get_or_create_ai_stack_for_side (side_number side)
 Gets the AI stack for the specified side, create it if it doesn't exist. More...
 
static holderget_active_ai_holder_for_side (side_number side)
 Gets active holder for specified side. More...
 
static ai_compositeget_active_ai_for_side (side_number side)
 Gets active AI for specified side. More...
 

Static Private Attributes

static AI_map_of_stacks ai_map_
 
static std::deque
< command_history_item
history_
 
static long history_item_counter_ = 1
 
static game_infoai_info_
 
static events::generic_event map_changed_
 
static events::generic_event recruit_list_changed_
 
static events::generic_event user_interact_
 
static events::generic_event sync_network_
 
static events::generic_event tod_changed_
 
static events::generic_event gamestate_changed_
 
static events::generic_event turn_started_
 
static int last_interact_ = 0
 
static int num_interact_ = 0
 

Detailed Description

Class that manages AIs for all sides and manages AI redeployment.

This class is responsible for managing the AI lifecycle It can be accessed like this: ai::manager::foo(...);

Definition at line 119 of file manager.hpp.

Member Typedef Documentation

typedef std::map< side_number, std::stack< holder > > ai::manager::AI_map_of_stacks
private

Definition at line 467 of file manager.hpp.

Constructor & Destructor Documentation

ai::manager::manager ( )
private

Member Function Documentation

bool ai::manager::add_ai_for_side ( side_number  side,
const std::string ai_algorithm_type,
bool  replace = true 
)
static

Adds active AI for specified side from parameters.

Note
Running this command may invalidate references previously returned by manager. AI is not initialized at this point.
Parameters
sideside number (1-based, as in game_info).
ai_algorithm_typetype of AI algorithm to create.
replaceshould new ai replace the current ai or 'be placed on top of it'.
Returns
true if successful.
Todo:
1.9 add error reporting

Definition at line 667 of file manager.cpp.

References get_or_create_ai_stack_for_side(), and remove_ai_for_side().

bool ai::manager::add_ai_for_side_from_config ( side_number  side,
const config cfg,
bool  replace = true 
)
static

Adds active AI for specified side from cfg.

Note
Running this command may invalidate references previously returned by manager. AI is not initialized at this point.
Parameters
sideside number (1-based, as in game_info).
cfgthe config from which all ai parameters are to be read.
replaceshould new ai replace the current ai or 'be placed on top of it'.
Returns
true if successful.

Definition at line 651 of file manager.cpp.

References get_or_create_ai_stack_for_side(), ai::configuration::parse_side_config(), and remove_ai_for_side().

Referenced by add_ai_for_side_from_file(), and team::team_info::read().

bool ai::manager::add_ai_for_side_from_file ( side_number  side,
const std::string file,
bool  replace = true 
)
static

Adds active AI for specified side from file.

Note
Running this command may invalidate references previously returned by manager. AI is not initialized at this point.
Parameters
sideside number (1-based, as in game_info).
filefile name, follows the usual WML convention.
replaceshould new ai replace the current ai or 'be placed on top of it'.
Returns
true if successful.
Todo:
1.9 add error reporting

Definition at line 640 of file manager.cpp.

References add_ai_for_side_from_config(), ERR_AI_MANAGER, and ai::configuration::get_side_config_from_file().

Referenced by internal_evaluate_command(), game_lua_kernel::intf_modify_side(), and team::team_info::read().

void ai::manager::add_gamestate_observer ( events::observer event_observer)
static
void ai::manager::add_map_changed_observer ( events::observer event_observer)
static

Adds an observer of 'ai_map_changed' event.

Definition at line 396 of file manager.cpp.

References events::generic_event::attach_handler(), and map_changed_.

Referenced by ai::keeps_cache::keeps_cache().

void ai::manager::add_observer ( events::observer event_observer)
static

Adds observer of game events.

Should be called in playsingle_controller 's constructor.

Definition at line 355 of file manager.cpp.

References events::generic_event::attach_handler(), gamestate_changed_, sync_network_, turn_started_, and user_interact_.

void ai::manager::add_recruit_list_changed_observer ( events::observer event_observer)
static
void ai::manager::add_tod_changed_observer ( events::observer event_observer)
static

Adds an observer of 'ai_tod_changed' event.

Definition at line 385 of file manager.cpp.

References events::generic_event::attach_handler(), and tod_changed_.

Referenced by ai::aspect::redeploy().

void ai::manager::add_turn_started_observer ( events::observer event_observer)
static

Adds an observer of 'ai_turn_started' event.

Definition at line 408 of file manager.cpp.

References events::generic_event::attach_handler(), and turn_started_.

Referenced by ai::keeps_cache::keeps_cache(), and ai::aspect::redeploy().

void ai::manager::clear_ai_info ( )
static

Clears AI information.

Should be called in playsingle_controller 's destructor.

Definition at line 349 of file manager.cpp.

References ai_info_.

Referenced by set_ai_info().

void ai::manager::clear_ais ( )
static

Clears all the AIs.

Note
Running this command may invalidate references previously returned by manager. For example, this is called from the destructor of playsingle_controller. It is necessary to do this if any of the info structures used by the AI goes out of scope.

Definition at line 705 of file manager.cpp.

References ai_map_.

Referenced by playsingle_controller::~playsingle_controller().

const std::string ai::manager::evaluate_command ( side_number  side,
const std::string str 
)
static

Evaluates a string command using command AI.

Note
Running this command may invalidate references previously returned by manager. Will intercept those commands which start with '!' and '?', and will try to evaluate them as internal commands.
Parameters
sideside number (1-based).
strstring to evaluate.
Returns
string result of evaluation.

Definition at line 483 of file manager.cpp.

References ai::ai_composite::evaluate(), get_active_ai_for_side(), history_, history_item_counter_, internal_evaluate_command(), LOG_AI_MANAGER, MAX_HISTORY_SIZE, raise_gamestate_changed(), and should_intercept().

Referenced by events::menu_handler::do_ai_formula(), and internal_evaluate_command().

ai_composite & ai::manager::get_active_ai_for_side ( side_number  side)
staticprivate

Gets active AI for specified side.

Note
Running this command may invalidate references previously returned by manager.
Parameters
sideside number (1-based, as in game_info).
Returns
a reference to the active AI.
Note
This reference may become invalid after specific manager operations.

Definition at line 837 of file manager.cpp.

References get_active_ai_holder_for_side(), and ai::holder::get_ai_ref().

Referenced by evaluate_command(), and play_turn().

holder & ai::manager::get_active_ai_holder_for_side ( side_number  side)
staticprivate
ai::holder & ai::manager::get_active_ai_holder_for_side_dbg ( side_number  side)
static

Gets the active AI holder for debug purposes.

Will only work in debug mode, otherwise returns a reference to an empty holder

Parameters
sideside number(1-based)
Returns
debug ? active holder : empty holder

Definition at line 749 of file manager.cpp.

References game_config::debug, and get_active_ai_holder_for_side().

Referenced by intf_debug_ai().

std::string ai::manager::get_active_ai_identifier_for_side ( side_number  side)
static

Gets AI algorithm identifier for active AI of the given side.

Parameters
sideside number (1-based).
Returns
ai identifier for the active AI

Definition at line 744 of file manager.cpp.

References get_active_ai_holder_for_side(), and ai::holder::get_ai_identifier().

Referenced by play_controller::check_victory(), internal_evaluate_command(), and ai_testing::log_game_start().

game_info & ai::manager::get_active_ai_info_for_side ( side_number  side)
static

Gets AI info for active AI of the given side.

Parameters
sideside number (1-based).
Returns
a reference to active AI info.

Definition at line 765 of file manager.cpp.

References ai_info_.

Referenced by ai::action_result::get_info(), ai::readonly_context_impl::get_info(), and ai::readwrite_context_impl::get_info_w().

std::string ai::manager::get_active_ai_overview_for_side ( side_number  side)
static

Gets AI Overview for active AI of the given side.

Parameters
sideside number (1-based)
Returns
an ai overview

Definition at line 732 of file manager.cpp.

References get_active_ai_holder_for_side(), and ai::holder::get_ai_overview().

Referenced by gui2::team_mode_controller::handle_stuff_list_selection().

std::string ai::manager::get_active_ai_structure_for_side ( side_number  side)
static

Gets AI Structure for active AI of the given side.

Parameters
sideside number (1-based)
Returns
an ai structure

Definition at line 738 of file manager.cpp.

References get_active_ai_holder_for_side(), and ai::holder::get_ai_structure().

Referenced by gui2::team_mode_controller::handle_stuff_list_selection().

game_info & ai::manager::get_ai_info ( )
static

Gets global AI-game info.

Returns
a reference to the AI-game info.

Definition at line 771 of file manager.cpp.

References ai_info_.

Referenced by ai::attack_analysis::attack_close(), and play_turn().

std::stack< holder > & ai::manager::get_or_create_ai_stack_for_side ( side_number  side)
staticprivate

Gets the AI stack for the specified side, create it if it doesn't exist.

Definition at line 807 of file manager.cpp.

References ai_map_.

Referenced by add_ai_for_side(), add_ai_for_side_from_config(), get_active_ai_holder_for_side(), remove_ai_for_side(), and remove_all_ais_for_side().

const std::string ai::manager::internal_evaluate_command ( side_number  side,
const std::string str 
)
staticprivate

Evaluates an internal manager command.

Parameters
sideside number (1-based).
strstring to evaluate.
Returns
string result of evaluation.
Todo:
1.9 rewrite this function to use a fai or lua parser.
Todo:
1.9 extract to separate class which will use fai or lua parser

add_ai side file

replace_ai side file

remove_ai side

Definition at line 526 of file manager.cpp.

References add_ai_for_side_from_file(), evaluate_command(), get_active_ai_identifier_for_side(), history_, history_item_counter_, lexical_cast(), utils::parenthetical_split(), and remove_ai_for_side().

Referenced by evaluate_command().

void ai::manager::modify_active_ai_config_old_for_side ( side_number  side,
const config::const_child_itors ai_parameters 
)
static

Modifies AI parameters for active AI of the given side.

This function is provided for backward-compatibility with [modify_side][ai]...[/ai][/modify_side] It can only add new facets to aspects

Parameters
sideside_number (1-based, as in game_info).
ai_parametersAI parameters to be modified.

Definition at line 714 of file manager.cpp.

References get_active_ai_holder_for_side(), and ai::holder::modify_side_ai_config().

Referenced by game_lua_kernel::intf_modify_side().

void ai::manager::modify_active_ai_for_side ( ai::side_number  side,
const config cfg 
)
static

Modifies AI parameters for active AI of the given side.

This function is a backend for [modify_ai] tag

Parameters
sideside_number (1-based, as in game_info).
cfg- content of [modify_ai] tag

Definition at line 722 of file manager.cpp.

References ai_info_, get_active_ai_holder_for_side(), and ai::holder::modify_ai().

Referenced by intf_modify_ai(), game_lua_kernel::intf_modify_ai_wml(), ai::ai_default_rca::candidate_action_evaluation_loop::remove_completed_cas(), and ai::ai_default_rca::move_leader_to_goals_phase::remove_goal().

void ai::manager::play_turn ( side_number  side)
static
void ai::manager::raise_gamestate_changed ( )
static
void ai::manager::raise_map_changed ( )
static

Notifies all observers of 'ai_map_changed' event.

Definition at line 474 of file manager.cpp.

References map_changed_, and events::generic_event::notify_observers().

Referenced by game_events::WML_HANDLER_FUNCTION().

void ai::manager::raise_recruit_list_changed ( )
static

Notifies all observers of 'ai_recruit_list_changed' event.

Definition at line 469 of file manager.cpp.

References events::generic_event::notify_observers(), and recruit_list_changed_.

Referenced by team::add_recruit(), and team::set_recruits().

void ai::manager::raise_sync_network ( )
static

Notifies all observers of 'ai_sync_network' event.

Basically a request from the AI to sync the network.

Definition at line 449 of file manager.cpp.

References events::generic_event::notify_observers(), and sync_network_.

void ai::manager::raise_tod_changed ( )
static

Notifies all observers of 'ai_tod_changed' event.

Definition at line 459 of file manager.cpp.

References events::generic_event::notify_observers(), and tod_changed_.

Referenced by play_turn().

void ai::manager::raise_turn_started ( )
static

Notifies all observers of 'ai_turn_started' event.

Definition at line 464 of file manager.cpp.

References events::generic_event::notify_observers(), and turn_started_.

Referenced by play_turn().

void ai::manager::raise_user_interact ( )
static

Notifies all observers of 'ai_user_interact' event.

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.

Definition at line 431 of file manager.cpp.

References last_interact_, events::generic_event::notify_observers(), num_interact_, resources::simulation_, and user_interact_.

Referenced by ai::ai_default_rca::aspect_attacks_base::do_attack_analysis(), and ai::readonly_context_impl::raise_user_interact().

void ai::manager::remove_ai_for_side ( side_number  side)
static

Removes top-level AI from side.

Note
Running this command may invalidate references previously returned by manager.
Parameters
sideside number (1-based, as in game_info).

Definition at line 685 of file manager.cpp.

References get_or_create_ai_stack_for_side().

Referenced by add_ai_for_side(), add_ai_for_side_from_config(), and internal_evaluate_command().

void ai::manager::remove_all_ais_for_side ( side_number  side)
static

Removes all AIs from side.

Note
Running this command may invalidate references previously returned by manager.
Parameters
sideside number (1-based, as in game_info).

Definition at line 694 of file manager.cpp.

References get_or_create_ai_stack_for_side().

void ai::manager::remove_gamestate_observer ( events::observer event_observer)
static
void ai::manager::remove_map_changed_observer ( events::observer event_observer)
static

Deletes an observer of 'ai_map_changed' event.

Definition at line 420 of file manager.cpp.

References events::generic_event::detach_handler(), and map_changed_.

Referenced by ai::keeps_cache::~keeps_cache().

void ai::manager::remove_observer ( events::observer event_observer)
static

Removes an observer of game events.

Should be called in playsingle_controller 's destructor.

Definition at line 363 of file manager.cpp.

References events::generic_event::detach_handler(), gamestate_changed_, sync_network_, turn_started_, and user_interact_.

Referenced by playsingle_controller::~playsingle_controller().

void ai::manager::remove_recruit_list_changed_observer ( events::observer event_observer)
static

Deletes an observer of 'ai_recruit_list_changed' event.

Definition at line 414 of file manager.cpp.

References events::generic_event::detach_handler(), and recruit_list_changed_.

Referenced by ai::default_recruitment::recruitment::recruit_situation_change_observer::~recruit_situation_change_observer().

void ai::manager::remove_tod_changed_observer ( events::observer event_observer)
static

Deletes an observer of 'ai_tod_changed' event.

Definition at line 390 of file manager.cpp.

References events::generic_event::detach_handler(), and tod_changed_.

Referenced by ai::aspect::redeploy(), and ai::aspect::~aspect().

void ai::manager::remove_turn_started_observer ( events::observer event_observer)
static

Deletes an observer of 'ai_turn_started' event.

Definition at line 426 of file manager.cpp.

References events::generic_event::detach_handler(), and turn_started_.

Referenced by ai::aspect::redeploy(), ai::aspect::~aspect(), and ai::keeps_cache::~keeps_cache().

void ai::manager::set_ai_info ( const game_info info)
static

Sets AI information.

Parameters
infoai_information to be set.

Definition at line 339 of file manager.cpp.

References ai_info_, clear_ai_info(), and ai::registry::init().

bool ai::manager::should_intercept ( const std::string str)
staticprivate

Determines if the command should be intercepted and evaluated as internal command.

Parameters
strcommand string to check.
Returns
true if the command should be intercepted and evaluated.

Definition at line 504 of file manager.cpp.

Referenced by evaluate_command().

config ai::manager::to_config ( side_number  side)
static

Gets AI config for active AI of the given side.

Parameters
sideside number (1-based).
Returns
a config object for the active AI

Definition at line 759 of file manager.cpp.

References get_active_ai_holder_for_side(), and ai::holder::to_config().

Referenced by gui2::team_mode_controller::handle_stuff_list_selection(), and team::team_info::write().

Member Data Documentation

game_info * ai::manager::ai_info_
staticprivate
manager::AI_map_of_stacks ai::manager::ai_map_
staticprivate

Definition at line 468 of file manager.hpp.

Referenced by clear_ais(), and get_or_create_ai_stack_for_side().

const std::string ai::manager::AI_TYPE_AI2
static

Definition at line 134 of file manager.hpp.

const std::string ai::manager::AI_TYPE_COMPOSITE_AI = "composite_ai"
static

Definition at line 129 of file manager.hpp.

const std::string ai::manager::AI_TYPE_DEFAULT = "default"
static

Definition at line 135 of file manager.hpp.

const std::string ai::manager::AI_TYPE_DFOOL_AI
static

Definition at line 133 of file manager.hpp.

const std::string ai::manager::AI_TYPE_FORMULA_AI = "formula_ai"
static

Definition at line 132 of file manager.hpp.

const std::string ai::manager::AI_TYPE_IDLE_AI = "idle_ai"
static

Definition at line 131 of file manager.hpp.

const std::string ai::manager::AI_TYPE_SAMPLE_AI = "sample_ai"
static

Definition at line 130 of file manager.hpp.

events::generic_event ai::manager::gamestate_changed_
staticprivate
std::deque< command_history_item > ai::manager::history_
staticprivate

Definition at line 469 of file manager.hpp.

Referenced by evaluate_command(), and internal_evaluate_command().

long ai::manager::history_item_counter_ = 1
staticprivate

Definition at line 470 of file manager.hpp.

Referenced by evaluate_command(), and internal_evaluate_command().

int ai::manager::last_interact_ = 0
staticprivate

Definition at line 480 of file manager.hpp.

Referenced by play_turn(), and raise_user_interact().

events::generic_event ai::manager::map_changed_
staticprivate
const size_t ai::manager::MAX_HISTORY_SIZE = 200
static

Definition at line 127 of file manager.hpp.

Referenced by evaluate_command().

int ai::manager::num_interact_ = 0
staticprivate

Definition at line 481 of file manager.hpp.

Referenced by play_turn(), and raise_user_interact().

events::generic_event ai::manager::recruit_list_changed_
staticprivate
events::generic_event ai::manager::sync_network_
staticprivate

Definition at line 476 of file manager.hpp.

Referenced by add_observer(), raise_sync_network(), and remove_observer().

events::generic_event ai::manager::tod_changed_
staticprivate
events::generic_event ai::manager::turn_started_
staticprivate
events::generic_event ai::manager::user_interact_
staticprivate

Definition at line 475 of file manager.hpp.

Referenced by add_observer(), raise_user_interact(), and remove_observer().


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