I'm writing a text-mode (python + ncurses), turn-based rpg based loosely on 3rd edition D&D and Neverwinter Nights (the PC game) rules. It's not going to be a full implementation, but 'in the spirit' of them. I already have quite a bit of the basics in place; movement, a few base classes and races, rule-based encounters (shop/npc/monster X shows up when condition Y is true), dynamic generation of monsters, player and enemy stats and random, levelled equipment etc. It's enough to start moving around a basic world, talking to people, examining objects and location and now I'm moving on to (melee) combat.

I'm interested in what people would expect of an algorithm that co-ordinates the computer-controlled attacks of an enemy party against the player. How smart should the computer controlled enemy be (in my implementation the player will take control of each of his own characters in turn, and they wont be AI controlled, but the enemy party will - attacking, spell casting etc at-will).

For example; if an enemy character initially chooses a random pc from the players party on round 1, should that enemy continue to attack the same target in subsequent rounds. How about if another pc then attacks that enemy? What if the enemy is unable to cause damage to that pc (ie say the pc has something crazy like damage reduction 100/+5 - should the programme know in advance?), should they switch to another in the players party?

Has anyone implemented anything similar and would be able to provide any pointers?

  • \$\begingroup\$ I'm confused about what you're asking. Do you want to know how to implement these things? or do want to know what to implement? \$\endgroup\$
    – ThatOneGuy
    Commented Jul 10, 2013 at 21:48
  • \$\begingroup\$ @user1895420 - it's really asking what complexity people expect when they're playing such a game, would you expect the computer to use such tactics against you, or is it even necessary, if an enemy just continues hammering away at your burly Knight in +5 plate armour with no chance of success do you consider it 'broken'? What factors should be taken in to account by the enemy attack routines in this situation? \$\endgroup\$ Commented Jul 10, 2013 at 21:54
  • 1
    \$\begingroup\$ So, you're kind of polling for opinions about what people expect? This isn't really the site for poll type questions. I'm not sure there's a correct answer to this question. It's like to be "Primarily opinion based". You should make the AI to your standards and no less. You're the authority here, it's your game. \$\endgroup\$
    – House
    Commented Jul 10, 2013 at 22:01
  • 1
    \$\begingroup\$ I'm not sure there's a good answer to that. An inexperienced player might not even realize armor does anything. While a very experienced player might notice much more eleborate 'broken' behaviors. EDIT: seems Byte56 beat me to it. \$\endgroup\$
    – ThatOneGuy
    Commented Jul 10, 2013 at 22:01
  • 4
    \$\begingroup\$ Depends on the game. If the draw of the game is tactical combat, I expect the AI to be good at tactics. If the draw of the game is story, then combat AI can be dumber. You need more of a game design than "like D&D." \$\endgroup\$ Commented Jul 10, 2013 at 22:06

3 Answers 3


So what I've decided to do is a combination of the suggestions here and from Strategies for monster targeting AI in a turn-based combat system.

What I will do is have each enemy character scan the available player characters and calculate three scores:

Target Threat Rating

  • Difference in level between the target and the enemy character
  • Melee targets get extra points for two handed or dual-wielded weapons, weapon level and STR bonuses
  • Spellcasters get extra points for their highest spell level and INT bonuses
  • Bonus points for the number of extra attacks per round
  • Bonus points for every N levels of armour class
  • Bonus points for every N% of HP remaining

Target Value

  • Points scored for being a party leader
  • Points scored for every N% of HP lost
  • Points scored if this character is actively providing buffs to the party

Target Difficulty

  • This will represent how potentially difficult the target will be to damage from either melee, magic, ranged or touch attacks.

In addition, I plan on using the INT/WIS rating of an enemy creature to decide if it can take advantage of the three ratings; creatures with lower intelligence may only use target value, for example, whereas more intelligence will bring target threat and target difficulty in to the mix. Therefore more intelligent creatures should be able to decide if a more threatening player with a high value is worth attacking (and which attack type to use).

Changes in the ratings between rounds may affect which character the enemy attacks - though some element of 'stickyness' will be used to prevent flip/flop between characters on every turn.


One thing to consider is how "smart" the unit is. You seem to be framing the question as if the AI is the dungeon master, and he is playing the NPCs verses the player's party. A good DM wouldn't always play an NPC's combat up to the ability of their character sheets.

For example a brutish orc might attack the first thing it sees even when that's not the best battle plan. All unit's in an opposing battle might not have line of sight on each other or reliable communication. In your example, an attacking fighter wouldn't know about the damage reduction effect (as he hasn't detected the magic auror granting the effect). A rule like, if my attacks aren't working, I should fall back seems reasonable.

Having DMed quiet a few games of DnD I can tell you that enemies don't need much AI to seem realistic and challenging. Even something as simple as compound actions like pulling back to reapply a buff seem much more complex to the player who tends to have his mind on what his battle plan is more then what the opponents could be.

  • 1
    \$\begingroup\$ Exactly. In the pencil-and-paper game, NPC choices are informed primarily by "How intelligent this guy is supposed to be" and "what this guy's training and habits are." A skilled yet dumb-as-an-ox swordsman probably has great reflexive reactions to common battlefield situations, but would get into trouble if in any situation he's unfamiliar with; Conversely, a bookish but brilliant wizard might have no idea how to react when ambushed - but you can bet he'll think up a strategy in a hurry. \$\endgroup\$
    – GMJoe
    Commented Jul 11, 2013 at 5:11
  • \$\begingroup\$ Interesting, thanks. Whether for good or bad, the 'monsters' in my game are composed of the same basic races and classes as the player party, so have access to the same statistics - so it should be possible to model something along this line of behaviour taking into account their INT/WIS. \$\endgroup\$ Commented Jul 11, 2013 at 7:38
  • \$\begingroup\$ Another interesting perspective in this previous question: gamedev.stackexchange.com/questions/12071/… with respect to assigning scores to the highest threat characters. I think I have some interesting ideas to work with, thanks! \$\endgroup\$ Commented Jul 11, 2013 at 16:35

Since it is a DnD game I would suggest going for a bit of randomness. I have implemented a turn based rpg battle system (1 player vs N monster and something the player has to defend) and the algorithm I thought of was the following:

1) Store different strategies in an array (For example focus on hero with physical attacks, focus on spell-caster, attack different targets, fall back, use special abilities)

2) Every N turns change the behavior of all units to one of the options in the array. That way you give the idea that they work as a team.

3) Every turn change the behavior of one of the enemies (that way they will look more "alive")

4)Change the behavior of all units before half of them have gone through point 4 so they don't look like they are acting on their own.

Hope it is useful to you.

Ps if you are going to implement difficulties, just add more elements of the array which have the worse choices for the player, for example add 3 times 'focus on the fragile part of the team' instead of one.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .