ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.32
Committed: Mon Jun 12 13:26:14 2006 UTC (17 years, 11 months ago) by root
Branch: MAIN
Changes since 1.31: +176 -12 lines
Log Message:
first round of npc dialog window, some ui bugfixes

File Contents

# User Rev Content
1 root 1.1 package CFClient::Protocol;
2    
3     use utf8;
4     use strict;
5    
6 root 1.2 use Crossfire::Protocol::Constants;
7    
8 root 1.20 use CFClient;
9 root 1.1 use CFClient::UI;
10    
11     use base 'Crossfire::Protocol::Base';
12    
13     sub new {
14     my $class = shift;
15    
16     my $self = $class->SUPER::new (@_);
17    
18     $self->{map_widget}->clr_commands;
19    
20 root 1.21 my $cmd_help = CFClient::load_pod CFClient::find_rcfile "pod/command_help.pod", command_help => 1, sub {
21     my ($pom) = @_;
22 root 1.1
23 root 1.21 my @cmd_help;
24 root 1.1
25 root 1.21 for my $head2 ($pom->head1->[-2]->head2) {
26     $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
27     or next;
28 root 1.1
29 root 1.21 my $cmd = $1;
30     my @args = split /\|/, $2;
31     @args = (".*") unless @args;
32    
33     $_ = $_ eq ".*" ? "" : " $_"
34     for @args;
35    
36     my $text = CFClient::pod_to_pango $head2->content;
37    
38     push @cmd_help, ["$cmd$_", $text]
39     for sort { (length $a) <=> (length $b) }
40     @args;
41     }
42 root 1.6
43 root 1.21 \@cmd_help
44     };
45 root 1.1
46 root 1.21 $self->{map_widget}->add_command (@$_)
47     for @$cmd_help;
48 root 1.1
49     $self->{noface} = new_from_file CFClient::Texture
50     CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1;
51    
52     $self->{open_container} = 0;
53    
54     # "global"
55     $self->{tilecache} = CFClient::db_table "tilecache";
56     $self->{facemap} = CFClient::db_table "facemap";
57    
58     # per server
59     $self->{mapcache} = CFClient::db_table "mapcache_$self->{host}_$self->{port}";
60    
61     $self
62     }
63    
64     sub stats_update {
65     my ($self, $stats) = @_;
66    
67 elmex 1.4 if (my $exp = $stats->{+CS_STAT_EXP64}) {
68 root 1.1 my $diff = $exp - $self->{prev_exp};
69 elmex 1.5 $self->{statusbox}->add ("$diff experience gained", group => "experience $diff", fg => [0.5, 1, 0.5, 0.8], timeout => 5)
70 root 1.1 if exists $self->{prev_exp} && $diff;
71     $self->{prev_exp} = $exp;
72     }
73    
74     ::update_stats_window ($stats);
75     }
76    
77     sub user_send {
78     my ($self, $command) = @_;
79    
80 elmex 1.9 if ($self->{record}) {
81     push @{$self->{record}}, $command;
82     }
83    
84 root 1.1 $self->send_command ($command);
85 root 1.2 ::status $command;
86 root 1.1 }
87    
88 elmex 1.9 sub start_record {
89     my ($self) = @_;
90    
91     $self->{record} = [];
92     }
93    
94     sub stop_record {
95     my ($self) = @_;
96     return delete $self->{record};
97     }
98    
99 root 1.1 sub map_scroll {
100     my ($self, $dx, $dy) = @_;
101    
102     $self->{map}->scroll ($dx, $dy);
103     }
104    
105     sub feed_map1a {
106     my ($self, $data) = @_;
107    
108     $self->{map}->map1a_update ($data);
109     $self->{map_widget}->update;
110     }
111    
112     sub flush_map {
113     my ($self) = @_;
114    
115     my $map_info = delete $self->{map_info}
116     or return;
117    
118     my ($hash, $x, $y, $w, $h) = @$map_info;
119    
120     my $data = $self->{map}->get_rect ($x, $y, $w, $h);
121     $self->{mapcache}->put ($hash => Compress::LZF::compress $data);
122     #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
123     }
124    
125     sub map_clear {
126     my ($self) = @_;
127    
128     $self->flush_map;
129     delete $self->{neigh_map};
130    
131     $self->{map}->clear;
132     }
133    
134    
135     sub load_map($$$) {
136     my ($self, $hash, $x, $y) = @_;
137    
138     if (defined (my $data = $self->{mapcache}->get ($hash))) {
139     $data = Compress::LZF::decompress $data;
140     #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
141     for my $id ($self->{map}->set_rect ($x, $y, $data)) {
142     my $data = $self->{tilecache}->get ($id)
143     or next;
144    
145     $self->set_texture ($id => $data);
146     }
147     }
148     }
149    
150     # hardcode /world/world_xxx_xxx map names, the savings are enourmous,
151     # (server resource,s latency, bandwidth), so this hack is warranted.
152     # the right fix is to make real tiled maps with an overview file
153     sub send_mapinfo {
154     my ($self, $data, $cb) = @_;
155    
156     if ($self->{map_info}[0] =~ m%^/world/world_(\d\d\d)_(\d\d\d)$%) {
157     my ($wx, $wy) = ($1, $2);
158    
159     if ($data =~ /^spatial ([1-4]+)$/) {
160     my @dx = (0, 0, 1, 0, -1);
161     my @dy = (0, -1, 0, 1, 0);
162     my ($dx, $dy);
163    
164     for (split //, $1) {
165     $dx += $dx[$_];
166     $dy += $dy[$_];
167     }
168    
169     $cb->(spatial => 15,
170     $self->{map_info}[1] - $self->{map}->ox + $dx * 50,
171     $self->{map_info}[2] - $self->{map}->oy + $dy * 50,
172     50, 50,
173     sprintf "/world/world_%03d_%03d", $wx + $dx, $wy + $dy
174     );
175    
176     return;
177     }
178     }
179    
180     $self->SUPER::send_mapinfo ($data, $cb);
181     }
182    
183     # this method does a "flood fill" into every tile direction
184     # it assumes that tiles are arranged in a rectangular grid,
185     # i.e. a map is the same as the left of the right map etc.
186     # failure to comply are harmless and result in display errors
187     # at worst.
188     sub flood_fill {
189     my ($self, $block, $gx, $gy, $path, $hash, $flags) = @_;
190    
191     # the server does not allow map paths > 6
192     return if 7 <= length $path;
193    
194     my ($x0, $y0, $x1, $y1) = @{$self->{neigh_rect}};
195    
196     for (
197     [1, 3, 0, -1],
198     [2, 4, 1, 0],
199     [3, 1, 0, 1],
200     [4, 2, -1, 0],
201     ) {
202     my ($tile, $tile2, $dx, $dy) = @$_;
203    
204     next if $block & (1 << $tile);
205     my $block = $block | (1 << $tile2);
206    
207     my $gx = $gx + $dx;
208     my $gy = $gy + $dy;
209    
210     next unless $flags & (1 << ($tile - 1));
211     next if $self->{neigh_grid}{$gx, $gy}++;
212    
213     my $neigh = $self->{neigh_map}{$hash} ||= [];
214     if (my $info = $neigh->[$tile]) {
215     my ($flags, $x, $y, $w, $h, $hash) = @$info;
216    
217     $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
218     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
219    
220     } else {
221     $self->send_mapinfo ("spatial $path$tile", sub {
222     my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
223    
224     return if $mode ne "spatial";
225    
226     $x += $self->{map}->ox;
227     $y += $self->{map}->oy;
228    
229     $self->load_map ($hash, $x, $y)
230     unless $self->{neigh_map}{$hash}[5]++;#d#
231    
232     $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
233    
234     $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
235     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
236     });
237     }
238     }
239     }
240    
241     sub map_change {
242     my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
243    
244     $self->flush_map;
245    
246     my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
247    
248     my $mapmapw = $self->{mapmap}->{w};
249     my $mapmaph = $self->{mapmap}->{h};
250    
251     $self->{neigh_rect} = [
252     $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
253     $ox + $mapmapw * 0.5 + $w, $oy + $mapmapw * 0.5 + $h,
254     ];
255    
256     delete $self->{neigh_grid};
257    
258     $x += $ox;
259     $y += $oy;
260    
261     $self->{map_info} = [$hash, $x, $y, $w, $h];
262    
263     (my $map = $hash) =~ s/^.*?\/([^\/]+)$/\1/;
264     $::STATWIDS->{map}->set_text ("Map: " . $map);
265    
266     $self->load_map ($hash, $x, $y);
267     $self->flood_fill (0, 0, 0, "", $hash, $flags);
268     }
269    
270     sub face_find {
271     my ($self, $facenum, $face) = @_;
272    
273     my $hash = "$face->{chksum},$face->{name}";
274    
275     my $id = $self->{facemap}->get ($hash);
276    
277     unless ($id) {
278     # create new id for face
279     # I love transactions
280     for (1..100) {
281     my $txn = $CFClient::DB_ENV->txn_begin;
282 root 1.15 my $status = $self->{facemap}->db_get (id => $id);
283 root 1.1 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
284     $id = ($id || 16) + 1;
285     if ($self->{facemap}->put (id => $id) == 0
286     && $self->{facemap}->put ($hash => $id) == 0) {
287     $txn->txn_commit;
288    
289     goto gotid;
290     }
291     }
292 root 1.15 $txn->txn_abort;
293 root 1.1 }
294    
295     CFClient::fatal "maximum number of transaction retries reached - database problems?";
296     }
297    
298     gotid:
299     $face->{id} = $id;
300     $self->{map}->set_face ($facenum => $id);
301     $self->{faceid}[$facenum] = $id;#d#
302    
303     my $face = $self->{tilecache}->get ($id);
304    
305     if ($face) {
306     #$self->face_prefetch;
307     $face
308     } else {
309     my $tex = $self->{noface};
310     $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
311     undef
312     };
313     }
314    
315     sub face_update {
316     my ($self, $facenum, $face) = @_;
317    
318     $self->{tilecache}->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
319    
320     $self->set_texture ($face->{id} => delete $face->{image});
321     }
322    
323     sub set_texture {
324     my ($self, $id, $data) = @_;
325    
326     $self->{texture}[$id] ||= do {
327     my $tex =
328     new_from_image CFClient::Texture
329     $data, minify => 1, mipmap => 1;
330    
331     $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
332     $self->{map_widget}->update;
333    
334     $tex
335     };
336     }
337    
338     sub sound_play {
339     my ($self, $x, $y, $soundnum, $type) = @_;
340    
341     $self->{sound_play}->($x, $y, $soundnum, $type);
342     }
343    
344     my $LAST_QUERY; # server is stupid, stupid, stupid
345    
346     sub query {
347     my ($self, $flags, $prompt) = @_;
348    
349     $prompt = $LAST_QUERY unless length $prompt;
350     $LAST_QUERY = $prompt;
351    
352 root 1.22 $self->{query}-> ($self, $flags, $prompt);
353 root 1.1 }
354    
355     sub drawinfo {
356     my ($self, $color, $text) = @_;
357    
358     my @color = (
359     [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
360     [1.00, 1.00, 1.00],
361     [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
362     [1.00, 0.00, 0.00],
363     [1.00, 0.54, 0.00],
364     [0.11, 0.56, 1.00],
365     [0.93, 0.46, 0.00],
366     [0.18, 0.54, 0.34],
367     [0.56, 0.73, 0.56],
368     [0.80, 0.80, 0.80],
369     [0.55, 0.41, 0.13],
370     [0.99, 0.77, 0.26],
371     [0.74, 0.65, 0.41],
372     );
373    
374     my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0];
375    
376 root 1.23 # try to create single paragraphs of multiple lines sent by the server
377 root 1.24 $text =~ s/(?<=\S)\n(?=\w)/ /g;
378 root 1.23
379 root 1.1 $text = CFClient::UI::Label::escape $text;
380     $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
381     $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
382    
383     $self->{logview}->add_paragraph ($color[$color],
384     join "\n", map "$time $_", split /\n/, $text);
385    
386 elmex 1.5 $self->{statusbox}->add ($text,
387 root 1.1 group => $text,
388     fg => $color[$color],
389 root 1.18 timeout => $color >= 2 ? 60 : 10,
390 root 1.1 tooltip_font => $::FONT_FIXED,
391     );
392     }
393    
394     sub drawextinfo {
395     my ($self, $color, $type, $subtype, $message) = @_;
396    
397     $self->drawinfo ($color, $message);
398     }
399    
400     sub spell_add {
401     my ($self, $spell) = @_;
402    
403 root 1.27 # try to create single paragraphs of multiple lines sent by the server
404     $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
405     $spell->{message} =~ s/\n+$//;
406     $spell->{message} ||= "Server did not provide a description for this spell.";
407    
408 root 1.16 $::SETUP_SPELLS->add_spell ($spell);
409 elmex 1.14
410 root 1.1 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message});
411     $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message});
412     }
413    
414     sub spell_delete {
415     my ($self, $spell) = @_;
416 root 1.17 $::SETUP_SPELLS->remove_spell ($spell);
417 root 1.1 }
418    
419     sub addme_success {
420     my ($self) = @_;
421    
422 root 1.21 my $skill_help = CFClient::load_pod CFClient::find_rcfile "pod/skill_help.pod", skill_help => 1, sub {
423     my ($pom) = @_;
424 root 1.1
425 root 1.21 my %skill_help;
426 root 1.1
427 root 1.21 for my $head2 ($pom->head1->[3]->head2) {
428     $skill_help{$head2->title} = CFClient::pod_to_pango $head2->content;
429     }
430    
431     \%skill_help
432     };
433 root 1.1
434     for my $skill (values %{$self->{skill_info}}) {
435     $self->{map_widget}->add_command ("ready_skill $skill",
436     (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n")
437 root 1.21 . $skill_help->{$skill});
438 root 1.1 $self->{map_widget}->add_command ("use_skill $skill",
439     (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n")
440 root 1.21 . $skill_help->{$skill});
441 root 1.1 }
442     }
443    
444     sub eof {
445     my ($self) = @_;
446    
447     $self->{map_widget}->clr_commands;
448    
449     ::stop_game ();
450     }
451    
452     sub image_info {
453     my ($self, $numfaces) = @_;
454    
455     $self->{num_faces} = $numfaces;
456     $self->{face_prefetch} = [1 .. $numfaces];
457     $self->face_prefetch;
458     }
459    
460     sub face_prefetch {
461     my ($self) = @_;
462    
463     return unless $::CFG->{face_prefetch};
464    
465     if ($self->{num_faces}) {
466     return if @{ $self->{send_queue} || [] };
467     my $todo = @{ $self->{face_prefetch} }
468     or return;
469    
470     my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
471    
472     $self->send ("requestinfo image_sums $face $face");
473    
474 elmex 1.5 $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo",
475 root 1.19 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
476 root 1.1 } elsif (!exists $self->{num_faces}) {
477     $self->send ("requestinfo image_info");
478    
479     $self->{num_faces} = 0;
480    
481 elmex 1.5 $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch",
482 root 1.19 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
483 root 1.1 }
484     }
485    
486     sub update_floorbox {
487     $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub {
488     return unless $::CONN;
489    
490     $::FLOORBOX->clear;
491    
492     my $row;
493     for (@{ $::CONN->{container}{0} }) {
494 root 1.25 if ($row < 6) {
495 root 1.1 local $_->{face_widget}; # hack to force recreation of widget
496     local $_->{desc_widget}; # hack to force recreation of widget
497     CFClient::Item::update_widgets $_;
498    
499     $::FLOORBOX->add (0, $row, $_->{face_widget});
500     $::FLOORBOX->add (1, $row, $_->{desc_widget});
501    
502     $row++;
503     } else {
504 root 1.25 $::FLOORBOX->add (1, $row, new CFClient::UI::Button
505 root 1.30 text => "More...",
506 root 1.25 on_activate => sub { $::INV_WINDOW->toggle_visibility },
507     );
508 root 1.1 last;
509     }
510     }
511     });
512    
513     $::WANT_REFRESH++;
514     }
515    
516     sub set_opencont {
517     my ($conn, $tag, $name) = @_;
518     $conn->{open_container} = $tag;
519 elmex 1.10
520     $::INV_RIGHT_HB->clear ();
521     $::INV_RIGHT_HB->add (new CFClient::UI::Label align => 0, expand => 1, text => $name);
522    
523     if ($tag != 0) { # Floor isn't closable, is it?
524     $::INV_RIGHT_HB->add (new CFClient::UI::Button
525     text => "Close container",
526     tooltip => "Close the currently open container (if one is open)",
527     on_activate => sub {
528     $::CONN->send ("apply $tag") # $::CONN->{open_container}")
529     if $tag != 0;
530     #if $CONN->{open_container} != 0;
531     },
532     );
533     }
534    
535 root 1.1 $::INVR->set_items ($conn->{container}{$tag});
536     }
537    
538     sub update_container {
539     my ($tag) = @_;
540    
541     $::INVR->set_items ($::CONN->{container}{$::CONN->{open_container}})
542     if $tag == $::CONN->{open_container};
543     }
544    
545     sub container_add {
546     my ($self, $tag, $items) = @_;
547    
548     #d# print "container_add: container $tag ($self->{player}{tag})\n";
549    
550     if ($tag == 0) {
551     update_floorbox;
552     update_container (0);
553     } elsif ($tag == $self->{player}{tag}) {
554     $::INV->set_items ($self->{container}{$self->{player}{tag}})
555     } else {
556     update_container ($tag);
557     }
558    
559     # $self-<{player}{tag} => player inv
560     #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}};
561     }
562    
563     sub container_clear {
564     my ($self, $tag) = @_;
565    
566     #d# print "container_clear: container $tag ($self->{player}{tag})\n";
567    
568     if ($tag == 0) {
569     update_floorbox;
570     update_container (0);
571     } elsif ($tag == $self->{player}{tag}) {
572     $::INV->set_items ($self->{container}{$tag})
573 elmex 1.12 } else {
574     update_container ($tag);
575 root 1.1 }
576    
577     # use PApp::Util; warn PApp::Util::dumpval $self->{container}{0};
578     }
579    
580     sub item_delete {
581     my ($self, @items) = @_;
582    
583     for (@items) {
584     #d# print "item_delete: $_->{tag} from $_->{container} ($self->{player}{tag})\n";
585    
586     if ($_->{container} == 0) {
587     update_floorbox;
588     update_container ($_->{tag});
589     } elsif ($_->{container} == $self->{player}{tag}) {
590     $::INV->set_items ($self->{container}{$self->{player}{tag}})
591     } else {
592 elmex 1.12 update_container ($_->{container});
593 root 1.1 }
594     }
595     }
596    
597     sub item_update {
598     my ($self, $item) = @_;
599    
600     #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
601    
602     if ($item->{tag} == $self->{player}{tag}) {
603     $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $item->{weight} / 1000);
604     return;
605     }
606    
607     CFClient::Item::update_widgets $item;
608    
609 root 1.2 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
610 root 1.1 set_opencont ($::CONN, 0, "Floor");
611    
612 root 1.2 } elsif ($item->{flags} & F_OPEN) {
613 root 1.1 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item);
614     } else {
615     if ($item->{container} == 0) {
616     update_floorbox;
617     update_container (0);
618     } elsif ($item->{container} == $self->{player}{tag}) {
619     $::INV->set_items ($self->{container}{$item->{container}})
620     }
621     }
622     }
623    
624 elmex 1.3 sub player_update {
625     my ($self, $player) = @_;
626 root 1.26
627 elmex 1.3 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000);
628 root 1.28 }
629    
630 root 1.32 sub update_server_info {
631     my ($self) = @_;
632    
633     my @yesno = ("<span foreground='red'>no</span>", "<span foreground='green'>yes</span>");
634    
635     $::SERVER_INFO->set_markup (
636     "server <tt>$self->{host}:$self->{port}</tt>\n"
637     . "protocol version <tt>$self->{version}</tt>\n"
638     . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
639     . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n"
640     . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
641     . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
642     . "map size $self->{mapw}×$self->{maph}\n"
643     );
644     }
645    
646 root 1.28 sub logged_in {
647     my ($self) = @_;
648    
649 root 1.32 $self->send_ext_req (cfplus_support => "1", sub {
650 root 1.28 $self->{cfplus_ext} = $_[0];
651 root 1.32 $self->update_server_info;
652     });
653 root 1.31
654 root 1.32 $self->update_server_info;
655 root 1.13
656     $self->send_command ("output-sync $::CFG->{output_sync}");
657     $self->send_command ("output-count $::CFG->{output_count}");
658 root 1.29 $self->send_command ("pickup $::CFG->{pickup}");
659 elmex 1.3 }
660    
661 root 1.32 sub lookat {
662     my ($self, $x, $y) = @_;
663    
664     if ($self->{cfplus_ext}) {
665     $self->send_ext_req (lookat => "$x $y", sub {
666     my %res = split /\x00/, $_[0];
667    
668     if (exists $res{npc_dialog}) {
669     # start npc chat dialog
670     $self->{npc_dialog} = new CFClient::NPCDialog::
671     dx => $x,
672     dy => $y,
673     title => "$res{npc_dialog} (NPC)",
674     conn => $self,
675     ;
676     }
677     });
678     }
679    
680     $self->send ("lookat $x $y");
681     }
682    
683     sub destroy {
684     my ($self) = @_;
685    
686     $self->{npc_dialog}->destroy
687     if $self->{npc_dialog};
688    
689     $self->SUPER::destroy;
690     }
691    
692     package CFClient::NPCDialog;
693    
694     our @ISA = 'CFClient::UI::FancyFrame';
695    
696     sub new {
697     my $class = shift;
698    
699     my $self = $class->SUPER::new (
700     x => 'center',
701     y => 'center',
702     name => "npc_dialog",
703     force_w => $::WIDTH * 0.7,
704     force_h => $::HEIGHT * 0.7,
705     title => "NPC Dialog",
706     kw => { hi => 0, yes => 0, no => 0 },
707     @_,
708     );
709    
710     Scalar::Util::weaken (my $this = $self);
711    
712     # better use a pane...
713     $self->add (my $hbox = new CFClient::UI::HBox);
714     $hbox->add ($self->{textview} = new CFClient::UI::TextScroller expand => 1);
715    
716     $hbox->add (my $vbox = new CFClient::UI::VBox);
717    
718     $vbox->add (new CFClient::UI::Label text => "Message Entry:");
719     $vbox->add ($self->{entry} = new CFClient::UI::Entry
720     tooltip => "Enter a message you want to tell the NPC and press <b>return</b>.\n\n"
721     . "Sometimes you have to tell an NPC something you cannot find out during "
722     . "a normal conversation (such as a password). In those cases you have to use "
723     . "this text entry. You can also enter responses manually instead of using the response "
724     . "buttons below.",
725     on_activate => sub {
726     my ($entry, $text) = @_;
727    
728     return unless $text =~ /\S/;
729    
730     $entry->set_text ("");
731     $this->send ($text);
732     },
733     );
734    
735     $vbox->add ($self->{options} = new CFClient::UI::VBox);
736    
737     $self->{close_button} = new CFClient::UI::Button
738     text => "Bye (close)",
739     tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.",
740     on_activate => sub { $this->destroy },
741     ;
742    
743     $self->update_options;
744    
745     $self->{token} = $self->{conn}->ext_token;
746     $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) });
747     $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}");
748    
749     $self->{entry}->focus_in;
750    
751     $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n");
752    
753     $self->show;
754     $self
755     };
756    
757     sub update_options {
758     my ($self) = @_;
759    
760     Scalar::Util::weaken $self;
761    
762     $self->{options}->clear;
763     $self->{options}->add ($self->{close_button});
764    
765     for my $kw (sort keys %{ $self->{kw} }) {
766     $self->{options}->add (new CFClient::UI::Button
767     text => $kw,
768     on_activate => sub {
769     $self->send ($kw);
770     },
771     );
772     }
773     }
774    
775     sub feed {
776     my ($self, $data) = @_;
777    
778     my ($type, $msg) = split / /, $data, 2;
779    
780     if ($type eq "msg") {
781     my ($msg, @kw) = split /\x00/, $msg;
782     $self->{kw}{$_} = 1 for @kw;
783    
784     warn "<<<<@kw>>>>\n";#d#
785    
786     $msg = CFClient::UI::Label::escape $msg;
787     my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
788     $msg =~ s/($match)/<span foreground='#c0c0ff' underline='none'>$1<\/span>/gi; # underline when http-ready, huh.
789    
790     $self->{textview}->add_paragraph ([1, 1, 1, 1], "\n$msg");
791     $self->update_options;
792     } else {
793     $self->destroy;
794     }
795    
796     1
797     }
798    
799     sub send {
800     my ($self, $msg) = @_;
801    
802     $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg");
803     $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg);
804     }
805    
806     sub destroy {
807     my ($self) = @_;
808    
809     #Carp::cluck "debug\n";#d# #todo# enable: destroyx gets called twice because scalar keys {} is 1
810    
811     delete $self->{conn}{npc_dialog};
812     $self->{conn}->disconnect_ext ($self->{token});
813    
814     $self->SUPER::destroy;
815     }
816    
817 root 1.1 1;