blib/lib/Game/Floppy/Terminal.pm

filter: Statementstmt 88.1% 379 / 430 Branchbran 79.2% 198 / 250 Conditioncond 75.0% 75 / 100 MC/DCmcdc 52.7% 29 / 55 Subroutinesub 96.0% 49 / 51 Podpod 94.4% 17 / 18 totaltotal 82.6% 747 / 904 CC 169 SCAR 25.7
CC
169
Cov
84%
run
37.3
board_lines
21.1
BUILD
20.3
CRAP 13.0
« blib/lib/Game/Floppy/Raylib.pm ↑ Summary blib/lib/Game/Floppy.pm »
1 package Game::Floppy::Terminal;
2  
3 6 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Game::Floppy;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Game::Floppy::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Game::Floppy::Ticker;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 our $VERSION = '0.09';
17  
18 6 my %GLYPH = (
19 wide => {
20 pipe => "\xE2\x96\x88",
21 top => "\xE2\x96\x80",
22 bottom => "\xE2\x96\x84",
23 bird => "\xE2\x97\x8F",
24 centre => "\xE2\x97\x89",
25 sky => ' ',
26 h => "\xE2\x94\x80", v => "\xE2\x94\x82",
27 tl => "\xE2\x95\xAD", tr => "\xE2\x95\xAE", bl => "\xE2\x95\xB0", br => "\xE2\x95\xAF",
28 },
29 ascii => {
30 pipe => '#',
31 top => '=',
32 bottom => '=',
33 bird => 'o',
34 centre => '@',
35 sky => ' ',
36 h => '-', v => '|',
37 tl => '+', tr => '+', bl => '+', br => '+',
38 },
39 );
40  
41 6 my %SGR = (
42 pipe => '32',
43 top => '32',
44 bottom => '32',
45 bird => '1;36',
46 centre => '1;36',
47 frame => '2',
48 status => '1',
49 paused => '1;33',
50 over => '1;31',
51 legend => '2',
52 );
53  
54 6 my %SEQUENCE = (
55 'A' => 'up',
56 'B' => 'down',
57 'C' => 'right',
58 'D' => 'left',
59 'H' => 'home',
60 'F' => 'end',
61 '1~' => 'home',
62 '4~' => 'end',
63 '5~' => 'page_up',
64 '6~' => 'page_down',
65 '7~' => 'home',
66 '8~' => 'end',
67 );
68  
69 6 my %CONTROL = (
70 "\r" => 'enter',
71 "\n" => 'enter',
72 "\t" => 'tab',
73 "\x7f" => 'backspace',
74 "\x08" => 'backspace',
75 "\x03" => 'interrupt',
76 "\x04" => 'eof',
77 );
78  
79 6 my %FLAP = map { $_ => 1 } ' ', 'up', 'enter', 'w', 'k';
80  
81 6 my $SEQUENCE_WAIT = 0.05;
82  
83 6 my ($MIN_ROWS, $MAX_ROWS) = (10, 25);
84  
85 has in => (
86 is => 'rw',
87 isa => Any,
88 7 ▶ default => sub { \*STDIN }
Subroutine __ANON__: called CC=1 SCAR=0.0
89 6 );
90  
91 has out => (
92 is => 'rw',
93 isa => Any,
94 7 ▶ default => sub { \*STDOUT }
Subroutine __ANON__: called CC=1 SCAR=0.0
95 6 );
96  
97 6 has cols => (is => 'ro');
98 6 has rows => (is => 'ro');
99  
100 has seed => (
101 is => 'ro',
102 isa => Str,
103 default => ''
104 6 );
105  
106 has level => (
107 is => 'ro',
108 isa => Int,
109 default => 3
110 6 );
111  
112 has watch => (
113 is => 'ro',
114 isa => Int,
115 default => 0
116 6 );
117  
118 has quiet => (
119 is => 'ro',
120 isa => Int,
121 default => 0
122 6 );
123  
124 has ascii => (
125 is => 'ro',
126 isa => Int,
127 default => 0
128 6 );
129  
130 has max_ticks => (
131 is => 'ro',
132 isa => Int,
133 default => 0
134 6 );
135  
136 has game => (
137 is => 'rw',
138 isa => Object
139 6 );
140  
141 has raw => (
142 is => 'rw',
143 isa => Int,
144 default => 0
145 6 );
146  
147 6 has keysource => (is => 'rw');
148  
149 has pending => (
150 is => 'rw',
151 isa => ArrayRef,
152 42 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
153 6 );
154  
155 has clock => (
156 is => 'ro',
157 isa => Any,
158 7 ▶ default => sub { \&Time::HiRes::time }
Subroutine __ANON__: called CC=1 SCAR=0.0
159 6 );
160  
161 has sleeper => (
162 is => 'ro',
163 isa => Any,
164 16 ▶ default => sub { sub { select undef, undef, undef, $_[0] } }
Subroutine __ANON__: called CC=1 SCAR=0.0
165 6 );
166  
167 has ticker => (
168 is => 'rw',
169 isa => Object
170 6 );
171  
172 6 has columns => (is => 'rw');
173 6 has lines => (is => 'rw');
174  
175 has frames => (
176 is => 'rw',
177 isa => Int,
178 default => 0
179 6 );
180  
181 has refused => (
182 is => 'rw',
183 isa => Int,
184 default => 0
185 6 );
186  
187 has games => (
188 is => 'rw',
189 isa => Int,
190 default => 0
191 6 );
192  
193 has best => (
194 is => 'rw',
195 isa => Int,
196 default => 0
197 6 );
198  
199 has stopped => (
200 is => 'rw',
201 isa => Str,
202 default => ''
203 6 );
204  
205 has refusal => (
206 is => 'rw',
207 isa => Str,
208 default => ''
209 6 );
210  
211 has _wants_ansi => (
212 is => 'ro',
213 init_arg => 'ansi',
214 private => 1
215 6 );
216  
217 has _wants_interactive => (
218 is => 'ro',
219 init_arg => 'interactive',
220 private => 1
221 6 );
222  
223 has _hid_cursor => (
224 is => 'rw',
225 isa => Int,
226 default => 0,
227 private => 1
228 6 );
229  
230 6 has _view_cols => (is => 'rw', private => 1);
231 6 has _view_rows => (is => 'rw', private => 1);
232  
233 6 ▶my $clamp = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
234 6 my ($value, $low, $high) = @_;
235 6 ▶ return $low if $value < $low;
Branch: 1 branch
BranchTrueFalseTotal
if $value < $low 2 4 6
236 4 ▶ return $high if $value > $high;
Branch: 1 branch
BranchTrueFalseTotal
if $value > $high 1 3 4
237 3 return $value;
238 6 };
239  
240 11 ▶my $cols_for = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
241 11 my ($game, $rows) = @_;
242 11 return int($rows * $game->width / $game->height * 2 + 0.5);
243 6 };
244  
245 42 ▶sub BUILD {
Subroutine BUILD: called CC=20 SCAR=30.1
Pod undocumented
246 42 my ($self) = @_;
247  
248 42 Game::Floppy::Bot->describe($self->level);
249  
250 40 for my $name (qw/ cols rows columns lines max_ticks /) {
251 196 my $value = $self->$name;
252 196 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 85 111 196
253 111 ▶ Carp::croak("Game::Floppy::Terminal: $name must be a whole number, not '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A[0-9]+\z/u 1 110 111
254 unless $value =~ /\A[0-9]+\z/;
255 }
256  
257 39 ▶ $self->stopped('') unless defined $self->stopped;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->stopped 39 0 39
258 39 ▶ $self->refusal('') unless defined $self->refusal;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->refusal 39 0 39
259  
260 39 ▶ Carp::croak('Game::Floppy::Terminal: the clock is a code reference')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->clock eq "CODE" 1 38 39
261 unless ref $self->clock eq 'CODE';
262 38 ▶ Carp::croak('Game::Floppy::Terminal: the sleeper is a code reference')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->sleeper eq "CODE" 0 38 38
263 unless ref $self->sleeper eq 'CODE';
264  
265 38 ▶ unless ($self->game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->game) 38 0 38
266 38 my $seed = $self->seed;
267 38 ▶ $seed = Digest::SHA::sha256_hex(join ':', $self->clock->(), $$)
Branch: 1 branch
BranchTrueFalseTotal
unless length $seed 3 35 38
268 unless CORE::length $seed;
269 38 $self->game(Game::Floppy->new(seed => $seed));
270 }
271  
272 38 my ($w, $h) = $self->_measure;
273 38 ▶ my $rows = defined $self->rows ? $self->rows
Branch: 2 branches
BranchTrueFalseTotal
defined $self->rows ? : 30 8 38
$h ? : 4 4 8
274 : $h ? $clamp->($h - 5, $MIN_ROWS, $MAX_ROWS) : 20;
275 38 ▶ my $cols = defined $self->cols ? $self->cols : $cols_for->($self->game, $rows);
Branch: 1 branch
BranchTrueFalseTotal
defined $self->cols ? : 29 9 38
276 38 ▶ if (!defined $self->cols && !defined $self->rows && $w && $cols + 2 > $w) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->cols || defined $self->rows and $w and $cols + 2 > $w) 2 36 38
Condition100%9/9
not defined $self->cols || defined $self->rows and $w and $cols + 2 > $w
!ll&&!rl&&r
3422
not defined $self->cols || defined $self->rows and $w
!ll&&!rl&&r
3044
defined $self->cols || defined $self->rows
l!l&&r!l&&!r
2918
MC/DC100%4/4
not defined $self->cols || defined $self->rows and $w and $cols + 2 > $w
defined $self->cols defined $self->rows $w $cols + 2 > $w
Truth table100%5/5
not(A || B) and C and D
A= defined $self->colsB= defined $self->rowsC= $wD= $cols + 2 > $w
ABCDresult
1XXX0
01XX0
000X0
00100
00111
277 2 $rows = $clamp->(int(($w - 2) / ($self->game->width / $self->game->height * 2)), $MIN_ROWS, $MAX_ROWS);
278 2 $cols = $cols_for->($self->game, $rows);
279 }
280 38 ▶ Carp::croak('Game::Floppy::Terminal: a view is at least 10 by 10')
Branch: 1 branch
BranchTrueFalseTotal
if $cols < 10 or $rows < 10 2 36 38
Condition66%2/3
$cols < 10 or $rows < 10
l!l&&r!l&&!r
0236
MC/DC50%1/2
$cols < 10 or $rows < 10
$cols < 10 $rows < 10
Truth table66%2/3
A or B
A= $cols < 10B= $rows < 10
ABresult
1X1
011
000
281 if $cols < 10 || $rows < 10;
282 36 $self->_view_cols($cols);
283 36 $self->_view_rows($rows);
284  
285 36 ▶ $self->ticker(Game::Floppy::Ticker->new(clock => $self->clock)) unless $self->ticker;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ticker 36 0 36
286  
287 36 return;
288 }
289  
290 3696 ▶sub view { my ($self) = @_; return ($self->_view_cols, $self->_view_rows) }
Subroutine view: called CC=1 SCAR=0.0
Pod documented
291  
292 1166100 ▶sub ansi {
Subroutine ansi: called CC=6 SCAR=19.9
Pod documented
293 1166100 my ($self) = @_;
294 1166100 ▶ return 0 if defined $ENV{NO_COLOR} && CORE::length $ENV{NO_COLOR};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"} 266 1165834 1166100
Condition66%2/3
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
!ll&&!rl&&r
11658340266
MC/DC50%1/2
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
defined $ENV{"NO_COLOR"} length $ENV{"NO_COLOR"}
Truth table66%2/3
A and B
A= defined $ENV{"NO_COLOR"}B= length $ENV{"NO_COLOR"}
ABresult
0X0
100
111
295 1165834 ▶ return $self->_wants_ansi ? 1 : 0 if defined $self->_wants_ansi;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_wants_ansi 1165834 0 1165834
$self->_wants_ansi ? : 1061 1164773 1165834
296 0 ▶ return -t $self->out ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-t $self->out ? : 0 0 0
297 }
298  
299 3732 ▶sub interactive {
Subroutine interactive: called CC=4 SCAR=14.3
Pod documented
300 3732 my ($self) = @_;
301 3732 ▶ return $self->_wants_interactive ? 1 : 0 if defined $self->_wants_interactive;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_wants_interactive 3714 18 3732
$self->_wants_interactive ? : 0 3714 3714
302 18 ▶ return -t $self->out ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-t $self->out ? : 0 18 18
303 }
304  
305 11 ▶sub keys_available {
Subroutine keys_available: called CC=5 SCAR=23.4
Pod documented
306 11 my ($self) = @_;
307 11 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 0 11 11
308 11 ▶ return 0 unless defined $self->in && -t $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->in and -t $self->in 11 0 11
Condition33%1/3
defined $self->in and -t $self->in
!ll&&!rl&&r
0110
MC/DC0%0/2
defined $self->in and -t $self->in
defined $self->in -t $self->in
Truth table33%1/3
A and B
A= defined $self->inB= -t $self->in
ABresult
0X0
100
111
309 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
310 }
311  
312 29 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=20.4
Pod documented
313 29 my ($self) = @_;
314 29 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 29 29
315 29 ▶ if ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 20 9 29
316 20 $self->raw(1);
317 20 return $self;
318 }
319 9 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 9 0 9
320 0 ▶ return undef unless eval { Term::ReadKey::ReadMode(3, $self->in); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { Term::ReadKey::ReadMode(3, $self->in); 1 } } 0 0 0
321 0 $self->raw(1);
322 0 return $self;
323 }
324  
325 31 ▶sub leave_raw {
Subroutine leave_raw: called CC=4 SCAR=15.2
Pod documented
326 31 my ($self) = @_;
327 31 ▶ if ($self->_hid_cursor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_hid_cursor) 0 31 31
328 0 $self->_print("\e[?25h");
329 0 $self->_hid_cursor(0);
330 }
331 31 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 11 20 31
332 20 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 20 20
333 20 $self->raw(0);
334 20 return $self;
335 }
336  
337 4 ▶sub cannot_play {
Subroutine cannot_play: called CC=1 SCAR=0.0
Pod documented
338 4 return 'A real-time game needs a terminal and Term::ReadKey to read the keys.'
339 . ' Install Term::ReadKey, or run floppy --watch to watch the bot play.';
340 }
341  
342 70 ▶sub _measure {
Subroutine _measure: called CC=6 SCAR=26.2
343 70 my ($self) = @_;
344 70 ▶ return ($self->columns, $self->lines) if defined $self->columns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->columns 8 62 70
345 62 ▶ return () if $self->keysource || !$self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource or not $self->interactive 62 0 62
Condition66%2/3
$self->keysource or not $self->interactive
l!l&&r!l&&!r
43190
MC/DC0%0/2
$self->keysource or not $self->interactive
$self->keysource not $self->interactive
Truth table66%2/3
A or not(B)
A= $self->keysourceB= not $self->interactive
ABresult
1X1
011
000
346 0 ▶ return () unless eval { require Term::ReadKey; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Term::ReadKey; 1 } } 0 0 0
347 0 my ($w, $h) = eval { Term::ReadKey::GetTerminalSize($self->out) };
348 0 ▶ return $w ? ($w, $h) : ();
Branch: 1 branch
BranchTrueFalseTotal
$w ? : 0 0 0
349 }
350  
351 32 ▶sub too_narrow {
Subroutine too_narrow: called CC=6 SCAR=18.0
Pod documented
352 32 my ($self) = @_;
353 32 my ($w, $h) = $self->_measure;
354 32 ▶ return undef unless $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $w 29 3 32
355 3 my ($cols, $rows) = $self->view;
356 3 my ($need_w, $need_h) = ($cols + 2, $rows + 5);
357 3 ▶ return undef if $w >= $need_w && (!$h || $h >= $need_h);
Branch: 1 branch
BranchTrueFalseTotal
if $w >= $need_w and !$h || $h >= $need_h 1 2 3
Condition83%5/6
$w >= $need_w and !$h || $h >= $need_h
!ll&&!rl&&r
111
not $h or $h >= $need_h
l!l&&r!l&&!r
011
MC/DC66%2/3
$w >= $need_w and !$h || $h >= $need_h
$w >= $need_w not $h $h >= $need_h
Truth table75%3/4
A and not(B) or C
A= $w >= $need_wB= not $hC= $h >= $need_h
ABCresult
0XX0
1000
11X1
1011
358 2 ▶ return sprintf 'This window is %d columns by %d lines, and a view %d by %d needs %d by %d.'
Condition50%1/2
$h || 0
l!l
20
MC/DC0%0/1
$h || 0
$h
Truth table50%1/2
A
A= $h
Aresult
11
00
359 . ' Widen it, or ask for a smaller view with --cols and --rows.',
360 $w, ($h || 0), $cols, $rows, $need_w, $need_h;
361 }
362  
363 3773 ▶sub _read_char {
Subroutine _read_char: called CC=7 SCAR=22.7
364 3773 my ($self, $wait) = @_;
365  
366 3773 my $pending = $self->pending;
367 3773 ▶ return shift @$pending if @$pending;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending 2 3771 3773
368 3771 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 741 3030 3771
369  
370 3030 ▶ unless ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->raw) 3030 0 3030
371 3030 ▶ $self->sleeper->($wait) if $wait;
Branch: 1 branch
BranchTrueFalseTotal
if $wait 3030 0 3030
372 3030 return undef;
373 }
374  
375 0 ▶ my $mode = !defined $wait ? 0 : $wait > 0 ? $wait : -1;
Branch: 2 branches
BranchTrueFalseTotal
!defined($wait) ? : 0 0 0
$wait > 0 ? : 0 0 0
376 0 return Term::ReadKey::ReadKey($mode, $self->in);
377 }
378  
379 3755 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
380 3755 my ($self, $wait) = @_;
381 3755 my $char = $self->_read_char($wait);
382 3755 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 3699 56 3755
383 56 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $CONTROL{$char} 5 51 56
384 51 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 9 42 51
385 42 return $char;
386 }
387  
388 9 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=21.0
Pod documented
389 9 my ($self) = @_;
390  
391 9 my $opener = $self->_read_char($SEQUENCE_WAIT);
392 9 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 9 9
393  
394 9 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 2 7 9
Condition100%3/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
612
MC/DC100%2/2
$opener eq "[" or $opener eq "O"
$opener eq "[" $opener eq "O"
Truth table100%3/3
A or B
A= $opener eq "["B= $opener eq "O"
ABresult
1X1
011
000
395 2 unshift @{ $self->pending }, $opener;
396 2 return 'escape';
397 }
398  
399 7 my $tail = '';
400 7 ▶ while (CORE::length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 9 0 9
401 9 my $char = $self->_read_char($SEQUENCE_WAIT);
402 9 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 9 9
403 9 $tail .= $char;
404 9 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/u 7 2 9
405 }
406  
407 7 ▶ return $SEQUENCE{$tail} || 'escape';
Condition50%1/2
$SEQUENCE{$tail} || 'escape'
l!l
70
MC/DC0%0/1
$SEQUENCE{$tail} || 'escape'
$SEQUENCE{$tail}
Truth table50%1/2
A
A= $SEQUENCE{$tail}
Aresult
11
00
408 }
409  
410 3688 ▶sub _glyphs { return $GLYPH{ $_[0]->ascii ? 'ascii' : 'wide' } }
Subroutine _glyphs: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->ascii ? : 3687 1 3688
411  
412 1166100 ▶sub _paint {
Subroutine _paint: called CC=4 SCAR=13.9
413 1166100 my ($self, $text, $key) = @_;
414 1166100 ▶ return $text unless $self->ansi;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ansi 1165039 1061 1166100
415 1061 ▶ return $text unless defined $key && defined $SGR{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and defined $SGR{$key} 1032 29 1061
Condition66%2/3
defined $key and defined $SGR{$key}
!ll&&!rl&&r
1032029
MC/DC50%1/2
defined $key and defined $SGR{$key}
defined $key defined $SGR{$key}
Truth table66%2/3
A and B
A= defined $keyB= defined $SGR{$key}
ABresult
0X0
100
111
416 29 return "\e[" . $SGR{$key} . 'm' . $text . "\e[0m";
417 }
418  
419 3713 ▶sub _say {
Subroutine _say: called CC=2 SCAR=6.9
420 3713 my ($self, @lines) = @_;
421 3713 my $out = $self->out;
422 3713 print {$out} "$_\n" for @lines;
423 3713 return;
424 }
425  
426 0 ▶sub _print {
Subroutine _print: not called CC=1 SCAR=6.9
427 0 my ($self, $text) = @_;
428 0 my $out = $self->out;
429 0 print {$out} $text;
430 0 my $was = select $out;
431 0 $| = 1;
432 0 select $was;
433 0 return;
434 }
435  
436 3687 ▶sub status_line {
Subroutine status_line: called CC=5 SCAR=16.1
Pod documented
437 3687 my ($self, $note) = @_;
438 3687 my $game = $self->game;
439 3687 my $line = sprintf ' Floppy score %d best %d', $game->score, $self->best;
440 3687 $line = $self->_paint($line, 'status');
441 3687 ▶ if (defined $note && CORE::length $note) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $note and length $note) 22 3665 3687
Condition66%2/3
defined $note and length $note
!ll&&!rl&&r
3665022
MC/DC50%1/2
defined $note and length $note
defined $note length $note
Truth table66%2/3
A and B
A= defined $noteB= length $note
ABresult
0X0
100
111
442 22 ▶ my $key = $note eq 'paused' ? 'paused' : $note =~ /\AGame over/ ? 'over' : 'legend';
Branch: 2 branches
BranchTrueFalseTotal
$note eq 'paused' ? : 3 19 22
$note =~ /\AGame over/u ? : 4 15 19
443 22 $line .= ' ' . $self->_paint($note, $key);
444 }
445 3687 return $line;
446 }
447  
448 3687 ▶sub legend {
Subroutine legend: called CC=4 SCAR=13.9
Pod documented
449 3687 my ($self, $mode) = @_;
450 3687 ▶ $mode = '' unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 3683 4 3687
451 3687 ▶ my $text = $self->watch
Branch: 2 branches
BranchTrueFalseTotal
$self->watch ? : 3487 200 3687
$mode eq 'again' ? : 2 198 200
452 ? ' watching level ' . $self->level . ': q quits, p pauses'
453 : $mode eq 'again' ? ' space plays again; q quits'
454 : ' space, up, enter, w or k flap; p pauses; q quits';
455 3687 return $self->_paint($text, 'legend');
456 }
457  
458 3688 ▶sub board_lines {
Subroutine board_lines: called CC=21 SCAR=30.5
Pod documented
459 3688 my ($self) = @_;
460 3688 my $game = $self->game;
461 3688 my $g = $self->_glyphs;
462 3688 my ($cols, $rows) = $self->view;
463 3688 my ($width, $height) = ($game->width, $game->height);
464 3688 my $ppc = $width / $cols;
465 3688 my $ppr = $height / $rows;
466  
467 3688 my @cell = map { [ ('sky') x $cols ] } 1 .. $rows;
468  
469 3688 for my $tube (@{ $game->tubes }) {
470 4941 my $from = int($tube->{x} / $ppc);
471 4941 my $to = int(($tube->{x} + $game->tube_width - 1) / $ppc);
472 4941 for my $c ($from .. $to) {
473 18142 ▶ next if $c < 0 || $c >= $cols;
Branch: 1 branch
BranchTrueFalseTotal
if $c < 0 or $c >= $cols 1135 17007 18142
Condition66%2/3
$c < 0 or $c >= $cols
l!l&&r!l&&!r
0113517007
MC/DC50%1/2
$c < 0 or $c >= $cols
$c < 0 $c >= $cols
Truth table66%2/3
A or B
A= $c < 0B= $c >= $cols
ABresult
1X1
011
000
474 17007 for my $r (0 .. $rows - 1) {
475 180260 my ($y0, $y1) = ($r * $ppr, ($r + 1) * $ppr);
476 180260 ▶ if ($y1 <= $tube->{gap_top}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($y1 <= $tube->{'gap_top'}) { } 60449 119811 180260
elsif ($y0 < $tube->{'gap_top'} and $tube->{'gap_top'} - $y0 >= $ppr / 2) { } 6972 112839 119811
elsif ($y0 >= $tube->{'gap_bottom'}) { } 43577 69262 112839
elsif ($y1 > $tube->{'gap_bottom'} and $y1 - $tube->{'gap_bottom'} >= $ppr / 2) { } 6359 117184 123543
477 60449 $cell[$r][$c] = 'pipe';
478 }
479 ▶ elsif ($y0 < $tube->{gap_top} && $tube->{gap_top} - $y0 >= $ppr / 2) {
Condition100%3/3
$y0 < $tube->{"gap_top"} and $tube->{"gap_top"} - $y0 >= $ppr / 2
!ll&&!rl&&r
10390489356972
MC/DC100%2/2
$y0 < $tube->{"gap_top"} and $tube->{"gap_top"} - $y0 >= $ppr / 2
$y0 < $tube->{"gap_top"} $tube->{"gap_top"} - $y0 >= $ppr / 2
Truth table100%3/3
A and B
A= $y0 < $tube->{"gap_top"}B= $tube->{"gap_top"} - $y0 >= $ppr / 2
ABresult
0X0
100
111
480 6972 $cell[$r][$c] = 'top';
481 }
482 elsif ($y0 >= $tube->{gap_bottom}) {
483 43577 $cell[$r][$c] = 'pipe';
484 }
485 ▶ elsif ($y1 > $tube->{gap_bottom} && $y1 - $tube->{gap_bottom} >= $ppr / 2) {
Condition100%3/3
$y1 > $tube->{"gap_bottom"} and $y1 - $tube->{"gap_bottom"} >= $ppr / 2
!ll&&!rl&&r
5428186226359
MC/DC100%2/2
$y1 > $tube->{"gap_bottom"} and $y1 - $tube->{"gap_bottom"} >= $ppr / 2
$y1 > $tube->{"gap_bottom"} $y1 - $tube->{"gap_bottom"} >= $ppr / 2
Truth table100%3/3
A and B
A= $y1 > $tube->{"gap_bottom"}B= $y1 - $tube->{"gap_bottom"} >= $ppr / 2
ABresult
0X0
100
111
486 6359 $cell[$r][$c] = 'bottom';
487 }
488 }
489 }
490 }
491  
492 3688 my $bird = $game->bird;
493 3688 my $r2 = $bird->{radius} ** 2;
494 3688 for my $r (0 .. $rows - 1) {
495 39300 for my $c (0 .. $cols - 1) {
496 1147640 my $dx = ($c + 0.5) * $ppc - $bird->{x};
497 1147640 my $dy = ($r + 0.5) * $ppr - $bird->{y};
498 1147640 ▶ $cell[$r][$c] = 'bird' if $dx * $dx + $dy * $dy <= $r2;
Branch: 1 branch
BranchTrueFalseTotal
if $dx * $dx + $dy * $dy <= $r2 9554 1138086 1147640
499 }
500 }
501 3688 my ($bc, $br) = (int($bird->{x} / $ppc), int($bird->{y} / $ppr));
502 3688 ▶ $cell[$br][$bc] = 'centre' if $br >= 0 && $br < $rows && $bc >= 0 && $bc < $cols;
Branch: 1 branch
BranchTrueFalseTotal
if $br >= 0 and $br < $rows and $bc >= 0 and $bc < $cols 3686 2 3688
Condition66%6/9
$br >= 0 and $br < $rows and $bc >= 0 and $bc < $cols
!ll&&!rl&&r
203686
$br >= 0 and $br < $rows and $bc >= 0
!ll&&!rl&&r
203686
$br >= 0 and $br < $rows
!ll&&!rl&&r
023686
MC/DC25%1/4
$br >= 0 and $br < $rows and $bc >= 0 and $bc < $cols
$br >= 0 $br < $rows $bc >= 0 $bc < $cols
Truth table40%2/5
A and B and C and D
A= $br >= 0B= $br < $rowsC= $bc >= 0D= $bc < $cols
ABCDresult
0XXX0
10XX0
110X0
11100
11111
503  
504 3688 my $v = $self->_paint($g->{v}, 'frame');
505 3688 my @lines = ($self->_paint($g->{tl} . ($g->{h} x $cols) . $g->{tr}, 'frame'));
506 3688 for my $r (0 .. $rows - 1) {
507 39300 ▶ push @lines, $v . join('', map { my $k = $cell[$r][$_]; $self->_paint($g->{$k}, $k eq 'sky' ? undef : $k) } 0 .. $cols - 1) . $v;
Branch: 1 branch
BranchTrueFalseTotal
$k eq 'sky' ? : 1020729 126911 1147640
508 }
509 3688 push @lines, $self->_paint($g->{bl} . ($g->{h} x $cols) . $g->{br}, 'frame');
510 3688 return @lines;
511 }
512  
513 3687 ▶sub frame_lines {
Subroutine frame_lines: called CC=1 SCAR=0.0
Pod documented
514 3687 my ($self, $note, $mode) = @_;
515 3687 return ($self->status_line($note), $self->board_lines, $self->legend($mode));
516 }
517  
518 3725 ▶sub _frame {
Subroutine _frame: called CC=3 SCAR=11.3
519 3725 my ($self, $note, $mode) = @_;
520 3725 ▶ return if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 38 3687 3725
521 3687 $self->frames($self->frames + 1);
522 3687 my @lines = $self->frame_lines($note, $mode);
523 3687 ▶ if ($self->interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interactive) { } 0 3687 3687
524 0 $self->_print("\e[H" . join('', map { "$_\e[K\n" } @lines) . "\e[J");
525 }
526 else {
527 3687 $self->_say(@lines, '');
528 }
529 3687 return;
530 }
531  
532 26 ▶sub _intro {
Subroutine _intro: called CC=3 SCAR=16.0
533 26 my ($self) = @_;
534 26 ▶ return unless $self->interactive && !$self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive and not $self->quiet 26 0 26
Condition33%1/3
$self->interactive and not $self->quiet
!ll&&!rl&&r
2600
MC/DC0%0/2
$self->interactive and not $self->quiet
$self->interactive not $self->quiet
Truth table33%1/3
A and not(B)
A= $self->interactiveB= not $self->quiet
ABresult
0X0
100
111
535 0 $self->_print("\e[2J\e[?25l");
536 0 $self->_hid_cursor(1);
537 0 return;
538 }
539  
540 3511 ▶sub _bot_turn {
Subroutine _bot_turn: called CC=2 SCAR=6.9
541 3511 my ($self) = @_;
542 3511 my $game = $self->game;
543 3511 ▶ $game->flap if Game::Floppy::Bot->choose($game, level => $self->level);
Branch: 1 branch
BranchTrueFalseTotal
if "Game::Floppy::Bot"->choose($game, "level", $self->level) 342 3169 3511
544 3511 return;
545 }
546  
547 39 ▶sub _on_key {
Subroutine _on_key: called CC=9 SCAR=22.0
548 39 my ($self, $key) = @_;
549 39 ▶ return 0 if !defined $key || $key eq 'q' || $key eq 'eof' || $key eq 'interrupt';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt" 16 23 39
Condition77%7/9
not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
16023
not defined $key or $key eq "q" or $key eq "eof"
l!l&&r!l&&!r
16023
not defined $key or $key eq "q"
l!l&&r!l&&!r
11523
MC/DC50%2/4
not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "q" $key eq "eof" $key eq "interrupt"
Truth table60%3/5
not(A) or B or C or D
A= not defined $keyB= $key eq "q"C= $key eq "eof"D= $key eq "interrupt"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
550 23 ▶ if ($FLAP{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FLAP{$key}) 17 6 23
551 17 ▶ return 1 if $self->watch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->watch 2 15 17
552 15 ▶ $self->refused($self->refused + 1) unless $self->game->flap;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game->flap 1 14 15
553 15 return 1;
554 }
555 6 ▶ return $self->_pause if $key eq 'p';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "p" 3 3 6
556 3 return 1;
557 }
558  
559 3 ▶sub _pause {
Subroutine _pause: called CC=7 SCAR=19.7
560 3 my ($self) = @_;
561 3 $self->_frame('paused');
562 3 while (1) {
563 3 my $key = $self->read_key(undef);
564 3 ▶ return 0 if !defined $key || $key eq 'q' || $key eq 'eof' || $key eq 'interrupt';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt" 1 2 3
Condition66%6/9
not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
102
not defined $key or $key eq "q" or $key eq "eof"
l!l&&r!l&&!r
102
not defined $key or $key eq "q"
l!l&&r!l&&!r
102
MC/DC25%1/4
not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "q" $key eq "eof" $key eq "interrupt"
Truth table40%2/5
not(A) or B or C or D
A= not defined $keyB= $key eq "q"C= $key eq "eof"D= $key eq "interrupt"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
565 2 ▶ last if $key eq 'p';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "p" 1 1 2
566 1 ▶ if ($FLAP{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FLAP{$key}) 1 0 1
567 1 $self->_on_key($key);
568 1 last;
569 }
570 }
571 2 $self->ticker->start(1 / 20);
572 2 return 1;
573 }
574  
575 6 my $PERIOD = 1 / 20;
576  
577 27 ▶my $one_game = sub {
Subroutine __ANON__: called CC=15 SCAR=27.1
578 27 my ($self, $queued) = @_;
579 27 my $game = $self->game;
580 27 my $ticker = $self->ticker;
581  
582 27 $self->games($self->games + 1);
583 27 ▶ $self->_frame($self->watch || $queued ? undef : 'space to start');
Branch: 1 branch
BranchTrueFalseTotal
$self->watch || $queued ? : 11 16 27
Condition100%3/3
$self->watch or $queued
l!l&&r!l&&!r
10116
MC/DC100%2/2
$self->watch or $queued
$self->watch $queued
Truth table100%3/3
A or B
A= $self->watchB= $queued
ABresult
1X1
011
000
584  
585 27 ▶ unless ($self->watch || $queued) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->watch or $queued) 16 11 27
Condition100%3/3
$self->watch or $queued
l!l&&r!l&&!r
10116
MC/DC100%2/2
$self->watch or $queued
$self->watch $queued
Truth table100%3/3
A or B
A= $self->watchB= $queued
ABresult
1X1
011
000
586 16 my $key = $self->read_key(undef);
587 16 ▶ return 'quit' unless $self->_on_key($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_on_key($key) 1 15 16
588 }
589  
590 26 $ticker->start($PERIOD);
591  
592 26 ▶ while ($game->status ne 'finished') {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->status ne "finished") 3723 4 3727
593 3723 ▶ return 'capped' if $self->max_ticks && $ticker->ticks >= $self->max_ticks;
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_ticks and $ticker->ticks >= $self->max_ticks 6 3717 3723
Condition100%3/3
$self->max_ticks and $ticker->ticks >= $self->max_ticks
!ll&&!rl&&r
20235156
MC/DC100%2/2
$self->max_ticks and $ticker->ticks >= $self->max_ticks
$self->max_ticks $ticker->ticks >= $self->max_ticks
Truth table100%3/3
A and B
A= $self->max_ticksB= $ticker->ticks >= $self->max_ticks
ABresult
0X0
100
111
594  
595 3717 ▶ unless ($ticker->due) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ticker->due) 3717 0 3717
596 3717 my $key = $self->read_key($ticker->remaining);
597 3717 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 22 3695 3717
598 22 ▶ return 'quit' unless $self->_on_key($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_on_key($key) 16 6 22
599 6 next;
600 }
601 }
602  
603 3695 ▶ $self->_bot_turn if $self->watch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->watch 3511 184 3695
604 3695 my $step = $game->tick;
605 3695 $ticker->tick($PERIOD);
606 3695 ▶ $self->best($game->score) if $game->score > $self->best;
Branch: 1 branch
BranchTrueFalseTotal
if $game->score > $self->best 27 3668 3695
607 3695 ▶ $self->_frame($step->{died} ? ($game->result->stringify, 'again') : ());
Branch: 1 branch
BranchTrueFalseTotal
$step->{'died'} ? : 4 3691 3695
608 }
609  
610 4 return 'died';
611 6 };
612  
613 26 ▶my $finish = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
614 26 my ($self) = @_;
615 26 my $game = $self->game;
616 26 ▶ $game->quit unless $game->status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "finished" 23 3 26
617 26 $self->leave_raw;
618 26 my $result = $game->result;
619 26 $self->_say($result->stringify);
620 26 return $result;
621 6 };
622  
623 30 ▶sub start {
Subroutine start: called CC=12 SCAR=24.9
Pod documented
624 30 my ($self) = @_;
625 30 my $game = $self->game;
626  
627 30 ▶ if (my $why = $self->too_narrow) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = $self->too_narrow) 1 29 30
628 1 $self->refusal($why);
629 1 $self->stopped('narrow');
630 1 return undef;
631 }
632  
633 29 ▶ if ($self->watch) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->watch) { } 10 19 29
elsif (not $self->enter_raw) { } 3 16 19
634 10 $self->enter_raw;
635 }
636 elsif (!$self->enter_raw) {
637 3 $self->refusal($self->cannot_play);
638 3 $self->stopped('keys');
639 3 return undef;
640 }
641  
642 26 $self->_intro;
643  
644 26 my $queued = 0;
645 26 while (1) {
646 27 my $how = $self->$one_game($queued);
647  
648 27 ▶ if ($how ne 'died') {
Branch: 1 branch
BranchTrueFalseTotal
if ($how ne "died") 23 4 27
649 23 $self->stopped($how);
650 23 return $self->$finish;
651 }
652  
653 4 ▶ return $self->$finish if $self->watch || $self->max_ticks;
Branch: 1 branch
BranchTrueFalseTotal
if $self->watch or $self->max_ticks 2 2 4
Condition66%2/3
$self->watch or $self->max_ticks
l!l&&r!l&&!r
202
MC/DC50%1/2
$self->watch or $self->max_ticks
$self->watch $self->max_ticks
Truth table66%2/3
A or B
A= $self->watchB= $self->max_ticks
ABresult
1X1
011
000
654  
655 2 while (1) {
656 2 my $key = $self->read_key(undef);
657 2 ▶ if (!defined $key || $key eq 'q' || $key eq 'eof' || $key eq 'interrupt') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt") 1 1 2
Condition66%6/9
not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
101
not defined $key or $key eq "q" or $key eq "eof"
l!l&&r!l&&!r
101
not defined $key or $key eq "q"
l!l&&r!l&&!r
101
MC/DC25%1/4
not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "q" $key eq "eof" $key eq "interrupt"
Truth table40%2/5
not(A) or B or C or D
A= not defined $keyB= $key eq "q"C= $key eq "eof"D= $key eq "interrupt"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
658 1 $self->stopped('quit');
659 1 return $self->$finish;
660 }
661 1 ▶ last if $FLAP{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $FLAP{$key} 1 0 1
662 }
663  
664 1 $game->reset;
665 1 $game->flap;
666 1 $queued = 1;
667 }
668 }
669  
670 3 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
Pod documented
671 3 return <<'USAGE';
672 usage: floppy [options]
673  
674 --seed TEXT the game to play: the same seed and the same flaps are the same game
675 --watch watch the bot play
676 --level N the bot's level for --watch, 1 to 3 (default 3)
677 --ticks N stop after N ticks (0, the default, plays to the end)
678 --rows N, --cols N the view in cells; the default fits the window, at most 25 rows
679 --quiet no frames, only how it ended
680 --ascii plain characters instead of block glyphs
681 --colour, --no-colour force colour on or off; NO_COLOR in the environment wins
682 --raylib open a window instead of playing in the terminal
683 (needs Raylib::FFI and perl 5.38 or later)
684 --version, --help
685  
686 keys: space, up, enter, w or k flap; p pauses; q quits; after a death, space plays again.
687  
688 exit status: 0 the game ended or you quit; 2 a bad option, or a game that could not start.
689 USAGE
690 }
691  
692 2 ▶my $sentence = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
693 2 my ($why) = @_;
694 2 $why =~ s/ at \S+ line \d+\.?\s*\z//s;
695 2 $why =~ s/\A(?:Game::Floppy(?:::\w+)*|floppy): //;
696 2 $why =~ s/\s+\z//;
697 2 ▶ $why .= '.' unless $why =~ /[.!?]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $why =~ /[.!?]\z/u 2 0 2
698 2 return $why;
699 6 };
700  
701 11 ▶sub run {
Subroutine run: called CC=25 SCAR=36.2
Pod documented
702 11 my ($class, @argv) = @_;
703  
704 11 my %opt;
705 11 my $ok = eval {
706 11 ▶ local $SIG{__WARN__} = sub { my $w = $_[0]; $w =~ s/\s+\z//; print STDERR "floppy: $w\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
707 11 Getopt::Long::GetOptionsFromArray(\@argv, \%opt,
708 'seed=s', 'level|l=i', 'watch|w', 'ticks=i', 'rows=i', 'cols=i',
709 'quiet|q', 'ascii|a', 'colour|color!', 'raylib', 'version|v', 'help|h');
710 };
711 11 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 10 11
712 1 print STDERR usage();
713 1 return 2;
714 }
715 10 ▶ if ($opt{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 1 9 10
716 1 print usage();
717 1 return 0;
718 }
719 9 ▶ if ($opt{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"version"}) 1 8 9
720 1 print "floppy $VERSION\n";
721 1 return 0;
722 }
723 8 ▶ if (@argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 1 7 8
724 1 print STDERR "floppy: I do not know '$argv[0]'.\n", usage();
725 1 return 2;
726 }
727  
728 7 my %with;
729 7 ▶ $with{seed} = $opt{seed} if defined $opt{seed};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"seed"} 3 4 7
730 7 ▶ $with{level} = $opt{level} if defined $opt{level};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"level"} 1 6 7
731 7 ▶ $with{max_ticks} = $opt{ticks} if defined $opt{ticks};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"ticks"} 5 2 7
732 7 ▶ $with{rows} = $opt{rows} if defined $opt{rows};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"rows"} 3 4 7
733 7 ▶ $with{cols} = $opt{cols} if defined $opt{cols};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"cols"} 2 5 7
734 7 ▶ $with{watch} = 1 if $opt{watch};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"watch"} 5 2 7
735 7 ▶ $with{quiet} = 1 if $opt{quiet};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"quiet"} 2 5 7
736 7 ▶ $with{ascii} = 1 if $opt{ascii};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"ascii"} 1 6 7
737 7 ▶ $with{ansi} = $opt{colour} ? 1 : 0 if defined $opt{colour};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opt{"colour"} 1 6 7
$opt{'colour'} ? : 0 1 1
738  
739 7 ▶ if ($opt{raylib}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"raylib"}) 0 7 7
740 0 ▶ unless (eval { require Game::Floppy::Raylib; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Game::Floppy::Raylib; 1 } }) 0 0 0
741 0 print STDERR "floppy: the window is not part of this build; play in the terminal.\n";
742 0 return 2;
743 }
744 0 ▶ unless (Game::Floppy::Raylib->available) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Game::Floppy::Raylib"->available) 0 0 0
745 0 print STDERR 'floppy: ' . Game::Floppy::Raylib->why_not . "\n";
746 0 return 2;
747 }
748 0 delete @with{qw/ quiet ascii ansi rows cols /};
749 0 my $window = eval { Game::Floppy::Raylib->new(%with) };
750 0 ▶ unless ($window) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($window) 0 0 0
751 0 print STDERR 'floppy: ' . $sentence->($@) . "\n";
752 0 return 2;
753 }
754 0 ▶ return $window->start ? 0 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$window->start ? : 0 0 0
755 }
756  
757 7 my $terminal = eval { $class->new(%with) };
758 7 ▶ unless ($terminal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terminal) 2 5 7
759 2 print STDERR 'floppy: ' . $sentence->($@) . "\n";
760 2 return 2;
761 }
762  
763 5 local @SIG{qw/ INT TERM HUP /};
764 5 for my $signal (qw/ INT TERM HUP /) {
765 0 ▶ $SIG{$signal} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
766 0 $terminal->leave_raw;
767 0 $SIG{$signal} = 'DEFAULT';
768 0 kill $signal, $$;
769 15 };
770 }
771  
772 5 my $result = eval { $terminal->start };
773 5 my $died = $@;
774 5 $terminal->leave_raw;
775 5 ▶ die $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 0 5 5
776  
777 5 ▶ return 0 if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 3 2 5
778 2 ▶ print STDERR 'floppy: ' . $terminal->refusal . "\n" if CORE::length $terminal->refusal;
Branch: 1 branch
BranchTrueFalseTotal
if length $terminal->refusal 2 0 2
779 2 return 2;
780 }
781  
782 6 1;
783  
784 __END__
« blib/lib/Game/Floppy/Raylib.pm ↑ Summary blib/lib/Game/Floppy.pm »