In last week’s sketches, you saw two different ways to implement simple suspicious behaviors for an action game. This Sunday you’ll see how well these two systems handle an additional attack behavior.
What’s the Challenge?
“Implement the attack behavior of an immobile soldier. When an enemy contact is in sight, the soldier should load his weapon, and fire at will until the target is lost.”
The biggest difficulty in this example is to make sure that the attack behaviors override all the other behaviors. The soldier shouldn’t act suspicious if there’s a target visible!
The Finite State Machine
The cycle of attack behaviors is very similar to the suspicious cycle. However, one has priority over the other, so there needs to be transitions from all of the suspicious states into the new attack cycle.
To do this, it makes sense to use a hierarchical FSM. The HFSM allows generalized transitions to apply to composite states, so by regrouping all the suspicious states together, only one transition is required rather than many specialized ones.
The generalized transition into the SHOUT & AIM state is only engaged when an enemy is visible. Once the soldier has aimed, the FIRE AT WILL state takes over and continues until the target is no longer valid. Then a default transition takes the FSM into the LOWER WEAPON state, which leads back into the composite state. The default IDLE state in the composite is enabled again.
The Behavior Tree
Using the behavior tree (BT) approach, another sequence is created for the ATTACK behavior (similarly to the INVESTIGATE). This sequence can only execute when a target is visible.
To enable this new sequence, it needs to be plugged into the root of the behavior tree.
The root behavior is the same selector as used in last week’s example. It dynamically selects the highest priority child behavior that is able to execute. So the top priority is ATTACK, with INVESTIGATE and IDLE as fallback options.
Verdict: You Decide!
With the HFSM, priorities in the behaviors are implemented using more transitions that must be connected up manually. Using composite states with generalized transitions reduces the amount of work necessary to wire up individual states, while still allowing complete control over the logic. The disadvantage of this approach is that the “hierarchy” and structure of the graph do not reflect priorities very explicitly.
Using the BT, there are no new concepts required to implement the attack behaviors compared to the suspicious behaviors. It’s just another sequence that gets plugged into the priority selector. The tree reveals the overall priorities rather intuitively, and the hierarchy of behaviors is more obvious: the sequences are mid-level and the selector is at the top level.
So which technique do you think won this faceoff? Discuss.