Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Xml.Serialization;
- using Rocket.API;
- using Rocket.API.Collections;
- using Rocket.API.Serialisation;
- using Rocket.Core.Plugins;
- using Rocket.Unturned.Chat;
- using Rocket.Unturned.Effects;
- using SDG.Unturned;
- using UnityEngine;
- using Logger = Rocket.Core.Logging.Logger;
- namespace ZaupFeast
- {
- internal class Feast : RocketPlugin<FeastConfiguration>
- {
- public static Feast Instance = null;
- private List<Locs> locations = new List<Locs>();
- internal Locs nextLocation;
- private byte msgNum;
- private DateTime lastMsg;
- private bool running = false;
- private List<Vector3> drops;
- private List<Item> items;
- private List<Item> items2;
- private int numItems;
- public override TranslationList DefaultTranslations
- {
- get
- {
- return new TranslationList()
- {
- {"box_info","Vyber si jeden box!"},
- {"now_feast_msg","Otevřel jsi truhlu {0}!"}
- };
- }
- }
- protected override void Load()
- {
- // Make this able to be used a singleton
- Feast.Instance = this;
- // Set some variables to empty to start.
- this.drops = new List<Vector3>();
- this.items = new List<Item>();
- this.items2 = new List<Item>();
- this.numItems = 0;
- // Did we load the configuration file? If not, just end.
- if (Configuration.Instance.Items == null || ! Configuration.Instance.Items.Any())
- {
- Rocket.Core.Logging.Logger.Log("Failed to load the configuration file. Turned off feast. Restart to try again.");
- return;
- }
- Provider.onServerHosted += () =>
- {
- if (LevelNodes.nodes == null)
- LevelNodes.load();
- };
- }
- internal void runFeast()
- {
- // Get how many items are going to drop and setup the item id list.
- int itemsNum = UnityEngine.Random.Range((int)Feast.Instance.Configuration.Instance.MinItemsDrop, (int)Feast.Instance.Configuration.Instance.MaxItemsDrop+1);
- List<int> list = new List<int>();
- foreach (FeastItem current in Feast.Instance.Configuration.Instance.Items)
- {
- if (current.Location.Contains(Feast.Instance.nextLocation.Name) || current.Location.Contains("all") || current.Location.Contains("All"))
- {
- for (byte b = 0; b < current.Chance; b++)
- {
- list.Add(current.Id);
- }
- }
- }
- // Figure out the base point of the feast drop.
- Vector3 p = Feast.Instance.nextLocation.Pos;
- /*p.y = 500;
- RaycastHit hit;
- Physics.Raycast(p, Vector3.down, out hit, 500);
- p = hit.point;*/
- // Get the ending item positions and items
- List<Vector3> drops = new List<Vector3>();
- List<Item> items = new List<Item>();
- for (int i = 0; i < itemsNum; i++)
- {
- int item = UnityEngine.Random.Range(0, list.Count);
- int num2 = list[item];
- Vector3 pos1 = new Vector3();
- pos1.x += p.x + (float)UnityEngine.Random.Range((int)Feast.Instance.Configuration.Instance.DropRadius * -1, (int)Feast.Instance.Configuration.Instance.DropRadius + 1);
- pos1.z += p.z + (float)UnityEngine.Random.Range((int)Feast.Instance.Configuration.Instance.DropRadius * -1, (int)Feast.Instance.Configuration.Instance.DropRadius + 1);
- pos1.y = 200;
- RaycastHit hit;
- Physics.Raycast(pos1, Vector3.down, out hit, 500);
- Vector3 pos = hit.point;
- Item g = new Item((ushort)num2, true);
- //drops[i] = pos;
- drops.Add(pos);
- //items[i] = g;
- items.Add(g);
- list.RemoveAt(item);
- }
- Feast.Instance.numItems = itemsNum;
- Feast.Instance.drops = drops;
- Feast.Instance.items = items;
- // See if skydrop is turned on. If it is, let's start the effects.
- if (Feast.Instance.Configuration.Instance.SkyDrop)
- {
- EffectAsset plane = Assets.find(EAssetType.EFFECT, Feast.Instance.Configuration.Instance.PlaneEffectId) as EffectAsset;
- if (plane == null)
- {
- Logger.Log("The skydrop plane bundle is not the server! Cannot trigger the airdrop animation. Just sending items.");
- this.spawnItems(this.items, drops);
- }
- else
- {
- Vector3 loc = new Vector3(Feast.Instance.nextLocation.Pos.x, Feast.Instance.nextLocation.Pos.y + 50, Feast.Instance.nextLocation.Pos.z + 500);
- SDG.Unturned.EffectManager.instance.channel.send("tellEffectPoint", ESteamCall.CLIENTS, loc, 1024, ESteamPacket.UPDATE_UNRELIABLE_BUFFER, new object[] { Feast.Instance.Configuration.Instance.SkydropEffectId, loc });
- }
- }
- else
- {
- // No effects, just spawn in the items on the ground.
- this.spawnItems(this.items, drops);
- }
- // Reset the feast for the next round.
- }
- internal void runFeast2()
- {
- // Get how many items are going to drop and setup the item id list.
- int itemsNum = UnityEngine.Random.Range((int)Feast.Instance.Configuration.Instance.MinItemsDrop, (int)Feast.Instance.Configuration.Instance.MaxItemsDrop + 1);
- List<int> list = new List<int>();
- foreach (FeastItem2 current in Feast.Instance.Configuration.Instance.Items2)
- {
- if (current.Location.Contains(Feast.Instance.nextLocation.Name) || current.Location.Contains("all") || current.Location.Contains("All"))
- {
- for (byte b = 0; b < current.Chance; b++)
- {
- list.Add(current.Id);
- }
- }
- }
- // Figure out the base point of the feast drop.
- Vector3 p = Feast.Instance.nextLocation.Pos;
- /*p.y = 500;
- RaycastHit hit;
- Physics.Raycast(p, Vector3.down, out hit, 500);
- p = hit.point;*/
- // Get the ending item positions and items
- List<Vector3> drops = new List<Vector3>();
- List<Item> items2 = new List<Item>();
- for (int i = 0; i < itemsNum; i++)
- {
- int item = UnityEngine.Random.Range(0, list.Count);
- int num2 = list[item];
- Vector3 pos1 = new Vector3();
- pos1.x += p.x + (float)UnityEngine.Random.Range((int)Feast.Instance.Configuration.Instance.DropRadius * -1, (int)Feast.Instance.Configuration.Instance.DropRadius + 1);
- pos1.z += p.z + (float)UnityEngine.Random.Range((int)Feast.Instance.Configuration.Instance.DropRadius * -1, (int)Feast.Instance.Configuration.Instance.DropRadius + 1);
- pos1.y = 200;
- RaycastHit hit;
- Physics.Raycast(pos1, Vector3.down, out hit, 500);
- Vector3 pos = hit.point;
- Item g = new Item((ushort)num2, true);
- //drops[i] = pos;
- drops.Add(pos);
- //items[i] = g;
- items2.Add(g);
- list.RemoveAt(item);
- }
- Feast.Instance.numItems = itemsNum;
- Feast.Instance.drops = drops;
- Feast.Instance.items2 = items2;
- // See if skydrop is turned on. If it is, let's start the effects.
- if (Feast.Instance.Configuration.Instance.SkyDrop)
- {
- EffectAsset plane = Assets.find(EAssetType.EFFECT, Feast.Instance.Configuration.Instance.PlaneEffectId) as EffectAsset;
- if (plane == null)
- {
- Logger.Log("The skydrop plane bundle is not the server! Cannot trigger the airdrop animation. Just sending items.");
- this.spawnItems2(this.items2, drops);
- }
- else
- {
- Vector3 loc = new Vector3(Feast.Instance.nextLocation.Pos.x, Feast.Instance.nextLocation.Pos.y + 50, Feast.Instance.nextLocation.Pos.z + 500);
- SDG.Unturned.EffectManager.instance.channel.send("tellEffectPoint", ESteamCall.CLIENTS, loc, 1024, ESteamPacket.UPDATE_UNRELIABLE_BUFFER, new object[] { Feast.Instance.Configuration.Instance.SkydropEffectId, loc });
- }
- }
- else
- {
- // No effects, just spawn in the items on the ground.
- this.spawnItems2(this.items2, drops);
- }
- // Reset the feast for the next round.
- }
- private void spawnItems(List<Item> items, List<Vector3> drops)
- {
- if (items.Count != drops.Count)
- {
- Logger.Log("Something went wrong. Feast didn't drop!");
- return;
- }
- for (int d = 0; d < items.Count; d++ ) {
- ItemManager.dropItem(items[d], drops[d], false, true, true);
- }
- }
- private void spawnItems2(List<Item> items2, List<Vector3> drops)
- {
- if (items2.Count != drops.Count)
- {
- Logger.Log("Something went wrong. Feast didn't drop!");
- return;
- }
- for (int d = 0; d < items2.Count; d++)
- {
- ItemManager.dropItem(items2[d], drops[d], false, true, true);
- }
- }
- private void checkFeast()
- {
- try
- {
- if (Feast.Instance == null) Logger.LogError("Feast.Instance is NULL");
- if (Feast.Instance.Configuration == null) Logger.LogError("(Feast.Instance.Configuration is NULL");
- if (Feast.Instance.Configuration.Instance == null) Logger.LogError("Feast.Instance.Configuration.Instance is NULL");
- if (Feast.Instance.Configuration.Instance.Enabled)
- {
- if (Feast.Instance.lastMsg == null) Logger.LogError("Feast.Instance.lastMsg is NULL");
- {
- byte b = Feast.Instance.msgNum;
- if (b != 0)
- {
- UnturnedChat.Say(Feast.Instance.Translate("coming_feast_msg",Feast.Instance.nextLocation.Name, Feast.Instance.msgNum), UnturnedChat.GetColorFromName(Feast.Instance.Configuration.Instance.MessageColor, Color.red));
- Feast.Instance.lastMsg = DateTime.Now;
- Feast.Instance.msgNum -= 1;
- }
- else
- {
- UnturnedChat.Say(Feast.Instance.Translate("now_feast_msg", Feast.Instance.nextLocation.Name), UnturnedChat.GetColorFromName(Feast.Instance.Configuration.Instance.MessageColor, Color.red));
- Feast.Instance.lastMsg = DateTime.Now;
- Feast.Instance.runFeast();
- }
- }
- }
- }
- catch (Exception ex)
- {
- Logger.LogException(ex, "Exception in checkFeast");
- }
- }
- private void initializeNodes()
- {
- nodesInitialised = true;
- if (LevelNodes.nodes == null && (DateTime.Now - startTime).TotalSeconds > 5) LevelNodes.load();
- foreach (Node n in LevelNodes.nodes)
- {
- if (n.type == ENodeType.LOCATION)
- {
- Locs loc = new Locs(n.point, ((LocationNode)n).name);
- this.locations.Add(loc);
- }
- }
- // Get all the locations used by the items and remove any invalid items.
- List<string> usedlocs = new List<string>();
- if (Configuration == null) Logger.LogError("Doh! Configuration is NULL");
- if (Configuration.Instance == null) Logger.LogError("Doh! Configuration.Instance is NULL");
- if (Configuration.Instance.Items == null) Logger.LogError("Configuration.Instance.Items is NULL");
- foreach (FeastItem f in Configuration.Instance.Items.ToList())
- {
- ItemAsset itemAsset = (ItemAsset)Assets.find(EAssetType.ITEM, f.Id);
- if (itemAsset == null && itemAsset.isPro)
- {
- Configuration.Instance.Items.Remove(f);
- }
- else
- {
- usedlocs = usedlocs.Concat(f.Location).ToList();
- }
- }
- // Remove any unused map locations from the location list.
- List<string> locations = usedlocs.Distinct().ToList();
- List<Locs> locs2 = this.locations.ToList();
- if (!locations.Contains("all") && !locations.Contains("All"))
- {
- foreach (Locs a in locs2)
- {
- if (locations.IndexOf(a.Name) == -1)
- {
- this.locations.Remove(a);
- }
- }
- }
- // 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.
- this.running = true;
- }
- private bool nodesInitialised = false;
- private DateTime startTime = DateTime.Now;
- public void FixedUpdate()
- {
- try
- {
- if(State == PluginState.Loaded)
- {
- if (nodesInitialised)
- {
- if (Feast.Instance.Configuration.Instance.Enabled && Feast.Instance.running)
- Feast.Instance.checkFeast();
- }
- else
- {
- if ((DateTime.Now - startTime).TotalSeconds > 5)
- initializeNodes();
- }
- }
- }
- catch (Exception ex)
- {
- Logger.LogError("Doh! LevelNodes.nodes is NULL");
- Logger.LogException(ex, "Exception in FixedUpdate");
- }
- }
- }
- internal class Locs
- {
- private Vector3 v;
- private string n;
- public Vector3 Pos
- {
- get
- {
- return this.v;
- }
- }
- public string Name
- {
- get
- {
- return this.n;
- }
- }
- public Locs(Vector3 Pos, string Name)
- {
- this.v = Pos;
- this.n = Name;
- }
- }
- public class FeastItem
- {
- public ushort Id;
- public string Name;
- public byte Chance;
- [XmlArray(ElementName = "Locations")]
- public List<string> Location;
- public FeastItem(ushort id, string name, byte chance, List<string> locs)
- {
- this.Id = id;
- this.Name = name;
- this.Chance = chance;
- this.Location = locs;
- }
- public FeastItem()
- {
- }
- }
- public class FeastItem2
- {
- public ushort Id;
- public string Name;
- public byte Chance;
- [XmlArray(ElementName = "Locations")]
- public List<string> Location;
- public FeastItem2(ushort id, string name, byte chance, List<string> locs)
- {
- this.Id = id;
- this.Name = name;
- this.Chance = chance;
- this.Location = locs;
- }
- public FeastItem2()
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement