forked from 0xABAD/behavior_tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbehaviorTree.js
198 lines (187 loc) · 4.31 KB
/
behaviorTree.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const NodeType = {
FALLBACK: 1,
SEQUENCE: 2,
PARALLEL: 3,
ACTION: 4,
CONDITION: 5
}
const NodeStatus = {
SUCCESS: 1,
FAILED: 2,
RUNNING: 3
}
class Node {
constructor(name, nodeType, children = undefined) {
this.name = name
this.nodeType = nodeType
this.children = children || null;
this._active = false
this.nodeStatus = NodeStatus.FAILED;
}
status() {
return this.nodeStatus;
}
setStatus(status) {
this.nodeStatus = status;
}
active() {
return this._active;
}
setActive(isActive) {
this._active = isActive;
}
tick() {
this.setActive(true);
return this.status();
}
updateNameStatus(name, status) {
if (this.name == name) {
this.setStatus(status);
}
if (this.children) {
for (let i = 0; i < this.children.length; i++) {
this.children[i].updateNameStatus(name, status);
}
}
}
deactivate() {
this.setActive(false);
if (this.children) {
for (let i = 0; i < this.children.length; i++) {
this.children[i].deactivate();
}
}
}
}
class Fallback extends Node {
constructor(children = []) {
super('?', NodeType.FALLBACK, children || []);
}
tick() {
this.setActive(true);
for (let i = 0; i < this.children.length; i++) {
let s = this.children[i].tick();
this.setStatus(s);
if (s == NodeStatus.RUNNING || s == NodeStatus.SUCCESS) {
return this.status();
}
}
this.setStatus(NodeStatus.FAILED);
return this.status();
}
}
class Sequence extends Node {
constructor(children = []) {
super('\u2192', NodeType.SEQUENCE, children);
}
tick() {
this.setActive(true);
for (let i = 0; i < this.children.length; i++) {
let s = this.children[i].tick();
this.setStatus(s);
if (s == NodeStatus.RUNNING || s == NodeStatus.FAILED) {
return this.status();
}
}
this.setStatus(NodeStatus.SUCCESS);
return this.status();
}
}
class Parallel extends Node {
constructor(successCount, children = []) {
super('\u21C9', NodeType.PARALLEL, children || []);
this.successCount = successCount;
}
tick() {
this.setActive(true);
let succeeded = 0,
failed = 0,
kidCount = this.children.length;
for (let i = 0; i < this.children.length; i++) {
let s = this.children[i].tick();
if (s == NodeStatus.SUCCESS) {
succeeded++;
}
if (s == NodeStatus.FAILED) {
failed++;
}
}
let st = NodeStatus.RUNNING;
if (succeeded >= this.successCount) {
st = NodeStatus.SUCCESS;
} else if (failed > kidCount - this.successCount) {
st = NodeStatus.FAILED;
}
this.setStatus(st);
return st;
}
}
class Action extends Node {
constructor(name, status = NodeStatus.RUNNING) {
super(name, NodeType.ACTION);
this.setStatus(status);
}
nextStatus() {
if (this.nodeStatus == NodeStatus.RUNNING) {
this.setStatus(NodeStatus.SUCCESS);
} else if (this.nodeStatus == NodeStatus.SUCCESS) {
this.setStatus(NodeStatus.FAILED);
} else {
this.setStatus(NodeStatus.RUNNING);
}
}
}
class Condition extends Node {
constructor(name, hasNot = false, status = NodeStatus.FAILED) {
super(name, NodeType.CONDITION);
this.hasNot = hasNot;
this.setStatus(status);
}
status() {
let nodeStatus = super.status();
if (this.hasNot) {
switch (nodeStatus) {
case NodeStatus.SUCCESS: return NodeStatus.FAILED;
case NodeStatus.FAILED: return NodeStatus.SUCCESS;
}
}
return nodeStatus;
}
nextStatus(status) {
if (this.nodeStatus == NodeStatus.SUCCESS) {
this.setStatus(NodeStatus.FAILED);
}
else if (this.nodeStatus == NodeStatus.FAILED) {
this.setStatus(NodeStatus.SUCCESS);
}
}
}
/**
* Gets friendly status
* @param {number} status tree node status
* @returns {string} user-friendly status string
*/
function getFriendlyStatus(status) {
switch (status) {
case NodeStatus.FAILED:
return 'Failed';
case NodeStatus.SUCCESS:
return 'Success';
case NodeStatus.RUNNING:
return 'Running';
default:
return 'Unknown';
}
}
if (typeof exports !== 'undefined' && exports) {
exports.bt = {
Node,
Fallback,
Sequence,
Parallel,
Action,
Condition,
NodeType,
NodeStatus,
};
}