Unity, AI, Procedural Generation
January, 2023 - In development
Procedural generation of a hexagonal grid using the Wave Function Collapse algorithm. (Work in progress)
This project is currently in development. It is an AI simulation
where multiple agents must perform different tasks in a procedurally
generated hexagonal world. Multiple AI techniques have been used to
generate the world and bring to life the AI agents.
The repository of this project is private but temporary access may be
given to anyone interested in its implementation.
Contact me for more information.
For this demo I wanted to explore hexagonal grids, and how can they be implemented. I decided to have a grid generator as open as possible. Some of the features of this generator are:
Hexagonal grid formation
Rectangular grid formation
As for the Node, it contains its position, a list of all the paths
it shares with other Nodes and a reference to the tile (the
actual GameObject) it holds. On the other hand, a Path is a
connection between an in_node (the Node it belongs to),
an out_node (the neighbouring Node) and a cost (for pathfinding
purposes).
This implementation works as it is but, in its current state, it
is not very efficient. For instance, for each two adjacent Nodes,
there are two different Paths, from one Node to the other and
viceversa. This can be reducted two one Path and, when traversing,
take into consideration the current node you are in. Another possible
optimization could be to remove the Paths alltogether and use an
adjacency matrix with the cost to move from one node to others.
The Wave Function Collapse
algorithm is a generative algorithm that is bases on the restrictions
between adjacent pixels/tiles. It has been widely used to generate
2D images, tilemaps and even 3D models.
My implementation of the algorithm is a mix of all the sources I
found when researching about the algorithm adapted to the hexagonal
grid that I am using and the overall idea of the world I want to
generate.
The first iteration of the algorithm that I implemented had as input:
I want the cities generated to be connected between each others
as naturally as possible. At first, I tried to generate the
roads with the WFC algorithm, however, this added so much
complexity to the algorithm that the number of viable solutions
dropped significantly. Moreover, the roads generated were not
very natural, nor optimal.
Therefore, I decided to use an A* pathfinding algorithm to
generate the roads between the cities. It is now in its first
iteration and, for now, it only generates the optimal path
between every city. This aproximation, even if it achieves the
goal, does not take into account pre-existing roads. So you can
find in a situation where two roads are parallel for a great
distance to the same city, when they could have been merged.
I am currently working in a variation of the algorithm to resolve
this issue. I am testing some of these options when generating
a road from one city to another:
Current implementation of the pathfinder.