ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
(Generate patch)

Comparing deliantra/Deliantra/Deliantra.pm (file contents):
Revision 1.121 by root, Mon Apr 14 00:35:17 2008 UTC vs.
Revision 1.143 by root, Wed Oct 6 01:53:51 2010 UTC

1=head1 NAME 1=head1 NAME
2 2
3Deliantra - Deliantra suppport module to read/write archetypes, maps etc. 3Deliantra - Deliantra suppport module to read/write archetypes, maps etc.
4 4
5=over 4
6
5=cut 7=cut
6 8
7package Deliantra; 9package Deliantra;
8 10
9our $VERSION = '1.14'; 11our $VERSION = '1.30';
10 12
11use strict; 13use common::sense;
12 14
13use base 'Exporter'; 15use base 'Exporter';
14 16
15use Carp (); 17use Carp ();
16use File::Spec; 18use File::Spec;
24 editor_archs arch_extents 26 editor_archs arch_extents
25); 27);
26 28
27use JSON::XS qw(decode_json encode_json); 29use JSON::XS qw(decode_json encode_json);
28 30
29our $LIB = $ENV{DELIANTRA_LIBDIR} || $ENV{CROSSFIRE_LIBDIR}; 31our $LIB = $ENV{DELIANTRA_LIBDIR};
30
31our $OLDDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire"
32 : $ENV{AppData} ? "$ENV{APPDATA}/crossfire"
33 : File::Spec->tmpdir . "/crossfire";
34 32
35our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.deliantra" 33our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.deliantra"
36 : $ENV{AppData} ? "$ENV{APPDATA}/deliantra" 34 : $ENV{AppData} ? "$ENV{APPDATA}/deliantra"
37 : File::Spec->tmpdir . "/deliantra"; 35 : File::Spec->tmpdir . "/deliantra";
38 36
58 56
59# same as in server save routine, to (hopefully) be compatible 57# same as in server save routine, to (hopefully) be compatible
60# to the other editors. 58# to the other editors.
61our @FIELD_ORDER_MAP = (qw( 59our @FIELD_ORDER_MAP = (qw(
62 file_format_version 60 file_format_version
63 name attach swap_time reset_timeout fixed_resettime difficulty region 61 name attach swap_time reset_timeout fixed_resettime difficulty
62 region music
64 shopitems shopgreed shopmin shopmax shoprace 63 shopitems shopgreed shopmin shopmax shoprace
65 darkness width height enter_x enter_y msg maplore 64 darkness width height enter_x enter_y msg maplore
66 unique template 65 unique template
67 outdoor temp pressure humid windspeed winddir sky nosmooth 66 outdoor temp pressure humid windspeed winddir sky nosmooth
68 tile_path_1 tile_path_2 tile_path_3 tile_path_4 67 tile_path_1 tile_path_2 tile_path_3 tile_path_4
73 72
74 elevation 73 elevation
75 74
76 name name_pl custom_name attach title race 75 name name_pl custom_name attach title race
77 slaying skill msg lore other_arch 76 slaying skill msg lore other_arch
78 face animation is_animated 77 sound sound_destroy face animation is_animated
79 magicmap smoothlevel smoothface 78 magicmap glyph smoothlevel smoothface
80 str dex con wis pow cha int 79 str dex con wis pow cha int
81 hp maxhp sp maxsp grace maxgrace 80 hp maxhp sp maxsp grace maxgrace
82 exp perm_exp expmul 81 exp perm_exp expmul
83 food dam luck wc ac x y speed speed_left move_state attack_movement 82 food dam luck wc ac x y speed speed_left move_state attack_movement
84 nrof level direction type subtype attacktype 83 nrof level direction type subtype attacktype
134 trigger => 10, 133 trigger => 10,
135 close => 11, 134 close => 11,
136 timer => 12, 135 timer => 12,
137); 136);
138 137
138# 1 up 2 right 4 down 8 left
139our %WALLDIR = (
140 0 => 0,
141 1_2 => 1,
142 1_4 => 2,
143 2_2_1 => 3,
144 1_1 => 4,
145 2_1_1 => 5,
146 2_2_2 => 6,
147 3_2 => 7,
148 1_3 => 8,
149 2_2_4 => 9,
150 2_1_2 => 10,
151 3_1 => 11,
152 2_2_3 => 12,
153 3_4 => 13,
154 3_3 => 14,
155 4 => 15,
156);
157
139sub MOVE_WALK (){ 0x01 } 158sub MOVE_WALK (){ 0x01 }
140sub MOVE_FLY_LOW (){ 0x02 } 159sub MOVE_FLY_LOW (){ 0x02 }
141sub MOVE_FLY_HIGH (){ 0x04 } 160sub MOVE_FLY_HIGH (){ 0x04 }
142sub MOVE_FLYING (){ 0x06 } 161sub MOVE_FLYING (){ 0x06 }
143sub MOVE_SWIM (){ 0x08 } 162sub MOVE_SWIM (){ 0x08 }
155 boat => MOVE_BOAT, 174 boat => MOVE_BOAT,
156 ship => MOVE_SHIP, 175 ship => MOVE_SHIP,
157 all => MOVE_ALL, 176 all => MOVE_ALL,
158); 177);
159 178
160our @MOVE_TYPE = keys %MOVE_TYPE; 179our @MOVE_TYPE = qw(all walk flying fly_low fly_high swim boat ship);
161 180
162{ 181{
163 package Deliantra::MoveType; 182 package Deliantra::MoveType;
164 183
165 use overload 184 use overload
166 '=' => sub { bless [@{$_[0]}], ref $_[0] }, 185 '=' => sub { bless [@{$_[0]}], ref $_[0] },
167 '""' => \&as_string, 186 '""' => \&as_string,
168 '>=' => sub { $_[0][0] & $MOVE_TYPE{$_[1]} ? $_[0][1] & $MOVE_TYPE{$_[1]} : undef }, 187 '>=' => sub { $_[0][0] & $MOVE_TYPE{$_[1]} ? $_[0][1] & $MOVE_TYPE{$_[1]} : undef },
188 '<=' => sub {
189 ($_[0][0] & $MOVE_TYPE{$_[1]}) == $MOVE_TYPE{$_[1]}
190 ? $_[0][1] & $MOVE_TYPE{$_[1]}
191 : undef
192 },
169 '+=' => sub { $_[0][0] |= $MOVE_TYPE{$_[1]}; $_[0][1] |= $MOVE_TYPE{$_[1]}; &normalise }, 193 '+=' => sub { $_[0][0] |= $MOVE_TYPE{$_[1]}; $_[0][1] |= $MOVE_TYPE{$_[1]}; &normalise },
170 '-=' => sub { $_[0][0] |= $MOVE_TYPE{$_[1]}; $_[0][1] &= ~$MOVE_TYPE{$_[1]}; &normalise }, 194 '-=' => sub { $_[0][0] |= $MOVE_TYPE{$_[1]}; $_[0][1] &= ~$MOVE_TYPE{$_[1]}; &normalise },
171 '/=' => sub { $_[0][0] &= ~$MOVE_TYPE{$_[1]}; &normalise }, 195 '/=' => sub { $_[0][0] &= ~$MOVE_TYPE{$_[1]}; &normalise },
172 'x=' => sub { 196 'x=' => sub { # toggle between off, + and -
173 my $cur = $_[0] >= $_[1]; 197 my $cur = $_[0] >= $_[1];
174 if (!defined $cur) { 198 if (!defined $cur) {
175 if ($_[0] >= "all") { 199 if ($_[0] >= "all") {
176 $_[0] -= $_[1]; 200 $_[0] -= $_[1];
177 } else { 201 } else {
186 $_[0] 210 $_[0]
187 }, 211 },
188 'eq' => sub { "$_[0]" eq "$_[1]" }, 212 'eq' => sub { "$_[0]" eq "$_[1]" },
189 'ne' => sub { "$_[0]" ne "$_[1]" }, 213 'ne' => sub { "$_[0]" ne "$_[1]" },
190 ; 214 ;
215
216 sub TO_JSON {
217 $_[0][0]
218 }
191} 219}
192 220
193sub Deliantra::MoveType::new { 221sub Deliantra::MoveType::new {
194 my ($class, $string) = @_; 222 my ($class, $string) = @_;
195 223
337 365
338# object as in "Object xxx", i.e. archetypes 366# object as in "Object xxx", i.e. archetypes
339sub normalize_object($) { 367sub normalize_object($) {
340 my ($ob) = @_; 368 my ($ob) = @_;
341 369
370 delete $ob->{editable}; # deprecated
371
342 # convert material bitset to materialname, if possible 372 # convert material bitset to materialname, if possible
343 if (exists $ob->{material}) { 373 if (exists $ob->{material}) {
344 if (!$ob->{material}) { 374 if (!$ob->{material}) {
345 delete $ob->{material}; 375 delete $ob->{material};
346 } elsif (exists $ob->{materialname}) { 376 } elsif (exists $ob->{materialname}) {
389 $ob->{$attr} = new Deliantra::MoveType $ob->{$attr}; 419 $ob->{$attr} = new Deliantra::MoveType $ob->{$attr};
390 } 420 }
391 421
392 # convert outdated movement flags to new movement sets 422 # convert outdated movement flags to new movement sets
393 if (defined (my $v = delete $ob->{no_pass})) { 423 if (defined (my $v = delete $ob->{no_pass})) {
394 $ob->{move_block} = new Deliantra::MoveType $v ? "all" : ""; 424 $ob->{move_block} = new Deliantra::MoveType $v ? "all" : "0";
395 } 425 }
396 if (defined (my $v = delete $ob->{slow_move})) { 426 if (defined (my $v = delete $ob->{slow_move})) {
397 $ob->{move_slow} += "walk"; 427 $ob->{move_slow} += "walk";
398 $ob->{move_slow_penalty} = $v; 428 $ob->{move_slow_penalty} = $v;
399 } 429 }
400 if (defined (my $v = delete $ob->{walk_on})) { 430 if (defined (my $v = delete $ob->{walk_on})) {
401 $ob->{move_on} ||= new Deliantra::MoveType; if ($v) { $ob->{move_on} += "walk" } else { $ob->{move_on} -= "walk" } 431 $ob->{move_on} ||= new Deliantra::MoveType; if ($v) { $ob->{move_on} += "walk" } else { $ob->{move_on} -= "walk" }
402 } 432 }
403 if (defined (my $v = delete $ob->{walk_off})) { 433 if (defined (my $v = delete $ob->{walk_off})) {
404 $ob->{move_off} ||= new Deliantra::MoveType; if ($v) { $ob->{move_off} += "walk" } else { $ob->{move_off} -= "walk" } 434 $ob->{move_off} ||= new Deliantra::MoveType; if ($v) { $ob->{move_off} += "walk" } else { $ob->{move_off} -= "walk" }
405 } 435 }
406 if (defined (my $v = delete $ob->{fly_on})) { 436 if (defined (my $v = delete $ob->{fly_on})) {
434# arch as in "arch xxx", ie.. objects 464# arch as in "arch xxx", ie.. objects
435sub normalize_arch($) { 465sub normalize_arch($) {
436 my ($ob) = @_; 466 my ($ob) = @_;
437 467
438 normalize_object $ob; 468 normalize_object $ob;
469
470 return if $ob->{_atype} eq "object";
439 471
440 my $arch = $ARCH{$ob->{_name}} 472 my $arch = $ARCH{$ob->{_name}}
441 or (warn "$ob->{_name}: no such archetype", return $ob); 473 or (warn "$ob->{_name}: no such archetype", return $ob);
442 474
443 if ($arch->{type} == 22) { # map 475 if ($arch->{type} == 22) { # map
518 550
519 my %arc; 551 my %arc;
520 my ($more, $prev); 552 my ($more, $prev);
521 my $comment; 553 my $comment;
522 554
523 open my $fh, "<:raw:perlio:utf8", $path 555 open my $fh, "<:utf8", $path
524 or Carp::croak "$path: $!"; 556 or Carp::croak "$path: $!";
525 557
526# binmode $fh; 558# binmode $fh;
527 559
528 my $parse_block; $parse_block = sub { 560 my $parse_block; $parse_block = sub {
803 } else { 835 } else {
804 $root = $Deliantra::Data::TYPE{Misc}; 836 $root = $Deliantra::Data::TYPE{Misc};
805 } 837 }
806 } 838 }
807 839
840 my (%ignore);
808 my @import = ($root); 841 my @import = ($root);
809
810 unshift @import, \%Deliantra::Data::DEFAULT_ATTR
811 unless $type == 116;
812 842
813 my (%ignore); 843 my @new_import;
814 my (@section_order, %section, @attr_order);
815
816 while (my $type = shift @import) { 844 while (my $type = shift @import) {
845 # first import everything we will need:
817 push @import, 846 push @import,
818 grep $_, 847 grep $_,
819 map $Deliantra::Data::TYPE{$_}, 848 map $Deliantra::Data::TYPE{$_},
820 @{$type->{import} || []}; 849 @{$type->{import} || []};
821 850
851 # and compute the ignored attributes
852 for (@{$type->{ignore} || []}) {
853 $ignore{$_}++ for ref $_ ? @$_ : $_;
854 }
855
856 push @new_import, $type;
857 }
858 (@import) = @new_import;
859
860 # then add defaults to the back of the list, so they are added
861 # as last resort.
862 push @import, \%Deliantra::Data::DEFAULT_ATTR
863 unless $type == 116;
864
865 my (@section_order, %section, @attr_order);
866
867 # @import = root, imported, default
868 while (my $type = pop @import) {
822 $attr->{$_} ||= $type->{$_} 869 $attr->{$_} ||= $type->{$_}
823 for qw(name desc use); 870 for qw(name desc use);
824
825 for (@{$type->{ignore} || []}) {
826 $ignore{$_}++ for ref $_ ? @$_ : $_;
827 }
828 871
829 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) { 872 for ([general => ($type->{attr} || [])], @{$type->{section} || []}) {
830 my ($name, $attr) = @$_; 873 my ($name, $attr) = @$_;
831 push @section_order, $name; 874 push @section_order, $name;
832 for (@$attr) { 875 for (@$attr) {
833 my ($k, $v) = @$_; 876 my ($k, $v) = @$_;
834 push @attr_order, $k; 877 push @attr_order, $k;
835 $section{$name}{$k} ||= $v; 878 $section{$name}{$k} = $v; # overwrite, so that the root decides
836 }
837 } 879 }
880 }
881 }
882
883 # remove ignores for "root" type
884 for (
885 map @{$_->[1]}, # section attributes
886 [general => ($root->{attr} || [])],
887 @{$root->{section} || []}
888 ) {
889 my ($k, $v) = @$_;
890 # skip fixed attributes, if they are ignored thats fine
891 next if $v->{type} eq 'fixed';
892
893 delete $ignore{$k}; # if the attributes are defined explicitly they
894 # should NOT be ignored. ignore should mainly
895 # hit imported/inherited attributes.
838 } 896 }
839 897
840 $attr->{section} = [ 898 $attr->{section} = [
841 map !exists $section{$_} ? () : do { 899 map !exists $section{$_} ? () : do {
842 my $attr = delete $section{$_}; 900 my $attr = delete $section{$_};
846 map exists $attr->{$_} && !$ignore{$_} 904 map exists $attr->{$_} && !$ignore{$_}
847 ? [$_ => delete $attr->{$_}] : (), 905 ? [$_ => delete $attr->{$_}] : (),
848 @attr_order 906 @attr_order
849 ] 907 ]
850 }, 908 },
851
852 exists $section{$_} ? [$_ => delete $section{$_}] : (), 909 exists $section{$_} ? [$_ => delete $section{$_}] : (),
853 @section_order 910 @section_order
854 ]; 911 ];
855 912
856 $attr 913 $attr
857} 914}
985 1042
986 \%cache 1043 \%cache
987 }; 1044 };
988} 1045}
989 1046
1047=back
1048
990=head1 AUTHOR 1049=head1 AUTHOR
991 1050
992 Marc Lehmann <schmorp@schmorp.de> 1051 Marc Lehmann <schmorp@schmorp.de>
993 http://home.schmorp.de/ 1052 http://home.schmorp.de/
994 1053

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines