# Thread: Find nearest field.

1. ## Find nearest field.

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.
end;

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

My problem / Question:
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.

2. 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] for example)

3. 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!

4. 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.

5. You can put all your buildings/whatever in a list, then go through the items to find what you want.

6. Sounds like a distance transform to me.

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Comodo SSL