ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
Revision: 1.39
Committed: Sun Mar 12 23:35:03 2006 UTC (18 years, 2 months ago) by root
Branch: MAIN
Changes since 1.38: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 =head1 NAME
2    
3     Crossfire - Crossfire maphandling
4    
5     =cut
6    
7 root 1.4 package Crossfire;
8    
9 elmex 1.3 our $VERSION = '0.1';
10 elmex 1.1
11     use strict;
12    
13 root 1.7 use base 'Exporter';
14    
15 root 1.13 use Carp ();
16 root 1.21 use File::Spec;
17 root 1.15 use List::Util qw(min max);
18 root 1.34 use Storable;
19 elmex 1.1
20 elmex 1.11 our @EXPORT =
21 root 1.15 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents);
22 root 1.7
23 root 1.4 our $LIB = $ENV{CROSSFIRE_LIBDIR}
24 root 1.13 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
25 elmex 1.1
26 root 1.7 sub TILESIZE (){ 32 }
27 elmex 1.1
28 root 1.25 our $VARDIR;
29 root 1.15 our %ARCH;
30     our %FACE;
31     our $TILE;
32 elmex 1.1
33 root 1.13 our %FIELD_MULTILINE = (
34 root 1.14 msg => "endmsg",
35     lore => "endlore",
36 root 1.13 );
37    
38     # not used yet, maybe alphabetical is ok
39     our @FIELD_ORDER = (qw(name name_pl));
40    
41 root 1.14 sub MOVE_WALK (){ 0x1 }
42     sub MOVE_FLY_LOW (){ 0x2 }
43     sub MOVE_FLY_HIGH (){ 0x4 }
44     sub MOVE_FLYING (){ 0x6 }
45     sub MOVE_SWIM (){ 0x8 }
46     sub MOVE_ALL (){ 0xf }
47    
48 root 1.28 sub load_ref($) {
49     my ($path) = @_;
50    
51 root 1.31 open my $fh, "<", $path
52     or die "$path: $!";
53     binmode $fh;
54 root 1.28 local $/;
55 root 1.33
56 root 1.34 Storable::thaw <$fh>
57 root 1.28 }
58    
59     sub save_ref($$) {
60     my ($ref, $path) = @_;
61    
62 root 1.31 open my $fh, ">", "$path~"
63 root 1.28 or die "$path~: $!";
64 root 1.31 binmode $fh;
65 root 1.34 print $fh Storable::freeze $ref;
66 root 1.28 close $fh;
67     rename "$path~", $path
68     or die "$path: $!";
69     }
70    
71 root 1.14 sub normalize_arch($) {
72     my ($ob) = @_;
73    
74 root 1.15 my $arch = $ARCH{$ob->{_name}}
75 root 1.14 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 root 1.15 next if $k eq "_name";
127 root 1.14 delete $ob->{$k};
128     }
129     }
130    
131     $ob
132     }
133 root 1.13
134 root 1.4 sub read_pak($;$) {
135     my ($path, $cache) = @_;
136 elmex 1.1
137     eval {
138 root 1.4 defined $cache
139     && -M $cache < -M $path
140 root 1.28 && load_ref $cache
141 elmex 1.1 } or do {
142     my %pak;
143    
144 root 1.31 open my $fh, "<", $path
145 root 1.13 or Carp::croak "$_[0]: $!";
146 root 1.31 binmode $fh;
147 elmex 1.1 while (<$fh>) {
148     my ($type, $id, $len, $path) = split;
149     $path =~ s/.*\///;
150     read $fh, $pak{$path}, $len;
151     }
152    
153 root 1.28 save_ref \%pak, $cache
154 root 1.4 if defined $cache;
155 elmex 1.1
156     \%pak
157     }
158     }
159    
160     sub read_arch($;$) {
161 root 1.4 my ($path, $cache) = @_;
162    
163     eval {
164     defined $cache
165     && -M $cache < -M $path
166 root 1.28 && load_ref $cache
167 root 1.4 } or do {
168     my %arc;
169     my ($more, $prev);
170    
171 root 1.31 open my $fh, "<", $path
172 root 1.13 or Carp::croak "$path: $!";
173 elmex 1.1
174 root 1.31 binmode $fh;
175    
176 root 1.4 my $parse_block; $parse_block = sub {
177     my %arc = @_;
178 elmex 1.2
179 root 1.4 while (<$fh>) {
180     s/\s+$//;
181     if (/^end$/i) {
182     last;
183     } elsif (/^arch (\S+)$/) {
184 root 1.14 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
185 root 1.4 } elsif (/^lore$/) {
186     while (<$fh>) {
187     last if /^endlore\s*$/i;
188     $arc{lore} .= $_;
189     }
190     } elsif (/^msg$/) {
191     while (<$fh>) {
192     last if /^endmsg\s*$/i;
193     $arc{msg} .= $_;
194     }
195     } elsif (/^(\S+)\s*(.*)$/) {
196     $arc{lc $1} = $2;
197     } elsif (/^\s*($|#)/) {
198     #
199     } else {
200     warn "$path: unparsable line '$_' in arch $arc{_name}";
201     }
202     }
203 elmex 1.1
204 root 1.4 \%arc
205     };
206 elmex 1.1
207     while (<$fh>) {
208     s/\s+$//;
209 root 1.4 if (/^more$/i) {
210     $more = $prev;
211     } elsif (/^object (\S+)$/i) {
212     my $name = $1;
213     my $arc = $parse_block->(_name => $name);
214    
215     if ($more) {
216     $more->{more} = $arc;
217     } else {
218     $arc{$name} = $arc;
219 elmex 1.1 }
220 root 1.4 $prev = $arc;
221     $more = undef;
222     } elsif (/^arch (\S+)$/i) {
223 root 1.14 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
224 elmex 1.1 } elsif (/^\s*($|#)/) {
225     #
226     } else {
227 root 1.4 warn "$path: unparseable top-level line '$_'";
228 elmex 1.1 }
229     }
230    
231 root 1.4 undef $parse_block; # work around bug in perl not freeing $fh etc.
232 elmex 1.2
233 root 1.28 save_ref \%arc, $cache
234 root 1.4 if defined $cache;
235 elmex 1.1
236 root 1.4 \%arc
237 elmex 1.2 }
238 elmex 1.1 }
239    
240 elmex 1.10 # 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
243     sub editor_archs {
244     my %paths;
245    
246 root 1.15 for (keys %ARCH) {
247     my $arch = $ARCH{$_};
248 elmex 1.10 push @{$paths{$arch->{editor_folder}}}, \$arch;
249     }
250    
251 root 1.15 \%paths
252 elmex 1.10 }
253    
254 root 1.17 =item ($minx, $miny, $maxx, $maxy) = arch_extents $arch
255    
256     arch_extents determines the extents of the given arch's face(s), linked
257     faces and single faces are handled here it returns (minx, miny, maxx,
258     maxy)
259    
260     =cut
261    
262 root 1.15 sub arch_extents {
263 elmex 1.10 my ($a) = @_;
264    
265 root 1.15 my $o = $ARCH{$a->{_name}}
266     or return;
267 elmex 1.10
268 root 1.15 my $face = $FACE{$a->{face} || $o->{face}}
269     or (warn "no face data found for arch '$a->{_name}'"), return;
270 elmex 1.10
271 root 1.15 if ($face->{w} > 1 || $face->{h} > 1) {
272     # bigface
273     return (0, 0, $face->{w} - 1, $face->{h} - 1);
274    
275     } elsif ($o->{more}) {
276     # linked face
277     my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
278    
279     for (; $o; $o = $o->{more}) {
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     }
285    
286     return ($minx, $miny, $maxx, $maxy);
287 elmex 1.10
288     } else {
289     # single face
290 root 1.15 return (0, 0, 0, 0);
291 elmex 1.10 }
292     }
293    
294 root 1.19 =item $type = arch_attr $arch
295 root 1.17
296     Returns a hashref describing the object and its attributes. It can contain
297     the following keys:
298    
299     name the name, suitable for display purposes
300     ignore
301     attr
302     desc
303     use
304     section => [name => \%attr, name => \%attr]
305 root 1.19 import
306 root 1.17
307     =cut
308    
309     sub arch_attr($) {
310     my ($arch) = @_;
311    
312     require Crossfire::Data;
313    
314 root 1.36 my $root;
315 root 1.17
316     if ($arch->{type} > 0) {
317 root 1.36 $root = $Crossfire::Data::ATTR{$arch->{type}+0};
318 root 1.17 } else {
319 root 1.36 $root = $Crossfire::Data::TYPE{Misc};
320 root 1.18
321     type:
322     for (@Crossfire::Data::ATTR0) {
323     my $req = $_->{required}
324     or die "internal error: ATTR0 without 'required'";
325    
326 root 1.35 keys %$req;
327 root 1.18 while (my ($k, $v) = each %$req) {
328     next type
329     unless $arch->{$k} == $v;
330     }
331    
332 root 1.36 $root = $_;
333 root 1.18 }
334 root 1.17 }
335    
336 root 1.36 my $attr = { };
337    
338 root 1.39 my @import = ($root, \%Crossfire::Data::DEFAULT_ATTR);
339 root 1.36 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 root 1.38 map exists $attr->{$_} && !$ignore{$_}
370     ? [$_ => delete $attr->{$_}] : (),
371 root 1.36 @attr_order
372     ]
373     },
374    
375     exists $section{$_} ? [$_ => delete $section{$_}] : (),
376     @section_order
377     ];
378    
379     use PApp::Util;
380     warn PApp::Util::dumpval $attr;
381    
382     $attr
383 root 1.17 }
384    
385 root 1.16 sub arch_edit_sections {
386     # if (edit_type == IGUIConstants.TILE_EDIT_NONE)
387     # edit_type = 0;
388     # else if (edit_type != 0) {
389     # // all flags from 'check_type' must be unset in this arch because they get recalculated now
390     # edit_type &= ~check_type;
391     # }
392     #
393     # }
394     # if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 &&
395     # getAttributeValue("alive", defarch) == 1 &&
396     # (getAttributeValue("monster", defarch) == 1 ||
397     # getAttributeValue("generator", defarch) == 1)) {
398     # // Monster: monsters/npcs/generators
399     # edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
400     # }
401     # if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
402     # arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
403     # // Walls
404     # edit_type |= IGUIConstants.TILE_EDIT_WALL;
405     # }
406     # if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
407     # getAttributeValue("connected", defarch) != 0) {
408     # // Connected Objects
409     # edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
410     # }
411     # if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
412     # arch_type == 66 || arch_type == 41 || arch_type == 95) {
413     # // Exit: teleporter/exit/trapdoors
414     # edit_type |= IGUIConstants.TILE_EDIT_EXIT;
415     # }
416     # if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
417     # getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
418     # arch_type == 5 || arch_type == 36 || arch_type == 60 ||
419     # arch_type == 85 || arch_type == 111 || arch_type == 123 ||
420     # arch_type == 124 || arch_type == 130)) {
421     # // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
422     # edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
423     # }
424     # if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
425     # arch_type == 20 || arch_type == 23 || arch_type == 26 ||
426     # arch_type == 91 || arch_type == 21 || arch_type == 24) {
427     # // Door: door/special door/gates + keys
428     # edit_type |= IGUIConstants.TILE_EDIT_DOOR;
429     # }
430     # if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
431     # getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
432     # arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
433     # arch_type == 35 || arch_type == 39 || arch_type == 70 ||
434     # arch_type == 87 || arch_type == 99 || arch_type == 100 ||
435     # arch_type == 104 || arch_type == 109 || arch_type == 113 ||
436     # arch_type == 122 || arch_type == 3)) {
437     # // Equipment: weapons/armour/wands/rods
438     # edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
439     # }
440     #
441     # return(edit_type);
442     #
443     #
444     }
445    
446 root 1.24 sub init($) {
447     my ($cachedir) = @_;
448    
449     return if %ARCH;
450    
451     mkdir $cachedir, 0777;
452     *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
453     }
454    
455 root 1.29 $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
456 root 1.15
457 root 1.25 init $VARDIR;
458 root 1.15
459 elmex 1.1 =head1 AUTHOR
460    
461     Marc Lehmann <schmorp@schmorp.de>
462     http://home.schmorp.de/
463    
464     Robin Redeker <elmex@ta-sa.org>
465     http://www.ta-sa.org/
466    
467     =cut
468 root 1.4
469     1