Grail (C++)  1.3.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
DebugInfoGenerator.h
1 // Copyright QED Software 2023.
2 
3 #pragma once
4 
5 #include "IDebugSnapshotReceiver.h"
6 #include "ITimestampProvider.h"
7 #include "../../GrailData/DebugInfo/GrailDebugInfo.h"
8 
9 #include <cmath>
10 #include <memory>
11 
12 namespace grail
13 {
15  {
16  public:
17  DebugInfoGenerator(const std::shared_ptr<ITimestampProvider>& timestampProvider);
18  virtual ~DebugInfoGenerator() override;
19 
20  const data::GrailDebugInfo& GetDebugInfo() const;
21  void ResetDebugInfo();
22 #ifndef BUILD_ARM
23  void SaveToFile(const std::string& filePath, bool useBinaryFormat = true) const;
24 #endif
25  virtual void ProcessSnapshot(data::GrailStateSnapshot& snapshot) override;
26  virtual void ProcessSnapshot(data::UtilityReasonerSnapshot& snapshot) override;
27  virtual void ProcessSnapshot(data::PlannerReasonerSnapshot& snapshot) override;
28  virtual void ProcessSnapshot(data::SimulatedGameReasonerSnapshot& snapshot) override;
29 
30  private:
31  template <typename SnapshotType>
32  void ProcessNewSnapshot(SnapshotType& snapshot,
33  std::vector<SnapshotType>& snapshotList)
34  {
35  debugInfo.endTime = std::fmax(snapshot.absoluteTime, debugInfo.endTime);
36  snapshot.relativeTime = timestampProvider->GetTimestamp() - debugInfo.startTime;
37  snapshotList.push_back(snapshot);
38  }
39 
40  template <typename SnapshotType>
41  void ProcessReasonerSnapshot(SnapshotType& reasonerSnapshot,
42  std::map<size_t, std::vector<SnapshotType>>& listsPerEntity,
43  size_t entityId)
44  {
45  auto iter = listsPerEntity.find(entityId);
46  if(iter == listsPerEntity.end())
47  {
48  std::vector<SnapshotType> snapshots;
49  ProcessNewSnapshot(reasonerSnapshot, snapshots);
50  listsPerEntity[entityId] = std::move(snapshots);
51  }
52  else
53  {
54  ProcessNewSnapshot(reasonerSnapshot, iter->second);
55  }
56  }
57 
58  std::shared_ptr<ITimestampProvider> timestampProvider = nullptr;
59  data::GrailDebugInfo debugInfo{};
60  };
61 }
grail::data::GrailDebugInfo
Definition: GrailDebugInfo.h:20
grail::data::UtilityReasonerSnapshot
Definition: UtilityReasonerSnapshot.h:15
grail::data::SimulatedGameReasonerSnapshot
Definition: SimulatedGamesSnapshots.h:129
grail::DebugInfoGenerator
Definition: DebugInfoGenerator.h:14
grail::data::PlannerReasonerSnapshot
Definition: PlannerSnapshots.h:92
grail::IDebugSnapshotReceiver
Definition: IDebugSnapshotReceiver.h:16
grail::data::GrailStateSnapshot
Definition: GrailStateSnapshot.h:18