SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
Filter.h
Go to the documentation of this file.
1 
6 #pragma once
7 
9 #include "filter/IFilter.h"
10 
11 #define FILTER_NAME "Filter"
12 
13 namespace ai {
14 
20 class Filter: public ICondition {
21 protected:
22  Filters _filters;
23 
24  void getConditionNameWithValue(std::stringstream& s, const AIPtr& entity) override {
25  bool first = true;
26  s << "(";
27  auto copy = entity->_filteredEntities;
28  for (const FilterPtr& filter : _filters) {
29  if (!first)
30  s << ",";
31  s << filter->getName() << "{" << filter->getParameters() << "}[";
32  entity->_filteredEntities.clear();
33  filter->filter(entity);
34  bool firstChr = true;
35  int cnt = 0;
36  for (CharacterId id : entity->_filteredEntities) {
37  if (!firstChr)
38  s << ",";
39  s << id;
40  firstChr = false;
41  ++cnt;
42  if (cnt > 15) {
43  s << ",...";
44  break;
45  }
46  }
47  s << "]";
48  first = false;
49  }
50  entity->_filteredEntities = copy;
51  s << ")";
52  }
53 
54 public:
56 
57  explicit Filter (const Filters& filters) :
58  ICondition(FILTER_NAME, ""), _filters(filters) {
59  }
60 
66  bool evaluate(const AIPtr& entity) override {
67  entity->_filteredEntities.clear();
68  for (const FilterPtr filter : _filters) {
69  filter->filter(entity);
70  }
71  return !entity->_filteredEntities.empty();
72  }
73 };
74 
75 inline ConditionPtr Filter::Factory::create(const ConditionFactoryContext *ctx) const {
76  return std::make_shared<Filter>(ctx->filters);
77 }
78 
79 }
A condition can be placed on a TreeNode to decide which node is going to get executed. In general they are stateless. If they are not, it should explicitly get noted.
Definition: ICondition.h:124
void getConditionNameWithValue(std::stringstream &s, const AIPtr &entity) override
Override this method to get a more detailed result in getNameWithConditions()
Definition: Filter.h:24
Condition related stuff.
bool evaluate(const AIPtr &entity) override
Executes the attached filters and wiped the last filter results for the given AI entity.
Definition: Filter.h:66
#define CONDITION_FACTORY_NO_IMPL(ConditionName)
A condition factory macro to ease and unify the registration at AIRegistry. You still have to impleme...
Definition: ICondition.h:41
The filter condition executes some selection filters (IFilter) and evaluates to true if the resulting...
Definition: Filter.h:20