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