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.12 by root, Wed May 12 20:55:30 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.01';
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}
152 $self->{h} = y/\n// + 1; 151 $self->{h} = y/\n// + 1;
153 } 152 }
154} 153}
155 154
156=item $text = $level->as_text 155=item $text = $level->as_text
156
157Returns the level in xsb/text format - every row of the level is one line
158ended by a newline, e.g.:
159
160 "###\n# #\n###\n"
157 161
158=cut 162=cut
159 163
160sub as_text { 164sub as_text {
161 my ($self) = @_; 165 my ($self) = @_;
209 pack "wwB*", $w - 2, $s, $data 213 pack "wwB*", $w - 2, $s, $data
210} 214}
211 215
212=item @lines = $level->as_lines 216=item @lines = $level->as_lines
213 217
218Returns the level as a list of rows, each row is a text representation of
219the respective level row, e.g.:
220
221 ("###", "# #", "###")
222
214=cut 223=cut
215 224
216sub as_lines { 225sub as_lines {
217 split /\n/, $_[0]{data} 226 split /\n/, $_[0]{data}
218} 227}
219 228
220=item $line = $level->as_rle 229=item $line = $level->as_rle
221 230
222http://www.sokobano.de/wiki/index.php?title=Level_format 231http://www.sokobano.de/wiki/index.php?title=Level_format
232
233Example:
234
235 "3#|# #|3#"
223 236
224=cut 237=cut
225 238
226sub as_rle { 239sub as_rle {
227 my $data = $_[0]{data}; 240 my $data = $_[0]{data};
246 $self->pos2xy ($-[0]); 259 $self->pos2xy ($-[0]);
247} 260}
248 261
249=item $level->hflip 262=item $level->hflip
250 263
264Mirror horizontally.
265
251=item $level->vflip 266=item $level->vflip
252 267
253=item $level->transpose # topleft to bottomright 268Mirror vertically.
269
270=item $level->transpose
271
272Transpose level (mirror at top-left/bottom-right diagonal).
254 273
255=item $level->rotate_90 274=item $level->rotate_90
256 275
276Rotate by 90 degrees clockwise.
277
257=item $level->rotate_180 278=item $level->rotate_180
279
280Rotate by 180 degrees clockwise.
258 281
259=cut 282=cut
260 283
261sub hflip { 284sub hflip {
262 $_[0]{data} = join "\n", map { scalar reverse $_ } split /\n/, $_[0]{data}; 285 $_[0]{data} = join "\n", map { scalar reverse $_ } split /\n/, $_[0]{data};
350 # phew, done 373 # phew, done
351} 374}
352 375
353=item $id = $level->normalise 376=item $id = $level->normalise
354 377
355normalises the level map and calculates/returns it's identity code 378Simplifies the level map and calculates/returns its identity code.
356 379.
357http://www.sourcecode.se/sokoban/level_id.php, assume uppercase and hex. 380http://www.sourcecode.se/sokoban/level_id.php, assume uppercase and hex.
358 381
359=cut 382=cut
360 383
361sub normalise { 384sub normalise {
395Games::Sokoban objects in an arrayref. 418Games::Sokoban objects in an arrayref.
396 419
397=cut 420=cut
398 421
399sub load_sokevo($) { 422sub load_sokevo($) {
400 open my $fh, "<", $_[0] 423 open my $fh, "<:crlf", $_[0]
401 or Carp::croak "$_[0]: $!"; 424 or Carp::croak "$_[0]: $!";
402 425
403 my @levels; 426 my @levels;
404 427
428 # skip file header
429 local $/ = "\n\n";
430 scalar <$fh>;
431
405 while (<$fh>) { 432 while (<$fh>) {
406 if (/^##+$/) { 433 chomp;
407 my $data = $_; 434 my %meta = split /(?:: |\n)/;
408 while (<$fh>) {
409 $data .= $_;
410 last if /^$/;
411 }
412 435
436 $_ = <$fh>;
437
438 /^##+\n/ or last;
439
413 # sokevo internally locks some cells 440 # sokevo internally locks some cells
414 $data =~ y/^%:,;-=?/ #.$* +#/; 441 y/^%:,;-=?/ #.$* +#/;
415 442
443 # skip levels without pusher
444 y/@+// or next;
445
416 push @levels, new Games::Sokoban data => $data; 446 push @levels, new Games::Sokoban data => $_, meta => \%meta;
417 }
418 } 447 }
419 448
420 \@levels 449 \@levels
421} 450}
422 451

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines