| 1 | # Copyright (C) 2009-2021 Alex Schroeder <alex@gnu.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # This program is free software: you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # the terms of the GNU Affero General Public License as published by the Free | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # Software Foundation, either version 3 of the License, or (at your option) any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # later version. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # You should have received a copy of the GNU Affero General Public License along | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | Game::TextMapper::Schroeder::Base - a base role for map generators | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # create a map | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | package World; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | use Modern::Perl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | use Game::TextMapper::Schroeder::Base; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | use Role::Tiny::With; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | with 'Game::TextMapper::Schroeder::Base'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # use it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | package main; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | my $map = World->new(height => 10, width => 10); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | Map generators that work for both hex maps and square maps use this role and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | either the Hex or Square role to provide basic functionality for their regions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | such as the number of neighbours they have (six or four). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | package Game::TextMapper::Schroeder::Base; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 11 | ▶ | use Game::TextMapper::Point; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 11 | ▶ | use Modern::Perl '2018'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 11 | ▶ | use Mojo::Base -role; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # We're assuming that $width and $height have two digits (10 <= n <= 99). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 11 | has width => 30; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 11 | has height => 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 84295 | ▶ | sub coordinates { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
coordinates: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 84295 | my ($x, $y) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 84295 | return Game::TextMapper::Point::coord($x, $y); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | =head2 xy($coordinates) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | C<$coordinates> is a string with four digites and interpreted as coordinates and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | returned, e.g. returns (2, 3) for "0203". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 109325 | ▶ | sub xy { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
xy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 109325 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 109325 | my $coordinates = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 109325 | return (substr($coordinates, 0, 2), substr($coordinates, 2)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | =head2 legal($x, $y) or $legal($coordinates) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | say "legal" if $map->legal(10,10); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | Turn $coordinates into ($x, $y), assuming each to be two digits, i.e. "0203" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | turns into (2, 3). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | Return ($x, $y) if the coordinates are legal, i.e. on the map. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 82906 | ▶ | sub legal { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
legal: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 82906 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 82906 | my ($x, $y) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 82906 | ▶ | ($x, $y) = $self->xy($x) if not defined $y; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 82906 | ▶ | return @_ if $x > 0 and $x <= $self->width and $y > 0 and $y <= $self->height; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$x > 0 and $x <= $self->width and $y > 0 and $y <= $self->height
$x > 0 and $x <= $self->width and $y > 0
$x > 0 and $x <= $self->width
MC/DC100%4/4
$x > 0 and $x <= $self->width and $y > 0 and $y <= $self->height
$x > 0
$x <= $self->width
$y > 0
$y <= $self->height
Truth table100%5/5
A and B and C and D
A= $x > 0B= $x <= $self->widthC= $y > 0D= $y <= $self->height
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | =head2 remove_closer_than($limit, @coordinates) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | Each element of @coordinates is a string with four digites and interpreted as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | coordinates, e.g. "0203" is treated as (2, 3). Returns a list where each element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | is no closer than $limit to any existing element. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | This depends on L<Game::TextMapper::Schroeder::Base> being used as a role by a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | class that implements C<distance>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 40 | ▶ | sub remove_closer_than { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove_closer_than: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 40 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 40 | my ($limit, @hexes) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 40 | my @filtered; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | HEX: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 40 | for my $hex (@hexes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 710 | my ($x1, $y1) = $self->xy($hex); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # check distances with all the hexes already in the list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 710 | for my $existing (@filtered) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 5635 | my ($x2, $y2) = $self->xy($existing); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 5635 | my $distance = $self->distance($x1, $y1, $x2, $y2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # warn "Distance between $x1$y1 and $x2$y2 is $distance\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 5635 | ▶ | next HEX if $distance < $limit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # if this hex wasn't skipped, it goes on to the list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 215 | push(@filtered, $hex); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 40 | return @filtered; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | =head2 flat($altitude) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | my $altitude = {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | $map->flat($altitude); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | say $altitude->{"0203"}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | Initialize the altitude map; this is required so that we have a list of legal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | hex coordinates somewhere. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 8 | ▶ | sub flat { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flat: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 8 | my ($altitude) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 8 | for my $y (1 .. $self->height) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 100 | for my $x (1 .. $self->width) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 3600 | my $coordinates = coordinates($x, $y); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 3600 | $altitude->{$coordinates} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | =head2 direction($from, $to) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | Return the direction (an integer) to step from C<$from> to reach C<$to>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | This depends on L<Game::TextMapper::Schroeder::Base> being used as a role by a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | class that implements C<neighbors> and C<neighbor>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 266 | ▶ | sub direction { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
direction: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 266 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 266 | my ($from, $to) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 266 | for my $i ($self->neighbors()) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 716 | ▶ | return $i if $to eq coordinates($self->neighbor($from, $i)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | L<Game::TextMapper::Schroeder::Hex> and L<Game::TextMapper::Schroeder::Square> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | both use this class to provide common functionality. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||