| 1 | # For license, docs, see the POD documentation at the end of this file | ||||||||||
| 2 | |||||||||||
| 3 | package Games::Roguelike::Utils; | ||||||||||
| 4 | |||||||||||
| 5 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | # this breaks under perl < 5.8 | ||||||||||
| 8 | # use Exporter qw(import); | ||||||||||
| 9 | |||||||||||
| 10 | # Direction helpers | ||||||||||
| 11 | |||||||||||
| 12 | 5 | our $VERSION = '0.4.' . [qw$Revision: 236 $]->[1]; | |||||||||
| 13 | |||||||||||
| 14 | 5 | our $DIRN = 8; # number of ways to move (don't count ".") | |||||||||
| 15 | 5 | our @DIRS = ('n','s','e','w','ne','se','nw','sw', '.'); # names of dirs (zero indexed array) | |||||||||
| 16 | 5 | our @DD = ([0,-1],[0,1],[1,0],[-1,0],[1,-1],[1,1],[-1,-1],[-1,1],[0,0]); # map offsets caused by moving in these dirs | |||||||||
| 17 | 5 | our %DD = ('n'=>[0,-1],'s'=>[0,1],'e'=>[1,0],'w'=>[-1,0],'ne'=>[1,-1],'se'=>[1,1],'nw'=>[-1,-1],'sw'=>[-1,1], '.'=>[0,0]); # name/to/offset map | |||||||||
| 18 | 5 | our %DI = ('n'=>0,'s'=>1,'e'=>2,'w'=>3,'ne'=>4,'se'=>5,'nw'=>6,'sw'=>7,'.'=>8); # name/to/index map | |||||||||
| 19 | 5 | our @CWDIRS = ('n','ne','e','se','s','sw','w','nw'); #clockwise directions | |||||||||
| 20 | |||||||||||
| 21 | 5 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 5 | require Exporter; | |||||||||
| 23 | 5 | *{import} = \&Exporter::import; | |||||||||
| 24 | 5 | our @EXPORT_OK = qw(min max ardel rarr distance randsort intify randi $DIRN @DD %DD %DI @DIRS @CWDIRS round rpad); | |||||||||
| 25 | 5 | our %EXPORT_TAGS = (all=>\@EXPORT_OK); | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 5 | ▶ | use Games::Roguelike::Area; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | # try to load C version for speed | ||||||||||
| 31 | 0 | ▶ | eval 'use Games::Roguelike::Utils::Pov_C'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||
| 32 | |||||||||||
| 33 | 5 | ▶ | if (!defined(&distance)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 6214 | ▶ | eval(' | ||||||||
|
Subroutine
distance: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 35 | sub distance { | ||||||||||
| 36 | return sqrt(($_[0]-$_[2])*($_[0]-$_[2])+($_[1]-$_[3])*($_[1]-$_[3])); | ||||||||||
| 37 | } | ||||||||||
| 38 | '); | ||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 17 | ▶ | sub intify { | ||||||||
|
Subroutine
intify: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 42 | 17 | for (@_) { | |||||||||
| 43 | 34 | $_=int($_); | |||||||||
| 44 | } | ||||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 5 | ▶ | sub randsort { | ||||||||
|
Subroutine
randsort: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 48 | 5 | my @a = @_; | |||||||||
| 49 | 5 | my @d; | |||||||||
| 50 | 5 | ▶ | while (@a) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 32 | push @d, splice(@a, rand()*scalar(@a), 1); | |||||||||
| 52 | } | ||||||||||
| 53 | 5 | return @d; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 6279 | ▶ | sub round { | ||||||||
|
Subroutine
round: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 57 | 6279 | return int($_[0]+0.5); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 2672 | ▶ | sub randi { | ||||||||
|
Subroutine
randi: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 61 | 2672 | my ($a, $b) = @_; | |||||||||
| 62 | 2672 | ▶ | if ($b) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | # rand num between a and b, inclusive | ||||||||||
| 64 | 0 | return $a+int(rand()*($b-$a+1)); | |||||||||
| 65 | } else { | ||||||||||
| 66 | # rand num between 0 and a-1 | ||||||||||
| 67 | 2672 | return int(rand()*$a); | |||||||||
| 68 | } | ||||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | sub ardel { | ||||||||
|
Subroutine
ardel: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 72 | 0 | my ($ar, $t) = @_; | |||||||||
| 73 | 0 | ▶ | for (my $i=0;$i<=$#{$ar};++$i) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | ▶ | splice(@{$ar},$i,1) if $ar->[$i] eq $t; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | } | ||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 2 | ▶ | sub max { | ||||||||
|
Subroutine
max: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 79 | 2 | my ($a, $b) = @_; | |||||||||
| 80 | 2 | ▶ | return $a >= $b ? $a : $b; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 9 | ▶ | sub min { | ||||||||
|
Subroutine
min: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 84 | 9 | my ($a, $b) = @_; | |||||||||
| 85 | 9 | ▶ | return $a <= $b ? $a : $b; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | sub rarr { | ||||||||
|
Subroutine
rarr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 89 | 0 | my ($arr) = @_; | |||||||||
| 90 | 0 | return $arr->[$#{$arr}*rand()]; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | sub rpad { | ||||||||
|
Subroutine
rpad: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 94 | 0 | my ($str, $len, $char) = @_; | |||||||||
| 95 | 0 | ▶ | $char = ' ' if $char eq ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | $str .= $char x ($len - length($str)); | |||||||||
| 97 | 0 | return $str; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | =head1 NAME | ||||||||||
| 101 | |||||||||||
| 102 | Games::Roguelike::Utils - Convenience functions and exports for roguelikes | ||||||||||
| 103 | |||||||||||
| 104 | =head1 SYNOPSIS | ||||||||||
| 105 | |||||||||||
| 106 | use Games::Roguelike::Utils qw(:all); | ||||||||||
| 107 | |||||||||||
| 108 | =head1 DESCRIPTION | ||||||||||
| 109 | |||||||||||
| 110 | Non-object oriented functions that are generally helpful for roguelike programming, and are used by other roguelike modules. | ||||||||||
| 111 | |||||||||||
| 112 | =head2 FUNCTIONS | ||||||||||
| 113 | |||||||||||
| 114 | =over | ||||||||||
| 115 | |||||||||||
| 116 | =item min (a, b) | ||||||||||
| 117 | |||||||||||
| 118 | =item max (a, b) | ||||||||||
| 119 | |||||||||||
| 120 | Returns min/max of 2 passed values | ||||||||||
| 121 | |||||||||||
| 122 | =item distance(x1, y1, x2, y2); | ||||||||||
| 123 | |||||||||||
| 124 | Returns the distance between 2 points, uses Inline C version if available | ||||||||||
| 125 | |||||||||||
| 126 | =item randsort(array); | ||||||||||
| 127 | |||||||||||
| 128 | Randomly sorts its arguments and returns the random array. | ||||||||||
| 129 | |||||||||||
| 130 | =item randi (a[, b]) | ||||||||||
| 131 | |||||||||||
| 132 | With 2 arguments, returns a random integer from a to b, inclusive. | ||||||||||
| 133 | |||||||||||
| 134 | With 1 argument, returns a random integer form 0 to a-1. | ||||||||||
| 135 | |||||||||||
| 136 | =item rpad (string, length [, char]) | ||||||||||
| 137 | |||||||||||
| 138 | Pads string out to length using spaces or "char" if one is specified. | ||||||||||
| 139 | |||||||||||
| 140 | =back | ||||||||||
| 141 | |||||||||||
| 142 | =head2 VARIABLES | ||||||||||
| 143 | |||||||||||
| 144 | =over | ||||||||||
| 145 | |||||||||||
| 146 | =item %DD - direction delta hash | ||||||||||
| 147 | |||||||||||
| 148 | Hash mapping direction names to array ref offsets. | ||||||||||
| 149 | |||||||||||
| 150 | 'n' =>[0,-1], # north decreases y, and leaves x alone | ||||||||||
| 151 | ... | ||||||||||
| 152 | 'se'=>[1, 1], # southeast increases y, and increases x | ||||||||||
| 153 | |||||||||||
| 154 | =item @DD - direction delta list | ||||||||||
| 155 | |||||||||||
| 156 | Array with delta entries as above, sorted as: 'n','s','e','w','ne','se','nw','sw', '.' | ||||||||||
| 157 | |||||||||||
| 158 | =item @DIRS - direction list | ||||||||||
| 159 | |||||||||||
| 160 | The array ('n','s','e','w','ne','se','nw','sw','.') | ||||||||||
| 161 | |||||||||||
| 162 | =item @CWDIRS - clockwise direction list | ||||||||||
| 163 | |||||||||||
| 164 | The array ('n','ne','e','se','s','sw','w','nw'), used in door-ok and other quadrant-scanning algorithms. | ||||||||||
| 165 | |||||||||||
| 166 | =item @DI - direction index | ||||||||||
| 167 | |||||||||||
| 168 | Maps 0=>'n', 1=>'s' ... etc. as in @DIRS | ||||||||||
| 169 | |||||||||||
| 170 | =item @DIRN - number of directions (8) | ||||||||||
| 171 | |||||||||||
| 172 | =back | ||||||||||
| 173 | |||||||||||
| 174 | =head1 SEE ALSO | ||||||||||
| 175 | |||||||||||
| 176 | L<Games::Roguelike::World> | ||||||||||
| 177 | |||||||||||
| 178 | =head1 AUTHOR | ||||||||||
| 179 | |||||||||||
| 180 | Erik Aronesty C<earonesty@cpan.org> | ||||||||||
| 181 | |||||||||||
| 182 | =head1 LICENSE | ||||||||||
| 183 | |||||||||||
| 184 | This program is free software; you can redistribute it and/or | ||||||||||
| 185 | modify it under the same terms as Perl itself. | ||||||||||
| 186 | |||||||||||
| 187 | See L<http://www.perl.com/perl/misc/Artistic.html> or the included LICENSE file. | ||||||||||
| 188 | |||||||||||
| 189 | =cut | ||||||||||
| 190 | |||||||||||
| 191 | 5 | 1; | |||||||||