SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
AIStateMessage.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  typedef std::vector<AIStateWorld> States;
20  States _states;
21 
22  void readState (streamContainer& in) {
23  const ai::CharacterId id = readInt(in);
24  const float x = readFloat(in);
25  const float y = readFloat(in);
26  const float z = readFloat(in);
27  const float orientation = readFloat(in);
28  const glm::vec3 position(x, y, z);
29 
30  AIStateWorld tree(id, position, orientation);
31  CharacterAttributes& attributes = tree.getAttributes();
32  readAttributes(in, attributes);
33  _states.push_back(tree);
34  }
35 
36  void writeState (streamContainer& out, const AIStateWorld& state) const {
37  addInt(out, state.getId());
38  const glm::vec3& position = state.getPosition();
39  addFloat(out, position.x);
40  addFloat(out, position.y);
41  addFloat(out, position.z);
42  addFloat(out, state.getOrientation());
43  writeAttributes(out, state.getAttributes());
44  }
45 
46  void writeAttributes(streamContainer& out, const CharacterAttributes& attributes) const {
47  addShort(out, static_cast<int16_t>(attributes.size()));
48  for (CharacterAttributes::const_iterator i = attributes.begin(); i != attributes.end(); ++i) {
49  addString(out, i->first);
50  addString(out, i->second);
51  }
52  }
53 
54  void readAttributes(streamContainer& in, CharacterAttributes& attributes) const {
55  const int size = readShort(in);
56  attributes.reserve(size);
57  for (int i = 0; i < size; ++i) {
58  const std::string& key = readString(in);
59  const std::string& value = readString(in);
60  attributes.insert(std::make_pair(key, value));
61  }
62  }
63 
64 public:
65  AIStateMessage() :
66  IProtocolMessage(PROTO_STATE) {
67  }
68 
69  explicit AIStateMessage(streamContainer& in) :
70  IProtocolMessage(PROTO_STATE) {
71  const int treeSize = readInt(in);
72  for (int i = 0; i < treeSize; ++i) {
73  readState(in);
74  }
75  }
76 
77  void addState(const AIStateWorld& tree) {
78  _states.push_back(tree);
79  }
80 
81  void addState(AIStateWorld&& tree) {
82  _states.push_back(std::move(tree));
83  }
84 
85  void serialize(streamContainer& out) const override {
86  addByte(out, _id);
87  addInt(out, static_cast<int>(_states.size()));
88  for (States::const_iterator i = _states.begin(); i != _states.end(); ++i) {
89  writeState(out, *i);
90  }
91  }
92 
93  inline const std::vector<AIStateWorld>& getStates() const {
94  return _states;
95  }
96 };
97 
98 }
float getOrientation() const
Definition: AIStubTypes.h:215
This is a representation of a character state for the serialization.
Definition: AIStubTypes.h:174
A protocol message is used for the serialization of the ai states for remote debugging.
Definition: IProtocolMessage.h:60
const ai::CharacterId & getId() const
Definition: AIStubTypes.h:206
Message for the remote debugging interface.
Definition: AIStateMessage.h:17
const CharacterAttributes & getAttributes() const
Definition: AIStubTypes.h:229
const glm::vec3 & getPosition() const
Definition: AIStubTypes.h:222