How to Randomize Map Generation - BriannaLadson/TerraForge GitHub Wiki

How to Randomize Map Generation

This tutorial shows you how to randomize TerraForge's noise settings to create a new, unique biome map every time the script runs.


Starter Code

Here is the starter code.

# Import TerraForge
from terraforge import TerraForge

# Noise Type Settings
elevation = {
	"seed": 0,
	"octaves": 10,
	"persistence": .5,
	"lacunarity": 2,
	"min_color": "#000000",
	"max_color": "#FFFFFF",
	"falloff": {
		"type": "radial",
		"strength": 0,
	},
	"zoom": 1,
}

noise_types = {
	"elevation": elevation,
}

# Initialize TerraForge While Passing in Settings
generator = TerraForge(noise_types=noise_types)

# Generate Map
generator.generate()

Generating Random Numbers

We'll be using the random module to randomly generate numbers, but we need to import it at the top of the file first.

# Import TerraForge
from terraforge import TerraForge

# Import random
import random

random.randint(min, max) randomly selects an integer in the range of min and max.

random.uniform(min, max) randomly selects a float in the range of min and max.


Updating the Noise Types

Now, we need to update the elevation noise type settings to use random numbers instead of hard-coded integers and floats.

# Noise Type Settings
elevation = {
	"seed": random.randint(-1000, 1000), # Can be any integer
	"octaves": random.randint(1, 10), # Can be any positive integer
	"persistence": random.uniform(.1, 1), # Can be 0 < float <= 1
	"lacunarity": random.uniform(1, 2), # Can be 0 < float
	"min_color": "#000000",
	"max_color": "#FFFFFF",
	"falloff": {
		"type": "radial",
		"strength": random.uniform(0, 1), # Can be 0 <= float <= 1
	},
	"zoom": random.uniform(.1, 1), # Can be 0 < float
}

Generating Random Colors

The random module can also be used to generate hex colors, if you also want to randomize the min_color and max_color.

First, create a function that returns a random hex. Add it before the elevation noise type dictionary.

def random_color():
	return f"#{random.randint(0, 0xFFFFFF):06x}"

Then replace the hex values for the min/max_color keys in the dictionary with random_color().

# Noise Type Settings
elevation = {
	"seed": random.randint(-1000, 1000), # Can be any integer
	"octaves": random.randint(1, 10), # Can be any positive integer
	"persistence": random.uniform(.1, 1), # Can be 0 < float <= 1
	"lacunarity": random.uniform(1, 2), # Can be 0 < float
	"min_color": random_color(),
	"max_color": random_color(),
	"falloff": {
		"type": "radial",
		"strength": random.uniform(0, 1), # Can be 0 <= float <= 1
	},
	"zoom": random.uniform(.1, 1), # Can be 0 < float
}

Examples

Now, you have a script that generates a random biome map every time you call it. Below, are some examples.

biome_map elevation_map

biome_map elevation_map

biome_map elevation_map