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.147 by root, Thu Aug 9 03:40:01 2007 UTC vs.
Revision 1.156 by root, Tue Aug 21 00:59:34 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 ywidget => 1,#d# 26 xwidget1 => 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;
59 my (%cap) = @_; 59 my (%cap) = @_;
60 60
61 #$self->send ("setup sound 0"); # we use a different protocol 61 #$self->send ("setup sound 0"); # we use a different protocol
62 $self->update_fx_want; 62 $self->update_fx_want;
63 63
64 $self->send_exti_req (resource => "exp_table", sub {
65 my ($exp_table) = @_;
66
67 $self->register_face_handler ($exp_table, sub {
68 my ($face) = @_;
69
70 $self->{exp_table} = $self->{json_coder}->decode (delete $face->{data});
71
72 #TODO: update all interested parties
73 });
74
75 ()
76 });
77
64 if (my $ts = $cap{tileset}) { 78 if (my $ts = $cap{tileset}) {
65 if (my ($default) = grep $_->[2] & 1, @$ts) { 79 if (my ($default) = grep $_->[2] & 1, @$ts) {
66 $self->{tileset} = $default; 80 $self->{tileset} = $default;
67 $self->{tilesize} = $default->[3]; 81 $self->{tilesize} = $default->[3];
68 $self->setup_req (tileset => $default->[0]); 82 $self->setup_req (tileset => $default->[0]);
104 # widgetset create 118 # widgetset create
105 $self->connect_ext (ws_c => sub { 119 $self->connect_ext (ws_c => sub {
106 my ($ws, $id, $class, $args) = @_; 120 my ($ws, $id, $class, $args) = @_;
107 121
108 for my $ev (grep /^on_/, keys %$args) { 122 for my $ev (grep /^on_/, keys %$args) {
123 my $rid = $args->{$ev};
109 $args->{$ev} = sub { 124 $args->{$ev} = sub {
110 my $id = shift->{s_id}; 125 my $id = shift->{s_id};
111 $self->send_exti_msg (w_e => $id, $ev, \@_); 126 $self->send_exti_msg (w_e => $id, $rid, @_);
112 127
113 1 128 1
114 }; 129 };
115 } 130 }
116 131
117 if (my $widget = eval { 132 $self->widget_associate (
133 $ws, $id => scalar eval {
118 local $SIG{__DIE__}; 134 local $SIG{__DIE__};
119 "CFPlus::UI::$class"->new ( 135 "CFPlus::UI::$class"->new (%$args)
120 %$args,
121 s_ws => $ws,
122 s_id => $id,
123 )
124 } 136 }
125 ) {
126 $self->{widget}{$id}
127 = $self->{widgetset}{$ws}{w}{$id}
128 = $widget;
129
130 $widget->connect (on_destroy => sub {
131 my ($widget) = @_;
132
133 delete $self->{widget}{$widget->{s_id}};
134 delete $self->{widgetset}{$widget->{s_ws}}{$widget->{s_id}};
135 });
136 } else {
137 warn "server failed creating client-side widget " . (CFPlus::to_json $class) . ": $@\n"; 137 ) or warn "server failed creating client-side widget " . (CFPlus::to_json $class) . ": $@\n";
138 $self->send_exti_msg (w_e => $id, "destroy"); 138 });
139
140 # widgetset associate
141 $self->connect_ext (ws_a => sub {
142 my (%ass) = @_;
143
144 # everything that has a name, wether conceivably useful or not
145 my %wkw = (
146 root => $CFPlus::UI::ROOT,
147 tooltip => $CFPlus::UI::TOOLTIP,
148
149 mapwidget => $::MAPWIDGET,
150 buttonbar => $::BUTTONBAR,
151 metaserver => $::METASERVER,
152 buttonbar => $::BUTTONBAR,
153 login_button => $::LOGIN_BUTTON,
154 quit_dialog => $::QUIT_DIALOG,
155 host_entry => $::HOST_ENTRY,
156 metaserver => $::METASERVER,
157 server_info => $::SERVER_INFO,
158
159 setup_dialog => $::SETUP_DIALOG,
160 setup_notebook => $::SETUP_NOTEBOOK,
161 setup_server => $::SETUP_SERVER,
162 setup_keyboard => $::SETUP_KEYBOARD,
163
164 pl_notebook => $::PL_NOTEBOOK,
165 pl_window => $::PL_WINDOW,
166 inventory_page => $::INVENTORY_PAGE,
167 stats_page => $::STATS_PAGE,
168 skill_page => $::SKILL_PAGE,
169 spell_page => $::SPELL_PAGE,
170 spell_list => $::SPELL_LIST,
171
172 floorbox => $::FLOORBOX,
173 help_window => $::HELP_WINDOW,
174 message_window => $::MESSAGE_WINDOW,
175 statusbox => $::SDTATUSBOX,
176
177 inv => $::INV,
178 invr => $::INVR,
179 invr_hb => $::INVR_HB,
180
181 );
182
183 while (my ($id, $name) = each %ass) {
184 $self->widget_associate (undef, $id => $wkw{$name})
185 or warn "server failed to associate non-existent well-known widget $name\n";
139 } 186 }
140 }); 187 });
141 188
142 # widget call 189 # widget call
143 $self->connect_ext (w_c => sub { 190 $self->connect_ext (w_c => sub {
144 my ($id, $rid, $method, $args) = @_; 191 my ($id, $rid, $method, @args) = @_;
145 192
146 my $w = $self->{widget}{$id} 193 my $w = $self->{widget}{$id}
147 or return; 194 or return;
148 195
149 $args ||= [];
150
151 if ($rid) { 196 if ($rid) {
152 $self->send_exti_msg (w_r => $rid, [$w->$method (@$args)]); 197 $self->send_exti_msg (w_r => $rid, $w->$method (@args));
153 } else { 198 } else {
154 $w->$method (@$args); 199 $w->$method (@args);
155 } 200 }
156 }); 201 });
157 202
158 # widget set 203 # widget set
159 $self->connect_ext (w_s => sub { 204 $self->connect_ext (w_s => sub {
219 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}"; 264 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
220 265
221 $self 266 $self
222} 267}
223 268
269sub widget_associate {
270 my ($self, $ws, $id, $widget) = @_;
271
272 if ($widget) {
273 $widget->{s_id} = $id;
274 $self->{widget}{$id} = $widget;
275
276 if ($ws) {
277 $widget->{s_ws} = $ws;
278 $self->{widgetset}{$ws}{w}{$id} = $widget;
279 }
280
281 $widget->connect (on_destroy => sub {
282 my ($widget) = @_;
283
284 delete $self->{widget}{$widget->{s_id}};
285 delete $self->{widgetset}{$widget->{s_ws}}{$widget->{s_id}}
286 if exists $widget->{s_ws};
287 });
288
289 1
290 } else {
291 $self->send_exti_msg (w_e => $id, undef);
292
293 0
294 }
295}
296
224sub update_fx_want { 297sub update_fx_want {
225 my ($self) = @_; 298 my ($self) = @_;
226 299
227 $self->send_exti_msg (fx_want => { 300 $self->send_exti_msg (fx_want => {
228 3 => !!$::CFG->{bgm_enable}, # FT_MUSIC 301 3 => !!$::CFG->{bgm_enable}, # FT_MUSIC
229 5 => !!$::CFG->{audio_enable}, # FT_SOUND 302 5 => !!$::CFG->{audio_enable}, # FT_SOUND
303 6 => 1, # FT_RSRC
230 }); 304 });
231} 305}
232 306
233sub logprint { 307sub logprint {
234 my ($self, @a) = @_; 308 my ($self, @a) = @_;
506} 580}
507 581
508sub feed_map1a { 582sub feed_map1a {
509 my ($self, $data) = @_; 583 my ($self, $data) = @_;
510 584
511 $self->{map}->map1a_update ($data, $self->{setup}{extmap}); 585 my $missing = $self->{map}->map1a_update ($data, $self->{setup}{extmap});
586 my $delay;
587
588 for my $tile (@$missing) {
589 next if $self->{delay}{$tile};
590
591 $delay = 1;
592
593 if (my $tex = $::CONN->{texture}[$tile]) {
594 $tex->upload;
595 } else {
596 $self->{delay}{$tile} = 1;
597
598 # we assume the face is in-flight and will eventually come
599 push @{$self->{tile_cb}{$tile}}, sub {
600 delete $self->{delay}{$tile};
601 $_[0]->upload;
602 };
603 }
604 }
605
606 if ($delay) {
607 # delay the map drawing a tiny bit in the hope of getting the missing fetched
608 Event->timer (after => 0.03, cb => sub {
609 $_[0]->w->cancel;
610 $self->{map_widget}->update;
611 });
612 } else {
512 $self->{map_widget}->update; 613 $self->{map_widget}->update;
614 }
513} 615}
514 616
515sub magicmap { 617sub magicmap {
516 my ($self, $w, $h, $x, $y, $data) = @_; 618 my ($self, $w, $h, $x, $y, $data) = @_;
517 619
524 my $map_info = delete $self->{map_info} 626 my $map_info = delete $self->{map_info}
525 or return; 627 or return;
526 628
527 my ($hash, $x, $y, $w, $h) = @$map_info; 629 my ($hash, $x, $y, $w, $h) = @$map_info;
528 630
529 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 631 my $data = Compress::LZF::compress $self->{map}->get_rect ($x, $y, $w, $h);
632 $self->{map_cache_new}{$hash} = \$data;
530 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { }; 633 CFPlus::DB::put $self->{mapcache} => $hash => $data, sub { };
531 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
532} 634}
533 635
534sub map_clear { 636sub map_clear {
535 my ($self) = @_; 637 my ($self) = @_;
536 638
542} 644}
543 645
544sub bg_fetch { 646sub bg_fetch {
545 my ($self) = @_; 647 my ($self) = @_;
546 648
547 my $id; 649 my $tile;
548 650
549 do { 651 do {
550 $id = pop @{$self->{bg_fetch}} 652 $tile = pop @{$self->{bg_fetch}}
551 or return; 653 or return;
552 } while $self->{texture}[$id]; 654 } while $self->{texture}[$tile];
553 655
554 CFPlus::DB::exists tilecache => $id, sub { 656 CFPlus::DB::get tilecache => $tile, sub {
555 my ($exists) = @_; 657 my ($data) = @_;
556 658
557 return unless $self->{map}; # stop when destroyed 659 return unless $self->{map}; # stop when destroyed
558 660
661 if (defined $data) {
559 $self->have_tile ($id) 662 $self->have_tile ($tile, $data);
560 if $exists; 663 $self->{texture}[$tile]->upload;
664 }
561 665
562 $self->bg_fetch; 666 $self->bg_fetch;
563 }; 667 };
564} 668}
565 669
566sub load_map($$$) { 670sub load_map($$$) {
567 my ($self, $hash, $x, $y) = @_; 671 my ($self, $hash, $x, $y) = @_;
568 672
569 my $gen = $self->{map_change_gen}; 673 my $gen = $self->{map_change_gen};
570 674
571 CFPlus::DB::get $self->{mapcache} => $hash, sub { 675 my $cb = sub {
572 return unless $gen == $self->{map_change_gen}; 676 return unless $gen == $self->{map_change_gen};
573 677
574 my ($data) = @_; 678 my ($data) = @_;
575 679
576 if (defined $data) { 680 if (defined $data) {
681 $self->{map_cache_new}{$hash} = \$data;
682
577 $data = Compress::LZF::decompress $data; 683 my $data = Compress::LZF::decompress $data;
578 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
579 684
580 my $inprogress = @{ $self->{bg_fetch} || [] }; 685 my $inprogress = @{ $self->{bg_fetch} || [] };
581 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data); 686 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data);
582 $self->bg_fetch unless $inprogress; 687 $self->bg_fetch unless $inprogress;
583 } 688 }
584 }; 689 };
690
691 if (my $rdata = $self->{map_cache_old}{$hash}) {
692 $cb->($$rdata);
693 } else {
694 CFPlus::DB::get $self->{mapcache} => $hash, $cb;
695 }
585} 696}
586 697
587# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 698# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
588# (server resource,s latency, bandwidth), so this hack is warranted. 699# (server resource,s latency, bandwidth), so this hack is warranted.
589# the right fix is to make real tiled maps with an overview file 700# the right fix is to make real tiled maps with an overview file
682 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 793 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
683 794
684 $self->flush_map; 795 $self->flush_map;
685 796
686 ++$self->{map_change_gen}; 797 ++$self->{map_change_gen};
798 $self->{map_cache_old} = delete $self->{map_cache_new};
687 799
688 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 800 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
689 801
690 my $mapmapw = $self->{mapmap}->{w}; 802 my $mapmapw = $self->{mapmap}->{w};
691 my $mapmaph = $self->{mapmap}->{h}; 803 my $mapmaph = $self->{mapmap}->{h};
713 my ($self, $facenum, $face, $cb) = @_; 825 my ($self, $facenum, $face, $cb) = @_;
714 826
715 if ($face->{type} == 0) { # FT_FACE 827 if ($face->{type} == 0) { # FT_FACE
716 my $id = CFPlus::DB::get_tile_id_sync $face->{name}; 828 my $id = CFPlus::DB::get_tile_id_sync $face->{name};
717 829
718 $face->{id} = $id; 830 $face->{id} = $id;
719 $self->{faceid}[$facenum] = $id;
720
721 $self->{map}->set_tileid ($facenum => $id); 831 $self->{map}->set_tileid ($facenum => $id);
722 832
723 CFPlus::DB::get tilecache => $id, $cb; 833 CFPlus::DB::get tilecache => $id, $cb;
724 834
725 } elsif ($face->{type} & 1) { # with metadata 835 } elsif ($face->{type} & 1) { # with metadata
726 #::message ({ markup => "starting to download song #$face/$pri, check your output-rate setting if your connection gets laggy." });
727 CFPlus::DB::get res_meta => $face->{name}, $cb; 836 CFPlus::DB::get res_meta => $face->{name}, $cb;
837
838 } else { # no metadata
839 CFPlus::DB::get res_data => $face->{name}, $cb;
728 } 840 }
729} 841}
730 842
731sub face_update { 843sub face_update {
732 my ($self, $facenum, $face, $changed) = @_; 844 my ($self, $facenum, $face, $changed) = @_;
733 845
734 if ($face->{type} == 0) { # FT_FACE 846 if ($face->{type} == 0) {
847 # image, FT_FACE
735 CFPlus::DB::put tilecache => $face->{id} => $face->{data}, sub { } 848 CFPlus::DB::put tilecache => $face->{id} => $face->{data}, sub { }
736 if $changed; 849 if $changed;
737 850
738 $self->have_tile ($face->{id}); 851 $self->have_tile ($face->{id}, delete $face->{data});
739 852
740 } elsif ($face->{type} & 1) { # split metadata 853 } elsif ($face->{type} & 1) {
854 # split metadata case, FT_MUSIC, FT_SOUND
741 if ($changed) { # new data 855 if ($changed) { # new data
742 my ($meta_json, $data) = unpack "(w/a*)*", delete $face->{data}; 856 my ($meta, $data) = unpack "(w/a*)*", $face->{data};
743 $face->{meta} = $self->{json_coder}->decode ($meta_json); 857 $face->{data} = $meta;
744 858
745 CFPlus::DB::put res_data => $face->{name} => $data, sub { }; 859 CFPlus::DB::put res_data => $face->{name} => $data, sub { };
746 CFPlus::DB::put res_meta => $face->{name} => $meta_json, sub { }; 860 CFPlus::DB::put res_meta => $face->{name} => $meta, sub { };
747 } else { 861 }
862
748 $face->{meta} = $self->{json_coder}->decode (delete $face->{data}); 863 $face->{data} = $self->{json_coder}->decode ($face->{data});
749 }
750 864
751 ::add_license ($face); 865 ::add_license ($face);
752 866
753 if ($face->{type} == 3) { # FT_MUSIC 867 if ($face->{type} == 3) { # FT_MUSIC
754 $self->{music_meta}{$facenum} = $face;
755
756 ::message ({ markup => "downloaded song #$facenum" }) 868 ::message ({ markup => "downloaded song #$facenum" })
757 if $changed; 869 if $changed;
758 870
759 &::audio_music_push ($facenum); 871 &::audio_music_push ($facenum);
760 } elsif ($face->{type} == 5) { # FT_SOUND 872 } elsif ($face->{type} == 5) { # FT_SOUND
761 $self->{sound_meta}{$facenum} = $face;
762
763 ::message ({ markup => "downloaded sound #$facenum" }) 873 ::message ({ markup => "downloaded sound #$facenum" })
764 if $changed; 874 if $changed;
765 875
766 &::audio_sound_push ($facenum); 876 &::audio_sound_push ($facenum);
767 } 877 }
768 878
879 } else {
880 # flat resource case, FT_RSRC
881 CFPlus::DB::put res_data => $face->{name} => $face->{data}, sub { }
882 if $changed;
883 }
884
885 if (my $cbs = $self->{face_cb}{$facenum}) {
886 $_->($face, $changed) for @$cbs;
769 } 887 }
770} 888}
771 889
772sub smooth_update { 890sub smooth_update {
773 my ($self, $facenum, $face) = @_; 891 my ($self, $facenum, $face) = @_;
774 892
775 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel}); 893 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
776} 894}
777 895
778sub have_tile { 896sub have_tile {
779 my ($self, $tile) = @_; 897 my ($self, $tile, $data) = @_;
780 898
781 return unless $self->{map}; 899 return unless $self->{map};
782 900
783 $self->{texture}[$tile] ||= my $tex = 901 my $tex = $self->{texture}[$tile] ||=
784 new CFPlus::Texture 902 new CFPlus::Texture
785 tile => $tile, minify => 1, mipmap => 1, delay => 1; 903 tile => $tile,
904 image => $data, delete_image => 1,
905 minify => 1, mipmap => 1;
786 906
787 $_->() for @{(delete $self->{face_cb}{$tile}) || []}; 907 if (my $cbs = delete $self->{tile_cb}{$tile}) {
908 $_->($tex) for @$cbs;
909 }
788} 910}
789 911
790sub connect_face_update { 912# call in non-void context registers a temporary
913# hook with handle, otherwise its permanent
914sub on_face_change {
791 my ($self, $id, $cb) = @_; 915 my ($self, $num, $cb) = @_;
792 916
793 push @{$self->{face_cb}{$id}}, $cb; 917 push @{$self->{face_cb}{$num}}, $cb;
794 918
919 defined wantarray
795 CFPlus::guard { 920 ? CFPlus::guard {
796 @{$self->{face_cb}{$id}} 921 @{$self->{face_cb}{$num}}
797 = grep $_ != $cb, 922 = grep $_ != $cb,
798 @{$self->{face_cb}{$id}}; 923 @{$self->{face_cb}{$num}};
799 } 924 }
925 : ()
926}
927
928# call in non-void context registers a temporary
929# hook with handle, otherwise its permanent
930sub register_face_handler {
931 my ($self, $num, $cb) = @_;
932
933 # invoke if available right now
934 $cb->($self->{face}[$num], 0)
935 unless exists $self->{face}[$num]{loading};
936
937 # future changes
938 $self->on_face_change ($num => $cb)
800} 939}
801 940
802sub sound_play { 941sub sound_play {
803 my ($self, $type, $face, $dx, $dy, $vol) = @_; 942 my ($self, $type, $face, $dx, $dy, $vol) = @_;
804 943
876 $self->{$type} = $text; 1015 $self->{$type} = $text;
877 } else { 1016 } else {
878 $self->logprint ("msg: ", $text); 1017 $self->logprint ("msg: ", $text);
879 return if $color < 0; # negative color == ignore if not understood 1018 return if $color < 0; # negative color == ignore if not understood
880 1019
881 my $fg = $CF_COLOR[$color & 0x1f] || [1, 0, 0]; 1020 my $fg = $CF_COLOR[$color & NDI_COLOR_MASK] || [1, 0, 0];
882 1021
883 ## try to create single paragraphs of multiple lines sent by the server 1022 ## try to create single paragraphs of multiple lines sent by the server
884 # no longer neecssary with TRT servers 1023 # no longer neecssary with TRT servers
885 #$text =~ s/(?<=\S)\n(?=\w)/ /g; 1024 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
886 1025
887 ::message ({ fg => $fg, markup => $_, type => $type, extra => [@extra] }) 1026 ::message ({
1027 fg => $fg,
1028 markup => $_,
1029 type => $type,
1030 extra => [@extra],
1031 color_flags => $color
888 for split /\n/, $text; 1032 }) for split /\n/, $text;
889 1033
890 $self->{statusbox}->add ($text, 1034 $self->{statusbox}->add ($text,
891 group => $text, 1035 group => $text,
892 fg => $fg, 1036 fg => $fg,
893 timeout => $color >= 2 ? 180 : 10, 1037 timeout => $color >= 2 ? 180 : 10,
987 } 1131 }
988 1132
989 $::FLOORBOX->add_at (@add); 1133 $::FLOORBOX->add_at (@add);
990 }); 1134 });
991 1135
992 $::WANT_REFRESH++; 1136 $::WANT_REFRESH->start;
993} 1137}
994 1138
995sub set_opencont { 1139sub set_opencont {
996 my ($conn, $tag, $name) = @_; 1140 my ($conn, $tag, $name) = @_;
997 $conn->{open_container} = $tag; 1141 $conn->{open_container} = $tag;
998 update_floorbox; 1142 update_floorbox;
999 1143
1000 $::INV_RIGHT_HB->clear (); 1144 $::INVR_HB->clear ();
1001 $::INV_RIGHT_HB->add (new CFPlus::UI::Label align => 0, expand => 1, text => $name); 1145 $::INVR_HB->add (new CFPlus::UI::Label align => 0, expand => 1, text => $name);
1002 1146
1003 if ($tag != 0) { # Floor isn't closable, is it? 1147 if ($tag != 0) { # Floor isn't closable, is it?
1004 $::INV_RIGHT_HB->add (new CFPlus::UI::Button 1148 $::INVR_HB->add (new CFPlus::UI::Button
1005 text => "Close container", 1149 text => "Close container",
1006 tooltip => "Close the currently open container (if one is open)", 1150 tooltip => "Close the currently open container (if one is open)",
1007 on_activate => sub { 1151 on_activate => sub {
1008 $::CONN->send ("apply $tag") # $::CONN->{open_container}") 1152 $::CONN->send ("apply $tag") # $::CONN->{open_container}")
1009 if $tag != 0; 1153 if $tag != 0;
1010 #if $CONN->{open_container} != 0;
1011 0 1154 0
1012 }, 1155 },
1013 ); 1156 );
1014 } 1157 }
1015 1158

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines