… | |
… | |
5 | =cut |
5 | =cut |
6 | |
6 | |
7 | package Crossfire; |
7 | package Crossfire; |
8 | |
8 | |
9 | our $VERSION = '0.1'; |
9 | our $VERSION = '0.1'; |
|
|
10 | |
|
|
11 | use strict; |
|
|
12 | |
10 | use base 'Exporter'; |
13 | use base 'Exporter'; |
11 | our @EXPORT = qw(read_pak read_arch arch2map); |
|
|
12 | |
|
|
13 | use strict; |
|
|
14 | |
14 | |
15 | use Storable; |
15 | use Storable; |
|
|
16 | |
|
|
17 | #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 | ); |
16 | |
24 | |
17 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
25 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
18 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
26 | or die "\$CROSSFIRE_LIBDIR must be set\n"; |
19 | |
27 | |
20 | sub T (){ 32 } |
28 | sub TILESIZE (){ 32 } |
21 | |
29 | |
22 | our $ARCH; |
30 | our $ARCH; |
23 | our $TILE; |
|
|
24 | |
31 | |
25 | sub read_pak($;$) { |
32 | sub read_pak($;$) { |
26 | my ($path, $cache) = @_; |
33 | my ($path, $cache) = @_; |
27 | |
34 | |
28 | eval { |
35 | eval { |
… | |
… | |
123 | |
130 | |
124 | \%arc |
131 | \%arc |
125 | } |
132 | } |
126 | } |
133 | } |
127 | |
134 | |
|
|
135 | # 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 | # 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 | |
128 | sub arch2map($;$) { |
308 | sub arch2map($;$) { |
129 | my ($mapa) = @_; |
309 | my ($mapa) = @_; |
130 | |
310 | |
131 | my %meta; |
311 | my %meta; |
132 | |
312 | |
133 | my ($mapx, $mapy); |
313 | my ($mapx, $mapy); |
134 | |
314 | |
135 | my $map; |
315 | my $map; |
136 | |
316 | |
137 | for (@{ $mapa->{arch} }) { |
317 | for (@{ $mapa->{arch} }) { |
138 | my ($x, $y) = ($_->{x}, $_->{y}); |
318 | my ($x, $y) = (delete $_->{x}, delete $_->{y}); |
139 | |
319 | |
140 | if ($_->{_name} eq "map") { |
320 | if ($_->{_name} eq "map") { |
141 | $meta{info} = $_; |
321 | $meta{info} = $_; |
142 | |
322 | |
143 | $mapx = $_->{width} || $x; |
323 | $mapx = $_->{width} || $x; |
… | |
… | |
153 | } |
333 | } |
154 | } |
334 | } |
155 | |
335 | |
156 | $meta{width} = $mapx; |
336 | $meta{width} = $mapx; |
157 | $meta{height} = $mapy; |
337 | $meta{height} = $mapy; |
|
|
338 | $meta{map} = $map; |
158 | |
339 | |
159 | \%meta |
340 | \%meta |
160 | } |
341 | } |
161 | |
342 | |
162 | sub init($) { |
343 | sub init($) { |
163 | my ($cachedir) = @_; |
344 | my ($cachedir) = @_; |
164 | |
345 | |
165 | $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
346 | $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
166 | $TILE = read_pak "$LIB/crossfire.0", "$cachedir/crossfire.0.pst"; |
|
|
167 | } |
347 | } |
168 | |
348 | |
169 | =head1 AUTHOR |
349 | =head1 AUTHOR |
170 | |
350 | |
171 | Marc Lehmann <schmorp@schmorp.de> |
351 | Marc Lehmann <schmorp@schmorp.de> |