From 0d2832ac43d31be444c4777aa5139684876effce Mon Sep 17 00:00:00 2001 From: C09 <100433934+CI09@users.noreply.github.com> Date: Fri, 18 Oct 2024 19:08:03 +0200 Subject: [PATCH 1/5] Add contextual music rarity --- simulation_parameters/Constants.cs | 2 ++ src/general/Jukebox.cs | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/simulation_parameters/Constants.cs b/simulation_parameters/Constants.cs index c6fa595fb9..ae21c5d61a 100644 --- a/simulation_parameters/Constants.cs +++ b/simulation_parameters/Constants.cs @@ -264,6 +264,8 @@ public static class Constants public const float BASE_CELL_DENSITY = 1000; + public const int CONTEXTUAL_MUSIC_RARITY = 3; + public const float MICROBE_MOVEMENT_SOUND_EMIT_COOLDOWN = 1.3f; // Note that the rotation speed is reversed, i.e. lower values mean faster diff --git a/src/general/Jukebox.cs b/src/general/Jukebox.cs index 5e841b22f9..f29b17f806 100644 --- a/src/general/Jukebox.cs +++ b/src/general/Jukebox.cs @@ -556,6 +556,18 @@ private void PlayNextTrackFromList(TrackList list, Func getPla if (tracks.Length == 0) return; + var random = new XoShiRo128starstar(); + + var contextMusicOnly = tracks.Where(c => c.ExclusiveToContexts != null); + + if (contextMusicOnly.Count() > 0) + { + if (random.Next(0, Constants.CONTEXTUAL_MUSIC_RARITY) == 0) + { + tracks = contextMusicOnly.ToArray(); + } + } + if (mode == TrackList.Order.Sequential) { list.LastPlayedIndex = (list.LastPlayedIndex + 1) % tracks.Length; @@ -564,7 +576,6 @@ private void PlayNextTrackFromList(TrackList list, Func getPla } else { - var random = new XoShiRo128starstar(); int nextIndex; if (mode == TrackList.Order.Random) From 5817f1c5fd551bda62f8c325d17c442bee17bb33 Mon Sep 17 00:00:00 2001 From: C09 <100433934+CI09@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:07:30 +0200 Subject: [PATCH 2/5] Style fix? --- src/general/Jukebox.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/general/Jukebox.cs b/src/general/Jukebox.cs index f29b17f806..196392fecc 100644 --- a/src/general/Jukebox.cs +++ b/src/general/Jukebox.cs @@ -558,13 +558,13 @@ private void PlayNextTrackFromList(TrackList list, Func getPla var random = new XoShiRo128starstar(); - var contextMusicOnly = tracks.Where(c => c.ExclusiveToContexts != null); + var contextMusicOnly = tracks.Where(c => c.ExclusiveToContexts != null).ToArray(); if (contextMusicOnly.Count() > 0) { if (random.Next(0, Constants.CONTEXTUAL_MUSIC_RARITY) == 0) { - tracks = contextMusicOnly.ToArray(); + tracks = contextMusicOnly; } } From 729dcebb9f06c58e8bc77341f738a59e2ed9983d Mon Sep 17 00:00:00 2001 From: C09 <100433934+CI09@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:35:23 +0200 Subject: [PATCH 3/5] Style fix. --- src/general/Jukebox.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/general/Jukebox.cs b/src/general/Jukebox.cs index 196392fecc..89b2250095 100644 --- a/src/general/Jukebox.cs +++ b/src/general/Jukebox.cs @@ -560,7 +560,7 @@ private void PlayNextTrackFromList(TrackList list, Func getPla var contextMusicOnly = tracks.Where(c => c.ExclusiveToContexts != null).ToArray(); - if (contextMusicOnly.Count() > 0) + if (contextMusicOnly.Length > 0) { if (random.Next(0, Constants.CONTEXTUAL_MUSIC_RARITY) == 0) { From 1c25fa0e0ff4a4b289193cc6dc23fd07d29a1351 Mon Sep 17 00:00:00 2001 From: C09 <100433934+CI09@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:53:08 +0200 Subject: [PATCH 4/5] Reorder --- src/general/Jukebox.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/general/Jukebox.cs b/src/general/Jukebox.cs index 89b2250095..f525eb36f5 100644 --- a/src/general/Jukebox.cs +++ b/src/general/Jukebox.cs @@ -558,11 +558,11 @@ private void PlayNextTrackFromList(TrackList list, Func getPla var random = new XoShiRo128starstar(); - var contextMusicOnly = tracks.Where(c => c.ExclusiveToContexts != null).ToArray(); - - if (contextMusicOnly.Length > 0) + if (random.Next(0, Constants.CONTEXTUAL_MUSIC_RARITY) == 0) { - if (random.Next(0, Constants.CONTEXTUAL_MUSIC_RARITY) == 0) + var contextMusicOnly = tracks.Where(c => c.ExclusiveToContexts != null).ToArray(); + + if (contextMusicOnly.Length > 0) { tracks = contextMusicOnly; } From 9d0863e62471d9c3bbd9823720059f39c1e14d9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henri=20Hyyryl=C3=A4inen?= Date: Mon, 21 Oct 2024 10:39:04 +0300 Subject: [PATCH 5/5] Fix duplicate memory allocation and improve the code clarity a bit and I added a TODO comment about fixing the music repeats caused by the system --- simulation_parameters/Constants.cs | 2 +- src/general/Jukebox.cs | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/simulation_parameters/Constants.cs b/simulation_parameters/Constants.cs index ae21c5d61a..438bb9de91 100644 --- a/simulation_parameters/Constants.cs +++ b/simulation_parameters/Constants.cs @@ -264,7 +264,7 @@ public static class Constants public const float BASE_CELL_DENSITY = 1000; - public const int CONTEXTUAL_MUSIC_RARITY = 3; + public const float CONTEXTUAL_ONLY_MUSIC_CHANCE = 0.33f; public const float MICROBE_MOVEMENT_SOUND_EMIT_COOLDOWN = 1.3f; diff --git a/src/general/Jukebox.cs b/src/general/Jukebox.cs index f525eb36f5..37c97b519a 100644 --- a/src/general/Jukebox.cs +++ b/src/general/Jukebox.cs @@ -551,23 +551,29 @@ private void StartPlayingFromMissingLists(MusicCategory target) private void PlayNextTrackFromList(TrackList list, Func getPlayer, int playerToUse) { var mode = list.TrackOrder; - var tracks = list.GetTracksForContexts(activeContexts).ToArray(); - - if (tracks.Length == 0) - return; + TrackList.Track[]? tracks = null; var random = new XoShiRo128starstar(); - if (random.Next(0, Constants.CONTEXTUAL_MUSIC_RARITY) == 0) + if (random.NextFloat() <= Constants.CONTEXTUAL_ONLY_MUSIC_CHANCE) { - var contextMusicOnly = tracks.Where(c => c.ExclusiveToContexts != null).ToArray(); + var contextMusicOnly = list.GetTracksForContexts(activeContexts).Where(c => c.ExclusiveToContexts != null) + .ToArray(); if (contextMusicOnly.Length > 0) { tracks = contextMusicOnly; + + // TODO: it would be nice to ensure that contextual track doesn't cause the same track to play in a row + // Currently it is way more likely as most contexts only have one track for them. } } + tracks ??= list.GetTracksForContexts(activeContexts).ToArray(); + + if (tracks.Length == 0) + return; + if (mode == TrackList.Order.Sequential) { list.LastPlayedIndex = (list.LastPlayedIndex + 1) % tracks.Length;