Skip to content

WorldGeneration

Oxillen Glow edited this page Aug 25, 2026 · 3 revisions

Code at: https://github.com/OxillenGlow/MtSharpGrain/blob/main/src/main/resources/chunkgen.js

World Generation

Mtsharpgrain uses a deterministic, procedural world generation system to create its terrain. Every chunk is generated using a combination of mathematical functions and pseudo-randomness, ensuring consistency across the world and between game sessions.


Overview

The world is divided into 16x16x16 chunks, and each chunk is generated independently using a seed value. The generation pipeline combines multiple layers of terrain features, including:

  • Base heightmap (rolling sine/cosine waves)
  • Sparse terrain features (mountains and slashes)
  • Polar ice caps (linear cone-shaped ice layers)
  • Subsurface layers (dirt/rock gradient and underground ores)
  • Surface decorations (stray dirt/rock nubs)
  • Chunk-level templates (rare storage structures)
  • Spherical "ball" structures (glass shell with grass interior)

Generation Pipeline

1. Deterministic Hash Function (hash2)

  • A pure function that takes (ix, iz, seed) and returns a pseudo-random value in [0, 1).
  • Used as the single source of randomness for all other functions, ensuring 100% order-independent and reproducible generation.

2. Base Heightmap (baseHeight)

  • A combination of three stacked sine/cosine waves at different frequencies and amplitudes.
  • Creates a rolling, Mars-like dune terrain with a height range of roughly ±10 blocks.
  • Formula:
    h += 6   * Math.sin(wx * 0.013 + seed)        * Math.cos(wz * 0.011 - seed);
    h += 3   * Math.sin(wx * 0.041 - seed * 0.7)  * Math.cos(wz * 0.037 + seed * 0.7);
    h += 1.2 * Math.sin(wx * 0.097 + seed * 1.3)  * Math.cos(wz * 0.083 - seed * 1.3);

3. Sparse Terrain Features (terrainFeatureDelta)

  • Mountains: Cone-shaped peaks with a radius of 40–150 blocks and a height of 20–80 blocks.
  • Slashes: Capsule-shaped pits with a length of 200–1000 blocks, width of 20–60 blocks, and depth of 15–50 blocks.
  • Features are spawned in ~40% of cells (20% mountains, 20% slashes).
  • Features are placed on a coarse grid with a cell size of 600 blocks and a maximum reach of 600 blocks.

4. Polar Ice Caps (iceThickness)

  • A linear cone centered at the world origin (0, 0).
  • Full thickness (10 blocks) at the origin, tapering to 0 at a radius of 10,000 blocks.
  • Ice layers are raised by 1.5 blocks where they exist.

5. Subsurface Layers

Dirt/Rock Gradient (subsurfaceBlock)

  • The top 4 layers below the surface use a dirt/rock gradient based on depth:
    • 99% dirt at the topmost layer.
    • 80% dirt at the second layer.
    • 60% dirt at the third layer.
    • 10% dirt at the fourth layer.
  • Below this, the terrain is pure rock.

Underground Materials (pickUndergroundBlock)

  • Stone is the default underground block.
  • Crystal Ore (block ID 5) has a chance of spawning that increases with depth:
    • Base chance: 1%
    • Increases by 0.06% per block below the surface (capped at 10%).

6. Surface Decorations

  • 2% chance of a stray dirt nub (block ID 3).
  • 1% chance of a stray rock nub (block ID 2).
  • 5% chance of an extra dirt block on top of the surface (only on dirt, not ice).

7. Chunk-Level Templates

  • All-Air Chunks: If a chunk is entirely above the surface, there is a 0.5% chance of replacing it with a storage template (storageAir).
  • All-Underground Chunks: If a chunk is entirely below the surface, there is a 10% chance of replacing it with a storage template (storageGround).

8. Spherical "Ball" Structures

  • 5% of chunks that contain both air and ground will generate a spherical structure.
  • Radius: 5–10 blocks.
  • Composition:
    • Inner region: Dirt is replaced with grass (block ID 4).
    • Outer shell (2-block thick): Glass (block ID 10).
  • Placement: Centered on the surface boundary (between air and ground).

Block IDs

ID Block Type
0 Air
2 Rock
3 Dirt
4 Grass
5 Crystal Ore
6 Ice
7 Not naturally spawned
8 Not naturally spawned
10 Glass

Customization Tips

  • Adjust the amplitudes and frequencies in baseHeight to change the rolling terrain shape.
  • Modify the feature spawn rates in featureAt to control the density of mountains and slashes.
  • Tweak the ice cap radius and thickness in iceThickness to change polar regions.
  • Change the dirt/rock gradient in DIRT_CHANCE_BY_DEPTH to alter subsurface layers.
  • Adjust the ore spawn chance in pickUndergroundBlock to control resource distribution.

Performance Notes

  • The generation is fully deterministic, meaning the same seed will always produce the same world.
  • Chunks are generated independently, allowing for infinite world expansion without pre-generation.
  • The use of hash2 ensures no reliance on Math.random(), making the system thread-safe and reproducible.

Image MtSharpGrain


(don't worry ANYONE can make their own mod as modding is super easy especially with AI. You can make a mini game mod of whatever fun idea you had!)

jVisualScripting (still in construction 🚧)

To download:

Go to Downloads scroll down to bottom of the release card and click the assets to find the build for your platform. For more see install page

Clone this wiki locally