blib/lib/NetHack/PriceID.pm

filter: Statementstmt 96.2% 103 / 107 Branchbran 87.0% 47 / 54 Conditioncond 81.8% 9 / 11 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 91.7% 178 / 194 CC 33 SCAR 21.9
CC
33
Cov
92%
priceid_buy
17.0
priceid_sell
7.0
_canonicalize_args
5.0
CRAP 9.0
↑ Summary
1 package NetHack::PriceID;
2 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 $NetHack::PriceID::AUTHORITY = 'cpan:SARTAK';
4 }
5 {
6 9 $NetHack::PriceID::VERSION = '0.05';
7 }
8 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 # ABSTRACT: identify NetHack items using shopkeepers
12  
13 9 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 our @EXPORT_OK = qw(priceid priceid_buy priceid_sell priceid_base);
15 9 our %EXPORT_TAGS = ('all' => \@EXPORT_OK);
16  
17 9 our %glyph2type = (
18 '"' => 'amulet',
19 '?' => 'scroll',
20 '+' => 'spellbook',
21 '=' => 'ring',
22 '!' => 'potion',
23 '/' => 'wand',
24 '(' => 'tool',
25 '[' => 'armor',
26 );
27  
28 9 our %item_table = (
29 amulet => {
30 0 => ['cheap plastic imitation of the Amulet of Yendor'],
31 150 => ['change', 'ESP', 'life saving', 'magical breathing',
32 'reflection', 'restful sleep', 'strangulation',
33 'unchanging', 'versus poison'],
34 30000 => ['Amulet of Yendor'],
35 },
36  
37 scroll => {
38 20 => ['identify'],
39 50 => ['light'],
40 60 => ['blank paper', 'enchant weapon'],
41 80 => ['enchant armor', 'remove curse'],
42 100 => ['confuse monster', 'destroy armor', 'fire',
43 'food detection', 'gold detection', 'magic mapping',
44 'scare monster', 'teleportation'],
45 200 => ['amnesia', 'create monster', 'earth', 'taming'],
46 300 => ['charging', 'genocide', 'punishment', 'stinking cloud'],
47 },
48  
49 spellbook => {
50 100 => ['detect monsters', 'force bolt', 'healing', 'jumping',
51 'knock', 'light', 'protection', 'sleep'],
52 200 => ['confuse monster', 'create monster', 'cure blindness',
53 'detect food', 'drain life', 'magic missile',
54 'slow monster', 'wizard lock'],
55 300 => ['cause fear', 'charm monster', 'clairvoyance',
56 'cure sickness', 'detect unseen', 'extra healing',
57 'haste self', 'identify', 'remove curse',
58 'stone to flesh'],
59 400 => ['cone of cold', 'detect treasure', 'fireball',
60 'invisibility', 'levitation', 'restore ability'],
61 500 => ['dig', 'magic mapping'],
62 600 => ['create familiar', 'polymorph', 'teleport away',
63 'turn undead'],
64 700 => ['cancellation', 'finger of death'],
65 },
66  
67 potion => {
68 0 => ['uncursed water'],
69 50 => ['booze', 'fruit juice', 'see invisible', 'sickness'],
70 100 => ['confusion', 'extra healing', 'hallucination', 'healing',
71 'restore ability', 'sleeping', '(un)holy water'],
72 150 => ['blindness', 'gain energy', 'invisibility',
73 'monster detection', 'object detection'],
74 200 => ['enlightenment', 'full healing', 'levitation', 'polymorph',
75 'speed'],
76 250 => ['acid', 'oil'],
77 300 => ['gain ability', 'gain level', 'paralysis'],
78 },
79  
80 ring => {
81 100 => ['adornment', 'hunger', 'protection',
82 'protection from shape changers', 'stealth',
83 'sustain ability', 'warning'],
84 150 => ['aggravate monster', 'cold resistance',
85 'gain constitution', 'gain strength', 'increase accuracy',
86 'increase damage', 'invisibility', 'poison resistance',
87 'see invisible', 'shock resistance'],
88 200 => ['fire resistance', 'free action', 'levitation',
89 'regeneration', 'searching', 'slow digestion',
90 'teleportation'],
91 300 => ['conflict', 'polymorph', 'polymorph control',
92 'teleport control'],
93 },
94  
95 wand => {
96 0 => ['uncharged'],
97 100 => ['light', 'nothing'],
98 150 => ['digging', 'enlightenment', 'locking', 'magic missile',
99 'make invisible', 'opening', 'probing',
100 'secret door detection', 'slow monster', 'speed monster',
101 'striking', 'undead turning'],
102 175 => ['cold', 'fire', 'lightning', 'sleep'],
103 200 => ['cancellation', 'create monster', 'polymorph',
104 'teleportation'],
105 500 => ['death', 'wishing'],
106 },
107  
108 bag => {
109 2 => ['sack'],
110 100 => ['bag of holding', 'oilskin sack', 'bag of tricks'],
111 },
112  
113 lamp => {
114 10 => ['oil lamp'],
115 50 => ['magic lamp'],
116 },
117  
118 flute => {
119 12 => ['wooden flute'],
120 36 => ['magic flute'],
121 },
122  
123 horn => {
124 15 => ['tooled horn'],
125 50 => ['fire horn', 'frost horn', 'horn of plenty'],
126 },
127  
128 # shirt => {
129 # 2 => ['T-shirt'],
130 # 3 => ['Hawaiian shirt'],
131 # },
132  
133 # suit => {
134 # },
135  
136 cloak => {
137 50 => ['cloak of displacement', 'cloak of protection', 'oilskin cloak'],
138 60 => ['cloak of invisibility', 'cloak of magic resistance',
139 'elven cloak'],
140 },
141  
142 helmet => {
143 1 => ['dunce cap'],
144 10 => ['helmet'],
145 50 => ['helm of brilliance', 'helm of opposite alignment',
146 'helm of telepathy'],
147 80 => ['cornuthaum'],
148 },
149  
150 gloves => {
151 8 => ['leather gloves'],
152 50 => ['gauntlets of dexterity', 'gauntlets of fumbling',
153 'gauntlets of power'],
154 },
155  
156 # shield => {
157 # 1 => [],
158 # },
159  
160 boots => {
161 8 => ['elven boots', 'kicking boots'],
162 30 => ['fumble boots', 'levitation boots'],
163 50 => ['jumping boots', 'speed boots', 'water walking boots'],
164 },
165 );
166  
167 # dynamically construct a list of all tools from each tool subtype
168 9 for my $in (qw/bag lamp flute horn/) {
169 36 ▶ while (my ($price, $items) = each %{ $item_table{$in} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$item_table{$in};}) 72 36 108
170 72 ▶ @{$item_table{tool}{$price}} = sort @{$item_table{tool}{$price} || []},
Condition100%2/2
$item_table{'tool'}{$price} || []
l!l
963
MC/DC100%1/1
$item_table{'tool'}{$price} || []
$item_table{'tool'}{$price}
Truth table100%2/2
A
A= $item_table{'tool'}{$price}
Aresult
11
00
171 @$items;
172 }
173 }
174  
175 # dynamically construct a list of all armor from each armor subtype
176 9 for my $in (qw/shirt suit cloak helmet gloves shield boots/) {
177 # automatically calculate +1 .. +6
178 63 my @prices = reverse sort keys %{ $item_table{$in} };
179 63 for my $price (@prices) {
180 99 for my $enchantment (1 .. 6) {
181 594 my $newprice = $price + 10 * $enchantment;
182  
183 594 for my $item (@{ $item_table{$in}{$price} }) {
184 1242 push @{ $item_table{$in}{$newprice} }, "+$enchantment $item";
185 }
186 }
187 }
188  
189 63 ▶ while (my ($price, $items) = each %{ $item_table{$in} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$item_table{$in};}) 531 63 594
190 531 ▶ @{$item_table{armor}{$price}} = sort @{$item_table{armor}{$price}||[]},
Condition100%2/2
$item_table{'armor'}{$price} || []
l!l
279252
MC/DC100%1/1
$item_table{'armor'}{$price} || []
$item_table{'armor'}{$price}
Truth table100%2/2
A
A= $item_table{'armor'}{$price}
Aresult
11
00
191 @$items;
192 }
193 }
194  
195 4 ▶sub _croak {
Subroutine _croak: called CC=1 SCAR=0.0
196 4 require Carp;
197 4 Carp::croak(@_);
198 }
199  
200 125 ▶sub priceid {
Subroutine priceid: called CC=4 SCAR=13.9
Pod documented
201 125 my %args = _canonicalize_args(@_);
202 122 my @base;
203  
204 122 ▶ if ($args{in} eq 'sell') {
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'in'} eq 'sell') { } 55 67 122
elsif ($args{'in'} eq 'buy') { } 54 13 67
elsif ($args{'in'} eq 'base') { } 13 0 13
205 55 @base = priceid_sell(%args, out => 'base');
206 }
207 elsif ($args{in} eq 'buy') {
208 54 @base = priceid_buy(%args, out => 'base');
209 }
210 elsif ($args{in} eq 'base') {
211 13 @base = priceid_base(%args, out => 'base');
212 }
213  
214 121 return _canonicalize_output(\%args, @base);
215 }
216  
217 54 ▶sub priceid_buy {
Subroutine priceid_buy: called CC=16 SCAR=28.3
Pod documented
218 54 my %args = _canonicalize_args(@_);
219 54 my @base;
220  
221 _croak "Calculating 'buy' prices requires that you set 'charisma'."
222 54 ▶ if !defined $args{charisma};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"charisma"} 1 53 54
223  
224 53 for my $base (keys %{ $item_table{ $args{type} } }) {
225 572 my $tmp = $base;
226  
227 572 ▶ $tmp = 5 if !$tmp;
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp 4 568 572
228  
229 572 my $surcharge = $tmp + $tmp / 3;
230  
231 572 for ($tmp, $surcharge) {
232 1102 ▶ $_ += $_ / 3 if $args{tourist};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"tourist"} 26 1076 1102
233 1102 ▶ $_ += $_ / 3 if $args{dunce};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"dunce"} 26 1076 1102
234  
235 1102 ▶ if ($args{charisma} > 18) { $_ /= 2 }
Branch: 6 branches
BranchTrueFalseTotal
if ($args{'charisma'} > 18) { } 12 1090 1102
elsif ($args{'charisma'} > 17) { } 0 1090 1090
elsif ($args{'charisma'} > 15) { } 0 1090 1090
elsif ($args{'charisma'} < 6) { } 11 1079 1090
elsif ($args{'charisma'} < 8) { } 0 1079 1079
elsif ($args{'charisma'} < 11) { } 1079 0 1079
236 0 elsif ($args{charisma} > 17) { $_ -= $_ / 3 }
237 0 elsif ($args{charisma} > 15) { $_ -= $_ / 4 }
238 11 elsif ($args{charisma} < 6) { $_ *= 2 }
239 0 elsif ($args{charisma} < 8) { $_ += $_ / 2 }
240 1079 elsif ($args{charisma} < 11) { $_ += $_ / 3 }
241  
242 1102 ▶ $_ = 1 if $_ <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_ <= 0 0 1102 1102
243  
244 1102 ▶ if ($args{angry}) { $_ += ($_ + 2) / 3 }
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"angry"}) 13 1089 1102
245  
246 1102 ▶ if (($_ * $args{quan}) == $args{amount}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ * $args{"quan"} == $args{"amount"}) 59 1043 1102
247 59 push @base, $base;
248 59 last;
249 }
250 }
251 }
252  
253 53 return _canonicalize_output(\%args, @base);
254 }
255  
256 56 ▶sub priceid_sell {
Subroutine priceid_sell: called CC=7 SCAR=19.5
Pod documented
257 56 my %args = _canonicalize_args(@_);
258 56 my @base;
259  
260 56 for my $base (keys %{ $item_table{ $args{type} } }) {
261 791 my $tmp = $base * $args{quan};
262  
263 791 ▶ if ($args{tourist}) { $tmp /= 3 }
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'tourist'}) { } 7 784 791
elsif ($args{'dunce'}) { } 0 784 784
264 0 elsif ($args{dunce}) { $tmp /= 3 }
265 784 else { $tmp /= 2 }
266  
267 791 my $surcharge = $tmp - $tmp / 4;
268 791 ▶ $surcharge = $tmp unless $tmp > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp > 1 41 750 791
269  
270 791 for ($tmp, $surcharge) {
271 1535 ▶ if ($_ == $args{amount}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ == $args{"amount"}) 68 1467 1535
272 68 push @base, $base;
273 68 last;
274 }
275 }
276 }
277  
278 56 return _canonicalize_output(\%args, @base);
279 }
280  
281 15 ▶sub priceid_base {
Subroutine priceid_base: called CC=1 SCAR=0.0
Pod documented
282 15 my %args = _canonicalize_args(@_);
283 15 return _canonicalize_output(\%args, $args{amount});
284 }
285  
286 250 ▶sub _canonicalize_args {
Subroutine _canonicalize_args: called CC=5 SCAR=16.1
287 250 my %args = (
288 in => 'base',
289 out => 'names',
290 quan => 1,
291 @_,
292 );
293  
294 _croak "Price IDing requires that you set 'amount'"
295 250 ▶ if !defined $args{amount};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"amount"} 1 249 250
296  
297 _croak "Price IDing requires that you set 'type'"
298 249 ▶ if !defined $args{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"type"} 1 248 249
299  
300 248 ▶ $args{type} = $glyph2type{ $args{type} } || $args{type};
Condition66%2/3
$NetHack::PriceID::glyph2type{$args{'type'}} || $args{'type'}
l!l&&r!l&&!r
741740
MC/DC0%0/2
$NetHack::PriceID::glyph2type{$args{'type'}} || $args{'type'}
$NetHack::PriceID::glyph2type{$args{'type'}} $args{'type'}
Truth table66%2/3
A || B
A= $NetHack::PriceID::glyph2type{$args{'type'}}B= $args{'type'}
ABresult
1X1
011
000
301  
302 _croak "Unknown item type: $args{type}"
303 248 ▶ if !exists $item_table{ $args{type} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $NetHack::PriceID::item_table{$args{"type"}} 1 247 248
304  
305 247 return %args;
306 }
307  
308 245 ▶sub _canonicalize_output {
Subroutine _canonicalize_output: called CC=5 SCAR=16.1
309 245 my $args = shift;
310  
311 2 ▶ return map { [$_, @{ $item_table{ $args->{type} }{ $_ } || [] }] } sort @_
Condition50%1/2
$NetHack::PriceID::item_table{$args->{'type'}}{$_} || []
l!l
20
MC/DC0%0/1
$NetHack::PriceID::item_table{$args->{'type'}}{$_} || []
$NetHack::PriceID::item_table{$args->{'type'}}{$_}
Truth table50%1/2
A
A= $NetHack::PriceID::item_table{$args->{'type'}}{$_}
Aresult
11
00
312 245 ▶ if $args->{out} eq 'both';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"out"} eq "both" 1 244 245
313  
314 244 ▶ return sort @_ if $args->{out} eq 'base';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"out"} eq "base" 127 117 244
315 117 ▶ return sort map {@{ $item_table{ $args->{type} }{ $_ } || [] }} @_;
Condition100%2/2
$NetHack::PriceID::item_table{$args->{'type'}}{$_} || []
l!l
1331
MC/DC100%1/1
$NetHack::PriceID::item_table{$args->{'type'}}{$_} || []
$NetHack::PriceID::item_table{$args->{'type'}}{$_}
Truth table100%2/2
A
A= $NetHack::PriceID::item_table{$args->{'type'}}{$_}
Aresult
11
00
316 }
317  
318 9 1;
319  
320 __END__
↑ Summary