-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBehaviourTree.cpp
More file actions
executable file
·75 lines (68 loc) · 2.2 KB
/
BehaviourTree.cpp
File metadata and controls
executable file
·75 lines (68 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <map>
#include <iterator>
#include "BehaviourTree.h"
#ifdef BT_BUILD_WITH_JSON
std::map<std::string, std::function<BT_status(void)> > g_BTree_registered_tasks = {
// register all functions for all behaviour trees here
{ "", [] { return BT_status::SUCCESS; } }
};
std::string BT_getConfigJson(const std::string& path) {
std::string sjson;
std::ifstream ifs;
ifs.open(path.c_str());
while (!ifs.eof()) {
std::string tmp;
ifs >> tmp;
sjson += tmp;
}
return sjson;
}
/**
* \InputParam
* \json: Json::Value output from json::reader.parse
* \task_func_tbl: map from json string to function pointer
* \Return
* \root pointer
*/
BT_task* BT_buildTreeWithJsonValue(const Json::Value& json)
{
if (json["type"] == "Selector") {
BT_task* root = new BT_selector();
for (uint32_t i = 0; i < json["children"].size(); i++) {
root->addChild( BT_buildTreeWithJsonValue(json["children"].operator[](i)) );
}
return root;
} else if (json["type"] == "RandomSelector") {
BT_task* root = new BT_random_selector();
for (uint32_t i = 0; i < json["children"].size(); i++) {
root->addChild(BT_buildTreeWithJsonValue(json["children"].operator[](i)));
}
return root;
} else if (json["type"] == "Sequence") {
BT_task* rootseq = new BT_sequence();
for (uint32_t i = 0; i<json["children"].size(); i++) {
rootseq->addChild( BT_buildTreeWithJsonValue(json["children"].operator[](i)) );
}
return rootseq;
} else if (json["type"] == "Action") {
// FIXME: Map string name to function names, add more statements when adding more functions
BT_task* b = new BT_task(
g_BTree_registered_tasks[json["func"].asString()],
json["name"].asString(),
(json["sim"].asString() == "true") ? BT_status::SUCCESS : BT_status::FAILURE
);
return b;
} else if (json["type"] == "Condition") {
BT_task* b = new BT_task(
g_BTree_registered_tasks[json["func"].asString()],
json["name"].asString(),
(json["sim"].asString() == "true") ? BT_status::SUCCESS : BT_status::FAILURE
);
return b;
} else if (json["type"] == "Start") {
return BT_buildTreeWithJsonValue(json["children"].operator[](0) );
} else {
return BT_buildTreeWithJsonValue(json["nodes"].operator[](0) );
}
}
#endif