Guest User

pda_radio_music_mute.script

a guest
May 6th, 2026
70
0
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. -- ============================================================
  2. -- pda_radio_music_mute.script
  3. -- Place in: ...\Stalker Anomaly\gamedata\scripts\
  4. --
  5. -- What it does:
  6. -- Hooks action_radio_start / action_radio_stop in
  7. -- ui_pda_radio_tab to mute snd_volume_music while the PDA
  8. -- radio (or music player) is active, then restores it.
  9. --
  10. -- Shares the volume backup with xrs_dyn_music so combat
  11. -- music and the radio mute never fight over snd_volume_music.
  12. -- ============================================================
  13.  
  14.  
  15. -- ----------------------------------------------------------------
  16. -- State
  17. -- ----------------------------------------------------------------
  18.  
  19. local is_radio_muted = false -- true while we have silenced the music
  20. local vol_backup = nil -- the volume we captured before muting
  21.  
  22.  
  23. -- ----------------------------------------------------------------
  24. -- Volume helpers
  25. -- Use exec_console_cmd to stay consistent with xrs_dyn_music
  26. -- ----------------------------------------------------------------
  27.  
  28. local function get_vol()
  29. return get_console_cmd(2, "snd_volume_music")
  30. end
  31.  
  32. local function set_vol(v)
  33. exec_console_cmd("snd_volume_music " .. tostring(v))
  34. end
  35.  
  36.  
  37. -- ----------------------------------------------------------------
  38. -- Sync backup into xrs_dyn_music so it does not clobber our mute
  39. -- when combat kicks in mid-radio.
  40. -- ----------------------------------------------------------------
  41.  
  42. local function sync_dyn_music(vol)
  43. if xrs_dyn_music then
  44. xrs_dyn_music.ambient_vol = vol
  45. end
  46. end
  47.  
  48.  
  49. -- ----------------------------------------------------------------
  50. -- Core: mute
  51. -- ----------------------------------------------------------------
  52.  
  53. local function mute()
  54. if is_radio_muted then return end
  55. is_radio_muted = true
  56. vol_backup = get_vol()
  57. sync_dyn_music(vol_backup)
  58. set_vol(0)
  59. end
  60.  
  61.  
  62. -- ----------------------------------------------------------------
  63. -- Core: restore
  64. -- ----------------------------------------------------------------
  65.  
  66. local function restore()
  67. if not is_radio_muted then return end
  68. is_radio_muted = false
  69. if vol_backup ~= nil then
  70. set_vol(vol_backup)
  71. sync_dyn_music(vol_backup)
  72. vol_backup = nil
  73. end
  74. end
  75.  
  76.  
  77. -- ----------------------------------------------------------------
  78. -- Hook wrappers
  79. --
  80. -- We replace the global action_radio_start / action_radio_stop
  81. -- (and their music-player equivalents) in ui_pda_radio_tab with
  82. -- thin wrappers that call the original then fire our logic.
  83. --
  84. -- NOTE: radio_state / plyr_state are *local* inside ui_pda_radio_tab
  85. -- and cannot be read from outside the module. We call mute()
  86. -- unconditionally after start -- safe because mute() itself is
  87. -- idempotent (guards with is_radio_muted). The original start
  88. -- functions already guard against double-starts internally.
  89. --
  90. -- Known gap: action_plyr_next / action_plyr_previous can silently
  91. -- stop the player when the playlist is exhausted (without routing
  92. -- through action_plyr_stop). We cannot detect this without reading
  93. -- the local plyr_state, so ambient stays muted until the user
  94. -- presses stop manually. Hooking next/prev and calling restore()
  95. -- would un-mute on every normal track change, which is worse.
  96. -- ----------------------------------------------------------------
  97.  
  98. local _orig_radio_start = nil
  99. local _orig_radio_stop = nil
  100. local _orig_plyr_start = nil
  101. local _orig_plyr_stop = nil
  102.  
  103. local function install_hooks()
  104. if not ui_pda_radio_tab then
  105. printf("[radio_mute] ui_pda_radio_tab not found -- hooks skipped")
  106. return
  107. end
  108.  
  109. if not _orig_radio_start then
  110. _orig_radio_start = ui_pda_radio_tab.action_radio_start
  111. ui_pda_radio_tab.action_radio_start = function(...)
  112. _orig_radio_start(...)
  113. mute()
  114. end
  115. end
  116.  
  117. if not _orig_radio_stop then
  118. _orig_radio_stop = ui_pda_radio_tab.action_radio_stop
  119. ui_pda_radio_tab.action_radio_stop = function(...)
  120. _orig_radio_stop(...)
  121. restore()
  122. end
  123. end
  124.  
  125. if not _orig_plyr_start then
  126. _orig_plyr_start = ui_pda_radio_tab.action_plyr_start
  127. ui_pda_radio_tab.action_plyr_start = function(...)
  128. _orig_plyr_start(...)
  129. mute()
  130. end
  131. end
  132.  
  133. if not _orig_plyr_stop then
  134. _orig_plyr_stop = ui_pda_radio_tab.action_plyr_stop
  135. ui_pda_radio_tab.action_plyr_stop = function(...)
  136. _orig_plyr_stop(...)
  137. restore()
  138. end
  139. end
  140. end
  141.  
  142. local function remove_hooks()
  143. if _orig_radio_start then ui_pda_radio_tab.action_radio_start = _orig_radio_start; _orig_radio_start = nil end
  144. if _orig_radio_stop then ui_pda_radio_tab.action_radio_stop = _orig_radio_stop; _orig_radio_stop = nil end
  145. if _orig_plyr_start then ui_pda_radio_tab.action_plyr_start = _orig_plyr_start; _orig_plyr_start = nil end
  146. if _orig_plyr_stop then ui_pda_radio_tab.action_plyr_stop = _orig_plyr_stop; _orig_plyr_stop = nil end
  147. end
  148.  
  149.  
  150. -- ----------------------------------------------------------------
  151. -- Lifecycle callbacks
  152. -- ----------------------------------------------------------------
  153.  
  154. local function on_first_update()
  155. install_hooks()
  156. end
  157.  
  158. local function on_load()
  159. if is_radio_muted then restore() end
  160. is_radio_muted = false
  161. vol_backup = nil
  162. _orig_radio_start = nil
  163. _orig_radio_stop = nil
  164. _orig_plyr_start = nil
  165. _orig_plyr_stop = nil
  166. install_hooks()
  167. end
  168.  
  169. local function on_game_end()
  170. if is_radio_muted then restore() end
  171. remove_hooks()
  172. end
  173.  
  174.  
  175. -- ----------------------------------------------------------------
  176. -- Register
  177. -- ----------------------------------------------------------------
  178.  
  179. function on_game_start()
  180. RegisterScriptCallback("actor_on_first_update", on_first_update)
  181. RegisterScriptCallback("on_game_load", on_load)
  182. RegisterScriptCallback("on_game_end", on_game_end)
  183. end
  184.  
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment