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.6 by elmex, Sun Feb 5 00:28:09 2006 UTC vs.
Revision 1.37 by root, Sun Mar 12 23:21:53 2006 UTC

5=cut 5=cut
6 6
7package Crossfire; 7package Crossfire;
8 8
9our $VERSION = '0.1'; 9our $VERSION = '0.1';
10
11use strict;
12
10use base 'Exporter'; 13use base 'Exporter';
11our @EXPORT = qw(read_pak read_arch arch2map);
12 14
13use strict; 15use Carp ();
14 16use File::Spec;
17use List::Util qw(min max);
15use Storable; 18use Storable;
16 19
20our @EXPORT =
21 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents);
22
17our $LIB = $ENV{CROSSFIRE_LIBDIR} 23our $LIB = $ENV{CROSSFIRE_LIBDIR}
18 or die "\$CROSSFIRE_LIBDIR must be set\n"; 24 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
19 25
20sub T (){ 32 } 26sub TILESIZE (){ 32 }
21 27
28our $VARDIR;
22our $ARCH; 29our %ARCH;
30our %FACE;
23our $TILE; 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}
24 133
25sub read_pak($;$) { 134sub read_pak($;$) {
26 my ($path, $cache) = @_; 135 my ($path, $cache) = @_;
27 136
28 eval { 137 eval {
29 defined $cache 138 defined $cache
30 && -M $cache < -M $path 139 && -M $cache < -M $path
31 && Storable::retrieve $cache 140 && load_ref $cache
32 } or do { 141 } or do {
33 my %pak; 142 my %pak;
34 143
35 open my $fh, "<:raw", $path 144 open my $fh, "<", $path
36 or die "$_[0]: $!"; 145 or Carp::croak "$_[0]: $!";
146 binmode $fh;
37 while (<$fh>) { 147 while (<$fh>) {
38 my ($type, $id, $len, $path) = split; 148 my ($type, $id, $len, $path) = split;
39 $path =~ s/.*\///; 149 $path =~ s/.*\///;
40 read $fh, $pak{$path}, $len; 150 read $fh, $pak{$path}, $len;
41 } 151 }
42 152
43 Storable::nstore \%pak, $cache 153 save_ref \%pak, $cache
44 if defined $cache; 154 if defined $cache;
45 155
46 \%pak 156 \%pak
47 } 157 }
48} 158}
51 my ($path, $cache) = @_; 161 my ($path, $cache) = @_;
52 162
53 eval { 163 eval {
54 defined $cache 164 defined $cache
55 && -M $cache < -M $path 165 && -M $cache < -M $path
56 && Storable::retrieve $cache 166 && load_ref $cache
57 } or do { 167 } or do {
58 my %arc; 168 my %arc;
59 my ($more, $prev); 169 my ($more, $prev);
60 170
61 open my $fh, "<:raw", $path 171 open my $fh, "<", $path
62 or die "$path: $!"; 172 or Carp::croak "$path: $!";
173
174 binmode $fh;
63 175
64 my $parse_block; $parse_block = sub { 176 my $parse_block; $parse_block = sub {
65 my %arc = @_; 177 my %arc = @_;
66 178
67 while (<$fh>) { 179 while (<$fh>) {
68 s/\s+$//; 180 s/\s+$//;
69 if (/^end$/i) { 181 if (/^end$/i) {
70 last; 182 last;
71 } elsif (/^arch (\S+)$/) { 183 } elsif (/^arch (\S+)$/) {
72 push @{ $arc{inventory} }, $parse_block->(_name => $1); 184 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
73 } elsif (/^lore$/) { 185 } elsif (/^lore$/) {
74 while (<$fh>) { 186 while (<$fh>) {
75 last if /^endlore\s*$/i; 187 last if /^endlore\s*$/i;
76 $arc{lore} .= $_; 188 $arc{lore} .= $_;
77 } 189 }
106 $arc{$name} = $arc; 218 $arc{$name} = $arc;
107 } 219 }
108 $prev = $arc; 220 $prev = $arc;
109 $more = undef; 221 $more = undef;
110 } elsif (/^arch (\S+)$/i) { 222 } elsif (/^arch (\S+)$/i) {
111 push @{ $arc{arch} }, $parse_block->(_name => $1); 223 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
112 } elsif (/^\s*($|#)/) { 224 } elsif (/^\s*($|#)/) {
113 # 225 #
114 } else { 226 } else {
115 warn "$path: unparseable top-level line '$_'"; 227 warn "$path: unparseable top-level line '$_'";
116 } 228 }
117 } 229 }
118 230
119 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.
120 232
121 Storable::nstore \%arc, $cache 233 save_ref \%arc, $cache
122 if defined $cache; 234 if defined $cache;
123 235
124 \%arc 236 \%arc
125 } 237 }
126} 238}
127 239
128sub 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 {
129 my ($mapa) = @_; 263 my ($a) = @_;
130 264
131 my %meta; 265 my $o = $ARCH{$a->{_name}}
266 or return;
132 267
133 my ($mapx, $mapy); 268 my $face = $FACE{$a->{face} || $o->{face}}
269 or (warn "no face data found for arch '$a->{_name}'"), return;
134 270
135 my $map; 271 if ($face->{w} > 1 || $face->{h} > 1) {
272 # bigface
273 return (0, 0, $face->{w} - 1, $face->{h} - 1);
136 274
137 for (@{ $mapa->{arch} }) { 275 } elsif ($o->{more}) {
138 my ($x, $y) = ($_->{x}, $_->{y}); 276 # linked face
277 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
139 278
140 if ($_->{_name} eq "map") { 279 for (; $o; $o = $o->{more}) {
141 $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 }
142 285
143 $mapx = $_->{width} || $x; 286 return ($minx, $miny, $maxx, $maxy);
144 $mapy = $_->{height} || $y; 287
145 } else { 288 } else {
146 push @{ $map->[$x][$y] }, $_; 289 # single face
290 return (0, 0, 0, 0);
291 }
292}
147 293
148 # arch map is unreliable w.r.t. width and height 294=item $type = arch_attr $arch
149 $mapx = $x + 1 if $mapx <= $x; 295
150 $mapy = $y + 1 if $mapy <= $y; 296Returns a hashref describing the object and its attributes. It can contain
151 #$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:
152 #$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 ($arch) = @_;
311
312 require Crossfire::Data;
313
314 my $root;
315
316 if ($arch->{type} > 0) {
317 $root = $Crossfire::Data::ATTR{$arch->{type}+0};
318 } else {
319 $root = $Crossfire::Data::TYPE{Misc};
320
321 type:
322 for (@Crossfire::Data::ATTR0) {
323 my $req = $_->{required}
324 or die "internal error: ATTR0 without 'required'";
325
326 keys %$req;
327 while (my ($k, $v) = each %$req) {
328 next type
329 unless $arch->{$k} == $v;
330 }
331
332 $root = $_;
333 }
334 }
335
336 my $attr = { };
337
338 my @import = $root || \%Crossfire::Data::DEFAULT_ATTR;
339 my (%ignore);
340 my (@section_order, %section, @attr_order);
341
342 while (my $type = shift @import) {
343 push @import, @{$type->{import} || []};
344
345 $attr->{$_} ||= $type->{$_}
346 for qw(name desc use);
347
348 for (@{$type->{ignore} || []}) {
349 $ignore{$_}++ for ref $_ ? @$_ : $_;
350 }
351
352 for ([general => ($type->{attr} || {})], @{$type->{section} || []}) {
353 my ($name, $attr) = @$_;
354 push @section_order, $name;
355 for (@$attr) {
356 my ($k, $v) = @$_;
357 push @attr_order, $k;
358 $section{$name}{$k} ||= $v;
359 }
360 }
361 }
362
363 $attr->{section} = [
364 map !exists $section{$_} ? () : do {
365 my $attr = delete $section{$_};
366
367 [
368 $_,
369 map exists $attr->{$_} && !ignore{$_} ? [$_ => delete $attr->{$_}] : (),
370 @attr_order
371 ]
372 },
373
374 exists $section{$_} ? [$_ => delete $section{$_}] : (),
375 @section_order
376 ];
377
378 use PApp::Util;
379 warn PApp::Util::dumpval $attr;
380
381 $attr
382}
383
384sub arch_edit_sections {
385# if (edit_type == IGUIConstants.TILE_EDIT_NONE)
386# edit_type = 0;
387# else if (edit_type != 0) {
388# // all flags from 'check_type' must be unset in this arch because they get recalculated now
389# edit_type &= ~check_type;
153 } 390# }
154 } 391#
155 392# }
156 $meta{width} = $mapx; 393# if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 &&
157 $meta{height} = $mapy; 394# getAttributeValue("alive", defarch) == 1 &&
158 395# (getAttributeValue("monster", defarch) == 1 ||
159 \%meta 396# getAttributeValue("generator", defarch) == 1)) {
397# // Monster: monsters/npcs/generators
398# edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
399# }
400# if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
401# arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
402# // Walls
403# edit_type |= IGUIConstants.TILE_EDIT_WALL;
404# }
405# if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
406# getAttributeValue("connected", defarch) != 0) {
407# // Connected Objects
408# edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
409# }
410# if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
411# arch_type == 66 || arch_type == 41 || arch_type == 95) {
412# // Exit: teleporter/exit/trapdoors
413# edit_type |= IGUIConstants.TILE_EDIT_EXIT;
414# }
415# if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
416# getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
417# arch_type == 5 || arch_type == 36 || arch_type == 60 ||
418# arch_type == 85 || arch_type == 111 || arch_type == 123 ||
419# arch_type == 124 || arch_type == 130)) {
420# // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
421# edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
422# }
423# if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
424# arch_type == 20 || arch_type == 23 || arch_type == 26 ||
425# arch_type == 91 || arch_type == 21 || arch_type == 24) {
426# // Door: door/special door/gates + keys
427# edit_type |= IGUIConstants.TILE_EDIT_DOOR;
428# }
429# if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
430# getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
431# arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
432# arch_type == 35 || arch_type == 39 || arch_type == 70 ||
433# arch_type == 87 || arch_type == 99 || arch_type == 100 ||
434# arch_type == 104 || arch_type == 109 || arch_type == 113 ||
435# arch_type == 122 || arch_type == 3)) {
436# // Equipment: weapons/armour/wands/rods
437# edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
438# }
439#
440# return(edit_type);
441#
442#
160} 443}
161 444
162sub init($) { 445sub init($) {
163 my ($cachedir) = @_; 446 my ($cachedir) = @_;
164 447
448 return if %ARCH;
449
450 mkdir $cachedir, 0777;
165 $ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 451 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
166 $TILE = read_pak "$LIB/crossfire.0", "$cachedir/crossfire.0.pst";
167} 452}
453
454$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
455
456init $VARDIR;
168 457
169=head1 AUTHOR 458=head1 AUTHOR
170 459
171 Marc Lehmann <schmorp@schmorp.de> 460 Marc Lehmann <schmorp@schmorp.de>
172 http://home.schmorp.de/ 461 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines