|
-
November 14th, 2007, 09:26 AM
#6
Re: AI for Game Programming
For an MMORPG, A.I. is quite simple when compared to other game genres (especially FPS and RTS.) I'd still go for an FSM, yes, but since an MMORPG server has to handle a great deal of actors at once, you should keep it simple. Looking at a standard enemy in a popular MMORPG such as World of Warcraft, the following states can be seen (with some very simple pseudo-code):
Code:
switch (actor.State) {
Idle/Patrolling - The actor is simply moving around in a defined area, or along a defined path.
Code:
case IDLE:
// Make sure the actor is always moving along its path when idle.
if (!actor.Moving)
{
actor.MoveTo(actor.PatrolPath.NextPosition());
}
// Look for an enemy in range.
// 'Zone' would be the area the actor is in, and 'Units' the units in that
// zone (which could be both players or other NPCs - if you want to go that
// far.) Consider this optimization because checking all units in the game
// would slow everything down.
foreach (Unit unit in actor.Zone.Units)
{
if (actor.StanceTowards(unit) == HOSTILE && actor.DistanceTo(unit) <= actor.AttackRange)
{
actor.Target = unit;
actor.State = ATTACKING;
break;
}
}
break;
Attacking - The player was within a certain range of the actor. You may want to perform some kind of line-of-sight check before changing into this state to avoid the actor attacking a player on the other side of a wall. In this state, the actor will move towards the player and once close enough to use its weapon, it will fire it.
Code:
case ATTACKING:
// Make the actor flee if it's too badly hurt.
if (actor.Health <= actor.FleeTreshold)
{
actor.State = FLEEING;
break;
}
// Make sure the actor doesn't chase its target forever. If it's too far from
// its original position - outside the so-called "leash"-range - make it reset.
if (actor.DistanceTo(actor.PatrolPath.CurrentPosition) >= actor.LeashRange)
{
actor.Target = null;
actor.Health = actor.MaxHealth;
actor.MoveTo(actor.PatrolPath.CurrentPosition);
actor.State = IDLE;
break;
}
// Check if the target of the actor is within weapons' range.
if (actor.DistanceTo(actor.Target) <= actor.WeaponsRange)
{
// Make the actor stop and use its weapon.
actor.Stop();
actor.UseWeapon();
}
else
{
// Move the actor closer to its target.
actor.MoveTo(actor.Target.Position);
}
break;
Fleeing - The actor has been severely hurt (its health is below a pre-defined treshold) and will try to run away from its target.
Code:
case FLEEING:
// If the actor has somehow regained health, return to attacking.
// Raising treshold by 10% prevents the actor switching back and forth
// between attacking and fleeing.
if (actor.Health > actor.FleeTreshold * 1.1)
{
actor.State = ATTACKING;
break;
}
// Perform a 180 degree turn from the target.
actor.TurnTo(actor.DirectionTo(actor.Target) + 180);
// Move in the current direction.
actor.Move();
break;
That should give you a general idea of the structure of an FSM. As you can see, you only ever have one state running, and switch to other states based on certain conditions. This makes the A.I. easy to develop because you only need to concentrate on what's happening in one state at a time (for example, once the actor is attacking the player you won't have to worry about it continuing patrolling and once the actor is fleeing, you won't have to worry about preventing it from running back to the player and attacking it, etc.)
Last edited by andreasblixt; November 14th, 2007 at 10:00 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|