| 1 | #!perl | ||||||||||
| 2 | # | ||||||||||
| 3 | # xomb - launcher for Game::Xomb | ||||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use 5.24.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use Getopt::Long qw(GetOptions); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Game::Xomb; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use POSIX qw(strftime); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | $0 = 'xomb'; | |||||||||
| 12 | |||||||||||
| 13 | GetOptions( | ||||||||||
| 14 | 'delay=f' => \$Game::Xomb::Draw_Delay, | ||||||||||
| 15 | 'err=s' => \my $err, | ||||||||||
| 16 | 'help|h|?' => \&emit_help, | ||||||||||
| 17 | 'replay-delay=f' => \$Game::Xomb::Replay_Delay, | ||||||||||
| 18 | 'replay=s' => \my $replay, | ||||||||||
| 19 | 'save=s' => \my $save, | ||||||||||
| 20 | 'seed=i' => \$Game::Xomb::Seed, | ||||||||||
| 21 | 'today|daily' => \my $Flag_Today, | ||||||||||
| 22 | 1 | ▶ | 'version|v' => sub { say $Game::Xomb::VERSION; exit 1 }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 3 | ▶ | ) or exit 64; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | |||||||||||
| 25 | 0 | ▶ | if (-t STDERR) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 0 | close STDERR; | |||||||||
| 27 | 0 | ▶ | if (defined $err) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | ▶ | unless (open STDERR, '>', $err) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | say "Could not redirect STDERR to '$err': $!"; | |||||||||
| 30 | 0 | exit 1; | |||||||||
| 31 | } | ||||||||||
| 32 | 0 | STDERR->autoflush(1); | |||||||||
| 33 | } | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | if (defined $replay) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | ▶ | open $Game::Xomb::Replay_FH, '<', $replay | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | or die "could not read '$replay': $!\n"; | ||||||||||
| 39 | 0 | my $header = readline $Game::Xomb::Replay_FH; | |||||||||
| 40 | 0 | chomp $header; | |||||||||
| 41 | # NOTE version information is ignored for now | ||||||||||
| 42 | 0 | $Game::Xomb::Seed = (split ':', $header, 2)[1]; | |||||||||
| 43 | 0 | $Game::Xomb::RKFN = \&Game::Xomb::replay; | |||||||||
| 44 | } else { | ||||||||||
| 45 | 0 | $Game::Xomb::RKFN = \&Game::Xomb::getkey; | |||||||||
| 46 | 0 | ▶ | unless (defined $Game::Xomb::Seed) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | # the daily seed is a tradition from Brogue | ||||||||||
| 48 | 0 | ▶ | if ($Flag_Today) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | $Game::Xomb::Seed = strftime '%Y%m%d', localtime; | |||||||||
| 50 | } else { | ||||||||||
| 51 | # eh, it's a game | ||||||||||
| 52 | # https://flak.tedunangst.com/post/random-in-the-wild | ||||||||||
| 53 | 0 | $Game::Xomb::Seed = time(); | |||||||||
| 54 | } | ||||||||||
| 55 | } | ||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | # jsf.c takes a uint32_t; ensure that the Perl variable matches what | ||||||||||
| 59 | # that code internalizes | ||||||||||
| 60 | 0 | $Game::Xomb::Seed = unpack 'L', pack 'L', $Game::Xomb::Seed; | |||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | if (defined $save) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | ▶ | open $Game::Xomb::Save_FH, '>', $save or die "could not write to '$save': $!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 0 | $Game::Xomb::Save_FH->autoflush(1); | |||||||||
| 65 | 0 | say $Game::Xomb::Save_FH $Game::Xomb::VERSION . ':' . $Game::Xomb::Seed; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 0 | Game::Xomb::game_loop; | |||||||||
| 69 | |||||||||||
| 70 | 0 | exit(1); # NOTREACHED | |||||||||
| 71 | |||||||||||
| 72 | 1 | ▶ | sub emit_help { | ||||||||
|
Subroutine
emit_help: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 1 | warn <<'TLDR'; | |||||||||
| 74 | Usage: xomb [options] | ||||||||||
| 75 | |||||||||||
| 76 | --err=file - send STDERR to this file if not already redirected | ||||||||||
| 77 | --version - shows xomb version and then quits | ||||||||||
| 78 | |||||||||||
| 79 | --seed - set the game seed to a particular value | ||||||||||
| 80 | --today - set the game seed to YYYYMMDD in the local timezone | ||||||||||
| 81 | |||||||||||
| 82 | --save=file - save the seed+keystrokes used to this file | ||||||||||
| 83 | --replay=file - replay from a saved game file | ||||||||||
| 84 | |||||||||||
| 85 | --delay=f - delay for when player may want to take notice | ||||||||||
| 86 | --replay-delay=f - delay between moves on replay | ||||||||||
| 87 | |||||||||||
| 88 | xomb hides STDERR by default to not show warning or error messages; | ||||||||||
| 89 | debug with a command similar to | ||||||||||
| 90 | |||||||||||
| 91 | xomb 2>log | ||||||||||
| 92 | xomb --err=log | ||||||||||
| 93 | |||||||||||
| 94 | and then inspect the log file when things do go awry | ||||||||||
| 95 | |||||||||||
| 96 | TLDR | ||||||||||
| 97 | 1 | exit 64; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | __END__ | ||||||||||