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.7 by root, Sun Feb 5 01:53:59 2006 UTC vs.
Revision 1.47 by root, Thu Mar 16 01:42:22 2006 UTC

10 10
11use strict; 11use strict;
12 12
13use base 'Exporter'; 13use base 'Exporter';
14 14
15use Carp ();
16use File::Spec;
17use List::Util qw(min max);
15use Storable; 18use Storable;
16 19
17our @EXPORT = qw(read_pak read_arch arch2map $ARCH TILESIZE); 20our @EXPORT =
21 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents);
18 22
19our $LIB = $ENV{CROSSFIRE_LIBDIR} 23our $LIB = $ENV{CROSSFIRE_LIBDIR}
20 or die "\$CROSSFIRE_LIBDIR must be set\n"; 24 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
21 25
22sub TILESIZE (){ 32 } 26sub TILESIZE (){ 32 }
23 27
28our $VARDIR;
24our $ARCH; 29our %ARCH;
30our %FACE;
31our $TILE;
32
33our %FIELD_MULTILINE = (
34 msg => "endmsg",
35 lore => "endlore",
36);
37
38# not used yet, maybe alphabetical is ok
39our @FIELD_ORDER = (qw(name name_pl));
40
41sub MOVE_WALK (){ 0x1 }
42sub MOVE_FLY_LOW (){ 0x2 }
43sub MOVE_FLY_HIGH (){ 0x4 }
44sub MOVE_FLYING (){ 0x6 }
45sub MOVE_SWIM (){ 0x8 }
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}
70
71sub normalize_arch($) {
72 my ($ob) = @_;
73
74 my $arch = $ARCH{$ob->{_name}}
75 or (warn "$ob->{_name}: no such archetype", return $ob);
76
77 delete $ob->{$_} for qw(can_knockback can_parry can_impale can_cut can_dam_armour can_apply);
78
79 if ($arch->{type} == 22) { # map
80 my %normalize = (
81 "enter_x" => "hp",
82 "enter_y" => "sp",
83 "width" => "x",
84 "height" => "y",
85 "reset_timeout" => "weight",
86 "swap_time" => "value",
87 "difficulty" => "level",
88 "darkness" => "invisible",
89 "fixed_resettime" => "stand_still",
90 );
91
92 while (my ($k2, $k1) = each %normalize) {
93 if (defined (my $v = delete $ob->{$k1})) {
94 $ob->{$k2} = $v;
95 }
96 }
97 }
98
99 if (defined (my $v = delete $ob->{no_pass})) {
100 $ob->{move_block} = $v ? MOVE_ALL : 0;
101 }
102 if (defined (my $v = delete $ob->{walk_on})) {
103 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_WALK
104 : $ob->{move_on} & ~MOVE_WALK;
105 }
106 if (defined (my $v = delete $ob->{walk_off})) {
107 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_WALK
108 : $ob->{move_off} & ~MOVE_WALK;
109 }
110 if (defined (my $v = delete $ob->{fly_on})) {
111 $ob->{move_on} = $v ? $ob->{move_on} | MOVE_FLY_LOW
112 : $ob->{move_on} & ~MOVE_FLY_LOW;
113 }
114 if (defined (my $v = delete $ob->{fly_off})) {
115 $ob->{move_off} = $v ? $ob->{move_off} | MOVE_FLY_LOW
116 : $ob->{move_off} & ~MOVE_FLY_LOW;
117 }
118 if (defined (my $v = delete $ob->{flying})) {
119 $ob->{move_type} = $v ? $ob->{move_type} | MOVE_FLY_LOW
120 : $ob->{move_type} & ~MOVE_FLY_LOW;
121 }
122
123 # if value matches archetype default, delete
124 while (my ($k, $v) = each %$ob) {
125 if (exists $arch->{$k} and $arch->{$k} eq $v) {
126 next if $k eq "_name";
127 delete $ob->{$k};
128 }
129 }
130
131 $ob
132}
25 133
26sub read_pak($;$) { 134sub read_pak($;$) {
27 my ($path, $cache) = @_; 135 my ($path, $cache) = @_;
28 136
29 eval { 137 eval {
30 defined $cache 138 defined $cache
31 && -M $cache < -M $path 139 && -M $cache < -M $path
32 && Storable::retrieve $cache 140 && load_ref $cache
33 } or do { 141 } or do {
34 my %pak; 142 my %pak;
35 143
36 open my $fh, "<:raw", $path 144 open my $fh, "<", $path
37 or die "$_[0]: $!"; 145 or Carp::croak "$_[0]: $!";
146 binmode $fh;
38 while (<$fh>) { 147 while (<$fh>) {
39 my ($type, $id, $len, $path) = split; 148 my ($type, $id, $len, $path) = split;
40 $path =~ s/.*\///; 149 $path =~ s/.*\///;
41 read $fh, $pak{$path}, $len; 150 read $fh, $pak{$path}, $len;
42 } 151 }
43 152
44 Storable::nstore \%pak, $cache 153 save_ref \%pak, $cache
45 if defined $cache; 154 if defined $cache;
46 155
47 \%pak 156 \%pak
48 } 157 }
49} 158}
52 my ($path, $cache) = @_; 161 my ($path, $cache) = @_;
53 162
54 eval { 163 eval {
55 defined $cache 164 defined $cache
56 && -M $cache < -M $path 165 && -M $cache < -M $path
57 && Storable::retrieve $cache 166 && load_ref $cache
58 } or do { 167 } or do {
59 my %arc; 168 my %arc;
60 my ($more, $prev); 169 my ($more, $prev);
61 170
62 open my $fh, "<:raw", $path 171 open my $fh, "<", $path
63 or die "$path: $!"; 172 or Carp::croak "$path: $!";
173
174 binmode $fh;
64 175
65 my $parse_block; $parse_block = sub { 176 my $parse_block; $parse_block = sub {
66 my %arc = @_; 177 my %arc = @_;
67 178
68 while (<$fh>) { 179 while (<$fh>) {
69 s/\s+$//; 180 s/\s+$//;
70 if (/^end$/i) { 181 if (/^end$/i) {
71 last; 182 last;
72 } elsif (/^arch (\S+)$/) { 183 } elsif (/^arch (\S+)$/) {
73 push @{ $arc{inventory} }, $parse_block->(_name => $1); 184 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
74 } elsif (/^lore$/) { 185 } elsif (/^lore$/) {
75 while (<$fh>) { 186 while (<$fh>) {
76 last if /^endlore\s*$/i; 187 last if /^endlore\s*$/i;
77 $arc{lore} .= $_; 188 $arc{lore} .= $_;
78 } 189 }
107 $arc{$name} = $arc; 218 $arc{$name} = $arc;
108 } 219 }
109 $prev = $arc; 220 $prev = $arc;
110 $more = undef; 221 $more = undef;
111 } elsif (/^arch (\S+)$/i) { 222 } elsif (/^arch (\S+)$/i) {
112 push @{ $arc{arch} }, $parse_block->(_name => $1); 223 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
113 } elsif (/^\s*($|#)/) { 224 } elsif (/^\s*($|#)/) {
114 # 225 #
115 } else { 226 } else {
116 warn "$path: unparseable top-level line '$_'"; 227 warn "$path: unparseable top-level line '$_'";
117 } 228 }
118 } 229 }
119 230
120 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.
121 232
122 Storable::nstore \%arc, $cache 233 save_ref \%arc, $cache
123 if defined $cache; 234 if defined $cache;
124 235
125 \%arc 236 \%arc
126 } 237 }
127} 238}
128 239
129sub arch2map($;$) { 240# put all archs into a hash with editor_face as it's key
241# NOTE: the arrays in the hash values are references to
242# the archs from $ARCH
243sub editor_archs {
244 my %paths;
245
246 for (keys %ARCH) {
247 my $arch = $ARCH{$_};
248 push @{$paths{$arch->{editor_folder}}}, \$arch;
249 }
250
251 \%paths
252}
253
254=item ($minx, $miny, $maxx, $maxy) = arch_extents $arch
255
256arch_extents determines the extents of the given arch's face(s), linked
257faces and single faces are handled here it returns (minx, miny, maxx,
258maxy)
259
260=cut
261
262sub arch_extents {
130 my ($mapa) = @_; 263 my ($a) = @_;
131 264
132 my %meta; 265 my $o = $ARCH{$a->{_name}}
266 or return;
133 267
134 my ($mapx, $mapy); 268 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"}
269 or (warn "no face data found for arch '$a->{_name}'"), return;
135 270
136 my $map; 271 if ($face->{w} > 1 || $face->{h} > 1) {
272 # bigface
273 return (0, 0, $face->{w} - 1, $face->{h} - 1);
137 274
138 for (@{ $mapa->{arch} }) { 275 } elsif ($o->{more}) {
139 my ($x, $y) = ($_->{x}, $_->{y}); 276 # linked face
277 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
140 278
141 if ($_->{_name} eq "map") { 279 for (; $o; $o = $o->{more}) {
142 $meta{info} = $_; 280 $minx = min $minx, $o->{x};
281 $miny = min $miny, $o->{y};
282 $maxx = max $maxx, $o->{x};
283 $maxy = max $maxy, $o->{y};
284 }
143 285
144 $mapx = $_->{width} || $x; 286 return ($minx, $miny, $maxx, $maxy);
145 $mapy = $_->{height} || $y; 287
146 } else { 288 } else {
147 push @{ $map->[$x][$y] }, $_; 289 # single face
290 return (0, 0, 0, 0);
291 }
292}
148 293
149 # arch map is unreliable w.r.t. width and height 294=item $type = arch_attr $arch
150 $mapx = $x + 1 if $mapx <= $x; 295
151 $mapy = $y + 1 if $mapy <= $y; 296Returns a hashref describing the object and its attributes. It can contain
152 #$mapx = $a->{x} + 1, warn "$mapname: arch '$a->{_name}' outside map width at ($a->{x}|$a->{y})\n" if $mapx <= $a->{x}; 297the following keys:
153 #$mapy = $a->{y} + 1, warn "$mapname: arch '$a->{_name}' outside map height at ($a->{x}|$a->{y})\n" if $mapy <= $a->{y}; 298
299 name the name, suitable for display purposes
300 ignore
301 attr
302 desc
303 use
304 section => [name => \%attr, name => \%attr]
305 import
306
307=cut
308
309sub arch_attr($) {
310 my ($obj) = @_;
311
312 require Crossfire::Data;
313
314 my $root;
315
316 my $arch = $ARCH{ $obj->{_name} };
317 my $type = $obj->{type} || $arch->{type};
318
319 if ($type > 0) {
320 $root = $Crossfire::Data::ATTR{$type};
321 } else {
322 $root = $Crossfire::Data::TYPE{Misc};
323
324 type:
325 for (@Crossfire::Data::ATTR0) {
326 my $req = $_->{required}
327 or die "internal error: ATTR0 without 'required'";
328
329 keys %$req;
330 while (my ($k, $v) = each %$req) {
331 next type
332 unless $obj->{$k} eq $v || $arch->{$k} eq $v;
333 }
334
335 $root = $_;
336 }
337 }
338
339 my $attr = { };
340
341 my @import = ($root);
342
343 unshift @import, \%Crossfire::Data::DEFAULT_ATTR
344 unless $type == 116;
345
346 my (%ignore);
347 my (@section_order, %section, @attr_order);
348
349 while (my $type = shift @import) {
350 push @import, @{$type->{import} || []};
351
352 $attr->{$_} ||= $type->{$_}
353 for qw(name desc use);
354
355 for (@{$type->{ignore} || []}) {
356 $ignore{$_}++ for ref $_ ? @$_ : $_;
357 }
358
359 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) {
360 my ($name, $attr) = @$_;
361 push @section_order, $name;
362 for (@$attr) {
363 my ($k, $v) = @$_;
364 push @attr_order, $k;
365 $section{$name}{$k} ||= $v;
366 }
367 }
368 }
369
370 $attr->{section} = [
371 map !exists $section{$_} ? () : do {
372 my $attr = delete $section{$_};
373
374 [
375 $_,
376 map exists $attr->{$_} && !$ignore{$_}
377 ? [$_ => delete $attr->{$_}] : (),
378 @attr_order
379 ]
380 },
381
382 exists $section{$_} ? [$_ => delete $section{$_}] : (),
383 @section_order
384 ];
385
386 $attr
387}
388
389sub arch_edit_sections {
390# if (edit_type == IGUIConstants.TILE_EDIT_NONE)
391# edit_type = 0;
392# else if (edit_type != 0) {
393# // all flags from 'check_type' must be unset in this arch because they get recalculated now
394# edit_type &= ~check_type;
154 } 395# }
155 } 396#
156 397# }
157 $meta{width} = $mapx; 398# if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 &&
158 $meta{height} = $mapy; 399# getAttributeValue("alive", defarch) == 1 &&
159 400# (getAttributeValue("monster", defarch) == 1 ||
160 \%meta 401# getAttributeValue("generator", defarch) == 1)) {
402# // Monster: monsters/npcs/generators
403# edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
404# }
405# if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
406# arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
407# // Walls
408# edit_type |= IGUIConstants.TILE_EDIT_WALL;
409# }
410# if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
411# getAttributeValue("connected", defarch) != 0) {
412# // Connected Objects
413# edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
414# }
415# if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
416# arch_type == 66 || arch_type == 41 || arch_type == 95) {
417# // Exit: teleporter/exit/trapdoors
418# edit_type |= IGUIConstants.TILE_EDIT_EXIT;
419# }
420# if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
421# getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
422# arch_type == 5 || arch_type == 36 || arch_type == 60 ||
423# arch_type == 85 || arch_type == 111 || arch_type == 123 ||
424# arch_type == 124 || arch_type == 130)) {
425# // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
426# edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
427# }
428# if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
429# arch_type == 20 || arch_type == 23 || arch_type == 26 ||
430# arch_type == 91 || arch_type == 21 || arch_type == 24) {
431# // Door: door/special door/gates + keys
432# edit_type |= IGUIConstants.TILE_EDIT_DOOR;
433# }
434# if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
435# getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
436# arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
437# arch_type == 35 || arch_type == 39 || arch_type == 70 ||
438# arch_type == 87 || arch_type == 99 || arch_type == 100 ||
439# arch_type == 104 || arch_type == 109 || arch_type == 113 ||
440# arch_type == 122 || arch_type == 3)) {
441# // Equipment: weapons/armour/wands/rods
442# edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
443# }
444#
445# return(edit_type);
446#
447#
161} 448}
162 449
163sub init($) { 450sub init($) {
164 my ($cachedir) = @_; 451 my ($cachedir) = @_;
165 452
453 return if %ARCH;
454
455 mkdir $cachedir, 0777;
166 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 456 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
167} 457}
458
459$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
460
461init $VARDIR;
168 462
169=head1 AUTHOR 463=head1 AUTHOR
170 464
171 Marc Lehmann <schmorp@schmorp.de> 465 Marc Lehmann <schmorp@schmorp.de>
172 http://home.schmorp.de/ 466 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines