--- deliantra/Deliantra-Client/bin/cfplus 2007/07/27 18:04:43 1.187 +++ deliantra/Deliantra-Client/bin/cfplus 2007/07/31 00:50:04 1.194 @@ -73,6 +73,7 @@ use Time::HiRes 'time'; use Event; +use List::Util qw(max min); use Crossfire; use Crossfire::Protocol::Constants; @@ -164,11 +165,12 @@ our $MUSIC_DEFAULT = "in_a_heartbeat.ogg"; our @MUSIC_WANT; our $MUSIC_START; -our $MUSIC_PLAYING; +our $MUSIC_PLAYING_DATA; +our $MUSIC_PLAYING_META; our $MUSIC_PLAYER; our $MUSIC_RESUME = 30; # resume music when players less than these many seconds before our @SOUNDS; # event => file mapping -our %AUDIO_CHUNKS; # audio files +our %AUDIO_CHUNK; # audio files our $ALT_ENTER_MESSAGE; our $STATUSBOX; @@ -461,18 +463,6 @@ setup_req => { smoothing => $CFG->{map_smoothing}*1, }, - - sound_play => sub { - my ($x, $y, $soundnum, $type) = @_; - - $SDL_MIXER - or return; - - my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]} - or return; - - $chunk->play; - }, }; if ($CONN) { @@ -493,7 +483,7 @@ $SPELL_LIST->clear_spells; $CFPlus::UI::ROOT->emit (stop_game => ! ! $CONN); - &audio_music_set ([]); + &audio_music_set_ambient ([]); return unless $CONN; @@ -1083,7 +1073,7 @@ on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{host} = $value; - 0 + 1 } ); @@ -1093,7 +1083,7 @@ other => $METASERVER, tooltip => "Show a list of available crossfire servers", on_activate => sub { $METASERVER->toggle_visibility; 0 }, - on_visibility_change => sub { $METASERVER->hide unless $_[1]; 0 }, + on_visibility_change => sub { $METASERVER->hide unless $_[1]; 1 }, ); } @@ -1101,7 +1091,7 @@ $table->add_at (1, 4, new CFPlus::UI::Entry text => $CFG->{profile}{default}{user}, tooltip => "The name of your character on the server", - on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{user} = $value } + on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{user} = $value; 1 } ); $table->add_at (0, 5, new CFPlus::UI::Label valign => 0, align => 1, text => "Password"); @@ -1109,7 +1099,7 @@ text => $CFG->{profile}{default}{password}, hidden => 1, tooltip => "The password for your character", - on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{password} = $value } + on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{password} = $value; 1 } ); $table->add_at (0, 7, new CFPlus::UI::Label valign => 0, align => 1, text => "Map Size"); @@ -1120,53 +1110,41 @@ . "If you set this to a high value you will be able to see further, " . "but you also increase bandwidth requirements and latency. " . "This option is only used once at log-in.", - on_changed => sub { my ($self, $value) = @_; $CFG->{mapsize} = $self->{range}[0] = $value = int $value; 0 }, + on_changed => sub { my ($self, $value) = @_; $CFG->{mapsize} = $self->{range}[0] = $value = int $value; 1 }, ); - $table->add_at (0, 8, new CFPlus::UI::Label valign => 0, align => 1, text => "Face Prefetch"); - $table->add_at (1, 8, new CFPlus::UI::CheckBox - state => $CFG->{face_prefetch}, - tooltip => "Background Image Prefetch\n\n" - . "If enabled, the client automatically pre-fetches images from the server. " - . "This might increase or create lag, but increases the chances " - . "of faces being ready for display when you encounter them. " - . "It also uses up server bandwidth on every connect, " - . "so only set it if you really need to prefetch images. " - . "This option can be set and unset any time.", - on_changed => sub { $CFG->{face_prefetch} = $_[1]; 0 }, - ); - - $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Rate"); - $table->add_at (1, 9, new CFPlus::UI::Entry + $table->add_at (0, 8, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Rate"); + $table->add_at (1, 8, new CFPlus::UI::Entry text => $CFG->{output_rate}, - tooltip => "The approximate bandwidth in bytes per second that the server should not exceed " - . "when sending images, to ensure interactiveness. When 0 or unset, the server " - . "default will be used, which is usually around 100kb/s.", - on_changed => sub { $CFG->{output_rate} = $_[1]; 0 }, + tooltip => "The maximum bandwidth in bytes per second that the server should not exceed " + . "when sending data. When 0 or unset, the server " + . "default will be used, which is usually around 100kb/s. Most servers will " + . "dynamically find an optimal rate, so adjust this only when necessary.", + on_changed => sub { $CFG->{output_rate} = $_[1]; 1 }, ); - $table->add_at (0, 10, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Count"); - $table->add_at (1, 10, new CFPlus::UI::Entry + $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Count"); + $table->add_at (1, 9, new CFPlus::UI::Entry text => $CFG->{output_count}, tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.", - on_changed => sub { $CFG->{output_count} = $_[1]; 0 }, + on_changed => sub { $CFG->{output_count} = $_[1]; 1 }, ); - $table->add_at (0, 11, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Sync"); - $table->add_at (1, 11, new CFPlus::UI::Entry + $table->add_at (0, 10, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Sync"); + $table->add_at (1, 10, new CFPlus::UI::Entry text => $CFG->{output_sync}, tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.", - on_changed => sub { $CFG->{output_sync} = $_[1]; 0 }, + on_changed => sub { $CFG->{output_sync} = $_[1]; 1 }, ); - $table->add_at (1, 12, $LOGIN_BUTTON = new CFPlus::UI::Button + $table->add_at (1, 11, $LOGIN_BUTTON = new CFPlus::UI::Button expand => 1, align => 0, text => "Login", on_activate => sub { $CONN ? stop_game : start_game; - 0 + 1 }, ); @@ -1814,10 +1792,61 @@ sub audio_channel_finished { my ($channel) = @_; - #warn "channel $channel finished\n";#d# +# warn "channel $channel finished\n";#d# } -sub audio_music_set { +our %AUDIO_PLAY; +our %AUDIO_CHUNK; + +sub audio_sound_push($) { + my ($face) = @_; + + if (my $chunk = $AUDIO_CHUNK{$face}) { + for (grep $_->[0] >= Event::time, @{(delete $AUDIO_PLAY{$face}) || []}) { + my (undef, $dx, $dy, $vol) = @$_; + + my $channel = CFPlus::Channel::find; + $channel->volume (128 + $vol); + $dx = $dx / 10 * 255; + $channel->set_panning ((min 255, 255 - $dx), (min 255, 255 + $dx)); + +# my $angle = $dx ? : $dx < 0 ? +# my $distance = -$vol; +# $channel->set_position ($angle, $distance); + + $chunk->play ($channel); + } + } else { + # sound_meta not set means data is in flight either way + my $meta = $CONN->{sound_meta}{$face} + or return; + + # fetch from database + CFPlus::DB::get res_data => $meta->{name}, sub { + my $vol = $meta->{meta}{volume} || 1; + my $rwops = new CFPlus::RW $_[0]; + my $chunk = new CFPlus::MixChunk $rwops; + $chunk->volume ($vol * 128); + $AUDIO_CHUNK{$face} = $chunk; + + audio_sound_push ($face); + }; + } +} + +sub audio_sound_play { + my ($face, $dx, $dy, $vol) = @_; + + $SDL_MIXER + or return; + + my $queue = $AUDIO_PLAY{$face} ||= []; + push @$queue, [Event::time + 0.2, $dx, $dy, $vol]; # delay sound by max. 0.2s + audio_sound_push $face + unless @$queue > 1; +} + +sub audio_music_set_ambient { my ($songs) = @_; my @want = @@ -1832,22 +1861,28 @@ } sub audio_music_start { - my $path = $MUSIC_PLAYING->{path} - or return; + my $meta = $MUSIC_PLAYING_META; - CFPlus::DB::prefetch_file $path, 1024_000, sub { + CFPlus::DB::get res_data => $meta->{name}, sub { return unless $SDL_MIXER; # music might have changed... - $path eq $MUSIC_PLAYING->{path} + $meta eq $MUSIC_PLAYING_META or return &audio_music_start (); - $MUSIC_PLAYER = new_from_file CFPlus::MixMusic $path; + $MUSIC_PLAYING_DATA = \$_[0]; + + my $rwops = $meta->{path} + ? new_from_file CFPlus::RW $meta->{path} + : new CFPlus::RW $$MUSIC_PLAYING_DATA; + + $MUSIC_PLAYER = new CFPlus::MixMusic $rwops + or ((warn CFPlus::Mix_GetError), return); # pretty fatal error my $NOW = time; - if ($MUSIC_PLAYING->{stop_time} > $NOW - $MUSIC_RESUME) { - my $pos = $MUSIC_PLAYING->{stop_pos}; + if ($MUSIC_PLAYING_META->{stop_time} > $NOW - $MUSIC_RESUME) { + my $pos = $MUSIC_PLAYING_META->{stop_pos}; $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos); $MUSIC_START = time - $pos; } else { @@ -1855,8 +1890,8 @@ $MUSIC_START = time; } - delete $MUSIC_PLAYING->{stop_time}; - delete $MUSIC_PLAYING->{stop_pos}; + delete $MUSIC_PLAYING_META->{stop_time}; + delete $MUSIC_PLAYING_META->{stop_pos}; } } @@ -1868,14 +1903,14 @@ @MUSIC_WANT = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_WANT; # if the currently playing song is acceptable, let it continue - return if $MUSIC_PLAYING - && grep $MUSIC_PLAYING->{path} eq $_->{path}, @MUSIC_WANT; + return if $MUSIC_PLAYING_META + && grep $MUSIC_PLAYING_META == $_, @MUSIC_WANT; my $NOW = time; - if ($MUSIC_PLAYING) { - $MUSIC_PLAYING->{stop_time} = $NOW; - $MUSIC_PLAYING->{stop_pos} = $NOW - $MUSIC_START; + if ($MUSIC_PLAYING_META) { + $MUSIC_PLAYING_META->{stop_time} = $NOW; + $MUSIC_PLAYING_META->{stop_pos} = $NOW - $MUSIC_START; CFPlus::MixMusic::fade_out 1000; } else { # sort by stop time, oldest first @@ -1883,7 +1918,7 @@ # if the most recently-played piece played very recently, # resume it, else choose the oldest piece for rotation. - $MUSIC_PLAYING = + $MUSIC_PLAYING_META = $MUSIC_WANT[-1]{stop_time} > $NOW - $MUSIC_RESUME ? $MUSIC_WANT[-1] : $MUSIC_WANT[0]; @@ -1893,45 +1928,27 @@ } sub audio_music_finished { - $MUSIC_PLAYING = undef; undef $MUSIC_PLAYER; + undef $MUSIC_PLAYING_META; + undef $MUSIC_PLAYING_DATA; audio_music_changed; } sub audio_init { if ($CFG->{audio_enable}) { - if (open my $fh, "<", CFPlus::find_rcfile "sounds/config") { - $SDL_MIXER = !CFPlus::Mix_OpenAudio; - - unless ($SDL_MIXER) { - status "Unable to open sound device: there will be no sound"; - return; - } + $ENV{MIX_EFFECTSMAXSPEED} = 1; + $SDL_MIXER = !CFPlus::Mix_OpenAudio; - CFPlus::Mix_AllocateChannels 8; - CFPlus::MixMusic::volume $CFG->{bgm_volume} * 128; + unless ($SDL_MIXER) { + status "Unable to open sound device: there will be no sound"; + return; + } - audio_music_finished; + CFPlus::Mix_AllocateChannels 16; + CFPlus::MixMusic::volume $CFG->{bgm_volume} * 128; - local $_; - while (<$fh>) { - next if /^\s*#/; - next if /^\s*$/; - - my ($file, $volume, $event) = split /\s+/, $_, 3; - - push @SOUNDS, "$volume,$file"; - - $AUDIO_CHUNKS{"$volume,$file"} ||= do { - my $chunk = new_from_file CFPlus::MixChunk CFPlus::find_rcfile "sounds/$file"; - $chunk->volume ($volume * 128 / 100); - $chunk - }; - } - } else { - status "unable to open sound config: $!"; - } + audio_music_finished; } else { undef $SDL_MIXER; } @@ -1941,7 +1958,7 @@ CFPlus::Mix_CloseAudio if $SDL_MIXER; undef $SDL_MIXER; @SOUNDS = (); - %AUDIO_CHUNKS = (); + %AUDIO_CHUNK = (); } my %animate_object; @@ -1949,8 +1966,6 @@ my $fps = 9; -my %demo;#d# - sub force_refresh { $fps = $fps * 0.95 + 1 / (($NOW - $LAST_REFRESH) || 0.1) * 0.05; debug sprintf "%3.2f", $fps if $ENV{CFPLUS_DEBUG} & 4; @@ -1992,15 +2007,6 @@ delete $animate_object{$widget}; } -# check once/second for faces that need to be prefetched -# this should, of course, only run on demand, but -# SDL forces worse things on us.... - -Event->timer (after => 1, interval => 0.25, cb => sub { - $CONN->face_prefetch - if $CONN; -}); - %SDL_CB = ( CFPlus::SDL_QUIT => sub { exit; @@ -2053,8 +2059,6 @@ my %DEF_CFG = ( sdl_mode => 0, - width => 640, - height => 480, fullscreen => 0, fast => 0, map_scale => 1, @@ -2070,7 +2074,6 @@ audio_enable => 1, bgm_enable => 1, bgm_volume => 0.25, - face_prefetch => 0, output_sync => 1, output_count => 1, output_rate => "",