Advertisement
Seuss_CZ

Untitled

Jan 17th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml.Serialization;
  5. using Rocket.API;
  6. using Rocket.API.Collections;
  7. using Rocket.API.Serialisation;
  8. using Rocket.Core.Plugins;
  9. using Rocket.Unturned.Chat;
  10. using Rocket.Unturned.Effects;
  11. using SDG.Unturned;
  12. using UnityEngine;
  13. using Logger = Rocket.Core.Logging.Logger;
  14.  
  15. namespace ZaupFeast
  16. {
  17. internal class Feast : RocketPlugin<FeastConfiguration>
  18. {
  19. public static Feast Instance = null;
  20. private List<Locs> locations = new List<Locs>();
  21. internal Locs nextLocation;
  22. private byte msgNum;
  23. private DateTime lastMsg;
  24. private bool running = false;
  25. private List<Vector3> drops;
  26. private List<Item> items;
  27. private List<Item> items2;
  28. private int numItems;
  29.  
  30. public override TranslationList DefaultTranslations
  31. {
  32. get
  33. {
  34. return new TranslationList()
  35. {
  36. {"box_info","Vyber si jeden box!"},
  37. {"now_feast_msg","Otevřel jsi truhlu {0}!"}
  38. };
  39. }
  40. }
  41.  
  42. protected override void Load()
  43. {
  44. // Make this able to be used a singleton
  45. Feast.Instance = this;
  46.  
  47. // Set some variables to empty to start.
  48. this.drops = new List<Vector3>();
  49. this.items = new List<Item>();
  50. this.items2 = new List<Item>();
  51. this.numItems = 0;
  52.  
  53. // Did we load the configuration file? If not, just end.
  54. if (Configuration.Instance.Items == null || ! Configuration.Instance.Items.Any())
  55. {
  56. Rocket.Core.Logging.Logger.Log("Failed to load the configuration file. Turned off feast. Restart to try again.");
  57. return;
  58. }
  59. Provider.onServerHosted += () =>
  60. {
  61. if (LevelNodes.nodes == null)
  62. LevelNodes.load();
  63. };
  64. }
  65.  
  66. internal void runFeast()
  67. {
  68. // Get how many items are going to drop and setup the item id list.
  69. int itemsNum = UnityEngine.Random.Range((int)Feast.Instance.Configuration.Instance.MinItemsDrop, (int)Feast.Instance.Configuration.Instance.MaxItemsDrop+1);
  70. List<int> list = new List<int>();
  71. foreach (FeastItem current in Feast.Instance.Configuration.Instance.Items)
  72. {
  73. if (current.Location.Contains(Feast.Instance.nextLocation.Name) || current.Location.Contains("all") || current.Location.Contains("All"))
  74. {
  75. for (byte b = 0; b < current.Chance; b++)
  76. {
  77. list.Add(current.Id);
  78. }
  79. }
  80. }
  81.  
  82. // Figure out the base point of the feast drop.
  83. Vector3 p = Feast.Instance.nextLocation.Pos;
  84. /*p.y = 500;
  85. RaycastHit hit;
  86. Physics.Raycast(p, Vector3.down, out hit, 500);
  87. p = hit.point;*/
  88.  
  89. // Get the ending item positions and items
  90. List<Vector3> drops = new List<Vector3>();
  91. List<Item> items = new List<Item>();
  92. for (int i = 0; i < itemsNum; i++)
  93. {
  94. int item = UnityEngine.Random.Range(0, list.Count);
  95. int num2 = list[item];
  96. Vector3 pos1 = new Vector3();
  97. pos1.x += p.x + (float)UnityEngine.Random.Range((int)Feast.Instance.Configuration.Instance.DropRadius * -1, (int)Feast.Instance.Configuration.Instance.DropRadius + 1);
  98. pos1.z += p.z + (float)UnityEngine.Random.Range((int)Feast.Instance.Configuration.Instance.DropRadius * -1, (int)Feast.Instance.Configuration.Instance.DropRadius + 1);
  99. pos1.y = 200;
  100. RaycastHit hit;
  101. Physics.Raycast(pos1, Vector3.down, out hit, 500);
  102. Vector3 pos = hit.point;
  103. Item g = new Item((ushort)num2, true);
  104. //drops[i] = pos;
  105. drops.Add(pos);
  106. //items[i] = g;
  107. items.Add(g);
  108. list.RemoveAt(item);
  109. }
  110. Feast.Instance.numItems = itemsNum;
  111. Feast.Instance.drops = drops;
  112. Feast.Instance.items = items;
  113. // See if skydrop is turned on. If it is, let's start the effects.
  114.  
  115. if (Feast.Instance.Configuration.Instance.SkyDrop)
  116. {
  117. EffectAsset plane = Assets.find(EAssetType.EFFECT, Feast.Instance.Configuration.Instance.PlaneEffectId) as EffectAsset;
  118.  
  119. if (plane == null)
  120. {
  121. Logger.Log("The skydrop plane bundle is not the server! Cannot trigger the airdrop animation. Just sending items.");
  122. this.spawnItems(this.items, drops);
  123. }
  124. else
  125. {
  126. Vector3 loc = new Vector3(Feast.Instance.nextLocation.Pos.x, Feast.Instance.nextLocation.Pos.y + 50, Feast.Instance.nextLocation.Pos.z + 500);
  127. SDG.Unturned.EffectManager.instance.channel.send("tellEffectPoint", ESteamCall.CLIENTS, loc, 1024, ESteamPacket.UPDATE_UNRELIABLE_BUFFER, new object[] { Feast.Instance.Configuration.Instance.SkydropEffectId, loc });
  128. }
  129. }
  130. else
  131. {
  132. // No effects, just spawn in the items on the ground.
  133. this.spawnItems(this.items, drops);
  134. }
  135.  
  136. // Reset the feast for the next round.
  137. }
  138.  
  139. internal void runFeast2()
  140. {
  141. // Get how many items are going to drop and setup the item id list.
  142. int itemsNum = UnityEngine.Random.Range((int)Feast.Instance.Configuration.Instance.MinItemsDrop, (int)Feast.Instance.Configuration.Instance.MaxItemsDrop + 1);
  143. List<int> list = new List<int>();
  144. foreach (FeastItem2 current in Feast.Instance.Configuration.Instance.Items2)
  145. {
  146. if (current.Location.Contains(Feast.Instance.nextLocation.Name) || current.Location.Contains("all") || current.Location.Contains("All"))
  147. {
  148. for (byte b = 0; b < current.Chance; b++)
  149. {
  150. list.Add(current.Id);
  151. }
  152. }
  153. }
  154.  
  155. // Figure out the base point of the feast drop.
  156. Vector3 p = Feast.Instance.nextLocation.Pos;
  157. /*p.y = 500;
  158. RaycastHit hit;
  159. Physics.Raycast(p, Vector3.down, out hit, 500);
  160. p = hit.point;*/
  161.  
  162. // Get the ending item positions and items
  163. List<Vector3> drops = new List<Vector3>();
  164. List<Item> items2 = new List<Item>();
  165. for (int i = 0; i < itemsNum; i++)
  166. {
  167. int item = UnityEngine.Random.Range(0, list.Count);
  168. int num2 = list[item];
  169. Vector3 pos1 = new Vector3();
  170. pos1.x += p.x + (float)UnityEngine.Random.Range((int)Feast.Instance.Configuration.Instance.DropRadius * -1, (int)Feast.Instance.Configuration.Instance.DropRadius + 1);
  171. pos1.z += p.z + (float)UnityEngine.Random.Range((int)Feast.Instance.Configuration.Instance.DropRadius * -1, (int)Feast.Instance.Configuration.Instance.DropRadius + 1);
  172. pos1.y = 200;
  173. RaycastHit hit;
  174. Physics.Raycast(pos1, Vector3.down, out hit, 500);
  175. Vector3 pos = hit.point;
  176. Item g = new Item((ushort)num2, true);
  177. //drops[i] = pos;
  178. drops.Add(pos);
  179. //items[i] = g;
  180. items2.Add(g);
  181. list.RemoveAt(item);
  182. }
  183. Feast.Instance.numItems = itemsNum;
  184. Feast.Instance.drops = drops;
  185. Feast.Instance.items2 = items2;
  186. // See if skydrop is turned on. If it is, let's start the effects.
  187.  
  188. if (Feast.Instance.Configuration.Instance.SkyDrop)
  189. {
  190. EffectAsset plane = Assets.find(EAssetType.EFFECT, Feast.Instance.Configuration.Instance.PlaneEffectId) as EffectAsset;
  191.  
  192. if (plane == null)
  193. {
  194. Logger.Log("The skydrop plane bundle is not the server! Cannot trigger the airdrop animation. Just sending items.");
  195. this.spawnItems2(this.items2, drops);
  196. }
  197. else
  198. {
  199. Vector3 loc = new Vector3(Feast.Instance.nextLocation.Pos.x, Feast.Instance.nextLocation.Pos.y + 50, Feast.Instance.nextLocation.Pos.z + 500);
  200. SDG.Unturned.EffectManager.instance.channel.send("tellEffectPoint", ESteamCall.CLIENTS, loc, 1024, ESteamPacket.UPDATE_UNRELIABLE_BUFFER, new object[] { Feast.Instance.Configuration.Instance.SkydropEffectId, loc });
  201. }
  202. }
  203. else
  204. {
  205. // No effects, just spawn in the items on the ground.
  206. this.spawnItems2(this.items2, drops);
  207. }
  208.  
  209. // Reset the feast for the next round.
  210. }
  211.  
  212.  
  213. private void spawnItems(List<Item> items, List<Vector3> drops)
  214. {
  215. if (items.Count != drops.Count)
  216. {
  217. Logger.Log("Something went wrong. Feast didn't drop!");
  218. return;
  219. }
  220. for (int d = 0; d < items.Count; d++ ) {
  221. ItemManager.dropItem(items[d], drops[d], false, true, true);
  222. }
  223. }
  224.  
  225. private void spawnItems2(List<Item> items2, List<Vector3> drops)
  226. {
  227. if (items2.Count != drops.Count)
  228. {
  229. Logger.Log("Something went wrong. Feast didn't drop!");
  230. return;
  231. }
  232. for (int d = 0; d < items2.Count; d++)
  233. {
  234. ItemManager.dropItem(items2[d], drops[d], false, true, true);
  235. }
  236. }
  237.  
  238.  
  239. private void checkFeast()
  240. {
  241. try
  242. {
  243. if (Feast.Instance == null) Logger.LogError("Feast.Instance is NULL");
  244. if (Feast.Instance.Configuration == null) Logger.LogError("(Feast.Instance.Configuration is NULL");
  245. if (Feast.Instance.Configuration.Instance == null) Logger.LogError("Feast.Instance.Configuration.Instance is NULL");
  246. if (Feast.Instance.Configuration.Instance.Enabled)
  247. {
  248. if (Feast.Instance.lastMsg == null) Logger.LogError("Feast.Instance.lastMsg is NULL");
  249. {
  250. byte b = Feast.Instance.msgNum;
  251. if (b != 0)
  252. {
  253. UnturnedChat.Say(Feast.Instance.Translate("coming_feast_msg",Feast.Instance.nextLocation.Name, Feast.Instance.msgNum), UnturnedChat.GetColorFromName(Feast.Instance.Configuration.Instance.MessageColor, Color.red));
  254. Feast.Instance.lastMsg = DateTime.Now;
  255. Feast.Instance.msgNum -= 1;
  256. }
  257. else
  258. {
  259. UnturnedChat.Say(Feast.Instance.Translate("now_feast_msg", Feast.Instance.nextLocation.Name), UnturnedChat.GetColorFromName(Feast.Instance.Configuration.Instance.MessageColor, Color.red));
  260. Feast.Instance.lastMsg = DateTime.Now;
  261. Feast.Instance.runFeast();
  262. }
  263. }
  264. }
  265. }
  266. catch (Exception ex)
  267. {
  268. Logger.LogException(ex, "Exception in checkFeast");
  269. }
  270. }
  271.  
  272. private void initializeNodes()
  273. {
  274. nodesInitialised = true;
  275. if (LevelNodes.nodes == null && (DateTime.Now - startTime).TotalSeconds > 5) LevelNodes.load();
  276. foreach (Node n in LevelNodes.nodes)
  277. {
  278. if (n.type == ENodeType.LOCATION)
  279. {
  280. Locs loc = new Locs(n.point, ((LocationNode)n).name);
  281. this.locations.Add(loc);
  282. }
  283. }
  284.  
  285. // Get all the locations used by the items and remove any invalid items.
  286. List<string> usedlocs = new List<string>();
  287.  
  288. if (Configuration == null) Logger.LogError("Doh! Configuration is NULL");
  289. if (Configuration.Instance == null) Logger.LogError("Doh! Configuration.Instance is NULL");
  290. if (Configuration.Instance.Items == null) Logger.LogError("Configuration.Instance.Items is NULL");
  291. foreach (FeastItem f in Configuration.Instance.Items.ToList())
  292. {
  293. ItemAsset itemAsset = (ItemAsset)Assets.find(EAssetType.ITEM, f.Id);
  294. if (itemAsset == null && itemAsset.isPro)
  295. {
  296. Configuration.Instance.Items.Remove(f);
  297. }
  298. else
  299. {
  300. usedlocs = usedlocs.Concat(f.Location).ToList();
  301. }
  302. }
  303.  
  304.  
  305. // Remove any unused map locations from the location list.
  306. List<string> locations = usedlocs.Distinct().ToList();
  307. List<Locs> locs2 = this.locations.ToList();
  308. if (!locations.Contains("all") && !locations.Contains("All"))
  309. {
  310. foreach (Locs a in locs2)
  311. {
  312. if (locations.IndexOf(a.Name) == -1)
  313. {
  314. this.locations.Remove(a);
  315. }
  316. }
  317. }
  318.  
  319. // Now that it is all set up, we'll all the plugin to start running. This was just to keep the FixedUpdate from running before the top finished running.
  320. this.running = true;
  321. }
  322.  
  323.  
  324.  
  325. private bool nodesInitialised = false;
  326. private DateTime startTime = DateTime.Now;
  327.  
  328. public void FixedUpdate()
  329. {
  330. try
  331. {
  332. if(State == PluginState.Loaded)
  333. {
  334. if (nodesInitialised)
  335. {
  336. if (Feast.Instance.Configuration.Instance.Enabled && Feast.Instance.running)
  337. Feast.Instance.checkFeast();
  338. }
  339. else
  340. {
  341. if ((DateTime.Now - startTime).TotalSeconds > 5)
  342. initializeNodes();
  343. }
  344. }
  345. }
  346. catch (Exception ex)
  347. {
  348. Logger.LogError("Doh! LevelNodes.nodes is NULL");
  349. Logger.LogException(ex, "Exception in FixedUpdate");
  350. }
  351. }
  352. }
  353.  
  354. internal class Locs
  355. {
  356. private Vector3 v;
  357. private string n;
  358. public Vector3 Pos
  359. {
  360. get
  361. {
  362. return this.v;
  363. }
  364. }
  365. public string Name
  366. {
  367. get
  368. {
  369. return this.n;
  370. }
  371. }
  372. public Locs(Vector3 Pos, string Name)
  373. {
  374. this.v = Pos;
  375. this.n = Name;
  376. }
  377. }
  378.  
  379. public class FeastItem
  380. {
  381. public ushort Id;
  382. public string Name;
  383. public byte Chance;
  384. [XmlArray(ElementName = "Locations")]
  385. public List<string> Location;
  386. public FeastItem(ushort id, string name, byte chance, List<string> locs)
  387. {
  388. this.Id = id;
  389. this.Name = name;
  390. this.Chance = chance;
  391. this.Location = locs;
  392. }
  393. public FeastItem()
  394. {
  395. }
  396. }
  397.  
  398. public class FeastItem2
  399. {
  400. public ushort Id;
  401. public string Name;
  402. public byte Chance;
  403. [XmlArray(ElementName = "Locations")]
  404. public List<string> Location;
  405. public FeastItem2(ushort id, string name, byte chance, List<string> locs)
  406. {
  407. this.Id = id;
  408. this.Name = name;
  409. this.Chance = chance;
  410. this.Location = locs;
  411. }
  412. public FeastItem2()
  413. {
  414. }
  415. }
  416.  
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement