Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

I have been working a lot on the code from last weekend and I didn't have time yet to talk about it here.
I made big progress with the engine : I finished to implement artificial intelligence, interaction system (what happened when player click on an object), and an event system.

What all these systems have in common for this project? they all use behavior trees.
I decided to use JBT (Java Behavior Tree) after I have read this article by Chris Simpson, who used it for his own game Project Zomboid, and if you are not familiar with behavior tree and need a good introduction about the concepts, you should definitely read it.

The only down-side of using JBT, is that this implementation requires to use Eclipse for editing the trees, since the tree editor is built upon Eclipse's extension system, and thus can be launched only from inside Eclipse. Also the source code have not been updated for the last 3 years, and even if the library itself is complete and entirely functional (I didn't find any bug so far), Eclipse has changed since then, and it seems it would not be easy to make it work with one recent version of Eclipse (newer than Kepler, 4.3). But everything is possible to add or change, since it's open-source!

There is also JBT's User Guide which gives a lot of useful information about how to configure and use it in your project.
Here is the process for making a behavior tree with JBT:
1) Write a domain file to describe the custom nodes you will use in the graph.
2) Write a configuration file to list all of the domains defined at 1).
3) Generate Java classes corresponding to those nodes using ActionsAndConditionsGenerator.jar (you can build this one yourself from JBT source).
4) Edit the generated classes to implement them (and so make the link with your game engine and logic).
5) Launch editor and make behavior trees using custom nodes + built-ins nodes.
6) Write another configuration file to list all your behavior trees and group them into "libraries".
7) Generate Java classes corresponding to those trees using BTLibraryGenerator.jar (you can also build this one yourself from JBT source).
8) Then behavior trees can be instantiated from the code by loading a graph name from a specific library instance:

     // IBTLibrary libraryName
     // String graphName
     IContext context = ContextFactory.createContext(libraryName);
     ModelTask tree = aiLib.getBT(graphName); // find the model for the tree
     IBTExecutor btExecutor = BTExecutorFactory.createBTExecutor(tree, context); // instantiate it

9) And in the game loop, each tree instance currently running need to be ticked every frame:

     btExecutor.tick();

10) Tree instance's status can be checked this way:

     if (btExecutor.getStatus() != Status.RUNNING)
     {
           // the tree terminated!
     }



Now let's see how I have been using it for each of the 3 systems.


Artificial Intelligence

This is the most common and straight-forward way of using behavior trees.
Usually, the tree will consists in a loop which will analyse the world around, check conditions (for example, which entity is in which state and where) and perform actions (such as moving, interacting with one object, etc).

Here is one example I made to test the AI system:

And here is the result applied in-game (I didn't have another model yet, so they all have the same model with the player, but smaller):



Interactions

When the player clicks on an entity, it will trigger a behavior tree corresponding to the entity type.
This is a different type of tree, since it will play once and then stop, but the content of the graph is pretty similar: it will contains a sequence of conditions and actions.
One difference also is that it will generally not checking much about the environment, but mostly check the state of the two entities : the object itself, and the character who triggered the interaction. Actions will usually apply to one of them too.


Here is one very simple example, which is the default behavior for objects which don't have a specific type:



Events


I decided to use behavior trees for event system too, because there are so many features that I need for them which already exist in the behavior trees.
Events are divided in two types of trees:
- triggers, which are actually AI behaviors, but instead of applying it to AI characters who are moving around, it's applied to static entities which will just check for conditions and trigger some actions.
- events, which are actually interactions, but instead of being triggered by the player clicking on an entity, it's triggered by custom conditions defined in the behavior tree itself (it usually will wait in a node for a specific condition).


For example, there is a mechanism for the player to jump to another planet from a specific spot.
Instead of using any UI, we decided to completely integrate it as a point'n click mechanism:
- When the player pass by the planet jumping spot, one special "Planet Switch" entity appears there.
- The main character will say something about it (to help the player to understand what is it).
- If the player clicks on this "Planet Switch" entity, main character will move there and start jumping to the new planet.

For this, I needed first a trigger for the "Planet Switch" entity: it will be hidden at first, and will be shown only after checking the distance with the player.
Here is the tree:

As you can in the graph there are 2 AI_Trigger nodes which trigger specific event trees.

PlanetSwitchOn Event:


PlanetSwitchOff Event:


And here is the result in-game when putting all of this together:



Now that I have the core architecture for the gameplay, "only" left to do is actually implementing bunch of nodes and writing all of the behavior trees for everything that player will encounter during the game!