mapgen2 icon indicating copy to clipboard operation
mapgen2 copied to clipboard

Instantiation example?

Open stefvw93 opened this issue 4 years ago • 3 comments

Could you please provide a minimal example on how to initialize a new map generator -- especially the makeRandInt method is confusing to me?

I tried this myself, it works but I don't know exactly how.

new MapGenerator(
  new MeshBuilder()
    .addPoints([
      [0, 0],
      [10, 0],
      [0, 10],
      [20, 0],
      [0, 20],
      [30, 0],
      [0, 30],
    ])
    .create(),

  // noisyEdgeOptions: {length, amplitude, seed}
  {
    amplitude: 0.2,
    length: 4,
    seed: 12345,
  },

  // makeRandInt: function(seed) -> function(N) -> an int from 0 to N-1
  function (seed: number) {
    return function () {
      return Math.round(Math.random() * seed);
    };
  }
)

Also very nice work on this, I really appreciate your work and the related blog article you wrote!

stefvw93 avatar Feb 24 '21 12:02 stefvw93

An example is a great idea. I'll start working on one. It's been many years since I used this library so I've forgotten some of it :-)

redblobgames avatar Feb 25 '21 17:02 redblobgames

makeRandInt is here: https://github.com/redblobgames/prng/blob/master/index.js

If you want to use Math.random instead of a seed, you can use

                  function (_seed) {
                      return function (N) {
                          return Math.round(Math.random() * N);
                      };
                  }

redblobgames avatar Feb 25 '21 17:02 redblobgames

Here's a full example that generates a mesh using Poisson disc points, and then calculates the map (rivers, elevation, biomes, etc.)

const SimplexNoise =  require('simplex-noise');
const DualMesh =      require('@redblobgames/dual-mesh');
const MeshBuilder =   require('@redblobgames/dual-mesh/create');
const Map =           require('@redblobgames/mapgen2');
const Poisson =       require('poisson-disk-sampling');
const {makeRandInt} = require('@redblobgames/prng');

let mesh = new DualMesh(
    new MeshBuilder()
        .addPoisson(Poisson, 100)
        .create()
);

map.calculate({
    noise: new SimplexNoise(),
    shape: {round: 0.5, inflate: 0.4, amplitudes: [1/2, 1/4, 1/8, 1/16]},
    numRivers: 30,
    drainageSeed: 0,
    riverSeed: 0,
    noisyEdge: {length: 10, amplitude: 0.2, seed: 0},
    biomeBias: {north_temperature: 0, south_temperature: 0, moisture: 0},
});

redblobgames avatar Feb 25 '21 17:02 redblobgames