Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: stevon8ter on July 28, 2013, 06:09:30 pm

Title: Best random map generating technique [lua]
Post by: stevon8ter on July 28, 2013, 06:09:30 pm
well, i want to create a 2D minecraft like game on my ipad, it would probably be a map of 1000*50, but i wouldn't know how to create a random world, one that makes sense, with little hills and no floating blocks etc....
Title: Re: Best random map generating technique [lua]
Post by: ben_g on July 28, 2013, 07:08:24 pm
I am still a noob in lua, so I won't be able to help you a lot with the actual coding. But for the algorithm, I can give you this algorithm based on what I used in my 2D minecraft clone:
Code: [Select]
- Set a height variable to what you want for the average height of the level
loop accros each collum:
   - generate a random number and use that to determine if the ground level should get higher or lower (for example, a random number between 0 and 4: 0: decrease height variable, 4: increase height variable, other values: do nothing.) You might want to decrease the chance of moving up when above the average height, and decrease the chance of gowing down when under the average height to make the world height fluctuate around the average height. You can make the chance of changing the height depend on a biome variable as well.
   - fill the collum from 0 to a random number of blocks between the ground height of that collum. (set the bounds of that random to something that looks right)
  If the height for that collum is above the water height: (the water height depends on what type of map you want. Higher than the average height will give you islands, lower than the average height will give you lakes.)
     - fill the collum from above the last stone block to 1 block under the world height with dirt. Place a block of grass at the top
  else:
     - fill the collum from above the last stone block to the world height with dirt. Randomly replace the top block with sand or clay if desired.
     - fill the collum from the last dirt/sand/clay block up to the water height with water.
  endif
end loop

tree placing loop: (repeat a random number of times. More means more trees, less means less trees)
   - Choose a random collum.
   - Get the location and type of the topmost block
  If the topmost block is grass:
     - place the blocks that form the tree at the location you got from the previous step
  endif
end loop

ore generation loop:
   - shoose a random location (it's reccomended to use a logaritmic scale for the y-coordinate to make ores more common at the bottom of the world)
   - try to replace some stone blocks on and around that location by a vain of random ore (more valuable ores being less common)
end loop

It's very basic, but you should be able to make it more complex and make it generate more beautifull worlds.
Title: Re: Best random map generating technique [lua]
Post by: blue_bear_94 on July 28, 2013, 07:17:59 pm
Well, Minecraft uses Perlin noise for most of the terrain, but since the map will be finite, you could potentially use some other way.
Title: Re: Best random map generating technique [lua]
Post by: AngelFish on July 28, 2013, 07:27:52 pm
well, i want to create a 2D minecraft like game on my ipad, it would probably be a map of 1000*50, but i wouldn't know how to create a random world, one that makes sense, with little hills and no floating blocks etc....

Unfortunately I'm on very slow internet, so I can't link in any of the many good demonstrations online, but a very common way to do this is simply to layer a bunch of 2-dimensional signals onto one another. This is the basis of perlin noise, among other things. You generate a random map of sine waves of each "octave" of noise you want and merely add all of these maps together, which will generate terrain with detail up to half the wavelength of the smallest octave.

An even simpler (conceptually, at least) method to do the same thing is to take a sample of random noise in the interval from half the wavelength of the smallest detail you want up to twice the wavelength of the largest features you want and apply an inverse fourier transform to it. This does the same thing as the previous method, but it involves fewer lines of code if you have a library to handle the IFFT.
Title: Re: Best random map generating technique [lua]
Post by: stevon8ter on July 28, 2013, 07:44:03 pm
uhmmmmmmmmmmmmmm

well i think, for in the beginning i'll use this kind of style: (cause i dunno what you're saying, maybe too tired xp)
- start from bottom
- 1 layer bedrock
- then a random between 20 and 30 of stone, with at some random locations a random ore
- then 40 - #stone - #ores - #bedrock
- then 10 layers sky, with sometimes a lake / mountain



and who knows, i might someday make it an infinite map xd