Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******Twinkie Industries Automation Presents*******************************************
- * *
- * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * * * * * * * * * * * * *
- * * * * * * * * * * * * * * * * * *
- * * * * * * * * * * * * * * *
- * * * * * * * * * * * * * * * * * * * * * * * *
- * *
- *******Valkyrie Automation System***********************************************v1.4*****/
- // --------------------------------------------------------------------------------------------------
- // EDIT THESE IF YOU WANT TO
- /// <summary>
- /// Must be present in the title of all blocks controlled...basically every block on the ship.
- /// </summary>
- const string NAME_OF_GRID = "[Herja]";
- /// <summary>
- /// Startup arg - you can pass this arg through the run command to force a startup.
- /// This string is case insensitive.
- /// </summary>
- const string START_KEY = "start";
- /// <summary>
- /// Set to true if this is a drone and the antenna needs to always remain on.
- /// </summary>
- const bool IS_DRONE = true;
- // --------------------------------------------------------------------------------------------------
- // DON'T EDIT ANYTHING BELOW
- // Dear scripters, the member fields are at the bottom so they don't tempt the players to edit them.
- public void Main(string arg)
- {
- //Check status of bird
- MyPowerState currentStatus = UpdateStatus();
- Echo(Enum.GetName(typeof(MyPowerState), currentStatus));
- if (arg.Equals(START_KEY, System.StringComparison.InvariantCultureIgnoreCase) || currentStatus == MyPowerState.Undocking)
- SetPowerOn();
- else if (currentStatus == MyPowerState.Docking)
- SetPowerOff();
- }
- // Turn off the power
- // 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.
- private void SetPowerOff()
- {
- foreach (IMyTerminalBlock block in m_blocks)
- {
- if (!NameCheck(block))
- continue;
- // Order of if-blocks are set for performance
- if (block is IMyShipConnector)
- {
- IMyShipConnector connector = block as IMyShipConnector;
- if (connector.Status != MyShipConnectorStatus.Connected)
- connector.ToggleConnect();
- //connector.GetActionWithName("SwitchLock").Apply(connector);
- continue;
- }
- if (block is IMyReactor ||
- block is IMySensorBlock ||
- block is IMyLightingBlock ||
- block is IMyReflectorLight ||
- block is IMyThrust ||
- block is IMyGyro)
- {
- block.GetActionWithName("OnOff_Off").Apply(block);
- continue;
- }
- if (block is IMyRadioAntenna && !IS_DRONE)
- {
- (block as IMyRadioAntenna).Enabled = false;
- }
- if (block is IMyBatteryBlock)
- {
- (block as IMyBatteryBlock).ChargeMode = ChargeMode.Recharge;
- }
- }
- }
- // Power on the ship and undock
- private void SetPowerOn()
- {
- m_isUndocking = true;
- foreach (IMyTerminalBlock block in m_blocks)
- {
- if (!NameCheck(block))
- continue;
- if (block is IMyReactor)
- {
- (block as IMyReactor).Enabled = true;
- continue;
- }
- if (block is IMyBatteryBlock)
- {
- (block as IMyBatteryBlock).ChargeMode = ChargeMode.Auto;
- }
- }
- }
- private void Undock()
- {
- foreach (IMyTerminalBlock block in m_blocks)
- {
- if (!NameCheck(block))
- continue;
- if (block is IMyShipConnector)
- {
- IMyShipConnector connector = block as IMyShipConnector;
- connector.ToggleConnect();
- //connector.GetActionWithName("SwitchLock").Apply(connector);
- //connector.GetActionWithName("OnOff_Off").Apply(connector);
- continue;
- }
- if (block is IMyLandingGear)
- {
- block.GetActionWithName("Unlock").Apply(block);
- }
- if (block is IMyRadioAntenna ||
- block is IMySensorBlock ||
- block is IMyThrust ||
- block is IMyGyro ||
- (block is IMyLightingBlock && block.CustomName.IndexOf("Cntr") >= 0))
- {
- block.GetActionWithName("OnOff_On").Apply(block);
- }
- }
- }
- // Note: Using some implicit casts in foreach statements without full testing in script environment.
- /// <summary>Updates the bird status variables</summary>
- MyPowerState UpdateStatus()
- {
- // All blocks
- if (m_blocks.Count == 0)
- GridTerminalSystem.GetBlocks(m_blocks);
- // If undocking is in progress then finish that rather than completing the update
- if (m_isUndocking)
- {
- Undock();
- m_isUndocking = false;
- return MyPowerState.On;
- }
- if (m_connectors.Count == 0)
- GridTerminalSystem.GetBlocksOfType<IMyShipConnector>(m_connectors, NameCheck);
- if (m_landingGear.Count == 0)
- GridTerminalSystem.GetBlocksOfType<IMyLandingGear>(m_landingGear, NameCheck);
- if (m_reactors.Count == 0)
- GridTerminalSystem.GetBlocksOfType<IMyReactor>(m_reactors, NameCheck);
- if (m_batteries.Count == 0)
- GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(m_batteries, NameCheck);
- // Connector
- IMyShipConnector connector = m_connectors[0] as IMyShipConnector;//the first connector
- bool m_connectorHasPower = connector.Enabled; //connector is turned on
- bool m_connectorIsPaired = connector.Status != MyShipConnectorStatus.Unconnected;//checks for yellow or green
- bool m_landingGearIsLocked = UpdateLandingGear();
- bool m_birdHasPower = UpdatePower();
- // Decision
- if ((m_connectorHasPower && m_connectorIsPaired && !m_landingGearIsLocked && !m_birdHasPower))
- return MyPowerState.Undocking;
- if (!m_connectorIsPaired && !m_landingGearIsLocked && m_birdHasPower)
- return MyPowerState.On;
- if (m_connectorHasPower && m_connectorIsPaired && m_landingGearIsLocked && m_birdHasPower)
- return MyPowerState.Docking;
- if (m_connectorHasPower && m_connectorIsPaired && m_landingGearIsLocked && !m_birdHasPower)
- return MyPowerState.Off;
- return MyPowerState.Unknown;
- }
- /// <summary>TIοΏ½ NameCheck Filter v2.0 - Returns true if the block contains the string NAME_OF_BIRD in its name.</summary>
- bool NameCheck(IMyTerminalBlock block) => block.CustomName.Contains(NAME_OF_GRID);
- ///<summary>Returns true if any landing gear is locked.</summary>
- private bool UpdateLandingGear()
- {
- bool locked = false;
- if (m_landingGear.Count != 0)
- {
- foreach (IMyLandingGear gear in m_landingGear)
- {
- if (gear.LockMode == LandingGearMode.Locked)
- {
- locked = true;
- break;
- }
- }
- }
- return locked;
- }
- ///<summary>Returns true if there is an available power source.</summary>
- private bool UpdatePower()
- {
- bool m_birdHasPower = false;
- if (m_reactors.Count != 0)
- {
- foreach (IMyReactor reactor in m_reactors)
- {
- if (reactor.Enabled)
- {
- m_birdHasPower = true;
- break;
- }
- }
- }
- if (m_batteries.Count != 0 && !m_birdHasPower)
- {
- foreach (IMyBatteryBlock battery in m_batteries)
- {
- if (battery.Enabled && battery.ChargeMode != ChargeMode.Recharge)
- {
- m_birdHasPower = true;
- break;
- }
- }
- }
- return m_birdHasPower;
- }
- bool m_isUndocking = false;
- List<IMyTerminalBlock> m_blocks = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> m_connectors = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> m_landingGear = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> m_reactors = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> m_batteries = new List<IMyTerminalBlock>();
- enum MyPowerState
- {
- Off,
- On,
- Docking,
- Undocking,
- Unknown
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement