Rabid Lion Games


I’ve been working on the structure for Alta’s AI system in the last few days. I recently read a blog post by a fellow Xna dev on his issues with structuring AI code (link to follow), and I thought I’d share my approach.

I’ve created 3 main engine classes to manage AI, 2 of which are base classes designed to be extended in game code.

The two base classes are the AEAIState class and the AEAIBehaviour class. The AEAIState class represents the finite states that a AI can be in, and it has responsibilty for determining whether an AI should change state or not based on the information available to it (flags and variables in the AECharacter class it is owned by).

The AEAIBehaviour class represents a set of responses or actions, to be associated with one or more AEAIStates.

Each of these classes need to be extended by game code to add the logic for changing state (in AEAIState’s think() method) and to specify actions (in AEAIBehaviour’s act() method).

The third of these classes is the AEIntelligence class. Each AECharacter can have at most one AEIntelligence class associated with it. Each instance of this class holds a list of available states for this AI and a dictionary that associates a behaviour with each state.

The instance also keeps track of the currentState and associated behaviour. Each frame the AECharacter class calls the current syate’s think() method to see if the state should change, and then the act() method on the behaviour associated with the current state.

For code organisation, I keep all states associated with a given AI in a single code file, and similarly with behaviours.

This structure should provide a good alternative for the usual massive switch statement without spreading out AI code into hundreds of files. It should also allow for plenty of code reuse, as parent AI states and behaviours with common code could be created in game code, with more specific versions inheriting from then.

I’ll be sure to make a note of how this approach works out for me, but thought I’d share it in case it gave anyone else any good ideas

Leave a Reply

6 × = six

Proudly powered by WordPress. Theme developed with WordPress Theme Generator.
Copyright © Rabid Lion Games. All rights reserved.