Lua API: Add SetSoundMask to disable playing certain sounds
authorWuzzy <Wuzzy2@mail.ru>
Thu, 08 Mar 2018 18:15:06 +0100
changeset 13126 de07c8423beb
parent 13125 be5d9fd2c56a
child 13127 d52d79f35558
Lua API: Add SetSoundMask to disable playing certain sounds
ChangeLog.txt
hedgewars/uScript.pas
hedgewars/uSound.pas
hedgewars/uVariables.pas
--- a/ChangeLog.txt	Thu Mar 08 17:07:14 2018 +0100
+++ b/ChangeLog.txt	Thu Mar 08 18:15:06 2018 +0100
@@ -92,6 +92,7 @@
  + New call: SpawnSupplyCrate(x, y, content, [, amount]): Spawn ammo or utility crate, depending on content
  + New call: HealHog(gearUid, healthBoost[, showMessage[, tint]]): Heal hedgehog with graphical effects and message
  + New call: SetTeamLabel(teamname[, label]): Set an arbitrary label for a team, will be displayed next to the team bar
+ + New call: SetSoundMask(soundId, isMasked): Allows to disable playing a sound effect from engine
  + New callback: onEndTurn(): Called at the end of a turn (when gears have settled)
  + New hedgehog effect: heArtillery: Per-hedgehog artillery mode (can't walk). Values: 1 = permanently active. 2 = temporarily active (sniper rifle). 0 = not active
  * AddAmmo now automatically unselects weapon if it would remove current ammo from current hedgehog
--- a/hedgewars/uScript.pas	Thu Mar 08 17:07:14 2018 +0100
+++ b/hedgewars/uScript.pas	Thu Mar 08 18:15:06 2018 +0100
@@ -2059,18 +2059,37 @@
             begin
             // no gear specified
             if n = 1 then
-                PlaySound(TSound(s))
+                PlaySound(TSound(s), false, true)
             else
                 begin
                 gear:= GearByUID(Trunc(lua_tonumber(L, 2)));
                 if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
-                    AddVoice(TSound(s),gear^.Hedgehog^.Team^.Voicepack)
+                    AddVoice(TSound(s), gear^.Hedgehog^.Team^.Voicepack, true)
                 end;
             end;
         end;
     lc_playsound:= 0;
 end;
 
+function lc_setsoundmask(L : Plua_State) : LongInt; Cdecl;
+var s: LongInt;
+    soundState: boolean;
+const
+    call = 'SetSoundMasked';
+    params = 'soundId, isMasked]';
+begin
+    if CheckLuaParamCount(L, 2, call, params) then
+        begin
+        s:= LuaToSoundOrd(L, 1, call, params);
+        if s <> Ord(sndNone) then
+            begin
+            soundState:= lua_toboolean(L, 2);
+            MaskedSounds[TSound(s)]:= soundState;
+            end;
+        end;
+    lc_setsoundmask:= 0;
+end;
+
 function lc_addteam(L : Plua_State) : LongInt; Cdecl;
 var np: LongInt;
 begin
@@ -3785,6 +3804,7 @@
 lua_register(luaState, _P'SetAmmo', @lc_setammo);
 lua_register(luaState, _P'SetAmmoDelay', @lc_setammodelay);
 lua_register(luaState, _P'PlaySound', @lc_playsound);
+lua_register(luaState, _P'SetSoundMask', @lc_setsoundmask);
 lua_register(luaState, _P'GetTeamName', @lc_getteamname);
 lua_register(luaState, _P'GetTeamIndex', @lc_getteamindex);
 lua_register(luaState, _P'GetTeamClan', @lc_getteamclan);
--- a/hedgewars/uSound.pas	Thu Mar 08 17:07:14 2018 +0100
+++ b/hedgewars/uSound.pas	Thu Mar 08 18:15:06 2018 +0100
@@ -63,8 +63,10 @@
 // then the sound's playback won't be interrupted if asked to play again.
 procedure PlaySound(snd: TSound);
 procedure PlaySound(snd: TSound; keepPlaying: boolean);
+procedure PlaySound(snd: TSound; keepPlaying: boolean; ignoreMask: boolean);
 procedure PlaySoundV(snd: TSound; voicepack: PVoicepack);
 procedure PlaySoundV(snd: TSound; voicepack: PVoicepack; keepPlaying: boolean);
+procedure PlaySoundV(snd: TSound; voicepack: PVoicepack; keepPlaying: boolean; ignoreMask: boolean);
 
 // Plays sound snd [of voicepack] in a loop, but starts with fadems milliseconds of fade-in.
 // Returns sound channel of the looped sound.
@@ -80,6 +82,7 @@
 procedure StopSoundChan(chn, fadems: LongInt);
 
 procedure AddVoice(snd: TSound; voicepack: PVoicepack);
+procedure AddVoice(snd: TSound; voicepack: PVoicepack; ignoreMask: boolean);
 procedure PlayNextVoice;
 
 
@@ -414,20 +417,30 @@
 
 procedure PlaySound(snd: TSound);
 begin
-    PlaySoundV(snd, nil, false);
+    PlaySoundV(snd, nil, false, false);
 end;
 
 procedure PlaySound(snd: TSound; keepPlaying: boolean);
 begin
-    PlaySoundV(snd, nil, keepPlaying);
+    PlaySoundV(snd, nil, keepPlaying, false);
+end;
+
+procedure PlaySound(snd: TSound; keepPlaying: boolean; ignoreMask: boolean);
+begin
+    PlaySoundV(snd, nil, keepPlaying, ignoreMask);
 end;
 
 procedure PlaySoundV(snd: TSound; voicepack: PVoicepack);
 begin
-    PlaySoundV(snd, voicepack, false);
+    PlaySoundV(snd, voicepack, false, false);
 end;
 
 procedure PlaySoundV(snd: TSound; voicepack: PVoicepack; keepPlaying: boolean);
+begin
+    PlaySoundV(snd, voicepack, keepPlaying, false);
+end;
+
+procedure PlaySoundV(snd: TSound; voicepack: PVoicepack; keepPlaying: boolean; ignoreMask: boolean);
 var s:shortstring;
 rwops: PSDL_RWops;
 begin
@@ -437,6 +450,9 @@
     if keepPlaying and (lastChan[snd] <> -1) and (Mix_Playing(lastChan[snd]) <> 0) then
         exit;
 
+    if (ignoreMask = false) and (MaskedSounds[snd] = true) then
+        exit;
+
     if (voicepack <> nil) then
         begin
         if (voicepack^.chunks[snd] = nil) and (Soundz[snd].Path = ptVoices) and (Soundz[snd].FileName <> '') then
@@ -486,10 +502,19 @@
 end;
 
 procedure AddVoice(snd: TSound; voicepack: PVoicepack);
+begin
+    AddVoice(snd, voicepack, false);
+end;
+
+procedure AddVoice(snd: TSound; voicepack: PVoicepack; ignoreMask: boolean);
 var i : LongInt;
 begin
+
     if (not isSoundEnabled) or fastUntilLag or ((LastVoice.snd = snd) and  (LastVoice.voicepack = voicepack)) then
         exit;
+    if (ignoreMask = false) and (MaskedSounds[snd] = true) then
+        exit;
+
     if (snd = sndVictory) or (snd = sndFlawless) then
         begin
         Mix_FadeOutChannel(-1, 800);
--- a/hedgewars/uVariables.pas	Thu Mar 08 17:07:14 2018 +0100
+++ b/hedgewars/uVariables.pas	Thu Mar 08 18:15:06 2018 +0100
@@ -254,6 +254,8 @@
     LuaEndTurnRequested: boolean;
     LuaNoEndTurnTaunts: boolean;
 
+    MaskedSounds : array[TSound] of boolean;
+
     LastVoice : TVoice;
 
     mobileRecord: TMobileRecord;
@@ -2640,6 +2642,7 @@
 procedure initModule;
 var s: shortstring;
     i: integer;
+    t: TSound;
 begin
     // init LastVoice
     LastVoice.snd:= sndNone;
@@ -2901,6 +2904,9 @@
     LuaEndTurnRequested:= false;
     LuaNoEndTurnTaunts:= false;
 
+    for t:= Low(TSound) to High(TSound) do
+        MaskedSounds[t]:= false;
+
     UIDisplay:= uiAll;
     LocalMessage:= 0;