ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
Revision: 1.101
Committed: Tue Apr 10 09:37:03 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
Changes since 1.100: +3 -3 lines
Log Message:
implement support for sx command and new-style smoothing

File Contents

# Content
1 =head1 NAME
2
3 Crossfire - Crossfire maphandling
4
5 =cut
6
7 package Crossfire;
8
9 our $VERSION = '0.98';
10
11 use strict;
12
13 use base 'Exporter';
14
15 use Carp ();
16 use File::Spec;
17 use List::Util qw(min max);
18 use Storable qw(freeze thaw);
19
20 our @EXPORT = qw(
21 read_pak read_arch *ARCH TILESIZE $TILE *FACE editor_archs arch_extents
22 );
23
24 use JSON::XS qw(from_json to_json);
25
26 our $LIB = $ENV{CROSSFIRE_LIBDIR};
27
28 our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire"
29 : $ENV{AppData} ? "$ENV{APPDATA}/crossfire"
30 : File::Spec->tmpdir . "/crossfire";
31
32 mkdir $VARDIR, 0777;
33
34 sub TILESIZE (){ 32 }
35
36 our %ARCH;
37 our %FACE;
38 our $TILE;
39
40 our %FIELD_MULTILINE = (
41 msg => "endmsg",
42 lore => "endlore",
43 maplore => "endmaplore",
44 );
45
46 # movement bit type, PITA
47 our %FIELD_MOVEMENT = map +($_ => undef),
48 qw(move_type move_block move_allow move_on move_off move_slow);
49
50 # same as in server save routine, to (hopefully) be compatible
51 # to the other editors.
52 our @FIELD_ORDER_MAP = (qw(
53 file_format_version
54 name attach swap_time reset_timeout fixed_resettime difficulty region
55 shopitems shopgreed shopmin shopmax shoprace
56 darkness width height enter_x enter_y msg maplore
57 unique template
58 outdoor temp pressure humid windspeed winddir sky nosmooth
59 tile_path_1 tile_path_2 tile_path_3 tile_path_4
60 ));
61
62 our @FIELD_ORDER = (qw(
63 elevation
64
65 name name_pl custom_name attach title race
66 slaying skill msg lore other_arch
67 is_animated animation face
68 magicmap smoothlevel smoothface
69 str dex con wis pow cha int
70 hp maxhp sp maxsp grace maxgrace
71 exp perm_exp expmul
72 food dam luck wc ac x y speed speed_left move_state attack_movement
73 nrof level direction type subtype attacktype
74
75 resist_physical resist_magic resist_fire resist_electricity
76 resist_cold resist_confusion resist_acid resist_drain
77 resist_weaponmagic resist_ghosthit resist_poison resist_slow
78 resist_paralyze resist_turn_undead resist_fear resist_cancellation
79 resist_deplete resist_death resist_chaos resist_counterspell
80 resist_godpower resist_holyword resist_blind resist_internal
81 resist_life_stealing resist_disease
82
83 path_attuned path_repelled path_denied material materialname
84 value carrying weight invisible state magic
85 last_heal last_sp last_grace last_eat
86 connected glow_radius randomitems npx_status npc_program
87 run_away pick_up container will_apply smoothlevel
88 current_weapon_script weapontype tooltype elevation client_type
89 item_power duration range
90 range_modifier duration_modifier dam_modifier gen_sp_armour
91 move_type move_block move_allow move_on move_off move_on move_slow move_slow_penalty
92
93 alive wiz was_wiz applied unpaid can_use_shield no_pick is_animated monster
94 friendly generator is_thrown auto_apply treasure player sold see_invisible
95 can_roll overlay_floor is_turnable is_used_up identified reflecting changing
96 splitting hitback startequip blocksview undead scared unaggressive
97 reflect_missile reflect_spell no_magic no_fix_player is_lightable tear_down
98 run_away pick_up unique no_drop can_cast_spell can_use_scroll can_use_range
99 can_use_bow can_use_armour can_use_weapon can_use_ring has_ready_range
100 has_ready_bow xrays is_floor lifesave no_strength sleep stand_still
101 random_move only_attack confused stealth cursed damned see_anywhere
102 known_magical known_cursed can_use_skill been_applied has_ready_scroll
103 can_use_rod can_use_horn make_invisible inv_locked is_wooded is_hilly
104 has_ready_skill has_ready_weapon no_skill_ident is_blind can_see_in_dark
105 is_cauldron is_dust no_steal one_hit berserk neutral no_attack no_damage
106 activate_on_push activate_on_release is_water use_content_on_gen is_buildable
107
108 body_range body_arm body_torso body_head body_neck body_skill
109 body_finger body_shoulder body_foot body_hand body_wrist body_waist
110 ));
111
112 our %EVENT_TYPE = (
113 apply => 1,
114 attack => 2,
115 death => 3,
116 drop => 4,
117 pickup => 5,
118 say => 6,
119 stop => 7,
120 time => 8,
121 throw => 9,
122 trigger => 10,
123 close => 11,
124 timer => 12,
125 );
126
127 sub MOVE_WALK (){ 0x01 }
128 sub MOVE_FLY_LOW (){ 0x02 }
129 sub MOVE_FLY_HIGH (){ 0x04 }
130 sub MOVE_FLYING (){ 0x06 }
131 sub MOVE_SWIM (){ 0x08 }
132 sub MOVE_BOAT (){ 0x10 }
133 sub MOVE_KNOWN (){ 0x1f } # all of above
134 sub MOVE_ALL (){ 0x10000 } # very special value
135
136 our %MOVE_TYPE = (
137 walk => MOVE_WALK,
138 fly_low => MOVE_FLY_LOW,
139 fly_high => MOVE_FLY_HIGH,
140 flying => MOVE_FLYING,
141 swim => MOVE_SWIM,
142 boat => MOVE_BOAT,
143 all => MOVE_ALL,
144 );
145
146 our @MOVE_TYPE = qw(all walk flying fly_low fly_high swim boat);
147
148 {
149 package Crossfire::MoveType;
150
151 use overload
152 '=' => sub { bless [@{$_[0]}], ref $_[0] },
153 '""' => \&as_string,
154 '>=' => sub { $_[0][0] & $MOVE_TYPE{$_[1]} ? $_[0][1] & $MOVE_TYPE{$_[1]} : undef },
155 '+=' => sub { $_[0][0] |= $MOVE_TYPE{$_[1]}; $_[0][1] |= $MOVE_TYPE{$_[1]}; &normalise },
156 '-=' => sub { $_[0][0] |= $MOVE_TYPE{$_[1]}; $_[0][1] &= ~$MOVE_TYPE{$_[1]}; &normalise },
157 '/=' => sub { $_[0][0] &= ~$MOVE_TYPE{$_[1]}; &normalise },
158 'x=' => sub {
159 my $cur = $_[0] >= $_[1];
160 if (!defined $cur) {
161 if ($_[0] >= "all") {
162 $_[0] -= $_[1];
163 } else {
164 $_[0] += $_[1];
165 }
166 } elsif ($cur) {
167 $_[0] -= $_[1];
168 } else {
169 $_[0] /= $_[1];
170 }
171
172 $_[0]
173 },
174 'eq' => sub { "$_[0]" eq "$_[1]" },
175 'ne' => sub { "$_[0]" ne "$_[1]" },
176 ;
177 }
178
179 sub Crossfire::MoveType::new {
180 my ($class, $string) = @_;
181
182 my $mask;
183 my $value;
184
185 if ($string =~ /^\s*\d+\s*$/) {
186 $mask = MOVE_ALL;
187 $value = $string+0;
188 } else {
189 for (split /\s+/, lc $string) {
190 if (s/^-//) {
191 $mask |= $MOVE_TYPE{$_};
192 $value &= ~$MOVE_TYPE{$_};
193 } else {
194 $mask |= $MOVE_TYPE{$_};
195 $value |= $MOVE_TYPE{$_};
196 }
197 }
198 }
199
200 (bless [$mask, $value], $class)->normalise
201 }
202
203 sub Crossfire::MoveType::normalise {
204 my ($self) = @_;
205
206 if ($self->[0] & MOVE_ALL) {
207 my $mask = ~(($self->[1] & MOVE_ALL ? $self->[1] : ~$self->[1]) & $self->[0] & ~MOVE_ALL);
208 $self->[0] &= $mask;
209 $self->[1] &= $mask;
210 }
211
212 $self->[1] &= $self->[0];
213
214 $self
215 }
216
217 sub Crossfire::MoveType::as_string {
218 my ($self) = @_;
219
220 my @res;
221
222 my ($mask, $value) = @$self;
223
224 for (@Crossfire::MOVE_TYPE) {
225 my $bit = $Crossfire::MOVE_TYPE{$_};
226 if (($mask & $bit) == $bit && (($value & $bit) == $bit || ($value & $bit) == 0)) {
227 $mask &= ~$bit;
228 push @res, $value & $bit ? $_ : "-$_";
229 }
230 }
231
232 join " ", @res
233 }
234
235 sub load_ref($) {
236 my ($path) = @_;
237
238 open my $fh, "<:raw:perlio", $path
239 or die "$path: $!";
240 local $/;
241
242 thaw <$fh>
243 }
244
245 sub save_ref($$) {
246 my ($ref, $path) = @_;
247
248 open my $fh, ">:raw:perlio", "$path~"
249 or die "$path~: $!";
250 print $fh freeze $ref;
251 close $fh;
252 rename "$path~", $path
253 or die "$path: $!";
254 }
255
256 my %attack_mask = (
257 physical => 0x00000001,
258 magic => 0x00000002,
259 fire => 0x00000004,
260 electricity => 0x00000008,
261 cold => 0x00000010,
262 confusion => 0x00000020,
263 acid => 0x00000040,
264 drain => 0x00000080,
265 weaponmagic => 0x00000100,
266 ghosthit => 0x00000200,
267 poison => 0x00000400,
268 slow => 0x00000800,
269 paralyze => 0x00001000,
270 turn_undead => 0x00002000,
271 fear => 0x00004000,
272 cancellation => 0x00008000,
273 deplete => 0x00010000,
274 death => 0x00020000,
275 chaos => 0x00040000,
276 counterspell => 0x00080000,
277 godpower => 0x00100000,
278 holyword => 0x00200000,
279 blind => 0x00400000,
280 internal => 0x00800000,
281 life_stealing => 0x01000000,
282 disease => 0x02000000,
283 );
284
285 sub _add_resist($$$) {
286 my ($ob, $mask, $value) = @_;
287
288 while (my ($k, $v) = each %attack_mask) {
289 $ob->{"resist_$k"} = min 100, max -100, $ob->{"resist_$k"} + $value if $mask & $v;
290 }
291 }
292
293 my %MATERIAL = reverse
294 paper => 1,
295 iron => 2,
296 glass => 4,
297 leather => 8,
298 wood => 16,
299 organic => 32,
300 stone => 64,
301 cloth => 128,
302 adamant => 256,
303 liquid => 512,
304 tin => 1024,
305 bone => 2048,
306 ice => 4096,
307
308 # guesses
309 runestone => 12,
310 bronze => 18,
311 "ancient wood" => 20,
312 glass => 36,
313 marble => 66,
314 ice => 68,
315 stone => 70,
316 stone => 80,
317 cloth => 136,
318 ironwood => 144,
319 adamantium => 258,
320 glacium => 260,
321 blood => 544,
322 ;
323
324 # object as in "Object xxx", i.e. archetypes
325 sub normalize_object($) {
326 my ($ob) = @_;
327
328 # convert material bitset to materialname, if possible
329 if (exists $ob->{material}) {
330 if (!$ob->{material}) {
331 delete $ob->{material};
332 } elsif (exists $ob->{materialname}) {
333 if ($MATERIAL{$ob->{material}} eq $ob->{materialname}) {
334 delete $ob->{material};
335 } else {
336 warn "object $ob->{_name} has both materialname ($ob->{materialname}) and material ($ob->{material}) set.\n";
337 delete $ob->{material}; # assume materilname is more specific and nuke material
338 }
339 } elsif (my $name = $MATERIAL{$ob->{material}}) {
340 delete $ob->{material};
341 $ob->{materialname} = $name;
342 } else {
343 warn "object $ob->{_name} has unknown material ($ob->{material}) set.\n";
344 }
345 }
346
347 # color_fg is used as default for magicmap if magicmap does not exist
348 $ob->{magicmap} ||= delete $ob->{color_fg} if exists $ob->{color_fg};
349
350 # nuke outdated or never supported fields
351 delete @$ob{qw(
352 can_knockback can_parry can_impale can_cut can_dam_armour
353 can_apply pass_thru can_pass_thru color_bg color_fg
354 )};
355
356 if (my $mask = delete $ob->{immune} ) { _add_resist $ob, $mask, 100; }
357 if (my $mask = delete $ob->{protected} ) { _add_resist $ob, $mask, 30; }
358 if (my $mask = delete $ob->{vulnerable}) { _add_resist $ob, $mask, -100; }
359
360 # convert movement strings to bitsets
361 for my $attr (keys %FIELD_MOVEMENT) {
362 next unless exists $ob->{$attr};
363
364 $ob->{$attr} = new Crossfire::MoveType $ob->{$attr};
365 }
366
367 # convert outdated movement flags to new movement sets
368 if (defined (my $v = delete $ob->{no_pass})) {
369 $ob->{move_block} = new Crossfire::MoveType $v ? "all" : "";
370 }
371 if (defined (my $v = delete $ob->{slow_move})) {
372 $ob->{move_slow} += "walk";
373 $ob->{move_slow_penalty} = $v;
374 }
375 if (defined (my $v = delete $ob->{walk_on})) {
376 $ob->{move_on} ||= new Crossfire::MoveType; if ($v) { $ob->{move_on} += "walk" } else { $ob->{move_on} -= "walk" }
377 }
378 if (defined (my $v = delete $ob->{walk_off})) {
379 $ob->{move_off} ||= new Crossfire::MoveType; if ($v) { $ob->{move_off} += "walk" } else { $ob->{move_off} -= "walk" }
380 }
381 if (defined (my $v = delete $ob->{fly_on})) {
382 $ob->{move_on} ||= new Crossfire::MoveType; if ($v) { $ob->{move_on} += "fly_low" } else { $ob->{move_on} -= "fly_low" }
383 }
384 if (defined (my $v = delete $ob->{fly_off})) {
385 $ob->{move_off} ||= new Crossfire::MoveType; if ($v) { $ob->{move_off} += "fly_low" } else { $ob->{move_off} -= "fly_low" }
386 }
387 if (defined (my $v = delete $ob->{flying})) {
388 $ob->{move_type} ||= new Crossfire::MoveType; if ($v) { $ob->{move_type} += "fly_low" } else { $ob->{move_type} -= "fly_low" }
389 }
390
391 # convert idiotic event_xxx things into objects
392 while (my ($event, $subtype) = each %EVENT_TYPE) {
393 if (exists $ob->{"event_${event}_plugin"}) {
394 push @{$ob->{inventory}}, {
395 _name => "event_$event",
396 title => delete $ob->{"event_${event}_plugin"},
397 slaying => delete $ob->{"event_${event}"},
398 name => delete $ob->{"event_${event}_options"},
399 };
400 }
401 }
402
403 # some archetypes had "+3" instead of the canonical "3", so fix
404 $ob->{dam} *= 1 if exists $ob->{dam};
405
406 $ob
407 }
408
409 # arch as in "arch xxx", ie.. objects
410 sub normalize_arch($) {
411 my ($ob) = @_;
412
413 normalize_object $ob;
414
415 my $arch = $ARCH{$ob->{_name}}
416 or (warn "$ob->{_name}: no such archetype", return $ob);
417
418 if ($arch->{type} == 22) { # map
419 my %normalize = (
420 "enter_x" => "hp",
421 "enter_y" => "sp",
422 "width" => "x",
423 "height" => "y",
424 "reset_timeout" => "weight",
425 "swap_time" => "value",
426 "difficulty" => "level",
427 "darkness" => "invisible",
428 "fixed_resettime" => "stand_still",
429 );
430
431 while (my ($k2, $k1) = each %normalize) {
432 if (defined (my $v = delete $ob->{$k1})) {
433 $ob->{$k2} = $v;
434 }
435 }
436 } else {
437 # if value matches archetype default, delete
438 while (my ($k, $v) = each %$ob) {
439 if (exists $arch->{$k} and $arch->{$k} eq $v) {
440 next if $k eq "_name";
441 delete $ob->{$k};
442 }
443 }
444 }
445
446 # a speciality for the editor
447 if (exists $ob->{attack_movement}) {
448 my $am = delete $ob->{attack_movement};
449 $ob->{attack_movement_bits_0_3} = $am & 15;
450 $ob->{attack_movement_bits_4_7} = $am & 240;
451 }
452
453 $ob
454 }
455
456 sub attr_thaw($) {
457 my ($ob) = @_;
458
459 $ob->{attach} = from_json $ob->{attach}
460 if exists $ob->{attach};
461
462 $ob
463 }
464
465 sub attr_freeze($) {
466 my ($ob) = @_;
467
468 $ob->{attach} = Crossfire::to_json $ob->{attach}
469 if exists $ob->{attach};
470
471 $ob
472 }
473
474 sub read_pak($) {
475 my ($path) = @_;
476
477 my %pak;
478
479 open my $fh, "<:raw:perlio", $path
480 or Carp::croak "$_[0]: $!";
481 binmode $fh;
482 while (<$fh>) {
483 my ($type, $id, $len, $path) = split;
484 $path =~ s/.*\///;
485 read $fh, $pak{$path}, $len;
486 }
487
488 \%pak
489 }
490
491 sub read_arch($;$) {
492 my ($path, $toplevel) = @_;
493
494 my %arc;
495 my ($more, $prev);
496 my $comment;
497
498 open my $fh, "<:raw:perlio:utf8", $path
499 or Carp::croak "$path: $!";
500
501 # binmode $fh;
502
503 my $parse_block; $parse_block = sub {
504 my %arc = @_;
505
506 while (<$fh>) {
507 s/\s+$//;
508 if (/^end$/i) {
509 last;
510
511 } elsif (/^arch (\S+)$/i) {
512 push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1);
513
514 } elsif (/^lore$/i) {
515 while (<$fh>) {
516 last if /^endlore\s*$/i;
517 $arc{lore} .= $_;
518 }
519 } elsif (/^msg$/i) {
520 while (<$fh>) {
521 last if /^endmsg\s*$/i;
522 $arc{msg} .= $_;
523 }
524 } elsif (/^anim$/i) {
525 while (<$fh>) {
526 last if /^mina\s*$/i;
527 chomp;
528 push @{ $arc{anim} }, $_;
529 }
530 } elsif (/^(\S+)\s*(.*)$/) {
531 $arc{lc $1} = $2;
532 } elsif (/^\s*#/) {
533 $arc{_comment} .= "$_\n";
534
535 } elsif (/^\s*$/) {
536 #
537 } else {
538 warn "$path: unparsable line '$_' in arch $arc{_name}";
539 }
540 }
541
542 \%arc
543 };
544
545 while (<$fh>) {
546 s/\s+$//;
547 if (/^more$/i) {
548 $more = $prev;
549 } elsif (/^object (\S+)$/i) {
550 my $name = $1;
551 my $arc = attr_thaw normalize_object $parse_block->(_name => $name, _comment => $comment);
552 undef $comment;
553 delete $arc{_comment} unless length $arc{_comment};
554 $arc->{_atype} = 'object';
555
556 if ($more) {
557 $more->{more} = $arc;
558 } else {
559 $arc{$name} = $arc;
560 }
561 $prev = $arc;
562 $more = undef;
563 } elsif (/^arch (\S+)$/i) {
564 my $name = $1;
565 my $arc = attr_thaw normalize_arch $parse_block->(_name => $name, _comment => $comment);
566 undef $comment;
567 delete $arc{_comment} unless length $arc{_comment};
568 $arc->{_atype} = 'arch';
569
570 if ($more) {
571 $more->{more} = $arc;
572 } else {
573 push @{ $arc{arch} }, $arc;
574 }
575 $prev = $arc;
576 $more = undef;
577 } elsif ($toplevel && /^(\S+)\s+(.*)$/) {
578 if ($1 eq "lev_array") {
579 while (<$fh>) {
580 last if /^endplst\s*$/;
581 push @{$toplevel->{lev_array}}, $_+0;
582 }
583 } else {
584 $toplevel->{$1} = $2;
585 }
586 } elsif (/^\s*#/) {
587 $comment .= "$_\n";
588 } elsif (/^\s*($|#)/) {
589 #
590 } else {
591 die "$path: unparseable top-level line '$_'";
592 }
593 }
594
595 undef $parse_block; # work around bug in perl not freeing $fh etc.
596
597 \%arc
598 }
599
600 sub archlist_to_string {
601 my ($arch) = @_;
602
603 my $str;
604
605 my $append; $append = sub {
606 my %a = %{$_[0]};
607
608 Crossfire::attr_freeze \%a;
609 Crossfire::normalize_arch \%a;
610
611 # undo the bit-split we did before
612 if (exists $a{attack_movement_bits_0_3} or exists $a{attack_movement_bits_4_7}) {
613 $a{attack_movement} = (delete $a{attack_movement_bits_0_3})
614 | (delete $a{attack_movement_bits_4_7});
615 }
616
617 if (my $comment = delete $a{_comment}) {
618 if ($comment =~ /[^\n\s#]/) {
619 $str .= $comment;
620 }
621 }
622
623 $str .= ((exists $a{_atype}) ? $a{_atype} : 'arch'). " $a{_name}\n";
624
625 my $inv = delete $a{inventory};
626 my $more = delete $a{more}; # arches do not support 'more', but old maps can contain some
627 my $anim = delete $a{anim};
628
629 if ($a{_atype} eq 'object') {
630 $str .= join "\n", "anim", @$anim, "mina\n"
631 if $anim;
632 }
633
634 my @kv;
635
636 for ($a{_name} eq "map"
637 ? @Crossfire::FIELD_ORDER_MAP
638 : @Crossfire::FIELD_ORDER) {
639 push @kv, [$_, delete $a{$_}]
640 if exists $a{$_};
641 }
642
643 for (sort keys %a) {
644 next if /^_/; # ignore our _-keys
645 push @kv, [$_, delete $a{$_}];
646 }
647
648 for (@kv) {
649 my ($k, $v) = @$_;
650
651 if (my $end = $Crossfire::FIELD_MULTILINE{$k}) {
652 $v =~ s/\n$//;
653 $str .= "$k\n$v\n$end\n";
654 } else {
655 $str .= "$k $v\n";
656 }
657 }
658
659 if ($inv) {
660 $append->($_) for @$inv;
661 }
662
663 $str .= "end\n";
664
665 if ($a{_atype} eq 'object') {
666 if ($more) {
667 $str .= "more\n";
668 $append->($more) if $more;
669 } else {
670 $str .= "\n";
671 }
672 }
673 };
674
675 for (@$arch) {
676 $append->($_);
677 }
678
679 $str
680 }
681
682 # put all archs into a hash with editor_face as it's key
683 # NOTE: the arrays in the hash values are references to
684 # the archs from $ARCH
685 sub editor_archs {
686 my %paths;
687
688 for (keys %ARCH) {
689 my $arch = $ARCH{$_};
690 push @{$paths{$arch->{editor_folder}}}, $arch;
691 }
692
693 \%paths
694 }
695
696 =item ($minx, $miny, $maxx, $maxy) = arch_extents $arch
697
698 arch_extents determines the extents of the given arch's face(s), linked
699 faces and single faces are handled here it returns (minx, miny, maxx,
700 maxy)
701
702 =cut
703
704 sub arch_extents {
705 my ($a) = @_;
706
707 my $o = $ARCH{$a->{_name}}
708 or return;
709
710 my $face = $FACE{$a->{face} || $o->{face} || "blank.111"};
711 unless ($face) {
712 $face = $FACE{"blank.x11"}
713 or (warn "no face data found for arch '$a->{_name}'"), return;
714 }
715
716 if ($face->{w} > 1 || $face->{h} > 1) {
717 # bigface
718 return (0, 0, $face->{w} - 1, $face->{h} - 1);
719
720 } elsif ($o->{more}) {
721 # linked face
722 my ($minx, $miny, $maxx, $maxy) = ($o->{x}, $o->{y}) x 2;
723
724 for (; $o; $o = $o->{more}) {
725 $minx = min $minx, $o->{x};
726 $miny = min $miny, $o->{y};
727 $maxx = max $maxx, $o->{x};
728 $maxy = max $maxy, $o->{y};
729 }
730
731 return ($minx, $miny, $maxx, $maxy);
732
733 } else {
734 # single face
735 return (0, 0, 0, 0);
736 }
737 }
738
739 =item $type = arch_attr $arch
740
741 Returns a hashref describing the object and its attributes. It can contain
742 the following keys:
743
744 name the name, suitable for display purposes
745 ignore
746 attr
747 desc
748 use
749 section => [name => \%attr, name => \%attr]
750 import
751
752 =cut
753
754 sub arch_attr($) {
755 my ($obj) = @_;
756
757 require Crossfire::Data;
758
759 my $root;
760 my $attr = { };
761
762 my $arch = $ARCH{ $obj->{_name} };
763 my $type = $obj->{type} || $arch->{type};
764
765 if ($type > 0) {
766 $root = $Crossfire::Data::ATTR{$type};
767 } else {
768 my %a = (%$arch, %$obj);
769
770 if ($a{is_floor} && !$a{alive}) {
771 $root = $Crossfire::Data::TYPE{Floor};
772 } elsif (!$a{is_floor} && $a{alive} && !$a{tear_down}) {
773 $root = $Crossfire::Data::TYPE{"Monster & NPC"};
774 } elsif (!$a{is_floor} && !$a{alive} && $a{move_block}) {
775 $root = $Crossfire::Data::TYPE{Wall};
776 } elsif (!$a{is_floor} && $a{alive} && $a{tear_down}) {
777 $root = $Crossfire::Data::TYPE{"Weak Wall"};
778 } else {
779 $root = $Crossfire::Data::TYPE{Misc};
780 }
781 }
782
783 my @import = ($root);
784
785 unshift @import, \%Crossfire::Data::DEFAULT_ATTR
786 unless $type == 116;
787
788 my (%ignore);
789 my (@section_order, %section, @attr_order);
790
791 while (my $type = shift @import) {
792 push @import, @{$type->{import} || []};
793
794 $attr->{$_} ||= $type->{$_}
795 for qw(name desc use);
796
797 for (@{$type->{ignore} || []}) {
798 $ignore{$_}++ for ref $_ ? @$_ : $_;
799 }
800
801 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) {
802 my ($name, $attr) = @$_;
803 push @section_order, $name;
804 for (@$attr) {
805 my ($k, $v) = @$_;
806 push @attr_order, $k;
807 $section{$name}{$k} ||= $v;
808 }
809 }
810 }
811
812 $attr->{section} = [
813 map !exists $section{$_} ? () : do {
814 my $attr = delete $section{$_};
815
816 [
817 $_,
818 map exists $attr->{$_} && !$ignore{$_}
819 ? [$_ => delete $attr->{$_}] : (),
820 @attr_order
821 ]
822 },
823
824 exists $section{$_} ? [$_ => delete $section{$_}] : (),
825 @section_order
826 ];
827
828 $attr
829 }
830
831 sub cache_file($$&&) {
832 my ($src, $cache, $load, $create) = @_;
833
834 my ($size, $mtime) = (stat $src)[7,9]
835 or Carp::croak "$src: $!";
836
837 if (-e $cache) {
838 my $ref = eval { load_ref $cache };
839
840 if ($ref->{version} == 1
841 && $ref->{size} == $size
842 && $ref->{mtime} == $mtime
843 && eval { $load->($ref->{data}); 1 }) {
844 return;
845 }
846 }
847
848 my $ref = {
849 version => 1,
850 size => $size,
851 mtime => $mtime,
852 data => $create->(),
853 };
854
855 $load->($ref->{data});
856
857 save_ref $ref, $cache;
858 }
859
860 =item set_libdir $path
861
862 Sets the library directory to the given path
863 (default: $ENV{CROSSFIRE_LIBDIR}).
864
865 You have to (re-)load the archetypes and tilecache manually after steting
866 the library path.
867
868 =cut
869
870 sub set_libdir($) {
871 $LIB = $_[0];
872 }
873
874 =item load_archetypes
875
876 (Re-)Load archetypes into %ARCH.
877
878 =cut
879
880 sub load_archetypes() {
881 cache_file "$LIB/archetypes", "$VARDIR/archetypes.pst", sub {
882 *ARCH = $_[0];
883 }, sub {
884 read_arch "$LIB/archetypes"
885 };
886 }
887
888 =item load_tilecache
889
890 (Re-)Load %TILE and %FACE.
891
892 =cut
893
894 sub load_tilecache() {
895 require Gtk2;
896
897 cache_file "$LIB/crossfire.0", "$VARDIR/tilecache.pst", sub {
898 $TILE = new_from_file Gtk2::Gdk::Pixbuf "$VARDIR/tilecache.png"
899 or die "$VARDIR/tilecache.png: $!";
900 *FACE = $_[0];
901 }, sub {
902 my $tile = read_pak "$LIB/crossfire.0";
903
904 my %cache;
905
906 my $idx = 0;
907
908 for my $name (sort keys %$tile) {
909 my $pb = new Gtk2::Gdk::PixbufLoader;
910 $pb->write ($tile->{$name});
911 $pb->close;
912 my $pb = $pb->get_pixbuf;
913
914 my $tile = $cache{$name} = {
915 pb => $pb,
916 idx => $idx,
917 w => int $pb->get_width / TILESIZE,
918 h => int $pb->get_height / TILESIZE,
919 };
920
921
922 $idx += $tile->{w} * $tile->{h};
923 }
924
925 my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64;
926
927 while (my ($name, $tile) = each %cache) {
928 my $tpb = delete $tile->{pb};
929 my $ofs = $tile->{idx};
930
931 for my $x (0 .. $tile->{w} - 1) {
932 for my $y (0 .. $tile->{h} - 1) {
933 my $idx = $ofs + $x + $y * $tile->{w};
934 $tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE,
935 $pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64);
936 }
937 }
938 }
939
940 $pb->save ("$VARDIR/tilecache.png", "png", compression => 1);
941
942 \%cache
943 };
944 }
945
946 =head1 AUTHOR
947
948 Marc Lehmann <schmorp@schmorp.de>
949 http://home.schmorp.de/
950
951 Robin Redeker <elmex@ta-sa.org>
952 http://www.ta-sa.org/
953
954 =cut
955
956 1