WebSim Python Projects: Procedural World Generation

Welcome to our cutting-edge project on Procedural World Generation! In this demonstration, we'll explore how to create vast, unique, and immersive worlds using advanced algorithms and Python programming.

Project Overview

Our Procedural World Generation system uses a combination of noise algorithms, cellular automata, and advanced terrain generation techniques to create diverse and realistic landscapes. The system can generate:

Key Components

1. Terrain Generation using Perlin Noise


import numpy as np
from noise import snoise2

def generate_terrain(width, height, scale=100.0, octaves=6, persistence=0.5, lacunarity=2.0):
    world = np.zeros((width, height))
    for i in range(width):
        for j in range(height):
            world[i][j] = snoise2(i/scale, 
                                  j/scale, 
                                  octaves=octaves, 
                                  persistence=persistence, 
                                  lacunarity=lacunarity)
    return world

2. Biome Assignment


def assign_biomes(world, moisture):
    biome_world = np.zeros_like(world, dtype=object)
    for i in range(world.shape[0]):
        for j in range(world.shape[1]):
            if world[i][j] < -0.2:
                biome_world[i][j] = 'OCEAN'
            elif world[i][j] < 0:
                biome_world[i][j] = 'BEACH'
            elif world[i][j] < 0.3:
                if moisture[i][j] < 0.3:
                    biome_world[i][j] = 'DESERT'
                elif moisture[i][j] < 0.6:
                    biome_world[i][j] = 'PLAINS'
                else:
                    biome_world[i][j] = 'FOREST'
            elif world[i][j] < 0.7:
                if moisture[i][j] < 0.4:
                    biome_world[i][j] = 'HILLS'
                else:
                    biome_world[i][j] = 'FOREST'
            else:
                if moisture[i][j] < 0.3:
                    biome_world[i][j] = 'MOUNTAINS'
                else:
                    biome_world[i][j] = 'SNOWY_MOUNTAINS'
    return biome_world

3. River Generation


def generate_rivers(world, num_rivers=5):
    rivers = []
    for _ in range(num_rivers):
        x, y = np.random.randint(0, world.shape[0]), np.random.randint(0, world.shape[1])
        river = [(x, y)]
        while world[x][y] > -0.2:  # Until we reach the ocean
            neighbors = [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]
            valid_neighbors = [(nx, ny) for nx, ny in neighbors 
                               if 0 <= nx < world.shape[0] and 0 <= ny < world.shape[1]]
            x, y = min(valid_neighbors, key=lambda coord: world[coord[0]][coord[1]])
            river.append((x, y))
        rivers.append(river)
    return rivers

4. Vegetation Placement


def place_vegetation(biome_world, density=0.1):
    vegetation = np.zeros_like(biome_world, dtype=object)
    for i in range(biome_world.shape[0]):
        for j in range(biome_world.shape[1]):
            if biome_world[i][j] == 'FOREST' and np.random.random() < density:
                vegetation[i][j] = 'TREE'
            elif biome_world[i][j] == 'PLAINS' and np.random.random() < density/2:
                vegetation[i][j] = 'GRASS'
    return vegetation

Putting It All Together


class WorldGenerator:
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def generate_world(self):
        # Generate base terrain
        self.terrain = generate_terrain(self.width, self.height)
        
        # Generate moisture map
        self.moisture = generate_terrain(self.width, self.height, scale=150.0)
        
        # Assign biomes
        self.biomes = assign_biomes(self.terrain, self.moisture)
        
        # Generate rivers
        self.rivers = generate_rivers(self.terrain)
        
        # Place vegetation
        self.vegetation = place_vegetation(self.biomes)

    def get_world_data(self):
        return {
            'terrain': self.terrain,
            'biomes': self.biomes,
            'rivers': self.rivers,
            'vegetation': self.vegetation
        }

Interactive World Generation Demo

Experience our procedural world generation system in action! Generate a unique world and explore its features.

Future Enhancements

We're constantly working to improve our Procedural World Generation system. Some planned enhancements include:

Explore More WebSim Python Projects