   Basic structure of an AI system
  I'll post this here instead of in the AI forum since it's not really a quesion about AI as such, more about how to structure the basic framework that lets you write an AI in the first place. I'd appreciate some pointers on my first draft.So I have a Creature class. All the creatures that currently exist will be stored together and treated alike, every frame the Creatures "InjectTimePulse" method will be called which passes in the time since the last frame.Each creature stores a component called "Logicable", (to go with "Drawable" and "Physicable", probably not great names). One the roles of Logicable is store what I think of as the most primitive state information, this is the most basic information about what the creature should be doing right now, this will probably be represented as an enumeration over the set of basic verbs, so walking to a target location, attacking a target creature, casting a spell etc.  This information is queried by the InjecTimePulse method which updates the creature in the correct way, e.g updates it's attack cycle, moves it in the right direction or whatever.This primitive state will for some creatures just be set by player input. For AI controlled creatures the Logicable class will need a set of event handling methods, for example:OnHeartBeat - called every few secondsOnPercieve - called when a new object enters LOSOnDamaged OnSpellCastAtEventually I might want these to be run in a script of some kind but I don't want to think about that right now. Now the idea is that I'll also have an AIEventManager class. Various subsystems will need a pointer to this class. In the right conditions they put an event into some sort of queue. The evnt will need to have a pointer or handle to the relevant Logicable, and an event identifier. (Either an enum or a string, or a struct which could also store some arguments.) Every frame the AIEventManager will get as far through the queue as it can. I'm not quite sure how to judge this, to start with I might just make it clear the whole thing every frame, I can imagine if you get lots of requests that need LOS or path finding at once the you might want to distribute them.So if I make a simple goal to start with that I want my creature to react to being damaged by an offscreen projectile by moving towards the attacker and retaliating if it finds it. The projectile system will call the Creatures Damage() function, the damage function will post the OnDamaged event to the queue with arguments describing the direction and type. The OnDamaged gets called by the manager and sets the creatures basic state to move to new target location in the direction of the projectile. Then I have other system (not thought about this yet) which tracks the LOS of each creature and registers that the creature has seen someone new. This posts the OnPercieve event with infomation about what he saw. The OnPerceive event then sets the state to attack the target if it's hostile.   
  Sounds like you are interested in a state machine for your actors.  This article over in the gamedev.net wiki talks about using state machines for AI.If the wiki is slow (as it often is), use the Google cache.   ;  Sounds like you are on the right track. - As frob mentioned, you'll need an enum for AIState. For example "idle", "attacking", "fleeing", "wandering", etc. - In your projectile class, it might be convenient to to have a reference to the "owner" of the projectile that shot it. Thus when you call "TakeDamage" on an AI, they will know who shot them and can react accordingly.- Your AI might need a "CurrentTarget" variable, which is a reference to whatever they are currently attacking. If they are idle it will be null. - If you have a lot of units (say in an RTS), have you thought about how to optimize your AI decisions? Like Collision detection, AI is an N^2 problem. Preferably, your AI decisions should use the same spatial partitioning method as your collision detection, so every AI decision only considers a subset of all units in the game (those nearby).   ;  Thanks for the points.Quote:Original post by EJH- If you have a lot of units (say in an RTS), have you thought about how to optimize your AI decisions? Like Collision detection, AI is an N^2 problem. Preferably, your AI decisions should use the same spatial partitioning method as your collision detection, so every AI decision only considers a subset of all units in the game (those nearby).I'm not sure about this. The project I'm working on is an RPG type game so I don't think the number involved wuold be that great, also I suspect the problems might be easier since there's little in the way of high level coordination involved. (Although perhaps more difficult in that there's more in the way of scripted actions.) Although I am interested in RTS AI as well.I had planned to create some kind of LOS subsystem that will be plugged into the same spatial partioning thing the collision detection will use. I'm not quite sure how it will work, it'll need to be able to send OnPercieve messages and also allow all AI decisions to be able to easily iterate over all the creatures they can see. (You'd possibly need some sort of memory of recently seen objects and their last know position and the direction they were moving.)   ;  Your system seems to be based on Neverwinter Nights' AI. You could do far worse than imitating them. Two things I think you need to give some detailed thought are how to get and where (and how to store) information about the creature's environment and how exactly the creature's action queue will work (WalkToTarget X is fine for high granularity but at some point you'll have to decompose it into a set of small tasks that get the job done).From the interesting ideas bin: NWN coordinated AI by using many hidden "chat" messages to communicate among the AI actors. For instance, an attacked creature would shout "I_WAS_ATTACKED" on the local channel, and any creature within range of the shout (shout already had a distance check system so the only ones to hear it would be creatures in range) could formulate their course of action.   