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.8 by root, Wed May 12 00:24:37 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]])
74 72
75Sets the level from the given data. 73Sets the level from the given data.
76 74
77=cut 75=cut
78 76
119 117
120 } else { 118 } else {
121 Carp::croak "$format: unsupported sokoban level format requested"; 119 Carp::croak "$format: unsupported sokoban level format requested";
122 } 120 }
123 121
122 $self->{format} = $format;
124 $self->update; 123 $self->update;
125 } 124 }
126 125
127 $_[0]{data} 126 $_[0]{data}
128} 127}
246 $self->pos2xy ($-[0]); 245 $self->pos2xy ($-[0]);
247} 246}
248 247
249=item $level->hflip 248=item $level->hflip
250 249
250Mirror horizontally.
251
251=item $level->vflip 252=item $level->vflip
252 253
253=item $level->transpose # topleft to bottomright 254Mirror vertically.
255
256=item $level->transpose
257
258Transpose level (mirror at top-left/bottom-right diagonal).
254 259
255=item $level->rotate_90 260=item $level->rotate_90
256 261
262Rotate by 90 degrees clockwise.
263
257=item $level->rotate_180 264=item $level->rotate_180
265
266Rotate by 180 degrees clockwise.
258 267
259=cut 268=cut
260 269
261sub hflip { 270sub hflip {
262 $_[0]{data} = join "\n", map { scalar reverse $_ } split /\n/, $_[0]{data}; 271 $_[0]{data} = join "\n", map { scalar reverse $_ } split /\n/, $_[0]{data};
350 # phew, done 359 # phew, done
351} 360}
352 361
353=item $id = $level->normalise 362=item $id = $level->normalise
354 363
355normalises the level map and calculates/returns it's identity code 364Simplifies the level map and calculates/returns its identity code.
356 365.
357http://www.sourcecode.se/sokoban/level_id.php, assume uppercase and hex. 366http://www.sourcecode.se/sokoban/level_id.php, assume uppercase and hex.
358 367
359=cut 368=cut
360 369
361sub normalise { 370sub normalise {
395Games::Sokoban objects in an arrayref. 404Games::Sokoban objects in an arrayref.
396 405
397=cut 406=cut
398 407
399sub load_sokevo($) { 408sub load_sokevo($) {
400 open my $fh, "<", $_[0] 409 open my $fh, "<:crlf", $_[0]
401 or Carp::croak "$_[0]: $!"; 410 or Carp::croak "$_[0]: $!";
402 411
403 my @levels; 412 my @levels;
404 413
414 # skip file header
415 local $/ = "\n\n";
416 scalar <$fh>;
417
405 while (<$fh>) { 418 while (<$fh>) {
406 if (/^##+$/) { 419 chomp;
407 my $data = $_; 420 my %meta = split /(?:: |\n)/;
408 while (<$fh>) {
409 $data .= $_;
410 last if /^$/;
411 }
412 421
422 $_ = <$fh>;
423
424 /^##+\n/ or last;
425
413 # sokevo internally locks some cells 426 # sokevo internally locks some cells
414 $data =~ y/^%:,;-=?/ #.$* +#/; 427 y/^%:,;-=?/ #.$* +#/;
415 428
429 # skip levels without pusher
430 y/@+// or next;
431
416 push @levels, new Games::Sokoban data => $data; 432 push @levels, new Games::Sokoban data => $_, meta => \%meta;
417 }
418 } 433 }
419 434
420 \@levels 435 \@levels
421} 436}
422 437

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines