worldgen

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2025 License: MulanPSL-2.0 Imports: 3 Imported by: 0

README

Dynamic World Generation Library

A highly configurable, deterministic 2D world generation library for Go, focused on seamless chunk-based terrain generation with difficulty scaling and border consistency.

Features

  • Deterministic Generation: Same seed + coordinates = identical output
  • Chunk-based Architecture: 32×32 tile chunks for infinite worlds
  • Difficulty Scaling: Adjust terrain danger levels (0.0-1.0)
  • Seamless Transitions: Similarity coefficient controls border consistency
  • Minimal Dependencies: Pure Go with Perlin noise support
  • Thread-safe: Safe for concurrent chunk generation

Installation

go get gitee.com/sweetiron/world-gen

Quick Start

package main

import (
	"fmt"
	"log"
	
	"gitee.com/sweetiron/world-gen"
)

func main() {
	// Initialize generator with seed
	seed := int64(42)
	generator := worldgen.NewNoiseGenerator(seed)
	
	// Generate a chunk
	coord := worldgen.ChunkCoord{X: 0, Y: 0}
	difficulty := 0.7  // Medium-hard difficulty
	similarity := 0.8  // High border consistency
	
	chunk, err := generator.GenerateChunk(seed, coord, difficulty, similarity, nil)
	if err != nil {
		log.Fatal(err)
	}
	
	// Print first few tiles
	for y := 0; y < 5; y++ {
		for x := 0; x < 5; x++ {
			fmt.Printf("%s ", worldgen.TileTypeString(chunk[y][x]))
		}
		fmt.Println()
	}
}

Core Concepts

Chunk Coordinates

Chunks are identified by ChunkCoord{X, Y} coordinates. Each chunk contains 32×32 tiles.

Tile Types
  • TileTypeEmpty - Empty space
  • TileTypeGrass - Grassy plains
  • TileTypeWater - Water bodies
  • TileTypeForest - Dense forests
  • TileTypeMountain - Mountainous terrain
  • TileTypeDesert - Sandy deserts
  • TileTypeLava - Dangerous lava
Parameters
  • Seed (int64): Ensures reproducible randomness
  • Difficulty (float64 0.0-1.0): Controls terrain danger
    • 0.0: Safe (grass, forest, water)
    • 1.0: Dangerous (lava, mountains, desert)
  • Similarity (float64 0.0-1.0): Border consistency with neighbors
    • 0.0: No blending
    • 1.0: Perfect seamless transitions

Advanced Usage

With Neighbor Blending
// Create chunk cache
chunkCache := make(map[worldgen.ChunkCoord]worldgen.ChunkData)

// Function to query neighbors
getNeighbor := func(coord worldgen.ChunkCoord) (worldgen.ChunkData, error) {
	if data, exists := chunkCache[coord]; exists {
		return data, nil
	}
	return nil, nil
}

// Generate chunk with neighbors
coord := worldgen.ChunkCoord{X: 1, Y: 1}
neighbors := make(map[worldgen.ChunkCoord]worldgen.ChunkData)

// Query adjacent chunks
for _, offset := range []worldgen.ChunkCoord{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} {
	neighborCoord := worldgen.ChunkCoord{X: coord.X + offset.X, Y: coord.Y + offset.Y}
	if neighborData, err := getNeighbor(neighborCoord); err == nil && neighborData != nil {
		neighbors[neighborCoord] = neighborData
	}
}

chunk, err := generator.GenerateChunk(seed, coord, difficulty, similarity, neighbors)
Custom Generators

Implement the Generator interface:

type CustomGenerator struct {
	seed int64
}

func (g *CustomGenerator) GenerateChunk(seed int64, coord worldgen.ChunkCoord, 
	difficulty, similarity float64, neighbors map[worldgen.ChunkCoord]worldgen.ChunkData) (worldgen.ChunkData, error) {
	// Your custom generation logic
}

Examples

Check the example/ directory for:

  • Basic generation demo
  • Difficulty scaling examples
  • Similarity blending demonstrations

API Reference

Types
  • ChunkCoord: {X, Y int} - Chunk coordinates
  • TileType: int - Terrain type enumeration
  • ChunkData: [][]TileType - 32×32 tile data
  • Generator: Core generation interface
  • NeighborProvider: func(ChunkCoord) (ChunkData, error) - Neighbor query function
Constants
  • ChunkSize = 32 - Chunk dimensions
  • DefaultDifficulty = 0.5 - Medium difficulty
  • DefaultSimilarity = 0.8 - High similarity
Functions
  • NewNoiseGenerator(seed int64) Generator - Create noise-based generator
  • TileTypeString(TileType) string - Get human-readable tile name

Performance

  • Single chunk generation: ~100μs (typical)
  • Memory efficient: ~4KB per chunk (32×32 × 4 bytes)
  • Thread-safe for concurrent generation

Contributing

  1. Follow Go coding standards
  2. Add comprehensive tests
  3. Maintain deterministic behavior
  4. Ensure thread safety
  5. Document public APIs

License

Mulan Permissive Software License v2 (MulanPSL-2.0) - see LICENSE file for details.

Documentation

Overview

Package worldgen provides a dynamic world generation library for 2D games. It offers configurable, deterministic terrain generation with support for difficulty scaling and seamless chunk transitions.

Index

Constants

View Source
const (
	TileTypeEmpty    = worldgen.TileTypeEmpty
	TileTypeGrass    = worldgen.TileTypeGrass
	TileTypeWater    = worldgen.TileTypeWater
	TileTypeForest   = worldgen.TileTypeForest
	TileTypeMountain = worldgen.TileTypeMountain
	TileTypeDesert   = worldgen.TileTypeDesert
	TileTypeLava     = worldgen.TileTypeLava

	ChunkSize         = worldgen.ChunkSize
	DefaultDifficulty = worldgen.DefaultDifficulty
	DefaultSimilarity = worldgen.DefaultSimilarity
)

Constants for tile types and default values.

Variables

This section is empty.

Functions

func TileTypeString

func TileTypeString(t TileType) string

TileTypeString returns a human-readable representation of the TileType.

Types

type ChunkCoord

type ChunkCoord = worldgen.ChunkCoord

ChunkCoord represents the unique coordinates of a map chunk.

type ChunkData

type ChunkData = worldgen.ChunkData

ChunkData represents the tile data for a single chunk.

type Generator

type Generator = worldgen.Generator

Generator is the core interface for world generation algorithms.

func NewNoiseGenerator

func NewNoiseGenerator(seed int64) Generator

NewNoiseGenerator creates a new noise-based world generator with the given seed.

type NeighborProvider

type NeighborProvider = worldgen.NeighborProvider

NeighborProvider is a function type for querying neighboring chunk data.

type NoiseGenerator

type NoiseGenerator struct {
	// contains filtered or unexported fields
}

NoiseGenerator implements worldgen.Generator using Perlin noise.

func (*NoiseGenerator) GenerateChunk

func (g *NoiseGenerator) GenerateChunk(coord ChunkCoord,
	difficulty, similarity float64, neighbors map[ChunkCoord]ChunkData) (ChunkData, error)

GenerateChunk generates a chunk of terrain using Perlin noise.

type TileType

type TileType = worldgen.TileType

TileType represents the type of a single tile on the map.

Directories

Path Synopsis
example
basic command
Example program demonstrating the world generation library.
Example program demonstrating the world generation library.
difficulty command
Example demonstrating difficulty-based terrain generation.
Example demonstrating difficulty-based terrain generation.
similarity command
Example demonstrating similarity-based edge blending.
Example demonstrating similarity-based edge blending.
internal
worldgen
Package worldgen provides core types and interfaces for dynamic world generation.
Package worldgen provides core types and interfaces for dynamic world generation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL