RegionCombinerModule.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using log4net;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Client;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Framework.Console;
  38. using OpenSim.Region.PhysicsModules.SharedBase;
  39. using Mono.Addins;
  40. namespace OpenSim.Region.RegionCombinerModule
  41. {
  42. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "RegionCombinerModule")]
  43. public class RegionCombinerModule : ISharedRegionModule, IRegionCombinerModule
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. // private static string LogHeader = "[REGION COMBINER MODULE]";
  47. public string Name
  48. {
  49. get { return "RegionCombinerModule"; }
  50. }
  51. public Type ReplaceableInterface
  52. {
  53. get { return null; }
  54. }
  55. /// <summary>
  56. /// Is this module enabled?
  57. /// </summary>
  58. private bool m_combineContiguousRegions = false;
  59. /// <summary>
  60. /// This holds the root regions for the megaregions.
  61. /// </summary>
  62. /// <remarks>
  63. /// Usually there is only ever one megaregion (and hence only one entry here).
  64. /// </remarks>
  65. private Dictionary<UUID, RegionConnections> m_regions = new Dictionary<UUID, RegionConnections>();
  66. /// <summary>
  67. /// The scenes that comprise the megaregion.
  68. /// </summary>
  69. private Dictionary<UUID, Scene> m_startingScenes = new Dictionary<UUID, Scene>();
  70. public void Initialise(IConfigSource source)
  71. {
  72. IConfig myConfig = source.Configs["Startup"];
  73. m_combineContiguousRegions = myConfig.GetBoolean("CombineContiguousRegions", false);
  74. MainConsole.Instance.Commands.AddCommand(
  75. "RegionCombinerModule", false, "fix-phantoms", "fix-phantoms",
  76. "Fixes phantom objects after an import to a megaregion or a change from a megaregion back to normal regions",
  77. FixPhantoms);
  78. }
  79. public void Close()
  80. {
  81. }
  82. public void AddRegion(Scene scene)
  83. {
  84. if (m_combineContiguousRegions)
  85. scene.RegisterModuleInterface<IRegionCombinerModule>(this);
  86. }
  87. public void RemoveRegion(Scene scene)
  88. {
  89. lock (m_startingScenes)
  90. m_startingScenes.Remove(scene.RegionInfo.originRegionID);
  91. }
  92. public void RegionLoaded(Scene scene)
  93. {
  94. lock (m_startingScenes)
  95. m_startingScenes.Add(scene.RegionInfo.originRegionID, scene);
  96. if (m_combineContiguousRegions)
  97. {
  98. RegionLoadedDoWork(scene);
  99. scene.EventManager.OnNewPresence += NewPresence;
  100. }
  101. }
  102. public bool IsRootForMegaregion(UUID regionId)
  103. {
  104. lock (m_regions)
  105. return m_regions.ContainsKey(regionId);
  106. }
  107. public Vector2 GetSizeOfMegaregion(UUID regionId)
  108. {
  109. lock (m_regions)
  110. {
  111. if (m_regions.ContainsKey(regionId))
  112. {
  113. RegionConnections rootConn = m_regions[regionId];
  114. return new Vector2((float)rootConn.XEnd, (float)rootConn.YEnd);
  115. }
  116. }
  117. throw new Exception(string.Format("Region with id {0} not found", regionId));
  118. }
  119. // Test to see if this postiion (relative to the region) is within the area covered
  120. // by this megaregion.
  121. public bool PositionIsInMegaregion(UUID currentRegion, int xx, int yy)
  122. {
  123. bool ret = false;
  124. if (xx < 0 || yy < 0)
  125. return ret;
  126. foreach (RegionConnections rootRegion in m_regions.Values)
  127. {
  128. if (currentRegion == rootRegion.RegionId)
  129. {
  130. // The caller is in the root region so this is an easy test
  131. if (xx < rootRegion.XEnd && yy < rootRegion.YEnd)
  132. {
  133. ret = true;
  134. }
  135. break;
  136. }
  137. else
  138. {
  139. // Maybe the caller is in one of the sub-regions
  140. foreach (RegionData childRegion in rootRegion.ConnectedRegions)
  141. {
  142. if (currentRegion == childRegion.RegionId)
  143. {
  144. // This is a child. Diddle the offsets and check if in
  145. Vector3 positionInMegaregion = childRegion.Offset;
  146. positionInMegaregion.X += xx;
  147. positionInMegaregion.Y += yy;
  148. if (positionInMegaregion.X < rootRegion.XEnd && positionInMegaregion.Y < rootRegion.YEnd)
  149. {
  150. ret = true;
  151. }
  152. break;
  153. }
  154. }
  155. }
  156. }
  157. return ret;
  158. }
  159. private void NewPresence(ScenePresence presence)
  160. {
  161. if (presence.IsChildAgent)
  162. {
  163. byte[] throttleData;
  164. try
  165. {
  166. throttleData = presence.ControllingClient.GetThrottlesPacked(1);
  167. }
  168. catch (NotImplementedException)
  169. {
  170. return;
  171. }
  172. if (throttleData == null)
  173. return;
  174. if (throttleData.Length == 0)
  175. return;
  176. if (throttleData.Length != 28)
  177. return;
  178. byte[] adjData;
  179. int pos = 0;
  180. if (!BitConverter.IsLittleEndian)
  181. {
  182. byte[] newData = new byte[7 * 4];
  183. Buffer.BlockCopy(throttleData, 0, newData, 0, 7 * 4);
  184. for (int i = 0; i < 7; i++)
  185. Array.Reverse(newData, i * 4, 4);
  186. adjData = newData;
  187. }
  188. else
  189. {
  190. adjData = throttleData;
  191. }
  192. // 0.125f converts from bits to bytes
  193. int resend = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
  194. int land = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
  195. int wind = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
  196. int cloud = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
  197. int task = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
  198. int texture = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); pos += 4;
  199. int asset = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f);
  200. // State is a subcategory of task that we allocate a percentage to
  201. //int total = resend + land + wind + cloud + task + texture + asset;
  202. byte[] data = new byte[7 * 4];
  203. int ii = 0;
  204. Buffer.BlockCopy(Utils.FloatToBytes(resend), 0, data, ii, 4); ii += 4;
  205. Buffer.BlockCopy(Utils.FloatToBytes(land * 50), 0, data, ii, 4); ii += 4;
  206. Buffer.BlockCopy(Utils.FloatToBytes(wind), 0, data, ii, 4); ii += 4;
  207. Buffer.BlockCopy(Utils.FloatToBytes(cloud), 0, data, ii, 4); ii += 4;
  208. Buffer.BlockCopy(Utils.FloatToBytes(task), 0, data, ii, 4); ii += 4;
  209. Buffer.BlockCopy(Utils.FloatToBytes(texture), 0, data, ii, 4); ii += 4;
  210. Buffer.BlockCopy(Utils.FloatToBytes(asset), 0, data, ii, 4);
  211. try
  212. {
  213. presence.ControllingClient.SetChildAgentThrottle(data);
  214. }
  215. catch (NotImplementedException)
  216. {
  217. return;
  218. }
  219. }
  220. }
  221. private void RegionLoadedDoWork(Scene scene)
  222. {
  223. /*
  224. // For testing on a single instance
  225. if (scene.RegionInfo.RegionLocX == 1004 && scene.RegionInfo.RegionLocY == 1000)
  226. return;
  227. //
  228. */
  229. RegionConnections newConn = new RegionConnections();
  230. newConn.ConnectedRegions = new List<RegionData>();
  231. newConn.RegionScene = scene;
  232. newConn.RegionLandChannel = scene.LandChannel;
  233. newConn.RegionId = scene.RegionInfo.originRegionID;
  234. newConn.X = scene.RegionInfo.RegionLocX;
  235. newConn.Y = scene.RegionInfo.RegionLocY;
  236. newConn.XEnd = scene.RegionInfo.RegionSizeX;
  237. newConn.YEnd = scene.RegionInfo.RegionSizeX;
  238. lock (m_regions)
  239. {
  240. bool connectedYN = false;
  241. foreach (RegionConnections rootConn in m_regions.Values)
  242. {
  243. #region commented
  244. /*
  245. // If we're one region over +x +y
  246. //xxy
  247. //xxx
  248. //xxx
  249. if ((((int)conn.X * (int)Constants.RegionSize) + conn.XEnd
  250. == (regionConnections.X * (int)Constants.RegionSize))
  251. && (((int)conn.Y * (int)Constants.RegionSize) - conn.YEnd
  252. == (regionConnections.Y * (int)Constants.RegionSize)))
  253. {
  254. Vector3 offset = Vector3.Zero;
  255. offset.X = (((regionConnections.X * (int) Constants.RegionSize)) -
  256. ((conn.X * (int) Constants.RegionSize)));
  257. offset.Y = (((regionConnections.Y * (int) Constants.RegionSize)) -
  258. ((conn.Y * (int) Constants.RegionSize)));
  259. Vector3 extents = Vector3.Zero;
  260. extents.Y = regionConnections.YEnd + conn.YEnd;
  261. extents.X = conn.XEnd + conn.XEnd;
  262. m_log.DebugFormat("Scene: {0} to the northwest of Scene{1}. Offset: {2}. Extents:{3}",
  263. conn.RegionScene.RegionInfo.RegionName,
  264. regionConnections.RegionScene.RegionInfo.RegionName,
  265. offset, extents);
  266. scene.PhysicsScene.Combine(conn.RegionScene.PhysicsScene, offset, extents);
  267. connectedYN = true;
  268. break;
  269. }
  270. */
  271. /*
  272. //If we're one region over x +y
  273. //xxx
  274. //xxx
  275. //xyx
  276. if ((((int)conn.X * (int)Constants.RegionSize)
  277. == (regionConnections.X * (int)Constants.RegionSize))
  278. && (((int)conn.Y * (int)Constants.RegionSize) - conn.YEnd
  279. == (regionConnections.Y * (int)Constants.RegionSize)))
  280. {
  281. Vector3 offset = Vector3.Zero;
  282. offset.X = (((regionConnections.X * (int)Constants.RegionSize)) -
  283. ((conn.X * (int)Constants.RegionSize)));
  284. offset.Y = (((regionConnections.Y * (int)Constants.RegionSize)) -
  285. ((conn.Y * (int)Constants.RegionSize)));
  286. Vector3 extents = Vector3.Zero;
  287. extents.Y = regionConnections.YEnd + conn.YEnd;
  288. extents.X = conn.XEnd;
  289. m_log.DebugFormat("Scene: {0} to the north of Scene{1}. Offset: {2}. Extents:{3}",
  290. conn.RegionScene.RegionInfo.RegionName,
  291. regionConnections.RegionScene.RegionInfo.RegionName, offset, extents);
  292. scene.PhysicsScene.Combine(conn.RegionScene.PhysicsScene, offset, extents);
  293. connectedYN = true;
  294. break;
  295. }
  296. */
  297. /*
  298. // If we're one region over -x +y
  299. //xxx
  300. //xxx
  301. //yxx
  302. if ((((int)conn.X * (int)Constants.RegionSize) - conn.XEnd
  303. == (regionConnections.X * (int)Constants.RegionSize))
  304. && (((int)conn.Y * (int)Constants.RegionSize) - conn.YEnd
  305. == (regionConnections.Y * (int)Constants.RegionSize)))
  306. {
  307. Vector3 offset = Vector3.Zero;
  308. offset.X = (((regionConnections.X * (int)Constants.RegionSize)) -
  309. ((conn.X * (int)Constants.RegionSize)));
  310. offset.Y = (((regionConnections.Y * (int)Constants.RegionSize)) -
  311. ((conn.Y * (int)Constants.RegionSize)));
  312. Vector3 extents = Vector3.Zero;
  313. extents.Y = regionConnections.YEnd + conn.YEnd;
  314. extents.X = conn.XEnd + conn.XEnd;
  315. m_log.DebugFormat("Scene: {0} to the northeast of Scene. Offset: {2}. Extents:{3}",
  316. conn.RegionScene.RegionInfo.RegionName,
  317. regionConnections.RegionScene.RegionInfo.RegionName, offset, extents);
  318. scene.PhysicsScene.Combine(conn.RegionScene.PhysicsScene, offset, extents);
  319. connectedYN = true;
  320. break;
  321. }
  322. */
  323. /*
  324. // If we're one region over -x y
  325. //xxx
  326. //yxx
  327. //xxx
  328. if ((((int)conn.X * (int)Constants.RegionSize) - conn.XEnd
  329. == (regionConnections.X * (int)Constants.RegionSize))
  330. && (((int)conn.Y * (int)Constants.RegionSize)
  331. == (regionConnections.Y * (int)Constants.RegionSize)))
  332. {
  333. Vector3 offset = Vector3.Zero;
  334. offset.X = (((regionConnections.X * (int)Constants.RegionSize)) -
  335. ((conn.X * (int)Constants.RegionSize)));
  336. offset.Y = (((regionConnections.Y * (int)Constants.RegionSize)) -
  337. ((conn.Y * (int)Constants.RegionSize)));
  338. Vector3 extents = Vector3.Zero;
  339. extents.Y = regionConnections.YEnd;
  340. extents.X = conn.XEnd + conn.XEnd;
  341. m_log.DebugFormat("Scene: {0} to the east of Scene{1} Offset: {2}. Extents:{3}",
  342. conn.RegionScene.RegionInfo.RegionName,
  343. regionConnections.RegionScene.RegionInfo.RegionName, offset, extents);
  344. scene.PhysicsScene.Combine(conn.RegionScene.PhysicsScene, offset, extents);
  345. connectedYN = true;
  346. break;
  347. }
  348. */
  349. /*
  350. // If we're one region over -x -y
  351. //yxx
  352. //xxx
  353. //xxx
  354. if ((((int)conn.X * (int)Constants.RegionSize) - conn.XEnd
  355. == (regionConnections.X * (int)Constants.RegionSize))
  356. && (((int)conn.Y * (int)Constants.RegionSize) + conn.YEnd
  357. == (regionConnections.Y * (int)Constants.RegionSize)))
  358. {
  359. Vector3 offset = Vector3.Zero;
  360. offset.X = (((regionConnections.X * (int)Constants.RegionSize)) -
  361. ((conn.X * (int)Constants.RegionSize)));
  362. offset.Y = (((regionConnections.Y * (int)Constants.RegionSize)) -
  363. ((conn.Y * (int)Constants.RegionSize)));
  364. Vector3 extents = Vector3.Zero;
  365. extents.Y = regionConnections.YEnd + conn.YEnd;
  366. extents.X = conn.XEnd + conn.XEnd;
  367. m_log.DebugFormat("Scene: {0} to the northeast of Scene{1} Offset: {2}. Extents:{3}",
  368. conn.RegionScene.RegionInfo.RegionName,
  369. regionConnections.RegionScene.RegionInfo.RegionName, offset, extents);
  370. scene.PhysicsScene.Combine(conn.RegionScene.PhysicsScene, offset, extents);
  371. connectedYN = true;
  372. break;
  373. }
  374. */
  375. #endregion
  376. // Check to see if this new region is adjacent to the root region.
  377. // Note that we expect the regions to be combined from the root region outward
  378. // thus the requirement for the ordering in the configuration files.
  379. // If we're one region over +x y (i.e. root region is to the west)
  380. //xxx
  381. //xxy
  382. //xxx
  383. if (rootConn.PosX + rootConn.XEnd >= newConn.PosX && rootConn.PosY >= newConn.PosY)
  384. {
  385. connectedYN = DoWorkForOneRegionOverPlusXY(rootConn, newConn, scene);
  386. break;
  387. }
  388. // If we're one region over x +y (i.e. root region is to the south)
  389. //xyx
  390. //xxx
  391. //xxx
  392. if (rootConn.PosX >= newConn.PosX && rootConn.PosY + rootConn.YEnd >= newConn.PosY)
  393. {
  394. connectedYN = DoWorkForOneRegionOverPlusXY(rootConn, newConn, scene);
  395. break;
  396. }
  397. // If we're one region over +x +y (i.e. root region is to the south-west)
  398. //xxy
  399. //xxx
  400. //xxx
  401. if (rootConn.PosX + rootConn.XEnd >= newConn.PosX && rootConn.PosY + rootConn.YEnd >= newConn.PosY)
  402. {
  403. connectedYN = DoWorkForOneRegionOverPlusXY(rootConn, newConn, scene);
  404. break;
  405. }
  406. }
  407. // If !connectYN means that this region is a root region
  408. if (!connectedYN)
  409. {
  410. DoWorkForRootRegion(newConn, scene);
  411. }
  412. }
  413. }
  414. private bool DoWorkForOneRegionOverPlusXY(RegionConnections rootConn, RegionConnections newConn, Scene scene)
  415. {
  416. // Offset (in meters) from the base of this region to the base of the root region.
  417. Vector3 offset = Vector3.Zero;
  418. offset.X = newConn.PosX - rootConn.PosX;
  419. offset.Y = newConn.PosY - rootConn.PosY;
  420. // The new total size of the region (in meters)
  421. // We just extend the X and Y dimensions so the extent might temporarily include areas without regions.
  422. Vector3 extents = Vector3.Zero;
  423. extents.X = Math.Max(rootConn.XEnd, offset.X + newConn.RegionScene.RegionInfo.RegionSizeX);
  424. extents.Y = Math.Max(rootConn.YEnd, offset.Y + newConn.RegionScene.RegionInfo.RegionSizeY);
  425. rootConn.UpdateExtents(extents);
  426. m_log.DebugFormat(
  427. "[REGION COMBINER MODULE]: Root region {0} is to the west of region {1}, Offset: {2}, Extents: {3}",
  428. rootConn.RegionScene.RegionInfo.RegionName,
  429. newConn.RegionScene.RegionInfo.RegionName, offset, extents);
  430. RegionData ConnectedRegion = new RegionData();
  431. ConnectedRegion.Offset = offset;
  432. ConnectedRegion.RegionId = scene.RegionInfo.originRegionID;
  433. ConnectedRegion.RegionScene = scene;
  434. rootConn.ConnectedRegions.Add(ConnectedRegion);
  435. // Inform root region Physics about the extents of this region
  436. rootConn.RegionScene.PhysicsScene.Combine(null, Vector3.Zero, extents);
  437. // Inform Child region that it needs to forward it's terrain to the root region
  438. scene.PhysicsScene.Combine(rootConn.RegionScene.PhysicsScene, offset, Vector3.Zero);
  439. // Reset Terrain.. since terrain loads before we get here, we need to load
  440. // it again so it loads in the root region
  441. scene.PhysicsScene.SetTerrain(scene.Heightmap.GetFloatsSerialised());
  442. // Create a client event forwarder and add this region's events to the root region.
  443. if (rootConn.ClientEventForwarder != null)
  444. rootConn.ClientEventForwarder.AddSceneToEventForwarding(scene);
  445. return true;
  446. }
  447. /*
  448. * 20140215 radams1: The border stuff was removed and the addition of regions to the mega-regions
  449. * was generalized. These functions are not needed for the generalized solution but left for reference.
  450. private bool DoWorkForOneRegionOverXPlusY(RegionConnections rootConn, RegionConnections newConn, Scene scene)
  451. {
  452. Vector3 offset = Vector3.Zero;
  453. offset.X = newConn.PosX - rootConn.PosX;
  454. offset.Y = newConn.PosY - rootConn.PosY;
  455. Vector3 extents = Vector3.Zero;
  456. extents.Y = newConn.YEnd + rootConn.YEnd;
  457. extents.X = rootConn.XEnd;
  458. rootConn.UpdateExtents(extents);
  459. RegionData ConnectedRegion = new RegionData();
  460. ConnectedRegion.Offset = offset;
  461. ConnectedRegion.RegionId = scene.RegionInfo.originRegionID;
  462. ConnectedRegion.RegionScene = scene;
  463. rootConn.ConnectedRegions.Add(ConnectedRegion);
  464. m_log.DebugFormat(
  465. "[REGION COMBINER MODULE]: Root region {0} is to the south of region {1}, Offset: {2}, Extents: {3}",
  466. rootConn.RegionScene.RegionInfo.RegionName,
  467. newConn.RegionScene.RegionInfo.RegionName, offset, extents);
  468. rootConn.RegionScene.PhysicsScene.Combine(null, Vector3.Zero, extents);
  469. scene.PhysicsScene.Combine(rootConn.RegionScene.PhysicsScene, offset, Vector3.Zero);
  470. // Reset Terrain.. since terrain normally loads first.
  471. //conn.RegionScene.PhysicsScene.SetTerrain(conn.RegionScene.Heightmap.GetFloatsSerialised());
  472. scene.PhysicsScene.SetTerrain(scene.Heightmap.GetFloatsSerialised());
  473. //conn.RegionScene.PhysicsScene.SetTerrain(conn.RegionScene.Heightmap.GetFloatsSerialised());
  474. if (rootConn.ClientEventForwarder != null)
  475. rootConn.ClientEventForwarder.AddSceneToEventForwarding(scene);
  476. return true;
  477. }
  478. private bool DoWorkForOneRegionOverPlusXPlusY(RegionConnections rootConn, RegionConnections newConn, Scene scene)
  479. {
  480. Vector3 offset = Vector3.Zero;
  481. offset.X = newConn.PosX - rootConn.PosX;
  482. offset.Y = newConn.PosY - rootConn.PosY;
  483. Vector3 extents = Vector3.Zero;
  484. // We do not want to inflate the extents for regions strictly to the NE of the root region, since this
  485. // would double count regions strictly to the north and east that have already been added.
  486. // extents.Y = regionConnections.YEnd + conn.YEnd;
  487. // extents.X = regionConnections.XEnd + conn.XEnd;
  488. // conn.UpdateExtents(extents);
  489. extents.Y = rootConn.YEnd;
  490. extents.X = rootConn.XEnd;
  491. RegionData ConnectedRegion = new RegionData();
  492. ConnectedRegion.Offset = offset;
  493. ConnectedRegion.RegionId = scene.RegionInfo.originRegionID;
  494. ConnectedRegion.RegionScene = scene;
  495. rootConn.ConnectedRegions.Add(ConnectedRegion);
  496. m_log.DebugFormat(
  497. "[REGION COMBINER MODULE]: Region {0} is to the southwest of Scene {1}, Offset: {2}, Extents: {3}",
  498. rootConn.RegionScene.RegionInfo.RegionName,
  499. newConn.RegionScene.RegionInfo.RegionName, offset, extents);
  500. rootConn.RegionScene.PhysicsScene.Combine(null, Vector3.Zero, extents);
  501. scene.PhysicsScene.Combine(rootConn.RegionScene.PhysicsScene, offset, Vector3.Zero);
  502. // Reset Terrain.. since terrain normally loads first.
  503. //conn.RegionScene.PhysicsScene.SetTerrain(conn.RegionScene.Heightmap.GetFloatsSerialised());
  504. scene.PhysicsScene.SetTerrain(scene.Heightmap.GetFloatsSerialised());
  505. //conn.RegionScene.PhysicsScene.SetTerrain(conn.RegionScene.Heightmap.GetFloatsSerialised());
  506. if (rootConn.ClientEventForwarder != null)
  507. rootConn.ClientEventForwarder.AddSceneToEventForwarding(scene);
  508. return true;
  509. //scene.PhysicsScene.Combine(conn.RegionScene.PhysicsScene, offset,extents);
  510. }
  511. */
  512. private void DoWorkForRootRegion(RegionConnections rootConn, Scene scene)
  513. {
  514. m_log.DebugFormat("[REGION COMBINER MODULE]: Adding root region {0}", scene.RegionInfo.RegionName);
  515. RegionData rdata = new RegionData();
  516. rdata.Offset = Vector3.Zero;
  517. rdata.RegionId = scene.RegionInfo.originRegionID;
  518. rdata.RegionScene = scene;
  519. // save it's land channel
  520. rootConn.RegionLandChannel = scene.LandChannel;
  521. // Substitue our landchannel
  522. RegionCombinerLargeLandChannel lnd = new RegionCombinerLargeLandChannel(rdata, scene.LandChannel,
  523. rootConn.ConnectedRegions);
  524. scene.LandChannel = lnd;
  525. // Forward the permissions modules of each of the connected regions to the root region
  526. lock (m_regions)
  527. {
  528. foreach (RegionData r in rootConn.ConnectedRegions)
  529. {
  530. ForwardPermissionRequests(rootConn, r.RegionScene);
  531. }
  532. // Create the root region's Client Event Forwarder
  533. rootConn.ClientEventForwarder = new RegionCombinerClientEventForwarder(rootConn);
  534. // Sets up the CoarseLocationUpdate forwarder for this root region
  535. scene.EventManager.OnNewPresence += SetCoarseLocationDelegate;
  536. // Adds this root region to a dictionary of regions that are connectable
  537. m_regions.Add(scene.RegionInfo.originRegionID, rootConn);
  538. }
  539. }
  540. private void SetCoarseLocationDelegate(ScenePresence presence)
  541. {
  542. presence.SetSendCoarseLocationMethod(SendCoarseLocationUpdates);
  543. }
  544. // This delegate was refactored for non-combined regions.
  545. // This combined region version will not use the pre-compiled lists of locations and ids
  546. private void SendCoarseLocationUpdates(UUID sceneId, ScenePresence presence, List<Vector3> coarseLocations, List<UUID> avatarUUIDs)
  547. {
  548. RegionConnections connectiondata = null;
  549. lock (m_regions)
  550. {
  551. if (m_regions.ContainsKey(sceneId))
  552. connectiondata = m_regions[sceneId];
  553. else
  554. return;
  555. }
  556. List<Vector3> CoarseLocations = new List<Vector3>();
  557. List<UUID> AvatarUUIDs = new List<UUID>();
  558. connectiondata.RegionScene.ForEachRootScenePresence(delegate(ScenePresence sp)
  559. {
  560. if (sp.UUID != presence.UUID)
  561. {
  562. CoarseLocations.Add(sp.AbsolutePosition);
  563. AvatarUUIDs.Add(sp.UUID);
  564. }
  565. });
  566. DistributeCoarseLocationUpdates(CoarseLocations, AvatarUUIDs, connectiondata, presence);
  567. }
  568. private void DistributeCoarseLocationUpdates(List<Vector3> locations, List<UUID> uuids,
  569. RegionConnections connectiondata, ScenePresence rootPresence)
  570. {
  571. RegionData[] rdata = connectiondata.ConnectedRegions.ToArray();
  572. //List<IClientAPI> clients = new List<IClientAPI>();
  573. Dictionary<Vector2, RegionCoarseLocationStruct> updates = new Dictionary<Vector2, RegionCoarseLocationStruct>();
  574. // Root Region entry
  575. RegionCoarseLocationStruct rootupdatedata = new RegionCoarseLocationStruct();
  576. rootupdatedata.Locations = new List<Vector3>();
  577. rootupdatedata.Uuids = new List<UUID>();
  578. rootupdatedata.Offset = Vector2.Zero;
  579. rootupdatedata.UserAPI = rootPresence.ControllingClient;
  580. if (rootupdatedata.UserAPI != null)
  581. updates.Add(Vector2.Zero, rootupdatedata);
  582. //Each Region needs an entry or we will end up with dead minimap dots
  583. foreach (RegionData regiondata in rdata)
  584. {
  585. Vector2 offset = new Vector2(regiondata.Offset.X, regiondata.Offset.Y);
  586. RegionCoarseLocationStruct updatedata = new RegionCoarseLocationStruct();
  587. updatedata.Locations = new List<Vector3>();
  588. updatedata.Uuids = new List<UUID>();
  589. updatedata.Offset = offset;
  590. if (offset == Vector2.Zero)
  591. updatedata.UserAPI = rootPresence.ControllingClient;
  592. else
  593. updatedata.UserAPI = LocateUsersChildAgentIClientAPI(offset, rootPresence.UUID, rdata);
  594. if (updatedata.UserAPI != null)
  595. updates.Add(offset, updatedata);
  596. }
  597. // go over the locations and assign them to an IClientAPI
  598. for (int i = 0; i < locations.Count; i++)
  599. //{locations[i]/(int) Constants.RegionSize;
  600. {
  601. Vector3 pPosition = new Vector3((int)locations[i].X / (int)Constants.RegionSize,
  602. (int)locations[i].Y / (int)Constants.RegionSize, locations[i].Z);
  603. Vector2 offset = new Vector2(pPosition.X*(int) Constants.RegionSize,
  604. pPosition.Y*(int) Constants.RegionSize);
  605. if (!updates.ContainsKey(offset))
  606. {
  607. // This shouldn't happen
  608. RegionCoarseLocationStruct updatedata = new RegionCoarseLocationStruct();
  609. updatedata.Locations = new List<Vector3>();
  610. updatedata.Uuids = new List<UUID>();
  611. updatedata.Offset = offset;
  612. if (offset == Vector2.Zero)
  613. updatedata.UserAPI = rootPresence.ControllingClient;
  614. else
  615. updatedata.UserAPI = LocateUsersChildAgentIClientAPI(offset, rootPresence.UUID, rdata);
  616. updates.Add(offset,updatedata);
  617. }
  618. updates[offset].Locations.Add(locations[i]);
  619. updates[offset].Uuids.Add(uuids[i]);
  620. }
  621. // Send out the CoarseLocationupdates from their respective client connection based on where the avatar is
  622. foreach (Vector2 offset in updates.Keys)
  623. {
  624. if (updates[offset].UserAPI != null)
  625. {
  626. updates[offset].UserAPI.SendCoarseLocationUpdate(updates[offset].Uuids,updates[offset].Locations);
  627. }
  628. }
  629. }
  630. /// <summary>
  631. /// Locates a the Client of a particular region in an Array of RegionData based on offset
  632. /// </summary>
  633. /// <param name="offset"></param>
  634. /// <param name="uUID"></param>
  635. /// <param name="rdata"></param>
  636. /// <returns>IClientAPI or null</returns>
  637. private IClientAPI LocateUsersChildAgentIClientAPI(Vector2 offset, UUID uUID, RegionData[] rdata)
  638. {
  639. IClientAPI returnclient = null;
  640. foreach (RegionData r in rdata)
  641. {
  642. if (r.Offset.X == offset.X && r.Offset.Y == offset.Y)
  643. {
  644. return r.RegionScene.SceneGraph.GetControllingClient(uUID);
  645. }
  646. }
  647. return returnclient;
  648. }
  649. public void PostInitialise()
  650. {
  651. }
  652. // /// <summary>
  653. // /// TODO:
  654. // /// </summary>
  655. // /// <param name="rdata"></param>
  656. // public void UnCombineRegion(RegionData rdata)
  657. // {
  658. // lock (m_regions)
  659. // {
  660. // if (m_regions.ContainsKey(rdata.RegionId))
  661. // {
  662. // // uncombine root region and virtual regions
  663. // }
  664. // else
  665. // {
  666. // foreach (RegionConnections r in m_regions.Values)
  667. // {
  668. // foreach (RegionData rd in r.ConnectedRegions)
  669. // {
  670. // if (rd.RegionId == rdata.RegionId)
  671. // {
  672. // // uncombine virtual region
  673. // }
  674. // }
  675. // }
  676. // }
  677. // }
  678. // }
  679. public void ForwardPermissionRequests(RegionConnections BigRegion, Scene VirtualRegion)
  680. {
  681. if (BigRegion.PermissionModule == null)
  682. BigRegion.PermissionModule = new RegionCombinerPermissionModule(BigRegion.RegionScene);
  683. VirtualRegion.Permissions.OnBypassPermissions += BigRegion.PermissionModule.BypassPermissions;
  684. VirtualRegion.Permissions.OnSetBypassPermissions += BigRegion.PermissionModule.SetBypassPermissions;
  685. VirtualRegion.Permissions.OnPropagatePermissions += BigRegion.PermissionModule.PropagatePermissions;
  686. VirtualRegion.Permissions.OnGenerateClientFlags += BigRegion.PermissionModule.GenerateClientFlags;
  687. VirtualRegion.Permissions.OnAbandonParcel += BigRegion.PermissionModule.CanAbandonParcel;
  688. VirtualRegion.Permissions.OnReclaimParcel += BigRegion.PermissionModule.CanReclaimParcel;
  689. VirtualRegion.Permissions.OnDeedParcel += BigRegion.PermissionModule.CanDeedParcel;
  690. VirtualRegion.Permissions.OnDeedObject += BigRegion.PermissionModule.CanDeedObject;
  691. VirtualRegion.Permissions.OnIsGod += BigRegion.PermissionModule.IsGod;
  692. VirtualRegion.Permissions.OnDuplicateObject += BigRegion.PermissionModule.CanDuplicateObject;
  693. VirtualRegion.Permissions.OnDeleteObject += BigRegion.PermissionModule.CanDeleteObject; //MAYBE FULLY IMPLEMENTED
  694. VirtualRegion.Permissions.OnEditObject += BigRegion.PermissionModule.CanEditObject; //MAYBE FULLY IMPLEMENTED
  695. VirtualRegion.Permissions.OnEditParcelProperties += BigRegion.PermissionModule.CanEditParcelProperties; //MAYBE FULLY IMPLEMENTED
  696. VirtualRegion.Permissions.OnInstantMessage += BigRegion.PermissionModule.CanInstantMessage;
  697. VirtualRegion.Permissions.OnInventoryTransfer += BigRegion.PermissionModule.CanInventoryTransfer; //NOT YET IMPLEMENTED
  698. VirtualRegion.Permissions.OnIssueEstateCommand += BigRegion.PermissionModule.CanIssueEstateCommand; //FULLY IMPLEMENTED
  699. VirtualRegion.Permissions.OnMoveObject += BigRegion.PermissionModule.CanMoveObject; //MAYBE FULLY IMPLEMENTED
  700. VirtualRegion.Permissions.OnObjectEntry += BigRegion.PermissionModule.CanObjectEntry;
  701. VirtualRegion.Permissions.OnReturnObjects += BigRegion.PermissionModule.CanReturnObjects; //NOT YET IMPLEMENTED
  702. VirtualRegion.Permissions.OnRezObject += BigRegion.PermissionModule.CanRezObject; //MAYBE FULLY IMPLEMENTED
  703. VirtualRegion.Permissions.OnRunConsoleCommand += BigRegion.PermissionModule.CanRunConsoleCommand;
  704. VirtualRegion.Permissions.OnRunScript += BigRegion.PermissionModule.CanRunScript; //NOT YET IMPLEMENTED
  705. VirtualRegion.Permissions.OnCompileScript += BigRegion.PermissionModule.CanCompileScript;
  706. VirtualRegion.Permissions.OnSellParcel += BigRegion.PermissionModule.CanSellParcel;
  707. VirtualRegion.Permissions.OnTakeObject += BigRegion.PermissionModule.CanTakeObject;
  708. VirtualRegion.Permissions.OnTakeCopyObject += BigRegion.PermissionModule.CanTakeCopyObject;
  709. VirtualRegion.Permissions.OnTerraformLand += BigRegion.PermissionModule.CanTerraformLand;
  710. VirtualRegion.Permissions.OnLinkObject += BigRegion.PermissionModule.CanLinkObject; //NOT YET IMPLEMENTED
  711. VirtualRegion.Permissions.OnDelinkObject += BigRegion.PermissionModule.CanDelinkObject; //NOT YET IMPLEMENTED
  712. VirtualRegion.Permissions.OnBuyLand += BigRegion.PermissionModule.CanBuyLand; //NOT YET IMPLEMENTED
  713. VirtualRegion.Permissions.OnViewNotecard += BigRegion.PermissionModule.CanViewNotecard; //NOT YET IMPLEMENTED
  714. VirtualRegion.Permissions.OnViewScript += BigRegion.PermissionModule.CanViewScript; //NOT YET IMPLEMENTED
  715. VirtualRegion.Permissions.OnEditNotecard += BigRegion.PermissionModule.CanEditNotecard; //NOT YET IMPLEMENTED
  716. VirtualRegion.Permissions.OnEditScript += BigRegion.PermissionModule.CanEditScript; //NOT YET IMPLEMENTED
  717. VirtualRegion.Permissions.OnCreateObjectInventory += BigRegion.PermissionModule.CanCreateObjectInventory; //NOT IMPLEMENTED HERE
  718. VirtualRegion.Permissions.OnEditObjectInventory += BigRegion.PermissionModule.CanEditObjectInventory;//MAYBE FULLY IMPLEMENTED
  719. VirtualRegion.Permissions.OnCopyObjectInventory += BigRegion.PermissionModule.CanCopyObjectInventory; //NOT YET IMPLEMENTED
  720. VirtualRegion.Permissions.OnDeleteObjectInventory += BigRegion.PermissionModule.CanDeleteObjectInventory; //NOT YET IMPLEMENTED
  721. VirtualRegion.Permissions.OnResetScript += BigRegion.PermissionModule.CanResetScript;
  722. VirtualRegion.Permissions.OnCreateUserInventory += BigRegion.PermissionModule.CanCreateUserInventory; //NOT YET IMPLEMENTED
  723. VirtualRegion.Permissions.OnCopyUserInventory += BigRegion.PermissionModule.CanCopyUserInventory; //NOT YET IMPLEMENTED
  724. VirtualRegion.Permissions.OnEditUserInventory += BigRegion.PermissionModule.CanEditUserInventory; //NOT YET IMPLEMENTED
  725. VirtualRegion.Permissions.OnDeleteUserInventory += BigRegion.PermissionModule.CanDeleteUserInventory; //NOT YET IMPLEMENTED
  726. VirtualRegion.Permissions.OnTeleport += BigRegion.PermissionModule.CanTeleport; //NOT YET IMPLEMENTED
  727. }
  728. #region console commands
  729. public void FixPhantoms(string module, string[] cmdparams)
  730. {
  731. List<Scene> scenes = new List<Scene>(m_startingScenes.Values);
  732. foreach (Scene s in scenes)
  733. {
  734. MainConsole.Instance.OutputFormat("Fixing phantoms for {0}", s.RegionInfo.RegionName);
  735. s.ForEachSOG(so => so.AbsolutePosition = so.AbsolutePosition);
  736. }
  737. }
  738. #endregion
  739. }
  740. }