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.139 by root, Sun Jul 29 18:22:34 2007 UTC vs.
Revision 1.153 by root, Mon Aug 13 14:24:17 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 ("setup sound 0"); # we use a different protocol 61 #$self->send ("setup sound 0"); # we use a different protocol
64 62 $self->update_fx_want;
65 $self->send_exti_msg (fx_want => {
66 3 => !!$::CFG->{bgm_enable}, # FT_MUSIC
67 5 => !!$::CFG->{audio_enable}, # FT_SOUND
68 });
69 63
70 if (my $ts = $cap{tileset}) { 64 if (my $ts = $cap{tileset}) {
71 if (my ($default) = grep $_->[2] & 1, @$ts) { 65 if (my ($default) = grep $_->[2] & 1, @$ts) {
72 $self->{tileset} = $default; 66 $self->{tileset} = $default;
73 $self->{tilesize} = $default->[3]; 67 $self->{tilesize} = $default->[3];
225 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}"; 219 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
226 220
227 $self 221 $self
228} 222}
229 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
230sub logprint { 233sub logprint {
231 my ($self, @a) = @_; 234 my ($self, @a) = @_;
232 235
233 $self->{log_fh} ||= do { 236 CFPlus::DB::logprint "$Crossfire::VARDIR/log.$self->{host}" => (join "", @a), sub { };
234 my $path = "$Crossfire::VARDIR/log.$self->{host}";
235
236 open my $fh, ">>:utf8", $path
237 or die "Couldn't open logfile $path: $!";
238
239 $fh->autoflush (1);
240
241 $fh;
242 };
243
244 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
245
246 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
247 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
248
249 print {$self->{log_fh}} "$ts ", @a, "\n";
250} 237}
251 238
252sub _stat_numdiff { 239sub _stat_numdiff {
253 my ($self, $name, $old, $new) = @_; 240 my ($self, $name, $old, $new) = @_;
254 241
519} 506}
520 507
521sub feed_map1a { 508sub feed_map1a {
522 my ($self, $data) = @_; 509 my ($self, $data) = @_;
523 510
524 $self->{map}->map1a_update ($data, $self->{setup}{extmap}); 511 my $missing = $self->{map}->map1a_update ($data, $self->{setup}{extmap});
512 my $delay;
513
514 for my $tile (@$missing) {
515 next if $self->{delay}{$tile};
516
517 $delay = 1;
518
519 if (my $tex = $::CONN->{texture}[$tile]) {
520 $tex->upload;
521 } else {
522 $self->{delay}{$tile} = 1;
523
524 # we assume the face is in-flight and will eventually come
525 push @{$self->{face_cb}{$tile}}, sub {
526 delete $self->{delay}{$tile};
527 $_[0]->upload;
528 };
529 }
530 }
531
532 if ($delay) {
533 # delay the map drawing a tiny bit in the hope of getting the missing fetched
534 Event->timer (after => 0.03, cb => sub {
535 $_[0]->w->cancel;
536 $self->{map_widget}->update;
537 });
538 } else {
525 $self->{map_widget}->update; 539 $self->{map_widget}->update;
540 }
526} 541}
527 542
528sub magicmap { 543sub magicmap {
529 my ($self, $w, $h, $x, $y, $data) = @_; 544 my ($self, $w, $h, $x, $y, $data) = @_;
530 545
537 my $map_info = delete $self->{map_info} 552 my $map_info = delete $self->{map_info}
538 or return; 553 or return;
539 554
540 my ($hash, $x, $y, $w, $h) = @$map_info; 555 my ($hash, $x, $y, $w, $h) = @$map_info;
541 556
542 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 557 my $data = Compress::LZF::compress $self->{map}->get_rect ($x, $y, $w, $h);
558 $self->{map_cache_new}{$hash} = \$data;
543 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { }; 559 CFPlus::DB::put $self->{mapcache} => $hash => $data, sub { };
544 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
545} 560}
546 561
547sub map_clear { 562sub map_clear {
548 my ($self) = @_; 563 my ($self) = @_;
549 564
562 do { 577 do {
563 $id = pop @{$self->{bg_fetch}} 578 $id = pop @{$self->{bg_fetch}}
564 or return; 579 or return;
565 } while $self->{texture}[$id]; 580 } while $self->{texture}[$id];
566 581
567 CFPlus::DB::get tilecache => $id, sub { 582 CFPlus::DB::exists tilecache => $id, sub {
568 my ($data) = @_; 583 my ($exists) = @_;
569 584
570 return unless $self->{map}; # stop when destroyed 585 return unless $self->{map}; # stop when destroyed
571 586
572 $self->set_texture ($id => $data) 587 if ($exists) {
573 if defined $data; 588 $self->have_tile ($id);
589 $self->{texture}[$id]->upload;
590 }
574 591
575 $self->bg_fetch; 592 $self->bg_fetch;
576 }; 593 };
577} 594}
578 595
579sub load_map($$$) { 596sub load_map($$$) {
580 my ($self, $hash, $x, $y) = @_; 597 my ($self, $hash, $x, $y) = @_;
581 598
582 my $gen = $self->{map_change_gen}; 599 my $gen = $self->{map_change_gen};
583 600
584 CFPlus::DB::get $self->{mapcache} => $hash, sub { 601 my $cb = sub {
585 return unless $gen == $self->{map_change_gen}; 602 return unless $gen == $self->{map_change_gen};
586 603
587 my ($data) = @_; 604 my ($data) = @_;
588 605
589 if (defined $data) { 606 if (defined $data) {
607 $self->{map_cache_new}{$hash} = \$data;
608
590 $data = Compress::LZF::decompress $data; 609 my $data = Compress::LZF::decompress $data;
591 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
592 610
593 my $inprogress = @{ $self->{bg_fetch} || [] }; 611 my $inprogress = @{ $self->{bg_fetch} || [] };
594 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data); 612 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data);
595 $self->bg_fetch unless $inprogress; 613 $self->bg_fetch unless $inprogress;
596 } 614 }
597 }; 615 };
616
617 if (my $rdata = $self->{map_cache_old}{$hash}) {
618 $cb->($$rdata);
619 } else {
620 CFPlus::DB::get $self->{mapcache} => $hash, $cb;
621 }
598} 622}
599 623
600# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 624# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
601# (server resource,s latency, bandwidth), so this hack is warranted. 625# (server resource,s latency, bandwidth), so this hack is warranted.
602# the right fix is to make real tiled maps with an overview file 626# the right fix is to make real tiled maps with an overview file
695 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 719 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
696 720
697 $self->flush_map; 721 $self->flush_map;
698 722
699 ++$self->{map_change_gen}; 723 ++$self->{map_change_gen};
724 $self->{map_cache_old} = delete $self->{map_cache_new};
700 725
701 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 726 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
702 727
703 my $mapmapw = $self->{mapmap}->{w}; 728 my $mapmapw = $self->{mapmap}->{w};
704 my $mapmaph = $self->{mapmap}->{h}; 729 my $mapmaph = $self->{mapmap}->{h};
734 $self->{map}->set_tileid ($facenum => $id); 759 $self->{map}->set_tileid ($facenum => $id);
735 760
736 CFPlus::DB::get tilecache => $id, $cb; 761 CFPlus::DB::get tilecache => $id, $cb;
737 762
738 } elsif ($face->{type} & 1) { # with metadata 763 } 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-metadata" => $face->{name}, $cb; 764 CFPlus::DB::get res_meta => $face->{name}, $cb;
741 } 765 }
742} 766}
743 767
744sub face_update { 768sub face_update {
745 my ($self, $facenum, $face, $changed) = @_; 769 my ($self, $facenum, $face, $changed) = @_;
746 770
747 if ($face->{type} == 0) { # FT_FACE 771 if ($face->{type} == 0) { # FT_FACE
748 CFPlus::DB::put tilecache => $face->{id} => $face->{data}, sub { } 772 CFPlus::DB::put tilecache => $face->{id} => $face->{data}, sub { }
749 if $changed; 773 if $changed;
750 774
751 $self->set_texture ($face->{id} => delete $face->{data}); 775 $self->have_tile ($face->{id});
752 776
753 } elsif ($face->{type} & 1) { # split metadata 777 } elsif ($face->{type} & 1) { # split metadata
754 my $cb = sub { };
755 my $path;
756
757 if ($face->{type} == 3) { # FT_MUSIC
758 # store externally
759 $path = CFPlus::DB::path_of_res $face->{name};
760
761 $cb = sub {
762 $self->{music_meta}{$facenum} = $face;
763
764 ::message ({ markup => "downloaded song #$facenum" })
765 if $changed;
766
767 &::audio_music_set ($self->{songs});
768 };
769 } elsif ($face->{type} == 5) { # FT_SOUND
770 $cb = sub {
771 $self->{sound_meta}{$facenum} = $face;
772
773 use Data::Dumper; warn Dumper $face;#d#
774 ::message ({ markup => "downloaded sound #$facenum" })
775 if $changed;
776 };
777 }
778
779
780 if ($changed) { # new data 778 if ($changed) { # new data
781 my ($meta_json, $data) = unpack "(w/a*)*", delete $face->{data}; 779 my ($meta_json, $data) = unpack "(w/a*)*", delete $face->{data};
782
783 my $put_cb = sub {
784 CFPlus::DB::put "res-metadata" => $face->{name} => $meta_json, sub {
785 $face->{meta} = $self->{json_coder}->decode ($meta_json); 780 $face->{meta} = $self->{json_coder}->decode ($meta_json);
786 $face->{path} = $path;
787 781
788 $cb->();
789 };
790 };
791
792 if (defined $path) {
793 CFPlus::DB::write_file $path, $data, $put_cb;
794 } else {
795 CFPlus::DB::put "res-data" => $face->{name} => $data, $put_cb; 782 CFPlus::DB::put res_data => $face->{name} => $data, sub { };
796 } 783 CFPlus::DB::put res_meta => $face->{name} => $meta_json, sub { };
797 } else { 784 } else {
798 $face->{meta} = $self->{json_coder}->decode (delete $face->{data}); 785 $face->{meta} = $self->{json_coder}->decode (delete $face->{data});
799 $face->{path} = $path;
800 $cb->();
801 } 786 }
787
788 ::add_license ($face);
789
790 if ($face->{type} == 3) { # FT_MUSIC
791 $self->{music_meta}{$facenum} = $face;
792
793 ::message ({ markup => "downloaded song #$facenum" })
794 if $changed;
795
796 &::audio_music_push ($facenum);
797 } elsif ($face->{type} == 5) { # FT_SOUND
798 $self->{sound_meta}{$facenum} = $face;
799
800 ::message ({ markup => "downloaded sound #$facenum" })
801 if $changed;
802
803 &::audio_sound_push ($facenum);
804 }
805
802 } 806 }
803} 807}
804 808
805sub smooth_update { 809sub smooth_update {
806 my ($self, $facenum, $face) = @_; 810 my ($self, $facenum, $face) = @_;
807 811
808 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel}); 812 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
809} 813}
810 814
811sub set_texture { 815sub have_tile {
812 my ($self, $id, $data) = @_; 816 my ($self, $tile) = @_;
813 817
814 return unless $self->{map}; 818 return unless $self->{map};
815 819
816 $self->{texture}[$id] = my $tex = 820 my $tex = $self->{texture}[$tile] ||=
817 new_from_image CFPlus::Texture 821 new CFPlus::Texture
818 $data, minify => 1, mipmap => 1; 822 tile => $tile, minify => 1, mipmap => 1, delay => 1;
819 823
820 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
821 $self->{map_widget}->update;
822
823 $_->() for @{(delete $self->{face_cb}{$id}) || []}; 824 $_->($tex) for @{(delete $self->{face_cb}{$tile}) || []};
824} 825}
825 826
826sub connect_face_update { 827sub connect_face_update {
827 my ($self, $id, $cb) = @_; 828 my ($self, $id, $cb) = @_;
828 829
834 @{$self->{face_cb}{$id}}; 835 @{$self->{face_cb}{$id}};
835 } 836 }
836} 837}
837 838
838sub sound_play { 839sub sound_play {
839 my ($self, $x, $y, $soundnum, $type) = @_; 840 my ($self, $type, $face, $dx, $dy, $vol) = @_;
840 warn "sound play <@_>\n";#d#
841 841
842 #$self->{sound_play}->($x, $y, $soundnum, $type); 842 &::audio_sound_play ($face, $dx, $dy, $vol)
843 unless $type & 1; # odd types are silent for future expansion
843} 844}
844 845
845my $LAST_QUERY; # server is stupid, stupid, stupid 846my $LAST_QUERY; # server is stupid, stupid, stupid
846 847
847sub query { 848sub query {
1023 } 1024 }
1024 1025
1025 $::FLOORBOX->add_at (@add); 1026 $::FLOORBOX->add_at (@add);
1026 }); 1027 });
1027 1028
1028 $::WANT_REFRESH++; 1029 $::WANT_REFRESH->start;
1029} 1030}
1030 1031
1031sub set_opencont { 1032sub set_opencont {
1032 my ($conn, $tag, $name) = @_; 1033 my ($conn, $tag, $name) = @_;
1033 $conn->{open_container} = $tag; 1034 $conn->{open_container} = $tag;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines