1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Crossfire - Crossfire maphandling |
3 | Deliantra - Deliantra suppport module to read/write archetypes, maps etc. |
|
|
4 | |
|
|
5 | =over 4 |
4 | |
6 | |
5 | =cut |
7 | =cut |
6 | |
8 | |
7 | package Crossfire; |
9 | package Deliantra; |
8 | |
10 | |
9 | our $VERSION = '0.99'; |
11 | our $VERSION = '1.25'; |
10 | |
12 | |
11 | use strict; |
13 | use common::sense; |
12 | |
14 | |
13 | use base 'Exporter'; |
15 | use base 'Exporter'; |
14 | |
16 | |
15 | use Carp (); |
17 | use Carp (); |
16 | use File::Spec; |
18 | use File::Spec; |
17 | use List::Util qw(min max); |
19 | use List::Util qw(min max); |
18 | use Storable qw(freeze thaw); |
20 | use Storable qw(freeze thaw); |
19 | |
21 | |
20 | our @EXPORT = qw( |
22 | our @EXPORT = qw( |
21 | read_pak read_arch *ARCH TILESIZE $TILE *FACE editor_archs arch_extents |
23 | read_pak read_arch |
|
|
24 | *ARCH $TILE *FACE *FACEDATA |
|
|
25 | TILESIZE CACHESTRIDE |
|
|
26 | editor_archs arch_extents |
22 | ); |
27 | ); |
23 | |
28 | |
24 | use JSON::XS qw(from_json to_json); |
29 | use JSON::XS qw(decode_json encode_json); |
25 | |
30 | |
26 | our $LIB = $ENV{CROSSFIRE_LIBDIR}; |
31 | our $LIB = $ENV{DELIANTRA_LIBDIR}; |
27 | |
32 | |
28 | our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.crossfire" |
33 | our $VARDIR = $ENV{HOME} ? "$ENV{HOME}/.deliantra" |
29 | : $ENV{AppData} ? "$ENV{APPDATA}/crossfire" |
34 | : $ENV{AppData} ? "$ENV{APPDATA}/deliantra" |
30 | : File::Spec->tmpdir . "/crossfire"; |
35 | : File::Spec->tmpdir . "/deliantra"; |
31 | |
36 | |
32 | mkdir $VARDIR, 0777; |
37 | mkdir $VARDIR, 0777; |
33 | |
38 | |
34 | sub TILESIZE (){ 32 } |
39 | sub TILESIZE (){ 32 } |
|
|
40 | sub CACHESTRIDE (){ 64 } |
35 | |
41 | |
36 | our %ARCH; |
42 | our %ARCH; |
|
|
43 | our %FACE; # face32 |
37 | our %FACE; |
44 | our %FACEDATA; |
38 | our $TILE; |
45 | our $TILE; |
39 | |
46 | |
40 | our %FIELD_MULTILINE = ( |
47 | our %FIELD_MULTILINE = ( |
41 | msg => "endmsg", |
48 | msg => "endmsg", |
42 | lore => "endlore", |
49 | lore => "endlore", |
… | |
… | |
64 | |
71 | |
65 | elevation |
72 | elevation |
66 | |
73 | |
67 | name name_pl custom_name attach title race |
74 | name name_pl custom_name attach title race |
68 | slaying skill msg lore other_arch |
75 | slaying skill msg lore other_arch |
69 | face animation is_animated |
76 | sound sound_destroy face animation is_animated |
70 | magicmap smoothlevel smoothface |
77 | magicmap smoothlevel smoothface |
71 | str dex con wis pow cha int |
78 | str dex con wis pow cha int |
72 | hp maxhp sp maxsp grace maxgrace |
79 | hp maxhp sp maxsp grace maxgrace |
73 | exp perm_exp expmul |
80 | exp perm_exp expmul |
74 | food dam luck wc ac x y speed speed_left move_state attack_movement |
81 | food dam luck wc ac x y speed speed_left move_state attack_movement |
… | |
… | |
146 | boat => MOVE_BOAT, |
153 | boat => MOVE_BOAT, |
147 | ship => MOVE_SHIP, |
154 | ship => MOVE_SHIP, |
148 | all => MOVE_ALL, |
155 | all => MOVE_ALL, |
149 | ); |
156 | ); |
150 | |
157 | |
151 | our @MOVE_TYPE = keys %MOVE_TYPE; |
158 | our @MOVE_TYPE = qw(all walk flying fly_low fly_high swim boat ship); |
152 | |
159 | |
153 | { |
160 | { |
154 | package Crossfire::MoveType; |
161 | package Deliantra::MoveType; |
155 | |
162 | |
156 | use overload |
163 | use overload |
157 | '=' => sub { bless [@{$_[0]}], ref $_[0] }, |
164 | '=' => sub { bless [@{$_[0]}], ref $_[0] }, |
158 | '""' => \&as_string, |
165 | '""' => \&as_string, |
159 | '>=' => sub { $_[0][0] & $MOVE_TYPE{$_[1]} ? $_[0][1] & $MOVE_TYPE{$_[1]} : undef }, |
166 | '>=' => sub { $_[0][0] & $MOVE_TYPE{$_[1]} ? $_[0][1] & $MOVE_TYPE{$_[1]} : undef }, |
|
|
167 | '<=' => sub { |
|
|
168 | ($_[0][0] & $MOVE_TYPE{$_[1]}) == $MOVE_TYPE{$_[1]} |
|
|
169 | ? $_[0][1] & $MOVE_TYPE{$_[1]} |
|
|
170 | : undef |
|
|
171 | }, |
160 | '+=' => sub { $_[0][0] |= $MOVE_TYPE{$_[1]}; $_[0][1] |= $MOVE_TYPE{$_[1]}; &normalise }, |
172 | '+=' => sub { $_[0][0] |= $MOVE_TYPE{$_[1]}; $_[0][1] |= $MOVE_TYPE{$_[1]}; &normalise }, |
161 | '-=' => sub { $_[0][0] |= $MOVE_TYPE{$_[1]}; $_[0][1] &= ~$MOVE_TYPE{$_[1]}; &normalise }, |
173 | '-=' => sub { $_[0][0] |= $MOVE_TYPE{$_[1]}; $_[0][1] &= ~$MOVE_TYPE{$_[1]}; &normalise }, |
162 | '/=' => sub { $_[0][0] &= ~$MOVE_TYPE{$_[1]}; &normalise }, |
174 | '/=' => sub { $_[0][0] &= ~$MOVE_TYPE{$_[1]}; &normalise }, |
163 | 'x=' => sub { |
175 | 'x=' => sub { # toggle between off, + and - |
164 | my $cur = $_[0] >= $_[1]; |
176 | my $cur = $_[0] >= $_[1]; |
165 | if (!defined $cur) { |
177 | if (!defined $cur) { |
166 | if ($_[0] >= "all") { |
178 | if ($_[0] >= "all") { |
167 | $_[0] -= $_[1]; |
179 | $_[0] -= $_[1]; |
168 | } else { |
180 | } else { |
… | |
… | |
177 | $_[0] |
189 | $_[0] |
178 | }, |
190 | }, |
179 | 'eq' => sub { "$_[0]" eq "$_[1]" }, |
191 | 'eq' => sub { "$_[0]" eq "$_[1]" }, |
180 | 'ne' => sub { "$_[0]" ne "$_[1]" }, |
192 | 'ne' => sub { "$_[0]" ne "$_[1]" }, |
181 | ; |
193 | ; |
182 | } |
|
|
183 | |
194 | |
184 | sub Crossfire::MoveType::new { |
195 | sub TO_JSON { |
|
|
196 | $_[0][0] |
|
|
197 | } |
|
|
198 | } |
|
|
199 | |
|
|
200 | sub Deliantra::MoveType::new { |
185 | my ($class, $string) = @_; |
201 | my ($class, $string) = @_; |
186 | |
202 | |
187 | my $mask; |
203 | my $mask; |
188 | my $value; |
204 | my $value; |
189 | |
205 | |
… | |
… | |
203 | } |
219 | } |
204 | |
220 | |
205 | (bless [$mask, $value], $class)->normalise |
221 | (bless [$mask, $value], $class)->normalise |
206 | } |
222 | } |
207 | |
223 | |
208 | sub Crossfire::MoveType::normalise { |
224 | sub Deliantra::MoveType::normalise { |
209 | my ($self) = @_; |
225 | my ($self) = @_; |
210 | |
226 | |
211 | if ($self->[0] & MOVE_ALL) { |
227 | if ($self->[0] & MOVE_ALL) { |
212 | my $mask = ~(($self->[1] & MOVE_ALL ? $self->[1] : ~$self->[1]) & $self->[0] & ~MOVE_ALL); |
228 | my $mask = ~(($self->[1] & MOVE_ALL ? $self->[1] : ~$self->[1]) & $self->[0] & ~MOVE_ALL); |
213 | $self->[0] &= $mask; |
229 | $self->[0] &= $mask; |
… | |
… | |
217 | $self->[1] &= $self->[0]; |
233 | $self->[1] &= $self->[0]; |
218 | |
234 | |
219 | $self |
235 | $self |
220 | } |
236 | } |
221 | |
237 | |
222 | sub Crossfire::MoveType::as_string { |
238 | sub Deliantra::MoveType::as_string { |
223 | my ($self) = @_; |
239 | my ($self) = @_; |
224 | |
240 | |
225 | my @res; |
241 | my @res; |
226 | |
242 | |
227 | my ($mask, $value) = @$self; |
243 | my ($mask, $value) = @$self; |
228 | |
244 | |
229 | for (@Crossfire::MOVE_TYPE) { |
245 | for (@Deliantra::MOVE_TYPE) { |
230 | my $bit = $Crossfire::MOVE_TYPE{$_}; |
246 | my $bit = $Deliantra::MOVE_TYPE{$_}; |
231 | if (($mask & $bit) == $bit && (($value & $bit) == $bit || ($value & $bit) == 0)) { |
247 | if (($mask & $bit) == $bit && (($value & $bit) == $bit || ($value & $bit) == 0)) { |
232 | $mask &= ~$bit; |
248 | $mask &= ~$bit; |
233 | push @res, $value & $bit ? $_ : "-$_"; |
249 | push @res, $value & $bit ? $_ : "-$_"; |
234 | } |
250 | } |
235 | } |
251 | } |
… | |
… | |
328 | |
344 | |
329 | # object as in "Object xxx", i.e. archetypes |
345 | # object as in "Object xxx", i.e. archetypes |
330 | sub normalize_object($) { |
346 | sub normalize_object($) { |
331 | my ($ob) = @_; |
347 | my ($ob) = @_; |
332 | |
348 | |
|
|
349 | delete $ob->{editable}; # deprecated |
|
|
350 | |
333 | # convert material bitset to materialname, if possible |
351 | # convert material bitset to materialname, if possible |
334 | if (exists $ob->{material}) { |
352 | if (exists $ob->{material}) { |
335 | if (!$ob->{material}) { |
353 | if (!$ob->{material}) { |
336 | delete $ob->{material}; |
354 | delete $ob->{material}; |
337 | } elsif (exists $ob->{materialname}) { |
355 | } elsif (exists $ob->{materialname}) { |
… | |
… | |
375 | |
393 | |
376 | # convert movement strings to bitsets |
394 | # convert movement strings to bitsets |
377 | for my $attr (keys %FIELD_MOVEMENT) { |
395 | for my $attr (keys %FIELD_MOVEMENT) { |
378 | next unless exists $ob->{$attr}; |
396 | next unless exists $ob->{$attr}; |
379 | |
397 | |
380 | $ob->{$attr} = new Crossfire::MoveType $ob->{$attr}; |
398 | $ob->{$attr} = new Deliantra::MoveType $ob->{$attr}; |
381 | } |
399 | } |
382 | |
400 | |
383 | # convert outdated movement flags to new movement sets |
401 | # convert outdated movement flags to new movement sets |
384 | if (defined (my $v = delete $ob->{no_pass})) { |
402 | if (defined (my $v = delete $ob->{no_pass})) { |
385 | $ob->{move_block} = new Crossfire::MoveType $v ? "all" : ""; |
403 | $ob->{move_block} = new Deliantra::MoveType $v ? "all" : "0"; |
386 | } |
404 | } |
387 | if (defined (my $v = delete $ob->{slow_move})) { |
405 | if (defined (my $v = delete $ob->{slow_move})) { |
388 | $ob->{move_slow} += "walk"; |
406 | $ob->{move_slow} += "walk"; |
389 | $ob->{move_slow_penalty} = $v; |
407 | $ob->{move_slow_penalty} = $v; |
390 | } |
408 | } |
391 | if (defined (my $v = delete $ob->{walk_on})) { |
409 | if (defined (my $v = delete $ob->{walk_on})) { |
392 | $ob->{move_on} ||= new Crossfire::MoveType; if ($v) { $ob->{move_on} += "walk" } else { $ob->{move_on} -= "walk" } |
410 | $ob->{move_on} ||= new Deliantra::MoveType; if ($v) { $ob->{move_on} += "walk" } else { $ob->{move_on} -= "walk" } |
393 | } |
411 | } |
394 | if (defined (my $v = delete $ob->{walk_off})) { |
412 | if (defined (my $v = delete $ob->{walk_off})) { |
395 | $ob->{move_off} ||= new Crossfire::MoveType; if ($v) { $ob->{move_off} += "walk" } else { $ob->{move_off} -= "walk" } |
413 | $ob->{move_off} ||= new Deliantra::MoveType; if ($v) { $ob->{move_off} += "walk" } else { $ob->{move_off} -= "walk" } |
396 | } |
414 | } |
397 | if (defined (my $v = delete $ob->{fly_on})) { |
415 | if (defined (my $v = delete $ob->{fly_on})) { |
398 | $ob->{move_on} ||= new Crossfire::MoveType; if ($v) { $ob->{move_on} += "fly_low" } else { $ob->{move_on} -= "fly_low" } |
416 | $ob->{move_on} ||= new Deliantra::MoveType; if ($v) { $ob->{move_on} += "fly_low" } else { $ob->{move_on} -= "fly_low" } |
399 | } |
417 | } |
400 | if (defined (my $v = delete $ob->{fly_off})) { |
418 | if (defined (my $v = delete $ob->{fly_off})) { |
401 | $ob->{move_off} ||= new Crossfire::MoveType; if ($v) { $ob->{move_off} += "fly_low" } else { $ob->{move_off} -= "fly_low" } |
419 | $ob->{move_off} ||= new Deliantra::MoveType; if ($v) { $ob->{move_off} += "fly_low" } else { $ob->{move_off} -= "fly_low" } |
402 | } |
420 | } |
403 | if (defined (my $v = delete $ob->{flying})) { |
421 | if (defined (my $v = delete $ob->{flying})) { |
404 | $ob->{move_type} ||= new Crossfire::MoveType; if ($v) { $ob->{move_type} += "fly_low" } else { $ob->{move_type} -= "fly_low" } |
422 | $ob->{move_type} ||= new Deliantra::MoveType; if ($v) { $ob->{move_type} += "fly_low" } else { $ob->{move_type} -= "fly_low" } |
405 | } |
423 | } |
406 | |
424 | |
407 | # convert idiotic event_xxx things into objects |
425 | # convert idiotic event_xxx things into objects |
408 | while (my ($event, $subtype) = each %EVENT_TYPE) { |
426 | while (my ($event, $subtype) = each %EVENT_TYPE) { |
409 | if (exists $ob->{"event_${event}_plugin"}) { |
427 | if (exists $ob->{"event_${event}_plugin"}) { |
… | |
… | |
425 | # arch as in "arch xxx", ie.. objects |
443 | # arch as in "arch xxx", ie.. objects |
426 | sub normalize_arch($) { |
444 | sub normalize_arch($) { |
427 | my ($ob) = @_; |
445 | my ($ob) = @_; |
428 | |
446 | |
429 | normalize_object $ob; |
447 | normalize_object $ob; |
|
|
448 | |
|
|
449 | return if $ob->{_atype} eq "object"; |
430 | |
450 | |
431 | my $arch = $ARCH{$ob->{_name}} |
451 | my $arch = $ARCH{$ob->{_name}} |
432 | or (warn "$ob->{_name}: no such archetype", return $ob); |
452 | or (warn "$ob->{_name}: no such archetype", return $ob); |
433 | |
453 | |
434 | if ($arch->{type} == 22) { # map |
454 | if ($arch->{type} == 22) { # map |
… | |
… | |
470 | } |
490 | } |
471 | |
491 | |
472 | sub attr_thaw($) { |
492 | sub attr_thaw($) { |
473 | my ($ob) = @_; |
493 | my ($ob) = @_; |
474 | |
494 | |
475 | $ob->{attach} = from_json $ob->{attach} |
495 | $ob->{attach} = decode_json $ob->{attach} |
476 | if exists $ob->{attach}; |
496 | if exists $ob->{attach}; |
477 | |
497 | |
478 | $ob |
498 | $ob |
479 | } |
499 | } |
480 | |
500 | |
481 | sub attr_freeze($) { |
501 | sub attr_freeze($) { |
482 | my ($ob) = @_; |
502 | my ($ob) = @_; |
483 | |
503 | |
484 | $ob->{attach} = Crossfire::to_json $ob->{attach} |
504 | $ob->{attach} = JSON::XS->new->utf8->canonical->encode ($ob->{attach}) |
485 | if exists $ob->{attach}; |
505 | if exists $ob->{attach}; |
486 | |
506 | |
487 | $ob |
507 | $ob |
488 | } |
508 | } |
489 | |
509 | |
… | |
… | |
509 | |
529 | |
510 | my %arc; |
530 | my %arc; |
511 | my ($more, $prev); |
531 | my ($more, $prev); |
512 | my $comment; |
532 | my $comment; |
513 | |
533 | |
514 | open my $fh, "<:raw:perlio:utf8", $path |
534 | open my $fh, "<:utf8", $path |
515 | or Carp::croak "$path: $!"; |
535 | or Carp::croak "$path: $!"; |
516 | |
536 | |
517 | # binmode $fh; |
537 | # binmode $fh; |
518 | |
538 | |
519 | my $parse_block; $parse_block = sub { |
539 | my $parse_block; $parse_block = sub { |
… | |
… | |
619 | my $str; |
639 | my $str; |
620 | |
640 | |
621 | my $append; $append = sub { |
641 | my $append; $append = sub { |
622 | my %a = %{$_[0]}; |
642 | my %a = %{$_[0]}; |
623 | |
643 | |
624 | Crossfire::attr_freeze \%a; |
644 | Deliantra::attr_freeze \%a; |
625 | Crossfire::normalize_arch \%a; |
645 | Deliantra::normalize_arch \%a; |
626 | |
646 | |
627 | # undo the bit-split we did before |
647 | # undo the bit-split we did before |
628 | if (exists $a{attack_movement_bits_0_3} or exists $a{attack_movement_bits_4_7}) { |
648 | if (exists $a{attack_movement_bits_0_3} or exists $a{attack_movement_bits_4_7}) { |
629 | $a{attack_movement} = (delete $a{attack_movement_bits_0_3}) |
649 | $a{attack_movement} = (delete $a{attack_movement_bits_0_3}) |
630 | | (delete $a{attack_movement_bits_4_7}); |
650 | | (delete $a{attack_movement_bits_4_7}); |
… | |
… | |
648 | } |
668 | } |
649 | |
669 | |
650 | my @kv; |
670 | my @kv; |
651 | |
671 | |
652 | for ($a{_name} eq "map" |
672 | for ($a{_name} eq "map" |
653 | ? @Crossfire::FIELD_ORDER_MAP |
673 | ? @Deliantra::FIELD_ORDER_MAP |
654 | : @Crossfire::FIELD_ORDER) { |
674 | : @Deliantra::FIELD_ORDER) { |
655 | push @kv, [$_, delete $a{$_}] |
675 | push @kv, [$_, delete $a{$_}] |
656 | if exists $a{$_}; |
676 | if exists $a{$_}; |
657 | } |
677 | } |
658 | |
678 | |
659 | for (sort keys %a) { |
679 | for (sort keys %a) { |
… | |
… | |
662 | } |
682 | } |
663 | |
683 | |
664 | for (@kv) { |
684 | for (@kv) { |
665 | my ($k, $v) = @$_; |
685 | my ($k, $v) = @$_; |
666 | |
686 | |
667 | if (my $end = $Crossfire::FIELD_MULTILINE{$k}) { |
687 | if (my $end = $Deliantra::FIELD_MULTILINE{$k}) { |
668 | $v =~ s/\n$//; |
688 | $v =~ s/\n$//; |
669 | $str .= "$k\n$v\n$end\n"; |
689 | $str .= "$k\n$v\n$end\n"; |
670 | } else { |
690 | } else { |
671 | $str .= "$k $v\n"; |
691 | $str .= "$k $v\n"; |
672 | } |
692 | } |
… | |
… | |
768 | =cut |
788 | =cut |
769 | |
789 | |
770 | sub arch_attr($) { |
790 | sub arch_attr($) { |
771 | my ($obj) = @_; |
791 | my ($obj) = @_; |
772 | |
792 | |
773 | require Crossfire::Data; |
793 | require Deliantra::Data; |
774 | |
794 | |
775 | my $root; |
795 | my $root; |
776 | my $attr = { }; |
796 | my $attr = { }; |
777 | |
797 | |
778 | my $arch = $ARCH{ $obj->{_name} }; |
798 | my $arch = $ARCH{ $obj->{_name} }; |
779 | my $type = $obj->{type} || $arch->{type}; |
799 | my $type = $obj->{type} || $arch->{type}; |
780 | |
800 | |
781 | if ($type > 0) { |
801 | if ($type > 0) { |
782 | $root = $Crossfire::Data::ATTR{$type}; |
802 | $root = $Deliantra::Data::ATTR{$type}; |
783 | } else { |
803 | } else { |
784 | my %a = (%$arch, %$obj); |
804 | my %a = (%$arch, %$obj); |
785 | |
805 | |
786 | if ($a{is_floor} && !$a{alive}) { |
806 | if ($a{is_floor} && !$a{alive}) { |
787 | $root = $Crossfire::Data::TYPE{Floor}; |
807 | $root = $Deliantra::Data::TYPE{Floor}; |
788 | } elsif (!$a{is_floor} && $a{alive} && !$a{tear_down}) { |
808 | } elsif (!$a{is_floor} && $a{alive} && !$a{tear_down}) { |
789 | $root = $Crossfire::Data::TYPE{"Monster & NPC"}; |
809 | $root = $Deliantra::Data::TYPE{"Monster & NPC"}; |
790 | } elsif (!$a{is_floor} && !$a{alive} && $a{move_block}) { |
810 | } elsif (!$a{is_floor} && !$a{alive} && $a{move_block}) { |
791 | $root = $Crossfire::Data::TYPE{Wall}; |
811 | $root = $Deliantra::Data::TYPE{Wall}; |
792 | } elsif (!$a{is_floor} && $a{alive} && $a{tear_down}) { |
812 | } elsif (!$a{is_floor} && $a{alive} && $a{tear_down}) { |
793 | $root = $Crossfire::Data::TYPE{"Weak Wall"}; |
813 | $root = $Deliantra::Data::TYPE{"Weak Wall"}; |
794 | } else { |
814 | } else { |
795 | $root = $Crossfire::Data::TYPE{Misc}; |
815 | $root = $Deliantra::Data::TYPE{Misc}; |
796 | } |
816 | } |
797 | } |
817 | } |
798 | |
818 | |
799 | my @import = ($root); |
819 | my @import = ($root); |
800 | |
820 | |
801 | unshift @import, \%Crossfire::Data::DEFAULT_ATTR |
821 | unshift @import, \%Deliantra::Data::DEFAULT_ATTR |
802 | unless $type == 116; |
822 | unless $type == 116; |
803 | |
823 | |
804 | my (%ignore); |
824 | my (%ignore); |
805 | my (@section_order, %section, @attr_order); |
825 | my (@section_order, %section, @attr_order); |
806 | |
826 | |
807 | while (my $type = shift @import) { |
827 | while (my $type = shift @import) { |
|
|
828 | push @import, |
|
|
829 | grep $_, |
|
|
830 | map $Deliantra::Data::TYPE{$_}, |
808 | push @import, @{$type->{import} || []}; |
831 | @{$type->{import} || []}; |
809 | |
832 | |
810 | $attr->{$_} ||= $type->{$_} |
833 | $attr->{$_} ||= $type->{$_} |
811 | for qw(name desc use); |
834 | for qw(name desc use); |
812 | |
835 | |
813 | for (@{$type->{ignore} || []}) { |
836 | for (@{$type->{ignore} || []}) { |
… | |
… | |
902 | } |
925 | } |
903 | |
926 | |
904 | sub construct_tilecache_pb { |
927 | sub construct_tilecache_pb { |
905 | my ($idx, $cache) = @_; |
928 | my ($idx, $cache) = @_; |
906 | |
929 | |
907 | my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64; |
930 | my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, CACHESTRIDE * TILESIZE, TILESIZE * int +($idx + CACHESTRIDE - 1) / CACHESTRIDE; |
908 | |
931 | |
909 | while (my ($name, $tile) = each %$cache) { |
932 | while (my ($name, $tile) = each %$cache) { |
910 | my $tpb = delete $tile->{pb}; |
933 | my $tpb = delete $tile->{pb}; |
911 | my $ofs = $tile->{idx}; |
934 | my $ofs = $tile->{idx}; |
912 | |
935 | |
913 | for my $x (0 .. $tile->{w} - 1) { |
936 | for my $x (0 .. $tile->{w} - 1) { |
914 | for my $y (0 .. $tile->{h} - 1) { |
937 | for my $y (0 .. $tile->{h} - 1) { |
915 | my $idx = $ofs + $x + $y * $tile->{w}; |
938 | my $idx = $ofs + $x + $y * $tile->{w}; |
916 | $tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE, |
939 | $tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE, |
917 | $pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64); |
940 | $pb, ($idx % CACHESTRIDE) * TILESIZE, TILESIZE * int $idx / CACHESTRIDE); |
918 | } |
941 | } |
919 | } |
942 | } |
920 | } |
943 | } |
921 | |
944 | |
922 | $pb->save ("$VARDIR/tilecache.png", "png", compression => 1); |
945 | $pb->save ("$VARDIR/tilecache.png", "png", compression => 1); |
… | |
… | |
938 | =cut |
961 | =cut |
939 | |
962 | |
940 | sub load_tilecache() { |
963 | sub load_tilecache() { |
941 | require Gtk2; |
964 | require Gtk2; |
942 | |
965 | |
943 | if (-e "$LIB/facedata") { # Crossfire TRT faces |
|
|
944 | cache_file "$LIB/facedata", "$VARDIR/tilecache.pst", \&use_tilecache, |
966 | cache_file "$LIB/facedata", "$VARDIR/tilecache.pst", \&use_tilecache, |
945 | sub { |
967 | sub { |
946 | my %cache; |
968 | my %cache; |
947 | my $facedata = Storable::retrieve "$LIB/facedata"; |
969 | my $facedata = Storable::retrieve "$LIB/facedata"; |
948 | |
970 | |
949 | $facedata->{version} == 2 |
971 | $facedata->{version} == 2 |
950 | or die "$LIB/facedata: version mismatch, cannot proceed."; |
972 | or die "$LIB/facedata: version mismatch, cannot proceed."; |
951 | |
973 | |
952 | my $faces = $facedata->{faceinfo}; |
974 | my $faces = $facedata->{faceinfo}; |
953 | my $idx = 0; |
975 | my $idx = 0; |
954 | |
976 | |
955 | for (sort keys %$faces) { |
977 | for (sort keys %$faces) { |
956 | my ($face, $info) = ($_, $faces->{$_}); |
978 | my ($face, $info) = ($_, $faces->{$_}); |
957 | |
979 | |
958 | my $pb = new Gtk2::Gdk::PixbufLoader; |
980 | my $pb = new Gtk2::Gdk::PixbufLoader; |
959 | $pb->write ($info->{data32}); |
981 | $pb->write ($info->{data32}); |
960 | $pb->close; |
982 | $pb->close; |
961 | my $pb = $pb->get_pixbuf; |
983 | my $pb = $pb->get_pixbuf; |
962 | |
984 | |
963 | my $tile = $cache{$face} = { |
985 | my $tile = $cache{$face} = { |
964 | pb => $pb, |
986 | pb => $pb, |
965 | idx => $idx, |
987 | idx => $idx, |
966 | w => int $pb->get_width / TILESIZE, |
988 | w => int $pb->get_width / TILESIZE, |
967 | h => int $pb->get_height / TILESIZE, |
989 | h => int $pb->get_height / TILESIZE, |
968 | }; |
990 | }; |
969 | |
991 | |
970 | $idx += $tile->{w} * $tile->{h}; |
992 | $idx += $tile->{w} * $tile->{h}; |
971 | } |
993 | } |
972 | |
994 | |
973 | construct_tilecache_pb $idx, \%cache; |
995 | construct_tilecache_pb $idx, \%cache; |
974 | |
996 | |
975 | \%cache |
997 | \%cache |
976 | }; |
998 | }; |
977 | |
|
|
978 | } elsif (-e "$LIB/crossfire.0") { # Crossfire1 version |
|
|
979 | cache_file "$LIB/crossfire.0", "$VARDIR/tilecache.pst", \&use_tilecache, |
|
|
980 | sub { |
|
|
981 | my $tile = read_pak "$LIB/crossfire.0"; |
|
|
982 | |
|
|
983 | my %cache; |
|
|
984 | |
|
|
985 | my $idx = 0; |
|
|
986 | |
|
|
987 | for my $name (sort keys %$tile) { |
|
|
988 | my $pb = new Gtk2::Gdk::PixbufLoader; |
|
|
989 | $pb->write ($tile->{$name}); |
|
|
990 | $pb->close; |
|
|
991 | my $pb = $pb->get_pixbuf; |
|
|
992 | |
|
|
993 | my $tile = $cache{$name} = { |
|
|
994 | pb => $pb, |
|
|
995 | idx => $idx, |
|
|
996 | w => int $pb->get_width / TILESIZE, |
|
|
997 | h => int $pb->get_height / TILESIZE, |
|
|
998 | }; |
|
|
999 | |
|
|
1000 | $idx += $tile->{w} * $tile->{h}; |
|
|
1001 | } |
|
|
1002 | |
|
|
1003 | construct_tilecache_pb $idx, \%cache; |
|
|
1004 | |
|
|
1005 | \%cache |
|
|
1006 | }; |
|
|
1007 | |
|
|
1008 | } |
|
|
1009 | } |
999 | } |
|
|
1000 | |
|
|
1001 | =back |
1010 | |
1002 | |
1011 | =head1 AUTHOR |
1003 | =head1 AUTHOR |
1012 | |
1004 | |
1013 | Marc Lehmann <schmorp@schmorp.de> |
1005 | Marc Lehmann <schmorp@schmorp.de> |
1014 | http://home.schmorp.de/ |
1006 | http://home.schmorp.de/ |
… | |
… | |
1017 | http://www.ta-sa.org/ |
1009 | http://www.ta-sa.org/ |
1018 | |
1010 | |
1019 | =cut |
1011 | =cut |
1020 | |
1012 | |
1021 | 1 |
1013 | 1 |
|
|
1014 | |