| 1 | package Games::Terrain::DiamondSquare; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 1 | $Games::Terrain::DiamondSquare::VERSION = '0.02'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | ## ABSTRACT: Random terrain generation via the Diamond Square algorithm | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use List::Util 'sum'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use POSIX 'floor'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | our @EXPORT_OK = 'create_terrain'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | my ( $FULL_SIZE, $ROUGHNESS ); | |||||||||||||||||||||||||||
| 16 | 1 | ▶ | use constant NW => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use constant NE => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use constant SW => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use constant SE => 3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | sub create_terrain { | ||||||||||||||||||||||||||
|
Subroutine
create_terrain: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 1 | my ( $height, $width, $roughness ) = @_; | |||||||||||||||||||||||||||
| 23 | 1 | ▶ | $roughness ||= .5; | ||||||||||||||||||||||||||
|
Condition50%1/2
$roughness ||= "0.5"
MC/DC0%0/1
$roughness ||= "0.5"
$roughness
Truth table50%1/2
A
A= $roughness
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # seed the four corners of the grid with random color values | ||||||||||||||||||||||||||||
| 26 | 1 | my @corners = map {rand} 1 .. 4; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | $ROUGHNESS = $roughness; | |||||||||||||||||||||||||||
| 29 | 1 | $FULL_SIZE = $height + $width; | |||||||||||||||||||||||||||
| 30 | 1 | my @points; | |||||||||||||||||||||||||||
| 31 | 1 | subdivide( \@points, 0, 0, $height, $width, \@corners ); | |||||||||||||||||||||||||||
| 32 | 1 | return \@points; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 9557 | ▶ | sub subdivide { | ||||||||||||||||||||||||||
|
Subroutine
subdivide: called CC=8 SCAR=21.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 9557 | my ( $points, $x, $y, $height, $width, $corners ) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 9557 | ▶ | if ( $height > 1 || $width > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$height > 1 or $width > 1
MC/DC100%2/2
$height > 1 or $width > 1
$height > 1
$width > 1
Truth table100%3/3
A or B
A= $height > 1B= $width > 1
| |||||||||||||||||||||||||||||
| 39 | 2389 | my $new_height = floor( $height / 2 ); | |||||||||||||||||||||||||||
| 40 | 2389 | my $new_width = floor( $width / 2 ); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2389 | my $middle | |||||||||||||||||||||||||||
| 43 | = sum(@$corners) / 4 + displace( $new_height + $new_width ); | ||||||||||||||||||||||||||||
| 44 | 2389 | my $edge_1 = ( $corners->[NW] + $corners->[NE] ) / 2; | |||||||||||||||||||||||||||
| 45 | 2389 | my $edge_2 = ( $corners->[NE] + $corners->[SW] ) / 2; | |||||||||||||||||||||||||||
| 46 | 2389 | my $edge_3 = ( $corners->[SW] + $corners->[SE] ) / 2; | |||||||||||||||||||||||||||
| 47 | 2389 | my $edge_4 = ( $corners->[SE] + $corners->[NW] ) / 2; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | $_ = constrain($_) | ||||||||||||||||||||||||||||
| 50 | 2389 | foreach $middle, $edge_1, $edge_2, $edge_3, $edge_4; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # do it again for each of the four new grids. | ||||||||||||||||||||||||||||
| 53 | 2389 | subdivide( | |||||||||||||||||||||||||||
| 54 | $points, $x, $y, $new_height, $new_width, | ||||||||||||||||||||||||||||
| 55 | [ $corners->[NW], $edge_1, $middle, $edge_4 ] | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | 2389 | subdivide( | |||||||||||||||||||||||||||
| 58 | $points, $x + $new_height, $y, $height - $new_height, $new_width, | ||||||||||||||||||||||||||||
| 59 | [ $edge_1, $corners->[NE], $edge_2, $middle ] | ||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||
| 61 | 2389 | subdivide( | |||||||||||||||||||||||||||
| 62 | $points, $x + $new_height, $y + $new_width, $height - $new_height, | ||||||||||||||||||||||||||||
| 63 | $width - $new_width, | ||||||||||||||||||||||||||||
| 64 | [ $middle, $edge_2, $corners->[SW], $edge_3 ] | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | 2389 | subdivide( | |||||||||||||||||||||||||||
| 67 | $points, $x, $y + $new_width, $new_height, $width - $new_width, | ||||||||||||||||||||||||||||
| 68 | [ $edge_4, $middle, $edge_3, $corners->[SE] ] | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | else # this is the "base case," where each grid piece is less than the size of a pixel. | ||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # the corners of the grid piece will be averaged and drawn as a single pixel. | ||||||||||||||||||||||||||||
| 75 | 7168 | my $c = sum(@$corners) / 4; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 7168 | $points->[$x][$y] = $c; | |||||||||||||||||||||||||||
| 78 | 7168 | ▶ | if ( $height == 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | $points->[ $x + 1 ][$y] = $c; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 7168 | ▶ | if ( $width == 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $points->[$x][ $y + 1 ] = $c; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 7168 | ▶ | if ( $height == 2 and $width == 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$height == 2 and $width == 2
MC/DC0%0/2
$height == 2 and $width == 2
$height == 2
$width == 2
Truth table33%1/3
A and B
A= $height == 2B= $width == 2
| |||||||||||||||||||||||||||||
| 85 | 0 | $points->[ $x + 1 ][ $y + 1 ] = $c; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 9557 | return; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 11945 | ▶ | sub constrain { | ||||||||||||||||||||||||||
|
Subroutine
constrain: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 11945 | my $num = shift; | |||||||||||||||||||||||||||
| 93 | return | ||||||||||||||||||||||||||||
| 94 | 11945 | ▶ | $num < 0 ? 0 | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 95 | : $num > 1 ? 1 | ||||||||||||||||||||||||||||
| 96 | : $num; | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2389 | ▶ | sub displace { | ||||||||||||||||||||||||||
|
Subroutine
displace: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | 2389 | my $curr_size = shift; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2389 | my $max = $curr_size / $FULL_SIZE * $ROUGHNESS; | |||||||||||||||||||||||||||
| 103 | 2389 | return ( rand() - 0.5 ) * $max; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | 1; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||