=head1 NAME Crossfire - Crossfire maphandling =cut package Crossfire; our $VERSION = '0.1'; use strict; use Storable; our $LIB = $ENV{CROSSFIRE_LIBDIR} or die "\$CROSSFIRE_LIBDIR must be set\n"; sub T (){ 32 } our $ARCH; our $TILE; sub read_pak($;$) { my ($path, $cache) = @_; eval { defined $cache && -M $cache < -M $path && Storable::retrieve $cache } or do { my %pak; open my $fh, "<:raw", $path or die "$_[0]: $!"; while (<$fh>) { my ($type, $id, $len, $path) = split; $path =~ s/.*\///; read $fh, $pak{$path}, $len; } Storable::nstore \%pak, $cache if defined $cache; \%pak } } sub read_arch($;$) { my ($path, $cache) = @_; eval { defined $cache && -M $cache < -M $path && Storable::retrieve $cache } or do { my %arc; my ($more, $prev); open my $fh, "<:raw", $path or die "$path: $!"; my $parse_block; $parse_block = sub { my %arc = @_; while (<$fh>) { s/\s+$//; if (/^end$/i) { last; } elsif (/^arch (\S+)$/) { push @{ $arc{inventory} }, $parse_block->(_name => $1); } elsif (/^lore$/) { while (<$fh>) { last if /^endlore\s*$/i; $arc{lore} .= $_; } } elsif (/^msg$/) { while (<$fh>) { last if /^endmsg\s*$/i; $arc{msg} .= $_; } } elsif (/^(\S+)\s*(.*)$/) { $arc{lc $1} = $2; } elsif (/^\s*($|#)/) { # } else { warn "$path: unparsable line '$_' in arch $arc{_name}"; } } \%arc }; while (<$fh>) { s/\s+$//; if (/^more$/i) { $more = $prev; } elsif (/^object (\S+)$/i) { my $name = $1; my $arc = $parse_block->(_name => $name); if ($more) { $more->{more} = $arc; } else { $arc{$name} = $arc; } $prev = $arc; $more = undef; } elsif (/^arch (\S+)$/i) { push @{ $arc{arch} }, $parse_block->(_name => $1); } elsif (/^\s*($|#)/) { # } else { warn "$path: unparseable top-level line '$_'"; } } undef $parse_block; # work around bug in perl not freeing $fh etc. Storable::nstore \%arc, $cache if defined $cache; \%arc } } sub arch2map($;$) { my ($mapa) = @_; my %meta; my ($mapx, $mapy); my $map; for (@{ $mapa->{arch} }) { my ($x, $y) = ($_->{x}, $_->{y}); if ($_->{_name} eq "map") { $meta{info} = $_; $mapx = $_->{width} || $x; $mapy = $_->{height} || $y; } else { push @{ $map->[$x][$y] }, $_; # arch map is unreliable w.r.t. width and height $mapx = $x + 1 if $mapx <= $x; $mapy = $y + 1 if $mapy <= $y; #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x}; #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; } } $meta{width} = $mapx; $meta{height} = $mapy; \%meta } sub init($) { my ($cachedir) = @_; $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; $TILE = read_pak "$LIB/crossfire.0", "$cachedir/crossfire.0.pst"; } =head1 AUTHOR Marc Lehmann http://home.schmorp.de/ Robin Redeker http://www.ta-sa.org/ =cut 1