Item.pm

filter: Statementstmt 66.6% 46 / 69 Branchbran 41.6% 15 / 36 Conditioncond 33.3% 3 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 53.8% 7 / 13 Podpod 100% 6 / 6 totaltotal 56.1% 78 / 139 CC 22 SCAR 27.0
CC
22
Cov
56%
AUTOLOAD
42.0
setcont
16.2
x
6.0
CRAP 14.9
« World/Daemon.pm ↑ Summary Utils.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Games::Roguelike::Item;
4  
5 1 ▶use Games::Roguelike::Utils qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Games::Roguelike::Console;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Games::Roguelike::Area;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw(croak confess carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $AUTOLOAD;
11  
12 =head1 NAME
13  
14 Games::Roguelike::Item - Roguelike item object
15  
16 =head1 SYNOPSIS
17  
18 package myItem;
19 use base 'Games::Roguelike::Item';
20  
21 $i = myItem->new($area, sym=>'!', x=>5,y=>6); # creates an item at location 5, 6
22 # with symbol '!', inside area $area
23  
24 =head1 DESCRIPTION
25  
26 Item object used by drawing routines in Roguelke::Area
27  
28 =head2 METHODS
29  
30 =over 4
31  
32 =cut
33  
34 =item new($container, %opts)
35  
36 $container is usually an "area" or "mob" or another "item" object.
37  
38 At a minimum, it must support the additem and delitem methods.
39  
40 The "new" method automatically calls additem on the container.
41  
42 Options include:
43  
44 sym : symbol this item is drawn with
45 color : color this item is drawn with
46 x : map location of item
47 y : map location of item
48  
49 Other options are saved in the hash as "user defined" options.
50  
51 =cut
52  
53 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
54 3 my $pkg = shift;
55  
56 3 my $cont = shift;
57  
58 3 ▶ croak("can't create item without container argument")
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($cont, "additem") 0 3 3
59 unless UNIVERSAL::can($cont, 'additem');
60
61 3 my $self = {};
62  
63 3 $self->{sym}='$'; # default, just so there is one
64 3 $self->{color}='bold yellow'; # default
65  
66 3 ▶ while( my ($k, $v) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 24 3 27
67 24 $self->{$k} = $v;
68 }
69
70 3 bless $self, $pkg;
71  
72 3 $cont->additem($self);
73  
74 3 return $self;
75 }
76  
77 =item x()
78  
79 =item y()
80  
81 Return the item's x/y members only if the item is in an ::Area object,
82  
83 Otherwise, return the container's x and y members.
84  
85 Direct access to the $item->{x}/{y} members is encouraged if you don't care how it's contained.
86  
87 =cut
88  
89 0 ▶sub x {
Subroutine x: not called CC=2 SCAR=17.9
Pod documented
90 0 my $self = shift;
91 0 ▶ if (!$self->{inarea}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inarea"}) 0 0 0
92 0 return $self->{cont}->x;
93 }
94 0 return $self->{x};
95 }
96  
97 0 ▶sub y {
Subroutine y: not called CC=2 SCAR=17.9
Pod documented
98 0 my $self = shift;
99 0 ▶ if (!$self->{inarea}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inarea"}) 0 0 0
100 0 return $self->{cont}->y;
101 }
102 0 return $self->{y};
103 }
104  
105 =item setcont(newcont)
106  
107 Sets the container for an item, returns 0 if it's already contained within that continer.
108  
109 Dies if the container has no {items} list (ie: can't contain things)
110  
111 ** Should only ever be called**
112 - by the container's "additem" method, and
113 - only if the container is derived from ::Area, ::Mob or ::Item.
114  
115 (This can & will be made generic at some point)
116  
117 =cut
118  
119 3 ▶sub setcont {
Subroutine setcont: called CC=13 SCAR=27.9
Pod documented
120 3 my $self = shift;
121 3 my $cont = shift;
122  
123 3 ▶ confess("not an item") unless $self->isa('Games::Roguelike::Item');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa("Games::Roguelike::Item") 0 3 3
124 3 ▶ confess("not an container") unless ref($cont->{items}) && UNIVERSAL::can($cont, 'additem');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cont->{"items"} and UNIVERSAL::can($cont, "additem") 0 3 3
Condition33%1/3
ref $cont->{"items"} and UNIVERSAL::can($cont, "additem")
!ll&&!rl&&r
003
MC/DC0%0/2
ref $cont->{"items"} and UNIVERSAL::can($cont, "additem")
ref $cont->{"items"} UNIVERSAL::can($cont, "additem")
Truth table33%1/3
A and B
A= ref $cont->{"items"}B= UNIVERSAL::can($cont, "additem")
ABresult
0X0
100
111
125  
126 3 ▶ if ($cont) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cont) { } 3 0 3
127 3 ▶ if (!defined($self->{cont}) || $cont != $self->{cont}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"cont"} or $cont != $self->{"cont"}) 2 1 3
Condition66%2/3
not defined $self->{"cont"} or $cont != $self->{"cont"}
l!l&&r!l&&!r
201
MC/DC50%1/2
not defined $self->{"cont"} or $cont != $self->{"cont"}
not defined $self->{"cont"} $cont != $self->{"cont"}
Truth table66%2/3
not(A) or B
A= not defined $self->{"cont"}B= $cont != $self->{"cont"}
ABresult
1X1
011
000
128 2 ▶ $self->{in} = $cont->isa('Games::Roguelike::Area') ? 'area'
Branch: 3 branches
BranchTrueFalseTotal
$cont->isa('Games::Roguelike::Area') ? : 1 1 2
$cont->isa('Games::Roguelike::Mob') ? : 1 0 1
$cont->isa('Games::Roguelike::Item') ? : 0 0 0
129 : $cont->isa('Games::Roguelike::Mob') ? 'mob'
130 : $cont->isa('Games::Roguelike::Item') ? 'item' : 'void';
131  
132 2 $self->{"in" . $self->{in}} = 1;
133  
134 # for now, do this, until interface is better documented
135 die("item must be in an area, mob or another item as a container")
136 2 ▶ if $self->{invoid};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"invoid"} 0 2 2
137  
138 2 ▶ $self->{cont}->delitem($self) if $self->{cont};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cont"} 0 2 2
139 2 push @{$cont->{items}}, $self;
140 2 $self->{cont} = $cont;
141  
142 2 ▶ if ($self->{inarea}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'inarea'}) { } 1 1 2
143 1 $self->{r} = $cont;
144 } else {
145 1 $self->{r} = $cont->{r};
146 }
147  
148 2 return 1;
149 }
150 1 return 0;
151 } else {
152 0 return $self->{cont};
153 }
154 }
155  
156 # perl accessors are slow compared to just accessing the hash directly
157 # autoload is even slower
158 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=6 SCAR=37.4
159 0 my $self = shift;
160 0 ▶ my $pkg = ref($self) or croak("$self is not an object");
Branch: 1 branch
BranchTrueFalseTotal
my $pkg = ref $self or croak("$self is not an object") 0 0 0
161  
162 0 my $name = $AUTOLOAD;
163 0 $name =~ s/.*://; # strip fully-qualified portion
164 0 ▶ $name =~ s/^set// if @_ && !exists $self->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if @_ and not exists $self->{$name} 0 0 0
Condition0%0/3
@_ and not exists $self->{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and not exists $self->{$name}
@_ not exists $self->{$name}
Truth table0%0/3
A and not(B)
A= @_B= not exists $self->{$name}
ABresult
0X0
100
111
165  
166 0 ▶ unless (exists $self->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$name}) 0 0 0
167 0 croak "Can't access `$name' field in class $pkg";
168 }
169  
170 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
171 0 return $self->{$name} = $_[0];
172 } else {
173 0 return $self->{$name};
174 }
175 }
176  
177 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
178 }
179  
180 =item additem (item)
181  
182 Add item to reside within me. Override this to make backpacks, etc.
183  
184 Return value 0 = can't add, too full/or not a backpack
185 Return value 1 = add ok
186 Return value -1 = move occured, but not added
187  
188 Default implementation is to return "0", cannot add.
189  
190 =cut
191  
192 0 ▶sub additem {
Subroutine additem: not called CC=1 SCAR=6.9
Pod documented
193 0 my $self = shift;
194 0 return 0; # i'm not a backpack
195 }
196  
197 =item delitem (item)
198  
199 Deletes item from within me. Override this to make backpacks, etc.
200  
201 =cut
202  
203 0 ▶sub delitem {
Subroutine delitem: not called CC=1 SCAR=6.9
Pod documented
204 0 my $self = shift;
205 0 croak("this should never be called, since additem always returns 0");
206 }
207  
208 =back
209  
210 =head1 SEE ALSO
211  
212 L<Games::Roguelike::Area>
213  
214 =cut
215  
216 1 1;
« World/Daemon.pm ↑ Summary Utils.pm »