… | |
… | |
12 | |
12 | |
13 | use base 'Exporter'; |
13 | use base 'Exporter'; |
14 | |
14 | |
15 | use Carp (); |
15 | use Carp (); |
16 | use Storable; |
16 | use Storable; |
|
|
17 | use File::Spec; |
17 | use List::Util qw(min max); |
18 | use List::Util qw(min max); |
18 | |
19 | |
19 | #XXX: The map_* procedures scream for a map-object |
20 | #XXX: The map_* procedures scream for a map-object |
20 | |
21 | |
21 | our @EXPORT = |
22 | our @EXPORT = |
… | |
… | |
24 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
25 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
25 | or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; |
26 | or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; |
26 | |
27 | |
27 | sub TILESIZE (){ 32 } |
28 | sub TILESIZE (){ 32 } |
28 | |
29 | |
29 | our $CACHEDIR; |
30 | our $VARDIR; |
30 | our %ARCH; |
31 | our %ARCH; |
31 | our %FACE; |
32 | our %FACE; |
32 | our $TILE; |
33 | our $TILE; |
33 | |
34 | |
34 | our %FIELD_MULTILINE = ( |
35 | our %FIELD_MULTILINE = ( |
… | |
… | |
112 | sub read_pak($;$) { |
113 | sub read_pak($;$) { |
113 | my ($path, $cache) = @_; |
114 | my ($path, $cache) = @_; |
114 | |
115 | |
115 | eval { |
116 | eval { |
116 | defined $cache |
117 | defined $cache |
|
|
118 | && -e $cache |
117 | && -M $cache < -M $path |
119 | && -M $cache < -M $path |
118 | && Storable::retrieve $cache |
120 | && Storable::retrieve ($cache) |
119 | } or do { |
121 | } or do { |
120 | my %pak; |
122 | my %pak; |
121 | |
123 | |
122 | open my $fh, "<:raw", $path |
124 | open my $fh, "<:raw", $path |
123 | or Carp::croak "$_[0]: $!"; |
125 | or Carp::croak "$_[0]: $!"; |
… | |
… | |
125 | my ($type, $id, $len, $path) = split; |
127 | my ($type, $id, $len, $path) = split; |
126 | $path =~ s/.*\///; |
128 | $path =~ s/.*\///; |
127 | read $fh, $pak{$path}, $len; |
129 | read $fh, $pak{$path}, $len; |
128 | } |
130 | } |
129 | |
131 | |
130 | Storable::nstore \%pak, $cache |
132 | Storable::nstore (\%pak, $cache) |
131 | if defined $cache; |
133 | if defined $cache; |
132 | |
134 | |
133 | \%pak |
135 | \%pak |
134 | } |
136 | } |
135 | } |
137 | } |
… | |
… | |
137 | sub read_arch($;$) { |
139 | sub read_arch($;$) { |
138 | my ($path, $cache) = @_; |
140 | my ($path, $cache) = @_; |
139 | |
141 | |
140 | eval { |
142 | eval { |
141 | defined $cache |
143 | defined $cache |
|
|
144 | && -e $cache |
142 | && -M $cache < -M $path |
145 | && -M $cache < -M $path |
143 | && Storable::retrieve $cache |
146 | && Storable::retrieve ($cache) |
144 | } or do { |
147 | } or do { |
145 | my %arc; |
148 | my %arc; |
146 | my ($more, $prev); |
149 | my ($more, $prev); |
147 | |
150 | |
148 | open my $fh, "<:raw", $path |
151 | open my $fh, "<:raw", $path |
… | |
… | |
203 | } |
206 | } |
204 | } |
207 | } |
205 | |
208 | |
206 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
209 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
207 | |
210 | |
208 | Storable::nstore \%arc, $cache |
211 | Storable::nstore (\%arc, $cache) |
209 | if defined $cache; |
212 | if defined $cache; |
210 | |
213 | |
211 | \%arc |
214 | \%arc |
212 | } |
215 | } |
213 | } |
216 | } |
… | |
… | |
224 | } |
227 | } |
225 | |
228 | |
226 | \%paths |
229 | \%paths |
227 | } |
230 | } |
228 | |
231 | |
|
|
232 | =item ($minx, $miny, $maxx, $maxy) = arch_extents $arch |
|
|
233 | |
229 | # arch_extents determines the extents of a given arch |
234 | arch_extents determines the extents of the given arch's face(s), linked |
230 | # bigfaces, linked faces and single faces are handled here |
235 | faces and single faces are handled here it returns (minx, miny, maxx, |
231 | # it returns (minx, miny, maxx, maxy) |
236 | maxy) |
|
|
237 | |
|
|
238 | =cut |
|
|
239 | |
232 | sub arch_extents { |
240 | sub arch_extents { |
233 | my ($a) = @_; |
241 | my ($a) = @_; |
234 | |
242 | |
235 | my $o = $ARCH{$a->{_name}} |
243 | my $o = $ARCH{$a->{_name}} |
236 | or return; |
244 | or return; |
… | |
… | |
259 | # single face |
267 | # single face |
260 | return (0, 0, 0, 0); |
268 | return (0, 0, 0, 0); |
261 | } |
269 | } |
262 | } |
270 | } |
263 | |
271 | |
264 | sub init($) { |
272 | =item $type = arch_attr $arch |
|
|
273 | |
|
|
274 | Returns a hashref describing the object and its attributes. It can contain |
|
|
275 | the following keys: |
|
|
276 | |
|
|
277 | name the name, suitable for display purposes |
|
|
278 | ignore |
|
|
279 | attr |
|
|
280 | desc |
|
|
281 | use |
|
|
282 | section => [name => \%attr, name => \%attr] |
|
|
283 | import |
|
|
284 | |
|
|
285 | =cut |
|
|
286 | |
|
|
287 | sub arch_attr($) { |
265 | my ($cachedir) = @_; |
288 | my ($arch) = @_; |
266 | |
289 | |
267 | return if %ARCH; |
290 | require Crossfire::Data; |
268 | |
291 | |
269 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
292 | my $attr; |
|
|
293 | |
|
|
294 | if ($arch->{type} > 0) { |
|
|
295 | $attr = $Crossfire::Data::ATTR{$arch->{type}+0}; |
|
|
296 | } else { |
|
|
297 | $attr = $Crossfire::Data::TYPE{Misc}; |
|
|
298 | |
|
|
299 | type: |
|
|
300 | for (@Crossfire::Data::ATTR0) { |
|
|
301 | my $req = $_->{required} |
|
|
302 | or die "internal error: ATTR0 without 'required'"; |
|
|
303 | |
|
|
304 | while (my ($k, $v) = each %$req) { |
|
|
305 | next type |
|
|
306 | unless $arch->{$k} == $v; |
|
|
307 | } |
|
|
308 | |
|
|
309 | $attr = $_; |
|
|
310 | } |
|
|
311 | } |
|
|
312 | |
|
|
313 | $attr || \%Crossfire::Data::DEFAULT_ATTR; |
270 | } |
314 | } |
271 | |
315 | |
272 | sub arch_edit_sections { |
316 | sub arch_edit_sections { |
273 | # if (edit_type == IGUIConstants.TILE_EDIT_NONE) |
317 | # if (edit_type == IGUIConstants.TILE_EDIT_NONE) |
274 | # edit_type = 0; |
318 | # edit_type = 0; |
… | |
… | |
328 | # return(edit_type); |
372 | # return(edit_type); |
329 | # |
373 | # |
330 | # |
374 | # |
331 | } |
375 | } |
332 | |
376 | |
333 | $CACHEDIR ||= "$ENV{HOME}/.crossfire"; |
377 | sub init($) { |
|
|
378 | my ($cachedir) = @_; |
334 | |
379 | |
|
|
380 | return if %ARCH; |
|
|
381 | |
|
|
382 | mkdir $cachedir, 0777; |
|
|
383 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
|
|
384 | } |
|
|
385 | |
|
|
386 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; |
|
|
387 | |
335 | init $CACHEDIR; |
388 | init $VARDIR; |
336 | |
389 | |
337 | =head1 AUTHOR |
390 | =head1 AUTHOR |
338 | |
391 | |
339 | Marc Lehmann <schmorp@schmorp.de> |
392 | Marc Lehmann <schmorp@schmorp.de> |
340 | http://home.schmorp.de/ |
393 | http://home.schmorp.de/ |