I am currently building a AI system for a game. I am familiar with State design pattern and implemented different states for each agent in my game.

Different states are like Running, Attack, Idle, Cover, Support etc.

In this game, I have around 10 agents which have mainly two behaviours - Defender and Attacker.

I made a class AIManager which have reference of all agents in the game. This class decides which agent will do what but I am facing issues in managing this class. There are many rules in the game like -

  1. There will be two teams.
  2. Each team will have 2 defender and 3 attackers.
  3. Attacker will attack attackers and defenders.
  4. Defender only attack to defenders.
  5. At a time only two agents can attack a single agent.
  6. If defender is in cover, it will not go to support state.
  7. If defender is attacking, it will not go to run state.
  8. If attacker is attacking, it can run away on specific condition. ... etc

To make these rules applicable, my AIManager class is totally mess, contains lots of if/else, need to check every possibilities, for loops. I am sure I am not doing right in implementing these game logic because when I need to change some rules or need to add another state, I need to go through all my game logic. This manager code is totally unstable.

I want to know how to tackle this situation, Is there any design pattern which I should follow or any suggestion would be of great help.

  • 2
    \$\begingroup\$ There are lots and lots of different ways to implement game AI. So I am afraid that this question is too broad. But one solution you might want to look into are behaviour trees. \$\endgroup\$ – Philipp Mar 8 '18 at 12:24

"Managers" are widely considered anti-patterns. Why don't agents themselves implement these rules? For starters, all the attacker-specific logic belongs in Attacker. All the Defender-specific code goes to its class. Common code goes to their common base class (Agent). The Team class is pretty trivial as it chiefly needs to create 3 attackers and 2 defenders.

You now see that the "Manager" class is rather pointless as it would only have to deal with creating 2 teams. That's approximately one line of code.

|improve this answer|||||
  • 1
    \$\begingroup\$ Managers can be useful in AI when it comes to coordinating multiple AI actors. For example, a manager can determine a number of goals with different priorities and then assign individual agents to goals depending on the goal priority and their fitness to achieve that goal. When you try to implement this on the agent-level you will have difficulties to determine if the current agent is really the best agent for a specific task which hasn't got anything better to do. \$\endgroup\$ – Philipp Mar 8 '18 at 15:08
  • 2
    \$\begingroup\$ @Philipp: That's moving the goalposts - coordination is a manager function, but not one of the nine listed in the question. Even so, that still does not mean the manager class should be running the entire agent logic. In fact, I'd expect there to be two instances, one per team. \$\endgroup\$ – MSalters Mar 8 '18 at 16:38
  • \$\begingroup\$ @MSalters: As you said, attacker-specific logic belongs in Attacker class. As agents have different states for e.g. Attack :- Now Attacker attack behaviour is different than of Defender, so should I make different states for Attacker and Defender? \$\endgroup\$ – LebRon Mar 12 '18 at 6:24
  • \$\begingroup\$ @MSalters: As you mentioned .. "but not one of the nine listed in the question...." If I consider point 5: "At a time only two agents can attack a single agent". Who will decide whether any agent should attack or not? \$\endgroup\$ – LebRon Mar 12 '18 at 6:29

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