Guest User

Zombified Initiative

a guest
Aug 14th, 2023
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.17 KB | None | 0 0
  1. namespace ZombifiedInitiative;
  2.  
  3. using Il2CppInterop.Runtime.InteropTypes.Arrays;
  4. using UnityEngine;
  5. using BepInEx;
  6. using BepInEx.Logging;
  7. using BepInEx.Unity.IL2CPP;
  8. using GTFO.API;
  9. using LevelGeneration;
  10. using Agents;
  11. using Player;
  12. using Enemies;
  13.  
  14. [BepInDependency("dev.gtfomodding.gtfo-api")]
  15. [BepInPlugin("org.bepinex.plugins.zombified-initiative", "Zombified Initiative", "0.0.2.0")]
  16. public class ZombifiedInitiative : BasePlugin
  17. {
  18.     public override void Load()
  19.     {
  20.         var component = AddComponent<Component>();
  21.         EventAPI.OnExpeditionStarted += component.FindBots;
  22.         component.Log = Log;
  23.     }
  24.  
  25.  
  26.     private class Component : MonoBehaviour
  27.     {
  28.         private enum Bot
  29.         {
  30.             DAUDA,
  31.             HACKETT,
  32.             BISHOP
  33.         }
  34.  
  35.         private Dictionary<Bot, string> botNameMapping = new Dictionary<Bot, string>
  36.         {
  37.             { Bot.DAUDA, "Dauda" },
  38.             { Bot.HACKETT, "Hackett" },
  39.             { Bot.BISHOP, "Bishop" }
  40.         };
  41.  
  42.         public ManualLogSource Log;
  43.         private Il2CppArrayBase<PlayerAIBot> _bots;
  44.         private float _manualActionsPriority = 5f;
  45.         private float _manualActionsHaste = 1f;
  46.         private bool _debug;
  47.  
  48.  
  49.         private void Awake()
  50.         {
  51.             _bots = new Il2CppReferenceArray<PlayerAIBot>(0);
  52.         }
  53.  
  54.  
  55.         private void Start()
  56.         {
  57.             Print("Zombified Initiative has been added as component", true);
  58.         }
  59.  
  60.  
  61.         private void Update()
  62.         {
  63.             if (Input.GetKeyDown(KeyCode.L))
  64.                 SwitchDebug();
  65.  
  66.             if (Input.GetKeyDown(KeyCode.P))
  67.             {
  68.                 PreventManualActions();
  69.             }
  70.  
  71.             PreventAutoResourcePickups();
  72.             PreventAutoResourceUses();
  73.  
  74.             if (Input.GetKey(KeyCode.Alpha8))
  75.                 SendBot(Bot.DAUDA);
  76.  
  77.             if (Input.GetKey(KeyCode.Alpha9))
  78.                 SendBot(Bot.HACKETT);
  79.  
  80.             if (Input.GetKey(KeyCode.Alpha0))
  81.                 SendBot(Bot.BISHOP);
  82.  
  83.             void SendBot(Bot bot)
  84.             {
  85.                 if (Input.GetMouseButtonDown(2))
  86.                 {
  87.                     var monster = GetMonsterUnderPlayerAim();
  88.                     if (monster != null)
  89.                     {
  90.                         SendBotToKillEnemy(bot, monster,
  91.                             PlayerBotActionAttack.StanceEnum.All,
  92.                             PlayerBotActionAttack.AttackMeansEnum.All,
  93.                             PlayerBotActionWalk.Descriptor.PostureEnum.Stand);
  94.                     }
  95.                 }
  96.  
  97.                 if (Input.GetKeyDown(KeyCode.U))
  98.                 {
  99.                     var item = GetItemUnderPlayerAim();
  100.                     if (item != null)
  101.                         SendBotToPickupItem(bot, item /*, true*/);
  102.                 }
  103.  
  104.                 if (Input.GetKeyDown(KeyCode.I))
  105.                     SendBotToShareResourcePack(bot, GetHumanUnderPlayerAim());
  106.  
  107.                 // if (Input.GetKeyDown(KeyCode.J))
  108.                 //     MakeBotThrowResourcePack(bot, RaycastHits().First().point);
  109.             }
  110.         }
  111.  
  112.  
  113.         public void FindBots()
  114.         {
  115.             _bots = FindObjectsOfType<PlayerAIBot>();
  116.             Print("Found bots");
  117.         }
  118.  
  119.  
  120.         #region Preventions
  121.         private void PreventManualActions()
  122.         {
  123.             var actionsToRemove = new List<PlayerBotActionBase>();
  124.             var haste = _manualActionsHaste - 0.01f;
  125.  
  126.             foreach (var bot in _bots)
  127.             {
  128.                 foreach (var action in bot.Actions)
  129.                 {
  130.                     if (action.GetIl2CppType().Name == "PlayerBotActionAttack")
  131.                     {
  132.                         var descriptor = action.DescBase.Cast<PlayerBotActionAttack.Descriptor>();
  133.                         if (descriptor.Haste > haste)
  134.                         {
  135.                             actionsToRemove.Add(action);
  136.                             continue;
  137.                         }
  138.                     }
  139.  
  140.                     if (action.GetIl2CppType().Name == "PlayerBotActionCollectItem")
  141.                     {
  142.                         var descriptor = action.DescBase.Cast<PlayerBotActionCollectItem.Descriptor>();
  143.                         if (descriptor.Haste > haste)
  144.                         {
  145.                             actionsToRemove.Add(action);
  146.                             continue;
  147.                         }
  148.                     }
  149.  
  150.                     if (action.GetIl2CppType().Name == "PlayerBotActionShareResourcePack")
  151.                     {
  152.                         var descriptor = action.DescBase.Cast<PlayerBotActionShareResourcePack.Descriptor>();
  153.                         if (descriptor.Haste > haste)
  154.                         {
  155.                             actionsToRemove.Add(action);
  156.                             continue;
  157.                         }
  158.                     }
  159.                 }
  160.  
  161.                 foreach (var action in actionsToRemove)
  162.                 {
  163.                     // bot.Actions.Remove(action); // Queued stop
  164.                     bot.StopAction(action.DescBase); // Instant stop
  165.                     Print(bot.Agent.PlayerName + "'s manual actions were cancelled");
  166.                 }
  167.  
  168.                 actionsToRemove.Clear();
  169.             }
  170.         }
  171.  
  172.  
  173.         private void PreventAutoResourcePickups()
  174.         {
  175.             var actionsToRemove = new List<PlayerBotActionBase>();
  176.  
  177.             foreach (var bot in _bots)
  178.             {
  179.                 foreach (var action in bot.Actions)
  180.                 {
  181.                     if (action.GetIl2CppType().Name != "PlayerBotActionCollectItem")
  182.                         continue;
  183.  
  184.                     var descriptor = action.DescBase.Cast<PlayerBotActionCollectItem.Descriptor>();
  185.  
  186.                     // TODO Find out what desinfection resource pack is really called, the name below is a guess
  187.                     var itemIsDesinfectionPack = descriptor.TargetItem.PublicName == "Desinfection Pack";
  188.                     var itemIsMediPack = descriptor.TargetItem.PublicName == "MediPack";
  189.                     var itemIsAmmoPack = descriptor.TargetItem.PublicName == "Ammo Pack";
  190.                     var itemIsToolRefillPack = descriptor.TargetItem.PublicName == "Tool Refill Pack";
  191.  
  192.                     var itemIsPack = itemIsToolRefillPack || itemIsAmmoPack || itemIsMediPack || itemIsDesinfectionPack;
  193.                     if (descriptor.Haste < _manualActionsHaste && itemIsPack)
  194.                         actionsToRemove.Add(action);
  195.                 }
  196.  
  197.                 foreach (var action in actionsToRemove)
  198.                 {
  199.                     bot.Actions.Remove(action); // Queued stop
  200.                     // bot.StopAction(action.DescBase); // Instant stop
  201.                     Print("Prevented " + bot.Agent.PlayerName + " from resource pickup");
  202.                 }
  203.  
  204.                 actionsToRemove.Clear();
  205.             }
  206.         }
  207.  
  208.  
  209.         private void PreventAutoResourceUses()
  210.         {
  211.             var actionsToRemove = new List<PlayerBotActionBase>();
  212.  
  213.             foreach (var bot in _bots)
  214.             {
  215.                 foreach (var action in bot.Actions)
  216.                 {
  217.                     if (action.GetIl2CppType().Name != "PlayerBotActionShareResourcePack")
  218.                         continue;
  219.  
  220.                     var descriptor = action.DescBase.Cast<PlayerBotActionShareResourcePack.Descriptor>();
  221.                     if (descriptor.Haste < _manualActionsHaste)
  222.                     {
  223.                         actionsToRemove.Add(action);
  224.                     }
  225.                 }
  226.  
  227.                 foreach (var action in actionsToRemove)
  228.                 {
  229.                     bot.Actions.Remove(action); // Queued stop
  230.                     // bot.StopAction(action.DescBase); // Instant stop
  231.                     Print("Prevented " + bot.Agent.PlayerName + " from sharing resource pack");
  232.                 }
  233.  
  234.                 actionsToRemove.Clear();
  235.             }
  236.         }
  237.         #endregion
  238.  
  239.  
  240.         #region Attack monster
  241.         private EnemyAgent GetMonsterUnderPlayerAim()
  242.         {
  243.             return GetObjectUnderPlayerAim<EnemyAgent>
  244.                 (enemy => "Found monster: " + enemy.EnemyData.name);
  245.         }
  246.  
  247.  
  248.         private void SendBotToKillEnemy(Bot chosenBot, Agent enemy,
  249.             PlayerBotActionAttack.StanceEnum stance,
  250.             PlayerBotActionAttack.AttackMeansEnum means,
  251.             PlayerBotActionWalk.Descriptor.PostureEnum posture)
  252.         {
  253.             var bot = _bots.First(bot => bot.Agent.PlayerName == botNameMapping[chosenBot]);
  254.             if (bot == null)
  255.                 return;
  256.  
  257.             ExecuteBotAction(bot, new PlayerBotActionAttack.Descriptor(bot)
  258.                 {
  259.                     // Status = PlayerBotActionBase.Descriptor.StatusType.Active,
  260.                     // Bot = bot,
  261.                     Stance = stance,
  262.                     Means = means,
  263.                     Posture = posture,
  264.                     TargetAgent = enemy,
  265.                     Prio = _manualActionsPriority,
  266.                     Haste = _manualActionsHaste,
  267.                 },
  268.                 "Added kill enemy action to " + bot.Agent.PlayerName);
  269.         }
  270.         #endregion
  271.  
  272.  
  273.         #region Item pickup
  274.         private ItemInLevel GetItemUnderPlayerAim()
  275.         {
  276.             return GetObjectUnderPlayerAim<ItemInLevel>
  277.                 (item => "Found item: " + item.PublicName);
  278.         }
  279.  
  280.  
  281.         private void SendBotToPickupItem(Bot chosenBot, ItemInLevel item /*, bool resourcePack = false*/)
  282.         {
  283.             var bot = _bots.First(bot => bot.Agent.PlayerName == botNameMapping[chosenBot]);
  284.             if (bot == null)
  285.                 return;
  286.  
  287.             // if (resourcePack)
  288.             //     if (item.GetIl2CppType().Name != "Interact_Pickup_PickupItem")
  289.             //         return;
  290.  
  291.             ExecuteBotAction(bot, new PlayerBotActionCollectItem.Descriptor(bot)
  292.                 {
  293.                     // Status = PlayerBotActionBase.Descriptor.StatusType.Active,
  294.                     // Bot = bot,
  295.                     TargetItem = item,
  296.                     TargetContainer = item.container,
  297.                     TargetPosition = item.transform.position,
  298.                     Prio = _manualActionsPriority,
  299.                     Haste = _manualActionsHaste,
  300.                 },
  301.                 "Added collect item action to " + bot.Agent.PlayerName);
  302.         }
  303.         #endregion
  304.  
  305.  
  306.         #region Resource pack sharing
  307.         private PlayerAgent GetHumanUnderPlayerAim()
  308.         {
  309.             var playerAIBot = GetObjectUnderPlayerAim<PlayerAIBot>
  310.                 (bot => "Found bot: " + bot.Agent.PlayerName);
  311.             if (playerAIBot != null)
  312.                 return playerAIBot.Agent;
  313.  
  314.             var otherPlayerAgent = GetObjectUnderPlayerAim<PlayerAgent>
  315.                 (player => "Found other player: " + player.PlayerName);
  316.             if (otherPlayerAgent != null)
  317.                 return otherPlayerAgent;
  318.  
  319.             var localPlayerAgent = FindObjectOfType<LocalPlayerAgent>().Cast<PlayerAgent>();
  320.             Print("Found local player: " + localPlayerAgent.PlayerName);
  321.             return localPlayerAgent;
  322.         }
  323.  
  324.  
  325.         private void SendBotToShareResourcePack(Bot chosenBot, PlayerAgent human)
  326.         {
  327.             var bot = _bots.First(bot => bot.Agent.PlayerName == botNameMapping[chosenBot]);
  328.             if (bot == null)
  329.                 return;
  330.  
  331.             BackpackItem backpackItem = null;
  332.             var gotBackpackItem = bot.Backpack.HasBackpackItem(InventorySlot.ResourcePack) &&
  333.                                   bot.Backpack.TryGetBackpackItem(InventorySlot.ResourcePack, out backpackItem);
  334.             if (!gotBackpackItem)
  335.                 return;
  336.  
  337.             var resourcePack = backpackItem.Instance.Cast<ItemEquippable>();
  338.             bot.Inventory.DoEquipItem(resourcePack);
  339.  
  340.             ExecuteBotAction(bot, new PlayerBotActionShareResourcePack.Descriptor(bot)
  341.                 {
  342.                     // Status = PlayerBotActionBase.Descriptor.StatusType.Active,
  343.                     // Bot = bot,
  344.                     Receiver = human,
  345.                     Item = resourcePack,
  346.                     Prio = _manualActionsPriority,
  347.                     Haste = _manualActionsHaste,
  348.                 },
  349.                 "Added share resource action to " + bot.Agent.PlayerName);
  350.         }
  351.         #endregion
  352.  
  353.  
  354.         /*private void MakeBotThrowResourcePack(Bot chosenBot, Vector3 targetPosition)
  355.         {
  356.             var bot = _bots.First(bot => bot.Agent.PlayerName == botNameMapping[chosenBot]);
  357.             if (bot == null)
  358.                 return;
  359.  
  360.             BackpackItem backpackItem = null;
  361.             var gotBackpackItem = bot.Backpack.HasBackpackItem(InventorySlot.ResourcePack) &&
  362.                                   bot.Backpack.TryGetBackpackItem(InventorySlot.ResourcePack, out backpackItem);
  363.             if (!gotBackpackItem)
  364.                 return;
  365.  
  366.             var resourcePack = backpackItem.Instance.Cast<ItemEquippable>();
  367.             bot.Inventory.DoEquipItem(resourcePack);
  368.  
  369.             ExecuteBotAction(bot, new PlayerBotActionThrowItem.Descriptor(bot)
  370.                 {
  371.                     // Status = PlayerBotActionBase.Descriptor.StatusType.Active,
  372.                     // Bot = bot,
  373.                     TargetPosition = targetPosition,
  374.                     Item = resourcePack,
  375.                     Prio = _manualActionsPriority,
  376.                     Haste = _manualActionsHaste,
  377.                 },
  378.                 "Added throw resource pack to " + bot.Agent.PlayerName);
  379.         }*/
  380.  
  381.  
  382.         /*private SentryGunInstance GetSentryUnderPlayerAim()
  383.         {
  384.             var sentryGun = GetObjectUnderPlayerAim<SentryGunInstance>
  385.                 (sentryGun => "Found sentry gun: " + sentryGun.PublicName);
  386.             if (sentryGun != null)
  387.                 return sentryGun;
  388.             return null;
  389.         }*/
  390.  
  391.  
  392.         private void ExecuteBotAction(PlayerAIBot bot, PlayerBotActionBase.Descriptor descriptor, string message)
  393.         {
  394.             // bot.Actions.Add(new PlayerBotActionBase(descriptor));
  395.             // bot.StartQueuedActions();
  396.             // bot.UpdateActions();
  397.             bot.StartAction(descriptor);
  398.             Print(message);
  399.         }
  400.  
  401.  
  402.         private T GetObjectUnderPlayerAim<T>(Func<T, string> message) where T : class
  403.         {
  404.             foreach (var raycastHit in RaycastHits())
  405.             {
  406.                 var component = raycastHit.collider.GetComponentInParent<T>();
  407.                 if (component == null) continue;
  408.                 Print(message(component));
  409.                 return component;
  410.             }
  411.  
  412.             return null;
  413.         }
  414.  
  415.  
  416.         private Il2CppStructArray<RaycastHit> RaycastHits()
  417.         {
  418.             return Physics.RaycastAll(Camera.current.ScreenPointToRay(Input.mousePosition));
  419.         }
  420.  
  421.  
  422.         private void SwitchDebug()
  423.         {
  424.             _debug = !_debug;
  425.         }
  426.  
  427.  
  428.         private void Print(string text, bool forced = false)
  429.         {
  430.             if (_debug || forced)
  431.                 Log.LogInfo(text);
  432.         }
  433.     }
  434. }
Add Comment
Please, Sign In to add comment