I'm currently using Perlin Noise to generate heightmaps and model moisture levels as well as tree density to create terrain (in the form of 2D tilemaps - think dwarf fortress).

How can I generate things like caves, or other landmarks like massive boulders or buildings?

I don't want to model these features (i.e. I don't care about the inside of the cave), I just want the game to be able to say 'there is a cave here' when you're at the x,y position of the cave, and importantly, follow rules about where caves might be (e.g. side of a mountain / rocky areas).

I also want to be able to generate this in a procedural way, similar to perlin noise, so that with any 'seed', the same landmarks will always appear.

I'm struggling to think of a way to create these features, and have them always reappear with the same seed. Does anyone have any ideas? :)


Depending on your perlin noise implementation you may have some kind of pseudo random 0..1 float generation . In my implementation I use a gradient array and a permutation table and given two int coords i get a value :

private float pseudoRandomFromPerm(int x, int y)
    return grad[(x + p[y & 255]+Seed) % grad.GetLength(0)];

I also have a 3d version of that and it looks like this:

private float pseudoRandomFromPerm(int x, int y, int z)
    return grad[(x + p[y + p[z & 255] & 255]+Seed) % grad.GetLength(0)];

So assuming that in your perlin algorithm you call a 2d generator at x,y , try using a 3d generator at x,y ; call it with z=0 in terrain generation call it with z=1000 (take some distance from zero) to 'decide' if in x,y there's a cave or a building or anything else. When you get the value for v=pseudorandom3d(x,y,1000) manage the results (assuming it is in 0..1 range)

if v<=0.05 and (current terrain conditions) then
 cave entrance
else if v>0.05 and v<=0.07 and (current terrain conditions) then
  • \$\begingroup\$ Thanks very much, using an extra dimension makes total sense! I'll try this. In fact, I should probably be putting my moisture map and tree density map on other z levels instead of separate generators. \$\endgroup\$
    – Ashley
    Dec 14 '15 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.