ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
Revision: 1.127
Committed: Mon Sep 15 23:39:13 2008 UTC (15 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-1_222
Changes since 1.126: +1 -1 lines
Log Message:
1.222

File Contents

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