World.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. using System.Collections.Generic;
  28. using OpenMetaverse;
  29. using OpenSim.Framework;
  30. using OpenSim.Region.Framework.Interfaces;
  31. using OpenSim.Region.Framework.Scenes;
  32. using OpenSim.Region.OptionalModules.Scripting.Minimodule.WorldX;
  33. namespace OpenSim.Region.OptionalModules.Scripting.Minimodule
  34. {
  35. public class World : System.MarshalByRefObject, IWorld, IWorldAudio
  36. {
  37. private readonly Scene m_internalScene;
  38. private readonly ISecurityCredential m_security;
  39. private readonly Heightmap m_heights;
  40. private readonly ObjectAccessor m_objs;
  41. public World(Scene internalScene, ISecurityCredential securityCredential)
  42. {
  43. m_security = securityCredential;
  44. m_internalScene = internalScene;
  45. m_heights = new Heightmap(m_internalScene);
  46. m_objs = new ObjectAccessor(m_internalScene, securityCredential);
  47. }
  48. #region Events
  49. #region OnNewUser
  50. private event OnNewUserDelegate _OnNewUser;
  51. private bool _OnNewUserActive;
  52. public event OnNewUserDelegate OnNewUser
  53. {
  54. add
  55. {
  56. if (!_OnNewUserActive)
  57. {
  58. _OnNewUserActive = true;
  59. m_internalScene.EventManager.OnNewPresence += EventManager_OnNewPresence;
  60. }
  61. _OnNewUser += value;
  62. }
  63. remove
  64. {
  65. _OnNewUser -= value;
  66. if (_OnNewUser == null)
  67. {
  68. _OnNewUserActive = false;
  69. m_internalScene.EventManager.OnNewPresence -= EventManager_OnNewPresence;
  70. }
  71. }
  72. }
  73. void EventManager_OnNewPresence(ScenePresence presence)
  74. {
  75. if (_OnNewUser != null)
  76. {
  77. NewUserEventArgs e = new NewUserEventArgs();
  78. e.Avatar = new SPAvatar(m_internalScene, presence.UUID, m_security);
  79. _OnNewUser(this, e);
  80. }
  81. }
  82. #endregion
  83. #region OnChat
  84. private event OnChatDelegate _OnChat;
  85. private bool _OnChatActive;
  86. public IWorldAudio Audio
  87. {
  88. get { return this; }
  89. }
  90. public event OnChatDelegate OnChat
  91. {
  92. add
  93. {
  94. if (!_OnChatActive)
  95. {
  96. _OnChatActive = true;
  97. m_internalScene.EventManager.OnChatFromClient += EventManager_OnChatFromClient;
  98. m_internalScene.EventManager.OnChatFromWorld += EventManager_OnChatFromWorld;
  99. }
  100. _OnChat += value;
  101. }
  102. remove
  103. {
  104. _OnChat -= value;
  105. if (_OnChat == null)
  106. {
  107. _OnChatActive = false;
  108. m_internalScene.EventManager.OnChatFromClient -= EventManager_OnChatFromClient;
  109. m_internalScene.EventManager.OnChatFromWorld -= EventManager_OnChatFromWorld;
  110. }
  111. }
  112. }
  113. void EventManager_OnChatFromWorld(object sender, OSChatMessage chat)
  114. {
  115. if (_OnChat != null)
  116. {
  117. HandleChatPacket(chat);
  118. return;
  119. }
  120. }
  121. private void HandleChatPacket(OSChatMessage chat)
  122. {
  123. if (string.IsNullOrEmpty(chat.Message))
  124. return;
  125. // Object?
  126. if (chat.Sender == null && chat.SenderObject != null)
  127. {
  128. ChatEventArgs e = new ChatEventArgs();
  129. e.Sender = new SOPObject(m_internalScene, ((SceneObjectPart) chat.SenderObject).LocalId, m_security);
  130. e.Text = chat.Message;
  131. _OnChat(this, e);
  132. return;
  133. }
  134. // Avatar?
  135. if (chat.Sender != null && chat.SenderObject == null)
  136. {
  137. ChatEventArgs e = new ChatEventArgs();
  138. e.Sender = new SPAvatar(m_internalScene, chat.SenderUUID, m_security);
  139. e.Text = chat.Message;
  140. _OnChat(this, e);
  141. return;
  142. }
  143. // Skip if other
  144. }
  145. void EventManager_OnChatFromClient(object sender, OSChatMessage chat)
  146. {
  147. if (_OnChat != null)
  148. {
  149. HandleChatPacket(chat);
  150. return;
  151. }
  152. }
  153. #endregion
  154. #endregion
  155. public IObjectAccessor Objects
  156. {
  157. get { return m_objs; }
  158. }
  159. public IParcel[] Parcels
  160. {
  161. get
  162. {
  163. List<ILandObject> m_los = m_internalScene.LandChannel.AllParcels();
  164. List<IParcel> m_parcels = new List<IParcel>(m_los.Count);
  165. foreach (ILandObject landObject in m_los)
  166. {
  167. m_parcels.Add(new LOParcel(m_internalScene, landObject.LandData.LocalID));
  168. }
  169. return m_parcels.ToArray();
  170. }
  171. }
  172. public IAvatar[] Avatars
  173. {
  174. get
  175. {
  176. List<EntityBase> ents = m_internalScene.Entities.GetAllByType<ScenePresence>();
  177. IAvatar[] rets = new IAvatar[ents.Count];
  178. for (int i = 0; i < ents.Count; i++)
  179. {
  180. EntityBase ent = ents[i];
  181. rets[i] = new SPAvatar(m_internalScene, ent.UUID, m_security);
  182. }
  183. return rets;
  184. }
  185. }
  186. public IHeightmap Terrain
  187. {
  188. get { return m_heights; }
  189. }
  190. #region Implementation of IWorldAudio
  191. public void PlaySound(UUID audio, Vector3 position, double volume)
  192. {
  193. ISoundModule soundModule = m_internalScene.RequestModuleInterface<ISoundModule>();
  194. if (soundModule != null)
  195. {
  196. soundModule.TriggerSound(audio, UUID.Zero, UUID.Zero, UUID.Zero, volume, position,
  197. m_internalScene.RegionInfo.RegionHandle);
  198. }
  199. }
  200. public void PlaySound(UUID audio, Vector3 position)
  201. {
  202. ISoundModule soundModule = m_internalScene.RequestModuleInterface<ISoundModule>();
  203. if (soundModule != null)
  204. {
  205. soundModule.TriggerSound(audio, UUID.Zero, UUID.Zero, UUID.Zero, 1.0, position,
  206. m_internalScene.RegionInfo.RegionHandle);
  207. }
  208. }
  209. #endregion
  210. }
  211. }