SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
Random.h
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include "filter/IFilter.h"
8 #include "common/Random.h"
9 
10 namespace ai {
11 
15 class Random: public IFilter {
16 protected:
17  Filters _filters;
18  int _n;
19 public:
20  Random(const std::string& parameters, const Filters& filters) :
21  IFilter("Random", parameters), _filters(filters) {
22  ai_assert(filters.size() == 1, "Random must have one child");
23  _n = std::stoi(parameters);
24  }
25 
26  FILTER_ACTION_FACTORY(Random)
27 
28  void filter (const AIPtr& entity) override;
29 };
30 
31 inline void Random::filter (const AIPtr& entity) {
32  FilteredEntities& filtered = getFilteredEntities(entity);
33  const FilteredEntities copy = filtered;
34  filtered.clear();
35  _filters.front()->filter(entity);
36  FilteredEntities rndFiltered = getFilteredEntities(entity);
37  ai::shuffle(rndFiltered.begin(), rndFiltered.end());
38  rndFiltered.resize(_n);
39  for (auto& e : copy) {
40  filtered.push_back(e);
41  }
42  for (auto& e : rndFiltered) {
43  filtered.push_back(e);
44  }
45 }
46 
47 }
#define ai_assert(condition,...)
Provide your own assert - this is only executed in DEBUG mode.
Definition: Types.h:75
This filter will preserve only a few random entries.
Definition: Random.h:15