Grail (C++)  1.3.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  size_t id = 0;
21  std::map<std::string, std::string> privateBlackboardData{};
22  std::vector<std::string> localSharedBlackboardAccess{};
23  std::vector<std::string> globalSharedBlackboardAccess{};
24  std::vector<std::string> entityTags{};
25  BehaviorData currentBehaviorData{};
26  };
27 
28  inline flatbuffers::Offset<generated::EntityState> Pack(flatbuffers::FlatBufferBuilder& builder,
29  const EntityState& entityState)
30  {
31  std::vector<flatbuffers::Offset<generated::StringStringMap>> packedPrivateBlackboards{};
32  packedPrivateBlackboards.reserve(entityState.privateBlackboardData.size());
33  std::vector<flatbuffers::Offset<flatbuffers::String>> packedLocalBlackboards{};
34  packedLocalBlackboards.reserve(entityState.localSharedBlackboardAccess.size());
35  std::vector<flatbuffers::Offset<flatbuffers::String>> packedGlobalBlackboards{};
36  packedGlobalBlackboards.reserve(entityState.globalSharedBlackboardAccess.size());
37  std::vector<flatbuffers::Offset<flatbuffers::String>> packedTags{};
38  packedTags.reserve(entityState.entityTags.size());
39 
40  std::for_each(entityState.privateBlackboardData.begin(), entityState.privateBlackboardData.end(),
41  [&packedPrivateBlackboards, &builder](const auto& blackboardData)
42  {
43  packedPrivateBlackboards.push_back(generated::CreateStringStringMap(builder,
44  builder.CreateString(blackboardData.first),
45  builder.CreateString(blackboardData.second)));
46  });
47  std::for_each(entityState.localSharedBlackboardAccess.begin(), entityState.localSharedBlackboardAccess.end(),
48  [&packedLocalBlackboards, &builder](const auto& blackboardName)
49  {
50  packedLocalBlackboards.push_back(builder.CreateString(blackboardName));
51  });
52  std::for_each(entityState.globalSharedBlackboardAccess.begin(), entityState.globalSharedBlackboardAccess.end(),
53  [&packedGlobalBlackboards, &builder](const auto& blackboardName)
54  {
55  packedGlobalBlackboards.push_back(builder.CreateString(blackboardName));
56  });
57  std::for_each(entityState.entityTags.begin(), entityState.entityTags.end(),
58  [&packedTags, &builder](const auto& entityTag)
59  {
60  packedTags.push_back(builder.CreateString(entityTag));
61  });
62  return generated::CreateEntityState(builder,
63  builder.CreateString(entityState.name),
64  entityState.id,
65  builder.CreateVector(packedPrivateBlackboards),
66  builder.CreateVector(packedLocalBlackboards),
67  builder.CreateVector(packedGlobalBlackboards),
68  builder.CreateVector(packedTags),
69  Pack(builder, entityState.currentBehaviorData));
70  }
71 
72  inline EntityState Unpack(const generated::EntityState& entityState)
73  {
74  EntityState deserializedState{entityState.name()->str(), entityState.id()};
75  for(const auto& blackboardData : *entityState.privateBlackboardData())
76  {
77  deserializedState.privateBlackboardData.insert({blackboardData->key()->str(), blackboardData->value()->str()});
78  }
79  deserializedState.localSharedBlackboardAccess.reserve(entityState.localSharedBlackboardAccess()->size());
80  for(const auto& blackboardName : *entityState.localSharedBlackboardAccess())
81  {
82  deserializedState.localSharedBlackboardAccess.emplace_back(blackboardName->str());
83  }
84  deserializedState.globalSharedBlackboardAccess.reserve(entityState.globalSharedBlackboardAccess()->size());
85  for(const auto& blackboardName : *entityState.globalSharedBlackboardAccess())
86  {
87  deserializedState.globalSharedBlackboardAccess.emplace_back(blackboardName->str());
88  }
89  deserializedState.entityTags.reserve(entityState.entityTags()->size());
90  for(const auto& entityTag : *entityState.entityTags())
91  {
92  deserializedState.entityTags.emplace_back(entityTag->str());
93  }
94  deserializedState.currentBehaviorData = Unpack(*entityState.currentBehaviorData());
95  return deserializedState;
96  }
97 }
98 }
99 
100 #endif
grail::data::EntityState
Definition: EntityState.h:17
grail::data::BehaviorData
Definition: BehaviorData.h:16