View Full Version : Find nearest field.

21-06-2011, 02:13 PM
Hi folks. :-[
I am making a game where you have to build buildings on a map, to gather resources. The map is a 2D-grid of squares. The squares can be different types, like Gras, Forest and water. Each square has an item in an 2D-array with all the information that is stored to that field. The game also has a computer-player, where im making the AI now.

The record with the fieldinfo:
TSquare = record
Typ: string; //What terrain the square is ('Wather', 'Forest', 'Mountain', 'Gras').
Building: TBuilding; //Information about the building.

TBuilding = record.
Typ: string; //What kind of building it is ('Farm', 'Mill', 'Bakery', 'Marketplace' etc.).
//Some other things here :P

My problem / Question: :P
Now i want to make is a function where you give in a string and a TPoint, where the string is the landscape which the procedure has to find the nearest from, and the TPoint is the field where the mainbuilding/storage-area is. Is there a simple way to do this? Are there any examples on how to do this in pascal?

I use lazarus V. 0.9.30 and windows XP.

Thanks for help. ;)

21-06-2011, 03:00 PM
If I understand you correctly then you're looking for a path finding algorithm. It's not a particularly easy topic, but there have been some threads on this, here on PGD. I'm pretty sure you'll be able to find some source code. too ([Here] (http://www.pascalgamedevelopment.com/showthread.php?719-A*-pathfinding-example-%21/page9&highlight=pathfinding) for example)

21-06-2011, 03:05 PM
Actually I just want to know which field is the nearest of one certain type, but i guess its some of the same. Ill look at the tread you posted. Thanks!

21-06-2011, 03:22 PM
you can go through the entire map field by field and check the distance to the point, this way finding the smallest distance and therefore the closest field.
another way would be a very simplified version of path finding where you trace starting from the initial point in all directions until you reach the field of the type you are looking for.
the first way is very easy to code. the second way is a bit more difficult but it can provide a path from the initial point to the field. let me know if you need some help with the code.

21-06-2011, 03:39 PM
You can put all your buildings/whatever in a list, then go through the items to find what you want.

26-09-2011, 01:17 PM
Sounds like a distance transform to me.

Do you want to know distances around objects or as the bird flies?