Advertisement
ForbodingAngel

Untitled

Feb 25th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. function gadget:GetInfo()
  2. return {
  3. name = "Randomised Death Sounds",
  4. desc = "Assign and play classes of unit death sounds",
  5. author = "FLOZi (C. Lawrence), rewrite of DeathSounds.lua by Argh",
  6. date = "19/05/2011",
  7. license = "Public Domain",
  8. layer = 1,
  9. enabled = true
  10. }
  11. end
  12.  
  13. -- Localisations
  14. local random = math.random
  15.  
  16. -- Synced Read
  17. local GetUnitDefID = Spring.GetUnitDefID
  18. local GetUnitNeutral = Spring.GetUnitNeutral
  19. local GetUnitPosition = Spring.GetUnitPosition
  20.  
  21. -- Unsynced Ctrl
  22. local PlaySoundFile = Spring.PlaySoundFile
  23.  
  24. -- constants
  25. local DEFAULT_VOLUME = 15
  26. local SOUNDS_PATH = "sounds/deathsounds/"
  27. local GAIA_TEAM_ID = Spring.GetGaiaTeamID()
  28.  
  29. -- variables
  30. local soundClasses = {}
  31. local soundClassSizes = {}
  32. local udSoundCache = {}
  33. local udVolumeCache = {}
  34.  
  35. -- included for RecursiveFileSearch
  36. local VFSUtils = VFS.Include('gamedata/VFSUtils.lua')
  37.  
  38. if (gadgetHandler:IsSyncedCode()) then
  39. -- SYNCED
  40.  
  41. function gadget:Initialize()
  42. local subDirs = VFS.SubDirs(SOUNDS_PATH)
  43. for i = 1, #subDirs do
  44. local subDir = subDirs[i]
  45. if not subDir:match("/%.svn") then
  46. local dirName = string.gsub(subDir,SOUNDS_PATH,'')
  47. dirName = string.gsub(dirName,"/",'')
  48. -- Spring.Echo (dirName)
  49. soundClasses[dirName] = RecursiveFileSearch(subDir)
  50. soundClassSizes[dirName] = #soundClasses[dirName]
  51. end
  52. end
  53. for unitDefID, unitDef in pairs(UnitDefs) do
  54. local cp = unitDef.customParams
  55. if cp and cp.death_sounds then
  56. udSoundCache[unitDefID] = cp.death_sounds
  57. udVolumeCache[unitDefID] = cp.death_volume or DEFAULT_VOLUME
  58. end
  59. end
  60. end
  61.  
  62.  
  63. function gadget:UnitDestroyed(unitID, unitDefID, teamId,attackerID)
  64. if attackerID ~= nil then --Add this so that units who are destroyed via lua (like salvaging) or self destructed, will not play an overlapping sound effect
  65. local soundClass = udSoundCache[unitDefID]
  66. -- Spring.Echo (soundClass)
  67. -- Spring.Echo (soundClasses)
  68. -- Spring.Echo (soundClassSizes)
  69. if soundClass then
  70. local choice = random(soundClassSizes[soundClass])
  71. local x, y, z = GetUnitPosition(unitID)
  72. local volume = udVolumeCache[unitDefID]
  73. PlaySoundFile(soundClasses[soundClass][choice], volume, x, y, z)
  74. end
  75. end
  76. end
  77.  
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement