World.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. e.Channel = chat.Channel;
  132. _OnChat(this, e);
  133. return;
  134. }
  135. // Avatar?
  136. if (chat.Sender != null && chat.SenderObject == null)
  137. {
  138. ChatEventArgs e = new ChatEventArgs();
  139. e.Sender = new SPAvatar(m_internalScene, chat.SenderUUID, m_security);
  140. e.Text = chat.Message;
  141. e.Channel = chat.Channel;
  142. _OnChat(this, e);
  143. return;
  144. }
  145. // Skip if other
  146. }
  147. void EventManager_OnChatFromClient(object sender, OSChatMessage chat)
  148. {
  149. if (_OnChat != null)
  150. {
  151. HandleChatPacket(chat);
  152. return;
  153. }
  154. }
  155. #endregion
  156. #endregion
  157. public IObjectAccessor Objects
  158. {
  159. get { return m_objs; }
  160. }
  161. public IParcel[] Parcels
  162. {
  163. get
  164. {
  165. List<ILandObject> m_los = m_internalScene.LandChannel.AllParcels();
  166. List<IParcel> m_parcels = new List<IParcel>(m_los.Count);
  167. foreach (ILandObject landObject in m_los)
  168. {
  169. m_parcels.Add(new LOParcel(m_internalScene, landObject.LandData.LocalID));
  170. }
  171. return m_parcels.ToArray();
  172. }
  173. }
  174. public IAvatar[] Avatars
  175. {
  176. get
  177. {
  178. List<EntityBase> ents = m_internalScene.Entities.GetAllByType<ScenePresence>();
  179. IAvatar[] rets = new IAvatar[ents.Count];
  180. for (int i = 0; i < ents.Count; i++)
  181. {
  182. EntityBase ent = ents[i];
  183. rets[i] = new SPAvatar(m_internalScene, ent.UUID, m_security);
  184. }
  185. return rets;
  186. }
  187. }
  188. public IHeightmap Terrain
  189. {
  190. get { return m_heights; }
  191. }
  192. #region Implementation of IWorldAudio
  193. public void PlaySound(UUID audio, Vector3 position, double volume)
  194. {
  195. ISoundModule soundModule = m_internalScene.RequestModuleInterface<ISoundModule>();
  196. if (soundModule != null)
  197. {
  198. soundModule.TriggerSound(audio, UUID.Zero, UUID.Zero, UUID.Zero, volume, position,
  199. m_internalScene.RegionInfo.RegionHandle);
  200. }
  201. }
  202. public void PlaySound(UUID audio, Vector3 position)
  203. {
  204. ISoundModule soundModule = m_internalScene.RequestModuleInterface<ISoundModule>();
  205. if (soundModule != null)
  206. {
  207. soundModule.TriggerSound(audio, UUID.Zero, UUID.Zero, UUID.Zero, 1.0, position,
  208. m_internalScene.RegionInfo.RegionHandle);
  209. }
  210. }
  211. #endregion
  212. }
  213. }