Grail (C++)  1.3.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
GrailDebugInfo.h
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_DEBUG_INFO_H
4 #define GRAIL_DEBUG_INFO_H
5 
6 #include "GrailStateSnapshot.h"
7 #include "PlannerSnapshots.h"
8 #include "SimulatedGamesSnapshots.h"
9 #include "UtilityReasonerSnapshot.h"
10 #include "../DataSerializationVersions.h"
11 #include "Flatbuffers/GrailDebugInfo_generated.h"
12 
13 #include <map>
14 #include <vector>
15 
16 namespace grail
17 {
18 namespace data
19 {
21  {
22  int version = data_serialization_versions::DEBUG_SERIALIZATION_VERSION;
23  float startTime = 0.0f;
24  float endTime = 0.0f;
25  std::vector<GrailStateSnapshot> grailStateSnapshots{};
26  std::map<size_t, std::vector<SimulatedGameReasonerSnapshot>> simGameReasonerSnapshotsPerEntityId{};
27  std::map<size_t, std::vector<UtilityReasonerSnapshot>> utilityReasonerSnapshotsPerEntityId{};
28  std::map<size_t, std::vector<PlannerReasonerSnapshot>> plannerReasonerSnapshotsPerEntityId{};
29  };
30 
31  inline flatbuffers::Offset<generated::GrailDebugInfo> Pack(flatbuffers::FlatBufferBuilder& builder,
32  const GrailDebugInfo& snapshot)
33  {
34  std::vector<flatbuffers::Offset<generated::GrailStateSnapshot>> packedGrailStates{};
35  packedGrailStates.reserve(snapshot.grailStateSnapshots.size());
36  std::vector<flatbuffers::Offset<generated::SimulatedGameReasonerSnapshot>> packedSimGames{};
37  packedSimGames.reserve(snapshot.simGameReasonerSnapshotsPerEntityId.size());
38  std::vector<flatbuffers::Offset<generated::UtilityReasonerSnapshot>> packedUtility{};
39  packedUtility.reserve(snapshot.utilityReasonerSnapshotsPerEntityId.size());
40  std::vector<flatbuffers::Offset<generated::PlannerReasonerSnapshot>> packedPlanners{};
41  packedPlanners.reserve(snapshot.plannerReasonerSnapshotsPerEntityId.size());
42 
43  std::for_each(snapshot.grailStateSnapshots.begin(), snapshot.grailStateSnapshots.end(),
44  [&packedGrailStates, &builder](const auto& state)
45  {
46  packedGrailStates.push_back(Pack(builder, state));
47  });
48  std::for_each(snapshot.simGameReasonerSnapshotsPerEntityId.begin(), snapshot.simGameReasonerSnapshotsPerEntityId.end(),
49  [&packedSimGames, &builder] (const auto& reasonerSnapshots)
50  {
51  std::for_each(reasonerSnapshots.second.begin(), reasonerSnapshots.second.end(),
52  [&packedSimGames, &builder](const auto reasonerSnapshot)
53  {
54  packedSimGames.push_back(Pack(builder, reasonerSnapshot));
55  });
56  });
57  std::for_each(snapshot.utilityReasonerSnapshotsPerEntityId.begin(), snapshot.utilityReasonerSnapshotsPerEntityId.end(),
58  [&packedUtility, &builder] (const auto& reasonerSnapshots)
59  {
60  std::for_each(reasonerSnapshots.second.begin(), reasonerSnapshots.second.end(),
61  [&packedUtility, &builder](const auto reasonerSnapshot)
62  {
63  packedUtility.push_back(Pack(builder, reasonerSnapshot));
64  });
65  });
66  std::for_each(snapshot.plannerReasonerSnapshotsPerEntityId.begin(), snapshot.plannerReasonerSnapshotsPerEntityId.end(),
67  [&packedPlanners, &builder] (const auto& reasonerSnapshots)
68  {
69  std::for_each(reasonerSnapshots.second.begin(), reasonerSnapshots.second.end(),
70  [&packedPlanners, &builder](const auto reasonerSnapshot)
71  {
72  packedPlanners.push_back(Pack(builder, reasonerSnapshot));
73  });
74  });
75 
76  return generated::CreateGrailDebugInfo(builder,
77  snapshot.version,
78  snapshot.startTime,
79  snapshot.endTime,
80  builder.CreateVector(packedGrailStates),
81  builder.CreateVector(packedSimGames),
82  builder.CreateVector(packedUtility),
83  builder.CreateVector(packedPlanners));
84  }
85 
86  inline GrailDebugInfo Unpack(const generated::GrailDebugInfo& snapshot)
87  {
88  GrailDebugInfo deserializedData{};
89  deserializedData.version = snapshot.version();
90  deserializedData.startTime = snapshot.startTime();
91  deserializedData.endTime = snapshot.endTime();
92 
93  deserializedData.grailStateSnapshots.reserve(snapshot.grailStateSnapshots()->size());
94  for(const auto& grailState : *snapshot.grailStateSnapshots())
95  {
96  deserializedData.grailStateSnapshots.emplace_back(Unpack(*grailState));
97  }
98  for(const auto& simGame : *snapshot.simGameReasonerSnapshots())
99  {
100  deserializedData.simGameReasonerSnapshotsPerEntityId[simGame->entityId()].emplace_back(Unpack(*simGame));
101  }
102  for(const auto& utility : *snapshot.utilityReasonerSnapshots())
103  {
104  deserializedData.utilityReasonerSnapshotsPerEntityId[utility->entityId()].emplace_back(Unpack(*utility));
105  }
106  for(const auto& planner : *snapshot.plannerReasonerSnapshots())
107  {
108  deserializedData.plannerReasonerSnapshotsPerEntityId[planner->entityId()].emplace_back(Unpack(*planner));
109  }
110 
111  return deserializedData;
112  }
113 }
114 }
115 
116 #endif
grail::data::GrailDebugInfo
Definition: GrailDebugInfo.h:20