1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Crossfire - Crossfire maphandling |
3 | Deliantra - Deliantra suppport module to read/write archetypes, maps etc. |
4 | |
4 | |
5 | =cut |
5 | =cut |
6 | |
6 | |
7 | package Crossfire; |
7 | package Deliantra; |
8 | |
8 | |
9 | our $VERSION = '1.0'; |
9 | our $VERSION = '1.221'; |
10 | |
10 | |
11 | use strict; |
11 | use strict; |
12 | |
12 | |
13 | use base 'Exporter'; |
13 | use base 'Exporter'; |
14 | |
14 | |
… | |
… | |
22 | *ARCH $TILE *FACE *FACEDATA |
22 | *ARCH $TILE *FACE *FACEDATA |
23 | TILESIZE CACHESTRIDE |
23 | TILESIZE CACHESTRIDE |
24 | editor_archs arch_extents |
24 | editor_archs arch_extents |
25 | ); |
25 | ); |
26 | |
26 | |
27 | use JSON::XS qw(from_json to_json); |
27 | use JSON::XS qw(decode_json encode_json); |
28 | |
28 | |
29 | our $LIB = $ENV{CROSSFIRE_LIBDIR}; |
29 | our $LIB = $ENV{DELIANTRA_LIBDIR}; |
30 | |
30 | |
31 | our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" |
31 | our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.deliantra" |
32 | : $ENV{AppData} ? "$ENV{APPDATA}/crossfire" |
32 | : $ENV{AppData} ? "$ENV{APPDATA}/deliantra" |
33 | : File::Spec->tmpdir . "/crossfire"; |
33 | : File::Spec->tmpdir . "/deliantra"; |
34 | |
34 | |
35 | mkdir $VARDIR, 0777; |
35 | mkdir $VARDIR, 0777; |
36 | |
36 | |
37 | sub TILESIZE (){ 32 } |
37 | sub TILESIZE (){ 32 } |
38 | sub CACHESTRIDE (){ 64 } |
38 | sub CACHESTRIDE (){ 64 } |
… | |
… | |
154 | ); |
154 | ); |
155 | |
155 | |
156 | our @MOVE_TYPE = keys %MOVE_TYPE; |
156 | our @MOVE_TYPE = keys %MOVE_TYPE; |
157 | |
157 | |
158 | { |
158 | { |
159 | package Crossfire::MoveType; |
159 | package Deliantra::MoveType; |
160 | |
160 | |
161 | use overload |
161 | use overload |
162 | '=' => sub { bless [@{$_[0]}], ref $_[0] }, |
162 | '=' => sub { bless [@{$_[0]}], ref $_[0] }, |
163 | '""' => \&as_string, |
163 | '""' => \&as_string, |
164 | '>=' => sub { $_[0][0] & $MOVE_TYPE{$_[1]} ? $_[0][1] & $MOVE_TYPE{$_[1]} : undef }, |
164 | '>=' => sub { $_[0][0] & $MOVE_TYPE{$_[1]} ? $_[0][1] & $MOVE_TYPE{$_[1]} : undef }, |
… | |
… | |
184 | 'eq' => sub { "$_[0]" eq "$_[1]" }, |
184 | 'eq' => sub { "$_[0]" eq "$_[1]" }, |
185 | 'ne' => sub { "$_[0]" ne "$_[1]" }, |
185 | 'ne' => sub { "$_[0]" ne "$_[1]" }, |
186 | ; |
186 | ; |
187 | } |
187 | } |
188 | |
188 | |
189 | sub Crossfire::MoveType::new { |
189 | sub Deliantra::MoveType::new { |
190 | my ($class, $string) = @_; |
190 | my ($class, $string) = @_; |
191 | |
191 | |
192 | my $mask; |
192 | my $mask; |
193 | my $value; |
193 | my $value; |
194 | |
194 | |
… | |
… | |
208 | } |
208 | } |
209 | |
209 | |
210 | (bless [$mask, $value], $class)->normalise |
210 | (bless [$mask, $value], $class)->normalise |
211 | } |
211 | } |
212 | |
212 | |
213 | sub Crossfire::MoveType::normalise { |
213 | sub Deliantra::MoveType::normalise { |
214 | my ($self) = @_; |
214 | my ($self) = @_; |
215 | |
215 | |
216 | if ($self->[0] & MOVE_ALL) { |
216 | if ($self->[0] & MOVE_ALL) { |
217 | my $mask = ~(($self->[1] & MOVE_ALL ? $self->[1] : ~$self->[1]) & $self->[0] & ~MOVE_ALL); |
217 | my $mask = ~(($self->[1] & MOVE_ALL ? $self->[1] : ~$self->[1]) & $self->[0] & ~MOVE_ALL); |
218 | $self->[0] &= $mask; |
218 | $self->[0] &= $mask; |
… | |
… | |
222 | $self->[1] &= $self->[0]; |
222 | $self->[1] &= $self->[0]; |
223 | |
223 | |
224 | $self |
224 | $self |
225 | } |
225 | } |
226 | |
226 | |
227 | sub Crossfire::MoveType::as_string { |
227 | sub Deliantra::MoveType::as_string { |
228 | my ($self) = @_; |
228 | my ($self) = @_; |
229 | |
229 | |
230 | my @res; |
230 | my @res; |
231 | |
231 | |
232 | my ($mask, $value) = @$self; |
232 | my ($mask, $value) = @$self; |
233 | |
233 | |
234 | for (@Crossfire::MOVE_TYPE) { |
234 | for (@Deliantra::MOVE_TYPE) { |
235 | my $bit = $Crossfire::MOVE_TYPE{$_}; |
235 | my $bit = $Deliantra::MOVE_TYPE{$_}; |
236 | if (($mask & $bit) == $bit && (($value & $bit) == $bit || ($value & $bit) == 0)) { |
236 | if (($mask & $bit) == $bit && (($value & $bit) == $bit || ($value & $bit) == 0)) { |
237 | $mask &= ~$bit; |
237 | $mask &= ~$bit; |
238 | push @res, $value & $bit ? $_ : "-$_"; |
238 | push @res, $value & $bit ? $_ : "-$_"; |
239 | } |
239 | } |
240 | } |
240 | } |
… | |
… | |
380 | |
380 | |
381 | # convert movement strings to bitsets |
381 | # convert movement strings to bitsets |
382 | for my $attr (keys %FIELD_MOVEMENT) { |
382 | for my $attr (keys %FIELD_MOVEMENT) { |
383 | next unless exists $ob->{$attr}; |
383 | next unless exists $ob->{$attr}; |
384 | |
384 | |
385 | $ob->{$attr} = new Crossfire::MoveType $ob->{$attr}; |
385 | $ob->{$attr} = new Deliantra::MoveType $ob->{$attr}; |
386 | } |
386 | } |
387 | |
387 | |
388 | # convert outdated movement flags to new movement sets |
388 | # convert outdated movement flags to new movement sets |
389 | if (defined (my $v = delete $ob->{no_pass})) { |
389 | if (defined (my $v = delete $ob->{no_pass})) { |
390 | $ob->{move_block} = new Crossfire::MoveType $v ? "all" : ""; |
390 | $ob->{move_block} = new Deliantra::MoveType $v ? "all" : ""; |
391 | } |
391 | } |
392 | if (defined (my $v = delete $ob->{slow_move})) { |
392 | if (defined (my $v = delete $ob->{slow_move})) { |
393 | $ob->{move_slow} += "walk"; |
393 | $ob->{move_slow} += "walk"; |
394 | $ob->{move_slow_penalty} = $v; |
394 | $ob->{move_slow_penalty} = $v; |
395 | } |
395 | } |
396 | if (defined (my $v = delete $ob->{walk_on})) { |
396 | if (defined (my $v = delete $ob->{walk_on})) { |
397 | $ob->{move_on} ||= new Crossfire::MoveType; if ($v) { $ob->{move_on} += "walk" } else { $ob->{move_on} -= "walk" } |
397 | $ob->{move_on} ||= new Deliantra::MoveType; if ($v) { $ob->{move_on} += "walk" } else { $ob->{move_on} -= "walk" } |
398 | } |
398 | } |
399 | if (defined (my $v = delete $ob->{walk_off})) { |
399 | if (defined (my $v = delete $ob->{walk_off})) { |
400 | $ob->{move_off} ||= new Crossfire::MoveType; if ($v) { $ob->{move_off} += "walk" } else { $ob->{move_off} -= "walk" } |
400 | $ob->{move_off} ||= new Deliantra::MoveType; if ($v) { $ob->{move_off} += "walk" } else { $ob->{move_off} -= "walk" } |
401 | } |
401 | } |
402 | if (defined (my $v = delete $ob->{fly_on})) { |
402 | if (defined (my $v = delete $ob->{fly_on})) { |
403 | $ob->{move_on} ||= new Crossfire::MoveType; if ($v) { $ob->{move_on} += "fly_low" } else { $ob->{move_on} -= "fly_low" } |
403 | $ob->{move_on} ||= new Deliantra::MoveType; if ($v) { $ob->{move_on} += "fly_low" } else { $ob->{move_on} -= "fly_low" } |
404 | } |
404 | } |
405 | if (defined (my $v = delete $ob->{fly_off})) { |
405 | if (defined (my $v = delete $ob->{fly_off})) { |
406 | $ob->{move_off} ||= new Crossfire::MoveType; if ($v) { $ob->{move_off} += "fly_low" } else { $ob->{move_off} -= "fly_low" } |
406 | $ob->{move_off} ||= new Deliantra::MoveType; if ($v) { $ob->{move_off} += "fly_low" } else { $ob->{move_off} -= "fly_low" } |
407 | } |
407 | } |
408 | if (defined (my $v = delete $ob->{flying})) { |
408 | if (defined (my $v = delete $ob->{flying})) { |
409 | $ob->{move_type} ||= new Crossfire::MoveType; if ($v) { $ob->{move_type} += "fly_low" } else { $ob->{move_type} -= "fly_low" } |
409 | $ob->{move_type} ||= new Deliantra::MoveType; if ($v) { $ob->{move_type} += "fly_low" } else { $ob->{move_type} -= "fly_low" } |
410 | } |
410 | } |
411 | |
411 | |
412 | # convert idiotic event_xxx things into objects |
412 | # convert idiotic event_xxx things into objects |
413 | while (my ($event, $subtype) = each %EVENT_TYPE) { |
413 | while (my ($event, $subtype) = each %EVENT_TYPE) { |
414 | if (exists $ob->{"event_${event}_plugin"}) { |
414 | if (exists $ob->{"event_${event}_plugin"}) { |
… | |
… | |
475 | } |
475 | } |
476 | |
476 | |
477 | sub attr_thaw($) { |
477 | sub attr_thaw($) { |
478 | my ($ob) = @_; |
478 | my ($ob) = @_; |
479 | |
479 | |
480 | $ob->{attach} = from_json $ob->{attach} |
480 | $ob->{attach} = decode_json $ob->{attach} |
481 | if exists $ob->{attach}; |
481 | if exists $ob->{attach}; |
482 | |
482 | |
483 | $ob |
483 | $ob |
484 | } |
484 | } |
485 | |
485 | |
486 | sub attr_freeze($) { |
486 | sub attr_freeze($) { |
487 | my ($ob) = @_; |
487 | my ($ob) = @_; |
488 | |
488 | |
489 | $ob->{attach} = Crossfire::to_json $ob->{attach} |
489 | $ob->{attach} = JSON::XS->new->utf8->canonical->encode ($ob->{attach}) |
490 | if exists $ob->{attach}; |
490 | if exists $ob->{attach}; |
491 | |
491 | |
492 | $ob |
492 | $ob |
493 | } |
493 | } |
494 | |
494 | |
… | |
… | |
624 | my $str; |
624 | my $str; |
625 | |
625 | |
626 | my $append; $append = sub { |
626 | my $append; $append = sub { |
627 | my %a = %{$_[0]}; |
627 | my %a = %{$_[0]}; |
628 | |
628 | |
629 | Crossfire::attr_freeze \%a; |
629 | Deliantra::attr_freeze \%a; |
630 | Crossfire::normalize_arch \%a; |
630 | Deliantra::normalize_arch \%a; |
631 | |
631 | |
632 | # undo the bit-split we did before |
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}) { |
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}) |
634 | $a{attack_movement} = (delete $a{attack_movement_bits_0_3}) |
635 | | (delete $a{attack_movement_bits_4_7}); |
635 | | (delete $a{attack_movement_bits_4_7}); |
… | |
… | |
653 | } |
653 | } |
654 | |
654 | |
655 | my @kv; |
655 | my @kv; |
656 | |
656 | |
657 | for ($a{_name} eq "map" |
657 | for ($a{_name} eq "map" |
658 | ? @Crossfire::FIELD_ORDER_MAP |
658 | ? @Deliantra::FIELD_ORDER_MAP |
659 | : @Crossfire::FIELD_ORDER) { |
659 | : @Deliantra::FIELD_ORDER) { |
660 | push @kv, [$_, delete $a{$_}] |
660 | push @kv, [$_, delete $a{$_}] |
661 | if exists $a{$_}; |
661 | if exists $a{$_}; |
662 | } |
662 | } |
663 | |
663 | |
664 | for (sort keys %a) { |
664 | for (sort keys %a) { |
… | |
… | |
667 | } |
667 | } |
668 | |
668 | |
669 | for (@kv) { |
669 | for (@kv) { |
670 | my ($k, $v) = @$_; |
670 | my ($k, $v) = @$_; |
671 | |
671 | |
672 | if (my $end = $Crossfire::FIELD_MULTILINE{$k}) { |
672 | if (my $end = $Deliantra::FIELD_MULTILINE{$k}) { |
673 | $v =~ s/\n$//; |
673 | $v =~ s/\n$//; |
674 | $str .= "$k\n$v\n$end\n"; |
674 | $str .= "$k\n$v\n$end\n"; |
675 | } else { |
675 | } else { |
676 | $str .= "$k $v\n"; |
676 | $str .= "$k $v\n"; |
677 | } |
677 | } |
… | |
… | |
773 | =cut |
773 | =cut |
774 | |
774 | |
775 | sub arch_attr($) { |
775 | sub arch_attr($) { |
776 | my ($obj) = @_; |
776 | my ($obj) = @_; |
777 | |
777 | |
778 | require Crossfire::Data; |
778 | require Deliantra::Data; |
779 | |
779 | |
780 | my $root; |
780 | my $root; |
781 | my $attr = { }; |
781 | my $attr = { }; |
782 | |
782 | |
783 | my $arch = $ARCH{ $obj->{_name} }; |
783 | my $arch = $ARCH{ $obj->{_name} }; |
784 | my $type = $obj->{type} || $arch->{type}; |
784 | my $type = $obj->{type} || $arch->{type}; |
785 | |
785 | |
786 | if ($type > 0) { |
786 | if ($type > 0) { |
787 | $root = $Crossfire::Data::ATTR{$type}; |
787 | $root = $Deliantra::Data::ATTR{$type}; |
788 | } else { |
788 | } else { |
789 | my %a = (%$arch, %$obj); |
789 | my %a = (%$arch, %$obj); |
790 | |
790 | |
791 | if ($a{is_floor} && !$a{alive}) { |
791 | if ($a{is_floor} && !$a{alive}) { |
792 | $root = $Crossfire::Data::TYPE{Floor}; |
792 | $root = $Deliantra::Data::TYPE{Floor}; |
793 | } elsif (!$a{is_floor} && $a{alive} && !$a{tear_down}) { |
793 | } elsif (!$a{is_floor} && $a{alive} && !$a{tear_down}) { |
794 | $root = $Crossfire::Data::TYPE{"Monster & NPC"}; |
794 | $root = $Deliantra::Data::TYPE{"Monster & NPC"}; |
795 | } elsif (!$a{is_floor} && !$a{alive} && $a{move_block}) { |
795 | } elsif (!$a{is_floor} && !$a{alive} && $a{move_block}) { |
796 | $root = $Crossfire::Data::TYPE{Wall}; |
796 | $root = $Deliantra::Data::TYPE{Wall}; |
797 | } elsif (!$a{is_floor} && $a{alive} && $a{tear_down}) { |
797 | } elsif (!$a{is_floor} && $a{alive} && $a{tear_down}) { |
798 | $root = $Crossfire::Data::TYPE{"Weak Wall"}; |
798 | $root = $Deliantra::Data::TYPE{"Weak Wall"}; |
799 | } else { |
799 | } else { |
800 | $root = $Crossfire::Data::TYPE{Misc}; |
800 | $root = $Deliantra::Data::TYPE{Misc}; |
801 | } |
801 | } |
802 | } |
802 | } |
803 | |
803 | |
804 | my @import = ($root); |
804 | my @import = ($root); |
805 | |
805 | |
806 | unshift @import, \%Crossfire::Data::DEFAULT_ATTR |
806 | unshift @import, \%Deliantra::Data::DEFAULT_ATTR |
807 | unless $type == 116; |
807 | unless $type == 116; |
808 | |
808 | |
809 | my (%ignore); |
809 | my (%ignore); |
810 | my (@section_order, %section, @attr_order); |
810 | my (@section_order, %section, @attr_order); |
811 | |
811 | |
812 | while (my $type = shift @import) { |
812 | while (my $type = shift @import) { |
|
|
813 | push @import, |
|
|
814 | grep $_, |
|
|
815 | map $Deliantra::Data::TYPE{$_}, |
813 | push @import, @{$type->{import} || []}; |
816 | @{$type->{import} || []}; |
814 | |
817 | |
815 | $attr->{$_} ||= $type->{$_} |
818 | $attr->{$_} ||= $type->{$_} |
816 | for qw(name desc use); |
819 | for qw(name desc use); |
817 | |
820 | |
818 | for (@{$type->{ignore} || []}) { |
821 | for (@{$type->{ignore} || []}) { |
… | |
… | |
943 | =cut |
946 | =cut |
944 | |
947 | |
945 | sub load_tilecache() { |
948 | sub load_tilecache() { |
946 | require Gtk2; |
949 | require Gtk2; |
947 | |
950 | |
948 | if (-e "$LIB/facedata") { # Crossfire TRT faces |
|
|
949 | cache_file "$LIB/facedata", "$VARDIR/tilecache.pst", \&use_tilecache, |
951 | cache_file "$LIB/facedata", "$VARDIR/tilecache.pst", \&use_tilecache, |
950 | sub { |
952 | sub { |
951 | my %cache; |
953 | my %cache; |
952 | my $facedata = Storable::retrieve "$LIB/facedata"; |
954 | my $facedata = Storable::retrieve "$LIB/facedata"; |
953 | |
955 | |
954 | $facedata->{version} == 2 |
956 | $facedata->{version} == 2 |
955 | or die "$LIB/facedata: version mismatch, cannot proceed."; |
957 | or die "$LIB/facedata: version mismatch, cannot proceed."; |
956 | |
958 | |
957 | my $faces = $facedata->{faceinfo}; |
959 | my $faces = $facedata->{faceinfo}; |
958 | my $idx = 0; |
960 | my $idx = 0; |
959 | |
961 | |
960 | for (sort keys %$faces) { |
962 | for (sort keys %$faces) { |
961 | my ($face, $info) = ($_, $faces->{$_}); |
963 | my ($face, $info) = ($_, $faces->{$_}); |
962 | |
964 | |
963 | my $pb = new Gtk2::Gdk::PixbufLoader; |
965 | my $pb = new Gtk2::Gdk::PixbufLoader; |
964 | $pb->write ($info->{data32}); |
966 | $pb->write ($info->{data32}); |
965 | $pb->close; |
967 | $pb->close; |
966 | my $pb = $pb->get_pixbuf; |
968 | my $pb = $pb->get_pixbuf; |
967 | |
969 | |
968 | my $tile = $cache{$face} = { |
970 | my $tile = $cache{$face} = { |
969 | pb => $pb, |
971 | pb => $pb, |
970 | idx => $idx, |
972 | idx => $idx, |
971 | w => int $pb->get_width / TILESIZE, |
973 | w => int $pb->get_width / TILESIZE, |
972 | h => int $pb->get_height / TILESIZE, |
974 | h => int $pb->get_height / TILESIZE, |
973 | }; |
975 | }; |
974 | |
976 | |
975 | $idx += $tile->{w} * $tile->{h}; |
977 | $idx += $tile->{w} * $tile->{h}; |
976 | } |
978 | } |
977 | |
979 | |
978 | construct_tilecache_pb $idx, \%cache; |
980 | construct_tilecache_pb $idx, \%cache; |
979 | |
981 | |
980 | \%cache |
982 | \%cache |
981 | }; |
983 | }; |
982 | |
|
|
983 | *FACEDATA = Storable::retrieve "$LIB/facedata"; |
|
|
984 | |
|
|
985 | } elsif (-e "$LIB/crossfire.0") { # Crossfire1 version |
|
|
986 | cache_file "$LIB/crossfire.0", "$VARDIR/tilecache.pst", \&use_tilecache, |
|
|
987 | sub { |
|
|
988 | my $tile = read_pak "$LIB/crossfire.0"; |
|
|
989 | |
|
|
990 | my %cache; |
|
|
991 | |
|
|
992 | my $idx = 0; |
|
|
993 | |
|
|
994 | for my $name (sort keys %$tile) { |
|
|
995 | my $pb = new Gtk2::Gdk::PixbufLoader; |
|
|
996 | $pb->write ($tile->{$name}); |
|
|
997 | $pb->close; |
|
|
998 | my $pb = $pb->get_pixbuf; |
|
|
999 | |
|
|
1000 | my $tile = $cache{$name} = { |
|
|
1001 | pb => $pb, |
|
|
1002 | idx => $idx, |
|
|
1003 | w => int $pb->get_width / TILESIZE, |
|
|
1004 | h => int $pb->get_height / TILESIZE, |
|
|
1005 | }; |
|
|
1006 | |
|
|
1007 | $idx += $tile->{w} * $tile->{h}; |
|
|
1008 | } |
|
|
1009 | |
|
|
1010 | construct_tilecache_pb $idx, \%cache; |
|
|
1011 | |
|
|
1012 | \%cache |
|
|
1013 | }; |
|
|
1014 | |
|
|
1015 | *FACEDATA = { }; |
|
|
1016 | } |
|
|
1017 | } |
984 | } |
1018 | |
985 | |
1019 | =head1 AUTHOR |
986 | =head1 AUTHOR |
1020 | |
987 | |
1021 | Marc Lehmann <schmorp@schmorp.de> |
988 | Marc Lehmann <schmorp@schmorp.de> |