示例#1
0
    private void RunInternal(ZACommons commons, EventDriver eventDriver)
    {
        var lowBattery = ZACommons.GetBlockWithName <IMyTimerBlock>(commons.Blocks, LOW_BATTERY_NAME);

        // Don't bother if there's no timer block or handler
        if (lowBatteryHandler == null && lowBattery == null)
        {
            return;
        }

        var batteries = ZACommons.GetBlocksOfType <IMyBatteryBlock>(commons.Blocks, battery => battery.IsFunctional && ((IMyBatteryBlock)battery).Enabled);

        // Avoid divide-by-zero in case there are no batteries
        if (batteries.Count == 0)
        {
            return;
        }

        var currentStoredPower = 0.0f;
        var maxStoredPower     = 0.0f;

        // Hmm, doesn't check battery recharge state...
        // With the "full-auto mode" (if it worked as advertised),
        // it probably doesn't make sense to check input/output state anyway
        for (var e = batteries.GetEnumerator(); e.MoveNext();)
        {
            var battery = e.Current as IMyBatteryBlock;

            currentStoredPower += battery.CurrentStoredPower;
            maxStoredPower     += battery.MaxStoredPower;
        }

        var batteryPercent = currentStoredPower / maxStoredPower;

        if (!Triggered && batteryPercent < BATTERY_THRESHOLD)
        {
            Triggered = true;
            if (lowBatteryHandler != null)
            {
                lowBatteryHandler.LowBattery(commons, eventDriver, true);
            }
            if (lowBattery != null)
            {
                lowBattery.ApplyAction("Start");
            }
        }
        else if (Triggered && batteryPercent >= BATTERY_THRESHOLD)
        {
            Triggered = false;
            if (lowBatteryHandler != null)
            {
                lowBatteryHandler.LowBattery(commons, eventDriver, false);
            }
        }
    }
    public static bool StartTimerBlockWithName(IEnumerable <IMyTerminalBlock> blocks, string name,
                                               Func <IMyTimerBlock, bool> condition = null)
    {
        var timer = ZACommons.GetBlockWithName <IMyTimerBlock>(blocks, name);

        if (timer != null && timer.Enabled && !timer.IsCountingDown &&
            (condition == null || condition(timer)))
        {
            timer.StartCountdown();
            return(true);
        }
        return(false);
    }