Skip to content

Commit

Permalink
Merge pull request #501 from DebugOk/Wizmerge-2/12-part-2
Browse files Browse the repository at this point in the history
Merge wizden up to 02/12 - Part 2
  • Loading branch information
DebugOk authored Dec 2, 2023
2 parents ebfd7f4 + fb0fcc4 commit 14c7ba4
Show file tree
Hide file tree
Showing 591 changed files with 46,622 additions and 45,189 deletions.
58 changes: 31 additions & 27 deletions Content.Client/Audio/AmbientSoundSystem.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using System.Linq;
using System.Numerics;
using Content.Shared.Audio;
using Content.Shared.CCVar;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Player;
using Robust.Shared.Audio;
using Robust.Shared.Log;
using Robust.Shared.Configuration;
using Robust.Shared.Map;
using Robust.Shared.Physics;
using Robust.Shared.Random;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using System.Linq;
using System.Numerics;
using Robust.Client.GameObjects;
using Robust.Shared.Audio.Effects;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Player;

namespace Content.Client.Audio;
//TODO: This is using a incomplete version of the whole "only play nearest sounds" algo, that breaks down a bit should the ambient sound cap get hit.
Expand Down Expand Up @@ -41,14 +46,18 @@ protected override void QueueUpdate(EntityUid uid, AmbientSoundComponent ambienc
private TimeSpan _targetTime = TimeSpan.Zero;
private float _ambienceVolume = 0.0f;

private static AudioParams _params = AudioParams.Default.WithVariation(0.01f).WithLoop(true).WithAttenuation(Attenuation.LinearDistance);
private static AudioParams _params = AudioParams.Default
.WithVariation(0.01f)
.WithLoop(true)
.WithAttenuation(Attenuation.LinearDistance)
.WithMaxDistance(7f);

/// <summary>
/// How many times we can be playing 1 particular sound at once.
/// </summary>
private int MaxSingleSound => (int) (_maxAmbientCount / (16.0f / 6.0f));

private readonly Dictionary<Entity<AmbientSoundComponent>, (IPlayingAudioStream? Stream, SoundSpecifier Sound, string Path)> _playingSounds = new();
private readonly Dictionary<AmbientSoundComponent, (EntityUid? Stream, SoundSpecifier Sound, string Path)> _playingSounds = new();
private readonly Dictionary<string, int> _playingCount = new();

public bool OverlayEnabled
Expand Down Expand Up @@ -98,10 +107,10 @@ public override void Initialize()

private void OnShutdown(EntityUid uid, AmbientSoundComponent component, ComponentShutdown args)
{
if (!_playingSounds.Remove((uid, component), out var sound))
if (!_playingSounds.Remove(component, out var sound))
return;

sound.Stream?.Stop();
_audio.Stop(sound.Stream);
_playingCount[sound.Path] -= 1;
if (_playingCount[sound.Path] == 0)
_playingCount.Remove(sound.Path);
Expand All @@ -111,13 +120,13 @@ private void SetAmbienceVolume(float value)
{
_ambienceVolume = value;

foreach (var ((_, comp), values) in _playingSounds)
foreach (var (comp, values) in _playingSounds)
{
if (values.Stream == null)
continue;

var stream = (AudioSystem.PlayingStream) values.Stream;
stream.Volume = _params.Volume + comp.Volume + _ambienceVolume;
var stream = values.Stream;
_audio.SetVolume(stream, _params.Volume + comp.Volume + _ambienceVolume);
}
}
private void SetCooldown(float value) => _cooldown = value;
Expand Down Expand Up @@ -177,7 +186,7 @@ private void ClearSounds()
{
foreach (var (stream, _, _) in _playingSounds.Values)
{
stream?.Stop();
_audio.Stop(stream);
}

_playingSounds.Clear();
Expand All @@ -186,7 +195,7 @@ private void ClearSounds()

private readonly struct QueryState
{
public readonly Dictionary<string, List<(float Importance, Entity<AmbientSoundComponent>)>> SourceDict = new();
public readonly Dictionary<string, List<(float Importance, AmbientSoundComponent)>> SourceDict = new();
public readonly Vector2 MapPos;
public readonly TransformComponent Player;
public readonly EntityQuery<TransformComponent> Query;
Expand Down Expand Up @@ -224,7 +233,7 @@ private static bool Callback(

// Prioritize far away & loud sounds.
var importance = range * (ambientComp.Volume + 32);
state.SourceDict.GetOrNew(key).Add((importance, (ambientComp.Owner, ambientComp)));
state.SourceDict.GetOrNew(key).Add((importance, ambientComp));
return true;
}

Expand All @@ -238,10 +247,9 @@ private void ProcessNearbyAmbience(TransformComponent playerXform)
var mapPos = playerXform.MapPosition;

// Remove out-of-range ambiences
foreach (var (ent, sound) in _playingSounds)
foreach (var (comp, sound) in _playingSounds)
{
var entity = ent.Owner;
var comp = ent.Comp;
var entity = comp.Owner;

if (comp.Enabled &&
// Don't keep playing sounds that have changed since.
Expand All @@ -258,8 +266,8 @@ private void ProcessNearbyAmbience(TransformComponent playerXform)
continue;
}

sound.Stream?.Stop();
_playingSounds.Remove((entity, comp));
_audio.Stop(sound.Stream);
_playingSounds.Remove(comp);
_playingCount[sound.Path] -= 1;
if (_playingCount[sound.Path] == 0)
_playingCount.Remove(sound.Path);
Expand All @@ -284,12 +292,11 @@ private void ProcessNearbyAmbience(TransformComponent playerXform)

sources.Sort(static (a, b) => b.Importance.CompareTo(a.Importance));

foreach (var (_, ent) in sources)
foreach (var (_, comp) in sources)
{
var uid = ent.Owner;
var comp = ent.Comp;
var uid = comp.Owner;

if (_playingSounds.ContainsKey(ent) ||
if (_playingSounds.ContainsKey(comp) ||
metaQuery.GetComponent(uid).EntityPaused)
continue;

Expand All @@ -299,11 +306,8 @@ private void ProcessNearbyAmbience(TransformComponent playerXform)
.WithPlayOffset(_random.NextFloat(0.0f, 100.0f))
.WithMaxDistance(comp.Range);

var stream = _audio.PlayPvs(comp.Sound, uid, audioParams);
if (stream == null)
continue;

_playingSounds[ent] = (stream, comp.Sound, key);
var stream = _audio.PlayEntity(comp.Sound, Filter.Local(), uid, false, audioParams);
_playingSounds[comp] = (stream.Value.Entity, comp.Sound, key);
playingCount++;

if (_playingSounds.Count >= _maxAmbientCount)
Expand Down
8 changes: 4 additions & 4 deletions Content.Client/Audio/BackgroundAudioSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Robust.Client;
using Robust.Client.State;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Configuration;
using Robust.Shared.Player;

Expand All @@ -21,7 +22,7 @@ public sealed class BackgroundAudioSystem : EntitySystem

private readonly AudioParams _lobbyParams = new(-5f, 1, "Master", 0, 0, 0, true, 0f);

private IPlayingAudioStream? _lobbyStream;
private EntityUid? _lobbyStream;

public override void Initialize()
{
Expand Down Expand Up @@ -118,12 +119,11 @@ public void StartLobbyMusic()
}

_lobbyStream = _audio.PlayGlobal(file, Filter.Local(), false,
_lobbyParams.WithVolume(_lobbyParams.Volume + _configManager.GetCVar(CCVars.LobbyMusicVolume)));
_lobbyParams.WithVolume(_lobbyParams.Volume + _configManager.GetCVar(CCVars.LobbyMusicVolume)))?.Entity;
}

private void EndLobbyMusic()
{
_lobbyStream?.Stop();
_lobbyStream = null;
_lobbyStream = _audio.Stop(_lobbyStream);
}
}
25 changes: 14 additions & 11 deletions Content.Client/Audio/ClientGlobalSoundSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Content.Shared.CCVar;
using Content.Shared.GameTicking;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Configuration;
using Robust.Shared.Player;

Expand All @@ -14,11 +15,11 @@ public sealed class ClientGlobalSoundSystem : SharedGlobalSoundSystem

// Admin music
private bool _adminAudioEnabled = true;
private List<IPlayingAudioStream?> _adminAudio = new(1);
private List<EntityUid?> _adminAudio = new(1);

// Event sounds (e.g. nuke timer)
private bool _eventAudioEnabled = true;
private Dictionary<StationEventMusicType, IPlayingAudioStream?> _eventAudio = new(1);
private Dictionary<StationEventMusicType, EntityUid?> _eventAudio = new(1);

public override void Initialize()
{
Expand Down Expand Up @@ -49,13 +50,13 @@ private void ClearAudio()
{
foreach (var stream in _adminAudio)
{
stream?.Stop();
_audio.Stop(stream);
}
_adminAudio.Clear();

foreach (var (_, stream) in _eventAudio)
foreach (var stream in _eventAudio.Values)
{
stream?.Stop();
_audio.Stop(stream);
}

_eventAudio.Clear();
Expand All @@ -66,7 +67,7 @@ private void PlayAdminSound(AdminSoundEvent soundEvent)
if(!_adminAudioEnabled) return;

var stream = _audio.PlayGlobal(soundEvent.Filename, Filter.Local(), false, soundEvent.AudioParams);
_adminAudio.Add(stream);
_adminAudio.Add(stream.Value.Entity);
}

private void PlayStationEventMusic(StationEventMusicEvent soundEvent)
Expand All @@ -75,7 +76,7 @@ private void PlayStationEventMusic(StationEventMusicEvent soundEvent)
if(!_eventAudioEnabled || _eventAudio.ContainsKey(soundEvent.Type)) return;

var stream = _audio.PlayGlobal(soundEvent.Filename, Filter.Local(), false, soundEvent.AudioParams);
_eventAudio.Add(soundEvent.Type, stream);
_eventAudio.Add(soundEvent.Type, stream.Value.Entity);
}

private void PlayGameSound(GameGlobalSoundEvent soundEvent)
Expand All @@ -85,8 +86,10 @@ private void PlayGameSound(GameGlobalSoundEvent soundEvent)

private void StopStationEventMusic(StopStationEventMusic soundEvent)
{
if (!_eventAudio.TryGetValue(soundEvent.Type, out var stream)) return;
stream?.Stop();
if (!_eventAudio.TryGetValue(soundEvent.Type, out var stream))
return;

_audio.Stop(stream);
_eventAudio.Remove(soundEvent.Type);
}

Expand All @@ -96,7 +99,7 @@ private void ToggleAdminSound(bool enabled)
if (_adminAudioEnabled) return;
foreach (var stream in _adminAudio)
{
stream?.Stop();
_audio.Stop(stream);
}
_adminAudio.Clear();
}
Expand All @@ -107,7 +110,7 @@ private void ToggleStationEventMusic(bool enabled)
if (_eventAudioEnabled) return;
foreach (var stream in _eventAudio)
{
stream.Value?.Stop();
_audio.Stop(stream.Value);
}
_eventAudio.Clear();
}
Expand Down
39 changes: 18 additions & 21 deletions Content.Client/Audio/ContentAudioSystem.AmbientMusic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Robust.Client.ResourceManagement;
using Robust.Client.State;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Components;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Configuration;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
Expand Down Expand Up @@ -39,7 +41,7 @@ public sealed partial class ContentAudioSystem
// Don't need to worry about this being serializable or pauseable as it doesn't affect the sim.
private TimeSpan _nextAudio;

private AudioSystem.PlayingStream? _ambientMusicStream;
private EntityUid? _ambientMusicStream;
private AmbientMusicPrototype? _musicProto;

/// <summary>
Expand All @@ -58,12 +60,6 @@ public sealed partial class ContentAudioSystem

private void InitializeAmbientMusic()
{
// TODO: Shitty preload
foreach (var audio in _proto.Index<SoundCollectionPrototype>("AmbienceSpace").PickFiles)
{
_resource.GetResource<AudioResource>(audio.ToString());
}

_configManager.OnValueChanged(CCVars.AmbientMusicVolume, AmbienceCVarChanged, true);
_sawmill = IoCManager.Resolve<ILogManager>().GetSawmill("audio.ambience");

Expand All @@ -83,7 +79,7 @@ private void AmbienceCVarChanged(float obj)

if (_ambientMusicStream != null && _musicProto != null)
{
_ambientMusicStream.Volume = _musicProto.Sound.Params.Volume + _volumeSlider;
_audio.SetVolume(_ambientMusicStream, _musicProto.Sound.Params.Volume + _volumeSlider);
}
}

Expand All @@ -92,7 +88,7 @@ private void ShutdownAmbientMusic()
_configManager.UnsubValueChanged(CCVars.AmbientMusicVolume, AmbienceCVarChanged);
_proto.PrototypesReloaded -= OnProtoReload;
_state.OnStateChanged -= OnStateChange;
_ambientMusicStream?.Stop();
_ambientMusicStream = _audio.Stop(_ambientMusicStream);
}

private void OnProtoReload(PrototypesReloadedEventArgs obj)
Expand Down Expand Up @@ -129,8 +125,7 @@ private void SetupAmbientSounds()
private void OnRoundEndMessage(RoundEndMessageEvent ev)
{
// If scoreboard shows then just stop the music
_ambientMusicStream?.Stop();
_ambientMusicStream = null;
_ambientMusicStream = _audio.Stop(_ambientMusicStream);
_nextAudio = TimeSpan.FromMinutes(3);
}

Expand Down Expand Up @@ -170,15 +165,20 @@ private void UpdateAmbientMusic()
return;
}

var isDone = _ambientMusicStream?.Done;
bool? isDone = null;

if (TryComp(_ambientMusicStream, out AudioComponent? audioComp))
{
isDone = !audioComp.Playing;
}

if (_interruptable)
{
var player = _player.LocalPlayer?.ControlledEntity;
var player = _player.LocalSession?.AttachedEntity;

if (player == null || _musicProto == null || !_rules.IsTrue(player.Value, _proto.Index<RulesPrototype>(_musicProto.Rules)))
{
FadeOut(_ambientMusicStream, AmbientMusicFadeTime);
FadeOut(_ambientMusicStream, duration: AmbientMusicFadeTime);
_musicProto = null;
_interruptable = false;
isDone = true;
Expand Down Expand Up @@ -221,14 +221,11 @@ private void UpdateAmbientMusic()
false,
AudioParams.Default.WithVolume(_musicProto.Sound.Params.Volume + _volumeSlider));

if (strim != null)
{
_ambientMusicStream = (AudioSystem.PlayingStream) strim;
_ambientMusicStream = strim.Value.Entity;

if (_musicProto.FadeIn)
{
FadeIn(_ambientMusicStream, AmbientMusicFadeTime);
}
if (_musicProto.FadeIn)
{
FadeIn(_ambientMusicStream, strim.Value.Component, AmbientMusicFadeTime);
}

// Refresh the list
Expand Down
Loading

0 comments on commit 14c7ba4

Please sign in to comment.