ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/Util.pm
Revision: 1.15
Committed: Sun Apr 2 17:57:37 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.14: +4 -0 lines
Log Message:
moved documentation out of the notebook and added some more pseudohtml parsing

File Contents

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