# Dynamic Terrain Generation with Diamond-Square Algorithm

Discussion in 'Scripting' started by awilki01, Oct 3, 2016.

1. ### awilki01

Joined:
Jan 25, 2015
Posts:
11
This is the first time I've done a video like this. I hope it can help someone down the road......

2. ### dudester

Joined:
Oct 25, 2014
Posts:
323
good video , good looking terrain only problem is that its doesnt make the same terrain everytime based on a seed but im sure thats easily solvable . To be honest i had no idea about the diamond square algorithm until now it'll maybe help make my voxel terrain im making a bit more interesting.

3. ### awilki01

Joined:
Jan 25, 2015
Posts:
11
Thanks, dudester! I appreciate you watching! I never thought about the seed issue you mention. My intent for the game I'm making with this method was always under the guise of generating different unique terrain each time.

Perhaps you could export the height data and reuse it over and over. ***Edit: See my comments below about seeding the algorithm. *** But, there is definitely a benefit to using a given seed that always yields the same outcome. I'm currently building a 3D lundar lander style game using this diamond-square method. I added different size landing pads randomly on the terrain. Since each time you start the level the terrain is unique, I had to programmatically determine and set the landing spots where they should be based on the generated terrain. It was sort of a pain, but I finally got there.

Last edited: Oct 4, 2016
4. ### takatok

Joined:
Aug 18, 2016
Posts:
1,496
Even for procedurally generated terrain, that is unique each gameplay its very useful to include a seed feature. Its pretty easy to program in an int Seed, and a bool useSeed and use the seed if the bool is set true. Makes it much easier to debug issues of your generating algorithm. You can even get a random number to use as a seed. Then each time you play your game prints out "Using seed: xxxxx" then if you spot an error you can just check useSeed and manually input the seed printed out when you noticed the error.

Then you just turn it off for the release build.

5. ### awilki01

Joined:
Jan 25, 2015
Posts:
11
You are absolutely correct! I never really considered this because in my implementation I always wanted different terrain each time the game played so I never even considered seeding... But your comments about debugging were spot on. I was actually thinking you couldn't seed this algorithm, but I was vastly mistaken. And, now that I figured out how to do it, I feel a little embarrassed.

All you have to do is add a seed to the Random object created in line 34 of the following gist: