Grail (C++)  1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
EntityState.h
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_ENTITY_STATE_H
4 #define GRAIL_ENTITY_STATE_H
5 
6 #include "BehaviorData.h"
7 #include "Flatbuffers/EntityState_generated.h"
8 
9 #include <map>
10 #include <string>
11 #include <vector>
12 
13 namespace grail
14 {
15 namespace data
16 {
17  struct EntityState
18  {
19  std::string name{};
20  std::size_t id = 0;
21  std::map<std::string, std::string> privateBlackboardData{};
22  std::vector<std::string> entityTags{};
23  BehaviorData currentBehaviorData{};
24  };
25 
26  inline flatbuffers::Offset<generated::EntityState> Pack(flatbuffers::FlatBufferBuilder& builder,
27  const EntityState& entityState)
28  {
29  std::vector<flatbuffers::Offset<generated::StringStringMap>> packedPrivateBlackboards{};
30  packedPrivateBlackboards.reserve(entityState.privateBlackboardData.size());
31  std::vector<flatbuffers::Offset<flatbuffers::String>> packedTags{};
32  packedTags.reserve(entityState.entityTags.size());
33 
34  std::for_each(entityState.privateBlackboardData.begin(), entityState.privateBlackboardData.end(),
35  [&packedPrivateBlackboards, &builder](const auto& blackboardData)
36  {
37  packedPrivateBlackboards.push_back(generated::CreateStringStringMap(builder,
38  builder.CreateString(blackboardData.first),
39  builder.CreateString(blackboardData.second)));
40  });
41  std::for_each(entityState.entityTags.begin(), entityState.entityTags.end(),
42  [&packedTags, &builder](const auto& entityTag)
43  {
44  packedTags.push_back(builder.CreateString(entityTag));
45  });
46  return generated::CreateEntityState(builder,
47  builder.CreateString(entityState.name),
48  entityState.id,
49  builder.CreateVector(packedPrivateBlackboards),
50  builder.CreateVector(packedTags),
51  Pack(builder, entityState.currentBehaviorData));
52  }
53 
54  inline EntityState Unpack(const generated::EntityState& entityState)
55  {
56  EntityState deserializedState{entityState.name()->str(), entityState.id()};
57  for(const auto& blackboardData : *entityState.privateBlackboardData())
58  {
59  deserializedState.privateBlackboardData.insert({blackboardData->key()->str(), blackboardData->value()->str()});
60  }
61  deserializedState.entityTags.reserve(entityState.entityTags()->size());
62  for(const auto& entityTag : *entityState.entityTags())
63  {
64  deserializedState.entityTags.emplace_back(entityTag->str());
65  }
66  deserializedState.currentBehaviorData = Unpack(*entityState.currentBehaviorData());
67  return deserializedState;
68  }
69 }
70 }
71 
72 #endif
grail::data::EntityState
Definition: EntityState.h:17
grail::data::BehaviorData
Definition: BehaviorData.h:16