Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
## CHAPTER 12 Artificial Intelligence Techniques This chapter covers the following topics: • Antagonist movement techniques • Finite state machines • Probability machines • Path finding techniques There is no more dangerous opponent than a living, breathing human being. Humans can be incredibly unpredictable, and good game players can change tactics in a fraction of a nanosecond to take advantage of some new predicament within the game world. This is what makes multiplayer games so attractive; when facing real opponents, the replayability factor of just about any game is dramatically extended. However, you can’t always hook up with another human player. Some people may not even have the necessary hardware, and some may just want to play a game by themselves without worrying about the competition level introduced by battling other humans. In order for a single–player game to grab and hold the player’s interest, it needs to be challenging, as good graphics and sound will entertain a user for only so long. This is where artificial intelligence techniques come in. In essence, artificial intelligence is the process of simulating intelligent thought and behavior through algorithmic methods. As one would surmise from such a broad topic, a discussion of artificial intelligence could cover volumes upon volumes. Indeed, artificial intelligence is an area of specialty in the realm of computer science. Several large colleges and corporations spend millions of dollars (or more) yearly on furthering the research of artificial intelligence. Everything from simulating the thought process of an ant to accurately modeling the interaction of neurons in the human brain is being researched, with varying degrees of success. Of course, most of these scholarly experiments employ technology that is either beyond the reach of game programmers or is simply “too much” for a game application. What we’re interested in accomplishing is not an accurate model of living, thinking organisms, but a close facsimile thereof. We need game entities that appear to behave in an intelligent manner, but they do not have to truly be intelligent. As long as they challenge the player and provide a few surprises every now and then, we’ve done our job. In this chapter, we examine various techniques for implementing simplistic artificial intelligence that will give our game’s enemy units a sense of personality and behavior. Since we are concentrating on artificial intelligence programming, the examples in this chapter will use very simple graphics so we don’t unnecessarily complicate the code. ### Movement The most noticeable display of artificial intelligence is in the movement of enemy units. Indeed, in most action games, the user is going to be primarily concerned with the movement of enemy units, such as whether they are aggressively chasing the user’s ship or running like cowards. Let’s look at several techniques that can be used to breathe life into the movement of game world antagonists. #### Random Movement Perhaps the simplest simulation of artificial intelligence is to introduce an element of randomness. When an enemy unit moves in a fashion that is unpredictable to the player, that unit can appear to exhibit a rudimentary intelligence. Consider the apparently random flight path of a common housefly. It darts about here and there, yet it always appears to have a mind of its own and to know where it’s going. Random movement is simple to implement. For example, say we have a very basic sprite structure defined as:
    TSprite = record

      XPos, YPos: Integer;        // horizontal and vertical sprite position

    end;
To implement random movement, we can simply use the Object Pascal function Random to return a number indicating the number of units to move in a particular direction. The Random function takes a single integer argument as the extent of the range of numbers to be returned, and returns an integer in the range 0<=X<Argument. If we specify a range of 3, which would randomly return the numbers 0, 1, or 2, and then subtract 1, our modified range of returned numbers would be -1, 0, or 1. By adding this translated number to the horizontal and vertical positions of our sprite, it would appear to move about randomly. The following listing demonstrates this technique. Listing 12–1: Random movement
    type

       .

       .

       .

      TSprite = record

        XPos, YPos: Integer;

      end;

    var

      Enemy: TSprite;

    implementation

    procedure TfrmDXAppMain.FormActivate(Sender: TObject);

    begin

       .

       .

       .

      {initialize the enemy’s starting position}

      Enemy.XPos := 320;

      Enemy.YPos := 240;

       .

       .

       .

    end;

       .

       .

       .

    procedure TfrmDXAppMain.DrawSurfaces;

    var

      SrfcDc: HDC;

      TempCanvas: TCanvas;

    begin

      {erase the last frame of animation}

      ColorFill(FBackBuffer, clBlack, nil);

      {move the enemy randomly. this could be either no movement

       or movement by one pixel in a positive or negative direction}

      Enemy.XPos := Enemy.XPos + Random(3)–1;

      Enemy.YPos := Enemy.YPos + Random(3)–1;

      {check for collisions with the side of the screen}

      if Enemy.XPos < 0 then

        Enemy.XPos := 0;

      if Enemy.XPos > DXWIDTH – 1 then

        Enemy.XPos := DXWIDTH – 1;

      if Enemy.YPos < 0 then

        Enemy.YPos := 0;

      if Enemy.YPos > DXHEIGHT – 1 then

        Enemy.YPos := DXHEIGHT – 1;

      {retrieve a device context for use with GDI output techniques}

      FBackBuffer.GetDC(SrfcDc);

      TempCanvas := TCanvas.Create;

      try

        {initialize the canvas properties}

        TempCanvas.Handle := SrfcDc;

        TempCanvas.Font.Color := clWhite;

        TempCanvas.Pen.Style := psClear;

        TempCanvas.Brush.Style := bsSolid;

        TempCanvas.Brush.Color := clRed;

        {draw a small red circle at the current sprite position}

        TempCanvas.Ellipse(Enemy.XPos, Enemy.YPos, Enemy.XPos + 3, Enemy.YPos + 3);

        {draw some text}

        TempCanvas.Brush.Style := bsClear;

        TempCanvas.TextOut(0, 0, ‘Random Movement’)

      finally

        {clean up the temporary canvas and device context}

        TempCanvas.Handle := 0;

        FBackBuffer.ReleaseDC(SrfcDc);

        TempCanvas.Free;

      end;

    end;
This does indeed move our sprite around the screen randomly, but it merely jitters around the middle of the screen and doesn’t really project an air of intelligence at all. We could improve this by waiting a random amount of time before changing directions, which would give us a more housefly–like behavior, but there is no real intelligence underneath, and this enemy would pose no true threat to the player. #### Aggression/Evasion In order to portray a sense of intelligence, enemy units should react in a manner dictated somewhat by the actions of the player. This could be anything from the player’s movements (or lack thereof) to the number of rockets or bullets the player launched to the status of a player’s shields or hit points. When an enemy reacts to the player’s actions or current status, its intelligence can be convincingly real. This can also give an enemy a sense of personality. An enemy that actively hunts down the player can be perceived as confident, powerful, and aggressive. By contrast, an enemy that constantly eludes the player or turns tail can be perceived as cowardly, injured, or perhaps even cunning, like an assassin stalking its target.
| Tip: Varying the velocity, perhaps according to the proximity of the player, will enhance this technique and improve the perceived intelligence.
Previous | Table of Contents | Next

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.