… | |
… | |
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 | |
|
|
224 | sub 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 | |
230 | sub logprint { |
233 | sub 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 | |
252 | sub _stat_numdiff { |
239 | sub _stat_numdiff { |
253 | my ($self, $name, $old, $new) = @_; |
240 | my ($self, $name, $old, $new) = @_; |
254 | |
241 | |
… | |
… | |
562 | do { |
549 | do { |
563 | $id = pop @{$self->{bg_fetch}} |
550 | $id = pop @{$self->{bg_fetch}} |
564 | or return; |
551 | or return; |
565 | } while $self->{texture}[$id]; |
552 | } while $self->{texture}[$id]; |
566 | |
553 | |
567 | CFPlus::DB::get tilecache => $id, sub { |
554 | CFPlus::DB::exists tilecache => $id, sub { |
568 | my ($data) = @_; |
555 | my ($exists) = @_; |
569 | |
556 | |
570 | return unless $self->{map}; # stop when destroyed |
557 | return unless $self->{map}; # stop when destroyed |
571 | |
558 | |
572 | $self->set_texture ($id => $data) |
559 | $self->have_tile ($id) |
573 | if defined $data; |
560 | if $exists; |
574 | |
561 | |
575 | $self->bg_fetch; |
562 | $self->bg_fetch; |
576 | }; |
563 | }; |
577 | } |
564 | } |
578 | |
565 | |
… | |
… | |
735 | |
722 | |
736 | CFPlus::DB::get tilecache => $id, $cb; |
723 | CFPlus::DB::get tilecache => $id, $cb; |
737 | |
724 | |
738 | } elsif ($face->{type} & 1) { # with metadata |
725 | } 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." }); |
726 | #::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; |
727 | CFPlus::DB::get res_meta => $face->{name}, $cb; |
741 | } |
728 | } |
742 | } |
729 | } |
743 | |
730 | |
744 | sub face_update { |
731 | sub face_update { |
745 | my ($self, $facenum, $face, $changed) = @_; |
732 | my ($self, $facenum, $face, $changed) = @_; |
746 | |
733 | |
747 | if ($face->{type} == 0) { # FT_FACE |
734 | if ($face->{type} == 0) { # FT_FACE |
748 | CFPlus::DB::put tilecache => $face->{id} => $face->{data}, sub { } |
735 | CFPlus::DB::put tilecache => $face->{id} => $face->{data}, sub { } |
749 | if $changed; |
736 | if $changed; |
750 | |
737 | |
751 | $self->set_texture ($face->{id} => delete $face->{data}); |
738 | $self->have_tile ($face->{id}); |
752 | |
739 | |
753 | } elsif ($face->{type} & 1) { # split metadata |
740 | } 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 | } |
|
|
766 | |
|
|
767 | if ($changed) { # new data |
741 | if ($changed) { # new data |
768 | my ($meta_json, $data) = unpack "(w/a*)*", delete $face->{data}; |
742 | my ($meta_json, $data) = unpack "(w/a*)*", delete $face->{data}; |
769 | my $path = CFPlus::DB::path_of_res $face->{name}; |
|
|
770 | |
|
|
771 | CFPlus::DB::write_file $path, $data, sub { |
|
|
772 | CFPlus::DB::put "res-metadata" => $face->{name} => $meta_json, sub { |
|
|
773 | $face->{meta} = $self->{json_coder}->decode ($meta_json); |
743 | $face->{meta} = $self->{json_coder}->decode ($meta_json); |
774 | $face->{path} = $path; |
|
|
775 | |
744 | |
776 | $cb->(); |
745 | CFPlus::DB::put res_data => $face->{name} => $data, sub { }; |
777 | }; |
746 | CFPlus::DB::put res_meta => $face->{name} => $meta_json, sub { }; |
778 | }; |
|
|
779 | } else { |
747 | } else { |
780 | $face->{meta} = $self->{json_coder}->decode (delete $face->{data}); |
748 | $face->{meta} = $self->{json_coder}->decode (delete $face->{data}); |
781 | $cb->(); |
|
|
782 | } |
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 | |
783 | } |
769 | } |
784 | } |
770 | } |
785 | |
771 | |
786 | sub smooth_update { |
772 | sub smooth_update { |
787 | my ($self, $facenum, $face) = @_; |
773 | my ($self, $facenum, $face) = @_; |
788 | |
774 | |
789 | $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel}); |
775 | $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel}); |
790 | } |
776 | } |
791 | |
777 | |
792 | sub set_texture { |
778 | sub have_tile { |
793 | my ($self, $id, $data) = @_; |
779 | my ($self, $tile) = @_; |
794 | |
780 | |
795 | return unless $self->{map}; |
781 | return unless $self->{map}; |
796 | |
782 | |
797 | $self->{texture}[$id] = my $tex = |
783 | $self->{texture}[$tile] ||= my $tex = |
798 | new_from_image CFPlus::Texture |
784 | new CFPlus::Texture |
799 | $data, minify => 1, mipmap => 1; |
785 | tile => $tile, minify => 1, mipmap => 1, delay => 1; |
800 | |
786 | |
801 | $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
|
|
802 | $self->{map_widget}->update; |
|
|
803 | |
|
|
804 | $_->() for @{(delete $self->{face_cb}{$id}) || []}; |
787 | $_->() for @{(delete $self->{face_cb}{$tile}) || []}; |
805 | } |
788 | } |
806 | |
789 | |
807 | sub connect_face_update { |
790 | sub connect_face_update { |
808 | my ($self, $id, $cb) = @_; |
791 | my ($self, $id, $cb) = @_; |
809 | |
792 | |
… | |
… | |
815 | @{$self->{face_cb}{$id}}; |
798 | @{$self->{face_cb}{$id}}; |
816 | } |
799 | } |
817 | } |
800 | } |
818 | |
801 | |
819 | sub sound_play { |
802 | sub sound_play { |
820 | my ($self, $x, $y, $soundnum, $type) = @_; |
803 | my ($self, $type, $face, $dx, $dy, $vol) = @_; |
821 | warn "sound play <@_>\n";#d# |
|
|
822 | |
804 | |
823 | #$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 |
824 | } |
807 | } |
825 | |
808 | |
826 | my $LAST_QUERY; # server is stupid, stupid, stupid |
809 | my $LAST_QUERY; # server is stupid, stupid, stupid |
827 | |
810 | |
828 | sub query { |
811 | sub query { |
… | |
… | |
893 | $self->{$type} = $text; |
876 | $self->{$type} = $text; |
894 | } else { |
877 | } else { |
895 | $self->logprint ("msg: ", $text); |
878 | $self->logprint ("msg: ", $text); |
896 | return if $color < 0; # negative color == ignore if not understood |
879 | return if $color < 0; # negative color == ignore if not understood |
897 | |
880 | |
898 | my $fg = $CF_COLOR[$color % @CF_COLOR]; |
881 | my $fg = $CF_COLOR[$color & 0x1f] || [1, 0, 0]; |
899 | |
882 | |
900 | ## 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 |
901 | # no longer neecssary with TRT servers |
884 | # no longer neecssary with TRT servers |
902 | #$text =~ s/(?<=\S)\n(?=\w)/ /g; |
885 | #$text =~ s/(?<=\S)\n(?=\w)/ /g; |
903 | |
886 | |
… | |
… | |
964 | my ($self) = @_; |
947 | my ($self) = @_; |
965 | |
948 | |
966 | $self->{map_widget}->clr_commands; |
949 | $self->{map_widget}->clr_commands; |
967 | |
950 | |
968 | ::stop_game (); |
951 | ::stop_game (); |
969 | } |
|
|
970 | |
|
|
971 | sub image_info { |
|
|
972 | my ($self, $numfaces) = @_; |
|
|
973 | |
|
|
974 | $self->{num_faces} = $numfaces; |
|
|
975 | $self->{face_prefetch} = [1 .. $numfaces]; |
|
|
976 | $self->face_prefetch; |
|
|
977 | } |
|
|
978 | |
|
|
979 | sub face_prefetch { |
|
|
980 | my ($self) = @_; |
|
|
981 | |
|
|
982 | return unless $::CFG->{face_prefetch}; |
|
|
983 | |
|
|
984 | if ($self->{num_faces}) { |
|
|
985 | return if @{ $self->{send_queue} || [] }; |
|
|
986 | my $todo = @{ $self->{face_prefetch} } |
|
|
987 | or return; |
|
|
988 | |
|
|
989 | my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, (); |
|
|
990 | |
|
|
991 | $self->send ("requestinfo image_sums $face $face"); |
|
|
992 | |
|
|
993 | $self->{statusbox}->add (CFPlus::asxml "prefetching $todo", |
|
|
994 | group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]); |
|
|
995 | } elsif (!exists $self->{num_faces}) { |
|
|
996 | $self->send ("requestinfo image_info"); |
|
|
997 | |
|
|
998 | $self->{num_faces} = 0; |
|
|
999 | |
|
|
1000 | $self->{statusbox}->add (CFPlus::asxml "starting to prefetch", |
|
|
1001 | group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]); |
|
|
1002 | } |
|
|
1003 | } |
952 | } |
1004 | |
953 | |
1005 | sub update_floorbox { |
954 | sub update_floorbox { |
1006 | $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub { |
955 | $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub { |
1007 | return unless $::CONN; |
956 | return unless $::CONN; |