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.35 by root, Thu Jun 15 13:40:02 2006 UTC vs.
Revision 1.57 by root, Tue Jul 11 15:30:23 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 ($self, $name, $old, $new) = @_;
88 90
91 my $diff = $new - $old;
92
93 $diff = 0.01 * int $diff * 100;
94
95 0.1 >= abs $diff ? ()
96 : $diff < 0 ? "$name$diff" : "$name+$diff"
97}
98
99sub _stat_skillmaskdiff {
100 my ($self, $name, $old, $new) = @_;
101
102 my $diff = $old ^ $new
103 or return;
104
105 my @diff = map
106 {
107 $diff & $_
108 ? (($new & $_ ? "+" : "-") . $self->{spell_paths}{$_})
109 : ()
110 }
111 sort { $a <=> $b } keys %{$self->{spell_paths}};
112
113 join "", @diff
114}
115
116# all stats that are chacked against changes
117my @statchange = (
118 [&CS_STAT_STR => \&_stat_numdiff, "Str"],
119 [&CS_STAT_INT => \&_stat_numdiff, "Int"],
120 [&CS_STAT_WIS => \&_stat_numdiff, "Wis"],
121 [&CS_STAT_DEX => \&_stat_numdiff, "Dex"],
122 [&CS_STAT_CON => \&_stat_numdiff, "Con"],
123 [&CS_STAT_CHA => \&_stat_numdiff, "Cha"],
124 [&CS_STAT_POW => \&_stat_numdiff, "Pow"],
125 [&CS_STAT_WC => \&_stat_numdiff, "Wc"],
126 [&CS_STAT_AC => \&_stat_numdiff, "Ac"],
127 [&CS_STAT_DAM => \&_stat_numdiff, "Dam"],
128 [&CS_STAT_SPEED => \&_stat_numdiff, "Speed"],
129 [&CS_STAT_WEAP_SP => \&_stat_numdiff, "WSp"],
130 [&CS_STAT_MAXHP => \&_stat_numdiff, "HP"],
131 [&CS_STAT_MAXSP => \&_stat_numdiff, "Mana"],
132 [&CS_STAT_MAXGRACE => \&_stat_numdiff, "Grace"],
133 [&CS_STAT_WEIGHT_LIM => \&_stat_numdiff, "Weight"],
134 [&CS_STAT_SPELL_ATTUNE => \&_stat_skillmaskdiff, "attuned"],
135 [&CS_STAT_SPELL_REPEL => \&_stat_skillmaskdiff, "repelled"],
136 [&CS_STAT_SPELL_DENY => \&_stat_skillmaskdiff, "denied"],
137 [&CS_STAT_RES_PHYS => \&_stat_numdiff, "phys"],
138 [&CS_STAT_RES_MAG => \&_stat_numdiff, "magic"],
139 [&CS_STAT_RES_FIRE => \&_stat_numdiff, "fire"],
140 [&CS_STAT_RES_ELEC => \&_stat_numdiff, "electricity"],
141 [&CS_STAT_RES_COLD => \&_stat_numdiff, "cold"],
142 [&CS_STAT_RES_CONF => \&_stat_numdiff, "confusion"],
143 [&CS_STAT_RES_ACID => \&_stat_numdiff, "acid"],
144 [&CS_STAT_RES_DRAIN => \&_stat_numdiff, "drain"],
145 [&CS_STAT_RES_GHOSTHIT => \&_stat_numdiff, "ghosthit"],
146 [&CS_STAT_RES_POISON => \&_stat_numdiff, "poison"],
147 [&CS_STAT_RES_SLOW => \&_stat_numdiff, "slow"],
148 [&CS_STAT_RES_PARA => \&_stat_numdiff, "paralyse"],
149 [&CS_STAT_TURN_UNDEAD => \&_stat_numdiff, "turnundead"],
150 [&CS_STAT_RES_FEAR => \&_stat_numdiff, "fear"],
151 [&CS_STAT_RES_DEPLETE => \&_stat_numdiff, "depletion"],
152 [&CS_STAT_RES_DEATH => \&_stat_numdiff, "death"],
153 [&CS_STAT_RES_HOLYWORD => \&_stat_numdiff, "godpower"],
154 [&CS_STAT_RES_BLIND => \&_stat_numdiff, "blind"],
155);
89 156
90sub stats_update { 157sub stats_update {
91 my ($self, $stats) = @_; 158 my ($self, $stats) = @_;
92 159
93 if (my $exp = $stats->{+CS_STAT_EXP64}) { 160 if (my $prev = $self->{prev_stats}) {
94 my $diff = $exp - $self->{prev_exp}; 161 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) 162 $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; 163 }
97 $self->{prev_exp} = $exp; 164
165 if (
166 my @diffs = map $_->[1]->($self, $_->[2], $prev->{$_->[0]}, $stats->{$_->[0]}), @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
132sub feed_map1a { 207sub feed_map1a {
133 my ($self, $data) = @_; 208 my ($self, $data) = @_;
134 209
135 $self->{map}->map1a_update ($data); 210 $self->{map}->map1a_update ($data);
136 $self->{map_widget}->update; 211 $self->{map_widget}->update;
212}
213
214sub magicmap {
215 my ($self, $w, $h, $x, $y, $data) = @_;
216
217 $self->{map_widget}->set_magicmap ($w, $h, $x, $y, $data);
137} 218}
138 219
139sub flush_map { 220sub flush_map {
140 my ($self) = @_; 221 my ($self) = @_;
141 222
154 235
155 $self->flush_map; 236 $self->flush_map;
156 delete $self->{neigh_map}; 237 delete $self->{neigh_map};
157 238
158 $self->{map}->clear; 239 $self->{map}->clear;
240 delete $self->{map_widget}{magicmap};
159} 241}
160 242
161 243
162sub load_map($$$) { 244sub load_map($$$) {
163 my ($self, $hash, $x, $y) = @_; 245 my ($self, $hash, $x, $y) = @_;
340} 422}
341 423
342sub face_update { 424sub face_update {
343 my ($self, $facenum, $face) = @_; 425 my ($self, $facenum, $face) = @_;
344 426
345 $self->{tilecache}->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes 427 $self->{tilecache}->put ($face->{id} => $face->{image})
428 if $self->{tilecache}->get ($face->{id}) ne $face->{image};
346 429
347 $self->set_texture ($face->{id} => delete $face->{image}); 430 $self->set_texture ($face->{id} => delete $face->{image});
348} 431}
349 432
350sub set_texture { 433sub set_texture {
407 490
408 $text = CFClient::UI::Label::escape $text; 491 $text = CFClient::UI::Label::escape $text;
409 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 492 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
410 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; 493 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
411 494
412 $self->{logview}->add_paragraph ($color[$color], 495 $self->{logview}->add_paragraph ($color[$color], $_)
413 join "\n", map "$time $_", split /\n/, $text); 496 for map "$time $_", split /\n/, $text;
497 $self->{logview}->scroll_to_bottom;
414 498
415 $self->{statusbox}->add ($text, 499 $self->{statusbox}->add ($text,
416 group => $text, 500 group => $text,
417 fg => $color[$color], 501 fg => $color[$color],
418 timeout => $color >= 2 ? 60 : 10, 502 timeout => $color >= 2 ? 60 : 10,
432 # try to create single paragraphs of multiple lines sent by the server 516 # try to create single paragraphs of multiple lines sent by the server
433 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g; 517 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
434 $spell->{message} =~ s/\n+$//; 518 $spell->{message} =~ s/\n+$//;
435 $spell->{message} ||= "Server did not provide a description for this spell."; 519 $spell->{message} ||= "Server did not provide a description for this spell.";
436 520
437 $::SETUP_SPELLS->add_spell ($spell); 521 $::SPELL_PAGE->add_spell ($spell);
438 522
439 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 523 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message});
440 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 524 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message});
441} 525}
442 526
443sub spell_delete { 527sub spell_delete {
444 my ($self, $spell) = @_; 528 my ($self, $spell) = @_;
529
445 $::SETUP_SPELLS->remove_spell ($spell); 530 $::SPELL_PAGE->remove_spell ($spell);
446} 531}
447 532
448sub addme_success { 533sub addme_success {
449 my ($self) = @_; 534 my ($self) = @_;
450 535
517 return unless $::CONN; 602 return unless $::CONN;
518 603
519 $::FLOORBOX->clear; 604 $::FLOORBOX->clear;
520 605
521 my $row; 606 my $row;
522 for (@{ $::CONN->{container}{0} }) { 607 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{0} }) {
523 if ($row < 6) { 608 if ($row < 6) {
524 local $_->{face_widget}; # hack to force recreation of widget 609 local $_->{face_widget}; # hack to force recreation of widget
525 local $_->{desc_widget}; # hack to force recreation of widget 610 local $_->{desc_widget}; # hack to force recreation of widget
526 CFClient::Item::update_widgets $_; 611 CFClient::Item::update_widgets $_;
527 612
530 615
531 $row++; 616 $row++;
532 } else { 617 } else {
533 $::FLOORBOX->add (1, $row, new CFClient::UI::Button 618 $::FLOORBOX->add (1, $row, new CFClient::UI::Button
534 text => "More...", 619 text => "More...",
535 on_activate => sub { $::INV_WINDOW->toggle_visibility }, 620 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 },
536 ); 621 );
537 last; 622 last;
538 } 623 }
539 } 624 }
540 }); 625 });
555 tooltip => "Close the currently open container (if one is open)", 640 tooltip => "Close the currently open container (if one is open)",
556 on_activate => sub { 641 on_activate => sub {
557 $::CONN->send ("apply $tag") # $::CONN->{open_container}") 642 $::CONN->send ("apply $tag") # $::CONN->{open_container}")
558 if $tag != 0; 643 if $tag != 0;
559 #if $CONN->{open_container} != 0; 644 #if $CONN->{open_container} != 0;
645 0
560 }, 646 },
561 ); 647 );
562 } 648 }
563 649
564 $::INVR->set_items ($conn->{container}{$tag}); 650 $::INVR->set_items ($conn->{container}{$tag});
565} 651}
566 652
567sub update_container { 653sub update_containers {
568 my ($tag) = @_; 654 my ($self) = @_;
569 655
570 $::INVR->set_items ($::CONN->{container}{$::CONN->{open_container}}) 656 $CFClient::UI::ROOT->on_refresh ("update_containers_$self" => sub {
657 my $todo = delete $self->{update_container}
658 or return;
659
660 for my $tag (keys %$todo) {
661 if ($tag == 0) {
662 update_floorbox;
663 $::INVR->set_items ($self->{container}{0})
571 if $tag == $::CONN->{open_container}; 664 if $tag == $self->{open_container};
665 } elsif ($tag == $self->{player}{tag}) {
666 $::INV->set_items ($self->{container}{$tag})
667 } else {
668 $::INVR->set_items ($self->{container}{$tag})
669 if $tag == $self->{open_container};
670 }
671 }
672 });
572} 673}
573 674
574sub container_add { 675sub container_add {
575 my ($self, $tag, $items) = @_; 676 my ($self, $tag, $items) = @_;
576 677
577 #d# print "container_add: container $tag ($self->{player}{tag})\n"; 678 $self->{update_container}{$tag}++;
578 679 $self->update_containers;
579 if ($tag == 0) {
580 update_floorbox;
581 update_container (0);
582 } elsif ($tag == $self->{player}{tag}) {
583 $::INV->set_items ($self->{container}{$self->{player}{tag}})
584 } else {
585 update_container ($tag);
586 }
587
588 # $self-<{player}{tag} => player inv
589 #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}};
590} 680}
591 681
592sub container_clear { 682sub container_clear {
593 my ($self, $tag) = @_; 683 my ($self, $tag) = @_;
594 684
595 #d# print "container_clear: container $tag ($self->{player}{tag})\n"; 685 $self->{update_container}{$tag}++;
596 686 $self->update_containers;
597 if ($tag == 0) {
598 update_floorbox;
599 update_container (0);
600 } elsif ($tag == $self->{player}{tag}) {
601 $::INV->set_items ($self->{container}{$tag})
602 } else {
603 update_container ($tag);
604 }
605
606# use PApp::Util; warn PApp::Util::dumpval $self->{container}{0};
607} 687}
608 688
609sub item_delete { 689sub item_delete {
610 my ($self, @items) = @_; 690 my ($self, @items) = @_;
611 691
692 $self->{update_container}{$_->{container}}++
612 for (@items) { 693 for @items;
613 #d# print "item_delete: $_->{tag} from $_->{container} ($self->{player}{tag})\n";
614
615 if ($_->{container} == 0) {
616 update_floorbox;
617 update_container ($_->{tag});
618 } elsif ($_->{container} == $self->{player}{tag}) {
619 $::INV->set_items ($self->{container}{$self->{player}{tag}})
620 } else {
621 update_container ($_->{container});
622 }
623 } 694
695 $self->update_containers;
624} 696}
625 697
626sub item_update { 698sub item_update {
627 my ($self, $item) = @_; 699 my ($self, $item) = @_;
628 700
629 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n"; 701 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
630 702
631 if ($item->{tag} == $self->{player}{tag}) {
632 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $item->{weight} / 1000);
633 return;
634 }
635
636 CFClient::Item::update_widgets $item; 703 CFClient::Item::update_widgets $item;
637 704
638 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { 705 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
639 set_opencont ($::CONN, 0, "Floor"); 706 set_opencont ($::CONN, 0, "Floor");
640 707
641 } elsif ($item->{flags} & F_OPEN) { 708 } elsif ($item->{flags} & F_OPEN) {
642 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); 709 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item);
710
643 } else { 711 } else {
644 if ($item->{container} == 0) { 712 $self->{update_container}{$item->{container}}++;
645 update_floorbox;
646 update_container (0); 713 $self->update_containers;
647 } elsif ($item->{container} == $self->{player}{tag}) {
648 $::INV->set_items ($self->{container}{$item->{container}})
649 }
650 } 714 }
651} 715}
652 716
653sub player_update { 717sub player_update {
654 my ($self, $player) = @_; 718 my ($self, $player) = @_;
655
656 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); 719 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000);
657} 720}
658 721
659sub update_server_info { 722sub update_server_info {
660 my ($self) = @_; 723 my ($self) = @_;
756 819
757 return unless $text =~ /\S/; 820 return unless $text =~ /\S/;
758 821
759 $entry->set_text (""); 822 $entry->set_text ("");
760 $this->send ($text); 823 $this->send ($text);
824
825 0
761 }, 826 },
762 ); 827 );
763 828
764 $vbox->add ($self->{options} = new CFClient::UI::VBox); 829 $vbox->add ($self->{options} = new CFClient::UI::VBox);
765 830
766 $self->{close_button} = new CFClient::UI::Button 831 $self->{bye_button} = new CFClient::UI::Button
767 text => "Bye (close)", 832 text => "Bye (close)",
768 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.", 833 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.",
769 on_activate => sub { $this->destroy }, 834 on_activate => sub { $this->destroy; 0 },
770 ; 835 ;
771 836
772 $self->update_options; 837 $self->update_options;
773 838
774 $self->{token} = $self->{conn}->ext_token; 839 $self->{token} = $self->{conn}->ext_token;
775 $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) }); 840 $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) });
776 $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}"); 841 $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}");
777 842
778 $self->{entry}->focus_in; 843 $self->{entry}->grab_focus;
779 844
780 $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n"); 845 $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n");
781 846
782 $self->show; 847 $self->show;
783 $self 848 $self
787 my ($self) = @_; 852 my ($self) = @_;
788 853
789 Scalar::Util::weaken $self; 854 Scalar::Util::weaken $self;
790 855
791 $self->{options}->clear; 856 $self->{options}->clear;
792 $self->{options}->add ($self->{close_button}); 857 $self->{options}->add ($self->{bye_button});
793 858
794 for my $kw (sort keys %{ $self->{kw} }) { 859 for my $kw (sort keys %{ $self->{kw} }) {
795 $self->{options}->add (new CFClient::UI::Button 860 $self->{options}->add (new CFClient::UI::Button
796 text => $kw, 861 text => $kw,
797 on_activate => sub { 862 on_activate => sub {
798 $self->send ($kw); 863 $self->send ($kw);
864 0
799 }, 865 },
800 ); 866 );
801 } 867 }
802} 868}
803 869
804sub feed { 870sub feed {
805 my ($self, $data) = @_; 871 my ($self, $data) = @_;
872
873 Scalar::Util::weaken $self;
806 874
807 my ($type, $msg) = split / /, $data, 2; 875 my ($type, $msg) = split / /, $data, 2;
808 876
809 if ($type eq "msg") { 877 if ($type eq "msg") {
810 my ($msg, @kw) = split /\x00/, $msg; 878 my ($msg, @kw) = split /\x00/, $msg;
811 $self->{kw}{$_} = 1 for @kw; 879 $self->{kw}{$_} = 1 for @kw;
812 880
813 $msg = CFClient::UI::Label::escape $msg; 881 $msg = "\n" . CFClient::UI::Label::escape $msg;
814 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; 882 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
815 $msg =~ s/($match)/<span foreground='#c0c0ff' underline='none'>$1<\/span>/gi; # underline when http-ready, huh. 883 my @link;
884 $msg =~ s{
885 ($match)
886 }{
887 my $kw = $1;
888
889 push @link, new CFClient::UI::Label
890 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
891 can_hover => 1,
892 can_events => 1,
893 padding_x => 0,
894 padding_y => 0,
895 on_button_up => sub {
896 $self->send ($kw);
897 };
898
899 chr 0xfffc
900 }giex;
816 901
817 $self->{textview}->add_paragraph ([1, 1, 1, 1], "\n$msg"); 902 $self->{textview}->add_paragraph ([1, 1, 1, 1], [$msg, @link]);
903 $self->{textview}->scroll_to_bottom;
818 $self->update_options; 904 $self->update_options;
819 } else { 905 } else {
820 $self->destroy; 906 $self->destroy;
821 } 907 }
822 908
826sub send { 912sub send {
827 my ($self, $msg) = @_; 913 my ($self, $msg) = @_;
828 914
829 $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg"); 915 $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg");
830 $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg); 916 $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg);
917 $self->{textview}->scroll_to_bottom;
831} 918}
832 919
833sub destroy { 920sub destroy {
834 my ($self) = @_; 921 my ($self) = @_;
835 922
836 #Carp::cluck "debug\n";#d# #todo# enable: destroyx gets called twice because scalar keys {} is 1 923 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1
837 924
925 $self->{conn}->send ("ext npc_dialog_end $self->{token}") if $self->{token};
838 delete $self->{conn}{npc_dialog}; 926 delete $self->{conn}{npc_dialog};
839 $self->{conn}->disconnect_ext ($self->{token}); 927 $self->{conn}->disconnect_ext ($self->{token});
840 928
841 $self->SUPER::destroy; 929 $self->SUPER::destroy;
842} 930}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines