Whiplash141

Whip's Camera Tripwire Script v5

Aug 17th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. /*
  2. /// Whip's Raycast Tripwire Code v5 - 8/6/17 ///
  3. _____________________________________________________________________________________________________
  4. ///DESCRIPTION///
  5.  
  6.     The code uses camera(s) to serve as tripwires to trigger warheads and timers. The code will
  7. grab all warheads on the grid as this is designed for torpedo systems. You only need to name
  8. the cameras and (optionally) the timers.
  9.  
  10.     You can configure the range of the tripwire below in the VARIABLES section. Also the code will
  11. ignore planets and friendly targets by default, but you can change this behavior as well in the
  12. VARIABLES section.
  13.  
  14.     This code also has a minimum arming time where the tripwires will be inactive. This time is counted
  15. after the first triggering of this code.
  16. _____________________________________________________________________________________________________
  17. ///INSTRUCTIONS///
  18.  
  19. 1.) Place this code in a program block
  20. 2.) Make a timer block with the actions:
  21.     - Trigger Now itself
  22.     - Start itself
  23.     - Run this program
  24. 3.) Add "Tripwire" into the name of your camera
  25. 4.) (Optional): Add "Tripwire" to the name of any timer you want triggered when the tripwire is tripped
  26. 5.) Trigger the timer to begin the arming sequence
  27.  
  28. */
  29.  
  30. //===================================================================================================
  31. // VARIABLES - You can modify these
  32. //===================================================================================================
  33. const string cameraName = "Tripwire"; //name of cameras to serve as tripwires
  34. const string timerName = "Tripwire"; //Name of timers to be triggered on tripwire being crossed
  35. const double range = 4; //range of tripwire (forward of camera's face)
  36. const double minumumArmTime = 3; //time after first triggering that the tripwire cameras will not be armed
  37. const bool ignorePlanetSurface = true; //if the code should ignore planet surfaces
  38. const bool ignoreFriends = true; //if the code should ignore friendlies
  39.  
  40. //===================================================================================================
  41. // DO NOT TOUCH ANYTHING BELOW // DO NOT TOUCH ANYTHING BELOW // DO NOT TOUCH ANYTHING BELOW //
  42. //===================================================================================================
  43.  
  44. double currentTimeElapsed = 0;
  45.  
  46. void Main()
  47. {
  48.     if (currentTimeElapsed < minumumArmTime)
  49.     {
  50.         currentTimeElapsed += Runtime.TimeSinceLastRun.TotalSeconds;
  51.         Echo($"Arming... \nTime Left: {minumumArmTime - currentTimeElapsed}");
  52.         return;
  53.     }
  54.  
  55.     Echo("< Tripwire Armed >");
  56.     Echo($"Range: {range} m");
  57.  
  58.     var cameras = new List<IMyCameraBlock>();
  59.     GridTerminalSystem.GetBlocksOfType(cameras, block => block.CustomName.Contains(cameraName));
  60.  
  61.     if (cameras.Count == 0)
  62.     {
  63.         Echo($"Error: No camera named '{cameraName}' was found");
  64.         return;
  65.     }
  66.     Echo($"Camera Count: {cameras.Count}");
  67.  
  68.     foreach (IMyCameraBlock thisCamera in cameras)
  69.     {
  70.         thisCamera.EnableRaycast = true;
  71.         var targetInfo = thisCamera.Raycast(range);
  72.  
  73.         if (targetInfo.IsEmpty())
  74.         {
  75.             Echo("No target detected");
  76.             continue;
  77.         }
  78.         else if (ignorePlanetSurface && targetInfo.Type.ToString() == "Planet")
  79.         {
  80.             Echo("Planet detected\nIgnoring...");
  81.             continue;
  82.         }
  83.         else if(ignoreFriends && (targetInfo.Relationship == MyRelationsBetweenPlayerAndBlock.FactionShare || targetInfo.Relationship == MyRelationsBetweenPlayerAndBlock.Owner))
  84.         {
  85.             Echo("Friendly detected\nIgnoring...");
  86.             continue;
  87.         }
  88.         else
  89.         {
  90.             Echo("Target detected");
  91.             Detonate();
  92.             Trigger();
  93.             return;
  94.         }
  95.     }
  96. }
  97.  
  98. void Detonate()
  99. {
  100.     var warheads = new List<IMyWarhead>();
  101.     GridTerminalSystem.GetBlocksOfType(warheads);
  102.     foreach (var thisWarhead in warheads)
  103.     {
  104.         thisWarhead.SetValue<bool>("Safety", true);
  105.  
  106.         if (thisWarhead.CustomName.ToLower().Contains("start"))
  107.             thisWarhead.ApplyAction("StartCountdown");
  108.         else
  109.             thisWarhead.ApplyAction("Detonate");
  110.     }
  111. }
  112.  
  113. void Trigger()
  114. {
  115.     var timers = new List<IMyTimerBlock>();
  116.     GridTerminalSystem.GetBlocksOfType(timers, block => block.CustomName.Contains(timerName));
  117.     foreach (IMyWarhead thisTimer in timers)
  118.     {
  119.         thisTimer.ApplyAction("TriggerNow");
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment