-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbehavior_tree.js
More file actions
212 lines (193 loc) · 4.42 KB
/
behavior_tree.js
File metadata and controls
212 lines (193 loc) · 4.42 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// My attempt at implementing behavior trees,
// as described by Damian Isla, Chris Hecker,
// Alex Champandard, and Bjoern Knafla.
//
// I'm incompetent in javascript, so this probably
// looks very awkward. Sorry.
//
// Note there's a dependency on Object.extend(subclass, superclass),
// to implement inheritance.
'use strict'
function BehaviorTree() {
var Done = function() {
}
Done.prototype.done = function() {
return true
}
Done.prototype.pushLeft = function() {
return this
}
Done.prototype.pushRight = function() {
return this
}
this.Done = Done; // stick it into the namespace
var failure = new Done()
this.failure = failure; // stick it into the namespace
var success = new Done()
this.success = success; // stick it into the namespace
var Running = function() {
}
Running.prototype.done = function() {
return false
}
Running.prototype.pushLeft = function() {
return new Left(this)
}
Running.prototype.pushRight = function() {
return new Right(this)
}
this.Running = Running
var sequential_and = {}
sequential_and.shortCutsOn = function(result) {
return result === failure
}
sequential_and.id = function() {
return success
}
sequential_and.killer = failure
sequential_and.reactive = function() {
return false
}
this.sequential_and = sequential_and
var sequential_or = {}
sequential_or.shortCutsOn = function(result) {
return result === success
}
sequential_or.id = function() {
return failure
}
sequential_or.killer = success
sequential_or.reactive = function() {
return false
}
this.sequential_or = sequential_or
// Reactive means you're going to perform the test every time
var reactive_and = {}
reactive_and.shortCutsOn = function(result) {
return result === failure
}
reactive_and.id = function() {
return success
}
reactive_and.killer = failure
reactive_and.reactive = function() {
return true
}
this.reactive_and = reactive_and
var reactive_or = {}
reactive_or.shortCutsOn = function(result) {
return result === success
}
reactive_or.id = function() {
return failure
}
reactive_or.killer = success
reactive_or.reactive = function() {
return true
}
this.reactive_or = reactive_or
function Branch(left, op, right) {
this.left = left
this.op = op
this.right = right
}
Branch.prototype.myEval = function(left_lambda, right_lambda) {
var x = left_lambda()
if (this.op.shortCutsOn(x)) {
return x
} else if (!x.done()) {
return x.pushLeft()
} else if (x === this.op.id()) {
return right_lambda().pushRight()
}
throw new Error("This should not happen")
}
Branch.prototype.start = function() {
var that = this
return this.myEval(function() {
return that.left.start()
}, function() {
return that.right.start()
})
}
this.Branch = Branch
function Leaf(x) {
this.x = x
}
Leaf.prototype.start = function() {
return this.x
}
this.Leaf = Leaf
function Delay(delayed) {
this.delayed = delayed
}
Delay.prototype.start = function() {
return new Resume()
}
Delay.prototype.resume = function() {
return this.delayed.start()
}
this.Delay = Delay
function Resume() {
}
Object.extend(Resume, Running)
Resume.prototype.step = function(delay) {
return delay.resume()
}
Resume.prototype.toString = function() {
return ''
}
this.Resume = Resume
function Left(path) {
this.path = path
}
Object.extend(Left, Running)
Left.prototype.step = function(tree) {
var that = this
return tree.myEval(function() {
return that.path.step(tree.left);
}, function() {
return tree.right.start();
})
}
Left.prototype.toString = function() {
return 'l' + this.path
}
this.Left = Left
function Right(path) {
this.path = path
}
Object.extend(Right, Running)
Right.prototype.step = function(tree) {
var that = this
if (tree.op.reactive()) {
return tree.myEval(function() {
return tree.left.start();
}, function() {
return that.path.step(tree.right);
})
} else {
return this.path.step(tree.right).pushRight()
}
}
Right.prototype.toString = function() {
return 'r' + this.path
}
this.Right = Right
function Prioritized(items) {
return items.reduce(function(l, r) {
return new Branch(l, reactive_or, r)
})
}
this.Prioritized = Prioritized
function Sequence(items) {
return items.reduce(function(l, r) {
return new Branch(l, sequential_and, r)
})
}
this.Sequence = Sequence
function Rule(test, action) {
return new Branch(test, reactive_and, action)
}
this.Rule = Rule
}