Under the hood, and then water!


What was worked on today.

  1. The world map is shared instead of cloned.

    • The macro map (128x128 cells 64 meters each) was being deep copied via clone() for every chunk dispatched to a worker thread. With up to 8 chunks generating per frame, it ended up resulting in alot of pointless copying.
    • MacroMap now lives behind an Arc and is frozen after generation. Threads clone the pointer, not the data. If the world regenerates mid-load, the threads keep the old map alive until they finish.
    • I deleted a #[derive(Clone)]from the MacroMap struct. If I or anyone else tries to do a deep copy of it again the compiler will refuses it.
    • The Chunk loader was also calling Vec::contains for every active chunk against 300 expected chunks every frame. Which ends up being around 90k comparisons. I swapped it for a HashSet.
  2. Real logging and timing with tracing.

    • I replaced env_logger with tracing and tracing-subscriber. The default filter is warn for everything, debug for my crate and the Vulkan loader module logging is turned off.
    • Spans wrap fixed_update, render and each generate_chunk call, which is tagged with its chunk coordinates. I have span close events on and every span prints its own time.busy and its at trace level so it costs nothing in normal game runs (Debug mode is separate and can lag the terminal).
    • I have been seeing Vulkan errors but they come from an implicit layer from a frame-generation tool I have installed, and its library didn’t export the symbol the loader expected so the loader skipped it and its not a real error.
  3. Deterministic RNG streams.

    • World generation had four separate sources of randomness, including a custom LCG for rivers and a seed +100/ seed + 200 offset for noise. The offsets cause issues because world 42’s moisture noise is world 142’s elevation data. A single shared generator is worse because if I added something new in one system it shifts every other world as well.

    • Now there is a Stream enum (Elevation, Moisture, Rivers, Forest, History)and so on each mixed with a fixed salt. Each streams seed is built from the world seed and its salt like this

      const GOLDEN: u64 = 0x9e3779b97f4a7c15;
      
      pub fn splitmix64(state: u64) -> u64 {
          let mut z = state.wrapping_add(GOLDEN);
          z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
          z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb);
          z ^ (z >> 31)
      }
      
      pub fn stream_seed(world_seed: u32, stream: Stream) -> u64 {
          splitmix64(splitmix64(world_seed as u64) ^ stream.salt())
      }
    • splitmix64 is a bijection, so two different salts can never produce the same stream for the same world seed. This guarantee is why the world seed gets hashed first then XORed with salt, and not simply added to it like I was doing before.

    • Salts are written out in a match not taken from the enum’s order. If someone reorders the variants every world would change and break saved games.

    • I wrote the generator myself instead of using rand. It’s about 10 lines behind a StreamRng type so I can swap it later without touching call sites.

  4. Seam check between near and far terrain.

    • The distant mesh discards pixels within 544 m of the player (10 chunks × 64 m, minus a 96 m margin). I worked out the worst case by hand. The nearest unloaded chunk can be about 572 m away, so there’s roughly 28 m of margin. I found no gaps in testing. The weak spot is that the discard doesn’t check whether chunks are actually ready, so a very fast flight could still show a hole.
  5. The current rivers are ugly and also a very basic implementation

    • The old river carver drops a particle that follows the terrains elevation gradient with a little inertia (Like barely any) this digs as it goes. Alot of the map is quite flat because I was compressing the elevations below 0.45 into 0.005 to 0.12. This resulted in a gradient that was nearly zero so the particle basically just stops.
    • The particle has no guarantee of reaching an outlet(lake or any other large body of water) and can just get stuck in a pit. Its steps are snapped to 8 directions so the paths are very jagged rivers dont ever merge and they are all the same width currently.
    • The water mesh is a sheet terrain height + river_mask * 30 - 5 it follows all terrain bumps and doesnt have its own water level.
  6. I started a new system to replace it.

    • The designed pipeline is fill depressions in the terrain, compute flow direction, accumulate flow then carve by accumulation. The first part of this is being written and tested.

    • It uses Priority-Flood (Barnes 2014). Outlets (the map edges and sea cells) go into a min-heap and I always expand the lowest cell on the frontier. A neighbour’s filled height is max(own height, spill height + ε), with ε = 1e-6. That epsilon turns flat areas into extremely gentle slopes, so every cell ends up with a strictly lower neighbour.

      • Rust’s BinaryHeapis a max-heap, and f32 isn’t Ord because of NaN. So I wrote a small Node type whose Ord flips the comparison on total_cmp and breaks ties by cell index, which also keeps generation deterministic.
      • Lakes fall out of this for free.filled − elevationis the water depth, and filled is the lake surface. I only count a lake where the depth passes a threshold, so tiny noise pits don’t become ponds. The terrain itself keeps its true elevation, and the filled grid is stored separately as a water table.

A small note:

I hope you enjoy these and continue to read these even if the game doesn’t interest you. I hope to have the rivers done in the next couple of days and I tend to post here when I have something interesting to share I dont have a strict posting schedule or timeline I had tons of time to work on the game and the engine today. I also want to share I am not sure how long of development time this game will take I have spent about a year or so just on the engine. I had a version of the engine with a different vision for my games, but after thinking for a while on it I decided to port what I could to Rust. I now have it to a spot where I feel its close to my original idea and vision, I am not focusing on graphics or shaders like how I was before. I am now focusing heavily on the systems aspect and simulating things I find interesting to build an overall dream game.