Improving the AI: Squad Warfare
Now that the ships can navigate, they need to work together and choose sensible common targets. The way I'm going to do this is to form Squads of ships that are initially near each other. When the program starts up, a random # of squads are designated and the ships, in groups, take off to their targets. When the ships get within a predesignated range, their drones are fired. I ran into several problems implementing all this. I had to: 1) figure out how to determine which ships were close to each other to form the squads, 2) balance the number of ships in each squad, 3) choose different, but effective targets for each squad. Here's the main procedure used for all of this:
Public Sub Check_Squads()
If SquadCount = 0 Then
SquadCount = 1
Dim SquadSize As Integer: ' average number of
ships in each squad
Dim CurrentSquad As Integer: ' the current squad
that will be assigned
Dim SquadShipCount As Integer: ' the current ship
to be selected for a squad - up to squadsize
Dim SquadTotal As Integer: ' the total # of ships
currently assigned to a squad
'This will only be run ONCE to set initial
positions
SquadNumber = Int(Rnd(1) * 7) + 3
'Assign Squads
SquadSize = 25 / SquadNumber
SquadShipCount = 0
Dim Pick As Integer
Do
CurrentSquad = CurrentSquad + 1
If CurrentSquad > SquadNumber Then
CurrentSquad = SquadNumber
While SquadShipCount < SquadSize And
SquadTotal < 25
'pick the next ship in sequence that is not
already assigned to a squad to be the first in a new squad
For T = 1 To 25
If
Ship(T + 25, 10) = 0 Then
Pick
= T
Ship(Pick
+ 25, 10) = CurrentSquad
SquadTotal
= SquadTotal + 1
SquadShipCount
= SquadShipCount + 1
Exit
For
End
If
Next T
Dim DistanceList(25, 2)
As Integer
Dim ActualDistance As
Single
'find
the nearest ships to the first one found not assigned to a squad
For T = 1 To 25
DistanceList(T,
1) = T: 'set current ship
ActualDistance
= Sqr((Abs(Ship(Pick + 25, 1) - Ship(T + 25, 1)) + Abs(Ship(Pick + 25, 2) - Ship(T + 25,
2))) ^ 2)
DistanceList(T,
2) = ActualDistance
If
DistanceList(T, 2) = 0 Then DistanceList(T, 2) = 500: 'don't include
the reference ship!
Next T
'use a
crude bubble sort to arrange them in order from nearest to furthest
Dim Temp As Integer
For X = 1 To 25
For
Y = 1 To 25
If
DistanceList(X, 2) < DistanceList(Y, 2) Then
'swap the values
Temp
= DistanceList(X, 1): DistanceList(X, 1) = DistanceList(Y, 1): DistanceList(Y, 1) = Temp
Temp
= DistanceList(X, 2): DistanceList(X, 2) = DistanceList(Y, 2): DistanceList(Y, 2) = Temp
End
If
Next
Y
Next X
Dim CurrentCount As
Integer
'assign the nearest to a common squad not already assigned
While SquadShipCount
< SquadSize And CurrentCount < 25
CurrentCount
= CurrentCount + 1
If
Ship(DistanceList(CurrentCount, 1) + 25, 10) = 0 Then
Ship(DistanceList(CurrentCount,
1) + 25, 10) = CurrentSquad
SquadShipCount
= SquadShipCount + 1
SquadTotal
= SquadTotal + 1
End
If
Wend
Wend
SquadShipCount = 0
If SquadTotal = 25 Then Exit Do
DoEvents
Loop
Dim C As Integer
'assign the new target values
For T = 1 To SquadNumber
While Squad(T, 1) = 0
C = 10000
DoEvents
TX = Rnd(1) * 200 + 250
TY = Rnd(1) * 300
For X = 1 To 25
If
C > Sqr((Abs(Ship(X, 1) - TX) + Abs(Ship(X, 2) - TY)) ^ 2) Then
' this is the current closest ship to point "TX,TY"
C
= Sqr((Abs(Ship(X, 1) - TX) + Abs(Ship(X, 2) - TY)) ^ 2)
Good
= 1
For
Y = 1 To 10
If
Squad(Y, 1) = X Then Good = 0
Next
Y
If
Good = 1 Then Squad(T, 1) = X: 'this will eventually choose the
closest
End
If
Next X
Wend
Next T
'transfer squad target info to each ship
For T = 1 To 25
Ship(T + 25, 8) = Squad(Ship(T + 25, 10), 1)
Next T
End If
'this is for displaying what the variables are
doing and will be deleted later
Dim A$
A$ = ""
For X = 1 To 10
tot = 0
For T = 1 To 25
If Ship(T + 25, 10) = X Then tot = tot + 1
Next T
A$ = A$ + Str$(X) + ": #:" + Str$(tot) + " targ:" +
Str$(Squad(X, 1)) + Chr$(13)
Next X
ControlDisplay.Label1.Caption = A$
End Sub
I changed the explosion routine to accomadate more explosions. The first time the computer ships moved in and started destroying ships, there were dozens of explosions taking place and the routines couldn't keep up!
Here's where we're at: