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.187 by elmex, Fri Jul 27 18:04:43 2007 UTC vs.
Revision 1.191 by root, Sun Jul 29 21:31:48 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};
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; 0 },
1124 ); 1114 );
1125 1115
1126 $table->add_at (0, 8, new CFPlus::UI::Label valign => 0, align => 1, text => "Face Prefetch");
1127 $table->add_at (1, 8, new CFPlus::UI::CheckBox
1128 state => $CFG->{face_prefetch},
1129 tooltip => "<b>Background Image Prefetch</b>\n\n"
1130 . "If enabled, the client automatically pre-fetches images from the server. "
1131 . "This might increase or create lag, but increases the chances "
1132 . "of faces being ready for display when you encounter them. "
1133 . "It also uses up server bandwidth on every connect, "
1134 . "so only set it if you really need to prefetch images. "
1135 . "This option can be set and unset any time.",
1136 on_changed => sub { $CFG->{face_prefetch} = $_[1]; 0 },
1137 );
1138
1139 $table->add_at (0, 9, 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");
1140 $table->add_at (1, 9, new CFPlus::UI::Entry 1117 $table->add_at (1, 8, new CFPlus::UI::Entry
1141 text => $CFG->{output_rate}, 1118 text => $CFG->{output_rate},
1142 tooltip => "The approximate bandwidth in bytes per second that the server should not exceed " 1119 tooltip => "The approximate bandwidth in bytes per second that the server should not exceed "
1143 . "when sending images, to ensure interactiveness. When 0 or unset, the server " 1120 . "when sending images, to ensure interactiveness. When 0 or unset, the server "
1144 . "default will be used, which is usually around 100kb/s.", 1121 . "default will be used, which is usually around 100kb/s.",
1145 on_changed => sub { $CFG->{output_rate} = $_[1]; 0 }, 1122 on_changed => sub { $CFG->{output_rate} = $_[1]; 0 },
1146 ); 1123 );
1147 1124
1148 $table->add_at (0, 10, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Count"); 1125 $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Count");
1149 $table->add_at (1, 10, new CFPlus::UI::Entry 1126 $table->add_at (1, 9, new CFPlus::UI::Entry
1150 text => $CFG->{output_count}, 1127 text => $CFG->{output_count},
1151 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.", 1128 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.",
1152 on_changed => sub { $CFG->{output_count} = $_[1]; 0 }, 1129 on_changed => sub { $CFG->{output_count} = $_[1]; 0 },
1153 ); 1130 );
1154 1131
1155 $table->add_at (0, 11, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Sync"); 1132 $table->add_at (0, 10, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Sync");
1156 $table->add_at (1, 11, new CFPlus::UI::Entry 1133 $table->add_at (1, 10, new CFPlus::UI::Entry
1157 text => $CFG->{output_sync}, 1134 text => $CFG->{output_sync},
1158 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.", 1135 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.",
1159 on_changed => sub { $CFG->{output_sync} = $_[1]; 0 }, 1136 on_changed => sub { $CFG->{output_sync} = $_[1]; 0 },
1160 ); 1137 );
1161 1138
1162 $table->add_at (1, 12, $LOGIN_BUTTON = new CFPlus::UI::Button 1139 $table->add_at (1, 11, $LOGIN_BUTTON = new CFPlus::UI::Button
1163 expand => 1, 1140 expand => 1,
1164 align => 0, 1141 align => 0,
1165 text => "Login", 1142 text => "Login",
1166 on_activate => sub { 1143 on_activate => sub {
1167 $CONN ? stop_game 1144 $CONN ? stop_game
1812} 1789}
1813 1790
1814sub audio_channel_finished { 1791sub audio_channel_finished {
1815 my ($channel) = @_; 1792 my ($channel) = @_;
1816 1793
1817 #warn "channel $channel finished\n";#d# 1794# warn "channel $channel finished\n";#d#
1795}
1796
1797our %AUDIO_PLAY;
1798our %AUDIO_CHUNK;
1799
1800sub audio_sound_push($) {
1801 my ($face) = @_;
1802
1803 if (my $chunk = $AUDIO_CHUNK{$face}) {
1804 for (grep $_->[0] >= Event::time, @{(delete $AUDIO_PLAY{$face}) || []}) {
1805 my (undef, $dx, $dy, $vol) = @$_;
1806
1807 my $channel = $chunk->play
1808 or return;
1809
1810 $channel->volume (128 + $vol);
1811 $dx = $dx / 10 * 255;
1812 $channel->set_panning ((min 255, 255 - $dx), (min 255, 255 + $dx));
1813# my $angle = $dx ? : $dx < 0 ?
1814# my $distance = -$vol;
1815# $channel->set_position ($angle, $distance);
1816 }
1817 } else {
1818 # sound_meta not set means data is in flight either way
1819 my $meta = $CONN->{sound_meta}{$face}
1820 or return;
1821
1822 # fetch from database
1823 CFPlus::DB::get res_data => $meta->{name}, sub {
1824 my $vol = $meta->{meta}{volume} || 100;
1825 my $rwops = new CFPlus::RW $_[0];
1826 my $chunk = new CFPlus::MixChunk $rwops;
1827 $chunk->volume ($vol * 128 / 100);
1828 $AUDIO_CHUNK{$face} = $chunk;
1829
1830 audio_sound_push ($face);
1831 };
1832 }
1833}
1834
1835sub audio_sound_play {
1836 my ($face, $dx, $dy, $vol) = @_;
1837
1838 $SDL_MIXER
1839 or return;
1840
1841 my $queue = $AUDIO_PLAY{$face} ||= [];
1842 push @$queue, [Event::time + 0.2, $dx, $dy, $vol]; # delay sound by max. 0.2s
1843 audio_sound_push $face
1844 unless @$queue > 1;
1818} 1845}
1819 1846
1820sub audio_music_set { 1847sub audio_music_set {
1821 my ($songs) = @_; 1848 my ($songs) = @_;
1822 1849
1830 &audio_music_changed (); 1857 &audio_music_changed ();
1831 } 1858 }
1832} 1859}
1833 1860
1834sub audio_music_start { 1861sub audio_music_start {
1835 my $path = $MUSIC_PLAYING->{path} 1862 my $meta = $MUSIC_PLAYING_META;
1836 or return;
1837 1863
1838 CFPlus::DB::prefetch_file $path, 1024_000, sub { 1864 CFPlus::DB::get res_data => $meta->{name}, sub {
1839 return unless $SDL_MIXER; 1865 return unless $SDL_MIXER;
1840 1866
1841 # music might have changed... 1867 # music might have changed...
1842 $path eq $MUSIC_PLAYING->{path} 1868 $meta eq $MUSIC_PLAYING_META
1843 or return &audio_music_start (); 1869 or return &audio_music_start ();
1844 1870
1871 $MUSIC_PLAYING_DATA = \$_[0];
1872
1873 my $rwops = $meta->{path}
1874 ? new_from_file CFPlus::RW $meta->{path}
1875 : new CFPlus::RW $$MUSIC_PLAYING_DATA;
1876
1845 $MUSIC_PLAYER = new_from_file CFPlus::MixMusic $path; 1877 $MUSIC_PLAYER = new CFPlus::MixMusic $rwops
1878 or ((warn CFPlus::Mix_GetError), return); # pretty fatal error
1846 1879
1847 my $NOW = time; 1880 my $NOW = time;
1848 1881
1849 if ($MUSIC_PLAYING->{stop_time} > $NOW - $MUSIC_RESUME) { 1882 if ($MUSIC_PLAYING_META->{stop_time} > $NOW - $MUSIC_RESUME) {
1850 my $pos = $MUSIC_PLAYING->{stop_pos}; 1883 my $pos = $MUSIC_PLAYING_META->{stop_pos};
1851 $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos); 1884 $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos);
1852 $MUSIC_START = time - $pos; 1885 $MUSIC_START = time - $pos;
1853 } else { 1886 } else {
1854 $MUSIC_PLAYER->play (0); 1887 $MUSIC_PLAYER->play (0);
1855 $MUSIC_START = time; 1888 $MUSIC_START = time;
1856 } 1889 }
1857 1890
1858 delete $MUSIC_PLAYING->{stop_time}; 1891 delete $MUSIC_PLAYING_META->{stop_time};
1859 delete $MUSIC_PLAYING->{stop_pos}; 1892 delete $MUSIC_PLAYING_META->{stop_pos};
1860 } 1893 }
1861} 1894}
1862 1895
1863sub audio_music_changed { 1896sub audio_music_changed {
1864 return unless $CFG->{bgm_enable}; 1897 return unless $CFG->{bgm_enable};
1866 1899
1867 # default MUSIC_WANT == MUSIC_DEFAULT 1900 # default MUSIC_WANT == MUSIC_DEFAULT
1868 @MUSIC_WANT = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_WANT; 1901 @MUSIC_WANT = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_WANT;
1869 1902
1870 # if the currently playing song is acceptable, let it continue 1903 # if the currently playing song is acceptable, let it continue
1871 return if $MUSIC_PLAYING 1904 return if $MUSIC_PLAYING_META
1872 && grep $MUSIC_PLAYING->{path} eq $_->{path}, @MUSIC_WANT; 1905 && grep $MUSIC_PLAYING_META == $_, @MUSIC_WANT;
1873 1906
1874 my $NOW = time; 1907 my $NOW = time;
1875 1908
1876 if ($MUSIC_PLAYING) { 1909 if ($MUSIC_PLAYING_META) {
1877 $MUSIC_PLAYING->{stop_time} = $NOW; 1910 $MUSIC_PLAYING_META->{stop_time} = $NOW;
1878 $MUSIC_PLAYING->{stop_pos} = $NOW - $MUSIC_START; 1911 $MUSIC_PLAYING_META->{stop_pos} = $NOW - $MUSIC_START;
1879 CFPlus::MixMusic::fade_out 1000; 1912 CFPlus::MixMusic::fade_out 1000;
1880 } else { 1913 } else {
1881 # sort by stop time, oldest first 1914 # sort by stop time, oldest first
1882 @MUSIC_WANT = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_WANT; 1915 @MUSIC_WANT = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_WANT;
1883 1916
1884 # if the most recently-played piece played very recently, 1917 # if the most recently-played piece played very recently,
1885 # resume it, else choose the oldest piece for rotation. 1918 # resume it, else choose the oldest piece for rotation.
1886 $MUSIC_PLAYING = 1919 $MUSIC_PLAYING_META =
1887 $MUSIC_WANT[-1]{stop_time} > $NOW - $MUSIC_RESUME 1920 $MUSIC_WANT[-1]{stop_time} > $NOW - $MUSIC_RESUME
1888 ? $MUSIC_WANT[-1] 1921 ? $MUSIC_WANT[-1]
1889 : $MUSIC_WANT[0]; 1922 : $MUSIC_WANT[0];
1890 1923
1891 audio_music_start; 1924 audio_music_start;
1892 } 1925 }
1893} 1926}
1894 1927
1895sub audio_music_finished { 1928sub audio_music_finished {
1896 $MUSIC_PLAYING = undef;
1897 undef $MUSIC_PLAYER; 1929 undef $MUSIC_PLAYER;
1930 undef $MUSIC_PLAYING_META;
1931 undef $MUSIC_PLAYING_DATA;
1898 1932
1899 audio_music_changed; 1933 audio_music_changed;
1900} 1934}
1901 1935
1902sub audio_init { 1936sub audio_init {
1903 if ($CFG->{audio_enable}) { 1937 if ($CFG->{audio_enable}) {
1904 if (open my $fh, "<", CFPlus::find_rcfile "sounds/config") { 1938 $ENV{MIX_EFFECTSMAXSPEED} = 1;
1905 $SDL_MIXER = !CFPlus::Mix_OpenAudio; 1939 $SDL_MIXER = !CFPlus::Mix_OpenAudio;
1906 1940
1907 unless ($SDL_MIXER) { 1941 unless ($SDL_MIXER) {
1908 status "Unable to open sound device: there will be no sound"; 1942 status "Unable to open sound device: there will be no sound";
1909 return; 1943 return;
1910 }
1911
1912 CFPlus::Mix_AllocateChannels 8;
1913 CFPlus::MixMusic::volume $CFG->{bgm_volume} * 128;
1914
1915 audio_music_finished;
1916
1917 local $_;
1918 while (<$fh>) {
1919 next if /^\s*#/;
1920 next if /^\s*$/;
1921
1922 my ($file, $volume, $event) = split /\s+/, $_, 3;
1923
1924 push @SOUNDS, "$volume,$file";
1925
1926 $AUDIO_CHUNKS{"$volume,$file"} ||= do {
1927 my $chunk = new_from_file CFPlus::MixChunk CFPlus::find_rcfile "sounds/$file";
1928 $chunk->volume ($volume * 128 / 100);
1929 $chunk
1930 };
1931 }
1932 } else {
1933 status "unable to open sound config: $!";
1934 } 1944 }
1945
1946 CFPlus::Mix_AllocateChannels 16;
1947 CFPlus::MixMusic::volume $CFG->{bgm_volume} * 128;
1948
1949 audio_music_finished;
1935 } else { 1950 } else {
1936 undef $SDL_MIXER; 1951 undef $SDL_MIXER;
1937 } 1952 }
1938} 1953}
1939 1954
1989 2004
1990sub animation_stop { 2005sub animation_stop {
1991 my ($widget) = @_; 2006 my ($widget) = @_;
1992 delete $animate_object{$widget}; 2007 delete $animate_object{$widget};
1993} 2008}
1994
1995# check once/second for faces that need to be prefetched
1996# this should, of course, only run on demand, but
1997# SDL forces worse things on us....
1998
1999Event->timer (after => 1, interval => 0.25, cb => sub {
2000 $CONN->face_prefetch
2001 if $CONN;
2002});
2003 2009
2004%SDL_CB = ( 2010%SDL_CB = (
2005 CFPlus::SDL_QUIT => sub { 2011 CFPlus::SDL_QUIT => sub {
2006 exit; 2012 exit;
2007 }, 2013 },
2068 stat_fontsize => 0.7, 2074 stat_fontsize => 0.7,
2069 mapsize => 100, 2075 mapsize => 100,
2070 audio_enable => 1, 2076 audio_enable => 1,
2071 bgm_enable => 1, 2077 bgm_enable => 1,
2072 bgm_volume => 0.25, 2078 bgm_volume => 0.25,
2073 face_prefetch => 0,
2074 output_sync => 1, 2079 output_sync => 1,
2075 output_count => 1, 2080 output_count => 1,
2076 output_rate => "", 2081 output_rate => "",
2077 pickup => 0, 2082 pickup => 0,
2078 inv_sort => "mtime", 2083 inv_sort => "mtime",

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines