Skip to content

Commit

Permalink
Rate limit for notification sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
drogoganor committed Oct 18, 2018
1 parent 3e73357 commit d2966fe
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
25 changes: 21 additions & 4 deletions OpenRA.Game/GameRules/SoundInfo.cs
Expand Up @@ -21,6 +21,7 @@ public class SoundInfo
public readonly Dictionary<string, string[]> Prefixes = new Dictionary<string, string[]>();
public readonly Dictionary<string, string[]> Voices = new Dictionary<string, string[]>();
public readonly Dictionary<string, string[]> Notifications = new Dictionary<string, string[]>();
public readonly Dictionary<string, int> NotificationRateLimits = new Dictionary<string, int>();
public readonly string DefaultVariant = ".aud";
public readonly string DefaultPrefix = "";
public readonly HashSet<string> DisableVariants = new HashSet<string>();
Expand All @@ -33,19 +34,26 @@ public SoundInfo(MiniYaml y)
{
FieldLoader.Load(this, y);

VoicePools = Exts.Lazy(() => Voices.ToDictionary(a => a.Key, a => new SoundPool(a.Value)));
NotificationsPools = Exts.Lazy(() => Notifications.ToDictionary(a => a.Key, a => new SoundPool(a.Value)));
VoicePools = Exts.Lazy(() => Voices.ToDictionary(a => a.Key,
a => new SoundPool(NotificationRateLimits.ContainsKey(a.Key) ? NotificationRateLimits[a.Key] : 0, a.Value)));
NotificationsPools = Exts.Lazy(() => Notifications.ToDictionary(a => a.Key,
a => new SoundPool(NotificationRateLimits.ContainsKey(a.Key) ? NotificationRateLimits[a.Key] : 0, a.Value)));
}
}

public class SoundPool
{
readonly string[] clips;
readonly int rateLimit;
readonly List<string> liveclips = new List<string>();
readonly TimeSpan rateSpan;
DateTime lastPlayed = DateTime.MinValue;

public SoundPool(params string[] clips)
public SoundPool(int rateLimit, params string[] clips)
{
this.clips = clips;
this.rateLimit = rateLimit;
this.rateSpan = new TimeSpan(0, 0, 0, 0, rateLimit);
}

public string GetNext()
Expand All @@ -54,7 +62,16 @@ public string GetNext()
liveclips.AddRange(clips);

if (liveclips.Count == 0)
return null; /* avoid crashing if there's no clips at all */
return null; /* avoid crashing if there's no clips at all */

// Perform rate limiting if necessary
if (rateLimit != 0)
{
var now = DateTime.Now;
if (lastPlayed != DateTime.MinValue && now < lastPlayed + rateSpan)
return null;
lastPlayed = now;
}

var i = Game.CosmeticRandom.Next(liveclips.Count);
var s = liveclips[i];
Expand Down
2 changes: 2 additions & 0 deletions OpenRA.Game/Sound/Sound.cs
Expand Up @@ -12,9 +12,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.FileSystem;
using OpenRA.GameRules;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA
{
Expand Down
5 changes: 5 additions & 0 deletions mods/cnc/audio/notifications.yaml
Expand Up @@ -46,6 +46,11 @@ Speech:
UnitReady: unitredy
Win: accom1
DisablePrefixes: AbilityInsufficientPower, BaseAttack, Building, BuildingCannotPlaceAudio, BuildingInProgress, BuildingLost, Cancelled, CivilianBuildingCaptured, CivilianKilled, ConstructionComplete, EnemyUnitsApproaching, EnemyStructureDestroyed, EnemyPlanesApproaching, HarvesterAttack, InsufficientPower, IonCannonCharging, IonCannonReady, Leave, Lose, LowPower, MissionAccomplished, MissionFailed, NewOptions, NoBuild, NodStructureDestroyed, NotReady, NuclearWarheadApproaching, NuclearWeaponAvailable, NuclearWeaponLaunched, OnHold, PrimaryBuildingSelected, Reinforce, Repairing, SelectTarget, SilosNeeded, Training, UnitLost, UnitReady, Win
NotificationRateLimits:
Building: 600
Training: 800
UnitReady: 900
Cancelled: 500

Sounds:
Notifications:
Expand Down
5 changes: 5 additions & 0 deletions mods/d2k/audio/notifications.yaml
Expand Up @@ -58,6 +58,11 @@ Speech:
Win: MWIN
WormAttack: WATTK
WormSign: WSIGN
NotificationRateLimits:
Building: 600
Training: 800
UnitReady: 900
Cancelled: 500

Sounds:
DefaultVariant: .WAV
Expand Down
5 changes: 5 additions & 0 deletions mods/ra/audio/notifications.yaml
Expand Up @@ -113,6 +113,11 @@ Speech:
WarningFourMinutesRemaining: 4minr
WarningFiveMinutesRemaining: 5minr
Win: misnwon1
NotificationRateLimits:
Building: 600
Training: 800
UnitReady: 900
Cancelled: 500

Sounds:
Notifications:
Expand Down
5 changes: 5 additions & 0 deletions mods/ts/audio/speech-generic.yaml
Expand Up @@ -81,3 +81,8 @@ Speech:
WarningInboundTacticalNukeDetected: 00-n098
Win: 00-i284
YouHaveResigned: 00-i288
NotificationRateLimits:
Building: 600
Training: 800
UnitReady: 900
Cancelled: 500

0 comments on commit d2966fe

Please sign in to comment.