Skip to content

Commit

Permalink
Move filtering code out of the unit formula manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Mar 29, 2016
1 parent 49fc414 commit 7b6dea5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
13 changes: 12 additions & 1 deletion src/units/filter.cpp
Expand Up @@ -35,6 +35,8 @@
#include "units/types.hpp"
#include "variable.hpp" // needed for vconfig, scoped unit
#include "wml_exception.hpp" // needed for FAIL
#include "formula/callable_objects.hpp"
#include "formula/formula.hpp"

#include <boost/foreach.hpp>
#include <boost/optional.hpp>
Expand Down Expand Up @@ -551,7 +553,16 @@ bool basic_unit_filter_impl::internal_matches_filter(const unit & u, const map_l
}
}
if (!vcfg["formula"].blank()) {
if (!u.formula_manager().matches_filter(vcfg["formula"], loc, u)) {
try {
const unit_callable callable(loc,u);
const game_logic::formula form(vcfg["formula"]);
if(!form.evaluate(callable).as_bool()) {
return false;
}
return true;
} catch(game_logic::formula_error& e) {
lg::wml_error() << "Formula error in unit filter: " << e.type << " at " << e.filename << ':' << e.line << ")\n";
// Formulae with syntax errors match nothing
return false;
}
}
Expand Down
16 changes: 0 additions & 16 deletions src/units/formula_manager.cpp
Expand Up @@ -23,22 +23,6 @@

#include <boost/foreach.hpp>

bool unit_formula_manager::matches_filter(const std::string & cfg_formula, const map_location & loc, const unit & me)
{
try {
const unit_callable callable(loc,me);
const game_logic::formula form(cfg_formula);
if(!form.evaluate(callable).as_bool()) {///@todo use formula_ai
return false;
}
return true;
} catch(game_logic::formula_error& e) {
lg::wml_error() << "Formula error in unit filter: " << e.type << " at " << e.filename << ':' << e.line << ")\n";
// Formulae with syntax errors match nothing
return false;
}
}

void unit_formula_manager::add_formula_var(std::string str, variant var)
{
if(!formula_vars_) formula_vars_ = new game_logic::map_formula_callable;
Expand Down
2 changes: 0 additions & 2 deletions src/units/formula_manager.hpp
Expand Up @@ -43,8 +43,6 @@ class unit_formula_manager {
const std::string& get_loop_formula() const { return unit_loop_formula_; }
const std::string& get_priority_formula() const { return unit_priority_formula_; }

bool matches_filter( const std::string & cfg_formula, const map_location & loc, const unit & me);

void read(const config & ai);
void write(config & cfg);

Expand Down

0 comments on commit 7b6dea5

Please sign in to comment.