Whiplash141

Whip's New Komodo Launch System v3

Nov 20th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.40 KB | None | 0 0
  1. /*
  2. //// Whip's Komodo Launch System Script - revision 03 - 10/21/16 ////
  3. */
  4.  
  5. //Name of missile
  6.     const string missileName = "Missile";
  7.  
  8. //Name of silo door
  9.     const string missileSiloName = "Silo";
  10.  
  11. //Total number of missiles
  12.     int numberOfMissiles = 6; //must be greater or equal to one
  13.  
  14. //---------------------------------------------------------------------------
  15. //NO TOUCH
  16. //---------------------------------------------------------------------------
  17.     int currentMissileNumber = 1;
  18.     bool retry = false;
  19.     List<IMyProgrammableBlock> programs = new List<IMyProgrammableBlock>();
  20.  
  21. void Main(string arg)
  22. {
  23.     Echo("WMI Missile Fire System\n");
  24.  
  25.     if (numberOfMissiles < 1) // mainly for idiot proofing
  26.     {
  27.         Echo("Uhh you can't have less than one missile bro-nacho");
  28.     }
  29.  
  30.     switch (arg.ToLower())
  31.     {
  32.         case "fire":
  33.             FireNextMissile();
  34.             break;
  35.  
  36.         case "reset":
  37.             currentMissileNumber = 1;
  38.             break;
  39.  
  40.         case "kill":
  41.             KillMissile();
  42.             break;
  43.  
  44.         default:
  45.             break;
  46.     }
  47.  
  48.     for (int i = 1; i < numberOfMissiles; i++)
  49.     {
  50.         if(retry) //keeps running until it finds a missile to fire
  51.             FireNextMissile();
  52.     }
  53.     retry = false;
  54. }
  55.  
  56. void FireNextMissile()
  57. {
  58.     List<IMyTerminalBlock> missileTimerList = new List<IMyTerminalBlock>();
  59.     List<IMyTerminalBlock> siloDoorList = new List<IMyTerminalBlock>();
  60.     GridTerminalSystem.SearchBlocksOfName(missileName + " " + currentMissileNumber.ToString(), missileTimerList, IsTimer);
  61.     GridTerminalSystem.SearchBlocksOfName(missileSiloName + " " + currentMissileNumber.ToString(), siloDoorList, IsDoor);
  62.  
  63.     if (missileTimerList.Count == 0) //aborts if no timers are found
  64.     {
  65.         Echo("Skipping: No missiles found with name '" + missileName + " " + currentMissileNumber.ToString() + "'");
  66.         retry = true;
  67.     }
  68.     else {
  69.         retry = false;
  70.  
  71.         //triggers missile timer
  72.         for (int i = 0; i < missileTimerList.Count; i++)
  73.         {
  74.             var thisMissileTimer = missileTimerList[i] as IMyTimerBlock;
  75.  
  76.             if (thisMissileTimer != null)
  77.             {
  78.                 thisMissileTimer.ApplyAction("TriggerNow"); //triggers it
  79.                 Echo(thisMissileTimer.CustomName + " was triggered");
  80.             }
  81.         }
  82.  
  83.         //opens our doors
  84.         for (int i = 0; i < siloDoorList.Count; i++)
  85.         {
  86.             var thisSiloDoor = siloDoorList[i] as IMyDoor;
  87.  
  88.             if (thisSiloDoor != null)
  89.             {
  90.                 thisSiloDoor.ApplyAction("OnOff_On"); //ensures it is on
  91.                 thisSiloDoor.ApplyAction("Open_On"); //triggers it
  92.             }
  93.         }
  94.  
  95.         //adds to active program list
  96.         var tempList = new List<IMyTerminalBlock>();
  97.         GridTerminalSystem.SearchBlocksOfName(missileName + " " + currentMissileNumber.ToString(), tempList, IsProgram);
  98.         foreach(IMyProgrammableBlock thisProgram in tempList)
  99.         {
  100.             programs.Add(thisProgram as IMyProgrammableBlock);
  101.         }
  102.     }
  103.  
  104.     if (currentMissileNumber >= numberOfMissiles)
  105.     {
  106.         currentMissileNumber = 1;
  107.     }
  108.     else {
  109.         currentMissileNumber++;
  110.     }
  111. }
  112.  
  113. void KillMissile()
  114. {
  115.     Echo("Guidance Killed for " + programs.Count.ToString() + " missile(s)");
  116.     foreach (IMyProgrammableBlock thisProgram in programs)
  117.     {
  118.         bool blockExists = DoesBlockExist( thisProgram );
  119.         if ( blockExists )
  120.         {
  121.             thisProgram.TryRun("kill");
  122.         }
  123.     }
  124.     programs.Clear();
  125. }
  126.  
  127. bool DoesBlockExist( IMyTerminalBlock thisBlock )
  128. {
  129.     Vector3I pos = thisBlock.Position;
  130.     bool exists = thisBlock.CubeGrid.CubeExists( pos );
  131.     return exists;
  132. }
  133.  
  134. bool IsProgram(IMyTerminalBlock test_block) //checks if block is program
  135. {
  136.     var cast_program = test_block as IMyProgrammableBlock;
  137.     return cast_program != null;
  138. }
  139.  
  140. bool IsTimer(IMyTerminalBlock test_block) //checks if block is timer
  141. {
  142.     var cast_timer = test_block as IMyTimerBlock;
  143.     return cast_timer != null;
  144. }
  145.  
  146. bool IsDoor(IMyTerminalBlock test_block) //checks if block is timer
  147. {
  148.     var cast_door = test_block as IMyDoor;
  149.     return cast_door != null;
  150. }
Add Comment
Please, Sign In to add comment