Saturday 3 November 2007

Unangband monster AI - part four (Formation)

You may want to read either part three or from the start of this article.

At this point, I recommend you download Unangband and check out the monster AI behaviour. The easiest way to do this is as follows:

  1. Start a new game. Create a character - pick a man of Bree for your race for the moment (Maia is fine if you've chosen that option). Class doesn't matter, but warriors have more hit points.
  2. Go into debug mode. Command is Ctrl-a. Confirm you want to continue.
  3. Jump to the Battle of Five Armies. This is Ctrl-a, j, dungeon is 26, level 0.
  4. You should find yourself on a big open plan with a mix of allies around you.
  5. Use either the numpad or roguelike keys or mouse to move around. Watch as your allies mow down the enemy, which will usually be orcs (o) or ogres (O).
  6. Steer clear of orc priests - they're light blue o's and will target you directly with wound spells. Alternately, Ctrl-a, e, and give yourself 1000 experience or so.
  7. If you have problems finding enemies, Ctrl-a, D, will detect every monster on the level. You can use the 'L'ocate or 'W'here command to scroll around the full level map.
  8. Ctrl-a, j, dungeon 26, level 0 will generate up new troops for you when you run out (actually a whole new level).
  9. ENTER or ? for help.
Now, depending on the mix of troops you start with you'll see one of two behaviours:
  1. Warriors will charge up and engage the enemy. They'll form a front when they engage, that will spread out width wise until pretty much all of them are fighting.
  2. Archers will charge up until they're approximately 6-8 grids away. They'll then start shooting. They'll also form a front, using the MFLAG_PUSH behaviour that I described in part three.
This is usually fun, and would be a great mini-game in its own right. Implementing allies was relatively straight-forward under the 4GAI. I had already modified monster combat and spell casting so that monsters could target and damage each other - and all I added was a target selection routine for allied monsters (and monsters that had been attacked by your allies).

The target selection routine is broken down into two parts: ranged attacks, which just involves picking any target within the monster's line of sight and using a spell against it, and movement, which involves picking the closest enemy monster and moving to the grid it occupies. These are not especially complicated, although somewhat inefficient.

The differing behaviour between warriors and archers comes from the best_range computation. Archers tend to have a best_range of 6, warriors of 1. In the target selection routine for movement, if the closest target picked is closer than the archer's best range, the archer forgets about it.

This is an unusual decision, but involves a complication that I haven't elaborated on. Your allies use your current target, if they are unable to find a target of their own. So when an archer forgets about their current target, they instead by default moves towards your current position, or whichever position you have targetted. The in-game tips, which you probably skipped over, outline the options you have:

=== Commanding your troops ===

Most honoured general,

I have hand-picked a select group of your finest warriors from
your homeland with which to fight alongside you in this, your
hour of need. If you find you are in a bad position or the
troops inadequately provided for when you first arrive at the
battle, you can depart, and I will endeavour to supply you with
better forces.

In general, the troops you command will follow you and fight
intelligently when you are nearby. Archers and spell casters
will stand off of targets and bombard them with spells and
arrows. However, archers and spell casters will take a guide
from your current position, and move towards that position if
they are too close to the target. This may mean they foolishly
follow you into battle. Target a grid to hold, as I outline
below, in order to avoid this.

Your troops will take also take lead from targets that you
specify, as follows:

If you specify a monster target, your troops will move towards
that monster if they are not engaged in direct combat. Archers
and spell-casters will keep a distance from the target.

If you specify a race of monsters, using the 's' key while
targetting, your troops will preferentially target that race
against all others, ignoring other targets and direct combat
if they can see one of the race you specify.

If you specify a grid target, your troops will move towards
that position and hold it if they are not engaged in direct
combat. Archers and spell-casters will also move up to the
position: you can use this to specify your archers and spell
casters stay back while you engage in melee with the enemy.

You can also specify a grid position with the 'a' key, whilst
targetting. This will cause your troops to assault the target
grid. Troops in an assault will disregard nearby combat in
favour of attacking monsters near the grid that you are
assaulting. Archers and spell casters will also seek out
targets near the grid that you are assaulting, but will
prefer to move back towards your position if they are too
close to the target in question.

Once you have specified a grid target, either to hold or
assault it, your troops will continue to prefer that
location. To cancel the orders, you must either specify
another target, or hit the Escape key whilst targetting. At
that point they will begin to follow you again.

As always,

This enables me for the most part, to avoid having to add any specific commands for ordering a player's allies around. The assault and hold commands are there to allow the player to elabourate on what they mean by pointing at a grid (attack it vs. run for it).

In part five, I'll discuss the commands I will have to add (you may have guessed, if you've played with the AI enough), as well as the problems I had when I chose 'Werewolf' as the player's race.

No comments: