ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
Revision: 1.99
Committed: Fri Mar 23 18:25:24 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
Changes since 1.98: +1 -11 lines
Log Message:
switch to JSON::XS

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 root 1.95 our $VERSION = '0.97';
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 root 1.53 use Storable qw(freeze thaw);
19 elmex 1.1
20 root 1.50 our @EXPORT = qw(
21     read_pak read_arch *ARCH TILESIZE $TILE *FACE editor_archs arch_extents
22     );
23    
24 root 1.99 use JSON::XS qw(from_json to_json);
25 root 1.70
26 root 1.50 our $LIB = $ENV{CROSSFIRE_LIBDIR};
27 root 1.7
28 root 1.75 our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire"
29     : $ENV{AppData} ? "$ENV{APPDATA}/crossfire"
30     : File::Spec->tmpdir . "/crossfire";
31 root 1.50
32     mkdir $VARDIR, 0777;
33 elmex 1.1
34 root 1.7 sub TILESIZE (){ 32 }
35 elmex 1.1
36 root 1.15 our %ARCH;
37     our %FACE;
38     our $TILE;
39 elmex 1.1
40 root 1.13 our %FIELD_MULTILINE = (
41 root 1.50 msg => "endmsg",
42     lore => "endlore",
43     maplore => "endmaplore",
44 root 1.13 );
45    
46 root 1.58 # 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 root 1.54 # same as in server save routine, to (hopefully) be compatible
51     # to the other editors.
52 root 1.56 our @FIELD_ORDER_MAP = (qw(
53 root 1.86 file_format_version
54 root 1.70 name attach swap_time reset_timeout fixed_resettime difficulty region
55 root 1.56 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 root 1.54 our @FIELD_ORDER = (qw(
63 root 1.57 elevation
64    
65 root 1.70 name name_pl custom_name attach title race
66 root 1.54 slaying skill msg lore other_arch face
67 root 1.56 #todo-events
68 root 1.54 animation is_animated
69 root 1.59 str dex con wis pow cha int
70 root 1.54 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 root 1.59 nrof level direction type subtype attacktype
74 root 1.54
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 root 1.13
112 root 1.62 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 root 1.50 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 root 1.58 sub MOVE_KNOWN (){ 0x1f } # all of above
134 root 1.90 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 root 1.93 '=' => sub { bless [@{$_[0]}], ref $_[0] },
153 root 1.90 '""' => \&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 root 1.94 if ($_[0] >= "all") {
162     $_[0] -= $_[1];
163     } else {
164     $_[0] += $_[1];
165     }
166 root 1.90 } elsif ($cur) {
167     $_[0] -= $_[1];
168     } else {
169     $_[0] /= $_[1];
170     }
171    
172     $_[0]
173     },
174 root 1.91 'eq' => sub { "$_[0]" eq "$_[1]" },
175     'ne' => sub { "$_[0]" ne "$_[1]" },
176 root 1.90 ;
177     }
178    
179     sub Crossfire::MoveType::new {
180     my ($class, $string) = @_;
181    
182     my $mask;
183     my $value;
184    
185 root 1.92 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 root 1.90 }
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 root 1.14
235 root 1.28 sub load_ref($) {
236     my ($path) = @_;
237    
238 root 1.63 open my $fh, "<:raw:perlio", $path
239 root 1.31 or die "$path: $!";
240 root 1.28 local $/;
241 root 1.33
242 root 1.53 thaw <$fh>
243 root 1.28 }
244    
245     sub save_ref($$) {
246     my ($ref, $path) = @_;
247    
248 root 1.63 open my $fh, ">:raw:perlio", "$path~"
249 root 1.28 or die "$path~: $!";
250 root 1.53 print $fh freeze $ref;
251 root 1.28 close $fh;
252     rename "$path~", $path
253     or die "$path: $!";
254     }
255    
256 root 1.72 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 root 1.73 $ob->{"resist_$k"} = min 100, max -100, $ob->{"resist_$k"} + $value if $mask & $v;
290 root 1.72 }
291     }
292    
293 root 1.82 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 root 1.83
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 root 1.85 adamantium => 258,
320 root 1.84 glacium => 260,
321 root 1.83 blood => 544,
322 root 1.82 ;
323    
324 root 1.62 # object as in "Object xxx", i.e. archetypes
325 root 1.52 sub normalize_object($) {
326 root 1.14 my ($ob) = @_;
327    
328 root 1.82 # 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 root 1.83 } elsif (my $name = $MATERIAL{$ob->{material}}) {
340     delete $ob->{material};
341     $ob->{materialname} = $name;
342 root 1.82 } else {
343 root 1.83 warn "object $ob->{_name} has unknown material ($ob->{material}) set.\n";
344 root 1.82 }
345     }
346    
347 root 1.96 # 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 root 1.62 # nuke outdated or never supported fields
351 root 1.72 delete @$ob{qw(
352 root 1.50 can_knockback can_parry can_impale can_cut can_dam_armour
353 root 1.98 can_apply pass_thru can_pass_thru color_bg color_fg
354 root 1.72 )};
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 root 1.14
360 root 1.62 # convert movement strings to bitsets
361 root 1.58 for my $attr (keys %FIELD_MOVEMENT) {
362 root 1.50 next unless exists $ob->{$attr};
363 root 1.58
364 root 1.90 $ob->{$attr} = new Crossfire::MoveType $ob->{$attr};
365 root 1.50 }
366    
367 root 1.62 # convert outdated movement flags to new movement sets
368 root 1.14 if (defined (my $v = delete $ob->{no_pass})) {
369 root 1.90 $ob->{move_block} = new Crossfire::MoveType $v ? "all" : "";
370 root 1.14 }
371 root 1.50 if (defined (my $v = delete $ob->{slow_move})) {
372 root 1.90 $ob->{move_slow} += "walk";
373 root 1.50 $ob->{move_slow_penalty} = $v;
374     }
375 root 1.14 if (defined (my $v = delete $ob->{walk_on})) {
376 root 1.90 $ob->{move_on} ||= new Crossfire::MoveType; if ($v) { $ob->{move_on} += "walk" } else { $ob->{move_on} -= "walk" }
377 root 1.14 }
378     if (defined (my $v = delete $ob->{walk_off})) {
379 root 1.90 $ob->{move_off} ||= new Crossfire::MoveType; if ($v) { $ob->{move_off} += "walk" } else { $ob->{move_off} -= "walk" }
380 root 1.14 }
381     if (defined (my $v = delete $ob->{fly_on})) {
382 root 1.90 $ob->{move_on} ||= new Crossfire::MoveType; if ($v) { $ob->{move_on} += "fly_low" } else { $ob->{move_on} -= "fly_low" }
383 root 1.14 }
384     if (defined (my $v = delete $ob->{fly_off})) {
385 root 1.90 $ob->{move_off} ||= new Crossfire::MoveType; if ($v) { $ob->{move_off} += "fly_low" } else { $ob->{move_off} -= "fly_low" }
386 root 1.14 }
387     if (defined (my $v = delete $ob->{flying})) {
388 root 1.90 $ob->{move_type} ||= new Crossfire::MoveType; if ($v) { $ob->{move_type} += "fly_low" } else { $ob->{move_type} -= "fly_low" }
389 root 1.14 }
390    
391 root 1.62 # 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 root 1.73 # some archetypes had "+3" instead of the canonical "3", so fix
404     $ob->{dam} *= 1 if exists $ob->{dam};
405    
406 root 1.52 $ob
407     }
408    
409 root 1.62 # arch as in "arch xxx", ie.. objects
410 root 1.52 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 root 1.14 }
444     }
445    
446 root 1.57 # 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 root 1.14 $ob
454     }
455 root 1.13
456 root 1.70 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 root 1.50 sub read_pak($) {
475     my ($path) = @_;
476    
477     my %pak;
478 elmex 1.1
479 root 1.63 open my $fh, "<:raw:perlio", $path
480 root 1.50 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 elmex 1.1 }
487 root 1.50
488     \%pak
489 elmex 1.1 }
490    
491 root 1.64 sub read_arch($;$) {
492     my ($path, $toplevel) = @_;
493 root 1.50
494     my %arc;
495     my ($more, $prev);
496 root 1.80 my $comment;
497 root 1.50
498 root 1.63 open my $fh, "<:raw:perlio:utf8", $path
499 root 1.50 or Carp::croak "$path: $!";
500    
501 elmex 1.68 # binmode $fh;
502 elmex 1.1
503 root 1.50 my $parse_block; $parse_block = sub {
504     my %arc = @_;
505 elmex 1.1
506     while (<$fh>) {
507     s/\s+$//;
508 root 1.50 if (/^end$/i) {
509     last;
510 root 1.80
511 root 1.55 } elsif (/^arch (\S+)$/i) {
512 root 1.70 push @{ $arc{inventory} }, attr_thaw normalize_arch $parse_block->(_name => $1);
513 root 1.80
514 root 1.55 } elsif (/^lore$/i) {
515 root 1.50 while (<$fh>) {
516     last if /^endlore\s*$/i;
517     $arc{lore} .= $_;
518 elmex 1.1 }
519 root 1.55 } elsif (/^msg$/i) {
520 root 1.50 while (<$fh>) {
521     last if /^endmsg\s*$/i;
522     $arc{msg} .= $_;
523     }
524 root 1.65 } elsif (/^anim$/i) {
525     while (<$fh>) {
526     last if /^mina\s*$/i;
527     chomp;
528     push @{ $arc{anim} }, $_;
529     }
530 root 1.50 } elsif (/^(\S+)\s*(.*)$/) {
531     $arc{lc $1} = $2;
532 root 1.80 } elsif (/^\s*#/) {
533 root 1.81 $arc{_comment} .= "$_\n";
534 root 1.80
535     } elsif (/^\s*$/) {
536 elmex 1.1 #
537     } else {
538 root 1.50 warn "$path: unparsable line '$_' in arch $arc{_name}";
539 elmex 1.1 }
540     }
541    
542 root 1.50 \%arc
543     };
544 elmex 1.2
545 root 1.50 while (<$fh>) {
546     s/\s+$//;
547     if (/^more$/i) {
548     $more = $prev;
549     } elsif (/^object (\S+)$/i) {
550     my $name = $1;
551 root 1.80 my $arc = attr_thaw normalize_object $parse_block->(_name => $name, _comment => $comment);
552 root 1.81 undef $comment;
553 root 1.80 delete $arc{_comment} unless length $arc{_comment};
554 elmex 1.71 $arc->{_atype} = 'object';
555 elmex 1.1
556 root 1.50 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 root 1.55 my $name = $1;
565 root 1.80 my $arc = attr_thaw normalize_arch $parse_block->(_name => $name, _comment => $comment);
566 root 1.81 undef $comment;
567 root 1.80 delete $arc{_comment} unless length $arc{_comment};
568 elmex 1.71 $arc->{_atype} = 'arch';
569 root 1.55
570     if ($more) {
571     $more->{more} = $arc;
572     } else {
573     push @{ $arc{arch} }, $arc;
574     }
575     $prev = $arc;
576     $more = undef;
577 root 1.64 } 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 root 1.81 } elsif (/^\s*#/) {
587     $comment .= "$_\n";
588 root 1.50 } elsif (/^\s*($|#)/) {
589     #
590     } else {
591 root 1.64 die "$path: unparseable top-level line '$_'";
592 root 1.50 }
593 elmex 1.2 }
594 root 1.50
595     undef $parse_block; # work around bug in perl not freeing $fh etc.
596    
597     \%arc
598 elmex 1.1 }
599    
600 elmex 1.71 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 root 1.81 if (my $comment = delete $a{_comment}) {
618     if ($comment =~ /[^\n\s#]/) {
619     $str .= $comment;
620     }
621     }
622    
623 elmex 1.71 $str .= ((exists $a{_atype}) ? $a{_atype} : 'arch'). " $a{_name}\n";
624    
625 root 1.81 my $inv = delete $a{inventory};
626 elmex 1.71 my $more = delete $a{more}; # arches do not support 'more', but old maps can contain some
627     my $anim = delete $a{anim};
628    
629 root 1.87 if ($a{_atype} eq 'object') {
630     $str .= join "\n", "anim", @$anim, "mina\n"
631     if $anim;
632     }
633    
634 elmex 1.71 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 root 1.81 if ($a{_atype} eq 'object') {
666     if ($more) {
667     $str .= "more\n";
668     $append->($more) if $more;
669     } else {
670     $str .= "\n";
671     }
672 elmex 1.71 }
673     };
674    
675     for (@$arch) {
676     $append->($_);
677     }
678    
679     $str
680     }
681    
682 elmex 1.10 # 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 root 1.15 for (keys %ARCH) {
689     my $arch = $ARCH{$_};
690 root 1.62 push @{$paths{$arch->{editor_folder}}}, $arch;
691 elmex 1.10 }
692    
693 root 1.15 \%paths
694 elmex 1.10 }
695    
696 root 1.17 =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 root 1.15 sub arch_extents {
705 elmex 1.10 my ($a) = @_;
706    
707 root 1.15 my $o = $ARCH{$a->{_name}}
708     or return;
709 elmex 1.10
710 elmex 1.79 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 elmex 1.10
716 root 1.15 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 elmex 1.10
733     } else {
734     # single face
735 root 1.15 return (0, 0, 0, 0);
736 elmex 1.10 }
737     }
738    
739 root 1.19 =item $type = arch_attr $arch
740 root 1.17
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 root 1.19 import
751 root 1.17
752     =cut
753    
754     sub arch_attr($) {
755 root 1.46 my ($obj) = @_;
756 root 1.17
757     require Crossfire::Data;
758    
759 root 1.36 my $root;
760 root 1.49 my $attr = { };
761 root 1.46
762     my $arch = $ARCH{ $obj->{_name} };
763     my $type = $obj->{type} || $arch->{type};
764 root 1.17
765 root 1.46 if ($type > 0) {
766     $root = $Crossfire::Data::ATTR{$type};
767 root 1.17 } else {
768 root 1.61 my %a = (%$arch, %$obj);
769 root 1.18
770 root 1.61 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 root 1.18 }
781 root 1.17 }
782    
783 root 1.47 my @import = ($root);
784    
785     unshift @import, \%Crossfire::Data::DEFAULT_ATTR
786     unless $type == 116;
787    
788 root 1.36 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 root 1.43 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) {
802 root 1.36 my ($name, $attr) = @$_;
803     push @section_order, $name;
804 root 1.43 for (@$attr) {
805 root 1.36 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 root 1.38 map exists $attr->{$_} && !$ignore{$_}
819     ? [$_ => delete $attr->{$_}] : (),
820 root 1.36 @attr_order
821     ]
822     },
823    
824     exists $section{$_} ? [$_ => delete $section{$_}] : (),
825     @section_order
826     ];
827    
828     $attr
829 root 1.17 }
830    
831 root 1.50 sub cache_file($$&&) {
832     my ($src, $cache, $load, $create) = @_;
833 root 1.24
834 root 1.50 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 root 1.24 }
873    
874 root 1.50 =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 root 1.15
888 root 1.50 =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 root 1.60 my $pb = new Gtk2::Gdk::PixbufLoader;
910     $pb->write ($tile->{$name});
911     $pb->close;
912     my $pb = $pb->get_pixbuf;
913 root 1.50
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 root 1.60 $pb->save ("$VARDIR/tilecache.png", "png", compression => 1);
941 root 1.50
942     \%cache
943     };
944     }
945 root 1.15
946 elmex 1.1 =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 root 1.4
956     1