Grail (C++)  1.3.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
GrailStateSnapshot.h
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_STATE_SNAPSHOT_H
4 #define GRAIL_STATE_SNAPSHOT_H
5 
6 #include "BlackboardState.h"
7 #include "EntityGroupData.h"
8 #include "EntityState.h"
9 #include "Flatbuffers/GrailStateSnapshot_generated.h"
10 
11 #include <string>
12 #include <vector>
13 
14 namespace grail
15 {
16 namespace data
17 {
19  {
20  float absoluteTime = 0.0f;
21  float relativeTime = 0.0f;
22  std::vector<std::string> tags{};
23  std::vector<BlackboardState> sharedBlackboards{};
24  std::vector<EntityGroupData> entityGroups{};
25  std::vector<EntityState> entityData{};
26  };
27 
28  inline flatbuffers::Offset<generated::GrailStateSnapshot> Pack(flatbuffers::FlatBufferBuilder& builder,
29  const GrailStateSnapshot& snapshot)
30  {
31  std::vector<flatbuffers::Offset<generated::BlackboardState>> packedSharedBlackboards{};
32  packedSharedBlackboards.reserve(snapshot.sharedBlackboards.size());
33  std::vector<flatbuffers::Offset<generated::EntityGroupData>> packedEntityGroups{};
34  packedEntityGroups.reserve(snapshot.entityGroups.size());
35  std::vector<flatbuffers::Offset<generated::EntityState>> packedEntityData{};
36  packedEntityData.reserve(snapshot.entityData.size());
37  std::vector<flatbuffers::Offset<flatbuffers::String>> packedTags{};
38  packedTags.reserve(snapshot.tags.size());
39  std::for_each(snapshot.tags.begin(), snapshot.tags.end(),
40  [&packedTags, &builder](const auto& tag)
41  {
42  packedTags.push_back(builder.CreateString(tag));
43  });
44  std::for_each(snapshot.sharedBlackboards.begin(), snapshot.sharedBlackboards.end(),
45  [&packedSharedBlackboards, &builder](const auto& blackboard)
46  {
47  packedSharedBlackboards.push_back(Pack(builder, blackboard));
48  });
49  std::for_each(snapshot.entityGroups.begin(), snapshot.entityGroups.end(),
50  [&packedEntityGroups, &builder](const auto& entityGroup)
51  {
52  packedEntityGroups.push_back(Pack(builder, entityGroup));
53  });
54  std::for_each(snapshot.entityData.begin(), snapshot.entityData.end(),
55  [&packedEntityData, &builder](const auto& entityData)
56  {
57  packedEntityData.push_back(Pack(builder, entityData));
58  });
59  return generated::CreateGrailStateSnapshot(builder,
60  snapshot.absoluteTime,
61  snapshot.relativeTime,
62  builder.CreateVector(packedTags),
63  builder.CreateVector(packedSharedBlackboards),
64  builder.CreateVector(packedEntityGroups),
65  builder.CreateVector(packedEntityData));
66  }
67 
68  inline GrailStateSnapshot Unpack(const generated::GrailStateSnapshot& snapshot)
69  {
70  GrailStateSnapshot deserializedData{};
71  deserializedData.absoluteTime = snapshot.absoluteTime();
72  deserializedData.relativeTime = snapshot.relativeTime();
73 
74  deserializedData.tags.reserve(snapshot.tags()->size());
75  for(const auto& tag : *snapshot.tags())
76  {
77  deserializedData.tags.emplace_back(tag->str());
78  }
79  deserializedData.sharedBlackboards.reserve(snapshot.sharedBlackboards()->size());
80  for(const auto& blackboardState : *snapshot.sharedBlackboards())
81  {
82  deserializedData.sharedBlackboards.emplace_back(Unpack(*blackboardState));
83  }
84  deserializedData.entityGroups.reserve(snapshot.entityGroups()->size());
85  for(const auto& entityGroup : *snapshot.entityGroups())
86  {
87  deserializedData.entityGroups.emplace_back(Unpack(*entityGroup));
88  }
89  deserializedData.entityData.reserve(snapshot.entityData()->size());
90  for(const auto& entityData : *snapshot.entityData())
91  {
92  deserializedData.entityData.emplace_back(Unpack(*entityData));
93  }
94  return deserializedData;
95  }
96 }
97 }
98 
99 #endif
grail::data::GrailStateSnapshot
Definition: GrailStateSnapshot.h:18