Skip to content

Commit

Permalink
Rate limits for notification sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
drogoganor authored and pchote committed Dec 23, 2018
1 parent 9b3ddee commit bbc83c1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
41 changes: 37 additions & 4 deletions OpenRA.Game/GameRules/SoundInfo.cs
Expand Up @@ -33,28 +33,61 @@ 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(0, a.Value)));
NotificationsPools = Exts.Lazy(() => ParseSoundPool(y, "Notifications"));
}

Dictionary<string, SoundPool> ParseSoundPool(MiniYaml y, string key)
{
var ret = new Dictionary<string, SoundPool>();
var classifiction = y.Nodes.First(x => x.Key == key);
foreach (var t in classifiction.Value.Nodes)
{
var rateLimit = 0;
var rateLimitNode = t.Value.Nodes.FirstOrDefault(x => x.Key == "RateLimit");
if (rateLimitNode != null)
rateLimit = FieldLoader.GetValue<int>(rateLimitNode.Key, rateLimitNode.Value.Value);

var names = FieldLoader.GetValue<string[]>(t.Key, t.Value.Value);
var sp = new SoundPool(rateLimit, names);
ret.Add(t.Key, sp);
}

return ret;
}
}

public class SoundPool
{
readonly string[] clips;
readonly int rateLimit;
readonly List<string> liveclips = new List<string>();
long lastPlayed = 0;

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

public string GetNext()
{
if (liveclips.Count == 0)
liveclips.AddRange(clips);

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

// Perform rate limiting if necessary
if (rateLimit != 0)
{
var now = Game.RunTime;
if (lastPlayed != 0 && now < lastPlayed + rateLimit)
return null;

lastPlayed = now;
}

var i = Game.CosmeticRandom.Next(liveclips.Count);
var s = liveclips[i];
Expand Down
4 changes: 4 additions & 0 deletions mods/cnc/audio/notifications.yaml
Expand Up @@ -6,11 +6,13 @@ Speech:
AirstrikeReady: airredy1
BaseAttack: baseatk1
Building: bldging1
RateLimit: 600
BuildingCannotPlaceAudio: deploy1
BuildingCaptured: capt1
BuildingInProgress: bldg1
BuildingLost: strclost
Cancelled: cancel1
RateLimit: 500
CivilianBuildingCaptured: civcapt1
CivilianKilled: civdead1
ConstructionComplete: constru1
Expand Down Expand Up @@ -41,9 +43,11 @@ Speech:
SilosNeeded: silos1
StartGame:
Training: bldging1
RateLimit: 800
UnitDestroyed: dead1
UnitLost: unitlost
UnitReady: unitredy
RateLimit: 900
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

Expand Down
4 changes: 4 additions & 0 deletions mods/d2k/audio/notifications.yaml
Expand Up @@ -13,11 +13,13 @@ Speech:
Notifications:
BaseAttack: ATACK
Building: BUILD
RateLimit: 600
BuildingCannotPlaceAudio: PLACE
BuildingCaptured: CAPT
BuildingLost: BLOST
BuildingReady: BDRDY
Cancelled: CANCL
RateLimit: 500
CannotDeploy: DPLOY
DeathHandMissilePrepping: PREP
DeathHandMissileReady: DHRDY
Expand Down Expand Up @@ -50,8 +52,10 @@ Speech:
TMinusThree: 3MIN
TMinusTwo: 2MIN
Training: TRAIN
RateLimit: 800
UnitLost: ULOST
UnitReady: UNRDY
RateLimit: 900
UnitRepaired: GANEW
UpgradeOptions: UPGOP
Upgrading: UPGRD
Expand Down
4 changes: 4 additions & 0 deletions mods/ra/audio/notifications.yaml
Expand Up @@ -17,12 +17,14 @@ Speech:
AtomBombPrepping: atprep1
BaseAttack: baseatk1
Building: abldgin1
RateLimit: 600
BuildingCannotPlaceAudio: nodeply1
BuildingCaptured: strucap1
BuildingInfiltrated: bldginf1
BuildingInProgress: progres1
BuildingProgress: bldgprg1
Cancelled: cancld1
RateLimit: 500
ChronosphereCharging: chrochr1
ChronosphereReady: chrordy1
ChronosphereTestSuccessful: chroyes1
Expand Down Expand Up @@ -97,12 +99,14 @@ Speech:
TimerStarted: timergo1
TimerStopped: timerno1
Training: train1
RateLimit: 800
TwentyMinutesRemaining: 20minr
UnitArmorUpgraded: armorup1
UnitFirepowerUpgraded: firepo1
UnitFull: unitful1
UnitLost: unitlst1
UnitReady: unitrdy1
RateLimit: 900
UnitRepaired: unitrep1
UnitSold: unitsld1
UnitSpeedUpgraded: unitspd1
Expand Down
4 changes: 4 additions & 0 deletions mods/ts/audio/speech-generic.yaml
Expand Up @@ -14,10 +14,12 @@ Speech:
BridgeRepaired2: 00-n128
BridgeRepaired: 00-i118
Building: 00-i216
RateLimit: 600
BuildingCannotPlaceAudio: 00-i016 #CannotDeployHere
BuildingCaptured: 00-i056
BuildingInfiltrated: 00-i014
Cancelled: 00-i220
RateLimit: 500
ChemicalMissileReady: 00-i152
CivilianKilled: 00-n018
CloakedUnitDetected: 00-i172
Expand Down Expand Up @@ -71,10 +73,12 @@ Speech:
TiberiumMissileReady2: 01-n194
TiberiumMissileReady: 01-n176
Training: 00-i062
RateLimit: 800
UnitArmourUpgraded: 00-i068
UnitFirepowerUpgraded: 00-i070
UnitLost: 00-i074
UnitReady: 00-i076
RateLimit: 900
UnitRepaired: 00-i078
UnitSold: 00-i226
UnitSpeedUpgraded: 00-i080
Expand Down

0 comments on commit bbc83c1

Please sign in to comment.