Thursday 28 June 2012

A.I Finite State Machine


I have a great book Programming Game AI By Example by Mat Buckland. The first chapter was an eye opener for me as it is a Maths and Physics Primer which is just what I needed, I have not used either of those parts of my Brain since 1988 and they are a bit whithered :P

Anyway I digress. The next chapter is on Finite State Machines (FSM), I have a little experience of these having played with writing very simple CA's (Cellular Automata) way back when.

So I thought, "I have not seen any examples for A.I in XNA yet, wouldn't it be nice, using the examples in this book to come up with a FSM A.I for XNA" So that is what I have tried to do here in this post.

The basic model I put forward here IMHO is quite extensible, you have two base class's and an interface, the BaseAgent, BaseState and IBaseAgent. These objects are the framework of the FSM, to create a new Agent just inherit from the BaseAgent and create an agent interface to go with you new Agent class off the back of IBaseAgent and create your new states inheriting from the BaseState for each, then have your None Player Character or Bot (NPC) inherit from the new interface and write your AI logic for your NPC in the interface stubs.

In this example project I have just implemented the base states:

Patrol
Fight
Flee
isSafe
runAway

I guess these are the most basic items an NPC will need, you can always add other states like Seek and Chase or anything else you may need your NPC to do.

The NPC Patrols...
 

The NPC Fights!
The NPC Flees!

Again, this is a simple example, but I hope it gives you a base to work from and develop your own FSM's and as ever, if you think I am going about it wrong, leave a comment, it's the only way I can improve my examples.

Controls
AWSD - Translate Camera
Mouse - Rotate camera.
Move to the plane while it is patrolling in a circle, it will engage you and once it has lost enough hits will flee (don't worry, it loses hits just by being close to you)

GenericXNAExampleAIFSM.zip

2 comments:

  1. Hello Charles, thank you for uploading the links again. I am currently working on AI and was wondering if you could upload this sample again as well for reference. Thank you! I greatly appreciate your help.

    ReplyDelete
    Replies
    1. Yep, will do, again, will be an hour or so before the link is fixed :)

      Delete