Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //USER SETTINGS
- public string
- enemiesDetectedTimerKeyword = "danger",
- safeTimerKeyword = "safe",
- damagedTimerKeyword = "damage",
- builtTimerKeyword = "built";
- //END OF USER SETTINGS
- public bool currentlyCheckingForEnemies = true, hasTarget = false, hasDamage = false;
- public IMyTimerBlock timerEnemiesDetected, timerSafe, timerDamaged, timerBuilt;
- public int currentDamageBlock = 0, totalDamageBlocks = 0;
- public Program()
- {
- Runtime.UpdateFrequency = UpdateFrequency.Update1;
- List<IMyTimerBlock> timerList = new List<IMyTimerBlock>();
- string tempKey = enemiesDetectedTimerKeyword.ToLower();
- GridTerminalSystem.GetBlocksOfType<IMyTimerBlock>(timerList, b => b.CustomName.ToLower().Contains(tempKey));
- if (timerList.Count > 0) timerEnemiesDetected = timerList[0];
- tempKey = safeTimerKeyword.ToLower();
- GridTerminalSystem.GetBlocksOfType<IMyTimerBlock>(timerList, b => b.CustomName.ToLower().Contains(tempKey));
- if (timerList.Count > 0) timerSafe = timerList[0];
- tempKey = damagedTimerKeyword.ToLower();
- GridTerminalSystem.GetBlocksOfType<IMyTimerBlock>(timerList, b => b.CustomName.ToLower().Contains(tempKey));
- if (timerList.Count > 0) timerDamaged = timerList[0];
- tempKey = damagedTimerKeyword.ToLower();
- GridTerminalSystem.GetBlocksOfType<IMyTimerBlock>(timerList, b => b.CustomName.ToLower().Contains(builtTimerKeyword));
- if (timerList.Count > 0) timerBuilt = timerList[0];
- }
- public void Main(string argument, UpdateType updateSource)
- {
- if (currentlyCheckingForEnemies) CheckForEnemies();
- else
- {
- FindDamage();
- Echo($"Checking Block Health {currentDamageBlock}/{totalDamageBlocks}");
- }
- }
- public void CheckForEnemies()
- {
- Echo("Checking For Enemies");
- bool hadTarget = hasTarget;
- currentlyCheckingForEnemies = false;
- List<IMyLargeTurretBase> turretList = new List<IMyLargeTurretBase>();
- GridTerminalSystem.GetBlocksOfType<IMyLargeTurretBase>(turretList, b => b.HasTarget);
- hasTarget = turretList.Count > 0;
- if (hasTarget != hadTarget)
- {
- try
- {
- if (hasTarget) timerEnemiesDetected.Trigger();
- else timerSafe.Trigger();
- }
- catch { }
- }
- }
- public void FindDamage()
- {
- if (findDamageState == null) findDamageState = FindDamageState();
- bool moreActions = findDamageState.MoveNext();
- if (!moreActions)
- {
- currentlyCheckingForEnemies = true;
- try
- {
- findDamageState.Dispose();
- findDamageState = null;
- }
- catch { }
- }
- }
- public IEnumerator<bool> findDamageState;
- public IEnumerator<bool> FindDamageState()
- {
- List<IMyTerminalBlock> blockList = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(blockList);
- totalDamageBlocks = blockList.Count;
- currentDamageBlock = 0;
- yield return true;
- bool foundDamage = false;
- int reps = 0;
- for (int i = 0; i < blockList.Count; i++)
- {
- try
- {
- IMySlimBlock slimBlock = blockList[i].CubeGrid.GetCubeBlock(blockList[i].Position);
- IMyTerminalBlock block = (IMyTerminalBlock)slimBlock.FatBlock;
- if (slimBlock.CurrentDamage > 0f)
- {
- if (!block.CustomName.StartsWith("*DMG*")) block.CustomName = $"*DMG*{block.CustomName}";
- foundDamage = true;
- }
- else
- {
- if (block.CustomName.StartsWith("*DMG*")) block.CustomName = block.CustomName.Substring(5);
- }
- }
- catch { }
- currentDamageBlock++;
- reps++;
- if (reps >= 10)
- {
- yield return true;
- reps = 0;
- }
- }
- if (foundDamage != hasDamage)
- {
- hasDamage = foundDamage;
- if (hasDamage) timerDamaged.Trigger();
- else timerBuilt.Trigger();
- }
- yield return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement