Controlling AI with triggers

Chapter 5, Artificial Intelligence, deals with several methods to control AI in games. As we learned in that chapter, control and predictability are very important. Even if we have the smartest AI in the world, as programmers, we want to be able to know that the AI will perform a certain action at a certain time. This is where triggers can be extremely useful. In fact, with a good trigger system there might not be need for much AI at all.

One example of trigger usage might be a warehouse where guards are in a patrolling state. Once the player reaches a certain area (maybe where they should not go), an alarm is triggered. At this point, we also want the guards to switch to a more aggressive state.

Getting ready

This recipe will link the trigger system we created previously in the chapter with the StateMachine-based AIControl class from the Decision making – Finite State Machine recipe in Chapter 5, Artificial Intelligence. Even if you haven't followed the recipes in Chapter 5, Artificial Intelligence, but have a different class controlling AI, it should be quite easy to adapt this recipe to accommodate that class.

How to do it...

As with the previous examples, we begin by creating a new class that extends the ScriptObject interface. We can call it AIScriptControl.

  1. It needs to have an AIControl field called aiControl and a Class<AIState> field called targetState.
  2. It might also have a Spatial called target.
  3. Finally, we add a Boolean called enabled.
  4. In its trigger method, we should call onTrigger if enabled is true.
  5. In the onTrigger method, we apply targetState to aiControl:
    aiControl.setState(targetState);
  6. If target is not null, we call aiControl.setTarget(target).
  7. The StateMachine for the AIControl we created was a closed system and it didn't need any external input to change the states. Now, we need to be able to trigger it externally so let's add a setter method in the AIControl. Create a new method called setState, which takes a Class<AIState> called state as an input parameter.
  8. Inside, we check whether spatial has the supplied state, and enable it if possible:
    if(spatial.getControl(state) != null){
    spatial.getControl(state).setEnabled(true);
    }

How it works...

This recipe follows the pattern we established in the Creating a trigger system recipe. In the onTrigger method, we apply targetState, which will change the behavior and actions of the AI. For example, it can change from PatrolState to AttackState. We only supply the class type and not a whole instance of the class since the AI should already have the state and it might be configured already. In this way, we tell the AI to simply change the state if it is available. We also have a target field, in case the new state requires that.

There's more...

It doesn't have to end with that. We can, for example, with some modification trigger the AI to start walking a path, turn in a certain direction, or take cover and other things. This functionality can either be built into this class or be made as separate classes.

To explore an example in detail, let's have a look at what will be needed to have the AI move to a specific location once AIScriptControl is triggered.

  1. We will need an AIState, which handles moving to a set location. Chapter 5, Artificial Intellegence, explains this. The SeekCoverState can easily be modified to only have a target field rather than a list to choose from.
  2. We will need something to function as a waypoint or target. Again, the CoverPoint control from the same recipe can function as a waypoint too. It can also be extended so that using cover at WayPoint is an option within the class.
  3. Finally, we will need to pass WayPoint to the state. Since we're not supplying a whole class, we can't set it in AIState itself. One way would be to pass it through the setTarget method of AIControl.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.167.183