Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Random tile map generator

This thread is locked; no one can reply to it. rss feed Print
Random tile map generator
matthew9090
Member #13,004
July 2011

I can't find a good example or tutorial about random terrain with a tile map. i got the scrolling to work :) but my friend (who is designing the game, i program it) wants a random terrain generator like minecraft, but in 2d. I am using a tilemap and an array full of numbers like 2 for sky and 1 for dirt etc... So has anyone got any examples that i could use for help?

Kris Asick
Member #1,424
July 2001

This is a very complicated subject because terrain is a natural thing, and it's very difficult to simulate something natural if you don't understand the mathematics or science behind it.

If I were you, I'd start by looking into "plasma fractals". Plasma fractal algorithms are the most basic method you can use to generate a random landscape. You can then set a water height and have all tiles at or below that height as water.

Beyond that, it really depends on the type of game you're making and what kind of elements you expect to find in the game. There's no one single algorithm that is going to apply to your game; you need to use a bunch that match up with the type of terrain you want.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

weapon_S
Member #7,859
October 2006
avatar

What kind of game is it? Most notably: top-down or side-scroll?
Fill terrain in randomly. Store it somewhere. Load it from somewhere.
That's as far as my knowledge goes...

Audric
Member #907
January 2001

The most basic that you can do is for each tile, to set its content depending on a random number: For example 70% dirt, 25% boulder, 5% gem.
You'll see that it gets the right proportions, statistically, but it doesn't feel natural.

You can try search for topics like "roguelike wilderness generation in C", because the term "wilderness" in roguelike games refers to overland areas (as opposed to dungeons and caves) with forests, mountains etc, and the games that implement it use algorithms that try make the generation feel natural.

Procedural generation isn't necessarily very complicated. One example can be:
1) initialize all tiles 75% dirt 25% rock
2) Repeat 2 or 3 times: for each block of 2x2 tiles that contains 2 rocks, make the other 2 tiles rocks too (this step makes groups of boulders "expand")
3) For each block of 2x2 tiles that contains only 1 rock: 33% chance that this rock becomes dirt (This reduces single rocks, but leaves some.)
4) For each tile that is a rock: 10% chance that it turns into gem or something.

Use trial and error to adjust the algorithm according to what provides good gameplay.

matthew9090
Member #13,004
July 2011

Thanks for the reply's. I am making a side scrolling platform game like terraria. I got part of it working and it generated some hills but now in the debugger it comes up with a segmentation fault. I got read of the random bit and it works but without it doesn't. hers the bit which doesn't work:

#SelectExpand
1int num_of_hills = (rand()%7)+6; //number of hills 2for(int x = 0; x<num_of_hills;x++) 3{ 4 int pos = (rand()%width)+40; //so it doesn't hit the player 5 int hill_height = (rand()%3)+2; //generate different hill height 6 7 switch(hill_height) 8 { 9 case 2: //only doing 2 for now 10 tilemap[10][pos] = 1; 11 tilemap[11][pos] = 1; 12 tilemap[11][pos-1] = 1; 13 tilemap[10][pos-1] = 0; 14 tilemap[9][pos] = 0; 15 tilemap[11][pos+1] = 1; 16 tilemap[10][pos+1] = 0; 17 break; 18 19 case 3: 20 tilemap[11][pos-2] = 1; 21 tilemap[11][pos-1] = 1; 22 tilemap[11][pos] = 1; 23 tilemap[11][pos+1] = 1; 24 tilemap[11][pos+2] = 1; 25 tilemap[10][pos] = 1; 26 tilemap[10][pos+1] = 1; 27 tilemap[10][pos-1] = 1; 28 tilemap[10][pos-2] = 0; 29 tilemap[10][pos+2] = 0; 30 tilemap[9][pos-1] = 0; 31 tilemap[9][pos] = 1; 32 tilemap[9][pos+1] = 0; 33 tilemap[8][pos] = 0; 34 break; 35 36 default: 37 tilemap[10][pos] = 0; 38 tilemap[11][pos] = 1; 39 break; 40 41 } 42}

J-Gamer
Member #12,491
January 2011
avatar

int pos = (rand()%width)+40; //so it doesn't hit the player

This can easily go OVER the value of width, and thus you will try to access an element past the end of the array ==> segfault.

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

Andrei Ellman
Member #3,434
April 2003

If you want to generate a heightmap, I agree with what Kris said about the plasma fractals. See Diamond-square algorithm and Plasma Effect (BTW if you want some code for doing the plasma effect, feel free to pinch the code in my ChromaPlas screensaver). See also Perlin noise.

If you want to just choose random terrain and have a set of bitmaps that transition smoothly between the different types of terrain, one thing you could do is assign a bitfield to each edge of the tile that stores information about what the tile contains at the edge (rock in the left part of the edge, dirt in the right part of the edge, grass, etc.) and loop from top-left to bottom-right. When choosing a tile, pick a random tile with an equal edge-properties bitfield for the adjacent tile to the top or left on the opposing side of the tile (and for the top or left edge, just accept any tile). If you want certain squares to have only certain tiles, then you should also check the right and downward bitfields as well if there's already a tile there.

The above technique makes a random map but not necessarily a natural one. An alternative would be to use the first technique (the plasma fractal) but using a threshold for each type of terrain (if the value of the fractal is above a certain level, then use this value). You would choose the order of which type of terrain to apply to get the result you desire. For example, if you don't want vegetation on any rocky terrain, first you would apply the fractal to vegetation, then to rockyness, and finally to water (usually, water would be done last).

AE.

--
Don't let the illegitimates turn you into carbon.

matthew9090
Member #13,004
July 2011

ok, thanks i might use that if i know how to implement it into my code in the terrain.h. I have read a bit about fractal landscapes put how would i use that in a 2d random tilemap which every block goes up by 1 block or down by 1 block? it seams complicated for a 13 year old

Audric
Member #907
January 2001

For a side view, the first step of generation will probably be to produce a clear separation of sky and ground. Maybe there are code and ideas to find in terrain generation of Worms / Liero / Scorched Earth games.
They often use pixel units, but tiles are like big pixels :)

Johan Halmén
Member #1,550
September 2001

Spend 60 seconds on Mspaint and draw something that shows us what you want. A sky vs. ground landscape like a horizon? Or a land vs. water thing like a top view map?

Here's how a square-diamond landscape looks like:
{"name":"1479_large.gif","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/6\/4611c37e662cbd23e45ed38cbc235f51.gif","w":558,"h":419,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/6\/4611c37e662cbd23e45ed38cbc235f51"}1479_large.gif
It's a heightmap, with an add-on of some light source stuff.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Go to: