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.37 by root, Thu Jun 15 15:47:36 2006 UTC vs.
Revision 1.52 by root, Mon Jul 3 22:04:25 2006 UTC

83 83
84 print $fh "$ts ", @a, "\n"; 84 print $fh "$ts ", @a, "\n";
85 $fh->flush; 85 $fh->flush;
86} 86}
87 87
88sub _stat_numdiff {
89 my ($old, $new) = @_;
88 90
91 my $diff = $new - $old;
92 $diff > 0 ? "+$diff" : $diff
93}
94
95sub _stat_skillmaskdiff {
96 my ($old, $new) = @_;
97
98 my @diff;
99
100 my $changed = $old ^ $new;
101
102 my @diff = map {
103 $changed & (1<<$_)
104 ? (($new & (1<<$_) ? "+" : "-") . "skill$_")
105 : ()
106 }
107 0 .. 31; # TODO
108
109 join "", @diff
110}
111
112# all stats that are chacked against changes
113my @statchange = (
114 [&CS_STAT_STR => \&_stat_numdiff, "Str"],
115 [&CS_STAT_INT => \&_stat_numdiff, "Int"],
116 [&CS_STAT_WIS => \&_stat_numdiff, "Wis"],
117 [&CS_STAT_DEX => \&_stat_numdiff, "Dex"],
118 [&CS_STAT_CON => \&_stat_numdiff, "Con"],
119 [&CS_STAT_CHA => \&_stat_numdiff, "Cha"],
120 [&CS_STAT_POW => \&_stat_numdiff, "Pow"],
121 [&CS_STAT_WC => \&_stat_numdiff, "Wc"],
122 [&CS_STAT_AC => \&_stat_numdiff, "Ac"],
123 [&CS_STAT_DAM => \&_stat_numdiff, "Dam"],
124 [&CS_STAT_SPEED => \&_stat_numdiff, "Speed"],
125 [&CS_STAT_WEAP_SP => \&_stat_numdiff, "WSp"],
126 [&CS_STAT_MAXHP => \&_stat_numdiff, "HP"],
127 [&CS_STAT_MAXSP => \&_stat_numdiff, "Mana"],
128 [&CS_STAT_MAXGRACE => \&_stat_numdiff, "Grace"],
129 [&CS_STAT_WEIGHT_LIM => \&_stat_numdiff, "Weight"],
130 [&CS_STAT_SPELL_ATTUNE => \&_stat_skillmaskdiff, "attuned"],
131 [&CS_STAT_SPELL_REPEL => \&_stat_skillmaskdiff, "repelled"],
132 [&CS_STAT_SPELL_DENY => \&_stat_skillmaskdiff, "denied"],
133 [&CS_STAT_RES_PHYS => \&_stat_numdiff, "phys"],
134 [&CS_STAT_RES_MAG => \&_stat_numdiff, "magic"],
135 [&CS_STAT_RES_FIRE => \&_stat_numdiff, "fire"],
136 [&CS_STAT_RES_ELEC => \&_stat_numdiff, "electricity"],
137 [&CS_STAT_RES_COLD => \&_stat_numdiff, "cold"],
138 [&CS_STAT_RES_CONF => \&_stat_numdiff, "confusion"],
139 [&CS_STAT_RES_ACID => \&_stat_numdiff, "acid"],
140 [&CS_STAT_RES_DRAIN => \&_stat_numdiff, "drain"],
141 [&CS_STAT_RES_GHOSTHIT => \&_stat_numdiff, "ghosthit"],
142 [&CS_STAT_RES_POISON => \&_stat_numdiff, "poison"],
143 [&CS_STAT_RES_SLOW => \&_stat_numdiff, "slow"],
144 [&CS_STAT_RES_PARA => \&_stat_numdiff, "paralyse"],
145 [&CS_STAT_TURN_UNDEAD => \&_stat_numdiff, "turnundead"],
146 [&CS_STAT_RES_FEAR => \&_stat_numdiff, "fear"],
147 [&CS_STAT_RES_DEPLETE => \&_stat_numdiff, "depletion"],
148 [&CS_STAT_RES_DEATH => \&_stat_numdiff, "death"],
149 [&CS_STAT_RES_HOLYWORD => \&_stat_numdiff, "godpower"],
150 [&CS_STAT_RES_BLIND => \&_stat_numdiff, "blind"],
151);
89 152
90sub stats_update { 153sub stats_update {
91 my ($self, $stats) = @_; 154 my ($self, $stats) = @_;
92 155
93 if (my $exp = $stats->{+CS_STAT_EXP64}) { 156 if (my $prev = $self->{prev_stats}) {
94 my $diff = $exp - $self->{prev_exp}; 157 if (my $diff = $stats->{+CS_STAT_EXP64} - $prev->{+CS_STAT_EXP64}) {
95 $self->{statusbox}->add ("$diff experience gained", group => "experience $diff", fg => [0.5, 1, 0.5, 0.8], timeout => 5) 158 $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; 159 }
97 $self->{prev_exp} = $exp; 160
161 if (
162 my @diffs = map {
163 $stats->{$_->[0]} != $prev->{$_->[0]}
164 ? $_->[2] . $_->[1]->($prev->{$_->[0]}, $stats->{$_->[0]}) : ();
165 }
166 @statchange
167 ) {
168 my $msg = "<b>stat change</b>: " . (join " ", @diffs);
169 $self->{statusbox}->add ($msg, group => $msg, fg => [0.8, 1, 0.2, 1], timeout => 10);
170 }
98 } 171 }
172
173 $self->{prev_stats} = { %$stats };
99 174
100 ::update_stats_window ($stats); 175 ::update_stats_window ($stats);
101} 176}
102 177
103sub user_send { 178sub user_send {
107 push @{$self->{record}}, $command; 182 push @{$self->{record}}, $command;
108 } 183 }
109 184
110 $self->logprint ("send: ", $command); 185 $self->logprint ("send: ", $command);
111 $self->send_command ($command); 186 $self->send_command ($command);
112 ::status $command; 187 ::status ($command);
113} 188}
114 189
115sub start_record { 190sub start_record {
116 my ($self) = @_; 191 my ($self) = @_;
117 192
414 489
415 $text = CFClient::UI::Label::escape $text; 490 $text = CFClient::UI::Label::escape $text;
416 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 491 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
417 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; 492 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
418 493
419 $self->{logview}->add_paragraph ($color[$color], 494 $self->{logview}->add_paragraph ($color[$color], $_)
420 join "\n", map "$time $_", split /\n/, $text); 495 for map "$time $_", split /\n/, $text;
496 $self->{logview}->scroll_to_bottom;
421 497
422 $self->{statusbox}->add ($text, 498 $self->{statusbox}->add ($text,
423 group => $text, 499 group => $text,
424 fg => $color[$color], 500 fg => $color[$color],
425 timeout => $color >= 2 ? 60 : 10, 501 timeout => $color >= 2 ? 60 : 10,
439 # try to create single paragraphs of multiple lines sent by the server 515 # try to create single paragraphs of multiple lines sent by the server
440 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g; 516 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
441 $spell->{message} =~ s/\n+$//; 517 $spell->{message} =~ s/\n+$//;
442 $spell->{message} ||= "Server did not provide a description for this spell."; 518 $spell->{message} ||= "Server did not provide a description for this spell.";
443 519
444 $::SETUP_SPELLS->add_spell ($spell); 520 $::SPELL_PAGE->add_spell ($spell);
445 521
446 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 522 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message});
447 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 523 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message});
448} 524}
449 525
450sub spell_delete { 526sub spell_delete {
451 my ($self, $spell) = @_; 527 my ($self, $spell) = @_;
528
452 $::SETUP_SPELLS->remove_spell ($spell); 529 $::SPELL_PAGE->remove_spell ($spell);
453} 530}
454 531
455sub addme_success { 532sub addme_success {
456 my ($self) = @_; 533 my ($self) = @_;
457 534
524 return unless $::CONN; 601 return unless $::CONN;
525 602
526 $::FLOORBOX->clear; 603 $::FLOORBOX->clear;
527 604
528 my $row; 605 my $row;
529 for (@{ $::CONN->{container}{0} }) { 606 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{0} }) {
530 if ($row < 6) { 607 if ($row < 6) {
531 local $_->{face_widget}; # hack to force recreation of widget 608 local $_->{face_widget}; # hack to force recreation of widget
532 local $_->{desc_widget}; # hack to force recreation of widget 609 local $_->{desc_widget}; # hack to force recreation of widget
533 CFClient::Item::update_widgets $_; 610 CFClient::Item::update_widgets $_;
534 611
537 614
538 $row++; 615 $row++;
539 } else { 616 } else {
540 $::FLOORBOX->add (1, $row, new CFClient::UI::Button 617 $::FLOORBOX->add (1, $row, new CFClient::UI::Button
541 text => "More...", 618 text => "More...",
542 on_activate => sub { $::INV_WINDOW->toggle_visibility }, 619 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 },
543 ); 620 );
544 last; 621 last;
545 } 622 }
546 } 623 }
547 }); 624 });
562 tooltip => "Close the currently open container (if one is open)", 639 tooltip => "Close the currently open container (if one is open)",
563 on_activate => sub { 640 on_activate => sub {
564 $::CONN->send ("apply $tag") # $::CONN->{open_container}") 641 $::CONN->send ("apply $tag") # $::CONN->{open_container}")
565 if $tag != 0; 642 if $tag != 0;
566 #if $CONN->{open_container} != 0; 643 #if $CONN->{open_container} != 0;
644 0
567 }, 645 },
568 ); 646 );
569 } 647 }
570 648
571 $::INVR->set_items ($conn->{container}{$tag}); 649 $::INVR->set_items ($conn->{container}{$tag});
572} 650}
573 651
574sub update_container { 652sub update_containers {
575 my ($tag) = @_; 653 my ($self) = @_;
576 654
577 $::INVR->set_items ($::CONN->{container}{$::CONN->{open_container}}) 655 $CFClient::UI::ROOT->on_refresh ("update_containers_$self" => sub {
656 for my $tag (keys %{ delete $self->{update_container} }) {
657 if ($tag == 0) {
658 update_floorbox;
659 $::INVR->set_items ($self->{container}{0})
578 if $tag == $::CONN->{open_container}; 660 if $tag == $self->{open_container};
661 } elsif ($tag == $self->{player}{tag}) {
662 $::INV->set_items ($self->{container}{$tag})
663 } else {
664 $::INVR->set_items ($self->{container}{$tag})
665 if $tag == $self->{open_container};
666 }
667 }
668 });
579} 669}
580 670
581sub container_add { 671sub container_add {
582 my ($self, $tag, $items) = @_; 672 my ($self, $tag, $items) = @_;
583 673
584 #d# print "container_add: container $tag ($self->{player}{tag})\n"; 674 $self->{update_container}{$tag}++;
585 675 $self->update_containers;
586 if ($tag == 0) {
587 update_floorbox;
588 update_container (0);
589 } elsif ($tag == $self->{player}{tag}) {
590 $::INV->set_items ($self->{container}{$self->{player}{tag}})
591 } else {
592 update_container ($tag);
593 }
594
595 # $self-<{player}{tag} => player inv
596 #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}};
597} 676}
598 677
599sub container_clear { 678sub container_clear {
600 my ($self, $tag) = @_; 679 my ($self, $tag) = @_;
601 680
602 #d# print "container_clear: container $tag ($self->{player}{tag})\n"; 681 $self->{update_container}{$tag}++;
603 682 $self->update_containers;
604 if ($tag == 0) {
605 update_floorbox;
606 update_container (0);
607 } elsif ($tag == $self->{player}{tag}) {
608 $::INV->set_items ($self->{container}{$tag})
609 } else {
610 update_container ($tag);
611 }
612
613# use PApp::Util; warn PApp::Util::dumpval $self->{container}{0};
614} 683}
615 684
616sub item_delete { 685sub item_delete {
617 my ($self, @items) = @_; 686 my ($self, @items) = @_;
618 687
688 $self->{update_container}{$_->{container}}++
619 for (@items) { 689 for @items;
620 #d# print "item_delete: $_->{tag} from $_->{container} ($self->{player}{tag})\n";
621
622 if ($_->{container} == 0) {
623 update_floorbox;
624 update_container ($_->{tag});
625 } elsif ($_->{container} == $self->{player}{tag}) {
626 $::INV->set_items ($self->{container}{$self->{player}{tag}})
627 } else {
628 update_container ($_->{container});
629 }
630 } 690
691 $self->update_containers;
631} 692}
632 693
633sub item_update { 694sub item_update {
634 my ($self, $item) = @_; 695 my ($self, $item) = @_;
635 696
636 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n"; 697 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
637 698
638 if ($item->{tag} == $self->{player}{tag}) {
639 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $item->{weight} / 1000);
640 return;
641 }
642
643 CFClient::Item::update_widgets $item; 699 CFClient::Item::update_widgets $item;
644 700
645 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { 701 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
646 set_opencont ($::CONN, 0, "Floor"); 702 set_opencont ($::CONN, 0, "Floor");
647 703
648 } elsif ($item->{flags} & F_OPEN) { 704 } elsif ($item->{flags} & F_OPEN) {
649 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); 705 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item);
706
650 } else { 707 } else {
708 $self->{update_container}{$item->{container}}++;
709 $self->update_containers;
651 if ($item->{container} == 0) { 710# if ($item->{container} == 0) {
652 update_floorbox; 711# update_floorbox;
653 update_container (0); 712# update_container (0);
654 } elsif ($item->{container} == $self->{player}{tag}) { 713# } elsif ($item->{container} == $self->{player}{tag}) {
655 $::INV->set_items ($self->{container}{$item->{container}}) 714# $::INV->set_items ($self->{container}{$item->{container}})
656 } 715# }
657 } 716 }
658} 717}
659 718
660sub player_update { 719sub player_update {
661 my ($self, $player) = @_; 720 my ($self, $player) = @_;
662
663 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); 721 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000);
664} 722}
665 723
666sub update_server_info { 724sub update_server_info {
667 my ($self) = @_; 725 my ($self) = @_;
763 821
764 return unless $text =~ /\S/; 822 return unless $text =~ /\S/;
765 823
766 $entry->set_text (""); 824 $entry->set_text ("");
767 $this->send ($text); 825 $this->send ($text);
826
827 0
768 }, 828 },
769 ); 829 );
770 830
771 $vbox->add ($self->{options} = new CFClient::UI::VBox); 831 $vbox->add ($self->{options} = new CFClient::UI::VBox);
772 832
773 $self->{close_button} = new CFClient::UI::Button 833 $self->{bye_button} = new CFClient::UI::Button
774 text => "Bye (close)", 834 text => "Bye (close)",
775 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.", 835 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.",
776 on_activate => sub { $this->destroy }, 836 on_activate => sub { $this->destroy; 0 },
777 ; 837 ;
778 838
779 $self->update_options; 839 $self->update_options;
780 840
781 $self->{token} = $self->{conn}->ext_token; 841 $self->{token} = $self->{conn}->ext_token;
782 $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) }); 842 $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) });
783 $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}"); 843 $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}");
784 844
785 $self->{entry}->focus_in; 845 $self->{entry}->grab_focus;
786 846
787 $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n"); 847 $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n");
788 848
789 $self->show; 849 $self->show;
790 $self 850 $self
794 my ($self) = @_; 854 my ($self) = @_;
795 855
796 Scalar::Util::weaken $self; 856 Scalar::Util::weaken $self;
797 857
798 $self->{options}->clear; 858 $self->{options}->clear;
799 $self->{options}->add ($self->{close_button}); 859 $self->{options}->add ($self->{bye_button});
800 860
801 for my $kw (sort keys %{ $self->{kw} }) { 861 for my $kw (sort keys %{ $self->{kw} }) {
802 $self->{options}->add (new CFClient::UI::Button 862 $self->{options}->add (new CFClient::UI::Button
803 text => $kw, 863 text => $kw,
804 on_activate => sub { 864 on_activate => sub {
805 $self->send ($kw); 865 $self->send ($kw);
866 0
806 }, 867 },
807 ); 868 );
808 } 869 }
809} 870}
810 871
811sub feed { 872sub feed {
812 my ($self, $data) = @_; 873 my ($self, $data) = @_;
874
875 Scalar::Util::weaken $self;
813 876
814 my ($type, $msg) = split / /, $data, 2; 877 my ($type, $msg) = split / /, $data, 2;
815 878
816 if ($type eq "msg") { 879 if ($type eq "msg") {
817 my ($msg, @kw) = split /\x00/, $msg; 880 my ($msg, @kw) = split /\x00/, $msg;
818 $self->{kw}{$_} = 1 for @kw; 881 $self->{kw}{$_} = 1 for @kw;
819 882
820 $msg = CFClient::UI::Label::escape $msg; 883 $msg = "\n" . CFClient::UI::Label::escape $msg;
821 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; 884 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
822 $msg =~ s/($match)/<span foreground='#c0c0ff' underline='none'>$1<\/span>/gi; # underline when http-ready, huh. 885 my @link;
886 $msg =~ s{
887 ($match)
888 }{
889 my $kw = $1;
890
891 push @link, new CFClient::UI::Label
892 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
893 can_hover => 1,
894 can_events => 1,
895 padding_x => 0,
896 padding_y => 0,
897 on_button_up => sub {
898 $self->send ($kw);
899 };
900
901 chr 0xfffc
902 }giex;
823 903
824 $self->{textview}->add_paragraph ([1, 1, 1, 1], "\n$msg"); 904 $self->{textview}->add_paragraph ([1, 1, 1, 1], [$msg, @link]);
905 $self->{textview}->scroll_to_bottom;
825 $self->update_options; 906 $self->update_options;
826 } else { 907 } else {
827 $self->destroy; 908 $self->destroy;
828 } 909 }
829 910
833sub send { 914sub send {
834 my ($self, $msg) = @_; 915 my ($self, $msg) = @_;
835 916
836 $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg"); 917 $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg");
837 $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg); 918 $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg);
919 $self->{textview}->scroll_to_bottom;
838} 920}
839 921
840sub destroy { 922sub destroy {
841 my ($self) = @_; 923 my ($self) = @_;
842 924
843 #Carp::cluck "debug\n";#d# #todo# enable: destroyx gets called twice because scalar keys {} is 1 925 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1
844 926
845 delete $self->{conn}{npc_dialog}; 927 delete $self->{conn}{npc_dialog};
846 $self->{conn}->disconnect_ext ($self->{token}); 928 $self->{conn}->disconnect_ext ($self->{token});
847 929
848 $self->SUPER::destroy; 930 $self->SUPER::destroy;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines