Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Oxide.Core;
  5. using Oxide.Core.Plugins;
  6. using Rust;
  7. using UnityEngine;
  8.  
  9. namespace Oxide.Plugins
  10. {
  11. // TODO LIST
  12. // Helicopter networking/targeting testing
  13.  
  14. [Info("Deathmatch", "Kappasaurus", "1.0.0")]
  15.  
  16. class Deathmatch : RustPlugin
  17. {
  18. #region Data and Classes
  19.  
  20. private static Deathmatch Instance;
  21. private List<Spawn> Spawns = new List<Spawn>();
  22.  
  23. private class Location
  24. {
  25. public float X, Y, Z;
  26.  
  27. public Location()
  28. {
  29. }
  30.  
  31. public Location(float x, float y, float z)
  32. {
  33. X = x;
  34. Y = y;
  35. Z = z;
  36. }
  37.  
  38. public static Location FromVector3(Vector3 vector) => new Location(vector.x, vector.y, vector.z);
  39.  
  40. public Vector3 ToVector3() => new Vector3(X, Y, Z);
  41. }
  42.  
  43. private class Spawn
  44. {
  45. public int Number;
  46. public Location Location;
  47.  
  48. public Spawn()
  49. {
  50. }
  51.  
  52. public Spawn(int number, Location location)
  53. {
  54. Number = number;
  55. Location = location;
  56. }
  57. }
  58.  
  59. public void SaveData() => Interface.Oxide.DataFileSystem.WriteObject(Title, Spawns);
  60. public void ReadData() => Spawns = Interface.Oxide.DataFileSystem.ReadObject<List<Spawn>>(Title);
  61.  
  62. #endregion
  63.  
  64. #region Configuration
  65.  
  66. private struct Configuration
  67. {
  68. public static string KitName = "event";
  69. public static int KillStreakMin = 5;
  70. public static int MaxPlayers = 30;
  71. public static bool ResetSpawnsWithNewMap = true;
  72. }
  73.  
  74. private new void LoadConfig()
  75. {
  76. GetConfig(ref Configuration.KitName, "Event kit");
  77. GetConfig(ref Configuration.KillStreakMin, "Minimum kills for a streak");
  78. GetConfig(ref Configuration.MaxPlayers, "Maximum players at once");
  79. GetConfig(ref Configuration.ResetSpawnsWithNewMap, "Reset spawns with new map");
  80.  
  81. SaveConfig();
  82. }
  83.  
  84. protected override void LoadDefaultConfig() => PrintWarning("Generating new config file...");
  85.  
  86. #endregion
  87.  
  88. #region Chat Commands
  89.  
  90. [ChatCommand("event")]
  91. private void EventCommand(BasePlayer player, string command, string[] args)
  92. {
  93. if (Spawns.Count == 0)
  94. {
  95. Message(player, "No Spawns");
  96. return;
  97. }
  98.  
  99. if (args.Length == 0)
  100. {
  101. Message(player, "No Argument");
  102. return;
  103. }
  104.  
  105. switch (args[0].ToLower())
  106. {
  107. case "join":
  108.  
  109. if (players.Count >= BasePlayer.activePlayerList.Count && !HasPermission(player))
  110. {
  111. Message(player, "Arena Full");
  112. return;
  113. }
  114.  
  115. if (usageTimes.ContainsKey(player))
  116. {
  117. if (DateTime.UtcNow - usageTimes[player] > new TimeSpan(0, 0, 5))
  118. {
  119. Message(player, "Cooldown");
  120. return;
  121. }
  122. }
  123.  
  124. if (players.Contains(player))
  125. {
  126. Message(player, "Already Joined");
  127. return;
  128. }
  129.  
  130. if (!usageTimes.ContainsKey(player))
  131. {
  132. usageTimes.Add(player, DateTime.UtcNow);
  133. }
  134.  
  135. Add(player);
  136.  
  137. break;
  138.  
  139. case "leave":
  140.  
  141. if (!players.Contains(player))
  142. {
  143. Message(player, "Hasn't Joined");
  144. return;
  145. }
  146.  
  147. Remove(player);
  148.  
  149. break;
  150.  
  151. default:
  152.  
  153. Message(player, "Unknown Argument");
  154.  
  155. break;
  156. }
  157. }
  158.  
  159. [ChatCommand("spawn")]
  160. private void SpawnCommand(BasePlayer player, string command, string[] args)
  161. {
  162. if (!HasPermission(player))
  163. {
  164. Message(player, "No Permission");
  165. return;
  166. }
  167.  
  168. if (args.Length == 0)
  169. {
  170. Message(player, "No Argument");
  171. return;
  172. }
  173.  
  174. switch (args[0].ToLower())
  175. {
  176. case "add":
  177. {
  178. PrintToChat(player, lang.GetMessage("Spawn Added", this, player.UserIDString), Spawns.Count + 1);
  179. var spawn = new Spawn(Spawns.Count + 1, Location.FromVector3(player.transform.position));
  180. Spawns.Add(spawn);
  181. SaveData();
  182. PrintWarning("Drawing sphere to accomodate for changes to spawns.");
  183. var spawns = Spawns.Select(position => position.Location.ToVector3()).ToList();
  184. sphere.Remove();
  185. DrawSphere(spawns);
  186. }
  187. break;
  188.  
  189. case "remove":
  190. {
  191. if (args.Length != 2)
  192. {
  193. Message(player, "No Argument");
  194. }
  195.  
  196. var spawn = Spawns.FirstOrDefault(x => x.Number.ToString() == args[1]);
  197.  
  198. if (spawn == null)
  199. {
  200. Message(player, "Spawn Not Found");
  201. return;
  202. }
  203.  
  204. PrintToChat(player, lang.GetMessage("Spawn Removed", this, player.UserIDString), spawn.Number);
  205. Spawns.Remove(spawn);
  206. SaveData();
  207. PrintWarning("Drawing sphere to accomodate for changes to spawns.");
  208. var spawns = Spawns.Select(position => position.Location.ToVector3()).ToList();
  209. sphere.Remove();
  210. DrawSphere(spawns);
  211. }
  212. break;
  213.  
  214. case "reset":
  215. {
  216. if (Spawns.Count == 0)
  217. {
  218. Message(player, "No Spawns Saved");
  219. return;
  220. }
  221.  
  222. Message(player, "Spawns Reset");
  223. Spawns.Clear();
  224. SaveData();
  225. sphere.Remove();
  226. }
  227. break;
  228.  
  229. case "list":
  230. {
  231. if (Spawns.Count == 0)
  232. {
  233. Message(player, "No Spawns Saved");
  234. return;
  235. }
  236.  
  237. PrintToChat(player, lang.GetMessage("Spawn List", this, player.UserIDString), Spawns.Select(x => x.Number).ToSentence());
  238. }
  239. break;
  240.  
  241. default:
  242. {
  243. Message(player, "Unknown Argument");
  244. }
  245. break;
  246. }
  247. }
  248.  
  249. #endregion
  250.  
  251. #region Item Saving
  252.  
  253. private class Inventory
  254. {
  255. public List<InventoryItem> ContainerBelt = new List<InventoryItem>();
  256. public List<InventoryItem> ContainerWear = new List<InventoryItem>();
  257. public List<InventoryItem> ContainerMain = new List<InventoryItem>();
  258. }
  259.  
  260. private class InventoryItem
  261. {
  262. public string ItemName;
  263. public int Amount;
  264. public ulong SkinID;
  265. public bool Weapon;
  266. public List<string> Attachments;
  267. }
  268.  
  269. private Inventory GetInventory(BasePlayer player)
  270. {
  271. var inventory = new Inventory();
  272.  
  273. foreach (var item in player.inventory.containerBelt.itemList)
  274. {
  275. inventory.ContainerBelt.Add(ProcessItem(item));
  276. }
  277.  
  278. foreach (var item in player.inventory.containerWear.itemList)
  279. {
  280. inventory.ContainerWear.Add(ProcessItem(item));
  281. }
  282.  
  283. foreach (var item in player.inventory.containerMain.itemList)
  284. {
  285. inventory.ContainerMain.Add(ProcessItem(item));
  286. }
  287.  
  288. return inventory;
  289. }
  290.  
  291. private void GiveInventory(BasePlayer player)
  292. {
  293. player.inventory.Strip();
  294.  
  295. foreach (var item in inventories[player].ContainerBelt)
  296. {
  297. var giveItem = item.Weapon ? BuildWeapon(item.ItemName, item.SkinID, item.Attachments) : BuildItem(item.ItemName, item.Amount, item.SkinID);
  298. player.inventory.GiveItem(giveItem, player.inventory.containerMain);
  299. }
  300.  
  301. foreach (var item in inventories[player].ContainerWear)
  302. {
  303. var giveItem = item.Weapon ? BuildWeapon(item.ItemName, item.SkinID, item.Attachments) : BuildItem(item.ItemName, item.Amount, item.SkinID);
  304. player.inventory.GiveItem(giveItem, player.inventory.containerWear);
  305. }
  306.  
  307. foreach (var item in inventories[player].ContainerMain)
  308. {
  309. var giveItem = item.Weapon ? BuildWeapon(item.ItemName, item.SkinID, item.Attachments) : BuildItem(item.ItemName, item.Amount, item.SkinID);
  310. player.inventory.GiveItem(giveItem, player.inventory.containerMain);
  311. }
  312.  
  313. inventories.Remove(player);
  314. }
  315.  
  316. private Item BuildWeapon(string name, ulong skin, List<string> attachments)
  317. {
  318. var item = ItemManager.CreateByName(name, 1, skin);
  319. var weapon = item.GetHeldEntity() as BaseProjectile;
  320.  
  321. if (weapon != null)
  322. {
  323. weapon.primaryMagazine.contents = weapon.primaryMagazine.capacity;
  324. }
  325.  
  326. if (attachments == null)
  327. {
  328. return item;
  329. }
  330.  
  331. foreach (var attachment in attachments)
  332. {
  333. BuildItem(attachment, 1).MoveToContainer(item.contents);
  334. }
  335.  
  336. return item;
  337. }
  338.  
  339. private Item BuildItem(string name, int amount, ulong skin = 0)
  340. {
  341. if (amount < 1)
  342. {
  343. amount = 1;
  344. }
  345.  
  346. var item = ItemManager.CreateByName(name, amount, skin);
  347.  
  348. return item;
  349. }
  350.  
  351. private InventoryItem ProcessItem(Item item)
  352. {
  353. var inventoryItem = new InventoryItem
  354. {
  355. ItemName = item.info.shortname,
  356. Amount = item.amount,
  357. SkinID = item.skin,
  358. Attachments = new List<string>(),
  359. };
  360.  
  361. if (item.info.category.ToString() != "Weapon")
  362. {
  363. return inventoryItem;
  364. }
  365.  
  366. var weapon = item.GetHeldEntity() as BaseProjectile;
  367.  
  368. if (weapon == null || weapon.primaryMagazine == null)
  369. {
  370. return inventoryItem;
  371. }
  372.  
  373. inventoryItem.Weapon = true;
  374.  
  375. if (weapon.primaryMagazine.ammoType.shortname == null)
  376. {
  377. return inventoryItem;
  378. }
  379.  
  380. if (item.contents == null)
  381. {
  382. return inventoryItem;
  383. }
  384.  
  385. foreach (var attachment in item.contents.itemList)
  386. {
  387. if (attachment.info.itemid != 0)
  388. {
  389. inventoryItem.Attachments.Add(attachment.info.shortname);
  390. }
  391. }
  392.  
  393. return inventoryItem;
  394. }
  395.  
  396. #endregion
  397.  
  398. #region Oxide Hooks
  399.  
  400. private void Init()
  401. {
  402. PrintWarning("Warning, without Kits this plugin is rendered useless and may cause excessive errors.");
  403.  
  404. // DATA
  405. Instance = this;
  406. ReadData();
  407.  
  408. // CONFIGURATION
  409. LoadConfig();
  410.  
  411. // PERMISSION
  412. permission.RegisterPermission("deathmatch.admin", this);
  413.  
  414. // MESSAGE
  415. if (Spawns.Count == 0)
  416. {
  417. return;
  418. }
  419.  
  420. PrintToChat(lang.GetMessage("Event Message", this));
  421.  
  422. timer.Repeat(600f, 0, () =>
  423. {
  424. foreach (var player in BasePlayer.activePlayerList)
  425. {
  426. if (!players.Contains(player))
  427. {
  428. PrintToChat(lang.GetMessage("Event Message", this));
  429. }
  430. }
  431. });
  432.  
  433. // SPHERES
  434. timer.Once(30f, () =>
  435. {
  436. PrintWarning("Drawing sphere.");
  437. var spawns = Spawns.Select(position => position.Location.ToVector3()).ToList();
  438. DrawSphere(spawns);
  439. });
  440. }
  441.  
  442. private void OnNewSave()
  443. {
  444. if (!Configuration.ResetSpawnsWithNewMap)
  445. {
  446. return;
  447. }
  448.  
  449. PrintWarning("Resetting spawns due to new map.");
  450. Spawns.Clear();
  451. SaveData();
  452. }
  453.  
  454. private void OnPlayerDisconnected(BasePlayer player)
  455. {
  456. if (players.Contains(player))
  457. {
  458. Remove(player);
  459. }
  460. }
  461.  
  462. private void OnPlayerInit(BasePlayer player)
  463. {
  464. Message(player, "Event Message");
  465. }
  466.  
  467. private object OnPlayerDie(BasePlayer player, HitInfo hitInfo)
  468. {
  469. try
  470. {
  471. if (!players.Contains(player))
  472. {
  473. return null;
  474. }
  475.  
  476. var attacker = hitInfo.InitiatorPlayer;
  477.  
  478. if (attacker == player)
  479. {
  480. Respawn(player);
  481. return false;
  482. }
  483.  
  484.  
  485. if (attacker != null)
  486. {
  487. PrintToChat(player, lang.GetMessage("Victim", this, player.UserIDString), attacker.displayName);
  488. PrintToChat(attacker, lang.GetMessage("Attacker", this, attacker.UserIDString), player.displayName);
  489.  
  490. if (kills.ContainsKey(attacker))
  491. {
  492. kills[attacker]++;
  493.  
  494. }
  495. else
  496. {
  497. kills.Add(attacker, 1);
  498. }
  499.  
  500. if (kills.Max().Value == kills[attacker])
  501. {
  502. foreach (var target in players)
  503. {
  504. PrintToChat(target, lang.GetMessage("Most Kills", this, target.UserIDString), attacker.displayName, kills[attacker]);
  505. }
  506. }
  507.  
  508. if (killStreaks.ContainsKey(attacker))
  509. {
  510. killStreaks[attacker]++;
  511.  
  512. if (killStreaks[attacker] >= Configuration.KillStreakMin)
  513. {
  514. foreach (var target in players)
  515. {
  516. PrintToChat(target, lang.GetMessage("Kill Streak", this, target.UserIDString), attacker.displayName, killStreaks[attacker]);
  517. }
  518. }
  519. }
  520. else
  521. {
  522. killStreaks.Add(attacker, 1);
  523. }
  524.  
  525. if (killStreaks.ContainsKey(player))
  526. {
  527. killStreaks.Remove(player);
  528. }
  529. }
  530. }
  531. catch
  532. {
  533. }
  534.  
  535. Respawn(player);
  536. return false;
  537. }
  538.  
  539. private object CanBeWounded(BasePlayer player, HitInfo info)
  540. {
  541. if (players.Contains(player))
  542. {
  543. return false;
  544. }
  545.  
  546. return null;
  547. }
  548.  
  549. private void OnRunPlayerMetabolism(PlayerMetabolism metabolism, BaseCombatEntity entity)
  550. {
  551. var player = entity.ToPlayer();
  552.  
  553. if (player == null)
  554. {
  555. return;
  556. }
  557.  
  558. if (!players.Contains(player))
  559. {
  560. return;
  561. }
  562.  
  563. player.metabolism.calories.value = 500f;
  564. player.metabolism.hydration.value = 250f;
  565. player.metabolism.comfort.value = player.health < 90f ? 100f : 0f;
  566. player.metabolism.temperature.value = 30f;
  567. player.metabolism.bleeding.value = 0f;
  568. }
  569.  
  570. private void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
  571. {
  572. if (!players.Contains(entity?.ToPlayer()))
  573. {
  574. return;
  575. }
  576.  
  577. if (info.damageTypes.GetMajorityDamageType() != DamageType.Fall)
  578. {
  579. return;
  580. }
  581.  
  582. info.damageTypes = new DamageTypeList();
  583. }
  584.  
  585. private void OnLoseCondition(Item item, ref float amount)
  586. {
  587. var player = item.GetOwnerPlayer();
  588. var info = item?.info;
  589.  
  590. if (!players.Contains(player))
  591. {
  592. return;
  593. }
  594.  
  595. if (info == null)
  596. {
  597. return;
  598. }
  599.  
  600. if (item.hasCondition)
  601. {
  602. item.RepairCondition(amount);
  603. }
  604.  
  605. }
  606.  
  607. /*private object CanMoveItem(Item item, PlayerInventory playerLoot, uint targetContainer, int targetSlot, int numItems)
  608. {
  609. var player = item.GetOwnerPlayer();
  610.  
  611. if (player == null)
  612. {
  613. return null;
  614. }
  615.  
  616. if (players.Contains(player))
  617. {
  618. return false;
  619. }
  620.  
  621. return null;
  622. }*/
  623.  
  624. private object OnItemAction(Item item, string action)
  625. {
  626. var player = item.GetOwnerPlayer();
  627.  
  628. if (player == null)
  629. {
  630. return null;
  631. }
  632.  
  633. if (players.Contains(player))
  634. {
  635. return false;
  636. }
  637.  
  638. return null;
  639. }
  640.  
  641. private void Unload(BasePlayer player)
  642. {
  643. foreach (var target in players)
  644. {
  645. Message(target, "Emergency Ending");
  646. Remove(target, true);
  647. }
  648.  
  649. sphere?.Remove();
  650. }
  651.  
  652. private object OnHelicopterTarget(HelicopterTurret turret, BaseCombatEntity entity)
  653. {
  654. var player = entity.ToPlayer();
  655.  
  656. if (player == null)
  657. {
  658. return null;
  659. }
  660.  
  661. if (players.Contains(player))
  662. {
  663. return false;
  664. }
  665.  
  666. return null;
  667. }
  668.  
  669. /*private object CanNetworkTo(BaseNetworkable entity, BaseNetworkable target)
  670. {
  671. var player = entity as BasePlayer ?? (entity as HeldEntity)?.GetOwnerPlayer();
  672. var targetPlayer = target as BasePlayer ?? (target as HeldEntity)?.GetOwnerPlayer();
  673.  
  674. if ((players.Contains(player) || players.Contains(targetPlayer)) && (entity is BaseHelicopter || target is BaseHelicopter))
  675. {
  676. return false;
  677. }
  678.  
  679. if (player == null || targetPlayer == null || targetPlayer == player)
  680. {
  681. return null;
  682. }
  683.  
  684. if (players.Contains(player) && !players.Contains(targetPlayer) || players.Contains(targetPlayer) && !players.Contains(player))
  685. {
  686. return false;
  687. }
  688.  
  689. return null;
  690. }*/
  691.  
  692. #endregion
  693.  
  694. #region Metabolism
  695.  
  696. class Metabolism
  697. {
  698. public float Calories, Hydration, Bleed, Health, Wetness, RadiationLevel, RadiationPoison;
  699. }
  700.  
  701. private Metabolism GetMetabolism(BasePlayer player)
  702. {
  703. var newMetabolism = new Metabolism
  704. {
  705. Calories = player.metabolism.calories.value,
  706. Hydration = player.metabolism.hydration.value,
  707. Bleed = player.metabolism.bleeding.value,
  708. Health = player.health,
  709. Wetness = player.metabolism.wetness.value,
  710. RadiationLevel = player.metabolism.radiation_level.value,
  711. RadiationPoison = player.metabolism.radiation_level.value
  712. };
  713.  
  714. return newMetabolism;
  715.  
  716. }
  717.  
  718. private void RestoreMetabolism(BasePlayer player)
  719. {
  720. player.metabolism.calories.value = metabolism[player].Calories;
  721. player.metabolism.hydration.value = metabolism[player].Hydration;
  722. player.metabolism.bleeding.value = metabolism[player].Bleed;
  723. player.metabolism.wetness.value = metabolism[player].Wetness;
  724. player.metabolism.radiation_level.value = metabolism[player].RadiationLevel;
  725. player.metabolism.radiation_poison.value = metabolism[player].RadiationPoison;
  726. player.health = metabolism[player].Health;
  727. }
  728.  
  729. #endregion
  730.  
  731. #region Variables
  732.  
  733. [PluginReference]
  734. private Plugin Kits;
  735. private List<BasePlayer> players = new List<BasePlayer>();
  736. private Dictionary<BasePlayer, Vector3> locations = new Dictionary<BasePlayer, Vector3>();
  737. private Dictionary<BasePlayer, Inventory> inventories = new Dictionary<BasePlayer, Inventory>();
  738. private Dictionary<BasePlayer, DateTime> usageTimes = new Dictionary<BasePlayer, DateTime>();
  739. private Dictionary<BasePlayer, Metabolism> metabolism = new Dictionary<BasePlayer, Metabolism>();
  740. private Dictionary<BasePlayer, int> killStreaks = new Dictionary<BasePlayer, int>();
  741. private Dictionary<BasePlayer, int> kills = new Dictionary<BasePlayer, int>();
  742. private List<BasePlayer> outOfBoundsPlayers = new List<BasePlayer>();
  743.  
  744. // SPHERES
  745. private static int spheres;
  746. private static float radius;
  747. private float distance;
  748. private Sphere sphere;
  749.  
  750. #endregion
  751.  
  752. #region Helpers
  753.  
  754. // CONFIGURATION
  755. private void GetConfig<T>(ref T variable, params string[] path)
  756. {
  757. if (path.Length == 0)
  758. {
  759. return;
  760. }
  761.  
  762. if (Config.Get(path) == null)
  763. {
  764. Config.Set(path.Concat(new object[] { variable }).ToArray());
  765. PrintWarning($"Added field to config: {string.Join("/", path)}");
  766. }
  767.  
  768. variable = (T)Convert.ChangeType(Config.Get(path), typeof(T));
  769. }
  770.  
  771. // MESSAGES
  772. protected override void LoadDefaultMessages()
  773. {
  774. lang.RegisterMessages(new Dictionary<string, string>
  775. {
  776. ["No Permission"] = "Error, you lack permission.",
  777. ["No Argument"] = "Error, no argument.",
  778. ["Unknown Argument"] = "Error, unknown argument.",
  779. ["Not Active"] = "Error, event not active.",
  780. ["Already Joined"] = "Error, you're already participating in the event.",
  781. ["Hasn't Joined"] = "Error, you're not currently participating in the event.",
  782. ["No Spawns"] = "Error, no spawns are saved, try contacting your administrator.",
  783. ["Spawn Not Found"] = "Error, spawn doesn't exist.",
  784. ["Spawn Added"] = "Spawn sucessfully added with name <color=#ADD8E6>{0}</color>.",
  785. ["Spawn Removed"] = "Spawn <color=#ADD8E6>{0}</color> sucessfully removed.",
  786. ["Spawns Reset"] = "Sucessfully reset all spawns.",
  787. ["Spawn List"] = "The following spawn(s) are currently saved: <color=#ADD8E6>{0}</color>",
  788. ["No Spawns Saved"] = "Error, no spawns saved.",
  789. ["Joined"] = "Sucessfully joined the game, use \"<color=#ADD8E6>/event leave</color>\" to exit.",
  790. ["Left"] = "Sucessfully left the game.",
  791. ["All Joined"] = "<color=#ADD8E6>{0}</color> joined the game for a total of <color=#ADD8E6>{1}</color> player(s).",
  792. ["All Left"] = "<color=#ADD8E6>{0}</color> left the game. <color=#ADD8E6>{1}</color> player(s) left.",
  793. ["Cooldown"] = "Error, you're doing that too fast. Try again in 5 minutes.",
  794. ["Emergency Ending"] = "Game ending due to technical difficulties.",
  795. ["Attacker"] = "You killed <color=#ADD8E6>{0}</color>.",
  796. ["Victim"] = "<color=#ADD8E6>{0}</color> killed you.",
  797. ["Kill Streak"] = "<color=#ADD8E6>{0}</color> is on a kill streak with <color=#ADD8E6>{1}</color> kills!",
  798. ["Kit Block"] = "Error, you may not redeem kits in the arena.",
  799. ["Event Message"] = "The deathmatch event is currently active, use \"<color=#ADD8E6>/event join</color>\" to participate!",
  800. ["Arena Full"] = "Error, arena has reached the player cap, try again later.",
  801. ["Teleport Block"] = "Error, you may not use teleportation, if you'd like to leave simply type \"<color=#ADD8E6>/event leave</color>\" to exit.",
  802. ["Most Kills"] = "<color=#ADD8E6>{0}</color> is in the lead with <color=#ADD8E6>{1}</color> kills!",
  803. ["Out of Bounds"] = "Error, you're out of bounds. If you don't enter the arena soon you'll die!"
  804. }, this);
  805. }
  806.  
  807. private void Message(BasePlayer player, string messageKey)
  808. {
  809. PrintToChat(player, lang.GetMessage(messageKey, this, player.UserIDString));
  810. }
  811.  
  812. // TELEPORTATION
  813. private void Teleport(BasePlayer player, Vector3 position)
  814. {
  815. if (player.net?.connection != null)
  816. {
  817. player.ClientRPCPlayer(null, player, "StartLoading");
  818. }
  819.  
  820. StartSleeping(player);
  821. player.MovePosition(position);
  822.  
  823. if (player.net?.connection != null)
  824. {
  825. player.ClientRPCPlayer(null, player, "ForcePositionTo", position);
  826. }
  827.  
  828. if (player.net?.connection != null)
  829. {
  830. player.SetPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot, true);
  831. }
  832.  
  833. player.UpdateNetworkGroup();
  834. player.SendNetworkUpdateImmediate();
  835.  
  836. if (player.net?.connection == null)
  837. {
  838. return;
  839. }
  840.  
  841. try
  842. {
  843. player.ClearEntityQueue();
  844. }
  845. catch
  846. {
  847. }
  848.  
  849. player.SendFullSnapshot();
  850. }
  851.  
  852. private void StartSleeping(BasePlayer player)
  853. {
  854. if (player.IsSleeping())
  855. {
  856. return;
  857. }
  858.  
  859. player.SetPlayerFlag(BasePlayer.PlayerFlags.Sleeping, true);
  860.  
  861. if (!BasePlayer.sleepingPlayerList.Contains(player))
  862. {
  863. BasePlayer.sleepingPlayerList.Add(player);
  864. }
  865.  
  866. player.CancelInvoke("InventoryUpdate");
  867. }
  868.  
  869. // EVENT KIT
  870. private void GiveKit(BasePlayer player, string kitName)
  871. {
  872. player.inventory.Strip();
  873. Kits?.CallHook("GiveKit", player, kitName);
  874. }
  875.  
  876. // OTHER
  877. private void Respawn(BasePlayer player)
  878. {
  879. var spawn = Spawns.GetRandom((uint)DateTime.UtcNow.Millisecond);
  880. Teleport(player, spawn.Location.ToVector3());
  881.  
  882. player.health = 100;
  883. player.metabolism.wetness.value = 0f;
  884. player.metabolism.radiation_level.value = 0f;
  885. player.metabolism.radiation_poison.value = 0f;
  886. player.StopWounded();
  887.  
  888. GiveKit(player, Configuration.KitName);
  889. LockInventory(player);
  890. }
  891.  
  892. private void Remove(BasePlayer player, bool unload = false)
  893. {
  894. PrintToChat(player, lang.GetMessage("Left", this, player.UserIDString));
  895.  
  896. foreach (var target in players)
  897. {
  898. if (target != player && !unload)
  899. {
  900. PrintToChat(target, lang.GetMessage("All Left", this, target.UserIDString), player.displayName, players.Count - 1);
  901. }
  902. }
  903.  
  904. if (!unload)
  905. {
  906. players.Remove(player);
  907. }
  908.  
  909. Teleport(player, locations[player]);
  910. locations.Remove(player);
  911. outOfBoundsPlayers.Remove(player);
  912. player.inventory.Strip();
  913. GiveInventory(player);
  914. RestoreMetabolism(player);
  915. metabolism.Remove(player);
  916. killStreaks.Remove(player);
  917. UnlockInventory(player);
  918. }
  919.  
  920. private void Add(BasePlayer player)
  921. {
  922. PrintToChat(player, lang.GetMessage("Joined", this, player.UserIDString));
  923.  
  924. foreach (var target in players)
  925. {
  926. if (target != player)
  927. {
  928. PrintToChat(target, lang.GetMessage("All Joined", this, target.UserIDString), player.displayName, players.Count + 1);
  929. }
  930. }
  931.  
  932. players.Add(player);
  933. locations.Add(player, new Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z));
  934. inventories.Add(player, GetInventory(player));
  935. player.inventory.Strip();
  936. metabolism.Add(player, GetMetabolism(player));
  937. Respawn(player);
  938. }
  939.  
  940. private void LockInventory(BasePlayer player)
  941. {
  942. player.inventory.containerWear.SetFlag(ItemContainer.Flag.IsLocked, true);
  943. }
  944.  
  945. private void UnlockInventory(BasePlayer player)
  946. {
  947. player.inventory.containerWear.SetFlag(ItemContainer.Flag.IsLocked, false);
  948. }
  949.  
  950. // PERMISSION
  951. private bool HasPermission(BasePlayer player, string perm = "deathmatch.admin") => player.IsAdmin || permission.UserHasPermission(player.UserIDString, perm);
  952.  
  953. #endregion
  954.  
  955. #region Spheres
  956.  
  957. public class Sphere
  958. {
  959. private readonly BaseEntity sphereEntity;
  960. private readonly BaseEntity visSphereEntity;
  961.  
  962. public Sphere(Vector3 position)
  963. {
  964. sphereEntity = GameManager.server.CreateEntity("assets/prefabs/visualization/sphere.prefab", position);
  965. sphereEntity.name = $"{spheres++}";
  966. var sphere = sphereEntity.GetComponent<SphereEntity>();
  967. sphere.currentRadius = radius * 2 + 30;
  968. sphere.lerpRadius = sphere.currentRadius;
  969. sphereEntity.Spawn();
  970. var trigger = sphereEntity.gameObject.AddComponent<SphereTrigger>();
  971. trigger.Area = this;
  972.  
  973. // VIS SPHERE
  974. visSphereEntity = GameManager.server.CreateEntity("assets/prefabs/visualization/sphere.prefab", position);
  975. var visSphere = visSphereEntity.GetComponent<SphereEntity>();
  976. visSphere.currentRadius = sphere.currentRadius;
  977. visSphere.lerpRadius = sphere.currentRadius;
  978.  
  979. for (var i = 0; i <= 10; i++)
  980. {
  981. visSphereEntity.name = $"{i + 5}";
  982. visSphereEntity.Instantiate();
  983. }
  984. }
  985.  
  986. public void Remove()
  987. {
  988. sphereEntity.Kill();
  989. visSphereEntity.Kill();
  990. }
  991. }
  992.  
  993. public class SphereTrigger : TriggerBase
  994. {
  995. public Sphere Area;
  996.  
  997. internal virtual void Awake()
  998. {
  999. gameObject.layer = LayerMask.NameToLayer("Trigger");
  1000. var collider = gameObject.AddComponent<SphereCollider>();
  1001. collider.radius = radius + 15;
  1002. collider.isTrigger = true;
  1003. }
  1004.  
  1005. internal virtual GameObject InterestedInObject(GameObject obj)
  1006. {
  1007. var player = obj.ToBaseEntity() as BasePlayer;
  1008. return !player ? null : obj;
  1009. }
  1010.  
  1011. internal virtual void OnTriggerEnter(Collider collider)
  1012. {
  1013. var go = InterestedInObject(collider.gameObject);
  1014.  
  1015. if (go == null)
  1016. {
  1017. return;
  1018. }
  1019.  
  1020. if (contents == null)
  1021. {
  1022. contents = new HashSet<GameObject>();
  1023. }
  1024.  
  1025. if (contents.Contains(go))
  1026. {
  1027. return;
  1028. }
  1029.  
  1030. contents.Add(go);
  1031. OnEntityEnter(go.ToBaseEntity());
  1032. }
  1033.  
  1034. internal virtual void OnTriggerExit(Collider collider)
  1035. {
  1036. if (collider == null) return;
  1037. var go = InterestedInObject(collider.gameObject);
  1038. if (go == null) return;
  1039. if (contents == null) return;
  1040. contents.Remove(go);
  1041. OnEntityLeave(go.ToBaseEntity());
  1042. }
  1043.  
  1044. internal virtual void OnEntityEnter(BaseEntity ent)
  1045. {
  1046. if (ent == null) return;
  1047. if (entityContents == null) entityContents = new HashSet<BaseEntity>();
  1048. entityContents.Add(ent);
  1049. ent.EnterTrigger(this);
  1050. Instance.CallHook("OnTriggerEnter", this, ent);
  1051. }
  1052.  
  1053. internal virtual void OnEntityLeave(BaseEntity ent)
  1054. {
  1055. if (ent == null) return;
  1056. if (entityContents == null) return;
  1057. entityContents.Remove(ent);
  1058. ent.LeaveTrigger(this);
  1059. Instance.CallHook("OnTriggerLeave", this, ent);
  1060. }
  1061. }
  1062.  
  1063. private void OnTriggerEnter(TriggerBase trigger, BaseEntity entity)
  1064. {
  1065. var area = trigger as SphereTrigger;
  1066.  
  1067. if (area == null)
  1068. {
  1069. return;
  1070. }
  1071.  
  1072. var player = entity as BasePlayer;
  1073.  
  1074. if (player == null)
  1075. {
  1076. return;
  1077. }
  1078.  
  1079. if (outOfBoundsPlayers.Contains(player))
  1080. {
  1081. outOfBoundsPlayers.Remove(player);
  1082. player.metabolism.radiation_poison.value = 0f;
  1083. player.metabolism.radiation_level.value = 0f;
  1084. }
  1085. }
  1086.  
  1087. private void OnTriggerLeave(TriggerBase trigger, BaseEntity entity)
  1088. {
  1089. var area = trigger as SphereTrigger;
  1090.  
  1091. if (area == null)
  1092. {
  1093. return;
  1094. }
  1095.  
  1096. var player = entity as BasePlayer;
  1097.  
  1098. if (player == null)
  1099. {
  1100. return;
  1101. }
  1102.  
  1103. if (!players.Contains(player))
  1104. {
  1105. return;
  1106. }
  1107.  
  1108. outOfBoundsPlayers.Add(player);
  1109. Message(player, "Out of Bounds");
  1110.  
  1111. var outOfBoundsTimer = timer.Repeat(1f, 0, () =>
  1112. {
  1113. if (!outOfBoundsPlayers.Contains(player))
  1114. {
  1115. return;
  1116. }
  1117.  
  1118. player.metabolism.radiation_level.value += UnityEngine.Random.Range(1f, 3f);
  1119. player.metabolism.radiation_poison.value +=UnityEngine.Random.Range(1f, 3f);
  1120. });
  1121. }
  1122.  
  1123. private void DrawSphere(List<Vector3> positionList)
  1124. {
  1125. if (positionList.Count == 1)
  1126. {
  1127. sphere = new Sphere(positionList[0]);
  1128. return;
  1129. }
  1130.  
  1131. var center = new Vector3(0, 0, 0);
  1132.  
  1133. foreach (var position in positionList)
  1134. {
  1135. center += position;
  1136.  
  1137. foreach (Vector3 t in positionList)
  1138. {
  1139. if (Vector3.Distance(position, t) / 2 > radius)
  1140. {
  1141. radius = Vector3.Distance(position, t);
  1142. }
  1143. }
  1144. }
  1145.  
  1146. sphere = new Sphere(center / positionList.Count);
  1147. }
  1148.  
  1149. #endregion
  1150.  
  1151. #region Other
  1152.  
  1153. private object canRedeemKit(BasePlayer player) => players.Contains(player) ? lang.GetMessage("Kit Block", this) : null;
  1154.  
  1155. private object CanTeleport(BasePlayer player) => players.Contains(player) ? lang.GetMessage("Teleport Block", this) : null;
  1156.  
  1157. #endregion
  1158. }
  1159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement