The Artificial Intelligence Module

Here's what I need to accomplish in this section:   Before I can start writing the AI portion into the game, I need to run some experiments which will require writing another program.  Once this puppy is running half way decent, I'll transplant it into the actual game.  The AI will need to do several things such as moving the ships around in a half way intelligent fashion while avoiding collisions with asteroids, pick the best target, decide when the best time is to fire weapons, and even when is a good time to kamikaze in and self destruct.  For now, All I'm going to worry about is navigation.  These ships will navigate using a method called "Line of Site Navigation".  Basically, navigation will take place in small, short segments that will eventually lead to the target.  Cruise missiles use this method to find their targets.  At the end of this section (or earlier, if you want to), you can download the program and run it.  All this was written using VB5.  I'm just going to cover the essential parts of the code.  There are basically two subroutines to look at:

 

This first part sets up some stuff like: 3 obstructions, starting positions and slope angles (I'll explain).

Public Sub Startup()

    these are the beginning "X" and "Y" locations

    TX = 542: TY = 185

    SX = 100: SY = 185

    Trackform.PSet (TX, TY), RGB(0, 255, 0)

    these are the obstructions

    Trackform.Line (120, 165)-(140, 205), RGB(200, 200, 200), BF

    Trackform.Line (200, 165)-(220, 205), RGB(200, 200, 200), BF

    Trackform.Line (480, 165)-(500, 205), RGB(200, 200, 200), BF

 

    Dim Position As Integer

    Position = 0

    remember the good ole "data" and "restore" commands?  Well, "SlopeData" simply holds the info that will be read into "SlopeTrack()"

    SlopeData = "0 ,-.2 ,.2 ,-.5 ,.5 ,-.8 ,1.2 ,-2.2,1.8 ,-3 ,5 ,-10 ,8 ,-13 "

    For T = 1 To 14

        SlopeTrack(T) = Val(Mid(SlopeData, ((T - 1) * 5) + 1, 4)): ' Read values into Slope(T)

    Next T

    Trackform.Visible = True

End Sub

 

OK, now the fun part, look over these variables...

 

Private Sub Timer1_Timer()

'********** VARIABLES **********

' > RD < Reset Data: this is used restart the data search in "SlopeData()"

' > SlopeCount < Slope Counter: used to keep track of dimensional position in "SlopeData()"

' > SX & SY < Ship X/Y position: the current position of the computer controlled object

' > SlopeX & SlopeY < X/Y angle to target: used to determine direction of target

' > TX & TY < X/Y position of target: used to track current postion of target

' > SD < Current SlopeTrack() value: used to determine which angle to use for navigation

' > SlopeTrack() < Slope Information: contains various slope values to try during navigation

' > X1 & Y1 & X2 & Y2 < Temporary variables: used to project possible navigation routes

' > D1 & D2 < Distance "1" and "2": gives the distance between TX & SX and TY & SY

' > DL < Long Distance: holds the value of whichever is greater: D1 or D2

' > XI & YI < X and Y Increments: the amount to add to SX and SY to move toward target

' > Good < Good move or not: stores the result of whether there is an obstruction in path

' > DIS < Distance to target: the same as "DL" but never goes over "20"

' > SXI & SYI < X and Y Increments: same as "XI" and "YI" if there are no obstructions in path

' > T < Temporary variable: used in for-next loops

' > RandomLock < Random Slope: used to return a random slope value if fixed values don't work

' > GoodCount < Good counter: used to delay reseting of RD - aids in navigation

' > Maxtrack < Maximum Track: used in conjunction with "GoodCount"

 

When looking over the code, just refer back to what each variable does to get an idea of what's going on...

BTW - this routine is looped through repeatedly just to move a few pixels!  However, it will be located in a timer routine to regulate its speed...

 

'****************************************************'

'********** Computer Movement Algorithms ************'

'****************************************************'

    If RD = 0 Then SlopeCount = 0: RD = 1

    SlopeX = (SX - TX)

    SlopeY = (SY - TY)

    If RandomLock > 0 Then RandomLock = RandomLock - 1

    If RandomLock = 0 Then SlopeCount = SlopeCount + 1          RandomLock will normally be "0"

    SD = SlopeTrack(SlopeCount)

 

    X1 = SX

    Y1 = SY

    X2 = TX - SlopeY * SD

    Y2 = TY + SlopeX * SD

 

    D1 = X2 - X1

    D2 = Y2 - Y1

 

    DL = Abs(D1): If Abs(D2) > Abs(D1) Then DL = Abs(D2)

 

    If DL < 1 Then DL = 0.1: ' On top of target

    XI = (D1 / DL)

    YI = (D2 / DL)

 

    Good = 0

    If DL > 20 Then DIS = 20 Else DIS = DL

    If RandomLock > 0 Then DIS = 20

    For T = 1 To DIS

        X1 = X1 + XI

        Y1 = Y1 + YI

        P = Trackform.Point(X1, Y1) : ' this will return a color value of a pixal on the screen

        If P > 255 Then Good = 1 : ' if Good=1 then an obstruction was detected

    Next T

 

    Line1.X1 = SX : ' these "Line1" values are here to view what target directions are being considered for demo purposes

    Line1.Y1 = SY : ' they aren't actually needed for proper functioning

    Line1.X2 = X1 : ' ditto

    Line1.Y2 = Y1 : ' ditto

 

    If Good = 0 Then

        If GoodCount = 0 Then

             RD = 0

             GoodCount = 3

             Maxtrack = Maxtrack - 1

             If Maxtrack < 0 Then Maxtrack = 0

        Else

             GoodCount = GoodCount - 1

        End If

        If Maxtrack = 0 Then RD = 0

        SXI = XI

        SYI = YI

    Else

        If Maxtrack < 10 Then Maxtrack = Maxtrack + 1

    End If

    If SD = 8 Then

        SD = (Rnd(1) * 50) - 25: 'add a random factor to shift tough searches

        RandomLock = 10: 'set how long to follow random course

        Text1.Text = RandomLock

    End If

 

    Text1.Text = SD

 

    If SD = -13 Then RD = 0: SXI = 0: SYI = 0

    Trackform.PSet (SX, SY), 0

 

    If Point(SX + SXI, SY + SYI) < 255 Then

        SX = SX + SXI

       SY = SY + SYI

    End If

 

    Trackform.PSet (SX, SY), RGB(255, 0, 0)

End Sub

 

So, basically, this routine will lead a computer controlled ship (CCS) on a bee-line course to its target unless there is an obstacle in its path.   When an obstacle is detected (it will scan out 20 pixels) the program starts using the slope information to look for different paths to take.  If you look at the SlopeData values, you'll see they start with small values, and then progress to greater (this can be viewed by running the program).  The test program places a green pixel on the screen (this is the target) and a red pixel (this is the CCS).  You can move the target around by pointing and clicking (to test different situations).  You can also view the slope info (called "line of site") to see what directions are being considered.  There are also slow / fast buttons to view what's going on better.

Download the program