ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Games-Sokoban/Sokoban.pm
(Generate patch)

Comparing Games-Sokoban/Sokoban.pm (file contents):
Revision 1.5 by root, Tue May 11 23:48:40 2010 UTC vs.
Revision 1.10 by root, Wed May 12 17:52:11 2010 UTC

22use common::sense; 22use common::sense;
23 23
24use Carp (); 24use Carp ();
25use List::Util (); 25use List::Util ();
26 26
27our $VERSION = '0.02'; 27our $VERSION = '1.0';
28 28
29=item $level = new Games::Sokoban [format => "text|rle|binpack"], [data => "###..."] 29=item $level = new Games::Sokoban [format => "text|rle|binpack"], [data => "###..."]
30 30
31=cut 31=cut
32 32
58sub detect_format($) { 58sub detect_format($) {
59 my ($data) = @_; 59 my ($data) = @_;
60 60
61 return "text" if $data =~ /^[ #\@\*\$\.\+\015\012\-_]+$/; 61 return "text" if $data =~ /^[ #\@\*\$\.\+\015\012\-_]+$/;
62 62
63 warn $data;#d#
64 return "rle" if $data =~ /^[ #\@\*\$\.\+\015\012\-_|1-9]+$/; 63 return "rle" if $data =~ /^[ #\@\*\$\.\+\015\012\-_|1-9]+$/;
65 exit 5;#d#
66 64
67 my ($a, $b) = unpack "ww", $data; 65 my ($a, $b) = unpack "ww", $data;
68 return "binpack" if defined $a && defined $b; 66 return "binpack" if defined $a && defined $b;
69 67
70 Carp::croak "unable to autodetect sokoban level format"; 68 Carp::croak "unable to autodetect sokoban level format";
71} 69}
72 70
73=item $level->data ([$new_data, [$new_data_format]]]) 71=item $level->data ([$new_data, [$new_data_format]])
72
73Sets the level from the given data.
74 74
75=cut 75=cut
76 76
77sub data { 77sub data {
78 if (@_ > 1) {
78 my ($self, $data, $format) = @_; 79 my ($self, $data, $format) = @_;
79 80
80 $format ||= detect_format $data; 81 $format ||= detect_format $data;
81 82
82 if ($format eq "text" or $format eq "rle") { 83 if ($format eq "text" or $format eq "rle") {
83 $data =~ y/-_|/ \n/; 84 $data =~ y/-_|/ \n/;
84 $data =~ s/(\d)(.)/$2 x $1/ge; 85 $data =~ s/(\d)(.)/$2 x $1/ge;
85 my @lines = split /[\015\012]+/, $data; 86 my @lines = split /[\015\012]+/, $data;
86 my $w = List::Util::max map length, @lines; 87 my $w = List::Util::max map length, @lines;
87 88
88 $_ .= " " x ($w - length) 89 $_ .= " " x ($w - length)
89 for @lines; 90 for @lines;
90 91
91 $self->{data} = join "\n", @lines; 92 $self->{data} = join "\n", @lines;
92 93
93 } elsif ($format eq "binpack") { 94 } elsif ($format eq "binpack") {
94 (my ($w, $s), $data) = unpack "wwB*", $data; 95 (my ($w, $s), $data) = unpack "wwB*", $data;
95 96
96 my @enc = ('#', '$', '.', ' ', ' ', '###', '*', '# '); 97 my @enc = ('#', '$', '.', ' ', ' ', '###', '*', '# ');
97 98
98 $data = join "", 99 $data = join "",
99 map $enc[$_], 100 map $enc[$_],
100 unpack "C*", 101 unpack "C*",
101 pack "(b*)*", 102 pack "(b*)*",
102 unpack "(a3)*", $data; 103 unpack "(a3)*", $data;
103 104
104 # clip extra chars (max. 2) 105 # clip extra chars (max. 2)
105 my $extra = (length $data) % $w; 106 my $extra = (length $data) % $w;
106 substr $data, -$extra, $extra, "" if $extra; 107 substr $data, -$extra, $extra, "" if $extra;
107 108
108 (substr $data, $s, 1) =~ y/ ./@+/; 109 (substr $data, $s, 1) =~ y/ ./@+/;
109 110
110 $self->{data} = 111 $self->{data} =
111 join "\n", 112 join "\n",
112 map "#$_#", 113 map "#$_#",
113 "#" x $w, 114 "#" x $w,
114 (unpack "(a$w)*", $data), 115 (unpack "(a$w)*", $data),
115 "#" x $w; 116 "#" x $w;
116 117
117 } else { 118 } else {
118 Carp::croak "$format: unsupported sokoban level format requested"; 119 Carp::croak "$format: unsupported sokoban level format requested";
119 } 120 }
120 121
122 $self->{format} = $format;
121 $self->update; 123 $self->update;
124 }
122 125
123 ($self->{data}) 126 $_[0]{data}
124} 127}
125 128
126sub pos2xy { 129sub pos2xy {
127 use integer; 130 use integer;
128 131
158 161
159 "$self->{data}\n" 162 "$self->{data}\n"
160} 163}
161 164
162=item $binary = $level->as_binpack 165=item $binary = $level->as_binpack
166
167Binpack is a very compact binary format (usually 17% of the size of an xsb
168file), that is still reasonably easy to encode/decode.
169
170It only tries to store simplified levels with full fidelity - other levels
171can be slightly changed outside the playable area.
163 172
164=cut 173=cut
165 174
166sub as_binpack { 175sub as_binpack {
167 my ($self) = @_; 176 my ($self) = @_;
236 $self->pos2xy ($-[0]); 245 $self->pos2xy ($-[0]);
237} 246}
238 247
239=item $level->hflip 248=item $level->hflip
240 249
250Mirror horizontally.
251
241=item $level->vflip 252=item $level->vflip
242 253
243=item $level->transpose # topleft to bottomright 254Mirror vertically.
255
256=item $level->transpose
257
258Transpose level (mirror at top-left/bottom-right diagonal).
244 259
245=item $level->rotate_90 260=item $level->rotate_90
246 261
262Rotate by 90 degrees clockwise.
263
247=item $level->rotate_180 264=item $level->rotate_180
265
266Rotate by 180 degrees clockwise.
248 267
249=cut 268=cut
250 269
251sub hflip { 270sub hflip {
252 $_[0]{data} = join "\n", map { scalar reverse $_ } split /\n/, $_[0]{data}; 271 $_[0]{data} = join "\n", map { scalar reverse $_ } split /\n/, $_[0]{data};
340 # phew, done 359 # phew, done
341} 360}
342 361
343=item $id = $level->normalise 362=item $id = $level->normalise
344 363
345normalises the level map and calculates/returns it's identity code 364Simplifies the level map and calculates/returns its identity code.
346 365.
347http://www.sourcecode.se/sokoban/level_id.php, assume uppercase and hex. 366http://www.sourcecode.se/sokoban/level_id.php, assume uppercase and hex.
348 367
349=cut 368=cut
350 369
351sub normalise { 370sub normalise {
385Games::Sokoban objects in an arrayref. 404Games::Sokoban objects in an arrayref.
386 405
387=cut 406=cut
388 407
389sub load_sokevo($) { 408sub load_sokevo($) {
390 open my $fh, "<", $_[0] 409 open my $fh, "<:crlf", $_[0]
391 or Carp::croak "$_[0]: $!"; 410 or Carp::croak "$_[0]: $!";
392 411
393 my @levels; 412 my @levels;
394 413
414 # skip file header
415 local $/ = "\n\n";
416 scalar <$fh>;
417
395 while (<$fh>) { 418 while (<$fh>) {
396 if (/^##+$/) { 419 chomp;
397 my $data = $_; 420 my %meta = split /(?:: |\n)/;
398 while (<$fh>) {
399 $data .= $_;
400 last if /^$/;
401 }
402 421
422 $_ = <$fh>;
423
424 /^##+\n/ or last;
425
426 # sokevo internally locks some cells
427 y/^%:,;-=?/ #.$* +#/;
428
429 # skip levels without pusher
430 y/@+// or next;
431
403 push @levels, new Games::Sokoban data => $data; 432 push @levels, new Games::Sokoban data => $_, meta => \%meta;
404 }
405 } 433 }
406 434
407 \@levels 435 \@levels
408} 436}
409 437

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines