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