Skip to content

Commit

Permalink
Fix “filter_adjacent_location” not working in ability
Browse files Browse the repository at this point in the history
This is not a stricto sensus repair since in this configuration [filter_adjacent_location] will have to be encoded inside a [filter] filter, but the advantage is to generalize the use of this sub-filter to events and others, in addition to having a functional version for the abilities. I don't know if @stevecotton will succeed in repairing the current filter and I therefore propose an alternative.
  • Loading branch information
newfrenchy83 committed Oct 11, 2024
1 parent 1689f5e commit 0e21864
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/schema/filters/unit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
{FILTER_TAG "has_attack" weapon ()}
{FILTER_TAG "filter_vision" vision ()}
{FILTER_TAG "filter_adjacent" adjacent ()}
{FILTER_TAG "filter_adjacent_location" adjacent_location ()}
{FILTER_TAG "filter_location" location ()}
{FILTER_TAG "filter_side" side ()}
{FILTER_TAG "experimental_filter_ability" abilities ()}
Expand Down
40 changes: 40 additions & 0 deletions src/units/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,43 @@ struct unit_filter_adjacent : public unit_filter_base
const vconfig cfg_;
};

struct unit_filter_adjacent_location : public unit_filter_base
{
unit_filter_adjacent_location(const vconfig& cfg)
: child_(cfg)
, cfg_(cfg)
{
}

virtual bool matches(const unit_filter_args& args) const override
{
terrain_filter adj_filter(cfg_, resources::filter_con, false);
const auto adjacent = get_adjacent_tiles(args.loc);
int match_count=0;

config::attribute_value i_adjacent = cfg_["adjacent"];
std::vector<map_location::direction> dirs;
if (i_adjacent.empty()) {
dirs = map_location::all_directions();
} else {
dirs = map_location::parse_directions(i_adjacent);
}
for (map_location::direction dir : dirs) {
if(!adj_filter.match(adjacent[static_cast<int>(dir)])) {
continue;
}
++match_count;
}

static std::vector<std::pair<int,int>> default_counts = utils::parse_ranges_unsigned("1-6");
config::attribute_value i_count = cfg_["count"];
return in_ranges(match_count, !i_count.blank() ? utils::parse_ranges_unsigned(i_count) : default_counts);
}

const unit_filter_compound child_;
const vconfig cfg_;
};


template<typename F>
struct unit_filter_child_literal : public unit_filter_base
Expand Down Expand Up @@ -764,6 +801,9 @@ void unit_filter_compound::fill(vconfig cfg)
else if (child.first == "filter_adjacent") {
children_.emplace_back(new unit_filter_adjacent(child.second));
}
else if (child.first == "filter_adjacent_location") {
children_.emplace_back(new unit_filter_adjacent_location(child.second));
}
else if (child.first == "filter_location") {
create_child(child.second, [](const vconfig& c, const unit_filter_args& args) {
return terrain_filter(c, args.fc, args.use_flat_tod).match(args.loc);
Expand Down

0 comments on commit 0e21864

Please sign in to comment.