PEScene.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 copyrightD
  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. using System;
  28. using System.Collections.Generic;
  29. using Nini.Config;
  30. using log4net;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Physics.Manager;
  33. using OpenMetaverse;
  34. using OpenSim.Region.Framework;
  35. using OpenSim.Region.CoreModules.RegionSync.RegionSyncModule;
  36. namespace OpenSim.Region.Physics.PEPlugin
  37. {
  38. public class PEScene : PhysicsScene
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  41. private List<PECharacter> m_avatars = new List<PECharacter>();
  42. private List<PEPrim> m_prims = new List<PEPrim>();
  43. private float[] m_heightMap;
  44. public PEScene(string identifier)
  45. {
  46. }
  47. public override void Initialise(IMesher meshmerizer, IConfigSource config)
  48. {
  49. }
  50. public override PhysicsActor AddAvatar(string avName, Vector3 position, Vector3 size, bool isFlying)
  51. {
  52. PECharacter actor = new PECharacter(avName, this, position, null, size, 0f, 0f, .5f, 1f,
  53. 1f, 1f, .5f, .5f);
  54. lock (m_avatars) m_avatars.Add(actor);
  55. return actor;
  56. }
  57. public override void RemoveAvatar(PhysicsActor actor)
  58. {
  59. try
  60. {
  61. lock (m_avatars) m_avatars.Remove((PECharacter)actor);
  62. }
  63. catch (Exception e)
  64. {
  65. m_log.WarnFormat("[RPE]: Attempt to remove avatar that is not in physics scene: {0}", e);
  66. }
  67. }
  68. public override void RemovePrim(PhysicsActor prim)
  69. {
  70. try
  71. {
  72. lock (m_prims) m_prims.Remove((PEPrim)prim);
  73. }
  74. catch (Exception e)
  75. {
  76. m_log.WarnFormat("[RPE]: Attempt to remove prim that is not in physics scene: {0}", e);
  77. }
  78. }
  79. public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
  80. Vector3 size, Quaternion rotation, bool isPhysical, uint localID)
  81. {
  82. PEPrim prim = new PEPrim(primName, this, position, size, rotation, null, pbs, isPhysical, null);
  83. prim.LocalID = localID;
  84. lock (m_prims) m_prims.Add(prim);
  85. return prim;
  86. }
  87. public override void AddPhysicsActorTaint(PhysicsActor prim) { }
  88. public override float Simulate(float timeStep)
  89. {
  90. // if we are a physics engine server, send update information
  91. if (SceneToPhysEngineSyncServer.IsPhysEngineScene2S())
  92. {
  93. if (SceneToPhysEngineSyncServer.IsActivePhysEngineScene2S())
  94. {
  95. // m_log.DebugFormat("[RPE]: Simulate. p={0}, a={1}", m_prims.Count, m_avatars.Count);
  96. /*
  97. lock (m_prims)
  98. {
  99. foreach (PEPrim prim in m_prims)
  100. {
  101. // if the values have changed and it was I who changed them, send an update
  102. if (prim.ChangingActorID == RegionSyncServerModule.ActorID && prim.lastValues.Changed(prim))
  103. {
  104. SceneToPhysEngineSyncServer.RouteUpdate(prim);
  105. }
  106. }
  107. }
  108. */
  109. lock (m_avatars)
  110. {
  111. foreach (PECharacter actor in m_avatars)
  112. {
  113. // m_log.DebugFormat("[RPE]: Simulate. p={0}, a={1}", m_prims.Count, m_avatars.Count);
  114. // if the values have changed and it was I who changed them, send an update
  115. if (actor.ChangingActorID == RegionSyncServerModule.ActorID && actor.lastValues.Changed(actor))
  116. {
  117. SceneToPhysEngineSyncServer.RouteUpdate(actor);
  118. actor.ChangingActorID = "YY";
  119. }
  120. }
  121. }
  122. }
  123. return 60f;
  124. }
  125. /*
  126. // code borrowed from BasicPhysics to do just avatar movement
  127. foreach (PECharacter actor in m_avatars)
  128. {
  129. Vector3 actorPosition = actor.Position;
  130. Vector3 actorVelocity = actor.Velocity;
  131. actorPosition.X += actor.Velocity.X*timeStep;
  132. actorPosition.Y += actor.Velocity.Y*timeStep;
  133. actorPosition.Y = Math.Max(actorPosition.Y, 0.1f);
  134. actorPosition.Y = Math.Min(actorPosition.Y, Constants.RegionSize - 0.1f);
  135. actorPosition.X = Math.Max(actorPosition.X, 0.1f);
  136. actorPosition.X = Math.Min(actorPosition.X, Constants.RegionSize - 0.1f);
  137. float height = 25.0F;
  138. try
  139. {
  140. height = m_heightMap[(int)actor.Position.Y * Constants.RegionSize + (int)actor.Position.X] + actor.Size.Z;
  141. }
  142. catch (OverflowException)
  143. {
  144. m_log.WarnFormat("[RPE]: Actor out of range: {0}", actor.SOPName, actor.Position.ToString());
  145. }
  146. if (actor.Flying)
  147. {
  148. if (actor.Position.Z + (actor.Velocity.Z*timeStep) <
  149. m_heightMap[(int)actor.Position.Y * Constants.RegionSize + (int)actor.Position.X] + 2)
  150. {
  151. actorPosition.Z = height;
  152. actorVelocity.Z = 0;
  153. actor.IsColliding = true;
  154. }
  155. else
  156. {
  157. actorPosition.Z += actor.Velocity.Z*timeStep;
  158. actor.IsColliding = false;
  159. }
  160. }
  161. else
  162. {
  163. actorPosition.Z = height;
  164. actorVelocity.Z = 0;
  165. actor.IsColliding = true;
  166. }
  167. actor.Position = actorPosition;
  168. actor.Velocity = actorVelocity;
  169. }
  170. */
  171. return 60f; // returns frames per second
  172. }
  173. public override void GetResults() { }
  174. public override void SetTerrain(float[] heightMap) {
  175. m_heightMap = heightMap;
  176. }
  177. public override void SetWaterLevel(float baseheight) { }
  178. public override void DeleteTerrain() { }
  179. public override void Dispose() { }
  180. public override Dictionary<uint, float> GetTopColliders()
  181. {
  182. return new Dictionary<uint, float>();
  183. }
  184. public override bool IsThreaded { get { return false; } }
  185. }
  186. }