20-07-2007, 06:34 AM
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):
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.
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.
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.
If the npc has no target then it will just randomly make a happy move to anywhere.
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]](http://br.geocities.com/black_ages/MS/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.
Now we need to initialize this matrix, and we'll do it at InitServer Sub.
UnderAdd this:Now that it's all there, we need to remove the old AI, almost all of it.
Inside GameAI Sub, remove all this code:It is inside this If:I mean the most inside scope, the last one. Now we can start adding our AI. You'll probably have something like this:Now, right after add this:Don't forget to declare CloseToPlayer at GameAI headers, like this: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: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]](http://br.geocities.com/black_ages/MS/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 variablesWe also need to initialize this 3 vectors, so go back to InitServer and underAdd this: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:Now inside that If(If not close to player then) add this: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: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: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: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
Much less processing. For this, lets at the top of our AI system code, and take a look at this part of the code :Replace the: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
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)
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
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
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
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.
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
Under
Code:
Call SpawnAllMapNpcs
Code:
NV = (MAX_MAPX + 1) * (MAX_MAPY + 1) - 1
ReDim Mat(0 To NV, 0 To NV) As Byte
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
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
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
Code:
DidWalk = False
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
Code:
Dim CloseToPlayer As Boolean
Now add an If to make sure the npc is not close to the player, like this:
Code:
If Not CloseToPlayer Then
End If
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
Code:
ReDim Mat(0 To NV, 0 To NV) As Byte
Code:
ReDim Marked(0 To NV) As Byte
ReDim PathLine(0 To NV) As Integer
ReDim Path(0 To NV) As Integer
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
Code:
o = MapNpc(Y, X).Y * (MAX_MAPX + 1) + MapNpc(Y, X).X
d = GetPlayerY(Target) * (MAX_MAPX + 1) + GetPlayerX(Target)
Code:
For i = 0 To NV
Marked(i) = 0
PathLine(i) = -1
Path(i) = -1
Next i
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
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

Code:
For J = 0 To NV
Mat(J, i) = 0
Mat(i, J) = 0
Next J
Code:
Mat(J, i) = 0
Mat(i, J) = 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