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.15 by elmex, Sun Apr 2 17:57:37 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/<\/?\s*li>//gi;
31 $html =~ s/<\/?\s*ul>//gi;
32 $html =~ s/&gt;/>/g;
33 $html =~ s/&lt;/</g;
34 $html
35}
36
37sub 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
66sub 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
79sub def($$) {
80 return defined ($_[0]) ? $_[0] : $_[1];
81}
82
83sub 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
106sub 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
112sub 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}
19 129
20sub classify_arch_layer { 130sub classify_arch_layer {
21 my ($arch) = @_; 131 my ($arch) = @_;
22 132
23 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals) 133 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals)
30 140
31 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor 141 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor
32 142
33 return 'between'; 143 return 'between';
34 } 144 }
145}
146
147sub arch_is_exit {
148 my ($a) = @_;
149 my $type = $Crossfire::ARCH{$a->{_name}}->{type};
150 return $type eq '66' || $type eq '41';
151}
152
153sub 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
160sub 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
167sub 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
173sub 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
203sub stack_find_floor {
204 my ($stack, $dir) = @_;
205 return stack_find ($stack, $dir, \&arch_is_floor);
206}
207
208sub stack_find_wall {
209 my ($stack, $dir) = @_;
210 return stack_find ($stack, $dir, \&arch_is_wall);
35} 211}
36 212
37sub insert_arch_stack_layer { 213sub insert_arch_stack_layer {
38 my ($stack, $arch) = @_; 214 my ($stack, $arch) = @_;
39 215
85 } 261 }
86 262
87 return \@outstack; 263 return \@outstack;
88} 264}
89 265
266sub 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
90sub replace_arch_stack_layer { 294sub replace_arch_stack_layer {
91 my ($stack, $arch) = @_; 295 my ($stack, $arch) = @_;
92 296
93 my @outstack; 297 my @outstack;
94 298

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines