Games::Terrain::DiamondSquare - Random terrain generation via the Diamond Square algorithm
version 0.02
use Games::Terrain::DiamondSquare 'create_terrain';
my $terrain = create_terrain( $height, $width, $roughness );
foreach my $row (@$terrain) {
foreach my $square (@$row) {
# $square is a "height" value from 0.0 to 1.0. Do with it as you will
}
}
From Wikipedia: The diamond-square algorithm is a method for generating highly realistic heightmaps for computer graphics. It's a fractal method of generating random terrain "heights" which is reasonably fast (though this being Perl, it's not fast enough for, say, real-time rendering). A proper C implementation would be nice here.
There is a tohtml.pl
example in the examples
directory of this
distribution.
my $terrain = create_terrain( $height, $width );
# or
my $terrain = create_terrain( $height, $width, $roughness );
This function accepts integer $height
and $width
arguments and an
optional $roughness
parameter. The latter is a float from 0.0 to 1.0
indicating how "rough" the map should be. Lower numbers generate smoother
maps. Defaults to 0.5
.
Here's an example terrain generated from a test script:
$$$$$$$$$$$$$$$$$$$$$$$####################*********************!**!!!!!!!!!!!!!
$$$$$$$$$$$$$$$$$$$$$$#####################****************************!!!!!!!!!
$$$$$$$$$$$$$$$$$$$$$$###########################*#*##*#*####************!!!!!!!
####$####$#$#$$$$$$$$$##########################################**********!!!!!!
################$$$$$$$$##########################################**********!!!!
#################$$$$$$$$$##########################################**********!!
*###############$$$$$$$$$$$#########################$##$#$$$$########**********!
****#*############$$#$##$##########################$$$$$$$$$$$########**********
********#############################################$$$$$$$$#########**********
*************#########################################$#$##$########************
!*!*************###################################################*************
!!!!!!*!**********####*##*#*#######################################*************
!!!!!!!!!!!*****************############$##########################*************
=!!!!!!!!!!!!***************###########$$$##########################**#*#*******
====!!!!!!!!!!!!***********############$$$$$#############################*******
;=======!!=!!!!!!!**********##**#######$##############################**********
;;;;;;========!!!!!!!***************###############################*************
;;;;;;;;========!!!!!!!!*!*!**********############*##############************###
;;;;;;;;;;;=====!!!!!!!!!!!!!!!********#*#**************######*************#####
:;;;;;;;;;;======!!!!!!!!!!!!!!!***************************#*#*********#########
:::;;;;;;;;;======!!!!!!!!!!!!!!!*************************************##########
::::::;;;;;;=======!!!!!!!!!!!!!!!*!*******************************#############
~:::::;;;;;;;;=========!!=!!!!!!!!!!!!!*!***************************############
~~::::::;;;;;;;;=;===========!=!=!!!!!!!!!!!!!!!*********************#*#########
~~~~:::::::;;;;;;;;=;===============!!!!!!!!!!!!!!!*********************########
~~~~~~:::::::;;;;;;;;;;;;;=;==========!!!!!!!!!!!!!*!*******************########
-~~~~~~~::::::::::;;;;;;;;;;;;;==========!=!!!!!!!!!!!!*****************########
----~~~~~~~::::::::::;;;;;;;;;;;;=============!!!!!!!!!***************##########
------~~~~~~~~:~:::::::;;;;;;;;;;;;;============!!!!!!!!**************#######$$$
,--------~-~~~~~~~:::::::::::;;;;;;;;;==========!!!!!!!!!***********#########$$$
You can read about the algorithm at http://www.gameprogrammer.com/fractal.html#diamond
This implementation is based off of http://www.smokycogs.com/blog/plasma-fractals/.
Curtis "Ovid" Poe ovid@cpan.org
This software is copyright (c) 2013 by Curtis "Ovid" Poe.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.