Advertisement
Guest User

Untitled

a guest
Apr 6th, 2016
1,774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using System.Reflection;
  7. using Rust;
  8. using Oxide.Core;
  9. using System.Collections;
  10. using System.Globalization;
  11. using System.IO;
  12. using Oxide.Core.Plugins;
  13. using ConVar;
  14.  
  15. /*
  16.     TODO:
  17.         Write some kind of reflection static class to make changing private stuff more clean
  18.  
  19. */
  20.  
  21. namespace Oxide.Plugins
  22. {
  23.     #region oxide plugin
  24.     [Info("AimTrain", "Ardivaba", 0.1)]
  25.     [Description("Git gud.")]
  26.     public class AimBots : RustPlugin
  27.     {
  28.         #region properties
  29.         public static AimBots Instance;
  30.         #endregion
  31.         #region main methods
  32.         public void Chat(string chat)
  33.         {
  34.             PrintToChat(chat);
  35.         }
  36.  
  37.         void UnlimitedAmmo(BaseProjectile projectile, BasePlayer player)
  38.         {
  39.             projectile.GetItem().condition = projectile.GetItem().info.condition.max;
  40.             projectile.primaryMagazine.contents = projectile.primaryMagazine.capacity;
  41.             projectile.SendNetworkUpdateImmediate();
  42.         }
  43.         #endregion
  44.  
  45.         #region oxide hooks
  46.         void OnWeaponFired(BaseProjectile projectile, BasePlayer player)
  47.         {
  48.             UnlimitedAmmo(projectile, player);
  49.         }
  50.  
  51.         void OnTick()
  52.         {
  53.             BaseBot[] bots = GameObject.FindObjectsOfType<BaseBot>();
  54.             if(bots.Length < 5)
  55.             {
  56.                 PlayerCorpse[] corpses = GameObject.FindObjectsOfType<PlayerCorpse>();
  57.  
  58.                 //TODO: Filter out non-bot corpses
  59.                 foreach (PlayerCorpse corpse in corpses)
  60.                 {
  61.                     //corpse.Kill(BaseNetworkable.DestroyMode.Gib);
  62.                 }
  63.  
  64.                 BotFactory.SpawnSavedBots();
  65.             }
  66.         }
  67.  
  68.         void Loaded()
  69.         {
  70.             AimBots.Instance = this;
  71.             TitleResolver.InitializeTitleResolver();
  72.             BotFactory.ReadBotsData();
  73.  
  74.             BotFactory.AddComposition("Naked", "Static", "Idle", "Naked", "Not Updated");
  75.             BotFactory.AddComposition("Naked Strafer", "Strafing", "Idle", "Naked", "Slowly Updated");
  76.         }
  77.         #endregion
  78.  
  79.         #region console commands
  80.         [ConsoleCommand("bots.data.clear")]
  81.         void CmdClearData(ConsoleSystem.Arg arg)
  82.         {
  83.             BotFactory.ClearData();
  84.         }
  85.  
  86.         [ConsoleCommand("bots.create")]
  87.         void CmdCreateSavedBot(ConsoleSystem.Arg arg)
  88.         {
  89.             Assert.Test(arg.Player() != null, "AimBots.CmdCreateSavedBot: arg.Player() is null!");
  90.             BotFactory.CreateBot(arg.Player().transform.position, "Naked");
  91.         }
  92.  
  93.         [ConsoleCommand("bots.spawn.saved")]
  94.         void CmdCreateBotsFromData(ConsoleSystem.Arg arg)
  95.         {
  96.             BotFactory.SpawnSavedBots();
  97.         }
  98.  
  99.         [ConsoleCommand("bots.clearCorpses")]
  100.         void CmdCreateBot(ConsoleSystem.Arg arg)
  101.         {
  102.             PlayerCorpse[] corpses = GameObject.FindObjectsOfType<PlayerCorpse>();
  103.  
  104.             //TODO: Filter out non-bot corpses
  105.             foreach(PlayerCorpse corpse in corpses)
  106.             {
  107.                 corpse.Kill(BaseNetworkable.DestroyMode.Gib);
  108.             }
  109.         }
  110.  
  111.         [ConsoleCommand("bots.titleResolver.debug")]
  112.         void CmdDebugTitleResolver(ConsoleSystem.Arg arg)
  113.         {
  114.             TitleResolver.TitleToType("derp");
  115.         }
  116.         #endregion
  117.     }
  118.     #endregion
  119.  
  120.     #region bot factory
  121.     public static class BotFactory
  122.     {
  123.         public static Dictionary<string, string[]> BotCompositions;
  124.  
  125.         public static void AddComposition(string name, params string[] classes)
  126.         {
  127.             if (BotCompositions.ContainsKey(name))
  128.                 return;
  129.  
  130.             BotCompositions.Add(name, classes);
  131.             WriteBotsData();
  132.         }
  133.  
  134.         public static string[] GetComposition(string name)
  135.         {
  136.             Assert.Test(BotCompositions.ContainsKey(name), "BotFactory.GetCompositions: Bot composition with name: " + name + " doesn't exist!");
  137.             return BotCompositions[name];
  138.         }
  139.  
  140.         public static List<BotData> BotDatas;
  141.  
  142.         public static void ReadBotsData()
  143.         {
  144.             BotCompositions = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<string, string[]>>("BotCompositions");
  145.             BotDatas = Interface.Oxide.DataFileSystem.ReadObject<List<BotData>>("Bots");
  146.         }
  147.  
  148.         public static void WriteBotsData()
  149.         {
  150.             Interface.Oxide.DataFileSystem.WriteObject("BotCompositions", BotCompositions);
  151.             Interface.Oxide.DataFileSystem.WriteObject("Bots", BotDatas);
  152.         }
  153.  
  154.         public static void ClearData()
  155.         {
  156.             BotCompositions.Clear();
  157.             BotDatas.Clear();
  158.             Interface.Oxide.DataFileSystem.WriteObject("Bots", BotDatas);
  159.             Interface.Oxide.DataFileSystem.WriteObject("BotCompositions", BotCompositions);
  160.         }
  161.  
  162.         public static void CreateBot(Vector3 position, string compositionName)
  163.         {
  164.             Assert.Test(position != null, "BotFactory.CreateBot: position is null!");
  165.  
  166.             BotData data = new BotData();
  167.             data.PosX = position.x;
  168.             data.PosY = position.y;
  169.             data.PosZ = position.z;
  170.  
  171.             Assert.Test(position != null, "BotFactory.CreateBot: BotDatas is null!");
  172.             data.ID = (ulong)(BotDatas.Count + 1);
  173.             data.CompositionTitle = "Naked Strafer";
  174.             BotDatas.Add(data);
  175.  
  176.             WriteBotsData();
  177.  
  178.             SpawnBot(position, data);
  179.         }
  180.  
  181.         public static BasePlayer SpawnBot(Vector3 position, BotData data)
  182.         {
  183.             var newPlayer = GameManager.server.CreateEntity("assets/prefabs/player/player.prefab", position, Quaternion.identity);
  184.  
  185.             //Set flag Reserved1 so that we can identify BasePlayers that are bots.
  186.             newPlayer.SetFlag(BaseEntity.Flags.Reserved1, true);
  187.  
  188.             var bot = newPlayer.gameObject.AddComponent<BaseBot>();
  189.             bot.Data = data;
  190.  
  191.             string name = "";
  192.             foreach(string classTitle in BotFactory.GetComposition(data.CompositionTitle))
  193.             {
  194.                 var obj = newPlayer.gameObject.AddComponent(TitleResolver.TitleToType(classTitle));
  195.                 name += (string)obj.GetType().GetField("Title").GetValue(obj) + " ";
  196.             }
  197.  
  198.             //var movement = (BaseBotMovement)newPlayer.gameObject.AddComponent(TitleResolver.TitleToType("Static"));
  199.             //var animation = (BaseBotAnimation)newPlayer.gameObject.AddComponent(TitleResolver.TitleToType("Idle"));
  200.             //var equipment = (BaseBotEquipment)newPlayer.gameObject.AddComponent(TitleResolver.TitleToType("Naked"));
  201.             //var networking = (BaseBotNetworking)newPlayer.gameObject.AddComponent(TitleResolver.TitleToType("Updated"));
  202.  
  203.             SetName(newPlayer.GetComponent<BasePlayer>(), name);
  204.  
  205.             newPlayer.Spawn(true);
  206.  
  207.             newPlayer.SendNetworkUpdate();
  208.  
  209.             BasePlayer basePlayer = (BasePlayer)newPlayer;
  210.             basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(340009023), basePlayer.inventory.containerWear);
  211.             basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(1554697726), basePlayer.inventory.containerWear);
  212.             basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-1883959124), basePlayer.inventory.containerWear);
  213.  
  214.             return newPlayer.gameObject.GetComponent<BasePlayer>();
  215.         }
  216.  
  217.         public static void SpawnSavedBots()
  218.         {
  219.             ReadBotsData();
  220.  
  221.             List<ulong> existingIDs = new List<ulong>();
  222.  
  223.             BaseBot[] bots = GameObject.FindObjectsOfType<BaseBot>();
  224.             foreach(BaseBot bot in bots)
  225.             {
  226.                 if(bot.BasePlayer.IsAlive() && !bot.BasePlayer.IsWounded())
  227.                     existingIDs.Add(bot.Data.ID);
  228.  
  229.                 if (bot.BasePlayer.IsWounded())
  230.                     bot.BasePlayer.Kill();
  231.             }
  232.  
  233.             foreach(BotData data in BotDatas)
  234.             {
  235.                 if (!existingIDs.Contains(data.ID))
  236.                 {
  237.                     SpawnBot(new Vector3(data.PosX, data.PosY, data.PosZ), data);
  238.                 }
  239.             }
  240.         }
  241.  
  242.         private static void SetName(BasePlayer player, string name)
  243.         {
  244.             player.PrivateField("_displayName").Value = name;
  245.         }
  246.     }
  247.     #endregion
  248.  
  249.     #region bot data
  250.     public class BotData
  251.     {
  252.         public ulong ID;
  253.         public float PosX;
  254.         public float PosY;
  255.         public float PosZ;
  256.         public string CompositionTitle;
  257.     }
  258.     #endregion
  259.  
  260.     #region base bot behaviour
  261.     /*
  262.         TODO: Add privates so that we have to GetComponent only once
  263.     */
  264.     public class BaseBotBehaviour : BaseMonoBehaviour
  265.     {
  266.         public bool IsDirty = false;
  267.         public BaseBotEquipment Equipment { get { return GetComponent<BaseBotEquipment>(); } }
  268.         public BaseBotMovement Movement { get { return GetComponent<BaseBotMovement>(); } }
  269.         public BaseBot Bot { get { return GetComponent<BaseBot>(); } }
  270.         public BasePlayer BasePlayer { get { return GetComponent<BasePlayer>(); } }
  271.     }
  272.     #endregion
  273.  
  274.     #region bot classes
  275.     public class BaseBot : BaseBotBehaviour
  276.     {
  277.         public BotData Data;
  278.         void Start()
  279.         {
  280.             //BasePlayer.OwnerID = Data.ID;
  281.             Equipment.Give();
  282.         }
  283.  
  284.         void FixedUpdate()
  285.         {
  286.             Movement.Move();
  287.         }
  288.     }
  289.     #endregion
  290.  
  291.     #region bot equipment classes
  292.     public class BaseBotEquipment : BaseBotBehaviour
  293.     {
  294.         public string Title = "Naked";
  295.         public void Give()
  296.         {
  297.  
  298.         }
  299.     }
  300.     #endregion
  301.  
  302.     #region bot movement classes
  303.     public class BaseBotMovement : BaseBotBehaviour
  304.     {
  305.         public string Title = "Static";
  306.  
  307.         void Start()
  308.         {
  309.         }
  310.  
  311.         public virtual void Move()
  312.         {
  313.         }
  314.  
  315.         public Vector3 FixHeight(Vector3 position)
  316.         {
  317.             position.y = TerrainMeta.HeightMap.GetHeight(position);
  318.  
  319.             return position;
  320.         }
  321.     }
  322.  
  323.     public class StrafingBotMovement : BaseBotMovement
  324.     {
  325.         public new string Title = "Strafing";
  326.  
  327.         Vector3 targetPosition;
  328.  
  329.         void Start()
  330.         {
  331.             targetPosition = transform.position;
  332.         }
  333.  
  334.         public void FixedUpdate()
  335.         {
  336.             transform.position = Vector3.MoveTowards(transform.position, targetPosition, 2.5f * UnityEngine.Time.fixedDeltaTime);
  337.  
  338.             if(Vector3.Distance(transform.position, targetPosition) < 0.5f)
  339.             {
  340.                 //targetPosition = transform.position + transform.right * UnityEngine.Random.Range(-5.0f, 5.0f);
  341.             }
  342.  
  343.             IsDirty = true;
  344.  
  345.             transform.position = FixHeight(transform.position);
  346.         }
  347.     }
  348.     #endregion
  349.  
  350.     #region bot networking classes
  351.     public class BaseBotNetworking : BaseBotBehaviour
  352.     {
  353.         public string Title = "Not Updated";
  354.  
  355.         public virtual void FixedUpdate()
  356.         {
  357.         }
  358.     }
  359.  
  360.     public class UpdatedBotNetworking : BaseBotNetworking
  361.     {
  362.         public new string Title = "Updated";
  363.         public override void FixedUpdate()
  364.         {
  365.             if (IsDirty)
  366.             {
  367.                 BasePlayer.SendNetworkUpdate(BasePlayer.NetworkQueue.Update);
  368.                 IsDirty = false;
  369.             }
  370.         }
  371.     }
  372.  
  373.     public class SlowlyUpdatedBotNetworking : BaseBotNetworking
  374.     {
  375.         public new string Title = "Slowly Updated";
  376.  
  377.         float TimeLeft = 0.0f;
  378.         public override void FixedUpdate()
  379.         {
  380.             if (TimeLeft <= 0.0f)
  381.             {
  382.                 BasePlayer.SendNetworkUpdate(BasePlayer.NetworkQueue.Update);
  383.                 IsDirty = false;
  384.                 TimeLeft = 0.5f;
  385.             }
  386.  
  387.             TimeLeft -= UnityEngine.Time.fixedDeltaTime;
  388.         }
  389.     }
  390.     #endregion
  391.  
  392.     #region bot animation classes
  393.     public class BaseBotAnimation : BaseBotBehaviour
  394.     {
  395.         public string Title = "Idle";
  396.  
  397.         ModelState modelState;
  398.         void Start()
  399.         {
  400.             modelState = GetModelState();
  401.             modelState.sprinting = true;
  402.  
  403.             SetDefaultAnimationState();
  404.         }
  405.  
  406.         ModelState GetModelState()
  407.         {
  408.             return (ModelState)BasePlayer.PrivateField("modelState").Value;
  409.         }
  410.  
  411.         void SetDefaultAnimationState()
  412.         {
  413.             modelState.onground = true;
  414.         }
  415.     }
  416.     #endregion
  417.  
  418.     #region helper classes
  419.     #region reflection helpers
  420.     public static class ReflectionEx
  421.     {
  422.         public class PrivateFieldData
  423.         {
  424.             public object ObjectInstance;
  425.             public FieldInfo FieldInfo;
  426.             public object Value
  427.             {
  428.                 get
  429.                 {
  430.                     return FieldInfo.GetValue(ObjectInstance);
  431.                 }
  432.  
  433.                 set
  434.                 {
  435.                     FieldInfo.SetValue(ObjectInstance, value);
  436.                 }
  437.             }
  438.         }
  439.  
  440.         public static PrivateFieldData PrivateField(this object obj, string name)
  441.         {
  442.             PrivateFieldData data = new PrivateFieldData
  443.             {
  444.                 ObjectInstance = obj,
  445.                 FieldInfo = obj.GetType().GetField(name, BindingFlags.NonPublic | BindingFlags.Instance)
  446.             };
  447.  
  448.             return data;
  449.         }
  450.     }
  451.     #endregion
  452.  
  453.     #region title resolver
  454.     public static class TitleResolver
  455.     {
  456.         public static Dictionary<string, Type> StringsToType;
  457.         public static Dictionary<Type, string> TypesToString;
  458.  
  459.         public static void InitializeTitleResolver()
  460.         {
  461.             StringsToType = new Dictionary<string, Type>();
  462.             TypesToString = new Dictionary<Type, string>();
  463.  
  464.             List<string> titles = new List<string>();
  465.  
  466.             var listOfBotBehaviours = (from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
  467.                                        from assemblyType in domainAssembly.GetTypes()
  468.                                        where typeof(BaseBotBehaviour).IsAssignableFrom(assemblyType)
  469.                                        select assemblyType).ToArray();
  470.  
  471.             foreach (Type behaviour in listOfBotBehaviours)
  472.             {
  473.                 var titleField = behaviour.GetField("Title");
  474.                 if (titleField != null)
  475.                 {
  476.                     string title = (string)titleField.GetValue(Activator.CreateInstance(behaviour));
  477.  
  478.                     StringsToType.Add(title, behaviour);
  479.                     TypesToString.Add(behaviour, title);
  480.                 }
  481.             }
  482.         }
  483.  
  484.         public static Type TitleToType(string title)
  485.         {
  486.             return StringsToType[title];
  487.         }
  488.  
  489.         public static string TypeToTitle(Type type)
  490.         {
  491.             return TypesToString[type];
  492.         }
  493.     }
  494.     #endregion
  495.     #endregion
  496. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement