Random terrain

Make games! Discuss those games here.

Moderators: Bob the Hamster, marionline, SDHawk

User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Random terrain

Post by BMR »

So I've been dabbling with generating random terrain. I can successfully generate a map of pretty much any size with some code I pounded out when I should have been sleeping last night. Here are a few screenies of generated terrain:

Image Image Image Image


What I can't figure out though, is how to save that map for the next play through. I suppose I could save the tile values in a whole bunch of globals, but that would get rather messy. I thought of saving everything to a string using number to string but said string doesn't persist over different playthroughs.

Any ideas? Thanks in advance!
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Wow. From this distance, those rivers look really nice!

I recommend regenerating the map on the next playthrough. The only other option is to use global variables. If 16384 isn't enough, then you could store it in multiple save files using "export globals". But that's really messy.

In the not-to-distant future we want to let you save maps, strings, slices, and other things not currently saved in save games.

Anyway, to regenerate the map, use the "reseed random" command, like so:

Code: Select all

#Before generating a map:
map seed := random(-2147483648, 2147483647)
reseed random(map seed)
#generate map randomly....

#To regenerate:
reseed random(map seed)
#generate map randomly....
Last edited by TMC on Mon Nov 12, 2012 3:58 am, edited 1 time in total.
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

Ah, I see, that makes it a lot easier then. So the map seed is stored in a global, and then I just reuse that map seed for later playthroughs? Makes sense, thanks!


Regarding the rivers, they're not yet exactly how I want them to work yet though. Currently, the script picks a random point on the edge of the map, and works its way to the opposite side. The direction and the flow is influenced by underlying terrain. e.g. The river tries to avoid grass tiles if possible, most visible in the first screenie. If it can't avoid the grass tiles, it'll just go straight through randomly.

I've been tinkering around with the idea of using zones and multiple maps and teleport to map to simulate Z-levels, but that might be a bit much for what I'm trying to accomplish. I'll definitely experiment with it in the future though.

Thanks again for the solution!


EDIT: Just tested it, works perfectly, thanks!
Last edited by BMR on Mon Nov 12, 2012 4:12 am, edited 1 time in total.
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Multiple Z levels? What do you mean? Do you want to generate Dwarf Fortress-style 3D maps?
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

Playing around with the code a bit more. Added lakes, cliffs, and tweaked the rivers to flow a bit more the way I want them to.

Screenies!

Image Image Image
Image Image Image
Image Image Image

Pretty much all of the settings are adjustable in the code, so it's possible to make more archipelago-y or more continent-y lands.

I originally intended to use this code in Legacy for certain areas, but it's a bit too resource-intensive and clunky to use it multiple times. The generating takes long enough (around 12 seconds for a 128 × 128 map) that Game warns me I might be in an infinite loop. I'll probably put this on the back burner for a bit though, and recycle it for an OHR survival roguelike or something similar.

There are still quite a few things that need adding, like trees, caves, more shrubs ni!, elevation, ravines, etc... Right now, the only things implemented are basic terrain, rivers, lakes, the surrounding water, cliffs, and a few basic plants. I'd also like to work an elevation map so I can do some more realistic flow for the rivers. That would allow me to do drainage for the lakes as well. So much I want to do! But I must focus instead on Legacy for HotOHR, hehe.

Still, if anyone wants the code or anything, just tell me, I'm more than willing to share.

Cheers!
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

TMC wrote:Multiple Z levels? What do you mean? Do you want to generate Dwarf Fortress-style 3D maps?
Something like that. I couldn't sleep last night, and was thinking about how it would be possible to implement. I don't have a clear idea yet, but I was thinking Z-level boundaries could be done with zones. So Z-level 1 on Map 01 could be marked as Zone 1. On map 1, areas that are a higher Z-Level could be marked as Zone 2. Lower areas would be Zone 3 It wouldn't be too difficult to write a script that detects a change in Zone on each step. If a change from Zone 1 to Zone 2 is detected, teleport map could move the player to Map 2 while keeping X and Y coordinates. On Map 2, areas that were marked as Zone 1 on Map 1 would instead be Zone 3 (lower area) on Map 2.

I probably explained that rather poorly, hehe. That's what I get for late night/early morning brainstorming sessions. I haven't tried to implement the code yet, but I think it would be doable without being overly complicated. But I could be wrong.
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

Pseudo z-levels work!

Now to combine this with the random terrain generator in order to automatically create multiple linked maps! Shouldn't be too difficult... I hope... :v:
Last edited by BMR on Mon Nov 12, 2012 8:45 am, edited 1 time in total.
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
User avatar
Bob the Hamster
Liquid Metal King Slime
Posts: 7460
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

This is all very cool!
User avatar
Spoonweaver
Liquid Metal King Slime
Posts: 6247
Joined: Mon Dec 08, 2008 7:07 am
Location: Home
Contact:

Post by Spoonweaver »

Indeed. I don't think I've ever seen a good outdoor rogue-like. You may be onto something there.
User avatar
Matokage
Slime Knight
Posts: 275
Joined: Sat May 26, 2012 11:48 pm
Contact:

Post by Matokage »

BMR, Are you a wizard?! A plotscript Wizard?!
"I can't buy food with glory"
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

misac wrote:BMR, Are you a wizard?! A plotscript Wizard?!
Hardly =p If anyone is a plotscript wizard, then maybe TMC or James. Spoon is pretty great at scripting as well, look at Tim-Tim the Mighty Gnome or Tim Tim's Tower, those are pretty nifty. I have a rather long way to go before I can consider myself a plotscript wizard :v:
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

Dash it all, this has really captured my mind and I can't put it down. After doing some brainstorming (another sleepless night) I believe I've figured out how to generate a pseudo-3D map ala-Dwarf Fortress.

My first step would be to generate a height map using zones. Height can range from 0 to 127, so I have 128 different height values. Each tile will have a different zone written to it, depending on what my height map generator generates. Using the method TMC provided, I'll run this same generation again over 16 different maps.

Now that each map has the same height-map (zones), I can run another script for each map to figure out what goes where. Water could fill all areas below a certain height. Grass could only grow on areas that are relatively flat over a certain area to simulate plains. Areas with very varied heights would be rocky. If there's a very drastic change over a certain range, then a script could figure out that it's a cliff.

Different maps would thus have different terrain generated on them. Using the z-level walking I made yesterday (or was it two days ago?) the player would be able to walk between different levels almost seamlessly. Going to a "low" map, the player would be more likely to see rivers and water. Going to a "mid" map, rivers wouldn't be visible, rather they would be seen on the map as empty space leading to a lower z-level. The same goes for higher z-levels.

I feel that the main hurdle here would be generating a good height map. I've done a bit (read: a lot) of reading, and I've implemented a diamond-square algorithm to create a height map. The results are still a bit rough as seen below, but a little bit more tweaking will probably allow me to make better height maps.

I'm also planning on having the code also run rivers based on the generated terrain. The script would randomly pick a point on the edge map. It would then find its way "down" the height map by checking all eight directions of that specific point for the "lowest" of heights. Alternatively, I could use A* pathfinding with a target somewhere in the center of the map with height being the pathfinding cost. It would then continue in this way until it reaches either a predefined lowest value, the target point, or the maximum river length. I also plan to make it so that as the river runs, it also erodes the land nearby by a predefined value thus allowing for the formation of canyons if multiple rivers run through an area.

Anywho, screenies of height map generation below:

Image Image Image Image Image
Image Image Image Image Image
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
User avatar
Bob the Hamster
Liquid Metal King Slime
Posts: 7460
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

Yay! diamond square!

I am a fan of perlin simplex noise.

Not that the results look any different from diamond-square :)
User avatar
BMR
Metal King Slime
Posts: 3310
Joined: Mon Feb 27, 2012 2:46 pm
Location: The Philippines
Contact:

Post by BMR »

Was actually trying to do Perlin at first, and while I can wrap my head around the theory behind it, I haven't quite figured out how to actually implement it yet :v:
Being from the third world, I reserve the right to speak in the third person.

Using Editor version wip 20170527 gfx_sdl+fb music_sdl
User avatar
Bob the Hamster
Liquid Metal King Slime
Posts: 7460
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

Yeah, and I can see how it would be extra difficult to implement in plotscripting due to the lack of any floating point number support.

Although I have used perlin simplex noise myself, I haven't actually ever written my own implementation. I just do

Code: Select all

import perlin
simplex = perlin.SimplexNoise()
Python spoils me :)
Post Reply