# Provinces structure (irregular, RISK like 2D map)

This topic is 2106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm making a strategy game and the map is made out of irregular areas (provinces), like for example in Europe Universalis or RISK. Since I was always doing traditional 2D grid map I wanted to talk about it so I can grasp the concept more.

At the moment I made provinces (areas) as 1D list. Then I added neighbour variables to each province (max 6 neighbours).

Tell me if that's the "correct" way of doing this.

Simplified code:

class CRegion
{
public:
int neighbor[6];
} Region[256];

Note:

- the performance is almost irrelevant, the total map is very small and the game is turn based

Edited by Acharis

##### Share on other sites

That should work fine.  You have an easy way to iterate over every region (just a simple for loop), and you have a way to find the neighbors of a given region.  What other operations might you want to do on your regions?  That might alter your choice in structures.

##### Share on other sites

This may be way beyond what you are looking to do at the moment, but you might want to look at this article on generating nice-looking irregular maps.  It uses Voronai diagrams, which are pretty complicated (I've been thinking about it and trying to correctly implement Fortune's Algorithm for quite a while now...) but very cool.

##### Share on other sites

Looks like you've got the basics of an adjacency list there.  I would personally use some form of dynamic container instead of a static array.  Vector would probably be ideal since it should never change once everything is loaded.  And from my perspective it provides a more intuitive/usable interface to your list of neighbors.  eg. say you have 3 neighbors.  How do you know you only have 3 neighbors with a static array.  What data is contained in the other 3 elements of the array?

The other option would be an adjacency matrix (you're really conceptually dealing with a graph structure here).  Since you've said your total map is very small, the memory overhead of a matrix should be small.  On the other hand, the main advantage of a matrix over a list is faster lookups of adjacency information and you said performance is almost irrelevant.

All that being said, you're probably on the right track already for your purposes.  I'd still just recommend using dynamic containers though.

##### Share on other sites

An adjacency list/graph would work. What I might recommend you do is store all the adjacency info in a single flat array/vector, end-to-end. Then, in the list of provinces, store a pointer to the first adjacency that corresponds to the province. From that, you have an iterator pair (using the next province's first adjacency as the .end() iterator) that defines all the adjacency of each province (you'll need to add one last "dummy" province for the end, or special-case the last province somehow). This ought to give relatively good performance for both iterative and random access to the information.

If memory space were a concern and the number of provinces is relatively low, you could also do some tricks with prime numbers.

##### Share on other sites

Looks like you've got the basics of an adjacency list there.  I would personally use some form of dynamic container instead of a static array.  Vector would probably be ideal since it should never change once everything is loaded.  And from my perspective it provides a more intuitive/usable interface to your list of neighbors.  eg. say you have 3 neighbors.  How do you know you only have 3 neighbors with a static array.  What data is contained in the other 3 elements of the array?

They are storing indices, so one could just stick -1s in there for invalid entries.  (and/or they could keep a count variable)

A vector list could be used (though why, unless regions actually change?), or a dynamic array, but it's probably overkill for a non-issue.  I mean, worst case scenario, where all the regions are in a straight line and only have a left/right neighbor, thats wasting 256 * 4 presumably 32-bit integers, and really, they could be using bytes if they really wanted to save memory, since they have a hardcoded 256 region limit.  So, basically 1MB could be lost, but only in worst case scenarios, and thats really not all that much.

Edited by ferrous

##### Share on other sites

That should work fine. You have an easy way to iterate over every region (just a simple for loop), and you have a way to find the neighbors of a given region. What other operations might you want to do on your regions? That might alter your choice in structures.
I'm not sure... that's why I ask people who did these things in the past.

Most likely I will need:

- the distance from the capital (distance in number of provinces, not hexes)

- pathfinding

I would personally use some form of dynamic container instead of a static array. Vector would probably be ideal since it should never change once everything is loaded. And from my perspective it provides a more intuitive/usable interface to your list of neighbors. eg. say you have 3 neighbors. How do you know you only have 3 neighbors with a static array. What data is contained in the other 3 elements of the array?
All empty neighbours variables hold "0" (there will be no province numbered 0). I was thinking that would be the easiest way of implementing it.

##### Share on other sites

Looks like you've got the basics of an adjacency list there.  I would personally use some form of dynamic container instead of a static array.  Vector would probably be ideal since it should never change once everything is loaded.  And from my perspective it provides a more intuitive/usable interface to your list of neighbors.  eg. say you have 3 neighbors.  How do you know you only have 3 neighbors with a static array.  What data is contained in the other 3 elements of the array?

They are storing indices, so one could just stick -1s in there for invalid entries.  (and/or they could keep a count variable)

A vector list could be used (though why, unless regions actually change?), or a dynamic array, but it's probably overkill for a non-issue.  I mean, worst case scenario, where all the regions are in a straight line and only have a left/right neighbor, thats wasting 256 * 4 presumably 32-bit integers, and really, they could be using bytes if they really wanted to save memory, since they have a hardcoded 256 region limit.  So, basically 1MB could be lost, but only in worst case scenarios, and thats really not all that much.

I wasn't even looking at it from a memory usage perspective, but a code readability and maintainability perspective (which are really priority one in my opinion).

In the static case, you are iterating over all the elements of the container that are NOT a specific, arbitrary value.

In the dynamic case, you are iterating over all the elements of the container.

On a side note, I wouldn't even assume that a vector implementation would use less memory, as it's entirely possible that the average capacity of each vector is as large as, or even larger than, the static array.

##### Share on other sites

Looks like you've got the basics of an adjacency list there.  I would personally use some form of dynamic container instead of a static array.  Vector would probably be ideal since it should never change once everything is loaded.  And from my perspective it provides a more intuitive/usable interface to your list of neighbors.  eg. say you have 3 neighbors.  How do you know you only have 3 neighbors with a static array.  What data is contained in the other 3 elements of the array?

They are storing indices, so one could just stick -1s in there for invalid entries.  (and/or they could keep a count variable)

A vector list could be used (though why, unless regions actually change?), or a dynamic array, but it's probably overkill for a non-issue.  I mean, worst case scenario, where all the regions are in a straight line and only have a left/right neighbor, thats wasting 256 * 4 presumably 32-bit integers, and really, they could be using bytes if they really wanted to save memory, since they have a hardcoded 256 region limit.  So, basically 1MB could be lost, but only in worst case scenarios, and thats really not all that much.

I wasn't even looking at it from a memory usage perspective, but a code readability and maintainability perspective (which are really priority one in my opinion).

In the static case, you are iterating over all the elements of the container that are NOT a specific, arbitrary value.

In the dynamic case, you are iterating over all the elements of the container.

On a side note, I wouldn't even assume that a vector implementation would use less memory, as it's entirely possible that the average capacity of each vector is as large as, or even larger than, the static array.

Eh, it's down to personal taste, pretty much.  I personally dislike Vector, only because it's so much messier than the C# equivalent.  =)

That said, the vector version wouldn't waste that much memory, assuming the implementor either sets the reserve size or shrinks to fit during the construction of the regions.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 14
• 30
• 9
• 16
• 22