ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/Util.pm
(Generate patch)

Comparing deliantra/gde/GCE/Util.pm (file contents):
Revision 1.1 by elmex, Mon Feb 20 18:21:04 2006 UTC vs.
Revision 1.16 by elmex, Sun Apr 2 18:32:42 2006 UTC

13 13
14use Carp (); 14use Carp ();
15use Storable; 15use Storable;
16use List::Util qw(min max); 16use List::Util qw(min max);
17 17
18our @EXPORT = qw(insert_arch_stack_layer replace_arch_stack_layer); 18use Crossfire;
19use Crossfire::MapWidget;
20use File::Spec::Functions;
21use Cwd 'abs_path';
22
23our @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
25sub pseudohtml2txt {
26 my ($html) = @_;
27
28 $html =~ s/<br\s*?\/?>/\n/gsi;
29 $html =~ s/<b>(.*?)<\/b>/_\1_/gsi;
30 $html =~ s/<li>/\n* /gi;
31 $html =~ s/<\/?\s*li>//gi;
32 $html =~ s/<\/?\s*ul>//gi;
33 $html =~ s/&gt;/>/g;
34 $html =~ s/&lt;/</g;
35 $html
36}
37
38sub exit_paths {
39 my ($mappath, $map1path, $map2path) = @_;
40 $mappath = abs_path $mappath;
41 $map1path = abs_path $map1path;
42 $map2path = abs_path $map2path;
43
44 if ( (substr $map1path, 0, length $mappath) eq $mappath
45 and (substr $map2path, 0, length $mappath) eq $mappath) {
46 substr $map1path, 0, length $mappath, '';
47 substr $map2path, 0, length $mappath, '';
48
49 my ($v1, $d1, $f1) = File::Spec->splitpath ($map1path);
50 my ($v2, $d2, $f2) = File::Spec->splitpath ($map2path);
51
52 my @di1 = File::Spec->splitdir ($d1);
53 my @di2 = File::Spec->splitdir ($d2);
54
55 if ((defined $di1[1]) and (defined $di2[1]) and $di1[1] eq $di2[1]) {
56 my $m1 = File::Spec->abs2rel ($map1path, File::Spec->catdir (@di2));
57 my $m2 = File::Spec->abs2rel ($map2path, File::Spec->catdir (@di1));
58 return ($m1, $m2);
59 } else {
60 return ($map1path, $map2path);
61 }
62 } else {
63 return ('', '');
64 }
65}
66
67sub map2abs {
68 my ($dest, $mape) = @_;
69
70 my $dir;
71 if (File::Spec->file_name_is_absolute($dest)) {
72 $dir = catdir ($::CFG->{MAPDIR}, $dest);
73 } else {
74 my ($v, $p, $f) = File::Spec->splitpath ($mape->{path});
75 $dir = File::Spec->rel2abs ($dest, File::Spec->catpath ($v, $p));
76 }
77 return $dir;
78}
79
80sub def($$) {
81 return defined ($_[0]) ? $_[0] : $_[1];
82}
83
84sub quick_msg {
85 my $wid = shift;
86 my $msg;
87 my $win = $::MAINWIN;
88 if (ref $wid) {
89 $win = $wid;
90 $msg = shift;
91 } else {
92 $msg = $wid;
93 }
94 my $dia = Gtk2::Dialog->new ('Message', $win, 'destroy-with-parent', 'gtk-ok' => 'none');
95
96 my $lbl = Gtk2::Label->new ($msg);
97 $dia->vbox->add ($lbl);
98 $dia->signal_connect (response => sub { $_[0]->destroy });
99
100 unless (defined $_[0]) {
101 Glib::Timeout->add (1000, sub { $dia->destroy; 0 });
102 }
103
104 $dia->show_all;
105}
106
107sub new_arch_pb {
108 # this is awful, is this really the best way?
109 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
110 return $pb;
111}
112
113sub fill_pb_from_arch {
114 my ($pb, $a) = @_;
115
116 my $o = $Crossfire::ARCH{$a->{_name}};
117 my $face = $Crossfire::FACE{$a->{face} || $o->{face} || "blank.111"}
118 or warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n";
119
120 $face or return;
121
122 $pb->fill (0x00000000);
123 $TILE->composite ($pb,
124 0, 0,
125 TILESIZE, TILESIZE,
126 - ($face->{idx} % 64) * TILESIZE, - TILESIZE * int $face->{idx} / 64,
127 1, 1, 'nearest', 255
128 );
129}
19 130
20sub classify_arch_layer { 131sub classify_arch_layer {
21 my ($arch) = @_; 132 my ($arch) = @_;
22 133
23 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals) 134 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals)
30 141
31 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor 142 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor
32 143
33 return 'between'; 144 return 'between';
34 } 145 }
146}
147
148sub arch_is_exit {
149 my ($a) = @_;
150 my $type = $Crossfire::ARCH{$a->{_name}}->{type};
151 return $type eq '66' || $type eq '41';
152}
153
154sub arch_is_floor {
155 my ($a) = @_;
156 my $ar = Crossfire::arch_attr $a;
157 return (substr $ar->{name}, 0, 5) eq 'Floor';
158#return $Crossfire::ARCH{$a->{_name}}->{is_floor};
159}
160
161sub arch_is_wall {
162 my ($a) = @_;
163 my $ar = Crossfire::arch_attr $a;
164 return $ar->{name} eq 'Wall';
165#return $Crossfire::ARCH{$a->{_name}}->{no_pass};
166}
167
168sub arch_is_monster {
169 my ($a) = @_;
170 my $arch = $Crossfire::ARCH{$a->{_name}};
171 return $arch->{alive} and ($arch->{monster} or $arch->{generator});
172}
173
174sub stack_find {
175 my ($stack, $dir, $pred) = @_;
176
177
178 if ($dir eq 'from_top') {
179 my $i = scalar (@$stack) - 1;
180 if ($i < 0) { $i = 0 }
181
182 for (reverse @$stack) {
183 $pred->($_)
184 and return $i;
185
186 $i--;
187 }
188
189 } else {
190 my $i = 0;
191
192 for (@$stack) {
193 $pred->($_)
194 and return $i;
195
196 $i++;
197 }
198 }
199
200 return 0;
201
202}
203
204sub stack_find_floor {
205 my ($stack, $dir) = @_;
206 return stack_find ($stack, $dir, \&arch_is_floor);
207}
208
209sub stack_find_wall {
210 my ($stack, $dir) = @_;
211 return stack_find ($stack, $dir, \&arch_is_wall);
35} 212}
36 213
37sub insert_arch_stack_layer { 214sub insert_arch_stack_layer {
38 my ($stack, $arch) = @_; 215 my ($stack, $arch) = @_;
39 216
85 } 262 }
86 263
87 return \@outstack; 264 return \@outstack;
88} 265}
89 266
267sub add_table_widget {
268 my ($table, $row, $data, $type, $cb) = @_;
269 my $edwid;
270
271 if ($type eq 'string') {
272 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
273 $edwid = Gtk2::Entry->new;
274 $edwid->set_text ($data->[1]);
275 $edwid->signal_connect (changed => sub {
276 $data->[1] = $_[0]->get_text;
277 $cb->($data->[1]) if $cb;
278 });
279 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
280
281 } elsif ($type eq 'button') {
282 $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($data), 0, 2, $row, $row + 1);
283 $b->signal_connect (clicked => ($cb || sub {}));
284
285 } elsif ($type eq 'label') {
286 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
287 $edwid = Gtk2::Label->new ($data->[1]);
288 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
289
290 } else {
291 $edwid = Gtk2::Label->new ("FOO");
292 }
293}
294
90sub replace_arch_stack_layer { 295sub replace_arch_stack_layer {
91 my ($stack, $arch) = @_; 296 my ($stack, $arch) = @_;
92 297
93 my @outstack; 298 my @outstack;
94 299

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines