Пример #1
0
        public Program()
        {
            Runtime.UpdateFrequency = UpdateFrequency.Update100;

            solarsAndWind = new PowerSourceCollection <IMyPowerProducer>(GridTerminalSystem, Me, b => !(b is IMyReactor || b is IMyBatteryBlock));
            batteries     = new BatteryCollection(GridTerminalSystem, Me);
            reactors      = new PowerSourceCollection <IMyReactor>(GridTerminalSystem, Me, b => b.CustomName.Contains("Reactor"));
            hydrogengines = new PowerSourceCollection <IMyReactor>(GridTerminalSystem, Me, b => !b.CustomName.Contains("Reactor"));

            GridTerminalSystem.GetBlocksOfType <IMyTextPanel>(displays, b => b.CustomName.Contains(DISPLAY_TAG) && b.CubeGrid == Me.CubeGrid);
        }
        public Program()
        {
            Runtime.UpdateFrequency = UpdateFrequency.Update100;

            solars        = new PowerSourceCollection(this, Me, b => b is IMyPowerProducer && !(b is IMyReactor || b is IMyBatteryBlock) && b.BlockDefinition.TypeId.ToString().Contains("Solar"));
            wind          = new PowerSourceCollection(this, Me, b => b is IMyPowerProducer && !(b is IMyReactor || b is IMyBatteryBlock) && b.BlockDefinition.TypeId.ToString().Contains("Wind"));
            batteries     = new BatteryCollection(this, Me);
            reactors      = new PowerSourceCollection(this, Me, b => b is IMyReactor && b.BlockDefinition.TypeId.ToString().Contains("Reactor"));
            hydrogengines = new PowerSourceCollection(this, Me, b => b is IMyPowerProducer && b.BlockDefinition.TypeId.ToString().Contains("Hydrogen"));

            GridTerminalSystem.GetBlocksOfType <IMyTextPanel>(displays, b => b.CustomName.Contains(DISPLAY_TAG) && b.CubeGrid == Me.CubeGrid);

            indicatorSize.X = (screenSize.X - edgePadding * 2) / 2;
            indicatorSize.Y = lineSize - lineGap;
        }
        public void Main()           //called each cycle
        {
            List <MySpriteDrawFrame> li = new List <MySpriteDrawFrame>();

            foreach (IMyTextPanel scr in displays)
            {
                li.Add(prepareScreen(scr));
            }

            float has        = batteries.getStoredEnergy();
            float cap        = batteries.getCapacity();
            float f2         = has / cap;
            bool  lowBattery = f2 < BATTERY_REACTOR_MIN;

            float load = getCurrentPowerDemand(Me.CubeGrid);
            float dy   = 0;

            drawText(li, edgePadding, dy, "Grid power demand is " + String.Format("{0:0.000}", load) + "MW");
            dy += lineSize;
            float remaining  = load;
            bool  batteryUse = false;

            foreach (string s in SOURCE_PRIORITY)
            {
                PowerSourceCollection c = getCollection(s);
                float capacity          = c.getMaxGeneration();
                if (capacity <= 0)
                {
                    Echo("Power source type " + s + " is unavailable. Skipping.");
                    continue;
                }
                bool enable = remaining > 0;
                if (s == "Battery")
                {
                    bool allowRecharge = true;
                    if (lowBattery)
                    {
                        if (enable)
                        {
                            allowRecharge = ENABLE_BATTERY_CHARGE_IF_LOW;
                        }
                        enable = false;
                    }
                    if (enable)
                    {
                        batteries.setCharging(false, true);
                        batteryUse = true;
                    }
                    else
                    {
                        batteries.setCharging(allowRecharge, false);
                    }
                }
                else
                {
                    c.setEnabled(enable);
                }
                remaining -= capacity;
                float f = c.getTotalGeneration() / capacity;
                drawText(li, edgePadding, dy, s + ": " + String.Format("{0:0.0}", f * 100) + " % x " + String.Format("{0:0.000}", capacity) + "MW");
                drawBox(li, screenSize.X / 2 + edgePadding / 2, dy + lineSize, indicatorSize.X - edgePadding, indicatorSize.Y, GRAY);
                drawBox(li, screenSize.X / 2 + edgePadding / 2, dy + lineSize - 4, indicatorSize.X - edgePadding, indicatorSize.Y - 8, enable ? YELLOW_COLOR : RED_COLOR);
                if (f > 0)
                {
                    drawBox(li, screenSize.X / 2 + edgePadding / 2, dy + lineSize - 4, (indicatorSize.X - edgePadding) * f - 4, indicatorSize.Y - 8, GREEN_COLOR);
                }
                Echo("Power source type " + s + ": " + (enable ? "In Use, producing up to " + capacity + "MW" : "Offline"));
                dy += lineSize;
            }

            dy += lineSize * 2;
            Color batColor = lowBattery ? (tick % 2 == 0 ? RED_COLOR : YELLOW_COLOR) : Color.White;

            drawText(li, edgePadding, dy, "Batteries: " + String.Format("{0:0}", has) + "MWh / " + String.Format("{0:0}", cap) + "MWh", batColor);
            Color indicator = batteryUse ? YELLOW_COLOR : BLUE_COLOR;

            drawBox(li, screenSize.X / 2 + edgePadding / 2, dy + lineSize, indicatorSize.X - edgePadding, indicatorSize.Y, GRAY);
            drawBox(li, screenSize.X / 2 + edgePadding / 2, dy + lineSize - 4, (indicatorSize.X - edgePadding) * f2 - 4, indicatorSize.Y - 8, indicator);
            if (batteryUse)
            {
                Echo("Batteries are discharging.");
            }
            else
            {
                Echo("Batteries are recharging.");
            }

            dy += lineSize * 2;

            if (remaining > 0)
            {
                drawText(li, edgePadding, dy, "Power supply exceeded by " + remaining + "MW!", Color.Red);
            }

            foreach (MySpriteDrawFrame frame in li)
            {
                frame.Dispose();
            }
            tick++;
        }