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.110 by root, Tue Jun 26 05:06:01 2007 UTC vs.
Revision 1.140 by root, Sun Jul 29 18:56:02 2007 UTC

21 21
22 my $self = $class->SUPER::new (%arg, 22 my $self = $class->SUPER::new (%arg,
23 setup_req => { 23 setup_req => {
24 extmap => 1, 24 extmap => 1,
25 excmd => 1, 25 excmd => 1,
26 xwidget => 1, 26 ywidget => 1,#d#
27 %{$arg{setup_req} || {}}, 27 %{$arg{setup_req} || {}},
28 }, 28 },
29 ); 29 );
30 30
31 $self->{map_widget}->clr_commands; 31 $self->{map_widget}->clr_commands;
48 sort { (length $a) <=> (length $b) } 48 sort { (length $a) <=> (length $b) }
49 @args 49 @args
50 } sort { $a->{par} <=> $b->{par} } 50 } sort { $a->{par} <=> $b->{par} }
51 CFPlus::Pod::find command => "*"; 51 CFPlus::Pod::find command => "*";
52 52
53 $self->connect_ext (music => sub {
54 my ($ev) = @_;
55
56 &::audio_music_set ($self->{songs} = $ev->{play})
57 if $ev->{play};
58 });
59
53 $self->connect_ext (event_capabilities => sub { 60 $self->connect_ext (capabilities => sub {
54 my ($cap) = @_; 61 my (%cap) = @_;
55 62
63 #$self->send ("setup sound 0"); # we use a different protocol
64
65 $self->send_exti_msg (fx_want => {
66 3 => !!$::CFG->{bgm_enable}, # FT_MUSIC
67 5 => !!$::CFG->{audio_enable}, # FT_SOUND
68 });
69
56 if (my $ts = $cap->{tileset}) { 70 if (my $ts = $cap{tileset}) {
57 if (my ($default) = grep $_->[2] & 1, @$ts) { 71 if (my ($default) = grep $_->[2] & 1, @$ts) {
58 $self->{tileset} = $default; 72 $self->{tileset} = $default;
59 $self->{tilesize} = $default->[3]; 73 $self->{tilesize} = $default->[3];
60 $self->setup_req (tileset => $default->[0]); 74 $self->setup_req (tileset => $default->[0]);
61 75
65 $self->setup_req (mapsize => "${w}x${h}"); 79 $self->setup_req (mapsize => "${w}x${h}");
66 } 80 }
67 } 81 }
68 }); 82 });
69 83
84 $self->{json_coder}
85 ->convert_blessed
86 ->filter_json_single_key_object (__w_ => sub {
87 $self->{widget}{$_[0]}
88 });
89
90 # widgetset new
70 $self->connect_ext (ws_n => sub { 91 $self->connect_ext (ws_n => sub {
71 my ($arg) = @_; 92 my ($id) = @_;
72 93
73 $self->{widgetset}{$arg{id}} = { 94 $self->{widgetset}{$id} = {
74 w => {}, 95 w => {},
75 }; 96 };
76 }); 97 });
77 98
99 # widgetset destroy
78 $self->connect_ext (ws_d => sub { 100 $self->connect_ext (ws_d => sub {
79 my ($arg) = @_; 101 my ($id) = @_;
80 102
81 my $ws = delete $self->{widgetset}{$arg{id}} 103 my $ws = delete $self->{widgetset}{$id}
82 or return; 104 or return;
83 105
84 $_->destroy 106 $_->destroy
85 for values %{$ws->{w}}; 107 for values %{$ws->{w}};
86 }); 108 });
87 109
110 # widgetset create
88 $self->connect_ext (ws_c => sub { 111 $self->connect_ext (ws_c => sub {
89 my ($arg) = @_; 112 my ($ws, $id, $class, $args) = @_;
90
91 my $args = $arg->{args} || {};
92 113
93 for my $ev (grep /^on_/, keys %$args) { 114 for my $ev (grep /^on_/, keys %$args) {
94 $args->{$ev} = sub { 115 $args->{$ev} = sub {
95 my $id = shift->{s_id}; 116 my $id = shift->{s_id};
96 $self->send_exti_msg (w_e => id => $id, name => $ev, args => \@_); 117 $self->send_exti_msg (w_e => $id, $ev, \@_);
97 118
98 1 119 1
99 }; 120 };
100 } 121 }
101 122
102 if (my $widget = eval { 123 if (my $widget = eval {
103 local $SIG{__DIE__}; 124 local $SIG{__DIE__};
104 "CFPlus::UI::$arg->{class}"->new ( 125 "CFPlus::UI::$class"->new (
105 %$args, 126 %$args,
106 s_ws => $arg->{ws}, 127 s_ws => $ws,
107 s_id => $arg->{id}, 128 s_id => $id,
108 ) 129 )
109 } 130 }
110 ) { 131 ) {
111 $self->{widget}{$arg->{id}} 132 $self->{widget}{$id}
112 = $self->{widgetset}{$arg->{ws}}{w}{$arg->{id}} 133 = $self->{widgetset}{$ws}{w}{$id}
113 = $widget; 134 = $widget;
114 135
115 $widget->connect (on_destroy => sub { 136 $widget->connect (on_destroy => sub {
116 my ($widget) = @_; 137 my ($widget) = @_;
117 138
118 delete $self->{widget}{$widget->{s_id}}; 139 delete $self->{widget}{$widget->{s_id}};
119 delete $self->{widgetset}{$widget->{s_ws}}{$widget->{s_id}}; 140 delete $self->{widgetset}{$widget->{s_ws}}{$widget->{s_id}};
120 }); 141 });
121 } else { 142 } else {
122 warn "server failed creating client-side widget " . (CFPlus::to_json $arg) . ": $@\n"; 143 warn "server failed creating client-side widget " . (CFPlus::to_json $class) . ": $@\n";
123 $self->send_exti_msg (w_e => id => $arg->{id}, name => "destroy"); 144 $self->send_exti_msg (w_e => $id, "destroy");
124 } 145 }
125 }); 146 });
126 147
148 # widget call
127 $self->connect_ext (w_c => sub { 149 $self->connect_ext (w_c => sub {
128 my ($arg) = @_; 150 my ($id, $rid, $method, $args) = @_;
129 151
130 my $w = $self->{widget}{$arg->{id}} 152 my $w = $self->{widget}{$id}
131 or return; 153 or return;
132 my $m = $arg->{name};
133 154
134 my @a = map { 155 $args ||= [];
135 "HASH" eq ref && 1 == (scalar keys %$_) && exists $_->{__widget_ref__}
136 ? $self->{widget}{$_->{__widget_ref__}}
137 : $_
138 } @{ $arg->{args} || [] };
139 156
140 if (exists $arg->{rid}) { 157 if ($rid) {
141 $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [$w->$m (@a)]); 158 $self->send_exti_msg (w_r => $rid, [$w->$method (@$args)]);
142 } else { 159 } else {
143 $w->$m (@a); 160 $w->$method (@$args);
144 } 161 }
145 }); 162 });
146 163
164 # widget set
147 $self->connect_ext (w_s => sub { 165 $self->connect_ext (w_s => sub {
148 my ($arg) = @_; 166 my ($id, $attr) = @_;
149 167
150 my $w = $self->{widget}{$arg->{id}} 168 my $w = $self->{widget}{$id}
151 or return; 169 or return;
152 170
153 $w->{$arg->{name}} = $arg->{value}; 171 for (my $i = 0; $i < $#$attr; $i += 2) {
172 my ($member, $value) = @$attr[$i, $i+1];
173 if (defined $value) {
174 $w->{$member} = $value;
175 } else {
176 delete $w->{$member};
177 }
178 $w->{parent}->realloc if $member =~ /^c_/ && $w->{visible};
179 }
154 }); 180 });
155 181
182 # widget get
156 $self->connect_ext (w_g => sub { 183 $self->connect_ext (w_g => sub {
157 my ($arg) = @_; 184 my ($id, $rid, $attr) = @_;
158 185
159 my $w = $self->{widget}{$arg->{id}} 186 my $w = $self->{widget}{$id}
160 or return; 187 or return;
161 188
162 $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [$w->{$arg->{name}}]); 189 $self->send_exti_msg (w_r => $rid, [map $w->{$_}, @$attr]);
163 }); 190 });
164 191
192 # message window
193 $self->connect_ext (channel_info => sub {
194 my ($info) = @_;
195 $self->{channels}->{$info->{id}} = $info;
196 $::MESSAGE_WINDOW->add_channel ($info);
197 });
198
199 # destroy widgets on logout
165 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub { 200 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub {
166 for my $ws (values %{delete $self->{widgetset} || {}}) { 201 for my $ws (values %{delete $self->{widgetset} || {}}) {
167 $_->destroy 202 $_->destroy
168 for values %{delete $ws->{w} || {}}; 203 for values %{delete $ws->{w} || {}};
169 } 204 }
382 my @skills = keys %{ $self->{skill_info} }; 417 my @skills = keys %{ $self->{skill_info} };
383 418
384 if (grep +(exists $stats->{$_}) != (exists $prev->{$_}), @skills) { 419 if (grep +(exists $stats->{$_}) != (exists $prev->{$_}), @skills) {
385 $sktbl->clear; 420 $sktbl->clear;
386 421
422 my @add;
423
424 push @add,
387 $sktbl->add (0, 0, new CFPlus::UI::Label text => "Experience", align => 1); 425 0, 0, (new CFPlus::UI::Label text => "Experience", align => 1),
388 $sktbl->add (1, 0, new CFPlus::UI::Label text => "Lvl.", align => 1); 426 1, 0, (new CFPlus::UI::Label text => "Lvl.", align => 1),
389 $sktbl->add (2, 0, new CFPlus::UI::Label text => "Skill", expand => 1); 427 2, 0, (new CFPlus::UI::Label text => "Skill", expand => 1),
390 $sktbl->add (3, 0, new CFPlus::UI::Label text => "Experience", align => 1); 428 3, 0, (new CFPlus::UI::Label text => "Experience", align => 1),
391 $sktbl->add (4, 0, new CFPlus::UI::Label text => "Lvl.", align => 1); 429 4, 0, (new CFPlus::UI::Label text => "Lvl.", align => 1),
392 $sktbl->add (5, 0, new CFPlus::UI::Label text => "Skill", expand => 1); 430 5, 0, (new CFPlus::UI::Label text => "Skill", expand => 1),
431 ;
393 432
394 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>"; 433 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>";
395 434
396 my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1); 435 my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
397 my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1); 436 my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
425 } 464 }
426 465
427 1 466 1
428 }; 467 };
429 468
469 push @add,
430 $sktbl->add ($x * 3 + 0, $y, $self->{stat_widget_exp}{$idx} = new CFPlus::UI::Label 470 $x * 3 + 0, $y, ($self->{stat_widget_exp}{$idx} = new CFPlus::UI::Label
431 text => "0", align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP); 471 align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP),
432 $sktbl->add ($x * 3 + 1, $y, $self->{stat_widget_lvl}{$idx} = new CFPlus::UI::Label 472 $x * 3 + 1, $y, ($self->{stat_widget_lvl}{$idx} = new CFPlus::UI::Label
433 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4, on_button_down => $spell_cb, @TOOLTIP_LVL); 473 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4, on_button_down => $spell_cb, @TOOLTIP_LVL),
434 $sktbl->add ($x * 3 + 2, $y, new CFPlus::UI::Label text => $name, on_button_down => $spell_cb, 474 $x * 3 + 2, $y, (new CFPlus::UI::Label text => $name, on_button_down => $spell_cb,
435 can_events => 1, can_hover => 1, tooltip => (CFPlus::Pod::section_label skill_description => $name) . $TOOLTIP_ALL); 475 can_events => 1, can_hover => 1, tooltip => (CFPlus::Pod::section_label skill_description => $name) . $TOOLTIP_ALL),
476 ;
436 477
437 $x++ and ($x, $y) = (0, $y + 1); 478 $x++ and ($x, $y) = (0, $y + 1);
438 } 479 }
480
481 $sktbl->add_at (@add);
439 } 482 }
440 483
441 for (grep exists $stats->{$_}, @skills) { 484 for (grep exists $stats->{$_}, @skills) {
442 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1])); 485 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1]));
443 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1); 486 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1);
680} 723}
681 724
682sub face_find { 725sub face_find {
683 my ($self, $facenum, $face, $cb) = @_; 726 my ($self, $facenum, $face, $cb) = @_;
684 727
685 my $hash = "$face->{chksum},$face->{name}"; 728 if ($face->{type} == 0) { # FT_FACE
686
687 my $id = CFPlus::DB::get_tile_id_sync $hash; 729 my $id = CFPlus::DB::get_tile_id_sync $face->{name};
688 730
689 $face->{id} = $id; 731 $face->{id} = $id;
690 $self->{faceid}[$facenum] = $id; 732 $self->{faceid}[$facenum] = $id;
691 733
692 $self->{map}->set_tileid ($facenum => $id); 734 $self->{map}->set_tileid ($facenum => $id);
693 735
694 CFPlus::DB::get tilecache => $id, $cb; 736 CFPlus::DB::get tilecache => $id, $cb;
737
738 } elsif ($face->{type} & 1) { # with metadata
739 #::message ({ markup => "starting to download song #$face/$pri, check your output-rate setting if your connection gets laggy." });
740 CFPlus::DB::get res_meta => $face->{name}, $cb;
741 }
695} 742}
696 743
697sub face_update { 744sub face_update {
698 my ($self, $facenum, $face, $changed) = @_; 745 my ($self, $facenum, $face, $changed) = @_;
699 746
747 if ($face->{type} == 0) { # FT_FACE
700 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { } 748 CFPlus::DB::put tilecache => $face->{id} => $face->{data}, sub { }
701 if $changed; 749 if $changed;
702 750
703 $self->set_texture ($face->{id} => delete $face->{image}); 751 $self->set_texture ($face->{id} => delete $face->{data});
752
753 } elsif ($face->{type} & 1) { # split metadata
754 my $cb = sub { };
755
756 if ($face->{type} == 3) { # FT_MUSIC
757 $cb = sub {
758 $self->{music_meta}{$facenum} = $face;
759
760 ::message ({ markup => "downloaded song #$facenum" })
761 if $changed;
762
763 &::audio_music_set ($self->{songs});
764 };
765 } elsif ($face->{type} == 5) { # FT_SOUND
766 $cb = sub {
767 $self->{sound_meta}{$facenum} = $face;
768
769 use Data::Dumper; warn Dumper $face;#d#
770 ::message ({ markup => "downloaded sound #$facenum" })
771 if $changed;
772 };
773 }
774
775
776 if ($changed) { # new data
777 my ($meta_json, $data) = unpack "(w/a*)*", delete $face->{data};
778
779 CFPlus::DB::put res_data => $face->{name} => $data, sub {
780 CFPlus::DB::put res_meta => $face->{name} => $meta_json, sub {
781 $face->{meta} = $self->{json_coder}->decode ($meta_json);
782
783 $cb->();
784 };
785 };
786
787 } else {
788 $face->{meta} = $self->{json_coder}->decode (delete $face->{data});
789 $cb->();
790 }
791 }
704} 792}
705 793
706sub smooth_update { 794sub smooth_update {
707 my ($self, $facenum, $face) = @_; 795 my ($self, $facenum, $face) = @_;
708 796
709 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel}); 797 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
710} 798}
711 799
712sub set_texture { 800sub set_texture {
713 my ($self, $id, $data) = @_; 801 my ($self, $id, $data) = @_;
802
803 return unless $self->{map};
714 804
715 $self->{texture}[$id] = my $tex = 805 $self->{texture}[$id] = my $tex =
716 new_from_image CFPlus::Texture 806 new_from_image CFPlus::Texture
717 $data, minify => 1, mipmap => 1; 807 $data, minify => 1, mipmap => 1;
718 808
719 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 809 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
720 $self->{map_widget}->update; 810 $self->{map_widget}->update;
811
812 $_->() for @{(delete $self->{face_cb}{$id}) || []};
813}
814
815sub connect_face_update {
816 my ($self, $id, $cb) = @_;
817
818 push @{$self->{face_cb}{$id}}, $cb;
819
820 CFPlus::guard {
821 @{$self->{face_cb}{$id}}
822 = grep $_ != $cb,
823 @{$self->{face_cb}{$id}};
824 }
721} 825}
722 826
723sub sound_play { 827sub sound_play {
724 my ($self, $x, $y, $soundnum, $type) = @_; 828 my ($self, $x, $y, $soundnum, $type) = @_;
829 warn "sound play <@_>\n";#d#
725 830
726 $self->{sound_play}->($x, $y, $soundnum, $type); 831 #$self->{sound_play}->($x, $y, $soundnum, $type);
727} 832}
728 833
729my $LAST_QUERY; # server is stupid, stupid, stupid 834my $LAST_QUERY; # server is stupid, stupid, stupid
730 835
731sub query { 836sub query {
796 $self->{$type} = $text; 901 $self->{$type} = $text;
797 } else { 902 } else {
798 $self->logprint ("msg: ", $text); 903 $self->logprint ("msg: ", $text);
799 return if $color < 0; # negative color == ignore if not understood 904 return if $color < 0; # negative color == ignore if not understood
800 905
801 my $fg = $CF_COLOR[$color % @CF_COLOR]; 906 my $fg = $CF_COLOR[$color & 0x1f] || [1, 0, 0];
802 907
803 ## try to create single paragraphs of multiple lines sent by the server 908 ## try to create single paragraphs of multiple lines sent by the server
804 # no longer neecssary with TRT servers 909 # no longer neecssary with TRT servers
805 #$text =~ s/(?<=\S)\n(?=\w)/ /g; 910 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
806 911
807 ::message ({ fg => $fg, markup => $_ }) 912 ::message ({ fg => $fg, markup => $_, type => $type, extra => [@extra] })
808 for split /\n/, $text; 913 for split /\n/, $text;
809 914
810 $self->{statusbox}->add ($text, 915 $self->{statusbox}->add ($text,
811 group => $text, 916 group => $text,
812 fg => $fg, 917 fg => $fg,
869 $self->{map_widget}->clr_commands; 974 $self->{map_widget}->clr_commands;
870 975
871 ::stop_game (); 976 ::stop_game ();
872} 977}
873 978
874sub image_info {
875 my ($self, $numfaces) = @_;
876
877 $self->{num_faces} = $numfaces;
878 $self->{face_prefetch} = [1 .. $numfaces];
879 $self->face_prefetch;
880}
881
882sub face_prefetch {
883 my ($self) = @_;
884
885 return unless $::CFG->{face_prefetch};
886
887 if ($self->{num_faces}) {
888 return if @{ $self->{send_queue} || [] };
889 my $todo = @{ $self->{face_prefetch} }
890 or return;
891
892 my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
893
894 $self->send ("requestinfo image_sums $face $face");
895
896 $self->{statusbox}->add (CFPlus::asxml "prefetching $todo",
897 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
898 } elsif (!exists $self->{num_faces}) {
899 $self->send ("requestinfo image_info");
900
901 $self->{num_faces} = 0;
902
903 $self->{statusbox}->add (CFPlus::asxml "starting to prefetch",
904 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
905 }
906}
907
908sub update_floorbox { 979sub update_floorbox {
909 $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub { 980 $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub {
910 return unless $::CONN; 981 return unless $::CONN;
911 982
912 $::FLOORBOX->clear; 983 $::FLOORBOX->clear;
984
985 my @add;
913 986
914 my $row; 987 my $row;
915 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) { 988 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) {
916 if ($row < 6) { 989 if ($row < 6) {
917 local $_->{face_widget}; # hack to force recreation of widget 990 local $_->{face_widget}; # hack to force recreation of widget
918 local $_->{desc_widget}; # hack to force recreation of widget 991 local $_->{desc_widget}; # hack to force recreation of widget
919 CFPlus::Item::update_widgets $_; 992 CFPlus::Item::update_widgets $_;
920 993
994 push @add,
921 $::FLOORBOX->add (0, $row, $_->{face_widget}); 995 0, $row, $_->{face_widget},
922 $::FLOORBOX->add (1, $row, $_->{desc_widget}); 996 1, $row, $_->{desc_widget};
923 997
924 $row++; 998 $row++;
925 } else { 999 } else {
926 $::FLOORBOX->add (1, $row, new CFPlus::UI::Button 1000 push @add, 1, $row, new CFPlus::UI::Button
927 text => "More...", 1001 text => "More...",
928 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 }, 1002 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 },
929 ); 1003 ;
930 last; 1004 last;
931 } 1005 }
932 } 1006 }
933 if ($::CONN->{open_container}) { 1007 if ($::CONN->{open_container}) {
934 $::FLOORBOX->add (1, $row++, new CFPlus::UI::Button 1008 push @add, 1, $row++, new CFPlus::UI::Button
935 text => "Close container", 1009 text => "Close container",
936 on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") } 1010 on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") }
937 ); 1011 ;
938 } 1012 }
1013
1014 $::FLOORBOX->add_at (@add);
939 }); 1015 });
940 1016
941 $::WANT_REFRESH++; 1017 $::WANT_REFRESH++;
942} 1018}
943 1019
1063 . "cfplus support $yesno[$self->{cfplus_ext} > 0]" 1139 . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
1064 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 1140 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
1065 . "map size $self->{mapw}×$self->{maph}\n" 1141 . "map size $self->{mapw}×$self->{maph}\n"
1066 ); 1142 );
1067 1143
1068 ::setup_build_button ($self->{editor_support}->{builder_ui});
1069} 1144}
1070 1145
1071sub logged_in { 1146sub logged_in {
1072 my ($self) = @_; 1147 my ($self) = @_;
1073 1148
1074 $self->send_ext_req (cfplus_support => version => 1, sub { 1149 $self->send_ext_req (cfplus_support => version => 2, sub {
1150 my (%msg) = @_;
1151
1075 $self->{cfplus_ext} = $_[0]{version}; 1152 $self->{cfplus_ext} = $msg{version};
1076 $self->update_server_info; 1153 $self->update_server_info;
1077 1154
1078 if ($self->{cfplus_ext} >= 2) { 1155 if ($self->{cfplus_ext} >= 2) {
1079 $self->send_ext_req ("editor_support", sub { 1156 $self->send_ext_req ("editor_support", sub {
1080 $self->{editor_support} = $_[0]; 1157 $self->{editor_support} = { @_ };
1081 $self->update_server_info; 1158 $self->update_server_info;
1082 1159
1083 0 1160 0
1084 }); 1161 });
1085 } 1162 }
1093 $self->send_command ("output-count $::CFG->{output_count}"); 1170 $self->send_command ("output-count $::CFG->{output_count}");
1094 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0; 1171 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
1095 $self->send_command ("pickup $::CFG->{pickup}"); 1172 $self->send_command ("pickup $::CFG->{pickup}");
1096} 1173}
1097 1174
1098sub buildat {
1099 my ($self, $builditem, $x, $y) = @_;
1100
1101 if ($self->{cfplus_ext}) {
1102 $self->send_ext_msg (builder_build => dx => $x, dy => $y, (ref ($builditem) eq 'HASH') ? %$builditem : (item => $builditem));
1103 }
1104}
1105
1106sub lookat { 1175sub lookat {
1107 my ($self, $x, $y) = @_; 1176 my ($self, $x, $y) = @_;
1108 1177
1109 if ($self->{cfplus_ext}) { 1178 if ($self->{cfplus_ext}) {
1110 $self->send_ext_req (lookat => dx => $x, dy => $y, sub { 1179 $self->send_ext_req (lookat => $x, $y, sub {
1111 my ($msg) = @_; 1180 my (%msg) = @_;
1112 1181
1113 if (exists $msg->{npc_dialog}) { 1182 if (exists $msg{npc_dialog}) {
1114 # start npc chat dialog 1183 # start npc chat dialog
1115 $self->{npc_dialog} = new CFPlus::NPCDialog:: 1184 $self->{npc_dialog} = new CFPlus::NPCDialog::
1116 dx => $x, 1185 token => $msg{npc_dialog},
1117 dy => $y,
1118 title => "$msg->{npc_dialog} (NPC)", 1186 title => "$msg{npc_dialog}[0] (NPC)",
1119 conn => $self, 1187 conn => $self,
1120 ; 1188 ;
1121 } 1189 }
1122 }); 1190 });
1123 } 1191 }
1130 1198
1131 (delete $self->{npc_dialog})->destroy 1199 (delete $self->{npc_dialog})->destroy
1132 if $self->{npc_dialog}; 1200 if $self->{npc_dialog};
1133 1201
1134 $self->SUPER::destroy; 1202 $self->SUPER::destroy;
1203
1204 %$self = ();
1135} 1205}
1136 1206
1137package CFPlus::NPCDialog; 1207package CFPlus::NPCDialog;
1138 1208
1139our @ISA = 'CFPlus::UI::Toplevel'; 1209our @ISA = 'CFPlus::UI::Toplevel';
1186 on_activate => sub { $this->destroy; 1 }, 1256 on_activate => sub { $this->destroy; 1 },
1187 ; 1257 ;
1188 1258
1189 $self->update_options; 1259 $self->update_options;
1190 1260
1191 $self->{id} = $self->{conn}->send_ext_req ( 1261 $self->{id} = "npc-channel-" . $self->{conn}->token;
1192 npc_dialog_begin => dx => $self->{dx}, dy => $self->{dy}, 1262 $self->{conn}->connect_ext ($self->{id} => sub {
1193 sub { $this && $this->feed (@_) } 1263 $this->feed (@_) if $this;
1194 ); 1264 });
1265
1266 $self->{conn}->send_ext_msg (npc_dialog_begin => $self->{id}, $self->{token});
1195 1267
1196 $self->{entry}->grab_focus; 1268 $self->{entry}->grab_focus;
1197 1269
1198 $self->{textview}->add_paragraph ({ 1270 $self->{textview}->add_paragraph ({
1199 fg => [1, 1, 0, 1], 1271 fg => [1, 1, 0, 1],
1222 ); 1294 );
1223 } 1295 }
1224} 1296}
1225 1297
1226sub feed { 1298sub feed {
1227 my ($self, $msg) = @_; 1299 my ($self, $type, @arg) = @_;
1228 1300
1229 CFPlus::weaken $self; 1301 CFPlus::weaken $self;
1230 1302
1231 if ($msg->{msgtype} eq "reply") { 1303 if ($type eq "update") {
1304 my (%info) = @arg;
1305
1232 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []}; 1306 $self->{kw}{$_} = 1 for @{$info{add_topics} || []};
1233 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []}; 1307 $self->{kw}{$_} = 0 for @{$info{del_topics} || []};
1234
1235 my $text = "\n" . CFPlus::Protocol::sanitise_xml $msg->{msg};
1236 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
1237 my @link;
1238 $text =~ s{
1239 ($match)
1240 }{
1241 my $kw = $1;
1242
1243 push @link, new CFPlus::UI::Label
1244 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
1245 can_hover => 1,
1246 can_events => 1,
1247 padding_x => 0,
1248 padding_y => 0,
1249 on_button_up => sub {
1250 $self->send ($kw);
1251 };
1252
1253 "\x{fffc}"
1254 }giex;
1255 1308
1309 if (exists $info{msg}) {
1310 my $text = "\n" . CFPlus::Protocol::sanitise_xml $info{msg};
1311 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
1312 my @link;
1313 $text =~ s{
1314 ($match)
1315 }{
1316 my $kw = $1;
1317
1318 push @link, new CFPlus::UI::Label
1319 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
1320 can_hover => 1,
1321 can_events => 1,
1322 padding_x => 0,
1323 padding_y => 0,
1324 on_button_up => sub {
1325 $self->send ($kw);
1326 };
1327
1328 "\x{fffc}"
1329 }giex;
1330
1256 $self->{textview}->add_paragraph ({ markup => $text, widget => \@link }); 1331 $self->{textview}->add_paragraph ({ markup => $text, widget => \@link });
1257 $self->{textview}->scroll_to_bottom; 1332 $self->{textview}->scroll_to_bottom;
1333 }
1334
1258 $self->update_options; 1335 $self->update_options;
1259 } else { 1336 } else {
1260 $self->destroy; 1337 $self->destroy;
1261 } 1338 }
1262 1339
1267 my ($self, $msg) = @_; 1344 my ($self, $msg) = @_;
1268 1345
1269 $self->{textview}->add_paragraph ({ markup => "\n" . CFPlus::asxml $msg }); 1346 $self->{textview}->add_paragraph ({ markup => "\n" . CFPlus::asxml $msg });
1270 $self->{textview}->scroll_to_bottom; 1347 $self->{textview}->scroll_to_bottom;
1271 1348
1272 $self->{conn}->send_ext_msg (npc_dialog_tell => msgid => $self->{id}, msg => $msg); 1349 $self->{conn}->send_ext_msg (npc_dialog_tell => $self->{id}, $msg);
1273} 1350}
1274 1351
1275sub destroy { 1352sub destroy {
1276 my ($self) = @_; 1353 my ($self) = @_;
1277 1354
1278 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1 1355 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1
1279 1356
1280 if ($self->{conn}) { 1357 if ($self->{conn}) {
1281 $self->{conn}->send_ext_msg (npc_dialog_end => msgid => $self->{id}) if $self->{id}; 1358 $self->{conn}->send_ext_msg (npc_dialog_end => $self->{id}) if $self->{id};
1282 delete $self->{conn}{npc_dialog}; 1359 delete $self->{conn}{npc_dialog};
1283 $self->{conn}->disconnect_ext ($self->{id}); 1360 $self->{conn}->disconnect_ext ($self->{id});
1284 } 1361 }
1285 1362
1286 $self->SUPER::destroy; 1363 $self->SUPER::destroy;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines