These scripts generate random heightmaps for use as textures or terrain models (or both).
The procedure is quite simple, start with a randomly filled screen and smoothen it out. This will generate a sprite with smooth "hills and valleys".
Download
You may save the output of the script and use that freely, but please give credit if you're using the script to generate heightmaps during runtime.
In the full scripts there is the option to add noise or make a looping texture. As they're quite long, I'll only post the really compact version of the script here for you to look at:
/*
create_heightmap_fast(size,detail)
Creates a random heightmap.
Size can be anything, as long as it's smaller than the game window.
Detail is the ammount of detail. Higher detail means more hills and valleys.
Detail is maximum 5, the minimum varies (but is mostly less than 0)
Script created by TheSnidr
www.thesnidr.com
*/
var s,g,pg,xx,yy;
argument0=log2(argument0)
argument1=median(argument0-5+round(argument1),3+frac(argument0),argument0)
s=power(2,argument1)
g=ds_grid_create(s,s)
for (xx=0;xx<s;xx+=1){for (yy=0;yy<s;yy+=1){
ds_grid_set(g,xx,yy,choose(0,1))}}
for (argument1+=1;argument1<=argument0;argument1+=1){
s=power(2,argument1)
pg=g
g=ds_grid_create(s,s)
for (xx=0;xx<s;xx+=1){for (yy=0;yy<s;yy+=1){
ds_grid_set(g,xx,yy,ds_grid_get_disk_mean(pg,xx/2,yy/2,1.5))}}
ds_grid_destroy(pg)}
for (xx=0;xx<s;xx+=1){for (yy=0;yy<s;yy+=1){
draw_point_color(xx,yy,make_color_hsv(0,0,240*ds_grid_get_disk_mean(g,xx,yy,1)))}}
ds_grid_destroy(g)
return sprite_create_from_screen(0,0,s,s,false,false,0,0);Please note, the full script (which you'll find a link to at the beginning of this post) is better commented.This script (the compact and faster version) will create a 512*512 heightmap in less than 2 seconds (on my computer at least).
The full script with all options turned on takes about twice as long (it is still not completely optimized), but it may in some cases look better.
Both scripts can return sprites of any size, but as the rendering time increases exponentially, you'd want to not make them too big. Also, the fast script draws and creates its sprite directly from the screen, meaning it will have to be smaller than the game window. Otherwise it won't render correctly. The full script draws its sprite onto a surface first, thus avoiding this problem.
The detail argument affects how random the terrain is. It can be maximum 5, meaning you'll only get random rubble. A detail of 2 or 3 looks good. The minimum detail varies proportionally with the size, but this isn't important.
Sprites smaller than 16*16 will look bad.
Here are some I made while playing around with the hue, saturation and value:

The fast script is the only one I have optimized to the max so far, but feel free to comment if you see any other ways to improve them!
Also, please don't remove the "made by TheSnidr" part!
Edited by TheSnidr, 08 May 2011 - 08:03 PM.


















