ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/Protocol.pm (file contents):
Revision 1.17 by root, Sat Jun 3 02:39:27 2006 UTC vs.
Revision 1.32 by root, Mon Jun 12 13:26:14 2006 UTC

3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Crossfire::Protocol::Constants; 6use Crossfire::Protocol::Constants;
7 7
8use CFClient;
8use CFClient::UI; 9use CFClient::UI;
9 10
10use base 'Crossfire::Protocol::Base'; 11use base 'Crossfire::Protocol::Base';
11 12
12sub new { 13sub new {
14 15
15 my $self = $class->SUPER::new (@_); 16 my $self = $class->SUPER::new (@_);
16 17
17 $self->{map_widget}->clr_commands; 18 $self->{map_widget}->clr_commands;
18 19
19 my $parser = new Pod::POM; 20 my $cmd_help = CFClient::load_pod CFClient::find_rcfile "pod/command_help.pod", command_help => 1, sub {
20 my $pod = $parser->parse_file (CFClient::find_rcfile "pod/command_help.pod"); 21 my ($pom) = @_;
21 22
23 my @cmd_help;
24
22 for my $head2 ($pod->head1->[-2]->head2) { 25 for my $head2 ($pom->head1->[-2]->head2) {
23 $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x 26 $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
24 or next; 27 or next;
25 28
26 my $cmd = $1; 29 my $cmd = $1;
27 my @args = split /\|/, $2; 30 my @args = split /\|/, $2;
28 @args = (".*") unless @args; 31 @args = (".*") unless @args;
29 32
30 $_ = $_ eq ".*" ? "" : " $_" 33 $_ = $_ eq ".*" ? "" : " $_"
31 for @args; 34 for @args;
32 35
33 my $text = CFClient::pod_to_pango $head2->content; 36 my $text = CFClient::pod_to_pango $head2->content;
34 37
35 $self->{map_widget}->add_command ("$cmd$_", $text) 38 push @cmd_help, ["$cmd$_", $text]
36 for sort { (length $a) <=> (length $b) } 39 for sort { (length $a) <=> (length $b) }
37 @args; 40 @args;
41 }
42
43 \@cmd_help
38 } 44 };
45
46 $self->{map_widget}->add_command (@$_)
47 for @$cmd_help;
39 48
40 $self->{noface} = new_from_file CFClient::Texture 49 $self->{noface} = new_from_file CFClient::Texture
41 CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1; 50 CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1;
42 51
43 $self->{open_container} = 0; 52 $self->{open_container} = 0;
338 my ($self, $flags, $prompt) = @_; 347 my ($self, $flags, $prompt) = @_;
339 348
340 $prompt = $LAST_QUERY unless length $prompt; 349 $prompt = $LAST_QUERY unless length $prompt;
341 $LAST_QUERY = $prompt; 350 $LAST_QUERY = $prompt;
342 351
343 my $dialog = new CFClient::UI::FancyFrame 352 $self->{query}-> ($self, $flags, $prompt);
344 x => "center",
345 y => "center",
346 title => "Query",
347 child => my $vbox = new CFClient::UI::VBox,
348 ;
349
350 $vbox->add (new CFClient::UI::Label
351 max_w => $::WIDTH * 0.4,
352 ellipsise => 0,
353 text => $prompt);
354
355 if ($flags & CS_QUERY_YESNO) {
356 $vbox->add (my $hbox = new CFClient::UI::HBox);
357 $hbox->add (new CFClient::UI::Button
358 text => "No",
359 on_activate => sub {
360 $self->send ("reply n");
361 $dialog->destroy;
362 $self->{map_widget}->focus_in;
363 }
364 );
365 $hbox->add (new CFClient::UI::Button
366 text => "Yes",
367 on_activate => sub {
368 $self->send ("reply y");
369 $dialog->destroy;
370 },
371 );
372
373 $dialog->focus_in;
374
375 } elsif ($flags & CS_QUERY_SINGLECHAR) {
376 $dialog->{tooltip} = "Press a key (click on the entry to make sure it has keyboard focus)";
377 $vbox->add (my $entry = new CFClient::UI::Entry
378 on_changed => sub {
379 $self->send ("reply $_[1]");
380 $dialog->destroy;
381 },
382 );
383
384 $entry->focus_in;
385
386 } else {
387 $dialog->{tooltip} = "Enter the reply and press return (click on the entry to make sure it has keyboard focus)";
388
389 $vbox->add (my $entry = new CFClient::UI::Entry
390 $flags & CS_QUERY_HIDEINPUT ? (hiddenchar => "*") : (),
391 on_activate => sub {
392 $self->send ("reply $_[1]");
393 $dialog->destroy;
394 },
395 );
396
397 $entry->focus_in;
398 }
399
400 $dialog->show;
401} 353}
402 354
403sub drawinfo { 355sub drawinfo {
404 my ($self, $color, $text) = @_; 356 my ($self, $color, $text) = @_;
405 357
419 [0.74, 0.65, 0.41], 371 [0.74, 0.65, 0.41],
420 ); 372 );
421 373
422 my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; 374 my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0];
423 375
376 # try to create single paragraphs of multiple lines sent by the server
377 $text =~ s/(?<=\S)\n(?=\w)/ /g;
378
424 $text = CFClient::UI::Label::escape $text; 379 $text = CFClient::UI::Label::escape $text;
425 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 380 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
426 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; 381 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
427 382
428 $self->{logview}->add_paragraph ($color[$color], 383 $self->{logview}->add_paragraph ($color[$color],
429 join "\n", map "$time $_", split /\n/, $text); 384 join "\n", map "$time $_", split /\n/, $text);
430 385
431 $self->{statusbox}->add ($text, 386 $self->{statusbox}->add ($text,
432 group => $text, 387 group => $text,
433 fg => $color[$color], 388 fg => $color[$color],
434 timeout => 10, 389 timeout => $color >= 2 ? 60 : 10,
435 tooltip_font => $::FONT_FIXED, 390 tooltip_font => $::FONT_FIXED,
436 ); 391 );
437} 392}
438 393
439sub drawextinfo { 394sub drawextinfo {
443} 398}
444 399
445sub spell_add { 400sub spell_add {
446 my ($self, $spell) = @_; 401 my ($self, $spell) = @_;
447 402
448 # TODO 403 # try to create single paragraphs of multiple lines sent by the server
449 # create a widget dynamically, using spell face (CF::Protocol downloads them) 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
450 $::SETUP_SPELLS->add_spell ($spell); 408 $::SETUP_SPELLS->add_spell ($spell);
451 409
452 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 410 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message});
453 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 411 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message});
454} 412}
459} 417}
460 418
461sub addme_success { 419sub addme_success {
462 my ($self) = @_; 420 my ($self) = @_;
463 421
464 my $parser = new Pod::POM; 422 my $skill_help = CFClient::load_pod CFClient::find_rcfile "pod/skill_help.pod", skill_help => 1, sub {
465 my $pod = $parser->parse_file (CFClient::find_rcfile "pod/skill_help.pod"); 423 my ($pom) = @_;
466 424
467 my %skill_tooltip; 425 my %skill_help;
468 426
469 for my $head2 ($pod->head1->[-2]->head2) { 427 for my $head2 ($pom->head1->[3]->head2) {
470 $skill_tooltip{$head2->title} = CFClient::pod_to_pango $head2->content; 428 $skill_help{$head2->title} = CFClient::pod_to_pango $head2->content;
429 }
430
431 \%skill_help
471 } 432 };
472 433
473 for my $skill (values %{$self->{skill_info}}) { 434 for my $skill (values %{$self->{skill_info}}) {
474 $self->{map_widget}->add_command ("ready_skill $skill", 435 $self->{map_widget}->add_command ("ready_skill $skill",
475 (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n") 436 (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n")
476 . $skill_tooltip{$skill}); 437 . $skill_help->{$skill});
477 $self->{map_widget}->add_command ("use_skill $skill", 438 $self->{map_widget}->add_command ("use_skill $skill",
478 (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n") 439 (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n")
479 . $skill_tooltip{$skill}); 440 . $skill_help->{$skill});
480 } 441 }
481} 442}
482 443
483sub eof { 444sub eof {
484 my ($self) = @_; 445 my ($self) = @_;
509 my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, (); 470 my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
510 471
511 $self->send ("requestinfo image_sums $face $face"); 472 $self->send ("requestinfo image_sums $face $face");
512 473
513 $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo", 474 $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo",
514 group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]); 475 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
515 } elsif (!exists $self->{num_faces}) { 476 } elsif (!exists $self->{num_faces}) {
516 $self->send ("requestinfo image_info"); 477 $self->send ("requestinfo image_info");
517 478
518 $self->{num_faces} = 0; 479 $self->{num_faces} = 0;
519 480
520 $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch", 481 $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch",
521 group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]); 482 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
522 } 483 }
523} 484}
524 485
525sub update_floorbox { 486sub update_floorbox {
526 $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub { 487 $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub {
528 489
529 $::FLOORBOX->clear; 490 $::FLOORBOX->clear;
530 491
531 my $row; 492 my $row;
532 for (@{ $::CONN->{container}{0} }) { 493 for (@{ $::CONN->{container}{0} }) {
533 if ($row < 7) { 494 if ($row < 6) {
534 local $_->{face_widget}; # hack to force recreation of widget 495 local $_->{face_widget}; # hack to force recreation of widget
535 local $_->{desc_widget}; # hack to force recreation of widget 496 local $_->{desc_widget}; # hack to force recreation of widget
536 CFClient::Item::update_widgets $_; 497 CFClient::Item::update_widgets $_;
537 498
538 $::FLOORBOX->add (0, $row, $_->{face_widget}); 499 $::FLOORBOX->add (0, $row, $_->{face_widget});
539 $::FLOORBOX->add (1, $row, $_->{desc_widget}); 500 $::FLOORBOX->add (1, $row, $_->{desc_widget});
540 501
541 $row++; 502 $row++;
542 } else { 503 } else {
543 $::FLOORBOX->add (1, $row, new CFClient::UI::Label text => "More..."); 504 $::FLOORBOX->add (1, $row, new CFClient::UI::Button
505 text => "More...",
506 on_activate => sub { $::INV_WINDOW->toggle_visibility },
507 );
544 last; 508 last;
545 } 509 }
546 } 510 }
547 }); 511 });
548 512
657 } 621 }
658} 622}
659 623
660sub player_update { 624sub player_update {
661 my ($self, $player) = @_; 625 my ($self, $player) = @_;
626
662 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); 627 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000);
628}
663 629
664 # do it here because it is ignored earlier, and there is no "login" event 630sub 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
646sub logged_in {
647 my ($self) = @_;
648
649 $self->send_ext_req (cfplus_support => "1", sub {
650 $self->{cfplus_ext} = $_[0];
651 $self->update_server_info;
652 });
653
654 $self->update_server_info;
655
665 $self->send_command ("output-sync $::CFG->{output_sync}"); 656 $self->send_command ("output-sync $::CFG->{output_sync}");
666 $self->send_command ("output-count $::CFG->{output_count}"); 657 $self->send_command ("output-count $::CFG->{output_count}");
658 $self->send_command ("pickup $::CFG->{pickup}");
659}
660
661sub 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
683sub destroy {
684 my ($self) = @_;
685
686 $self->{npc_dialog}->destroy
687 if $self->{npc_dialog};
688
689 $self->SUPER::destroy;
690}
691
692package CFClient::NPCDialog;
693
694our @ISA = 'CFClient::UI::FancyFrame';
695
696sub 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
757sub 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
775sub 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
799sub 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
806sub 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;
667} 815}
668 816
6691; 8171;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines