1 |
elmex |
1.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 |
elmex |
1.2 |
use Crossfire; |
19 |
|
|
use Crossfire::MapWidget; |
20 |
|
|
|
21 |
elmex |
1.7 |
our @EXPORT = qw(insert_arch_stack_layer replace_arch_stack_layer new_arch_pb fill_pb_from_arch arch_is_floor stack_find_floor stack_find_wall stack_find arch_is_wall arch_is_monster add_table_widget quick_msg def arch_is_exit); |
22 |
elmex |
1.5 |
|
23 |
|
|
sub def($$) { |
24 |
|
|
return defined ($_[0]) ? $_[0] : $_[1]; |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
sub quick_msg { |
28 |
|
|
my $wid = shift; |
29 |
|
|
my $msg; |
30 |
|
|
my $win = $::MAINWIN; |
31 |
|
|
if (ref $wid) { |
32 |
|
|
$win = $wid; |
33 |
|
|
$msg = shift; |
34 |
|
|
} else { |
35 |
|
|
$msg = $wid; |
36 |
|
|
} |
37 |
|
|
my $dia = Gtk2::Dialog->new ('Message', $win, 'destroy-with-parent', 'gtk-ok' => 'none'); |
38 |
|
|
|
39 |
|
|
my $lbl = Gtk2::Label->new ($msg); |
40 |
|
|
$dia->vbox->add ($lbl); |
41 |
|
|
$dia->signal_connect (response => sub { $_[0]->destroy }); |
42 |
|
|
|
43 |
|
|
unless (defined $_[0]) { |
44 |
|
|
Glib::Timeout->add (1000, sub { $dia->destroy; 0 }); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
$dia->show_all; |
48 |
|
|
} |
49 |
elmex |
1.2 |
|
50 |
|
|
sub new_arch_pb { |
51 |
|
|
# this is awful, is this really the best way? |
52 |
|
|
my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE; |
53 |
|
|
return $pb; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
sub fill_pb_from_arch { |
57 |
elmex |
1.6 |
my ($pb, $a) = @_; |
58 |
|
|
|
59 |
|
|
my $o = $Crossfire::ARCH{$a->{_name}}; |
60 |
|
|
my $face = $Crossfire::FACE{$a->{face} || $o->{face} || "blank.111"} |
61 |
|
|
or warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n"; |
62 |
|
|
|
63 |
|
|
$face or return; |
64 |
elmex |
1.2 |
|
65 |
|
|
$pb->fill (0x00000000); |
66 |
|
|
$TILE->composite ($pb, |
67 |
|
|
0, 0, |
68 |
|
|
TILESIZE, TILESIZE, |
69 |
elmex |
1.6 |
- ($face->{idx} % 64) * TILESIZE, - TILESIZE * int $face->{idx} / 64, |
70 |
elmex |
1.2 |
1, 1, 'nearest', 255 |
71 |
|
|
); |
72 |
|
|
} |
73 |
elmex |
1.1 |
|
74 |
|
|
sub classify_arch_layer { |
75 |
|
|
my ($arch) = @_; |
76 |
|
|
|
77 |
|
|
if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals) |
78 |
|
|
|
79 |
|
|
return 'below'; |
80 |
|
|
|
81 |
|
|
} elsif ($arch->{monster}) { |
82 |
|
|
|
83 |
|
|
return 'top'; |
84 |
|
|
|
85 |
|
|
} else { # $arch->{is_floor} and all other arches are 'between' monsters and floor |
86 |
|
|
|
87 |
|
|
return 'between'; |
88 |
|
|
} |
89 |
|
|
} |
90 |
|
|
|
91 |
elmex |
1.7 |
sub arch_is_exit { |
92 |
|
|
my ($a) = @_; |
93 |
|
|
my $type = $Crossfire::ARCH{$a->{_name}}->{type}; |
94 |
|
|
return $type eq '66' || $type eq '41'; |
95 |
|
|
} |
96 |
|
|
|
97 |
elmex |
1.2 |
sub arch_is_floor { |
98 |
|
|
my ($a) = @_; |
99 |
|
|
return $Crossfire::ARCH{$a->{_name}}->{is_floor}; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
sub arch_is_wall { |
103 |
|
|
my ($a) = @_; |
104 |
|
|
return $Crossfire::ARCH{$a->{_name}}->{no_pass}; |
105 |
|
|
} |
106 |
|
|
|
107 |
elmex |
1.3 |
sub arch_is_monster { |
108 |
|
|
my ($a) = @_; |
109 |
|
|
my $arch = $Crossfire::ARCH{$a->{_name}}; |
110 |
|
|
return $arch->{alive} and ($arch->{monster} or $arch->{generator}); |
111 |
|
|
} |
112 |
|
|
|
113 |
elmex |
1.2 |
sub stack_find { |
114 |
|
|
my ($stack, $dir, $pred) = @_; |
115 |
|
|
|
116 |
|
|
|
117 |
|
|
if ($dir eq 'from_top') { |
118 |
|
|
my $i = scalar (@$stack) - 1; |
119 |
|
|
if ($i < 0) { $i = 0 } |
120 |
|
|
|
121 |
|
|
for (reverse @$stack) { |
122 |
|
|
$pred->($_) |
123 |
|
|
and return $i; |
124 |
|
|
|
125 |
|
|
$i--; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
} else { |
129 |
|
|
my $i = 0; |
130 |
|
|
|
131 |
|
|
for (@$stack) { |
132 |
|
|
$pred->($_) |
133 |
|
|
and return $i; |
134 |
|
|
|
135 |
|
|
$i++; |
136 |
|
|
} |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
return 0; |
140 |
|
|
|
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
sub stack_find_floor { |
144 |
|
|
my ($stack, $dir) = @_; |
145 |
|
|
return stack_find ($stack, $dir, \&arch_is_floor); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
sub stack_find_wall { |
149 |
|
|
my ($stack, $dir) = @_; |
150 |
|
|
return stack_find ($stack, $dir, \&arch_is_wall); |
151 |
|
|
} |
152 |
|
|
|
153 |
elmex |
1.1 |
sub insert_arch_stack_layer { |
154 |
|
|
my ($stack, $arch) = @_; |
155 |
|
|
|
156 |
|
|
unless (@$stack) { |
157 |
|
|
return [ $arch ]; |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
my @outstack; |
161 |
|
|
|
162 |
|
|
my $l = classify_arch_layer ($Crossfire::ARCH{$arch->{_name}}); |
163 |
|
|
|
164 |
|
|
if ($l eq 'between') { |
165 |
|
|
|
166 |
|
|
# loop until we reached the first 'between' arch above 'below' arches and the floor |
167 |
|
|
while (my $a = shift @$stack) { |
168 |
|
|
|
169 |
|
|
unless ($Crossfire::ARCH{$a->{_name}}->{is_floor} |
170 |
|
|
or classify_arch_layer ($Crossfire::ARCH{$a->{_name}}) eq 'below') { |
171 |
|
|
|
172 |
|
|
unshift @$stack, $a; |
173 |
|
|
last; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
push @outstack, $a; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
# ignore duplicates |
180 |
|
|
# FIXME: Broken if non-floor are drawn (too tired to fix) |
181 |
|
|
return [ @outstack, @$stack ] |
182 |
|
|
if @outstack and $outstack[-1]->{_name} eq $arch->{_name}; |
183 |
|
|
|
184 |
|
|
push @outstack, ($arch, @$stack); |
185 |
|
|
|
186 |
|
|
} elsif ($l eq 'top') { |
187 |
|
|
|
188 |
|
|
# ignore duplicates |
189 |
|
|
return [ @$stack ] |
190 |
|
|
if $stack->[-1]->{_name} eq $arch->{_name}; |
191 |
|
|
|
192 |
|
|
@outstack = (@$stack, $arch); |
193 |
|
|
|
194 |
|
|
} else { |
195 |
|
|
|
196 |
|
|
# ignore duplicates |
197 |
|
|
return [ @$stack ] |
198 |
|
|
if $stack->[0]->{_name} eq $arch->{_name}; |
199 |
|
|
|
200 |
|
|
@outstack = ($arch, @$stack); |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
return \@outstack; |
204 |
|
|
} |
205 |
|
|
|
206 |
elmex |
1.4 |
sub add_table_widget { |
207 |
|
|
my ($table, $row, $data, $type, $cb) = @_; |
208 |
|
|
my $edwid; |
209 |
|
|
|
210 |
|
|
if ($type eq 'string') { |
211 |
|
|
$table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1); |
212 |
|
|
$edwid = Gtk2::Entry->new; |
213 |
|
|
$edwid->set_text ($data->[1]); |
214 |
|
|
$edwid->signal_connect (changed => sub { |
215 |
|
|
$data->[1] = $_[0]->get_text; |
216 |
|
|
$cb->($data->[1]) if $cb; |
217 |
|
|
}); |
218 |
|
|
$table->attach_defaults ($edwid, 1, 2, $row, $row + 1); |
219 |
|
|
|
220 |
|
|
} elsif ($type eq 'button') { |
221 |
|
|
$table->attach_defaults (my $b = Gtk2::Button->new_with_label ($data), 0, 2, $row, $row + 1); |
222 |
|
|
$b->signal_connect (clicked => ($cb || sub {})); |
223 |
|
|
|
224 |
|
|
} elsif ($type eq 'label') { |
225 |
|
|
$table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1); |
226 |
|
|
$edwid = Gtk2::Label->new ($data->[1]); |
227 |
|
|
$table->attach_defaults ($edwid, 1, 2, $row, $row + 1); |
228 |
|
|
|
229 |
|
|
} else { |
230 |
|
|
$edwid = Gtk2::Label->new ("FOO"); |
231 |
|
|
} |
232 |
|
|
} |
233 |
|
|
|
234 |
elmex |
1.1 |
sub replace_arch_stack_layer { |
235 |
|
|
my ($stack, $arch) = @_; |
236 |
|
|
|
237 |
|
|
my @outstack; |
238 |
|
|
|
239 |
|
|
my $l = classify_arch_layer ($Crossfire::ARCH{$arch->{_name}}); |
240 |
|
|
|
241 |
|
|
if ($l eq 'between') { |
242 |
|
|
|
243 |
|
|
while (shift @$stack) { |
244 |
|
|
last unless $Crossfire::ARCH{$_->{_name}}->{is_floor}; |
245 |
|
|
push @outstack, $_; |
246 |
|
|
} |
247 |
|
|
|
248 |
|
|
if (@outstack and $Crossfire::ARCH{$outstack[-1]->{_name}}->{is_floor}) { |
249 |
|
|
pop @outstack; |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
push @outstack, ($arch, @$stack); |
253 |
|
|
|
254 |
|
|
} elsif ($l eq 'top') { |
255 |
|
|
|
256 |
|
|
@outstack = (@$stack, $arch); |
257 |
|
|
|
258 |
|
|
} else { |
259 |
|
|
|
260 |
|
|
@outstack = ($arch, @$stack); |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
return \@outstack; |
264 |
|
|
} |
265 |
|
|
|
266 |
|
|
=head1 AUTHOR |
267 |
|
|
|
268 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
269 |
|
|
http://home.schmorp.de/ |
270 |
|
|
|
271 |
|
|
Robin Redeker <elmex@ta-sa.org> |
272 |
|
|
http://www.ta-sa.org/ |
273 |
|
|
|
274 |
|
|
=cut |
275 |
|
|
1; |