Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. RadioWavs = {};
  2.  
  3. RadioWavs.soundCache = {};
  4.  
  5. RadioWavs.files = {};
  6.  
  7. RadioWavs.files[1] = "RadioMusic/ComeAsYouAre"
  8.  
  9.  
  10. RadioWavs.stopSoundNumber = -1; -- number that triggers sounds to stop
  11. RadioWavs.cacheSize = 10; -- sound id's to keep in cache
  12.  
  13. function RadioWavs.PlaySound(_number, _source)
  14. local device = _source:getDeviceData()
  15. local emitter
  16. local sound3d
  17. local volume
  18.  
  19. device:setBaseVolumeRange(0)
  20. if device:isVehicleDevice() == false then
  21. emitter = device:getEmitter()
  22. volume = device:getDeviceVolume() * 0.4F
  23. sound3d = true
  24. else
  25. local vehiclePart = device:getParent()
  26. if vehiclePart then
  27. local vehicle = vehiclePart:getVehicle()
  28. if vehicle then
  29. emitter = vehicle:getEmitter() -- use car's emitter because car radios don't have one
  30. if vehicle == getPlayer():getVehicle() then -- player is in the car
  31. volume = device:getDeviceVolume() * 0.4F
  32. sound3d = false -- no 3d sound while in car, it sounds glitchy
  33. else
  34. volume = device:getDeviceVolume() * 0.2F
  35. sound3d = true
  36. end
  37. end
  38. end
  39. if not emitter then
  40. print("RadioWavs mod ERROR: no SoundEmitter found!")
  41. return
  42. end
  43. end
  44.  
  45. if _number==RadioWavs.stopSoundNumber then
  46. for _,t in ipairs(RadioWavs.soundCache) do
  47. emitter:stopSound(t.soundID);
  48. end
  49. elseif RadioWavs.files[_number] then
  50. emitter:setPos(_source:getX(), _source:getY(), _source:getZ())
  51. for _,t in ipairs(RadioWavs.soundCache) do
  52. if emitter:isPlaying(t.soundID) then
  53. if t.soundID == RadioWavs.files[_number] then
  54. emitter:setVolume(soundID, volume) -- song is already active, just pump up the volume
  55. return
  56. else
  57. emitter:stopSound(t.soundID); -- an old song, stop it
  58. end
  59. end
  60. end
  61. local soundID = emitter:playSound(RadioWavs.files[_number])
  62. emitter:setVolume(soundID, volume)
  63. emitter:set3D(soundID, sound3d)
  64.  
  65. local t = {}
  66. t.device = device
  67. t.emitter = emitter
  68. t.soundID = soundID
  69. t.channel = device:getChannel()
  70. table.insert( RadioWavs.soundCache, 1, t )
  71. if #RadioWavs.soundCache>RadioWavs.cacheSize then
  72. for i=RadioWavs.cacheSize+1,#RadioWavs.soundCache do
  73. table.remove(RadioWavs.soundCache,i)
  74. end
  75. end
  76. end
  77. end
  78.  
  79.  
  80.  
  81. function RadioWavs.split(str,sep)
  82. local sep, fields = sep or ":", {};
  83. local pattern = string.format("([^%s]+)", sep);
  84. str:gsub(pattern, function(c) fields[#fields+1] = c end);
  85. return fields;
  86. end
  87. function RadioWavs.OnDeviceText(_interactCodes, _x, _y, _z, _line, _source)
  88. local codes = RadioWavs.split(_interactCodes, ",");
  89. local rData = _source:getDeviceData();
  90. for _,_v in ipairs(codes) do
  91. if _v:len() > 4 then
  92. local code = string.sub(_v, 1, 3);
  93. local op = string.sub(_v, 4, 4);
  94. local amount = tonumber(string.sub(_v, 5, _v:len()));
  95. if op=="-" then
  96. amount = -amount
  97. end
  98. if amount ~= nil and code=="DRU" then
  99. RadioWavs.PlaySound(amount, _source);
  100. end
  101. end
  102. end
  103. end
  104. Events.OnDeviceText.Add( RadioWavs.OnDeviceText );
  105.  
  106.  
  107.  
  108. function RadioWavs.OnEnterVehicle(_player)
  109. local emitter = _player:getVehicle():getEmitter()
  110. for _,t in ipairs(RadioWavs.soundCache) do
  111. if t.emitter == emitter then
  112. t.emitter:setVolume(t.soundID, t.device:getDeviceVolume() * 0.4F);
  113. t.emitter:set3D(t.soundID, false) -- no 3d sound while in car, it sounds glitchy
  114. break
  115. end
  116. end
  117. end
  118. Events.OnEnterVehicle.Add( RadioWavs.OnEnterVehicle );
  119.  
  120. local originalExitVehicle = ISExitVehicle.perform;
  121. function ISExitVehicle:perform()
  122. local emitter = self.character:getVehicle():getEmitter()
  123. for _,t in ipairs(RadioWavs.soundCache) do
  124. if t.emitter == emitter then
  125. t.emitter:setVolume(t.soundID, t.device:getDeviceVolume() * 0.2F);
  126. t.emitter:set3D(t.soundID, true)-- enable 3d sound when outside the car
  127. break
  128. end
  129. end
  130. originalExitVehicle(self)
  131. end
  132.  
  133.  
  134. local originalToggle = ISRadioAction.performToggleOnOff;
  135. function ISRadioAction:performToggleOnOff()
  136. originalToggle(self);
  137.  
  138. local volume = 0
  139. if self.deviceData:getIsTurnedOn() then
  140. volume = self.deviceData:getDeviceVolume() * 0.4F
  141. end
  142. for _,t in ipairs(RadioWavs.soundCache) do
  143. if t.device == self.deviceData then
  144. t.emitter:setVolume(t.soundID, volume); -- mute/ummute sound
  145. end
  146. end
  147. end
  148.  
  149. local originalSetChannel = ISRadioAction.performSetChannel;
  150. function ISRadioAction:performSetChannel()
  151. for _,t in ipairs(RadioWavs.soundCache) do
  152. if t.device == self.deviceData then
  153. if t.channel == self.secondaryItem then -- switching back to chanel
  154. t.emitter:setVolume(t.soundID, t.device:getDeviceVolume() * 0.4F); -- ummute sound
  155. else
  156. t.emitter:setVolume(t.soundID, 0); -- mute sound, but keep playing
  157. end
  158. end
  159. end
  160. originalSetChannel(self);
  161. end
  162.  
  163. local originalSetVolume = ISRadioAction.performSetVolume;
  164. function ISRadioAction:performSetVolume()
  165. if self:isValidSetVolume() then
  166. originalSetVolume(self);
  167. for _,t in ipairs(RadioWavs.soundCache) do
  168. if t.device == self.deviceData then
  169. t.emitter:setVolume(t.soundID, t.device:getDeviceVolume() * 0.4F);
  170. end
  171. end
  172. end
  173. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement