ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.36
Committed: Thu Jun 15 15:33:58 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.35: +6 -0 lines
Log Message:
implemented preliminary magic mapping support

File Contents

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