SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
IsCloseToGroup.h
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include "ICondition.h"
8 #include "common/String.h"
9 #include "group/GroupMgr.h"
10 #include "zone/Zone.h"
11 
12 namespace ai {
13 
20 class IsCloseToGroup: public ICondition {
21 private:
22  GroupId _groupId;
23  float _distance;
24 public:
26 
27  explicit IsCloseToGroup(const std::string& parameters) :
28  ICondition("IsCloseToGroup", parameters) {
29  std::vector<std::string> tokens;
30  Str::splitString(_parameters, tokens, ",");
31  if (tokens.size() != 2) {
32  _groupId = -1;
33  _distance = -1.0f;
34  } else {
35  _groupId = std::stoi(tokens[0]);
36  _distance = std::stof(tokens[1]);
37  }
38  }
39 
40  virtual ~IsCloseToGroup() {
41  }
42 
43  bool evaluate(const AIPtr& entity) override {
44  if (_groupId == -1) {
45  return false;
46  }
47 
48  if (_distance < 0.0f) {
49  return false;
50  }
51 
52  const GroupMgr& mgr = entity->getZone()->getGroupMgr();
53  const glm::vec3& pos = mgr.getPosition(_groupId);
54  if (isInfinite(pos)) {
55  return false;
56  }
57  return glm::distance(pos, entity->getCharacter()->getPosition()) <= _distance;
58  }
59 };
60 
61 }
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
Checks whether the controlled AI is close to a particular group.
Definition: IsCloseToGroup.h:20
Maintains the groups a AI can be in.
Definition: GroupMgr.h:27
Condition related stuff.
glm::vec3 getPosition(GroupId id) const
Returns the average position of the group.
Definition: GroupMgr.h:259
bool evaluate(const AIPtr &entity) override
Checks whether the condition evaluates to true for the given entity.
Definition: IsCloseToGroup.h:43
#define CONDITION_FACTORY(ConditionName)
A condition factory macro to ease and unify the registration at AIRegistry.
Definition: ICondition.h:55