File Coverage

blib/lib/Games/Rezrov/ZIO_Generic.pm
Criterion Covered Total %
statement 40 74 54.0
branch 4 12 33.3
condition 0 6 0.0
subroutine 17 31 54.8
pod 0 24 0.0
total 61 147 41.5


line stmt bran cond sub pod time code
1             package Games::Rezrov::ZIO_Generic;
2             #
3             # shared/skeleton z-machine i/o, options, and speech
4             #
5             # FIX ME: provide abstract stub methods which die() w/message
6             # requiring implementation
7             #
8              
9 1     1   4 use strict;
  1         3  
  1         27  
10              
11 1     1   5 use Games::Rezrov::ZIO_Tools;
  1         2  
  1         59  
12 1     1   6 use Games::Rezrov::ZConst;
  1         2  
  1         15  
13 1     1   521 use Games::Rezrov::Speech;
  1         3  
  1         35  
14 1         5 use Games::Rezrov::MethodMaker qw(
15             current_window
16             zio_options
17             using_term_readline
18 1     1   6 );
  1         2  
19              
20             @Games::Rezrov::ZIO_Generic::ISA = qw(Games::Rezrov::Speech);
21             # additional ZIO methods
22              
23             my $buffer = "";
24              
25             sub new {
26 1     1 0 3 my ($type, %options) = @_;
27 1         4 my $self = {};
28 1         4 bless $self, $type;
29 1         55 $self->zio_options(\%options);
30 1 50       6 $self->init_speech_synthesis() if $options{"speak"};
31 1 50       6 $self->init_speech_recognition() if $options{"listen"};
32 1         5 return $self;
33             }
34              
35             sub can_split {
36             # true or false: can this zio split the screen?
37 0     0 0 0 return 1;
38             }
39              
40             sub groks_font_3 {
41             # true or false: can this zio handle graphical "font 3" z-characters?
42 1     1 0 4 return 0;
43             }
44              
45             sub fixed_font_default {
46             # true or false: does this zio use a fixed-width font?
47 19     19 0 103 return 1;
48             }
49              
50             sub can_change_title {
51             # true or false: can this zio change title?
52 0     0 0 0 return set_xterm_title();
53             }
54              
55             sub can_use_color {
56 2     2 0 27 return 0;
57             }
58              
59 1     1 0 32 sub split_window {}
60 7     7 0 45 sub set_text_style {}
61 0     0 0 0 sub clear_screen {}
62 0     0 0 0 sub color_change_notify {}
63              
64             sub set_game_title {
65 0     0 0 0 set_xterm_title($_[1]);
66             }
67              
68             sub manual_status_line {
69             # true or false: does this zio want to draw the status line itself?
70 0     0 0 0 return 0;
71             }
72              
73             sub get_buffer {
74             # get buffered text; fix me: return a ref?
75             # print STDERR "get_buf: $buffer\n";
76 29     29 0 377 return $buffer;
77             }
78              
79             sub reset_buffer {
80 29     29 0 76 $buffer = "";
81             }
82              
83             sub buffer_zchunk {
84             # receive a z-code string; newlines may be present.
85 35     35 0 65 my $nl = chr(Games::Rezrov::ZConst::Z_NEWLINE);
86 35         45 foreach (unpack "a" x length ${$_[1]}, ${$_[1]}) {
  35         108  
  35         361  
87             # this unpack() seems a little faster than a split().
88             # Any better way ???
89 812 100       1182 if ($_ eq $nl) {
90 6         29 Games::Rezrov::StoryFile::flush();
91 6         21 $_[0]->newline();
92             } else {
93 806         1165 $buffer .= $_;
94             }
95             }
96             }
97              
98             sub buffer_zchar {
99 12     12 0 55 $buffer .= chr($_[1]);
100             }
101              
102             sub set_font {
103             # print STDERR "set_font $_[1]\n";
104 0     0 0 0 return 0;
105             }
106              
107             sub play_sound_effect {
108 0     0 0 0 my ($self, $effect) = @_;
109             # flash();
110             }
111              
112             sub set_window {
113 4     4 0 206 $_[0]->current_window($_[1]);
114             }
115              
116 1     1 0 32 sub cleanup {
117             }
118              
119             sub DESTROY {
120             # in case of a crash, make sure we exit politely
121 0     0     $_[0]->cleanup();
122             }
123              
124             sub fatal_error {
125 0     0 0   my ($self, $msg) = @_;
126 0           $self->write_string("Fatal error: " . $msg);
127 0           $self->newline();
128 0           $self->get_input(1,1);
129 0           $self->cleanup();
130 0           exit 1;
131             }
132              
133             sub set_background_color {
134             # set the background to the current background color.
135             # That's the *whole* background, not just for the next characters
136             # to print (some games switch background colors before clearing
137             # the screen, which should reset the entire background to that
138             # color); eg "photopia.z5".
139             #
140             # "That's the *whole* bass..."
141 0     0 0   1;
142             }
143              
144             sub readline_init {
145             #
146             # try to initialize Term::Readline if desired and available
147             #
148             # FIX ME: rather than ->{readline}, ZOptions.pm?
149 0     0 0   my ($self) = @_;
150 0 0 0       if ($self->zio_options->{readline} and find_module('Term::ReadLine')) {
151 0           require Term::ReadLine;
152 0           my $tr = new Term::ReadLine "what?", \*main::STDIN, \*main::STDOUT;
153 0 0         unless (ref $tr eq "Term::ReadLine::Stub") {
154 0           $tr->ornaments(0);
155 0           $self->using_term_readline($tr);
156             # only set if available and active
157             }
158             }
159             }
160              
161             sub readline {
162             # read a line via Term::ReadLine
163             # readline insists on resetting the line so we need to give it
164             # everything up to the cursor position.
165 0     0 0   my ($self, $preloaded) = @_;
166             # FIX ME: preloaded input does NOT work with Term::ReadLine!
167              
168 0           my $line;
169             {
170 0     0     local $SIG{__WARN__} = sub {};
  0            
  0            
171             # disable warnings for readline call.
172             # Term::ReadLine::Perl spews undef messages when passed an
173             # undef prompt (e.g. when "Plundered Hearts" starts)
174 0           my $rl_ref = $_[0]->using_term_readline();
175 0           my $prompt = Games::Rezrov::StoryFile::prompt_buffer();
176              
177 0 0 0       if ($prompt and $rl_ref->ReadLine eq "Term::ReadLine::Gnu") {
178             # HACK:
179             # Term::ReadLine::Perl seems to erase line before prompt,
180             # but Term::ReadLine::Gnu doesn't. Since the prompt has already
181             # been displayed before ReadLine is called, when using Gnu
182             # version we need to erase it so we don't wind up with two.
183 0           $self->write_string(pack('c', Games::Rezrov::ZConst::ASCII_BS) x
184             length($prompt));
185             }
186            
187 0           $line = $rl_ref->readline($prompt);
188             # this doesn't work with v5+ preloaded input
189             }
190 0           return $line;
191             }
192              
193             1;