Skip to content
David Cumps
Game Programming - AI title slide

Game Programming: AI pathfinding

  • Microsoft Student Council, Hogeschool Antwerpen
  • in Dutch

A session in Dutch on pathfinding, the base of game AI, for the Microsoft Student Council meeting of 11 February 2005 at Hogeschool Antwerpen, a day for students themed Digital Entertainment. Running that day was also my first task as an intern at Microsoft: the speakers, the schedule, and welcoming and closing the meeting. The session builds an A* pathfinder step by step, then makes it fast with a binary heap. The inspiration came from Patrick Lester’s A* tutorial.

Why pathfinding. Finding the way is the base of game AI, and it has to be as short, as efficient and as fast as possible, because other parts build on it: calculating distances, navigation and avoiding obstacles.

Finding the shortest path. On a grid from point A to point B, with obstacles in between, A* keeps an open list of squares still to check and a closed list of squares already checked, and scores every square: F is the total, the cost so far (G) plus an estimate of the cost to go (H). Each round takes the square with the lowest F from the open list and closes it, then looks at its neighbours: obstacles and closed squares are ignored, new ones join the open list with their scores, and ones already on it get a new parent if this route reaches them more cheaply. The session walked through the first iterations by hand, where every square scoring 6 is checked before any scoring 8, until the goal lands on the open list and the path is traced back through each square’s parent.

A faster open list. Searching the open list for the lowest score is the bottleneck. A binary heap keeps the lowest item on top and fits in a plain array, with the top at index 1 and the children of item x at 2x and 2x+1. Adding an item puts it at the end and lets it bubble up, swapping with its parent while it’s smaller. Removing the top moves the last item to the top and lets it sink, swapping with its smallest child while it’s bigger. On a 100 by 100 map with a thousand nodes on the open list, keeping a sorted list takes around 500 operations per insert, where the heap needs one to three to add an item and about nine to remove one. To fit the pathfinder, the heap holds indexes, and the scores and parents live in separate arrays per square.

Where it came from. The pathfinder started in my entry for the Visual Gaming category of Microsoft’s Imagine Cup, where you write the AI for robots in a small game. Every turn had to finish inside a fixed time window, which is why the pathfinder got a binary heap and avoids boxing, keeps two-dimensional grids in one-dimensional arrays, and creates as few objects as possible.

The demo. PathViewer opens a map, either a bitmap or a Visual Gaming map file, and shows the pathfinder at work, colouring the squares on the open and closed lists as it searches. The source has three parts: the pathfinder with its binary heap, behind a small map interface; a typed list of points for the path; and the viewer itself, with maps from the competition.

The two posts behind it: A* algorithm and Binary heaps.

Technologies: C#, .NET Framework, Windows Forms, A*, binary heaps, SharpZipLib.

Given at

  • February 11, 2005: Microsoft Student Council, Hogeschool Antwerpen

Files

You May Also Like