Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 70.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Data;
  7.  
  8. using Uber.HabboHotel.Pets;
  9. using Uber.HabboHotel.RoomBots;
  10. using Uber.HabboHotel.Pathfinding;
  11. using Uber.HabboHotel.Items;
  12. using Uber.HabboHotel.GameClients;
  13. using Uber.Messages;
  14. using Uber.Storage;
  15.  
  16. namespace Uber.HabboHotel.Rooms
  17. {
  18. enum MatrixState
  19. {
  20. BLOCKED = 0,
  21. WALKABLE = 1,
  22. WALKABLE_LASTSTEP = 2
  23. }
  24.  
  25. class Room
  26. {
  27. private uint Id;
  28.  
  29. public string Name;
  30. public string Description;
  31. public string Type;
  32. public string Owner;
  33. public string Password;
  34. public int Category;
  35. public int State;
  36. public int UsersNow;
  37. public int UsersMax;
  38. public string ModelName;
  39. public string CCTs;
  40. public int Score;
  41. public List<string> Tags;
  42. public bool AllowPets;
  43. public bool AllowPetsEating;
  44. public bool AllowWalkthrough;
  45. public bool Hidewall;
  46.  
  47. public List<RoomUser> UserList;
  48. public int UserCounter = 0;
  49.  
  50. private int IdleTime;
  51.  
  52. public RoomIcon myIcon;
  53.  
  54. public List<uint> UsersWithRights;
  55. private Dictionary<uint, Double> Bans;
  56.  
  57. public List<uint> HasWaterEffect;
  58. public Dictionary<uint, Thread> HasThread;
  59. public Dictionary<uint, Thread> BallThread;
  60.  
  61. public RoomEvent Event;
  62.  
  63. public string Wallpaper;
  64. public string Floor;
  65. public string Landscape;
  66.  
  67. public List<RoomItem> Items;
  68. public MoodlightData MoodlightData;
  69.  
  70. public List<Trade> ActiveTrades;
  71.  
  72. public bool KeepAlive;
  73.  
  74. public MatrixState[,] Matrix;
  75. public bool[,] UserMatrix;
  76. public Coord[,] BedMatrix;
  77. public double[,] HeightMatrix;
  78. public double[,] TopStackHeight;
  79.  
  80. public Boolean HasOngoingEvent
  81. {
  82. get
  83. {
  84. if (Event != null)
  85. {
  86. return true;
  87. }
  88.  
  89. return false;
  90. }
  91. }
  92.  
  93. public RoomIcon Icon
  94. {
  95. get
  96. {
  97. return myIcon;
  98. }
  99.  
  100. set
  101. {
  102. myIcon = value;
  103. }
  104. }
  105.  
  106. public int UserCount
  107. {
  108. get
  109. {
  110. int i = 0;
  111.  
  112. lock (UserList)
  113. {
  114. foreach (RoomUser User in UserList)
  115. {
  116. if (User.IsBot)
  117. {
  118. continue;
  119. }
  120.  
  121. i++;
  122. }
  123. }
  124.  
  125. return i;
  126. }
  127. }
  128.  
  129. public int TagCount
  130. {
  131. get
  132. {
  133. return Tags.Count;
  134. }
  135. }
  136.  
  137. public RoomModel Model
  138. {
  139. get
  140. {
  141. return UberEnvironment.GetGame().GetRoomManager().GetModel(ModelName);
  142. }
  143. }
  144.  
  145. public uint RoomId
  146. {
  147. get
  148. {
  149. return Id;
  150. }
  151. }
  152.  
  153. public List<RoomItem> FloorItems
  154. {
  155. get
  156. {
  157. List<RoomItem> FloorItems = new List<RoomItem>();
  158.  
  159. lock (Items)
  160. {
  161. foreach (RoomItem Item in Items)
  162. {
  163. if (!Item.IsFloorItem)
  164. {
  165. continue;
  166. }
  167.  
  168. FloorItems.Add(Item);
  169. }
  170. }
  171.  
  172. return FloorItems;
  173. }
  174. }
  175.  
  176. public List<RoomItem> WallItems
  177. {
  178. get
  179. {
  180. List<RoomItem> WallItems = new List<RoomItem>();
  181.  
  182. lock (Items)
  183. {
  184. foreach (RoomItem Item in Items)
  185. {
  186. if (!Item.IsWallItem)
  187. {
  188. continue;
  189. }
  190.  
  191. WallItems.Add(Item);
  192. }
  193. }
  194.  
  195. return WallItems;
  196. }
  197. }
  198.  
  199. public Boolean CanTradeInRoom
  200. {
  201. get
  202. {
  203. if (IsPublic)
  204. {
  205. return false;
  206. }
  207.  
  208. return true;
  209. }
  210. }
  211.  
  212. public bool IsPublic
  213. {
  214. get
  215. {
  216. if (Type == "public")
  217. {
  218. return true;
  219. }
  220.  
  221. return false;
  222. }
  223. }
  224.  
  225. public int PetCount
  226. {
  227. get
  228. {
  229. int c = 0;
  230.  
  231. lock (UserList)
  232. {
  233. List<RoomUser>.Enumerator Users = this.UserList.GetEnumerator();
  234.  
  235. while (Users.MoveNext())
  236. {
  237. if (Users.Current.IsPet)
  238. {
  239. c++;
  240. }
  241. }
  242. }
  243.  
  244. return c;
  245. }
  246. }
  247.  
  248. public Room(uint Id, string Name, string Description, string Type, string Owner, int Category,
  249. int State, int UsersMax, string ModelName, string CCTs, int Score, List<string> Tags, bool AllowPets,
  250. bool AllowPetsEating, bool AllowWalkthrough, bool Hidewall, RoomIcon Icon, string Password, string Wallpaper, string Floor,
  251. string Landscape)
  252. {
  253. this.Id = Id;
  254. this.Name = Name;
  255. this.Description = Description;
  256. this.Owner = Owner;
  257. this.Category = Category;
  258. this.Type = Type;
  259. this.State = State;
  260. this.UsersNow = 0;
  261. this.UsersMax = UsersMax;
  262. this.ModelName = ModelName;
  263. this.CCTs = CCTs;
  264. this.Score = Score;
  265. this.Tags = Tags;
  266. this.AllowPets = AllowPets;
  267. this.AllowPetsEating = AllowPetsEating;
  268. this.AllowWalkthrough = AllowWalkthrough;
  269. this.Hidewall = Hidewall;
  270. this.UserCounter = 0;
  271. this.UserList = new List<RoomUser>();
  272. this.myIcon = Icon;
  273. this.Password = Password;
  274. this.Bans = new Dictionary<uint, double>();
  275. this.Event = null;
  276. this.Wallpaper = Wallpaper;
  277. this.Floor = Floor;
  278. this.Landscape = Landscape;
  279. this.Items = new List<RoomItem>();
  280. this.ActiveTrades = new List<Trade>();
  281. this.UserMatrix = new bool[Model.MapSizeX, Model.MapSizeY];
  282.  
  283. this.HasWaterEffect = new List<uint>();
  284. this.HasThread = new Dictionary<uint, Thread>();
  285. this.BallThread = new Dictionary<uint, Thread>();
  286.  
  287. this.IdleTime = 0;
  288.  
  289. this.KeepAlive = true;
  290.  
  291. LoadRights();
  292. LoadFurniture();
  293.  
  294. GenerateMaps();
  295. }
  296.  
  297. public void InitBots()
  298. {
  299. List<RoomBot> Bots = UberEnvironment.GetGame().GetBotManager().GetBotsForRoom(RoomId);
  300.  
  301. foreach (RoomBot Bot in Bots)
  302. {
  303. DeployBot(Bot);
  304. }
  305. }
  306.  
  307. public void InitPets()
  308. {
  309. List<Pet> Pets = new List<Pet>();
  310. DataTable Data = null;
  311.  
  312. using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
  313. {
  314. dbClient.AddParamWithValue("roomid", RoomId);
  315. Data = dbClient.ReadDataTable("SELECT * FROM user_pets WHERE room_id = @roomid");
  316. }
  317.  
  318. if (Data == null)
  319. {
  320. return;
  321. }
  322.  
  323. foreach (DataRow Row in Data.Rows)
  324. {
  325. Pet Pet = UberEnvironment.GetGame().GetCatalog().GeneratePetFromRow(Row);
  326. DeployBot(new RoomBot(Pet.PetId, RoomId, "pet", "freeroam", Pet.Name, "", Pet.Look, Pet.X, Pet.Y, (int)Pet.Z, 0, 0, 0, 0, 0), Pet);
  327. }
  328. }
  329.  
  330. public RoomUser DeployBot(RoomBot Bot)
  331. {
  332. return DeployBot(Bot, null);
  333. }
  334.  
  335. public RoomUser DeployBot(RoomBot Bot, Pet PetData)
  336. {
  337. RoomUser BotUser = new RoomUser(0, RoomId, UserCounter++);
  338.  
  339. if ((Bot.X > 0 && Bot.Y > 0) && Bot.X < Model.MapSizeX && Bot.Y < Model.MapSizeY)
  340. {
  341. BotUser.SetPos(Bot.X, Bot.Y, Bot.Z);
  342. BotUser.SetRot(Bot.Rot);
  343. }
  344. else
  345. {
  346. Bot.X = Model.DoorX;
  347. Bot.Y = Model.DoorY;
  348.  
  349. BotUser.SetPos(Model.DoorX, Model.DoorY, Model.DoorZ);
  350. BotUser.SetRot(Model.DoorOrientation);
  351. }
  352.  
  353. UserMatrix[Bot.X, Bot.Y] = true;
  354.  
  355. BotUser.BotData = Bot;
  356. BotUser.BotAI = Bot.GenerateBotAI(BotUser.VirtualId);
  357.  
  358. if (BotUser.IsPet)
  359. {
  360. BotUser.BotAI.Init((int)Bot.BotId, BotUser.VirtualId, RoomId);
  361. BotUser.PetData = PetData;
  362. BotUser.PetData.VirtualId = BotUser.VirtualId;
  363. }
  364. else
  365. {
  366. BotUser.BotAI.Init(-1, BotUser.VirtualId, RoomId);
  367. }
  368.  
  369. lock (UserList)
  370. {
  371. UserList.Add(BotUser);
  372. }
  373.  
  374. UpdateUserStatus(BotUser);
  375. BotUser.UpdateNeeded = true;
  376.  
  377. ServerMessage EnterMessage = new ServerMessage(28);
  378. EnterMessage.AppendInt32(1);
  379. BotUser.Serialize(EnterMessage);
  380. SendMessage(EnterMessage);
  381.  
  382. BotUser.BotAI.OnSelfEnterRoom();
  383.  
  384. return BotUser;
  385. }
  386.  
  387. public void RemoveBot(int VirtualId, bool Kicked)
  388. {
  389. RoomUser User = GetRoomUserByVirtualId(VirtualId);
  390.  
  391. if (User == null || !User.IsBot)
  392. {
  393. return;
  394. }
  395.  
  396. User.BotAI.OnSelfLeaveRoom(Kicked);
  397.  
  398. ServerMessage LeaveMessage = new ServerMessage(29);
  399. LeaveMessage.AppendRawInt32(User.VirtualId);
  400. SendMessage(LeaveMessage);
  401.  
  402. UserMatrix[User.X, User.Y] = false;
  403.  
  404. UserList.Remove(User);
  405. }
  406.  
  407. public void OnUserSay(RoomUser User, string Message, bool Shout)
  408. {
  409. lock (UserList)
  410. {
  411. foreach (RoomUser Usr in UserList)
  412. {
  413. if (!Usr.IsBot)
  414. {
  415. continue;
  416. }
  417.  
  418. if (Shout)
  419. {
  420. Usr.BotAI.OnUserShout(User, Message);
  421. }
  422. else
  423. {
  424. Usr.BotAI.OnUserSay(User, Message);
  425. }
  426. }
  427. }
  428. }
  429.  
  430. public void RegenerateUserMatrix()
  431. {
  432. lock (UserList)
  433. {
  434. this.UserMatrix = new bool[Model.MapSizeX, Model.MapSizeY];
  435. List<RoomUser>.Enumerator eUsers = this.UserList.GetEnumerator();
  436.  
  437. while (eUsers.MoveNext())
  438. {
  439. RoomUser User = eUsers.Current;
  440. this.UserMatrix[User.X, User.Y] = true;
  441. }
  442. }
  443. }
  444.  
  445. public void GenerateMaps()
  446. {
  447. // Create matrix arrays
  448. Matrix = new MatrixState[Model.MapSizeX, Model.MapSizeY];
  449. BedMatrix = new Coord[Model.MapSizeX, Model.MapSizeY];
  450. HeightMatrix = new double[Model.MapSizeX, Model.MapSizeY];
  451. TopStackHeight = new double[Model.MapSizeX, Model.MapSizeY];
  452.  
  453. // Fill in the basic data based purely on the heightmap
  454. for (int line = 0; line < Model.MapSizeY; line++)
  455. {
  456. for (int chr = 0; chr < Model.MapSizeX; chr++)
  457. {
  458. Matrix[chr, line] = MatrixState.BLOCKED;
  459. BedMatrix[chr, line] = new Coord(chr, line);
  460. HeightMatrix[chr, line] = 0;
  461. TopStackHeight[chr, line] = 0.0;
  462.  
  463. if (chr == Model.DoorX && line == Model.DoorY)
  464. {
  465. Matrix[chr, line] = MatrixState.WALKABLE_LASTSTEP;
  466. }
  467. else if (Model.SqState[chr, line] == SquareState.OPEN)
  468. {
  469. Matrix[chr, line] = MatrixState.WALKABLE;
  470. }
  471. else if (Model.SqState[chr, line] == SquareState.SEAT)
  472. {
  473. Matrix[chr, line] = MatrixState.WALKABLE_LASTSTEP;
  474. }
  475. }
  476. }
  477.  
  478. // Loop through the items in the room
  479. lock (Items)
  480. {
  481. foreach (RoomItem Item in Items)
  482. {
  483. // If we're dealing with anything other than a floor item, skip
  484. if (Item.GetBaseItem().Type.ToLower() != "s")
  485. {
  486. continue;
  487. }
  488.  
  489. // If this is a rug, ignore it.
  490. if (Item.GetBaseItem().Height <= 0)
  491. {
  492. continue;
  493. }
  494.  
  495. // Make sure we're the highest item here!
  496. if (TopStackHeight[Item.X, Item.Y] <= Item.Z)
  497. {
  498. TopStackHeight[Item.X, Item.Y] = Item.Z;
  499.  
  500. // If this item is walkable and on the floor, allow users to walk here.
  501. if (Item.GetBaseItem().Walkable)
  502. {
  503. Matrix[Item.X, Item.Y] = MatrixState.WALKABLE;
  504. HeightMatrix[Item.X, Item.Y] = Item.GetBaseItem().Height;
  505. }
  506. // If this item is a gate, open, and on the floor, allow users to walk here.
  507. else if (Item.Z <= (Model.SqFloorHeight[Item.X, Item.Y] + 0.1) && Item.GetBaseItem().InteractionType.ToLower() == "gate" && Item.ExtraData == "1")
  508. {
  509. Matrix[Item.X, Item.Y] = MatrixState.WALKABLE;
  510. }
  511. // If this item is a set or a bed, make it's square walkable (but only if last step)
  512. else if (Item.GetBaseItem().IsSeat || Item.GetBaseItem().InteractionType.ToLower() == "bed")
  513. {
  514. Matrix[Item.X, Item.Y] = MatrixState.WALKABLE_LASTSTEP;
  515. }
  516. // Finally, if it's none of those, block the square.
  517. else
  518. {
  519. Matrix[Item.X, Item.Y] = MatrixState.BLOCKED;
  520. }
  521. }
  522.  
  523. Dictionary<int, AffectedTile> Points = GetAffectedTiles(Item.GetBaseItem().Length, Item.GetBaseItem().Width, Item.X, Item.Y, Item.Rot);
  524.  
  525. if (Points == null)
  526. {
  527. Points = new Dictionary<int, AffectedTile>();
  528. }
  529.  
  530. foreach (AffectedTile Tile in Points.Values)
  531. {
  532. // Make sure we're the highest item here!
  533. if (TopStackHeight[Tile.X, Tile.Y] <= Item.Z)
  534. {
  535. TopStackHeight[Tile.X, Tile.Y] = Item.Z;
  536.  
  537. // If this item is walkable and on the floor, allow users to walk here.
  538. if (Item.GetBaseItem().Walkable)
  539. {
  540. Matrix[Tile.X, Tile.Y] = MatrixState.WALKABLE;
  541. HeightMatrix[Tile.X, Tile.Y] = Item.GetBaseItem().Height;
  542. }
  543. // If this item is a gate, open, and on the floor, allow users to walk here.
  544. else if (Item.Z <= (Model.SqFloorHeight[Item.X, Item.Y] + 0.1) && Item.GetBaseItem().InteractionType.ToLower() == "gate" && Item.ExtraData == "1")
  545. {
  546. Matrix[Tile.X, Tile.Y] = MatrixState.WALKABLE;
  547. }
  548. // If this item is a set or a bed, make it's square walkable (but only if last step)
  549. else if (Item.GetBaseItem().IsSeat || Item.GetBaseItem().InteractionType.ToLower() == "bed")
  550. {
  551. Matrix[Tile.X, Tile.Y] = MatrixState.WALKABLE_LASTSTEP;
  552. }
  553. // Finally, if it's none of those, block the square.
  554. else
  555. {
  556. Matrix[Tile.X, Tile.Y] = MatrixState.BLOCKED;
  557. }
  558. }
  559.  
  560. // Set bad maps
  561. if (Item.GetBaseItem().InteractionType.ToLower() == "bed")
  562. {
  563. if (Item.Rot == 0 || Item.Rot == 4)
  564. {
  565. BedMatrix[Tile.X, Tile.Y].y = Item.Y;
  566. }
  567.  
  568. if (Item.Rot == 2 || Item.Rot == 6)
  569. {
  570. BedMatrix[Tile.X, Tile.Y].x = Item.X;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. }
  577.  
  578. public void LoadRights()
  579. {
  580. this.UsersWithRights = new List<uint>();
  581.  
  582. DataTable Data = null;
  583.  
  584. using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
  585. {
  586. Data = dbClient.ReadDataTable("SELECT user_id FROM room_rights WHERE room_id = '" + Id + "'");
  587. }
  588.  
  589. if (Data == null)
  590. {
  591. return;
  592. }
  593.  
  594. foreach (DataRow Row in Data.Rows)
  595. {
  596. this.UsersWithRights.Add((uint)Row["user_id"]);
  597. }
  598. }
  599.  
  600. public void LoadFurniture()
  601. {
  602. this.Items.Clear();
  603.  
  604. DataTable Data = null;
  605.  
  606. using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
  607. {
  608. Data = dbClient.ReadDataTable("SELECT * FROM room_items WHERE room_id = '" + Id + "'");
  609. }
  610.  
  611. if (Data == null)
  612. {
  613. return;
  614. }
  615.  
  616. foreach (DataRow Row in Data.Rows)
  617. {
  618. RoomItem Item = new RoomItem((uint)Row["id"], RoomId, (uint)Row["base_item"], (string)Row["extra_data"],
  619. (int)Row["x"], (int)Row["y"], (Double)Row["z"], (int)Row["rot"], (string)Row["wall_pos"]);
  620.  
  621. switch (Item.GetBaseItem().InteractionType.ToLower())
  622. {
  623. case "dimmer":
  624.  
  625. if (MoodlightData == null)
  626. {
  627. MoodlightData = new MoodlightData(Item.Id);
  628. }
  629.  
  630. break;
  631. }
  632.  
  633. this.Items.Add(Item);
  634. }
  635. }
  636.  
  637. public Boolean CheckRights(GameClient Session)
  638. {
  639. return CheckRights(Session, false);
  640. }
  641.  
  642. public Boolean CheckRights(GameClient Session, bool RequireOwnership)
  643. {
  644. if (Session.GetHabbo().Username.ToLower() == Owner.ToLower())
  645. {
  646. return true;
  647. }
  648.  
  649. if (Session.GetHabbo().HasFuse("fuse_admin") || Session.GetHabbo().HasFuse("fuse_any_room_controller"))
  650. {
  651. return true;
  652. }
  653.  
  654. if (!RequireOwnership)
  655. {
  656. if (Session.GetHabbo().HasFuse("fuse_any_room_rights"))
  657. {
  658. return true;
  659. }
  660.  
  661. if (UsersWithRights.Contains(Session.GetHabbo().Id))
  662. {
  663. return true;
  664. }
  665. }
  666.  
  667. return false;
  668. }
  669.  
  670. public RoomItem GetItem(uint Id)
  671. {
  672. lock (Items)
  673. {
  674. foreach (RoomItem Item in Items)
  675. {
  676. if (Item.Id == Id)
  677. {
  678. return Item;
  679. }
  680. }
  681. }
  682.  
  683. return null;
  684. }
  685.  
  686. public void RemoveFurniture(GameClient Session, uint Id)
  687. {
  688. RoomItem Item = GetItem(Id);
  689.  
  690. if (Item == null)
  691. {
  692. return;
  693. }
  694.  
  695. Item.Interactor.OnRemove(Session, Item);
  696.  
  697. if (Item.IsWallItem)
  698. {
  699. ServerMessage Message = new ServerMessage(84);
  700. Message.AppendRawUInt(Item.Id);
  701. Message.AppendStringWithBreak("");
  702. Message.AppendBoolean(false);
  703. SendMessage(Message);
  704. }
  705. else if (Item.IsFloorItem)
  706. {
  707. ServerMessage Message = new ServerMessage(94);
  708. Message.AppendRawUInt(Item.Id);
  709. Message.AppendStringWithBreak("");
  710. Message.AppendBoolean(false);
  711. SendMessage(Message);
  712. }
  713.  
  714. lock (Items)
  715. {
  716. Items.Remove(Item);
  717. }
  718.  
  719. using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
  720. {
  721. dbClient.ExecuteQuery("DELETE FROM room_items WHERE id = '" + Id + "' AND room_id = '" + RoomId + "' LIMIT 1");
  722. }
  723.  
  724. GenerateMaps();
  725. UpdateUserStatusses();
  726. }
  727.  
  728. public bool CanWalk(int X, int Y, Double Z, Boolean LastStep)
  729. {
  730. if (X < 0 || X >= Model.MapSizeX || Y >= Model.MapSizeY || Y < 0)
  731. {
  732. return false;
  733. }
  734.  
  735. if (SquareHasUsers(X, Y, LastStep))
  736. {
  737. return false;
  738. }
  739.  
  740. if (Matrix[X, Y] == MatrixState.BLOCKED)
  741. {
  742. return false;
  743. }
  744. else if (Matrix[X, Y] == MatrixState.WALKABLE_LASTSTEP && !LastStep)
  745. {
  746. return false;
  747. }
  748.  
  749. return true;
  750. }
  751.  
  752. public void ProcessRoom()
  753. {
  754. int i = 0;
  755.  
  756. // Loop through all furni and process them if they want to be processed
  757. lock (Items)
  758. {
  759. List<RoomItem>.Enumerator eItems = this.Items.GetEnumerator();
  760.  
  761. while (eItems.MoveNext())
  762. {
  763. try
  764. {
  765. RoomItem Item = eItems.Current;
  766.  
  767. if (!Item.UpdateNeeded)
  768. {
  769. continue;
  770. }
  771.  
  772. Item.ProcessUpdates();
  773. }
  774. catch (NullReferenceException e)
  775. {
  776. UberEnvironment.GetLogging().WriteLine("NullReferenceException at ProcessRoom: " + e.ToString(), Core.LogLevel.Error);
  777. continue;
  778. }
  779. }
  780. }
  781.  
  782. // Loop through all users and bots and process them
  783. lock (UserList)
  784. {
  785. List<uint> ToRemove = new List<uint>();
  786. List<RoomUser>.Enumerator eUsers = this.UserList.GetEnumerator();
  787.  
  788. while (eUsers.MoveNext())
  789. {
  790. RoomUser User = eUsers.Current;
  791.  
  792. User.IdleTime++;
  793.  
  794. if (!User.IsAsleep && User.IdleTime >= 600)
  795. {
  796. User.IsAsleep = true;
  797.  
  798. ServerMessage FallAsleep = new ServerMessage(486);
  799. FallAsleep.AppendInt32(User.VirtualId);
  800. FallAsleep.AppendBoolean(true);
  801. SendMessage(FallAsleep);
  802. }
  803.  
  804. if (User.NeedsAutokick && !ToRemove.Contains(User.HabboId))
  805. {
  806. ToRemove.Add(User.HabboId);
  807. }
  808.  
  809. if (User.CarryItemID > 0)
  810. {
  811. User.CarryTimer--;
  812.  
  813. if (User.CarryTimer <= 0)
  814. {
  815. User.CarryItem(0);
  816. }
  817. }
  818.  
  819. bool invalidSetStep = false;
  820.  
  821. if (User.SetStep)
  822. {
  823. if (CanWalk(User.SetX, User.SetY, 0, true) || User.AllowOverride || AllowWalkthrough == true) // crap walkthrough fix
  824. {
  825. UserMatrix[User.X, User.Y] = false;
  826.  
  827. User.X = User.SetX;
  828. User.Y = User.SetY;
  829. User.Z = User.SetZ;
  830.  
  831. UserMatrix[User.X, User.Y] = true;
  832.  
  833. UpdateUserStatus(User);
  834. }
  835. else
  836. {
  837. invalidSetStep = true;
  838. }
  839.  
  840. User.SetStep = false;
  841. }
  842.  
  843. if (User.PathRecalcNeeded)
  844. {
  845. Pathfinder Pathfinder = new Pathfinder(this, User);
  846.  
  847. User.GoalX = User.PathRecalcX;
  848. User.GoalY = User.PathRecalcY;
  849.  
  850. User.Path.Clear();
  851. User.Path = Pathfinder.FindPath();
  852.  
  853. if (User.Path.Count > 1)
  854. {
  855. User.PathStep = 1;
  856. User.IsWalking = true;
  857. User.PathRecalcNeeded = false;
  858. }
  859. else
  860. {
  861. User.PathRecalcNeeded = false;
  862. User.Path.Clear();
  863. }
  864. }
  865.  
  866. if (User.IsWalking)
  867. {
  868. if (invalidSetStep || User.PathStep >= User.Path.Count || User.GoalX == User.X && User.Y == User.GoalY)
  869. {
  870. User.Path.Clear();
  871. User.IsWalking = false;
  872. User.RemoveStatus("mv");
  873. User.PathRecalcNeeded = false;
  874.  
  875. if (User.X == Model.DoorX && User.Y == Model.DoorY && !ToRemove.Contains(User.HabboId) && !User.IsBot)
  876. {
  877. ToRemove.Add(User.HabboId);
  878. }
  879.  
  880. UpdateUserStatus(User);
  881. }
  882. else
  883. {
  884. int k = (User.Path.Count - User.PathStep) - 1;
  885. Coord NextStep = User.Path[k];
  886. User.PathStep++;
  887.  
  888. int nextX = NextStep.x;
  889. int nextY = NextStep.y;
  890.  
  891. User.RemoveStatus("mv");
  892.  
  893. bool LastStep = false;
  894.  
  895. if (nextX == User.GoalX && nextY == User.GoalY)
  896. {
  897. LastStep = true;
  898. }
  899.  
  900. if (CanWalk(nextX, nextY, 0, LastStep) || User.AllowOverride)
  901. {
  902. double nextZ = SqAbsoluteHeight(nextX, nextY);
  903.  
  904. User.Statusses.Remove("lay");
  905. User.Statusses.Remove("sit");
  906. User.AddStatus("mv", nextX + "," + nextY + "," + nextZ.ToString().Replace(',','.'));
  907.  
  908. int newRot = Rotation.Calculate(User.X, User.Y, nextX, nextY);
  909.  
  910. User.RotBody = newRot;
  911. User.RotHead = newRot;
  912.  
  913. User.SetStep = true;
  914. User.SetX = BedMatrix[nextX, nextY].x;
  915. User.SetY = BedMatrix[nextX, nextY].y;
  916. User.SetZ = nextZ;
  917. }
  918. else
  919. {
  920. User.IsWalking = false;
  921. }
  922. }
  923.  
  924. User.UpdateNeeded = true;
  925. }
  926. else
  927. {
  928. if (User.Statusses.ContainsKey("mv"))
  929. {
  930. User.RemoveStatus("mv");
  931. User.UpdateNeeded = true;
  932. }
  933. }
  934.  
  935. if (User.IsBot)
  936. {
  937. User.BotAI.OnTimerTick();
  938. }
  939. else
  940. {
  941. i++; // we do not count bots. we do not take kindly to their kind 'round 'ere.
  942. }
  943. }
  944.  
  945. foreach (uint toRemove in ToRemove)
  946. {
  947. RemoveUserFromRoom(UberEnvironment.GetGame().GetClientManager().GetClientByHabbo(toRemove), true, false);
  948. }
  949. }
  950.  
  951. // Update idle time
  952. if (i >= 1)
  953. {
  954. this.IdleTime = 0;
  955. }
  956. else
  957. {
  958. this.IdleTime++;
  959. }
  960.  
  961. // If room has been idle for a while
  962. if (this.IdleTime >= 60)
  963. {
  964. UberEnvironment.GetLogging().WriteLine("[RoomMgr] Requesting unload of idle room - ID#: " + Id, Uber.Core.LogLevel.Debug);
  965. UberEnvironment.GetGame().GetRoomManager().RequestRoomUnload(Id);
  966. }
  967.  
  968. ServerMessage Updates = SerializeStatusUpdates(false);
  969.  
  970. if (Updates != null)
  971. {
  972. SendMessage(Updates);
  973. }
  974. }
  975.  
  976. #region User handling (General)
  977. public void AddUserToRoom(GameClient Session, bool Spectator)
  978. {
  979. RoomUser User = new RoomUser(Session.GetHabbo().Id, RoomId, UserCounter++);
  980.  
  981. if (Spectator)
  982. {
  983. User.IsSpectator = true;
  984. }
  985. else
  986. {
  987. User.SetPos(Model.DoorX, Model.DoorY, Model.DoorZ);
  988. User.SetRot(Model.DoorOrientation);
  989.  
  990. if (CheckRights(Session, true))
  991. {
  992. User.AddStatus("flatcrtl", "useradmin");
  993. }
  994. else if (CheckRights(Session))
  995. {
  996. User.AddStatus("flatcrtl", "");
  997. }
  998.  
  999. if (!User.IsBot && User.GetClient().GetHabbo().IsTeleporting)
  1000. {
  1001. RoomItem Item = GetItem(User.GetClient().GetHabbo().TeleporterId);
  1002.  
  1003. if (Item != null)
  1004. {
  1005. User.SetPos(Item.X, Item.Y, Item.Z);
  1006. User.SetRot(Item.Rot);
  1007.  
  1008. Item.InteractingUser2 = Session.GetHabbo().Id;
  1009. Item.ExtraData = "2";
  1010. Item.UpdateState(false, true);
  1011. }
  1012. }
  1013.  
  1014. User.GetClient().GetHabbo().IsTeleporting = false;
  1015. User.GetClient().GetHabbo().TeleporterId = 0;
  1016.  
  1017. ServerMessage EnterMessage = new ServerMessage(28);
  1018. EnterMessage.AppendInt32(1);
  1019. User.Serialize(EnterMessage);
  1020. SendMessage(EnterMessage);
  1021. }
  1022.  
  1023. UserList.Add(User);
  1024. Session.GetHabbo().OnEnterRoom(Id);
  1025.  
  1026. if (!Spectator)
  1027. {
  1028. UpdateUserCount();
  1029.  
  1030. lock (UserList)
  1031. {
  1032. foreach (RoomUser Usr in UserList)
  1033. {
  1034. if (!Usr.IsBot)
  1035. {
  1036. continue;
  1037. }
  1038.  
  1039. Usr.BotAI.OnUserEnterRoom(User);
  1040. }
  1041. }
  1042. }
  1043. }
  1044.  
  1045. public void RemoveUserFromRoom(GameClient Session, Boolean NotifyClient, Boolean NotifyKick)
  1046. {
  1047. try
  1048. {
  1049. if (Session == null)
  1050. {
  1051. return;
  1052. }
  1053.  
  1054. RoomUser User = GetRoomUserByHabbo(Session.GetHabbo().Id);
  1055.  
  1056. if (User == null)
  1057. {
  1058. return;
  1059. }
  1060.  
  1061. lock (UserList)
  1062. {
  1063. if (!UserList.Remove(GetRoomUserByHabbo(Session.GetHabbo().Id)))
  1064. {
  1065. return;
  1066. }
  1067. }
  1068.  
  1069. if (NotifyClient)
  1070. {
  1071. if (NotifyKick)
  1072. {
  1073. Session.GetMessageHandler().GetResponse().Init(33);
  1074. Session.GetMessageHandler().GetResponse().AppendInt32(4008);
  1075. Session.GetMessageHandler().SendResponse();
  1076. }
  1077.  
  1078. Session.GetMessageHandler().GetResponse().Init(18);
  1079. Session.GetMessageHandler().SendResponse();
  1080. }
  1081.  
  1082. List<RoomUser> PetsToRemove = new List<RoomUser>();
  1083.  
  1084. lock (UserList)
  1085. {
  1086. if (!User.IsSpectator)
  1087. {
  1088. if (User != null)
  1089. {
  1090. UserMatrix[User.X, User.Y] = false;
  1091.  
  1092. ServerMessage LeaveMessage = new ServerMessage(29);
  1093. LeaveMessage.AppendRawInt32(User.VirtualId);
  1094. SendMessage(LeaveMessage);
  1095. }
  1096.  
  1097. if (Session.GetHabbo() != null)
  1098. {
  1099. if (HasActiveTrade(Session.GetHabbo().Id))
  1100. {
  1101. TryStopTrade(Session.GetHabbo().Id);
  1102. }
  1103.  
  1104. if (Session.GetHabbo().Username.ToLower() == Owner.ToLower())
  1105. {
  1106. if (HasOngoingEvent)
  1107. {
  1108. Event = null;
  1109.  
  1110. ServerMessage Message = new ServerMessage(370);
  1111. Message.AppendStringWithBreak("-1");
  1112. SendMessage(Message);
  1113. }
  1114. }
  1115.  
  1116. Session.GetHabbo().OnLeaveRoom();
  1117. }
  1118. }
  1119.  
  1120. if (!User.IsSpectator)
  1121. {
  1122. UpdateUserCount();
  1123.  
  1124. List<RoomUser> Bots = new List<RoomUser>();
  1125.  
  1126. foreach (RoomUser Usr in UserList)
  1127. {
  1128. if (!Usr.IsBot)
  1129. {
  1130. continue;
  1131. }
  1132.  
  1133. Bots.Add(Usr);
  1134. }
  1135.  
  1136. foreach (RoomUser Bot in Bots)
  1137. {
  1138. Bot.BotAI.OnUserLeaveRoom(Session);
  1139.  
  1140. if (Bot.IsPet && Bot.PetData.OwnerId == Session.GetHabbo().Id && !CheckRights(Session, true))
  1141. {
  1142. PetsToRemove.Add(Bot);
  1143. }
  1144. }
  1145. }
  1146. }
  1147.  
  1148. foreach (RoomUser toRemove in PetsToRemove)
  1149. {
  1150. Session.GetHabbo().GetInventoryComponent().AddPet(toRemove.PetData);
  1151. RemoveBot(toRemove.VirtualId, false);
  1152. }
  1153. }
  1154. catch (NullReferenceException e) { UberEnvironment.GetLogging().WriteLine("NullReferenceException" + e.ToString(), Core.LogLevel.Error); }
  1155. }
  1156.  
  1157. public RoomUser GetPet(uint PetId)
  1158. {
  1159. lock (UserList)
  1160. {
  1161. List<RoomUser>.Enumerator Users = this.UserList.GetEnumerator();
  1162.  
  1163. while (Users.MoveNext())
  1164. {
  1165. RoomUser User = Users.Current;
  1166.  
  1167. if (User.IsBot && User.IsPet && User.PetData != null && User.PetData.PetId == PetId)
  1168. {
  1169. return User;
  1170. }
  1171. }
  1172. }
  1173.  
  1174. return null;
  1175. }
  1176.  
  1177. public bool RoomContainsPet(uint PetId)
  1178. {
  1179. return (GetPet(PetId) != null);
  1180. }
  1181.  
  1182. public void UpdateUserCount()
  1183. {
  1184. this.UsersNow = UserCount;
  1185.  
  1186. using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
  1187. {
  1188. dbClient.ExecuteQuery("UPDATE rooms SET users_now = '" + this.UsersNow + "' WHERE id = '" + Id + "' LIMIT 1");
  1189. }
  1190. }
  1191.  
  1192. public RoomUser GetRoomUserByVirtualId(int VirtualId)
  1193. {
  1194. lock (UserList)
  1195. {
  1196. foreach (RoomUser User in UserList)
  1197. {
  1198. if (User.VirtualId == VirtualId)
  1199. {
  1200. return User;
  1201. }
  1202. }
  1203. }
  1204.  
  1205. return null;
  1206. }
  1207.  
  1208. public RoomUser GetRoomUserByHabbo(uint Id)
  1209. {
  1210. lock (UserList)
  1211. {
  1212. foreach (RoomUser User in UserList)
  1213. {
  1214. if (User.IsBot)
  1215. {
  1216. continue;
  1217. }
  1218.  
  1219. if (User.HabboId == Id)
  1220. {
  1221. return User;
  1222. }
  1223. }
  1224. }
  1225.  
  1226. return null;
  1227. }
  1228.  
  1229. public RoomUser GetRoomUserByHabbo(string Name)
  1230. {
  1231. lock (UserList)
  1232. {
  1233. foreach (RoomUser User in UserList)
  1234. {
  1235. if (User.IsBot || User.GetClient().GetHabbo() == null)
  1236. {
  1237. continue;
  1238. }
  1239.  
  1240. if (User.GetClient().GetHabbo().Username.ToLower() == Name.ToLower())
  1241. {
  1242. return User;
  1243. }
  1244. }
  1245. }
  1246.  
  1247. return null;
  1248. }
  1249. #endregion
  1250.  
  1251. #region Communication
  1252. public void SendMessage(ServerMessage Message)
  1253. {
  1254. try
  1255. {
  1256. lock (UserList)
  1257. {
  1258. foreach (RoomUser User in this.UserList)
  1259. {
  1260. if (User.IsBot || User.GetClient() == null)
  1261. {
  1262. continue;
  1263. }
  1264.  
  1265. User.GetClient().SendMessage(Message);
  1266. }
  1267. }
  1268. }
  1269. catch (InvalidOperationException) { }
  1270. }
  1271.  
  1272. public void SendMessageToUsersWithRights(ServerMessage Message)
  1273. {
  1274. lock (UserList)
  1275. {
  1276. foreach (RoomUser User in UserList)
  1277. {
  1278. if (User.IsBot)
  1279. {
  1280. continue;
  1281. }
  1282.  
  1283. if (!CheckRights(User.GetClient()))
  1284. {
  1285. continue;
  1286. }
  1287.  
  1288. User.GetClient().SendMessage(Message);
  1289. }
  1290. }
  1291. }
  1292. #endregion
  1293.  
  1294. public void Destroy()
  1295. {
  1296. // Send kick message. No users should be left in this room, but if they are for whatever reason, they should leave.
  1297. SendMessage(new ServerMessage(18));
  1298.  
  1299. // Clear user list and dispose of the engine thread
  1300. this.IdleTime = 0;
  1301. this.KeepAlive = false;
  1302. UserList.Clear();
  1303. }
  1304.  
  1305. public ServerMessage SerializeStatusUpdates(Boolean All)
  1306. {
  1307. List<RoomUser> Users = new List<RoomUser>();
  1308.  
  1309. lock (UserList)
  1310. {
  1311. foreach (RoomUser User in UserList)
  1312. {
  1313. if (!All)
  1314. {
  1315. if (!User.UpdateNeeded)
  1316. {
  1317. continue;
  1318. }
  1319.  
  1320. User.UpdateNeeded = false;
  1321. }
  1322.  
  1323. Users.Add(User);
  1324. }
  1325. }
  1326.  
  1327. if (Users.Count == 0)
  1328. {
  1329. return null;
  1330. }
  1331.  
  1332. ServerMessage Message = new ServerMessage(34);
  1333. Message.AppendInt32(Users.Count);
  1334.  
  1335. foreach (RoomUser User in Users)
  1336. {
  1337. User.SerializeStatus(Message);
  1338. }
  1339.  
  1340. return Message;
  1341. }
  1342.  
  1343. #region Room Bans
  1344. public Boolean UserIsBanned(uint Id)
  1345. {
  1346. return Bans.ContainsKey(Id);
  1347. }
  1348.  
  1349. public void RemoveBan(uint Id)
  1350. {
  1351. Bans.Remove(Id);
  1352. }
  1353.  
  1354. public void AddBan(uint Id)
  1355. {
  1356. Bans.Add(Id, UberEnvironment.GetUnixTimestamp());
  1357. }
  1358.  
  1359. public Boolean HasBanExpired(uint Id)
  1360. {
  1361. if (!UserIsBanned(Id))
  1362. {
  1363. return true;
  1364. }
  1365.  
  1366. Double diff = UberEnvironment.GetUnixTimestamp() - Bans[Id];
  1367.  
  1368. if (diff > 900)
  1369. {
  1370. return true;
  1371. }
  1372.  
  1373. return false;
  1374. }
  1375. #endregion
  1376.  
  1377. public int ItemCountByType(String InteractionType)
  1378. {
  1379. int i = 0;
  1380.  
  1381. lock (Items)
  1382. {
  1383. foreach (RoomItem Item in Items)
  1384. {
  1385. if (Item.GetBaseItem().InteractionType.ToLower() == InteractionType.ToLower())
  1386. {
  1387. i++;
  1388. }
  1389. }
  1390. }
  1391.  
  1392. return i;
  1393. }
  1394.  
  1395. #region Trading
  1396. public bool HasActiveTrade(RoomUser User)
  1397. {
  1398. if (User.IsBot)
  1399. {
  1400. return false;
  1401. }
  1402.  
  1403. return HasActiveTrade(User.GetClient().GetHabbo().Id);
  1404. }
  1405.  
  1406. public bool HasActiveTrade(uint UserId)
  1407. {
  1408. lock (ActiveTrades)
  1409. {
  1410. foreach (Trade Trade in ActiveTrades)
  1411. {
  1412. if (Trade.ContainsUser(UserId))
  1413. {
  1414. return true;
  1415. }
  1416. }
  1417. }
  1418.  
  1419. return false;
  1420. }
  1421.  
  1422. public Trade GetUserTrade(RoomUser User)
  1423. {
  1424. if (User.IsBot)
  1425. {
  1426. return null;
  1427. }
  1428.  
  1429. return GetUserTrade(User.GetClient().GetHabbo().Id);
  1430. }
  1431.  
  1432. public Trade GetUserTrade(uint UserId)
  1433. {
  1434. lock (ActiveTrades)
  1435. {
  1436. foreach (Trade Trade in ActiveTrades)
  1437. {
  1438. if (Trade.ContainsUser(UserId))
  1439. {
  1440. return Trade;
  1441. }
  1442. }
  1443. }
  1444.  
  1445. return null;
  1446. }
  1447.  
  1448. public void TryStartTrade(RoomUser UserOne, RoomUser UserTwo)
  1449. {
  1450. if (UserOne == null || UserTwo == null || UserOne.IsBot || UserTwo.IsBot || UserOne.IsTrading || UserTwo.IsTrading || HasActiveTrade(UserOne) || HasActiveTrade(UserTwo))
  1451. {
  1452. return;
  1453. }
  1454.  
  1455. ActiveTrades.Add(new Trade(UserOne.GetClient().GetHabbo().Id, UserTwo.GetClient().GetHabbo().Id, RoomId));
  1456. }
  1457.  
  1458. public void TryStopTrade(uint UserId)
  1459. {
  1460. Trade Trade = GetUserTrade(UserId);
  1461.  
  1462. if (Trade == null)
  1463. {
  1464. return;
  1465. }
  1466.  
  1467. Trade.CloseTrade(UserId);
  1468. ActiveTrades.Remove(Trade);
  1469. }
  1470. #endregion
  1471.  
  1472. #region Furni handling and stacking
  1473. public bool SetFloorItem(GameClient Session, RoomItem Item, int newX, int newY, int newRot, bool newItem)
  1474. {
  1475. // Find affected tiles
  1476. Dictionary<int, AffectedTile> AffectedTiles = GetAffectedTiles(Item.GetBaseItem().Length, Item.GetBaseItem().Width, newX, newY, newRot);
  1477.  
  1478. // Verify tiles are valid
  1479. if (!ValidTile(newX, newY))
  1480. {
  1481. return false;
  1482. }
  1483.  
  1484. foreach (AffectedTile Tile in AffectedTiles.Values)
  1485. {
  1486. if (!ValidTile(Tile.X, Tile.Y))
  1487. {
  1488. return false;
  1489. }
  1490. }
  1491.  
  1492. // Start calculating new Z coordinate
  1493. Double newZ = Model.SqFloorHeight[newX, newY];
  1494.  
  1495. // Is the item trying to stack on itself!?
  1496. if (Item.Rot == newRot && Item.X == newX && Item.Y == newY && Item.Z != newZ)
  1497. {
  1498. return false;
  1499. }
  1500.  
  1501. // Make sure this tile is open and there are no users here
  1502. if (Model.SqState[newX, newY] != SquareState.OPEN)
  1503. {
  1504. return false;
  1505. }
  1506.  
  1507. foreach (AffectedTile Tile in AffectedTiles.Values)
  1508. {
  1509. if (Model.SqState[Tile.X, Tile.Y] != SquareState.OPEN)
  1510. {
  1511. return false;
  1512. }
  1513. }
  1514.  
  1515. // And that we have no users
  1516. if (!Item.GetBaseItem().IsSeat)
  1517. {
  1518. if (SquareHasUsers(newX, newY))
  1519. {
  1520. return false;
  1521. }
  1522.  
  1523. foreach (AffectedTile Tile in AffectedTiles.Values)
  1524. {
  1525. if (SquareHasUsers(Tile.X, Tile.Y))
  1526. {
  1527. return false;
  1528. }
  1529. }
  1530. }
  1531.  
  1532. // Find affected objects
  1533. List<RoomItem> ItemsOnTile = GetFurniObjects(newX, newY);
  1534. List<RoomItem> ItemsAffected = new List<RoomItem>();
  1535. List<RoomItem> ItemsComplete = new List<RoomItem>();
  1536.  
  1537. foreach (AffectedTile Tile in AffectedTiles.Values)
  1538. {
  1539. List<RoomItem> Temp = GetFurniObjects(Tile.X, Tile.Y);
  1540.  
  1541. if (Temp != null)
  1542. {
  1543. ItemsAffected.AddRange(Temp);
  1544. }
  1545. }
  1546.  
  1547. if (ItemsOnTile == null) ItemsOnTile = new List<RoomItem>();
  1548.  
  1549. ItemsComplete.AddRange(ItemsOnTile);
  1550. ItemsComplete.AddRange(ItemsAffected);
  1551.  
  1552. // Check for items in the stack that do not allow stacking on top of them
  1553. foreach (RoomItem I in ItemsComplete)
  1554. {
  1555. if (I.Id == Item.Id)
  1556. {
  1557. continue;
  1558. }
  1559.  
  1560. if (!I.GetBaseItem().Stackable)
  1561. {
  1562. return false;
  1563. }
  1564. }
  1565.  
  1566. // If this is a rotating action, maintain item at current height
  1567. if (Item.Rot != newRot && Item.X == newX && Item.Y == newY)
  1568. {
  1569. newZ = Item.Z;
  1570. }
  1571.  
  1572. // Are there any higher objects in the stack!?
  1573. foreach (RoomItem I in ItemsComplete)
  1574. {
  1575. if (I.Id == Item.Id)
  1576. {
  1577. continue; // cannot stack on self
  1578. }
  1579.  
  1580. if (I.TotalHeight > newZ)
  1581. {
  1582. newZ = I.TotalHeight;
  1583. }
  1584. }
  1585.  
  1586. // Verify the rotation is correct
  1587. if (newRot != 0 && newRot != 2 && newRot != 4 && newRot != 6 && newRot != 8)
  1588. {
  1589. newRot = 0;
  1590. }
  1591.  
  1592. Item.X = newX;
  1593. Item.Y = newY;
  1594. Item.Z = newZ;
  1595. Item.Rot = newRot;
  1596.  
  1597. Item.Interactor.OnPlace(Session, Item);
  1598.  
  1599. if (newItem)
  1600. {
  1601. using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
  1602. {
  1603. dbClient.AddParamWithValue("extra_data", Item.ExtraData);
  1604. dbClient.ExecuteQuery("INSERT INTO room_items (id,room_id,base_item,extra_data,x,y,z,rot,wall_pos) VALUES ('" + Item.Id + "','" + RoomId + "','" + Item.BaseItem + "',@extra_data,'" + Item.X + "','" + Item.Y + "','" + Item.Z + "','" + Item.Rot + "','')");
  1605. }
  1606.  
  1607. Items.Add(Item);
  1608.  
  1609. ServerMessage Message = new ServerMessage(93);
  1610. Item.Serialize(Message);
  1611. SendMessage(Message);
  1612. }
  1613. else
  1614. {
  1615. using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
  1616. {
  1617. dbClient.ExecuteQuery("UPDATE room_items SET x = '" + Item.X + "', y = '" + Item.Y + "', z = '" + Item.Z + "', rot = '" + Item.Rot + "', wall_pos = '' WHERE id = '" + Item.Id + "' LIMIT 1");
  1618. }
  1619.  
  1620. ServerMessage Message = new ServerMessage(95);
  1621. Item.Serialize(Message);
  1622. SendMessage(Message);
  1623. }
  1624.  
  1625. GenerateMaps();
  1626. UpdateUserStatusses();
  1627.  
  1628. return true;
  1629. }
  1630.  
  1631. public bool SetWallItem(GameClient Session, RoomItem Item)
  1632. {
  1633. Item.Interactor.OnPlace(Session, Item);
  1634.  
  1635. switch (Item.GetBaseItem().InteractionType.ToLower())
  1636. {
  1637. case "dimmer":
  1638.  
  1639. if (MoodlightData == null)
  1640. {
  1641. MoodlightData = new MoodlightData(Item.Id);
  1642. Item.ExtraData = MoodlightData.GenerateExtraData();
  1643. }
  1644.  
  1645. break;
  1646. }
  1647.  
  1648. using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
  1649. {
  1650. dbClient.AddParamWithValue("extra_data", Item.ExtraData);
  1651. dbClient.ExecuteQuery("INSERT INTO room_items (id,room_id,base_item,extra_data,x,y,z,rot,wall_pos) VALUES ('" + Item.Id + "','" + RoomId + "','" + Item.BaseItem + "',@extra_data,'0','0','0','0','" + Item.WallPos + "')");
  1652. }
  1653.  
  1654. Items.Add(Item);
  1655.  
  1656. ServerMessage Message = new ServerMessage(83);
  1657. Item.Serialize(Message);
  1658. SendMessage(Message);
  1659.  
  1660. return true;
  1661. }
  1662. #endregion
  1663.  
  1664. public void UpdateUserStatusses()
  1665. {
  1666. lock (UserList)
  1667. {
  1668. List<RoomUser>.Enumerator Users = UserList.GetEnumerator();
  1669.  
  1670. while (Users.MoveNext())
  1671. {
  1672. UpdateUserStatus(Users.Current);
  1673. }
  1674. }
  1675. }
  1676.  
  1677. public double SqAbsoluteHeight(int X, int Y)
  1678. {
  1679. List <RoomItem> ItemsOnSquare = GetFurniObjects(X, Y);
  1680. double HighestStack = 0;
  1681.  
  1682. bool deduct = false;
  1683. double deductable = 0.0;
  1684.  
  1685. if (ItemsOnSquare == null)
  1686. {
  1687. ItemsOnSquare = new List<RoomItem>();
  1688. }
  1689.  
  1690. if (ItemsOnSquare != null)
  1691. {
  1692. foreach (RoomItem Item in ItemsOnSquare)
  1693. {
  1694. if (Item.TotalHeight > HighestStack)
  1695. {
  1696. if (Item.GetBaseItem().IsSeat || Item.GetBaseItem().InteractionType.ToLower() == "bed")
  1697. {
  1698. deduct = true;
  1699. deductable = Item.GetBaseItem().Height;
  1700. }
  1701. else
  1702. {
  1703. deduct = false;
  1704. }
  1705.  
  1706. HighestStack = Item.TotalHeight;
  1707. }
  1708. }
  1709. }
  1710.  
  1711. double floorHeight = Model.SqFloorHeight[X, Y];
  1712. double stackHeight = HighestStack - Model.SqFloorHeight[X, Y];
  1713.  
  1714. if (deduct)
  1715. {
  1716. stackHeight -= deductable;
  1717. }
  1718.  
  1719. if (stackHeight < 0)
  1720. {
  1721. stackHeight = 0;
  1722. }
  1723.  
  1724. return (floorHeight + stackHeight);
  1725. }
  1726.  
  1727. public void UpdateUserStatus(RoomUser User)
  1728. {
  1729. if (HasThread.ContainsKey(User.HabboId))
  1730. {
  1731. HasThread[User.HabboId].Abort();
  1732. HasThread.Remove(User.HabboId);
  1733. }
  1734.  
  1735. if (BallThread.ContainsKey(User.HabboId))
  1736. {
  1737. BallThread[User.HabboId].Abort();
  1738. BallThread.Remove(User.HabboId);
  1739. }
  1740. if (User.Statusses.ContainsKey("lay") || User.Statusses.ContainsKey("sit"))
  1741. {
  1742. User.Statusses.Remove("lay");
  1743. User.Statusses.Remove("sit");
  1744. User.UpdateNeeded = true;
  1745. }
  1746.  
  1747. double newZ = SqAbsoluteHeight(User.X, User.Y);
  1748.  
  1749. if (newZ != User.Z)
  1750. {
  1751. User.Z = newZ;
  1752. User.UpdateNeeded = true;
  1753. }
  1754.  
  1755. if (Model.SqState[User.X, User.Y] == SquareState.SEAT)
  1756. {
  1757. if (!User.Statusses.ContainsKey("sit"))
  1758. {
  1759. User.Statusses.Add("sit", "1.0");
  1760. }
  1761.  
  1762. User.Z = Model.SqFloorHeight[User.X, User.Y];
  1763. User.RotHead = Model.SqSeatRot[User.X, User.Y];
  1764. User.RotBody = Model.SqSeatRot[User.X, User.Y];
  1765.  
  1766. User.UpdateNeeded = true;
  1767. }
  1768.  
  1769. List<RoomItem> ItemsOnSquare = GetFurniObjects(User.X, User.Y);
  1770.  
  1771. int BallX = 0;
  1772. int BallY = 0;
  1773.  
  1774. int Rot = User.RotBody;
  1775. if (Rot == 3)
  1776. {
  1777. BallX = User.X + 1;
  1778. BallY = User.Y + 1;
  1779. }
  1780. if (Rot == 4)
  1781. {
  1782. BallX = User.X;
  1783. BallY = User.Y + 1;
  1784. }
  1785. if (Rot == 1)
  1786. {
  1787. BallX = User.X + 1;
  1788. BallY = User.Y - 1;
  1789. }
  1790. if (Rot == 2)
  1791. {
  1792. BallX = User.X + 1;
  1793. BallY = User.Y;
  1794. }
  1795. if (Rot == 0)
  1796. {
  1797. BallX = User.X;
  1798. BallY = User.Y - 1;
  1799. }
  1800. if (Rot == 5)
  1801. {
  1802. BallX = User.X - 1;
  1803. BallY = User.Y + 1;
  1804. }
  1805. if (Rot == 6)
  1806. {
  1807. BallX = User.X - 1;
  1808. BallY = User.Y;
  1809. }
  1810. if (Rot == 7)
  1811. {
  1812. BallX = User.X - 1;
  1813. BallY = User.Y - 1;
  1814. }
  1815.  
  1816. List<RoomItem> Ball = new List<RoomItem>();
  1817. Ball = GetFurniObjects(BallX, BallY);
  1818.  
  1819. if (Ball == null)
  1820. Ball = new List<RoomItem>();
  1821.  
  1822. foreach (RoomItem Item in Ball)
  1823. {
  1824. if (Item.GetBaseItem().InteractionType.ToLower() == "ball")
  1825. {
  1826. Thread BallT = new Thread(delegate() { BallProcess(Item, User); });
  1827. BallT.Start();
  1828. BallThread.Add(User.HabboId, BallT);
  1829. }
  1830. }
  1831.  
  1832. if (ItemsOnSquare == null)
  1833. {
  1834. ItemsOnSquare = new List<RoomItem>();
  1835. if (HasWaterEffect.Contains(User.HabboId))
  1836. {
  1837. ServerMessage Message = new ServerMessage(485);
  1838. Message.AppendInt32(User.VirtualId);
  1839. Message.AppendInt32(0);
  1840. SendMessage(Message);
  1841. HasWaterEffect.Remove(User.HabboId);
  1842. User.UpdateNeeded = true;
  1843. }
  1844. }
  1845.  
  1846. foreach (RoomItem Item in ItemsOnSquare)
  1847. {
  1848. DataTable Data = null;
  1849.  
  1850. using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
  1851. {
  1852. Data = dbClient.ReadDataTable("SELECT * FROM wiredtrigger WHERE roomid = '" + RoomId + "'");
  1853. }
  1854.  
  1855. if (Data != null)
  1856. {
  1857. foreach (DataRow Row in Data.Rows)
  1858. {
  1859. if (Row["triggertype"].ToString() == "walkon")
  1860. {
  1861. if (int.Parse(Row["whattrigger"].ToString()) == Item.Id)
  1862. {
  1863. using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
  1864. {
  1865. if (dbClient.findsResult("select * from wiredaction where slotid = '" + Row["slotid"].ToString() + "'"))
  1866. {
  1867. string type = dbClient.ReadString("select typeaction from wiredaction where slotid = '" + Row["slotid"] + "'");
  1868. if (type == "status")
  1869. {
  1870. RoomItem ItemToChange = GetItem(uint.Parse(dbClient.ReadString("select itemid from wiredaction where slotid = '" + Row["slotid"] + "'")));
  1871. ItemToChange.ExtraData = dbClient.ReadString("select whataction from wiredaction where slotid = '" + Row["slotid"] + "'");
  1872. ItemToChange.UpdateState();
  1873. }
  1874. else if (type == "kick")
  1875. {
  1876. RemoveUserFromRoom(User.GetClient(), true, false);
  1877. }
  1878. }
  1879. }
  1880. }
  1881. }
  1882. }
  1883. }
  1884.  
  1885. if (Item.GetBaseItem().InteractionType.ToLower() != "water")
  1886. {
  1887. if (HasWaterEffect.Contains(User.HabboId))
  1888. {
  1889. ServerMessage Message = new ServerMessage(485);
  1890. Message.AppendInt32(User.VirtualId);
  1891. Message.AppendInt32(0);
  1892. SendMessage(Message);
  1893. User.UpdateNeeded = true;
  1894. HasWaterEffect.Remove(User.HabboId);
  1895. }
  1896. }
  1897.  
  1898. if (Item.GetBaseItem().InteractionType.ToLower() == "water")
  1899. {
  1900. if (!HasWaterEffect.Contains(User.HabboId))
  1901. {
  1902. int effectid;
  1903.  
  1904. if (Item.GetBaseItem().Name == "bw_water_1")
  1905. effectid = 30;
  1906. else effectid = 28;
  1907.  
  1908. ServerMessage Message = new ServerMessage(485);
  1909. Message.AppendInt32(User.VirtualId);
  1910. Message.AppendInt32(effectid);
  1911. SendMessage(Message);
  1912. User.UpdateNeeded = true;
  1913. HasWaterEffect.Add(User.HabboId);
  1914. }
  1915. }
  1916.  
  1917.  
  1918. if (Item.GetBaseItem().InteractionType.ToLower() == "roller")
  1919. {
  1920. ServerMessage Message = new ServerMessage(230);
  1921. User.MoveTo(Item.SquareInFront);
  1922. SendMessage(Message);
  1923. User.UpdateNeeded = true;
  1924.  
  1925. }
  1926.  
  1927.  
  1928. if (Item.GetBaseItem().IsSeat)
  1929. {
  1930. if (!User.Statusses.ContainsKey("sit"))
  1931. {
  1932. User.Statusses.Add("sit", Item.GetBaseItem().Height.ToString().Replace(',', '.'));
  1933. }
  1934.  
  1935. User.Z = Item.Z;
  1936. User.RotHead = Item.Rot;
  1937. User.RotBody = Item.Rot;
  1938.  
  1939. User.UpdateNeeded = true;
  1940. }
  1941.  
  1942. if (Item.GetBaseItem().InteractionType.ToLower() == "bed")
  1943. {
  1944. if (!User.Statusses.ContainsKey("lay"))
  1945. {
  1946. User.Statusses.Add("lay", Item.GetBaseItem().Height.ToString().Replace(',', '.') + " null");
  1947. }
  1948.  
  1949. User.Z = Item.Z;
  1950. User.RotHead = Item.Rot;
  1951. User.RotBody = Item.Rot;
  1952.  
  1953. User.UpdateNeeded = true;
  1954. }
  1955. }
  1956. }
  1957.  
  1958. public void BallProcess(RoomItem ItemNow, RoomUser UserNow)
  1959. {
  1960. Thread.Sleep(500);
  1961. int NewX = 0;
  1962. int NewY = 0;
  1963. int Rot = UserNow.RotBody;
  1964. if (Rot == 3)
  1965. {
  1966. NewX = UserNow.X + 2;
  1967. NewY = UserNow.Y + 2;
  1968. }
  1969. if (Rot == 4)
  1970. {
  1971. NewX = UserNow.X;
  1972. NewY = UserNow.Y + 2;
  1973. }
  1974. if (Rot == 1)
  1975. {
  1976. NewX = UserNow.X + 2;
  1977. NewY = UserNow.Y - 2;
  1978. }
  1979. if (Rot == 2)
  1980. {
  1981. NewX = UserNow.X + 2;
  1982. NewY = UserNow.Y;
  1983. }
  1984. if (Rot == 0)
  1985. {
  1986. NewX = UserNow.X;
  1987. NewY = UserNow.Y - 2;
  1988. }
  1989. if (Rot == 5)
  1990. {
  1991. NewX = UserNow.X - 2;
  1992. NewY = UserNow.Y + 2;
  1993. }
  1994. if (Rot == 6)
  1995. {
  1996. NewX = UserNow.X - 2;
  1997. NewY = UserNow.Y;
  1998. }
  1999. if (Rot == 7)
  2000. {
  2001. NewX = UserNow.X - 2;
  2002. NewY = UserNow.Y - 2;
  2003. }
  2004. SetFloorItem(UserNow.GetClient(), ItemNow, NewX, NewY, ItemNow.Rot, false);
  2005. }
  2006.  
  2007. public bool ValidTile(int X, int Y)
  2008. {
  2009. if (X < 0 || Y < 0 || X >= Model.MapSizeX || Y >= Model.MapSizeY)
  2010. {
  2011. return false;
  2012. }
  2013.  
  2014. return true;
  2015. }
  2016.  
  2017. public void TurnHeads(int X, int Y, uint SenderId)
  2018. {
  2019. lock (UserList)
  2020. {
  2021. foreach (RoomUser User in UserList)
  2022. {
  2023. if (User.HabboId == SenderId)
  2024. {
  2025. continue;
  2026. }
  2027.  
  2028. User.SetRot(Rotation.Calculate(User.X, User.Y, X, Y), true);
  2029. }
  2030. }
  2031. }
  2032.  
  2033. public List<RoomItem> GetFurniObjects(int X, int Y)
  2034. {
  2035. List<RoomItem> Results = new List<RoomItem>();
  2036.  
  2037. foreach (RoomItem Item in FloorItems)
  2038. {
  2039. if (Item.X == X && Item.Y == Y)
  2040. {
  2041. Results.Add(Item);
  2042. }
  2043.  
  2044. Dictionary<int, AffectedTile> PointList = GetAffectedTiles(Item.GetBaseItem().Length, Item.GetBaseItem().Width, Item.X, Item.Y, Item.Rot);
  2045.  
  2046. foreach (AffectedTile Tile in PointList.Values)
  2047. {
  2048. if (Tile.X == X && Tile.Y == Y)
  2049. {
  2050. Results.Add(Item);
  2051. }
  2052. }
  2053. }
  2054.  
  2055. if (Results.Count > 0)
  2056. {
  2057. return Results;
  2058. }
  2059.  
  2060. return null;
  2061. }
  2062.  
  2063. public RoomItem FindItem(uint Id)
  2064. {
  2065. lock (Items)
  2066. {
  2067. foreach (RoomItem Item in Items)
  2068. {
  2069. if (Item.Id == Id)
  2070. {
  2071. return Item;
  2072. }
  2073. }
  2074. }
  2075.  
  2076. return null;
  2077. }
  2078.  
  2079. public Dictionary<int, AffectedTile> GetAffectedTiles(int Length, int Width, int PosX, int PosY, int Rotation)
  2080. {
  2081. int x = 0;
  2082.  
  2083. Dictionary<int, AffectedTile> PointList = new Dictionary<int, AffectedTile>();
  2084.  
  2085. if (Length > 1)
  2086. {
  2087. if (Rotation == 0 || Rotation == 4)
  2088. {
  2089. for (int i = 1; i < Length; i++)
  2090. {
  2091. PointList.Add(x++, new AffectedTile(PosX, PosY + i, i));
  2092.  
  2093. for (int j = 1; j < Width; j++)
  2094. {
  2095. PointList.Add(x++, new AffectedTile(PosX + j, PosY + i, (i < j) ? j : i));
  2096. }
  2097. }
  2098. }
  2099. else if (Rotation == 2 || Rotation == 6)
  2100. {
  2101. for (int i = 1; i < Length; i++)
  2102. {
  2103. PointList.Add(x++, new AffectedTile(PosX + i, PosY, i));
  2104.  
  2105. for (int j = 1; j < Width; j++)
  2106. {
  2107. PointList.Add(x++, new AffectedTile(PosX + i, PosY + j, (i < j) ? j : i));
  2108. }
  2109. }
  2110. }
  2111. }
  2112.  
  2113. if (Width > 1)
  2114. {
  2115. if (Rotation == 0 || Rotation == 4)
  2116. {
  2117. for (int i = 1; i < Width; i++)
  2118. {
  2119. PointList.Add(x++, new AffectedTile(PosX + i, PosY, i));
  2120.  
  2121. for (int j = 1; j < Length; j++)
  2122. {
  2123. PointList.Add(x++, new AffectedTile(PosX + i, PosY + j, (i < j) ? j : i));
  2124. }
  2125. }
  2126. }
  2127. else if (Rotation == 2 || Rotation == 6)
  2128. {
  2129. for (int i = 1; i < Width; i++)
  2130. {
  2131. PointList.Add(x++, new AffectedTile(PosX, PosY + i, i));
  2132.  
  2133. for (int j = 1; j < Length; j++)
  2134. {
  2135. PointList.Add(x++, new AffectedTile(PosX + j, PosY + i, (i < j) ? j : i));
  2136. }
  2137. }
  2138. }
  2139. }
  2140.  
  2141. return PointList;
  2142. }
  2143.  
  2144. public bool SquareHasUsers(int X, int Y, bool LastStep)
  2145. {
  2146. if (AllowWalkthrough && !LastStep)
  2147. {
  2148. return false;
  2149. }
  2150.  
  2151. return SquareHasUsers(X, Y);
  2152. }
  2153.  
  2154. public bool SquareHasUsers(int X, int Y)
  2155. {
  2156. Coord Coord = BedMatrix[X, Y];
  2157. return UserMatrix[Coord.x, Coord.y];
  2158. }
  2159.  
  2160. //This function is based on the one from "Holograph Emulator"
  2161. public string WallPositionCheck(string wallPosition)
  2162. {
  2163. //:w=3,2 l=9,63 l
  2164. try
  2165. {
  2166. if (wallPosition.Contains(Convert.ToChar(13)))
  2167. { return null; }
  2168. if (wallPosition.Contains(Convert.ToChar(9)))
  2169. { return null; }
  2170.  
  2171. string[] posD = wallPosition.Split(' ');
  2172. if (posD[2] != "l" && posD[2] != "r")
  2173. return null;
  2174.  
  2175. string[] widD = posD[0].Substring(3).Split(',');
  2176. int widthX = int.Parse(widD[0]);
  2177. int widthY = int.Parse(widD[1]);
  2178. if (widthX < 0 || widthY < 0 || widthX > 200 || widthY > 200)
  2179. return null;
  2180.  
  2181. string[] lenD = posD[1].Substring(2).Split(',');
  2182. int lengthX = int.Parse(lenD[0]);
  2183. int lengthY = int.Parse(lenD[1]);
  2184. if (lengthX < 0 || lengthY < 0 || lengthX > 200 || lengthY > 200)
  2185. return null;
  2186.  
  2187. return ":w=" + widthX + "," + widthY + " " + "l=" + lengthX + "," + lengthY + " " + posD[2];
  2188. }
  2189. catch
  2190. {
  2191. return null;
  2192. }
  2193. }
  2194.  
  2195. public bool TilesTouching(int X1, int Y1, int X2, int Y2)
  2196. {
  2197. if (!(Math.Abs(X1 - X2) > 1 || Math.Abs(Y1 - Y2) > 1)) return true;
  2198. if (X1 == X2 && Y1 == Y2) return true;
  2199. return false;
  2200. }
  2201.  
  2202. public int TileDistance(int X1, int Y1, int X2, int Y2)
  2203. {
  2204. return Math.Abs(X1 - X2) + Math.Abs(Y1 - Y2);
  2205. }
  2206. }
  2207.  
  2208. public class AffectedTile
  2209. {
  2210. int mX;
  2211. int mY;
  2212. int mI;
  2213.  
  2214. public AffectedTile(int x, int y, int i)
  2215. {
  2216. mX = x;
  2217. mY = y;
  2218. mI = i;
  2219. }
  2220.  
  2221. public int X
  2222. {
  2223. get
  2224. {
  2225. return mX;
  2226. }
  2227. }
  2228.  
  2229. public int Y
  2230. {
  2231. get
  2232. {
  2233. return mY;
  2234. }
  2235. }
  2236.  
  2237. public int I
  2238. {
  2239. get
  2240. {
  2241. return mI;
  2242. }
  2243. }
  2244. }
  2245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement