Internship - Week 9 - Cockroach AI
Hey there,
Lets start working on the first creature. The first creature we will be creating is a simple cockroach. This cockroach is going to be able to walk, sleep, dig and poop. To implement the AI we will be using the Behave plug-in for unity created by Emil Johansen A.K.A "AngryAnt".
Behave is an easy to use plug-in that helps you design, integrate and run efficient AI behavior trees. It has a very nice node based editor that lets you drag, drop and connect nodes.
Behavior trees use different types of nodes to traverse the tree or call behaviors. There are Selector, Sequence, Repeater, Priority Selector, Parallel, Interrupter and Action nodes. I will explain what the nodes do that I used in Image 1.
Action: actions nodes are basically functions calls. They hold the name of the function that you want to be called when that node is ticked. For example when the dig node is ticked, the dig functions in my code will be called. Actions are the root nodes of a tree and cannot be connected to other action nodes. An action node return one of tree types: failure, success or running.
Selector and Sequence: these are the main nodes you use to traverse the tree. Their names are quite self explanatory. The Selector starts at the first child node, if this node return a failure, it will move on to the next node. When a child node returns success, It will return and stop going through it's child nodes. The sequence does exactly the opposite. It continues when a child node return success. When a failure is returned by a child node, it will stop and return.
Interrupter: this is basically a if statement. It calls an actions node, When this action node returns failure, it stops. When this actions node returns success, it continues.
When you're done creating your behavior tree, you can compile the behave library. when the library is done compiling you can start writing the code for each action node. The behave library contains functions that will be called when the behavior tree is running. All you need to do is override these functions to create your behaviors.
The only thing you need to do besides overriding these functions is ticking the tree. This means updating the tree. The tree will do the rest and call the necessary actions nodes.
But that is enough about Behave, if you want more information about it, go here: http://angryant.com/behave/
Now lets talk about the cockroach. He will be dumb and lack the ability to find a path, he will just look for a direction he can walk in and take some steps in that direction. For this we will need to check the creatures neighboring tiles, to see in which direction the creature can walk. We also need a check that makes sure we can only walk when the next tile in that direction is a floor tile.
The cockroach can dig when he is next to a wall. We will have to check each neighbor to see if there is a wall tile. There are some other checks we'll need but I'll explain those in a bit.
He can poop as well which is basically spawning an event. This and digging both require a timer function. We don't want them to constantly be digging or pooping.
Walking and digging also require a stamina variable, it will deplete when the cockroach performs one of these behaviors. Now it is also required to have a sleep behavior or another means of recovering stamina.
now if we implement all of this we get the final behavior tree:
And now the working AI:
(Using place holder sprites.)