Random Map Generator
The parts of the algorithm are following:
- Random number generator
- Shape generator
- Terrain generator
- Generator of resources and non-walk textures (mountains, water, etc.)
- Functions correcting shapes to secure smooth transitions of textures
- Texture generator
- Height generator
- Objects generator
1. Random number generator
RMG have it's own random number generator. This secures that scripts which use random numbers will be independent on the Seed. There is used simple modification of xorshift32 which generates only positive numbers (RMG doesn't need a good correlation).
2. Shape generator
RMG uses two types of shape generators:
- Linear interpolation of grid with random values - from those values are chosen highest and lowest values and it makes the shape of specific texture.
Example of shapes made by linear interpolation:
- Voronoi diagram - for non-walk textures and resources.
Example of shapes made by Voronoi diagram:
Terrain generator uses shapes from linear interpolation to construct various biomes. RMG works with multiple layers (with different size of grid) of this procedure to create nice terrain.
Example of multiple layers which makes various terrain:
Balanced resources, mountains or water are created from Voronoi diagram. I use this method because it prevents overwriting already existing tiles and gives quite nice shapes.
Example of generated resources from Voronoi diagram:
Example of multiple sets of resources with different Seed:
This is probably the hardest part. Textures in KaM have max 2 transitions (for example water <-> grass). However, in real case is possible to meet 4 different biomes in 1 tile. Even in maps made by best map makers you can find some bugs (non-smooth transitions). RMG must scan the neighborhood of each tile and check for smooth transition.
One of possible problems with transitions of textures (left side = generated shape, right side = created "smooth" transition):
Map before (top image) and after (down image) CA:
Example of 2 sides transition - map editor will never draw smooth transition here ... RMG does:
The example of tile decomposition with smooth transition:
The example of tile decomposition with 3 tile transition:
This function converts numbers which represent biomes into tiles with right direction and with nice variation.
Height generator use linear interpolation to calculate the height of each tile. The formula is following:
- Code:
height = value of linear interpolation array (sum of 2-3 layers of LI) + minimal height of specific biome (mountains have more, water less etc.) + variance of specific biome (random number in specific interval - mountains have huge, water nothing etc.) - smooth tile coefficient (if there are 3-4 different tiles in neighborhood then decrease height and hide ugly transition)
There are 2 ways how to create objects:
- Clumped objects - for example forests -> group of trees around randomly generated point. The type of tree corresponds with the type of biome (you will not find a conifer in a desert and a palm in a snow).
- Random objects - each x. tile is random object which is picked by biome where he is (each biome have it's own set of objects which can be used)
More examples of completed maps (note: there was not used mountain fix to secure that all resources can be mined)