28 #define ERR_AI LOG_STREAM(err, log_formula_ai)
37 eval_(new
formula(cfg[
"evaluation"], function_table)),
38 action_(new
formula(cfg[
"action"], function_table)),
49 ai->handle_exception(
e);
53 ERR_AI <<
"formula type error while evaluating candidate action: " <<
e.message;
60 const std::string& name,
const std::string&
type,
68 for(
const auto& [key, value] : filter_params->attribute_range())
71 new formula(value, function_table));
81 callable.
add(
"input", input);
98 candidate_action_filters::const_iterator me_filter =
filter_map_.find(
"me");
100 std::vector<variant> unit_vector;
104 if (
i->side() ==
ai->get_side() &&
i->movement_left() > 0) {
105 unit_vector.emplace_back(std::make_shared<unit_callable>(*
i));
116 filtered_units=my_units;
119 ai->handle_exception(
e,
"Error while executing filter formula for '" +
get_name() +
"' Candidate Action");
126 callable.
add(
"me", *
i);
154 candidate_action_filters::const_iterator me_filter =
filter_map_.find(
"me");
155 candidate_action_filters::const_iterator target_filter =
filter_map_.find(
"target");
157 std::vector<variant> my_res, enemy_res;
161 if (
i->side() ==
ai->get_side())
163 if (
i->attacks_left()) {
164 my_res.emplace_back(std::make_shared<unit_callable>(*
i));
168 if (
ai->current_team().is_enemy(
i->side()) && !
i->incapacitated() && !
i->invisible(
i->get_location())) {
169 enemy_res.emplace_back(std::make_shared<unit_callable>(*
i));
174 variant enemy_units(enemy_res);
176 variant filtered_my_units, filtered_enemy_units;
179 filtered_my_units =
do_filtering(
ai, my_units, me_filter->second);
181 filtered_my_units = my_units;
184 filtered_enemy_units =
do_filtering(
ai, enemy_units, target_filter->second);
186 filtered_enemy_units = enemy_units;
189 ai->handle_exception(
e,
"Error while executing filter formula for '" +
get_name() +
"' Candidate Action");
198 ERR_AI <<
"Error while executing filter formulas for '" +
get_name() +
"' Candidate Action: " <<
e.message;
202 std::vector<variant> my_units_flt;
203 std::vector<variant> enemy_units_flt;
208 ERR_AI <<
"ERROR in "<<
get_name() <<
"Candidate Action: Filter formula returned table that does not contain units";
211 my_units_flt.emplace_back(u_callable);
217 ERR_AI <<
"ERROR in "<<
get_name() <<
"Candidate Action: Filter formula returned table that does not contain units";
220 enemy_units_flt.emplace_back(u_callable);
223 for( std::size_t my_unit = 0 ; my_unit < my_units_flt.size() ; ++my_unit){
224 auto my_unit_callable = my_units_flt[my_unit].convert_to<
unit_callable>();
225 for( std::size_t enemy_unit = 0 ; enemy_unit < enemy_units_flt.size() ; ++enemy_unit){
226 auto enemy_unit_callable = enemy_units_flt[enemy_unit].convert_to<
unit_callable>();
227 if(
ai->can_reach_unit(my_unit_callable->get_location(), enemy_unit_callable->get_location())) {
230 callable.
add(
"me", filtered_my_units[my_unit]);
231 callable.
add(
"target", filtered_enemy_units[enemy_unit]);
237 my_unit_ = filtered_my_units[my_unit];
static lg::log_domain log_formula_ai("ai/engine/fai")
Defines formula ai candidate actions - headers.
A config object defines a single node in a WML file, with access to child nodes.
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Container associating units to locations.
attack_candidate_action(const std::string &name, const std::string &type, const config &cfg, function_symbol_table *function_table)
virtual void evaluate(ai::formula_ai *ai, unit_map &units)
virtual void update_callable_map(map_formula_callable &callable)
const std::string & get_name() const
int execute_formula(const const_formula_ptr &formula, const formula_callable &callable, const ai::formula_ai *ai)
base_candidate_action(const std::string &name, const std::string &type, const config &cfg, function_symbol_table *function_table)
variant do_filtering(ai::formula_ai *ai, variant &input, const_formula_ptr formula)
candidate_action_with_filters(const std::string &name, const std::string &type, const config &cfg, function_symbol_table *function_table)
candidate_action_filters filter_map_
move_candidate_action(const std::string &name, const std::string &type, const config &cfg, function_symbol_table *function_table)
virtual void update_callable_map(map_formula_callable &callable)
virtual void evaluate(ai::formula_ai *ai, unit_map &units)
Iterator class for the variant.
variant_iterator begin() const
std::size_t num_elements() const
variant_iterator end() const
Standard logging facilities (interface).
A small explanation about what's going on here: Each action has access to two game_info objects First...
std::shared_ptr< const formula > const_formula_ptr