Hello! I was looking for some help building a seed-based terrain generation system, like Minecraft. If a world for one player has seed 2, and another player loads a world with the same seed, the same terrain would generate.
I have got some chunks that I could implement. Right now, each chunk (32x32 studs) just needs to be a different brick colour, and different structures should appear (think villages in Minecraft, and other structures, like jungle temples are rarer)
I’ll leave you with math.noise, but just know that this is a very complicated subject in more than one aspect, if you’re going for the full concept of Minecraft generation.
So the way that seeding works is that it makes your pseudorandom generation the same across each input, for example
local seed = 1
Random.new(seed) -- the seed value can be made consistent
Random:NextInt(1, 10) -- this should return a consistent value
--However, Math.noise generates its noise from its inputs, not from the math.randomseed() function
math.noise(x, y, z) will always equal math.noise(x, y, z)
Therefor you can use that seed value to adjust the inputs of Math.noise
local seed = 1
local chunksize = 16
local scale = 10.71 -- math.noise only works with floating point numbers, integers will return 0
for x = 0, chunksize do
for y = 0, chunksize do
local height = math.noise((x / scale) * seed, (y/scale) * seed)
end
end
Thanks! Just to ask, how do I make it generate structures with rarity? For it to be consistent across worlds using the same seed, how would it be done?
You’ll need multiple noise maps for multiple different things. I’d give more in-depth information as I’ve worked directly with this sort of thing, but that’s also a project I’m still working on (albeit on the back-burner at the moment).
Creating structures using noise can be done by using a second noise map with a higher-scale.
So instead of generating noise to get the height of a voxel in a chunk, set your noise inputs based on the chunk position itself. This will allow you to handle things such as biomes and structure generation by using a separate layer of noise.
The code for generating a noise sample is the same, you just have to change the input.
Just a heads up: this only works if the RNG is called for each thing in the same order every time! If your goal is to have terrain that generates in chunks around the player as they move through the world, for “infinite” terrain, then you need to keep careful track of several RNGs to actually get the same result every time.
Just generating the terrain in a fixed number of chunks (and in a fixed order) is a lot of work already so this isn’t the first thing to deal with IMO, but it’s good to know for the future.