Grail (C++)  1.3.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
EntityGroupData.h
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_ENTITY_GROUP_DATA_H
4 #define GRAIL_ENTITY_GROUP_DATA_H
5 
6 #include <vector>
7 #include "BlackboardState.h"
8 #include "Flatbuffers/EntityGroupData_generated.h"
9 #include "Flatbuffers/UintVectorOfStringMap_generated.h"
10 
11 namespace grail
12 {
13 namespace data
14 {
16  {
17  std::string name{};
18  unsigned int id = 0;
19  std::vector<unsigned int> entityIds{};
20  std::vector<BlackboardState> groupSharedBlackboardStates{};
21  std::map<unsigned int, std::vector<std::string>> groupBlackboardConnections{};
22  std::map<unsigned int, std::vector<std::string>> globalBlackboardConnections{};
23  };
24 
25  inline flatbuffers::Offset<generated::EntityGroupData> Pack(flatbuffers::FlatBufferBuilder& builder,
26  const EntityGroupData& entityGroupData)
27  {
28  std::vector<flatbuffers::Offset<generated::UintVectorOfStringMap>> packedGroupConnections{};
29  packedGroupConnections.reserve(entityGroupData.groupBlackboardConnections.size());
30  std::vector<flatbuffers::Offset<generated::UintVectorOfStringMap>> packedGlobalConnections{};
31  packedGlobalConnections.reserve(entityGroupData.globalBlackboardConnections.size());
32  std::vector<unsigned int> packedEntityIds{};
33  packedEntityIds.reserve(entityGroupData.entityIds.size());
34  std::vector<flatbuffers::Offset<generated::BlackboardState>> packedBlackboardStates{};
35  packedBlackboardStates.reserve(entityGroupData.groupSharedBlackboardStates.size());
36  std::for_each(entityGroupData.groupBlackboardConnections.begin(), entityGroupData.groupBlackboardConnections.end(),
37  [&packedGroupConnections, &builder](const auto& connection)
38  {
39  packedGroupConnections.push_back(generated::CreateUintVectorOfStringMap(builder,
40  connection.first, builder.CreateVectorOfStrings(connection.second)));
41  });
42  std::for_each(entityGroupData.globalBlackboardConnections.begin(), entityGroupData.globalBlackboardConnections.end(),
43  [&packedGlobalConnections, &builder](const auto& connection)
44  {
45  packedGlobalConnections.push_back(generated::CreateUintVectorOfStringMap(builder,
46  connection.first, builder.CreateVectorOfStrings(connection.second)));
47  });
48  std::for_each(entityGroupData.entityIds.begin(), entityGroupData.entityIds.end(), [&packedEntityIds](const auto& entityId)
49  {
50  packedEntityIds.emplace_back(entityId);
51  });
52  std::for_each(entityGroupData.groupSharedBlackboardStates.begin(), entityGroupData.groupSharedBlackboardStates.end(),
53  [&packedBlackboardStates, & builder](const auto& state)
54  {
55  packedBlackboardStates.emplace_back(Pack(builder, state));
56  });
57  return generated::CreateEntityGroupData(builder, builder.CreateString(entityGroupData.name), entityGroupData.id,
58  builder.CreateVector(packedEntityIds), builder.CreateVector(packedBlackboardStates),
59  builder.CreateVector(packedGroupConnections),
60  builder.CreateVector(packedGlobalConnections));
61  }
62 
63  inline EntityGroupData Unpack(const generated::EntityGroupData& entityGroupData)
64  {
65  EntityGroupData deserializedData{entityGroupData.name()->str(), entityGroupData.id()};
66 
67  deserializedData.entityIds.reserve(entityGroupData.entityIds()->size());
68  for(const auto& id : *entityGroupData.entityIds())
69  {
70  deserializedData.entityIds.emplace_back(id);
71  }
72  deserializedData.groupSharedBlackboardStates.reserve(entityGroupData.groupSharedBlackboardStates()->size());
73  for(const auto& blackboardState : *entityGroupData.groupSharedBlackboardStates())
74  {
75  deserializedData.groupSharedBlackboardStates.emplace_back(Unpack(*blackboardState));
76  }
77  for(const auto& groupConnection : *entityGroupData.groupBlackboardConnections())
78  {
79  std::vector<std::string> blackboardIds{};
80  for(const auto& entry : *groupConnection->value())
81  {
82  blackboardIds.emplace_back(entry->str());
83  }
84  deserializedData.groupBlackboardConnections.insert({groupConnection->key(), blackboardIds});
85  }
86  for(const auto& globalConnection : *entityGroupData.globalBlackboardConnections())
87  {
88  std::vector<std::string> blackboardIds{};
89  for(const auto& entry : *globalConnection->value())
90  {
91  blackboardIds.emplace_back(entry->str());
92  }
93  deserializedData.globalBlackboardConnections.insert({globalConnection->key(), blackboardIds});
94  }
95  return deserializedData;
96  }
97 }
98 }
99 
100 #endif
grail::data::EntityGroupData
Definition: EntityGroupData.h:15