File Coverage

blib/lib/Games/Roguelike/Utils.pm
Criterion Covered Total %
statement 27 42 64.2
branch 4 10 40.0
condition n/a
subroutine 11 14 78.5
pod 6 10 60.0
total 48 76 63.1


line stmt bran cond sub pod time code
1             # For license, docs, see the POD documentation at the end of this file
2              
3             package Games::Roguelike::Utils;
4              
5 5     5   7428 use strict;
  5         10  
  5         1683  
6              
7             # this breaks under perl < 5.8
8             # use Exporter qw(import);
9              
10             # Direction helpers
11              
12             our $VERSION = '0.4.' . [qw$Revision: 236 $]->[1];
13              
14             our $DIRN = 8; # number of ways to move (don't count ".")
15             our @DIRS = ('n','s','e','w','ne','se','nw','sw', '.'); # names of dirs (zero indexed array)
16             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             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             our %DI = ('n'=>0,'s'=>1,'e'=>2,'w'=>3,'ne'=>4,'se'=>5,'nw'=>6,'sw'=>7,'.'=>8); # name/to/index map
19             our @CWDIRS = ('n','ne','e','se','s','sw','w','nw'); #clockwise directions
20              
21             BEGIN {
22 5     5   35 require Exporter;
23 5         23 *{import} = \&Exporter::import;
24 5         41 our @EXPORT_OK = qw(min max ardel rarr distance randsort intify randi $DIRN @DD %DD %DI @DIRS @CWDIRS round rpad);
25 5         125 our %EXPORT_TAGS = (all=>\@EXPORT_OK);
26             }
27              
28 5     5   6121 use Games::Roguelike::Area;
  5         16  
  5         11660  
29              
30             # try to load C version for speed
31 5     5   137 eval 'use Games::Roguelike::Utils::Pov_C';
  0         0  
  0         0  
32              
33             if (!defined(&distance)) {
34 9054     9054 1 54921 eval('
35             sub distance {
36             return sqrt(($_[0]-$_[2])*($_[0]-$_[2])+($_[1]-$_[3])*($_[1]-$_[3]));
37             }
38             ');
39             }
40              
41             sub intify {
42 25     25 0 108 for (@_) {
43 50         155 $_=int($_);
44             }
45             }
46              
47             sub randsort {
48 5     5 1 24 my @a = @_;
49 5         12 my @d;
50 5         20 while (@a) {
51 41         120 push @d, splice(@a, rand()*scalar(@a), 1);
52             }
53 5         29 return @d;
54             }
55              
56             sub round {
57 8355     8355 0 59758 return int($_[0]+0.5);
58             }
59              
60             sub randi {
61 2634     2634 1 3441 my ($a, $b) = @_;
62 2634 50       4488 if ($b) {
63             # rand num between a and b, inclusive
64 0         0 return $a+int(rand()*($b-$a+1));
65             } else {
66             # rand num between 0 and a-1
67 2634         8438 return int(rand()*$a);
68             }
69             }
70              
71             sub ardel {
72 0     0 0 0 my ($ar, $t) = @_;
73 0         0 for (my $i=0;$i<=$#{$ar};++$i) {
  0         0  
74 0 0       0 splice(@{$ar},$i,1) if $ar->[$i] eq $t;
  0         0  
75             }
76             }
77              
78             sub max {
79 2     2 1 4 my ($a, $b) = @_;
80 2 50       8 return $a >= $b ? $a : $b;
81             }
82              
83             sub min {
84 9     9 1 19 my ($a, $b) = @_;
85 9 100       47 return $a <= $b ? $a : $b;
86             }
87              
88             sub rarr {
89 0     0 0   my ($arr) = @_;
90 0           return $arr->[$#{$arr}*rand()];
  0            
91             }
92              
93             sub rpad {
94 0     0 1   my ($str, $len, $char) = @_;
95 0 0         $char = ' ' if $char eq '';
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
177              
178             =head1 AUTHOR
179              
180             Erik Aronesty C
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 or the included LICENSE file.
188              
189             =cut
190              
191             1;