Advertisement
NickNDS

Alert Script

Apr 17th, 2020
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.01 KB | None | 0 0
  1. //USER SETTINGS
  2.  
  3. public string
  4.     enemiesDetectedTimerKeyword = "danger",
  5.     safeTimerKeyword = "safe",
  6.     damagedTimerKeyword = "damage",
  7.     builtTimerKeyword = "built";
  8.  
  9. //END OF USER SETTINGS
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. public bool currentlyCheckingForEnemies = true, hasTarget = false, hasDamage = false;
  25.  
  26. public IMyTimerBlock timerEnemiesDetected, timerSafe, timerDamaged, timerBuilt;
  27.  
  28. public int currentDamageBlock = 0, totalDamageBlocks = 0;
  29.  
  30. public Program()
  31. {
  32.     Runtime.UpdateFrequency = UpdateFrequency.Update1;
  33.  
  34.     List<IMyTimerBlock> timerList = new List<IMyTimerBlock>();
  35.  
  36.     string tempKey = enemiesDetectedTimerKeyword.ToLower();
  37.     GridTerminalSystem.GetBlocksOfType<IMyTimerBlock>(timerList, b => b.CustomName.ToLower().Contains(tempKey));
  38.     if (timerList.Count > 0) timerEnemiesDetected = timerList[0];
  39.  
  40.     tempKey = safeTimerKeyword.ToLower();
  41.     GridTerminalSystem.GetBlocksOfType<IMyTimerBlock>(timerList, b => b.CustomName.ToLower().Contains(tempKey));
  42.     if (timerList.Count > 0) timerSafe = timerList[0];
  43.  
  44.     tempKey = damagedTimerKeyword.ToLower();
  45.     GridTerminalSystem.GetBlocksOfType<IMyTimerBlock>(timerList, b => b.CustomName.ToLower().Contains(tempKey));
  46.     if (timerList.Count > 0) timerDamaged = timerList[0];
  47.  
  48.     tempKey = damagedTimerKeyword.ToLower();
  49.     GridTerminalSystem.GetBlocksOfType<IMyTimerBlock>(timerList, b => b.CustomName.ToLower().Contains(builtTimerKeyword));
  50.     if (timerList.Count > 0) timerBuilt = timerList[0];
  51.  
  52. }
  53.  
  54. public void Main(string argument, UpdateType updateSource)
  55. {
  56.     if (currentlyCheckingForEnemies) CheckForEnemies();
  57.     else
  58.     {
  59.         FindDamage();
  60.         Echo($"Checking Block Health {currentDamageBlock}/{totalDamageBlocks}");
  61.     }
  62. }
  63.  
  64. public void CheckForEnemies()
  65. {
  66.     Echo("Checking For Enemies");
  67.     bool hadTarget = hasTarget;
  68.     currentlyCheckingForEnemies = false;
  69.     List<IMyLargeTurretBase> turretList = new List<IMyLargeTurretBase>();
  70.     GridTerminalSystem.GetBlocksOfType<IMyLargeTurretBase>(turretList, b => b.HasTarget);
  71.     hasTarget = turretList.Count > 0;
  72.  
  73.     if (hasTarget != hadTarget)
  74.     {
  75.         try
  76.         {
  77.             if (hasTarget) timerEnemiesDetected.Trigger();
  78.             else timerSafe.Trigger();
  79.         }
  80.         catch { }
  81.     }
  82. }
  83.  
  84. public void FindDamage()
  85. {
  86.     if (findDamageState == null) findDamageState = FindDamageState();
  87.  
  88.     bool moreActions = findDamageState.MoveNext();
  89.  
  90.     if (!moreActions)
  91.     {
  92.         currentlyCheckingForEnemies = true;
  93.         try
  94.         {
  95.             findDamageState.Dispose();
  96.             findDamageState = null;
  97.         }
  98.         catch { }
  99.     }
  100. }
  101.  
  102. public IEnumerator<bool> findDamageState;
  103.  
  104. public IEnumerator<bool> FindDamageState()
  105. {
  106.     List<IMyTerminalBlock> blockList = new List<IMyTerminalBlock>();
  107.     GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(blockList);
  108.     totalDamageBlocks = blockList.Count;
  109.     currentDamageBlock = 0;
  110.     yield return true;
  111.  
  112.     bool foundDamage = false;
  113.     int reps = 0;
  114.  
  115.     for (int i = 0; i < blockList.Count; i++)
  116.     {
  117.         try
  118.         {
  119.             IMySlimBlock slimBlock = blockList[i].CubeGrid.GetCubeBlock(blockList[i].Position);
  120.             IMyTerminalBlock block = (IMyTerminalBlock)slimBlock.FatBlock;
  121.             if (slimBlock.CurrentDamage > 0f)
  122.             {
  123.                 if (!block.CustomName.StartsWith("*DMG*")) block.CustomName = $"*DMG*{block.CustomName}";
  124.                 foundDamage = true;
  125.             }
  126.             else
  127.             {
  128.                 if (block.CustomName.StartsWith("*DMG*")) block.CustomName = block.CustomName.Substring(5);
  129.             }
  130.         }
  131.         catch { }
  132.         currentDamageBlock++;
  133.         reps++;
  134.         if (reps >= 10)
  135.         {
  136.             yield return true;
  137.             reps = 0;
  138.         }
  139.     }
  140.  
  141.     if (foundDamage != hasDamage)
  142.     {
  143.         hasDamage = foundDamage;
  144.         if (hasDamage) timerDamaged.Trigger();
  145.         else timerBuilt.Trigger();
  146.     }
  147.  
  148.     yield return false;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement