Advertisement
Xyberviri

AirdropControl.cs

Mar 29th, 2015
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.31 KB | None | 0 0
  1. // Reference: Oxide.Ext.Rust
  2.  
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System;
  6. using System.Data;
  7. using UnityEngine;
  8. using Oxide.Core;
  9.  
  10. namespace Oxide.Plugins
  11. {
  12.     [Info("AirdropControl", "Reneb", "1.1.5")]
  13.     class AirdropControl : RustPlugin
  14.     {
  15.         private FieldInfo CPstartPos;
  16.         private FieldInfo CPendPos;
  17.         private FieldInfo CPdropped;
  18.         private FieldInfo CPsecondsToTake;
  19.         private FieldInfo CPsecondsTaken;
  20.         private FieldInfo dropPosition;
  21.         private MethodInfo CreateEntity;
  22.         private Vector3 centerPos;
  23.         private float secondsToTake;
  24.         private BaseEntity cargoplane;
  25.         private Dictionary<CargoPlane, Vector3> dropPoint;
  26.         private Dictionary<CargoPlane, int> dropNumber;
  27.         private Dictionary<CargoPlane, double> nextDrop;
  28.         private static readonly DateTime epoch = new DateTime(1970, 1, 1);
  29.         private float dropMinX;
  30.         private float dropMaxX;
  31.         private float dropMinY;
  32.         private float dropMaxY;
  33.         private float dropMinZ;
  34.         private float dropMaxZ;
  35.         private string dropMessage;
  36.         private int dropMinCrates;
  37.         private int dropMaxCrates;
  38.         private float airdropSpeed;
  39.         private bool showDropLocation;
  40.         private bool Changed;
  41.         private System.Random getrandom;
  42.         private object syncLock;
  43.         private int minDropCratesInterval;
  44.         private int maxDropCratesInterval;
  45.         private double nextCheck;
  46.         private List<CargoPlane> RemoveListND;
  47.         private Dictionary<CargoPlane, int> RemoveListNUM;
  48.         private Dictionary<CargoPlane,double> AddDrop;
  49.         private Quaternion defaultRot = new Quaternion(1f,0f,0f,0f);
  50.  
  51.         void Loaded()
  52.         {
  53.             RemoveListND = new List<CargoPlane>();
  54.             RemoveListNUM = new Dictionary<CargoPlane, int>();
  55.             AddDrop = new Dictionary<CargoPlane, double>();
  56.             getrandom = new System.Random();
  57.             syncLock = new object();
  58.             centerPos = new UnityEngine.Vector3(0f, 0f, 0f);
  59.             dropPoint = new Dictionary<CargoPlane, Vector3>();
  60.             dropNumber = new Dictionary<CargoPlane, int>();
  61.             nextDrop = new Dictionary<CargoPlane, double>();
  62.             dropPosition = typeof(CargoPlane).GetField("dropPosition", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));
  63.             CPstartPos = typeof(CargoPlane).GetField("startPos", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));
  64.             CPendPos = typeof(CargoPlane).GetField("endPos", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));
  65.             CPdropped = typeof(CargoPlane).GetField("dropped", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));
  66.             CPsecondsToTake = typeof(CargoPlane).GetField("secondsToTake", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));
  67.             CPsecondsTaken = typeof(CargoPlane).GetField("secondsTaken", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));
  68.             LoadVariables();
  69.         }
  70.         object GetConfig(string menu, string datavalue, object defaultValue)
  71.         {
  72.             var data = Config[menu] as Dictionary<string, object>;
  73.             if (data == null)
  74.             {
  75.                 data = new Dictionary<string, object>();
  76.                 Config[menu] = data;
  77.                 Changed = true;
  78.             }
  79.             object value;
  80.             if (!data.TryGetValue(datavalue, out value))
  81.             {
  82.                 value = defaultValue;
  83.                 data[datavalue] = value;
  84.                 Changed = true;
  85.             }
  86.             return value;
  87.         }
  88.         void LoadVariables()
  89.         {
  90.             dropMinX = Convert.ToSingle(GetConfig("Drop", "MinX", -((World.Size/2) - 500)));
  91.             dropMaxX = Convert.ToSingle(GetConfig("Drop", "MaxX", ((World.Size / 2) - 500)));
  92.             dropMinZ = Convert.ToSingle(GetConfig("Drop", "MinZ", -((World.Size / 2) - 500)));
  93.             dropMaxZ = Convert.ToSingle(GetConfig("Drop", "MaxZ", ((World.Size / 2) - 500)));
  94.             dropMinY = Convert.ToSingle(GetConfig("Drop", "MinY", 200f));
  95.             dropMaxY = Convert.ToSingle(GetConfig("Drop", "MaxY", 300f));
  96.             dropMinCrates = Convert.ToInt32(GetConfig("Drop", "MinCrates", 1));
  97.             dropMaxCrates = Convert.ToInt32(GetConfig("Drop", "MaxCrates", 3));
  98.             minDropCratesInterval = Convert.ToInt32(GetConfig("Drop", "MinDropCratesInterval", 3));
  99.             maxDropCratesInterval = Convert.ToInt32(GetConfig("Drop", "MaxDropCratesInterval", 10));
  100.             showDropLocation = Convert.ToBoolean(GetConfig("Drop", "ShowDropLocation", true));
  101.             airdropSpeed = Convert.ToSingle(GetConfig("Airdrop", "Speed", 40f));
  102.             dropMessage = Convert.ToString(GetConfig("Messages","Inbound","Airdrop incoming! Dropping at {0} {1} {2}"));
  103.             if (Changed)
  104.             {
  105.                 SaveConfig();
  106.                 Changed = false;
  107.             }
  108.         }
  109.         void LoadDefaultConfig()
  110.         {
  111.             Puts("Airdrop Control: Creating a new config file");
  112.             Config.Clear(); // force clean new config
  113.             LoadVariables();
  114.         }
  115.         int GetRandomNumber(int min, int max)
  116.         {
  117.             return getrandom.Next(min, max);
  118.         }
  119.         double CurrentTime()
  120.         {
  121.             return System.DateTime.UtcNow.Subtract(epoch).TotalSeconds;
  122.         }
  123.         Vector3 FindDropPoint(CargoPlane cargoplane)
  124.         {
  125.             return (Vector3)dropPosition.GetValue(cargoplane);
  126.         }
  127.         int RandomCrateDrop(CargoPlane cargoplane)
  128.         {
  129.             return GetRandomNumber(dropMinCrates, dropMaxCrates+1);
  130.         }
  131.         double RandomDropInterval()
  132.         {
  133.             return Convert.ToDouble(GetRandomNumber(minDropCratesInterval, maxDropCratesInterval + 1));
  134.         }
  135.         Vector3 RandomDropPoint()
  136.         {
  137.             var RandomX = Convert.ToSingle(GetRandomNumber((int)dropMinX, (int)dropMaxX+1));
  138.             var RandomY = Convert.ToSingle(GetRandomNumber((int)dropMinY, (int)dropMaxY+1));
  139.             var RandomZ = Convert.ToSingle(GetRandomNumber((int)dropMinZ, (int)dropMaxZ+1));
  140.             if (RandomX == 0f)
  141.                 RandomX = 1f;
  142.             if (RandomZ == 0f)
  143.                 RandomZ = 1f;
  144.             return new UnityEngine.Vector3(RandomX, RandomY, RandomZ);
  145.         }
  146.         void FindStartAndEndPos(Vector3 target, out Vector3 startpos, out Vector3 endpos, out float distance)
  147.         {
  148.             var directionFromCenter = (target - centerPos).normalized;
  149.             var directionAngles = Quaternion.LookRotation( directionFromCenter );
  150.             var toRight = directionAngles * Vector3.right;
  151.             var toLeft = directionAngles * Vector3.left;
  152.             startpos = target;
  153.             var multiplier = 1000f;
  154.             var i = 0f;
  155.             for(int o=0;o<50;o++)
  156.             {
  157.                 var temPos = startpos + toRight * i * multiplier;
  158.                 if (((float)Math.Abs(temPos.x + multiplier) > (World.Size / 2)) || ((float)Math.Abs(temPos.x - multiplier) > (World.Size / 2)) || ((float)Math.Abs(temPos.z - multiplier) > (World.Size / 2)) || ((float)Math.Abs(temPos.z + multiplier) > (World.Size / 2)))
  159.                 {
  160.                     multiplier = multiplier / 10f;
  161.                     i = 0f;
  162.                 }
  163.                 else
  164.                 {
  165.                     temPos.y = startpos.y;
  166.                     startpos = temPos;
  167.                     i = i + 1f;
  168.                 }
  169.                 if (multiplier < 1f)
  170.                 {
  171.                     break;
  172.                 }
  173.             }
  174.             distance = Vector3.Distance(startpos, target);
  175.             endpos = target - toRight * distance;
  176.         }
  177.         void BroadcastToChat(string msg)
  178.         {
  179.             ConsoleSystem.Broadcast("chat.add \"SERVER\" " + msg.QuoteSafe() + " 1.0", new object[0]);
  180.         }
  181.         void OnTick()
  182.         {
  183.             if (CurrentTime() >= nextCheck)
  184.             {
  185.                 var currentTime = CurrentTime();
  186.                 if (nextDrop.Count > 0)
  187.                 {
  188.                     foreach (KeyValuePair<CargoPlane, double> entry in nextDrop)
  189.                     {
  190.                         if (entry.Value >= currentTime)
  191.                         {
  192.                             CPdropped.SetValue(entry.Key, false);
  193.                             RemoveListND.Add(entry.Key as CargoPlane);
  194.                         }
  195.                     }
  196.                     foreach (CargoPlane cp in RemoveListND)
  197.                     {
  198.                         nextDrop.Remove(cp);
  199.                     }
  200.                     RemoveListND.Clear();
  201.                 }
  202.                 nextCheck = currentTime + 1;
  203.             }
  204.         }
  205.         void CheckAirdropDrops()
  206.         {
  207.             foreach(KeyValuePair<CargoPlane, int> entry in dropNumber)
  208.             {
  209.                 if((bool)CPdropped.GetValue(entry.Key))
  210.                 {
  211.                     if(entry.Value > 1)
  212.                     {
  213.                         if (!(nextDrop.ContainsKey(entry.Key)))
  214.                         {
  215.                             AddDrop.Add(entry.Key, RandomDropInterval() + CurrentTime());
  216.                             RemoveListNUM.Add(entry.Key as CargoPlane, entry.Value - 1);
  217.                         }
  218.                     }
  219.                 }
  220.             }
  221.             foreach (KeyValuePair<CargoPlane, double> entry in AddDrop)
  222.             {
  223.                 nextDrop.Add(entry.Key, entry.Value);
  224.             }
  225.             AddDrop.Clear();
  226.             foreach (KeyValuePair<CargoPlane, int> entry in RemoveListNUM)
  227.             {
  228.                 if (entry.Value <= 0)
  229.                     dropNumber.Remove(entry.Key);
  230.                 else
  231.                     dropNumber[entry.Key] = entry.Value;
  232.             }
  233.             RemoveListNUM.Clear();
  234.         }
  235.         void OnEntitySpawned(BaseEntity entity)
  236.         {
  237.             if(entity != null)
  238.             {
  239.                 if (entity is CargoPlane)
  240.                 {
  241.                     var cargoplane = entity as CargoPlane;
  242.                     Vector3 startPos;
  243.                     Vector3 endPos;
  244.                     float distance;
  245.                    
  246.                     var dropTarget = FindDropPoint(cargoplane);
  247.                     if (showDropLocation)
  248.                     {
  249.                         BroadcastToChat(string.Format(dropMessage, dropTarget.x.ToString(), dropTarget.y.ToString(), dropTarget.z.ToString()));
  250.                     }
  251.                     Puts("Airdrop setting to drop at : " + dropTarget.ToString());
  252.                    
  253.                    
  254.                    
  255.                     dropNumber.Add(cargoplane, RandomCrateDrop(cargoplane));
  256.                 }
  257.                 else if(entity is SupplyDrop)
  258.                 {
  259.                     CheckAirdropDrops();
  260.                 }
  261.             }
  262.         }
  263.         void AllowNextDrop()
  264.         {
  265.             Interface.GetMod().CallHook("AllowDrop", new object[0] {});
  266.         }
  267.         [ConsoleCommand("airdrop.toplayer")]
  268.         void cmdConsoleAirdropToPlayer(ConsoleSystem.Arg arg)
  269.         {
  270.             if (arg.connection != null)
  271.             {
  272.                 if (arg.connection.authLevel < 1)
  273.                 {
  274.                     SendReply(arg, "You are not allowed to use this command");
  275.                     return;
  276.                 }
  277.             }
  278.             if (arg.Args == null ||  arg.Args.Length < 1)
  279.             {
  280.                 SendReply(arg, "You must select a player to check");
  281.                 return;
  282.             }
  283.             var target = BasePlayer.Find(arg.Args[0].ToString());
  284.             if (target == null || target.net == null || target.net.connection == null)
  285.             {
  286.                 SendReply(arg, "Target player not found");
  287.             }
  288.             else
  289.             {
  290.                 AllowNextDrop();
  291.                 BaseEntity entity = GameManager.server.CreateEntity("events/cargo_plane", new Vector3(), defaultRot);
  292.                 if (entity != null)
  293.                 {
  294.                     var targetPos = target.transform.position;
  295.                     targetPos.y = Convert.ToSingle(GetRandomNumber((int)dropMinY, (int)dropMaxY + 1));
  296.                     CargoPlane plane = entity.GetComponent<CargoPlane>();
  297.                     plane.InitDropPosition(targetPos);
  298.                     entity.Spawn(true);
  299.                    
  300.                     CPsecondsToTake.SetValue(plane, Vector3.Distance( (Vector3)CPendPos.GetValue(plane), (Vector3)CPstartPos.GetValue(plane) ) / airdropSpeed );
  301.                 }
  302.             }
  303.         }
  304.         [ConsoleCommand("airdrop.topos")]
  305.         void cmdConsoleAirdropToPos(ConsoleSystem.Arg arg)
  306.         {
  307.             if (arg.connection != null)
  308.             {
  309.                 if (arg.connection.authLevel < 1)
  310.                 {
  311.                     SendReply(arg, "You are not allowed to use this command");
  312.                     return;
  313.                 }
  314.             }
  315.             if (arg.Args == null || arg.Args.Length < 3)
  316.             {
  317.                 SendReply(arg, "You must give coordinates of destination ex: airdrop.topos 124 200 -453");
  318.                 return;
  319.             }
  320.             AllowNextDrop();
  321.             BaseEntity entity = GameManager.server.CreateEntity("events/cargo_plane", new Vector3(), defaultRot);
  322.             if (entity != null)
  323.             {
  324.                 var targetPos = new Vector3();
  325.                 targetPos.x = Convert.ToSingle(arg.Args[0]);
  326.                 targetPos.y = Convert.ToSingle(arg.Args[1]);
  327.                 targetPos.z = Convert.ToSingle(arg.Args[2]);
  328.                 CargoPlane plane = entity.GetComponent<CargoPlane>();
  329.                 plane.InitDropPosition(targetPos);
  330.                 entity.Spawn(true);
  331.                
  332.                 CPsecondsToTake.SetValue(plane, Vector3.Distance( (Vector3)CPendPos.GetValue(plane), (Vector3)CPstartPos.GetValue(plane) ) / airdropSpeed );
  333.             }
  334.         }
  335.         [ConsoleCommand("airdrop.massdrop")]
  336.         void cmdConsoleAirdropMassDrop(ConsoleSystem.Arg arg)
  337.         {
  338.             if (arg.connection != null)
  339.             {
  340.                 if (arg.connection.authLevel < 1)
  341.                 {
  342.                     SendReply(arg, "You are not allowed to use this command");
  343.                     return;
  344.                 }
  345.             }
  346.             if (arg.Args == null || arg.Args.Length < 1)
  347.             {
  348.                 SendReply(arg, "You must select the number of airdrops that you want");
  349.                 return;
  350.             }
  351.             for (int i = 0; i < Convert.ToInt32(arg.Args[0]); i++)
  352.             {
  353.                 AllowNextDrop();
  354.                 Vector3 dropposition = RandomDropPoint();
  355.                 BaseEntity entity = GameManager.server.CreateEntity("events/cargo_plane", new Vector3(), defaultRot);
  356.                 if (entity != null)
  357.                 {
  358.                     CargoPlane plane = entity.GetComponent<CargoPlane>();
  359.                     plane.InitDropPosition( dropposition );
  360.                     entity.Spawn(true);
  361.                     CPsecondsToTake.SetValue(plane, Vector3.Distance( (Vector3)CPendPos.GetValue(plane), (Vector3)CPstartPos.GetValue(plane) ) / airdropSpeed );
  362.                 }
  363.             }
  364.         }
  365.     }
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement