SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
AICharacterDetailsMessage.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "IProtocolMessage.h"
7 #include "AIStubTypes.h"
8 #include <vector>
9 
10 namespace ai {
11 
18 private:
19  CharacterId _chrId;
20  const AIStateAggro* _aggroPtr;
21  const AIStateNode* _rootPtr;
22  AIStateAggro _aggro;
23  AIStateNode _root;
24 
25  AIStateNode readNode (streamContainer& in) {
26  const int32_t nodeId = readInt(in);
27  const std::string& condition = readString(in);
28  const int64_t lastRun = readLong(in);
29  const TreeNodeStatus status = static_cast<TreeNodeStatus>(readByte(in));
30  const bool running = readBool(in);
31  const int16_t childrenCount = readShort(in);
32  AIStateNode node(nodeId, condition, lastRun, status, running);
33  for (uint8_t i = 0; i < childrenCount; ++i) {
34  const AIStateNode& child = readNode(in);
35  node.addChildren(child);
36  }
37  return node;
38  }
39 
40  void writeNode (streamContainer& out, const AIStateNode& node) const {
41  addInt(out, node.getNodeId());
42  addString(out, node.getCondition());
43  addLong(out, node.getLastRun());
44  addByte(out, node.getStatus());
45  addBool(out, node.isRunning());
46  const std::vector<AIStateNode>& children = node.getChildren();
47  addShort(out, static_cast<int16_t>(children.size()));
48  for (std::vector<AIStateNode>::const_iterator i = children.begin(); i != children.end(); ++i) {
49  writeNode(out, *i);
50  }
51  }
52 
53  void writeAggro(streamContainer& out, const AIStateAggro& aggro) const {
54  const std::vector<AIStateAggroEntry>& a = aggro.getAggro();
55  addShort(out, static_cast<int16_t>(a.size()));
56  for (std::vector<AIStateAggroEntry>::const_iterator i = a.begin(); i != a.end(); ++i) {
57  addInt(out, i->id);
58  addFloat(out, i->aggro);
59  }
60  }
61 
62  void readAggro(streamContainer& in, AIStateAggro& aggro) const {
63  const int size = readShort(in);
64  for (int i = 0; i < size; ++i) {
65  const CharacterId chrId = readInt(in);
66  const float aggroVal = readFloat(in);
67  aggro.addAggro(AIStateAggroEntry(chrId, aggroVal));
68  }
69  }
70 
71 public:
76  AICharacterDetailsMessage(const CharacterId& id, const AIStateAggro& aggro, const AIStateNode& root) :
77  IProtocolMessage(PROTO_CHARACTER_DETAILS), _chrId(id), _aggroPtr(&aggro), _rootPtr(&root) {
78  }
79 
80  explicit AICharacterDetailsMessage(streamContainer& in) :
81  IProtocolMessage(PROTO_CHARACTER_DETAILS), _aggroPtr(nullptr), _rootPtr(nullptr) {
82  _chrId = readInt(in);
83  readAggro(in, _aggro);
84  _root = readNode(in);
85  }
86 
87  void serialize(streamContainer& out) const override {
88  addByte(out, _id);
89  addInt(out, _chrId);
90  writeAggro(out, *_aggroPtr);
91  writeNode(out, *_rootPtr);
92  }
93 
94  inline const CharacterId& getCharacterId() const {
95  return _chrId;
96  }
97 
98  inline const AIStateAggro& getAggro() const {
99  if (_aggroPtr)
100  return *_aggroPtr;
101  return _aggro;
102  }
103 
104  inline const AIStateNode& getNode() const {
105  if (_rootPtr)
106  return *_rootPtr;
107  return _root;
108  }
109 };
110 
111 }
bool isRunning() const
Some nodes have a state that holds which children is currently running.
Definition: AIStubTypes.h:166
TreeNodeStatus getStatus() const
Definition: AIStubTypes.h:158
A protocol message is used for the serialization of the ai states for remote debugging.
Definition: IProtocolMessage.h:60
int64_t getLastRun() const
Definition: AIStubTypes.h:151
The list of aggro entry for a character.
Definition: AIStubTypes.h:30
Message for the remote debugging interface.
Definition: AICharacterDetailsMessage.h:17
AICharacterDetailsMessage(const CharacterId &id, const AIStateAggro &aggro, const AIStateNode &root)
Definition: AICharacterDetailsMessage.h:76
The aggro entry for the AIStateAggro.
Definition: AIStubTypes.h:19
This is a representation of a behaviour tree node for the serialization.
Definition: AIStubTypes.h:106