ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
Revision: 1.19
Committed: Wed Feb 22 23:02:59 2006 UTC (18 years, 2 months ago) by root
Branch: MAIN
Changes since 1.18: +4 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Crossfire - Crossfire maphandling
4
5 =cut
6
7 package Crossfire;
8
9 our $VERSION = '0.1';
10
11 use strict;
12
13 use base 'Exporter';
14
15 use Carp ();
16 use Storable;
17 use List::Util qw(min max);
18
19 #XXX: The map_* procedures scream for a map-object
20
21 our @EXPORT =
22 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents);
23
24 our $LIB = $ENV{CROSSFIRE_LIBDIR}
25 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
26
27 sub TILESIZE (){ 32 }
28
29 our $CACHEDIR;
30 our %ARCH;
31 our %FACE;
32 our $TILE;
33
34 our %FIELD_MULTILINE = (
35 msg => "endmsg",
36 lore => "endlore",
37 );
38
39 # not used yet, maybe alphabetical is ok
40 our @FIELD_ORDER = (qw(name name_pl));
41
42 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 my $arch = $ARCH{$ob->{_name}}
53 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 next if $k eq "_name";
105 delete $ob->{$k};
106 }
107 }
108
109 $ob
110 }
111
112 sub read_pak($;$) {
113 my ($path, $cache) = @_;
114
115 eval {
116 defined $cache
117 && -M $cache < -M $path
118 && Storable::retrieve $cache
119 } or do {
120 my %pak;
121
122 open my $fh, "<:raw", $path
123 or Carp::croak "$_[0]: $!";
124 while (<$fh>) {
125 my ($type, $id, $len, $path) = split;
126 $path =~ s/.*\///;
127 read $fh, $pak{$path}, $len;
128 }
129
130 Storable::nstore \%pak, $cache
131 if defined $cache;
132
133 \%pak
134 }
135 }
136
137 sub read_arch($;$) {
138 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 or Carp::croak "$path: $!";
150
151 my $parse_block; $parse_block = sub {
152 my %arc = @_;
153
154 while (<$fh>) {
155 s/\s+$//;
156 if (/^end$/i) {
157 last;
158 } elsif (/^arch (\S+)$/) {
159 push @{ $arc{inventory} }, normalize_arch $parse_block->(_name => $1);
160 } 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
179 \%arc
180 };
181
182 while (<$fh>) {
183 s/\s+$//;
184 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 }
195 $prev = $arc;
196 $more = undef;
197 } elsif (/^arch (\S+)$/i) {
198 push @{ $arc{arch} }, normalize_arch $parse_block->(_name => $1);
199 } elsif (/^\s*($|#)/) {
200 #
201 } else {
202 warn "$path: unparseable top-level line '$_'";
203 }
204 }
205
206 undef $parse_block; # work around bug in perl not freeing $fh etc.
207
208 Storable::nstore \%arc, $cache
209 if defined $cache;
210
211 \%arc
212 }
213 }
214
215 # 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 for (keys %ARCH) {
222 my $arch = $ARCH{$_};
223 push @{$paths{$arch->{editor_folder}}}, \$arch;
224 }
225
226 \%paths
227 }
228
229 =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 sub arch_extents {
238 my ($a) = @_;
239
240 my $o = $ARCH{$a->{_name}}
241 or return;
242
243 my $face = $FACE{$a->{face} || $o->{face}}
244 or (warn "no face data found for arch '$a->{_name}'"), return;
245
246 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
263 } else {
264 # single face
265 return (0, 0, 0, 0);
266 }
267 }
268
269 sub init($) {
270 my ($cachedir) = @_;
271
272 return if %ARCH;
273
274 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
275 }
276
277 =item $type = 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 import
289
290 =cut
291
292 sub arch_attr($) {
293 my ($arch) = @_;
294
295 require Crossfire::Data;
296
297 my $attr;
298
299 if ($arch->{type} > 0) {
300 $attr = $Crossfire::Data::ATTR{$arch->{type}+0};
301 } else {
302 $attr = $Crossfire::Data::TYPE{Misc};
303
304 type:
305 for (@Crossfire::Data::ATTR0) {
306 my $req = $_->{required}
307 or die "internal error: ATTR0 without 'required'";
308
309 while (my ($k, $v) = each %$req) {
310 next type
311 unless $arch->{$k} == $v;
312 }
313
314 $attr = $_;
315 }
316 }
317
318 use PApp::Util;
319 warn PApp::Util::dumpval $attr;
320
321 $attr || \%Crossfire::Data::DEFAULT_ATTR;
322 }
323
324 sub arch_edit_sections {
325 # if (edit_type == IGUIConstants.TILE_EDIT_NONE)
326 # edit_type = 0;
327 # else if (edit_type != 0) {
328 # // all flags from 'check_type' must be unset in this arch because they get recalculated now
329 # edit_type &= ~check_type;
330 # }
331 #
332 # }
333 # if ((check_type & IGUIConstants.TILE_EDIT_MONSTER) != 0 &&
334 # getAttributeValue("alive", defarch) == 1 &&
335 # (getAttributeValue("monster", defarch) == 1 ||
336 # getAttributeValue("generator", defarch) == 1)) {
337 # // Monster: monsters/npcs/generators
338 # edit_type |= IGUIConstants.TILE_EDIT_MONSTER;
339 # }
340 # if ((check_type & IGUIConstants.TILE_EDIT_WALL) != 0 &&
341 # arch_type == 0 && getAttributeValue("no_pass", defarch) == 1) {
342 # // Walls
343 # edit_type |= IGUIConstants.TILE_EDIT_WALL;
344 # }
345 # if ((check_type & IGUIConstants.TILE_EDIT_CONNECTED) != 0 &&
346 # getAttributeValue("connected", defarch) != 0) {
347 # // Connected Objects
348 # edit_type |= IGUIConstants.TILE_EDIT_CONNECTED;
349 # }
350 # if ((check_type & IGUIConstants.TILE_EDIT_EXIT) != 0 &&
351 # arch_type == 66 || arch_type == 41 || arch_type == 95) {
352 # // Exit: teleporter/exit/trapdoors
353 # edit_type |= IGUIConstants.TILE_EDIT_EXIT;
354 # }
355 # if ((check_type & IGUIConstants.TILE_EDIT_TREASURE) != 0 &&
356 # getAttributeValue("no_pick", defarch) == 0 && (arch_type == 4 ||
357 # arch_type == 5 || arch_type == 36 || arch_type == 60 ||
358 # arch_type == 85 || arch_type == 111 || arch_type == 123 ||
359 # arch_type == 124 || arch_type == 130)) {
360 # // Treasure: randomtreasure/money/gems/potions/spellbooks/scrolls
361 # edit_type |= IGUIConstants.TILE_EDIT_TREASURE;
362 # }
363 # if ((check_type & IGUIConstants.TILE_EDIT_DOOR) != 0 &&
364 # arch_type == 20 || arch_type == 23 || arch_type == 26 ||
365 # arch_type == 91 || arch_type == 21 || arch_type == 24) {
366 # // Door: door/special door/gates + keys
367 # edit_type |= IGUIConstants.TILE_EDIT_DOOR;
368 # }
369 # if ((check_type & IGUIConstants.TILE_EDIT_EQUIP) != 0 &&
370 # getAttributeValue("no_pick", defarch) == 0 && ((arch_type >= 13 &&
371 # arch_type <= 16) || arch_type == 33 || arch_type == 34 ||
372 # arch_type == 35 || arch_type == 39 || arch_type == 70 ||
373 # arch_type == 87 || arch_type == 99 || arch_type == 100 ||
374 # arch_type == 104 || arch_type == 109 || arch_type == 113 ||
375 # arch_type == 122 || arch_type == 3)) {
376 # // Equipment: weapons/armour/wands/rods
377 # edit_type |= IGUIConstants.TILE_EDIT_EQUIP;
378 # }
379 #
380 # return(edit_type);
381 #
382 #
383 }
384
385 $CACHEDIR ||= "$ENV{HOME}/.crossfire";
386
387 init $CACHEDIR;
388
389 =head1 AUTHOR
390
391 Marc Lehmann <schmorp@schmorp.de>
392 http://home.schmorp.de/
393
394 Robin Redeker <elmex@ta-sa.org>
395 http://www.ta-sa.org/
396
397 =cut
398
399 1