Fun With Behaviour Trees

Published October 03, 2012
Advertisement
This is a repost from my external blog http://codingintransit.blogspot.ca

Hey readers, the last two weeks I've been working on game resource management and progressing on my job system.

Job System

My initial plan for the job system is essentially that every job has a set of abilities. These abilities can be learned by having that job assigned. The user can issue requests which need to be executed but not directly use the abilities. I iterate over all the character looking for a character that has an ability which can execute the request. Initially I implemented the abilities as a chain of actions. This worked good for my basic needs, my first ability being a build item ability, which consisted of an action chain with a move action and a build item action. This worked but then I started looking at resource management( like using resources to build something, not like in game assets ) and quickly realized that my simple chain of actions was not going to get the job done.

Behaviour Trees

What I decided to do was to make each ability it's own Behaviour Tree. This way I can define very detailed behaviour for how each ability will work. For example my initial flow for the build ability is:

  • Check to see if character is holding construction resource
  • If they are
  • .....Check to see if character is in range of the item to build
  • .....If they are
  • .........Start using resources to build item
  • ....If they aren't
  • ........Move towards build area
  • If they aren't
  • ....Check to see if resources available on map
  • ....If there is
  • ........Check to see if character is in range of resource
  • ........If they are
  • ............Pick up as much resources as character can carry
  • ........If they aren't
  • ............Move towards closest resource pile
  • ....If there isn't
  • ........Fail out job can't be completed right now.


If the job can't be completed it's unbound from the character ability and returned to the bottom of the command queue.

Actions and Decisions

The way I've gone about implementing this is to set up two templated classes, Action and Decision. They both take a templated type which has the implementation for that type of action or decision. Decisions are designed to be the logic which decides which path to take in the tree. Actions are designed to be small pieces of logic which are at the end of a chain of decisions. This is all then linked together with fast delegates. Every decision has a pass, and a fail delegate. Decisions and actions then have a function for get a delegate instance which can be hooked into the pass or the fail.

Abilities

These behaviour trees are then assigned to an ability. Currently the only implemented ability is the build ability. The idea is that every job has a number of abilities that can be used by the character that has that job equipped. For example the build ability is a part of the Handyman job. The user doesn't actually control these abilities. What they do is generate a request, so

  • The user chooses a tile they want to build.
  • They generate a build request. This request goes into a global request queue that the characters have access to.
  • On the character update if they aren't already fulfilling a request they will check the request queue and see if any of their abilities can be used to perform a request.
  • If an ability can be used, that ability is bound to the request and the character can start performing the request.
  • When the request is completed or can no longer be executed the ability is unbound and the request is deleted.


I have a video demoing the evaluation of the build ability. The blue tiles are spots where I've laid down a build request and the crate in the corner is a resource crate that contains some construction resources.

Apologies for the frame rate. I recorded it on my mobile dev machine, a Toshiba Netbook, so the recording software taxed the system a bit.


?
Previous Entry I Love This Bar
Next Entry Earth Cubed
1 likes 4 comments

Comments

Ashaman73
Just a note, try to put everything a npc does into a single bht. Once you start to use different bht to control a single npc, you might run into trouble. To handle different jobs or tasks in my bht I use a special kind of node, call it dispatcher/selector whatever. It selects a sub-tree depending on a given attribute, ie. the job_id. Then each sub-tree contains the neccessary logic for your ability/job, but still in the context of the whole bht.

Something like this:

[CODE]
=root (priority)
== flee ?
== fight ?
== job (selects subtree depending on job_id)
=== build
=== clean
=== patrol
== idle

[/CODE]
October 04, 2012 05:24 AM
Dancin_Fool
Thanks for the tip, I was definitely planning on doing something like that when I get to that point. I guess saying that each ability is it's own tree isn't quite true. My plan is to evaluate a set of needs prior to trying to execute any requests which will work quite similar to what you laid out.

My background is in rendering so it's been interesting reading up on all the AI stuff.
October 04, 2012 05:38 AM
nhold
Hah, that's really cool! Hope you keep it up!
October 04, 2012 06:08 AM
Dancin_Fool
Thank you, glad you liked it!
October 04, 2012 01:12 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement