ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.108
Committed: Sun Jun 24 04:11:22 2007 UTC (16 years, 11 months ago) by root
Branch: MAIN
Changes since 1.107: +17 -9 lines
Log Message:
support protocol xml in npc dialogue

File Contents

# User Rev Content
1 root 1.69 package CFPlus::Protocol;
2 root 1.1
3     use utf8;
4     use strict;
5    
6 root 1.2 use Crossfire::Protocol::Constants;
7    
8 root 1.69 use CFPlus;
9 root 1.96 use CFPlus::DB;
10 root 1.69 use CFPlus::UI;
11     use CFPlus::Pod;
12 root 1.91 use CFPlus::Macro;
13     use CFPlus::Item;
14 root 1.1
15 root 1.87 use Crossfire::Protocol::Base 0.95;
16    
17 root 1.1 use base 'Crossfire::Protocol::Base';
18    
19     sub new {
20 root 1.101 my ($class, %arg) = @_;
21 root 1.1
22 root 1.101 my $self = $class->SUPER::new (%arg,
23 root 1.99 setup_req => {
24 root 1.101 extmap => 1,
25 root 1.103 excmd => 1,
26 root 1.101 %{$arg{setup_req} || {}},
27 root 1.99 },
28     );
29 root 1.1
30     $self->{map_widget}->clr_commands;
31    
32 root 1.72 my @cmd_help = map {
33     $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
34     or die "unparseable command help: $_->{kw}[0]";
35    
36     my $cmd = $1;
37     my @args = split /\|/, $2;
38     @args = (".*") unless @args;
39    
40     my (undef, @par) = CFPlus::Pod::section_of $_;
41     my $text = CFPlus::Pod::as_label @par;
42    
43     $_ = $_ eq ".*" ? "" : " $_"
44     for @args;
45    
46     map ["$cmd$_", $text],
47     sort { (length $a) <=> (length $b) }
48     @args
49 root 1.96 } sort { $a->{par} <=> $b->{par} }
50     CFPlus::Pod::find command => "*";
51 root 1.1
52 root 1.96 $self->connect_ext (event_capabilities => sub {
53     my ($cap) = @_;
54 root 1.95
55 root 1.96 if (my $ts = $cap->{tileset}) {
56     if (my ($default) = grep $_->[2] & 1, @$ts) {
57     $self->{tileset} = $default;
58     $self->{tilesize} = $default->[3];
59     $self->setup_req (tileset => $default->[0]);
60 root 1.95
61 root 1.96 my $w = int $self->{mapw} * 32 / $self->{tilesize};
62     my $h = int $self->{maph} * 32 / $self->{tilesize};
63 root 1.1
64 root 1.96 $self->setup_req (mapsize => "${w}x${h}");
65     }
66     }
67     });
68    
69     $self->{map_widget}->add_command (@$_)
70     for @cmd_help;
71 root 1.1
72 root 1.83 {
73     $self->{dialogue} = my $tex = new_from_file CFPlus::Texture
74     CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1;
75     $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}});
76     }
77    
78 root 1.96 {
79     $self->{noface} = my $tex = new_from_file CFPlus::Texture
80     CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
81     $self->{map}->set_texture (2, @$tex{qw(name w h s t)}, @{$tex->{minified}});
82     }
83    
84 root 1.1 $self->{open_container} = 0;
85    
86     # per server
87 root 1.96 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
88 root 1.1
89     $self
90     }
91    
92 elmex 1.34 sub logprint {
93     my ($self, @a) = @_;
94    
95 root 1.59 $self->{log_fh} ||= do {
96     my $path = "$Crossfire::VARDIR/log.$self->{host}";
97    
98     open my $fh, ">>:utf8", $path
99     or die "Couldn't open logfile $path: $!";
100 elmex 1.34
101 root 1.59 $fh->autoflush (1);
102    
103     $fh;
104     };
105 elmex 1.34
106 root 1.59 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
107 elmex 1.34
108     my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
109     $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
110    
111 root 1.59 print {$self->{log_fh}} "$ts ", @a, "\n";
112 elmex 1.34 }
113    
114 root 1.52 sub _stat_numdiff {
115 root 1.54 my ($self, $name, $old, $new) = @_;
116 root 1.52
117     my $diff = $new - $old;
118 root 1.54
119     $diff = 0.01 * int $diff * 100;
120    
121     0.1 >= abs $diff ? ()
122     : $diff < 0 ? "$name$diff" : "$name+$diff"
123 root 1.52 }
124    
125     sub _stat_skillmaskdiff {
126 root 1.54 my ($self, $name, $old, $new) = @_;
127 root 1.52
128 root 1.54 my $diff = $old ^ $new
129     or return;
130 root 1.52
131 root 1.53 my @diff = map
132     {
133     $diff & $_
134     ? (($new & $_ ? "+" : "-") . $self->{spell_paths}{$_})
135     : ()
136     }
137     sort { $a <=> $b } keys %{$self->{spell_paths}};
138 root 1.52
139     join "", @diff
140     }
141    
142     # all stats that are chacked against changes
143     my @statchange = (
144     [&CS_STAT_STR => \&_stat_numdiff, "Str"],
145     [&CS_STAT_INT => \&_stat_numdiff, "Int"],
146     [&CS_STAT_WIS => \&_stat_numdiff, "Wis"],
147     [&CS_STAT_DEX => \&_stat_numdiff, "Dex"],
148     [&CS_STAT_CON => \&_stat_numdiff, "Con"],
149     [&CS_STAT_CHA => \&_stat_numdiff, "Cha"],
150     [&CS_STAT_POW => \&_stat_numdiff, "Pow"],
151     [&CS_STAT_WC => \&_stat_numdiff, "Wc"],
152     [&CS_STAT_AC => \&_stat_numdiff, "Ac"],
153     [&CS_STAT_DAM => \&_stat_numdiff, "Dam"],
154     [&CS_STAT_SPEED => \&_stat_numdiff, "Speed"],
155     [&CS_STAT_WEAP_SP => \&_stat_numdiff, "WSp"],
156     [&CS_STAT_MAXHP => \&_stat_numdiff, "HP"],
157     [&CS_STAT_MAXSP => \&_stat_numdiff, "Mana"],
158     [&CS_STAT_MAXGRACE => \&_stat_numdiff, "Grace"],
159     [&CS_STAT_WEIGHT_LIM => \&_stat_numdiff, "Weight"],
160     [&CS_STAT_SPELL_ATTUNE => \&_stat_skillmaskdiff, "attuned"],
161     [&CS_STAT_SPELL_REPEL => \&_stat_skillmaskdiff, "repelled"],
162     [&CS_STAT_SPELL_DENY => \&_stat_skillmaskdiff, "denied"],
163     [&CS_STAT_RES_PHYS => \&_stat_numdiff, "phys"],
164     [&CS_STAT_RES_MAG => \&_stat_numdiff, "magic"],
165     [&CS_STAT_RES_FIRE => \&_stat_numdiff, "fire"],
166     [&CS_STAT_RES_ELEC => \&_stat_numdiff, "electricity"],
167     [&CS_STAT_RES_COLD => \&_stat_numdiff, "cold"],
168     [&CS_STAT_RES_CONF => \&_stat_numdiff, "confusion"],
169     [&CS_STAT_RES_ACID => \&_stat_numdiff, "acid"],
170     [&CS_STAT_RES_DRAIN => \&_stat_numdiff, "drain"],
171     [&CS_STAT_RES_GHOSTHIT => \&_stat_numdiff, "ghosthit"],
172     [&CS_STAT_RES_POISON => \&_stat_numdiff, "poison"],
173     [&CS_STAT_RES_SLOW => \&_stat_numdiff, "slow"],
174     [&CS_STAT_RES_PARA => \&_stat_numdiff, "paralyse"],
175     [&CS_STAT_TURN_UNDEAD => \&_stat_numdiff, "turnundead"],
176     [&CS_STAT_RES_FEAR => \&_stat_numdiff, "fear"],
177     [&CS_STAT_RES_DEPLETE => \&_stat_numdiff, "depletion"],
178     [&CS_STAT_RES_DEATH => \&_stat_numdiff, "death"],
179     [&CS_STAT_RES_HOLYWORD => \&_stat_numdiff, "godpower"],
180     [&CS_STAT_RES_BLIND => \&_stat_numdiff, "blind"],
181     );
182    
183 root 1.1 sub stats_update {
184     my ($self, $stats) = @_;
185    
186 root 1.59 my $prev = $self->{prev_stats} || { };
187    
188     if (my @diffs =
189     (
190     ($stats->{+CS_STAT_EXP64} > $prev->{+CS_STAT_EXP64} ? ($stats->{+CS_STAT_EXP64} - $prev->{+CS_STAT_EXP64}) . " experience gained" : ()),
191     map {
192     $stats->{$_} && $prev->{$_}
193     && $stats->{$_}[1] > $prev->{$_}[1] ? "($self->{skill_info}{$_}+" . ($stats->{$_}[1] - $prev->{$_}[1]) . ")" : ()
194     } sort { $a <=> $b } keys %{$self->{skill_info}}
195     )
196     ) {
197     my $msg = join " ", @diffs;
198     $self->{statusbox}->add ($msg, group => "experience $msg", fg => [0.5, 1, 0.5, 0.8], timeout => 5);
199     }
200    
201     if (
202     my @diffs = map $_->[1]->($self, $_->[2], $prev->{$_->[0]}, $stats->{$_->[0]}), @statchange
203     ) {
204     my $msg = "<b>stat change</b>: " . (join " ", @diffs);
205     $self->{statusbox}->add ($msg, group => "stat $msg", fg => [0.8, 1, 0.2, 1], timeout => 10);
206     }
207    
208     $self->update_stats_window ($stats, $prev);
209    
210     $self->{prev_stats} = { %$stats };
211     }
212    
213     my %RES_TBL = (
214     phys => CS_STAT_RES_PHYS,
215     magic => CS_STAT_RES_MAG,
216     fire => CS_STAT_RES_FIRE,
217     elec => CS_STAT_RES_ELEC,
218     cold => CS_STAT_RES_COLD,
219     conf => CS_STAT_RES_CONF,
220     acid => CS_STAT_RES_ACID,
221     drain => CS_STAT_RES_DRAIN,
222     ghit => CS_STAT_RES_GHOSTHIT,
223     pois => CS_STAT_RES_POISON,
224     slow => CS_STAT_RES_SLOW,
225     para => CS_STAT_RES_PARA,
226     tund => CS_STAT_TURN_UNDEAD,
227     fear => CS_STAT_RES_FEAR,
228     depl => CS_STAT_RES_DEPLETE,
229     deat => CS_STAT_RES_DEATH,
230     holyw => CS_STAT_RES_HOLYWORD,
231     blind => CS_STAT_RES_BLIND,
232     );
233    
234     sub update_stats_window {
235     my ($self, $stats, $prev) = @_;
236    
237     # I love text protocols...
238 root 1.52
239 root 1.59 my $hp = $stats->{+CS_STAT_HP} * 1;
240     my $hp_m = $stats->{+CS_STAT_MAXHP} * 1;
241     my $sp = $stats->{+CS_STAT_SP} * 1;
242     my $sp_m = $stats->{+CS_STAT_MAXSP} * 1;
243     my $fo = $stats->{+CS_STAT_FOOD} * 1;
244     my $fo_m = 999;
245     my $gr = $stats->{+CS_STAT_GRACE} * 1;
246     my $gr_m = $stats->{+CS_STAT_MAXGRACE} * 1;
247    
248     $::GAUGES->{hp} ->set_value ($hp, $hp_m);
249     $::GAUGES->{mana} ->set_value ($sp, $sp_m);
250     $::GAUGES->{food} ->set_value ($fo, $fo_m);
251     $::GAUGES->{grace} ->set_value ($gr, $gr_m);
252     $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64}))
253     . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")");
254 root 1.106 $::GAUGES->{range} ->set_text ($stats->{+CS_STAT_RANGE});
255 root 1.59 my $title = $stats->{+CS_STAT_TITLE};
256     $title =~ s/^Player: //;
257     $::STATWIDS->{title} ->set_text ("Title: " . $title);
258    
259     $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR});
260     $::STATWIDS->{st_dex} ->set_text (sprintf "%d" , $stats->{+CS_STAT_DEX});
261     $::STATWIDS->{st_con} ->set_text (sprintf "%d" , $stats->{+CS_STAT_CON});
262     $::STATWIDS->{st_int} ->set_text (sprintf "%d" , $stats->{+CS_STAT_INT});
263     $::STATWIDS->{st_wis} ->set_text (sprintf "%d" , $stats->{+CS_STAT_WIS});
264     $::STATWIDS->{st_pow} ->set_text (sprintf "%d" , $stats->{+CS_STAT_POW});
265     $::STATWIDS->{st_cha} ->set_text (sprintf "%d" , $stats->{+CS_STAT_CHA});
266     $::STATWIDS->{st_wc} ->set_text (sprintf "%d" , $stats->{+CS_STAT_WC});
267     $::STATWIDS->{st_ac} ->set_text (sprintf "%d" , $stats->{+CS_STAT_AC});
268     $::STATWIDS->{st_dam} ->set_text (sprintf "%d" , $stats->{+CS_STAT_DAM});
269     $::STATWIDS->{st_arm} ->set_text (sprintf "%d" , $stats->{+CS_STAT_RES_PHYS});
270     $::STATWIDS->{st_spd} ->set_text (sprintf "%.1f", $stats->{+CS_STAT_SPEED});
271     $::STATWIDS->{st_wspd}->set_text (sprintf "%.1f", $stats->{+CS_STAT_WEAP_SP});
272    
273 root 1.64 $self->update_weight;
274 root 1.59
275     $::STATWIDS->{"res_$_"}->set_text (sprintf "%d%", $stats->{$RES_TBL{$_}})
276     for keys %RES_TBL;
277    
278     my $sktbl = $::STATWIDS->{skill_tbl};
279     my @skills = keys %{ $self->{skill_info} };
280    
281     if (grep +(exists $stats->{$_}) != (exists $prev->{$_}), @skills) {
282     $sktbl->clear;
283    
284 root 1.69 $sktbl->add (0, 0, new CFPlus::UI::Label text => "Experience", align => 1);
285     $sktbl->add (1, 0, new CFPlus::UI::Label text => "Lvl.", align => 1);
286     $sktbl->add (2, 0, new CFPlus::UI::Label text => "Skill", expand => 1);
287     $sktbl->add (3, 0, new CFPlus::UI::Label text => "Experience", align => 1);
288     $sktbl->add (4, 0, new CFPlus::UI::Label text => "Lvl.", align => 1);
289     $sktbl->add (5, 0, new CFPlus::UI::Label text => "Skill", expand => 1);
290 root 1.59
291 elmex 1.68 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>";
292    
293     my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
294     my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
295    
296 root 1.65 my ($x, $y) = (0, 1);
297 root 1.59 for (
298     sort { $stats->{$b->[0]}[1] <=> $stats->{$a->[0]}[1] or $a->[1] cmp $b->[1] }
299     map [$_, $self->{skill_info}{$_}],
300     grep exists $stats->{$_},
301     @skills
302 root 1.52 ) {
303 root 1.59 my ($idx, $name) = @$_;
304    
305 elmex 1.68 my $spell_cb = sub {
306     my ($widget, $ev) = @_;
307    
308     if ($ev->{button} == 1) {
309     $::CONN->user_send ("ready_skill $name");
310     } elsif ($ev->{button} == 2) {
311     $::CONN->user_send ("use_skill $name");
312     } elsif ($ev->{button} == 3) {
313 root 1.90 my $shortname = CFPlus::shorten $name, 14;
314 root 1.69 (new CFPlus::UI::Menu
315 elmex 1.68 items => [
316 root 1.91 ["bind <i>ready_skill $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["ready_skill $name"] }],
317     ["bind <i>use_skill $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["use_skill $name"] }],
318 elmex 1.68 ],
319     )->popup ($ev);
320     } else {
321     return 0;
322     }
323    
324     1
325     };
326    
327 root 1.69 $sktbl->add ($x * 3 + 0, $y, $self->{stat_widget_exp}{$idx} = new CFPlus::UI::Label
328 elmex 1.68 text => "0", align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP);
329 root 1.69 $sktbl->add ($x * 3 + 1, $y, $self->{stat_widget_lvl}{$idx} = new CFPlus::UI::Label
330 elmex 1.68 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4, on_button_down => $spell_cb, @TOOLTIP_LVL);
331 root 1.74 $sktbl->add ($x * 3 + 2, $y, new CFPlus::UI::Label text => $name, on_button_down => $spell_cb,
332     can_events => 1, can_hover => 1, tooltip => (CFPlus::Pod::section_label skill_description => $name) . $TOOLTIP_ALL);
333 root 1.59
334     $x++ and ($x, $y) = (0, $y + 1);
335 root 1.52 }
336 root 1.1 }
337    
338 root 1.59 for (grep exists $stats->{$_}, @skills) {
339     $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1]));
340     $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1);
341     }
342 root 1.1 }
343    
344 root 1.91 sub macro_send {
345     my ($self, $macro) = @_;
346    
347     for my $cmd (@{ $macro->{action} }) {
348     $self->send_command ($cmd);
349     }
350     }
351    
352 root 1.1 sub user_send {
353     my ($self, $command) = @_;
354    
355 root 1.91 $self->{record}->($command)
356 root 1.87 if $self->{record};
357 elmex 1.9
358 elmex 1.34 $self->logprint ("send: ", $command);
359 root 1.1 $self->send_command ($command);
360 root 1.45 ::status ($command);
361 root 1.1 }
362    
363 root 1.91 sub record {
364     my ($self, $cb) = @_;
365 elmex 1.9
366 root 1.91 $self->{record} = $cb;
367 elmex 1.9 }
368    
369 root 1.1 sub map_scroll {
370     my ($self, $dx, $dy) = @_;
371    
372     $self->{map}->scroll ($dx, $dy);
373     }
374    
375     sub feed_map1a {
376     my ($self, $data) = @_;
377    
378 root 1.79 $self->{map}->map1a_update ($data, $self->{setup}{extmap});
379 root 1.1 $self->{map_widget}->update;
380     }
381    
382 root 1.36 sub magicmap {
383     my ($self, $w, $h, $x, $y, $data) = @_;
384    
385     $self->{map_widget}->set_magicmap ($w, $h, $x, $y, $data);
386     }
387    
388 root 1.1 sub flush_map {
389     my ($self) = @_;
390    
391     my $map_info = delete $self->{map_info}
392     or return;
393    
394     my ($hash, $x, $y, $w, $h) = @$map_info;
395    
396     my $data = $self->{map}->get_rect ($x, $y, $w, $h);
397 root 1.96 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { };
398 root 1.1 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
399     }
400    
401     sub map_clear {
402     my ($self) = @_;
403    
404     $self->flush_map;
405     delete $self->{neigh_map};
406    
407     $self->{map}->clear;
408 root 1.37 delete $self->{map_widget}{magicmap};
409 root 1.1 }
410    
411 root 1.97 sub bg_fetch {
412     my ($self) = @_;
413    
414     my $id;
415    
416     do {
417     $id = pop @{$self->{bg_fetch}}
418     or return;
419     } while $self->{texture}[$id];
420    
421     CFPlus::DB::get tilecache => $id, sub {
422     my ($data) = @_;
423    
424 root 1.98 return unless $self->{map}; # stop when destroyed
425    
426 root 1.97 $self->set_texture ($id => $data)
427     if defined $data;
428    
429     $self->bg_fetch;
430     };
431     }
432 root 1.1
433     sub load_map($$$) {
434     my ($self, $hash, $x, $y) = @_;
435    
436 root 1.100 my $gen = $self->{map_change_gen};
437    
438 root 1.96 CFPlus::DB::get $self->{mapcache} => $hash, sub {
439 root 1.100 return unless $gen == $self->{map_change_gen};
440    
441 root 1.96 my ($data) = @_;
442    
443     if (defined $data) {
444     $data = Compress::LZF::decompress $data;
445     #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
446 root 1.1
447 root 1.97 my $inprogress = @{ $self->{bg_fetch} || [] };
448 root 1.98 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data);
449 root 1.97 $self->bg_fetch unless $inprogress;
450 root 1.1 }
451 root 1.96 };
452 root 1.1 }
453    
454     # hardcode /world/world_xxx_xxx map names, the savings are enourmous,
455     # (server resource,s latency, bandwidth), so this hack is warranted.
456     # the right fix is to make real tiled maps with an overview file
457     sub send_mapinfo {
458     my ($self, $data, $cb) = @_;
459    
460     if ($self->{map_info}[0] =~ m%^/world/world_(\d\d\d)_(\d\d\d)$%) {
461     my ($wx, $wy) = ($1, $2);
462    
463     if ($data =~ /^spatial ([1-4]+)$/) {
464     my @dx = (0, 0, 1, 0, -1);
465     my @dy = (0, -1, 0, 1, 0);
466     my ($dx, $dy);
467    
468     for (split //, $1) {
469     $dx += $dx[$_];
470     $dy += $dy[$_];
471     }
472    
473     $cb->(spatial => 15,
474     $self->{map_info}[1] - $self->{map}->ox + $dx * 50,
475     $self->{map_info}[2] - $self->{map}->oy + $dy * 50,
476     50, 50,
477     sprintf "/world/world_%03d_%03d", $wx + $dx, $wy + $dy
478     );
479    
480     return;
481     }
482     }
483    
484     $self->SUPER::send_mapinfo ($data, $cb);
485     }
486    
487     # this method does a "flood fill" into every tile direction
488     # it assumes that tiles are arranged in a rectangular grid,
489     # i.e. a map is the same as the left of the right map etc.
490     # failure to comply are harmless and result in display errors
491     # at worst.
492     sub flood_fill {
493     my ($self, $block, $gx, $gy, $path, $hash, $flags) = @_;
494    
495     # the server does not allow map paths > 6
496     return if 7 <= length $path;
497    
498     my ($x0, $y0, $x1, $y1) = @{$self->{neigh_rect}};
499    
500     for (
501     [1, 3, 0, -1],
502     [2, 4, 1, 0],
503     [3, 1, 0, 1],
504     [4, 2, -1, 0],
505     ) {
506     my ($tile, $tile2, $dx, $dy) = @$_;
507    
508     next if $block & (1 << $tile);
509     my $block = $block | (1 << $tile2);
510    
511     my $gx = $gx + $dx;
512     my $gy = $gy + $dy;
513    
514     next unless $flags & (1 << ($tile - 1));
515     next if $self->{neigh_grid}{$gx, $gy}++;
516    
517     my $neigh = $self->{neigh_map}{$hash} ||= [];
518     if (my $info = $neigh->[$tile]) {
519     my ($flags, $x, $y, $w, $h, $hash) = @$info;
520    
521     $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
522     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
523    
524     } else {
525 root 1.100 my $gen = $self->{map_change_gen};
526 root 1.1 $self->send_mapinfo ("spatial $path$tile", sub {
527 root 1.100 return unless $gen == $self->{map_change_gen};
528    
529 root 1.1 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
530    
531     return if $mode ne "spatial";
532    
533     $x += $self->{map}->ox;
534     $y += $self->{map}->oy;
535    
536     $self->load_map ($hash, $x, $y)
537     unless $self->{neigh_map}{$hash}[5]++;#d#
538    
539     $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
540    
541     $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
542     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
543     });
544     }
545     }
546     }
547    
548     sub map_change {
549     my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
550    
551     $self->flush_map;
552    
553 root 1.100 ++$self->{map_change_gen};
554    
555 root 1.1 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
556    
557     my $mapmapw = $self->{mapmap}->{w};
558     my $mapmaph = $self->{mapmap}->{h};
559    
560     $self->{neigh_rect} = [
561     $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
562     $ox + $mapmapw * 0.5 + $w, $oy + $mapmapw * 0.5 + $h,
563     ];
564    
565     delete $self->{neigh_grid};
566    
567     $x += $ox;
568     $y += $oy;
569    
570     $self->{map_info} = [$hash, $x, $y, $w, $h];
571    
572     (my $map = $hash) =~ s/^.*?\/([^\/]+)$/\1/;
573     $::STATWIDS->{map}->set_text ("Map: " . $map);
574    
575     $self->load_map ($hash, $x, $y);
576     $self->flood_fill (0, 0, 0, "", $hash, $flags);
577     }
578    
579     sub face_find {
580 root 1.96 my ($self, $facenum, $face, $cb) = @_;
581 root 1.1
582     my $hash = "$face->{chksum},$face->{name}";
583    
584 root 1.96 my $id = CFPlus::DB::get_tile_id_sync $hash;
585 root 1.1
586 root 1.96 $face->{id} = $id;
587     $self->{faceid}[$facenum] = $id;
588 root 1.1
589 root 1.96 $self->{map}->set_tileid ($facenum => $id);
590 root 1.1
591 root 1.96 CFPlus::DB::get tilecache => $id, $cb;
592 root 1.1 }
593    
594     sub face_update {
595 root 1.58 my ($self, $facenum, $face, $changed) = @_;
596 root 1.1
597 root 1.96 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { }
598     if $changed;
599 root 1.1
600     $self->set_texture ($face->{id} => delete $face->{image});
601     }
602    
603 root 1.99 sub smooth_update {
604     my ($self, $facenum, $face) = @_;
605    
606     $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
607     }
608    
609 root 1.1 sub set_texture {
610     my ($self, $id, $data) = @_;
611    
612 root 1.98 $self->{texture}[$id] = my $tex =
613     new_from_image CFPlus::Texture
614     $data, minify => 1, mipmap => 1;
615 root 1.1
616 root 1.98 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
617     $self->{map_widget}->update;
618 root 1.1 }
619    
620     sub sound_play {
621     my ($self, $x, $y, $soundnum, $type) = @_;
622    
623     $self->{sound_play}->($x, $y, $soundnum, $type);
624     }
625    
626     my $LAST_QUERY; # server is stupid, stupid, stupid
627    
628     sub query {
629     my ($self, $flags, $prompt) = @_;
630    
631     $prompt = $LAST_QUERY unless length $prompt;
632     $LAST_QUERY = $prompt;
633    
634 root 1.22 $self->{query}-> ($self, $flags, $prompt);
635 root 1.1 }
636    
637 root 1.108 sub sanitise_xml($) {
638     local $_ = shift;
639    
640     # we now weed out all tags we do not support
641     s%<(?!/?i>|/?u>|/?b>|fg |/fg>)%&lt;%g;
642     # now all entities
643     s/&(?!amp;|lt;|gt;|apos;|quot;|#[0-9]+;|#x[0-9a-fA-F]+;)/&amp;/g;
644    
645     # handle some elements
646     s/<fg name='([^']*)'>(.*?)<\/fg>/<span foreground='$1'>$2<\/span>/gs;
647     s/<fg name="([^"]*)">(.*?)<\/fg>/<span foreground="$1">$2<\/span>/gs;
648    
649     $_
650     }
651    
652 root 1.105 our %NAME_TO_COLOR = (
653     black => 0,
654     white => 1,
655     darkblue => 2,
656     red => 3,
657     orange => 4,
658     lightblue => 5,
659     darkorange => 6,
660     green => 7,
661     darkgreen => 8,
662     grey => 9,
663     brown => 10,
664     yellow => 11,
665     tan => 12,
666     );
667    
668 root 1.103 our @CF_COLOR = (
669     [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
670     [1.00, 1.00, 1.00],
671     [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
672     [1.00, 0.00, 0.00],
673     [1.00, 0.54, 0.00],
674     [0.11, 0.56, 1.00],
675     [0.93, 0.46, 0.00],
676     [0.18, 0.54, 0.34],
677     [0.56, 0.73, 0.56],
678     [0.80, 0.80, 0.80],
679     [0.75, 0.61, 0.20],
680     [0.99, 0.77, 0.26],
681     [0.74, 0.65, 0.41],
682     );
683    
684 root 1.105 sub msg {
685     my ($self, $color, $type, $text, @extra) = @_;
686 root 1.1
687 root 1.108 $text = sanitise_xml $text;
688 root 1.107
689 root 1.105 if (my $cb = $self->{cb_msg}{$type}) {
690     $_->($self, $color, $type, $text, @extra) for values %$cb;
691     } elsif ($type =~ /^(?:chargen-race-title|chargen-race-description)$/) {
692     $type =~ s/-/_/g;
693     $self->{$type} = $text;
694     } else {
695     $self->logprint ("msg: ", $text);
696     return if $color < 0; # negative color == ignore if not understood
697 root 1.93
698 root 1.105 my $fg = $CF_COLOR[$color % @CF_COLOR];
699 elmex 1.34
700 root 1.105 ## try to create single paragraphs of multiple lines sent by the server
701     # no longer neecssary with TRT servers
702     #$text =~ s/(?<=\S)\n(?=\w)/ /g;
703    
704     ::message ({ fg => $fg, markup => $_ })
705     for split /\n/, $text;
706    
707     $self->{statusbox}->add ($text,
708     group => $text,
709     fg => $fg,
710     timeout => $color >= 2 ? 180 : 10,
711     tooltip_font => $::FONT_FIXED,
712     );
713     }
714 root 1.1 }
715    
716     sub spell_add {
717     my ($self, $spell) = @_;
718    
719 root 1.66 # try to create single paragraphs out of the multiple lines sent by the server
720 root 1.27 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
721     $spell->{message} =~ s/\n+$//;
722     $spell->{message} ||= "Server did not provide a description for this spell.";
723    
724 root 1.88 $::SPELL_LIST->add_spell ($spell);
725 elmex 1.14
726 root 1.69 $self->{map_widget}->add_command ("invoke $spell->{name}", CFPlus::asxml $spell->{message});
727     $self->{map_widget}->add_command ("cast $spell->{name}", CFPlus::asxml $spell->{message});
728 root 1.1 }
729    
730     sub spell_delete {
731     my ($self, $spell) = @_;
732 root 1.51
733 root 1.88 $::SPELL_LIST->remove_spell ($spell);
734 root 1.1 }
735    
736 root 1.87 sub setup {
737     my ($self, $setup) = @_;
738    
739 root 1.95 $self->{map_widget}->set_tilesize ($self->{tilesize});
740 root 1.87 $::MAP->resize ($self->{mapw}, $self->{maph});
741     }
742    
743 root 1.1 sub addme_success {
744     my ($self) = @_;
745    
746 root 1.72 my %skill_help;
747 root 1.21
748 root 1.72 for my $node (CFPlus::Pod::find skill_description => "*") {
749     my (undef, @par) = CFPlus::Pod::section_of $node;
750     $skill_help{$node->{kw}[0]} = CFPlus::Pod::as_label @par;
751 root 1.21 };
752 root 1.72
753 root 1.1 for my $skill (values %{$self->{skill_info}}) {
754     $self->{map_widget}->add_command ("ready_skill $skill",
755 root 1.69 (CFPlus::asxml "Ready the skill '$skill'\n\n")
756 root 1.72 . $skill_help{$skill});
757 root 1.1 $self->{map_widget}->add_command ("use_skill $skill",
758 root 1.69 (CFPlus::asxml "Immediately use the skill '$skill'\n\n")
759 root 1.72 . $skill_help{$skill});
760 root 1.1 }
761     }
762    
763     sub eof {
764     my ($self) = @_;
765    
766     $self->{map_widget}->clr_commands;
767    
768     ::stop_game ();
769     }
770    
771     sub image_info {
772     my ($self, $numfaces) = @_;
773    
774     $self->{num_faces} = $numfaces;
775     $self->{face_prefetch} = [1 .. $numfaces];
776     $self->face_prefetch;
777     }
778    
779     sub face_prefetch {
780     my ($self) = @_;
781    
782     return unless $::CFG->{face_prefetch};
783    
784     if ($self->{num_faces}) {
785     return if @{ $self->{send_queue} || [] };
786     my $todo = @{ $self->{face_prefetch} }
787     or return;
788    
789     my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
790    
791     $self->send ("requestinfo image_sums $face $face");
792    
793 root 1.69 $self->{statusbox}->add (CFPlus::asxml "prefetching $todo",
794 root 1.19 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
795 root 1.1 } elsif (!exists $self->{num_faces}) {
796     $self->send ("requestinfo image_info");
797    
798     $self->{num_faces} = 0;
799    
800 root 1.69 $self->{statusbox}->add (CFPlus::asxml "starting to prefetch",
801 root 1.19 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
802 root 1.1 }
803     }
804    
805     sub update_floorbox {
806 root 1.69 $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub {
807 root 1.1 return unless $::CONN;
808 elmex 1.62
809 root 1.1 $::FLOORBOX->clear;
810    
811     my $row;
812 elmex 1.62 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) {
813 root 1.25 if ($row < 6) {
814 root 1.1 local $_->{face_widget}; # hack to force recreation of widget
815     local $_->{desc_widget}; # hack to force recreation of widget
816 root 1.69 CFPlus::Item::update_widgets $_;
817 root 1.1
818     $::FLOORBOX->add (0, $row, $_->{face_widget});
819     $::FLOORBOX->add (1, $row, $_->{desc_widget});
820    
821     $row++;
822     } else {
823 root 1.69 $::FLOORBOX->add (1, $row, new CFPlus::UI::Button
824 root 1.30 text => "More...",
825 root 1.51 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 },
826 root 1.25 );
827 root 1.1 last;
828     }
829     }
830 elmex 1.62 if ($::CONN->{open_container}) {
831 root 1.69 $::FLOORBOX->add (1, $row++, new CFPlus::UI::Button
832 elmex 1.62 text => "Close container",
833     on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") }
834     );
835     }
836 root 1.1 });
837 elmex 1.62
838 root 1.1 $::WANT_REFRESH++;
839     }
840    
841     sub set_opencont {
842     my ($conn, $tag, $name) = @_;
843     $conn->{open_container} = $tag;
844 elmex 1.62 update_floorbox;
845 elmex 1.10
846     $::INV_RIGHT_HB->clear ();
847 root 1.69 $::INV_RIGHT_HB->add (new CFPlus::UI::Label align => 0, expand => 1, text => $name);
848 elmex 1.10
849     if ($tag != 0) { # Floor isn't closable, is it?
850 root 1.69 $::INV_RIGHT_HB->add (new CFPlus::UI::Button
851 elmex 1.10 text => "Close container",
852     tooltip => "Close the currently open container (if one is open)",
853     on_activate => sub {
854     $::CONN->send ("apply $tag") # $::CONN->{open_container}")
855     if $tag != 0;
856     #if $CONN->{open_container} != 0;
857 root 1.38 0
858 elmex 1.10 },
859     );
860     }
861    
862 root 1.1 $::INVR->set_items ($conn->{container}{$tag});
863     }
864    
865 root 1.47 sub update_containers {
866     my ($self) = @_;
867 root 1.1
868 root 1.69 $CFPlus::UI::ROOT->on_refresh ("update_containers_$self" => sub {
869 root 1.55 my $todo = delete $self->{update_container}
870     or return;
871    
872     for my $tag (keys %$todo) {
873 elmex 1.62 update_floorbox if $tag == 0 or $tag == $self->{open_container};
874 root 1.47 if ($tag == 0) {
875 root 1.48 $::INVR->set_items ($self->{container}{0})
876     if $tag == $self->{open_container};
877 root 1.47 } elsif ($tag == $self->{player}{tag}) {
878     $::INV->set_items ($self->{container}{$tag})
879     } else {
880 root 1.48 $::INVR->set_items ($self->{container}{$tag})
881     if $tag == $self->{open_container};
882 root 1.47 }
883     }
884     });
885 root 1.1 }
886    
887 root 1.48 sub container_add {
888     my ($self, $tag, $items) = @_;
889    
890     $self->{update_container}{$tag}++;
891     $self->update_containers;
892     }
893    
894 root 1.1 sub container_clear {
895     my ($self, $tag) = @_;
896    
897 root 1.47 $self->{update_container}{$tag}++;
898     $self->update_containers;
899 root 1.1 }
900    
901     sub item_delete {
902     my ($self, @items) = @_;
903    
904 root 1.49 $self->{update_container}{$_->{container}}++
905 root 1.47 for @items;
906    
907     $self->update_containers;
908 root 1.1 }
909    
910     sub item_update {
911     my ($self, $item) = @_;
912    
913     #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
914    
915 root 1.69 CFPlus::Item::update_widgets $item;
916 root 1.1
917 root 1.2 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
918 root 1.1 set_opencont ($::CONN, 0, "Floor");
919    
920 root 1.2 } elsif ($item->{flags} & F_OPEN) {
921 root 1.69 set_opencont ($::CONN, $item->{tag}, CFPlus::Item::desc_string $item);
922 root 1.47
923 root 1.1 } else {
924 root 1.47 $self->{update_container}{$item->{container}}++;
925     $self->update_containers;
926 root 1.1 }
927     }
928    
929 elmex 1.3 sub player_update {
930     my ($self, $player) = @_;
931 root 1.64
932     $self->update_weight;
933     }
934    
935     sub update_weight {
936     my ($self) = @_;
937    
938     my $weight = .001 * $self->{player}{weight};
939     my $limit = .001 * $self->{stat}{+CS_STAT_WEIGHT_LIM};
940    
941     $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $weight);
942     $::STATWIDS->{m_weight}->set_text (sprintf "%.1fkg", $limit);
943     $::STATWIDS->{i_weight}->set_text (sprintf "%.1f/%.1fkg", $weight, $limit);
944 root 1.28 }
945    
946 root 1.32 sub update_server_info {
947     my ($self) = @_;
948    
949     my @yesno = ("<span foreground='red'>no</span>", "<span foreground='green'>yes</span>");
950    
951     $::SERVER_INFO->set_markup (
952     "server <tt>$self->{host}:$self->{port}</tt>\n"
953     . "protocol version <tt>$self->{version}</tt>\n"
954     . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
955     . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n"
956 root 1.104 . "examine command support $yesno[$self->{setup}{excmd} > 0]\n"
957 root 1.86 . "editing support $yesno[!!$self->{editor_support}]\n"
958 root 1.80 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n"
959 root 1.104 . "big image protocol support $yesno[$self->{setup}{fxix} > 0]\n"
960 root 1.32 . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
961     . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
962     . "map size $self->{mapw}×$self->{maph}\n"
963     );
964 elmex 1.89
965     ::setup_build_button ($self->{editor_support}->{builder_ui});
966 root 1.32 }
967    
968 root 1.28 sub logged_in {
969     my ($self) = @_;
970    
971 root 1.70 $self->send_ext_req (cfplus_support => version => 1, sub {
972     $self->{cfplus_ext} = $_[0]{version};
973 root 1.32 $self->update_server_info;
974 root 1.70
975 root 1.85 if ($self->{cfplus_ext} >= 2) {
976     $self->send_ext_req ("editor_support", sub {
977 root 1.86 $self->{editor_support} = $_[0];
978 root 1.85 $self->update_server_info;
979    
980     0
981     });
982     }
983    
984 root 1.70 0
985 root 1.32 });
986 root 1.31
987 root 1.32 $self->update_server_info;
988 root 1.13
989     $self->send_command ("output-sync $::CFG->{output_sync}");
990     $self->send_command ("output-count $::CFG->{output_count}");
991 root 1.94 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
992 root 1.29 $self->send_command ("pickup $::CFG->{pickup}");
993 elmex 1.3 }
994    
995 elmex 1.89 sub buildat {
996     my ($self, $builditem, $x, $y) = @_;
997    
998     if ($self->{cfplus_ext}) {
999     $self->send_ext_msg (builder_build => dx => $x, dy => $y, (ref ($builditem) eq 'HASH') ? %$builditem : (item => $builditem));
1000     }
1001     }
1002    
1003 root 1.32 sub lookat {
1004     my ($self, $x, $y) = @_;
1005    
1006     if ($self->{cfplus_ext}) {
1007 root 1.70 $self->send_ext_req (lookat => dx => $x, dy => $y, sub {
1008     my ($msg) = @_;
1009 root 1.32
1010 root 1.70 if (exists $msg->{npc_dialog}) {
1011 root 1.32 # start npc chat dialog
1012 root 1.69 $self->{npc_dialog} = new CFPlus::NPCDialog::
1013 root 1.32 dx => $x,
1014     dy => $y,
1015 root 1.70 title => "$msg->{npc_dialog} (NPC)",
1016 root 1.32 conn => $self,
1017     ;
1018     }
1019     });
1020     }
1021    
1022     $self->send ("lookat $x $y");
1023     }
1024    
1025     sub destroy {
1026     my ($self) = @_;
1027    
1028 root 1.79 (delete $self->{npc_dialog})->destroy
1029 root 1.32 if $self->{npc_dialog};
1030    
1031     $self->SUPER::destroy;
1032     }
1033    
1034 root 1.69 package CFPlus::NPCDialog;
1035 root 1.32
1036 root 1.76 our @ISA = 'CFPlus::UI::Toplevel';
1037 root 1.32
1038     sub new {
1039     my $class = shift;
1040    
1041     my $self = $class->SUPER::new (
1042     x => 'center',
1043     y => 'center',
1044     name => "npc_dialog",
1045     force_w => $::WIDTH * 0.7,
1046     force_h => $::HEIGHT * 0.7,
1047     title => "NPC Dialog",
1048     kw => { hi => 0, yes => 0, no => 0 },
1049 root 1.64 has_close_button => 1,
1050 root 1.32 @_,
1051     );
1052    
1053 root 1.92 CFPlus::weaken (my $this = $self);
1054 root 1.32
1055 root 1.64 $self->connect (delete => sub { $this->destroy; 1 });
1056    
1057 root 1.32 # better use a pane...
1058 root 1.69 $self->add (my $hbox = new CFPlus::UI::HBox);
1059     $hbox->add ($self->{textview} = new CFPlus::UI::TextScroller expand => 1);
1060 root 1.32
1061 root 1.69 $hbox->add (my $vbox = new CFPlus::UI::VBox);
1062 root 1.32
1063 root 1.69 $vbox->add (new CFPlus::UI::Label text => "Message Entry:");
1064     $vbox->add ($self->{entry} = new CFPlus::UI::Entry
1065 root 1.73 tooltip => "#npc_message_entry",
1066 root 1.32 on_activate => sub {
1067     my ($entry, $text) = @_;
1068    
1069     return unless $text =~ /\S/;
1070    
1071     $entry->set_text ("");
1072     $this->send ($text);
1073 root 1.38
1074     0
1075 root 1.32 },
1076     );
1077    
1078 root 1.69 $vbox->add ($self->{options} = new CFPlus::UI::VBox);
1079 root 1.32
1080 root 1.69 $self->{bye_button} = new CFPlus::UI::Button
1081 root 1.32 text => "Bye (close)",
1082     tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.",
1083 root 1.64 on_activate => sub { $this->destroy; 1 },
1084 root 1.32 ;
1085    
1086     $self->update_options;
1087    
1088 root 1.70 $self->{id} = $self->{conn}->send_ext_req (
1089     npc_dialog_begin => dx => $self->{dx}, dy => $self->{dy},
1090     sub { $this && $this->feed (@_) }
1091     );
1092 root 1.32
1093 root 1.38 $self->{entry}->grab_focus;
1094 root 1.32
1095 root 1.67 $self->{textview}->add_paragraph ({
1096     fg => [1, 1, 0, 1],
1097     markup => "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n",
1098     });
1099 root 1.32
1100     $self->show;
1101     $self
1102     };
1103    
1104     sub update_options {
1105     my ($self) = @_;
1106    
1107 root 1.92 CFPlus::weaken $self;
1108 root 1.32
1109     $self->{options}->clear;
1110 root 1.39 $self->{options}->add ($self->{bye_button});
1111 root 1.32
1112     for my $kw (sort keys %{ $self->{kw} }) {
1113 root 1.69 $self->{options}->add (new CFPlus::UI::Button
1114 root 1.32 text => $kw,
1115     on_activate => sub {
1116     $self->send ($kw);
1117 root 1.38 0
1118 root 1.32 },
1119     );
1120     }
1121     }
1122    
1123     sub feed {
1124 root 1.70 my ($self, $msg) = @_;
1125 root 1.32
1126 root 1.92 CFPlus::weaken $self;
1127 root 1.42
1128 root 1.70 if ($msg->{msgtype} eq "reply") {
1129     $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []};
1130     $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []};
1131 root 1.61
1132 root 1.108 my $text = "\n" . CFPlus::Protocol::sanitise_xml $msg->{msg};
1133 root 1.32 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
1134 root 1.41 my @link;
1135 root 1.70 $text =~ s{
1136 root 1.41 ($match)
1137     }{
1138 root 1.42 my $kw = $1;
1139    
1140 root 1.69 push @link, new CFPlus::UI::Label
1141 root 1.42 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
1142 root 1.41 can_hover => 1,
1143 root 1.42 can_events => 1,
1144 root 1.43 padding_x => 0,
1145     padding_y => 0,
1146 root 1.42 on_button_up => sub {
1147     $self->send ($kw);
1148     };
1149 root 1.41
1150 root 1.75 "\x{fffc}"
1151 root 1.41 }giex;
1152 root 1.32
1153 root 1.70 $self->{textview}->add_paragraph ({ markup => $text, widget => \@link });
1154 root 1.42 $self->{textview}->scroll_to_bottom;
1155 root 1.32 $self->update_options;
1156     } else {
1157     $self->destroy;
1158     }
1159    
1160     1
1161     }
1162    
1163     sub send {
1164     my ($self, $msg) = @_;
1165    
1166 root 1.69 $self->{textview}->add_paragraph ({ markup => "\n" . CFPlus::asxml $msg });
1167 root 1.42 $self->{textview}->scroll_to_bottom;
1168 root 1.61
1169 root 1.70 $self->{conn}->send_ext_msg (npc_dialog_tell => msgid => $self->{id}, msg => $msg);
1170 root 1.32 }
1171    
1172     sub destroy {
1173     my ($self) = @_;
1174    
1175 root 1.39 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1
1176 root 1.32
1177 root 1.70 if ($self->{conn}) {
1178     $self->{conn}->send_ext_msg (npc_dialog_end => msgid => $self->{id}) if $self->{id};
1179     delete $self->{conn}{npc_dialog};
1180     $self->{conn}->disconnect_ext ($self->{id});
1181     }
1182 root 1.32
1183     $self->SUPER::destroy;
1184     }
1185    
1186 root 1.67 1
1187 root 1.84