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