RegionCombinerModule.cs 41 KB

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