ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
Revision: 1.10
Committed: Mon Feb 6 23:01:31 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.9: +137 -1 lines
Log Message:
implemented arch_extends and arch2pickmap

File Contents

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