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