--- deliantra/Deliantra-Client/bin/cfplus 2007/07/29 18:56:03 1.190 +++ deliantra/Deliantra-Client/bin/cfplus 2007/07/30 02:16:16 1.192 @@ -73,6 +73,7 @@ use Time::HiRes 'time'; use Event; +use List::Util qw(max min); use Crossfire; use Crossfire::Protocol::Constants; @@ -462,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) { @@ -1084,7 +1073,7 @@ on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{host} = $value; - 0 + 1 } ); @@ -1094,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 }, ); } @@ -1102,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"); @@ -1110,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"); @@ -1121,30 +1110,31 @@ . "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 => "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, 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, 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, 11, $LOGIN_BUTTON = new CFPlus::UI::Button @@ -1154,7 +1144,7 @@ on_activate => sub { $CONN ? stop_game : start_game; - 0 + 1 }, ); @@ -1802,7 +1792,57 @@ sub audio_channel_finished { my ($channel) = @_; - #warn "channel $channel finished\n";#d# +# warn "channel $channel finished\n";#d# +} + +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 = $chunk->play + or return; + + $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); + } + } 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} || 100; + my $rwops = new CFPlus::RW $_[0]; + my $chunk = new CFPlus::MixChunk $rwops; + $chunk->volume ($vol * 128 / 100); + $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 { @@ -1896,39 +1936,18 @@ sub audio_init { if ($CFG->{audio_enable}) { - if (open my $fh, "<", CFPlus::find_rcfile "sounds/config") { - $ENV{MIX_EFFECTSMAXSPEED} = 1; - $SDL_MIXER = !CFPlus::Mix_OpenAudio; + $ENV{MIX_EFFECTSMAXSPEED} = 1; + $SDL_MIXER = !CFPlus::Mix_OpenAudio; - unless ($SDL_MIXER) { - status "Unable to open sound device: there will be no sound"; - return; - } - - 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 $rwops = new_from_file CFPlus::RW CFPlus::find_rcfile "sounds/$file"; - my $chunk = new CFPlus::MixChunk $rwops; - $chunk->volume ($volume * 128 / 100); - $chunk - }; - } - } else { - status "unable to open sound config: $!"; - } + audio_music_finished; } else { undef $SDL_MIXER; }