Search code examples
randomdata-structuresgame-developmentprocedural-generationhexagonal-tiles

Need an algorithm to randomly generate groups on a hexagon grid


I created a hexagon grid based on information in this article https://www.redblobgames.com/grids/hexagons/

Here's how it looks so far: Grid of hexagons

I want to divide this grid into groups similar to this: Hex grid with groups

I want to have gaps in the grid but I also need all groups to be connected. Essentially I want to be able to move from any hexagon in a group to any other hexagon in a group.

My first idea is to pick a random tile for each group then add a neighboring tile to each group until a group touches another group, then maybe have a decreasing chance of adding another tile to that group. My main concern with this is that I could end up with 2 groups that touch each other but are not connected to the rest of the groups.

Any ideas are appreciated. Let me know if you need any more information.


Solution

  • My solution to this was to first remove edge tiles. I did that by creating a loop that randomly selects a tile adjacent to the edge and removes it. This is repeated a random amount of times. I do this several times then remove groups of inner tiles. I first select a random tile that is not adjacent to an edge and put it in a group, select one of its neighbors that is not adjacent to an edge and repeat a random amount of times. After that is complete I get some decent looking maps.

    The only other thing I had to account for was that sometimes "islands" would be created meaning 1 or a few tiles are not connected to the rest of the map. I solved this by creating a function to creates groups of adjacent tiles and delete all but the largest group.

    Here is an example: enter image description here