LandObject.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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 OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using OpenSim.Region.Framework.Scenes;
  35. using RegionFlags = OpenMetaverse.RegionFlags;
  36. namespace OpenSim.Region.CoreModules.World.Land
  37. {
  38. /// <summary>
  39. /// Keeps track of a specific piece of land's information
  40. /// </summary>
  41. public class LandObject : ILandObject
  42. {
  43. #region Member Variables
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. #pragma warning disable 0429
  46. private const int landArrayMax = ((int)((int)Constants.RegionSize / 4) >= 64) ? (int)((int)Constants.RegionSize / 4) : 64;
  47. #pragma warning restore 0429
  48. private bool[,] m_landBitmap = new bool[landArrayMax,landArrayMax];
  49. private int m_lastSeqId = 0;
  50. protected LandData m_landData = new LandData();
  51. protected Scene m_scene;
  52. protected List<SceneObjectGroup> primsOverMe = new List<SceneObjectGroup>();
  53. protected Dictionary<uint, UUID> m_listTransactions = new Dictionary<uint, UUID>();
  54. protected ExpiringCache<UUID, bool> m_groupMemberCache = new ExpiringCache<UUID, bool>();
  55. protected TimeSpan m_groupMemberCacheTimeout = TimeSpan.FromSeconds(30); // cache invalidation after 30 seconds
  56. public bool[,] LandBitmap
  57. {
  58. get { return m_landBitmap; }
  59. set { m_landBitmap = value; }
  60. }
  61. #endregion
  62. public int GetPrimsFree()
  63. {
  64. m_scene.EventManager.TriggerParcelPrimCountUpdate();
  65. int free = GetSimulatorMaxPrimCount() - m_landData.SimwidePrims;
  66. return free;
  67. }
  68. public LandData LandData
  69. {
  70. get { return m_landData; }
  71. set { m_landData = value; }
  72. }
  73. public IPrimCounts PrimCounts { get; set; }
  74. public UUID RegionUUID
  75. {
  76. get { return m_scene.RegionInfo.RegionID; }
  77. }
  78. public Vector3 StartPoint
  79. {
  80. get
  81. {
  82. for (int y = 0; y < landArrayMax; y++)
  83. {
  84. for (int x = 0; x < landArrayMax; x++)
  85. {
  86. if (LandBitmap[x, y])
  87. return new Vector3(x * 4, y * 4, 0);
  88. }
  89. }
  90. return new Vector3(-1, -1, -1);
  91. }
  92. }
  93. public Vector3 EndPoint
  94. {
  95. get
  96. {
  97. for (int y = landArrayMax - 1; y >= 0; y--)
  98. {
  99. for (int x = landArrayMax - 1; x >= 0; x--)
  100. {
  101. if (LandBitmap[x, y])
  102. {
  103. return new Vector3(x * 4 + 4, y * 4 + 4, 0);
  104. }
  105. }
  106. }
  107. return new Vector3(-1, -1, -1);
  108. }
  109. }
  110. #region Constructors
  111. public LandObject(UUID owner_id, bool is_group_owned, Scene scene)
  112. {
  113. m_scene = scene;
  114. LandData.OwnerID = owner_id;
  115. if (is_group_owned)
  116. LandData.GroupID = owner_id;
  117. else
  118. LandData.GroupID = UUID.Zero;
  119. LandData.IsGroupOwned = is_group_owned;
  120. }
  121. #endregion
  122. #region Member Functions
  123. #region General Functions
  124. /// <summary>
  125. /// Checks to see if this land object contains a point
  126. /// </summary>
  127. /// <param name="x"></param>
  128. /// <param name="y"></param>
  129. /// <returns>Returns true if the piece of land contains the specified point</returns>
  130. public bool ContainsPoint(int x, int y)
  131. {
  132. if (x >= 0 && y >= 0 && x < Constants.RegionSize && y < Constants.RegionSize)
  133. {
  134. return (LandBitmap[x / 4, y / 4] == true);
  135. }
  136. else
  137. {
  138. return false;
  139. }
  140. }
  141. public ILandObject Copy()
  142. {
  143. ILandObject newLand = new LandObject(LandData.OwnerID, LandData.IsGroupOwned, m_scene);
  144. //Place all new variables here!
  145. newLand.LandBitmap = (bool[,]) (LandBitmap.Clone());
  146. newLand.LandData = LandData.Copy();
  147. return newLand;
  148. }
  149. static overrideParcelMaxPrimCountDelegate overrideParcelMaxPrimCount;
  150. static overrideSimulatorMaxPrimCountDelegate overrideSimulatorMaxPrimCount;
  151. public void SetParcelObjectMaxOverride(overrideParcelMaxPrimCountDelegate overrideDel)
  152. {
  153. overrideParcelMaxPrimCount = overrideDel;
  154. }
  155. public void SetSimulatorObjectMaxOverride(overrideSimulatorMaxPrimCountDelegate overrideDel)
  156. {
  157. overrideSimulatorMaxPrimCount = overrideDel;
  158. }
  159. public int GetParcelMaxPrimCount()
  160. {
  161. if (overrideParcelMaxPrimCount != null)
  162. {
  163. return overrideParcelMaxPrimCount(this);
  164. }
  165. else
  166. {
  167. // Normal Calculations
  168. int parcelMax = (int)(((float)LandData.Area / 65536.0f)
  169. * (float)m_scene.RegionInfo.ObjectCapacity
  170. * (float)m_scene.RegionInfo.RegionSettings.ObjectBonus);
  171. // TODO: The calculation of ObjectBonus should be refactored. It does still not work in the same manner as SL!
  172. return parcelMax;
  173. }
  174. }
  175. public int GetSimulatorMaxPrimCount()
  176. {
  177. if (overrideSimulatorMaxPrimCount != null)
  178. {
  179. return overrideSimulatorMaxPrimCount(this);
  180. }
  181. else
  182. {
  183. //Normal Calculations
  184. int simMax = (int)(((float)LandData.SimwideArea / 65536.0f)
  185. * (float)m_scene.RegionInfo.ObjectCapacity);
  186. return simMax;
  187. }
  188. }
  189. #endregion
  190. #region Packet Request Handling
  191. public void SendLandProperties(int sequence_id, bool snap_selection, int request_result, IClientAPI remote_client)
  192. {
  193. IEstateModule estateModule = m_scene.RequestModuleInterface<IEstateModule>();
  194. uint regionFlags = 336723974 & ~((uint)(RegionFlags.AllowLandmark | RegionFlags.AllowSetHome));
  195. if (estateModule != null)
  196. regionFlags = estateModule.GetRegionFlags();
  197. int seq_id;
  198. if (snap_selection && (sequence_id == 0))
  199. {
  200. seq_id = m_lastSeqId;
  201. }
  202. else
  203. {
  204. seq_id = sequence_id;
  205. m_lastSeqId = seq_id;
  206. }
  207. remote_client.SendLandProperties(seq_id,
  208. snap_selection, request_result, this,
  209. (float)m_scene.RegionInfo.RegionSettings.ObjectBonus,
  210. GetParcelMaxPrimCount(),
  211. GetSimulatorMaxPrimCount(), regionFlags);
  212. }
  213. public void UpdateLandProperties(LandUpdateArgs args, IClientAPI remote_client)
  214. {
  215. //Needs later group support
  216. bool snap_selection = false;
  217. LandData newData = LandData.Copy();
  218. uint allowedDelta = 0;
  219. // These two are always blocked as no client can set them anyway
  220. // ParcelFlags.ForSaleObjects
  221. // ParcelFlags.LindenHome
  222. if (m_scene.Permissions.CanEditParcelProperties(remote_client.AgentId, this, GroupPowers.LandOptions))
  223. {
  224. allowedDelta |= (uint)(ParcelFlags.AllowLandmark |
  225. ParcelFlags.AllowTerraform |
  226. ParcelFlags.AllowDamage |
  227. ParcelFlags.CreateObjects |
  228. ParcelFlags.RestrictPushObject |
  229. ParcelFlags.AllowOtherScripts |
  230. ParcelFlags.AllowGroupScripts |
  231. ParcelFlags.CreateGroupObjects |
  232. ParcelFlags.AllowAPrimitiveEntry |
  233. ParcelFlags.AllowGroupObjectEntry |
  234. ParcelFlags.AllowFly);
  235. }
  236. if (m_scene.Permissions.CanEditParcelProperties(remote_client.AgentId, this, GroupPowers.LandSetSale))
  237. {
  238. if (args.AuthBuyerID != newData.AuthBuyerID ||
  239. args.SalePrice != newData.SalePrice)
  240. {
  241. snap_selection = true;
  242. }
  243. newData.AuthBuyerID = args.AuthBuyerID;
  244. newData.SalePrice = args.SalePrice;
  245. if (!LandData.IsGroupOwned)
  246. {
  247. newData.GroupID = args.GroupID;
  248. allowedDelta |= (uint)(ParcelFlags.AllowDeedToGroup |
  249. ParcelFlags.ContributeWithDeed |
  250. ParcelFlags.SellParcelObjects);
  251. }
  252. allowedDelta |= (uint)ParcelFlags.ForSale;
  253. }
  254. if (m_scene.Permissions.CanEditParcelProperties(remote_client.AgentId,this, GroupPowers.FindPlaces))
  255. {
  256. newData.Category = args.Category;
  257. allowedDelta |= (uint)(ParcelFlags.ShowDirectory |
  258. ParcelFlags.AllowPublish |
  259. ParcelFlags.MaturePublish);
  260. }
  261. if (m_scene.Permissions.CanEditParcelProperties(remote_client.AgentId,this, GroupPowers.LandChangeIdentity))
  262. {
  263. newData.Description = args.Desc;
  264. newData.Name = args.Name;
  265. newData.SnapshotID = args.SnapshotID;
  266. }
  267. if (m_scene.Permissions.CanEditParcelProperties(remote_client.AgentId,this, GroupPowers.SetLandingPoint))
  268. {
  269. newData.LandingType = args.LandingType;
  270. newData.UserLocation = args.UserLocation;
  271. newData.UserLookAt = args.UserLookAt;
  272. }
  273. if (m_scene.Permissions.CanEditParcelProperties(remote_client.AgentId,this, GroupPowers.ChangeMedia))
  274. {
  275. newData.MediaAutoScale = args.MediaAutoScale;
  276. newData.MediaID = args.MediaID;
  277. newData.MediaURL = args.MediaURL;
  278. newData.MusicURL = args.MusicURL;
  279. newData.MediaType = args.MediaType;
  280. newData.MediaDescription = args.MediaDescription;
  281. newData.MediaWidth = args.MediaWidth;
  282. newData.MediaHeight = args.MediaHeight;
  283. newData.MediaLoop = args.MediaLoop;
  284. newData.ObscureMusic = args.ObscureMusic;
  285. newData.ObscureMedia = args.ObscureMedia;
  286. allowedDelta |= (uint)(ParcelFlags.SoundLocal |
  287. ParcelFlags.UrlWebPage |
  288. ParcelFlags.UrlRawHtml |
  289. ParcelFlags.AllowVoiceChat |
  290. ParcelFlags.UseEstateVoiceChan);
  291. }
  292. if (m_scene.Permissions.CanEditParcelProperties(remote_client.AgentId,this, GroupPowers.LandManagePasses))
  293. {
  294. newData.PassHours = args.PassHours;
  295. newData.PassPrice = args.PassPrice;
  296. allowedDelta |= (uint)ParcelFlags.UsePassList;
  297. }
  298. if (m_scene.Permissions.CanEditParcelProperties(remote_client.AgentId, this, GroupPowers.LandManageAllowed))
  299. {
  300. allowedDelta |= (uint)(ParcelFlags.UseAccessGroup |
  301. ParcelFlags.UseAccessList);
  302. }
  303. if (m_scene.Permissions.CanEditParcelProperties(remote_client.AgentId, this, GroupPowers.LandManageBanned))
  304. {
  305. allowedDelta |= (uint)(ParcelFlags.UseBanList |
  306. ParcelFlags.DenyAnonymous |
  307. ParcelFlags.DenyAgeUnverified);
  308. }
  309. uint preserve = LandData.Flags & ~allowedDelta;
  310. newData.Flags = preserve | (args.ParcelFlags & allowedDelta);
  311. m_scene.LandChannel.UpdateLandObject(LandData.LocalID, newData);
  312. SendLandUpdateToAvatarsOverMe(snap_selection);
  313. }
  314. public void UpdateLandSold(UUID avatarID, UUID groupID, bool groupOwned, uint AuctionID, int claimprice, int area)
  315. {
  316. LandData newData = LandData.Copy();
  317. newData.OwnerID = avatarID;
  318. newData.GroupID = groupID;
  319. newData.IsGroupOwned = groupOwned;
  320. //newData.auctionID = AuctionID;
  321. newData.ClaimDate = Util.UnixTimeSinceEpoch();
  322. newData.ClaimPrice = claimprice;
  323. newData.SalePrice = 0;
  324. newData.AuthBuyerID = UUID.Zero;
  325. newData.Flags &= ~(uint) (ParcelFlags.ForSale | ParcelFlags.ForSaleObjects | ParcelFlags.SellParcelObjects | ParcelFlags.ShowDirectory);
  326. m_scene.LandChannel.UpdateLandObject(LandData.LocalID, newData);
  327. m_scene.EventManager.TriggerParcelPrimCountUpdate();
  328. SendLandUpdateToAvatarsOverMe(true);
  329. }
  330. public void DeedToGroup(UUID groupID)
  331. {
  332. LandData newData = LandData.Copy();
  333. newData.OwnerID = groupID;
  334. newData.GroupID = groupID;
  335. newData.IsGroupOwned = true;
  336. // Reset show in directory flag on deed
  337. newData.Flags &= ~(uint) (ParcelFlags.ForSale | ParcelFlags.ForSaleObjects | ParcelFlags.SellParcelObjects | ParcelFlags.ShowDirectory);
  338. m_scene.LandChannel.UpdateLandObject(LandData.LocalID, newData);
  339. m_scene.EventManager.TriggerParcelPrimCountUpdate();
  340. SendLandUpdateToAvatarsOverMe(true);
  341. }
  342. public bool IsEitherBannedOrRestricted(UUID avatar)
  343. {
  344. if (IsBannedFromLand(avatar))
  345. {
  346. return true;
  347. }
  348. else if (IsRestrictedFromLand(avatar))
  349. {
  350. return true;
  351. }
  352. return false;
  353. }
  354. public bool HasGroupAccess(UUID avatar)
  355. {
  356. if (LandData.GroupID != UUID.Zero && (LandData.Flags & (uint)ParcelFlags.UseAccessGroup) == (uint)ParcelFlags.UseAccessGroup)
  357. {
  358. ScenePresence sp;
  359. if (!m_scene.TryGetScenePresence(avatar, out sp))
  360. {
  361. bool isMember;
  362. if (m_groupMemberCache.TryGetValue(avatar, out isMember))
  363. {
  364. m_groupMemberCache.Update(avatar, isMember, m_groupMemberCacheTimeout);
  365. return isMember;
  366. }
  367. IGroupsModule groupsModule = m_scene.RequestModuleInterface<IGroupsModule>();
  368. if (groupsModule == null)
  369. return false;
  370. GroupMembershipData[] membership = groupsModule.GetMembershipData(avatar);
  371. if (membership == null || membership.Length == 0)
  372. {
  373. m_groupMemberCache.Add(avatar, false, m_groupMemberCacheTimeout);
  374. return false;
  375. }
  376. foreach (GroupMembershipData d in membership)
  377. {
  378. if (d.GroupID == LandData.GroupID)
  379. {
  380. m_groupMemberCache.Add(avatar, true, m_groupMemberCacheTimeout);
  381. return true;
  382. }
  383. }
  384. m_groupMemberCache.Add(avatar, false, m_groupMemberCacheTimeout);
  385. return false;
  386. }
  387. return sp.ControllingClient.IsGroupMember(LandData.GroupID);
  388. }
  389. return false;
  390. }
  391. public bool IsBannedFromLand(UUID avatar)
  392. {
  393. ExpireAccessList();
  394. if (m_scene.Permissions.IsAdministrator(avatar))
  395. return false;
  396. if (m_scene.RegionInfo.EstateSettings.IsEstateManagerOrOwner(avatar))
  397. return false;
  398. if (avatar == LandData.OwnerID)
  399. return false;
  400. if ((LandData.Flags & (uint) ParcelFlags.UseBanList) > 0)
  401. {
  402. if (LandData.ParcelAccessList.FindIndex(
  403. delegate(LandAccessEntry e)
  404. {
  405. if (e.AgentID == avatar && e.Flags == AccessList.Ban)
  406. return true;
  407. return false;
  408. }) != -1)
  409. {
  410. return true;
  411. }
  412. }
  413. return false;
  414. }
  415. public bool IsRestrictedFromLand(UUID avatar)
  416. {
  417. if ((LandData.Flags & (uint) ParcelFlags.UseAccessList) == 0)
  418. return false;
  419. if (m_scene.Permissions.IsAdministrator(avatar))
  420. return false;
  421. if (m_scene.RegionInfo.EstateSettings.IsEstateManagerOrOwner(avatar))
  422. return false;
  423. if (avatar == LandData.OwnerID)
  424. return false;
  425. if (HasGroupAccess(avatar))
  426. return false;
  427. return !IsInLandAccessList(avatar);
  428. }
  429. public bool IsInLandAccessList(UUID avatar)
  430. {
  431. ExpireAccessList();
  432. if (LandData.ParcelAccessList.FindIndex(
  433. delegate(LandAccessEntry e)
  434. {
  435. if (e.AgentID == avatar && e.Flags == AccessList.Access)
  436. return true;
  437. return false;
  438. }) == -1)
  439. {
  440. return false;
  441. }
  442. return true;
  443. }
  444. public void SendLandUpdateToClient(IClientAPI remote_client)
  445. {
  446. SendLandProperties(0, false, 0, remote_client);
  447. }
  448. public void SendLandUpdateToClient(bool snap_selection, IClientAPI remote_client)
  449. {
  450. m_scene.EventManager.TriggerParcelPrimCountUpdate();
  451. SendLandProperties(0, snap_selection, 0, remote_client);
  452. }
  453. public void SendLandUpdateToAvatarsOverMe()
  454. {
  455. SendLandUpdateToAvatarsOverMe(false);
  456. }
  457. public void SendLandUpdateToAvatarsOverMe(bool snap_selection)
  458. {
  459. m_scene.ForEachRootScenePresence(delegate(ScenePresence avatar)
  460. {
  461. ILandObject over = null;
  462. try
  463. {
  464. over =
  465. m_scene.LandChannel.GetLandObject(Util.Clamp<int>((int)Math.Round(avatar.AbsolutePosition.X), 0, ((int)Constants.RegionSize - 1)),
  466. Util.Clamp<int>((int)Math.Round(avatar.AbsolutePosition.Y), 0, ((int)Constants.RegionSize - 1)));
  467. }
  468. catch (Exception)
  469. {
  470. m_log.Warn("[LAND]: " + "unable to get land at x: " + Math.Round(avatar.AbsolutePosition.X) + " y: " +
  471. Math.Round(avatar.AbsolutePosition.Y));
  472. }
  473. if (over != null)
  474. {
  475. if (over.LandData.LocalID == LandData.LocalID)
  476. {
  477. if (((over.LandData.Flags & (uint)ParcelFlags.AllowDamage) != 0) &&
  478. m_scene.RegionInfo.RegionSettings.AllowDamage)
  479. avatar.Invulnerable = false;
  480. else
  481. avatar.Invulnerable = true;
  482. SendLandUpdateToClient(snap_selection, avatar.ControllingClient);
  483. }
  484. }
  485. });
  486. }
  487. #endregion
  488. #region AccessList Functions
  489. public List<LandAccessEntry> CreateAccessListArrayByFlag(AccessList flag)
  490. {
  491. ExpireAccessList();
  492. List<LandAccessEntry> list = new List<LandAccessEntry>();
  493. foreach (LandAccessEntry entry in LandData.ParcelAccessList)
  494. {
  495. if (entry.Flags == flag)
  496. list.Add(entry);
  497. }
  498. if (list.Count == 0)
  499. {
  500. LandAccessEntry e = new LandAccessEntry();
  501. e.AgentID = UUID.Zero;
  502. e.Flags = 0;
  503. e.Expires = 0;
  504. list.Add(e);
  505. }
  506. return list;
  507. }
  508. public void SendAccessList(UUID agentID, UUID sessionID, uint flags, int sequenceID,
  509. IClientAPI remote_client)
  510. {
  511. if (flags == (uint) AccessList.Access || flags == (uint) AccessList.Both)
  512. {
  513. List<LandAccessEntry> accessEntries = CreateAccessListArrayByFlag(AccessList.Access);
  514. remote_client.SendLandAccessListData(accessEntries,(uint) AccessList.Access,LandData.LocalID);
  515. }
  516. if (flags == (uint) AccessList.Ban || flags == (uint) AccessList.Both)
  517. {
  518. List<LandAccessEntry> accessEntries = CreateAccessListArrayByFlag(AccessList.Ban);
  519. remote_client.SendLandAccessListData(accessEntries, (uint)AccessList.Ban, LandData.LocalID);
  520. }
  521. }
  522. public void UpdateAccessList(uint flags, UUID transactionID,
  523. int sequenceID, int sections,
  524. List<LandAccessEntry> entries,
  525. IClientAPI remote_client)
  526. {
  527. LandData newData = LandData.Copy();
  528. if ((!m_listTransactions.ContainsKey(flags)) ||
  529. m_listTransactions[flags] != transactionID)
  530. {
  531. m_listTransactions[flags] = transactionID;
  532. List<LandAccessEntry> toRemove =
  533. new List<LandAccessEntry>();
  534. foreach (LandAccessEntry entry in newData.ParcelAccessList)
  535. {
  536. if (entry.Flags == (AccessList)flags)
  537. toRemove.Add(entry);
  538. }
  539. foreach (LandAccessEntry entry in toRemove)
  540. {
  541. newData.ParcelAccessList.Remove(entry);
  542. }
  543. // Checked here because this will always be the first
  544. // and only packet in a transaction
  545. if (entries.Count == 1 && entries[0].AgentID == UUID.Zero)
  546. {
  547. m_scene.LandChannel.UpdateLandObject(LandData.LocalID, newData);
  548. return;
  549. }
  550. }
  551. foreach (LandAccessEntry entry in entries)
  552. {
  553. LandAccessEntry temp =
  554. new LandAccessEntry();
  555. temp.AgentID = entry.AgentID;
  556. temp.Expires = entry.Expires;
  557. temp.Flags = (AccessList)flags;
  558. newData.ParcelAccessList.Add(temp);
  559. }
  560. m_scene.LandChannel.UpdateLandObject(LandData.LocalID, newData);
  561. }
  562. #endregion
  563. #region Update Functions
  564. public void UpdateLandBitmapByteArray()
  565. {
  566. LandData.Bitmap = ConvertLandBitmapToBytes();
  567. }
  568. /// <summary>
  569. /// Update all settings in land such as area, bitmap byte array, etc
  570. /// </summary>
  571. public void ForceUpdateLandInfo()
  572. {
  573. UpdateAABBAndAreaValues();
  574. UpdateLandBitmapByteArray();
  575. }
  576. public void SetLandBitmapFromByteArray()
  577. {
  578. LandBitmap = ConvertBytesToLandBitmap();
  579. }
  580. /// <summary>
  581. /// Updates the AABBMin and AABBMax values after area/shape modification of the land object
  582. /// </summary>
  583. private void UpdateAABBAndAreaValues()
  584. {
  585. int min_x = 64;
  586. int min_y = 64;
  587. int max_x = 0;
  588. int max_y = 0;
  589. int tempArea = 0;
  590. int x, y;
  591. for (x = 0; x < 64; x++)
  592. {
  593. for (y = 0; y < 64; y++)
  594. {
  595. if (LandBitmap[x, y] == true)
  596. {
  597. if (min_x > x) min_x = x;
  598. if (min_y > y) min_y = y;
  599. if (max_x < x) max_x = x;
  600. if (max_y < y) max_y = y;
  601. tempArea += 16; //16sqm peice of land
  602. }
  603. }
  604. }
  605. int tx = min_x * 4;
  606. if (tx > ((int)Constants.RegionSize - 1))
  607. tx = ((int)Constants.RegionSize - 1);
  608. int ty = min_y * 4;
  609. if (ty > ((int)Constants.RegionSize - 1))
  610. ty = ((int)Constants.RegionSize - 1);
  611. LandData.AABBMin =
  612. new Vector3(
  613. (float)(min_x * 4), (float)(min_y * 4), m_scene != null ? (float)m_scene.Heightmap[tx, ty] : 0);
  614. tx = max_x * 4;
  615. if (tx > ((int)Constants.RegionSize - 1))
  616. tx = ((int)Constants.RegionSize - 1);
  617. ty = max_y * 4;
  618. if (ty > ((int)Constants.RegionSize - 1))
  619. ty = ((int)Constants.RegionSize - 1);
  620. LandData.AABBMax
  621. = new Vector3(
  622. (float)(max_x * 4), (float)(max_y * 4), m_scene != null ? (float)m_scene.Heightmap[tx, ty] : 0);
  623. LandData.Area = tempArea;
  624. }
  625. #endregion
  626. #region Land Bitmap Functions
  627. /// <summary>
  628. /// Sets the land's bitmap manually
  629. /// </summary>
  630. /// <param name="bitmap">64x64 block representing where this land is on a map</param>
  631. public void SetLandBitmap(bool[,] bitmap)
  632. {
  633. if (bitmap.GetLength(0) != 64 || bitmap.GetLength(1) != 64 || bitmap.Rank != 2)
  634. {
  635. //Throw an exception - The bitmap is not 64x64
  636. //throw new Exception("Error: Invalid Parcel Bitmap");
  637. }
  638. else
  639. {
  640. //Valid: Lets set it
  641. LandBitmap = bitmap;
  642. ForceUpdateLandInfo();
  643. }
  644. }
  645. /// <summary>
  646. /// Gets the land's bitmap manually
  647. /// </summary>
  648. /// <returns></returns>
  649. public bool[,] GetLandBitmap()
  650. {
  651. return LandBitmap;
  652. }
  653. public bool[,] BasicFullRegionLandBitmap()
  654. {
  655. return GetSquareLandBitmap(0, 0, (int) Constants.RegionSize, (int) Constants.RegionSize);
  656. }
  657. public bool[,] GetSquareLandBitmap(int start_x, int start_y, int end_x, int end_y)
  658. {
  659. bool[,] tempBitmap = new bool[64,64];
  660. tempBitmap.Initialize();
  661. tempBitmap = ModifyLandBitmapSquare(tempBitmap, start_x, start_y, end_x, end_y, true);
  662. return tempBitmap;
  663. }
  664. /// <summary>
  665. /// Change a land bitmap at within a square and set those points to a specific value
  666. /// </summary>
  667. /// <param name="land_bitmap"></param>
  668. /// <param name="start_x"></param>
  669. /// <param name="start_y"></param>
  670. /// <param name="end_x"></param>
  671. /// <param name="end_y"></param>
  672. /// <param name="set_value"></param>
  673. /// <returns></returns>
  674. public bool[,] ModifyLandBitmapSquare(bool[,] land_bitmap, int start_x, int start_y, int end_x, int end_y,
  675. bool set_value)
  676. {
  677. if (land_bitmap.GetLength(0) != 64 || land_bitmap.GetLength(1) != 64 || land_bitmap.Rank != 2)
  678. {
  679. //Throw an exception - The bitmap is not 64x64
  680. //throw new Exception("Error: Invalid Parcel Bitmap in modifyLandBitmapSquare()");
  681. }
  682. int x, y;
  683. for (y = 0; y < 64; y++)
  684. {
  685. for (x = 0; x < 64; x++)
  686. {
  687. if (x >= start_x / 4 && x < end_x / 4
  688. && y >= start_y / 4 && y < end_y / 4)
  689. {
  690. land_bitmap[x, y] = set_value;
  691. }
  692. }
  693. }
  694. return land_bitmap;
  695. }
  696. /// <summary>
  697. /// Join the true values of 2 bitmaps together
  698. /// </summary>
  699. /// <param name="bitmap_base"></param>
  700. /// <param name="bitmap_add"></param>
  701. /// <returns></returns>
  702. public bool[,] MergeLandBitmaps(bool[,] bitmap_base, bool[,] bitmap_add)
  703. {
  704. if (bitmap_base.GetLength(0) != 64 || bitmap_base.GetLength(1) != 64 || bitmap_base.Rank != 2)
  705. {
  706. //Throw an exception - The bitmap is not 64x64
  707. throw new Exception("Error: Invalid Parcel Bitmap - Bitmap_base in mergeLandBitmaps");
  708. }
  709. if (bitmap_add.GetLength(0) != 64 || bitmap_add.GetLength(1) != 64 || bitmap_add.Rank != 2)
  710. {
  711. //Throw an exception - The bitmap is not 64x64
  712. throw new Exception("Error: Invalid Parcel Bitmap - Bitmap_add in mergeLandBitmaps");
  713. }
  714. int x, y;
  715. for (y = 0; y < 64; y++)
  716. {
  717. for (x = 0; x < 64; x++)
  718. {
  719. if (bitmap_add[x, y])
  720. {
  721. bitmap_base[x, y] = true;
  722. }
  723. }
  724. }
  725. return bitmap_base;
  726. }
  727. /// <summary>
  728. /// Converts the land bitmap to a packet friendly byte array
  729. /// </summary>
  730. /// <returns></returns>
  731. private byte[] ConvertLandBitmapToBytes()
  732. {
  733. byte[] tempConvertArr = new byte[512];
  734. byte tempByte = 0;
  735. int x, y, i, byteNum = 0;
  736. i = 0;
  737. for (y = 0; y < 64; y++)
  738. {
  739. for (x = 0; x < 64; x++)
  740. {
  741. tempByte = Convert.ToByte(tempByte | Convert.ToByte(LandBitmap[x, y]) << (i++ % 8));
  742. if (i % 8 == 0)
  743. {
  744. tempConvertArr[byteNum] = tempByte;
  745. tempByte = (byte) 0;
  746. i = 0;
  747. byteNum++;
  748. }
  749. }
  750. }
  751. return tempConvertArr;
  752. }
  753. private bool[,] ConvertBytesToLandBitmap()
  754. {
  755. bool[,] tempConvertMap = new bool[landArrayMax, landArrayMax];
  756. tempConvertMap.Initialize();
  757. byte tempByte = 0;
  758. int x = 0, y = 0, i = 0, bitNum = 0;
  759. for (i = 0; i < 512; i++)
  760. {
  761. tempByte = LandData.Bitmap[i];
  762. for (bitNum = 0; bitNum < 8; bitNum++)
  763. {
  764. bool bit = Convert.ToBoolean(Convert.ToByte(tempByte >> bitNum) & (byte) 1);
  765. tempConvertMap[x, y] = bit;
  766. x++;
  767. if (x > 63)
  768. {
  769. x = 0;
  770. y++;
  771. }
  772. }
  773. }
  774. return tempConvertMap;
  775. }
  776. #endregion
  777. #region Object Select and Object Owner Listing
  778. public void SendForceObjectSelect(int local_id, int request_type, List<UUID> returnIDs, IClientAPI remote_client)
  779. {
  780. if (m_scene.Permissions.CanEditParcelProperties(remote_client.AgentId, this, GroupPowers.LandOptions))
  781. {
  782. List<uint> resultLocalIDs = new List<uint>();
  783. try
  784. {
  785. lock (primsOverMe)
  786. {
  787. foreach (SceneObjectGroup obj in primsOverMe)
  788. {
  789. if (obj.LocalId > 0)
  790. {
  791. if (request_type == LandChannel.LAND_SELECT_OBJECTS_OWNER && obj.OwnerID == LandData.OwnerID)
  792. {
  793. resultLocalIDs.Add(obj.LocalId);
  794. }
  795. else if (request_type == LandChannel.LAND_SELECT_OBJECTS_GROUP && obj.GroupID == LandData.GroupID && LandData.GroupID != UUID.Zero)
  796. {
  797. resultLocalIDs.Add(obj.LocalId);
  798. }
  799. else if (request_type == LandChannel.LAND_SELECT_OBJECTS_OTHER &&
  800. obj.OwnerID != remote_client.AgentId)
  801. {
  802. resultLocalIDs.Add(obj.LocalId);
  803. }
  804. else if (request_type == (int)ObjectReturnType.List && returnIDs.Contains(obj.OwnerID))
  805. {
  806. resultLocalIDs.Add(obj.LocalId);
  807. }
  808. }
  809. }
  810. }
  811. } catch (InvalidOperationException)
  812. {
  813. m_log.Error("[LAND]: Unable to force select the parcel objects. Arr.");
  814. }
  815. remote_client.SendForceClientSelectObjects(resultLocalIDs);
  816. }
  817. }
  818. /// <summary>
  819. /// Notify the parcel owner each avatar that owns prims situated on their land. This notification includes
  820. /// aggreagete details such as the number of prims.
  821. ///
  822. /// </summary>
  823. /// <param name="remote_client">
  824. /// A <see cref="IClientAPI"/>
  825. /// </param>
  826. public void SendLandObjectOwners(IClientAPI remote_client)
  827. {
  828. if (m_scene.Permissions.CanEditParcelProperties(remote_client.AgentId, this, GroupPowers.LandOptions))
  829. {
  830. Dictionary<UUID, int> primCount = new Dictionary<UUID, int>();
  831. List<UUID> groups = new List<UUID>();
  832. lock (primsOverMe)
  833. {
  834. // m_log.DebugFormat(
  835. // "[LAND OBJECT]: Request for SendLandObjectOwners() from {0} with {1} known prims on region",
  836. // remote_client.Name, primsOverMe.Count);
  837. try
  838. {
  839. foreach (SceneObjectGroup obj in primsOverMe)
  840. {
  841. try
  842. {
  843. if (!primCount.ContainsKey(obj.OwnerID))
  844. {
  845. primCount.Add(obj.OwnerID, 0);
  846. }
  847. }
  848. catch (NullReferenceException)
  849. {
  850. m_log.Error("[LAND]: " + "Got Null Reference when searching land owners from the parcel panel");
  851. }
  852. try
  853. {
  854. primCount[obj.OwnerID] += obj.PrimCount;
  855. }
  856. catch (KeyNotFoundException)
  857. {
  858. m_log.Error("[LAND]: Unable to match a prim with it's owner.");
  859. }
  860. if (obj.OwnerID == obj.GroupID && (!groups.Contains(obj.OwnerID)))
  861. groups.Add(obj.OwnerID);
  862. }
  863. }
  864. catch (InvalidOperationException)
  865. {
  866. m_log.Error("[LAND]: Unable to Enumerate Land object arr.");
  867. }
  868. }
  869. remote_client.SendLandObjectOwners(LandData, groups, primCount);
  870. }
  871. }
  872. public Dictionary<UUID, int> GetLandObjectOwners()
  873. {
  874. Dictionary<UUID, int> ownersAndCount = new Dictionary<UUID, int>();
  875. lock (primsOverMe)
  876. {
  877. try
  878. {
  879. foreach (SceneObjectGroup obj in primsOverMe)
  880. {
  881. if (!ownersAndCount.ContainsKey(obj.OwnerID))
  882. {
  883. ownersAndCount.Add(obj.OwnerID, 0);
  884. }
  885. ownersAndCount[obj.OwnerID] += obj.PrimCount;
  886. }
  887. }
  888. catch (InvalidOperationException)
  889. {
  890. m_log.Error("[LAND]: Unable to enumerate land owners. arr.");
  891. }
  892. }
  893. return ownersAndCount;
  894. }
  895. #endregion
  896. #region Object Returning
  897. public void ReturnObject(SceneObjectGroup obj)
  898. {
  899. SceneObjectGroup[] objs = new SceneObjectGroup[1];
  900. objs[0] = obj;
  901. m_scene.returnObjects(objs, obj.OwnerID);
  902. }
  903. public void ReturnLandObjects(uint type, UUID[] owners, UUID[] tasks, IClientAPI remote_client)
  904. {
  905. // m_log.DebugFormat(
  906. // "[LAND OBJECT]: Request to return objects in {0} from {1}", LandData.Name, remote_client.Name);
  907. Dictionary<UUID,List<SceneObjectGroup>> returns = new Dictionary<UUID,List<SceneObjectGroup>>();
  908. lock (primsOverMe)
  909. {
  910. if (type == (uint)ObjectReturnType.Owner)
  911. {
  912. foreach (SceneObjectGroup obj in primsOverMe)
  913. {
  914. if (obj.OwnerID == m_landData.OwnerID)
  915. {
  916. if (!returns.ContainsKey(obj.OwnerID))
  917. returns[obj.OwnerID] =
  918. new List<SceneObjectGroup>();
  919. returns[obj.OwnerID].Add(obj);
  920. }
  921. }
  922. }
  923. else if (type == (uint)ObjectReturnType.Group && m_landData.GroupID != UUID.Zero)
  924. {
  925. foreach (SceneObjectGroup obj in primsOverMe)
  926. {
  927. if (obj.GroupID == m_landData.GroupID)
  928. {
  929. if (!returns.ContainsKey(obj.OwnerID))
  930. returns[obj.OwnerID] =
  931. new List<SceneObjectGroup>();
  932. returns[obj.OwnerID].Add(obj);
  933. }
  934. }
  935. }
  936. else if (type == (uint)ObjectReturnType.Other)
  937. {
  938. foreach (SceneObjectGroup obj in primsOverMe)
  939. {
  940. if (obj.OwnerID != m_landData.OwnerID &&
  941. (obj.GroupID != m_landData.GroupID ||
  942. m_landData.GroupID == UUID.Zero))
  943. {
  944. if (!returns.ContainsKey(obj.OwnerID))
  945. returns[obj.OwnerID] =
  946. new List<SceneObjectGroup>();
  947. returns[obj.OwnerID].Add(obj);
  948. }
  949. }
  950. }
  951. else if (type == (uint)ObjectReturnType.List)
  952. {
  953. List<UUID> ownerlist = new List<UUID>(owners);
  954. foreach (SceneObjectGroup obj in primsOverMe)
  955. {
  956. if (ownerlist.Contains(obj.OwnerID))
  957. {
  958. if (!returns.ContainsKey(obj.OwnerID))
  959. returns[obj.OwnerID] =
  960. new List<SceneObjectGroup>();
  961. returns[obj.OwnerID].Add(obj);
  962. }
  963. }
  964. }
  965. }
  966. foreach (List<SceneObjectGroup> ol in returns.Values)
  967. {
  968. if (m_scene.Permissions.CanReturnObjects(this, remote_client.AgentId, ol))
  969. m_scene.returnObjects(ol.ToArray(), remote_client.AgentId);
  970. }
  971. }
  972. #endregion
  973. #region Object Adding/Removing from Parcel
  974. public void ResetOverMeRecord()
  975. {
  976. lock (primsOverMe)
  977. primsOverMe.Clear();
  978. }
  979. public void AddPrimOverMe(SceneObjectGroup obj)
  980. {
  981. // m_log.DebugFormat("[LAND OBJECT]: Adding scene object {0} {1} over {2}", obj.Name, obj.LocalId, LandData.Name);
  982. lock (primsOverMe)
  983. primsOverMe.Add(obj);
  984. }
  985. public void RemovePrimFromOverMe(SceneObjectGroup obj)
  986. {
  987. // m_log.DebugFormat("[LAND OBJECT]: Removing scene object {0} {1} from over {2}", obj.Name, obj.LocalId, LandData.Name);
  988. lock (primsOverMe)
  989. primsOverMe.Remove(obj);
  990. }
  991. #endregion
  992. /// <summary>
  993. /// Set the media url for this land parcel
  994. /// </summary>
  995. /// <param name="url"></param>
  996. public void SetMediaUrl(string url)
  997. {
  998. LandData.MediaURL = url;
  999. SendLandUpdateToAvatarsOverMe();
  1000. }
  1001. /// <summary>
  1002. /// Set the music url for this land parcel
  1003. /// </summary>
  1004. /// <param name="url"></param>
  1005. public void SetMusicUrl(string url)
  1006. {
  1007. LandData.MusicURL = url;
  1008. SendLandUpdateToAvatarsOverMe();
  1009. }
  1010. /// <summary>
  1011. /// Get the music url for this land parcel
  1012. /// </summary>
  1013. /// <returns>The music url.</returns>
  1014. public string GetMusicUrl()
  1015. {
  1016. return LandData.MusicURL;
  1017. }
  1018. #endregion
  1019. private void ExpireAccessList()
  1020. {
  1021. List<LandAccessEntry> delete = new List<LandAccessEntry>();
  1022. foreach (LandAccessEntry entry in LandData.ParcelAccessList)
  1023. {
  1024. if (entry.Expires != 0 && entry.Expires < Util.UnixTimeSinceEpoch())
  1025. delete.Add(entry);
  1026. }
  1027. foreach (LandAccessEntry entry in delete)
  1028. LandData.ParcelAccessList.Remove(entry);
  1029. if (delete.Count > 0)
  1030. m_scene.EventManager.TriggerLandObjectUpdated((uint)LandData.LocalID, this);
  1031. }
  1032. }
  1033. }