1 |
package GCE::Util; |
2 |
=head1 NAME |
3 |
|
4 |
GCE::Util - some utility functions |
5 |
|
6 |
=over 4 |
7 |
|
8 |
=cut |
9 |
|
10 |
use base 'Exporter'; |
11 |
|
12 |
use Crossfire; |
13 |
|
14 |
use Carp (); |
15 |
use Storable; |
16 |
use List::Util qw(min max); |
17 |
|
18 |
our @EXPORT = qw(insert_arch_stack_layer replace_arch_stack_layer); |
19 |
|
20 |
sub classify_arch_layer { |
21 |
my ($arch) = @_; |
22 |
|
23 |
if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals) |
24 |
|
25 |
return 'below'; |
26 |
|
27 |
} elsif ($arch->{monster}) { |
28 |
|
29 |
return 'top'; |
30 |
|
31 |
} else { # $arch->{is_floor} and all other arches are 'between' monsters and floor |
32 |
|
33 |
return 'between'; |
34 |
} |
35 |
} |
36 |
|
37 |
sub insert_arch_stack_layer { |
38 |
my ($stack, $arch) = @_; |
39 |
|
40 |
unless (@$stack) { |
41 |
return [ $arch ]; |
42 |
} |
43 |
|
44 |
my @outstack; |
45 |
|
46 |
my $l = classify_arch_layer ($Crossfire::ARCH{$arch->{_name}}); |
47 |
|
48 |
if ($l eq 'between') { |
49 |
|
50 |
# loop until we reached the first 'between' arch above 'below' arches and the floor |
51 |
while (my $a = shift @$stack) { |
52 |
|
53 |
unless ($Crossfire::ARCH{$a->{_name}}->{is_floor} |
54 |
or classify_arch_layer ($Crossfire::ARCH{$a->{_name}}) eq 'below') { |
55 |
|
56 |
unshift @$stack, $a; |
57 |
last; |
58 |
} |
59 |
|
60 |
push @outstack, $a; |
61 |
} |
62 |
|
63 |
# ignore duplicates |
64 |
# FIXME: Broken if non-floor are drawn (too tired to fix) |
65 |
return [ @outstack, @$stack ] |
66 |
if @outstack and $outstack[-1]->{_name} eq $arch->{_name}; |
67 |
|
68 |
push @outstack, ($arch, @$stack); |
69 |
|
70 |
} elsif ($l eq 'top') { |
71 |
|
72 |
# ignore duplicates |
73 |
return [ @$stack ] |
74 |
if $stack->[-1]->{_name} eq $arch->{_name}; |
75 |
|
76 |
@outstack = (@$stack, $arch); |
77 |
|
78 |
} else { |
79 |
|
80 |
# ignore duplicates |
81 |
return [ @$stack ] |
82 |
if $stack->[0]->{_name} eq $arch->{_name}; |
83 |
|
84 |
@outstack = ($arch, @$stack); |
85 |
} |
86 |
|
87 |
return \@outstack; |
88 |
} |
89 |
|
90 |
sub replace_arch_stack_layer { |
91 |
my ($stack, $arch) = @_; |
92 |
|
93 |
my @outstack; |
94 |
|
95 |
my $l = classify_arch_layer ($Crossfire::ARCH{$arch->{_name}}); |
96 |
|
97 |
if ($l eq 'between') { |
98 |
|
99 |
while (shift @$stack) { |
100 |
last unless $Crossfire::ARCH{$_->{_name}}->{is_floor}; |
101 |
push @outstack, $_; |
102 |
} |
103 |
|
104 |
if (@outstack and $Crossfire::ARCH{$outstack[-1]->{_name}}->{is_floor}) { |
105 |
pop @outstack; |
106 |
} |
107 |
|
108 |
push @outstack, ($arch, @$stack); |
109 |
|
110 |
} elsif ($l eq 'top') { |
111 |
|
112 |
@outstack = (@$stack, $arch); |
113 |
|
114 |
} else { |
115 |
|
116 |
@outstack = ($arch, @$stack); |
117 |
} |
118 |
|
119 |
return \@outstack; |
120 |
} |
121 |
|
122 |
=head1 AUTHOR |
123 |
|
124 |
Marc Lehmann <schmorp@schmorp.de> |
125 |
http://home.schmorp.de/ |
126 |
|
127 |
Robin Redeker <elmex@ta-sa.org> |
128 |
http://www.ta-sa.org/ |
129 |
|
130 |
=cut |
131 |
1; |