Tricks of the Windows Game Programming Gurus:Making Silicon Think with Artificial Intelligence

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

Moreover, you might create events that could be tested with a text string parameter:

    if (EVENT("player dead")

        BEGIN

        // code here

        ENDIF

All this magic is accomplished by using clever global state variables and making sure to expose enough generic events to the scripts, along with system state variables (via functions) and a lot of text substitution via the preprocessor. Leaving out some of those details to keep things simple, let’s take a look at what you need for text substitutions thus far. Referring to Figure 12.29, each script that is compiled will be processed first through the C/C++ preprocessor.

Figure 12.29 Using the C/C++ preprocessor for a script language interpreter.

This is where you’re going to make all those text substitutions and convert your little script language back to C/C++. To make it work, you tell the scripter to save all the files with the extension script `.SCR` or something, and when the file is imported into your main C/C++ file for compilation, you make sure to first include the script translation header. Here’s the script translator for what you’ve so far:

    SCRIPTTRANS.H

    // variable translations

    #define REAL static float

    #define INTEGER static int

    // comparisons

    #define EQUALS   ==

    #define NOTEQUAL !=

    // block starts and ends

    #define BEGIN {

    #define END   }

    #define BEGINMAIN {

    #define ENDIF   }

    #define ENDWHILE }

    #define ENDELSE  }

    #define ENDMAIN }

    // looping

    #define GOTO goto

Then you include the following in your game code:

    #include "SCRIPTTRANS.H"

Then you include the actual script file somewhere in your game engine at the proper moment. You can do it at the beginning, or even in a function:

    Main_Game_Loop()

    {

    #include "script1.scr"

    // more code

    } // end main game loop

This part is up to you. The point is that the code the scripter writes must be compiled somehow, and it must be able to access the globals, see events, and make calls to the function set you expose. For example, here’s a crude script that fires an event (which I didn’t define at the count of 10):

    // the variables

    INTEGER index;

    index = 0;

    // the main section

    BEGINMAIN

    LBL_START:

    if (index EQUALS 10)

       BEGIN

       BLOWUP("self");

       ENDIF

    if (index < 10)

       BEGIN

       index = index + 1;

       GOTO LBL_START;

       ENDIF

    ENDMAIN

Obviously, you would have to define `BLOWUP()`, but you get the picture. This code would be translated by the preprocessor into the following:

    {

    static int index;

    index = 0;

    LBL_START:

    if (index == 10)

       {

       BLOWUP("self");

       }

    if (index < 10)

       {

       index = index + 1;

       goto LBL_START;

       }

    }

> Tip: You can tell the Visual C++ compiler to output the preprocessed C/C++ file with the compiler directive `/P`.

Cool, huh? Of course, I’m leaving out a lot of details, like problems with variable names colliding, accessing globals, debugging, making sure the script doesn’t sit in an endless loop, and so forth. However, I think that you have an idea about using the compiler as a building block of a scripting language.

### Artificial Neural Networks

Neural networks are one of those things that you keep hearing about but never seem to show up. Well, I can tell you for a fact that in the past 3-5 years, we’ve made leaps and bounds in the area of artificial neural networks. Not because there has been any major breakthrough, but because people are taking an interest in them, experimenting with them, and using them. In fact, there are a number of games that use extremely advanced neural networks: Creatures, Dogz, Fin Fin, and others.

A neural network is a model of the human brain. The brain consists of 10-100 billion brain cells. Each of these cells can both process and send information. Figure 12.30 is a model of a human brain cell or neuron.

Figure 12.30 A basic neuron.

There are three main parts to a neuron: the soma, the axon, and dendrites. The soma is the main cell body and performs the processing, while the axon transmits the signals to the dendrites, which then pass the signals to other neurons.

Each neuron has a fairly simple function: to process input and fire or not fire. Firing means sending an electrochemical signal. Neurons have a number of inputs, a single output (which may be distributed), and some rule for processing the inputs and generating an output. The rules for processing are extremely complex, but suffice it to say that a summation of signals occurs and the results of this summation cause the neuron to fire.

Well, that’s great, but how can you use this to make games appear to think? Well, instead of trying to create something as bold as thought or consciousness, maybe you can start by modeling simple memory, pattern recognition, and learning with some computer models. Our brains are very good at these tasks, while computers are very bad. It’s intriguing to explore the biological computers we have in our heads and see if we can take some ideas from them.

This is exactly what artificial neural networks (or simply neural networks) do. They’re simple models that can process information in parallel, just like our brains. Let’s take a look at the most basic kinds of artificial neurons or neurodes.

The first artificial neural networks were created in 1943 by McCulloch and Pitts, two electrical engineers who wanted to model electronic hardware after the human brain. They came up with what they called a neurode, as shown on the left side of Figure 12.31. Today the form of the neurode hasn’t changed much, as shown on the right side of Figure 12.31.

Figure 12.31 Basic artificial neurons.

A neurode consists of a number of inputs, X(i), that are scaled by weights, w(i), summed up, and then processed by an activation function. This activation function may be a simple threshold, as in the McCulloch-Pitts (MP) model, or a more complex step, linear, or exponential function. In the case of the MP model, the sum is compared to a threshold value. If the sum is greater than a threshold theta, the neurode fires. Otherwise it doesn’t. Mathematically, we have the following:

McCulloch-Pitts Neurode Summation Function

               n

    Output Y = Σ Xi * wi

              i=1

General Neurode with Bias

                     n

    Output Y = B*b + Σ Xi * wi

                    i=1

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

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 in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.