NoJobEngine 2.0.7
Loading...
Searching...
No Matches
BTAsset.h
Go to the documentation of this file.
1
6#pragma once
7#include "BTActionNode.h"
8#include "BTConditionNode.h"
9#include "BTSequence.h"
10#include "BTSelector.h"
11#include "BTTree.h"
12#include "BTDecorator.h"
13#include "BTService.h"
14#include "BTParallel.h"
15#include <filesystem>
16#include <fstream>
17#include <sstream>
18#include <string>
19#include <vector>
20#include <stdexcept>
21#include <unordered_set>
22
23namespace NoJob {
24// V1.9 portable, versioned, line-oriented asset. Indentation is not significant.
25// NODE <id> <parentId> <Sequence|Selector|Action|Condition> <nativeName-or-dash>
27 BTNodeID ID = 0, Parent = 0;
28 std::string Kind = "Sequence", Native = "-";
29 std::string Parameter = "-"; // Decorator observed key / Subtree asset path
30 float Interval = 0.5f; // Service interval in seconds
31};
32class BTAsset {
33public:
34 std::vector<BTAssetNode> Nodes;
35 static BTAsset Default() {
36 return {{{1,0,"Selector","-"},{2,1,"Sequence","-"},
37 {3,2,"Condition","CanSeeTarget"},{4,2,"Action","ChaseTarget"},
38 {5,1,"Action","StopMoving"}}};
39 }
40 void Validate() const {
41 if (Nodes.empty() || Nodes.size() > 4096) throw std::runtime_error("Invalid BT node count");
42 std::unordered_set<BTNodeID> ids;
43 std::size_t roots = 0;
44 for (const auto& n : Nodes) {
45 if (!n.ID || !ids.insert(n.ID).second) throw std::runtime_error("Duplicate/zero BT node ID");
46 if (n.Parent == 0) ++roots;
47 if (n.Kind != "Sequence" && n.Kind != "Selector" && n.Kind != "Action" && n.Kind != "Condition" &&
48 n.Kind != "Decorator" && n.Kind != "Service" && n.Kind != "Parallel")
49 throw std::runtime_error("Unknown BT node kind");
50 if ((n.Kind == "Action" || n.Kind == "Condition" || n.Kind == "Decorator" || n.Kind == "Service") && (n.Native.empty() || n.Native == "-"))
51 throw std::runtime_error("Missing native callback name");
52 }
53 if (roots != 1) throw std::runtime_error("BT requires exactly one root");
54 for (const auto& n : Nodes) if (n.Parent && !ids.contains(n.Parent))
55 throw std::runtime_error("BT parent not found");
56 for (const auto& n : Nodes) {
57 std::unordered_set<BTNodeID> visited;
58 auto current = n.ID;
59 while (current) {
60 if (!visited.insert(current).second) throw std::runtime_error("BT cycle detected");
61 const BTAssetNode* node = nullptr;
62 for (const auto& candidate : Nodes) if (candidate.ID == current) {node=&candidate;break;}
63 if (!node) throw std::runtime_error("BT missing ancestor");
64 current = node->Parent;
65 }
66 }
67 for (const auto& n : Nodes) {
68 std::size_t count=0;
69 for (const auto& child : Nodes) if (child.Parent==n.ID) ++count;
70 if ((n.Kind=="Decorator" || n.Kind=="Service") && count!=1)
71 throw std::runtime_error("Decorator/Service requires one child");
72 if (n.Kind=="Parallel" && count!=2)
73 throw std::runtime_error("Parallel requires exactly two children");
74 if (n.Kind=="Service" && n.Interval<=0.f)
75 throw std::runtime_error("Service interval must be positive");
76 }
77 for (const auto& n : Nodes) if (n.Parent) {
78 for (const auto& parent : Nodes) if (parent.ID == n.Parent &&
79 (parent.Kind == "Action" || parent.Kind == "Condition"))
80 throw std::runtime_error("BT leaf cannot have children");
81 }
82 }
83 void Save(const std::filesystem::path& path) const {
84 Validate();
85 if (!path.parent_path().empty()) std::filesystem::create_directories(path.parent_path());
86 std::ofstream out(path, std::ios::trunc);
87 if (!out) throw std::runtime_error("Cannot write BT asset: " + path.string());
88 out << "NJOB_BT 2\n";
89 for (const auto& n : Nodes) out << "NODE " << n.ID << ' ' << n.Parent << ' ' << n.Kind << ' ' << n.Native << ' ' << n.Parameter << ' ' << n.Interval << '\n';
90 if (!out) throw std::runtime_error("BT asset write failed");
91 }
92 static BTAsset Load(const std::filesystem::path& path) {
93 std::ifstream in(path);
94 if (!in) throw std::runtime_error("Cannot read BT asset: " + path.string());
95 std::string magic; int version = 0;
96 if (!(in >> magic >> version) || magic != "NJOB_BT" || (version != 1 && version != 2))
97 throw std::runtime_error("Unsupported BT asset version");
98 BTAsset asset;
99 std::string tag;
100 while (in >> tag) {
101 if (tag != "NODE") throw std::runtime_error("Unexpected BT asset token");
102 BTAssetNode node;
103 if (!(in >> node.ID >> node.Parent >> node.Kind >> node.Native)) throw std::runtime_error("Truncated BT asset");
104 if (version == 2 && !(in >> node.Parameter >> node.Interval))
105 throw std::runtime_error("Truncated BT v2 asset");
106 asset.Nodes.push_back(std::move(node));
107 }
108 if (!in.eof()) throw std::runtime_error("Invalid BT asset syntax");
109 asset.Validate();
110 return asset;
111 }
112 std::shared_ptr<const BTTree> Build(std::shared_ptr<const BTNativeRegistry> registry) const {
113 Validate();
114 if (!registry) throw std::invalid_argument("BT native registry required");
115 std::function<std::unique_ptr<BTNode>(BTNodeID)> make = [&](BTNodeID id) -> std::unique_ptr<BTNode> {
116 const BTAssetNode* node = nullptr;
117 for (const auto& n : Nodes) if (n.ID == id) { node = &n; break; }
118 if (!node) throw std::runtime_error("BT node missing");
119 if (node->Kind == "Decorator" || node->Kind == "Service") {
120 BTNodeID child = 0;
121 for (const auto& n : Nodes) if (n.Parent == id) { child = n.ID; break; }
122 if (node->Kind == "Decorator")
123 return std::make_unique<BTDecorator>(id,registry,node->Native,
124 node->Parameter == "-" ? "" : node->Parameter, make(child));
125 return std::make_unique<BTService>(id,registry,node->Native,node->Interval,make(child));
126 }
127 if (node->Kind == "Parallel") {
128 std::vector<BTNodeID> children;
129 for (const auto& n : Nodes) if (n.Parent == id) children.push_back(n.ID);
130 return std::make_unique<BTParallel>(id,make(children[0]),make(children[1]));
131 }
132 if (node->Kind == "Action") return std::make_unique<BTActionNode>(id,registry,node->Native);
133 if (node->Kind == "Condition") return std::make_unique<BTConditionNode>(id,registry,node->Native);
134 if (node->Kind == "Sequence") {
135 auto result = std::make_unique<BTSequence>(id);
136 for (const auto& n : Nodes) if (n.Parent == id) result->AddChild(make(n.ID));
137 return result;
138 }
139 auto result = std::make_unique<BTSelector>(id);
140 for (const auto& n : Nodes) if (n.Parent == id) result->AddChild(make(n.ID));
141 return result;
142 };
143 for (const auto& n : Nodes) if (!n.Parent) return std::make_shared<const BTTree>(make(n.ID));
144 throw std::runtime_error("Missing BT root");
145 }
146};
147} // namespace NoJob
BTActionNode declarations for the Behavior Trees subsystem.
BTConditionNode declarations for the Behavior Trees subsystem.
BTDecorator declarations for the Behavior Trees subsystem.
BTParallel declarations for the Behavior Trees subsystem.
BTSelector declarations for the Behavior Trees subsystem.
BTSequence declarations for the Behavior Trees subsystem.
BTService declarations for the Behavior Trees subsystem.
Behavior tree ownership and evaluation.
std::shared_ptr< const BTTree > Build(std::shared_ptr< const BTNativeRegistry > registry) const
Definition BTAsset.h:112
std::vector< BTAssetNode > Nodes
Definition BTAsset.h:34
static BTAsset Default()
Definition BTAsset.h:35
void Save(const std::filesystem::path &path) const
Definition BTAsset.h:83
void Validate() const
Definition BTAsset.h:40
static BTAsset Load(const std::filesystem::path &path)
Definition BTAsset.h:92
std::uint64_t BTNodeID
Definition BTContext.h:19
std::string Kind
Definition BTAsset.h:28
BTNodeID Parent
Definition BTAsset.h:27
std::string Parameter
Definition BTAsset.h:29
std::string Native
Definition BTAsset.h:28