klassekatze

FastMovers

Aug 19th, 2026
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.65 KB | None | 0 0
  1. // R e a d m e
  2. // -----------
  3. //
  4. // Name a LCD: FastMovers
  5. // It will display <N> FAST MOVERS INBOUND if there are incoming torps locked to your grid, else blank.
  6. // You probably also want to put it on hudlcd, see relevant plugin information
  7. //
  8. IMyTextSurface logscreen = null;
  9. public Program()
  10. {
  11.     List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  12.            
  13.     GridTerminalSystem.GetBlocksOfType(blocks);
  14.     for (int i = 0; i < blocks.Count; i++)
  15.     {
  16.         var block = blocks[i];
  17.         if (block is IMyTextSurface)
  18.         {
  19.             IMyTextSurface s = (IMyTextSurface)block;
  20.             if (block.Name.Contains("FastMovers"))
  21.             {
  22.                 logscreen = s;
  23.                 if(logscreen.ContentType != ContentType.TEXT_AND_IMAGE)
  24.                 {
  25.                     logscreen.ContentType = ContentType.TEXT_AND_IMAGE;
  26.                 }
  27.             }
  28.         }
  29.     }
  30.     Runtime.UpdateFrequency = UpdateFrequency.Update100;
  31. }
  32. WcPbApi api = new WcPbApi();
  33. string ld = "";
  34. public void Main(string argument, UpdateType updateSource)
  35. {
  36.     if(logscreen != null)
  37.     {
  38.         if (!api.isReady) api.Activate(Me);
  39.         if (api.isReady)
  40.         {
  41.             var plo = api.GetProjectilesLockedOn(Me.CubeGrid.EntityId);
  42.             var plocked = plo.Item2;
  43.             string d = "";
  44.             if(plocked > 0)
  45.             {
  46.                 d = plocked+(plocked > 1 ? " FAST MOVERS INBOUND": " FAST MOVER INBOUND");
  47.             }
  48.             if(d != ld)
  49.             {
  50.                 logscreen.WriteText(d);
  51.                 ld = d;
  52.             }
  53.             Echo(d);
  54.         }
  55.     }
  56. }
  57. }
  58. public class WcPbApi
  59. {
  60.     public string[] WcBlockTypeLabels = new string[]
  61.         {
  62. "Any",
  63. "Offense",
  64. "Utility",
  65. "Power",
  66. "Production",
  67. "Thrust",
  68. "Jumping",
  69. "Steering"        };
  70.  
  71.     private Action<ICollection<MyDefinitionId>> a;
  72.     private Func<IMyTerminalBlock, IDictionary<string, int>, bool> b;
  73.     private Action<IMyTerminalBlock, IDictionary<MyDetectedEntityInfo, float>> c;
  74.     private Func<long, bool> d;
  75.     private Func<long, int, MyDetectedEntityInfo> e;
  76.     private Func<IMyTerminalBlock, long, int, bool> f;
  77.     private Action<IMyTerminalBlock, bool, bool, int> g;
  78.     private Func<IMyTerminalBlock, bool> h;
  79.     private Action<IMyTerminalBlock, ICollection<MyDetectedEntityInfo>> i;
  80.     private Func<IMyTerminalBlock, ICollection<string>, int, bool> j;
  81.     private Action<IMyTerminalBlock, ICollection<string>, int> k;
  82.     private Func<IMyTerminalBlock, long, int, Vector3D?> l;
  83.  
  84.     private Func<IMyTerminalBlock, int, Matrix> m;
  85.     private Func<IMyTerminalBlock, int, Matrix> n;
  86.     private Func<IMyTerminalBlock, long, int, MyTuple<bool, Vector3D?>> o;
  87.     private Func<IMyTerminalBlock, int, string> p;
  88.     private Action<IMyTerminalBlock, int, string> q;
  89.     private Func<long, float> r;
  90.     private Func<IMyTerminalBlock, int, MyDetectedEntityInfo> s;
  91.     private Action<IMyTerminalBlock, long, int> t;
  92.     private Func<long, MyTuple<bool, int, int>> u;
  93.  
  94.     private Action<IMyTerminalBlock, bool, int> v;
  95.     private Func<IMyTerminalBlock, int, bool, bool, bool> w;
  96.     private Func<IMyTerminalBlock, int, float> x;
  97.     private Func<IMyTerminalBlock, int, MyTuple<Vector3D, Vector3D>> y;
  98.     private Func<IMyTerminalBlock, float> _getCurrentPower;
  99.     public Func<Sandbox.ModAPI.Ingame.IMyTerminalBlock, float> _getHeatLevel;
  100.  
  101.     public bool isReady = false;
  102.     IMyTerminalBlock pbBlock = null;
  103.     public bool Activate(IMyTerminalBlock pbBlock)
  104.     {
  105.         this.pbBlock = pbBlock;
  106.         var dict = pbBlock.GetProperty("WcPbAPI")?.As<IReadOnlyDictionary<string, Delegate>>().GetValue(pbBlock);
  107.         if (dict == null) throw new Exception("WcPbAPI failed to activate");
  108.         return ApiAssign(dict);
  109.     }
  110.  
  111.     public bool ApiAssign(IReadOnlyDictionary<string, Delegate> delegates)
  112.     {
  113.         if (delegates == null)
  114.             return false;
  115.         AssignMethod(delegates, "GetCoreWeapons", ref a);
  116.         AssignMethod(delegates, "GetBlockWeaponMap", ref b);
  117.         AssignMethod(delegates, "GetSortedThreats", ref c);
  118.         AssignMethod(delegates, "GetObstructions", ref i);
  119.         AssignMethod(delegates, "HasGridAi", ref d);
  120.         AssignMethod(delegates, "GetAiFocus", ref e);
  121.         AssignMethod(delegates, "SetAiFocus", ref f);
  122.         AssignMethod(delegates, "HasCoreWeapon", ref h);
  123.         AssignMethod(delegates, "GetPredictedTargetPosition", ref l);
  124.         AssignMethod(delegates, "GetTurretTargetTypes", ref j);
  125.         AssignMethod(delegates, "SetTurretTargetTypes", ref k);
  126.         AssignMethod(delegates, "GetWeaponAzimuthMatrix", ref m);
  127.         AssignMethod(delegates, "GetWeaponElevationMatrix", ref n);
  128.         AssignMethod(delegates, "IsTargetAlignedExtended", ref o);
  129.         AssignMethod(delegates, "GetActiveAmmo", ref p);
  130.         AssignMethod(delegates, "SetActiveAmmo", ref q);
  131.         AssignMethod(delegates, "GetConstructEffectiveDps", ref r);
  132.         AssignMethod(delegates, "GetWeaponTarget", ref s);
  133.         AssignMethod(delegates, "SetWeaponTarget", ref t);
  134.         AssignMethod(delegates, "GetProjectilesLockedOn", ref u);
  135.  
  136.         AssignMethod(delegates, "FireWeaponOnce", ref v);
  137.         AssignMethod(delegates, "ToggleWeaponFire", ref g);
  138.         AssignMethod(delegates, "IsWeaponReadyToFire", ref w);
  139.         AssignMethod(delegates, "GetMaxWeaponRange", ref x);
  140.         AssignMethod(delegates, "GetWeaponScope", ref y);
  141.  
  142.         AssignMethod(delegates, "GetCurrentPower", ref _getCurrentPower);
  143.         AssignMethod(delegates, "GetHeatLevel", ref _getHeatLevel);
  144.  
  145.         //Delegate.CreateDelegate(null, null);
  146.  
  147.         isReady = true;
  148.         return true;
  149.     }
  150.     private void AssignMethod<T>(IReadOnlyDictionary<string, Delegate> delegates, string name, ref T field) where T : class
  151.     {
  152.         if (delegates == null)
  153.         {
  154.             field = null;
  155.             return;
  156.         }
  157.         Delegate del;
  158.         if (!delegates.TryGetValue(name, out del))
  159.             throw new Exception($"{GetType().Name} :: Couldn't find {name} delegate of type {typeof(T)}");
  160.         field = del as T;
  161.         if (field == null)
  162.             throw new Exception(
  163.                 $"{GetType().Name} :: Delegate {name} is not type {typeof(T)}, instead it's: {del.GetType()}");
  164.     }
  165.  
  166.     public void GetAllCoreWeapons(ICollection<MyDefinitionId> collection) => a?.Invoke(collection);
  167.     public void GetSortedThreats(IDictionary<MyDetectedEntityInfo, float> collection) =>
  168.         c?.Invoke(pbBlock, collection);
  169.     public bool HasGridAi(long entity) => d?.Invoke(entity) ?? false;
  170.     public MyDetectedEntityInfo? GetAiFocus(long shooter, int priority = 0) => e?.Invoke(shooter, priority);
  171.  
  172.     public bool SetAiFocus(IMyTerminalBlock pBlock, long target, int priority = 0) =>
  173.         f?.Invoke(pBlock, target, priority) ?? false;
  174.  
  175.     public void ToggleWeaponFire(IMyTerminalBlock weapon, bool on, bool allWeapons, int weaponId = 0) =>
  176.         g?.Invoke(weapon, on, allWeapons, weaponId);
  177.     public bool HasCoreWeapon(IMyTerminalBlock weapon) => h?.Invoke(weapon) ?? false;
  178.  
  179.     public void GetObstructions(IMyTerminalBlock pBlock, ICollection<MyDetectedEntityInfo> collection) =>
  180.         i?.Invoke(pBlock, collection);
  181.  
  182.     public Vector3D? GetPredictedTargetPosition(IMyTerminalBlock weapon, long targetEnt, int weaponId) =>
  183.         l?.Invoke(weapon, targetEnt, weaponId) ?? null;
  184.  
  185.     public Matrix GetWeaponAzimuthMatrix(IMyTerminalBlock weapon, int weaponId) =>
  186.         m?.Invoke(weapon, weaponId) ?? Matrix.Zero;
  187.  
  188.     public Matrix GetWeaponElevationMatrix(IMyTerminalBlock weapon, int weaponId) =>
  189.         n?.Invoke(weapon, weaponId) ?? Matrix.Zero;
  190.  
  191.     public MyTuple<bool, Vector3D?> IsTargetAlignedExtended(IMyTerminalBlock weapon, long targetEnt, int weaponId) =>
  192.         o?.Invoke(weapon, targetEnt, weaponId) ?? new MyTuple<bool, Vector3D?>();
  193.     public string GetActiveAmmo(IMyTerminalBlock weapon, int weaponId) =>
  194.         p?.Invoke(weapon, weaponId) ?? null;
  195.  
  196.     public void SetActiveAmmo(IMyTerminalBlock weapon, int weaponId, string ammoType) =>
  197.         q?.Invoke(weapon, weaponId, ammoType);
  198.  
  199.     public float GetConstructEffectiveDps(long entity) => r?.Invoke(entity) ?? 0f;
  200.  
  201.     public MyDetectedEntityInfo? GetWeaponTarget(IMyTerminalBlock weapon, int weaponId = 0) =>
  202.         s?.Invoke(weapon, weaponId);
  203.  
  204.     public void SetWeaponTarget(IMyTerminalBlock weapon, long target, int weaponId = 0) =>
  205.         t?.Invoke(weapon, target, weaponId);
  206.  
  207.     public bool GetBlockWeaponMap(IMyTerminalBlock weaponBlock, IDictionary<string, int> collection) =>
  208.         b?.Invoke(weaponBlock, collection) ?? false;
  209.  
  210.     public MyTuple<bool, int, int> GetProjectilesLockedOn(long victim) =>
  211.         u?.Invoke(victim) ?? new MyTuple<bool, int, int>();
  212.  
  213.     public void FireWeaponOnce(IMyTerminalBlock weapon, bool allWeapons = true, int weaponId = 0) =>
  214.             v?.Invoke(weapon, allWeapons, weaponId);
  215.  
  216.  
  217.     public bool IsWeaponReadyToFire(IMyTerminalBlock weapon, int weaponId = 0, bool anyWeaponReady = true,
  218.         bool shootReady = false) =>
  219.         w?.Invoke(weapon, weaponId, anyWeaponReady, shootReady) ?? false;
  220.  
  221.     public float GetMaxWeaponRange(IMyTerminalBlock weapon, int weaponId) =>
  222.         x?.Invoke(weapon, weaponId) ?? 0f;
  223.  
  224.     public MyTuple<Vector3D, Vector3D> GetWeaponScope(IMyTerminalBlock weapon, int weaponId) =>
  225.         y?.Invoke(weapon, weaponId) ?? new MyTuple<Vector3D, Vector3D>();
  226.     public float GetCurrentPower(IMyTerminalBlock weapon) => _getCurrentPower?.Invoke(weapon) ?? 0f;
  227.  
  228.     public float GetHeatLevel(Sandbox.ModAPI.Ingame.IMyTerminalBlock weapon) => _getHeatLevel?.Invoke(weapon) ?? 0f;
  229.  
Advertisement
Add Comment
Please, Sign In to add comment