CollisionSounds.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // Ubit 2012
  28. using System;
  29. using System.Reflection;
  30. using System.Collections.Generic;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using log4net;
  34. namespace OpenSim.Region.Framework.Scenes
  35. {
  36. public struct CollisionForSoundInfo
  37. {
  38. public uint colliderID;
  39. public Vector3 position;
  40. public float relativeVel;
  41. }
  42. public static class CollisionSounds
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private const int MaxMaterials = 7;
  46. // part part
  47. private static UUID snd_StoneStone = new UUID("be7295c0-a158-11e1-b3dd-0800200c9a66");
  48. private static UUID snd_StoneMetal = new UUID("be7295c0-a158-11e1-b3dd-0800201c9a66");
  49. private static UUID snd_StoneGlass = new UUID("be7295c0-a158-11e1-b3dd-0800202c9a66");
  50. private static UUID snd_StoneWood = new UUID("be7295c0-a158-11e1-b3dd-0800203c9a66");
  51. private static UUID snd_StoneFlesh = new UUID("be7295c0-a158-11e1-b3dd-0800204c9a66");
  52. private static UUID snd_StonePlastic = new UUID("be7295c0-a158-11e1-b3dd-0800205c9a66");
  53. private static UUID snd_StoneRubber = new UUID("be7295c0-a158-11e1-b3dd-0800206c9a66");
  54. private static UUID snd_MetalMetal = new UUID("be7295c0-a158-11e1-b3dd-0801201c9a66");
  55. private static UUID snd_MetalGlass = new UUID("be7295c0-a158-11e1-b3dd-0801202c9a66");
  56. private static UUID snd_MetalWood = new UUID("be7295c0-a158-11e1-b3dd-0801203c9a66");
  57. private static UUID snd_MetalFlesh = new UUID("be7295c0-a158-11e1-b3dd-0801204c9a66");
  58. private static UUID snd_MetalPlastic = new UUID("be7295c0-a158-11e1-b3dd-0801205c9a66");
  59. private static UUID snd_MetalRubber = new UUID("be7295c0-a158-11e1-b3dd-0801206c9a66");
  60. private static UUID snd_GlassGlass = new UUID("be7295c0-a158-11e1-b3dd-0802202c9a66");
  61. private static UUID snd_GlassWood = new UUID("be7295c0-a158-11e1-b3dd-0802203c9a66");
  62. private static UUID snd_GlassFlesh = new UUID("be7295c0-a158-11e1-b3dd-0802204c9a66");
  63. private static UUID snd_GlassPlastic = new UUID("be7295c0-a158-11e1-b3dd-0802205c9a66");
  64. private static UUID snd_GlassRubber = new UUID("be7295c0-a158-11e1-b3dd-0802206c9a66");
  65. private static UUID snd_WoodWood = new UUID("be7295c0-a158-11e1-b3dd-0803203c9a66");
  66. private static UUID snd_WoodFlesh = new UUID("be7295c0-a158-11e1-b3dd-0803204c9a66");
  67. private static UUID snd_WoodPlastic = new UUID("be7295c0-a158-11e1-b3dd-0803205c9a66");
  68. private static UUID snd_WoodRubber = new UUID("be7295c0-a158-11e1-b3dd-0803206c9a66");
  69. private static UUID snd_FleshFlesh = new UUID("be7295c0-a158-11e1-b3dd-0804204c9a66");
  70. private static UUID snd_FleshPlastic = new UUID("be7295c0-a158-11e1-b3dd-0804205c9a66");
  71. private static UUID snd_FleshRubber = new UUID("be7295c0-a158-11e1-b3dd-0804206c9a66");
  72. private static UUID snd_PlasticPlastic = new UUID("be7295c0-a158-11e1-b3dd-0805205c9a66");
  73. private static UUID snd_PlasticRubber = new UUID("be7295c0-a158-11e1-b3dd-0805206c9a66");
  74. private static UUID snd_RubberRubber = new UUID("be7295c0-a158-11e1-b3dd-0806206c9a66");
  75. // terrain part
  76. private static UUID snd_TerrainStone = new UUID("be7295c0-a158-11e1-b3dd-0807200c9a66");
  77. private static UUID snd_TerrainMetal = new UUID("be7295c0-a158-11e1-b3dd-0807200c9a66");
  78. private static UUID snd_TerrainGlass = new UUID("be7295c0-a158-11e1-b3dd-0807200c9a66");
  79. private static UUID snd_TerrainWood = new UUID("be7295c0-a158-11e1-b3dd-0807200c9a66");
  80. private static UUID snd_TerrainFlesh = new UUID("be7295c0-a158-11e1-b3dd-0807200c9a66");
  81. private static UUID snd_TerrainPlastic = new UUID("be7295c0-a158-11e1-b3dd-0807200c9a66");
  82. private static UUID snd_TerrainRubber = new UUID("be7295c0-a158-11e1-b3dd-0807200c9a66");
  83. public static UUID[] m_TerrainPart = {
  84. snd_TerrainStone,
  85. snd_TerrainMetal,
  86. snd_TerrainGlass,
  87. snd_TerrainWood,
  88. snd_TerrainFlesh,
  89. snd_TerrainPlastic,
  90. snd_TerrainRubber
  91. };
  92. // simetric sounds
  93. public static UUID[] m_PartPart = {
  94. snd_StoneStone, snd_StoneMetal, snd_StoneGlass, snd_StoneWood, snd_StoneFlesh, snd_StonePlastic, snd_StoneRubber,
  95. snd_StoneMetal, snd_MetalMetal, snd_MetalGlass, snd_MetalWood, snd_MetalFlesh, snd_MetalPlastic, snd_MetalRubber,
  96. snd_StoneGlass, snd_MetalGlass, snd_GlassGlass, snd_GlassWood, snd_GlassFlesh, snd_GlassPlastic, snd_GlassRubber,
  97. snd_StoneWood, snd_MetalWood, snd_GlassWood, snd_WoodWood, snd_WoodFlesh, snd_WoodPlastic, snd_WoodRubber,
  98. snd_StoneFlesh, snd_MetalFlesh, snd_GlassFlesh, snd_WoodFlesh, snd_FleshFlesh, snd_FleshPlastic, snd_FleshRubber,
  99. snd_StonePlastic, snd_MetalPlastic, snd_GlassPlastic, snd_WoodPlastic, snd_FleshPlastic, snd_PlasticPlastic, snd_PlasticRubber,
  100. snd_StoneRubber, snd_MetalRubber, snd_GlassRubber, snd_WoodRubber, snd_FleshRubber, snd_PlasticRubber, snd_RubberRubber
  101. };
  102. public static void PartCollisionSound(SceneObjectPart part, List<CollisionForSoundInfo> collidersinfolist)
  103. {
  104. if (collidersinfolist.Count == 0 || part == null)
  105. return;
  106. if (part.VolumeDetectActive || (part.Flags & PrimFlags.Physics) == 0)
  107. return;
  108. if (part.ParentGroup == null)
  109. return;
  110. if (part.CollisionSoundType < 0)
  111. return;
  112. float volume = part.CollisionSoundVolume;
  113. UUID soundID = part.CollisionSound;
  114. bool HaveSound = false;
  115. switch (part.CollisionSoundType)
  116. {
  117. case 0: // default sounds
  118. volume = 1.0f;
  119. break;
  120. case 1: // selected sound
  121. if(soundID == part.invalidCollisionSoundUUID)
  122. return;
  123. HaveSound = true;
  124. break;
  125. case 2: // default sounds with volume set by script
  126. default:
  127. break;
  128. }
  129. if (volume == 0.0f)
  130. return;
  131. bool doneownsound = false;
  132. int thisMaterial = (int)part.Material;
  133. if (thisMaterial >= MaxMaterials)
  134. thisMaterial = 3;
  135. int thisMatScaled = thisMaterial * MaxMaterials;
  136. CollisionForSoundInfo colInfo;
  137. uint id;
  138. for(int i = 0; i < collidersinfolist.Count; i++)
  139. {
  140. colInfo = collidersinfolist[i];
  141. id = colInfo.colliderID;
  142. if (id == 0) // terrain collision
  143. {
  144. if (!doneownsound)
  145. {
  146. if (!HaveSound)
  147. {
  148. float vol = Math.Abs(colInfo.relativeVel);
  149. if (vol < 0.2f)
  150. continue;
  151. vol *= vol * .0625f; // 4m/s == full volume
  152. if (vol > 1.0f)
  153. vol = 1.0f;
  154. soundID = m_TerrainPart[thisMaterial];
  155. volume *= vol;
  156. }
  157. part.SendCollisionSound(soundID, volume, colInfo.position);
  158. doneownsound = true;
  159. }
  160. continue;
  161. }
  162. SceneObjectPart otherPart = part.ParentGroup.Scene.GetSceneObjectPart(id);
  163. if (otherPart != null)
  164. {
  165. if (otherPart.CollisionSoundType < 0 || otherPart.VolumeDetectActive)
  166. continue;
  167. if (!HaveSound)
  168. {
  169. if (otherPart.CollisionSoundType == 1)
  170. {
  171. soundID = otherPart.CollisionSound;
  172. volume = otherPart.CollisionSoundVolume;
  173. if (volume == 0.0f)
  174. continue;
  175. }
  176. else
  177. {
  178. if (otherPart.CollisionSoundType == 2)
  179. {
  180. volume = otherPart.CollisionSoundVolume;
  181. if (volume == 0.0f)
  182. continue;
  183. }
  184. float vol = Math.Abs(colInfo.relativeVel);
  185. if (vol < 0.2f)
  186. continue;
  187. vol *= vol * .0625f; // 4m/s == full volume
  188. if (vol > 1.0f)
  189. vol = 1.0f;
  190. int otherMaterial = (int)otherPart.Material;
  191. if (otherMaterial >= MaxMaterials)
  192. otherMaterial = 3;
  193. soundID = m_PartPart[thisMatScaled + otherMaterial];
  194. volume *= vol;
  195. }
  196. }
  197. if (doneownsound)
  198. otherPart.SendCollisionSound(soundID, volume, colInfo.position);
  199. else
  200. {
  201. part.SendCollisionSound(soundID, volume, colInfo.position);
  202. doneownsound = true;
  203. }
  204. }
  205. }
  206. }
  207. public static void AvatarCollisionSound(ScenePresence av, List<CollisionForSoundInfo> collidersinfolist)
  208. {
  209. if (collidersinfolist.Count == 0 || av == null)
  210. return;
  211. UUID soundID;
  212. int otherMaterial;
  213. int thisMaterial = 4; // flesh
  214. int thisMatScaled = thisMaterial * MaxMaterials;
  215. // bool doneownsound = false;
  216. CollisionForSoundInfo colInfo;
  217. uint id;
  218. float volume;
  219. for(int i = 0; i< collidersinfolist.Count; i++)
  220. {
  221. colInfo = collidersinfolist[i];
  222. id = colInfo.colliderID;
  223. if (id == 0) // no terrain collision sounds for now
  224. {
  225. continue;
  226. // volume = Math.Abs(colInfo.relativeVel);
  227. // if (volume < 0.2f)
  228. // continue;
  229. }
  230. SceneObjectPart otherPart = av.Scene.GetSceneObjectPart(id);
  231. if (otherPart != null)
  232. {
  233. if (otherPart.CollisionSoundType < 0)
  234. continue;
  235. if (otherPart.CollisionSoundType == 1 && otherPart.CollisionSoundVolume > 0f)
  236. otherPart.SendCollisionSound(otherPart.CollisionSound, otherPart.CollisionSoundVolume, colInfo.position);
  237. else
  238. {
  239. float volmod = 1.0f;
  240. if (otherPart.CollisionSoundType == 2)
  241. {
  242. volmod = otherPart.CollisionSoundVolume;
  243. if(volmod == 0.0)
  244. continue;
  245. }
  246. volume = Math.Abs(colInfo.relativeVel);
  247. // Most noral collisions (running into walls, stairs)
  248. // should never be heard.
  249. if (volume < 3.2f)
  250. continue;
  251. // m_log.DebugFormat("Collision speed was {0}", volume);
  252. // Cap to 0.2 times volume because climbing stairs should not be noisy
  253. // Also changed scaling
  254. volume *= volume * .0125f; // 4m/s == volume 0.2
  255. if (volume > 0.2f)
  256. volume = 0.2f;
  257. otherMaterial = (int)otherPart.Material;
  258. if (otherMaterial >= MaxMaterials)
  259. otherMaterial = 3;
  260. volume *= volmod;
  261. soundID = m_PartPart[thisMatScaled + otherMaterial];
  262. otherPart.SendCollisionSound(soundID, volume, colInfo.position);
  263. }
  264. continue;
  265. }
  266. /*
  267. else if (!doneownsound)
  268. {
  269. ScenePresence otherav = av.Scene.GetScenePresence(Id);
  270. if (otherav != null && (!otherav.IsChildAgent))
  271. {
  272. soundID = snd_FleshFlesh;
  273. av.SendCollisionSound(soundID, 1.0);
  274. doneownsound = true;
  275. }
  276. }
  277. */
  278. }
  279. }
  280. }
  281. }