Skip to content

Commit

Permalink
Merge pull request #1906 from gfgtdf/filter_refactor
Browse files Browse the repository at this point in the history
This refactors the unit filter so that it now parses as much as possible when
the filter is created to speed up the filter::matches function. This also makes
the provious optimisation of having a special empty filter unnecessary.
  • Loading branch information
gfgtdf committed Aug 27, 2017
2 parents 1b7bc55 + d2de54d commit 4dcbf6e
Show file tree
Hide file tree
Showing 9 changed files with 675 additions and 644 deletions.
6 changes: 2 additions & 4 deletions src/ai/lua/lua_object.cpp
Expand Up @@ -47,8 +47,7 @@ namespace ai {
}
lua_getfield(L, n, "own");
if(lua_istable(L, -1)) {
config cfg;
vconfig vcfg(cfg, true);
vconfig vcfg(config(), true);
if(luaW_tovconfig(L, -1, vcfg)) {
att->filter_own_.reset(new unit_filter(vcfg, resources::filter_con));
}
Expand All @@ -59,8 +58,7 @@ namespace ai {
}
lua_getfield(L, n, "enemy");
if(lua_istable(L, -1)) {
config cfg;
vconfig vcfg(cfg, true);
vconfig vcfg(config(), true);
if(luaW_tovconfig(L, -1, vcfg)) {
att->filter_enemy_.reset(new unit_filter(vcfg, resources::filter_con));
}
Expand Down
2 changes: 1 addition & 1 deletion src/scripting/lua_common.cpp
Expand Up @@ -799,7 +799,7 @@ bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg)
config cfg;
bool ok = luaW_toconfig(L, index, cfg);
if (!ok) return false;
vcfg = vconfig(cfg, true);
vcfg = vconfig(std::move(cfg));
break;
}
case LUA_TUSERDATA:
Expand Down
6 changes: 4 additions & 2 deletions src/side_filter.cpp
Expand Up @@ -123,8 +123,10 @@ bool side_filter::match_internal(const team &t) const
//Allow filtering on units
if(cfg_.has_child("has_unit")) {
const vconfig & ufilt_cfg = cfg_.child("has_unit");
if (!ufilter_)
ufilter_.reset(new unit_filter(ufilt_cfg, fc_, flat_));
if (!ufilter_) {
ufilter_.reset(new unit_filter(ufilt_cfg.make_safe(), fc_));
ufilter_->set_use_flat_tod(flat_);
}
bool found = false;
for(const unit &u : fc_->get_disp_context().units()) {
if (u.side() != t.side()) {
Expand Down
6 changes: 4 additions & 2 deletions src/terrain/filter.cpp
Expand Up @@ -165,8 +165,10 @@ bool terrain_filter::match_internal(const map_location& loc, const unit* ref_uni
const unit_map::const_iterator u = fc_->get_disp_context().units().find(loc);
if (!u.valid())
return false;
if (!cache_.ufilter_)
cache_.ufilter_.reset(new unit_filter(cfg_.child("filter"), fc_, flat_));
if (!cache_.ufilter_) {
cache_.ufilter_.reset(new unit_filter(cfg_.child("filter").make_safe(), fc_));
cache_.ufilter_->set_use_flat_tod(flat_);
}
if (!cache_.ufilter_->matches(*u, loc))
return false;
}
Expand Down
9 changes: 5 additions & 4 deletions src/units/abilities.cpp
Expand Up @@ -306,7 +306,7 @@ bool unit::ability_active(const std::string& ability,const config& cfg,const map
bool illuminates = ability == "illuminates";

if (const config &afilter = cfg.child("filter"))
if ( !unit_filter(vconfig(afilter), resources::filter_con, illuminates).matches(*this, loc) )
if ( !unit_filter(vconfig(afilter), resources::filter_con).set_use_flat_tod(illuminates).matches(*this, loc) )
return false;

map_location adjacent[6];
Expand All @@ -318,7 +318,8 @@ bool unit::ability_active(const std::string& ability,const config& cfg,const map
for (const config &i : cfg.child_range("filter_adjacent"))
{
size_t count = 0;
const unit_filter ufilt(vconfig(i), resources::filter_con, illuminates);
unit_filter ufilt(vconfig(i), resources::filter_con);
ufilt.set_use_flat_tod(illuminates);
std::vector<map_location::DIRECTION> dirs = map_location::parse_directions(i["adjacent"]);
for (const map_location::DIRECTION index : dirs)
{
Expand Down Expand Up @@ -389,7 +390,7 @@ bool unit::ability_affects_adjacent(const std::string& ability, const config& cf
}
const config &filter = i.child("filter");
if (!filter || //filter tag given
unit_filter(vconfig(filter), resources::filter_con, illuminates).matches(*this, loc, from) ) {
unit_filter(vconfig(filter), resources::filter_con).set_use_flat_tod(illuminates).matches(*this, loc, from) ) {
return true;
}
}
Expand All @@ -401,7 +402,7 @@ bool unit::ability_affects_self(const std::string& ability,const config& cfg,con
const config &filter = cfg.child("filter_self");
bool affect_self = cfg["affect_self"].to_bool(true);
if (!filter || !affect_self) return affect_self;
return unit_filter(vconfig(filter), resources::filter_con, ability == "illuminates").matches(*this, loc);
return unit_filter(vconfig(filter), resources::filter_con).set_use_flat_tod(ability == "illuminates").matches(*this, loc);
}

bool unit::has_ability_type(const std::string& ability) const
Expand Down

0 comments on commit 4dcbf6e

Please sign in to comment.