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