Range-finding

Range-finding is the process of finding all points within a certain range of a goal point. Here is a typical scenario that requires it: In a turned-based grid game, the player can only move a certain number of cells. You need to find out which cells the player can move to at each turn – to display it or prevent illegal moves. Range finding share many features with path finding (and you should examine that tutorial before reading this one). Let’s first look at the function definition:

The function returns a list of points, in the case above all points that are within 3 units of the start cell. Most arguments and steps are exactly the same as for path finding. Here is a summary of the steps:

1Implement a cell that supports the necessary functionality for the type of range-finding you want to do.
2Work out what types all your parameters should be. These are the same as for path finding, except for the range parameter (which can either be an int or float, depending on whether your distance metric uses ints or floats).
3Write a method or lambda expression to decide whether a cell is accessible or not.

4Write a lambda expression or method for determining the true cost of reaching two points if they are neighbors. (Since there is no heuristic cost involved with range finding, you need not worry about that). As with path finding, you get different results depending on the distance metric and your neighbor setup. Below is an example for some of the different options.