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.Text;
- using UnityEngine;
- using System.Reflection;
- using Rust;
- using Network.Visibility;
- using Oxide.Core;
- using Rust;
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using Oxide.Core.Plugins;
- namespace Oxide.Plugins
- {
- #region zone
- public class Zone
- {
- public List<int> Bots;
- public virtual void Start()
- {
- }
- }
- #endregion
- #region bot
- public class Bot
- {
- public ulong Id;
- public int ZoneId;
- public float PosX;
- public float PosY;
- public float PosZ;
- private BasePlayer BasePlayer;
- public Bot(int zone, ulong id, Vector3 position)
- {
- Id = id;
- PosX = position.x;
- PosY = position.y;
- PosZ = position.z;
- }
- #region main methods
- public void Spawn()
- {
- AimTrain.Instance.Chat("Spawning!");
- var newPlayer = GameManager.server.CreateEntity("assets/prefabs/player/player.prefab", GetPos(), Quaternion.identity);
- newPlayer.Spawn();
- newPlayer.SetFlag(BaseEntity.Flags.Reserved1, true);
- FieldInfo modelStateField = typeof(BasePlayer).GetField("modelState", BindingFlags.Instance | BindingFlags.NonPublic);
- object modelState = modelStateField.GetValue(newPlayer);
- modelState.GetType().GetProperty("onground").SetValue(modelState, true, null);
- newPlayer.SendNetworkUpdate();
- BasePlayer = newPlayer.GetComponent<BasePlayer>();
- BasePlayer.userID = Id;
- AimTrain.Instance.Chat("Spawned!");
- }
- public void Kill()
- {
- if (BasePlayer != null)
- {
- BasePlayer.Kill();
- BasePlayer.SendNetworkUpdate();
- }
- }
- #endregion
- #region utility methods
- Vector3 GetPos()
- {
- return new Vector3(PosX, PosY, PosZ);
- }
- #endregion
- }
- #endregion
- #region plugin
- [Info("AimTrain", "Ardivaba", 0.1)]
- [Description("Mkes you Trauzillz")]
- public class AimTrain : RustPlugin
- {
- #region data
- public static AimTrain Instance;
- public static HashSet<Bot> Bots;
- public static bool Moving = true;
- public static bool DEBUG_MSG = false;
- public static Vector3 Vector3Down;
- public static int groundLayer;
- public static readonly FieldInfo viewangles = typeof(BasePlayer).GetField("viewAngles", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));
- #endregion
- public static void msg(String message)
- {
- if (DEBUG_MSG)
- {
- Instance.Puts(message);
- }
- }
- #region public static main methods
- public static void CreateBot(Vector3 position)
- {
- Bot bot = new Bot(0, (ulong)Bots.Count, position);
- bot.Spawn();
- Bots.Add(bot);
- AimTrain.SaveBots();
- }
- public static void LoadBots()
- {
- Bots = Interface.Oxide.DataFileSystem.ReadObject<HashSet<Bot>>("Bots");
- }
- public static void SaveBots()
- {
- Interface.Oxide.DataFileSystem.WriteObject<HashSet<Bot>>("Bots", Bots, true);
- }
- #endregion
- #region main methods
- public void Chat(string chat)
- {
- PrintToChat(chat);
- }
- void UnlimitedAmmo(BaseProjectile projectile, BasePlayer player)
- {
- projectile.GetItem().condition = projectile.GetItem().info.condition.max;
- projectile.primaryMagazine.contents = projectile.primaryMagazine.capacity;
- projectile.SendNetworkUpdateImmediate();
- }
- #endregion
- private float lastCorpseCheck;
- #region oxide hooks
- void OnFrame()
- {
- //AutoClean corpses every 30s
- float corspeTimeDiffS = Time.time - lastCorpseCheck;
- if (corspeTimeDiffS > 30.0f)
- {
- lastCorpseCheck = Time.time;
- AimTrain.msg($"Cleaning up corpses diff = {corspeTimeDiffS}");
- BaseCorpse[] corpses = GameObject.FindObjectsOfType<BaseCorpse>();
- foreach (BaseCorpse corpse in corpses)
- {
- corpse.Kill();
- corpse.SendNetworkUpdate();
- }
- }
- StashContainer[] stashes = GameObject.FindObjectsOfType<StashContainer>();
- BasePlayer[] players = GameObject.FindObjectsOfType<BasePlayer>();
- //Puts($"players={players}");
- int botCount = 0;
- foreach (BasePlayer player in players)
- {
- if (player.IsWounded() && player.HasFlag(BaseEntity.Flags.Reserved1))
- {
- player.Kill();
- player.SendNetworkUpdate();
- headShots++;
- }
- if (player.HasFlag(BaseEntity.Flags.Reserved1))
- {
- botCount++;
- }
- }
- int stashCount = stashes.Count();
- if (botCount < botCounts)
- {
- if (stashes.Length > 0)
- {
- //Puts($"OnFrame: Bot count = {botCount} / {botCounts} stashes = {stashCount}");
- StashContainer randomStash = stashes[UnityEngine.Random.Range(0, stashes.Length - 1)];
- SpawnBot(randomStash.transform.position);
- }
- }
- }
- void Loaded()
- {
- AimTrain.Instance = this;
- AimTrain.LoadBots();
- OnLoadedClearBots();
- lastCorpseCheck = Time.time;
- AimTrain.Vector3Down = new Vector3(0f, -1f, 0f);
- groundLayer = LayerMask.GetMask("Construction", "Terrain", "World");
- }
- void OnLoadedClearBots()
- {
- AimTrain.msg("Loaded, clearing bots..");
- BasePlayer[] players = GameObject.FindObjectsOfType<BasePlayer>();
- foreach (BasePlayer player in players)
- {
- if (player.HasFlag(BaseEntity.Flags.Reserved1))
- {
- player.Kill();
- player.SendNetworkUpdate();
- }
- }
- BaseCorpse[] corpses = GameObject.FindObjectsOfType<BaseCorpse>();
- foreach (BaseCorpse corpse in corpses)
- {
- corpse.Kill();
- corpse.SendNetworkUpdate();
- }
- }
- void OnWeaponFired(BaseProjectile projectile, BasePlayer player)
- {
- UnlimitedAmmo(projectile, player);
- }
- int headShots = 0;
- void OnPlayerAttack(BasePlayer attacker, HitInfo info)
- {
- return;
- BasePlayer victim = info.HitEntity.GetComponent<BasePlayer>();
- if (victim != null)
- {
- if (victim.HasFlag(BaseEntity.Flags.Reserved1))
- {
- if (info.isHeadshot)
- {
- //headShots++;
- }
- }
- }
- if (headShots == 20)
- {
- PrintToChat("Headshot benchmark time: " + (DateTime.Now - startTime).TotalSeconds);
- headShots = 0;
- foreach (Bot bot in Bots)
- {
- bot.Kill();
- }
- }
- }
- #endregion
- int botCounts = 1;
- #region commands
- [ConsoleCommand("botCount")]
- void CmdBotCount(ConsoleSystem.Arg arg)
- {
- OnLoadedClearBots();
- botCounts = int.Parse(arg.Args[0]);
- PrintToChat("Bot count is now: " + botCounts);
- }
- [ConsoleCommand("createBot")]
- void CmdCreateBot(ConsoleSystem.Arg arg)
- {
- AimTrain.CreateBot(arg.Player().transform.position);
- PrintToChat("Where them bots at?");
- }
- [ConsoleCommand("killBots")]
- void CmdKillBots(ConsoleSystem.Arg arg)
- {
- PrintToChat("There are currently " + Bots.Count + " Bots");
- foreach (Bot bot in Bots)
- {
- bot.Kill();
- }
- }
- [ConsoleCommand("stashes")]
- void CmdStashes(ConsoleSystem.Arg arg)
- {
- StashContainer[] stashes = GameObject.FindObjectsOfType<StashContainer>();
- foreach (StashContainer stash in stashes)
- {
- stash.Kill();
- stash.SendNetworkUpdate();
- }
- }
- [ConsoleCommand("moving")]
- void CmdMoving(ConsoleSystem.Arg arg)
- {
- AimTrain.Moving = !AimTrain.Moving;
- }
- DateTime startTime;
- [ConsoleCommand("kcmo")]
- void CmdKcmo(ConsoleSystem.Arg arg)
- {
- headShots = 0;
- startTime = DateTime.Now;
- BasePlayer[] players = GameObject.FindObjectsOfType<BasePlayer>();
- foreach (BasePlayer player in players)
- {
- if (player.HasFlag(BaseEntity.Flags.Reserved1))
- {
- player.Kill();
- player.SendNetworkUpdate();
- }
- }
- BaseCorpse[] corpses = GameObject.FindObjectsOfType<BaseCorpse>();
- foreach (BaseCorpse corpse in corpses)
- {
- corpse.Kill();
- corpse.SendNetworkUpdate();
- }
- }
- void SpawnBot(Vector3 position)
- {
- //AimTrain.Instance.Chat("Spawning!");
- BasePlayer newPlayer = (BasePlayer)GameManager.server.CreateEntity("assets/prefabs/player/player.prefab", position, Quaternion.identity);
- newPlayer.Spawn();
- newPlayer.gameObject.AddComponent<BotMover>();
- newPlayer.SetFlag(BaseEntity.Flags.Reserved1, true);
- AimTrain.msg($"Spawnbot: modelState = {newPlayer.modelState} onground={newPlayer.modelState.onground}");
- newPlayer.modelState.onground = true;
- }
- #endregion
- }
- #endregion
- #region
- public class BotMover : MonoBehaviour
- {
- BasePlayer basePlayer;
- Vector3 startPosition;
- Vector3 targetPosition;
- //Rust defaults = 2.4 (walk) 5.5 (run)
- float maxSpeed = 7f;
- float minSpeed = 3.5f;
- Boolean _isLerping;
- Vector3 startPos;
- Vector3 endPos;
- float timeTakenDuringLerp = 0f;
- public void LookTowards(Vector3 pos)
- {
- if (pos != basePlayer.transform.position)
- SetViewAngle(Quaternion.LookRotation(pos - basePlayer.transform.position));
- }
- public void SetViewAngle(Quaternion viewAngles)
- {
- if (viewAngles.eulerAngles == default(Vector3))
- {
- return;
- }
- AimTrain.msg("Facing..");
- AimTrain.viewangles.SetValue(basePlayer, viewAngles.eulerAngles);
- basePlayer.SendNetworkUpdate(BasePlayer.NetworkQueue.Update);
- }
- void Start()
- {
- //AimTrain.Instance.Chat("Started!");
- basePlayer = GetComponent<BasePlayer>();
- startPosition = transform.position;
- targetPosition = startPosition;
- basePlayer.ChangeHealth(100.0f);
- basePlayer.SendNetworkUpdate();
- int random = UnityEngine.Random.Range(0, 3);
- int clothes = UnityEngine.Random.Range(0, 2);
- if (random == 0)//Metal
- {
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-46848560), basePlayer.inventory.containerWear);
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(1265861812), basePlayer.inventory.containerWear);
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-1595790889), basePlayer.inventory.containerWear);
- }
- else if (random == 1)
- {
- int random2 = UnityEngine.Random.Range(0, 2);
- if (random2 == 0)
- {
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(1260209393), basePlayer.inventory.containerWear);
- }
- else if (random2 == 1)
- {
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-2128719593), basePlayer.inventory.containerWear);
- }
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-288010497), basePlayer.inventory.containerWear);
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-1595790889), basePlayer.inventory.containerWear);
- }
- if (clothes == 0)
- {
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(115739308), basePlayer.inventory.containerWear);
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(707427396), basePlayer.inventory.containerWear);
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(1767561705), basePlayer.inventory.containerWear);
- }
- if (clothes == 1)
- {
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(106433500), basePlayer.inventory.containerWear);
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(-1211618504), basePlayer.inventory.containerWear);
- basePlayer.inventory.GiveItem(ItemManager.CreateByItemID(115739308), basePlayer.inventory.containerWear);
- }
- StartLerping();
- }
- int getStashCount()
- {
- List<StashContainer> stashList = new List<StashContainer>(GameObject.FindObjectsOfType<StashContainer>());
- return stashList.Count;
- }
- void StartLerping()
- {
- List<StashContainer> stashList = new List<StashContainer>(GameObject.FindObjectsOfType<StashContainer>());
- if (stashList.Count <= 1)
- {
- //Only one stash, so do nothing...
- _isLerping = false;
- return;
- }
- AimTrain.msg($"StashCount={stashList.Count}");
- //Remove the closest stash from the stash list, since we are already standing on it
- for (int i = stashList.Count - 1; i >= 0; i--)
- {
- float currentDistToStash = Vector3.Distance(transform.position, stashList.ElementAt(i).transform.position);
- if (currentDistToStash <= 12f)
- {
- AimTrain.msg($"Removing stash id={stashList.ElementAt(i).GetInstanceID()}, distTo={currentDistToStash}");
- stashList.RemoveAt(i);
- break;
- }
- }
- StashContainer randomStash;
- if (stashList.Count > 1)
- {
- int randomStashIndex = UnityEngine.Random.Range(0, stashList.Count);
- randomStash = stashList.ElementAt(randomStashIndex);
- AimTrain.msg($"Choosing stashindex {randomStashIndex} range=0,{stashList.Count-1}");
- }
- else
- {
- //Only one stash remaining to route to
- randomStash = stashList.ElementAt(0);
- }
- endPos = randomStash.transform.position;
- startPos = transform.position;
- float distanceToDestination = Vector3.Distance(startPos, endPos);
- timeTakenDuringLerp = distanceToDestination/UnityEngine.Random.Range(minSpeed,maxSpeed);
- AimTrain.msg($"Distance to destination= {distanceToDestination}, lerpTime={timeTakenDuringLerp}s");
- LookTowards(endPos);
- lastDelta = 0.0f;
- _isLerping = true;
- }
- public float GetMoveY(Vector3 position)
- {
- return GetGroundY(position);
- }
- public float GetGroundY(Vector3 position)
- {
- position = position + Vector3.up;
- RaycastHit hitinfo;
- if (Physics.Raycast(position, AimTrain.Vector3Down, out hitinfo, 100f, AimTrain.groundLayer))
- {
- return hitinfo.point.y;
- }
- return position.y - .5f;
- }
- //Duration to reach dest = distance/speed;
- float lastDelta = 0f;
- void Update()
- {
- //AimTrain.msg($"DeltaTime = {Time.deltaTime}");
- if (!AimTrain.Moving)
- {
- return;
- }
- if (_isLerping)
- {
- lastDelta += Time.deltaTime;
- float pct = lastDelta / timeTakenDuringLerp;
- //AimTrain.msg($"ld={lastDelta} pct={pct}%");
- Vector3 nextPos = Vector3.Lerp(startPos, endPos, pct);
- nextPos.y = GetMoveY(nextPos);
- basePlayer.MovePosition(nextPos);
- basePlayer.modelState.onground = true;
- var newEyesPos = endPos + new Vector3(0, 1.6f, 0);
- basePlayer.eyes.position.Set(newEyesPos.x, newEyesPos.y, newEyesPos.z);
- basePlayer.UpdatePlayerCollider(true);
- if (pct >= 1.0f)
- {
- //AimTrain.Instance.Chat("Arrived Lerp!");
- _isLerping = false;
- StartLerping();
- }
- }
- else
- {
- //Wait for at least 1+ stashes before doing anything
- if (getStashCount() > 1)
- {
- StartLerping();
- }
- }
- }
- void FixedUpdate()
- {
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement