Fancy Mansion – Pathfinding

This week I have been working with implementing an AI for Mr. Fancy, the owner of the Fancy Mansion. Mr. Fancy has a couple of states he enters, one where he patrols a path (we haven’t decided how this will be implemented, but probably with path-finding as well), one where he hears the player and moves to the tile the player was on (here we use path-finding), and a couple of other states where he chases and shoots the player.

Last week we were given a lecture on how to implement path-finding, specifically the method A* (A-star). What the method basically does is check the adjacent tiles and calculate the shortest path to the goal, and then check this tile’s adjacent tiles and etc, until you reach the goal. Though, the way the example code we were given works with obstacles would not work in our game. In the example the obstacles take up one whole tile, thus removing it from the calculation. In our game, the walls are thinner and do not take up a whole tile. So at one point tiles that Mr. Fancy should not be able to walk to will be walk-able at another point. To make this work we, just after creating the level, check all tiles and if their adjacent tiles are reachable from that specific tile. This is done by casting lines from the middle of the current tile to the middle of the adjacent tiles and check if any of these lines collide with any of the walls. Then I save this in a bool array with the size of 8 (there are at max eight tiles around each tile) for each floor tile, where false is unreachable and true reachable. In the picture below we can see the lines being cast (yellow) and the tiles that are not reachable from this tile (red) and tiles that are reachable (green).

FancyMansion_Blogpost_4_printscreen_1

In the next two pictures we can see how tiles that are not reachable at one point, are reachable at another point.

FancyMansion_Blogpost_4_printscreen_2_2

FancyMansion_Blogpost_4_printscreen_3_2

So when we now want Mr. Fancy to find a path to a specific tile he should know where he should not walk. Though I have not fully implemented the actual path-finding and make him move to the specific tile. This is something I am working on right now and it will hopefully be implemented by the end of the week.

Next week we have the Beta showcase and hopefully we will be ready for it!

About Sven Almberg

2014  Programming