A common task in game code is finding which of a stored set of points is nearest to another arbitrary point. For example, you might want to find which waypoint or monster generator is nearest to the player's current position. However, the search is time-consuming when there are a lot of stored points and the search is just looking through all of them to find the nearest. I've had a go at implementing a kd-tree, a data structure that speeds up the nearest-neighbour search dramatically (see the attached script file). It is currently a very basic implementation, but I'd be happy to hear suggestions for improvements of functionality or efficiency.