This is done using helper classes like PathFollower and most of the path construction is done internally.
<!-- Maybe add examples here later -->
Below you will find examples on how to have complete control over the path generation and so that it is not limited to NextBots only.
- navmesh - The navigation mesh.
- agent - The agent will use the generated path, for example a player bot or an NPC.
- path - The generated path, a set of nodes the agent must travel through to get to the goal
- node - Points which are connected with each other that signify the positions that can be reached
The CNavArea provides you with methods which allow you to quickly set up a completely custom A* pathfinding algorithm for your own purposes, such as pathfinding for player bots.
These methods are:
All of these are basic functions that are using in A* pathfinding algorithm.
Here's an example on how to use these methods to implement a basic A* algorithm from wikipedia:
The Astar() function will return one of the 3 values:
- If it is physically impossible to get to the goal CNavArea, it returns false
- If the start CNavArea is the same as the goal CNavArea, it returns true
- Otherwise it will return an array of CNavAreas which the bot/NPC has to travel through (one after another, the goal CNavArea being the very first entry in the array, use table.Reverse to reverse the array order if necessary) to get to the goal.
Below you will find a few examples on how to use this example function to move player bots using pathfinding to a certain target.
Here's an example function that takes 2 vectors are input for Astar() function:
In Garry's Mod, the navigation mesh is made of CNavAreas, not Vector positions. As a result, you will notice that examples here move the agent mostly from a center of one CNavArea to the center of another. This makes the generated paths look unnatural.
In order to achieve more natural paths, more advanced techniques will be needed when building positions the agent will need to traverse through, such as using the corners of CNavAreas, using Line of Sight checks to cut corners or discard certain positions, etc.
Please keep in mind that all of these examples are kept as simple as possible for educational purposes. It may not generate the absolutely most efficient paths. More work will be necessary to be put into the agents movement code for the paths to feel natural or be efficient.
This example code contains a function that will draw the generated path, directly from the Astar() function using the debugoverlay.
Please keep in mind that the debugoverlay only works in multiplayer and only when the developer console command is set to a non zero value.
This example shows how you can use the Astar() and drawThePath() functions together to test and debug your path generation.
This example shows the most basic way to move a player bot (an agent) alongside the generated path:
This example will make all bots on the server move towards the first player on the server. The path generated is not pretty, but the point of this example to show how to use the path to move an agent alongside a generated path.