The Battle for Wesnoth  1.19.5+dev
Public Types | Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
ai::formula_ai Class Reference

#include <ai.hpp>

Inheritance diagram for ai::formula_ai:

Public Types

typedef wfl::position_callable::move_map_backup move_map_backup
 
- Public Types inherited from ai::readonly_context
typedef std::map< std::pair< map_location, const unit_type * >, std::pair< battle_context_unit_stats, battle_context_unit_stats > > unit_stats_cache_t
 

Public Member Functions

 formula_ai (const formula_ai &)=delete
 
formula_aioperator= (const formula_ai &)=delete
 
 formula_ai (readonly_context &context, const config &cfg)
 
virtual ~formula_ai ()
 
virtual config to_config () const
 
std::string evaluate (const std::string &formula_str)
 
virtual void add_formula_function (const std::string &name, wfl::const_formula_ptr formula, wfl::const_formula_ptr precondition, const std::vector< std::string > &args)
 
void swap_move_map (move_map_backup &backup)
 
wfl::variant get_keeps () const
 
void on_create ()
 
int get_recursion_count () const override
 Get the value of the recursion counter. More...
 
const wfl::variantget_keeps_cache () const
 
bool can_reach_unit (map_location unit_A, map_location unit_B) const
 
void handle_exception (const wfl::formula_error &e) const
 
void handle_exception (const wfl::formula_error &e, const std::string &failed_operation) const
 
pathfind::teleport_map get_allowed_teleports (unit_map::iterator &unit_it) const
 
pathfind::plain_route shortest_path_calculator (const map_location &src, const map_location &dst, unit_map::iterator &unit_it, pathfind::teleport_map &allowed_teleports) const
 
wfl::formula_ptr create_optional_formula (const std::string &formula_string) const
 Create a new formula from the string, using the symbol table which is stored in the AI. More...
 
wfl::candidate_action_ptr load_candidate_action_from_config (const config &cfg)
 
void evaluate_candidate_action (wfl::candidate_action_ptr fai_ca)
 Evaluate the fai candidate action. More...
 
bool execute_candidate_action (wfl::candidate_action_ptr fai_ca)
 Execute the fai candidate action. More...
 
void set_ai_context (ai_context *context)
 
wfl::variant make_action (wfl::const_formula_ptr formula_, const wfl::formula_callable &variables)
 
- Public Member Functions inherited from ai::readonly_context_proxy
 readonly_context_proxy ()
 
virtual ~readonly_context_proxy ()
 
void init_readonly_context_proxy (readonly_context &target)
 
virtual readonly_contextget_readonly_context () override
 
virtual void on_readonly_context_create () override
 
virtual const teamcurrent_team () const override
 
virtual void diagnostic (const std::string &msg) override
 
virtual void log_message (const std::string &msg) override
 
virtual attack_result_ptr check_attack_action (const map_location &attacker_loc, const map_location &defender_loc, int attacker_weapon) override
 
virtual move_result_ptr check_move_action (const map_location &from, const map_location &to, bool remove_movement=true, bool unreach_is_ok=false) override
 
virtual recall_result_ptr check_recall_action (const std::string &id, const map_location &where=map_location::null_location(), const map_location &from=map_location::null_location()) override
 
virtual recruit_result_ptr check_recruit_action (const std::string &unit_name, const map_location &where=map_location::null_location(), const map_location &from=map_location::null_location()) override
 
virtual stopunit_result_ptr check_stopunit_action (const map_location &unit_location, bool remove_movement=true, bool remove_attacks=false) override
 
virtual synced_command_result_ptr check_synced_command_action (const std::string &lua_code, const map_location &location=map_location::null_location()) override
 
virtual void calculate_possible_moves (std::map< map_location, pathfind::paths > &possible_moves, move_map &srcdst, move_map &dstsrc, bool enemy, bool assume_full_movement=false, const terrain_filter *remove_destinations=nullptr) const override
 
virtual void calculate_moves (const unit_map &units, std::map< map_location, pathfind::paths > &possible_moves, move_map &srcdst, move_map &dstsrc, bool enemy, bool assume_full_movement=false, const terrain_filter *remove_destinations=nullptr, bool see_all=false) const override
 
virtual const game_infoget_info () const override
 
virtual void raise_user_interact () const override
 
const defensive_positionbest_defensive_position (const map_location &unit, const move_map &dstsrc, const move_map &srcdst, const move_map &enemy_dstsrc) const override
 
virtual std::map< map_location, defensive_position > & defensive_position_cache () const override
 
virtual const unit_advancements_aspectget_advancements () const override
 
virtual double get_aggression () const override
 
virtual bool get_allow_ally_villages () const override
 
virtual const aspect_mapget_aspects () const override
 
virtual aspect_mapget_aspects () override
 
virtual void add_aspects (std::vector< aspect_ptr > &aspects) override
 
virtual void add_facet (const std::string &id, const config &cfg) const override
 
virtual const attacks_vectorget_attacks () const override
 
virtual const wfl::variantget_attacks_as_variant () const override
 
virtual const terrain_filterget_avoid () const override
 
virtual double get_caution () const override
 
virtual const move_mapget_dstsrc () const override
 
virtual const move_mapget_enemy_dstsrc () const override
 
virtual const moves_mapget_enemy_possible_moves () const override
 
virtual const move_mapget_enemy_srcdst () const override
 
virtual engine_ptr get_engine_by_cfg (const config &cfg) override
 get engine by cfg, creating it if it is not created yet but known More...
 
virtual const std::vector< engine_ptr > & get_engines () const override
 
virtual std::vector< engine_ptr > & get_engines () override
 
virtual std::string get_grouping () const override
 
virtual const std::vector< goal_ptr > & get_goals () const override
 
virtual std::vector< goal_ptr > & get_goals () override
 
virtual double get_leader_aggression () const override
 
virtual config get_leader_goal () const override
 
virtual utils::variant< bool, std::vector< std::string > > get_leader_ignores_keep () const override
 
virtual double get_leader_value () const override
 
virtual utils::variant< bool, std::vector< std::string > > get_passive_leader () const override
 
virtual utils::variant< bool, std::vector< std::string > > get_passive_leader_shares_keep () const override
 
virtual const moves_mapget_possible_moves () const override
 
virtual double power_projection (const map_location &loc, const move_map &dstsrc) const override
 Function which finds how much 'power' a side can attack a certain location with. More...
 
virtual double get_recruitment_diversity () const override
 
virtual const config get_recruitment_instructions () const override
 
virtual const std::vector< std::string > get_recruitment_more () const override
 
virtual const std::vector< std::string > get_recruitment_pattern () const override
 
virtual int get_recruitment_randomness () const override
 
virtual const config get_recruitment_save_gold () const override
 
virtual const move_mapget_srcdst () const override
 
virtual double get_retreat_enemy_weight () const override
 
virtual double get_retreat_factor () const override
 
virtual double get_scout_village_targeting () const override
 
virtual bool get_simple_targeting () const override
 
virtual bool get_support_villages () const override
 
virtual double get_village_value () const override
 
virtual int get_villages_per_scout () const override
 
virtual bool is_active (const std::string &time_of_day, const std::string &turns) const override
 
virtual bool is_keep_ignoring_leader (const std::string &id) const override
 
virtual bool is_passive_leader (const std::string &id) const override
 
virtual bool is_passive_keep_sharing_leader (const std::string &id) const override
 
virtual bool is_dst_src_valid_lua () const override
 
virtual bool is_dst_src_enemy_valid_lua () const override
 
virtual bool is_src_dst_valid_lua () const override
 
virtual bool is_src_dst_enemy_valid_lua () const override
 
virtual void invalidate_defensive_position_cache () const override
 
virtual void invalidate_move_maps () const override
 
virtual void invalidate_keeps_cache () const override
 
virtual const std::set< map_location > & keeps () const override
 
virtual bool leader_can_reach_keep () const override
 
virtual const map_locationnearest_keep (const map_location &loc) const override
 
virtual void recalculate_move_maps () const override
 
virtual void recalculate_move_maps_enemy () const override
 
virtual void set_dst_src_valid_lua () override
 
virtual void set_dst_src_enemy_valid_lua () override
 
virtual void set_src_dst_valid_lua () override
 
virtual void set_src_dst_enemy_valid_lua () override
 
virtual const map_locationsuitable_keep (const map_location &leader_location, const pathfind::paths &leader_paths) const override
 get most suitable keep for leader - nearest free that can be reached in 1 turn, if none - return nearest occupied that can be reached in 1 turn, if none - return nearest keep, if none - return null_location More...
 
virtual config to_readonly_context_config () const override
 serialize to config More...
 
virtual unit_stats_cache_tunit_stats_cache () const override
 
- Public Member Functions inherited from ai::readonly_context
 readonly_context ()
 
virtual ~readonly_context ()
 
- Public Member Functions inherited from ai::side_context
virtual ~side_context ()
 empty destructor More...
 
 side_context ()
 empty constructor More...
 
- Public Member Functions inherited from ai::side_context_proxy
 side_context_proxy ()
 
virtual ~side_context_proxy ()
 
void init_side_context_proxy (side_context &target)
 
virtual side_number get_side () const override
 Get the side number. More...
 
virtual void set_side (side_number side) override
 Set the side number. More...
 
virtual side_contextget_side_context () override
 unwrap More...
 
virtual config to_side_context_config () const override
 serialize this context to config More...
 
- Public Member Functions inherited from wfl::formula_callable
 formula_callable (bool has_self=true)
 
virtual ~formula_callable ()
 
formula_callable_ptr fake_ptr ()
 
const_formula_callable_ptr fake_ptr () const
 
variant query_value (const std::string &key) const
 
void mutate_value (const std::string &key, const variant &value)
 
formula_input_vector inputs () const
 
bool equals (const formula_callable &other) const
 
bool less (const formula_callable &other) const
 
bool has_key (const std::string &key) const
 
void serialize (std::string &str) const
 
void subscribe_dtor (callable_die_subscriber *d) const
 
void unsubscribe_dtor (callable_die_subscriber *d) const
 

Private Member Functions

void display_message (const std::string &msg) const
 
virtual wfl::variant get_value (const std::string &key) const override
 
void set_value (const std::string &key, const wfl::variant &value) override
 
virtual void get_inputs (wfl::formula_input_vector &inputs) const override
 

Private Attributes

ai_contextai_ptr_
 
const config cfg_
 
recursion_counter recursion_counter_
 
wfl::variant keeps_cache_
 
wfl::attack_map_callable attacks_callable
 
wfl::map_formula_callable vars_
 
wfl::ai_function_symbol_table function_table_
 

Friends

class ai_default
 
ai_contextget_ai_context (wfl::const_formula_callable_ptr for_fai)
 

Additional Inherited Members

- Protected Types inherited from wfl::formula_callable
enum  TYPE {
  FORMULA_C , TERRAIN_C , LOCATION_C , UNIT_TYPE_C ,
  UNIT_C , ATTACK_TYPE_C , MOVE_PARTIAL_C , MOVE_C ,
  ATTACK_C , MOVE_MAP_C
}
 
- Protected Member Functions inherited from wfl::formula_callable
virtual int do_compare (const formula_callable *callable) const
 
virtual void serialize_to_string (std::string &) const
 
- Static Protected Member Functions inherited from wfl::formula_callable
template<typename T , typename K >
static variant convert_map (const std::map< T, K > &input_map)
 
template<typename T >
static variant convert_set (const std::set< T > &input_set)
 
template<typename T >
static variant convert_vector (const std::vector< T > &input_vector)
 
static void add_input (formula_input_vector &inputs, const std::string &key, formula_access access_type=formula_access::read_only)
 
- Protected Attributes inherited from wfl::formula_callable
TYPE type_
 
std::set< callable_die_subscriber * > dtor_notify
 

Detailed Description

Definition at line 68 of file ai.hpp.

Member Typedef Documentation

◆ move_map_backup

Definition at line 104 of file ai.hpp.

Constructor & Destructor Documentation

◆ formula_ai() [1/2]

ai::formula_ai::formula_ai ( const formula_ai )
delete

◆ formula_ai() [2/2]

ai::formula_ai::formula_ai ( readonly_context context,
const config cfg 
)
explicit

Definition at line 90 of file ai.cpp.

References ai::readonly_context_proxy::init_readonly_context_proxy(), and LOG_AI.

◆ ~formula_ai()

virtual ai::formula_ai::~formula_ai ( )
inlinevirtual

Definition at line 74 of file ai.hpp.

Member Function Documentation

◆ add_formula_function()

void ai::formula_ai::add_formula_function ( const std::string &  name,
wfl::const_formula_ptr  formula,
wfl::const_formula_ptr  precondition,
const std::vector< std::string > &  args 
)
virtual

Definition at line 253 of file ai.cpp.

References wfl::function_symbol_table::add_function(), and function_table_.

Referenced by on_create().

◆ can_reach_unit()

bool ai::formula_ai::can_reach_unit ( map_location  unit_A,
map_location  unit_B 
) const

Definition at line 608 of file ai.cpp.

References ai::readonly_context_proxy::get_srcdst(), i, tiles_adjacent(), and unit_moves().

◆ create_optional_formula()

formula_ptr ai::formula_ai::create_optional_formula ( const std::string &  formula_string) const

Create a new formula from the string, using the symbol table which is stored in the AI.

Parameters
formula_stringthe string from which a formula should be created
Returns
pointer to created function or
Return values
game_logic::formula_ptr()if there were any problems

Definition at line 133 of file ai.cpp.

References e, function_table_, and handle_exception().

Referenced by ai::stage_unit_formulas::do_play_stage(), on_create(), and ai::stage_side_formulas::on_create().

◆ display_message()

void ai::formula_ai::display_message ( const std::string &  msg) const
private

◆ evaluate()

std::string ai::formula_ai::evaluate ( const std::string &  formula_str)

◆ evaluate_candidate_action()

void ai::formula_ai::evaluate_candidate_action ( wfl::candidate_action_ptr  fai_ca)

Evaluate the fai candidate action.

Definition at line 658 of file ai.cpp.

References resources::gameboard.

Referenced by ai::fai_candidate_action_wrapper::evaluate().

◆ execute_candidate_action()

bool ai::formula_ai::execute_candidate_action ( wfl::candidate_action_ptr  fai_ca)

Execute the fai candidate action.

Returns
true if game state was changed
false if game state was not changed

Definition at line 664 of file ai.cpp.

References wfl::formula_callable::fake_ptr(), wfl::variant::is_empty(), and make_action().

Referenced by ai::fai_candidate_action_wrapper::execute().

◆ get_allowed_teleports()

pathfind::teleport_map ai::formula_ai::get_allowed_teleports ( unit_map::iterator unit_it) const

◆ get_inputs()

void ai::formula_ai::get_inputs ( wfl::formula_input_vector inputs) const
overrideprivatevirtual

Reimplemented from wfl::formula_callable.

Definition at line 549 of file ai.cpp.

References wfl::formula_callable::add_input(), and wfl::formula_callable::inputs().

◆ get_keeps()

variant ai::formula_ai::get_keeps ( ) const

Definition at line 585 of file ai.cpp.

References resources::gameboard, get_adjacent_tiles(), h, wfl::variant::is_null(), keeps_cache_, and w.

Referenced by get_value().

◆ get_keeps_cache()

const wfl::variant& ai::formula_ai::get_keeps_cache ( ) const
inline

Definition at line 114 of file ai.hpp.

References keeps_cache_.

◆ get_recursion_count()

int ai::formula_ai::get_recursion_count ( ) const
overridevirtual

Get the value of the recursion counter.

Reimplemented from ai::readonly_context_proxy.

Definition at line 86 of file ai.cpp.

References ai::recursion_counter::get_count(), and recursion_counter_.

◆ get_value()

variant ai::formula_ai::get_value ( const std::string &  key) const
overrideprivatevirtual

Implements wfl::formula_callable.

Definition at line 290 of file ai.cpp.

References attacks_callable, unit_type_data::build_all(), ai::readonly_context_proxy::current_team(), unit_map::end(), wfl::formula_callable::fake_ptr(), unit_type_data::find(), unit_map::find_leader(), unit_type::FULL, resources::gameboard, ai::readonly_context_proxy::get_aggression(), ai::readonly_context_proxy::get_allow_ally_villages(), ai::readonly_context_proxy::get_attacks_as_variant(), ai::readonly_context_proxy::get_avoid(), ai::readonly_context_proxy::get_caution(), ai::readonly_context_proxy::get_dstsrc(), ai::readonly_context_proxy::get_enemy_dstsrc(), ai::readonly_context_proxy::get_enemy_srcdst(), ai::readonly_context_proxy::get_grouping(), get_keeps(), ai::readonly_context_proxy::get_leader_aggression(), ai::readonly_context_proxy::get_leader_ignores_keep(), ai::readonly_context_proxy::get_leader_value(), terrain_filter::get_locations(), ai::readonly_context_proxy::get_passive_leader(), ai::readonly_context_proxy::get_passive_leader_shares_keep(), ai::readonly_context_proxy::get_recruitment_pattern(), ai::readonly_context_proxy::get_retreat_enemy_weight(), ai::readonly_context_proxy::get_retreat_factor(), ai::readonly_context_proxy::get_scout_village_targeting(), ai::side_context_proxy::get_side(), ai::readonly_context_proxy::get_srcdst(), ai::readonly_context_proxy::get_support_villages(), ai::readonly_context_proxy::get_village_value(), ai::readonly_context_proxy::get_villages_per_scout(), i, utf8::size(), t, game_board::teams(), resources::tod_manager, unit_types, game_board::units(), and vars_.

◆ handle_exception() [1/2]

void ai::formula_ai::handle_exception ( const wfl::formula_error e) const

◆ handle_exception() [2/2]

void ai::formula_ai::handle_exception ( const wfl::formula_error e,
const std::string &  failed_operation 
) const

Definition at line 112 of file ai.cpp.

References display_message(), e, and LOG_AI.

◆ load_candidate_action_from_config()

ca_ptr ai::formula_ai::load_candidate_action_from_config ( const config cfg)

Definition at line 66 of file ai.cpp.

References e, ERR_AI, function_table_, and handle_exception().

◆ make_action()

wfl::variant ai::formula_ai::make_action ( wfl::const_formula_ptr  formula_,
const wfl::formula_callable variables 
)

◆ on_create()

void ai::formula_ai::on_create ( )

◆ operator=()

formula_ai& ai::formula_ai::operator= ( const formula_ai )
delete

◆ set_ai_context()

void ai::formula_ai::set_ai_context ( ai_context context)

Definition at line 143 of file ai.cpp.

References ai_ptr_.

◆ set_value()

void ai::formula_ai::set_value ( const std::string &  key,
const wfl::variant value 
)
overrideprivatevirtual

Reimplemented from wfl::formula_callable.

Definition at line 581 of file ai.cpp.

References wfl::formula_callable::mutate_value(), and vars_.

◆ shortest_path_calculator()

pathfind::plain_route ai::formula_ai::shortest_path_calculator ( const map_location src,
const map_location dst,
unit_map::iterator unit_it,
pathfind::teleport_map allowed_teleports 
) const

◆ swap_move_map()

void ai::formula_ai::swap_move_map ( move_map_backup backup)

◆ to_config()

config ai::formula_ai::to_config ( ) const
virtual

Friends And Related Function Documentation

◆ ai_default

friend class ai_default
friend

Definition at line 167 of file ai.hpp.

◆ get_ai_context

ai_context& get_ai_context ( wfl::const_formula_callable_ptr  for_fai)
friend

Definition at line 37 of file callable_objects.cpp.

Member Data Documentation

◆ ai_ptr_

ai_context* ai::formula_ai::ai_ptr_
private

Definition at line 150 of file ai.hpp.

Referenced by evaluate(), make_action(), and set_ai_context().

◆ attacks_callable

wfl::attack_map_callable ai::formula_ai::attacks_callable
private

Definition at line 159 of file ai.hpp.

Referenced by get_value().

◆ cfg_

const config ai::formula_ai::cfg_
private

Definition at line 151 of file ai.hpp.

Referenced by on_create(), and to_config().

◆ function_table_

wfl::ai_function_symbol_table ai::formula_ai::function_table_
mutableprivate

◆ keeps_cache_

wfl::variant ai::formula_ai::keeps_cache_
mutableprivate

Definition at line 158 of file ai.hpp.

Referenced by get_keeps(), and get_keeps_cache().

◆ recursion_counter_

recursion_counter ai::formula_ai::recursion_counter_
private

Definition at line 152 of file ai.hpp.

Referenced by get_recursion_count().

◆ vars_

wfl::map_formula_callable ai::formula_ai::vars_
private

Definition at line 162 of file ai.hpp.

Referenced by get_value(), on_create(), set_value(), and to_config().


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