Articles > Version 0.15.0

Version 0.15.0

Version 0.15.0's image

Since my last post I worked on the AI pathfinding improvement. Up until now, the AI only used basic logic to move around. For example, in the patrol/wander state the AI agent would move in a random direction for a random amount of time or it would stop if it was blocked by an obstacle.


When chasing an enemy, the agent would simply move in a straight line toward it, ignoring all the obstacles.


The lack of environment knowledge for the agents made their movements feel dumb at times and also limited their gameplay possibilities. This is why I decided to use a proper path finding technique.



Since the maps on my game are tiles based, I decided to take advantage of this property and I created a navigation layer on all of them. This layer contains tiles for the walkable area only. Once the map is loaded, I create a graph representation of the walkable area using the data of the navigation layer. The agents can then map themselves on this graph to navigate in the level:

Each blue tile represent a graph node in the pathfinding system.


There is a lot of literature regarding pathfinding and I use one of the most common implementation (grid based A star) so I won’t talk about the implementation details here.

Instead, I will talk about the navigation strategy I implemented so far.



The wander strategy:

Using this strategy, the agent simply pick a random tile within its patrol area as its next destination. Then it compute a path from its current tile to the destination tile using the navigation graph. Once it got the tiles path, it transform it into a series of path points toward which it will navigate. On each tick the agent tries to adjust its movement input to reach the current path point goal and then process the next one until it runs out of path point.



The pursue strategy:

Using this strategy, the agent compute a path from its current tile to the player tile. Then in like manner as the wander strategy, it convert the tile path into path points and move through them. The issue with this strategy is that sometime the player can move on an area without a navigation layer like the corners around an obstacle:








In that case the agent keep its current computed path, if it has any, and then go in a straight line toward the player.

At that point lets say the player dies, now our agent can no longer use the navigation graph since it is no longer over the navigation layer. To fix this issue I kept it simple stupid, in such case, the agent pick a random point within its patrol area and tries to move there in a straight line, with a mechanism that will reset this process if it get stuck. Given a certain number of attempt (usually 1) the agent will find itself back on the navigation layer and will continue to navigate as expected.



The flee strategy:
Using this strategy, the agent first gather all the tiles he can go to. Then it exclude all the tiles that are within a certain range of the player it want to flee from. Next, it picks a random tile from the remaining as its new destination. Finally in the same fashion as the previous strategies, the agent compute the tile path, convert it into path point and follow them.



The pathfinding system also allowed me to have a better control on the monster location within a map. I implemented a reset to patrol area mechanism that will be triggered when a player tries to kite a monster too far from its spawn point. During the reset to spawn phase, the entity will regenerate all its life and become invulnerable (indicated by a particle effect). This make sure that some area within a map are free from any monster and safe for new players.



There are certainly some improvement that can be made on this pathfinding system such as limiting the maximal distance for a search, limiting the search time per frame, etc. But for now that will do it for me.




Published on: 21/08/2021 in News