CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2007
    Posts
    35

    AI for Game Programming

    I'm currently building a game engine and need a little help with the AI. I need to know where to began and find some help files about Artificial Intelligence. If anyone can give me a tutuorial or help me build one that will be fine? Mostly I'm still looking for a tutorial about game programming. I need a tutorial to explain to me what I need to do step by step by step and show me the source code and so on. Thank you to anyone who can help.

  2. #2
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: AI for Game Programming

    I'd go for a finite state machine (FSM) because they're easy to work with. Basically you give your actor an initial state and let it switch between states depending on what it senses. Say you've got an actor that's in the state "guarding" - it'll be staying put and looking around. If it it senses something - such as the player moving too much within range of the actor, it could switch to a "suspicious" state where it'll linger around its original position, possibly more sensitive to player movement than before.

    Here's an article on FSM:
    http://ai-depot.com/FiniteStateMachines/

  3. #3
    Join Date
    Feb 2007
    Posts
    35

    Re: AI for Game Programming

    Would this AI also become adaptive to players attacks and movements like the actor would switch attack patterns and stuff?

  4. #4
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: AI for Game Programming

    Generally FSM are predictable and not very suitable for adaptiveness. The reason I suggested it is because they are simple enough to understand and implement. If you're going for more complex, adaptive A.I. you'll be in for quite the challenge; fuzzy logic and neural networks are very complex concepts that challenge even the brightest of people.

    Generally, you'll want your A.I. to seem as intelligent as possible rather than being intelligent. Therefore you could make your A.I. not truly adapt to the player's attacks, but rather check what the player is doing and switching to one of several states (assuming you're using FSM.)

    For example, if the player shoots in the general direction of the actor (or simply in the vicinity of the actor), change the state to "evading" and go back to state "attacking" after x seconds. Your code for the "evading" state would involve more moving but it could still try to attack the player occasionally. The A.I. wouldn't understand that the player shoots bullets and that they do damage, but to the player it would seem that it does, because it's moving as if to avoid them.

  5. #5
    Join Date
    Feb 2007
    Posts
    35

    Re: AI for Game Programming

    Okay cool. Well this is an MMORPG type of game so would the AI be good for that type of game?

  6. #6
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    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;
    Code:
    }
    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.

  7. #7
    Join Date
    Feb 2007
    Posts
    35

    Re: AI for Game Programming

    What do you know about Character Creation? Allowing players to create there own characters with different variations of hair, body types, color so on and so forth?

  8. #8
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: AI for Game Programming

    That's not really a code-related question, in my opinion. It depends mostly on how your game is presented graphically.

    In general you split hair from body so that any hair style can be put on any body type. If it's 3D you'd have different textures for different hair/skin colors and different models for different hair styles/body types. If it's 2D you'd have different images for everything.

    Code-wise, you would just store some numbers that refer to the colors/types used.

  9. #9
    Join Date
    Feb 2007
    Posts
    35

    Re: AI for Game Programming

    Thanks you have been most helpful. So how about the game engine is that something that comes last because it stores everything before you run it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured