Mirage Engine
[Summer Competition] My AI! - Printable Version

+- Mirage Engine (https://mirage-engine.uk/forums)
+-- Forum: Mirage Source (Nostalgia) (https://mirage-engine.uk/forums/forumdisplay.php?fid=61)
+--- Forum: Archive (2006-2011) (https://mirage-engine.uk/forums/forumdisplay.php?fid=18)
+---- Forum: Resources (https://mirage-engine.uk/forums/forumdisplay.php?fid=49)
+---- Thread: [Summer Competition] My AI! (/showthread.php?tid=1112)

Pages: 1 2


[Summer Competition] My AI! - Dragoons Master - 20-07-2007

Difficulty: 5/5
Need to understand!


19/07/2007
I strongly suggest that it you are going to try to make this tutorial, READ ALL THE THINGS AT THE LINKS INSIDE THIS TUTORIAL!!!
AND BACKUP YOUR SOURCE!!!!
Well, initially, I'd like to thank William for this opportunity and I think this is going to be a great tutorial. The idea of this tutorial is to improve the AI(Artificial Intelligence) of your Mirage Source game. The actual AI is very dumb and it can easily miss the target, not going through the correct/best path. What I did was replace the hole AI with a better code. Let's examine the actual code(that's inside 2 loops, the main is looping the maps and the second is for each MapNpc):

Code:
' /////////////////////////////////////////////
                ' // This is used for NPC walking/targetting //
                ' /////////////////////////////////////////////
                ' Make sure theres a npc with the map
                If Map(y).Npc(x) > 0 And MapNpc(y, x).Num > 0 Then
                    Target = MapNpc(y, x).Target
                    
                    ' Check to see if its time for the npc to walk
                    If Npc(NpcNum).Behavior  NPC_BEHAVIOR_SHOPKEEPER Then
                        ' Check to see if we are following a player or not
                        If Target > 0 Then
                            ' Check if the player is even playing, if so follow'm
                            If IsPlaying(Target) And GetPlayerMap(Target) = y Then
                                DidWalk = False
                                
                                i = Int(Rnd * 5)
First it checks if theres really a npc at that map, if this npc is not a shop keeper, if this npc actually has a target and if this target(players) is online and at the same map this npc is. Then it randomizes a number from 0 to 4.
Code:
' Lets move the npc
                                Select Case i
                                    Case 0
                                        ' Up
                                        If MapNpc(y, x).y > GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_UP) Then
                                                Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Down
                                        If MapNpc(y, x).y < GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_DOWN) Then
                                                Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Left
                                        If MapNpc(y, x).x > GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_LEFT) Then
                                                Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Right
                                        If MapNpc(y, x).x < GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_RIGHT) Then
                                                Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                    
                                    Case 1
                                        ' Right
                                        If MapNpc(y, x).x < GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_RIGHT) Then
                                                Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Left
                                        If MapNpc(y, x).x > GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_LEFT) Then
                                                Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Down
                                        If MapNpc(y, x).y < GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_DOWN) Then
                                                Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Up
                                        If MapNpc(y, x).y > GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_UP) Then
                                                Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        
                                    Case 2
                                        ' Down
                                        If MapNpc(y, x).y < GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_DOWN) Then
                                                Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Up
                                        If MapNpc(y, x).y > GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_UP) Then
                                                Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Right
                                        If MapNpc(y, x).x < GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_RIGHT) Then
                                                Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Left
                                        If MapNpc(y, x).x > GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_LEFT) Then
                                                Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                    
                                    Case 3
                                        ' Left
                                        If MapNpc(y, x).x > GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_LEFT) Then
                                                Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Right
                                        If MapNpc(y, x).x < GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_RIGHT) Then
                                                Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Up
                                        If MapNpc(y, x).y > GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_UP) Then
                                                Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Down
                                        If MapNpc(y, x).y < GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_DOWN) Then
                                                Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                End Select
This number represents the direction(0 up, 1 right, 2 down, 3 left) that the npc will try to move first. If there is nothing on the map, that's a good way to move because you will randomly walk close to him and not all th way right(ex) and then all the way up, directly to the player.
Code:
' Check if we can't move and if player is behind something and if we can just switch dirs
                                If Not DidWalk Then
                                    If MapNpc(y, x).x - 1 = GetPlayerX(Target) And MapNpc(y, x).y = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_LEFT Then
                                            Call NpcDir(y, x, DIR_LEFT)
                                        End If
                                        DidWalk = True
                                    End If
                                    If MapNpc(y, x).x + 1 = GetPlayerX(Target) And MapNpc(y, x).y = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_RIGHT Then
                                            Call NpcDir(y, x, DIR_RIGHT)
                                        End If
                                        DidWalk = True
                                    End If
                                    If MapNpc(y, x).x = GetPlayerX(Target) And MapNpc(y, x).y - 1 = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_UP Then
                                            Call NpcDir(y, x, DIR_UP)
                                        End If
                                        DidWalk = True
                                    End If
                                    If MapNpc(y, x).x = GetPlayerX(Target) And MapNpc(y, x).y + 1 = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_DOWN Then
                                            Call NpcDir(y, x, DIR_DOWN)
                                        End If
                                        DidWalk = True
                                    End If
                                    
                                    ' We could not move so player must be behind something, walk randomly.
                                    If Not DidWalk Then
                                        i = Int(Rnd * 2)
                                        If i = 1 Then
                                            i = Int(Rnd * 4)
                                            If CanNpcMove(y, x, i) Then
                                                Call NpcMove(y, x, i, MOVING_WALKING)
                                            End If
                                        End If
                                    End If
                                End If
If you couldn't move that's probably something on the way(the player it self or something else), and this algorithm can't pass thru it, so it will just switch the npc's direction so it will be looking at the player. If even with that, the npc didn't move so it stills away from the player but it can't go closer to it, and it will just randomly walk any direction or just not move at all.
Code:
Else
                                MapNpc(y, x).Target = 0
                            End If
                        Else
                            i = Int(Rnd * 4)
                            If i = 1 Then
                                i = Int(Rnd * 4)
                                If CanNpcMove(y, x, i) Then
                                    Call NpcMove(y, x, i, MOVING_WALKING)
                                End If
                            End If
If the npc has no target then it will just randomly make a happy move to anywhere.
Code:
End If
                    End If
                End If

Well, this code works, but I really didn't like it, so I deleted every thing and made something all new, able to find the perfect path between the player and the npc, so the npc can make any puzzle and things like this. Actually in my game, this algorithm changed a bit because I'm using my scrolling maps system, but I have a backup here and I'll show you guys how I did it and how it works. It IS quite hard to understand because it uses a few theories from college and most of them are not easy to understand. I'll use a thing named graph, and this graph is quite good think. It is made of vertices and edges. Let's imagine each vertex as a tile and each edge as a possible bridge between each tile.
[Image: GraphMap.JPG]
This is a Screen Shot I've taken from a simple MSE map attached with this little balls and lines connecting the tiles that you can move from one to an other. That's basically a graph... a little bit simplified.
(take a look at http://en.wikipedia.org/wiki/Graph_theory and http://en.wikipedia.org/wiki/Graph_%28data_structure%29)

20/07/2007
If you read all the links and didn't understand anything or you are still missing something on your mind, READ IT AGAIN or ask for someone's help. Don't go further if you don't understand the things before this!
Well, now I'm going to talk about this graph and how the heck this is going to help us out with the AI.
What I did was showing you guys how this graph is represented on a visual world, but there is no such thing in programing so we need to store this data somewhere, somehow.
For this, I numerated each vertex and made a big matrix that holds the information of the connections. That's named adjacency matrix(http://en.wikipedia.org/wiki/Graph_%28d ... esentation). It's like this: we create a matrix NxN(N being the number of vertices we hold, in our case, (MAX_MAPX + 1) * (MAX_MAPY + 1) and fill it with zeros. Then we check if we can move directly from tile X to tile Y(X and Y is the tile number, the enumerated tiles), we just make matrix(X, Y) = 1. So if we look at the matrix and there is a 1 at position (3, 8), it means that we can move from tile 3 to tile 8. But we want our npc to move both ways so we make the same thing but from Y to X, like this matrix(Y, X) = 1.
Considering we already have this adjacency matrix filled with our data, we need now to find the shortest path from point A to point B and that's where the magic is, the BFS algorithm, Breadth-First Search algorithm(http://en.wikipedia.org/wiki/Breadth-first_search).
This is where the important thing happens, and the hard part too... This algorithm searches for the shortest path between vertex A and vertex B. By doing this, it loops thru all the vertices, starting from the end. So it's just like we do to find the correct path in a puzzle game. Instead of starting from the real start, we start from the end and tries to find the path, at least I do that xD. The BFS algorithm is quite easy to implement and there are not much problems, so I'll start to do it now. Focus on this because it's all the magic happening.

First, we need our adjacency matrix declared, let's do that inside modGeneral header.
Code:
' Used for AI
Public NV As Integer ' Number of Vertices
Public Mat() As Byte ' Our Adjacency Matrix
Now we need to initialize this matrix, and we'll do it at InitServer Sub.
Under
Code:
Call SpawnAllMapNpcs
Add this:
Code:
NV = (MAX_MAPX + 1) * (MAX_MAPY + 1) - 1
    ReDim Mat(0 To NV, 0 To NV) As Byte
Now that it's all there, we need to remove the old AI, almost all of it.
Inside GameAI Sub, remove all this code:
Code:
i = Int(Rnd * 5)
                                
                                ' Lets move the npc
                                Select Case i
                                    Case 0
                                        ' Up
                                        If MapNpc(y, x).y > GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_UP) Then
                                                Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Down
                                        If MapNpc(y, x).y < GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_DOWN) Then
                                                Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Left
                                        If MapNpc(y, x).x > GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_LEFT) Then
                                                Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Right
                                        If MapNpc(y, x).x < GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_RIGHT) Then
                                                Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                    
                                    Case 1
                                        ' Right
                                        If MapNpc(y, x).x < GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_RIGHT) Then
                                                Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Left
                                        If MapNpc(y, x).x > GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_LEFT) Then
                                                Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Down
                                        If MapNpc(y, x).y < GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_DOWN) Then
                                                Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Up
                                        If MapNpc(y, x).y > GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_UP) Then
                                                Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        
                                    Case 2
                                        ' Down
                                        If MapNpc(y, x).y < GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_DOWN) Then
                                                Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Up
                                        If MapNpc(y, x).y > GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_UP) Then
                                                Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Right
                                        If MapNpc(y, x).x < GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_RIGHT) Then
                                                Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Left
                                        If MapNpc(y, x).x > GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_LEFT) Then
                                                Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                    
                                    Case 3
                                        ' Left
                                        If MapNpc(y, x).x > GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_LEFT) Then
                                                Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Right
                                        If MapNpc(y, x).x < GetPlayerX(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_RIGHT) Then
                                                Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Up
                                        If MapNpc(y, x).y > GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_UP) Then
                                                Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                        ' Down
                                        If MapNpc(y, x).y < GetPlayerY(Target) And DidWalk = False Then
                                            If CanNpcMove(y, x, DIR_DOWN) Then
                                                Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                DidWalk = True
                                            End If
                                        End If
                                End Select
It is inside this If:
Code:
' /////////////////////////////////////////////
                ' // This is used for NPC walking/targetting //
                ' /////////////////////////////////////////////
                ' Make sure theres a npc with the map
                If Map(Y).Npc(X) > 0 And MapNpc(Y, X).num > 0 Then
                    Target = MapNpc(Y, X).Target
                    
                    ' Check to see if its time for the npc to walk
                    If Npc(NpcNum).Behavior  NPC_BEHAVIOR_SHOPKEEPER Then
                        ' Check to see if we are following a player or not
                        If Target > 0 Then
                            ' Check if the player is even playing, if so follow'm
                            If IsPlaying(Target) And GetPlayerMap(Target) = Y Then
                                DidWalk = False
I mean the most inside scope, the last one. Now we can start adding our AI. You'll probably have something like this:
Code:
' /////////////////////////////////////////////
                ' // This is used for NPC walking/targetting //
                ' /////////////////////////////////////////////
                ' Make sure theres a npc with the map
                If Map(y).Npc(x) > 0 And MapNpc(y, x).Num > 0 Then
                    Target = MapNpc(y, x).Target
                    
                    ' Check to see if its time for the npc to walk
                    If Npc(NpcNum).Behavior  NPC_BEHAVIOR_SHOPKEEPER Then
                        ' Check to see if we are following a player or not
                        If Target > 0 Then
                            ' Check if the player is even playing, if so follow'm
                            If IsPlaying(Target) And GetPlayerMap(Target) = y Then
                                DidWalk = False
                                
                                
                                ' Check if we can't move and if player is behind something and if we can just switch dirs
                                If Not DidWalk Then
                                    If MapNpc(y, x).x - 1 = GetPlayerX(Target) And MapNpc(y, x).y = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_LEFT Then
                                            Call NpcDir(y, x, DIR_LEFT)
                                        End If
                                        DidWalk = True
                                    End If
                                    If MapNpc(y, x).x + 1 = GetPlayerX(Target) And MapNpc(y, x).y = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_RIGHT Then
                                            Call NpcDir(y, x, DIR_RIGHT)
                                        End If
                                        DidWalk = True
                                    End If
                                    If MapNpc(y, x).x = GetPlayerX(Target) And MapNpc(y, x).y - 1 = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_UP Then
                                            Call NpcDir(y, x, DIR_UP)
                                        End If
                                        DidWalk = True
                                    End If
                                    If MapNpc(y, x).x = GetPlayerX(Target) And MapNpc(y, x).y + 1 = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_DOWN Then
                                            Call NpcDir(y, x, DIR_DOWN)
                                        End If
                                        DidWalk = True
                                    End If
                                    
                                    ' We could not move so player must be behind something, walk randomly.
                                    If Not DidWalk Then
                                        i = Int(Rnd * 2)
                                        If i = 1 Then
                                            i = Int(Rnd * 4)
                                            If CanNpcMove(y, x, i) Then
                                                Call NpcMove(y, x, i, MOVING_WALKING)
                                            End If
                                        End If
                                    End If
                                End If
                            Else
                                MapNpc(y, x).Target = 0
                            End If
                        Else
                            i = Int(Rnd * 4)
                            If i = 1 Then
                                i = Int(Rnd * 4)
                                If CanNpcMove(y, x, i) Then
                                    Call NpcMove(y, x, i, MOVING_WALKING)
                                End If
                            End If
                        End If
                    End If
                End If
Now, right after
Code:
DidWalk = False
add this:
Code:
CloseToPlayer = False
                                If (GetPlayerY(Target) + 1 = MapNpc(y, x).y) And (GetPlayerX(Target) = MapNpc(y, x).x) Then
                                    CloseToPlayer = True
                                Else
                                    If (GetPlayerY(Target) - 1 = MapNpc(y, x).y) And (GetPlayerX(Target) = MapNpc(y, x).x) Then
                                        CloseToPlayer = True
                                    Else
                                        If (GetPlayerY(Target) = MapNpc(y, x).y) And (GetPlayerX(Target) + 1 = MapNpc(y, x).x) Then
                                            CloseToPlayer = True
                                        Else
                                            If (GetPlayerY(Target) = MapNpc(y, x).y) And (GetPlayerX(Target) - 1 = MapNpc(y, x).x) Then
                                                CloseToPlayer = True
                                            End If
                                        End If
                                    End If
                                End If
Don't forget to declare CloseToPlayer at GameAI headers, like this:
Code:
Dim CloseToPlayer As Boolean
This sequence of If and Else is there only to make sure the npc is not already at the side of the player.
Now add an If to make sure the npc is not close to the player, like this:
Code:
If Not CloseToPlayer Then
                                    
                                End If
Now I'll show you how the adjacency matrix really is on a visual mode. I'll use the same image as I did before but I'll finish it.
[Image: GraphMap2.JPG]
Here it is. Took me a while of time to make this, with all the numbers xD
Well, now we can perfectly see that the npc(the pink thing) need to go from vertex 153 to vertex 51(the player).
If we where using the old AI it maybe(maybe) could go to vertex 53 but it's random, so it could also go only to 57 and stay there xD. The AI we are making here will literally be a genius. It will find the best path from vertex 153 to 51 and save this path into a vector. A vector? Wait, we didn't declared this vector! We'll need it! So come back to modGeneral's header and add this 3 variables
Code:
Public Marked() As Byte
Public PathLine() As Integer
Public Path() As Integer
We also need to initialize this 3 vectors, so go back to InitServer and under
Code:
ReDim Mat(0 To NV, 0 To NV) As Byte
Add this:
Code:
ReDim Marked(0 To NV) As Byte
    ReDim PathLine(0 To NV) As Integer
    ReDim Path(0 To NV) As Integer
Now we have the vector, actually 3 vectors: Marked, PathLine and Path.
Marked vector will store the information about the vertices we've already walked thru(a correct tile, that's part of the algorithm), PathLine will also store information that's part of the algorithm, we always add the tile we found(remember, destiny to origin(B->A)) to the PathLine. When the PathLine(o) is filed with information, doesn't mather when, the first time this is done, then we have the best path(it means we have it all).
That's complicated, it's how the algorithm works.
Now we need to store the information about where we are and where we want to go. For this we create 2 new variables, "o"(Origin) and "d"(Destiny). Create them at the top of GameAI Sub:
Code:
Dim o As Long
Dim d As Long
Now inside that If(If not close to player then) add this:
Code:
o = MapNpc(Y, X).Y * (MAX_MAPX + 1) + MapNpc(Y, X).X
                                    d = GetPlayerY(Target) * (MAX_MAPX + 1) + GetPlayerX(Target)
Our origin is the npcs position, but we need the vertex number that represent that tile, for this we use a little math with mapnpcs x and y together with the map's size. The same for the Destiny, but now we use the player's coordinates. Now we know from which vertex are leaving and the vertex we want to go. Now let's initialize the 3 vectors we are going to use:
Code:
For i = 0 To NV
                                        Marked(i) = 0
                                        PathLine(i) = -1
                                        Path(i) = -1
                                    Next i
We use 0 to Marked and -1 for PathLine and Path because they will store the vertices numbers, 0 to NV, so we use -1. Now that all the Vectors are OK, we need one more thing to really find the path, we still need to fill our adjacency matrix with the tile's block, player's position, npc's position and everything that may block the npc's way. That's a lot of code, here is a little part of it:
Code:
For i = 0 To NV
                                        VY = Int(i / (MAX_MAPX + 1))
                                        VX = i Mod (MAX_MAPX + 1)
                                        
                                        If VX >= 0 And VX < MAX_MAPX Then Mat(i, i + 1) = 1
                                        If VX > 0 And VX = 0 And VY < MAX_MAPY Then Mat(i, i + MAX_MAPX + 1) = 1
                                        If VY > 0 And VY  VX Then
                                                ' Left
                                                If CanNpcMove(y, x, DIR_LEFT) Then
                                                    Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                    DidWalk = True
                                                End If
                                            ElseIf MapNpc(y, x).x < VX Then
                                                ' Right
                                                If CanNpcMove(y, x, DIR_RIGHT) Then
                                                    Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                    DidWalk = True
                                                End If
                                            End If
DONE! Now it should work, run your game and take a look at the AI. Make a simple puzzle at the map and add a attack on sight npc, with a big range of sight. Just great, it should be working by now! But there really are a lot of improvements that needs to be done, and I really mean it. If you use this code right now, we won't be able to play, I mean, you will, but with a lot of players and a lot of npcs, just impossible, the npcs will have too much lag...
For fixing this, we will improve a LOT this code. what we are going to do is replace the:
Code:
For i = 0 To NV
The, one inside the While and a few more. Since we know how the graph format is, a very specific graph, we can change a lot of things. We know that each vertex has max of 4 connections, never more, so we need to change all this 192(in vanilla MS) loops and replace them with only 4 Wink Much less processing. For this, lets at the top of our AI system code, and take a look at this part of the code :
Code:
For J = 0 To NV
    Mat(J, i) = 0
    Mat(i, J) = 0
Next J
Replace the:
Code:
Mat(J, i) = 0
Mat(i, J) = 0
With this:[code] If VX >= 0 And VX < MAX_MAPX Then Mat(i, i + 1) = 0
If VX > 0 And VX = 0 And VY < MAX_MAPY Then Mat(i, i + MAX_MAPX + 1) = 0
If VY > 0 And VY = 0 And VY < MAX_MAPY Then
If Mat(PathLine(Start), PathLine(Start) + MAX_MAPX + 1) = 1 And Marked(PathLine(Start) + MAX_MAPX + 1) = 0 And Flag = 0 Then
Path(PathLine(Start) + MAX_MAPX + 1) = PathLine(Start)
PathLine(Finish) = PathLine(Start) + MAX_MAPX + 1
Marked(PathLine(Start) + MAX_MAPX + 1) = 1
Finish = Finish + 1
If PathLine(Finish - 1) = o Then
Flag = 1
End If
End If
End If

VY = Int(PathLine(Start) / (MAX_MAPX + 1))
VX = PathLine(Start) Mod (MAX_MAPX + 1)
If VX > 0 And VX = 0 And VX < MAX_MAPX Then
If Mat(PathLine(Start), PathLine(Start) + 1) = 1 And Marked(PathLine(Start) + 1) = 0 And Flag = 0 Then
Path(PathLine(Start) + 1) = PathLine(Start)
PathLine(Finish) = PathLine(Start) + 1
Marked(PathLine(Start) + 1) = 1
Finish = Finish + 1
If PathLine(Finish - 1) = o Then
Flag = 1
End If
End If
End If

VY = Int(PathLine(Start) / (MAX_MAPX + 1))
VX = PathLine(Start) Mod (MAX_MAPX + 1)
If VY > 0 And VY


Re: [Summer Competition] My AI! - Iluvlamp - 20-07-2007

Sexy, it'll be nice to see one of MSs greatest programmers way of doing NPC AI.


Re: [Summer Competition] My AI! - Dragoons Master - 21-07-2007

Iluvlamp Wrote:Sexy, it'll be nice to see one of MSs greatest programmers way of doing NPC AI.
Tnx ^^
I've updated the TUT a lot, almost finished, need the final and most important part and I'll make it a little bit faster so there will be less processing needed.


Re: [Summer Competition] My AI! - Robin - 21-07-2007

Mhhh, started reading through and it looks really interesting.

Can't wait to try it out ^_^


Re: [Summer Competition] My AI! - William - 21-07-2007

Yeah, I'd love to see were this is going. I started a custom AI before, but without these mathematical parts. Was going to use it for k2h since the default ai is kinda lame.


Re: [Summer Competition] My AI! - William - 21-07-2007

I will for sure add it. Thanks for finishing it Big Grin Ill respond how it goes later.


Re: [Summer Competition] My AI! - Robin - 21-07-2007

That was quite possibly both the easiest tutorial ever, and the best addition to mirage ever.

Now I really want to completely understand what's going on ;-;

Dragoons Master, you really are a genius.

I commend you!


Re: [Summer Competition] My AI! - William - 22-07-2007

As a conclusion, the AI works just as it should. I've noticed my game aint that fast, I will probably need to get a better connection and also improve my RAM with 1 more gb. And probably need byte packets client side too. And gotta fix position modification, and improve more things..

.. Im not 100% sure Im going to keep the AI in my game yet, just feels like its the top of the iceberg on crashing everything. Gotta improve what I already got. And I need to talk to DM more.


Re: [Summer Competition] My AI! - Dragoons Master - 26-07-2007

Has anyone with normal maps(no scrolling) tried this and found any bug?
I'll realy like to find one ^^


Re: [Summer Competition] My AI! - Robin - 26-07-2007

Not that I could find...

I noticed that sometimes the npcs would stop following, but a tiny bit of debugging found out that you used a different style blocked tile check that usual, so I just needed to edit that and it was fine.


Re: [Summer Competition] My AI! - William - 29-07-2007

Robin Wrote:Not that I could find...

I noticed that sometimes the npcs would stop following, but a tiny bit of debugging found out that you used a different style blocked tile check that usual, so I just needed to edit that and it was fine.
It looks to work pretty well, I will do more testing ut for now it seems to work.


Re: [Summer Competition] My AI! - Bradyok - 30-07-2007

GREAT tutorial, a bit over my head, but it's really useful. Just a quick note for the tile detection thing, npcs can only walk on item attributes, so the check if the door is open is useless. Also, for some reason only like 1 of the attack on sight npcs goes after me, even with multiple routes open to me; the others are as stupid as ever. I'de like to talk to you if you get a chance.


Re: [Summer Competition] My AI! - seraphelic - 18-11-2007

This is genius. I actually had an idea in the back of my mind to create "shortest path finding", but my god.. this is amazing.


Re: [Summer Competition] My AI! - seraphelic - 08-12-2007

I need some help, Ive tryed several times to implement this tutorial but still cant get it to work for Elysium. Heres my code:
Code:
' /////////////////////////////////////////////
                ' // This is used for NPC walking/targetting //
                ' /////////////////////////////////////////////
                ' Make sure theres a npc with the map
                If Map(y).Npc(x) > 0 And MapNpc(y, x).Num > 0 Then
                    Target = MapNpc(y, x).Target
                    
                    ' Check to see if its time for the npc to walk
                    If Npc(NpcNum).Behavior  NPC_BEHAVIOR_SHOPKEEPER Then
                        ' Check to see if we are following a player or not
                        If Target > 0 Then
                            ' Check if the player is even playing, if so follow'm
                            If IsPlaying(Target) And GetPlayerMap(Target) = y Then
                                DidWalk = False
                                CloseToPlayer = False
                                If (GetPlayerY(Target) + 1 = MapNpc(y, x).y) And (GetPlayerX(Target) = MapNpc(y, x).x) Then
                                    CloseToPlayer = True
                                Else
                                    If (GetPlayerY(Target) - 1 = MapNpc(y, x).y) And (GetPlayerX(Target) = MapNpc(y, x).x) Then
                                        CloseToPlayer = True
                                    Else
                                        If (GetPlayerY(Target) = MapNpc(y, x).y) And (GetPlayerX(Target) + 1 = MapNpc(y, x).x) Then
                                            CloseToPlayer = True
                                        Else
                                            If (GetPlayerY(Target) = MapNpc(y, x).y) And (GetPlayerX(Target) - 1 = MapNpc(y, x).x) Then
                                                CloseToPlayer = True
                                            End If
                                        End If
                                    End If
                                End If                                
                                If Not CloseToPlayer Then
                                    o = MapNpc(Y, X).Y * (MAX_MAPX + 1) + MapNpc(Y, X).X
                                    d = GetPlayerY(Target) * (MAX_MAPX + 1) + GetPlayerX(Target)
                                    For i = 0 To NV
                                        VY = Int(i / (MAX_MAPX + 1))
                                        VX = i Mod (MAX_MAPX + 1)
                                        If (Map(y).Tile(VX, VY).Type = TILE_TYPE_BLOCKED Or Map(y).Tile(VX, VY).Type = TILE_TYPE_NPCAVOID Or Map(y).Tile(VX, VY).Type = TILE_TYPE_WARP Or (Map(y).Tile(VX, VY).Type = TILE_TYPE_KEY And TempTile(y).DoorOpen(VX, VY) = NO)) Then
                                            For J = 0 To NV
                                                Mat(J, i) = 0
                                                Mat(i, J) = 0
                                            Next J
                                        End If
                                        For J = 1 To MAX_PLAYERS
                                            If IsPlaying(J) Then
                                                If GetPlayerMap(J) = y Then
                                                    If GetPlayerX(J) = VX And GetPlayerY(J) = VY And J  Target Then
                                                        For K = 0 To NV
                                                            Mat(i, K) = 0
                                                            Mat(K, i) = 0
                                                        Next K
                                                    End If
                                                End If
                                            End If
                                        Next J
                                        For J = 1 To MAX_MAP_NPCS
                                            If Map(y).Npc(J) > 0 And MapNpc(y, J).Num > 0 Then
                                                If MapNpc(y, J).x = VX And MapNpc(y, J).y = VY And J  x Then
                                                    For K = 0 To NV
                                                        Mat(i, K) = 0
                                                        Mat(K, i) = 0
                                                    Next K
                                                End If
                                            End If
                                        Next J
                                    Next i
                                    Flag = 0
                                    PathLine(0) = d
                                    Start = 0
                                    Finish = 1
                                    If o  d Then
                                        While (PathLine(Start)  -1 And Flag = 0)
                                            VY = Int(PathLine(Start) / (MAX_MAPX + 1))
                                            VX = PathLine(Start) Mod (MAX_MAPX + 1)
                                            If VY >= 0 And VY < MAX_MAPY Then
                                                If Mat(PathLine(Start), PathLine(Start) + MAX_MAPX + 1) = 1 And Marked(PathLine(Start) + MAX_MAPX + 1) = 0 And Flag = 0 Then
                                                    Path(PathLine(Start) + MAX_MAPX + 1) = PathLine(Start)
                                                    PathLine(Finish) = PathLine(Start) + MAX_MAPX + 1
                                                    Marked(PathLine(Start) + MAX_MAPX + 1) = 1
                                                    Finish = Finish + 1
                                                    If PathLine(Finish - 1) = o Then
                                                        Flag = 1
                                                    End If
                                                End If
                                            End If
                                            
                                            VY = Int(PathLine(Start) / (MAX_MAPX + 1))
                                            VX = PathLine(Start) Mod (MAX_MAPX + 1)
                                            If VX > 0 And VX = 0 And VX < MAX_MAPX Then
                                                If Mat(PathLine(Start), PathLine(Start) + 1) = 1 And Marked(PathLine(Start) + 1) = 0 And Flag = 0 Then
                                                    Path(PathLine(Start) + 1) = PathLine(Start)
                                                    PathLine(Finish) = PathLine(Start) + 1
                                                    Marked(PathLine(Start) + 1) = 1
                                                    Finish = Finish + 1
                                                    If PathLine(Finish - 1) = o Then
                                                        Flag = 1
                                                    End If
                                                End If
                                            End If
                                            
                                            VY = Int(PathLine(Start) / (MAX_MAPX + 1))
                                            VX = PathLine(Start) Mod (MAX_MAPX + 1)
                                            If VY > 0 And VY  VY Then
                                                If CanNpcMove(y, x, DIR_UP) Then
                                                    Call NpcMove(y, x, DIR_UP, MOVING_WALKING)
                                                    DidWalk = True
                                                End If
                                            ElseIf MapNpc(y, x).y < VY Then
                                                ' Down
                                                If CanNpcMove(y, x, DIR_DOWN) Then
                                                    Call NpcMove(y, x, DIR_DOWN, MOVING_WALKING)
                                                    DidWalk = True
                                                End If
                                            ElseIf MapNpc(y, x).x > VX Then
                                                ' Left
                                                If CanNpcMove(y, x, DIR_LEFT) Then
                                                    Call NpcMove(y, x, DIR_LEFT, MOVING_WALKING)
                                                    DidWalk = True
                                                End If
                                            ElseIf MapNpc(y, x).x < VX Then
                                                ' Right
                                                If CanNpcMove(y, x, DIR_RIGHT) Then
                                                    Call NpcMove(y, x, DIR_RIGHT, MOVING_WALKING)
                                                    DidWalk = True
                                                End If
                                            End If
                                                                              End If
                                ' Check if we can't move and if player is behind something and if we can just switch dirs
                                If Not DidWalk Then
                                    If MapNpc(y, x).x - 1 = GetPlayerX(Target) And MapNpc(y, x).y = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_LEFT Then
                                            Call NpcDir(y, x, DIR_LEFT)
                                        End If
                                        DidWalk = True
                                    End If
                                    If MapNpc(y, x).x + 1 = GetPlayerX(Target) And MapNpc(y, x).y = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_RIGHT Then
                                            Call NpcDir(y, x, DIR_RIGHT)
                                        End If
                                        DidWalk = True
                                    End If
                                    If MapNpc(y, x).x = GetPlayerX(Target) And MapNpc(y, x).y - 1 = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_UP Then
                                            Call NpcDir(y, x, DIR_UP)
                                        End If
                                        DidWalk = True
                                    End If
                                    If MapNpc(y, x).x = GetPlayerX(Target) And MapNpc(y, x).y + 1 = GetPlayerY(Target) Then
                                        If MapNpc(y, x).Dir  DIR_DOWN Then
                                            Call NpcDir(y, x, DIR_DOWN)
                                        End If
                                        DidWalk = True
                                    End If
                                    
                                    ' We could not move so player must be behind something, walk randomly.
                                    If Not DidWalk Then
                                        i = Int(Rnd * 2)
                                        If i = 1 Then
                                            i = Int(Rnd * 4)
                                            If CanNpcMove(y, x, i) Then
                                                Call NpcMove(y, x, i, MOVING_WALKING)
                                            End If
                                        End If
                                    End If
                                End If
                            Else
                                MapNpc(y, x).Target = 0
                            End If
                        Else
                            i = Int(Rnd * 4)
                            If i = 1 Then
                                i = Int(Rnd * 4)
                                If CanNpcMove(y, x, i) Then
                                    Call NpcMove(y, x, i, MOVING_WALKING)
                                End If
                            End If
                        End If
                    End If
                End If
Ive spent alot of time going through the scenarios and following the pathfinding, my main problem is that Flag never becomes 1. For some reason, my mat() causes subscript out of range. Because the InitServer sub is no longer used, I put this directly after Call SpawnAllMapNpcs in the main sub in modGeneral:
Code:
NV = (MAX_MAPX + 1) * (MAX_MAPY + 1) - 1
    ReDim Mat(0 To NV, 0 To NV) As Byte
    ReDim Marked(0 To NV) As Byte
    ReDim PathLine(0 To NV) As Integer
    ReDim Path(0 To NV) As Integer
Maybe thats the wrong place for elysium? Hope you can help, thanks!


Re: [Summer Competition] My AI! - Dragoons Master - 12-12-2007

I've never used elysium so I have absolutly no idea ><
Sry, can't help... If I had the time I would, but I'm in the last days of the semester here at college and I really need to study...


Re: [Summer Competition] My AI! - Robin - 12-12-2007

No difference between AI in Elysium and Mirage.


Re: [Summer Competition] My AI! - seraphelic - 16-12-2007

Robin Wrote:No difference between AI in Elysium and Mirage.

InitServer isnt used anymore:
[qoute="Dragoons Master"]Now we need to initialize this matrix, and we'll do it at InitServer Sub.[/qoute]

Ive tryed adding this so many times.


Re: [Summer Competition] My AI! - Dragoons Master - 17-12-2007

Just add it anywhere before:
Code:
'Lets start the server!
    ServerLoop
That MIGHT work, I'm not sure...


Re: [Summer Competition] My AI! - genusis - 16-02-2008

NVM i fixed it by changing

MapNpc(y, x).Target = 0
To
MapNpc(y, x).Target = 1

So now the NPC can attack right.


Re: [Summer Competition] My AI! - TehGrunt - 10-03-2008

Not wanting to necropost or anything, but I have a problem with this tutorial.

See, the AI was working perfectly, and I used genusis' fix, but for some reason, when I changed the number of maps/items/npcs, the NPCs stopped moving. I can interact, and use the speeches, but they won't follow me to attack. Also, they don't attack back. I was wondering if changing the number of NPCs did something?


Re: [Summer Competition] My AI! - genusis - 20-03-2008

you have to take the admin thing out so they can attack admins haha.


Re: [Summer Competition] My AI! - Dragoons Master - 20-03-2008

Ops, sry about that xD


Re: [Summer Competition] My AI! - TehGrunt - 31-03-2008

I wasn't an admin when I tested this. My players have also reported this happening , so..


Re: [Summer Competition] My AI! - Dragoons Master - 05-04-2008

It just does not happen to me, sry.


Re: [Summer Competition] My AI! - TehGrunt - 06-04-2008

Ok, well, thanks for trying Dragoons.