Skip to content

Commit

Permalink
add all_matches, first_match fcns to unit_filter, use at [object]
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jul 5, 2014
1 parent b00c90d commit daa48e0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/game_events/action_wml.cpp
Expand Up @@ -1410,6 +1410,9 @@ WML_HANDLER_FUNCTION(move_units_fake, /*event_info*/, cfg)
WML_HANDLER_FUNCTION(object, event_info, cfg)
{
const vconfig & filter = cfg.child("filter");
boost::optional<unit_filter> ufilt;
if (!filter.null())
ufilt = unit_filter(filter, resources::filter_con);

std::string id = cfg["id"];

Expand All @@ -1422,13 +1425,10 @@ WML_HANDLER_FUNCTION(object, event_info, cfg)
std::string text;

map_location loc;
if(!filter.null()) {
const unit_filter ufilt(filter, resources::filter_con);
BOOST_FOREACH(const unit &u, *resources::units) {
if ( ufilt(u) ) {
loc = u.get_location();
break;
}
if(ufilt) {
unit_const_ptr u_ptr = ufilt->first_match_on_map();
if (u_ptr) {
loc = u_ptr->get_location();
}
}

Expand All @@ -1440,7 +1440,7 @@ WML_HANDLER_FUNCTION(object, event_info, cfg)

std::string command_type = "then";

if ( u != resources::units->end() && (filter.null() || unit_filter(filter, resources::filter_con).matches(*u)) )
if ( u != resources::units->end() && (!ufilt || ufilt->matches(*u)) )
{
///@deprecated This can be removed (and a proper duration=level implemented) after 1.11.2
/// Don't forget to remove it from wmllint too!
Expand Down
41 changes: 39 additions & 2 deletions src/unit_filter.cpp
Expand Up @@ -59,12 +59,27 @@ static boost::shared_ptr<unit_filter_abstract_impl> construct(const vconfig & vc
/// Null unit filter is built when the input config is null
class null_unit_filter_impl : public unit_filter_abstract_impl {
public:
null_unit_filter_impl() {}
null_unit_filter_impl(const filter_context & fc) : fc_(fc) {}
virtual bool matches(const unit & /*u*/, const map_location & /*loc*/) const {
return true;
}
virtual std::vector<const unit *> all_matches_on_map() const {
std::vector<const unit *> ret;
BOOST_FOREACH(const unit & u, fc_.get_disp_context().units()) {
ret.push_back(&u);
}
return ret;
}

virtual unit_const_ptr first_match_on_map() const {
return fc_.get_disp_context().units().begin().get_shared_ptr();
}


virtual ~null_unit_filter_impl() {}

private:
const filter_context & fc_;
};

/// This enum helps to evaluate conditional filters
Expand Down Expand Up @@ -220,6 +235,8 @@ class basic_unit_filter_impl : public unit_filter_abstract_impl {
}

virtual bool matches(const unit & u, const map_location & loc) const;
virtual std::vector<const unit *> all_matches_on_map() const;
virtual unit_const_ptr first_match_on_map() const;

virtual ~basic_unit_filter_impl() {}
private:
Expand Down Expand Up @@ -277,7 +294,7 @@ class basic_unit_filter_impl : public unit_filter_abstract_impl {
static boost::shared_ptr<unit_filter_abstract_impl> construct(const vconfig & vcfg, const filter_context & fc, bool flat_tod)
{
if (vcfg.null()) {
return boost::make_shared<null_unit_filter_impl> ();
return boost::make_shared<null_unit_filter_impl> (fc);
}
return boost::make_shared<basic_unit_filter_impl>(vcfg, fc, flat_tod);
//TODO: Add more efficient implementations for special cases
Expand Down Expand Up @@ -580,3 +597,23 @@ bool basic_unit_filter_impl::internal_matches_filter(const unit & u, const map_l

return true;
}

std::vector<const unit *> basic_unit_filter_impl::all_matches_on_map() const {
std::vector<const unit *> ret;
BOOST_FOREACH(const unit & u, fc_.get_disp_context().units()) {
if (matches(u, u.get_location())) {
ret.push_back(&u);
}
}
return ret;
}

unit_const_ptr basic_unit_filter_impl::first_match_on_map() const {
const unit_map & units = fc_.get_disp_context().units();
for(unit_map::const_iterator u = units.begin(); u != units.end(); u++) {
if (matches(*u,u->get_location())) {
return u.get_shared_ptr();
}
}
return unit_const_ptr();
}
13 changes: 13 additions & 0 deletions src/unit_filter.hpp
Expand Up @@ -26,7 +26,10 @@
#ifndef INCLUDED_UNIT_FILTER_HPP_
#define INCLUDED_UNIT_FILTER_HPP_

#include "unit_ptr.hpp"

#include <boost/shared_ptr.hpp>
#include <vector>

class filter_context;
class unit;
Expand All @@ -36,6 +39,8 @@ struct map_location;
class unit_filter_abstract_impl {
public:
virtual bool matches(const unit & u, const map_location & loc) const = 0;
virtual std::vector<const unit*> all_matches_on_map() const = 0;
virtual unit_const_ptr first_match_on_map() const = 0;
virtual ~unit_filter_abstract_impl() {}
};

Expand Down Expand Up @@ -71,6 +76,14 @@ class unit_filter {
bool operator()(const unit & u) const {
return matches(u);
}

std::vector<const unit *> all_matches_on_map() const {
return impl_->all_matches_on_map();
}

unit_const_ptr first_match_on_map() const {
return impl_->first_match_on_map();
}
private:
boost::shared_ptr<unit_filter_abstract_impl> impl_;
};
Expand Down

0 comments on commit daa48e0

Please sign in to comment.