StoryFile.pm

filter: Statementstmt 52.9% 667 / 1260 Branchbran 30.1% 189 / 626 Conditioncond 23.0% 38 / 165 MC/DCmcdc 7.2% 7 / 97 Subroutinesub 67.8% 133 / 196 Podpod 0.0% 0 / 164 totaltotal 41.2% 1034 / 2508 CC 378 SCAR 45.3
CC
378
Cov
44%
read_line
686.5
output_stream
195.9
flush
96.0
CRAP 92.7
« ZIO_Curses.pm ↑ Summary ZIO_Tk.pm »
1 package Games::Rezrov::StoryFile;
2 # manages game file data and implements many non-io-related opcodes.
3 # Opcode inclusion made more sense in Java, where the data was a
4 # more sensible instance variable; oh well.
5 #
6 # This package is now STATIC, and does not instantiate objects, because:
7 # - it's faster not to have to dereference instance data
8 # - it's faster not to have to pass $self with every opcode/method call
9 #
10 # Not pretty, but v5+ games need all the speed they can get.
11 # See TPJ #13 for why :)
12 #
13  
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Carp qw(cluck croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 #use integer;
18 # "use integer" is required for mod() to work correctly; see
19 # math tests in "etude.z5"
20  
21 1 ▶use Games::Rezrov::ZHeader;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Games::Rezrov::ZObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Games::Rezrov::ZText;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Games::Rezrov::ZStatus;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Games::Rezrov::ZDict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Games::Rezrov::ZReceiver;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Games::Rezrov::ZConst;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Games::Rezrov::ZIO_Tools;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Games::Rezrov::ZObjectCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Games::Rezrov::Inliner;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Games::Rezrov::Quetzal;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Games::Rezrov::ZObjectStatus;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # constants are SLOWER when they're accessed in other packages...WTF??
35 1 ▶use constant FRAME_MAX_LOCAL_VARIABLES => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant FRAME_DUMMY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use constant FRAME_PROCEDURE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use constant FRAME_FUNCTION => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1 ▶use constant GV_SCORE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 # 8.2.3.1: global variable holding game score (v3)
42  
43 # frame indices:
44 1 ▶use constant FRAME_RPC => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use constant FRAME_ARGC => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use constant FRAME_CALL_TYPE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant FRAME_LOCAL => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant FRAME_ROUTINE => 18;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 # spec 5.2; there are 15 local vars
50  
51 1 ▶use constant CALL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use constant PRINT_PADDR => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 1 my $global_variable_address;
55 my $global_variable_word_addr;
56 1 my $header;
57 1 my $prompt_buffer;
58  
59 1 my $call_stack;
60 1 my $lines_wrote;
61 1 my $transcript_filename;
62  
63 # FIX ME, RESET THESE AS NECESSARY:
64 1 my $fm = 0;
65 1 my $selected_streams;
66 1 my $last_score = 0;
67 1 my $zios;
68 my $flushing;
69 1 my $wrote_something;
70 1 my $version;
71 1 my $groks_f3;
72 1 my $columns;
73 1 my $rows;
74 1 my ($ztext, $zstatus);
75 1 my $last_savefile;
76 1 my $quetzal;
77 1 my $tailing;
78 1 my $player_object;
79 1 my $player_confirmed;
80  
81 1 my $current_room;
82 1 my $object_cache;
83 1 my $current_input_stream;
84 1 my $input_filehandle;
85 1 my $window_cursors;
86 1 my $zdict;
87 1 my $push_command;
88 1 my $guessing_title;
89 1 my $full_version_output;
90 1 my $game_title;
91 1 my $last_prompt;
92 1 my $undo_slots;
93 1 my $last_input;
94 1 my $game_filename;
95 1 my $font_3_disabled;
96  
97 1 my %alternate_dictionaries;
98  
99 1 my %candidate_po;
100  
101 1 $Games::Rezrov::StoryFile::PC = 1;
102 # current game PC.
103  
104 1 $Games::Rezrov::StoryFile::STORY_BYTES = undef;
105 # story file data.
106 # HACK: this is *static* for speed. having to deref $self->bytes()
107 # all the time seems like it's going to be really slow.
108 # a further compromise might be to ditch the "object" approach altogether
109 # and just export all these functions; story data can still be kept
110 # "privately" in this module.
111  
112 1 my $dynamic_area;
113 # bytes in the story that can be changed by the game.
114 # Used for "verify" opcode and game saves/restores.
115 # (Also traditionally usually used for restarts, but we Lazily just
116 # reload the whole image)
117  
118 # more for-speed hacks related to writing bytes to the transcript stream:
119 1 my $current_window = Games::Rezrov::ZConst::LOWER_WIN;
120  
121 1 ▶use constant UNSIGNED_BYTE => 0xff;
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 1 ▶use constant LINEFEED => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 # ascii
125  
126 1 my $buffering;
127 1 my ($upper_lines, $lower_lines);
128 # HACKS, FIX ME
129  
130 1 my $current_frame;
131  
132 1 my $lines_read = 0;
133  
134 1 my $TYPO_NOTIFY;
135  
136 my $GLOBAL_TEMP_CONTROL;
137 1 my $GLOBAL_TEMP_OFFSET;
138  
139 1 my %Z_TRANSLATIONS = (
140 0x18 => "UP",
141 0x19 => "DOWN",
142 0x1a => "LEFT",
143 0x1b => "RIGHT",
144 179 => '|',
145 186 => '#',
146 196 => '-',
147 205 => '=',
148 );
149 # in beyond zork, when the ZIO can't handle font 3, game can
150 # send control characters
151  
152 1 my $INLINE_CODE = '
153 sub call {
154 my ($argv, $type) = @_;
155 # call a routine, either as a procedure (result thrown away)
156 # or a function (result stored). First argument of argv
157 # is address of function to call.
158 if ($argv->[0] == 0) {
159 # spec 6.4.3: calls to address 0 return 0
160 store_result(0) if ($type == FRAME_FUNCTION);
161 } else {
162 push_frame($type);
163 # make a new frame of specified type
164
165 $Games::Rezrov::StoryFile::PC = convert_packed_address($argv->[0], CALL);
166 # set the current PC
167
168 my $args = GET_BYTE();
169 # spec 5.2: routine begins with an arg count
170 die "impossible arg count of $args"
171 if ($args < 0 || $args > FRAME_MAX_LOCAL_VARIABLES);
172
173 # ZInterpreter.zdb.save("call type " + type + " argc:" + argc + " args:" + args); # debug
174 # current.arg_count = args;
175 my $argc = scalar @{$argv};
176 frame_argc($argc - 1);
177 # do not count procedure being called in argument count
178
179 my $arg;
180 my $local_count = 0;
181 my $i = 1;
182 while (--$args >= 0) {
183 # set local variables
184 $arg = $version >= 5 ? 0 : GET_WORD();
185 # spec 5.2.1: default variables follow if version < 5
186 # $_[0]->set_local_var(++$local_count, (--$argc > 0) ? $argv->[$i++] : $arg);
187 $current_frame->[FRAME_LOCAL + ++$local_count - 1] =
188 (--$argc > 0) ? $argv->[$i++] : $arg;
189 # set local variables. There used to be a set_local_var()
190 # method, but it was inlined for speed :(
191 }
192 }
193 }
194  
195 sub store_result_MV {
196 # called by opcodes producing a result (stores it).
197 my $where = GET_BYTE();
198 # see spec 4.2.2, 4.6.
199 # zip code handles this in store_operand, and in the case of
200 # variable zero, pushes a new variable onto the stack.
201 # The store_variable() call only SETS the topmost variable,
202 # and does not add a new one. Is that code ever reached? WTF!
203  
204 # printf STDERR "store_result: %s where:%d\n", $_[1], $where;
205 # print STDERR "$where\n";
206  
207 if ($where == 0) {
208 # routine stack: push value
209 # see zmach06e.txt section 7.1 (page 33):
210  
211 # A variable number is a byte that indicates a certain variable.
212 # The meaning of a variable number is:
213 # 0: the top of the routine stack;
214 # 1-15: the local variable with that number;
215 # 16-255: the global variable with that number minus 16.
216
217 # Writing to the variable with number 0 means to push a value onto
218 # the routine stack; reading this variable means pulling a value off.
219 routine_push(UNSIGNED_WORD($_[0]));
220 # make sure the value is cast into unsigned form.
221 # see add() for a lengthy debate on the subject.
222 } else {
223 set_variable($where, $_[0]);
224 # set_variable does casting for us
225 }
226 }
227  
228 sub store_result_GV {
229 # called by opcodes producing a result (stores it).
230 $GLOBAL_TEMP_CONTROL = GET_BYTE();
231 # see spec 4.2.2, 4.6.
232 # zip code handles this in store_operand, and in the case of
233 # variable zero, pushes a new variable onto the stack.
234 # The store_variable() call only SETS the topmost variable,
235 # and does not add a new one. Is that code ever reached? WTF!
236  
237 # printf STDERR "store_result: %s where:%d\n", $_[1], $GLOBAL_TEMP_CONTROL;
238 # print STDERR "$GLOBAL_TEMP_CONTROL\n";
239  
240 if ($GLOBAL_TEMP_CONTROL == 0) {
241 # routine stack: push value
242 # see zmach06e.txt section 7.1 (page 33):
243  
244 # A variable number is a byte that indicates a certain variable.
245 # The meaning of a variable number is:
246 # 0: the top of the routine stack;
247 # 1-15: the local variable with that number;
248 # 16-255: the global variable with that number minus 16.
249
250 # Writing to the variable with number 0 means to push a value onto
251 # the routine stack; reading this variable means pulling a value off.
252 routine_push(UNSIGNED_WORD($_[0]));
253 # make sure the value is cast into unsigned form.
254 # see add() for a lengthy debate on the subject.
255 } else {
256 set_variable($GLOBAL_TEMP_CONTROL, $_[0]);
257 # set_variable does casting for us
258 }
259 }
260  
261 sub conditional_jump_MV {
262 # see spec section 4.7, zmach06e.txt section 7.3
263 # argument: condition
264 # "my" vars version: prettier, but slower
265 my $control = GET_BYTE();
266
267 my $offset = $control & 0x3f;
268 # basic address is six low bits of the first byte.
269 if (($control & 0x40) == 0) {
270 # if "bit 6" is not set, address consists of the six (low) bits
271 # of the first byte plus the next 8 bits.
272 $offset = ($offset << 8) + GET_BYTE();
273 if (($offset & 0x2000) > 0) {
274 # if the highest bit (formerly bit 6 of the first byte)
275 # is set...
276 $offset |= 0xc000;
277 # turn on top two bits
278 # FIX ME: EXPLAIN THIS
279 }
280 }
281
282 if ($control & 0x80 ? $_[0] : !$_[0]) {
283 # normally, branch occurs when condition is false.
284 # however, if topmost bit is set, jump occurs when condition is true.
285 if ($offset > 1) {
286 # jump
287 jump($offset);
288 } else {
289 # instead of jump, this is a RTRUE (1) or RFALSE (0)
290 ret($offset);
291 }
292 }
293 }
294  
295 sub conditional_jump_GV {
296 # see spec section 4.7, zmach06e.txt section 7.3
297 # argument: condition
298 # global variables version: hideous, but faster? (no "my" variable create/destroy)
299 $GLOBAL_TEMP_CONTROL = GET_BYTE();
300
301 $GLOBAL_TEMP_OFFSET = $GLOBAL_TEMP_CONTROL & 0x3f;
302 # basic address is six low bits of the first byte.
303 if (($GLOBAL_TEMP_CONTROL & 0x40) == 0) {
304 # if "bit 6" is not set, address consists of the six (low) bits
305 # of the first byte plus the next 8 bits.
306 $GLOBAL_TEMP_OFFSET = ($GLOBAL_TEMP_OFFSET << 8) + GET_BYTE();
307 if (($GLOBAL_TEMP_OFFSET & 0x2000) > 0) {
308 # if the highest bit (formerly bit 6 of the first byte)
309 # is set...
310 $GLOBAL_TEMP_OFFSET |= 0xc000;
311 # turn on top two bits
312 # FIX ME: EXPLAIN THIS
313 }
314 }
315
316 if ($GLOBAL_TEMP_CONTROL & 0x80 ? $_[0] : !$_[0]) {
317 # normally, branch occurs when condition is false.
318 # however, if topmost bit is set, jump occurs when condition is true.
319 if ($GLOBAL_TEMP_OFFSET > 1) {
320 # jump
321 jump($GLOBAL_TEMP_OFFSET);
322 } else {
323 # instead of jump, this is a RTRUE (1) or RFALSE (0)
324 ret($GLOBAL_TEMP_OFFSET);
325 }
326 }
327 }
328  
329  
330 sub add {
331 # signed 16-bit addition
332 # args: x, y
333 # my ($self, $x, $y) = @_;
334 # die if $x & 0x8000 or $y & 0x8000;
335  
336 # my $result = unsigned_word(signed_word($x) + signed_word($y));
337 # this does not work correctly; example:
338 # die in zork 1 (teleport chasm, N [grue]), score has -10 added
339 # to it, result is 65526. Since value is always stored internally,
340 # do not worry about converting to unsigned. Brings up a larger issue:
341 # sometimes store_result writes data to the story, in which case
342 # we need an unsigned value! Solution -- do this casting only if
343 # we _need_ to, ie writing bytes to the story: see set_global_var()
344  
345 # Unfortunately, this breaks Trinity:
346 # count:538 pc:97444 type:2OP opcode:20 (add) operands:36910,100
347 # here we get into trouble because the sum uses the sign bit (0x8000)
348 # but it is an UNSIGNED value! So in this case we *must* make sure
349 # the result is unsigned. Solution #2: change store_result to
350 # make sure everything is unsigned. Cast to signed only when we are
351 # sure the data is signed (see set_variable, scores)
352
353 # store_result(signed_word($x) + signed_word($y));
354 store_result(SIGNED_WORD($_[0]) + SIGNED_WORD($_[1]));
355 }
356  
357 sub subtract {
358 # signed 16-bit subtraction: args: $x, $y
359 store_result(SIGNED_WORD($_[0]) - SIGNED_WORD($_[1]));
360 }
361  
362 sub multiply {
363 # signed 16-bit multiplication; args: $x, $y
364 store_result(SIGNED_WORD($_[0]) * SIGNED_WORD($_[1]));
365 }
366  
367 sub divide {
368 # signed 16-bit division; args: $x, $y
369 store_result(SIGNED_WORD($_[0]) / SIGNED_WORD($_[1]));
370 }
371  
372 sub compare_jg {
373 # jump if a is greater than b; signed 16-bit comparison
374 conditional_jump(SIGNED_WORD($_[0]) > SIGNED_WORD($_[1]));
375 }
376  
377 sub compare_jl {
378 # jump if a is less than b; signed 16-bit comparison
379 conditional_jump(SIGNED_WORD($_[0]) < SIGNED_WORD($_[1]));
380 }
381  
382 sub output_stream {
383 #
384 # select/deselect output streams.
385 #
386 my $str = SIGNED_WORD($_[0]);
387 my $table_start = $_[1];
388  
389 return if $str == 0;
390 # selecting stream 0 does nothing
391  
392 # print STDERR "output_stream $str\n";
393 my $astr = abs($str);
394 my $selecting = $str > 0 ? 1 : 0;
395 if ($astr == Games::Rezrov::ZConst::STREAM_REDIRECT) {
396 #
397 # stream 3: redirect output to a table exclusively (no other streams)
398 #
399 my $stack = $zios->[Games::Rezrov::ZConst::STREAM_REDIRECT];
400 if ($selecting) {
401 #
402 # selecting
403 #
404 my $buf = new Games::Rezrov::ZReceiver();
405 $buf->misc($table_start);
406 push @{$stack}, $buf;
407 fatal_error("illegal number of stream3 opens!") if @{$stack} > 16;
408 # 7.1.2.1.1: max 16 legal redirects
409 } else {
410 #
411 # deselecting: copy table to memory
412 #
413 my $buf = pop @{$stack};
414 my $table_start = $buf->misc();
415 my $pointer = $table_start + 2;
416 my $buffer = $buf->buffer();
417 # printf STDERR "Writing redirected chunk %s to %d\n", $buffer, $pointer;
418 for (my $i=0; $i < length($buffer); $i++) {
419 set_byte_at($pointer++, ord substr($buffer,$i,1));
420 }
421 set_word_at($table_start, ($pointer - $table_start - 2));
422 # record number of bytes written
423  
424 # printf STDERR "table redir; %d / %d = %s\n", length($buffer), get_word_at($table_start), get_string_at(24663, length($buffer));
425  
426 if (@{$stack}) {
427 # this is stacked; keep redirection on (7.1.2.1.1)
428 $selected_streams->[$astr] = 1;
429 }
430 if ($version == 6) {
431 # 7.1.2.1
432 fatal_error("stream 3 close under v6, needs a-fixin");
433 }
434 }
435 } elsif ($astr == Games::Rezrov::ZConst::STREAM_TRANSCRIPT) {
436 if ($selecting) {
437 # print STDERR "opening transcript\n";
438 if (my $filename = $transcript_filename ||
439 filename_prompt("-check" => 1,
440 "-ext" => "txt",
441 )) {
442 $transcript_filename = $filename;
443 # 7.1.1.2: only ask once per session
444 my $fh = new FileHandle;
445 if ($fh->open(">$filename")) {
446 $zios->[Games::Rezrov::ZConst::STREAM_TRANSCRIPT] = $fh;
447 } else {
448 write_text(sprintf "Yikes, I can\'t open %s: %s...", $filename, lc($!));
449 $selecting = 0;
450 }
451 } else {
452 $selecting = 0;
453 }
454 unless ($selecting) {
455 newline();
456 newline();
457 }
458 } else {
459 # closing transcript
460 my $fh = $zios->[Games::Rezrov::ZConst::STREAM_TRANSCRIPT];
461 $fh->close() if $fh;
462 }
463 } elsif ($astr == Games::Rezrov::ZConst::STREAM_COMMANDS) {
464 if ($selecting) {
465 my $filename = filename_prompt("-ext" => "cmd",
466 "-check" => 1);
467 if ($filename) {
468 my $fh = new FileHandle();
469 if ($fh->open(">$filename")) {
470 $zios->[Games::Rezrov::ZConst::STREAM_COMMANDS] = $fh;
471 write_text("Recording to $filename.");
472 } else {
473 write_text("Can\'t write to $filename.");
474 $selecting = 0;
475 }
476 }
477 } else {
478 my $fh = $zios->[Games::Rezrov::ZConst::STREAM_COMMANDS];
479 if ($fh) {
480 $fh->close();
481 write_text("Recording stopped.");
482 } else {
483 write_text("Um, I\'m not recording now.");
484 }
485 }
486 newline();
487 } elsif ($astr == Games::Rezrov::ZConst::STREAM_STEAL) {
488 # printf STDERR "steal: %s\n", $selecting;
489 $zios->[Games::Rezrov::ZConst::STREAM_STEAL] = $selecting ? new Games::Rezrov::ZReceiver() : undef;
490 } elsif ($astr != Games::Rezrov::ZConst::STREAM_SCREEN) {
491 fatal_error("Unknown stream $str");
492 }
493  
494 $selected_streams->[$astr] = $selecting;
495 }
496  
497 sub erase_window {
498 my $window = SIGNED_WORD($_[0]);
499 my $zio = screen_zio();
500 if ($window == -1) {
501 # 8.7.3.3:
502 # $self->split_window(Games::Rezrov::ZConst::UPPER_WIN, 0);
503 # WRONG!
504 split_window(0);
505 # collapse upper window to size 0
506 clear_screen();
507 # erase the entire screen
508 reset_write_count();
509 set_window(Games::Rezrov::ZConst::LOWER_WIN);
510 set_cursor(($version == 4 ? $rows : 1), 1);
511 # move cursor to the appropriate line for this version;
512 # hack: at least it\'s abstracted :)
513 } elsif ($window < 0 or $window > 1) {
514 $zio->fatal_error("erase_window $window !");
515 } else {
516 #
517 # erase specified window
518 #
519 my $restore = $zio->get_position(1);
520 my ($start, $end);
521 if ($window == Games::Rezrov::ZConst::UPPER_WIN) {
522 $start = 0;
523 $end = $upper_lines;
524 } elsif ($window == Games::Rezrov::ZConst::LOWER_WIN) {
525 $start = $upper_lines;
526 $end = $rows;
527 reset_write_count();
528 } else {
529 die "clear window $window!";
530 }
531 for (my $i = $start; $i < $end; $i++) {
532 # $zio->erase_line($i);
533 $zio->absolute_move(0, $i);
534 $zio->clear_to_eol();
535 }
536 &$restore();
537 # restore cursor position
538 }
539 }
540  
541 sub jump {
542 # unconditional jump; modifies PC
543 # see zmach06e.txt, section 8.4.
544 # argument: new offset
545 $Games::Rezrov::StoryFile::PC += SIGNED_WORD($_[0] - 2);
546 }
547  
548 sub print_num {
549 # print the given signed number.
550 write_text(SIGNED_WORD($_[0]));
551 }
552  
553 sub inc_jg {
554 my ($variable, $value) = @_;
555 # increment a variable, and branch if it is now greater than value.
556 # **indirect?**
557 $value = SIGNED_WORD($value);
558  
559 my $before = SIGNED_WORD(get_variable($variable));
560 my $new_val = SIGNED_WORD($before + 1);
561 set_variable($variable, $new_val);
562 conditional_jump($new_val > $value);
563 }
564  
565 sub increment {
566 # increment a variable (16 bits, signed). arg: variable #
567 # **indirect?**
568 my $value = SIGNED_WORD(get_variable($_[0])) + 1;
569 set_variable($_[0], UNSIGNED_WORD($value));
570 }
571  
572 sub decrement {
573 # decrement a variable (16 bits, signed)
574 # **indirect?**
575 my $value = SIGNED_WORD(get_variable($_[0])) - 1;
576 set_variable($_[0], UNSIGNED_WORD($value));
577 }
578  
579 sub dec_jl {
580 my ($variable, $value) = @_;
581 # decrement a signed 16-bit variable, and branch if it is now less than value.
582 # **indirect?**
583 $value = SIGNED_WORD($value);
584  
585 my $before = SIGNED_WORD(get_variable($variable));
586 my $new_val = SIGNED_WORD($before - 1);
587 set_variable($variable, UNSIGNED_WORD($new_val));
588 conditional_jump($new_val < $value);
589 }
590  
591 sub mod {
592 # store remainder after signed 16-bit division
593 if (1) {
594 use integer;
595 # without "use integer", "%" operator flunks etude.z5 tests
596 # (on all systems? linux anyway).
597 # For example: perl normally says (13 % -5) == -2;
598 # it "should" be 3, or (13 - (-5 * -2))
599 #
600 # "use integer" computes math ops in integer, thus always
601 # rounding towards zero and getting around the problem.
602 #
603 # Unfortunately, "use integer" must be scoped here lest it play
604 # havoc in other places which require floating point division:
605 # e.g. pixel-based text wrapping.
606 store_result(SIGNED_WORD($_[0]) % SIGNED_WORD($_[1]));
607 } else {
608 # an alternative workaround?:
609 my $x = SIGNED_WORD($_[0]);
610 my $y = SIGNED_WORD($_[1]);
611 my $times = int($x / $y);
612 # how many times does $y fit into $x; always round towards zero!
613 store_result($x - ($y * $times));
614 }
615 }
616  
617 sub z_store {
618 # opcode to set variable
619 # **indirect**
620 set_variable($_[0], $_[1], 1);
621 # when called as an opcode, set indirect stack reference flag
622 }
623  
624 sub set_variable {
625 # args:
626 # $_[0] = $variable
627 # $_[1] = $value
628 # $_[2] = if nonzero, "indirect stack reference" mode (see draft spec 1.1)
629 # printf STDERR "set_variable %s = %s\n", $_[1], $_[2];
630 # see spec 4.2.2
631 if ($_[0] == 0) {
632 # "top of routine stack": do we push, or just set?
633 # draft spec 1.1 says under certain circumstances we should just
634 # manipulate the first stack variable, and not push/pop.
635 #
636 # - for the 7 opcodes mentioned, frotz 2.43 only seems to follow
637 # the 1.1 spec for 3 of them:
638 #
639 # z_store, z_load, z_pull: set top of stack
640 # z_inc, z_inc_chk: push onto stack
641 # z_dec, z_dec_chk: pop from stack
642  
643 if ($_[2]) {
644 # indirect stack reference mode
645 $current_frame->[$#$current_frame] = UNSIGNED_WORD($_[1]);
646 # just set top variable, don\'t push
647 } else {
648 routine_push($_[1]);
649 }
650 } elsif ($_[0] <= 15) {
651 # local variable
652 $current_frame->[FRAME_LOCAL + $_[0] - 1] = UNSIGNED_WORD($_[1]);
653 # printf "set local var %d to %d\n", $_[0], $current_frame->[FRAME_LOCAL + $_[0] - 1];
654 # numbered starting at 1, not 0
655 } else {
656 # global
657 $_[0] -= 16;
658 # indexed starting at 0
659  
660 set_global_var($_[0], UNSIGNED_WORD($_[1]));
661 }
662 }
663  
664 sub art_shift {
665 # sect15.html#art_shift; ARiThmetic shift
666 my $number = SIGNED_WORD($_[0]);
667 my $places = SIGNED_WORD($_[1]);
668 store_result($places > 0 ? $number << $places : $number >> abs($places));
669 # sign bit persists after right shift
670 }
671  
672 sub log_shift {
673 # sect15.html#log_shift; LOGical shift
674 my $number = UNSIGNED_WORD($_[0]);
675 my $places = SIGNED_WORD($_[1]);
676 store_result($places > 0 ? $number << $places : abs($number) >> abs($places));
677 # sign bit cleared during right shift
678 }
679  
680 sub get_property_length {
681 # given the literal address of a property data block,
682 # find and store size of the property data (number of bytes).
683 # example usage: "inventory" cmd.
684 # arg: address
685 my $address = $_[0];
686  
687 # die "get_property_length";
688 # given the literal address of a property data block,
689 # find and store size of the property data (number of bytes).
690 # example usage: "inventory" cmd
691 my $addr = SIGNED_WORD($address - 1);
692 # subtract one because we are given data start location, not
693 # the index location (yuck). Also account for possible rollover;
694 # one example: (1) start sorcerer. (2) "ne" (3) "frotz me".
695 # int rollover crash: 0 becomes -1 instead of 65535.
696 my $size_byte = get_byte_at($addr & 0xffff);
697 my $result;
698 if ($version <= 3) {
699 # 12.4.1
700 $result = ($size_byte >> 5) + 1;
701 } else {
702 if (($size_byte & 0x80) > 0) {
703 # spec 12.4.2.1: this is the second size byte, length
704 # is in bottom 6 bits
705 $result = $size_byte & 0x3f;
706 if ($result == 0) {
707 # 12.4.2.1.1
708 # print STDERR "wacky inform compiler size; check this\n";
709 $result = 64;
710 }
711 } else {
712 # 12.4.2.2
713 $result = (($size_byte & 0x40) > 0) ? 2 : 1;
714 }
715 }
716 store_result($result);
717 }
718  
719 sub z_not {
720 # sect15.html#not
721 store_result(~$_[0]);
722 }
723  
724 sub zo_verify {
725 # verify game image.
726 # in most Infocom games this seems to be either "$ve" or "$verif".
727 # sect15.html#verify
728 my $stat = $header->static_memory_address();
729 my $flen = $header->file_length();
730 my $sum = 0;
731 for (my $i = 0x40; $i < $flen; $i++) {
732 $sum += ($i < $stat) ? save_area_byte($i) : GET_BYTE_AT($i);
733 }
734 $sum = $sum % 0x10000;
735 conditional_jump($sum == $header->file_checksum());
736 }
737  
738 sub copy_table {
739 # sect15.html#copy_table
740 my ($first, $second, $size) = @_;
741  
742 $size = SIGNED_WORD($size);
743  
744 # printf STDERR "table copy from %d=>%d; %s\n", $first, $second, get_string_at($first, $size);
745  
746 my $len = abs($size);
747 my $i;
748 if ($second == 0) {
749 # zero out all bytes in first table
750 for ($i = 0; $i < $len; $i++) {
751 set_byte_at($first + $i, 0);
752 }
753 } elsif ($size < 0) {
754 # we *must* copy forwards, even if this corrupts first table
755 # untested();
756 for ($i = 0; $i < $len; $i++) {
757 set_byte_at($second + $i, get_byte_at($first + $i));
758 }
759 } else {
760 # copy first into second; since they might overlap, save off first
761 my @buf;
762 for ($i = 0; $i < $len; $i++) {
763 $buf[$i] = get_byte_at($first + $i);
764 }
765 for ($i = 0; $i < $len; $i++) {
766 set_byte_at($second + $i, $buf[$i]);
767 }
768 }
769  
770 # printf STDERR "after table copy: %s\n", get_string_at($second, $size);
771 }
772  
773 sub set_global_var {
774 # set a global variable
775 set_word_at($global_variable_address + ($_[0] * 2), $_[1]);
776 # printf STDERR "set gv %d to %d\n", @_;
777  
778 if ($_[0] == GV_SCORE and
779 Games::Rezrov::ZOptions::EMULATE_NOTIFY() and
780 !$header->is_time_game()) {
781 # 8.2.3.1: "2nd" global variable holds score
782 # ("2nd" variable is index #1)
783 my $score = SIGNED_WORD($_[1]);
784 my $diff = $score - $last_score;
785 if ($diff and Games::Rezrov::ZOptions::notifying()) {
786 write_text(sprintf "[Your score just went %s by %d points, for a total of %d.]",
787 ($diff > 0 ? "up" : "down"),
788 abs($diff), $score);
789 newline();
790 if ($last_score == 0) {
791 write_text("[NOTE: you can toggle score notification on or off at any time with the NOTIFY command.]");
792 newline();
793 }
794 }
795 $last_score = $score;
796 }
797 }
798  
799 sub random {
800 my $value = SIGNED_WORD($_[0]);
801 # return a random number between 1 and specified number.
802 # With arg 0, seed random number generator, return 0
803 # With arg < 0, seed with that value, return 0
804 my $result = 0;
805 if ($value == 0) {
806 # seed the random number generator
807 srand();
808 } elsif ($value < 0) {
809 # use specified value as a seed
810 srand($value);
811 } else {
812 $result = int(rand($value)) + 1;
813 }
814 store_result($result);
815 }
816  
817 sub get_variable_GV2 {
818 # $_[0]: variable
819 # $_[1]: indirect stack reference mode
820 # global variables version: hideous, but faster? (no "my" variable create/destroy)
821 #
822 # Testing reveals heaviest data access seems to be in the order
823 # local variables, global variables, routine variables.
824 # Re-order to reflect this.
825 #
826
827 if ($_[0] > 0 and $_[0] <= 15) {
828 # a local variable
829 # print STDERR "get_variable: local\n";
830 return $current_frame->[FRAME_LOCAL + $_[0] - 1];
831 # numbered starting from 1, not 0
832 } elsif ($_[0] != 0) {
833 # a global variable
834 # print STDERR "get_variable: global\n";
835  
836 # disgusting, but possibly faster?
837 # use a global, avoiding declaration/destruction of $tmp:
838 $GLOBAL_TEMP_OFFSET = $global_variable_address + (($_[0] - 16) * 2);
839 return GET_WORD_AT($GLOBAL_TEMP_OFFSET);
840 } else {
841 # a routine stack variable
842 # section 4.2.2:
843 # pop from top of routine stack
844 # print STDERR "get_variable: routine\n";
845 if ($_[1]) {
846 # indirect stack reference
847 return $current_frame->[$#$current_frame];
848 } else {
849 return routine_pop();
850 }
851 }
852 }
853  
854 sub get_variable_GV3 {
855 # EXPERIMENTAL:
856 # halfway-to-inlinable, except for $_[1] :/
857 # - also assumes global variable table is on a word boundary,
858 # which sadly will not work
859  
860 # $_[0]: variable
861 # $_[1]: indirect stack reference mode
862 # global variables version: hideous, but faster? (no "my" variable create/destroy)
863 #
864 # Testing reveals heaviest data access seems to be in the order
865 # local variables, global variables, routine variables.
866 # Re-order to reflect this.
867 #
868
869 return ($_[0] > 0 and $_[0] <= 15) ?
870 $current_frame->[FRAME_LOCAL + $_[0] - 1]
871 # a local variable
872
873 : (
874  
875 $_[0] != 0 ?
876 # a global variable
877 # vec($Games::Rezrov::StoryFile::STORY_BYTES, ($global_variable_word_addr + $_[0] - 16), 16)
878 die("this will not work")
879 :
880 (
881 # a routine stack variable
882 # section 4.2.2:
883 # pop from top of routine stack
884 $_[1] ? $current_frame->[$#$current_frame] : routine_pop()
885 )
886  
887 );
888 }
889  
890 sub get_variable_GV4 {
891 # EXPERIMENTAL:
892 # halfway-to-inlinable, except for $_[1] :/
893  
894 # $_[0]: variable
895 # $_[1]: indirect stack reference mode
896 # global variables version: hideous, but faster? (no "my" variable create/destroy)
897 #
898 # Testing reveals heaviest data access seems to be in the order
899 # local variables, global variables, routine variables.
900 # Re-order to reflect this.
901 #
902
903 return ($_[0] > 0 and $_[0] <= 15) ?
904 $current_frame->[FRAME_LOCAL + $_[0] - 1]
905 # a local variable
906
907 : (
908  
909 $_[0] != 0 ?
910 # a global variable
911 # vec($Games::Rezrov::StoryFile::STORY_BYTES, ($global_variable_word_addr + $_[0] - 16), 16)
912 die("this will not work")
913 :
914 (
915 # a routine stack variable
916 # section 4.2.2:
917 # pop from top of routine stack
918 $_[1] ? $current_frame->[$#$current_frame] : routine_pop()
919 )
920  
921 );
922 }
923  
924  
925 ';
926  
927 1 Games::Rezrov::Inliner::inline(\$INLINE_CODE);
928 0 ▶eval $INLINE_CODE;
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine add: called CC=1 SCAR=2.6
Subroutine art_shift: not called CC=2 SCAR=11.6
Subroutine call: called CC=8 SCAR=33.0
Subroutine compare_jg: called CC=1 SCAR=2.6
Subroutine compare_jl: called CC=1 SCAR=2.6
Subroutine conditional_jump_GV: called CC=6 SCAR=28.2
Subroutine conditional_jump_MV: not called CC=6 SCAR=28.2
Subroutine copy_table: not called CC=7 SCAR=30.7
Subroutine dec_jl: called CC=1 SCAR=2.6
Subroutine decrement: not called CC=1 SCAR=2.6
Subroutine divide: called CC=1 SCAR=2.6
Subroutine erase_window: called CC=8 SCAR=33.0
Subroutine get_property_length: called CC=5 SCAR=25.2
Subroutine get_variable_GV2: called CC=5 SCAR=25.2
Subroutine get_variable_GV3: not called CC=5 SCAR=25.2
Subroutine get_variable_GV4: not called CC=5 SCAR=25.2
Subroutine inc_jg: called CC=1 SCAR=2.6
Subroutine increment: called CC=1 SCAR=2.6
Subroutine jump: called CC=1 SCAR=2.6
Subroutine log_shift: not called CC=2 SCAR=11.6
Subroutine mod: not called CC=1 SCAR=2.6
Subroutine multiply: called CC=1 SCAR=2.6
Subroutine output_stream: called CC=24 SCAR=52.8
Subroutine print_num: called CC=1 SCAR=2.6
Subroutine random: not called CC=3 SCAR=17.4
Subroutine set_global_var: called CC=8 SCAR=33.0
Subroutine set_variable: called CC=4 SCAR=21.7
Subroutine store_result_GV: called CC=2 SCAR=11.6
Subroutine store_result_MV: not called CC=2 SCAR=11.6
Subroutine subtract: called CC=1 SCAR=2.6
Subroutine z_not: not called CC=1 SCAR=2.6
Subroutine z_store: called CC=1 SCAR=2.6
Subroutine zo_verify: not called CC=3 SCAR=17.4
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 78 branches
BranchTrueFalseTotal
$places > 0 ? : 0 0 0
if ($argv->[0] == 0) { } 6 146 152
if $type == 2 6 0 6
if $args < 0 or $args > 15 0 146 146
if (--$args >= 0) 634 146 780
$version >= 5 ? : 0 634 634
--$argc > 0 ? : 232 402 634
if (($GLOBAL_TEMP_CONTROL & 64) == 0) 571 796 1367
if (($GLOBAL_TEMP_OFFSET & 8192) > 0) 486 85 571
if ($GLOBAL_TEMP_CONTROL & 128 ? $_[0] : !$_[0]) 898 469 1367
$GLOBAL_TEMP_CONTROL & 128 ? : 448 919 1367
if ($GLOBAL_TEMP_OFFSET > 1) { } 830 68 898
if (($control & 64) == 0) 0 0 0
if (($offset & 8192) > 0) 0 0 0
if ($control & 128 ? $_[0] : !$_[0]) 0 0 0
$control & 128 ? : 0 0 0
if ($offset > 1) { } 0 0 0
if ($second == 0) { } 0 0 0
elsif ($size < 0) { } 0 0 0
if ($i < $len) 0 0 0
if ($i < $len) 0 0 0
if ($i < $len) 0 0 0
if ($i < $len) 0 0 0
if ($window == -1) { } 1 0 1
elsif ($window < 0 or $window > 1) { } 0 0 0
$version == 4 ? : 0 1 1
if ($window == 1) { } 0 0 0
elsif ($window == 0) { } 0 0 0
if ($i < $end) 0 0 0
if ($version <= 3) { } 5 0 5
if (($size_byte & 128) > 0) { } 0 0 0
if ($result == 0) 0 0 0
($size_byte & 64) > 0 ? : 0 0 0
if ($_[0] > 0 and $_[0] <= 15) { } 4586 1740 6326
elsif ($_[0] != 0) { } 642 1098 1740
if ($_[1]) { } 0 1098 1098
$_[0] > 0 && $_[0] <= 15 ? : 0 0 0
$_[0] != 0 ? : 0 0 0
$_[1] ? : 0 0 0
$_[0] > 0 && $_[0] <= 15 ? : 0 0 0
$_[0] != 0 ? : 0 0 0
$_[1] ? : 0 0 0
$places > 0 ? : 0 0 0
if $str == 0 0 1 1
$str > 0 ? : 1 0 1
if ($astr == 3) { } 0 1 1
elsif ($astr == 2) { } 0 1 1
elsif ($astr == 4) { } 0 1 1
elsif ($astr == 5) { } 0 1 1
elsif ($astr != 1) { } 0 1 1
if ($selecting) { } 0 0 0
if @{$stack;} > 16 0 0 0
if ($i < length $buffer) 0 0 0
if (@{$stack;}) 0 0 0
if ($version == 6) 0 0 0
if ($selecting) { } 0 0 0
if (my $filename = $transcript_filename || filename_prompt('-check', 1, '-ext', 'txt')) { } 0 0 0
if ($fh->open(">$filename")) { } 0 0 0
unless ($selecting) 0 0 0
if $fh 0 0 0
if ($selecting) { } 0 0 0
if ($filename) 0 0 0
if ($fh->open(">$filename")) { } 0 0 0
if ($fh) { } 0 0 0
$selecting ? : 0 0 0
if ($value == 0) { } 0 0 0
elsif ($value < 0) { } 0 0 0
if ($_[0] == 1 and Games::Rezrov::ZOptions::EMULATE_NOTIFY() and not $header->is_time_game) 0 144 144
if ($diff and Games::Rezrov::ZOptions::notifying()) 0 0 0
$diff > 0 ? : 0 0 0
if ($last_score == 0) 0 0 0
if ($_[0] == 0) { } 0 1301 1301
elsif ($_[0] <= 15) { } 1157 144 1301
if ($_[2]) { } 0 0 0
if ($GLOBAL_TEMP_CONTROL == 0) { } 1140 557 1697
if ($where == 0) { } 0 0 0
if ($i < $flen) 0 0 0
$i < $stat ? : 0 0 0
Condition22%6/27
$args < 0 or $args > 15
l!l&&r!l&&!r
00146
$window < 0 or $window > 1
l!l&&r!l&&!r
000
$_[0] > 0 and $_[0] <= 15
!ll&&!rl&&r
10986424586
$_[0] > 0 and $_[0] <= 15
!ll&&!rl&&r
000
$_[0] > 0 and $_[0] <= 15
!ll&&!rl&&r
000
$transcript_filename || filename_prompt('-check', 1, '-ext', 'txt')
l!l&&r!l&&!r
000
$_[0] == 1 and Games::Rezrov::ZOptions::EMULATE_NOTIFY() and not $header->is_time_game
!ll&&!rl&&r
14400
$_[0] == 1 and Games::Rezrov::ZOptions::EMULATE_NOTIFY()
!ll&&!rl&&r
14400
$diff and Games::Rezrov::ZOptions::notifying()
!ll&&!rl&&r
000
MC/DC0%0/2
$args < 0 or $args > 15
$args < 0 $args > 15
MC/DC0%0/2
$window < 0 or $window > 1
$window < 0 $window > 1
MC/DC100%2/2
$_[0] > 0 and $_[0] <= 15
$_[0] > 0 $_[0] <= 15
MC/DC0%0/2
$_[0] > 0 and $_[0] <= 15
$_[0] > 0 $_[0] <= 15
MC/DC0%0/2
$_[0] > 0 and $_[0] <= 15
$_[0] > 0 $_[0] <= 15
MC/DC0%0/2
$transcript_filename || filename_prompt('-check', 1, '-ext', 'txt')
$transcript_filename filename_prompt('-check', 1, '-ext', 'txt')
MC/DC0%0/3
$_[0] == 1 and Games::Rezrov::ZOptions::EMULATE_NOTIFY() and not $header->is_time_game
$_[0] == 1 Games::Rezrov::ZOptions::EMULATE_NOTIFY() not $header->is_time_game
MC/DC0%0/2
$diff and Games::Rezrov::ZOptions::notifying()
$diff Games::Rezrov::ZOptions::notifying()
Truth table33%1/3
A or B
A= $args < 0B= $args > 15
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $window < 0B= $window > 1
ABresult
1X1
011
000
Truth table100%3/3
A and B
A= $_[0] > 0B= $_[0] <= 15
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $_[0] > 0B= $_[0] <= 15
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $_[0] > 0B= $_[0] <= 15
ABresult
0X0
100
111
Truth table0%0/3
A || B
A= $transcript_filenameB= filename_prompt('-check', 1, '-ext', 'txt')
ABresult
1X1
011
000
Truth table25%1/4
A and B and not(C)
A= $_[0] == 1B= Games::Rezrov::ZOptions::EMULATE_NOTIFY()C= not $header->is_time_game
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/3
A and B
A= $diffB= Games::Rezrov::ZOptions::notifying()
ABresult
0X0
100
111
929 1 undef $INLINE_CODE;
930  
931 1 if (1) {
932 1 *Games::Rezrov::StoryFile::conditional_jump = \&Games::Rezrov::StoryFile::conditional_jump_GV;
933 1 *Games::Rezrov::StoryFile::get_variable = \&Games::Rezrov::StoryFile::get_variable_GV2;
934 1 *Games::Rezrov::StoryFile::store_result = \&Games::Rezrov::StoryFile::store_result_GV;
935 } else {
936 *Games::Rezrov::StoryFile::conditional_jump = \&Games::Rezrov::StoryFile::conditional_jump_MV;
937 *Games::Rezrov::StoryFile::get_variable = \&Games::Rezrov::StoryFile::get_variable_MV;
938 *Games::Rezrov::StoryFile::store_result = \&Games::Rezrov::StoryFile::store_result_MV;
939 }
940  
941 1 1;
942  
943 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
944 1 my ($type, $filename, $zio) = @_;
945 1 my $self = [];
946 1 bless $self, $type;
947 1 $zio->set_window(Games::Rezrov::ZConst::LOWER_WIN);
948 1 $game_filename = $filename;
949 1 $zios = [];
950 1 $zios->[Games::Rezrov::ZConst::STREAM_SCREEN] = $zio;
951 1 $zios->[Games::Rezrov::ZConst::STREAM_REDIRECT] = [];
952 # this stream redirects to memory and can be a stack
953 1 $selected_streams = [];
954  
955 1 $version = 0;
956 # don't even ask :P
957  
958 1 return $self;
959 }
960  
961 372 ▶sub compare_jz {
Subroutine compare_jz: called CC=1 SCAR=0.0
Pod undocumented
962 # branch if the value is zero
963 372 conditional_jump($_[0] == 0);
964 }
965  
966 1 ▶sub setup {
Subroutine setup: called CC=13 SCAR=34.0
Pod undocumented
967 1 my $zio = screen_zio();
968  
969 1 ▶ die "zio did not set up geometry" unless $rows and $columns;
Branch: 1 branch
BranchTrueFalseTotal
unless $rows and $columns 0 1 1
Condition33%1/3
$rows and $columns
!ll&&!rl&&r
001
MC/DC0%0/2
$rows and $columns
$rows $columns
Truth table33%1/3
A and B
A= $rowsB= $columns
ABresult
0X0
100
111
970  
971 #
972 # Set up "loading" message:
973 #
974 1 ▶ if ($zio->can_split()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zio->can_split) 0 1 1
975 0 my $message = "The story is loading...";
976 0 clear_screen();
977 0 ▶ if ($zio->fixed_font_default()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zio->fixed_font_default) { } 0 0 0
978 0 my $start_x = int(($columns / 2) - length($message) / 2);
979 0 my $start_y = int($rows / 2);
980 0 $zio->write_string($message, $start_x, $start_y);
981 } else {
982 0 my $width = $zio->string_width($message);
983 0 my ($max_x, $max_y) = $zio->get_pixel_geometry();
984 0 my $pixel_center = ($max_x / 2) - ($width / 2);
985 0 my $column = ($pixel_center / $max_x) * $columns;
986 0 $zio->absolute_move(int($column), int($rows / 2));
987 0 $zio->write_string($message);
988 }
989 0 $zio->update();
990 }
991
992 1 load();
993  
994 1 my @no_title_games = (
995 [ Games::Rezrov::ZDict::SAMPLER1 ],
996 [ Games::Rezrov::ZDict::BEYOND_ZORK ]
997 );
998  
999 1 foreach my $game (@no_title_games) {
1000 2 my @v = @{$game};
1001 2 shift @v;
1002 2 ▶ if (is_this_game(@v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_this_game(@v)) 0 2 2
1003 0 Games::Rezrov::ZOptions::GUESS_TITLE(0);
1004 }
1005 }
1006  
1007 1 $current_input_stream = Games::Rezrov::ZConst::INPUT_KEYBOARD;
1008 1 $undo_slots = [];
1009 1 $window_cursors = [];
1010 # cursor positions for individual windows
1011 1 reset_write_count();
1012 1 $object_cache = new Games::Rezrov::ZObjectCache();
1013 1 $quetzal = new Games::Rezrov::Quetzal();
1014
1015 # story _must_ be loaded beyond this point...
1016 1 ▶ Games::Rezrov::ZOptions::EMULATE_NOTIFY(0) if ($version > 3);
Branch: 1 branch
BranchTrueFalseTotal
if $version > 3 0 1 1
1017 # our notification trick only works for v3 games
1018
1019 1 $ztext = new Games::Rezrov::ZText();
1020 1 $zstatus = new Games::Rezrov::ZStatus();
1021 1 $zdict = new Games::Rezrov::ZDict();
1022 1 ▶ if (Games::Rezrov::ZOptions::EMULATE_UNDO() and
Branch: 1 branch
BranchTrueFalseTotal
if (Games::Rezrov::ZOptions::EMULATE_UNDO() and $zdict->get_dictionary_address("undo")) 0 1 1
Condition33%1/3
Games::Rezrov::ZOptions::EMULATE_UNDO() and $zdict->get_dictionary_address("undo")
!ll&&!rl&&r
010
MC/DC0%0/2
Games::Rezrov::ZOptions::EMULATE_UNDO() and $zdict->get_dictionary_address("undo")
Games::Rezrov::ZOptions::EMULATE_UNDO() $zdict->get_dictionary_address("undo")
Truth table33%1/3
A and B
A= Games::Rezrov::ZOptions::EMULATE_UNDO()B= $zdict->get_dictionary_address("undo")
ABresult
0X0
100
111
1023 $zdict->get_dictionary_address("undo")) {
1024 # disable undo emulation for games that supply the word "undo"
1025 0 Games::Rezrov::ZOptions::EMULATE_UNDO(0);
1026 }
1027
1028 1 output_stream(Games::Rezrov::ZConst::STREAM_SCREEN());
1029
1030 1 $current_window = Games::Rezrov::ZConst::LOWER_WIN;
1031 # HACKS, FIX ME
1032 # $zio->set_version($self);
1033 1 erase_window(-1);
1034 # collapses the upper window
1035  
1036 1 ▶ if ($version <= 3 and
Branch: 1 branch
BranchTrueFalseTotal
if ($version <= 3 and $zio->can_split and not $zio->manual_status_line) 0 1 1
Condition33%2/6
$version <= 3 and $zio->can_split and not $zio->manual_status_line
!ll&&!rl&&r
100
$version <= 3 and $zio->can_split
!ll&&!rl&&r
010
MC/DC0%0/3
$version <= 3 and $zio->can_split and not $zio->manual_status_line
$version <= 3 $zio->can_split not $zio->manual_status_line
Truth table25%1/4
A and B and not(C)
A= $version <= 3B= $zio->can_splitC= not $zio->manual_status_line
ABCresult
0XX0
10X0
1100
1111
1037 $zio->can_split() and
1038 !$zio->manual_status_line()) {
1039 # Centralized management of the status line.
1040 # Perform a split_window(), we'll use the "upper window"
1041 # for the status line.
1042 # This is BROKEN: Seastalker is a v3 game that uses the upper window!
1043 0 split_window(1);
1044 }
1045
1046 1 set_window(Games::Rezrov::ZConst::LOWER_WIN);
1047  
1048 1 if (0) {
1049 # debugging
1050 set_cursor(1,1);
1051 my $message = "line 1, column 1";
1052 write_zchunk(\$message);
1053 screen_zio()->update();
1054 sleep 10;
1055 }
1056 }
1057  
1058 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
1059 # probably an unimplemented opcode.
1060 # Send output to the ZIO to print it, as STDERR might not be "visible"
1061 # for some ZIO implementations
1062 0 fatal_error(sprintf 'unknown sub "%s": unimplemented opcode?', $Games::Rezrov::StoryFile::AUTOLOAD);
1063 }
1064  
1065 2 ▶sub load {
Subroutine load: called CC=5 SCAR=16.2
Pod undocumented
1066 # completely (re-) load game data. Resets all state info.
1067 2 my ($just_version) = @_;
1068 2 my $size = -s $game_filename;
1069 2 ▶ open(GAME, $game_filename) || die "can't open $game_filename: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open GAME, $game_filename or die "can't open ${game_filename}: $!\n" 2 0 2
1070 2 binmode GAME;
1071 2 ▶ if ($just_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($just_version) { } 1 1 2
1072 #
1073 # hack: just get the version of the game (first byte).
1074 #
1075 # We do this so we can initialize the I/O layer and put up
1076 # a "loading" message while we wait. We need the version
1077 # to figure out whether to create a status line in the ZIO;
1078 # important for Tk version (visually annoying to create status
1079 # line later on)
1080 #
1081 1 my $buf;
1082 1 ▶ if (read(GAME, $buf, 1) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (read(GAME, $buf, 1) == 1) { } 1 0 1
1083 1 return unpack "C", $buf;
1084 } else {
1085 0 die "huh?";
1086 }
1087 } else {
1088 1 my $read = read(GAME, $Games::Rezrov::StoryFile::STORY_BYTES, $size);
1089 1 close GAME;
1090 1 ▶ die "read error" unless $read == $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $read == $size 0 1 1
1091  
1092 1 my $zio = screen_zio();
1093 1 $header = new Games::Rezrov::ZHeader($zio);
1094 1 $global_variable_address = $header->global_variable_address();
1095  
1096 1 $global_variable_word_addr = int($global_variable_address / 2);
1097 # is this always aligned on a word boundary???
1098 # NO! Many games do not. This won't work, but it would
1099 # have been nice to get words via a single vec() of size 16 rather than
1100 # two vecs() of size 8 and a shift!
1101  
1102 1 my $static = $header->static_memory_address();
1103 1 $dynamic_area = substr($Games::Rezrov::StoryFile::STORY_BYTES, 0, $static);
1104 # vec($dynamic_area, 0x50, 8) = 12;
1105
1106 # $self->header($header);
1107
1108 1 $version = $header->version();
1109 1 $groks_f3 = $zio->groks_font_3();
1110  
1111 # $last_score = 0;
1112 # for "NOTIFY" emulation
1113 1 reset_cheats();
1114 }
1115 }
1116  
1117 685 ▶sub get_byte_at {
Subroutine get_byte_at: called CC=1 SCAR=0.0
Pod undocumented
1118 # return an 8-bit byte at specified storyfile offset.
1119 # die unless @_ == 2;
1120 # print STDERR "get_byte_at $_[1]\n" if $_[1] < 0x38;
1121 # print STDERR "gba\n";
1122 685 return vec($Games::Rezrov::StoryFile::STORY_BYTES, $_[0], 8);
1123 }
1124  
1125 0 ▶sub save_area_byte {
Subroutine save_area_byte: not called CC=1 SCAR=6.9
Pod undocumented
1126 # return byte in "pristine" game image
1127 0 return vec($dynamic_area, $_[0], 8);
1128 }
1129  
1130 0 ▶sub get_save_area {
Subroutine get_save_area: not called CC=1 SCAR=6.9
Pod undocumented
1131 # return ref to "pristine" game image
1132 # Don't use this :)
1133 0 return \$dynamic_area;
1134 }
1135  
1136 4 ▶sub get_story {
Subroutine get_story: called CC=1 SCAR=0.0
Pod undocumented
1137 # return ref to game data
1138 # Don't use this :)
1139 4 return \$Games::Rezrov::StoryFile::STORY_BYTES;
1140 }
1141  
1142 627 ▶sub set_byte_at {
Subroutine set_byte_at: called CC=1 SCAR=0.0
Pod undocumented
1143 # set an 8-bit byte at the specified storyfile offset to the
1144 # specified value.
1145 # print STDERR "sba\n";
1146 627 vec($Games::Rezrov::StoryFile::STORY_BYTES, $_[0], 8) = $_[1];
1147 # printf STDERR " set_byte_at %s = %s\n", $_[1], $_[2];
1148 }
1149  
1150 419 ▶sub get_word_at {
Subroutine get_word_at: called CC=1 SCAR=0.0
Pod undocumented
1151 # return unsigned 16-bit word at specified offset
1152 # die unless @_ == 2;
1153
1154 # print STDERR "gwa\n";
1155  
1156 # return ((vec($Games::Rezrov::StoryFile::STORY_BYTES, $_[1], 8) << 8) + vec($Games::Rezrov::StoryFile::STORY_BYTES, $_[1] + 1, 8));
1157 # return unpack "n", substr($Games::Rezrov::StoryFile::STORY_BYTES, $_[1], 2);
1158  
1159 # using vec() and doing our bit-twiddling manually seems faster
1160 # than using unpack(), either with a substr...
1161 #
1162 # $x = unpack "n", substr($Games::Rezrov::StoryFile::STORY_BYTES, $where, 2);
1163 #
1164 # or with using null bytes in the unpack...
1165 #
1166 # $x = unpack "x$where n", $Games::Rezrov::StoryFile::STORY_BYTES
1167 #
1168 # Oh well...
1169
1170 # print STDERR "get_word_at $_[1]\n" if $_[1] < 0x38;
1171  
1172 419 return ((vec($Games::Rezrov::StoryFile::STORY_BYTES, $_[0], 8) << 8) +
1173 vec($Games::Rezrov::StoryFile::STORY_BYTES, $_[0] + 1, 8));
1174 }
1175  
1176 352 ▶sub set_word_at {
Subroutine set_word_at: called CC=3 SCAR=15.1
Pod undocumented
1177 # set 16-bit word at specified index to specified value
1178 # die unless @_ == 3;
1179 # croak if ($_[1] == 30823);
1180 # print STDERR "swa\n";
1181 352 vec($Games::Rezrov::StoryFile::STORY_BYTES, $_[0], 8) = ($_[1] >> 8) & UNSIGNED_BYTE;
1182 352 vec($Games::Rezrov::StoryFile::STORY_BYTES, $_[0] + 1, 8) = $_[1] & UNSIGNED_BYTE;
1183 352 ▶ if ($_[0] == Games::Rezrov::ZHeader::FLAGS_2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] == 16) 0 352 352
1184 # activity in flags controlling printer transcripting.
1185 # Transcripting is set by the game and not by its own opcode.
1186 # see 7.3, 7.4
1187 0 ▶ my $str = $_[1] & Games::Rezrov::ZHeader::TRANSCRIPT_ON ? Games::Rezrov::ZConst::STREAM_TRANSCRIPT : - Games::Rezrov::ZConst::STREAM_TRANSCRIPT;
Branch: 1 branch
BranchTrueFalseTotal
$_[1] & 1 ? : 0 0 0
1188 # temp variable to prevent "modification of read-only value"
1189 # error when output_stream() tries to cast @_ to signed short
1190  
1191 0 output_stream($str);
1192 # use stream-style notification to tell the game about transcripting
1193 }
1194 }
1195  
1196 5 ▶sub get_string_at {
Subroutine get_string_at: called CC=1 SCAR=0.0
Pod undocumented
1197 # return string of bytes at given offset
1198 5 return substr($Games::Rezrov::StoryFile::STORY_BYTES, $_[0], $_[1]);
1199 }
1200  
1201 2 ▶sub reset_cheats {
Subroutine reset_cheats: called CC=2 SCAR=6.9
Pod undocumented
1202 2 ▶ $zdict->bp_cheat_data(0) if $zdict;
Branch: 1 branch
BranchTrueFalseTotal
if $zdict 1 1 2
1203 # reset "angiotensin" cheat (Bureaucracy)
1204  
1205 2 $last_score = get_global_var(GV_SCORE);
1206 # print STDERR "last_score: $last_score\n";
1207 # for NOTIFY emulation not to get confused after restore
1208 # FIX ME: block off to use only if cheat active and correct game version.
1209 }
1210  
1211 1 ▶sub reset_game {
Subroutine reset_game: called CC=2 SCAR=7.0
Pod undocumented
1212 # init/reset game state
1213 1 $Games::Rezrov::StoryFile::PC = 0;
1214 1 $call_stack = [];
1215 1 $lines_read = 0;
1216  
1217 1 reset_cheats();
1218  
1219 1 ▶ if ($header->version() == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header->version == 6) { } 0 1 1
1220 # 5.4: "main" routine
1221 0 call_proc($header->first_instruction_address());
1222 } else {
1223 # 5.5
1224 1 push_frame(FRAME_DUMMY);
1225 # create toplevel "dummy" frame: no parent, but can still
1226 # create local and stack variables. Also consistent with
1227 # Quetzal savefile model
1228 1 $Games::Rezrov::StoryFile::PC = $header->first_instruction_address();
1229 }
1230 # FIX ME: we could pack the address and then do a standard call()...
1231
1232 1 set_buffering(1);
1233 # 7.2.1: buffering is always on for v1-3, on by default for v4+.
1234 # We call this here so each implementation of ZIO doesn't have
1235 # to set the default.
1236  
1237 1 reset_write_count();
1238 1 clear_screen();
1239 1 set_window(Games::Rezrov::ZConst::LOWER_WIN());
1240  
1241 # FIX ME: reset zios() array here!
1242 # centralize all this with setup() stuff...
1243 }
1244  
1245 0 ▶sub reset_storyfile {
Subroutine reset_storyfile: not called CC=1 SCAR=6.9
Pod undocumented
1246 # FIX ME: everything in the header should be wiped but the
1247 # "printer transcript bit," etc.
1248 0 load();
1249 # hack
1250 }
1251  
1252 147 ▶sub push_frame {
Subroutine push_frame: called CC=1 SCAR=0.0
Pod undocumented
1253 # push a call frame onto stack
1254 147 my ($type) = @_;
1255  
1256 147 $current_frame = [];
1257 147 frame_call_type($type);
1258 147 frame_return_pc($Games::Rezrov::StoryFile::PC);
1259 147 $#$current_frame = FRAME_ROUTINE - 1;
1260 # expand frame so routine variables will start to be added at correct index
1261 147 push @{$call_stack}, $current_frame;
1262 }
1263  
1264 0 ▶sub load_variable {
Subroutine load_variable: not called CC=1 SCAR=6.9
Pod undocumented
1265 # get the value of a variable and store it.
1266 # **indirect**
1267 0 store_result(get_variable($_[0], 1));
1268 }
1269  
1270 150 ▶sub convert_packed_address {
Subroutine convert_packed_address: called CC=9 SCAR=40.6
Pod undocumented
1271 # unpack a packed address. See spec 1.2.3
1272 150 ▶ if ($version >= 1 and $version <= 3) {
Branch: 4 branches
BranchTrueFalseTotal
if ($version >= 1 and $version <= 3) { } 150 0 150
elsif ($version == 4 or $version == 5) { } 0 0 0
elsif ($version == 6 or $version == 7) { } 0 0 0
elsif ($version == 8) { } 0 0 0
Condition33%1/3
$version >= 1 and $version <= 3
!ll&&!rl&&r
00150
MC/DC0%0/2
$version >= 1 and $version <= 3
$version >= 1 $version <= 3
Truth table33%1/3
A and B
A= $version >= 1B= $version <= 3
ABresult
0X0
100
111
1273 150 return $_[0] * 2;
1274 ▶ } elsif ($version == 4 or $version == 5) {
Condition0%0/3
$version == 4 or $version == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$version == 4 or $version == 5
$version == 4 $version == 5
Truth table0%0/3
A or B
A= $version == 4B= $version == 5
ABresult
1X1
011
000
1275 0 return $_[0] * 4;
1276 ▶ } elsif ($version == 6 or $version == 7) {
Condition0%0/3
$version == 6 or $version == 7
l!l&&r!l&&!r
000
MC/DC0%0/2
$version == 6 or $version == 7
$version == 6 $version == 7
Truth table0%0/3
A or B
A= $version == 6B= $version == 7
ABresult
1X1
011
000
1277 0 ▶ my $offset = $_[1] == CALL ? $header->routines_offset() : $header->strings_offset();
Branch: 1 branch
BranchTrueFalseTotal
$_[1] == 1 ? : 0 0 0
1278 0 return ($_[0] * 4) + (8 * $offset);
1279 # 4P + 8R_O Versions 6 and 7, for routine calls
1280 # 4P + 8S_O Versions 6 and 7, for print_paddr
1281 # R_O and S_O are the routine and strings offsets (specified in the header as words at $28 and $2a, respectively).
1282 } elsif ($version == 8) {
1283 0 return $_[0] * 8;
1284 } else {
1285 0 die "don't know how to unpack addr for version $version";
1286 }
1287 }
1288  
1289 142 ▶sub ret {
Subroutine ret: called CC=3 SCAR=12.7
Pod undocumented
1290 142 my ($value) = @_;
1291 # return from a subroutine
1292 142 my $call_type = pop_frame();
1293  
1294 142 ▶ if ($call_type == FRAME_FUNCTION) {
Branch: 2 branches
BranchTrueFalseTotal
if ($call_type == 2) { } 142 0 142
elsif ($call_type != 1) { } 0 0 0
1295 142 store_result($value);
1296 } elsif ($call_type != FRAME_PROCEDURE) {
1297 0 die("unknown frame call type!");
1298 }
1299 142 return $value;
1300 # might be needed for an interrupt call (not yet implemented)
1301 }
1302  
1303  
1304 0 ▶sub get_variable_MV {
Subroutine get_variable_MV: not called CC=4 SCAR=30.0
Pod undocumented
1305 # $_[0]: variable
1306 # $_[1]: indirect stack reference mode
1307 # "my" vars version: prettier, but slower
1308 0 ▶ if ($_[0] == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] == 0) { } 0 0 0
elsif ($_[0] <= 15) { } 0 0 0
1309 # section 4.2.2:
1310 # pop from top of routine stack
1311 # print STDERR "rp\n";
1312 0 ▶ if ($_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) { } 0 0 0
1313 # indirect stack reference
1314 0 return $current_frame->[$#$current_frame];
1315 } else {
1316 0 return routine_pop();
1317 }
1318 } elsif ($_[0] <= 15) {
1319 # a local variable
1320 # print STDERR "lv\n";
1321 0 return $current_frame->[FRAME_LOCAL + $_[0] - 1];
1322 # numbered starting from 1, not 0
1323 } else {
1324 # a global variable
1325 # print STDERR "gv\n";
1326 # return get_global_var($_[1] - 16);
1327 # most readable, but slowest
1328 # return get_word_at($_[0]->global_variable_address() + (($_[1] - 16) * 2));
1329 # faster, less readable
1330  
1331 0 my $tmp = $global_variable_address + (($_[0] - 16) * 2);
1332 # - 16 = convert to index starting at 0
1333 # print STDERR "get gv $_[0]\n";
1334 0 return ((vec($Games::Rezrov::StoryFile::STORY_BYTES, $tmp, 8) << 8) +
1335 vec($Games::Rezrov::StoryFile::STORY_BYTES, $tmp + 1, 8));
1336 # fastest, almost unreadable :(
1337  
1338 #
1339 # alternate approach:
1340 # disgusting, but possibly faster?
1341 # use a global, avoiding declaration/destruction of $tmp, above
1342 #
1343  
1344 # $GLOBAL_TMP = $global_variable_address + (($_[0] - 16) * 2);
1345 # return ((vec($Games::Rezrov::StoryFile::STORY_BYTES, $GLOBAL_TMP, 8) << 8) +
1346 # vec($Games::Rezrov::StoryFile::STORY_BYTES, $GLOBAL_TMP + 1, 8));
1347  
1348 }
1349 }
1350  
1351 0 ▶sub get_variable_GV {
Subroutine get_variable_GV: not called CC=4 SCAR=30.0
Pod undocumented
1352 # $_[0]: variable
1353 # $_[1]: indirect stack reference mode
1354 # global variables version: hideous, but faster? (no "my" variable create/destroy)
1355 0 ▶ if ($_[0] == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] == 0) { } 0 0 0
elsif ($_[0] <= 15) { } 0 0 0
1356 # section 4.2.2:
1357 # pop from top of routine stack
1358 # print STDERR "get_variable: routine\n";
1359 0 ▶ if ($_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) { } 0 0 0
1360 # indirect stack reference
1361 0 return $current_frame->[$#$current_frame];
1362 } else {
1363 0 return routine_pop();
1364 }
1365 } elsif ($_[0] <= 15) {
1366 # a local variable
1367 # print STDERR "get_variable: local\n";
1368 0 return $current_frame->[FRAME_LOCAL + $_[0] - 1];
1369 # numbered starting from 1, not 0
1370 } else {
1371 # a global variable
1372 # print STDERR "get_variable: global\n";
1373  
1374 #
1375 # disgusting, but possibly faster?
1376 # use a global, avoiding declaration/destruction of $tmp:
1377 #
1378  
1379 0 $GLOBAL_TEMP_OFFSET = $global_variable_address + (($_[0] - 16) * 2);
1380 0 return ((vec($Games::Rezrov::StoryFile::STORY_BYTES, $GLOBAL_TEMP_OFFSET, 8) << 8) +
1381 vec($Games::Rezrov::StoryFile::STORY_BYTES, $GLOBAL_TEMP_OFFSET + 1, 8));
1382  
1383 }
1384 }
1385  
1386 0 ▶sub unsigned_word {
Subroutine unsigned_word: not called CC=1 SCAR=6.9
Pod undocumented
1387 # pack a signed value into an unsigned value.
1388 # Necessary to ensure the sign bit is placed at 0x8000.
1389 0 return unpack "S", pack "s", $_[0];
1390 }
1391  
1392 228 ▶sub compare_je {
Subroutine compare_je: called CC=3 SCAR=11.0
Pod undocumented
1393 # branch if first operand is equal to any of the others
1394 228 my $first = shift;
1395 # print STDERR "je\n";
1396 228 foreach (@_) {
1397 299 ▶ conditional_jump(1), return if $_ == $first;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $first 55 244 299
1398 }
1399 173 conditional_jump(0);
1400 }
1401  
1402 202 ▶sub store_word {
Subroutine store_word: called CC=1 SCAR=0.0
Pod undocumented
1403 202 my ($array_address, $word_index, $value) = @_;
1404 # set a word at a specified offset in a specified array offset.
1405 202 $array_address += (2 * $word_index);
1406 202 set_word_at($array_address, $value);
1407 }
1408  
1409 546 ▶sub store_byte {
Subroutine store_byte: called CC=1 SCAR=0.0
Pod undocumented
1410 546 my ($array_address, $byte_index, $value) = @_;
1411 546 set_byte_at($array_address + $byte_index, $value);
1412 }
1413  
1414 142 ▶sub pop_frame {
Subroutine pop_frame: called CC=1 SCAR=0.0
Pod undocumented
1415 142 my $last_frame = pop @{$call_stack};
1416 142 my $call_type = $last_frame->[FRAME_CALL_TYPE];
1417 # print "pop: $call_type\n";
1418 142 $Games::Rezrov::StoryFile::PC = $last_frame->[FRAME_RPC];
1419 142 $current_frame = $call_stack->[$#$call_stack];
1420 # set frame to calling frame
1421 142 return $call_type;
1422 }
1423  
1424 215 ▶sub get_word_index {
Subroutine get_word_index: called CC=1 SCAR=0.0
Pod undocumented
1425 # get a word from the specified index of the specified array
1426 215 my ($address, $index) = @_;
1427 215 store_result(get_word_at($address + (2 * $index)));
1428 }
1429  
1430 0 ▶sub put_property {
Subroutine put_property: not called CC=1 SCAR=6.9
Pod undocumented
1431 0 my ($object, $property, $value) = @_;
1432 0 my $zobj = get_zobject($object);
1433 0 my $zprop = $zobj->get_property($property);
1434 0 $zprop->set_value($value);
1435 }
1436  
1437 63 ▶sub test_attr {
Subroutine test_attr: called CC=2 SCAR=7.0
Pod undocumented
1438 # jump if some object has an attribute set
1439 63 my ($object, $attribute) = @_;
1440 63 my $zobj = get_zobject($object);
1441 63 ▶ conditional_jump($zobj and $zobj->test_attr($attribute));
Condition66%2/3
$zobj && $zobj->test_attr($attribute)
!ll&&!rl&&r
04122
MC/DC50%1/2
$zobj && $zobj->test_attr($attribute)
$zobj $zobj->test_attr($attribute)
Truth table66%2/3
A && B
A= $zobjB= $zobj->test_attr($attribute)
ABresult
0X0
100
111
1442 # watch out for object 0
1443 }
1444  
1445 8 ▶sub set_attr {
Subroutine set_attr: called CC=2 SCAR=7.1
Pod undocumented
1446 # turn on given attribute of given object
1447 8 my ($object, $attribute) = @_;
1448 8 ▶ if (my $zobj = get_zobject($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $zobj = get_zobject($object)) 8 0 8
1449 # unless object 0
1450 8 $zobj->set_attr($attribute);
1451 }
1452 }
1453  
1454 3 ▶sub clear_attr {
Subroutine clear_attr: called CC=2 SCAR=7.1
Pod undocumented
1455 # clear given attribute of given object
1456 3 my ($object, $attribute) = @_;
1457 3 ▶ if (my $zobj = get_zobject($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $zobj = get_zobject($object)) 3 0 3
1458 # unless object 0
1459 3 $zobj->clear_attr($attribute);
1460 }
1461 }
1462  
1463 20 ▶sub print_text {
Subroutine print_text: called CC=1 SCAR=0.0
Pod undocumented
1464 # decode a string at the PC and move PC past it
1465 20 my $blob;
1466 20 ($blob, $Games::Rezrov::StoryFile::PC) = $ztext->decode_text($Games::Rezrov::StoryFile::PC);
1467 20 write_zchunk($blob);
1468 }
1469  
1470  
1471 35 ▶sub write_zchunk {
Subroutine write_zchunk: called CC=12 SCAR=38.9
Pod undocumented
1472 35 my $chunk = $_[0];
1473  
1474 # print STDERR "Chunk: $$chunk\n";
1475 35 ▶ if ($selected_streams->[Games::Rezrov::ZConst::STREAM_REDIRECT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selected_streams->[3]) { } 0 35 35
1476 # 7.1.2.2: when active, no other streams get output
1477 0 my $stack = $zios->[Games::Rezrov::ZConst::STREAM_REDIRECT];
1478 # printf STDERR "redirected chunk: %s\n", $$chunk;
1479 0 $stack->[$#$stack]->buffer_zchunk($chunk);
1480 } else {
1481 #
1482 # other streams
1483 #
1484 35 ▶ if ($selected_streams->[Games::Rezrov::ZConst::STREAM_SCREEN]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selected_streams->[1]) 35 0 35
1485 #
1486 # screen
1487 #
1488 35 ▶ if ($selected_streams->[Games::Rezrov::ZConst::STREAM_STEAL] and
Branch: 1 branch
BranchTrueFalseTotal
if ($selected_streams->[5] and $current_window == 0) { } 0 35 35
Condition33%1/3
$selected_streams->[5] and $current_window == 0
!ll&&!rl&&r
3500
MC/DC0%0/2
$selected_streams->[5] and $current_window == 0
$selected_streams->[5] $current_window == 0
Truth table33%1/3
A and B
A= $selected_streams->[5]B= $current_window == 0
ABresult
0X0
100
111
1489 $current_window == Games::Rezrov::ZConst::LOWER_WIN) {
1490 # temporarily steal lower window output
1491 0 $zios->[Games::Rezrov::ZConst::STREAM_STEAL]->buffer_zchunk($chunk);
1492 } else {
1493 35 my $zio = $zios->[Games::Rezrov::ZConst::STREAM_SCREEN];
1494
1495 35 ▶ if ($buffering and $current_window != Games::Rezrov::ZConst::UPPER_WIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffering and $current_window != 1) { } 35 0 35
Condition33%1/3
$buffering and $current_window != 1
!ll&&!rl&&r
0035
MC/DC0%0/2
$buffering and $current_window != 1
$buffering $current_window != 1
Truth table33%1/3
A and B
A= $bufferingB= $current_window != 1
ABresult
0X0
100
111
1496 35 $zio->buffer_zchunk($chunk);
1497 } else {
1498 0 foreach (unpack("c*", $$chunk)) {
1499 0 ▶ if ($_ == Games::Rezrov::ZConst::Z_NEWLINE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ == 13) { } 0 0 0
1500 0 $prompt_buffer = "";
1501 0 $zio->newline();
1502 } else {
1503 0 $zio->write_zchar($_);
1504 }
1505 }
1506 }
1507 }
1508 }
1509  
1510 35 ▶ if ($selected_streams->[Games::Rezrov::ZConst::STREAM_TRANSCRIPT] and
Branch: 1 branch
BranchTrueFalseTotal
if ($selected_streams->[2] and $current_window == 0) 0 35 35
Condition33%1/3
$selected_streams->[2] and $current_window == 0
!ll&&!rl&&r
3500
MC/DC0%0/2
$selected_streams->[2] and $current_window == 0
$selected_streams->[2] $current_window == 0
Truth table33%1/3
A and B
A= $selected_streams->[2]B= $current_window == 0
ABresult
0X0
100
111
1511 $current_window == Games::Rezrov::ZConst::LOWER_WIN) {
1512 #
1513 # Game transcript
1514 #
1515 0 ▶ if (my $fh = $zios->[Games::Rezrov::ZConst::STREAM_TRANSCRIPT]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $zios->[2]) 0 0 0
1516 0 my $c = $$chunk;
1517 0 my $nl = chr(Games::Rezrov::ZConst::Z_NEWLINE);
1518 0 $c =~ s/$nl/\n/g;
1519 0 print $fh $c;
1520 }
1521 }
1522 }
1523 }
1524
1525 1 ▶sub print_ret {
Subroutine print_ret: called CC=1 SCAR=0.0
Pod undocumented
1526 # print string at PC, move past it, then return true
1527 1 print_text();
1528 1 newline();
1529 1 rtrue();
1530 }
1531  
1532 17 ▶sub newline {
Subroutine newline: called CC=1 SCAR=0.0
Pod undocumented
1533 17 write_zchar(Games::Rezrov::ZConst::Z_NEWLINE());
1534 }
1535  
1536 628 ▶sub loadb {
Subroutine loadb: called CC=1 SCAR=0.0
Pod undocumented
1537 # get the byte at index "index" of array "array"
1538 628 my ($array, $index) = @_;
1539 628 store_result(get_byte_at($array + $index));
1540 }
1541  
1542 13 ▶sub bitwise_and {
Subroutine bitwise_and: called CC=1 SCAR=0.0
Pod undocumented
1543 # story bitwise "and" of the arguments.
1544 # FIX ME: signed???
1545 13 store_result($_[0] & $_[1]);
1546 }
1547  
1548 0 ▶sub bitwise_or {
Subroutine bitwise_or: not called CC=1 SCAR=6.9
Pod undocumented
1549 # story bitwise "or" of the arguments.
1550 # FIX ME: signed???
1551 0 store_result($_[0] | $_[1]);
1552 }
1553  
1554 24 ▶sub rtrue {
Subroutine rtrue: called CC=1 SCAR=0.0
Pod undocumented
1555 # return TRUE from this subroutine.
1556 24 ret(1);
1557 }
1558  
1559 4 ▶sub rfalse {
Subroutine rfalse: called CC=1 SCAR=0.0
Pod undocumented
1560 # return FALSE from this subroutine.
1561 4 ret(0);
1562 }
1563  
1564 7 ▶sub write_text {
Subroutine write_text: called CC=2 SCAR=7.2
Pod undocumented
1565 # write a given string to ZIO.
1566 7 write_zchunk(\$_[0]);
1567 7 ▶ newline() if $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 0 7 7
1568 # foreach (unpack "C*", $_[1]) {
1569 # $_[0]->write_zchar($_);
1570 # }
1571 }
1572  
1573 4 ▶sub insert_obj {
Subroutine insert_obj: called CC=12 SCAR=30.9
Pod undocumented
1574 4 my ($object, $destination_obj) = @_;
1575 # move object to become the first child of the destination
1576 # object.
1577 #
1578 # object = O, destination_obj = D
1579 #
1580 # reorganize me: move to ZObject?
1581
1582 # my $o = new Games::Rezrov::ZObject($object, $self);
1583 4 ▶ return unless $object;
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 4 4
1584 # if object being moved is ID 0, do nothing (bogus object)
1585  
1586 4 my $o = get_zobject($object);
1587 # my $d = new Games::Rezrov::ZObject($destination_obj, $self);
1588 4 my $d = get_zobject($destination_obj);
1589  
1590 4 ▶ if ($player_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($player_object) 3 1 4
1591 # already know the object ID for the player
1592 3 ▶ $current_room = $destination_obj if $player_object == $object;
Branch: 1 branch
BranchTrueFalseTotal
if $player_object == $object 0 3 3
1593 3 ▶ if ($tailing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tailing) 0 3 3
1594 # we're tailing an object...
1595 0 ▶ if ($tailing == $object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tailing == $object) 0 0 0
1596 0 newline();
1597 0 write_text(sprintf "Tailing %s: you are now in %s...", ${$o->print}, ${$d->print});
1598 0 newline();
1599 0 insert_obj($player_object, $destination_obj);
1600 # $self->suppress_hack();
1601 # $self->push_command("look");
1602 }
1603 }
1604 }
1605  
1606 4 ▶ unless ($player_confirmed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($player_confirmed) 1 3 4
1607 # unless ($player_confirmed or $push_command) {
1608 # record object movements to determine which is the "player"
1609 # object, aka "cretin" :)
1610 1 ▶ if ($object_cache->is_room($destination_obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_cache->is_room($destination_obj)) 1 0 1
1611 1 $candidate_po{$lines_read}{$object} = $destination_obj;
1612 }
1613 }
1614  
1615 4 ▶ if (Games::Rezrov::ZOptions::SNOOP_OBJECTS()) {
Branch: 1 branch
BranchTrueFalseTotal
if (Games::Rezrov::ZOptions::SNOOP_OBJECTS()) 0 4 4
1616 0 my $o1 = $o->print($ztext);
1617 0 ▶ my $o2 = $d ? $d->print($ztext) : "(null)";
Branch: 1 branch
BranchTrueFalseTotal
$d ? : 0 0 0
1618 0 write_text(sprintf '[Move "%s" to "%s"]', $$o1, $$o2);
1619 0 newline();
1620 }
1621
1622 4 $o->remove();
1623 # unlink o from its parent and siblings
1624
1625 4 $o->set_parent_id($destination_obj);
1626 # set new o's parent to d
1627
1628 4 ▶ if ($d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d) 4 0 4
1629 # look out for destination of object 0
1630 4 my $old_child_id = $d->get_child_id();
1631
1632 4 $d->set_child_id($object);
1633 # set d's child ID to o
1634
1635 4 ▶ if ($old_child_id > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_child_id > 0) 3 1 4
1636 # d had children; make them the new siblings of o,
1637 # which is now d's child.
1638 3 $o->set_sibling_id($old_child_id);
1639 }
1640 }
1641
1642 }
1643  
1644 0 ▶sub pull {
Subroutine pull: not called CC=3 SCAR=24.8
Pod undocumented
1645 # pop a value from a stack and store in specified variable.
1646 # **indirect**
1647 0 ▶ if ($version == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version == 6) { } 0 0 0
1648 0 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 0 0 0
1649 0 fatal_error("v6: pull from user stack");
1650 } else {
1651 0 store_result(routine_pop());
1652 # broken? (indirect var?)
1653 }
1654 } else {
1655 0 set_variable($_[0], routine_pop(), 1);
1656 # set indirect stack reference mode
1657 }
1658 }
1659  
1660 27 ▶sub jin {
Subroutine jin: called CC=6 SCAR=22.9
Pod undocumented
1661 # jump if parent of obj1 is obj2
1662 # or if obj2 is 0 (null) and obj1 has no parent.
1663 27 my ($obj1, $obj2) = @_;
1664 # my $x = new Games::Rezrov::ZObject($obj1, $self);
1665 27 ▶ if ($obj1 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj1 == 0) { } 0 27 27
1666 # no such object; consider its parent zero as well
1667 0 ▶ conditional_jump($obj2 == 0 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$obj2 == 0 ? : 0 0 0
1668 } else {
1669 27 my $x = get_zobject($obj1);
1670 27 my $jump = 0;
1671 27 ▶ if ($obj2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj2 == 0) { } 0 27 27
1672 0 ▶ $jump = ($x->get_parent_id() == 0 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$x->get_parent_id == 0 ? : 0 0 0
1673 0 write_text("[ jin(): untested! ]");
1674 0 newline();
1675 } else {
1676 27 ▶ $jump = $x->get_parent_id() == $obj2 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->get_parent_id == $obj2 ? : 8 19 27
1677 }
1678 27 conditional_jump($jump);
1679 }
1680 }
1681  
1682 4 ▶sub print_object {
Subroutine print_object: called CC=3 SCAR=11.2
Pod undocumented
1683 # print short name of object (Z-encoded string in object property header)
1684 4 my $zobj = get_zobject($_[0]);
1685 4 my $highlight = Games::Rezrov::ZOptions::HIGHLIGHT_OBJECTS();
1686 # set_text_style(Games::Rezrov::ZConst::STYLE_BOLD) if $highlight;
1687 4 my $old;
1688 4 ▶ $old = swap_text_style(Games::Rezrov::ZConst::STYLE_BOLD) if $highlight;
Branch: 1 branch
BranchTrueFalseTotal
if $highlight 0 4 4
1689 4 write_zchunk($zobj->print($ztext));
1690 # set_text_style(Games::Rezrov::ZConst::STYLE_ROMAN) if $highlight;
1691 4 ▶ toggle_text_style($old) if $highlight;
Branch: 1 branch
BranchTrueFalseTotal
if $highlight 0 4 4
1692  
1693 }
1694  
1695 32 ▶sub get_parent {
Subroutine get_parent: called CC=2 SCAR=7.2
Pod undocumented
1696 # get parent object of this object and store result.
1697 # arg: object
1698 32 my $zobj = get_zobject($_[0]);
1699 32 ▶ store_result($zobj ? $zobj->get_parent_id() : 0);
Branch: 1 branch
BranchTrueFalseTotal
$zobj ? : 32 0 32
1700 # if object ID 0, will be undef
1701 }
1702  
1703 23 ▶sub get_child {
Subroutine get_child: called CC=2 SCAR=7.0
Pod undocumented
1704 # get child object ID for this object ID and store result, then
1705 # jump if it exists.
1706 #
1707 # arg: object
1708 23 my $zobj = get_zobject($_[0]);
1709 23 ▶ my $id = $zobj ? $zobj->get_child_id() : 0;
Branch: 1 branch
BranchTrueFalseTotal
$zobj ? : 23 0 23
1710 # if object ID 0, will be undef
1711 23 store_result($id);
1712 23 conditional_jump($id != 0);
1713 }
1714  
1715 21 ▶sub get_sibling {
Subroutine get_sibling: called CC=2 SCAR=7.0
Pod undocumented
1716 # get sibling object ID for this object ID and store result, then
1717 # jump if it exists.
1718 #
1719 # arg: object
1720 21 my $zobj = get_zobject($_[0]);
1721 21 ▶ my $id = $zobj ? $zobj->get_sibling_id() : 0;
Branch: 1 branch
BranchTrueFalseTotal
$zobj ? : 21 0 21
1722 # if object ID 0, will be undef
1723 21 store_result($id);
1724 21 conditional_jump($id != 0);
1725 }
1726  
1727 25 ▶sub get_property {
Subroutine get_property: called CC=2 SCAR=8.1
Pod undocumented
1728 # retrieve the specified property of the specified object.
1729 # args: $object, $property
1730 25 ▶ if (my $zobj = get_zobject($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $zobj = get_zobject($_[0])) { } 25 0 25
1731 25 store_result($zobj->get_property($_[1])->get_value());
1732 } else {
1733 # object 0
1734 0 store_result(0);
1735 }
1736 }
1737  
1738 17 ▶sub ret_popped {
Subroutine ret_popped: called CC=1 SCAR=0.0
Pod undocumented
1739 # return with a variable popped from the routine stack.
1740 17 ret(routine_pop());
1741 }
1742  
1743 0 ▶sub stack_pop {
Subroutine stack_pop: not called CC=2 SCAR=17.9
Pod undocumented
1744 # pop and discard topmost variable from the stack
1745 0 ▶ if ($version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= 5) { } 0 0 0
1746 0 fatal_error("catch() unimplemented");
1747 } else {
1748 0 routine_pop();
1749 }
1750 }
1751  
1752 4 ▶sub read_line {
Subroutine read_line: called CC=53 SCAR=65.3
Pod undocumented
1753 4 my ($argv, $interpreter, $start_pc) = @_;
1754 # Read and tokenize a command.
1755 # multi-arg approach taken from zip; this call has many
1756 # possible arguments.
1757  
1758 4 my $text_address = $argv->[0];
1759 4 ▶ my $token_address = $argv->[1] || 0;
Condition50%1/2
$argv->[1] || 0
l!l
40
MC/DC0%0/1
$argv->[1] || 0
$argv->[1]
Truth table50%1/2
A
A= $argv->[1]
Aresult
11
00
1760 4 my $time = 0;
1761 4 my $routine = 0;
1762 4 $lines_read++;
1763  
1764 4 ▶ if (%candidate_po) {
Branch: 1 branch
BranchTrueFalseTotal
if (%candidate_po) 1 3 4
1765 # we have possible candidates for the player object.
1766 #
1767 # ZTUU: 1st move, 2 items moved to player, player moved to room
1768 # LGOP: "stool" and "it" (player) both moved to same room
1769 # SeaStalker: don't even get me started [FIX ME]...
1770 #
1771 # - checking for a toplevel child works
1772 1 my %seen;
1773 my %dest;
1774 0 my @turns = (sort {$a <=> $b} keys %candidate_po);
1775 # move through turns sequentially
1776  
1777 1 foreach my $turn (@turns) {
1778 1 ▶ while (my ($pid, $dest) = each %{$candidate_po{$turn}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$candidate_po{$turn};}) 1 1 2
1779 # printf STDERR "Turn %d; %s => %s\n", $turn, $pid, $dest;
1780 # my $zs = new Games::Rezrov::ZObjectStatus($pid, $object_cache);
1781 # printf "yow -- %s: \"%s\"\n", $pid, $zs->is_toplevel_child();
1782 # next unless $zs->is_toplevel_child();
1783 # this disambiguates the player in ZTUU, but doesn't work for
1784 # seastalker [toplevel child detection broken because of
1785 # wacky case in location names]
1786 1 $seen{$pid}++;
1787 1 $dest{$pid} = $dest;
1788 # the most recent destination
1789 }
1790 }
1791  
1792 1 ▶ if ($version <= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version <= 3) 1 0 1
1793 1 my $current_room = get_global_var(0);
1794 # 8.2.2.1
1795 1 my @candidates = grep {$dest{$_} == $current_room} keys %dest;
1796 1 ▶ if (@candidates == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@candidates == 1) 1 0 1
1797 # in version 3 games, the current room is stored in global
1798 # variable 0. If only one object moved to that target, that's it.
1799 1 $player_object = $candidates[0];
1800 1 $player_confirmed = 1;
1801 # print STDERR "v3 confirmed: player is $player_object\n";
1802 }
1803 }
1804  
1805 1 ▶ unless ($player_confirmed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($player_confirmed) 0 1 1
1806 0 my @ok = grep {$seen{$_} > 1} keys %seen;
1807 0 ▶ if (@ok == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@ok == 1) { } 0 0 0
elsif (keys %seen == 1) { } 0 0 0
1808 # exactly one object was observed moving multiple turns
1809 0 $player_object = $ok[0];
1810 # print STDERR "confirmed: player is $player_object \n";
1811 0 $player_confirmed = 1;
1812 } elsif (keys %seen == 1) {
1813 # in many games, the first object moved is the player.
1814 # Temporarily consider this the player until confirmation;
1815 # allows us to teleport even as the first move of the game.
1816 #
1817 # not true in: LGOP, ZTUU, etc...
1818 0 $player_object = (keys %seen)[0];
1819 # print STDERR "candidate po: $player_object \n";
1820 } else {
1821 # printf STDERR "failed: %d (%s) %d (%s)\n", scalar keys %seen, (join ",", keys %seen), scalar @ok, join(",", @ok);
1822 }
1823 }
1824  
1825 1 ▶ %candidate_po = () if $player_confirmed;
Branch: 1 branch
BranchTrueFalseTotal
if $player_confirmed 1 0 1
1826
1827 1 ▶ delete $candidate_po{shift @turns} if @turns > 3;
Branch: 1 branch
BranchTrueFalseTotal
if @turns > 3 0 1 1
1828 # remove tracking for oldest turns
1829  
1830 1 ▶ $current_room = $dest{$player_object} if $player_object;
Branch: 1 branch
BranchTrueFalseTotal
if $player_object 1 0 1
1831 }
1832
1833 4 my $max_text_length = get_byte_at($text_address);
1834 4 ▶ $max_text_length++ if ($version <= 4);
Branch: 1 branch
BranchTrueFalseTotal
if $version <= 4 4 0 4
1835 # sect15.html#sread
1836
1837 4 ▶ if (@{$argv} > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$argv;} > 2) 0 4 4
1838 # timeout / routine specified
1839 0 $time = $argv->[2];
1840 0 $routine = $argv->[3];
1841 }
1842  
1843 4 $zdict->blood_pressure_cheat_hook();
1844 # hack
1845
1846 4 flush();
1847 # flush any buffered output before the prompt.
1848 # Also very important before hijacking/restoring ZIO when guessing
1849 # the title.
1850  
1851 4 reset_write_count();
1852  
1853 4 my $bef_pc = $Games::Rezrov::StoryFile::PC;
1854 4 my $s = "";
1855  
1856 4 my $guess_title = Games::Rezrov::ZOptions::GUESS_TITLE();
1857
1858 4 ▶ if (is_stream_selected(Games::Rezrov::ZConst::STREAM_STEAL)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_stream_selected(5)) { } 0 4 4
elsif ($guess_title) { } 0 4 4
1859 # suppressing parser output up until the next prompt
1860 0 my $old = $zios->[Games::Rezrov::ZConst::STREAM_STEAL];
1861 0 output_stream(- Games::Rezrov::ZConst::STREAM_STEAL);
1862 0 my $suppressed = $old->buffer();
1863 # print STDERR "steal active: $suppressed\n";
1864 0 ▶ if ($push_command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($push_command) { } 0 0 0
1865 0 $s = $push_command;
1866 # print STDERR "pushing: $s\n";
1867 0 $push_command = "";
1868 } else {
1869 0 ▶ if ($guessing_title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($guessing_title) 0 0 0
1870 0 $full_version_output = $suppressed;
1871 0 ▶ if ($suppressed =~ /\s*(.*?)[\x0a\x0d]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suppressed =~ /\s*(.*?)[\x0a\x0d]/) 0 0 0
1872 0 $game_title = $1;
1873 0 screen_zio()->set_game_title("rezrov: " . $1);
1874 }
1875 }
1876 0 my $regexp = '.*' . chr(Games::Rezrov::ZConst::Z_NEWLINE);
1877 # delete everything before the prompt (everything up to last newline)
1878 0 $suppressed =~ s/$regexp//o;
1879 0 $last_prompt = $suppressed;
1880 0 $prompt_buffer = $suppressed;
1881 # because flush() never sees the output this came from
1882
1883 0 ▶ if ($guessing_title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($guessing_title) { } 0 0 0
1884 # prompt was printed "last time", don't print again
1885 0 $guessing_title = 0;
1886 } else {
1887 # print the prompt
1888 0 screen_zio()->write_string($suppressed);
1889 }
1890 }
1891 } elsif ($guess_title) {
1892 #
1893 # The axe crashes against the rock, throwing sparks!
1894 #
1895 0 ▶ if (!$game_title and $player_object) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $game_title and $player_object) 0 0 0
Condition0%0/3
not $game_title and $player_object
!ll&&!rl&&r
000
MC/DC0%0/2
not $game_title and $player_object
not $game_title $player_object
Truth table0%0/3
not(A) and B
A= not $game_titleB= $player_object
ABresult
0X0
100
111
1896 # delay submitting the "version" command until an object has been
1897 # moved; this necessary for games that read a line before the real
1898 # parser starts. Example: Leather Goddesses of Phobos.
1899 # Doesn't work: AMFV
1900 0 ▶ if ($zdict->get_dictionary_address("version")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zdict->get_dictionary_address('version')) { } 0 0 0
1901 0 $guessing_title = 1;
1902 0 $s = "version";
1903 # submit a surreptitious "version" command to the interpreter
1904 0 suppress_hack();
1905 # temporarily hijack output
1906 } else {
1907 # game doesn't understand "version"; forget it.
1908 # example: Advent.z5
1909 0 $game_title = "not gonna happen";
1910 0 screen_zio()->set_game_title("rezrov");
1911 }
1912 }
1913 }
1914  
1915 4 my $undo_data;
1916 4 ▶ if (Games::Rezrov::ZOptions::EMULATE_UNDO()) {
Branch: 1 branch
BranchTrueFalseTotal
if (Games::Rezrov::ZOptions::EMULATE_UNDO()) 4 0 4
1917 # save undo information
1918 4 my $tmp_pc = $Games::Rezrov::StoryFile::PC;
1919 4 $Games::Rezrov::StoryFile::PC = $start_pc;
1920 # fix me: move to quetzal itself
1921 4 $undo_data = $quetzal->save("", "-undo" => 1);
1922 4 $Games::Rezrov::StoryFile::PC = $tmp_pc;
1923 }
1924  
1925 4 ▶ unless (length $s) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $s) 4 0 4
1926 4 ▶ if ($current_input_stream == Games::Rezrov::ZConst::INPUT_FILE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_input_stream == 1) 4 0 4
1927 #
1928 # we're fetching commands from a script file.
1929 #
1930 4 $s = <$input_filehandle>;
1931 4 ▶ if (defined($s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) { } 4 0 4
1932 # got a command; display it
1933 4 chomp $s;
1934 4 ▶ write_text($s || "");
Condition50%1/2
$s || ''
l!l
40
MC/DC0%0/1
$s || ''
$s
Truth table50%1/2
A
A= $s
Aresult
11
00
1935 4 newline();
1936 } else {
1937 # end of file
1938 0 input_stream(Games::Rezrov::ZConst::INPUT_KEYBOARD);
1939 0 ▶ die "quitting!\n" if Games::Rezrov::ZOptions::PLAYBACK_DIE();
Branch: 1 branch
BranchTrueFalseTotal
if Games::Rezrov::ZOptions::PLAYBACK_DIE() 0 0 0
1940 0 $s = "";
1941 }
1942 }
1943  
1944 4 ▶ unless (length $s) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $s) 0 4 4
1945 #
1946 # Get commands from the user
1947 #
1948 0 my $initial_buf;
1949 0 ▶ if ($version <= 3) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version <= 3) { } 0 0 0
elsif ($version >= 5) { } 0 0 0
1950 0 display_status_line();
1951 } elsif ($version >= 5) {
1952 # sect15.html#read
1953 # there may be some text already displayed as if we had typed it
1954 0 my $initial = get_byte_at($text_address + 1);
1955 0 ▶ $initial_buf = get_string_at($text_address + 2, $initial) if $initial;
Branch: 1 branch
BranchTrueFalseTotal
if $initial 0 0 0
1956 }
1957  
1958 0 my $sz = screen_zio();
1959
1960 0 $s = $sz->get_input($max_text_length, 0,
1961 "-time" => $time,
1962 "-routine" => $routine,
1963 "-zi" => $interpreter,
1964 "-preloaded" => $initial_buf,
1965 );
1966  
1967 0 ▶ if ($s and $sz->speaking and $s !~ /^\#speak/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s and $sz->speaking and not $s =~ /^\#speak/) 0 0 0
Condition0%0/6
$s and $sz->speaking and not $s =~ /^\#speak/
!ll&&!rl&&r
000
$s and $sz->speaking
!ll&&!rl&&r
000
MC/DC0%0/3
$s and $sz->speaking and not $s =~ /^\#speak/
$s $sz->speaking not $s =~ /^\#speak/
Truth table0%0/4
A and B and not(C)
A= $sB= $sz->speakingC= not $s =~ /^\#speak/
ABCresult
0XX0
10X0
1100
1111
1968 0 $sz->speak($s, "-gender" => 2);
1969 # say command unless we intend to turn off speech
1970 }
1971 }
1972 }
1973 # printf STDERR "cmd: $s\n";
1974  
1975 4 ▶ if (Games::Rezrov::ZOptions::CORRECT_TYPOS()) {
Branch: 1 branch
BranchTrueFalseTotal
if (Games::Rezrov::ZOptions::CORRECT_TYPOS()) 4 0 4
1976 4 my $msg;
1977 4 ($s, $msg) = $zdict->correct_typos($s);
1978 4 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 0 4 4
1979 0 write_text($msg);
1980 0 newline();
1981 0 ▶ unless ($TYPO_NOTIFY) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($TYPO_NOTIFY) 0 0 0
1982 0 write_text("[NOTE: you can toggle typo correction on or off at any time with the #TYPO command.]");
1983 0 newline();
1984 0 $TYPO_NOTIFY=1;
1985 }
1986 }
1987 }
1988  
1989 4 ▶ if (Games::Rezrov::ZOptions::EMULATE_UNDO()) {
Branch: 1 branch
BranchTrueFalseTotal
if (Games::Rezrov::ZOptions::EMULATE_UNDO()) 4 0 4
1990 4 ▶ if ($s eq "undo") {
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq 'undo') { } 0 4 4
1991 # want to undo; restore the old data
1992 0 ▶ if (@{$undo_slots}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$undo_slots;}) { } 0 0 0
1993 0 $quetzal->restore("", pop @{$undo_slots});
1994 0 write_text("Undone");
1995 0 ▶ if (@{$undo_slots}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$undo_slots;}) 0 0 0
1996 0 ▶ write_text(sprintf " (%d more turn%s may be undone)", scalar @{$undo_slots}, (scalar @{$undo_slots} == 1 ? "" : "s"));
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$undo_slots;} == 1 ? : 0 0 0
1997 }
1998 0 write_text(".");
1999 0 newline();
2000 0 newline();
2001 0 ▶ write_text($last_prompt || ">");
Condition0%0/2
$last_prompt || '>'
l!l
00
MC/DC0%0/1
$last_prompt || '>'
$last_prompt
Truth table0%0/2
A
A= $last_prompt
Aresult
11
00
2002 # hack!
2003  
2004 0 ▶ if ($player_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($player_object) 0 0 0
2005 # after we "undo" we might be in a different room; find
2006 # the current one. Important if we try to pilfer something:
2007 # without this, it will go to the room before the undo!
2008 0 $object_cache->load_names();
2009 0 my $zstat = new Games::Rezrov::ZObjectStatus($player_object,
2010 $object_cache);
2011 0 ▶ if (my $parent = $zstat->parent_room()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $zstat->parent_room) 0 0 0
2012 0 $current_room = $parent->object_id();
2013 }
2014 }
2015  
2016 0 return;
2017 } else {
2018 0 write_text("Can't undo now, sorry.");
2019 0 newline();
2020 0 newline();
2021 0 suppress_hack();
2022 }
2023 } else {
2024 # save this undo slot
2025 4 push @{$undo_slots}, $undo_data;
2026 4 ▶ while (@{$undo_slots} > Games::Rezrov::ZOptions::UNDO_SLOTS()) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$undo_slots;} > Games::Rezrov::ZOptions::UNDO_SLOTS()) 0 4 4
2027 # pop old ones
2028 0 shift @{$undo_slots};
2029 }
2030 }
2031 }
2032  
2033 4 ▶ die("PC corrupt after get_input; was:$bef_pc now:" . $Games::Rezrov::StoryFile::PC)
Branch: 1 branch
BranchTrueFalseTotal
if $Games::Rezrov::StoryFile::PC != $bef_pc 0 4 4
2034 if ($Games::Rezrov::StoryFile::PC != $bef_pc);
2035 # interrupt routine sanity check
2036  
2037  
2038 4 stream_dup(Games::Rezrov::ZConst::STREAM_TRANSCRIPT, $s);
2039 4 stream_dup(Games::Rezrov::ZConst::STREAM_COMMANDS, $s);
2040  
2041 # printf STDERR "input: %s\n", $s;
2042 4 $s = substr($s, 0, $max_text_length);
2043 # truncate input if necessary
2044  
2045 4 $zdict->save_buffer($s, $text_address);
2046
2047 4 ▶ if ($version >= 5 && $token_address == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= 5 and $token_address == 0) { } 0 4 4
Condition33%1/3
$version >= 5 and $token_address == 0
!ll&&!rl&&r
400
MC/DC0%0/2
$version >= 5 and $token_address == 0
$version >= 5 $token_address == 0
Truth table33%1/3
A and B
A= $version >= 5B= $token_address == 0
ABresult
0X0
100
111
2048 # print STDERR "Skipping tokenization; test this!\n";
2049 } else {
2050 4 $zdict->tokenize_line($text_address,
2051 $token_address,
2052 "-len" => length($s),
2053 );
2054 }
2055  
2056 # $zdict->last_buffer($s);
2057 # last_input = s;
2058 4 ▶ store_result(10) if ($version >= 5);
Branch: 1 branch
BranchTrueFalseTotal
if $version >= 5 0 4 4
2059 # sect15.html#sread; store terminating char ("newline")
2060  
2061 4 $last_input = $s;
2062 # save last user input; used in "oops" emulation
2063 }
2064  
2065 0 ▶sub read_char {
Subroutine read_char: not called CC=3 SCAR=24.8
Pod undocumented
2066 0 my ($argv, $zi) = @_;
2067 # read a single character
2068 0 reset_write_count();
2069 0 flush();
2070 # die("read_char: 1st arg must be 1") if ($argv->[0] != 1);
2071 0 my $time = 0;
2072 0 my $routine = 0;
2073 0 ▶ if (@{$argv} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$argv;} > 1) 0 0 0
2074 0 $time = $argv->[1];
2075 0 $routine = $argv->[2];
2076 }
2077 0 my $result = screen_zio()->get_input(1, 1,
2078 "-time" => $time,
2079 "-routine" => $routine,
2080 "-zi" => $zi);
2081 0 my $code = ord(substr($result,0,1));
2082 0 ▶ $code = Games::Rezrov::ZConst::Z_NEWLINE if ($code == LINEFEED);
Branch: 1 branch
BranchTrueFalseTotal
if $code == 10 0 0 0
2083 # remap keyboard "linefeed" to what the Z-machine
2084 # will recognize as a "carriage return". This is required
2085 # for the startup form in "Bureaucracy", and probably other
2086 # places.
2087 #
2088 # - does keyboard ever return 13 (non-IBM-clones)?
2089 #
2090 # In spec terms:
2091 # - 10.7: only return characters defined in input stream
2092 # - 3.8: character "10" (linefeed) only defined for output.
2093 0 store_result($code);
2094 # store ascii value
2095 }
2096  
2097  
2098 0 ▶sub display_status_line {
Subroutine display_status_line: not called CC=7 SCAR=40.3
Pod undocumented
2099 # only called if needed; see spec 8.2
2100 0 my $zio = screen_zio();
2101 0 ▶ return unless $zio->can_split();
Branch: 1 branch
BranchTrueFalseTotal
unless $zio->can_split 0 0 0
2102 0 $zstatus->update();
2103 0 my $right_chunk;
2104 0 ▶ if ($zstatus->time_game()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zstatus->time_game) { } 0 0 0
2105 0 my $hours = $zstatus->hours();
2106 0 ▶ if (Games::Rezrov::ZOptions::TIME_24()) {
Branch: 1 branch
BranchTrueFalseTotal
if (Games::Rezrov::ZOptions::TIME_24()) { } 0 0 0
2107 0 $right_chunk = sprintf("Time: %d:%02d%s", $hours,
2108 $zstatus->minutes());
2109 } else {
2110 0 ▶ $right_chunk = sprintf("Time: %d:%02d%s",
Branch: 2 branches
BranchTrueFalseTotal
$hours > 12 ? : 0 0 0
$hours < 12 ? : 0 0 0
2111 ($hours > 12 ? $hours - 12 : $hours),
2112 $zstatus->minutes(),
2113 ($hours < 12 ? "am" : "pm"));
2114 }
2115 } else {
2116 0 $right_chunk = sprintf "Score:%d Moves:%d", $zstatus->score(), $zstatus->moves();
2117 }
2118
2119 0 ▶ if ($zio->manual_status_line()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zio->manual_status_line) { } 0 0 0
2120 # the ZIO wants to handle it
2121 0 $zio->status_hook($zstatus->location(), $right_chunk);
2122 } else {
2123 # "generic" status line handling; broken for screen-splitting v3 games
2124 0 my $restore = $zio->get_position(1);
2125 0 $zio->status_hook(0);
2126 0 $zio->write_string((" " x $columns), 0, 0);
2127 # erase
2128 0 $zio->write_string($zstatus->location(), 0, 0);
2129
2130 0 $zio->write_string($right_chunk, $columns - length($right_chunk), 0);
2131 0 $zio->status_hook(1);
2132 0 &$restore();
2133 }
2134 }
2135  
2136 4 ▶sub print_paddr {
Subroutine print_paddr: called CC=1 SCAR=0.0
Pod undocumented
2137 # print the string at the packed address given.
2138 # arg: address
2139 4 write_zchunk(scalar $ztext->decode_text(convert_packed_address($_[0], PRINT_PADDR)));
2140 }
2141  
2142 0 ▶sub print_addr {
Subroutine print_addr: not called CC=1 SCAR=6.9
Pod undocumented
2143 # print the string at the address given; address is not packed
2144 # example: hollywood hijinx: "n", "knock"
2145 0 write_zchunk($ztext->decode_text($_[0]));
2146 }
2147  
2148 0 ▶sub remove_object {
Subroutine remove_object: not called CC=4 SCAR=30.0
Pod undocumented
2149 # remove an object from its parent
2150 0 my ($object) = @_;
2151 0 ▶ if (my $zobj = get_zobject($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $zobj = get_zobject($object)) 0 0 0
2152 # beware object 0
2153 0 $zobj->remove();
2154 0 ▶ if ($tailing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tailing) 0 0 0
2155 0 ▶ if ($tailing == $zobj->object_id()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tailing == $zobj->object_id) 0 0 0
2156 0 write_text(sprintf "You can no longer tail %s.", ${$zobj->print});
2157 0 newline();
2158 0 $tailing = 0;
2159 }
2160 }
2161 }
2162 }
2163  
2164 12 ▶sub get_property_addr {
Subroutine get_property_addr: called CC=3 SCAR=12.0
Pod undocumented
2165 12 my ($object, $property) = @_;
2166 # store data address for given property of given object.
2167 # If property doesn't exist, store zero.
2168 12 ▶ if (my $zobj = get_zobject($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $zobj = get_zobject($object)) { } 12 0 12
2169 12 my $zprop = $zobj->get_property($property);
2170 12 ▶ if ($zprop->property_exists()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zprop->property_exists) { } 12 0 12
2171 12 my $addr = $zprop->get_data_address();
2172 # printf STDERR "get_prop_addr for %s/%s=%s\n", $object, $property, $addr;
2173 12 store_result($addr);
2174 } else {
2175 0 store_result(0);
2176 }
2177 } else {
2178 # object 0
2179 0 store_result(0);
2180 }
2181 }
2182  
2183 43 ▶sub test_flags {
Subroutine test_flags: called CC=1 SCAR=0.0
Pod undocumented
2184 # jump if all flags in bitmap are set
2185 43 my ($bitmap, $flags) = @_;
2186 43 conditional_jump(($bitmap & $flags) == $flags);
2187 }
2188  
2189 0 ▶sub get_next_property {
Subroutine get_next_property: not called CC=4 SCAR=30.0
Pod undocumented
2190 0 my ($object, $property) = @_;
2191 # return property number of the next property provided by
2192 # the given object's given property. With argument 0,
2193 # load property number of first property provided by that object.
2194 # example: zork 2 start, "get all"
2195  
2196 0 my $zobj = get_zobject($object);
2197  
2198 0 my $result = 0;
2199 0 ▶ if ($zobj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zobj) 0 0 0
2200 # look out for object 0
2201 0 ▶ if ($property == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($property == 0) { } 0 0 0
2202 # sect15.html#get_next_prop:
2203 # if called with zero, it gives the first property number present.
2204 0 my $zp = $zobj->get_property(Games::Rezrov::ZProperty::FIRST_PROPERTY);
2205 0 $result = $zp->property_number();
2206 } else {
2207 0 my $zp = $zobj->get_property($property);
2208 0 ▶ if ($zp->property_exists()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zp->property_exists) { } 0 0 0
2209 0 $result = $zp->get_next()->property_number();
2210 } else {
2211 0 die("attempt to get next after bogus property");
2212 }
2213 }
2214 }
2215 0 store_result($result);
2216 }
2217  
2218  
2219 0 ▶sub scan_table {
Subroutine scan_table: not called CC=7 SCAR=40.3
Pod undocumented
2220 # args: search, table, len [form]
2221 # Is "search" one of the entries in "table", which is "num_entries" entries
2222 # long? So return the address where it first occurs and branch. If not,
2223 # return 0 and don't. May be byte/word entries.
2224 0 my ($search, $table, $num_entries, $form) = @_;
2225 0 my ($entry_len, $check_len);
2226 0 ▶ if (defined $form) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $form) { } 0 0 0
2227 # write_text("[custom form, check me!]");
2228 # newline();
2229
2230 0 $entry_len = $form & 0x7f;
2231 # length of each entry in the table
2232 0 ▶ $check_len = ($form & 0x80) > 0 ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
($form & 128) > 0 ? : 0 0 0
2233 # how many of the first bytes in each entry to check
2234 } else {
2235 0 $check_len = $entry_len = 2;
2236 }
2237 0 my ($addr, $value, $entry_count);
2238 0 my $found = 0;
2239 0 ▶ for ($addr = $table, $entry_count = 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($entry_count < $num_entries) 0 0 0
2240 $entry_count < $num_entries;
2241 $entry_count++, $addr += $entry_len) {
2242 0 ▶ $value = ($check_len == 1) ?
Branch: 1 branch
BranchTrueFalseTotal
$check_len == 1 ? : 0 0 0
2243 get_byte_at($addr) : get_word_at($addr);
2244 # yeah, yeah, it'd be more efficient to have a separate
2245 # loop, one for byte and one for word...
2246 0 ▶ $found = 1, last if ($value == $search);
Branch: 1 branch
BranchTrueFalseTotal
if $value == $search 0 0 0
2247 }
2248
2249 0 ▶ store_result($found ? $addr : 0);
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 0 0 0
2250 0 conditional_jump($found);
2251 }
2252  
2253 3 ▶sub set_window {
Subroutine set_window: called CC=4 SCAR=18.9
Pod undocumented
2254 3 my ($window) = @_;
2255 # print STDERR "set_window $window\n";
2256 3 flush();
2257 3 my $zio = screen_zio();
2258  
2259 3 $window_cursors->[$current_window] = $zio->get_position(1);
2260 # save callback to restore cursor position when we return to
2261 # this window
2262
2263 3 $current_window = $window;
2264 # set current window
2265  
2266 3 ▶ if ($version >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= 4) { } 0 3 3
2267 0 ▶ if ($current_window == Games::Rezrov::ZConst::UPPER_WIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_window == 1) { } 0 0 0
2268 # 8.7.2: whenever upper window selected, cursor goes to top left
2269 0 set_cursor(1,1);
2270 } else {
2271 # restore old cursor position
2272 0 my $restore;
2273 0 ▶ if (defined $window_cursors->[$current_window]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $window_cursors->[$current_window]) { } 0 0 0
2274 # restore former cursor position
2275 0 &{$window_cursors->[$current_window]};
2276 } else {
2277 # first switch to window
2278 0 $zio->absolute_move(0, $rows - 1);
2279 # 8.7.2.2: in v4 lower window cursor is always on last line.
2280 }
2281 }
2282 } else {
2283 # in v3, cursor always in lower left
2284 3 $zio->absolute_move(0, $rows - 1);
2285 }
2286 3 $zio->set_window($window);
2287 # for any local housekeeping
2288 3 $zio->set_text_style(font_mask());
2289 # since we always print in fixed font in the upper window,
2290 # make sure the zio gets a chance to turn this on/off as we enter/leave;
2291 # example: photopia.
2292 }
2293  
2294 1 ▶sub set_cursor {
Subroutine set_cursor: called CC=3 SCAR=11.4
Pod undocumented
2295 1 my ($line, $column, $win) = @_;
2296 1 my $zio = screen_zio();
2297 1 ▶ $zio->fatal_error("set_cursor on window $win not supported") if $win;
Branch: 1 branch
BranchTrueFalseTotal
if $win 0 1 1
2298  
2299 1 $line--;
2300 1 $column--;
2301 # given starting at 1, not 0
2302
2303 # print STDERR "set_cursor\n";
2304 1 ▶ if ($current_window == Games::Rezrov::ZConst::UPPER_WIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_window == 1) { } 0 1 1
2305 # upper window: use offsets as specified
2306 0 $zio->absolute_move($column, $line);
2307 } else {
2308 # lower window: map coordinates given upper window size
2309 1 $zio->absolute_move($column, $line + $upper_lines);
2310 }
2311 }
2312  
2313  
2314 29 ▶sub write_zchar {
Subroutine write_zchar: called CC=22 SCAR=44.7
Pod undocumented
2315 #
2316 # write a decoded z-char to selected output streams.
2317 #
2318 29 ▶ return if $_[0] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] == 0 0 29 29
2319 # 3.8.2.1: "null" has no effect on any output stream
2320  
2321 29 ▶ if (($_[0] >= 179 and $_[0] <= 218) or
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] >= 179 and $_[0] <= 218 or $_[0] >= 24 and $_[0] <= 27) 0 29 29
Condition44%4/9
$_[0] >= 179 and $_[0] <= 218 or $_[0] >= 24 and $_[0] <= 27
l!l&&r!l&&!r
0029
$_[0] >= 179 and $_[0] <= 218
!ll&&!rl&&r
2900
$_[0] >= 24 and $_[0] <= 27
!ll&&!rl&&r
17120
MC/DC0%0/4
$_[0] >= 179 and $_[0] <= 218 or $_[0] >= 24 and $_[0] <= 27
$_[0] >= 179 $_[0] <= 218 $_[0] >= 24 $_[0] <= 27
Truth table28%2/7
A and B or C and D
A= $_[0] >= 179B= $_[0] <= 218C= $_[0] >= 24D= $_[0] <= 27
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
2322 ($_[0] >= 0x18 and $_[0] <= 0x1b)) {
2323 # sect16.html; convert IBM PC graphics codes
2324 0 ▶ my $trans = $Z_TRANSLATIONS{$_[0]} || "*";
Condition0%0/2
$Z_TRANSLATIONS{$_[0]} || '*'
l!l
00
MC/DC0%0/1
$Z_TRANSLATIONS{$_[0]} || '*'
$Z_TRANSLATIONS{$_[0]}
Truth table0%0/2
A
A= $Z_TRANSLATIONS{$_[0]}
Aresult
11
00
2325 # print STDERR "trans for $_[0] => $trans\n";
2326 0 ▶ if (length $trans == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $trans == 1) { } 0 0 0
2327 0 $_[0] = ord($trans);
2328 } else {
2329 0 write_zchunk(\$trans);
2330 0 return;
2331 }
2332 }
2333
2334 29 ▶ if ($selected_streams->[Games::Rezrov::ZConst::STREAM_REDIRECT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selected_streams->[3]) { } 0 29 29
2335 #
2336 # 7.1.2.2: when active, no other streams get output
2337 #
2338 0 my $stack = $zios->[Games::Rezrov::ZConst::STREAM_REDIRECT];
2339 # printf STDERR "redirected: %s (%d)\n", chr($_[0]), $_[0];
2340 0 $stack->[$#$stack]->write_zchar($_[0]);
2341 } else {
2342 #
2343 # all the other streams
2344 #
2345 29 ▶ if ($selected_streams->[Games::Rezrov::ZConst::STREAM_SCREEN]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selected_streams->[1]) 29 0 29
2346 #
2347 # screen
2348 #
2349 29 ▶ if ($selected_streams->[Games::Rezrov::ZConst::STREAM_SCREEN]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selected_streams->[1]) 29 0 29
2350 29 ▶ if ($selected_streams->[Games::Rezrov::ZConst::STREAM_STEAL] and
Branch: 1 branch
BranchTrueFalseTotal
if ($selected_streams->[5] and $current_window == 0) { } 0 29 29
Condition33%1/3
$selected_streams->[5] and $current_window == 0
!ll&&!rl&&r
2900
MC/DC0%0/2
$selected_streams->[5] and $current_window == 0
$selected_streams->[5] $current_window == 0
Truth table33%1/3
A and B
A= $selected_streams->[5]B= $current_window == 0
ABresult
0X0
100
111
2351 $current_window == Games::Rezrov::ZConst::LOWER_WIN) {
2352 # temporarily steal lower window output
2353 0 $zios->[Games::Rezrov::ZConst::STREAM_STEAL]->buffer_zchar($_[0]);
2354 } else {
2355 29 my $zio = $zios->[Games::Rezrov::ZConst::STREAM_SCREEN];
2356
2357 29 ▶ if ($buffering and $current_window != Games::Rezrov::ZConst::UPPER_WIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffering and $current_window != 1) { } 28 1 29
Condition66%2/3
$buffering and $current_window != 1
!ll&&!rl&&r
1028
MC/DC50%1/2
$buffering and $current_window != 1
$buffering $current_window != 1
Truth table66%2/3
A and B
A= $bufferingB= $current_window != 1
ABresult
0X0
100
111
2358 # 8.7.2.5: buffering never active in upper window (v. 3-5)
2359 28 ▶ if ($_[0] == Games::Rezrov::ZConst::Z_NEWLINE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] == 13) { } 16 12 28
2360 16 flush();
2361 16 $prompt_buffer = "";
2362 16 $zio->newline();
2363 } else {
2364 12 $zio->buffer_zchar($_[0]);
2365 }
2366 } else {
2367 # buffering off, or upper window
2368 1 ▶ if ($_[0] == Games::Rezrov::ZConst::Z_NEWLINE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] == 13) { } 1 0 1
2369 1 $prompt_buffer = "";
2370 1 $zio->newline();
2371 } else {
2372 0 $zio->write_zchar($_[0]);
2373 }
2374 }
2375 }
2376 }
2377 }
2378  
2379 29 ▶ if ($selected_streams->[Games::Rezrov::ZConst::STREAM_TRANSCRIPT] and
Branch: 1 branch
BranchTrueFalseTotal
if ($selected_streams->[2] and $current_window == 0) 0 29 29
Condition33%1/3
$selected_streams->[2] and $current_window == 0
!ll&&!rl&&r
2900
MC/DC0%0/2
$selected_streams->[2] and $current_window == 0
$selected_streams->[2] $current_window == 0
Truth table33%1/3
A and B
A= $selected_streams->[2]B= $current_window == 0
ABresult
0X0
100
111
2380 $current_window == Games::Rezrov::ZConst::LOWER_WIN) {
2381 #
2382 # Game transcript
2383 #
2384 0 my $fh = $zios->[Games::Rezrov::ZConst::STREAM_TRANSCRIPT];
2385 0 ▶ print $fh (($_[0] || 0) == Games::Rezrov::ZConst::Z_NEWLINE) ? ($\ || "\n") : chr($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
($_[0] || 0) == 13 ? : 0 0 0
Condition0%0/4
$_[0] || 0
l!l
00
$\ or "\n"
l!l
00
MC/DC0%0/1
$_[0] || 0
$_[0]
MC/DC0%0/1
$\ or "\n"
$\
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
Truth table0%0/2
A
A= $\
Aresult
11
00
2386 }
2387 }
2388 }
2389  
2390 44 ▶sub screen_zio {
Subroutine screen_zio: called CC=1 SCAR=0.0
Pod undocumented
2391 # get the ZIO for the screen
2392 44 return $zios->[Games::Rezrov::ZConst::STREAM_SCREEN];
2393 }
2394  
2395 0 ▶sub restore {
Subroutine restore: not called CC=8 SCAR=42.8
Pod undocumented
2396 # restore game
2397 0 ▶ my $filename = filename_prompt("-default" => $last_savefile || "",
Condition0%0/2
$last_savefile || ''
l!l
00
MC/DC0%0/1
$last_savefile || ''
$last_savefile
Truth table0%0/2
A
A= $last_savefile
Aresult
11
00
2398 "-ext" => "sav",
2399 );
2400 0 my $success = 0;
2401 0 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) 0 0 0
2402 0 $last_savefile = $filename;
2403 0 $success = $quetzal->restore($filename);
2404 0 ▶ if (!$success and $quetzal->error_message()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $success and $quetzal->error_message) 0 0 0
Condition0%0/3
not $success and $quetzal->error_message
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $quetzal->error_message
not $success $quetzal->error_message
Truth table0%0/3
not(A) and B
A= not $successB= $quetzal->error_message
ABresult
0X0
100
111
2405 0 write_text($quetzal->error_message());
2406 0 newline();
2407 }
2408 }
2409  
2410 0 reset_cheats();
2411  
2412 # $last_score = get_global_var(GV_SCORE);
2413 # for NOTIFY emulation not to get confused after restore
2414  
2415 0 ▶ if ($version <= 3) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version <= 3) { } 0 0 0
elsif ($version == 4) { } 0 0 0
2416 0 conditional_jump($success);
2417 } elsif ($version == 4) {
2418 # sect15.html#save
2419 0 ▶ store_result($success ? 2 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
2420 } else {
2421 0 store_result($success);
2422 }
2423 }
2424  
2425 0 ▶sub filename_prompt {
Subroutine filename_prompt: not called CC=8 SCAR=42.8
Pod undocumented
2426 # my ($self, $prompt, $exist_check, $snide) = @_;
2427 0 my (%options) = @_;
2428
2429 0 ▶ my $ext = $options{"-ext"} || die;
Condition0%0/2
$options{'-ext'} || die()
l!l
00
MC/DC0%0/1
$options{'-ext'} || die()
$options{'-ext'}
Truth table0%0/2
A
A= $options{'-ext'}
Aresult
11
00
2430 0 my $default;
2431 0 ▶ unless ($default = $options{"-default"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($default = $options{"-default"}) 0 0 0
2432 0 ($default = $game_filename) =~ s/\..*//;
2433 0 $default .= ".$ext";
2434 }
2435  
2436 0 my $zio = screen_zio();
2437 0 my $prompt = sprintf "Filename [%s]: ", $default;
2438 0 $zio->write_string(sprintf "Filename [%s]: ", $default);
2439 0 $prompt_buffer = $prompt;
2440 # write_text(sprintf "Filename [%s]: ", $default);
2441 0 ▶ my $filename = $zio->get_input(50, 0) || $default;
Condition0%0/3
$zio->get_input(50, 0) || $default
l!l&&r!l&&!r
000
MC/DC0%0/2
$zio->get_input(50, 0) || $default
$zio->get_input(50, 0) $default
Truth table0%0/3
A || B
A= $zio->get_input(50, 0)B= $default
ABresult
1X1
011
000
2442 0 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) 0 0 0
2443 0 ▶ if ($options{"-check"} and -f $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-check"} and -f $filename) 0 0 0
Condition0%0/3
$options{"-check"} and -f $filename
!ll&&!rl&&r
000
MC/DC0%0/2
$options{"-check"} and -f $filename
$options{"-check"} -f $filename
Truth table0%0/3
A and B
A= $options{"-check"}B= -f $filename
ABresult
0X0
100
111
2444 0 $zio->write_string($filename . " exists, overwrite? [y/n]: ");
2445 0 $zio->update();
2446 0 my $proceed = $zio->get_input(1, 1);
2447 0 ▶ if ($proceed =~ /y/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proceed =~ /y/i) { } 0 0 0
2448 0 write_text("Yes.");
2449 0 unlink($filename);
2450 } else {
2451 0 write_text("No.");
2452 0 $filename = "";
2453 }
2454 0 newline();
2455 }
2456 }
2457
2458 0 return $filename;
2459 }
2460  
2461 0 ▶sub save {
Subroutine save: not called CC=5 SCAR=34.0
Pod undocumented
2462 # save game
2463 0 my $filename = filename_prompt("-ext" => "sav",
2464 "-check" => 1);
2465 0 my $success = 0;
2466 0 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) 0 0 0
2467 0 $last_savefile = $filename;
2468 # $success = $q->save($filename, "-umem" => 1);
2469 0 $success = $quetzal->save($filename);
2470 0 ▶ if (!$success and $quetzal->error_message()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $success and $quetzal->error_message) 0 0 0
Condition0%0/3
not $success and $quetzal->error_message
!ll&&!rl&&r
000
MC/DC0%0/2
not $success and $quetzal->error_message
not $success $quetzal->error_message
Truth table0%0/3
not(A) and B
A= not $successB= $quetzal->error_message
ABresult
0X0
100
111
2471 0 write_text($quetzal->error_message());
2472 0 newline();
2473 }
2474 }
2475  
2476 0 ▶ if ($version <= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version <= 3) { } 0 0 0
2477 0 conditional_jump($success);
2478 } else {
2479 # v4 +
2480 0 store_result($success);
2481 }
2482 }
2483  
2484 0 ▶sub set_game_state {
Subroutine set_game_state: not called CC=1 SCAR=6.9
Pod undocumented
2485 # called from Quetzal restore routines
2486 0 my ($stack, $pc) = @_;
2487 0 $call_stack = $stack;
2488 0 $current_frame = $stack->[$#$stack];
2489 0 $Games::Rezrov::StoryFile::PC = $pc;
2490 }
2491  
2492 0 ▶sub snide_message {
Subroutine snide_message: not called CC=1 SCAR=6.9
Pod undocumented
2493 0 my @messages = ("Fine, be that way.",
2494 "Eh? Speak up!",
2495 "What?",
2496 );
2497 0 return $messages[int(rand(scalar @messages))];
2498 }
2499  
2500 0 ▶sub save_undo {
Subroutine save_undo: not called CC=1 SCAR=6.9
Pod undocumented
2501 # v5+, save to RAM
2502 # BROKEN
2503 0 if (0) {
2504 my $undo_data = $quetzal->save("", "-undo" => 1);
2505 # print "saved $undo_data\n";
2506 $undo_slots = [ $undo_data ];
2507 store_result(1);
2508 } else {
2509 # not supported
2510 0 store_result(-1);
2511 }
2512 }
2513  
2514 0 ▶sub restore_undo {
Subroutine restore_undo: not called CC=1 SCAR=6.9
Pod undocumented
2515 # v5+, restore from RAM
2516 # BROKEN
2517 0 if (0) {
2518 # print "restoring " . $undo_slots->[0] . "\n";
2519 my $status = @{$undo_slots} ? $quetzal->restore("", pop @{$undo_slots}) : 0;
2520 store_result($status);
2521 } else {
2522 0 store_result(0);
2523 }
2524 }
2525  
2526 0 ▶sub check_arg_count {
Subroutine check_arg_count: not called CC=1 SCAR=6.9
Pod undocumented
2527 # sect15.html#check_arg_count
2528 # branch if the given argument number has been provided by the routine
2529 # call to the current routine
2530 0 conditional_jump(frame_argc() >= $_[0]);
2531 }
2532  
2533 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
2534 # must be defined so our AUTOLOAD won't catch destructor and complain
2535 1 1;
2536 }
2537  
2538 0 ▶sub suppress_hack {
Subroutine suppress_hack: not called CC=1 SCAR=6.9
Pod undocumented
2539 # used when we're pulling a fast one with the parser,
2540 # intercepting user input. Suppress the game's output (usually
2541 # complaints about unknown vocabulary), restoring i/o and
2542 # printing the prompt (which is everything after the last
2543 # Games::Rezrov::ZConst::Z_NEWLINE) during the read_line() opcode.
2544 # cluck "suppress_hack\n";
2545 0 output_stream(Games::Rezrov::ZConst::STREAM_STEAL);
2546 }
2547  
2548 0 ▶sub print_table {
Subroutine print_table: not called CC=7 SCAR=40.3
Pod undocumented
2549 # print a "window" of text onscreen. Given text and width,
2550 # decode characters, moving down a line every "width" characters
2551 # to the same column (x position) where the table started.
2552 #
2553 # example: "sherlock", start game and enter "knock"
2554 0 my ($text, $width, $height, $skip) = @_;
2555 0 ▶ $height = 1 unless defined $height;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $height 0 0 0
2556 0 ▶ $skip = 0 unless defined $skip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $skip 0 0 0
2557 0 my $zio = screen_zio();
2558 0 my ($i, $j);
2559 0 my ($x, $y) = $zio->get_position();
2560  
2561 # printf STDERR "print_table: %s w:%d h:%d sk:%d\n", get_string_at($text, $width * $height), $width, $height, $skip;
2562  
2563 0 flush();
2564  
2565 0 my $char;
2566 0 ▶ for (my $i=0; $i < $height; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $height) 0 0 0
2567 0 ▶ for(my $j=0; $j < $width; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $width) 0 0 0
2568 # printf STDERR "pt: %d (%s)\n", get_byte_at($text), chr(get_byte_at($text));
2569 0 $char = get_byte_at($text++);
2570 # if ($char == Games::Rezrov::ZConst::Z_NEWLINE) {
2571 # die "hey now";
2572 # }
2573 0 write_zchar($char);
2574 }
2575 0 flush();
2576 # flush buffered text before moving to next line
2577 0 ▶ if ($skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) 0 0 0
2578 # optionally skip specified number of chars between lines
2579 0 untested();
2580 0 $text += $skip;
2581 }
2582 0 ▶ if ($height > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($height > 1) 0 0 0
2583 0 $zio->absolute_move($x, ++$y);
2584 # fix me: what if this goes out of bounds of the current window?
2585 }
2586 }
2587 }
2588  
2589 0 ▶sub set_font {
Subroutine set_font: not called CC=3 SCAR=24.8
Pod undocumented
2590 0 flush();
2591 0 ▶ if ($_[0] == 3 and $font_3_disabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] == 3 and $font_3_disabled) { } 0 0 0
Condition0%0/3
$_[0] == 3 and $font_3_disabled
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] == 3 and $font_3_disabled
$_[0] == 3 $font_3_disabled
Truth table0%0/3
A and B
A= $_[0] == 3B= $font_3_disabled
ABresult
0X0
100
111
2592 # game wants font 3 but user has disabled it.
2593 # Don't even inform the ZIO.
2594 0 store_result(0);
2595 } else {
2596 0 store_result(screen_zio()->set_font($_[0]));
2597 }
2598 }
2599  
2600 0 ▶sub set_color {
Subroutine set_color: not called CC=7 SCAR=40.3
Pod undocumented
2601 0 my ($fg, $bg, $win) = @_;
2602 0 ▶ die sprintf("v6; fix me! %s", join ",", @_) if defined $win;
Branch: 1 branch
BranchTrueFalseTotal
if defined $win 0 0 0
2603 0 my $zio = screen_zio();
2604 0 flush();
2605 0 ▶ if ($zio->can_use_color()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zio->can_use_color) 0 0 0
2606 0 foreach ([ $fg, 'fg' ],
2607 [ $bg, 'bg' ]) {
2608 0 my ($color_code, $method) = @{$_};
2609 0 ▶ if ($color_code == Games::Rezrov::ZConst::COLOR_CURRENT) {
Branch: 3 branches
BranchTrueFalseTotal
if ($color_code == 0) { } 0 0 0
elsif ($color_code == 1) { } 0 0 0
elsif (my $name = Games::Rezrov::ZConst::color_code_to_name($color_code)) { } 0 0 0
2610 # nop?
2611 0 print STDERR "set color to current; huh?\n";
2612 } elsif ($color_code == Games::Rezrov::ZConst::COLOR_DEFAULT) {
2613 0 my $m2 = 'default_' . $method;
2614 0 $zio->$method($zio->$m2());
2615 } elsif (my $name = Games::Rezrov::ZConst::color_code_to_name($color_code)) {
2616 0 $zio->$method($name);
2617 # printf STDERR "set %s to %s\n", $method, $name;
2618 } else {
2619 0 die "set_color(): eek, " . $color_code;
2620 }
2621 }
2622 0 $zio->color_change_notify();
2623 }
2624 }
2625
2626 0 ▶sub fatal_error {
Subroutine fatal_error: not called CC=1 SCAR=6.9
Pod undocumented
2627 0 my $zio = screen_zio();
2628 0 $zio->newline();
2629 0 $zio->fatal_error($_[0]);
2630 }
2631  
2632 1 ▶sub split_window {
Subroutine split_window: called CC=2 SCAR=7.1
Pod undocumented
2633 1 my ($lines) = @_;
2634 1 my $zio = screen_zio();
2635  
2636 1 $upper_lines = $lines;
2637 1 $lower_lines = $rows - $lines;
2638 # print STDERR "split_window to $lines, ll=$lower_lines ul=$upper_lines\n";
2639  
2640 1 my ($x, $y) = $zio->get_position();
2641 1 ▶ if ($y < $upper_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $upper_lines) 0 1 1
2642 # 8.7.2.2
2643 0 $zio->absolute_move($x, $upper_lines);
2644 }
2645 1 screen_zio()->split_window($lines);
2646 # any local housekeeping
2647 }
2648  
2649 0 ▶sub play_sound_effect {
Subroutine play_sound_effect: not called CC=1 SCAR=6.9
Pod undocumented
2650 # hmm, should we pass this through?
2651 0 screen_zio()->play_sound_effect(@_);
2652 }
2653  
2654 1 ▶sub input_stream {
Subroutine input_stream: called CC=9 SCAR=30.5
Pod undocumented
2655 1 my ($stream, $filename) = @_;
2656 # $filename is an extension (only used internally)
2657 1 $current_input_stream = $stream;
2658 1 ▶ if ($stream == Games::Rezrov::ZConst::INPUT_FILE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($stream == 1) { } 1 0 1
elsif ($stream eq 0) { } 0 0 0
2659 1 ▶ my $fn = $filename || filename_prompt("-ext" => "cmd");
Condition33%1/3
$filename || filename_prompt('-ext', 'cmd')
l!l&&r!l&&!r
100
MC/DC0%0/2
$filename || filename_prompt('-ext', 'cmd')
$filename filename_prompt('-ext', 'cmd')
Truth table33%1/3
A || B
A= $filenameB= filename_prompt('-ext', 'cmd')
ABresult
1X1
011
000
2660 # filename provided if playing back from command line
2661 1 my $ok = 0;
2662 1 ▶ if ($fn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn) 1 0 1
2663 1 ▶ if ($fn =~ /^\*main:/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fn =~ /^\*main:/) { } 1 0 1
elsif (open TRANS_IN, $fn) { } 0 0 0
2664 # hack for test.pl
2665 1 $ok = 1;
2666 1 $input_filehandle = $fn;
2667 } elsif (open(TRANS_IN, $fn)) {
2668 0 $ok = 1;
2669 0 $input_filehandle = \*TRANS_IN;
2670 0 ▶ write_text("Playing back commands from $fn...") unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 0 0
2671 # if name provided, don't print this message
2672 } else {
2673 0 write_text("Can't open \"$fn\" for playback: $!");
2674 }
2675 1 newline();
2676 }
2677 1 ▶ $current_input_stream = Games::Rezrov::ZConst::INPUT_KEYBOARD unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 1 1
2678 } elsif ($stream eq Games::Rezrov::ZConst::INPUT_KEYBOARD) {
2679 0 close TRANS_IN;
2680 } else {
2681 0 die;
2682 }
2683 }
2684  
2685 1 ▶sub set_buffering {
Subroutine set_buffering: called CC=1 SCAR=0.0
Pod undocumented
2686 # whether text buffering is active
2687 # printf STDERR "set_buffering: $_[0]\n";
2688 1 $buffering = $_[0] == 1;
2689 }
2690  
2691 # font_mask() or font_mask(newmask)
2692 # specifying newmask replaces the current font mask
2693 # In either case, the returned mask is fudged a bit (for example, STYLE_FIXED is coerced if we're the upper window).
2694  
2695 17 ▶sub font_mask {
Subroutine font_mask: called CC=4 SCAR=13.9
Pod undocumented
2696 17 ▶ $fm = $_[0] if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 6 11 17
2697 17 ▶ my $fm2 = $fm || 0;
Condition100%2/2
$fm || 0
l!l
413
MC/DC100%1/1
$fm || 0
$fm
Truth table100%2/2
A
A= $fm
Aresult
11
00
2698 17 ▶ $fm2 |= Games::Rezrov::ZConst::STYLE_FIXED
Branch: 1 branch
BranchTrueFalseTotal
if $current_window == 1 0 17 17
2699 if $current_window == Games::Rezrov::ZConst::UPPER_WIN;
2700 # 8.7.2.4:
2701 # An interpreter should use a fixed-pitch font when printing on the
2702 # upper window.
2703  
2704 17 if (0 and $header and $header->fixed_font_forced()) {
2705 # 8.1: game forcing use of fixed-width font
2706 # DISABLED: something seems to be wrong here...
2707 # photopia (all v5 games?) turn on this bit after 1 move?
2708 $fm2 |= Games::Rezrov::ZConst::STYLE_FIXED;
2709 }
2710  
2711 17 return $fm2;
2712 }
2713  
2714 4 ▶sub set_text_style {
Subroutine set_text_style: called CC=2 SCAR=6.9
Pod undocumented
2715 # sets the specified style bits on the font, *unless* the value
2716 # STYLE_ROMAN is specified in which case all style bits are cleared.
2717 4 my $text_style = $_[0];
2718 4 flush();
2719 4 my $mask = font_mask();
2720 4 ▶ if ($text_style == Games::Rezrov::ZConst::STYLE_ROMAN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_style == 0) { } 2 2 4
2721 # turn off all
2722 2 $mask = 0;
2723 } else {
2724 2 $mask |= $text_style;
2725 }
2726 4 $mask = font_mask($mask);
2727 # might be modified for upper window
2728
2729 4 screen_zio()->set_text_style($mask);
2730 4 return $mask;
2731 }
2732  
2733 2 ▶sub toggle_text_style {
Subroutine toggle_text_style: called CC=1 SCAR=0.0
Pod undocumented
2734 # toggle the specified style bits on the font. Rather pointless for
2735 # STYLE_ROMAN. Little more than XOR.
2736 2 my $text_style = $_[0];
2737 2 flush();
2738 2 set_text_style( font_mask( font_mask() ^ $text_style ) );
2739 }
2740  
2741 # 'swap' in the specified style bits.
2742 # Returns the bits that were actually changed.
2743 #
2744 # The idea is to be able to do this:
2745 # $tmp = swap_text_style( STYLE_FIXED );
2746 # print_fixed_width_text();
2747 # toggle_text_style($tmp);
2748 #
2749 # and not worry about whether or not the relevant style bit was already set
2750 # (if it was, the bit will be 0 in the return and so the toggle won't undo it)
2751 2 ▶sub swap_text_style {
Subroutine swap_text_style: called CC=1 SCAR=0.0
Pod undocumented
2752 2 my $old = font_mask();
2753 2 return $old ^ set_text_style(@_);
2754 }
2755  
2756 0 ▶sub register_newline {
Subroutine register_newline: not called CC=6 SCAR=37.4
Pod undocumented
2757 # called by the ZIO whenever a newline is printed.
2758 0 ▶ return unless ($wrote_something and
Branch: 1 branch
BranchTrueFalseTotal
unless $wrote_something and defined $current_window and $lower_lines and $current_window == 0 0 0 0
Condition0%0/9
$wrote_something and defined $current_window and $lower_lines and $current_window == 0
!ll&&!rl&&r
000
$wrote_something and defined $current_window and $lower_lines
!ll&&!rl&&r
000
$wrote_something and defined $current_window
!ll&&!rl&&r
000
MC/DC0%0/4
$wrote_something and defined $current_window and $lower_lines and $current_window == 0
$wrote_something defined $current_window $lower_lines $current_window == 0
Truth table0%0/5
A and B and C and D
A= $wrote_somethingB= defined $current_windowC= $lower_linesD= $current_window == 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
2759 # don't count newlines that occur before any text;
2760 # example: start of "plundered hearts", after initial RETURN
2761 defined($current_window) and
2762 $lower_lines and
2763 $current_window == Games::Rezrov::ZConst::LOWER_WIN);
2764 0 my $wrote = $lines_wrote + 1;
2765  
2766 # printf STDERR "rn: %d/%d\n", $wrote, $lower_lines;
2767
2768 0 ▶ if ($wrote >= ($lower_lines - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrote >= $lower_lines - 1) 0 0 0
2769 # need to pause; show prompt.
2770 # print STDERR "pausing...\n";
2771 0 my $zio = screen_zio();
2772 0 my $restore = $zio->get_position(1);
2773
2774 0 set_cursor($lower_lines, 1);
2775 0 my $more_prompt = "[MORE]";
2776 # my $old = font_mask();
2777 # set_text_style(Games::Rezrov::ZConst::STYLE_REVERSE);
2778 0 my $old = swap_text_style(Games::Rezrov::ZConst::STYLE_REVERSE);
2779 0 $zio->write_string($more_prompt);
2780 # set_text_style(Games::Rezrov::ZConst::STYLE_ROMAN);
2781 # font_mask($old);
2782 0 toggle_text_style($old);
2783 0 $zio->update();
2784 0 $zio->get_input(1,1);
2785 0 set_cursor($lower_lines, 1);
2786 0 $zio->clear_to_eol();
2787  
2788 # $zio->erase_line($lower_lines);
2789 # $zio->erase_line($lower_lines - 1);
2790 0 $wrote = 0;
2791 0 &$restore();
2792 # restore old position
2793 }
2794 0 $lines_wrote = $wrote;
2795 }
2796  
2797 35 ▶sub flush {
Subroutine flush: called CC=24 SCAR=45.6
Pod undocumented
2798 # flush and format the characters buffered by the ZIO
2799 35 ▶ return if $flushing;
Branch: 1 branch
BranchTrueFalseTotal
if $flushing 6 29 35
2800 # can happen w/combinations of attributes and pausing
2801 # cluck "flush";
2802 29 my $len;
2803 29 my $zio = screen_zio();
2804 29 my $buffer = $zio->get_buffer();
2805 # printf STDERR "flush: buffer= ->%s<-\n", $buffer;
2806 29 $zio->reset_buffer();
2807 29 ▶ return unless length $buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless length $buffer 9 20 29
2808 # print "fs\n";
2809 20 $flushing = 1;
2810 20 $wrote_something = 1;
2811  
2812 20 my $speech_buffer = $buffer;
2813 # save unmodified version for speech interface
2814  
2815 20 ▶ if (Games::Rezrov::ZOptions::BEAUTIFY_LOCATIONS() and
Branch: 2 branches
BranchTrueFalseTotal
if (Games::Rezrov::ZOptions::BEAUTIFY_LOCATIONS() and $version < 4 and likely_location(\$buffer)) { } 2 18 20
elsif (length $buffer) { } 18 0 18
Condition50%3/6
Games::Rezrov::ZOptions::BEAUTIFY_LOCATIONS() and $version < 4 and likely_location(\$buffer)
!ll&&!rl&&r
0182
Games::Rezrov::ZOptions::BEAUTIFY_LOCATIONS() and $version < 4
!ll&&!rl&&r
0020
MC/DC33%1/3
Games::Rezrov::ZOptions::BEAUTIFY_LOCATIONS() and $version < 4 and likely_location(\$buffer)
Games::Rezrov::ZOptions::BEAUTIFY_LOCATIONS() $version < 4 likely_location(\$buffer)
Truth table50%2/4
A and B and C
A= Games::Rezrov::ZOptions::BEAUTIFY_LOCATIONS()B= $version < 4C= likely_location(\$buffer)
ABCresult
0XX0
10X0
1100
1111
2816 $version < 4 and
2817 likely_location(\$buffer)) {
2818 # set_text_style(Games::Rezrov::ZConst::STYLE_BOLD);
2819 2 my $old = swap_text_style(Games::Rezrov::ZConst::STYLE_BOLD);
2820 2 $zio->write_string($buffer);
2821 # FIX ME: this might wrap; eg Tk, "Zork III: The Dungeon Master"
2822 # set_text_style(Games::Rezrov::ZConst::STYLE_ROMAN);
2823 2 toggle_text_style($old);
2824 } elsif (length $buffer) {
2825 18 $wrote_something = 1;
2826 18 my ($i, $have_left);
2827 18 ▶ if ($current_window != Games::Rezrov::ZConst::LOWER_WIN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($current_window != 0) { } 0 18 18
elsif (not $zio->fixed_font_default) { } 0 18 18
2828 # buffering in upper window: nonstandard hack in effect.
2829 # assume we know what we're doing :)
2830 # print STDERR "hack! \"$buffer\"\n";
2831 0 $zio->write_string($buffer);
2832 } elsif (!$zio->fixed_font_default()) {
2833 #
2834 # Variable font; graphical wrapping
2835 #
2836 0 my ($x, $y) = $zio->get_pixel_position();
2837 0 my $total_width = ($zio->get_pixel_geometry())[0];
2838 0 my $pixels_left = $total_width - $x;
2839 0 my $plen;
2840 0 ▶ while ($len = length($buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len = length $buffer) 0 0 0
2841 0 $plen = $zio->string_width($buffer);
2842 0 ▶ if ($plen < $pixels_left) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plen < $pixels_left) { } 0 0 0
2843 # it'll fit; we're done
2844 # print STDERR "fits: $buffer\n";
2845 0 $zio->write_string($buffer);
2846 0 last;
2847 } else {
2848 0 my $wrapped = 0;
2849 0 my $i = int(length($buffer) * ($pixels_left / $plen));
2850 # print STDERR "pl=$pixels_left, plen=$plen i=$i\n";
2851 0 ▶ while (substr($buffer,$i,1) ne " ") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($buffer, $i, 1) ne " ") 0 0 0
2852 # move ahead to a word boundary
2853 # print STDERR "boundarizing\n";
2854 0 ▶ last if ++$i >= $len;
Branch: 1 branch
BranchTrueFalseTotal
if ++$i >= $len 0 0 0
2855 }
2856  
2857 0 while (1) {
2858 0 $plen = $zio->string_width(substr($buffer,0,$i));
2859 # printf STDERR "%s = %s\n", substr($buffer,0,$i), $plen;
2860 0 ▶ if ($plen < $pixels_left) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plen < $pixels_left) { } 0 0 0
2861 # it'll fit
2862 0 $zio->write_string(substr($buffer,0,$i));
2863 0 $zio->newline();
2864 0 $buffer = substr($buffer, $i + 1);
2865 0 $wrapped = 1;
2866 0 last;
2867 } else {
2868 # retreat back a word
2869 0 ▶ while (--$i >= 0 and substr($buffer,$i,1) ne " ") { }
Branch: 1 branch
BranchTrueFalseTotal
if (--$i >= 0 and substr($buffer, $i, 1) ne " ") 0 0 0
Condition0%0/3
--$i >= 0 and substr($buffer, $i, 1) ne " "
!ll&&!rl&&r
000
MC/DC0%0/2
--$i >= 0 and substr($buffer, $i, 1) ne " "
--$i >= 0 substr($buffer, $i, 1) ne " "
Truth table0%0/3
A and B
A= --$i >= 0B= substr($buffer, $i, 1) ne " "
ABresult
0X0
100
111
2870 0 ▶ last if ($i < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 0 0 0
2871 }
2872 }
2873  
2874 0 ▶ $zio->newline() unless ($wrapped);
Branch: 1 branch
BranchTrueFalseTotal
unless $wrapped 0 0 0
2875 # if couldn't wrap at all on this line
2876 0 $pixels_left = $total_width;
2877 }
2878 }
2879 } else {
2880 #
2881 # Fixed font; do line/column wrapping
2882 #
2883 18 my ($x, $y) = $zio->get_position();
2884 18 $have_left = ($columns - $x);
2885 # Get start column position; we can't be sure we're starting at
2886 # column 0. This is an issue when flush() is called when changing
2887 # attributes. Example: "bureaucracy" intro paragraphs ("But
2888 # Happitec is going to be _much_ more fun...")
2889 18 ▶ while ($len = length($buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len = length $buffer) 22 0 22
2890 22 ▶ if ($len < $have_left) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $have_left) { } 18 4 22
2891 18 $zio->write_string($buffer);
2892 18 last;
2893 } else {
2894 # printf STDERR "wrapping: %d, %d, %s x:$x y:$y col:$columns\n", length $buffer, $have_left, $buffer;
2895 4 my $wrapped = 0;
2896 4 ▶ for ($i = $have_left - 1; $i > 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 20 0 20
2897 20 ▶ if (substr($buffer, $i, 1) eq " ") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($buffer, $i, 1) eq " ") 4 16 20
2898 4 $zio->write_string(substr($buffer, 0, $i));
2899 4 $zio->newline();
2900 4 $wrapped = 1;
2901 4 $buffer = substr($buffer, $i + 1);
2902 4 last;
2903 }
2904 }
2905 4 ▶ $zio->newline() unless $wrapped;
Branch: 1 branch
BranchTrueFalseTotal
unless $wrapped 0 4 4
2906 # if couldn't wrap at all
2907 4 $have_left = $columns;
2908 }
2909 }
2910 }
2911 18 $prompt_buffer = $buffer;
2912 # FIX ME
2913 }
2914  
2915 20 ▶ $zio->speak($speech_buffer) if $zio->speaking;
Branch: 1 branch
BranchTrueFalseTotal
if $zio->speaking 0 20 20
2916
2917 20 $flushing = 0;
2918 }
2919
2920 289 ▶sub likely_location {
Subroutine likely_location: called CC=12 SCAR=27.2
Pod undocumented
2921 #
2922 # STATIC: is the given string likely the name of a location?
2923 #
2924 # An earlier approach saved the buffer position before and after
2925 # StoryFile::object_print() opcode, and considered a string a
2926 # location if and only if the buffer was flushed with only an object
2927 # string in the buffer. Unfortunately this doesn't always work:
2928 #
2929 # Suspect: "Ballroom, Near Fireplace", where "Near Fireplace"
2930 # is an object, but Ballroom is not.
2931 #
2932 # It's not enough to check for all capitalized words:
2933 # Zork 1: "West of House"
2934 #
2935 # This approach "uglier" but works more often :)
2936 289 my $ref = shift;
2937 289 my $len = length $$ref;
2938 289 ▶ if ($len and $len < 50) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len and $len < 50) { } 281 8 289
Condition66%2/3
$len and $len < 50
!ll&&!rl&&r
08281
MC/DC50%1/2
$len and $len < 50
$len $len < 50
Truth table66%2/3
A and B
A= $lenB= $len < 50
ABresult
0X0
100
111
2939 # length?
2940 281 my $buffer = $$ref;
2941  
2942 281 ▶ return 0 unless $buffer =~ /^[A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $buffer =~ /^[A-Z]/ 202 79 281
2943 # must start uppercased
2944  
2945 79 ▶ return 0 if $buffer =~ /\W$/;
Branch: 1 branch
BranchTrueFalseTotal
if $buffer =~ /\W$/ 4 75 79
2946 # can't end with a non-alphanum:
2947 # minizork.z3:
2948 # >i
2949 # You have: <---------
2950 # A leaflet
2951  
2952 75 ▶ return 0 if $buffer =~ /^\w - /;
Branch: 1 branch
BranchTrueFalseTotal
if $buffer =~ /^\w - / 0 75 75
2953 # sampler1_r55.z3:
2954 # T - The Tutorial
2955  
2956 75 ▶ unless ($buffer =~ /[a-z]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer =~ /[a-z]/) 0 75 75
2957 # if all uppercase...
2958 0 ▶ return 0 if $buffer =~ /[^\w ]/;
Branch: 1 branch
BranchTrueFalseTotal
if $buffer =~ /[^\w ]/ 0 0 0
2959 # ...be extra strict about non-alphanumeric characters
2960 #
2961 # allowed: ENCHANTER
2962 # HOLLYWOOD HIJINX
2963 # but not:
2964 # ROBOT, GO NORTH (sampler, Planetfall)
2965 }
2966  
2967 75 ▶ if ($buffer =~ /\s[a-z]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer =~ /\s[a-z]+$/) 0 75 75
2968 # Can't end with a lowercase word;
2969 # Enchanter: "Flathead portrait"
2970 0 return 0;
2971 }
2972  
2973 75 ▶ return 0 if $buffer =~ /\s[a-z]\S{2,}\s+[a-z]\S{2,}/;
Branch: 1 branch
BranchTrueFalseTotal
if $buffer =~ /\s[a-z]\S{2,}\s+[a-z]\S{2,}/ 0 75 75
2974 # don't allow more than one "significant" lowercase-starting
2975 # word in a row.
2976 #
2977 # example: graffiti in Planetfall's brig:
2978 #
2979 # There once was a krip, name of Blather <--
2980 # Who told a young ensign named Smather <-- this is not caught here!
2981 # "I'll make you inherit
2982 # A trotting demerit <--
2983 # And ship you off to those stinking fawg-infested tar-pools of Krather".
2984 #
2985 # However, we must allow:
2986 #
2987 # Land of the Dead [Zork I]
2988 # Room in a Puzzle [Zork III]
2989  
2990 75 ▶ if ($buffer =~ /\s([a-z]\S*\s+){3,}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer =~ /\s([a-z]\S*\s+){3,}/) 0 75 75
2991 # in any case, don't allow 3 lowercase-starting words in a row.
2992 # back to the brig example:
2993 #
2994 # Who told a young ensign named Smather <-- we get this here
2995 # ^^^^^^^^^^^^^^^^^^^^^^^^^
2996 0 return 0;
2997 }
2998 # ( blech... )
2999  
3000 75 ▶ return $buffer =~ /[^\w\s,:\'\-]/ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$buffer =~ /[^\w\s,:\'\-]/ ? : 1 74 75
3001 # - commas allowed: Cutthroats, "Your Room, on the bed"
3002 # - dashes allowed: Zork I, "North-South Passage"
3003 # - apostrophes allowed: Zork II, "Dragon's Lair"
3004 # - colons allowed (for game titles): "Zork III: ..."
3005 # - otherwise, everything except whitespace and alphanums verboten.
3006 } else {
3007 8 return 0;
3008 }
3009 }
3010  
3011 0 ▶sub tokenize {
Subroutine tokenize: not called CC=4 SCAR=30.0
Pod undocumented
3012 0 my ($text, $parse, $dict, $flag) = @_;
3013  
3014 0 my $std_dictionary_addr = $header->dictionary_address();
3015 0 my $zd;
3016 0 ▶ if ($dict and $dict != $std_dictionary_addr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dict and $dict != $std_dictionary_addr) { } 0 0 0
Condition0%0/3
$dict and $dict != $std_dictionary_addr
!ll&&!rl&&r
000
MC/DC0%0/2
$dict and $dict != $std_dictionary_addr
$dict $dict != $std_dictionary_addr
Truth table0%0/3
A and B
A= $dictB= $dict != $std_dictionary_addr
ABresult
0X0
100
111
3017 # v5+; example: "beyond zork"
3018 0 ▶ unless ($zd = $alternate_dictionaries{$dict}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($zd = $alternate_dictionaries{$dict}) 0 0 0
3019 0 $zd = $alternate_dictionaries{$dict} = new Games::Rezrov::ZDict($dict);
3020 }
3021 } else {
3022 # use default/standard dictionary
3023 0 $zd = $zdict;
3024 }
3025  
3026 0 $zd->tokenize_line($text,
3027 $parse,
3028 "-flag" => $flag,
3029 );
3030 # die join ",", @_;
3031 }
3032  
3033 226 ▶sub get_zobject {
Subroutine get_zobject: called CC=1 SCAR=0.0
Pod undocumented
3034 # cache object requests; games seem to run about 5-10% faster,
3035 # the most gain seen in earlier games
3036 226 return $object_cache->get($_[0]);
3037  
3038 # create every time; slow overhead
3039 # return new Games::Rezrov::ZObject($_[0]);
3040 }
3041  
3042 4 ▶sub get_zobject_cache {
Subroutine get_zobject_cache: called CC=1 SCAR=0.0
Pod undocumented
3043 # you don't see this
3044 4 return $object_cache;
3045 }
3046  
3047 2 ▶sub rows {
Subroutine rows: called CC=4 SCAR=14.0
Pod undocumented
3048 2 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 1 1 2
3049 1 $rows = $_[0];
3050 1 ▶ $header->set_rows($rows) if $header;
Branch: 1 branch
BranchTrueFalseTotal
if $header 0 1 1
3051 1 reset_write_count();
3052 1 ▶ $lower_lines = $rows - $upper_lines if defined $upper_lines;
Branch: 1 branch
BranchTrueFalseTotal
if defined $upper_lines 0 1 1
3053 }
3054 2 return $rows;
3055 }
3056  
3057 2 ▶sub columns {
Subroutine columns: called CC=5 SCAR=17.2
Pod undocumented
3058 2 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 1 1 2
3059 # ZIO notifies us of its columns
3060 1 $columns = $_[0];
3061 1 ▶ $header->set_columns($_[0]) if $header;
Branch: 1 branch
BranchTrueFalseTotal
if $header 0 1 1
3062 1 ▶ display_status_line() if $version <= 3 and $zstatus;
Branch: 1 branch
BranchTrueFalseTotal
if $version <= 3 and $zstatus 0 1 1
Condition33%1/3
$version <= 3 and $zstatus
!ll&&!rl&&r
010
MC/DC0%0/2
$version <= 3 and $zstatus
$version <= 3 $zstatus
Truth table33%1/3
A and B
A= $version <= 3B= $zstatus
ABresult
0X0
100
111
3063 }
3064 2 return $columns;
3065 }
3066  
3067 8 ▶sub reset_write_count {
Subroutine reset_write_count: called CC=1 SCAR=0.0
Pod undocumented
3068 8 $lines_wrote = 0;
3069 8 $wrote_something = 0;
3070 }
3071  
3072 4 ▶sub get_pc {
Subroutine get_pc: called CC=1 SCAR=0.0
Pod undocumented
3073 4 return $Games::Rezrov::StoryFile::PC;
3074 }
3075  
3076 2 ▶sub clear_screen {
Subroutine clear_screen: called CC=6 SCAR=32.2
Pod undocumented
3077 2 my $zio = screen_zio();
3078
3079 2 ▶ if ($zio->can_use_color()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zio->can_use_color) 0 2 2
3080 0 ▶ my $fg = $zio->fg() || "";
Condition0%0/2
$zio->fg || ''
l!l
00
MC/DC0%0/1
$zio->fg || ''
$zio->fg
Truth table0%0/2
A
A= $zio->fg
Aresult
11
00
3081 0 ▶ my $bg = $zio->bg() || "";
Condition0%0/2
$zio->bg || ''
l!l
00
MC/DC0%0/1
$zio->bg || ''
$zio->bg
Truth table0%0/2
A
A= $zio->bg
Aresult
11
00
3082 0 ▶ my $dbg = $zio->default_bg() || "";
Condition0%0/2
$zio->default_bg || ''
l!l
00
MC/DC0%0/1
$zio->default_bg || ''
$zio->default_bg
Truth table0%0/2
A
A= $zio->default_bg
Aresult
11
00
3083 # FIX ME!
3084
3085 # printf STDERR "fg=%s/%s bg=%s/%s\n",$fg,$zio->default_fg, $bg, $zio->default_bg;
3086 0 ▶ if ($bg ne $dbg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bg ne $dbg) 0 0 0
3087 # the background color has changed; change the cursor color
3088 # to the current foreground color so we don't run the risk of it
3089 # "disappearing".
3090 0 $zio->cc($fg);
3091 }
3092 0 $zio->default_bg($bg);
3093 0 $zio->default_fg($fg);
3094 0 $zio->set_background_color();
3095 }
3096  
3097 2 $zio->clear_screen();
3098 }
3099  
3100 12 ▶sub is_stream_selected {
Subroutine is_stream_selected: called CC=1 SCAR=0.0
Pod undocumented
3101 12 return $selected_streams->[$_[0]];
3102 }
3103  
3104 8 ▶sub stream_dup {
Subroutine stream_dup: called CC=2 SCAR=9.2
Pod undocumented
3105 8 my ($stream, $string) = @_;
3106 8 ▶ if (is_stream_selected($stream)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_stream_selected($stream)) 0 8 8
3107 0 my $fh = $zios->[$stream];
3108 0 print $fh $string . $/;
3109 }
3110 }
3111  
3112 2 ▶sub is_this_game {
Subroutine is_this_game: called CC=3 SCAR=14.2
Pod undocumented
3113 # do the given release number, serial number, and checksum
3114 # match those of this game?
3115 2 my ($release, $serial, $checksum) = @_;
3116 # printf "%s\n%s\n", join(",", @_), join ",", $header->release_number, $header->serial_code, $header->file_checksum;
3117 2 ▶ return ($header->release_number() eq $release and
Condition33%2/6
$header->release_number eq $release && $header->serial_code == $serial && $header->file_checksum == $checksum
!ll&&!rl&&r
200
$header->release_number eq $release and $header->serial_code == $serial
!ll&&!rl&&r
200
MC/DC0%0/3
$header->release_number eq $release && $header->serial_code == $serial && $header->file_checksum == $checksum
$header->release_number eq $release $header->serial_code == $serial $header->file_checksum == $checksum
Truth table25%1/4
A and B && C
A= $header->release_number eq $releaseB= $header->serial_code == $serialC= $header->file_checksum == $checksum
ABCresult
0XX0
10X0
1100
1111
3118 $header->serial_code() == $serial and
3119 $header->file_checksum() == $checksum);
3120 }
3121  
3122 3 ▶sub get_global_var {
Subroutine get_global_var: called CC=1 SCAR=0.0
Pod undocumented
3123 # get the specified global variable
3124 # printf STDERR "get gv %d: %d\n", $_[0], get_word_at($global_variable_address + ($_[0] * 2));
3125 3 return get_word_at($global_variable_address + ($_[0] * 2));
3126 }
3127  
3128 1115 ▶sub routine_pop {
Subroutine routine_pop: called CC=2 SCAR=7.6
Pod undocumented
3129 # pop a variable from the routine stack.
3130 1115 ▶ if ($#$current_frame < FRAME_ROUTINE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_frame->$#* < 18) { } 0 1115 1115
3131 0 die "yikes: attempt to pop when no routine stack!\n";
3132 } else {
3133 1115 return pop @{$current_frame};
3134 }
3135 }
3136  
3137 1145 ▶sub routine_push {
Subroutine routine_push: called CC=1 SCAR=0.0
Pod undocumented
3138 # push a variable onto the routine stack
3139 1145 push @{$current_frame}, $_[0];
3140 }
3141  
3142 146 ▶sub frame_argc {
Subroutine frame_argc: called CC=2 SCAR=7.2
Pod undocumented
3143 146 ▶ $current_frame->[FRAME_ARGC] = $_[0] if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 146 0 146
3144 146 return $current_frame->[FRAME_ARGC];
3145 }
3146  
3147 147 ▶sub frame_call_type {
Subroutine frame_call_type: called CC=2 SCAR=7.2
Pod undocumented
3148 147 ▶ $current_frame->[FRAME_CALL_TYPE] = $_[0] if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 147 0 147
3149 147 return $current_frame->[FRAME_CALL_TYPE];
3150 }
3151  
3152 147 ▶sub frame_return_pc {
Subroutine frame_return_pc: called CC=2 SCAR=7.2
Pod undocumented
3153 147 ▶ $current_frame->[FRAME_RPC] = $_[0] if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 147 0 147
3154 147 return $current_frame->[FRAME_RPC];
3155 }
3156  
3157 1258 ▶sub header {
Subroutine header: called CC=1 SCAR=0.0
Pod undocumented
3158 1258 return $header;
3159 }
3160  
3161 21 ▶sub version {
Subroutine version: called CC=1 SCAR=0.0
Pod undocumented
3162 21 return $version;
3163 }
3164  
3165 1 ▶sub game_title {
Subroutine game_title: called CC=1 SCAR=0.0
Pod undocumented
3166 1 return $game_title;
3167 }
3168  
3169 2 ▶sub ztext {
Subroutine ztext: called CC=1 SCAR=0.0
Pod undocumented
3170 2 return $ztext;
3171 }
3172  
3173 0 ▶sub prompt_buffer {
Subroutine prompt_buffer: not called CC=2 SCAR=17.9
Pod undocumented
3174 0 ▶ $prompt_buffer = $_[0] if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 0 0
3175 0 return $prompt_buffer;
3176 }
3177  
3178 4 ▶sub call_stack {
Subroutine call_stack: called CC=1 SCAR=0.0
Pod undocumented
3179 # used by Quetzal
3180 4 return $call_stack;
3181 }
3182  
3183 70 ▶sub player_object {
Subroutine player_object: called CC=2 SCAR=7.2
Pod undocumented
3184 70 ▶ $player_object = $_[0] if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 70 70
3185 70 return $player_object;
3186 }
3187  
3188 70 ▶sub current_room {
Subroutine current_room: called CC=2 SCAR=7.2
Pod undocumented
3189 70 ▶ $current_room = $_[0] if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 70 70
3190 70 return $current_room;
3191 }
3192  
3193 0 ▶sub push_command {
Subroutine push_command: not called CC=1 SCAR=6.9
Pod undocumented
3194 # steal a turn from the player
3195 0 $push_command = shift;
3196 }
3197  
3198 0 ▶sub last_input {
Subroutine last_input: not called CC=1 SCAR=6.9
Pod undocumented
3199 # for "oops" emulation
3200 0 return $last_input;
3201 }
3202  
3203 0 ▶sub full_version_output {
Subroutine full_version_output: not called CC=1 SCAR=6.9
Pod undocumented
3204 # used by "help"
3205 0 return $full_version_output;
3206 }
3207  
3208 0 ▶sub tail {
Subroutine tail: not called CC=1 SCAR=6.9
Pod undocumented
3209 0 $tailing = $_[0];
3210 }
3211  
3212 152 ▶sub call_func {
Subroutine call_func: called CC=1 SCAR=0.0
Pod undocumented
3213 # call, "function" style (store result)
3214 152 call(\@_, FRAME_FUNCTION);
3215 }
3216  
3217 0 ▶sub call_proc {
Subroutine call_proc: not called CC=1 SCAR=6.9
Pod undocumented
3218 # call, "procedure" style (discard result)
3219 0 call(\@_, FRAME_PROCEDURE);
3220 }
3221  
3222 0 ▶sub throw {
Subroutine throw: not called CC=1 SCAR=6.9
Pod undocumented
3223 0 unimplemented();
3224 }
3225  
3226 0 ▶sub erase_line {
Subroutine erase_line: not called CC=2 SCAR=17.9
Pod undocumented
3227 0 untested();
3228 0 ▶ if ($_[0] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] == 1) 0 0 0
3229 # if value not 1, do nothing
3230 0 screen_zio()->clear_to_eol();
3231 }
3232 }
3233  
3234 0 ▶sub get_cursor {
Subroutine get_cursor: not called CC=1 SCAR=6.9
Pod undocumented
3235 # put cursor coordinates at given offset
3236 0 untested();
3237 0 my ($x, $y) = screen_zio()->get_position();
3238 0 set_word_at($_[0], $y);
3239 0 set_word_at($_[1], $x);
3240 }
3241  
3242 0 ▶sub untested {
Subroutine untested: not called CC=1 SCAR=6.9
Pod undocumented
3243 0 (my $subname = (caller(1))[3]) =~ s/.*://;
3244 0 printf STDERR "Untested opcode %s(); please email me if you see this!\n", $subname;
3245 }
3246  
3247 0 ▶sub unimplemented {
Subroutine unimplemented: not called CC=1 SCAR=6.9
Pod undocumented
3248 0 (my $subname = (caller(1))[3]) =~ s/.*://;
3249 0 fatal_error(sprintf 'opcode %s() unimplemented! Please email me.', $subname);
3250 }
3251  
3252  
3253 0 ▶sub encode_text {
Subroutine encode_text: not called CC=1 SCAR=6.9
Pod undocumented
3254 # blech
3255 0 unimplemented();
3256 }
3257  
3258 0 ▶sub nop {
Subroutine nop: not called CC=1 SCAR=6.9
Pod undocumented
3259 0 untested();
3260 }
3261  
3262 0 ▶sub piracy {
Subroutine piracy: not called CC=1 SCAR=6.9
Pod undocumented
3263 # sect15.html#piracy
3264 0 conditional_jump(1);
3265 }
3266  
3267 0 ▶sub not_or_possibly_call {
Subroutine not_or_possibly_call: not called CC=2 SCAR=17.9
Pod undocumented
3268 # :)
3269 # sect15.html#not
3270 0 ▶ if ($version < 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version < 5) { } 0 0 0
3271 0 z_not(@_);
3272 } else {
3273 0 call_proc(@_);
3274 }
3275 }
3276  
3277 0 ▶sub font_3_disabled {
Subroutine font_3_disabled: not called CC=2 SCAR=17.9
Pod undocumented
3278 0 ▶ $font_3_disabled = $_[0] if defined($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 0 0
3279 0 return $font_3_disabled;
3280 }
3281  
3282 0 ▶sub get_zdict {
Subroutine get_zdict: not called CC=1 SCAR=6.9
Pod undocumented
3283 0 return $zdict;
3284 }
3285  
3286 1 1;
« ZIO_Curses.pm ↑ Summary ZIO_Tk.pm »