ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/Util.pm
Revision: 1.22
Committed: Tue Nov 28 16:26:22 2006 UTC (17 years, 7 months ago) by elmex
Branch: MAIN
Changes since 1.21: +9 -2 lines
Log Message:
removed some debugging output.
and some changes on HashDialog.

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