ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
Revision: 1.11
Committed: Tue Feb 7 14:30:08 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.10: +44 -1 lines
Log Message:
added some utility functions for the stack of map tiles

File Contents

# User Rev Content
1 elmex 1.1 =head1 NAME
2    
3     Crossfire - Crossfire maphandling
4    
5     =cut
6    
7 root 1.4 package Crossfire;
8    
9 elmex 1.3 our $VERSION = '0.1';
10 elmex 1.1
11     use strict;
12    
13 root 1.7 use base 'Exporter';
14    
15 elmex 1.1 use Storable;
16    
17 elmex 1.11 #XXX: The map_* procedures scream for a map-object
18    
19     our @EXPORT =
20     qw(read_pak read_arch arch2map $ARCH TILESIZE editor_archs
21     arch2pickmap arch_extends
22     map_get_tile_stack map_push_tile_stack map_pop_tile_stack
23     );
24 root 1.7
25 root 1.4 our $LIB = $ENV{CROSSFIRE_LIBDIR}
26 elmex 1.1 or die "\$CROSSFIRE_LIBDIR must be set\n";
27    
28 root 1.7 sub TILESIZE (){ 32 }
29 elmex 1.1
30 root 1.4 our $ARCH;
31 elmex 1.1
32 root 1.4 sub read_pak($;$) {
33     my ($path, $cache) = @_;
34 elmex 1.1
35     eval {
36 root 1.4 defined $cache
37     && -M $cache < -M $path
38     && Storable::retrieve $cache
39 elmex 1.1 } or do {
40     my %pak;
41    
42     open my $fh, "<:raw", $path
43     or die "$_[0]: $!";
44     while (<$fh>) {
45     my ($type, $id, $len, $path) = split;
46     $path =~ s/.*\///;
47     read $fh, $pak{$path}, $len;
48     }
49    
50 root 1.4 Storable::nstore \%pak, $cache
51     if defined $cache;
52 elmex 1.1
53     \%pak
54     }
55     }
56    
57     sub read_arch($;$) {
58 root 1.4 my ($path, $cache) = @_;
59    
60     eval {
61     defined $cache
62     && -M $cache < -M $path
63     && Storable::retrieve $cache
64     } or do {
65     my %arc;
66     my ($more, $prev);
67    
68     open my $fh, "<:raw", $path
69     or die "$path: $!";
70 elmex 1.1
71 root 1.4 my $parse_block; $parse_block = sub {
72     my %arc = @_;
73 elmex 1.2
74 root 1.4 while (<$fh>) {
75     s/\s+$//;
76     if (/^end$/i) {
77     last;
78     } elsif (/^arch (\S+)$/) {
79     push @{ $arc{inventory} }, $parse_block->(_name => $1);
80     } elsif (/^lore$/) {
81     while (<$fh>) {
82     last if /^endlore\s*$/i;
83     $arc{lore} .= $_;
84     }
85     } elsif (/^msg$/) {
86     while (<$fh>) {
87     last if /^endmsg\s*$/i;
88     $arc{msg} .= $_;
89     }
90     } elsif (/^(\S+)\s*(.*)$/) {
91     $arc{lc $1} = $2;
92     } elsif (/^\s*($|#)/) {
93     #
94     } else {
95     warn "$path: unparsable line '$_' in arch $arc{_name}";
96     }
97     }
98 elmex 1.1
99 root 1.4 \%arc
100     };
101 elmex 1.1
102     while (<$fh>) {
103     s/\s+$//;
104 root 1.4 if (/^more$/i) {
105     $more = $prev;
106     } elsif (/^object (\S+)$/i) {
107     my $name = $1;
108     my $arc = $parse_block->(_name => $name);
109    
110     if ($more) {
111     $more->{more} = $arc;
112     } else {
113     $arc{$name} = $arc;
114 elmex 1.1 }
115 root 1.4 $prev = $arc;
116     $more = undef;
117     } elsif (/^arch (\S+)$/i) {
118     push @{ $arc{arch} }, $parse_block->(_name => $1);
119 elmex 1.1 } elsif (/^\s*($|#)/) {
120     #
121     } else {
122 root 1.4 warn "$path: unparseable top-level line '$_'";
123 elmex 1.1 }
124     }
125    
126 root 1.4 undef $parse_block; # work around bug in perl not freeing $fh etc.
127 elmex 1.2
128 root 1.4 Storable::nstore \%arc, $cache
129     if defined $cache;
130 elmex 1.1
131 root 1.4 \%arc
132 elmex 1.2 }
133 elmex 1.1 }
134    
135 elmex 1.11 # returns the arch/object stack from a tile on a map
136     sub map_get_tile_stack {
137     my ($map, $x, $y) = @_;
138     my $as;
139    
140     if ($x > 0 || $x < $map->{width}
141     || $y > 0 || $y < $map->{height}) {
142    
143     $as = $map->{map}{map}[$x][$y] || [];
144     }
145    
146     return $as;
147     }
148    
149     # pop the topmost arch/object from the stack of a tile on a map
150     sub map_pop_tile_stack {
151     my ($map, $x, $y) = @_;
152    
153     if ($x > 0 || $x < $map->{width}
154     || $y > 0 || $y < $map->{height}) {
155    
156     pop @{$map->{map}{map}[$x][$y]};
157     }
158     }
159    
160     # pushes the arch/object on the stack of a tile on a map
161     sub map_push_tile_stack {
162     my ($map, $x, $y, $arch) = @_;
163    
164     if ($x > 0 || $x < $map->{width}
165     || $y > 0 || $y < $map->{height}) {
166    
167     push @{$map->{map}{map}[$x][$y]}, $arch;
168     }
169     }
170    
171    
172 elmex 1.10 # put all archs into a hash with editor_face as it's key
173     # NOTE: the arrays in the hash values are references to
174     # the archs from $ARCH
175     sub editor_archs {
176     my %paths;
177    
178     for (keys %$ARCH) {
179     my $arch = $ARCH->{$_};
180     push @{$paths{$arch->{editor_folder}}}, \$arch;
181     }
182    
183     return \%paths;
184     }
185    
186     # arch_extends determines how the arch looks like on the map,
187     # bigfaces, linked faces and single faces are handled here
188     # it returns (<xoffset>, <yoffset>, <width>, <height>)
189     # NOTE: non rectangular linked faces are not considered
190     sub arch_extends {
191     my ($a) = @_;
192    
193     my $TC = \%Crossfire::Tilecache::TILECACHE;
194    
195     my $facename =
196     $a->{face} || $ARCH->{$a->{_name}}->{face}
197     or return ();
198    
199     my $tile = $TC->{$facename}
200     or (warn "no gfx found for arch '$facename' in arch_size ()"), return;
201    
202     if ($tile->{w} > 1 || $tile->{h} > 1) {
203     # bigfaces
204     return (0, 0, $tile->{w}, $tile->{h});
205    
206     } elsif ($a->{more}) {
207     # linked faces
208     my ($miw, $mih, $maw, $mah) = (0, 0, 0, 0);
209     do {
210     $miw > (0 + $a->{x}) and $miw = $a->{x};
211     $mih > (0 + $a->{y}) and $mih = $a->{y};
212     $maw < (0 + $a->{x}) and $maw = $a->{x};
213     $mah < (0 + $a->{y}) and $mah = $a->{y};
214     } while $a = $a->{more};
215    
216     return ($miw, $mih, ($maw - $miw) + 1, ($mah - $mih) + 1)
217    
218     } else {
219     # single face
220     return (0, 0, 1, 1);
221     }
222     }
223    
224     # arch2pickmap forms a list of archs to a pickmap
225     sub arch2pickmap {
226     my ($archs, $w) = @_;
227    
228     # sort archs alphabetiacally
229     my $archs = [ sort { ${$a}->{_name} cmp ${$b}->{_name} } @$archs ];
230    
231     $w ||= 10; # default width
232     my $num = @$archs;
233     my $map = { };
234     # overall placement coords
235     my $x = 0;
236     my $y = 0;
237    
238     my ($maxh, $maxw) = (0, 0); # maximum sizes, to set map width/height later
239     my $drawn_archs = 1; # line-break counter
240     my $max_line_height = 1;
241    
242     for (my $i = 0; $i < $num; $i++) {
243    
244     defined ${$archs->[$i]}->{face} or next;
245    
246     # check whether this tile was already written (see below at (b))
247     unless (defined $map->{map}[$x][$y]) {
248    
249     my ($xoffs, $yoffs, $arch_w, $arch_h) = arch_extends (${$archs->[$i]});
250    
251     # these are special placement coords, for chained faces which
252     # have a special placement offset
253     my ($place_x, $place_y) = ($x, $y);
254     $xoffs < 0 and
255     $place_x += -$xoffs;
256     $yoffs < 0 and
257     $place_y += -$yoffs;
258    
259     # iterate over the tiles this arch takes
260     # NOTE: Chained archs are maybe not a rectangle, but i don't care
261     # much for that on pickmaps
262    
263     for (my $xi = 0; $xi < $arch_w; $xi++) {
264     for (my $yi = 0; $yi < $arch_h; $yi++) {
265    
266     my ($lx, $ly) = ($x + $xi, $y + $yi);
267    
268     if ($lx == $place_x and $ly == $place_y) {
269     push @{$map->{map}[$place_x][$place_y]}, my $a = ${$archs->[$i]};
270    
271     } else {
272    
273     # (b): here we set occupied tiles, but without the arch
274     $map->{map}[$lx][$ly] = [];
275     }
276     }
277     }
278     $drawn_archs++;
279    
280     $x += $arch_w - 1;
281     $max_line_height < $arch_h
282     and $max_line_height = $arch_h;
283    
284     } else {
285     $i--;
286     }
287    
288    
289     $x++;
290    
291     if ($x > $w) {
292    
293     $y += $max_line_height;
294     $max_line_height = 1;
295     $x = 0;
296     }
297    
298     $maxw < ($x + 1) and $maxw = $x + 1;
299     $maxh < ($y + 1) and $maxh = $y + 1;
300     }
301    
302     $map->{height} = $maxh;
303     $map->{width} = $maxw;
304    
305     return $map;
306     }
307    
308 root 1.4 sub arch2map($;$) {
309     my ($mapa) = @_;
310 elmex 1.1
311     my %meta;
312    
313     my ($mapx, $mapy);
314    
315     my $map;
316    
317     for (@{ $mapa->{arch} }) {
318 root 1.9 my ($x, $y) = (delete $_->{x}, delete $_->{y});
319 elmex 1.1
320     if ($_->{_name} eq "map") {
321     $meta{info} = $_;
322    
323     $mapx = $_->{width} || $x;
324     $mapy = $_->{height} || $y;
325     } else {
326     push @{ $map->[$x][$y] }, $_;
327    
328     # arch map is unreliable w.r.t. width and height
329     $mapx = $x + 1 if $mapx <= $x;
330     $mapy = $y + 1 if $mapy <= $y;
331     #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x};
332     #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y};
333     }
334     }
335    
336     $meta{width} = $mapx;
337     $meta{height} = $mapy;
338 root 1.8 $meta{map} = $map;
339 elmex 1.1
340     \%meta
341     }
342    
343 root 1.4 sub init($) {
344     my ($cachedir) = @_;
345 elmex 1.1
346 root 1.4 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
347 elmex 1.1 }
348    
349     =head1 AUTHOR
350    
351     Marc Lehmann <schmorp@schmorp.de>
352     http://home.schmorp.de/
353    
354     Robin Redeker <elmex@ta-sa.org>
355     http://www.ta-sa.org/
356    
357     =cut
358 root 1.4
359     1