Game-TextMapper
view release on metacpan or search on metacpan
lib/Game/TextMapper/Schroeder/Island.pm view on Meta::CPAN
# Copyright (C) 2009-2021 Alex Schroeder <alex@gnu.org>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
=encoding utf8
=head1 NAME
Game::TextMapper::Schroeder::Island - generate an island chain
=head1 DESCRIPTION
This creates an island chain in an ocean, based on the idea of a hotspot moving
across the map. All regions atop the hotspot get raised at random; all regions
outside the hotspot are eroded at random. This leaves a chain of ever smaller
islands behind.
The rest of the code, river formation and all that, is based on the Alpine
algorithm and therefore it also requires the use of roles.
return Game::TextMapper::Schroeder::Island
->with_roles('Game::TextMapper::Schroeder::Square')->new()
->generate_map(@params);
=head1 SEE ALSO
L<Game::TextMapper::Schroeder::Alpine>
L<Game::TextMapper::Schroeder::Base>
L<Game::TextMapper::Schroeder::Hex>
L<Game::TextMapper::Schroeder::Square>
=cut
package Game::TextMapper::Schroeder::Island;
use Game::TextMapper::Log;
use Modern::Perl '2018';
use Mojo::Base 'Game::TextMapper::Schroeder::Alpine';
use Role::Tiny::With;
with 'Game::TextMapper::Schroeder::Base';
use List::Util qw'shuffle min max';
my $log = Game::TextMapper::Log->get;
has 'bottom' => 0;
has 'top' => 10;
has 'radius' => 5;
has 'hotspot';
sub ocean {
my $self = shift;
my ($world, $altitude) = @_;
for my $coordinates (sort keys %$altitude) {
if ($altitude->{$coordinates} <= $self->bottom) {
my $ocean = 1;
for my $i ($self->neighbors()) {
my ($x, $y) = $self->neighbor($coordinates, $i);
my $legal = $self->legal($x, $y);
my $other = coordinates($x, $y);
next if not $legal or $altitude->{$other} <= $self->bottom;
$ocean = 0;
}
$world->{$coordinates} = $ocean ? "ocean" : "water";
}
}
}
sub change {
my $self = shift;
return if $self->hotspot->[0] > $self->width - 2 * $self->radius;
my $world = shift;
my $altitude = shift;
# advance hotspot
if (rand() < 0.2) {
$self->hotspot->[0] += 1.5 * $self->radius;
} else {
$self->hotspot->[0]++;
}
if (rand() < 0.5) {
if (rand() > $self->hotspot->[1] / $self->height) {
$self->hotspot->[1]++;
} else {
$self->hotspot->[1]--;
}
}
# figure out who goes up and who goes down, if the hotspot is active
my %hot;
for my $x (max(1, $self->hotspot->[0] - $self->radius) .. min($self->width, $self->hotspot->[0] + $self->radius)) {
for my $y (max(1, $self->hotspot->[1] - $self->radius) .. min($self->height, $self->hotspot->[1] + $self->radius)) {
if ($self->distance($x, $y, @{$self->hotspot}) <= $self->radius) {
my $coordinates = coordinates($x, $y);
$hot{$coordinates} = 1;
}
}
}
# change the land
for my $coordinates (keys %$altitude) {
( run in 0.911 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )