Advertisement
LtTwinkie

Valkyrie Automation System v1.4

Jun 16th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.50 KB | None | 0 0
  1. /*******Twinkie Industries Automation Presents*******************************************
  2. *                                                                                       *
  3. *  * * * * *   *           *   * * * * *   *      *   *     *   * * * * *   * * * * *   *
  4. *      *        *         *        *       * *    *   *   *         *       *           *
  5. *      *         *   *   *         *       *  *   *   * *           *       * * * * *   *
  6. *      *          * * * *          *       *    * *   *   *         *       *           *
  7. *      *           *   *       * * * * *   *      *   *     *   * * * * *   * * * * *   *
  8. *                                                                                       *
  9. *******Valkyrie Automation System***********************************************v1.4*****/
  10.  
  11. // --------------------------------------------------------------------------------------------------
  12. //  EDIT THESE IF YOU WANT TO
  13.  
  14. /// <summary>
  15. /// Must be present in the title of all blocks controlled...basically every block on the ship.
  16. /// </summary>
  17. const string NAME_OF_GRID = "[Herja]";
  18.  
  19. /// <summary>
  20. /// Startup arg - you can pass this arg through the run command to force a startup.
  21. /// This string is case insensitive.
  22. /// </summary>
  23. const string START_KEY = "start";
  24.  
  25. /// <summary>
  26. /// Set to true if this is a drone and the antenna needs to always remain on.
  27. /// </summary>
  28. const bool IS_DRONE = true;
  29.  
  30. // --------------------------------------------------------------------------------------------------
  31.  
  32. // DON'T EDIT ANYTHING BELOW
  33.  
  34. // Dear scripters, the member fields are at the bottom so they don't tempt the players to edit them.
  35.  
  36. public void Main(string arg)
  37. {
  38.     //Check status of bird  
  39.     MyPowerState currentStatus = UpdateStatus();
  40.    
  41.     Echo(Enum.GetName(typeof(MyPowerState), currentStatus));
  42.  
  43.     if (arg.Equals(START_KEY, System.StringComparison.InvariantCultureIgnoreCase) || currentStatus == MyPowerState.Undocking)
  44.         SetPowerOn();
  45.  
  46.     else if (currentStatus == MyPowerState.Docking)
  47.         SetPowerOff();
  48.  
  49. }
  50.  
  51. // Turn off the power
  52. // Note: This potentially needs the same two step progression as the set power on / undock sequence if it can be triggered before the connector is locked.
  53. private void SetPowerOff()
  54. {
  55.     foreach (IMyTerminalBlock block in m_blocks)
  56.     {
  57.         if (!NameCheck(block))
  58.             continue;
  59.  
  60.         // Order of if-blocks are set for performance
  61.  
  62.         if (block is IMyShipConnector)
  63.         {
  64.             IMyShipConnector connector = block as IMyShipConnector;
  65.             if (connector.Status != MyShipConnectorStatus.Connected)
  66.                 connector.ToggleConnect();
  67.                 //connector.GetActionWithName("SwitchLock").Apply(connector);
  68.  
  69.             continue;
  70.         }
  71.  
  72.         if (block is IMyReactor ||
  73.             block is IMySensorBlock ||
  74.             block is IMyLightingBlock ||
  75.             block is IMyReflectorLight ||
  76.             block is IMyThrust ||
  77.             block is IMyGyro)
  78.         {
  79.             block.GetActionWithName("OnOff_Off").Apply(block);
  80.             continue;
  81.         }
  82.  
  83.         if (block is IMyRadioAntenna && !IS_DRONE)
  84.         {
  85.             (block as IMyRadioAntenna).Enabled = false;
  86.         }
  87.            
  88.         if (block is IMyBatteryBlock)
  89.         {
  90.             (block as IMyBatteryBlock).ChargeMode = ChargeMode.Recharge;
  91.         }
  92.     }
  93. }
  94.  
  95. // Power on the ship and undock
  96. private void SetPowerOn()
  97. {
  98.     m_isUndocking = true;
  99.  
  100.     foreach (IMyTerminalBlock block in m_blocks)
  101.     {
  102.         if (!NameCheck(block))
  103.             continue;
  104.  
  105.         if (block is IMyReactor)
  106.         {
  107.             (block as IMyReactor).Enabled = true;
  108.             continue;
  109.         }
  110.  
  111.         if (block is IMyBatteryBlock)
  112.         {
  113.             (block as IMyBatteryBlock).ChargeMode = ChargeMode.Auto;
  114.         }
  115.     }
  116. }
  117.  
  118. private void Undock()
  119. {
  120.     foreach (IMyTerminalBlock block in m_blocks)
  121.     {
  122.         if (!NameCheck(block))
  123.             continue;
  124.  
  125.         if (block is IMyShipConnector)
  126.         {
  127.             IMyShipConnector connector = block as IMyShipConnector;
  128.             connector.ToggleConnect();
  129.             //connector.GetActionWithName("SwitchLock").Apply(connector);
  130.             //connector.GetActionWithName("OnOff_Off").Apply(connector);
  131.             continue;
  132.         }
  133.  
  134.         if (block is IMyLandingGear)
  135.         {
  136.             block.GetActionWithName("Unlock").Apply(block);
  137.         }
  138.  
  139.         if (block is IMyRadioAntenna ||
  140.             block is IMySensorBlock ||
  141.             block is IMyThrust ||
  142.             block is IMyGyro ||
  143.             (block is IMyLightingBlock && block.CustomName.IndexOf("Cntr") >= 0))
  144.         {
  145.             block.GetActionWithName("OnOff_On").Apply(block);
  146.         }
  147.     }
  148. }
  149.  
  150. // Note: Using some implicit casts in foreach statements without full testing in script environment.
  151. /// <summary>Updates the bird status variables</summary>  
  152. MyPowerState UpdateStatus()
  153. {
  154.     // All blocks
  155.     if (m_blocks.Count == 0)
  156.         GridTerminalSystem.GetBlocks(m_blocks);
  157.  
  158.     // If undocking is in progress then finish that rather than completing the update
  159.     if (m_isUndocking)
  160.     {
  161.         Undock();
  162.         m_isUndocking = false;
  163.         return MyPowerState.On;
  164.     }
  165.  
  166.     if (m_connectors.Count == 0)
  167.         GridTerminalSystem.GetBlocksOfType<IMyShipConnector>(m_connectors, NameCheck);
  168.     if (m_landingGear.Count == 0)
  169.         GridTerminalSystem.GetBlocksOfType<IMyLandingGear>(m_landingGear, NameCheck);
  170.     if (m_reactors.Count == 0)
  171.         GridTerminalSystem.GetBlocksOfType<IMyReactor>(m_reactors, NameCheck);
  172.     if (m_batteries.Count == 0)
  173.         GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(m_batteries, NameCheck);
  174.  
  175.     // Connector
  176.     IMyShipConnector connector = m_connectors[0] as IMyShipConnector;//the first connector  
  177.     bool m_connectorHasPower = connector.Enabled; //connector is turned on  
  178.     bool m_connectorIsPaired = connector.Status != MyShipConnectorStatus.Unconnected;//checks for yellow or green
  179.  
  180.     bool m_landingGearIsLocked = UpdateLandingGear();
  181.     bool m_birdHasPower = UpdatePower();
  182.  
  183.     // Decision
  184.     if ((m_connectorHasPower && m_connectorIsPaired && !m_landingGearIsLocked && !m_birdHasPower))
  185.         return MyPowerState.Undocking;
  186.  
  187.     if (!m_connectorIsPaired && !m_landingGearIsLocked && m_birdHasPower)
  188.         return MyPowerState.On;
  189.  
  190.     if (m_connectorHasPower && m_connectorIsPaired && m_landingGearIsLocked && m_birdHasPower)
  191.         return MyPowerState.Docking;
  192.  
  193.     if (m_connectorHasPower && m_connectorIsPaired && m_landingGearIsLocked && !m_birdHasPower)
  194.         return MyPowerState.Off;
  195.  
  196.     return MyPowerState.Unknown;
  197. }
  198.  
  199. /// <summary>TIοΏ½ NameCheck Filter v2.0 - Returns true if the block contains the string NAME_OF_BIRD in its name.</summary>  
  200. bool NameCheck(IMyTerminalBlock block) => block.CustomName.Contains(NAME_OF_GRID);
  201.  
  202. ///<summary>Returns true if any landing gear is locked.</summary>
  203. private bool UpdateLandingGear()
  204. {
  205.     bool locked = false;
  206.     if (m_landingGear.Count != 0)
  207.     {
  208.         foreach (IMyLandingGear gear in m_landingGear)
  209.         {
  210.             if (gear.LockMode == LandingGearMode.Locked)
  211.             {
  212.                 locked = true;
  213.                 break;
  214.             }
  215.         }
  216.     }
  217.  
  218.     return locked;
  219. }
  220.  
  221. ///<summary>Returns true if there is an available power source.</summary>
  222. private bool UpdatePower()
  223. {
  224.     bool m_birdHasPower = false;
  225.     if (m_reactors.Count != 0)
  226.     {
  227.         foreach (IMyReactor reactor in m_reactors)
  228.         {
  229.             if (reactor.Enabled)
  230.             {
  231.                 m_birdHasPower = true;
  232.                 break;
  233.             }
  234.         }
  235.     }
  236.  
  237.     if (m_batteries.Count != 0 && !m_birdHasPower)
  238.     {
  239.         foreach (IMyBatteryBlock battery in m_batteries)
  240.         {
  241.             if (battery.Enabled && battery.ChargeMode != ChargeMode.Recharge)
  242.             {
  243.                 m_birdHasPower = true;
  244.                 break;
  245.             }
  246.         }
  247.     }
  248.  
  249.     return m_birdHasPower;
  250. }
  251.  
  252. bool m_isUndocking = false;
  253. List<IMyTerminalBlock> m_blocks = new List<IMyTerminalBlock>();
  254. List<IMyTerminalBlock> m_connectors = new List<IMyTerminalBlock>();
  255. List<IMyTerminalBlock> m_landingGear = new List<IMyTerminalBlock>();
  256. List<IMyTerminalBlock> m_reactors = new List<IMyTerminalBlock>();
  257. List<IMyTerminalBlock> m_batteries = new List<IMyTerminalBlock>();
  258.  
  259. enum MyPowerState
  260. {
  261.     Off,
  262.     On,
  263.     Docking,
  264.     Undocking,
  265.     Unknown
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement