Wednesday, October 3, 2012

Having Fun With Behaviour Trees

Hey Readers,

The last two weeks I've been working on resource management and progressing on the 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.



No comments:

Post a Comment