Author Topic: Best random map generating technique [lua]  (Read 4984 times)

0 Members and 1 Guest are viewing this topic.

Offline stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Best random map generating technique [lua]
« 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....
« Last Edit: July 28, 2013, 06:10:00 pm by stevon8ter »
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: Best random map generating technique [lua]
« Reply #1 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.
« Last Edit: July 28, 2013, 07:09:23 pm by ben_g »
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline blue_bear_94

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 801
  • Rating: +25/-35
  • Touhou Enthusiast / Former Troll / 68k Programmer
    • View Profile
Re: Best random map generating technique [lua]
« Reply #2 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.
Due to dissatisfaction, I will be inactive on Omnimaga until further notice. (?? THP hasn't been much success and there's also the CE. I might possibly be here for a while.)
If you want to implore me to come back, or otherwise contact me, I can be found on GitHub (bluebear94), Twitter (@melranosF_), Reddit (/u/Fluffy8x), or e-mail (if you know my address). As a last resort, send me a PM on Cemetech (bluebear94) or join Touhou Prono (don't be fooled by the name). I've also enabled notifications for PMs on Omnimaga, but I don't advise using that since I might be banned.
Elvyna (Sunrise) 4 5%
TI-84+SE User (2.30 2.55 MP 2.43)
TI-89 Titanium User (3.10)
Casio Prizm User? (1.02)
Bag  東方ぷろの

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Best random map generating technique [lua]
« Reply #3 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.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Re: Best random map generating technique [lua]
« Reply #4 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
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER