41 #define ERR_CF LOG_STREAM(err, log_config)
42 #define WRN_CF LOG_STREAM(warn, log_config)
43 #define DBG_CF LOG_STREAM(debug, log_config)
46 #define ERR_WML LOG_STREAM(err, log_wml)
53 , use_flat_tod_(false)
69 std::vector<const unit *> ret;
74 if(max_matches == 0) {
87 if (
matches(*u, u->get_location())) {
88 return u.get_shared_ptr();
98 unit_filter_xy(
const std::string& x,
const std::string& y) : x_(x), y_(y) {}
105 if(x.empty() && y.empty()) {
108 else if(x ==
"recall" && y ==
"recall") {
116 const std::string x_;
117 const std::string y_;
133 std::size_t radius = cfg_[
"radius"].to_int(1);
136 std::vector<map_location::direction> dirs;
137 for(
const unit& u : units) {
140 if(u.underlying_id() == args.
u.
underlying_id() || distance > radius || !child_.matches(
unit_filter_args{u, from_loc, &args.u, args.fc, args.use_flat_tod} )) {
144 for(
unsigned j = 0; j < adjacent.size(); ++j) {
145 bool adj_or_dist = distance != 1 ?
distance_between(adjacent[j], from_loc) == (distance - 1) : adjacent[j] == from_loc;
151 assert(dir >= 0 && dir <= 5);
153 if(!i_adjacent.
empty()) {
166 if(i_count.
empty() && match_count == 0) {
183 unit_filter_child_literal(
const vconfig& v,
const F&
f) : v_(v) , f_(
f) {}
192 template<
typename T,
typename F>
195 unit_filter_attribute_parsed(T&& v, F&&
f) : v_(std::move(v)), f_(std::move(
f)) {}
204 template<
typename C,
typename F>
207 unit_filter_attribute_literal(std::string&& v, C&&
c, F&&
f) : v_(std::move(v)), c_(std::move(
c)), f_(std::move(
f)) {}
212 return f_(c_(v), args);
219 class contains_dollar_visitor
220 #ifdef USING_BOOST_VARIANT
221 :
public boost::static_visitor<bool>
225 contains_dollar_visitor() {}
229 bool operator()(
const T&)
const {
return false; }
231 bool operator()(
const t_string&)
const {
return true; }
233 bool operator()(
const std::string&
s)
const
235 return s.find(
'$') != std::string::npos;
242 unit_filter_compound::unit_filter_compound(
const vconfig&
cfg)
270 case conditional_type::type::filter_and:
271 res = res &&
filter.second.matches(args);
273 case conditional_type::type::filter_or:
274 res = res ||
filter.second.matches(args);
276 case conditional_type::type::filter_not:
277 res = res && !
filter.second.matches(args);
287 if (!
filter->matches(args)) {
297 children_.emplace_back(
new unit_filter_child_literal<F>(
c, func));
300 template<
typename C,
typename F>
306 children_.emplace_back(
new unit_filter_attribute_literal<C, F>(std::move(v.
str()), std::move(conv), std::move(func)));
309 children_.emplace_back(
new unit_filter_attribute_parsed<decltype(conv(v)), F>(std::move(conv(v)), std::move(func)));
315 const config& literal =
cfg.get_config();
318 if(literal.
empty()) {
return; }
345 std::set<std::string> types_expanded;
346 for(
const std::string&
type : types) {
347 if(types_expanded.count(
type)) {
351 const auto& tree = ut->advancement_tree();
352 types_expanded.insert(tree.begin(), tree.end());
353 types_expanded.insert(
type);
376 for(
const std::string& variation_id : types) {
377 if (
type->has_variation(variation_id)) {
389 for(
const std::string& ability_id : abilities) {
402 for(
const std::string& ability : abilities) {
423 for(
const std::string& ability : abilities) {
436 std::sort(res.begin(), res.end());
440 [](
const std::vector<std::string>& check_traits,
const unit_filter_args& args)
444 std::vector<std::string> isect;
445 std::sort(have_traits.begin(), have_traits.end());
446 std::set_intersection(check_traits.begin(), check_traits.end(), have_traits.begin(), have_traits.end(), std::back_inserter(isect));
447 return !isect.empty();
463 return gender == args.
u.
gender();
472 }
catch(std::invalid_argument&) {
485 std::vector<int> res;
488 res.push_back(std::stoi(s));
489 }
catch(std::invalid_argument&) {
490 WRN_CF <<
"ignored invalid side='" << s <<
"' in filter";
501 create_attribute(literal[
"status"],
505 for(
const std::string& status : statuses) {
514 create_attribute(literal[
"has_weapon"],
520 if(a.id() == weapon) {
528 create_attribute(literal[
"role"],
536 create_attribute(literal[
"alignment"],
544 create_attribute(literal[
"ai_special"],
552 create_attribute(literal[
"usage"],
556 for(
const std::string& usage : usages) {
557 if(args.
u.
usage() == usage) {
565 create_attribute(literal[
"canrecruit"],
573 create_attribute(literal[
"recall_cost"],
575 [](
const std::vector<std::pair<int,int>>& ranges,
const unit_filter_args& args)
581 create_attribute(literal[
"level"],
583 [](
const std::vector<std::pair<int,int>>& ranges,
const unit_filter_args& args)
589 create_attribute(literal[
"defense"],
591 [](
const std::vector<std::pair<int,int>>& ranges,
const unit_filter_args& args)
594 return in_ranges(actual_defense, ranges);
598 create_attribute(literal[
"movement_cost"],
600 [](
const std::vector<std::pair<int,int>>& ranges,
const unit_filter_args& args)
607 create_attribute(literal[
"vision_cost"],
609 [](
const std::vector<std::pair<int,int>>& ranges,
const unit_filter_args& args)
616 create_attribute(literal[
"jamming_cost"],
618 [](
const std::vector<std::pair<int,int>>& ranges,
const unit_filter_args& args)
625 create_attribute(literal[
"lua_function"],
636 create_attribute(literal[
"formula"],
642 lg::log_to_chat() <<
"Formula error while evaluating formula in unit filter: " <<
e.type <<
" at "
643 <<
e.filename <<
':' <<
e.line <<
")\n";
644 ERR_WML <<
"Formula error while evaluating formula in unit filter: " <<
e.type <<
" at "
645 <<
e.filename <<
':' <<
e.line <<
")";
655 auto secondary = std::make_shared<wfl::unit_callable>(*args.
u2);
664 lg::log_to_chat() <<
"Formula error in unit filter: " <<
e.type <<
" at " <<
e.filename <<
':' <<
e.line <<
")\n";
665 ERR_WML <<
"Formula error in unit filter: " <<
e.type <<
" at " <<
e.filename <<
':' <<
e.line <<
")";
672 create_attribute(literal[
"find_in"],
680 for (
const config&
c : gd->get_variable_access_read(find_in).as_array())
682 if(
c[
"id"] == args.
u.
id()) {
697 if (!literal[
"x"].
blank() || !literal[
"y"].blank()) {
698 children_.emplace_back(
new unit_filter_xy(literal[
"x"], literal[
"y"]));
701 for(
auto child :
cfg.all_ordered()) {
704 cond_children_.emplace_back(std::piecewise_construct_t(), std::tuple(*cond), std::tuple(child.second));
706 else if (child.first ==
"filter_wml") {
708 config fwml = c.get_parsed_config();
712 config::all_children_itors ci = fwml.all_children_range();
713 if (fwml.all_children_count() == 1 && fwml.attribute_count() == 1 && ci.front().key ==
"variables") {
714 return args.u.variables().matches(ci.front().cfg);
718 return ucfg.matches(fwml);
722 else if (child.first ==
"filter_vision") {
724 std::set<int> viewers;
725 side_filter ssf(c, args.fc);
726 std::vector<int> sides = ssf.get_teams();
727 viewers.insert(sides.begin(), sides.end());
729 for (const int viewer : viewers) {
730 bool fogged = args.context().get_disp_context().get_team(viewer).fogged(args.loc);
732 bool hiding = args.context().get_disp_context().get_team(viewer).is_enemy(args.u.side()) && args.u.invisible(args.loc);
733 bool unit_hidden = fogged || hiding;
734 if (c[
"visible"].to_bool(true) != unit_hidden) {
741 else if (child.first ==
"filter_adjacent") {
742 children_.emplace_back(
new unit_filter_adjacent(child.second));
744 else if (child.first ==
"filter_location") {
746 return terrain_filter(c, args.fc, args.use_flat_tod).match(args.loc);
749 else if (child.first ==
"filter_side") {
751 return side_filter(c, args.fc).match(args.u.side());
754 else if ((child.first ==
"filter_ability") || (child.first ==
"experimental_filter_ability")) {
755 if(child.first ==
"experimental_filter_ability"){
759 if(!(c.get_parsed_config())[
"active"].to_bool()){
760 for(const ability_ptr& p_ab : args.u.abilities()) {
761 if(p_ab->matches_filter(c.get_parsed_config())) {
766 return specials_context_t::has_active_ability_matching_filter(args.u, args.loc, c.get_parsed_config());
771 else if (child.first ==
"has_attack") {
773 for(const attack_type& a : args.u.attacks()) {
774 if(a.matches_filter(c.get_parsed_config())) {
782 std::stringstream errmsg;
783 errmsg <<
"encountered a child [" << child.first <<
"] of a standard unit filter, it is being ignored";
static bool is_enemy(std::size_t side, std::size_t other_side)
Variant for storing WML attributes.
std::string str(const std::string &fallback="") const
auto apply_visitor(const V &visitor) const
Visitor support: Applies a visitor to the underlying variant.
bool blank() const
Tests for an attribute that was never set.
bool empty() const
Tests for an attribute that either was never set or was set to "".
A config object defines a single node in a WML file, with access to child nodes.
const team & get_team(int side) const
This getter takes a 1-based side number, not a 0-based team number.
virtual const gamemap & map() const =0
virtual const unit_map & units() const =0
virtual const display_context & get_disp_context() const =0
virtual game_lua_kernel * get_lua_kernel() const =0
virtual const game_data * get_game_data() const =0
terrain_code get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
bool on_board(const map_location &loc) const
Tell if a location is on the map.
static bool has_active_ability_id(const unit &un, map_location loc, const std::string &id)
bool is_enemy(int n) const
Visitor helper class to parse the upkeep value from a config.
Visitor helper class to fetch the appropriate upkeep value.
unit_filter(const vconfig &cfg)
const filter_context * fc_
bool matches(const unit &u, const map_location &loc) const
Determine if *this matches filter at a specified location.
std::vector< const unit * > all_matches_on_map(const map_location *loc=nullptr, const unit *other_unit=nullptr) const
unit_const_ptr first_match_on_map() const
unit_filter_impl::unit_filter_compound impl_
Container associating units to locations.
const std::string & id() const
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.
A single unit type that the player may recruit.
const std::string & parent_id() const
The id of the original type from which this (variation) descended.
This class represents a single unit of a specific type.
A variable-expanding proxy for the config class.
bool as_bool() const
Returns a boolean state of the variant value.
Definitions for the interface to Wesnoth Markup Language (WML).
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Interfaces for manipulating version numbers of engine, add-ons, etc.
active_ability_list get_abilities(const std::string &tag_name, const map_location &loc) const
Gets the unit's active abilities of a particular type if it were on a specified location.
bool has_ability_type(const std::string &ability) const
Check if the unit has an ability of a specific type.
bool has_ability_by_id(const std::string &ability) const
Check if the unit has an ability of a specific ID.
unit_alignments::type alignment() const
The alignment of this unit.
int level() const
The current level of this unit.
std::string usage() const
Gets this unit's usage.
const std::string & get_role() const
Gets this unit's role.
int recall_cost() const
How much gold it costs to recall this unit, or -1 if the side's default recall cost is used.
const std::string & variation() const
The ID of the variation of this unit's type.
bool get_state(const std::string &state) const
Check if the unit is affected by a status effect.
const std::string & type_id() const
The id of this unit's type.
const unit_race * race() const
Gets this unit's race.
const unit_type & type() const
This unit's type, accounting for gender and variation.
bool can_recruit() const
Whether this unit can recruit other units - ie, are they a leader unit.
const std::string & id() const
Gets this unit's id.
int side() const
The side this unit belongs to.
std::size_t underlying_id() const
This unit's unique internal ID.
unit_race::GENDER gender() const
The gender of this unit.
const t_string & name() const
Gets this unit's translatable display name.
@ STATE_GUARDIAN
The unit cannot be healed.
attack_itors attacks()
Gets an iterator over this unit's attacks.
int defense_modifier(const t_translation::terrain_code &terrain, const map_location &loc) const
The unit's defense on a given terrain.
const map_location & get_location() const
The current map location this unit is at.
int movement_cost(const t_translation::terrain_code &terrain) const
Get the unit's movement cost on a particular terrain.
int vision_cost(const t_translation::terrain_code &terrain) const
Get the unit's vision cost on a particular terrain.
int jamming_cost(const t_translation::terrain_code &terrain) const
Get the unit's jamming cost on a particular terrain.
int upkeep() const
Gets the amount of gold this unit costs a side per turn.
utils::variant< upkeep_full, upkeep_loyal, int > upkeep_t
std::vector< std::string > get_traits_list() const
Gets a list of the traits this unit currently has, including hidden traits.
std::size_t distance_between(const map_location &a, const map_location &b)
Function which gives the number of hexes between two tiles (i.e.
void get_adjacent_tiles(const map_location &a, utils::span< map_location, 6 > res)
Function which, given a location, will place all adjacent locations in res.
Standard logging facilities (interface).
bool in_ranges(const Cmp c, const std::vector< std::pair< Cmp, Cmp >> &ranges)
std::stringstream & log_to_chat()
Use this to show WML errors in the ingame chat.
std::function< int(lua_State *)> lua_function
filter_context * filter_con
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
Function which will interpolate variables, starting with '$' in the string 'str' with the equivalent ...
std::set< std::string > split_set(std::string_view s, char sep, const int flags)
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
auto * find_if(Container &container, const Predicate &predicate)
Convenience wrapper for using find_if on a container without needing to comare to end()
std::vector< std::pair< int, int > > parse_ranges_unsigned(const std::string &str)
Handles a comma-separated list of inputs to parse_range, in a context that does not expect negative v...
std::vector< std::string > split(const config_attribute_value &val)
std::shared_ptr< const unit > unit_const_ptr
unit_race::GENDER string_gender(const std::string &str, unit_race::GENDER def)
Encapsulates the map of the game.
static std::vector< direction > parse_directions(const std::string &str)
Parse_directions takes a comma-separated list, and filters out any invalid directions.
direction
Valid directions which can be moved in our hexagonal world.
bool matches_range(const std::string &xloc, const std::string &yloc) const
static std::string get_string(enum_type key)
Converts a enum to its string equivalent.
static constexpr utils::optional< enum_type > get_enum(const std::string_view value)
Converts a string into its enum equivalent.
const filter_context & context() const
virtual bool matches(const unit_filter_args &) const =0
bool filter_impl(const unit_filter_args &u) const
void fill(const vconfig &cfg)
void create_child(const vconfig &c, F func)
void create_attribute(const config::attribute_value &c, C conv, F func)
std::vector< std::pair< conditional_type::type, unit_filter_compound > > cond_children_
virtual bool matches(const unit_filter_args &u) const override
std::vector< std::shared_ptr< unit_filter_base > > children_
static map_location::direction s
unit_type_data unit_types
static lg::log_domain log_wml("wml")
static lg::log_domain log_config("config")