Advertisement
klassekatze

Summon Torpedo

Jul 2nd, 2023
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.33 KB | None | 0 0
  1. /*
  2.  * R e a d m e
  3.  * -----------
  4.  *
  5.  * SUMMON TORPEDO TESTING SCRIPT
  6.  *
  7.  * Purpose: conveniently summoning specific numbers of torpedoes to attack your grid as needed in creative for testing PDCs or other defensive qualities.
  8.  *
  9.  * 1. Put this script in a grid with torpedo launchers, power, and antenna.
  10.  * 2. Give torpedo launchers to enemy, such as pirates. do not give program block, power, antenna away - those should remain owned by you.
  11.  * 3. Add this script to another grid. in that other grid, run command "fire X" where X is a number. Observe as that other grid is shot by X number of torpedoes from the torpedo launcher grid.
  12.  */
  13.  
  14. T getBlock<T>(Func<IMyTerminalBlock, bool> f = null) where T : class
  15. {
  16.     List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  17.     GridTerminalSystem.GetBlocksOfType<T>(blocks, f);
  18.     return blocks.Count > 0 ? (T)blocks[0] : null;
  19. }
  20.  
  21. IMyRadioAntenna a = null;
  22.  
  23. List<IMyTerminalBlock> weapons = new List<IMyTerminalBlock>();
  24. WcPbApi api = null;
  25.  
  26. bool setup = false;
  27. public Program()
  28. {
  29.  
  30.     Runtime.UpdateFrequency = UpdateFrequency.Update1;
  31. }
  32. string _sharedSecret = "shhhh";
  33. IMyBroadcastListener _broadcastListener;
  34. string _broadcastTag = "SBT_";
  35.  
  36. int FIRE_NUM = 0;
  37.  
  38. void readMessages()
  39. {
  40.     while (_broadcastListener.HasPendingMessage)
  41.     {
  42.         MyIGCMessage myIGCMessage = _broadcastListener.AcceptMessage();
  43.         if (myIGCMessage.Tag == _broadcastTag)
  44.         {
  45.             var d = myIGCMessage.Data;
  46.             try
  47.             {
  48.                 if (d != null)
  49.                 {
  50.                     if (d is MyTuple<long, int>)
  51.                     {
  52.                         var dat = (MyTuple<long, int>)d;
  53.  
  54.                         api.SetAiFocus(Me,dat.Item1);
  55.                         FIRE_NUM = dat.Item2;
  56.  
  57.                     }
  58.                 }
  59.             }
  60.             catch (Exception e)
  61.             {
  62.                 Echo(e.ToString());
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. void update()
  69. {
  70.     if (FIRE_NUM > 0)
  71.     {
  72.         foreach (var w in weapons)
  73.         {
  74.             if (api.IsWeaponReadyToFire(w))
  75.             {
  76.                 var t = api.GetWeaponTarget(w);
  77.                 if(t.HasValue)
  78.                 {
  79.                     var tt = t.Value;
  80.                     if(tt.Type == MyDetectedEntityType.LargeGrid || tt.Type == MyDetectedEntityType.SmallGrid)
  81.                     {
  82.                         api.FireWeaponOnce(w);
  83.                         FIRE_NUM -= 1;
  84.                     }
  85.                 }
  86.             }
  87.  
  88.             if (FIRE_NUM < 1) break;
  89.         }
  90.         a.CustomName = FIRE_NUM + " torps queued";
  91.     }
  92. }
  93.  
  94. void requestTorpedo(int num)
  95. {
  96.     if (a != null)
  97.     {
  98.         MyTuple<long, int> pkt = new MyTuple<long, int>(Me.CubeGrid.EntityId, num);
  99.         IGC.SendBroadcastMessage(_broadcastTag, pkt);
  100.         a.CustomName = num + " torps requested";
  101.     }
  102. }
  103. public void Main(string argument, UpdateType updateSource)
  104. {
  105.     if(!setup)
  106.     {
  107.         setup = true;
  108.         a = getBlock<IMyRadioAntenna>();
  109.         a.Radius = 50000;
  110.         api = new WcPbApi();
  111.         api.Activate(Me);
  112.         GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(weapons, b => api.HasCoreWeapon(b));
  113.  
  114.         _broadcastTag += _sharedSecret;
  115.         _broadcastListener = IGC.RegisterBroadcastListener(_broadcastTag);
  116.         Echo("setup complete, weapons: "+ weapons.Count);
  117.     }
  118.  
  119.     if(argument != "") Echo("arg: " + argument);
  120.     try
  121.     {
  122.         readMessages();
  123.         update();
  124.         if (argument.StartsWith("fire "))
  125.         {
  126.             var num = int.Parse(argument.Split(' ')[1]);
  127.             requestTorpedo(num);
  128.             Echo("requested " + num);
  129.         }
  130.     }
  131.     catch (Exception e)
  132.     {
  133.         Echo(e.ToString());
  134.     }
  135.  
  136. }
  137.  
  138. }
  139.  
  140. public class WcPbApi
  141. {
  142.     public string[] WcBlockTypeLabels = new string[]
  143.         {
  144.             "Any",
  145.             "Offense",
  146.             "Utility",
  147.             "Power",
  148.             "Production",
  149.             "Thrust",
  150.             "Jumping",
  151.             "Steering"
  152.         };
  153.  
  154.     private Action<ICollection<MyDefinitionId>> _getCoreWeapons;
  155.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, IDictionary<string, int>, bool> _getBlockWeaponMap;
  156.     private Action<IMyTerminalBlock, IDictionary<MyDetectedEntityInfo, float>> _getSortedThreats;
  157.     private Func<long, bool> _hasGridAi;
  158.     //private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, long, int, Vector3D?> _getPredictedTargetPos;
  159.     //private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, int, Sandbox.ModAPI.Ingame.MyDetectedEntityInfo> _getWeaponTarget;
  160.     //private Action<ICollection<MyDefinitionId>> _getCoreTurrets;
  161.     private Func<long, int, Sandbox.ModAPI.Ingame.MyDetectedEntityInfo> _getAiFocus;
  162.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, long, int, bool> _setAiFocus;
  163.     private Action<Sandbox.ModAPI.Ingame.IMyTerminalBlock, bool, bool, int> _toggleWeaponFire;
  164.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, bool> _hasCoreWeapon;
  165.     private Action<Sandbox.ModAPI.Ingame.IMyTerminalBlock, ICollection<Sandbox.ModAPI.Ingame.MyDetectedEntityInfo>> _getObstructions;
  166.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, ICollection<string>, int, bool> _getTurretTargetTypes;
  167.     private Action<Sandbox.ModAPI.Ingame.IMyTerminalBlock, ICollection<string>, int> _setTurretTargetTypes;
  168.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, long, int, Vector3D?> _getPredictedTargetPos;
  169.  
  170.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, int, Matrix> _getWeaponAzimuthMatrix;
  171.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, int, Matrix> _getWeaponElevationMatrix;
  172.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, long, int, MyTuple<bool, Vector3D?>> _isTargetAlignedExtended;
  173.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, int, string> _getActiveAmmo;
  174.     private Action<Sandbox.ModAPI.Ingame.IMyTerminalBlock, int, string> _setActiveAmmo;
  175.     private Func<long, float> _getConstructEffectiveDps;
  176.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, int, Sandbox.ModAPI.Ingame.MyDetectedEntityInfo> _getWeaponTarget;
  177.     private Action<Sandbox.ModAPI.Ingame.IMyTerminalBlock, long, int> _setWeaponTarget;
  178.  
  179.     private Action<Sandbox.ModAPI.Ingame.IMyTerminalBlock, int, Action<long, int, ulong, long, Vector3D, bool>> _monitorProjectile;
  180.     private Action<Sandbox.ModAPI.Ingame.IMyTerminalBlock, int, Action<long, int, ulong, long, Vector3D, bool>> _unMonitorProjectile;
  181.     private Func<ulong, MyTuple<Vector3D, Vector3D, float, float, long, string>> _getProjectileState;
  182.     private Func<long, MyTuple<bool, int, int>> _getProjectilesLockedOn;
  183.  
  184.     private Action<Sandbox.ModAPI.Ingame.IMyTerminalBlock, bool, int> _fireWeaponOnce;
  185.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, int, bool, bool, bool> _isWeaponReadyToFire;
  186.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, int, float> _getMaxWeaponRange;
  187.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, int, MyTuple<Vector3D, Vector3D>> _getWeaponScope;
  188.     private Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, float> _currentPowerConsumption;
  189.  
  190.     public bool Activate(IMyTerminalBlock pbBlock)
  191.     {
  192.         var dict = pbBlock.GetProperty("WcPbAPI")?.As<IReadOnlyDictionary<string, Delegate>>().GetValue(pbBlock);
  193.         if (dict == null) throw new Exception("WcPbAPI failed to activate");
  194.         return ApiAssign(dict);
  195.     }
  196.  
  197.     public bool ApiAssign(IReadOnlyDictionary<string, Delegate> delegates)
  198.     {
  199.         if (delegates == null)
  200.             return false;
  201.         AssignMethod(delegates, "GetCoreWeapons", ref _getCoreWeapons);
  202.         AssignMethod(delegates, "GetBlockWeaponMap", ref _getBlockWeaponMap);
  203.         AssignMethod(delegates, "GetSortedThreats", ref _getSortedThreats);
  204.         AssignMethod(delegates, "GetObstructions", ref _getObstructions);
  205.         AssignMethod(delegates, "HasGridAi", ref _hasGridAi);
  206.         AssignMethod(delegates, "GetAiFocus", ref _getAiFocus);
  207.         AssignMethod(delegates, "SetAiFocus", ref _setAiFocus);
  208.         AssignMethod(delegates, "HasCoreWeapon", ref _hasCoreWeapon);
  209.         AssignMethod(delegates, "GetPredictedTargetPosition", ref _getPredictedTargetPos);
  210.         AssignMethod(delegates, "GetTurretTargetTypes", ref _getTurretTargetTypes);
  211.         AssignMethod(delegates, "SetTurretTargetTypes", ref _setTurretTargetTypes);
  212.         AssignMethod(delegates, "GetWeaponAzimuthMatrix", ref _getWeaponAzimuthMatrix);
  213.         AssignMethod(delegates, "GetWeaponElevationMatrix", ref _getWeaponElevationMatrix);
  214.         AssignMethod(delegates, "IsTargetAlignedExtended", ref _isTargetAlignedExtended);
  215.         AssignMethod(delegates, "GetActiveAmmo", ref _getActiveAmmo);
  216.         AssignMethod(delegates, "SetActiveAmmo", ref _setActiveAmmo);
  217.         AssignMethod(delegates, "GetConstructEffectiveDps", ref _getConstructEffectiveDps);
  218.         AssignMethod(delegates, "GetWeaponTarget", ref _getWeaponTarget);
  219.         AssignMethod(delegates, "SetWeaponTarget", ref _setWeaponTarget);
  220.         AssignMethod(delegates, "MonitorProjectile", ref _monitorProjectile);
  221.         AssignMethod(delegates, "UnMonitorProjectile", ref _unMonitorProjectile);
  222.         AssignMethod(delegates, "GetProjectileState", ref _getProjectileState);
  223.         AssignMethod(delegates, "GetProjectilesLockedOn", ref _getProjectilesLockedOn);
  224.  
  225.         AssignMethod(delegates, "FireWeaponOnce", ref _fireWeaponOnce);
  226.         AssignMethod(delegates, "ToggleWeaponFire", ref _toggleWeaponFire);
  227.         AssignMethod(delegates, "IsWeaponReadyToFire", ref _isWeaponReadyToFire);
  228.         AssignMethod(delegates, "GetMaxWeaponRange", ref _getMaxWeaponRange);
  229.         AssignMethod(delegates, "GetWeaponScope", ref _getWeaponScope);
  230.  
  231.         AssignMethod(delegates, "GetCurrentPower", ref _currentPowerConsumption);
  232.         return true;
  233.     }
  234.     private void AssignMethod<T>(IReadOnlyDictionary<string, Delegate> delegates, string name, ref T field) where T : class
  235.     {
  236.         if (delegates == null)
  237.         {
  238.             field = null;
  239.             return;
  240.         }
  241.         Delegate del;
  242.         if (!delegates.TryGetValue(name, out del))
  243.             throw new Exception($"{GetType().Name} :: Couldn't find {name} delegate of type {typeof(T)}");
  244.         field = del as T;
  245.         if (field == null)
  246.             throw new Exception(
  247.                 $"{GetType().Name} :: Delegate {name} is not type {typeof(T)}, instead it's: {del.GetType()}");
  248.     }
  249.  
  250.     public void GetAllCoreWeapons(ICollection<MyDefinitionId> collection) => _getCoreWeapons?.Invoke(collection);
  251.     public void GetSortedThreats(IMyTerminalBlock pbBlock, IDictionary<MyDetectedEntityInfo, float> collection) =>
  252.         _getSortedThreats?.Invoke(pbBlock, collection);
  253.     public bool HasGridAi(long entity) => _hasGridAi?.Invoke(entity) ?? false;
  254.     //public Vector3D? GetPredictedTargetPosition(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, long targetEnt, int weaponId) =>_getPredictedTargetPos?.Invoke(weapon, targetEnt, weaponId) ?? null;
  255.     // public MyDetectedEntityInfo? GetWeaponTarget(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, int weaponId = 0) => _getWeaponTarget?.Invoke(weapon, weaponId);
  256.     //public void GetAllCoreTurrets(ICollection<MyDefinitionId> collection) => _getCoreTurrets?.Invoke(collection);
  257.     public MyDetectedEntityInfo? GetAiFocus(long shooter, int priority = 0) => _getAiFocus?.Invoke(shooter, priority);
  258.  
  259.     public bool SetAiFocus(Sandbox.ModAPI.Ingame.IMyTerminalBlock pBlock, long target, int priority = 0) =>
  260.         _setAiFocus?.Invoke(pBlock, target, priority) ?? false;
  261.  
  262.     public void ToggleWeaponFire(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, bool on, bool allWeapons, int weaponId = 0) =>
  263.         _toggleWeaponFire?.Invoke(weapon, on, allWeapons, weaponId);
  264.     public bool HasCoreWeapon(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon) => _hasCoreWeapon?.Invoke(weapon) ?? false;
  265.  
  266.     public void GetObstructions(Sandbox.ModAPI.Ingame.IMyTerminalBlock pBlock, ICollection<Sandbox.ModAPI.Ingame.MyDetectedEntityInfo> collection) =>
  267.         _getObstructions?.Invoke(pBlock, collection);
  268.  
  269.     public Vector3D? GetPredictedTargetPosition(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, long targetEnt, int weaponId) =>
  270.         _getPredictedTargetPos?.Invoke(weapon, targetEnt, weaponId) ?? null;
  271.  
  272.     public Matrix GetWeaponAzimuthMatrix(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, int weaponId) =>
  273.         _getWeaponAzimuthMatrix?.Invoke(weapon, weaponId) ?? Matrix.Zero;
  274.  
  275.     public Matrix GetWeaponElevationMatrix(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, int weaponId) =>
  276.         _getWeaponElevationMatrix?.Invoke(weapon, weaponId) ?? Matrix.Zero;
  277.  
  278.     public MyTuple<bool, Vector3D?> IsTargetAlignedExtended(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, long targetEnt, int weaponId) =>
  279.         _isTargetAlignedExtended?.Invoke(weapon, targetEnt, weaponId) ?? new MyTuple<bool, Vector3D?>();
  280.     public string GetActiveAmmo(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, int weaponId) =>
  281.         _getActiveAmmo?.Invoke(weapon, weaponId) ?? null;
  282.  
  283.     public void SetActiveAmmo(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, int weaponId, string ammoType) =>
  284.         _setActiveAmmo?.Invoke(weapon, weaponId, ammoType);
  285.  
  286.     public float GetConstructEffectiveDps(long entity) => _getConstructEffectiveDps?.Invoke(entity) ?? 0f;
  287.  
  288.     public MyDetectedEntityInfo? GetWeaponTarget(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, int weaponId = 0) =>
  289.         _getWeaponTarget?.Invoke(weapon, weaponId);
  290.  
  291.     public void SetWeaponTarget(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, long target, int weaponId = 0) =>
  292.         _setWeaponTarget?.Invoke(weapon, target, weaponId);
  293.  
  294.     public void MonitorProjectileCallback(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, int weaponId, Action<long, int, ulong, long, Vector3D, bool> action) =>
  295.         _monitorProjectile?.Invoke(weapon, weaponId, action);
  296.  
  297.     public void UnMonitorProjectileCallback(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, int weaponId, Action<long, int, ulong, long, Vector3D, bool> action) =>
  298.         _unMonitorProjectile?.Invoke(weapon, weaponId, action);
  299.  
  300.     //// POs, Dir, baseDamageLeft, HealthLeft, TargetEntityId, AmmoName
  301.     public MyTuple<Vector3D, Vector3D, float, float, long, string> GetProjectileState(ulong projectileId) =>
  302.         _getProjectileState?.Invoke(projectileId) ?? new MyTuple<Vector3D, Vector3D, float, float, long, string>();
  303.  
  304.     public bool GetBlockWeaponMap(Sandbox.ModAPI.Ingame.IMyTerminalBlock weaponBlock, IDictionary<string, int> collection) =>
  305.         _getBlockWeaponMap?.Invoke(weaponBlock, collection) ?? false;
  306.  
  307.     public MyTuple<bool, int, int> GetProjectilesLockedOn(long victim) =>
  308.         _getProjectilesLockedOn?.Invoke(victim) ?? new MyTuple<bool, int, int>();
  309.  
  310.     public void FireWeaponOnce(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, bool allWeapons = true, int weaponId = 0) =>
  311.             _fireWeaponOnce?.Invoke(weapon, allWeapons, weaponId);
  312.  
  313.  
  314.     public bool IsWeaponReadyToFire(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, int weaponId = 0, bool anyWeaponReady = true,
  315.         bool shootReady = false) =>
  316.         _isWeaponReadyToFire?.Invoke(weapon, weaponId, anyWeaponReady, shootReady) ?? false;
  317.  
  318.     public float GetMaxWeaponRange(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, int weaponId) =>
  319.         _getMaxWeaponRange?.Invoke(weapon, weaponId) ?? 0f;
  320.  
  321.     public MyTuple<Vector3D, Vector3D> GetWeaponScope(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon, int weaponId) =>
  322.         _getWeaponScope?.Invoke(weapon, weaponId) ?? new MyTuple<Vector3D, Vector3D>();
  323.     public float GetCurrentPower(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon) => _currentPowerConsumption?.Invoke(weapon) ?? 0f;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement