Behavior Trees in TGB Experiment
by James Ford · 09/27/2008 (9:37 pm) · 9 comments
If you are interested in game AI and haven't heard of behavior trees you might want to check out AIGameDev.com. In short they are the new, sexier finite state machines, basically a good way of laying out behaviors for your game agents in a nice modular and reusable way.
I felt like messing around with them and maybe a few other AI techniques for some experience, and ended up with this...
Some "monster" agents getting hunted down by a "hero" agent.

To give you an idea what it looks like to put together a behavior tree in script...
Note that there's more code behind the scenes doing work of course, in fact some in C++.
So that looks fairly organized, maybe even a little "tree" like right? Still, creating them could be easier. So I've been working on an editor to help out.
Looking at the FighterBehaviorTree in game

So I'd say my review of behavior trees is "thumbs up". Once you have the framework in place, putting together a new tree consisting of a few sequences and selectors is a breeze.
Still I'm not doing anything very complex with these yet. Some more complex things I'd still like to try...
* building behavior trees dynamically, perhaps under the direction of a planner
And lots of stuff to add to the editor
* substituting one behavior for another ( correctly transferring over children )
* show the execution path in the tree editor
* Saving assembled trees to a managed script file
Of course there's plenty of other ai topics I'd like to look into
* fuzzy logic ( possibly used in a SelectorBehavior )
* a system for working and permanent memory of agents
* etc ad infinitum
I felt like messing around with them and maybe a few other AI techniques for some experience, and ended up with this...
Some "monster" agents getting hunted down by a "hero" agent.

To give you an idea what it looks like to put together a behavior tree in script...
Note that there's more code behind the scenes doing work of course, in fact some in C++.
function FighterBehaviorTree::create( %agent )
{
%tree = new BehaviorTree()
{
class = "FighterBehaviorTree";
agent = %agent;
};
%root = SelectorBehavior::create();
%tree.add( %root );
// Eat some Food
%beh = %root.add( SequenceBehavior::create() );
%beh.add( Condition::create( "HungryCondition", false, true ) );
%beh.add( ChatBehavior::create( "I'm hungry..." ) );
%sel = %beh.add( SelectorBehavior::create() );
%beh = %sel.add( SequenceBehavior::create() );
%beh.add( Condition::create( "HasFoodCondition", false, true ) );
%beh.add( ChatBehavior::create( "Eating some food!" ) );
%beh.add( LeafBehavior::create( "EatFoodBehavior" ) );
%beh.add( WaitBehavior::create( 2000 ) );
%beh = %sel.add( SequenceBehavior::create() );
%beh.add( Condition::create( "HasFoodCondition", false, false ) );
%beh.add( ChatBehavior::create( "Going to buy food!" ) );
%beh.add( MoveToPositionBehavior::create( $TavernPos, 0.1 ) );
%beh.add( LeafBehavior::create( "BuyFoodBehavior" ) );
// Kill a Monster
%beh = %root.add( SequenceBehavior::create() );
%beh.add( Condition::create( "MonstersNearbyCondition", true, true ) );
%sel = %beh.add( SelectorBehavior::create( "FightOrFleeSelector" ) );
%beh = %sel.add( SequenceBehavior::create() );
%beh.add( ChatBehavior::create( "Hunting a Monster!" ) );
%par = %beh.add( ParallelBehavior::create() );
%par.add( LeafBehavior::create( "TargetClosestEnemyBehavior" ) );
%par.add( FollowTargetBehavior::create( 15 ) );
%beh = %par.add( LoopBehavior::create( "FOREVER" ) );
%beh = %beh.add( SequenceBehavior::create() );
%beh.add( LeafBehavior::create( "ChooseWeaponBehavior" ) );
%beh.add( ChatBehavior::create( "Attacking!" ) );
%beh.add( LeafBehavior::create( "AttackTargetBehavior" ) );
%beh.add( WaitBehavior::create( 1000 ) );
// Rest at Inn
%beh = %root.add( SequenceBehavior::create() );
%beh.add( Condition::create( "TiredCondition", false ) );
%beh.add( Condition::create( "MonstersNearbyCondition", true, false ) );
%beh.add( MoveToPositionBehavior::create( $InnPos, 0.1 ) );
%beh.add( LeafBehavior::create( "SleepBehavior" ) );
// Wander
%seq = %root.add( SequenceBehavior::create() );
%seq.add( Condition::create( "MonstersNearbyCondition", true, false ) );
%beh = %seq.add( TimerBehavior::create( 3000 ) );
%beh.add( LeafBehavior::create( "WanderBehavior" ) );
%seq.add( WaitBehavior::create( 1500 ) );
return %tree;
}So that looks fairly organized, maybe even a little "tree" like right? Still, creating them could be easier. So I've been working on an editor to help out.
Looking at the FighterBehaviorTree in game

So I'd say my review of behavior trees is "thumbs up". Once you have the framework in place, putting together a new tree consisting of a few sequences and selectors is a breeze.
Still I'm not doing anything very complex with these yet. Some more complex things I'd still like to try...
* building behavior trees dynamically, perhaps under the direction of a planner
And lots of stuff to add to the editor
* substituting one behavior for another ( correctly transferring over children )
* show the execution path in the tree editor
* Saving assembled trees to a managed script file
Of course there's plenty of other ai topics I'd like to look into
* fuzzy logic ( possibly used in a SelectorBehavior )
* a system for working and permanent memory of agents
* etc ad infinitum
About the author
http://jamesdev.info
#2
09/28/2008 (7:22 am)
Cool, too bad I don't have Pro, it would come in handy.
#3
09/28/2008 (10:42 am)
That's seriously cool. At the moment I am simply using behaviors that act as part of a FSM. This works in that it allows me to expose variables in the level editor, but this is something I'd really like to get into at some point.
#4
09/28/2008 (12:39 pm)
Interesting thanks!
#5
Any rate, I enjoy the info you share James. If I can get an idea of the right questions to ask I'll try and send them your way.
09/28/2008 (9:47 pm)
Nice info! This is definitely stuff I'd like to learn more about and see from you since you've implemented a few AI projects. This reminds me of the goal driven agent behaviors discussed in Mat Buckland's book Programming Game AI by Example,which seams like a step up from your basic FSM. At some point I was inevitably going to start asking you questions about how you implemented some of the concepts in that book since I'm still unsure of where to start with implementing steering behaviors and understanding how to translate whats in Mat's book into script. I'm a little surprised more of the concepts aren't implemented yet since they're so basic and have been around for a while now. Any rate, I enjoy the info you share James. If I can get an idea of the right questions to ask I'll try and send them your way.
#6
09/29/2008 (11:01 am)
Nice!
#7
@Scott
Feel free to contact me via email or even gchat.
09/29/2008 (10:43 pm)
Thanks for the comments!@Scott
Feel free to contact me via email or even gchat.
#8
I would love the oppertunity to learn if you have a moment!
01/23/2009 (3:54 pm)
@James I sent a gchat invite because I wanted to speak at you about behavior treesI would love the oppertunity to learn if you have a moment!
Torque 3D Owner Novack
CyberianSoftware