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.19 by elmex, Mon Aug 14 03:55:50 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
18use Crossfire;
19use Crossfire::MapWidget;
20use File::Spec::Functions;
21use Cwd 'abs_path';
22
18our @EXPORT = qw(insert_arch_stack_layer replace_arch_stack_layer); 23our @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
28sub pseudohtml2txt {
29 my ($html) = @_;
30
31 $html =~ s/<br\s*?\/?>/\n/gsi;
32 $html =~ s/<b>(.*?)<\/b>/_\1_/gsi;
33 $html =~ s/<li>/\n* /gi;
34 $html =~ s/<\/?\s*li>//gi;
35 $html =~ s/<\/?\s*ul>//gi;
36 $html =~ s/&gt;/>/g;
37 $html =~ s/&lt;/</g;
38 $html
39}
40
41sub 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
70sub map2abs {
71 my ($dest, $mape) = @_;
72
73 $mappath = abs_path $mappath;
74 my $dir;
75 if (File::Spec->file_name_is_absolute($dest)) {
76 $dir = catdir ($::CFG->{MAPDIR}, $dest);
77 } 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
84sub def($$) {
85 return defined ($_[0]) ? $_[0] : $_[1];
86}
87
88sub 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
111sub 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
117sub fill_pb_from_arch {
118 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
126 $pb->fill (0x00000000);
127 $TILE->composite ($pb,
128 0, 0,
129 TILESIZE, TILESIZE,
130 - ($face->{idx} % 64) * TILESIZE, - TILESIZE * int $face->{idx} / 64,
131 1, 1, 'nearest', 255
132 );
133}
19 134
20sub classify_arch_layer { 135sub classify_arch_layer {
21 my ($arch) = @_; 136 my ($arch) = @_;
22 137
23 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals) 138 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals)
30 145
31 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor 146 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor
32 147
33 return 'between'; 148 return 'between';
34 } 149 }
150}
151
152sub arch_is_exit {
153 my ($a) = @_;
154 my $type = $Crossfire::ARCH{$a->{_name}}->{type};
155 return $type eq '66' || $type eq '41';
156}
157
158sub arch_is_floor {
159 my ($a) = @_;
160 my $ar = Crossfire::arch_attr $a;
161 warn "NAME: $ar->{name}\n";
162 return (
163 (substr $ar->{name}, 0, 5) eq 'Floor'
164 or (substr $ar->{name}, 0, 10) eq 'Shop Floor'
165 )
166}
167
168sub arch_is_connector {
169 my ($a) = @_;
170 my $ar = Crossfire::arch_attr $a;
171 my $has_connect_field = 0;
172
173 TOP: for (@{$ar->{section}}) {
174 my $name = shift @$_;
175 my @r = @$_;
176 if ($name eq 'general') {
177 for (@r) {
178 my ($k, $s) = ($_->[0], $_->[1]);
179 if ($k eq 'connected' && $s->{name} eq 'connection') {
180 $has_connect_field = 1;
181 last TOP;
182 }
183 }
184 last TOP;
185 }
186 }
187
188 return $has_connect_field;
189}
190
191sub arch_is_wall {
192 my ($a) = @_;
193 my $ar = Crossfire::arch_attr $a;
194 return $ar->{name} eq 'Wall';
195#return $Crossfire::ARCH{$a->{_name}}->{no_pass};
196}
197
198sub arch_is_monster {
199 my ($a) = @_;
200 my $arch = $Crossfire::ARCH{$a->{_name}};
201 return $arch->{alive} and ($arch->{monster} or $arch->{generator});
202}
203
204sub stack_find {
205 my ($stack, $dir, $pred) = @_;
206
207
208 if ($dir eq 'from_top') {
209 my $i = scalar (@$stack) - 1;
210 if ($i < 0) { $i = 0 }
211
212 for (reverse @$stack) {
213 $pred->($_)
214 and return $i;
215
216 $i--;
217 }
218
219 } else {
220 my $i = 0;
221
222 for (@$stack) {
223 $pred->($_)
224 and return $i;
225
226 $i++;
227 }
228 }
229
230 return 0;
231
232}
233
234sub stack_find_floor {
235 my ($stack, $dir) = @_;
236 return stack_find ($stack, $dir, \&arch_is_floor);
237}
238
239sub stack_find_wall {
240 my ($stack, $dir) = @_;
241 return stack_find ($stack, $dir, \&arch_is_wall);
35} 242}
36 243
37sub insert_arch_stack_layer { 244sub insert_arch_stack_layer {
38 my ($stack, $arch) = @_; 245 my ($stack, $arch) = @_;
39 246
85 } 292 }
86 293
87 return \@outstack; 294 return \@outstack;
88} 295}
89 296
297sub add_table_widget {
298 my ($table, $row, $data, $type, $cb) = @_;
299 my $edwid;
300
301 if ($type eq 'string') {
302 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
303 $edwid = Gtk2::Entry->new;
304 $edwid->set_text ($data->[1]);
305 $edwid->signal_connect (changed => sub {
306 $data->[1] = $_[0]->get_text;
307 $cb->($data->[1]) if $cb;
308 });
309 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
310
311 } elsif ($type eq 'button') {
312 $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($data), 0, 2, $row, $row + 1);
313 $b->signal_connect (clicked => ($cb || sub {}));
314
315 } elsif ($type eq 'label') {
316 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
317 $edwid = Gtk2::Label->new ($data->[1]);
318 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
319
320 } else {
321 $edwid = Gtk2::Label->new ("FOO");
322 }
323}
324
90sub replace_arch_stack_layer { 325sub replace_arch_stack_layer {
91 my ($stack, $arch) = @_; 326 my ($stack, $arch) = @_;
92 327
93 my @outstack; 328 my @outstack;
94 329

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines