My map has amorphous “territory” classes. Each territory has a list of territories which border it. These territories are using a combination of voronoi noise and other factors, to make them appear “natural”. In other words, it is not hexes or grids or predictable how many territories border each other, what shape they are, etc…
I want to make a list of lists for each territory, where list[0] of the lists is the first territories that border the territory, list[1] of the lists is the territories that border the territories of list[0] of the lists etc.
I am trying to make a recursive formula of some type, and it seems the pattern is almost there, bit I just cant quite reach it.
Right now, I am brute forcing it, with an i loop for first layer, j loop in that, k loop in that, l loop in that etc… …but Id rather have a number parameter, that tells it how many times/layers of territories it wants to include in the list of lists.
I am on phone, so no code, sorry. Dont bother if you would need to see code. (I mean that out of respect for your time).
In general you can write a recursive method which goes through the list of territories and recursively visit all the neighbors and then all the neighbors’ neighbors. You can make a counter how deep you’re in the recursion and then you can decide if you want to go deeper.
Then you can put the reference to the visited neighbors into a Set (if you don’t want later process items multiple times).
Ok after reading your post again I finally spotted what you are asking.
What you need to do is run BFS Breadth-first search - Wikipedia from each country. As a result of single BFS you will find out distance (measured in nodes/edges) from single country to every other country. Afterwards you can place it in whatever structure is more convenient for you.
Alternatively if you want to find out distance for all pairs of countries you could run Floyd-Warshal algorithm once https://en.wikipedia.org/wiki/Floyd–Warshall_algorithm . The code is very simple compared to other path finding algorithms. If you are interested only in some of the distances it’s not very efficient so you wouldn’t want to use it as general purpose shortest path search, but if you really need to calculate all distances it’s the best thing possible.
Floyd-Worshall might solve another issue I was having with AStar not quite doing the trick in some cases, (politically restricted territories in an rts essentially requires a different grid map for every “civilization”, but if i can do a kind of rough pathfind first…)