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.131 by root, Tue Jul 24 04:54:48 2007 UTC vs.
Revision 1.147 by root, Thu Aug 9 03:40:01 2007 UTC

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 { 53 $self->connect_ext (ambient_music => sub {
54 my ($ev) = @_; 54 my ($songs) = @_;
55 55 &::audio_music_set_ambient ($songs);
56 &::audio_music_set ($self->{songs} = $ev->{play})
57 if $ev->{play};
58 }); 56 });
59 57
60 $self->connect_ext (capabilities => sub { 58 $self->connect_ext (capabilities => sub {
61 my (%cap) = @_; 59 my (%cap) = @_;
62 60
63 $self->send_exti_msg (fx_want => { 61 #$self->send ("setup sound 0"); # we use a different protocol
64 3 => !!$::CFG->{bgm_enable}, # FT_MUSIC 62 $self->update_fx_want;
65 5 => !!$::CFG->{audio_enable}, # FT_SOUND
66 });
67 63
68 if (my $ts = $cap{tileset}) { 64 if (my $ts = $cap{tileset}) {
69 if (my ($default) = grep $_->[2] & 1, @$ts) { 65 if (my ($default) = grep $_->[2] & 1, @$ts) {
70 $self->{tileset} = $default; 66 $self->{tileset} = $default;
71 $self->{tilesize} = $default->[3]; 67 $self->{tilesize} = $default->[3];
185 or return; 181 or return;
186 182
187 $self->send_exti_msg (w_r => $rid, [map $w->{$_}, @$attr]); 183 $self->send_exti_msg (w_r => $rid, [map $w->{$_}, @$attr]);
188 }); 184 });
189 185
186 # message window
187 $self->connect_ext (channel_info => sub {
188 my ($info) = @_;
189 $self->{channels}->{$info->{id}} = $info;
190 $::MESSAGE_WINDOW->add_channel ($info);
191 });
192
190 # destroy widgets on logout 193 # destroy widgets on logout
191 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub { 194 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub {
192 for my $ws (values %{delete $self->{widgetset} || {}}) { 195 for my $ws (values %{delete $self->{widgetset} || {}}) {
193 $_->destroy 196 $_->destroy
194 for values %{delete $ws->{w} || {}}; 197 for values %{delete $ws->{w} || {}};
216 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}"; 219 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
217 220
218 $self 221 $self
219} 222}
220 223
224sub update_fx_want {
225 my ($self) = @_;
226
227 $self->send_exti_msg (fx_want => {
228 3 => !!$::CFG->{bgm_enable}, # FT_MUSIC
229 5 => !!$::CFG->{audio_enable}, # FT_SOUND
230 });
231}
232
221sub logprint { 233sub logprint {
222 my ($self, @a) = @_; 234 my ($self, @a) = @_;
223 235
224 $self->{log_fh} ||= do { 236 CFPlus::DB::logprint "$Crossfire::VARDIR/log.$self->{host}" => (join "", @a), sub { };
225 my $path = "$Crossfire::VARDIR/log.$self->{host}";
226
227 open my $fh, ">>:utf8", $path
228 or die "Couldn't open logfile $path: $!";
229
230 $fh->autoflush (1);
231
232 $fh;
233 };
234
235 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
236
237 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
238 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
239
240 print {$self->{log_fh}} "$ts ", @a, "\n";
241} 237}
242 238
243sub _stat_numdiff { 239sub _stat_numdiff {
244 my ($self, $name, $old, $new) = @_; 240 my ($self, $name, $old, $new) = @_;
245 241
553 do { 549 do {
554 $id = pop @{$self->{bg_fetch}} 550 $id = pop @{$self->{bg_fetch}}
555 or return; 551 or return;
556 } while $self->{texture}[$id]; 552 } while $self->{texture}[$id];
557 553
558 CFPlus::DB::get tilecache => $id, sub { 554 CFPlus::DB::exists tilecache => $id, sub {
559 my ($data) = @_; 555 my ($exists) = @_;
560 556
561 return unless $self->{map}; # stop when destroyed 557 return unless $self->{map}; # stop when destroyed
562 558
563 $self->set_texture ($id => $data) 559 $self->have_tile ($id)
564 if defined $data; 560 if $exists;
565 561
566 $self->bg_fetch; 562 $self->bg_fetch;
567 }; 563 };
568} 564}
569 565
726 722
727 CFPlus::DB::get tilecache => $id, $cb; 723 CFPlus::DB::get tilecache => $id, $cb;
728 724
729 } elsif ($face->{type} & 1) { # with metadata 725 } elsif ($face->{type} & 1) { # with metadata
730 #::message ({ markup => "starting to download song #$face/$pri, check your output-rate setting if your connection gets laggy." }); 726 #::message ({ markup => "starting to download song #$face/$pri, check your output-rate setting if your connection gets laggy." });
731 CFPlus::DB::get "res-metadata" => $face->{name}, $cb; 727 CFPlus::DB::get res_meta => $face->{name}, $cb;
732 } 728 }
733} 729}
734 730
735sub face_update { 731sub face_update {
736 my ($self, $facenum, $face, $changed) = @_; 732 my ($self, $facenum, $face, $changed) = @_;
737 733
738 if ($face->{type} == 0) { # FT_FACE 734 if ($face->{type} == 0) { # FT_FACE
739 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { } 735 CFPlus::DB::put tilecache => $face->{id} => $face->{data}, sub { }
740 if $changed; 736 if $changed;
741 737
742 $self->set_texture ($face->{id} => delete $face->{data}); 738 $self->have_tile ($face->{id});
743 739
744 } elsif ($face->{type} & 1) { # split metadata 740 } elsif ($face->{type} & 1) { # split metadata
745 my $cb = sub { };
746
747 if ($face->{type} == 3) { # FT_MUSIC
748 $cb = sub {
749 $self->{music_meta}{$facenum} = $face;
750
751 ::message ({ markup => "downloaded song #$facenum" })
752 if $changed;
753
754 &::audio_music_set ($self->{songs});
755 };
756 }
757
758 if ($changed) { # new data 741 if ($changed) { # new data
759 my ($meta_json, $data) = unpack "(w/a*)*", delete $face->{data}; 742 my ($meta_json, $data) = unpack "(w/a*)*", delete $face->{data};
760 my $path = CFPlus::DB::path_of_res $face->{name};
761
762 CFPlus::DB::write_file $path, $data, sub {
763 CFPlus::DB::put "res-metadata" => $face->{name} => $meta_json, sub {
764 $face->{meta} = $self->{json_coder}->decode ($meta_json); 743 $face->{meta} = $self->{json_coder}->decode ($meta_json);
765 $face->{path} = $path;
766 744
767 $cb->(); 745 CFPlus::DB::put res_data => $face->{name} => $data, sub { };
768 }; 746 CFPlus::DB::put res_meta => $face->{name} => $meta_json, sub { };
769 };
770 } else { 747 } else {
771 $face->{meta} = $self->{json_coder}->decode (delete $face->{data}); 748 $face->{meta} = $self->{json_coder}->decode (delete $face->{data});
772 $cb->();
773 } 749 }
750
751 ::add_license ($face);
752
753 if ($face->{type} == 3) { # FT_MUSIC
754 $self->{music_meta}{$facenum} = $face;
755
756 ::message ({ markup => "downloaded song #$facenum" })
757 if $changed;
758
759 &::audio_music_push ($facenum);
760 } elsif ($face->{type} == 5) { # FT_SOUND
761 $self->{sound_meta}{$facenum} = $face;
762
763 ::message ({ markup => "downloaded sound #$facenum" })
764 if $changed;
765
766 &::audio_sound_push ($facenum);
767 }
768
774 } 769 }
775} 770}
776 771
777sub smooth_update { 772sub smooth_update {
778 my ($self, $facenum, $face) = @_; 773 my ($self, $facenum, $face) = @_;
779 774
780 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel}); 775 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
781} 776}
782 777
783sub set_texture { 778sub have_tile {
784 my ($self, $id, $data) = @_; 779 my ($self, $tile) = @_;
785 780
786 return unless $self->{map}; 781 return unless $self->{map};
787 782
788 $self->{texture}[$id] = my $tex = 783 $self->{texture}[$tile] ||= my $tex =
789 new_from_image CFPlus::Texture 784 new CFPlus::Texture
790 $data, minify => 1, mipmap => 1; 785 tile => $tile, minify => 1, mipmap => 1, delay => 1;
791 786
792 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
793 $self->{map_widget}->update;
794
795 $_->() for @{(delete $self->{face_cb}{$id}) || []}; 787 $_->() for @{(delete $self->{face_cb}{$tile}) || []};
796} 788}
797 789
798sub connect_face_update { 790sub connect_face_update {
799 my ($self, $id, $cb) = @_; 791 my ($self, $id, $cb) = @_;
800 792
806 @{$self->{face_cb}{$id}}; 798 @{$self->{face_cb}{$id}};
807 } 799 }
808} 800}
809 801
810sub sound_play { 802sub sound_play {
811 my ($self, $x, $y, $soundnum, $type) = @_; 803 my ($self, $type, $face, $dx, $dy, $vol) = @_;
812 804
813 $self->{sound_play}->($x, $y, $soundnum, $type); 805 &::audio_sound_play ($face, $dx, $dy, $vol)
806 unless $type & 1; # odd types are silent for future expansion
814} 807}
815 808
816my $LAST_QUERY; # server is stupid, stupid, stupid 809my $LAST_QUERY; # server is stupid, stupid, stupid
817 810
818sub query { 811sub query {
883 $self->{$type} = $text; 876 $self->{$type} = $text;
884 } else { 877 } else {
885 $self->logprint ("msg: ", $text); 878 $self->logprint ("msg: ", $text);
886 return if $color < 0; # negative color == ignore if not understood 879 return if $color < 0; # negative color == ignore if not understood
887 880
888 my $fg = $CF_COLOR[$color % @CF_COLOR]; 881 my $fg = $CF_COLOR[$color & 0x1f] || [1, 0, 0];
889 882
890 ## try to create single paragraphs of multiple lines sent by the server 883 ## try to create single paragraphs of multiple lines sent by the server
891 # no longer neecssary with TRT servers 884 # no longer neecssary with TRT servers
892 #$text =~ s/(?<=\S)\n(?=\w)/ /g; 885 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
893 886
894 ::message ({ fg => $fg, markup => $_ }) 887 ::message ({ fg => $fg, markup => $_, type => $type, extra => [@extra] })
895 for split /\n/, $text; 888 for split /\n/, $text;
896 889
897 $self->{statusbox}->add ($text, 890 $self->{statusbox}->add ($text,
898 group => $text, 891 group => $text,
899 fg => $fg, 892 fg => $fg,
954 my ($self) = @_; 947 my ($self) = @_;
955 948
956 $self->{map_widget}->clr_commands; 949 $self->{map_widget}->clr_commands;
957 950
958 ::stop_game (); 951 ::stop_game ();
959}
960
961sub image_info {
962 my ($self, $numfaces) = @_;
963
964 $self->{num_faces} = $numfaces;
965 $self->{face_prefetch} = [1 .. $numfaces];
966 $self->face_prefetch;
967}
968
969sub face_prefetch {
970 my ($self) = @_;
971
972 return unless $::CFG->{face_prefetch};
973
974 if ($self->{num_faces}) {
975 return if @{ $self->{send_queue} || [] };
976 my $todo = @{ $self->{face_prefetch} }
977 or return;
978
979 my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
980
981 $self->send ("requestinfo image_sums $face $face");
982
983 $self->{statusbox}->add (CFPlus::asxml "prefetching $todo",
984 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
985 } elsif (!exists $self->{num_faces}) {
986 $self->send ("requestinfo image_info");
987
988 $self->{num_faces} = 0;
989
990 $self->{statusbox}->add (CFPlus::asxml "starting to prefetch",
991 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
992 }
993} 952}
994 953
995sub update_floorbox { 954sub update_floorbox {
996 $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub { 955 $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub {
997 return unless $::CONN; 956 return unless $::CONN;
1155 . "cfplus support $yesno[$self->{cfplus_ext} > 0]" 1114 . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
1156 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 1115 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
1157 . "map size $self->{mapw}×$self->{maph}\n" 1116 . "map size $self->{mapw}×$self->{maph}\n"
1158 ); 1117 );
1159 1118
1160 ::setup_build_button ($self->{editor_support}->{builder_ui});
1161} 1119}
1162 1120
1163sub logged_in { 1121sub logged_in {
1164 my ($self) = @_; 1122 my ($self) = @_;
1165 1123
1185 1143
1186 $self->send_command ("output-sync $::CFG->{output_sync}"); 1144 $self->send_command ("output-sync $::CFG->{output_sync}");
1187 $self->send_command ("output-count $::CFG->{output_count}"); 1145 $self->send_command ("output-count $::CFG->{output_count}");
1188 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0; 1146 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
1189 $self->send_command ("pickup $::CFG->{pickup}"); 1147 $self->send_command ("pickup $::CFG->{pickup}");
1190}
1191
1192sub buildat {
1193 my ($self, $builditem, $x, $y) = @_;
1194
1195 if ($self->{cfplus_ext}) {
1196 $self->send_ext_msg (builder_build => dx => $x, dy => $y, (ref ($builditem) eq 'HASH') ? %$builditem : (item => $builditem));
1197 }
1198} 1148}
1199 1149
1200sub lookat { 1150sub lookat {
1201 my ($self, $x, $y) = @_; 1151 my ($self, $x, $y) = @_;
1202 1152

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines