ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
(Generate patch)

Comparing deliantra/Deliantra/Deliantra.pm (file contents):
Revision 1.26 by root, Thu Feb 23 05:23:01 2006 UTC vs.
Revision 1.49 by root, Thu Mar 16 22:10:25 2006 UTC

11use strict; 11use strict;
12 12
13use base 'Exporter'; 13use base 'Exporter';
14 14
15use Carp (); 15use Carp ();
16use Storable;
17use File::Spec; 16use File::Spec;
18use List::Util qw(min max); 17use List::Util qw(min max);
19 18use Storable;
20#XXX: The map_* procedures scream for a map-object
21 19
22our @EXPORT = 20our @EXPORT =
23 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents); 21 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents);
24 22
25our $LIB = $ENV{CROSSFIRE_LIBDIR} 23our $LIB = $ENV{CROSSFIRE_LIBDIR}
44sub MOVE_FLY_LOW (){ 0x2 } 42sub MOVE_FLY_LOW (){ 0x2 }
45sub MOVE_FLY_HIGH (){ 0x4 } 43sub MOVE_FLY_HIGH (){ 0x4 }
46sub MOVE_FLYING (){ 0x6 } 44sub MOVE_FLYING (){ 0x6 }
47sub MOVE_SWIM (){ 0x8 } 45sub MOVE_SWIM (){ 0x8 }
48sub MOVE_ALL (){ 0xf } 46sub MOVE_ALL (){ 0xf }
47
48sub load_ref($) {
49 my ($path) = @_;
50
51 open my $fh, "<", $path
52 or die "$path: $!";
53 binmode $fh;
54 local $/;
55
56 Storable::thaw <$fh>
57}
58
59sub save_ref($$) {
60 my ($ref, $path) = @_;
61
62 open my $fh, ">", "$path~"
63 or die "$path~: $!";
64 binmode $fh;
65 print $fh Storable::freeze $ref;
66 close $fh;
67 rename "$path~", $path
68 or die "$path: $!";
69}
49 70
50sub normalize_arch($) { 71sub normalize_arch($) {
51 my ($ob) = @_; 72 my ($ob) = @_;
52 73
53 my $arch = $ARCH{$ob->{_name}} 74 my $arch = $ARCH{$ob->{_name}}
113sub read_pak($;$) { 134sub read_pak($;$) {
114 my ($path, $cache) = @_; 135 my ($path, $cache) = @_;
115 136
116 eval { 137 eval {
117 defined $cache 138 defined $cache
118 && -e $cache
119 && -M $cache < -M $path 139 && -M $cache < -M $path
120 && Storable::retrieve ($cache) 140 && load_ref $cache
121 } or do { 141 } or do {
122 my %pak; 142 my %pak;
123 143
124 open my $fh, "<:raw", $path 144 open my $fh, "<", $path
125 or Carp::croak "$_[0]: $!"; 145 or Carp::croak "$_[0]: $!";
146 binmode $fh;
126 while (<$fh>) { 147 while (<$fh>) {
127 my ($type, $id, $len, $path) = split; 148 my ($type, $id, $len, $path) = split;
128 $path =~ s/.*\///; 149 $path =~ s/.*\///;
129 read $fh, $pak{$path}, $len; 150 read $fh, $pak{$path}, $len;
130 } 151 }
131 152
132 Storable::nstore (\%pak, $cache) 153 save_ref \%pak, $cache
133 if defined $cache; 154 if defined $cache;
134 155
135 \%pak 156 \%pak
136 } 157 }
137} 158}
139sub read_arch($;$) { 160sub read_arch($;$) {
140 my ($path, $cache) = @_; 161 my ($path, $cache) = @_;
141 162
142 eval { 163 eval {
143 defined $cache 164 defined $cache
144 && -e $cache
145 && -M $cache < -M $path 165 && -M $cache < -M $path
146 && Storable::retrieve ($cache) 166 && load_ref $cache
147 } or do { 167 } or do {
148 my %arc; 168 my %arc;
149 my ($more, $prev); 169 my ($more, $prev);
150 170
151 open my $fh, "<:raw", $path 171 open my $fh, "<", $path
152 or Carp::croak "$path: $!"; 172 or Carp::croak "$path: $!";
173
174 binmode $fh;
153 175
154 my $parse_block; $parse_block = sub { 176 my $parse_block; $parse_block = sub {
155 my %arc = @_; 177 my %arc = @_;
156 178
157 while (<$fh>) { 179 while (<$fh>) {
206 } 228 }
207 } 229 }
208 230
209 undef $parse_block; # work around bug in perl not freeing $fh etc. 231 undef $parse_block; # work around bug in perl not freeing $fh etc.
210 232
211 warn "hoi\n";#d# 233 save_ref \%arc, $cache
212 Storable::nstore (\%arc, $cache)
213 if defined $cache; 234 if defined $cache;
214 warn "hox\n";#d#
215 235
216 \%arc 236 \%arc
217 } 237 }
218} 238}
219 239
243 my ($a) = @_; 263 my ($a) = @_;
244 264
245 my $o = $ARCH{$a->{_name}} 265 my $o = $ARCH{$a->{_name}}
246 or return; 266 or return;
247 267
248 my $face = $FACE{$a->{face} || $o->{face}} 268 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"}
249 or (warn "no face data found for arch '$a->{_name}'"), return; 269 or (warn "no face data found for arch '$a->{_name}'"), return;
250 270
251 if ($face->{w} > 1 || $face->{h} > 1) { 271 if ($face->{w} > 1 || $face->{h} > 1) {
252 # bigface 272 # bigface
253 return (0, 0, $face->{w} - 1, $face->{h} - 1); 273 return (0, 0, $face->{w} - 1, $face->{h} - 1);
285 import 305 import
286 306
287=cut 307=cut
288 308
289sub arch_attr($) { 309sub arch_attr($) {
290 my ($arch) = @_; 310 my ($obj) = @_;
291 311
292 require Crossfire::Data; 312 require Crossfire::Data;
293 313
314 my $root;
294 my $attr; 315 my $attr = { };
316
317 my $arch = $ARCH{ $obj->{_name} };
318 my $type = $obj->{type} || $arch->{type};
295 319
296 if ($arch->{type} > 0) { 320 if ($type > 0) {
297 $attr = $Crossfire::Data::ATTR{$arch->{type}+0}; 321 $root = $Crossfire::Data::ATTR{$type};
298 } else { 322 } else {
299 $attr = $Crossfire::Data::TYPE{Misc}; 323 $root = $Crossfire::Data::TYPE{Misc};
300 324
301 type: 325 type:
302 for (@Crossfire::Data::ATTR0) { 326 for (@Crossfire::Data::ATTR0) {
303 my $req = $_->{required} 327 my $req = $_->{required}
304 or die "internal error: ATTR0 without 'required'"; 328 or die "internal error: ATTR0 without 'required'";
305 329
330 keys %$req;
306 while (my ($k, $v) = each %$req) { 331 while (my ($k, $v) = each %$req) {
307 next type 332 next type
308 unless $arch->{$k} == $v; 333 unless $obj->{$k} == $v || $arch->{$k} == $v;
309 } 334 }
310 335
311 $attr = $_; 336 $root = $_;
312 } 337 }
338 }
339
340 my @import = ($root);
313 } 341
314
315 $attr || \%Crossfire::Data::DEFAULT_ATTR; 342 unshift @import, \%Crossfire::Data::DEFAULT_ATTR
343 unless $type == 116;
344
345 my (%ignore);
346 my (@section_order, %section, @attr_order);
347
348 while (my $type = shift @import) {
349 push @import, @{$type->{import} || []};
350
351 $attr->{$_} ||= $type->{$_}
352 for qw(name desc use);
353
354 for (@{$type->{ignore} || []}) {
355 $ignore{$_}++ for ref $_ ? @$_ : $_;
356 }
357
358 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) {
359 my ($name, $attr) = @$_;
360 push @section_order, $name;
361 for (@$attr) {
362 my ($k, $v) = @$_;
363 push @attr_order, $k;
364 $section{$name}{$k} ||= $v;
365 }
366 }
367 }
368
369 $attr->{section} = [
370 map !exists $section{$_} ? () : do {
371 my $attr = delete $section{$_};
372
373 [
374 $_,
375 map exists $attr->{$_} && !$ignore{$_}
376 ? [$_ => delete $attr->{$_}] : (),
377 @attr_order
378 ]
379 },
380
381 exists $section{$_} ? [$_ => delete $section{$_}] : (),
382 @section_order
383 ];
384
385 $attr
316} 386}
317 387
318sub arch_edit_sections { 388sub arch_edit_sections {
319# if (edit_type == IGUIConstants.TILE_EDIT_NONE) 389# if (edit_type == IGUIConstants.TILE_EDIT_NONE)
320# edit_type = 0; 390# edit_type = 0;
383 453
384 mkdir $cachedir, 0777; 454 mkdir $cachedir, 0777;
385 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 455 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
386} 456}
387 457
388$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; 458$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
389 459
390init $VARDIR; 460init $VARDIR;
391 461
392=head1 AUTHOR 462=head1 AUTHOR
393 463

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines