Behavior of occupants in Blood & Bullet
There's quite a lot of graphics in this one. Feel free to skip ahead for all the animations.
Since I moved over to Unity I've implemented the artificial intelligence (AI — the code that runs the computer controlled characters) in Blood & Bullet using behavior trees. I feel they are a good match in that they do what I need and they are easy enough to build and maintain. Behavior trees are made up by actions and conditions that can be organized into sequences. When linked together they form a tree structure, and that's where the name comes from I suppose.
This is a rough schematic of the behavior tree that an operator in B&B uses to open a door:
Most recently I've been working on the occupant AI, that is the AI for the opponent characters that reside in the building. Specifically I have worked on its "avoidance mode". An occupant will not simply flee when it is in this mode; it will act in different ways depending on some values stored on its character. I have started simple, so for the time being there are only three such values. Let's call them the behavior values:
Aggression - How likely the character is to resort to violence.
Avoidance - How likely the character is to try to avoid confrontation.
Morale - Determines for how long a character needs to think before taking action. It also decides how likely the character is to surrender.
In the sandbox the behavior values are randomized for each character. The player can set the lower and upper bounds for the randomization before starting a mission.
The sliders may look a bit confusing now. There are two for each value: one for the lower and one for the upper bound. They will be clearer before I release anything.
The avoidance behavior serves to keep the occupant away from the entry team operators and preferably out of sight. How far it is willing to go to acheive this goal depends on the character's aggression value. When the occupant is forced into action, the aggression value is used to decide what the occupant will do. It is a choice between aggressive and non-aggressive action. For now the morale value is not part of this but I will make it so that the occupant can also decide to surrender in some of these situations.
Below I will go through some of the situations that force the occupants into action and what the possible outcomes are.
When the occupant sees the operators, first of all it does not attack on sight when it runs the avoidance mode. It will try to put distance between itself and the operators. It doesn't want to turn its back on them when they are close though, so it starts backing away.
Backing through doors
When the occupant goes through an open door while backing away, it will attempt to close the door. When the door is closed it breaks the line of sight and the occupant feels confident enough to turn around and run.
If on the other hand, the occupant backs into a closed door or any other obstacle it will feel cornered. It will act to get out of this situation. In the animation below, on the top the occupant has a low aggression value and chooses to run toward and then past the operator. This may in turn prompt computer controlled operators into action. The occupant in the animation on the bottom has a high aggression value and chooses to attack.
The occupant has a rather similar reaction to that above when an operator advances quickly. Here the operator is first advancing on the occupant by walking speed, and the occupant can maintain the distance while going backwards. But then the operator starts sprinting. The occupant with a low aggression value, again on the top, turns around to run. The one with high aggression decides to engage.
A low aggression occupant will try to run away when being shot. If backed against a wall when this happens it will attempt to dash past the operator. A high aggression occupant fires back. The same reaction is also triggered when a nearby ally to the occupant is attacked.
Turning your back
For the same reason the occupants don't want to turn their back toward you if they can avoid it, you shouldn't turn on them either. They will take advantage and either run away or shoot you in the back. Again the aggression value of the occupant governs the outcome.
Finally they will run away when confronted with live grenades. This is regardless their behavior values.
That's it for this time, thank you for reading. I always appreciate a comment!