| 1 | # $Id: Compile.pm,v 1.3 2006-11-01 23:57:42 mike Exp $ | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Compile.pm - back-end compilation functions for Scott Adams game files. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Games::ScottAdams::Game; | ||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | sub compile { | ||||||||||||||||||||||||||
|
Subroutine
compile: called CC=33 SCAR=39.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 1 | my $this = shift(); | |||||||||||||||||||||||||||
| 11 | 1 | my $strict = $this->{-strict}; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | if (0) { | |||||||||||||||||||||||||||
| 14 | print STDERR "debugging aliases:\n"; | ||||||||||||||||||||||||||||
| 15 | my $nv = $this->{nvocab}; | ||||||||||||||||||||||||||||
| 16 | foreach my $word (keys %$nv) { | ||||||||||||||||||||||||||||
| 17 | print STDERR "$word -> ", join(',', @{ $nv->{$word} }), "\n"; | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | my $lightsource = $this->{lightsource}; | |||||||||||||||||||||||||||
| 22 | 1 | ▶ | if (defined $lightsource) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | # Swap nominated item with #9 (1-based) | ||||||||||||||||||||||||||||
| 24 | # May need to pad list so we can swap with the 9th | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | while (@{ $this->{items} } <= 9) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | push @{ $this->{items} }, new Games::ScottAdams::Item('', '', 0); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | my $slot = $this->resolve_item($lightsource, 'light source'); | |||||||||||||||||||||||||||
| 30 | 0 | $this->swap_items($slot, 9); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Compile actions before emitting header so we know the values | ||||||||||||||||||||||||||||
| 34 | # we'll need for $nactions and $nmessages. ($nactions is NOT in | ||||||||||||||||||||||||||||
| 35 | # general the size of $this->{actions}, since that may contain big | ||||||||||||||||||||||||||||
| 36 | # actions that have to be broken across two or more actions in the | ||||||||||||||||||||||||||||
| 37 | # compiled form.) | ||||||||||||||||||||||||||||
| 38 | 1 | my $nvocab = $this->_compile_vocab(); | |||||||||||||||||||||||||||
| 39 | 1 | my(@compiled_actions, @compiled_comments); | |||||||||||||||||||||||||||
| 40 | 1 | foreach my $action (@{ $this->{actions} }) { | |||||||||||||||||||||||||||
| 41 | 65 | my @tmp = $action->compile($this); | |||||||||||||||||||||||||||
| 42 | 65 | push @compiled_actions, @tmp; | |||||||||||||||||||||||||||
| 43 | 65 | ▶ | my $comment = $action->comment() || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$action->comment || ''
MC/DC100%1/1
$action->comment || ''
$action->comment
Truth table100%2/2
A
A= $action->comment
| |||||||||||||||||||||||||||||
| 44 | 65 | $comment =~ s/\"/\'/g; | |||||||||||||||||||||||||||
| 45 | 65 | push @compiled_comments, $comment, map { '' } 1..(@tmp-1); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | my $nitems = @{ $this->{items} } - 1; | |||||||||||||||||||||||||||
| 49 | 1 | my $nactions = @compiled_actions - 1; | |||||||||||||||||||||||||||
| 50 | 1 | my $nrooms = @{ $this->{rooms} } - 1; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | my $maxload = $this->{maxload}; | |||||||||||||||||||||||||||
| 53 | 1 | ▶ | if (!defined $maxload) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | _fatal("maximum load not defined") if $strict; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | $maxload = 6; # default from Adventureland | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | my $start = $this->{start}; | |||||||||||||||||||||||||||
| 59 | $start = $this->{_roomname1} | ||||||||||||||||||||||||||||
| 60 | 1 | ▶ | if !defined $start && !$strict; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $start or $strict
MC/DC0%0/2
defined $start or $strict
defined $start
$strict
Truth table33%1/3
A or B
A= defined $startB= $strict
| |||||||||||||||||||||||||||||
| 61 | 1 | $start = $this->resolve_room($start, 'start'); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | my $ntreasures = $this->{ntreasures}; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | my $wordlen = $this->{wordlen}; | |||||||||||||||||||||||||||
| 66 | 1 | ▶ | if (!defined $wordlen) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | _fatal("word length not defined") if $strict; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $wordlen = 3; # default from Adventureland | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | my $lighttime = $this->{lighttime}; | |||||||||||||||||||||||||||
| 72 | 1 | ▶ | if (!defined $lighttime) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | _fatal("light duration not defined") if $strict; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $lighttime = 125; # default from Adventureland | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | my $nmessages = @{ $this->{messages} } - 1; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | my $treasury = $this->{treasury}; | |||||||||||||||||||||||||||
| 80 | $treasury = $this->{_roomname1} | ||||||||||||||||||||||||||||
| 81 | 1 | ▶ | if !defined $treasury && !$strict; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $treasury or $strict
MC/DC0%0/2
defined $treasury or $strict
defined $treasury
$strict
Truth table33%1/3
A or B
A= defined $treasuryB= $strict
| |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | $treasury = $treasury eq "-" ? 255 : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | $this->resolve_room($treasury, 'treasury'); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | my $ident = $this->{ident}; | |||||||||||||||||||||||||||
| 86 | 1 | ▶ | if (!defined $ident) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | _fatal("adventure identifier not defined") if $strict; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $ident = 1; # default from Adventureland | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 1 | my $version = $this->{version}; | |||||||||||||||||||||||||||
| 91 | 1 | ▶ | if (!defined $version) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | _fatal("version number not defined") if $strict; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | $version = 416; # default from Adventureland | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Header of 16-bit values. How many of these should there be? | ||||||||||||||||||||||||||||
| 97 | # It's hard to say -- the documentation says there are fourteen of | ||||||||||||||||||||||||||||
| 98 | # them, and then lists only thirteen; and sample games that I've | ||||||||||||||||||||||||||||
| 99 | # looked at (e.g _Adventureland_) only seem to have twelve. I'll | ||||||||||||||||||||||||||||
| 100 | # go with the _Adventureland_ format. (Reading the ScottCurses.c | ||||||||||||||||||||||||||||
| 101 | # source appears to confirm this.) | ||||||||||||||||||||||||||||
| 102 | 1 | print ((76<<8)+84, "\n"); # unknown -- I'm using it as MT magic | |||||||||||||||||||||||||||
| 103 | 1 | print $nitems, "\n"; # number of items | |||||||||||||||||||||||||||
| 104 | 1 | print $nactions, "\n"; # number of actions | |||||||||||||||||||||||||||
| 105 | 1 | print $nvocab-1, "\n"; # number of nouns and verbs (same length!) | |||||||||||||||||||||||||||
| 106 | 1 | print $nrooms, "\n"; # number of rooms | |||||||||||||||||||||||||||
| 107 | 1 | print $maxload, "\n"; # maximum a player can carry | |||||||||||||||||||||||||||
| 108 | 1 | print $start, "\n"; # starting room | |||||||||||||||||||||||||||
| 109 | 1 | print $ntreasures, "\n"; # total treasures (*) | |||||||||||||||||||||||||||
| 110 | 1 | print $wordlen, "\n"; # word length | |||||||||||||||||||||||||||
| 111 | 1 | print $lighttime, "\n"; # time light source lasts | |||||||||||||||||||||||||||
| 112 | 1 | print $nmessages, "\n"; # number of messages | |||||||||||||||||||||||||||
| 113 | 1 | print $treasury, "\n"; # treasure room (leave things here to score) | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Actions. | ||||||||||||||||||||||||||||
| 116 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 117 | 1 | foreach my $compiled_action (@compiled_actions) { | |||||||||||||||||||||||||||
| 118 | 71 | print $compiled_action, "\n"; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Vocab. Verbs and nouns interleaved (one list padded if necessary.) | ||||||||||||||||||||||||||||
| 122 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 123 | 1 | ▶ | for (my $i = 0; $i < $nvocab; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 33 | my($verb, $noun) = ($this->{verbs}->[$i], $this->{nouns}->[$i]); | |||||||||||||||||||||||||||
| 125 | 33 | ▶ | $verb = '' if !defined $verb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 33 | ▶ | $noun = '' if !defined $noun; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 33 | print qq["$verb" "$noun"\n]; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Rooms. These are represented as a sequence of six integers (the | ||||||||||||||||||||||||||||
| 131 | # numbers of the rooms reached by moving North, South, East, West, | ||||||||||||||||||||||||||||
| 132 | # Up and Down respectively) followed by a description string in | ||||||||||||||||||||||||||||
| 133 | # double quotes. | ||||||||||||||||||||||||||||
| 134 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 135 | 1 | foreach my $room (@{ $this->{rooms} }) { | |||||||||||||||||||||||||||
| 136 | 20 | foreach my $dir (qw(n s e w u d)) { | |||||||||||||||||||||||||||
| 137 | 120 | my $dest = $room->exit($dir); | |||||||||||||||||||||||||||
| 138 | 120 | my $dnum; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 120 | ▶ | if (defined $dest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 30 | $dnum = $this->resolve_room($dest, 'exit'); | |||||||||||||||||||||||||||
| 142 | } else { | ||||||||||||||||||||||||||||
| 143 | 90 | $dnum = 0; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 120 | print "$dnum "; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 20 | my $desc = $room->desc(); | |||||||||||||||||||||||||||
| 150 | 20 | chomp($desc); | |||||||||||||||||||||||||||
| 151 | 20 | $desc =~ s/\"/\'/g; | |||||||||||||||||||||||||||
| 152 | 20 | print qq["$desc"\n]; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # Messages | ||||||||||||||||||||||||||||
| 156 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 157 | 1 | foreach my $msg (@{ $this->{messages} }) { | |||||||||||||||||||||||||||
| 158 | 46 | $msg =~ s/\"/\'/g; | |||||||||||||||||||||||||||
| 159 | 46 | print qq["$msg"\n]; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # Items. Each is a quoted description and item number. | ||||||||||||||||||||||||||||
| 163 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 164 | 1 | foreach my $item (@{ $this->{items} }) { | |||||||||||||||||||||||||||
| 165 | 33 | my $desc = $item->desc(); | |||||||||||||||||||||||||||
| 166 | 33 | chomp($desc); | |||||||||||||||||||||||||||
| 167 | 33 | $desc =~ s/\"/\'/g; | |||||||||||||||||||||||||||
| 168 | 33 | my $roomname = $item->where(); | |||||||||||||||||||||||||||
| 169 | 33 | ▶ | my $roomnum = !defined $roomname ? 0 : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | $this->resolve_room($roomname, 'position'); | ||||||||||||||||||||||||||||
| 171 | 33 | my $getdrop = $item->getdrop(); | |||||||||||||||||||||||||||
| 172 | 33 | ▶ | if (defined $getdrop) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 14 | ▶ | my $canonicalisedNoun = $this->resolve_noun($getdrop) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | or die "can't canonicalise gendrop '$getdrop'"; | ||||||||||||||||||||||||||||
| 175 | 14 | $getdrop = $this->{nouns}->[$canonicalisedNoun]; | |||||||||||||||||||||||||||
| 176 | 14 | $desc .= '/' . uc($getdrop) . '/'; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 33 | print qq["$desc" $roomnum\n]; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # Comments. One per compiled action | ||||||||||||||||||||||||||||
| 182 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 183 | 1 | foreach my $comment (@compiled_comments) { | |||||||||||||||||||||||||||
| 184 | 71 | print qq["$comment"\n]; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # Trailer. | ||||||||||||||||||||||||||||
| 188 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 189 | 1 | print $version, "\n"; # version number | |||||||||||||||||||||||||||
| 190 | 1 | print $ident, "\n"; # ident number | |||||||||||||||||||||||||||
| 191 | 1 | print 0, "\n"; # unknown additional magic number | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 76 | ▶ | sub resolve_room { | ||||||||||||||||||||||||||
|
Subroutine
resolve_room: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 196 | 76 | my $this = shift(); | |||||||||||||||||||||||||||
| 197 | 76 | my($roomname, $caption) = @_; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 76 | ▶ | _fatal("$caption room not defined!") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | if !defined $roomname; | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 76 | my $room = $this->{roomname}->{$roomname}; | |||||||||||||||||||||||||||
| 203 | 76 | ▶ | _fatal("$caption room '$roomname' doesn't exist!") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | if !defined $room; | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 76 | return $room->num(); | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 67 | ▶ | sub resolve_item { | ||||||||||||||||||||||||||
|
Subroutine
resolve_item: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 211 | 67 | my $this = shift(); | |||||||||||||||||||||||||||
| 212 | 67 | my($itemname, $caption) = @_; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 67 | ▶ | _fatal("$caption item not defined!") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | if !defined $itemname; | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 67 | my $item = $this->{itemname}->{$itemname}; | |||||||||||||||||||||||||||
| 218 | 67 | ▶ | _fatal("$caption item '$itemname' doesn't exist!") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | if !defined $item; | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 67 | return $item->num(); | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 55 | ▶ | sub resolve_message { | ||||||||||||||||||||||||||
|
Subroutine
resolve_message: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 226 | 55 | my $this = shift(); | |||||||||||||||||||||||||||
| 227 | 55 | my($msg) = @_; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 55 | ▶ | if (!defined $msg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 0 | warn "ignoring empty message"; | |||||||||||||||||||||||||||
| 231 | 0 | return 0; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 55 | my $val = $this->{msgmap}->{$msg}; | |||||||||||||||||||||||||||
| 235 | 55 | ▶ | if (!defined $val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 46 | $val = @{ $this->{messages} }; | |||||||||||||||||||||||||||
| 237 | 46 | push @{ $this->{messages} }, $msg; | |||||||||||||||||||||||||||
| 238 | 46 | $this->{msgmap}->{$msg} = $val; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 55 | return $val; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | ▶ | sub swap_items { | ||||||||||||||||||||||||||
|
Subroutine
swap_items: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 246 | 0 | my $this = shift(); | |||||||||||||||||||||||||||
| 247 | 0 | my($slot1, $slot2) = @_; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | my $tmpitem = $this->{items}->[$slot2]; | |||||||||||||||||||||||||||
| 250 | 0 | $this->{items}->[$slot2] = $this->{items}->[$slot1]; | |||||||||||||||||||||||||||
| 251 | 0 | $this->{items}->[$slot1] = $tmpitem; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 0 | foreach my $slot ($slot1, $slot2) { | |||||||||||||||||||||||||||
| 254 | # Cheeky back-door patch-up of number | ||||||||||||||||||||||||||||
| 255 | 0 | $this->{items}->[$slot]->{num} = $slot; | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | # Patch up object-name resolution | ||||||||||||||||||||||||||||
| 258 | 0 | my $name = $this->{items}->[$slot]->name(); | |||||||||||||||||||||||||||
| 259 | 0 | $this->{itemname}->{$name} = $this->{items}->[$slot]; | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | # PRIVATE to the _compile() method. | ||||||||||||||||||||||||||||
| 265 | # | ||||||||||||||||||||||||||||
| 266 | 1 | ▶ | sub _compile_vocab { | ||||||||||||||||||||||||||
|
Subroutine
_compile_vocab: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 267 | 1 | my $this = shift(); | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 1 | my @verbs = $this->_make_wordlist('v', ([ '<auto>', 0 ], | |||||||||||||||||||||||||||
| 270 | [ 'GO', 1 ], | ||||||||||||||||||||||||||||
| 271 | [ 'GET', 10 ], | ||||||||||||||||||||||||||||
| 272 | [ 'DROP', 18 ])); | ||||||||||||||||||||||||||||
| 273 | 1 | $this->{verbs} = \@verbs; | |||||||||||||||||||||||||||
| 274 | 1 | my @nouns = $this->_make_wordlist('n', ([ '<any>', 0 ], | |||||||||||||||||||||||||||
| 275 | [ 'NORTH', 1 ], | ||||||||||||||||||||||||||||
| 276 | [ 'SOUTH', 2 ], | ||||||||||||||||||||||||||||
| 277 | [ 'EAST', 3 ], | ||||||||||||||||||||||||||||
| 278 | [ 'WEST', 4 ], | ||||||||||||||||||||||||||||
| 279 | [ 'UP', 5 ], | ||||||||||||||||||||||||||||
| 280 | [ 'DOWN', 6 ])); | ||||||||||||||||||||||||||||
| 281 | 1 | $this->{nouns} = \@nouns; | |||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | # Find DVN (Difference between Verbs and Nouns) | ||||||||||||||||||||||||||||
| 284 | 1 | my $dvn = @{ $this->{verbs} } - @{ $this->{nouns} }; | |||||||||||||||||||||||||||
| 285 | 1 | ▶ | if ($dvn > 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 286 | 0 | push @{ $this->{nouns} }, map '', 1..$dvn; | |||||||||||||||||||||||||||
| 287 | } elsif ($dvn < 0) { | ||||||||||||||||||||||||||||
| 288 | 1 | push @{ $this->{verbs} }, map '', 1..-$dvn; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 1 | return scalar(@ { $this->{verbs} }); | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # PRIVATE to the _compile_vocab() method. | ||||||||||||||||||||||||||||
| 296 | 2 | ▶ | sub _make_wordlist { | ||||||||||||||||||||||||||
|
Subroutine
_make_wordlist: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 297 | 2 | my $this = shift(); | |||||||||||||||||||||||||||
| 298 | 2 | my($type, @specials) = @_; | |||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 2 | my @words; | |||||||||||||||||||||||||||
| 301 | 2 | foreach my $ref (@specials) { | |||||||||||||||||||||||||||
| 302 | 11 | my($word, $index) = @$ref; | |||||||||||||||||||||||||||
| 303 | 11 | my @list = $this->_extract_synonyms($type, $word); | |||||||||||||||||||||||||||
| 304 | 11 | $this->_insert_words($type, \@words, $index, @list); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | # Add non-specials. We could do a better job than this of fitting | ||||||||||||||||||||||||||||
| 308 | # the various-sized synonym-sets into the available slots, but | ||||||||||||||||||||||||||||
| 309 | # let's not lose sleep over it. | ||||||||||||||||||||||||||||
| 310 | 2 | my $vocab = $this->{$type . 'vocab'}; | |||||||||||||||||||||||||||
| 311 | 2 | foreach my $key (sort keys %$vocab) { | |||||||||||||||||||||||||||
| 312 | 42 | my @list = ($key, @{ $vocab->{$key} }); | |||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | # Find first area big enough to fit all the words in | ||||||||||||||||||||||||||||
| 315 | 42 | my $index = 1; | |||||||||||||||||||||||||||
| 316 | 42 | while (1) { | |||||||||||||||||||||||||||
| 317 | 668 | ▶ | die "no slots lower that 1000 for '$key'" if $index == 1000; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 668 | my $i; | |||||||||||||||||||||||||||
| 319 | 668 | ▶ | for ($i = 0; $i < @list; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | 668 | ▶ | last if defined $words[$index+$i]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | 668 | ▶ | last if $i == @list; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 626 | $index++; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | #warn "found slot $index for $type '$key'"; | ||||||||||||||||||||||||||||
| 326 | 42 | $this->_insert_words($type, \@words, $index, @list); | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 2 | return @words; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | # PRIVATE to the _make_wordlist() method. | ||||||||||||||||||||||||||||
| 334 | 11 | ▶ | sub _extract_synonyms { | ||||||||||||||||||||||||||
|
Subroutine
_extract_synonyms: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||
| 335 | 11 | my $this = shift(); | |||||||||||||||||||||||||||
| 336 | 11 | my($type, $word) = @_; | |||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 11 | my $vocab = $this->{$type . 'vocab'}; | |||||||||||||||||||||||||||
| 339 | 11 | my $listref = $vocab->{$word}; | |||||||||||||||||||||||||||
| 340 | 11 | ▶ | if (defined $listref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | # Lucky guess: it was head of its list | ||||||||||||||||||||||||||||
| 342 | 3 | delete $vocab->{$word}; | |||||||||||||||||||||||||||
| 343 | 3 | return ($word, @$listref); | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | # Check if its in the RHS of any of the lists. This is a slow | ||||||||||||||||||||||||||||
| 347 | # algorithm, but that's not going to be big problem. | ||||||||||||||||||||||||||||
| 348 | 8 | foreach my $key (keys %$vocab) { | |||||||||||||||||||||||||||
| 349 | 201 | my $listref = $vocab->{$key}; | |||||||||||||||||||||||||||
| 350 | 0 | ▶ | if (grep { $_ eq $word } @$listref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 0 | delete $vocab->{$key}; | |||||||||||||||||||||||||||
| 352 | 0 | return ($key, @$listref); | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | # Not found at all: must be an unreferenced special | ||||||||||||||||||||||||||||
| 357 | 8 | return ($word); | |||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | # PRIVATE to the _make_wordlist() method. | ||||||||||||||||||||||||||||
| 362 | 53 | ▶ | sub _insert_words { | ||||||||||||||||||||||||||
|
Subroutine
_insert_words: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 363 | 53 | my $this = shift(); | |||||||||||||||||||||||||||
| 364 | 53 | my ($type, $wordsref, $index, @list) = @_; | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 53 | ▶ | for (my $i = 0; $i < @list; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | 53 | my $word = $list[$i]; | |||||||||||||||||||||||||||
| 368 | 53 | ▶ | die "no slot for special '$word'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | if defined $wordsref->[$index+$i]; | ||||||||||||||||||||||||||||
| 370 | 53 | ▶ | $word = '*' . $word if $i > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | 53 | $wordsref->[$index+$i] = $word; | |||||||||||||||||||||||||||
| 372 | #warn "inserted $type '$word' at " . ($index+$i); | ||||||||||||||||||||||||||||
| 373 | 53 | $this->{$type . 'map'}->{$word} = $index+$i; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | # The next pair of methods get called back from | ||||||||||||||||||||||||||||
| 379 | # Games::ScottAdams::Action::compile() | ||||||||||||||||||||||||||||
| 380 | 65 | ▶ | sub resolve_verb { | ||||||||||||||||||||||||||
|
Subroutine
resolve_verb: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 381 | 65 | my $this = shift(); | |||||||||||||||||||||||||||
| 382 | 65 | return $this->_resolve_word(@_, 'verb', $this->{verbs}, $this->{vmap}); | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 61 | ▶ | sub resolve_noun { | ||||||||||||||||||||||||||
|
Subroutine
resolve_noun: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 386 | 61 | my $this = shift(); | |||||||||||||||||||||||||||
| 387 | 61 | return $this->_resolve_word(@_, 'noun', $this->{nouns}, $this->{nmap}); | |||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | # PRIVATE to the resolve_{verb,noun}() methods | ||||||||||||||||||||||||||||
| 391 | 126 | ▶ | sub _resolve_word { | ||||||||||||||||||||||||||
|
Subroutine
_resolve_word: called CC=6 SCAR=19.7
| |||||||||||||||||||||||||||||
| 392 | 126 | my $this = shift(); | |||||||||||||||||||||||||||
| 393 | 126 | my($word, $caption, $aref, $href) = @_; | |||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 126 | ▶ | return 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | if !defined $word; | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 100 | $word = uc($word); | |||||||||||||||||||||||||||
| 399 | 100 | my $val = $href->{$word}; | |||||||||||||||||||||||||||
| 400 | 100 | ▶ | if (!defined $val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | 0 | $val = $href->{'*' . $word}; | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 100 | ▶ | die "impossible: $caption '$word' undefined" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | if !defined $val; | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | # If we specified a synonym, revert to the type word | ||||||||||||||||||||||||||||
| 408 | 100 | ▶ | while ($val > 0 && $aref->[$val] =~ /^\*/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$val > 0 and $aref->[$val] =~ /^\*/
MC/DC0%0/2
$val > 0 and $aref->[$val] =~ /^\*/
$val > 0
$aref->[$val] =~ /^\*/
Truth table33%1/3
A and B
A= $val > 0B= $aref->[$val] =~ /^\*/
| |||||||||||||||||||||||||||||
| 409 | 0 | $val--; | |||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 100 | return $val; | |||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | # PRIVATE to Compile.pm | ||||||||||||||||||||||||||||
| 417 | 0 | ▶ | sub _fatal { | ||||||||||||||||||||||||||
|
Subroutine
_fatal: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 418 | 0 | return Games::ScottAdams::File::fatal(undef, @_); | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 1 | 1; | |||||||||||||||||||||||||||