Pathfinding
Hello again,
Since my last post, I’ve been working on improving the AI. I’ve made some changes to the state machine. Namely, each state now has a substate that it can fully control and manipulate. This is useful for things like combat, where the NPC might want to be able to chase a player, and then start attacking said player when in range.
I’ve also implemented a pathfinding algorithm. Namely, A* (A-Star). The way it works is, it searches adjacent tiles, with some favoring while avoiding unwalkable tiles until the desired tile is reached. The algorithm uses two different lists of tiles in order to keep track of tiles that have been checked, and tiles that need to be checked. The inner-workings of the algorithm are as follows:
Calculate the F, G, and H costs for the source tile, and then add it to the open list. This list contains tiles that need to be checked. F = G+H H = The heuristic cost from the selected tile, to the destination tile. I'm using the euclidian distance as my heuristic. G = The movement cost from one tile, to another. Straight movement = 1, diagonal movement = sqrt(2)
Get the tile with the lowest F-cost from the open list, and add it to the closed list.
Check the tiles that are adjacent to the one just added to the closed list, and ignore any tiles that are either in the closed list, or unwalkable. If a tile is in the open list, recalculate the G-cost for said tile, and make the tile previously added to the closed list the parent of the tile in the open list. Obviously, recalculate the F-cost in the event of a parent swap.
repeat steps 2-3 until the destination tile is found.











