mirror of
https://github.com/TangentFoxy/Behave.git
synced 2024-11-15 11:04:21 +00:00
addObject fully implemented
This commit is contained in:
parent
6a61891ade
commit
b34b6c8bc5
10
ReadMe.md
10
ReadMe.md
@ -13,10 +13,10 @@ bt = require "behave"
|
|||||||
|
|
||||||
tree = bt.Sequence({
|
tree = bt.Sequence({
|
||||||
bt.Node({
|
bt.Node({
|
||||||
run = function(self)
|
run = function(self, obj, ...)
|
||||||
-- do stuff
|
-- do stuff
|
||||||
end,
|
end,
|
||||||
finish = function(self)
|
finish = function(self, obj, ...)
|
||||||
-- finish up
|
-- finish up
|
||||||
end
|
end
|
||||||
}),
|
}),
|
||||||
@ -30,6 +30,9 @@ tree = bt.Sequence({
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
tree:addObject(obj)
|
||||||
|
tree:update(obj)
|
||||||
|
|
||||||
node1 = {
|
node1 = {
|
||||||
-- pretend this has run/finish functions or whatever
|
-- pretend this has run/finish functions or whatever
|
||||||
}
|
}
|
||||||
@ -74,6 +77,7 @@ will be passed to their children as they are called).
|
|||||||
Pass a table of nodes to these to set their contents. All composite nodes repeat
|
Pass a table of nodes to these to set their contents. All composite nodes repeat
|
||||||
after the entire tree has been processed.
|
after the entire tree has been processed.
|
||||||
|
|
||||||
|
- Composite: Super class of the other composite nodes.
|
||||||
- Sequence: Runs children until a failure or success of all.
|
- Sequence: Runs children until a failure or success of all.
|
||||||
- Selector: Runs children until a success or failure of all.
|
- Selector: Runs children until a success or failure of all.
|
||||||
- Random: Runs a random child and returns its result.
|
- Random: Runs a random child and returns its result.
|
||||||
@ -88,8 +92,8 @@ Pass a single node to these, except for `Repeat`, which needs a number followed
|
|||||||
by a node. All decorator nodes (except `RunOnce`) repeat after the entire tree
|
by a node. All decorator nodes (except `RunOnce`) repeat after the entire tree
|
||||||
has been processed.
|
has been processed.
|
||||||
|
|
||||||
|
- Decorator: Superclass. Just returns the result of its child node.
|
||||||
- Repeat: Repeats a node a specified number of times, fails if the node fails.
|
- Repeat: Repeats a node a specified number of times, fails if the node fails.
|
||||||
- Decorator: Does nothing except return the result of its child node.
|
|
||||||
- Succeed: Runs a node and returns success.
|
- Succeed: Runs a node and returns success.
|
||||||
- Fail: Runs a node and returns failure.
|
- Fail: Runs a node and returns failure.
|
||||||
- Invert: Runs a node, reporting a success on fail, and failure on success.
|
- Invert: Runs a node, reporting a success on fail, and failure on success.
|
||||||
|
239
behave.lua
239
behave.lua
@ -70,12 +70,63 @@ do
|
|||||||
_base_0.__class = _class_0
|
_base_0.__class = _class_0
|
||||||
Node = _class_0
|
Node = _class_0
|
||||||
end
|
end
|
||||||
local Sequence
|
local Composite
|
||||||
do
|
do
|
||||||
local _class_0
|
local _class_0
|
||||||
local _parent_0 = Node
|
local _parent_0 = Node
|
||||||
local _base_0 = {
|
local _base_0 = {
|
||||||
addObject = function(self, obj)
|
addObject = function(self, obj)
|
||||||
|
local _list_0 = self.nodes
|
||||||
|
for _index_0 = 1, #_list_0 do
|
||||||
|
local node = _list_0[_index_0]
|
||||||
|
node:addObject(obj)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
_base_0.__index = _base_0
|
||||||
|
setmetatable(_base_0, _parent_0.__base)
|
||||||
|
_class_0 = setmetatable({
|
||||||
|
__init = function(self, nodes)
|
||||||
|
if nodes == nil then
|
||||||
|
nodes = { }
|
||||||
|
end
|
||||||
|
self.nodes = nodes
|
||||||
|
return _class_0.__parent.__init(self)
|
||||||
|
end,
|
||||||
|
__base = _base_0,
|
||||||
|
__name = "Composite",
|
||||||
|
__parent = _parent_0
|
||||||
|
}, {
|
||||||
|
__index = function(cls, name)
|
||||||
|
local val = rawget(_base_0, name)
|
||||||
|
if val == nil then
|
||||||
|
local parent = rawget(cls, "__parent")
|
||||||
|
if parent then
|
||||||
|
return parent[name]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return val
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
__call = function(cls, ...)
|
||||||
|
local _self_0 = setmetatable({}, _base_0)
|
||||||
|
cls.__init(_self_0, ...)
|
||||||
|
return _self_0
|
||||||
|
end
|
||||||
|
})
|
||||||
|
_base_0.__class = _class_0
|
||||||
|
if _parent_0.__inherited then
|
||||||
|
_parent_0.__inherited(_parent_0, _class_0)
|
||||||
|
end
|
||||||
|
Composite = _class_0
|
||||||
|
end
|
||||||
|
local Sequence
|
||||||
|
do
|
||||||
|
local _class_0
|
||||||
|
local _parent_0 = Composite
|
||||||
|
local _base_0 = {
|
||||||
|
addObject = function(self, obj)
|
||||||
|
_class_0.__parent.__base.addObject(self)
|
||||||
obj[self.u] = {
|
obj[self.u] = {
|
||||||
index = 0,
|
index = 0,
|
||||||
running = 0
|
running = 0
|
||||||
@ -104,12 +155,8 @@ do
|
|||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
setmetatable(_base_0, _parent_0.__base)
|
setmetatable(_base_0, _parent_0.__base)
|
||||||
_class_0 = setmetatable({
|
_class_0 = setmetatable({
|
||||||
__init = function(self, nodes)
|
__init = function(self, ...)
|
||||||
if nodes == nil then
|
return _class_0.__parent.__init(self, ...)
|
||||||
nodes = { }
|
|
||||||
end
|
|
||||||
self.nodes = nodes
|
|
||||||
return _class_0.__parent.__init(self)
|
|
||||||
end,
|
end,
|
||||||
__base = _base_0,
|
__base = _base_0,
|
||||||
__name = "Sequence",
|
__name = "Sequence",
|
||||||
@ -141,9 +188,10 @@ end
|
|||||||
local Selector
|
local Selector
|
||||||
do
|
do
|
||||||
local _class_0
|
local _class_0
|
||||||
local _parent_0 = Node
|
local _parent_0 = Composite
|
||||||
local _base_0 = {
|
local _base_0 = {
|
||||||
addObject = function(self, obj)
|
addObject = function(self, obj)
|
||||||
|
_class_0.__parent.__base.addObject(self)
|
||||||
obj[self.u] = {
|
obj[self.u] = {
|
||||||
index = 0,
|
index = 0,
|
||||||
running = 0
|
running = 0
|
||||||
@ -172,12 +220,8 @@ do
|
|||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
setmetatable(_base_0, _parent_0.__base)
|
setmetatable(_base_0, _parent_0.__base)
|
||||||
_class_0 = setmetatable({
|
_class_0 = setmetatable({
|
||||||
__init = function(self, nodes)
|
__init = function(self, ...)
|
||||||
if nodes == nil then
|
return _class_0.__parent.__init(self, ...)
|
||||||
nodes = { }
|
|
||||||
end
|
|
||||||
self.nodes = nodes
|
|
||||||
return _class_0.__parent.__init(self)
|
|
||||||
end,
|
end,
|
||||||
__base = _base_0,
|
__base = _base_0,
|
||||||
__name = "Selector",
|
__name = "Selector",
|
||||||
@ -209,7 +253,7 @@ end
|
|||||||
local Random
|
local Random
|
||||||
do
|
do
|
||||||
local _class_0
|
local _class_0
|
||||||
local _parent_0 = Node
|
local _parent_0 = Composite
|
||||||
local _base_0 = {
|
local _base_0 = {
|
||||||
update = function(self, obj, ...)
|
update = function(self, obj, ...)
|
||||||
local index = math.floor(math.random() * #self.nodes + 1)
|
local index = math.floor(math.random() * #self.nodes + 1)
|
||||||
@ -219,12 +263,8 @@ do
|
|||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
setmetatable(_base_0, _parent_0.__base)
|
setmetatable(_base_0, _parent_0.__base)
|
||||||
_class_0 = setmetatable({
|
_class_0 = setmetatable({
|
||||||
__init = function(self, nodes)
|
__init = function(self, ...)
|
||||||
if nodes == nil then
|
return _class_0.__parent.__init(self, ...)
|
||||||
nodes = { }
|
|
||||||
end
|
|
||||||
self.nodes = nodes
|
|
||||||
return _class_0.__parent.__init(self)
|
|
||||||
end,
|
end,
|
||||||
__base = _base_0,
|
__base = _base_0,
|
||||||
__name = "Random",
|
__name = "Random",
|
||||||
@ -256,9 +296,10 @@ end
|
|||||||
local Randomizer
|
local Randomizer
|
||||||
do
|
do
|
||||||
local _class_0
|
local _class_0
|
||||||
local _parent_0 = Node
|
local _parent_0 = Composite
|
||||||
local _base_0 = {
|
local _base_0 = {
|
||||||
addObject = function(self, obj)
|
addObject = function(self, obj)
|
||||||
|
_class_0.__parent.__base.addObject(self)
|
||||||
obj[self.u] = {
|
obj[self.u] = {
|
||||||
running = false
|
running = false
|
||||||
}
|
}
|
||||||
@ -278,12 +319,8 @@ do
|
|||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
setmetatable(_base_0, _parent_0.__base)
|
setmetatable(_base_0, _parent_0.__base)
|
||||||
_class_0 = setmetatable({
|
_class_0 = setmetatable({
|
||||||
__init = function(self, nodes)
|
__init = function(self, ...)
|
||||||
if nodes == nil then
|
return _class_0.__parent.__init(self, ...)
|
||||||
nodes = { }
|
|
||||||
end
|
|
||||||
self.nodes = nodes
|
|
||||||
return _class_0.__parent.__init(self)
|
|
||||||
end,
|
end,
|
||||||
__base = _base_0,
|
__base = _base_0,
|
||||||
__name = "Randomizer",
|
__name = "Randomizer",
|
||||||
@ -430,82 +467,14 @@ do
|
|||||||
end
|
end
|
||||||
RandomSelector = _class_0
|
RandomSelector = _class_0
|
||||||
end
|
end
|
||||||
local Repeat
|
|
||||||
do
|
|
||||||
local _class_0
|
|
||||||
local _parent_0 = Node
|
|
||||||
local _base_0 = {
|
|
||||||
addObject = function(self, obj)
|
|
||||||
obj[self.u] = {
|
|
||||||
counter = 1,
|
|
||||||
running = false
|
|
||||||
}
|
|
||||||
end,
|
|
||||||
update = function(self, obj, ...)
|
|
||||||
local result = success
|
|
||||||
if obj[self.u].running then
|
|
||||||
result = self.node:update(obj, ...)
|
|
||||||
if not (result == running) then
|
|
||||||
obj[self.u].running = false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
while result == success and obj[self.u].counter < self.cycles do
|
|
||||||
obj[self.u].counter = obj[self.u].counter + 1
|
|
||||||
result = self.node:update(obj, ...)
|
|
||||||
end
|
|
||||||
if result == running then
|
|
||||||
obj[self.u].running = true
|
|
||||||
else
|
|
||||||
obj[self.u].counter = 1
|
|
||||||
end
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
}
|
|
||||||
_base_0.__index = _base_0
|
|
||||||
setmetatable(_base_0, _parent_0.__base)
|
|
||||||
_class_0 = setmetatable({
|
|
||||||
__init = function(self, cycles, node)
|
|
||||||
if cycles == nil then
|
|
||||||
cycles = 2
|
|
||||||
end
|
|
||||||
if node == nil then
|
|
||||||
node = Node()
|
|
||||||
end
|
|
||||||
self.cycles, self.node = cycles, node
|
|
||||||
return _class_0.__parent.__init(self)
|
|
||||||
end,
|
|
||||||
__base = _base_0,
|
|
||||||
__name = "Repeat",
|
|
||||||
__parent = _parent_0
|
|
||||||
}, {
|
|
||||||
__index = function(cls, name)
|
|
||||||
local val = rawget(_base_0, name)
|
|
||||||
if val == nil then
|
|
||||||
local parent = rawget(cls, "__parent")
|
|
||||||
if parent then
|
|
||||||
return parent[name]
|
|
||||||
end
|
|
||||||
else
|
|
||||||
return val
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
__call = function(cls, ...)
|
|
||||||
local _self_0 = setmetatable({}, _base_0)
|
|
||||||
cls.__init(_self_0, ...)
|
|
||||||
return _self_0
|
|
||||||
end
|
|
||||||
})
|
|
||||||
_base_0.__class = _class_0
|
|
||||||
if _parent_0.__inherited then
|
|
||||||
_parent_0.__inherited(_parent_0, _class_0)
|
|
||||||
end
|
|
||||||
Repeat = _class_0
|
|
||||||
end
|
|
||||||
local Decorator
|
local Decorator
|
||||||
do
|
do
|
||||||
local _class_0
|
local _class_0
|
||||||
local _parent_0 = Node
|
local _parent_0 = Node
|
||||||
local _base_0 = {
|
local _base_0 = {
|
||||||
|
addObject = function(self, obj)
|
||||||
|
return self.node:addObject(obj)
|
||||||
|
end,
|
||||||
update = function(self, obj, ...)
|
update = function(self, obj, ...)
|
||||||
return self.node:update(obj, ...)
|
return self.node:update(obj, ...)
|
||||||
end
|
end
|
||||||
@ -547,6 +516,78 @@ do
|
|||||||
end
|
end
|
||||||
Decorator = _class_0
|
Decorator = _class_0
|
||||||
end
|
end
|
||||||
|
local Repeat
|
||||||
|
do
|
||||||
|
local _class_0
|
||||||
|
local _parent_0 = Decorator
|
||||||
|
local _base_0 = {
|
||||||
|
addObject = function(self, obj)
|
||||||
|
_class_0.__parent.__base.addObject(self)
|
||||||
|
obj[self.u] = {
|
||||||
|
counter = 1,
|
||||||
|
running = false
|
||||||
|
}
|
||||||
|
end,
|
||||||
|
update = function(self, obj, ...)
|
||||||
|
local result = success
|
||||||
|
if obj[self.u].running then
|
||||||
|
result = self.node:update(obj, ...)
|
||||||
|
if not (result == running) then
|
||||||
|
obj[self.u].running = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
while result == success and obj[self.u].counter < self.cycles do
|
||||||
|
obj[self.u].counter = obj[self.u].counter + 1
|
||||||
|
result = self.node:update(obj, ...)
|
||||||
|
end
|
||||||
|
if result == running then
|
||||||
|
obj[self.u].running = true
|
||||||
|
else
|
||||||
|
obj[self.u].counter = 1
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
}
|
||||||
|
_base_0.__index = _base_0
|
||||||
|
setmetatable(_base_0, _parent_0.__base)
|
||||||
|
_class_0 = setmetatable({
|
||||||
|
__init = function(self, cycles, node)
|
||||||
|
if cycles == nil then
|
||||||
|
cycles = 2
|
||||||
|
end
|
||||||
|
if node == nil then
|
||||||
|
node = Node()
|
||||||
|
end
|
||||||
|
self.cycles, self.node = cycles, node
|
||||||
|
return _class_0.__parent.__init(self, self.node)
|
||||||
|
end,
|
||||||
|
__base = _base_0,
|
||||||
|
__name = "Repeat",
|
||||||
|
__parent = _parent_0
|
||||||
|
}, {
|
||||||
|
__index = function(cls, name)
|
||||||
|
local val = rawget(_base_0, name)
|
||||||
|
if val == nil then
|
||||||
|
local parent = rawget(cls, "__parent")
|
||||||
|
if parent then
|
||||||
|
return parent[name]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return val
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
__call = function(cls, ...)
|
||||||
|
local _self_0 = setmetatable({}, _base_0)
|
||||||
|
cls.__init(_self_0, ...)
|
||||||
|
return _self_0
|
||||||
|
end
|
||||||
|
})
|
||||||
|
_base_0.__class = _class_0
|
||||||
|
if _parent_0.__inherited then
|
||||||
|
_parent_0.__inherited(_parent_0, _class_0)
|
||||||
|
end
|
||||||
|
Repeat = _class_0
|
||||||
|
end
|
||||||
local Succeed
|
local Succeed
|
||||||
do
|
do
|
||||||
local _class_0
|
local _class_0
|
||||||
|
52
behave.moon
52
behave.moon
@ -33,12 +33,18 @@ class Node
|
|||||||
obj[@u].started = false
|
obj[@u].started = false
|
||||||
return result
|
return result
|
||||||
|
|
||||||
-- Runs children in order until one returns fail, or all succeed.
|
class Composite extends Node
|
||||||
class Sequence extends Node
|
|
||||||
new: (@nodes={}) =>
|
new: (@nodes={}) =>
|
||||||
super!
|
super!
|
||||||
|
|
||||||
addObject: (obj) =>
|
addObject: (obj) =>
|
||||||
|
for node in *@nodes
|
||||||
|
node\addObject obj
|
||||||
|
|
||||||
|
-- Runs children in order until one returns fail, or all succeed.
|
||||||
|
class Sequence extends Composite
|
||||||
|
addObject: (obj) =>
|
||||||
|
super!
|
||||||
obj[@u] = {
|
obj[@u] = {
|
||||||
index: 0
|
index: 0
|
||||||
running: 0
|
running: 0
|
||||||
@ -64,11 +70,9 @@ class Sequence extends Node
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
-- Runs children in order until one succeeds or all fail.
|
-- Runs children in order until one succeeds or all fail.
|
||||||
class Selector extends Node
|
class Selector extends Composite
|
||||||
new: (@nodes={}) =>
|
|
||||||
super!
|
|
||||||
|
|
||||||
addObject: (obj) =>
|
addObject: (obj) =>
|
||||||
|
super!
|
||||||
obj[@u] = {
|
obj[@u] = {
|
||||||
index: 0
|
index: 0
|
||||||
running: 0
|
running: 0
|
||||||
@ -93,19 +97,14 @@ class Selector extends Node
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
-- Runs a random child.
|
-- Runs a random child.
|
||||||
class Random extends Node
|
class Random extends Composite
|
||||||
new: (@nodes={}) =>
|
|
||||||
super!
|
|
||||||
|
|
||||||
update: (obj, ...) =>
|
update: (obj, ...) =>
|
||||||
index = math.floor math.random! * #@nodes + 1
|
index = math.floor math.random! * #@nodes + 1
|
||||||
return @nodes[index]\update obj, ...
|
return @nodes[index]\update obj, ...
|
||||||
|
|
||||||
class Randomizer extends Node
|
class Randomizer extends Composite
|
||||||
new: (@nodes={}) =>
|
|
||||||
super!
|
|
||||||
|
|
||||||
addObject: (obj) =>
|
addObject: (obj) =>
|
||||||
|
super!
|
||||||
obj[@u] = {
|
obj[@u] = {
|
||||||
running: false
|
running: false
|
||||||
}
|
}
|
||||||
@ -163,12 +162,23 @@ class RandomSelector extends Randomizer
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
-- Repeats a node a specified number of times, unless it fails.
|
class Decorator extends Node
|
||||||
class Repeat extends Node
|
new: (@node=Node!) =>
|
||||||
new: (@cycles=2, @node=Node!) =>
|
|
||||||
super!
|
super!
|
||||||
|
|
||||||
addObject: (obj) =>
|
addObject: (obj) =>
|
||||||
|
@node\addObject obj
|
||||||
|
|
||||||
|
update: (obj, ...) =>
|
||||||
|
return @node\update obj, ...
|
||||||
|
|
||||||
|
-- Repeats a node a specified number of times, unless it fails.
|
||||||
|
class Repeat extends Decorator
|
||||||
|
new: (@cycles=2, @node=Node!) =>
|
||||||
|
super @node
|
||||||
|
|
||||||
|
addObject: (obj) =>
|
||||||
|
super!
|
||||||
obj[@u] = {
|
obj[@u] = {
|
||||||
counter: 1
|
counter: 1
|
||||||
running: false
|
running: false
|
||||||
@ -193,13 +203,6 @@ class Repeat extends Node
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
class Decorator extends Node
|
|
||||||
new: (@node=Node!) =>
|
|
||||||
super!
|
|
||||||
|
|
||||||
update: (obj, ...) =>
|
|
||||||
return @node\update obj, ...
|
|
||||||
|
|
||||||
-- Returns success whether or not the node succeeds.
|
-- Returns success whether or not the node succeeds.
|
||||||
class Succeed extends Decorator
|
class Succeed extends Decorator
|
||||||
update: (obj, ...) =>
|
update: (obj, ...) =>
|
||||||
@ -285,6 +288,7 @@ setmetatable {
|
|||||||
:Node
|
:Node
|
||||||
|
|
||||||
-- Composite Nodes
|
-- Composite Nodes
|
||||||
|
:Composite
|
||||||
:Sequence
|
:Sequence
|
||||||
:Selector
|
:Selector
|
||||||
:Random
|
:Random
|
||||||
|
Loading…
Reference in New Issue
Block a user