ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/cfplus
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/cfplus (file contents):
Revision 1.189 by root, Sun Jul 29 18:22:34 2007 UTC vs.
Revision 1.192 by root, Mon Jul 30 02:16:16 2007 UTC

71unshift @INC, $ENV{PAR_TEMP} 71unshift @INC, $ENV{PAR_TEMP}
72 if %PAR::LibCache; 72 if %PAR::LibCache;
73 73
74use Time::HiRes 'time'; 74use Time::HiRes 'time';
75use Event; 75use Event;
76use List::Util qw(max min);
76 77
77use Crossfire; 78use Crossfire;
78use Crossfire::Protocol::Constants; 79use Crossfire::Protocol::Constants;
79 80
80use Compress::LZF; 81use Compress::LZF;
162 163
163our $SDL_MIXER; 164our $SDL_MIXER;
164our $MUSIC_DEFAULT = "in_a_heartbeat.ogg"; 165our $MUSIC_DEFAULT = "in_a_heartbeat.ogg";
165our @MUSIC_WANT; 166our @MUSIC_WANT;
166our $MUSIC_START; 167our $MUSIC_START;
167our $MUSIC_PLAYING; 168our $MUSIC_PLAYING_DATA;
169our $MUSIC_PLAYING_META;
168our $MUSIC_PLAYER; 170our $MUSIC_PLAYER;
169our $MUSIC_RESUME = 30; # resume music when players less than these many seconds before 171our $MUSIC_RESUME = 30; # resume music when players less than these many seconds before
170our @SOUNDS; # event => file mapping 172our @SOUNDS; # event => file mapping
171our %AUDIO_CHUNKS; # audio files 173our %AUDIO_CHUNKS; # audio files
172 174
458 mapmap => $MAPMAP, 460 mapmap => $MAPMAP,
459 query => \&server_query, 461 query => \&server_query,
460 462
461 setup_req => { 463 setup_req => {
462 smoothing => $CFG->{map_smoothing}*1, 464 smoothing => $CFG->{map_smoothing}*1,
463 },
464
465 sound_play => sub {
466 my ($x, $y, $soundnum, $type) = @_;
467
468 $SDL_MIXER
469 or return;
470
471 my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]}
472 or return;
473
474 $chunk->play;
475 }, 465 },
476 }; 466 };
477 467
478 if ($CONN) { 468 if ($CONN) {
479 CFPlus::lowdelay fileno $CONN->{fh}; 469 CFPlus::lowdelay fileno $CONN->{fh};
1081 text => $CFG->{profile}{default}{host}, 1071 text => $CFG->{profile}{default}{host},
1082 tooltip => "The hostname or ip address of the Crossfire(+) server to connect to", 1072 tooltip => "The hostname or ip address of the Crossfire(+) server to connect to",
1083 on_changed => sub { 1073 on_changed => sub {
1084 my ($self, $value) = @_; 1074 my ($self, $value) = @_;
1085 $CFG->{profile}{default}{host} = $value; 1075 $CFG->{profile}{default}{host} = $value;
1086 0 1076 1
1087 } 1077 }
1088 ); 1078 );
1089 1079
1090 $vbox->add (new CFPlus::UI::Button 1080 $vbox->add (new CFPlus::UI::Button
1091 expand => 1, 1081 expand => 1,
1092 text => "Server List", 1082 text => "Server List",
1093 other => $METASERVER, 1083 other => $METASERVER,
1094 tooltip => "Show a list of available crossfire servers", 1084 tooltip => "Show a list of available crossfire servers",
1095 on_activate => sub { $METASERVER->toggle_visibility; 0 }, 1085 on_activate => sub { $METASERVER->toggle_visibility; 0 },
1096 on_visibility_change => sub { $METASERVER->hide unless $_[1]; 0 }, 1086 on_visibility_change => sub { $METASERVER->hide unless $_[1]; 1 },
1097 ); 1087 );
1098 } 1088 }
1099 1089
1100 $table->add_at (0, 4, new CFPlus::UI::Label valign => 0, align => 1, text => "Username"); 1090 $table->add_at (0, 4, new CFPlus::UI::Label valign => 0, align => 1, text => "Username");
1101 $table->add_at (1, 4, new CFPlus::UI::Entry 1091 $table->add_at (1, 4, new CFPlus::UI::Entry
1102 text => $CFG->{profile}{default}{user}, 1092 text => $CFG->{profile}{default}{user},
1103 tooltip => "The name of your character on the server", 1093 tooltip => "The name of your character on the server",
1104 on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{user} = $value } 1094 on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{user} = $value; 1 }
1105 ); 1095 );
1106 1096
1107 $table->add_at (0, 5, new CFPlus::UI::Label valign => 0, align => 1, text => "Password"); 1097 $table->add_at (0, 5, new CFPlus::UI::Label valign => 0, align => 1, text => "Password");
1108 $table->add_at (1, 5, new CFPlus::UI::Entry 1098 $table->add_at (1, 5, new CFPlus::UI::Entry
1109 text => $CFG->{profile}{default}{password}, 1099 text => $CFG->{profile}{default}{password},
1110 hidden => 1, 1100 hidden => 1,
1111 tooltip => "The password for your character", 1101 tooltip => "The password for your character",
1112 on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{password} = $value } 1102 on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{password} = $value; 1 }
1113 ); 1103 );
1114 1104
1115 $table->add_at (0, 7, new CFPlus::UI::Label valign => 0, align => 1, text => "Map Size"); 1105 $table->add_at (0, 7, new CFPlus::UI::Label valign => 0, align => 1, text => "Map Size");
1116 $table->add_at (1, 7, new CFPlus::UI::Slider 1106 $table->add_at (1, 7, new CFPlus::UI::Slider
1117 force_w => 100, 1107 force_w => 100,
1118 range => [$CFG->{mapsize}, 10, 100, 0, 1], 1108 range => [$CFG->{mapsize}, 10, 100, 0, 1],
1119 tooltip => "This is the size of the portion of the map update the server sends you. " 1109 tooltip => "This is the size of the portion of the map update the server sends you. "
1120 . "If you set this to a high value you will be able to see further, " 1110 . "If you set this to a high value you will be able to see further, "
1121 . "but you also increase bandwidth requirements and latency. " 1111 . "but you also increase bandwidth requirements and latency. "
1122 . "This option is only used once at log-in.", 1112 . "This option is only used once at log-in.",
1123 on_changed => sub { my ($self, $value) = @_; $CFG->{mapsize} = $self->{range}[0] = $value = int $value; 0 }, 1113 on_changed => sub { my ($self, $value) = @_; $CFG->{mapsize} = $self->{range}[0] = $value = int $value; 1 },
1124 ); 1114 );
1125 1115
1126 $table->add_at (0, 8, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Rate"); 1116 $table->add_at (0, 8, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Rate");
1127 $table->add_at (1, 8, new CFPlus::UI::Entry 1117 $table->add_at (1, 8, new CFPlus::UI::Entry
1128 text => $CFG->{output_rate}, 1118 text => $CFG->{output_rate},
1129 tooltip => "The approximate bandwidth in bytes per second that the server should not exceed " 1119 tooltip => "The maximum bandwidth in bytes per second that the server should not exceed "
1130 . "when sending images, to ensure interactiveness. When 0 or unset, the server " 1120 . "when sending data. When 0 or unset, the server "
1131 . "default will be used, which is usually around 100kb/s.", 1121 . "default will be used, which is usually around 100kb/s. Most servers will "
1122 . "dynamically find an optimal rate, so adjust this only when necessary.",
1132 on_changed => sub { $CFG->{output_rate} = $_[1]; 0 }, 1123 on_changed => sub { $CFG->{output_rate} = $_[1]; 1 },
1133 ); 1124 );
1134 1125
1135 $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Count"); 1126 $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Count");
1136 $table->add_at (1, 9, new CFPlus::UI::Entry 1127 $table->add_at (1, 9, new CFPlus::UI::Entry
1137 text => $CFG->{output_count}, 1128 text => $CFG->{output_count},
1138 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.", 1129 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.",
1139 on_changed => sub { $CFG->{output_count} = $_[1]; 0 }, 1130 on_changed => sub { $CFG->{output_count} = $_[1]; 1 },
1140 ); 1131 );
1141 1132
1142 $table->add_at (0, 10, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Sync"); 1133 $table->add_at (0, 10, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Sync");
1143 $table->add_at (1, 10, new CFPlus::UI::Entry 1134 $table->add_at (1, 10, new CFPlus::UI::Entry
1144 text => $CFG->{output_sync}, 1135 text => $CFG->{output_sync},
1145 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.", 1136 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.",
1146 on_changed => sub { $CFG->{output_sync} = $_[1]; 0 }, 1137 on_changed => sub { $CFG->{output_sync} = $_[1]; 1 },
1147 ); 1138 );
1148 1139
1149 $table->add_at (1, 11, $LOGIN_BUTTON = new CFPlus::UI::Button 1140 $table->add_at (1, 11, $LOGIN_BUTTON = new CFPlus::UI::Button
1150 expand => 1, 1141 expand => 1,
1151 align => 0, 1142 align => 0,
1152 text => "Login", 1143 text => "Login",
1153 on_activate => sub { 1144 on_activate => sub {
1154 $CONN ? stop_game 1145 $CONN ? stop_game
1155 : start_game; 1146 : start_game;
1156 0 1147 1
1157 }, 1148 },
1158 ); 1149 );
1159 1150
1160 $vbox->add (new CFPlus::UI::FancyFrame 1151 $vbox->add (new CFPlus::UI::FancyFrame
1161 label => "Server Info", 1152 label => "Server Info",
1799} 1790}
1800 1791
1801sub audio_channel_finished { 1792sub audio_channel_finished {
1802 my ($channel) = @_; 1793 my ($channel) = @_;
1803 1794
1804 #warn "channel $channel finished\n";#d# 1795# warn "channel $channel finished\n";#d#
1796}
1797
1798our %AUDIO_PLAY;
1799our %AUDIO_CHUNK;
1800
1801sub audio_sound_push($) {
1802 my ($face) = @_;
1803
1804 if (my $chunk = $AUDIO_CHUNK{$face}) {
1805 for (grep $_->[0] >= Event::time, @{(delete $AUDIO_PLAY{$face}) || []}) {
1806 my (undef, $dx, $dy, $vol) = @$_;
1807
1808 my $channel = $chunk->play
1809 or return;
1810
1811 $channel->volume (128 + $vol);
1812 $dx = $dx / 10 * 255;
1813 $channel->set_panning ((min 255, 255 - $dx), (min 255, 255 + $dx));
1814# my $angle = $dx ? : $dx < 0 ?
1815# my $distance = -$vol;
1816# $channel->set_position ($angle, $distance);
1817 }
1818 } else {
1819 # sound_meta not set means data is in flight either way
1820 my $meta = $CONN->{sound_meta}{$face}
1821 or return;
1822
1823 # fetch from database
1824 CFPlus::DB::get res_data => $meta->{name}, sub {
1825 my $vol = $meta->{meta}{volume} || 100;
1826 my $rwops = new CFPlus::RW $_[0];
1827 my $chunk = new CFPlus::MixChunk $rwops;
1828 $chunk->volume ($vol * 128 / 100);
1829 $AUDIO_CHUNK{$face} = $chunk;
1830
1831 audio_sound_push ($face);
1832 };
1833 }
1834}
1835
1836sub audio_sound_play {
1837 my ($face, $dx, $dy, $vol) = @_;
1838
1839 $SDL_MIXER
1840 or return;
1841
1842 my $queue = $AUDIO_PLAY{$face} ||= [];
1843 push @$queue, [Event::time + 0.2, $dx, $dy, $vol]; # delay sound by max. 0.2s
1844 audio_sound_push $face
1845 unless @$queue > 1;
1805} 1846}
1806 1847
1807sub audio_music_set { 1848sub audio_music_set {
1808 my ($songs) = @_; 1849 my ($songs) = @_;
1809 1850
1817 &audio_music_changed (); 1858 &audio_music_changed ();
1818 } 1859 }
1819} 1860}
1820 1861
1821sub audio_music_start { 1862sub audio_music_start {
1822 my $path = $MUSIC_PLAYING->{path} 1863 my $meta = $MUSIC_PLAYING_META;
1823 or return;
1824 1864
1825 CFPlus::DB::prefetch_file $path, 1024_000, sub { 1865 CFPlus::DB::get res_data => $meta->{name}, sub {
1826 return unless $SDL_MIXER; 1866 return unless $SDL_MIXER;
1827 1867
1828 # music might have changed... 1868 # music might have changed...
1829 $path eq $MUSIC_PLAYING->{path} 1869 $meta eq $MUSIC_PLAYING_META
1830 or return &audio_music_start (); 1870 or return &audio_music_start ();
1831 1871
1832 my $rwops = new_from_file CFPlus::RW $path; 1872 $MUSIC_PLAYING_DATA = \$_[0];
1873
1874 my $rwops = $meta->{path}
1875 ? new_from_file CFPlus::RW $meta->{path}
1876 : new CFPlus::RW $$MUSIC_PLAYING_DATA;
1877
1833 $MUSIC_PLAYER = new CFPlus::MixMusic $rwops; 1878 $MUSIC_PLAYER = new CFPlus::MixMusic $rwops
1879 or ((warn CFPlus::Mix_GetError), return); # pretty fatal error
1834 1880
1835 my $NOW = time; 1881 my $NOW = time;
1836 1882
1837 if ($MUSIC_PLAYING->{stop_time} > $NOW - $MUSIC_RESUME) { 1883 if ($MUSIC_PLAYING_META->{stop_time} > $NOW - $MUSIC_RESUME) {
1838 my $pos = $MUSIC_PLAYING->{stop_pos}; 1884 my $pos = $MUSIC_PLAYING_META->{stop_pos};
1839 $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos); 1885 $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos);
1840 $MUSIC_START = time - $pos; 1886 $MUSIC_START = time - $pos;
1841 } else { 1887 } else {
1842 $MUSIC_PLAYER->play (0); 1888 $MUSIC_PLAYER->play (0);
1843 $MUSIC_START = time; 1889 $MUSIC_START = time;
1844 } 1890 }
1845 1891
1846 delete $MUSIC_PLAYING->{stop_time}; 1892 delete $MUSIC_PLAYING_META->{stop_time};
1847 delete $MUSIC_PLAYING->{stop_pos}; 1893 delete $MUSIC_PLAYING_META->{stop_pos};
1848 } 1894 }
1849} 1895}
1850 1896
1851sub audio_music_changed { 1897sub audio_music_changed {
1852 return unless $CFG->{bgm_enable}; 1898 return unless $CFG->{bgm_enable};
1854 1900
1855 # default MUSIC_WANT == MUSIC_DEFAULT 1901 # default MUSIC_WANT == MUSIC_DEFAULT
1856 @MUSIC_WANT = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_WANT; 1902 @MUSIC_WANT = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_WANT;
1857 1903
1858 # if the currently playing song is acceptable, let it continue 1904 # if the currently playing song is acceptable, let it continue
1859 return if $MUSIC_PLAYING 1905 return if $MUSIC_PLAYING_META
1860 && grep $MUSIC_PLAYING->{path} eq $_->{path}, @MUSIC_WANT; 1906 && grep $MUSIC_PLAYING_META == $_, @MUSIC_WANT;
1861 1907
1862 my $NOW = time; 1908 my $NOW = time;
1863 1909
1864 if ($MUSIC_PLAYING) { 1910 if ($MUSIC_PLAYING_META) {
1865 $MUSIC_PLAYING->{stop_time} = $NOW; 1911 $MUSIC_PLAYING_META->{stop_time} = $NOW;
1866 $MUSIC_PLAYING->{stop_pos} = $NOW - $MUSIC_START; 1912 $MUSIC_PLAYING_META->{stop_pos} = $NOW - $MUSIC_START;
1867 CFPlus::MixMusic::fade_out 1000; 1913 CFPlus::MixMusic::fade_out 1000;
1868 } else { 1914 } else {
1869 # sort by stop time, oldest first 1915 # sort by stop time, oldest first
1870 @MUSIC_WANT = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_WANT; 1916 @MUSIC_WANT = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_WANT;
1871 1917
1872 # if the most recently-played piece played very recently, 1918 # if the most recently-played piece played very recently,
1873 # resume it, else choose the oldest piece for rotation. 1919 # resume it, else choose the oldest piece for rotation.
1874 $MUSIC_PLAYING = 1920 $MUSIC_PLAYING_META =
1875 $MUSIC_WANT[-1]{stop_time} > $NOW - $MUSIC_RESUME 1921 $MUSIC_WANT[-1]{stop_time} > $NOW - $MUSIC_RESUME
1876 ? $MUSIC_WANT[-1] 1922 ? $MUSIC_WANT[-1]
1877 : $MUSIC_WANT[0]; 1923 : $MUSIC_WANT[0];
1878 1924
1879 audio_music_start; 1925 audio_music_start;
1880 } 1926 }
1881} 1927}
1882 1928
1883sub audio_music_finished { 1929sub audio_music_finished {
1884 $MUSIC_PLAYING = undef;
1885 undef $MUSIC_PLAYER; 1930 undef $MUSIC_PLAYER;
1931 undef $MUSIC_PLAYING_META;
1932 undef $MUSIC_PLAYING_DATA;
1886 1933
1887 audio_music_changed; 1934 audio_music_changed;
1888} 1935}
1889 1936
1890sub audio_init { 1937sub audio_init {
1891 if ($CFG->{audio_enable}) { 1938 if ($CFG->{audio_enable}) {
1892 if (open my $fh, "<", CFPlus::find_rcfile "sounds/config") {
1893 $ENV{MIX_EFFECTSMAXSPEED} = 1; 1939 $ENV{MIX_EFFECTSMAXSPEED} = 1;
1894 $SDL_MIXER = !CFPlus::Mix_OpenAudio; 1940 $SDL_MIXER = !CFPlus::Mix_OpenAudio;
1895 1941
1896 unless ($SDL_MIXER) { 1942 unless ($SDL_MIXER) {
1897 status "Unable to open sound device: there will be no sound"; 1943 status "Unable to open sound device: there will be no sound";
1898 return; 1944 return;
1899 }
1900
1901 CFPlus::Mix_AllocateChannels 8;
1902 CFPlus::MixMusic::volume $CFG->{bgm_volume} * 128;
1903
1904 audio_music_finished;
1905
1906 local $_;
1907 while (<$fh>) {
1908 next if /^\s*#/;
1909 next if /^\s*$/;
1910
1911 my ($file, $volume, $event) = split /\s+/, $_, 3;
1912
1913 push @SOUNDS, "$volume,$file";
1914
1915 $AUDIO_CHUNKS{"$volume,$file"} ||= do {
1916 my $rwops = new_from_file CFPlus::RW CFPlus::find_rcfile "sounds/$file";
1917 my $chunk = new CFPlus::MixChunk $rwops;
1918 $chunk->volume ($volume * 128 / 100);
1919 $chunk
1920 };
1921 }
1922 } else {
1923 status "unable to open sound config: $!";
1924 } 1945 }
1946
1947 CFPlus::Mix_AllocateChannels 16;
1948 CFPlus::MixMusic::volume $CFG->{bgm_volume} * 128;
1949
1950 audio_music_finished;
1925 } else { 1951 } else {
1926 undef $SDL_MIXER; 1952 undef $SDL_MIXER;
1927 } 1953 }
1928} 1954}
1929 1955

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines