在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):tanema/behaviourtree.lua开源软件地址(OpenSource Url):https://github.com/tanema/behaviourtree.lua开源编程语言(OpenSource Language):Lua 96.1%开源软件介绍(OpenSource Introduction):BehaviourTree.luaA Lua implementation of Behavior Trees ported from javascript here. They are useful for implementing AIs. If you need more information about Behavior Trees, look on GameDevAI, there is a nice video about Behavior Trees from Alex Champandard. There is also a nice read of Björn Knafla explaining how explaining how Behavior Trees work. InstallationJust copy the lib folder into your project folder, rename it (example: 'behaviourtree') For Plain Lua
For Love 2D
How to useCreating a simple taskA task is a simple local mytask = BehaviourTree.Task:new({
-- (optional) this function is called directly before the run method
-- is called. It allows you to setup things before starting to run
-- Beware: if task is resumed after calling running(), start is not called.
start = function(task, obj)
obj.isStarted = true
end,
-- (optional) this function is called directly after the run method
-- is completed with either success() or fail(). It allows you to clean up
-- things, after you run the task.
finish = function(task, obj)
obj.isStarted = false
end,
-- This is the meat of your task. The run method does everything you want it to do.
-- Finish it with one of these method calls:
-- success() - The task did run successfully
-- fail() - The task did fail
-- running() - The task is still running and will be called directly from parent node
run = function(task, obj)
task:success()
end
});
--you can also declare a task like this
local myothertask = BehaviourTree.Task:new()
function myothertask:start(obj)
obj.isStarted = true
end
function myothertask:finish(obj)
obj.isStarted = false
end
function myothertask:run(obj)
self:success()
end
--however the other syntax better lends itself to building an inline table The methods:
The interesting part:
Creating a sequenceA local mysequence = BehaviourTree.Sequence:new({
nodes = {
-- here comes in a list of nodes (Tasks, Sequences or Priorities)
-- as objects or as registered strings
}
}) Creating a priority selectorA local myselector = BehaviourTree.Priority:new({
nodes = {
-- here comes in a list of nodes (Tasks, Sequences or Priorities)
-- as objects or as registered strings
}
}) Creating a random selectorA local myselector = BehaviourTree.Random:new({
nodes = {
-- here comes in a list of nodes (Tasks, Sequences or Priorities)
-- as objects or as registered strings
}
}) Creating a behavior treeCreating a behavior tree is fairly simple. Just instantiate the local mytree = BehaviourTree:new({
tree = 'a selector' -- the value of tree can be either string (which is the registered name of a node), or any node
}) Run through the behavior treeBefore you let the tree do it's work you can add an object to the tree. This object will be passed into every mytree:setObject(someBot);
// do this in a loop:
mytree:run(); Using a lookup table for your tasksIf you need the same nodes multiple times in a tree (or even in different trees), there is an easy method to register this nodes, so you can simply reference it by given name. -- register a tree node using the registry
BehaviourTree.register('testtask', mytask)
-- or register anything automatically by giving it a name
BehaviourTree.Task:new({
name = 'registered task'
-- run impl.
})
Now you can simply use it by name Now putting it all togetherAnd now an example of how all could work together. BehaviourTree.Task:new({
name = 'bark',
run = function(task, dog)
dog:bark()
task:success()
end
})
local btree = BehaviourTree:new({
tree = BehaviourTree.Sequence:new({
nodes = {
'bark',
BehaviourTree.Task:new({
run = function(task, dog)
dog:randomlyWalk()
task:success()
end
}),
'bark',
BehaviourTree.Task:new({
run = function(task, dog)
if dog:standBesideATree() then
dog:liftALeg()
dog:pee()
task:success()
else
task:fail()
end
end
}),
}
})
});
local dog = Dog:new(--[[..]]) -- the nasty details of a dog are omitted
btree:setObject(dog)
for _ = 1, 20 do
btree:run()
end In this example the following happens: each pass on the for loop (our game loop), the dog barks – we implemented this with a registered node, because we do this twice – then it walks randomly around, then it barks again and then if it find's itself standing beside a tree it pees on the tree. DecoratorsInstead of a simple But it is useful as base class for new implementations, like the implemented local mysequence = BehaviourTree.Sequence:new({
nodes = {
-- here comes in a list of nodes (Tasks, Sequences or Priorities)
-- as objects or as registered strings
}
})
local decoratedSequence = BehaviourTree.InvertDecorator:new({
node: mysequence
}) Those three decorators are useful, but the most useful decorators are those you build for your project, that do stuff with your objects. Just check out the code, to see how simple it is, to create your decorator. ContributingIf you want to contribute? If you have some ideas or critics, just open an issue, here on github. If you want to get your hands dirty, you can fork this repo. But note: If you write code, don't forget to write tests. And then make a pull request. I'll be happy to see what's coming. ##Specs This project uses busted for its specs. If you want to run the specs, you will have to install it first. Then just execute the following: cd /folder/where/the/spec/folder/is
busted MIT LicenseCopyright (C) 2013 Georg Tavonius Copyright (C) 2014 Tim Anema Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论