Scene.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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 OpenSim 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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Threading;
  31. using System.Timers;
  32. using libsecondlife;
  33. using libsecondlife.Packets;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Communications;
  36. using OpenSim.Framework.Console;
  37. using OpenSim.Framework.Interfaces;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Framework.Types;
  40. using OpenSim.Physics.Manager;
  41. using OpenSim.Region.Caches;
  42. using OpenSim.Region.Environment.Scripting;
  43. using OpenSim.Region.Terrain;
  44. using Caps=OpenSim.Region.Capabilities.Caps;
  45. using Timer=System.Timers.Timer;
  46. namespace OpenSim.Region.Environment.Scenes
  47. {
  48. public delegate bool FilterAvatarList(ScenePresence avatar);
  49. public partial class Scene : SceneBase, ILocalStorageReceiver
  50. {
  51. protected Timer m_heartbeatTimer = new Timer();
  52. protected Dictionary<LLUUID, ScenePresence> Avatars;
  53. protected Dictionary<LLUUID, SceneObject> Prims;
  54. private PhysicsScene phyScene;
  55. private float timeStep = 0.1f;
  56. private Random Rand = new Random();
  57. private uint _primCount = 702000;
  58. private System.Threading.Mutex _primAllocateMutex = new Mutex(false);
  59. private int storageCount;
  60. private Mutex updateLock;
  61. protected AuthenticateSessionsBase authenticateHandler;
  62. protected RegionCommsListener regionCommsHost;
  63. protected CommunicationsManager commsManager;
  64. protected Dictionary<LLUUID, Caps> capsHandlers = new Dictionary<LLUUID, Caps>();
  65. protected BaseHttpServer httpListener;
  66. public ParcelManager parcelManager;
  67. public EstateManager estateManager;
  68. public EventManager eventManager;
  69. public ScriptManager scriptManager;
  70. #region Properties
  71. /// <summary>
  72. ///
  73. /// </summary>
  74. public PhysicsScene PhysScene
  75. {
  76. set
  77. {
  78. this.phyScene = value;
  79. }
  80. get
  81. {
  82. return (this.phyScene);
  83. }
  84. }
  85. #endregion
  86. #region Constructors
  87. /// <summary>
  88. /// Creates a new World class, and a region to go with it.
  89. /// </summary>
  90. /// <param name="clientThreads">Dictionary to contain client threads</param>
  91. /// <param name="regionHandle">Region Handle for this region</param>
  92. /// <param name="regionName">Region Name for this region</param>
  93. public Scene(ClientManager clientManager, RegionInfo regInfo, AuthenticateSessionsBase authen, CommunicationsManager commsMan, AssetCache assetCach, BaseHttpServer httpServer)
  94. {
  95. updateLock = new Mutex(false);
  96. this.authenticateHandler = authen;
  97. this.commsManager = commsMan;
  98. this.assetCache = assetCach;
  99. m_clientManager = clientManager;
  100. m_regInfo = regInfo;
  101. m_regionHandle = m_regInfo.RegionHandle;
  102. m_regionName = m_regInfo.RegionName;
  103. this.m_datastore = m_regInfo.DataStore;
  104. this.RegisterRegionWithComms();
  105. parcelManager = new ParcelManager(this, this.m_regInfo);
  106. estateManager = new EstateManager(this, this.m_regInfo);
  107. scriptManager = new ScriptManager(this);
  108. eventManager = new EventManager();
  109. MainLog.Instance.Verbose("World.cs - creating new entitities instance");
  110. Entities = new Dictionary<LLUUID, EntityBase>();
  111. Avatars = new Dictionary<LLUUID, ScenePresence>();
  112. Prims = new Dictionary<LLUUID, SceneObject>();
  113. MainLog.Instance.Verbose("World.cs - creating LandMap");
  114. Terrain = new TerrainEngine();
  115. ScenePresence.LoadAnims();
  116. this.httpListener = httpServer;
  117. }
  118. #endregion
  119. /// <summary>
  120. ///
  121. /// </summary>
  122. public void StartTimer()
  123. {
  124. m_heartbeatTimer.Enabled = true;
  125. m_heartbeatTimer.Interval = 100;
  126. m_heartbeatTimer.Elapsed += new ElapsedEventHandler(this.Heartbeat);
  127. }
  128. #region Update Methods
  129. /// <summary>
  130. /// Performs per-frame updates regularly
  131. /// </summary>
  132. /// <param name="sender"></param>
  133. /// <param name="e"></param>
  134. void Heartbeat(object sender, EventArgs e)
  135. {
  136. this.Update();
  137. }
  138. /// <summary>
  139. /// Performs per-frame updates on the world, this should be the central world loop
  140. /// </summary>
  141. public override void Update()
  142. {
  143. updateLock.WaitOne();
  144. try
  145. {
  146. if (this.phyScene.IsThreaded)
  147. {
  148. this.phyScene.GetResults();
  149. }
  150. foreach (LLUUID UUID in Entities.Keys)
  151. {
  152. Entities[UUID].updateMovement();
  153. }
  154. lock (this.m_syncRoot)
  155. {
  156. this.phyScene.Simulate(timeStep);
  157. }
  158. foreach (LLUUID UUID in Entities.Keys)
  159. {
  160. Entities[UUID].update();
  161. }
  162. // General purpose event manager
  163. eventManager.TriggerOnFrame();
  164. //backup world data
  165. this.storageCount++;
  166. if (storageCount > 1200) //set to how often you want to backup
  167. {
  168. this.Backup();
  169. storageCount = 0;
  170. }
  171. }
  172. catch (Exception e)
  173. {
  174. MainLog.Instance.Warn("World.cs: Update() - Failed with exception " + e.ToString());
  175. }
  176. updateLock.ReleaseMutex();
  177. }
  178. /// <summary>
  179. ///
  180. /// </summary>
  181. /// <returns></returns>
  182. public bool Backup()
  183. {
  184. return true;
  185. }
  186. #endregion
  187. #region Regenerate Terrain
  188. /// <summary>
  189. /// Rebuilds the terrain using a procedural algorithm
  190. /// </summary>
  191. public void RegenerateTerrain()
  192. {
  193. try
  194. {
  195. Terrain.hills();
  196. lock (this.m_syncRoot)
  197. {
  198. this.phyScene.SetTerrain(Terrain.getHeights1D());
  199. }
  200. this.localStorage.SaveMap(this.Terrain.getHeights1D());
  201. m_clientManager.ForEachClient(delegate(IClientAPI client)
  202. {
  203. this.SendLayerData(client);
  204. });
  205. foreach (LLUUID UUID in Entities.Keys)
  206. {
  207. Entities[UUID].LandRenegerated();
  208. }
  209. }
  210. catch (Exception e)
  211. {
  212. MainLog.Instance.Warn("World.cs: RegenerateTerrain() - Failed with exception " + e.ToString());
  213. }
  214. }
  215. /// <summary>
  216. /// Rebuilds the terrain using a 2D float array
  217. /// </summary>
  218. /// <param name="newMap">256,256 float array containing heights</param>
  219. public void RegenerateTerrain(float[,] newMap)
  220. {
  221. try
  222. {
  223. this.Terrain.setHeights2D(newMap);
  224. lock (this.m_syncRoot)
  225. {
  226. this.phyScene.SetTerrain(this.Terrain.getHeights1D());
  227. }
  228. this.localStorage.SaveMap(this.Terrain.getHeights1D());
  229. m_clientManager.ForEachClient(delegate(IClientAPI client)
  230. {
  231. this.SendLayerData(client);
  232. });
  233. foreach (LLUUID UUID in Entities.Keys)
  234. {
  235. Entities[UUID].LandRenegerated();
  236. }
  237. }
  238. catch (Exception e)
  239. {
  240. MainLog.Instance.Warn("World.cs: RegenerateTerrain() - Failed with exception " + e.ToString());
  241. }
  242. }
  243. /// <summary>
  244. /// Rebuilds the terrain assuming changes occured at a specified point[?]
  245. /// </summary>
  246. /// <param name="changes">???</param>
  247. /// <param name="pointx">???</param>
  248. /// <param name="pointy">???</param>
  249. public void RegenerateTerrain(bool changes, int pointx, int pointy)
  250. {
  251. try
  252. {
  253. if (changes)
  254. {
  255. /* Dont save here, rely on tainting system instead */
  256. m_clientManager.ForEachClient(delegate(IClientAPI client)
  257. {
  258. this.SendLayerData(pointx, pointy, client);
  259. });
  260. }
  261. }
  262. catch (Exception e)
  263. {
  264. MainLog.Instance.Warn("World.cs: RegenerateTerrain() - Failed with exception " + e.ToString());
  265. }
  266. }
  267. #endregion
  268. #region Load Terrain
  269. /// <summary>
  270. /// Loads the World heightmap
  271. /// </summary>
  272. ///
  273. public override void LoadWorldMap()
  274. {
  275. try
  276. {
  277. float[] map = this.localStorage.LoadWorld();
  278. if (map == null)
  279. {
  280. if (string.IsNullOrEmpty(this.m_regInfo.estateSettings.terrainFile))
  281. {
  282. Console.WriteLine("No default terrain, procedurally generating...");
  283. this.Terrain.hills();
  284. this.localStorage.SaveMap(this.Terrain.getHeights1D());
  285. }
  286. else
  287. {
  288. try
  289. {
  290. this.Terrain.loadFromFileF32(this.m_regInfo.estateSettings.terrainFile);
  291. this.Terrain *= this.m_regInfo.estateSettings.terrainMultiplier;
  292. }
  293. catch
  294. {
  295. Console.WriteLine("Unable to load default terrain, procedurally generating instead...");
  296. Terrain.hills();
  297. }
  298. this.localStorage.SaveMap(this.Terrain.getHeights1D());
  299. }
  300. }
  301. else
  302. {
  303. this.Terrain.setHeights1D(map);
  304. }
  305. CreateTerrainTexture();
  306. }
  307. catch (Exception e)
  308. {
  309. MainLog.Instance.Warn("World.cs: LoadWorldMap() - Failed with exception " + e.ToString());
  310. }
  311. }
  312. /// <summary>
  313. ///
  314. /// </summary>
  315. private void CreateTerrainTexture()
  316. {
  317. //create a texture asset of the terrain
  318. byte[] data = this.Terrain.exportJpegImage("defaultstripe.png");
  319. this.m_regInfo.estateSettings.terrainImageID = LLUUID.Random();
  320. AssetBase asset = new AssetBase();
  321. asset.FullID = this.m_regInfo.estateSettings.terrainImageID;
  322. asset.Data = data;
  323. asset.Name = "terrainImage";
  324. asset.Type = 0;
  325. this.assetCache.AddAsset(asset);
  326. }
  327. #endregion
  328. #region Primitives Methods
  329. /// <summary>
  330. /// Loads the World's objects
  331. /// </summary>
  332. public void LoadPrimsFromStorage()
  333. {
  334. try
  335. {
  336. MainLog.Instance.Verbose("World.cs: LoadPrimsFromStorage() - Loading primitives");
  337. this.localStorage.LoadPrimitives(this);
  338. }
  339. catch (Exception e)
  340. {
  341. MainLog.Instance.Warn("World.cs: LoadPrimsFromStorage() - Failed with exception " + e.ToString());
  342. }
  343. }
  344. /// <summary>
  345. /// Loads a specific object from storage
  346. /// </summary>
  347. /// <param name="prim">The object to load</param>
  348. public void PrimFromStorage(PrimData prim)
  349. {
  350. }
  351. /// <summary>
  352. /// Returns a new unallocated primitive ID
  353. /// </summary>
  354. /// <returns>A brand new primitive ID</returns>
  355. public uint PrimIDAllocate()
  356. {
  357. uint myID;
  358. _primAllocateMutex.WaitOne();
  359. ++_primCount;
  360. myID = _primCount;
  361. _primAllocateMutex.ReleaseMutex();
  362. return myID;
  363. }
  364. /// <summary>
  365. ///
  366. /// </summary>
  367. /// <param name="addPacket"></param>
  368. /// <param name="agentClient"></param>
  369. public void AddNewPrim(Packet addPacket, IClientAPI agentClient)
  370. {
  371. AddNewPrim((ObjectAddPacket)addPacket, agentClient.AgentId);
  372. }
  373. /// <summary>
  374. ///
  375. /// </summary>
  376. /// <param name="addPacket"></param>
  377. /// <param name="ownerID"></param>
  378. public void AddNewPrim(ObjectAddPacket addPacket, LLUUID ownerID)
  379. {
  380. try
  381. {
  382. SceneObject sceneOb = new SceneObject(m_regionHandle, this, addPacket, ownerID, this.PrimIDAllocate());
  383. this.Entities.Add(sceneOb.rootUUID, sceneOb);
  384. // Trigger event for listeners
  385. // eventManager.TriggerOnNewPrimitive(prim);
  386. }
  387. catch (Exception e)
  388. {
  389. MainLog.Instance.Warn("World.cs: AddNewPrim() - Failed with exception " + e.ToString());
  390. }
  391. }
  392. public override uint AddNewPrim(LLUUID ownerId, PrimData primData, LLVector3 pos, LLQuaternion rotation, LLUUID texture, int flags)
  393. {
  394. uint id = NextLocalId;
  395. throw new NotImplementedException("Not implemented yet.");
  396. }
  397. #endregion
  398. #region Add/Remove Avatar Methods
  399. /// <summary>
  400. ///
  401. /// </summary>
  402. /// <param name="remoteClient"></param
  403. /// <param name="agentID"></param>
  404. /// <param name="child"></param>
  405. public override void AddNewClient(IClientAPI client, bool child)
  406. {
  407. client.OnRegionHandShakeReply += this.SendLayerData;
  408. //remoteClient.OnRequestWearables += new GenericCall(this.GetInitialPrims);
  409. client.OnChatFromViewer += this.SimChat;
  410. client.OnInstantMessage += this.InstantMessage;
  411. client.OnRequestWearables += this.InformClientOfNeighbours;
  412. client.OnAddPrim += this.AddNewPrim;
  413. client.OnUpdatePrimGroupPosition += this.UpdatePrimPosition;
  414. client.OnUpdatePrimSinglePosition += this.UpdatePrimSinglePosition;
  415. client.OnUpdatePrimGroupRotation += this.UpdatePrimRotation;
  416. client.OnUpdatePrimGroupMouseRotation += this.UpdatePrimRotation;
  417. client.OnUpdatePrimSingleRotation += this.UpdatePrimSingleRotation;
  418. client.OnUpdatePrimScale += this.UpdatePrimScale;
  419. client.OnUpdatePrimShape += this.UpdatePrimShape;
  420. client.OnRequestMapBlocks += this.RequestMapBlocks;
  421. client.OnTeleportLocationRequest += this.RequestTeleportLocation;
  422. client.OnObjectSelect += this.SelectPrim;
  423. client.OnGrapUpdate += this.MoveObject;
  424. client.OnNameFromUUIDRequest += this.commsManager.HandleUUIDNameRequest;
  425. client.OnObjectDescription += this.PrimDescription;
  426. client.OnObjectName += this.PrimName;
  427. client.OnLinkObjects += this.LinkObjects;
  428. client.OnObjectDuplicate += this.DuplicateObject;
  429. /* remoteClient.OnParcelPropertiesRequest += new ParcelPropertiesRequest(parcelManager.handleParcelPropertiesRequest);
  430. remoteClient.OnParcelDivideRequest += new ParcelDivideRequest(parcelManager.handleParcelDivideRequest);
  431. remoteClient.OnParcelJoinRequest += new ParcelJoinRequest(parcelManager.handleParcelJoinRequest);
  432. remoteClient.OnParcelPropertiesUpdateRequest += new ParcelPropertiesUpdateRequest(parcelManager.handleParcelPropertiesUpdateRequest);
  433. remoteClient.OnEstateOwnerMessage += new EstateOwnerMessageRequest(estateManager.handleEstateOwnerMessage);
  434. */
  435. this.estateManager.sendRegionHandshake(client);
  436. CreateAndAddScenePresence(client);
  437. return;
  438. }
  439. protected void CreateAndAddScenePresence(IClientAPI client)
  440. {
  441. ScenePresence newAvatar = null;
  442. MainLog.Instance.Verbose("World.cs:AddViewerAgent() - Creating new avatar for remote viewer agent");
  443. newAvatar = new ScenePresence(client, this, this.m_regInfo);
  444. MainLog.Instance.Verbose("World.cs:AddViewerAgent() - Adding new avatar to world");
  445. MainLog.Instance.Verbose("World.cs:AddViewerAgent() - Starting RegionHandshake ");
  446. PhysicsVector pVec = new PhysicsVector(newAvatar.Pos.X, newAvatar.Pos.Y, newAvatar.Pos.Z);
  447. lock (this.m_syncRoot)
  448. {
  449. newAvatar.PhysActor = this.phyScene.AddAvatar(pVec);
  450. }
  451. lock (Entities)
  452. {
  453. if (!Entities.ContainsKey(client.AgentId))
  454. {
  455. this.Entities.Add(client.AgentId, newAvatar);
  456. }
  457. else
  458. {
  459. Entities[client.AgentId] = newAvatar;
  460. }
  461. }
  462. lock (Avatars)
  463. {
  464. if (Avatars.ContainsKey(client.AgentId))
  465. {
  466. Avatars[client.AgentId] = newAvatar;
  467. }
  468. else
  469. {
  470. this.Avatars.Add(client.AgentId, newAvatar);
  471. }
  472. }
  473. }
  474. /// <summary>
  475. ///
  476. /// </summary>
  477. /// <param name="agentID"></param>
  478. public override void RemoveClient(LLUUID agentID)
  479. {
  480. eventManager.TriggerOnRemovePresence(agentID);
  481. ScenePresence avatar = this.RequestAvatar(agentID);
  482. m_clientManager.ForEachClient(
  483. delegate(IClientAPI client)
  484. {
  485. client.SendKillObject(avatar.RegionHandle, avatar.LocalId);
  486. });
  487. lock (Avatars) {
  488. if (Avatars.ContainsKey(agentID)) {
  489. Avatars.Remove(agentID);
  490. }
  491. }
  492. lock (Entities) {
  493. if (Entities.ContainsKey(agentID)) {
  494. Entities.Remove(agentID);
  495. }
  496. }
  497. // TODO: Add the removal from physics ?
  498. return;
  499. }
  500. #endregion
  501. #region Request Avatars List Methods
  502. //The idea is to have a group of method that return a list of avatars meeting some requirement
  503. // ie it could be all Avatars within a certain range of the calling prim/avatar.
  504. /// <summary>
  505. /// Request a List of all Avatars in this World
  506. /// </summary>
  507. /// <returns></returns>
  508. public List<ScenePresence> RequestAvatarList()
  509. {
  510. List<ScenePresence> result = new List<ScenePresence>();
  511. foreach (ScenePresence avatar in Avatars.Values)
  512. {
  513. result.Add(avatar);
  514. }
  515. return result;
  516. }
  517. /// <summary>
  518. /// Request a filtered list of Avatars in this World
  519. /// </summary>
  520. /// <returns></returns>
  521. public List<ScenePresence> RequestAvatarList(FilterAvatarList filter)
  522. {
  523. List<ScenePresence> result = new List<ScenePresence>();
  524. foreach (ScenePresence avatar in Avatars.Values)
  525. {
  526. if (filter(avatar))
  527. {
  528. result.Add(avatar);
  529. }
  530. }
  531. return result;
  532. }
  533. /// <summary>
  534. /// Request a Avatar by UUID
  535. /// </summary>
  536. /// <param name="avatarID"></param>
  537. /// <returns></returns>
  538. public ScenePresence RequestAvatar(LLUUID avatarID)
  539. {
  540. if (this.Avatars.ContainsKey(avatarID))
  541. {
  542. return Avatars[avatarID];
  543. }
  544. return null;
  545. }
  546. #endregion
  547. /// <summary>
  548. ///
  549. /// </summary>
  550. /// <param name="entID"></param>
  551. /// <returns></returns>
  552. public bool DeleteEntity(LLUUID entID)
  553. {
  554. if (this.Entities.ContainsKey(entID))
  555. {
  556. this.Entities.Remove(entID);
  557. return true;
  558. }
  559. return false;
  560. }
  561. public void SendAllSceneObjectsToClient(IClientAPI client)
  562. {
  563. foreach (EntityBase ent in Entities.Values)
  564. {
  565. if (ent is SceneObject)
  566. {
  567. ((SceneObject)ent).SendAllChildPrimsToClient(client);
  568. }
  569. }
  570. }
  571. #region RegionCommsHost
  572. /// <summary>
  573. ///
  574. /// </summary>
  575. public void RegisterRegionWithComms()
  576. {
  577. this.regionCommsHost = this.commsManager.GridServer.RegisterRegion(this.m_regInfo);
  578. if (this.regionCommsHost != null)
  579. {
  580. this.regionCommsHost.OnExpectUser += this.NewUserConnection;
  581. this.regionCommsHost.OnAvatarCrossingIntoRegion += this.AgentCrossing;
  582. }
  583. }
  584. /// <summary>
  585. ///
  586. /// </summary>
  587. /// <param name="regionHandle"></param>
  588. /// <param name="agent"></param>
  589. public void NewUserConnection(ulong regionHandle, AgentCircuitData agent)
  590. {
  591. // Console.WriteLine("World.cs - add new user connection");
  592. //should just check that its meant for this region
  593. if (regionHandle == this.m_regInfo.RegionHandle)
  594. {
  595. if (agent.CapsPath != "")
  596. {
  597. //Console.WriteLine("new user, so creating caps handler for it");
  598. Caps cap = new Caps(this.assetCache, httpListener, this.m_regInfo.ExternalHostName, this.m_regInfo.ExternalEndPoint.Port, agent.CapsPath, agent.AgentID);
  599. cap.RegisterHandlers();
  600. this.capsHandlers.Add(agent.AgentID, cap);
  601. }
  602. this.authenticateHandler.AddNewCircuit(agent.circuitcode, agent);
  603. }
  604. }
  605. public void AgentCrossing(ulong regionHandle, LLUUID agentID, LLVector3 position)
  606. {
  607. if (regionHandle == this.m_regInfo.RegionHandle)
  608. {
  609. if (this.Avatars.ContainsKey(agentID))
  610. {
  611. this.Avatars[agentID].MakeAvatar(position);
  612. }
  613. }
  614. }
  615. /// <summary>
  616. ///
  617. /// </summary>
  618. public void InformClientOfNeighbours(IClientAPI remoteClient)
  619. {
  620. List<RegionInfo> neighbours = this.commsManager.GridServer.RequestNeighbours(this.m_regInfo);
  621. if (neighbours != null)
  622. {
  623. for (int i = 0; i < neighbours.Count; i++)
  624. {
  625. AgentCircuitData agent = remoteClient.RequestClientInfo();
  626. agent.BaseFolder = LLUUID.Zero;
  627. agent.InventoryFolder = LLUUID.Zero;
  628. agent.startpos = new LLVector3(128, 128, 70);
  629. agent.child = true;
  630. this.commsManager.InterRegion.InformRegionOfChildAgent(neighbours[i].RegionHandle, agent);
  631. remoteClient.InformClientOfNeighbour(neighbours[i].RegionHandle, neighbours[i].ExternalEndPoint);
  632. //this.capsHandlers[remoteClient.AgentId].CreateEstablishAgentComms("", System.Net.IPAddress.Parse(neighbours[i].CommsIPListenAddr) + ":" + neighbours[i].CommsIPListenPort);
  633. }
  634. }
  635. }
  636. /// <summary>
  637. ///
  638. /// </summary>
  639. /// <param name="regionHandle"></param>
  640. /// <returns></returns>
  641. public RegionInfo RequestNeighbouringRegionInfo(ulong regionHandle)
  642. {
  643. return this.commsManager.GridServer.RequestNeighbourInfo(regionHandle);
  644. }
  645. /// <summary>
  646. ///
  647. /// </summary>
  648. /// <param name="minX"></param>
  649. /// <param name="minY"></param>
  650. /// <param name="maxX"></param>
  651. /// <param name="maxY"></param>
  652. public void RequestMapBlocks(IClientAPI remoteClient, int minX, int minY, int maxX, int maxY)
  653. {
  654. List<MapBlockData> mapBlocks;
  655. mapBlocks = this.commsManager.GridServer.RequestNeighbourMapBlocks(minX, minY, maxX, maxY);
  656. remoteClient.SendMapBlock(mapBlocks);
  657. }
  658. /// <summary>
  659. ///
  660. /// </summary>
  661. /// <param name="remoteClient"></param>
  662. /// <param name="RegionHandle"></param>
  663. /// <param name="position"></param>
  664. /// <param name="lookAt"></param>
  665. /// <param name="flags"></param>
  666. public void RequestTeleportLocation(IClientAPI remoteClient, ulong regionHandle, LLVector3 position, LLVector3 lookAt, uint flags)
  667. {
  668. if (regionHandle == this.m_regionHandle)
  669. {
  670. if (this.Avatars.ContainsKey(remoteClient.AgentId))
  671. {
  672. remoteClient.SendTeleportLocationStart();
  673. remoteClient.SendLocalTeleport(position, lookAt, flags);
  674. this.Avatars[remoteClient.AgentId].Teleport(position);
  675. }
  676. }
  677. else
  678. {
  679. RegionInfo reg = this.RequestNeighbouringRegionInfo(regionHandle);
  680. if (reg != null)
  681. {
  682. remoteClient.SendTeleportLocationStart();
  683. AgentCircuitData agent = remoteClient.RequestClientInfo();
  684. agent.BaseFolder = LLUUID.Zero;
  685. agent.InventoryFolder = LLUUID.Zero;
  686. agent.startpos = new LLVector3(128, 128, 70);
  687. agent.child = true;
  688. this.commsManager.InterRegion.InformRegionOfChildAgent(regionHandle, agent);
  689. this.commsManager.InterRegion.ExpectAvatarCrossing(regionHandle, remoteClient.AgentId, position);
  690. remoteClient.SendRegionTeleport(regionHandle, 13, reg.ExternalEndPoint, 4, (1 << 4));
  691. }
  692. //remoteClient.SendTeleportCancel();
  693. }
  694. }
  695. /// <summary>
  696. ///
  697. /// </summary>
  698. /// <param name="regionhandle"></param>
  699. /// <param name="agentID"></param>
  700. /// <param name="position"></param>
  701. public bool InformNeighbourOfCrossing(ulong regionhandle, LLUUID agentID, LLVector3 position)
  702. {
  703. return this.commsManager.InterRegion.ExpectAvatarCrossing(regionhandle, agentID, position);
  704. }
  705. #endregion
  706. }
  707. }