1 |
=head1 NAME |
2 |
|
3 |
Games::Sokoban - load/transform/save sokoban levels in various formats |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Games::Sokoban; |
8 |
|
9 |
=head1 DESCRIPTION |
10 |
|
11 |
I needed something like this quickly - if you need better docs, you have to ask. |
12 |
|
13 |
Supports xsb (text), rle, sokevo and a small "binpack" format for input |
14 |
and output and can normalise levels as well as calculate unique IDs. |
15 |
|
16 |
=over 4 |
17 |
|
18 |
=cut |
19 |
|
20 |
package Games::Sokoban; |
21 |
|
22 |
use common::sense; |
23 |
|
24 |
use Carp (); |
25 |
use List::Util (); |
26 |
|
27 |
our $VERSION = '0.02'; |
28 |
|
29 |
=item $level = new Games::Sokoban [format => "text|rle|binpack"], [data => "###..."] |
30 |
|
31 |
=cut |
32 |
|
33 |
sub new { |
34 |
my ($class, %arg) = @_; |
35 |
|
36 |
my $self = bless \%arg, $class; |
37 |
|
38 |
$self->data (delete $self->{data}, delete $self->{format}) |
39 |
if exists $self->{data}; |
40 |
|
41 |
$self |
42 |
} |
43 |
|
44 |
=item $level = new_from_file Games::Sokoban $path[, $format] |
45 |
|
46 |
=cut |
47 |
|
48 |
sub new_from_file { |
49 |
my ($class, $path, $format) = @_; |
50 |
|
51 |
open my $fh, "<:perlio", $path |
52 |
or Carp::croak "$path: $!"; |
53 |
local $/; |
54 |
|
55 |
$class->new (data => (scalar <$fh>), format => $format) |
56 |
} |
57 |
|
58 |
sub detect_format($) { |
59 |
my ($data) = @_; |
60 |
|
61 |
return "text" if $data =~ /^[ #\@\*\$\.\+\015\012\-_]+$/; |
62 |
|
63 |
warn $data;#d# |
64 |
return "rle" if $data =~ /^[ #\@\*\$\.\+\015\012\-_|1-9]+$/; |
65 |
exit 5;#d# |
66 |
|
67 |
my ($a, $b) = unpack "ww", $data; |
68 |
return "binpack" if defined $a && defined $b; |
69 |
|
70 |
Carp::croak "unable to autodetect sokoban level format"; |
71 |
} |
72 |
|
73 |
=item $level->data ([$new_data, [$new_data_format]]]) |
74 |
|
75 |
Sets the level from the given data. |
76 |
|
77 |
=cut |
78 |
|
79 |
sub data { |
80 |
if (@_ > 1) { |
81 |
my ($self, $data, $format) = @_; |
82 |
|
83 |
$format ||= detect_format $data; |
84 |
|
85 |
if ($format eq "text" or $format eq "rle") { |
86 |
$data =~ y/-_|/ \n/; |
87 |
$data =~ s/(\d)(.)/$2 x $1/ge; |
88 |
my @lines = split /[\015\012]+/, $data; |
89 |
my $w = List::Util::max map length, @lines; |
90 |
|
91 |
$_ .= " " x ($w - length) |
92 |
for @lines; |
93 |
|
94 |
$self->{data} = join "\n", @lines; |
95 |
|
96 |
} elsif ($format eq "binpack") { |
97 |
(my ($w, $s), $data) = unpack "wwB*", $data; |
98 |
|
99 |
my @enc = ('#', '$', '.', ' ', ' ', '###', '*', '# '); |
100 |
|
101 |
$data = join "", |
102 |
map $enc[$_], |
103 |
unpack "C*", |
104 |
pack "(b*)*", |
105 |
unpack "(a3)*", $data; |
106 |
|
107 |
# clip extra chars (max. 2) |
108 |
my $extra = (length $data) % $w; |
109 |
substr $data, -$extra, $extra, "" if $extra; |
110 |
|
111 |
(substr $data, $s, 1) =~ y/ ./@+/; |
112 |
|
113 |
$self->{data} = |
114 |
join "\n", |
115 |
map "#$_#", |
116 |
"#" x $w, |
117 |
(unpack "(a$w)*", $data), |
118 |
"#" x $w; |
119 |
|
120 |
} else { |
121 |
Carp::croak "$format: unsupported sokoban level format requested"; |
122 |
} |
123 |
|
124 |
$self->update; |
125 |
} |
126 |
|
127 |
$_[0]{data} |
128 |
} |
129 |
|
130 |
sub pos2xy { |
131 |
use integer; |
132 |
|
133 |
$_[1] >= 0 |
134 |
or Carp::croak "illegal buffer offset"; |
135 |
|
136 |
( |
137 |
$_[1] % ($_[0]{w} + 1), |
138 |
$_[1] / ($_[0]{w} + 1), |
139 |
) |
140 |
} |
141 |
|
142 |
sub update { |
143 |
my ($self) = @_; |
144 |
|
145 |
for ($self->{data}) { |
146 |
s/^\n+//; |
147 |
s/\n$//; |
148 |
|
149 |
/^[^\n]+/ or die; |
150 |
|
151 |
$self->{w} = index $_, "\n"; |
152 |
$self->{h} = y/\n// + 1; |
153 |
} |
154 |
} |
155 |
|
156 |
=item $text = $level->as_text |
157 |
|
158 |
=cut |
159 |
|
160 |
sub as_text { |
161 |
my ($self) = @_; |
162 |
|
163 |
"$self->{data}\n" |
164 |
} |
165 |
|
166 |
=item $binary = $level->as_binpack |
167 |
|
168 |
=cut |
169 |
|
170 |
sub as_binpack { |
171 |
my ($self) = @_; |
172 |
|
173 |
my $binpack = chr $self->{w} - 2; |
174 |
|
175 |
my $w = $self->{w}; |
176 |
|
177 |
my $data = $self->{data}; |
178 |
|
179 |
# crop away all four borders |
180 |
$data =~ s/^#+\n//; |
181 |
$data =~ s/#+$//; |
182 |
$data =~ s/#$//mg; |
183 |
$data =~ s/^#//mg; |
184 |
|
185 |
$data =~ y/\n//d; |
186 |
|
187 |
$data =~ /[\@\+]/ or die; |
188 |
my $s = $-[0]; |
189 |
(substr $data, $s, 1) =~ y/@+/ ./; |
190 |
|
191 |
$data =~ s/\#\#\#/101/g; |
192 |
$data =~ s/\ \ \ /110/g; |
193 |
$data =~ s/\#\ /111/g; |
194 |
|
195 |
$data =~ s/\#/000/g; |
196 |
$data =~ s/\ /001/g; |
197 |
$data =~ s/\./010/g; |
198 |
$data =~ s/\*/011/g; |
199 |
$data =~ s/\$/100/g; |
200 |
|
201 |
# width, @-offset, data |
202 |
|
203 |
pack "wwB*", $w - 2, $s, $data |
204 |
} |
205 |
|
206 |
=item @lines = $level->as_lines |
207 |
|
208 |
=cut |
209 |
|
210 |
sub as_lines { |
211 |
split /\n/, $_[0]{data} |
212 |
} |
213 |
|
214 |
=item $line = $level->as_rle |
215 |
|
216 |
http://www.sokobano.de/wiki/index.php?title=Level_format |
217 |
|
218 |
=cut |
219 |
|
220 |
sub as_rle { |
221 |
my $data = $_[0]{data}; |
222 |
|
223 |
$data =~ s/ +$//mg; |
224 |
$data =~ y/\n /|-/; |
225 |
$data =~ s/((.)\2{2,8})/(length $1) . $2/ge; |
226 |
|
227 |
$data |
228 |
} |
229 |
|
230 |
=item ($x, $y) = $level->start |
231 |
|
232 |
Returns (0-based) starting coordinate. |
233 |
|
234 |
=cut |
235 |
|
236 |
sub start { |
237 |
my ($self) = @_; |
238 |
|
239 |
$self->{data} =~ /[\@\+]/ or Carp::croak "level has no starting point"; |
240 |
$self->pos2xy ($-[0]); |
241 |
} |
242 |
|
243 |
=item $level->hflip |
244 |
|
245 |
=item $level->vflip |
246 |
|
247 |
=item $level->transpose # topleft to bottomright |
248 |
|
249 |
=item $level->rotate_90 |
250 |
|
251 |
=item $level->rotate_180 |
252 |
|
253 |
=cut |
254 |
|
255 |
sub hflip { |
256 |
$_[0]{data} = join "\n", map { scalar reverse $_ } split /\n/, $_[0]{data}; |
257 |
} |
258 |
|
259 |
sub vflip { |
260 |
$_[0]{data} = join "\n", reverse split /\n/, $_[0]{data}; |
261 |
} |
262 |
|
263 |
sub transpose { |
264 |
my ($self) = @_; |
265 |
|
266 |
# there must be a more elegant way :/ |
267 |
my @c; |
268 |
|
269 |
for (split /\n/, $self->{data}) { |
270 |
my $i; |
271 |
|
272 |
$c[$i++] .= $_ for split //; |
273 |
} |
274 |
|
275 |
$self->{data} = join "\n", @c; |
276 |
($self->{w}, $self->{h}) = ($self->{h}, $self->{w}) |
277 |
} |
278 |
|
279 |
sub rotate_90 { |
280 |
$_[0]->vflip; |
281 |
$_[0]->transpose; |
282 |
} |
283 |
|
284 |
sub rotate_180 { |
285 |
$_[0]{data} = reverse $_[0]{data}; |
286 |
} |
287 |
|
288 |
=item $id = $level->simplify |
289 |
|
290 |
Detect playable area, crop to smallest size. |
291 |
|
292 |
=cut |
293 |
|
294 |
sub simplify { |
295 |
my ($self) = @_; |
296 |
|
297 |
# first detect playable area |
298 |
my ($w, $h) = ($self->{w}, $self->{h}); |
299 |
my ($x, $y) = $self->start; |
300 |
|
301 |
my @data = split /\n/, $self->{data}; |
302 |
my @mask = @data; |
303 |
|
304 |
y/#/\x00/c, y/#/\x7f/ for @mask; |
305 |
|
306 |
my @stack = [$x, $y, 0]; |
307 |
|
308 |
while (@stack) { |
309 |
my ($x, $y, $l) = @{ pop @stack }; |
310 |
my $line = $mask[$y]; |
311 |
|
312 |
for my $x ($x .. $x + $l) { |
313 |
(reverse substr $line, 0, $x + 1) =~ /\x00+/ |
314 |
or next; |
315 |
|
316 |
$l = $+[0]; |
317 |
|
318 |
$x -= $l - 1; |
319 |
(substr $line, $x) =~ /^\x00+/ or die; |
320 |
$l = $+[0]; |
321 |
|
322 |
substr $mask[$y], $x, $l, "\xff" x $l; |
323 |
|
324 |
push @stack, [$x, $y - 1, $l - 1] if $y > 0; |
325 |
push @stack, [$x, $y + 1, $l - 1] if $y < $h - 1; |
326 |
} |
327 |
} |
328 |
|
329 |
my $walls = "#" x $w; |
330 |
|
331 |
for (0 .. $h - 1) { |
332 |
$data[$_] = ($data[$_] & $mask[$_]) | ($walls & ~$mask[$_]); |
333 |
} |
334 |
|
335 |
# reduce borders |
336 |
pop @data while @data > 2 && $data[-2] eq $walls; # bottom |
337 |
shift @data while $data[1] eq $walls; # top |
338 |
|
339 |
for ($self->{data} = join "\n", @data) { |
340 |
s/#$//mg until /[^#]#$/m; # right |
341 |
s/^#//mg until /^#[^#]/m; # left |
342 |
} |
343 |
|
344 |
# phew, done |
345 |
} |
346 |
|
347 |
=item $id = $level->normalise |
348 |
|
349 |
normalises the level map and calculates/returns it's identity code |
350 |
|
351 |
http://www.sourcecode.se/sokoban/level_id.php, assume uppercase and hex. |
352 |
|
353 |
=cut |
354 |
|
355 |
sub normalise { |
356 |
my ($self) = @_; |
357 |
|
358 |
$self->simplify; |
359 |
|
360 |
require Digest::MD5; |
361 |
|
362 |
my ($best_md5, $best_data) = "\xff" x 9; |
363 |
|
364 |
my $chk = sub { |
365 |
my $md5 = substr Digest::MD5::md5 ("$self->{data}\n"), 0, 8; |
366 |
if ($md5 lt $best_md5) { |
367 |
$best_md5 = $md5; |
368 |
$best_data = $self->{data}; |
369 |
} |
370 |
}; |
371 |
|
372 |
$chk->(); $self->hflip; |
373 |
$chk->(); $self->vflip; |
374 |
$chk->(); $self->hflip; |
375 |
$chk->(); $self->rotate_90; |
376 |
$chk->(); $self->hflip; |
377 |
$chk->(); $self->vflip; |
378 |
$chk->(); $self->hflip; |
379 |
$chk->(); |
380 |
|
381 |
$self->data ($best_data, "text"); |
382 |
|
383 |
uc unpack "H*", $best_md5 |
384 |
} |
385 |
|
386 |
=item $levels = Games::Sokoban::load_sokevo $path |
387 |
|
388 |
Loads a sokevo snapshot/history file and returns all contained levels as |
389 |
Games::Sokoban objects in an arrayref. |
390 |
|
391 |
=cut |
392 |
|
393 |
sub load_sokevo($) { |
394 |
open my $fh, "<", $_[0] |
395 |
or Carp::croak "$_[0]: $!"; |
396 |
|
397 |
my @levels; |
398 |
|
399 |
while (<$fh>) { |
400 |
if (/^##+$/) { |
401 |
my $data = $_; |
402 |
while (<$fh>) { |
403 |
$data .= $_; |
404 |
last if /^$/; |
405 |
} |
406 |
|
407 |
push @levels, new Games::Sokoban data => $data; |
408 |
} |
409 |
} |
410 |
|
411 |
\@levels |
412 |
} |
413 |
|
414 |
1; |
415 |
|
416 |
=back |
417 |
|
418 |
=head1 AUTHOR |
419 |
|
420 |
Marc Lehmann <schmorp@schmorp.de> |
421 |
http://home.schmorp.de/ |
422 |
|
423 |
=cut |
424 |
|