ScottAdams/Action.pm

filter: Statementstmt 94.3% 101 / 107 Branchbran 72.5% 29 / 40 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 13 totaltotal 81.4% 154 / 189 CC 27 SCAR 23.1
CC
27
Cov
87%
compile
16.5
_lookup
12.9
comment
2.0
CRAP 10.1
« ScottAdams/Parse.pm ↑ Summary ScottAdams/File.pm »
1 # $Id: Action.pm,v 1.5 2006-11-04 10:11:11 mike Exp $
2  
3 # Action.pm - an action in a Scott Adams game.
4  
5 package Games::ScottAdams::Action;
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 65 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
10 65 my $class = shift();
11 65 my($verb, $noun, $num) = @_;
12  
13 65 return bless {
14 verb => $verb,
15 noun => $noun,
16 num => $num, # 0-based index into Game's list of actions
17 ### I don't think we actually use this
18 comment => undef, # optional comment to be written through
19 cond => [], # array of conditions to be satisfied
20 res => [], # array of results to be executed
21 }, $class;
22 }
23  
24  
25 65 ▶sub verb {
Subroutine verb: called CC=1 SCAR=0.0
Pod undocumented
26 65 my $this = shift();
27 65 return $this->{verb};
28 }
29  
30 65 ▶sub noun {
Subroutine noun: called CC=1 SCAR=0.0
Pod undocumented
31 65 my $this = shift();
32 65 return $this->{noun};
33 }
34  
35 73 ▶sub comment {
Subroutine comment: called CC=2 SCAR=6.9
Pod undocumented
36 73 my $this = shift();
37 73 my($name) = @_;
38  
39 73 my $old = $this->{comment};
40 73 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 4 69 73
41 4 $this->{comment} = $name;
42 }
43 73 return $old;
44 }
45  
46  
47 # We'd like to compile these up front so we can complain about
48 # unrecognised condition and actions while we still know where we are
49 # in the source file. Unfortunately, we can't do it in general as the
50 # action may refer to the names of rooms or items that have not yet
51 # been defined. So all we can do at this stage is remember them for
52 # later.
53 #
54 59 ▶sub add_cond {
Subroutine add_cond: called CC=1 SCAR=0.0
Pod undocumented
55 59 my $this = shift();
56 59 my($text) = @_;
57  
58 59 push @{ $this->{cond} }, $text;
59 }
60  
61  
62 121 ▶sub add_result {
Subroutine add_result: called CC=1 SCAR=0.0
Pod undocumented
63 121 my $this = shift();
64 121 my($text) = @_;
65  
66 121 push @{ $this->{res} }, $text;
67 }
68  
69  
70 # PRIVATE to the compile() method.
71 149 ▶sub ARG_NONE { 0 } # no argument
Subroutine ARG_NONE: called CC=1 SCAR=0.0
Pod undocumented
72 131 ▶sub ARG_NUM { 1 } # argument specifies a flag
Subroutine ARG_NUM: called CC=1 SCAR=0.0
Pod undocumented
73 91 ▶sub ARG_ROOM { 2 } # argument identifies a room
Subroutine ARG_ROOM: called CC=1 SCAR=0.0
Pod undocumented
74 82 ▶sub ARG_ITEM { 3 } # argument identifies an item
Subroutine ARG_ITEM: called CC=1 SCAR=0.0
Pod undocumented
75 2 ▶sub ARG_ITEMROOM { 4 } # arguments identify an item and a room
Subroutine ARG_ITEMROOM: called CC=1 SCAR=0.0
Pod undocumented
76 2 ▶sub ARG_ITEMITEM { 5 } # arguments identify two items
Subroutine ARG_ITEMITEM: called CC=1 SCAR=0.0
Pod undocumented
77  
78 1 ▶use vars qw(%_cond %_res); # Global as they need to be visible to "sad"
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 %_cond = (
80 carried => [ 1, ARG_ITEM ],
81 here => [ 2, ARG_ITEM ],
82 accessible => [ 3, ARG_ITEM ],
83 at => [ 4, ARG_ROOM ],
84 '!here' => [ 5, ARG_ITEM ],
85 '!carried' => [ 6, ARG_ITEM ],
86 '!at' => [ 7, ARG_ROOM ],
87 flag => [ 8, ARG_NUM ],
88 '!flag' => [ 9, ARG_NUM ],
89 loaded => [ 10, ARG_NONE ],
90 '!loaded' => [ 11, ARG_NONE ],
91 '!accessible' => [ 12, ARG_ITEM ],
92 exists => [ 13, ARG_ITEM ],
93 '!exists' => [ 14, ARG_ITEM ],
94 counter_le => [ 15, ARG_NUM ],
95 # counter_ge => [ 16, ARG_NUM ],
96 counter_gt => [ 16, ARG_NUM ],
97 # ### The documentation accompanying the scottfree
98 # interpreter says that condition 16 tests for
99 # current counter's value greater than or equal
100 # to the argument, but inspection of the source
101 # shows that it actually tests for strictly
102 # greater-than.
103 '!moved' => [ 17, ARG_ITEM ],
104 moved => [ 18, ARG_ITEM ],
105 counter_eq => [ 19, ARG_NUM ],
106 );
107  
108 1 %_res = (
109 get => [ 52, ARG_ITEM ],
110 drop => [ 53, ARG_ITEM ],
111 moveto => [ 54, ARG_ROOM ],
112 destroy => [ 55, ARG_ITEM ],
113 set_dark => [ 56, ARG_NONE ],
114 clear_dark => [ 57, ARG_NONE ],
115 set_flag => [ 58, ARG_NUM ],
116 destroy2 => [ 59, ARG_ITEM ],
117 # Same as 55 in ScottFree
118 clear_flag => [ 60, ARG_NUM ],
119 die => [ 61, ARG_NONE ],
120 put => [ 62, ARG_ITEMROOM ],
121 game_over => [ 63, ARG_NONE ],
122 look => [ 64, ARG_NONE ],
123 score => [ 65, ARG_NONE ],
124 inventory => [ 66, ARG_NONE ],
125 set_0 => [ 67, ARG_NONE ],
126 clear_0 => [ 68, ARG_NONE ],
127 refill_lamp => [ 69, ARG_NONE ], ### UNTESTED
128 clear_screen => [ 70, ARG_NONE ], ### UNTESTED
129 save_game => [ 71, ARG_NONE ],
130 swap => [ 72, ARG_ITEMITEM ],
131 continue => [ 73, ARG_NONE ], ### UNTESTED
132 # Automatic -- is there ever any need to use it explicitly?
133 superget => [ 74, ARG_ITEM ], ### UNTESTED
134 put_with => [ 75, ARG_ITEMITEM ],
135 look2 => [ 76, ARG_NONE ], ### UNTESTED
136 # Same as 64 in ScottFree
137 decrease_counter => [ 77, ARG_NONE ],
138 print_counter => [ 78, ARG_NONE ],
139 set_counter => [ 79, ARG_NUM ],
140 swap_loc_default => [ 80, ARG_NONE ],
141 select_counter => [ 81, ARG_NUM ], ### UNTESTED
142 # Current counter is swapped with specified backup counter
143 add_counter => [ 82, ARG_NUM ], ### UNTESTED
144 subtract_counter => [ 83, ARG_NUM ], ### UNTESTED
145 print_noun => [ 84, ARG_NONE ],
146 print_noun_nl => [ 85, ARG_NONE ],
147 nl => [ 86, ARG_NONE ],
148 swap_loc => [ 87, ARG_NUM ],
149 pause => [ 88, ARG_NONE ],
150 special => [ 89, ARG_NUM ],
151 # This is special -- see ../../../../scottfree/Definition
152 );
153  
154  
155 65 ▶sub compile {
Subroutine compile: called CC=16 SCAR=28.0
Pod undocumented
156 65 my $this = shift();
157 65 my($game) = @_;
158  
159 65 my $verb = $game->resolve_verb($this->verb());
160 65 my $noun = $this->noun();
161 65 ▶ if ($verb == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verb == 0) { } 18 47 65
162 # This is a %occur, so the noun is a percentage probability
163 18 ▶ $noun = 100 if !$noun;
Branch: 1 branch
BranchTrueFalseTotal
unless $noun 18 0 18
164 } else {
165 47 $noun = $game->resolve_noun($noun);
166 }
167  
168 65 my @condval = ( 150*$verb + $noun );
169 65 foreach my $cond (@{ $this->{cond} }) {
170 59 my($opcode, $arg) = _lookup($game, $cond, 'condition', \%_cond);
171 59 ▶ $arg = 0 if !defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 59 59
172 59 push @condval, $opcode + 20*$arg;
173 }
174  
175 65 ▶ die "Oops! SA format doesn't support >5 conditions in an action"
Branch: 1 branch
BranchTrueFalseTotal
if @condval > 6 0 65 65
176 if @condval > 6;
177  
178 # Now gather results, with parameters going on the end of @condval
179 #warn "handling results:\n" . join ('', map {"\t$_\n"}
180 # @{ $this->{res} });
181 65 my @resval;
182 65 foreach my $res (@{ $this->{res} }) {
183 121 my($opcode, @arg) = _lookup($game, $res, 'result', \%_res);
184 121 push @resval, [ $opcode, @arg ];
185 }
186  
187 # Right. This is slightly tricky. We now want to pack all the
188 # results, together with their parameters, into as few action
189 # octuplets as possible. We have four result slots available in
190 # the first one, together with zero or more parameter slots
191 # remaining in the condition area; thereafter, each action
192 # octuplet offers four more result slots together with five
193 # parameter slots in the condition area (which of course is one
194 # more than we'll ever need.)
195 65 my @conds; # list of completed octuplets
196 65 my $argslot = @condval; # 0-based index within current octuplet
197 65 my $resslot = 0; # 0-based index into "virtual array"
198 65 push @condval, map { 0 } 1..(8-@condval);
199  
200 65 ▶ for (my $i = 0; $i < @resval; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @resval) 121 65 186
201 121 my $res = $resval[$i];
202 121 my($opcode, @arg) = @$res;
203 121 @arg = grep { defined } @arg;
204  
205 ### Seems like 6 in next line should be 5. Think harder.
206 121 ▶ if ($argslot + @arg > 6 || $resslot == 4 ||
Branch: 1 branch
BranchTrueFalseTotal
if ($argslot + @arg > 6 or $resslot == 4 or $resslot == 3 and $i < @resval - 1) 6 115 121
Condition66%6/9
$argslot + @arg > 6 or $resslot == 4 or $resslot == 3 and $i < @resval - 1
l!l&&r!l&&!r
06115
$argslot + @arg > 6 or $resslot == 4
l!l&&r!l&&!r
00121
$resslot == 3 and $i < @resval - 1
!ll&&!rl&&r
11056
MC/DC50%2/4
$argslot + @arg > 6 or $resslot == 4 or $resslot == 3 and $i < @resval - 1
$argslot + @arg > 6 $resslot == 4 $resslot == 3 $i < @resval - 1
Truth table60%3/5
A or B or C and D
A= $argslot + @arg > 6B= $resslot == 4C= $resslot == 3D= $i < @resval - 1
ABCDresult
1XXX1
01XX1
00111
000X0
00100
207 ($resslot == 3 && $i < @resval-1)) {
208 # Current octuplet is full: skip to next
209 6 my $cindex = 6 + int($resslot/2);
210 6 ▶ $condval[$cindex] +=
Branch: 1 branch
BranchTrueFalseTotal
$resslot % 2 == 0 ? : 0 6 6
211 ($resslot % 2 == 0 ? 150 : 1) * 73;
212 6 push @conds, join(' ', @condval);
213 6 @condval = map { 0 } 1..8;
214 6 $argslot = 1; # because slot 0 holds verb & noun
215 6 $resslot = 0;
216 }
217  
218 121 my $cindex = 6 + int($resslot/2);
219 121 ▶ $condval[$cindex] +=
Branch: 1 branch
BranchTrueFalseTotal
$resslot % 2 == 0 ? : 88 33 121
220 ($resslot % 2 == 0 ? 150 : 1) * $opcode;
221 121 $resslot++;
222 121 foreach my $arg (@arg) {
223 60 ▶ if (!defined $arg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg) 0 60 60
224 0 print STDERR "", "arg in '@arg' (", scalar(@arg), ") undef\n";
225 }
226 60 $condval[$argslot] = 20*$arg;
227 60 $argslot++;
228 }
229 }
230  
231 65 push @conds, join(' ', @condval);
232 #print STDERR "", "returning conds: ", join(' -- ', @conds), "\n";
233 65 return @conds;
234 }
235  
236  
237 180 ▶sub _lookup {
Subroutine _lookup: called CC=11 SCAR=25.6
238 180 my($game, $text, $caption, $href) = @_;
239  
240 180 $text =~ s/^\s+//;
241 180 my($op, $arg) = split /\s+/, $text, 2;
242 180 ▶ if ($op eq 'msg') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "msg") 54 126 180
243 # This check is a hack, but does spot an otherwise subtle bug
244 54 ▶ die "Oops! `msg' used as a condition (missing %result line?)"
Branch: 1 branch
BranchTrueFalseTotal
if $caption eq "condition" 0 54 54
245 if $caption eq 'condition';
246  
247 54 my $mnum = $game->resolve_message($arg);
248 54 ▶ return ($mnum <= 51 ? $mnum : $mnum+50);
Branch: 1 branch
BranchTrueFalseTotal
$mnum <= 51 ? : 54 0 54
249 }
250  
251 126 my $ref = $href->{$op};
252 126 ▶ die "unrecognised $caption op '$op'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 0 126 126
253 if !defined $ref;
254  
255 126 my($opcode, $argtype) = @$ref;
256 126 ▶ if ($argtype == ARG_NONE) {
Branch: 6 branches
BranchTrueFalseTotal
if ($argtype == ARG_NONE()) { } 8 118 126
elsif ($argtype == ARG_NUM()) { } 30 88 118
elsif ($argtype == ARG_ROOM()) { } 21 67 88
elsif ($argtype == ARG_ITEM()) { } 66 1 67
elsif ($argtype == ARG_ITEMROOM()) { } 1 0 1
elsif ($argtype == ARG_ITEMITEM()) { } 0 0 0
257 8 return ($opcode);
258 } elsif ($argtype == ARG_NUM) {
259 # Numeric argument already has the right numeric value.
260 } elsif ($argtype == ARG_ROOM) {
261 21 $arg = $game->resolve_room($arg, 'action');
262 } elsif ($argtype == ARG_ITEM) {
263 66 $arg = $game->resolve_item($arg, 'action');
264 } elsif ($argtype == ARG_ITEMROOM) {
265 1 my($arg1, $arg2) = split /\s+/, $arg, 2;
266 1 $arg1 = $game->resolve_item($arg1, 'action');
267 1 $arg2 = $game->resolve_room($arg2, 'action');
268 1 return ($opcode, $arg1, $arg2);
269 } elsif ($argtype == ARG_ITEMITEM) {
270 0 my($arg1, $arg2) = split /\s+/, $arg, 2;
271 0 $arg1 = $game->resolve_item($arg1, 'action');
272 0 $arg2 = $game->resolve_item($arg2, 'action');
273 0 return ($opcode, $arg1, $arg2);
274 } else {
275 0 die "unsupported argument type $argtype for op '$op'";
276 }
277  
278 117 return ($opcode, $arg);
279 }
280  
281  
282 1 1;
« ScottAdams/Parse.pm ↑ Summary ScottAdams/File.pm »