Skip to content

Commit

Permalink
Expose second unit to unit filter formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Mar 29, 2016
1 parent 7b6dea5 commit d8ee9dc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog
Expand Up @@ -16,6 +16,7 @@ Version 1.13.4+dev:
* New ~SCALE_INTO_SHARP(w,h) IPF which preserves aspect ratio, using
nearest neighbor scaling.
* Support delayed_variable_substitution= in [on_undo], [on_redo]
* formula= in SUF can now reference $other_unit via the formula variable "other"
* AiWML:
* Simplified aspect syntax which works for all aspects, present and future:
* All aspects with simple values can be specified as key=value
Expand Down
18 changes: 12 additions & 6 deletions src/units/filter.cpp
Expand Up @@ -187,7 +187,7 @@ class basic_unit_filter_impl : public unit_filter_abstract_impl {
std::vector<unit_filter> cond_children_;
std::vector<conditional::TYPE> cond_child_types_;

bool internal_matches_filter(const unit & u, const map_location & loc) const;
bool internal_matches_filter(const unit & u, const map_location & loc, const unit* u2) const;
};

/** "Factory" method which constructs an appropriate implementation
Expand Down Expand Up @@ -233,13 +233,13 @@ bool basic_unit_filter_impl::matches(const unit & u, const map_location& loc, co
if (u2) {
const map_location& loc2 = u2->get_location();
scoped_xy_unit auto_store("other_unit", loc2.x, loc2.y, fc_.get_disp_context().units());
matches = internal_matches_filter(u, loc);
matches = internal_matches_filter(u, loc, u2);
} else {
matches = internal_matches_filter(u, loc);
matches = internal_matches_filter(u, loc, u2);
}
} else {
// If loc is invalid, then this is a recall list unit (already been scoped)
matches = internal_matches_filter(u, loc);
matches = internal_matches_filter(u, loc, NULL);
}

// Handle [and], [or], and [not] with in-order precedence
Expand All @@ -258,7 +258,7 @@ bool basic_unit_filter_impl::matches(const unit & u, const map_location& loc, co
return matches;
}

bool basic_unit_filter_impl::internal_matches_filter(const unit & u, const map_location& loc) const
bool basic_unit_filter_impl::internal_matches_filter(const unit & u, const map_location& loc, const unit* u2) const
{
if (!vcfg["name"].blank() && vcfg["name"].t_str() != u.name()) {
return false;
Expand Down Expand Up @@ -554,7 +554,13 @@ bool basic_unit_filter_impl::internal_matches_filter(const unit & u, const map_l
}
if (!vcfg["formula"].blank()) {
try {
const unit_callable callable(loc,u);
const unit_callable main(loc,u);
game_logic::map_formula_callable callable(&main);
if (u2) {
boost::intrusive_ptr<unit_callable> secondary(new unit_callable(*u2));
callable.add("other", variant(secondary.get()));
// It's not destroyed upon scope exit because the variant holds a reference
}
const game_logic::formula form(vcfg["formula"]);
if(!form.evaluate(callable).as_bool()) {
return false;
Expand Down

0 comments on commit d8ee9dc

Please sign in to comment.