Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's think for a moment a game where you'll have around 1k npcs, each one has to take its own decisions. Should each one has an object that decides what to do, or maybe exist one to process every unit?

Also, let's think about a complex ai, not just check if npc can see the player nor decides if npc will attack or not. Something like Dwarf Fortress does.

share|improve this question
This depends entirely on what you want the AI to do. AI is hugely game-specific. – Sean Middleditch Apr 7 '13 at 19:47

It depends on whether you want your NPCs to act as a collective or to act as individuals.

In the game I am developing at the moment I decided to control all AI objects are controlled by one central AI Controller. This has the advantage that it's much easier to implement cooperative behavior between the AI agents. When the AI controller is responsible for all agents and not just for one, it can make much better strategic decisions.

An example is when there are multiple areas which require protection by the AI agents. When one area has less defenders than the other, the AI controller can pick specific agents and send them to defend it. But when each agent makes its own decisions, it is hard to ensure that not all agents decide to relocate to the new weakpoint simultaneously or none at all decide to do so.

On the other hand, you might want to have an AI which is less challenging through good organization and behaves more realistically like individuals. Individuals can't communicate and coordinate in real-time with all others. They don't have access to all information available to their faction, and exchange information by communicating. They make their own decisions which might be suboptimal due to not having all relevant information.A stealth-based game, for example, relies on the mechanic that all the guards don't immediately become aware that one of their comrades just got his throat slit by the player. That might be a weakness of the AI from the strategical point of view, but it makes the antagonists behavior much more realistic which can result in a more statisfying experience. In such a game each agent should have its own AI object.

share|improve this answer

First, what is DF? I can name a handful of games that might be called DF =)

So many ways to organize, let's call the objects Soldiers so this reads better.

1) One AI that maintains all states internally, when a new Soldier is created it asks the AI to create a state setup for it; the soldier is controlled by the AI and matches its animations and all that to what the AI says that it's doing right now. Pro: AI has full control, data is localized, tweaking is centralized, soldier is basically just a playback mechanism; Con: custom types pollute the generic handlers.

2) One AI and the soldiers carry around their own state as their class. Pro: less maintenance work for the main AI, soldiers can customize their state changes locally, Con: AI has to be more complex to handle unexpected inputs from soldiers, behavior changes have to be made through all soldier classes that work with state.

Pro to both of the above: coordinated soldier activity is easy.

3) Localized AI for each soldier, probably descended from an AI or includes one. Pro: easy to customize behavior without having to touch both local and AI code, Con: soldiers working together will be difficult/complex to make happen, making global changes to how AI works will involve many classes.

Assuming that you really, truly need each and every unit to be independent and act on its own like the special snowflake that it is, then probably #3 is a better option. I suspect, however, that what you really want is for each unit to act on local state but still under global guidelines, in which case #1 or #2 might work better.

I personally prefer #1, but thats' because I like to build simulations where unit "type" determines activity and it makes the most sense and is easier to maintain.

share|improve this answer
I like the first option the best as well. Lends itself well to entity-component-systems. – jmegaffin Apr 8 '13 at 4:08

For many objets which share some common datastructures the flyweight pattern is ideal.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.