1
0

CollisionSounds.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 (part.CollisionSoundType < 0)
  105. return;
  106. if (collidersinfolist.Count == 0 || part == null)
  107. return;
  108. if (part.VolumeDetectActive || (part.Flags & PrimFlags.Physics) == 0)
  109. return;
  110. SceneObjectGroup sog = part.ParentGroup;
  111. if (sog == null || sog.IsDeleted || sog.inTransit)
  112. return;
  113. if(sog.CollisionSoundThrottled(part.CollisionSoundType))
  114. return;
  115. float volume = part.CollisionSoundVolume;
  116. UUID soundID = part.CollisionSound;
  117. bool HaveSound = false;
  118. switch (part.CollisionSoundType)
  119. {
  120. case 0: // default sounds
  121. volume = 1.0f;
  122. break;
  123. case 1: // selected sound
  124. if(soundID == part.invalidCollisionSoundUUID)
  125. return;
  126. HaveSound = true;
  127. break;
  128. case 2: // default sounds with volume set by script
  129. default:
  130. break;
  131. }
  132. if (volume == 0.0f)
  133. return;
  134. bool doneownsound = false;
  135. int thisMaterial = (int)part.Material;
  136. if (thisMaterial >= MaxMaterials)
  137. thisMaterial = 3;
  138. int thisMatScaled = thisMaterial * MaxMaterials;
  139. CollisionForSoundInfo colInfo;
  140. uint id;
  141. for(int i = 0; i < collidersinfolist.Count; i++)
  142. {
  143. colInfo = collidersinfolist[i];
  144. id = colInfo.colliderID;
  145. if (id == 0) // terrain collision
  146. {
  147. if (!doneownsound)
  148. {
  149. if (!HaveSound)
  150. {
  151. float vol = Math.Abs(colInfo.relativeVel);
  152. if (vol < 0.2f)
  153. continue;
  154. vol *= vol * .0625f; // 4m/s == full volume
  155. if (vol > 1.0f)
  156. vol = 1.0f;
  157. soundID = m_TerrainPart[thisMaterial];
  158. volume *= vol;
  159. }
  160. part.SendCollisionSound(soundID, volume, colInfo.position);
  161. doneownsound = true;
  162. }
  163. continue;
  164. }
  165. SceneObjectPart otherPart = sog.Scene.GetSceneObjectPart(id);
  166. if (otherPart != null)
  167. {
  168. SceneObjectGroup othersog = otherPart.ParentGroup;
  169. if(othersog == null || othersog.IsDeleted || othersog.inTransit)
  170. continue;
  171. int otherType = otherPart.CollisionSoundType;
  172. if (otherType < 0 || otherPart.VolumeDetectActive)
  173. continue;
  174. if (!HaveSound)
  175. {
  176. if(othersog.CollisionSoundThrottled(otherType))
  177. continue;
  178. if (otherType == 1)
  179. {
  180. soundID = otherPart.CollisionSound;
  181. volume = otherPart.CollisionSoundVolume;
  182. if (volume == 0.0f)
  183. continue;
  184. }
  185. else
  186. {
  187. if (otherType == 2)
  188. {
  189. volume = otherPart.CollisionSoundVolume;
  190. if (volume == 0.0f)
  191. continue;
  192. }
  193. float vol = Math.Abs(colInfo.relativeVel);
  194. if (vol < 0.2f)
  195. continue;
  196. vol *= vol * .0625f; // 4m/s == full volume
  197. if (vol > 1.0f)
  198. vol = 1.0f;
  199. int otherMaterial = (int)otherPart.Material;
  200. if (otherMaterial >= MaxMaterials)
  201. otherMaterial = 3;
  202. soundID = m_PartPart[thisMatScaled + otherMaterial];
  203. volume *= vol;
  204. }
  205. }
  206. if (doneownsound)
  207. otherPart.SendCollisionSound(soundID, volume, colInfo.position);
  208. else
  209. {
  210. part.SendCollisionSound(soundID, volume, colInfo.position);
  211. doneownsound = true;
  212. }
  213. }
  214. }
  215. }
  216. public static void AvatarCollisionSound(ScenePresence av, List<CollisionForSoundInfo> collidersinfolist)
  217. {
  218. if (collidersinfolist.Count == 0 || av == null)
  219. return;
  220. UUID soundID;
  221. int otherMaterial;
  222. int thisMaterial = 4; // flesh
  223. int thisMatScaled = thisMaterial * MaxMaterials;
  224. // bool doneownsound = false;
  225. CollisionForSoundInfo colInfo;
  226. uint id;
  227. float volume;
  228. for(int i = 0; i< collidersinfolist.Count; i++)
  229. {
  230. colInfo = collidersinfolist[i];
  231. id = colInfo.colliderID;
  232. if (id == 0) // no terrain collision sounds for now
  233. {
  234. continue;
  235. // volume = Math.Abs(colInfo.relativeVel);
  236. // if (volume < 0.2f)
  237. // continue;
  238. }
  239. SceneObjectPart otherPart = av.Scene.GetSceneObjectPart(id);
  240. if (otherPart != null)
  241. {
  242. if (otherPart.CollisionSoundType < 0)
  243. continue;
  244. if (otherPart.CollisionSoundType == 1 && otherPart.CollisionSoundVolume > 0f)
  245. otherPart.SendCollisionSound(otherPart.CollisionSound, otherPart.CollisionSoundVolume, colInfo.position);
  246. else
  247. {
  248. float volmod = 1.0f;
  249. if (otherPart.CollisionSoundType == 2)
  250. {
  251. volmod = otherPart.CollisionSoundVolume;
  252. if(volmod == 0.0)
  253. continue;
  254. }
  255. volume = Math.Abs(colInfo.relativeVel);
  256. // Most noral collisions (running into walls, stairs)
  257. // should never be heard.
  258. if (volume < 3.2f)
  259. continue;
  260. // m_log.DebugFormat("Collision speed was {0}", volume);
  261. // Cap to 0.2 times volume because climbing stairs should not be noisy
  262. // Also changed scaling
  263. volume *= volume * .0125f; // 4m/s == volume 0.2
  264. if (volume > 0.2f)
  265. volume = 0.2f;
  266. otherMaterial = (int)otherPart.Material;
  267. if (otherMaterial >= MaxMaterials)
  268. otherMaterial = 3;
  269. volume *= volmod;
  270. soundID = m_PartPart[thisMatScaled + otherMaterial];
  271. otherPart.SendCollisionSound(soundID, volume, colInfo.position);
  272. }
  273. continue;
  274. }
  275. /*
  276. else if (!doneownsound)
  277. {
  278. ScenePresence otherav = av.Scene.GetScenePresence(Id);
  279. if (otherav != null && (!otherav.IsChildAgent))
  280. {
  281. soundID = snd_FleshFlesh;
  282. av.SendCollisionSound(soundID, 1.0);
  283. doneownsound = true;
  284. }
  285. }
  286. */
  287. }
  288. }
  289. }
  290. }