Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.23 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.  
  8. using Rust;
  9.  
  10. using Network.Visibility;
  11. using Oxide.Core;
  12. using Rust;
  13. using System.Collections;
  14. using System.Collections.Generic;
  15. using System.Globalization;
  16. using System.IO;
  17. using Oxide.Core.Plugins;
  18.  
  19. namespace Oxide.Plugins
  20. {
  21.     #region zone
  22.     public class Zone
  23.     {
  24.         public List<int> Bots;
  25.  
  26.         public virtual void Start()
  27.         {
  28.  
  29.         }
  30.     }
  31.     #endregion
  32.  
  33.     #region bot
  34.     public class Bot
  35.     {
  36.         public ulong Id;
  37.         public int ZoneId;
  38.  
  39.         public float PosX;
  40.         public float PosY;
  41.         public float PosZ;
  42.  
  43.         private BasePlayer BasePlayer;
  44.  
  45.         public Bot(int zone, ulong id, Vector3 position)
  46.         {
  47.             Id = id;
  48.  
  49.             PosX = position.x;
  50.             PosY = position.y;
  51.             PosZ = position.z;
  52.         }
  53.  
  54.         #region main methods
  55.         public void Spawn()
  56.         {
  57.             AimTrain.Instance.Chat("Spawning!");
  58.  
  59.             var newPlayer = GameManager.server.CreateEntity("assets/prefabs/player/player.prefab", GetPos(), Quaternion.identity);
  60.             newPlayer.Spawn();
  61.  
  62.             newPlayer.SetFlag(BaseEntity.Flags.Reserved1, true);
  63.             FieldInfo modelStateField = typeof(BasePlayer).GetField("modelState", BindingFlags.Instance | BindingFlags.NonPublic);
  64.             object modelState = modelStateField.GetValue(newPlayer);
  65.             modelState.GetType().GetProperty("onground").SetValue(modelState, true, null);
  66.  
  67.             newPlayer.SendNetworkUpdate();
  68.  
  69.             BasePlayer = newPlayer.GetComponent<BasePlayer>();
  70.             BasePlayer.userID = Id;
  71.  
  72.             AimTrain.Instance.Chat("Spawned!");
  73.         }
  74.  
  75.         public void Kill()
  76.         {
  77.             if (BasePlayer != null)
  78.             {
  79.                 BasePlayer.Kill();
  80.                 BasePlayer.SendNetworkUpdate();
  81.             }
  82.         }
  83.         #endregion
  84.  
  85.         #region utility methods
  86.         Vector3 GetPos()
  87.         {
  88.             return new Vector3(PosX, PosY, PosZ);
  89.         }
  90.         #endregion
  91.     }
  92.     #endregion
  93.  
  94.     #region plugin
  95.     [Info("AimTrain", "Ardivaba", 0.1)]
  96.     [Description("Mkes you Trauzillz")]
  97.     public class AimTrain : RustPlugin
  98.     {
  99.         #region data
  100.         public static AimTrain Instance;
  101.         public static HashSet<Bot> Bots;
  102.         public static bool Moving = true;
  103.         public static bool DEBUG_MSG = false;
  104.  
  105.         public static Vector3 Vector3Down;
  106.         public static int groundLayer;
  107.  
  108.         public static readonly FieldInfo viewangles = typeof(BasePlayer).GetField("viewAngles", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));
  109.  
  110.         #endregion
  111.  
  112.         public static void msg(String message)
  113.         {
  114.             if (DEBUG_MSG)
  115.             {
  116.                 Instance.Puts(message);
  117.             }
  118.         }
  119.  
  120.         #region public static main methods
  121.         public static void CreateBot(Vector3 position)
  122.         {
  123.             Bot bot = new Bot(0, (ulong)Bots.Count, position);
  124.  
  125.             bot.Spawn();
  126.  
  127.             Bots.Add(bot);
  128.  
  129.             AimTrain.SaveBots();
  130.         }
  131.  
  132.         public static void LoadBots()
  133.         {
  134.             Bots = Interface.Oxide.DataFileSystem.ReadObject<HashSet<Bot>>("Bots");
  135.         }
  136.  
  137.         public static void SaveBots()
  138.         {
  139.             Interface.Oxide.DataFileSystem.WriteObject<HashSet<Bot>>("Bots", Bots, true);
  140.         }
  141.         #endregion
  142.  
  143.         #region main methods
  144.         public void Chat(string chat)
  145.         {
  146.             PrintToChat(chat);
  147.         }
  148.  
  149.         void UnlimitedAmmo(BaseProjectile projectile, BasePlayer player)
  150.         {
  151.             projectile.GetItem().condition = projectile.GetItem().info.condition.max;
  152.             projectile.primaryMagazine.contents = projectile.primaryMagazine.capacity;
  153.             projectile.SendNetworkUpdateImmediate();
  154.         }
  155.         #endregion
  156.  
  157.         private float lastCorpseCheck;
  158.  
  159.         #region oxide hooks
  160.         void OnFrame()
  161.         {
  162.             //AutoClean corpses every 30s
  163.             float corspeTimeDiffS = Time.time - lastCorpseCheck;
  164.             if (corspeTimeDiffS > 30.0f)
  165.             {
  166.                 lastCorpseCheck = Time.time;
  167.                 AimTrain.msg($"Cleaning up corpses diff = {corspeTimeDiffS}");
  168.                 BaseCorpse[] corpses = GameObject.FindObjectsOfType<BaseCorpse>();
  169.                 foreach (BaseCorpse corpse in corpses)
  170.                 {
  171.                     corpse.Kill();
  172.                     corpse.SendNetworkUpdate();
  173.                 }
  174.             }
  175.  
  176.             StashContainer[] stashes = GameObject.FindObjectsOfType<StashContainer>();
  177.             BasePlayer[] players = GameObject.FindObjectsOfType<BasePlayer>();
  178.  
  179.             //Puts($"players={players}");
  180.  
  181.             int botCount = 0;
  182.             foreach (BasePlayer player in players)
  183.             {
  184.                 if (player.IsWounded() && player.HasFlag(BaseEntity.Flags.Reserved1))
  185.                 {
  186.                     player.Kill();
  187.                     player.SendNetworkUpdate();
  188.                     headShots++;
  189.                 }
  190.                 if (player.HasFlag(BaseEntity.Flags.Reserved1))
  191.                 {
  192.                     botCount++;
  193.                 }
  194.             }
  195.  
  196.             int stashCount = stashes.Count();
  197.  
  198.             if (botCount < botCounts)
  199.             {
  200.                 if (stashes.Length > 0)
  201.                 {
  202.                     //Puts($"OnFrame: Bot count = {botCount} / {botCounts} stashes = {stashCount}");
  203.                     StashContainer randomStash = stashes[UnityEngine.Random.Range(0, stashes.Length - 1)];
  204.                     SpawnBot(randomStash.transform.position);
  205.                 }
  206.             }
  207.         }
  208.  
  209.         void Loaded()
  210.         {
  211.             AimTrain.Instance = this;
  212.             AimTrain.LoadBots();
  213.             OnLoadedClearBots();
  214.             lastCorpseCheck = Time.time;
  215.             AimTrain.Vector3Down = new Vector3(0f, -1f, 0f);
  216.             groundLayer = LayerMask.GetMask("Construction", "Terrain", "World");
  217.         }
  218.  
  219.         void OnLoadedClearBots()
  220.         {
  221.             AimTrain.msg("Loaded, clearing bots..");
  222.  
  223.             BasePlayer[] players = GameObject.FindObjectsOfType<BasePlayer>();
  224.             foreach (BasePlayer player in players)
  225.             {
  226.                 if (player.HasFlag(BaseEntity.Flags.Reserved1))
  227.                 {
  228.                     player.Kill();
  229.                     player.SendNetworkUpdate();
  230.                 }
  231.             }
  232.  
  233.             BaseCorpse[] corpses = GameObject.FindObjectsOfType<BaseCorpse>();
  234.             foreach (BaseCorpse corpse in corpses)
  235.             {
  236.                 corpse.Kill();
  237.                 corpse.SendNetworkUpdate();
  238.             }
  239.         }
  240.  
  241.         void OnWeaponFired(BaseProjectile projectile, BasePlayer player)
  242.         {
  243.             UnlimitedAmmo(projectile, player);
  244.         }
  245.  
  246.         int headShots = 0;
  247.         void OnPlayerAttack(BasePlayer attacker, HitInfo info)
  248.         {
  249.             return;
  250.             BasePlayer victim = info.HitEntity.GetComponent<BasePlayer>();
  251.             if (victim != null)
  252.             {
  253.                 if (victim.HasFlag(BaseEntity.Flags.Reserved1))
  254.                 {
  255.                     if (info.isHeadshot)
  256.                     {
  257.                         //headShots++;
  258.                     }
  259.                 }
  260.             }
  261.  
  262.             if (headShots == 20)
  263.             {
  264.                 PrintToChat("Headshot benchmark time: " + (DateTime.Now - startTime).TotalSeconds);
  265.                 headShots = 0;
  266.                 foreach (Bot bot in Bots)
  267.                 {
  268.                     bot.Kill();
  269.                 }
  270.             }
  271.         }
  272.         #endregion
  273.  
  274.         int botCounts = 1;
  275.         #region commands
  276.         [ConsoleCommand("botCount")]
  277.         void CmdBotCount(ConsoleSystem.Arg arg)
  278.         {
  279.             OnLoadedClearBots();
  280.  
  281.             botCounts = int.Parse(arg.Args[0]);
  282.             PrintToChat("Bot count is now: " + botCounts);
  283.         }
  284.  
  285.         [ConsoleCommand("createBot")]
  286.         void CmdCreateBot(ConsoleSystem.Arg arg)
  287.         {
  288.             AimTrain.CreateBot(arg.Player().transform.position);
  289.             PrintToChat("Where them bots at?");
  290.         }
  291.  
  292.         [ConsoleCommand("killBots")]
  293.         void CmdKillBots(ConsoleSystem.Arg arg)
  294.         {
  295.             PrintToChat("There are currently " + Bots.Count + " Bots");
  296.  
  297.             foreach (Bot bot in Bots)
  298.             {
  299.                 bot.Kill();
  300.             }
  301.         }
  302.  
  303.         [ConsoleCommand("stashes")]
  304.         void CmdStashes(ConsoleSystem.Arg arg)
  305.         {
  306.             StashContainer[] stashes = GameObject.FindObjectsOfType<StashContainer>();
  307.             foreach (StashContainer stash in stashes)
  308.             {
  309.                 stash.Kill();
  310.                 stash.SendNetworkUpdate();
  311.             }
  312.         }
  313.  
  314.         [ConsoleCommand("moving")]
  315.         void CmdMoving(ConsoleSystem.Arg arg)
  316.         {
  317.             AimTrain.Moving = !AimTrain.Moving;
  318.         }
  319.  
  320.         DateTime startTime;
  321.  
  322.         [ConsoleCommand("kcmo")]
  323.         void CmdKcmo(ConsoleSystem.Arg arg)
  324.         {
  325.             headShots = 0;
  326.             startTime = DateTime.Now;
  327.  
  328.             BasePlayer[] players = GameObject.FindObjectsOfType<BasePlayer>();
  329.             foreach (BasePlayer player in players)
  330.             {
  331.                 if (player.HasFlag(BaseEntity.Flags.Reserved1))
  332.                 {
  333.                     player.Kill();
  334.                     player.SendNetworkUpdate();
  335.                 }
  336.             }
  337.  
  338.             BaseCorpse[] corpses = GameObject.FindObjectsOfType<BaseCorpse>();
  339.             foreach (BaseCorpse corpse in corpses)
  340.             {
  341.                 corpse.Kill();
  342.                 corpse.SendNetworkUpdate();
  343.             }
  344.         }
  345.  
  346.         void SpawnBot(Vector3 position)
  347.         {
  348.             //AimTrain.Instance.Chat("Spawning!");
  349.             BasePlayer newPlayer = (BasePlayer)GameManager.server.CreateEntity("assets/prefabs/player/player.prefab", position, Quaternion.identity);
  350.             newPlayer.Spawn();
  351.             newPlayer.gameObject.AddComponent<BotMover>();
  352.             newPlayer.SetFlag(BaseEntity.Flags.Reserved1, true);
  353.  
  354.             AimTrain.msg($"Spawnbot: modelState = {newPlayer.modelState} onground={newPlayer.modelState.onground}");
  355.             newPlayer.modelState.onground = true;
  356.         }
  357.         #endregion
  358.     }
  359.     #endregion
  360.  
  361.     #region
  362.     public class BotMover : MonoBehaviour
  363.     {
  364.         BasePlayer basePlayer;
  365.         Vector3 startPosition;
  366.         Vector3 targetPosition;
  367.  
  368.         //Rust defaults = 2.4 (walk) 5.5 (run)
  369.         float maxSpeed = 7f;
  370.         float minSpeed = 3.5f;
  371.  
  372.         Boolean _isLerping;
  373.         Vector3 startPos;
  374.         Vector3 endPos;
  375.         float timeTakenDuringLerp = 0f;
  376.  
  377.  
  378.         public void LookTowards(Vector3 pos)
  379.         {
  380.             if (pos != basePlayer.transform.position)
  381.                 SetViewAngle(Quaternion.LookRotation(pos - basePlayer.transform.position));
  382.         }
  383.  
  384.         public void SetViewAngle(Quaternion viewAngles)
  385.         {
  386.             if (viewAngles.eulerAngles == default(Vector3))
  387.             {
  388.                 return;
  389.             }
  390.  
  391.             AimTrain.msg("Facing..");
  392.  
  393.             AimTrain.viewangles.SetValue(basePlayer, viewAngles.eulerAngles);
  394.             basePlayer.SendNetworkUpdate(BasePlayer.NetworkQueue.Update);
  395.         }
  396.  
  397.         void Start()
  398.         {
  399.             //AimTrain.Instance.Chat("Started!");
  400.  
  401.             basePlayer = GetComponent<BasePlayer>();
  402.             startPosition = transform.position;
  403.             targetPosition = startPosition;
  404.  
  405.             basePlayer.ChangeHealth(100.0f);
  406.             basePlayer.SendNetworkUpdate();
  407.  
  408.             int random = UnityEngine.Random.Range(0, 3);
  409.             int clothes = UnityEngine.Random.Range(0, 2);
  410.  
  411.             if (random == 0)//Metal
  412.             {
  413.                 basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-46848560), basePlayer.inventory.containerWear);
  414.                 basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(1265861812), basePlayer.inventory.containerWear);
  415.                 basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-1595790889), basePlayer.inventory.containerWear);
  416.             }
  417.             else if (random == 1)
  418.             {
  419.                 int random2 = UnityEngine.Random.Range(0, 2);
  420.                 if (random2 == 0)
  421.                 {
  422.                     basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(1260209393), basePlayer.inventory.containerWear);
  423.                 }
  424.                 else if (random2 == 1)
  425.                 {
  426.                     basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-2128719593), basePlayer.inventory.containerWear);
  427.                 }
  428.  
  429.                 basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-288010497), basePlayer.inventory.containerWear);
  430.                 basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-1595790889), basePlayer.inventory.containerWear);
  431.             }
  432.  
  433.             if (clothes == 0)
  434.             {
  435.                 basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(115739308), basePlayer.inventory.containerWear);
  436.                 basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(707427396), basePlayer.inventory.containerWear);
  437.                 basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(1767561705), basePlayer.inventory.containerWear);
  438.             }
  439.             if (clothes == 1)
  440.             {
  441.                 basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(106433500), basePlayer.inventory.containerWear);
  442.                 basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-1211618504), basePlayer.inventory.containerWear);
  443.                 basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(115739308), basePlayer.inventory.containerWear);
  444.             }
  445.  
  446.             StartLerping();
  447.         }
  448.        
  449.         int getStashCount()
  450.         {
  451.             List<StashContainer> stashList = new List<StashContainer>(GameObject.FindObjectsOfType<StashContainer>());
  452.             return stashList.Count;
  453.         }
  454.  
  455.         void StartLerping()
  456.         {
  457.             List<StashContainer> stashList = new List<StashContainer>(GameObject.FindObjectsOfType<StashContainer>());
  458.  
  459.             if (stashList.Count <= 1)
  460.             {
  461.                 //Only one stash, so do nothing...
  462.                 _isLerping = false;
  463.                 return;
  464.             }
  465.  
  466.             AimTrain.msg($"StashCount={stashList.Count}");
  467.  
  468.             //Remove the closest stash from the stash list, since we are already standing on it
  469.             for (int i = stashList.Count - 1; i >= 0; i--)
  470.             {
  471.                 float currentDistToStash = Vector3.Distance(transform.position, stashList.ElementAt(i).transform.position);
  472.  
  473.                 if (currentDistToStash <= 12f)
  474.                 {
  475.                     AimTrain.msg($"Removing stash id={stashList.ElementAt(i).GetInstanceID()}, distTo={currentDistToStash}");
  476.                     stashList.RemoveAt(i);
  477.                     break;
  478.                 }
  479.             }
  480.  
  481.             StashContainer randomStash;
  482.  
  483.             if (stashList.Count > 1)
  484.             {
  485.                 int randomStashIndex = UnityEngine.Random.Range(0, stashList.Count);
  486.                 randomStash = stashList.ElementAt(randomStashIndex);
  487.                 AimTrain.msg($"Choosing stashindex {randomStashIndex} range=0,{stashList.Count-1}");
  488.             }
  489.             else
  490.             {
  491.                 //Only one stash remaining to route to
  492.                 randomStash = stashList.ElementAt(0);
  493.             }
  494.  
  495.             endPos = randomStash.transform.position;
  496.             startPos = transform.position;
  497.            
  498.             float distanceToDestination = Vector3.Distance(startPos, endPos);
  499.             timeTakenDuringLerp = distanceToDestination/UnityEngine.Random.Range(minSpeed,maxSpeed);
  500.  
  501.             AimTrain.msg($"Distance to destination= {distanceToDestination}, lerpTime={timeTakenDuringLerp}s");
  502.  
  503.             LookTowards(endPos);
  504.  
  505.             lastDelta = 0.0f;
  506.             _isLerping = true;
  507.         }
  508.  
  509.         public float GetMoveY(Vector3 position)
  510.         {
  511.             return GetGroundY(position);
  512.         }
  513.  
  514.         public float GetGroundY(Vector3 position)
  515.         {
  516.             position = position + Vector3.up;
  517.             RaycastHit hitinfo;
  518.             if (Physics.Raycast(position, AimTrain.Vector3Down, out hitinfo, 100f, AimTrain.groundLayer))
  519.             {
  520.                 return hitinfo.point.y;
  521.             }
  522.             return position.y - .5f;
  523.         }
  524.  
  525.         //Duration to reach dest = distance/speed;
  526.         float lastDelta = 0f;
  527.  
  528.         void Update()
  529.         {
  530.             //AimTrain.msg($"DeltaTime = {Time.deltaTime}");
  531.             if (!AimTrain.Moving)
  532.             {
  533.                 return;
  534.             }
  535.  
  536.             if (_isLerping)
  537.             {
  538.                 lastDelta += Time.deltaTime;
  539.                 float pct = lastDelta / timeTakenDuringLerp;
  540.  
  541.                 //AimTrain.msg($"ld={lastDelta} pct={pct}%");
  542.  
  543.                 Vector3 nextPos = Vector3.Lerp(startPos, endPos, pct);
  544.                 nextPos.y = GetMoveY(nextPos);
  545.                 basePlayer.MovePosition(nextPos);
  546.                 basePlayer.modelState.onground = true;
  547.  
  548.                 var newEyesPos = endPos + new Vector3(0, 1.6f, 0);
  549.                 basePlayer.eyes.position.Set(newEyesPos.x, newEyesPos.y, newEyesPos.z);
  550.                 basePlayer.UpdatePlayerCollider(true);
  551.  
  552.                 if (pct >= 1.0f)
  553.                 {
  554.                     //AimTrain.Instance.Chat("Arrived Lerp!");
  555.                     _isLerping = false;
  556.                     StartLerping();
  557.                 }
  558.             }
  559.             else
  560.             {
  561.                 //Wait for at least 1+ stashes before doing anything
  562.                 if (getStashCount() > 1)
  563.                 {
  564.                     StartLerping();
  565.                 }
  566.             }
  567.         }
  568.  
  569.         void FixedUpdate()
  570.         {
  571.            
  572.         }
  573.     }
  574.     #endregion
  575. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement