Ember
Loading...
Searching...
No Matches
StateManager.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <memory>
6#include <mutex>
7#include <unordered_map>
8#include <vector>
9
10#include "Core/BehaviorTree.h"
12
13namespace Ember {
14namespace Network {
15
17 public:
19 using StateChangeCallback = std::function<void(int64_t node_id, NodeStatus old_status, NodeStatus new_status)>;
20 using TickCallback = std::function<void(int64_t tick_number)>;
21
22 struct TickSnapshot {
23 int64_t tick_number = 0;
24 int64_t timestamp_ms = 0;
25 std::unordered_map<int64_t, NodeStatus> states;
26 std::vector<int64_t> execution_path;
27 };
28
29 explicit StateManager(std::shared_ptr<EmberCore::BehaviorTree> tree);
30 ~StateManager() = default;
31
32 void ApplyTickUpdate(const Protocol::TickUpdate *update);
33
34 void ApplyBlackboardUpdate(const Protocol::BlackboardUpdate *update);
35
36 void Reset();
37
38 NodeStatus GetNodeStatus(int64_t node_id) const;
39 int64_t GetCurrentTick() const { return m_currentTick; }
40 const std::vector<int64_t> &GetExecutionPath() const { return m_executionPath; }
41
42 std::vector<int64_t> GetAndClearChangedNodes();
43
44 void EnableHistory(size_t max_ticks = 1000);
45 void DisableHistory();
46 bool IsHistoryEnabled() const { return m_historyEnabled; }
47 const std::vector<TickSnapshot> &GetHistory() const { return m_history; }
48 void SeekToTick(int64_t tick_number);
49
51 void AddTickCallback(TickCallback callback);
52
53 void ClearCallbacks();
54
55 private:
56 void NotifyStateChange(int64_t node_id, NodeStatus old_status, NodeStatus new_status);
57 void NotifyTick(int64_t tick_number);
58 void SaveSnapshot();
59
60 std::shared_ptr<EmberCore::BehaviorTree> m_tree;
61
62 std::unordered_map<int64_t, NodeStatus> m_nodeStates;
63 std::vector<int64_t> m_executionPath;
64 int64_t m_currentTick = 0;
65
66 bool m_historyEnabled = false;
67 size_t m_maxHistoryTicks = 1000;
68 std::vector<TickSnapshot> m_history;
69
70 std::vector<int64_t> m_changedNodeIds;
71
72 std::vector<StateChangeCallback> m_stateCallbacks;
73 std::vector<TickCallback> m_tickCallbacks;
74
75 mutable std::mutex m_mutex;
76};
77
78} // namespace Network
79} // namespace Ember
std::function< void(int64_t tick_number)> TickCallback
NodeStatus GetNodeStatus(int64_t node_id) const
void ApplyBlackboardUpdate(const Protocol::BlackboardUpdate *update)
void ApplyTickUpdate(const Protocol::TickUpdate *update)
std::vector< int64_t > m_changedNodeIds
void SeekToTick(int64_t tick_number)
int64_t GetCurrentTick() const
const std::vector< TickSnapshot > & GetHistory() const
std::vector< int64_t > GetAndClearChangedNodes()
std::unordered_map< int64_t, NodeStatus > m_nodeStates
void AddTickCallback(TickCallback callback)
std::vector< TickCallback > m_tickCallbacks
std::function< void(int64_t node_id, NodeStatus old_status, NodeStatus new_status)> StateChangeCallback
void AddStateChangeCallback(StateChangeCallback callback)
void NotifyStateChange(int64_t node_id, NodeStatus old_status, NodeStatus new_status)
const std::vector< int64_t > & GetExecutionPath() const
void EnableHistory(size_t max_ticks=1000)
std::vector< int64_t > m_executionPath
std::vector< TickSnapshot > m_history
Protocol::NodeStatus NodeStatus
StateManager(std::shared_ptr< EmberCore::BehaviorTree > tree)
std::shared_ptr< EmberCore::BehaviorTree > m_tree
void NotifyTick(int64_t tick_number)
std::vector< StateChangeCallback > m_stateCallbacks
std::unordered_map< int64_t, NodeStatus > states