public void UpdatePower()
 {
     if (PowerDistributor != null)
     {
         PowerDistributor.UpdateBeforeSimulation10();
     }
 }
        public virtual void OnAddedToGroup(MyGridLogicalGroupData group)
        {
            Debug.Assert(group.TerminalSystem != null, "Terminal system is null!");
            TerminalSystem   = group.TerminalSystem;
            PowerDistributor = group.PowerDistributor;
            WeaponSystem     = group.WeaponSystem;

            PowerDistributor.AddConsumer(ThrustSystem);
            PowerDistributor.AddConsumer(GyroSystem);
            PowerDistributor.AddConsumer(ConveyorSystem);

            foreach (var g in m_cubeGrid.BlockGroups)
            {
                TerminalSystem.AddUpdateGroup(g);
            }
            TerminalSystem.GroupAdded   += m_terminalSystem_GroupAdded;
            TerminalSystem.GroupRemoved += m_terminalSystem_GroupRemoved;

            foreach (var block in m_cubeGrid.GetBlocks())
            {
                if (block.FatBlock == null)
                {
                    continue;
                }
                if (!block.FatBlock.MarkedForClose)
                {
                    var functionalBlock = block.FatBlock as MyTerminalBlock;
                    if (functionalBlock != null)
                    {
                        TerminalSystem.Add(functionalBlock);
                    }

                    var producer = block.FatBlock as IMyPowerProducer;
                    if (producer != null)
                    {
                        PowerDistributor.AddProducer(producer);
                    }

                    var consumer = block.FatBlock as IMyPowerConsumer;
                    if (consumer != null)
                    {
                        PowerDistributor.AddConsumer(consumer);
                    }

                    var socketOwner = block.FatBlock as IMyRechargeSocketOwner;
                    if (socketOwner != null)
                    {
                        socketOwner.RechargeSocket.PowerDistributor = group.PowerDistributor;
                    }

                    var weapon = block.FatBlock as IMyGunObject <MyDeviceBase>;
                    if (weapon != null)
                    {
                        WeaponSystem.Register(weapon);
                    }
                }
            }
        }
示例#3
0
        internal void UpdateGridPower()
        {
            try
            {
                LastPowerUpdateTick  = Session.Tick;
                GridAvailablePower   = 0;
                GridMaxPower         = 0;
                GridCurrentPower     = 0;
                BatteryMaxPower      = 0;
                BatteryCurrentOutput = 0;
                BatteryCurrentInput  = 0;

                if (Session.Tick60)
                {
                    foreach (var battery in Batteries)
                    {
                        if (!battery.IsWorking)
                        {
                            continue;
                        }
                        var currentInput  = battery.CurrentInput;
                        var currentOutput = battery.CurrentOutput;
                        var maxOutput     = battery.MaxOutput;
                        if (currentInput > 0)
                        {
                            BatteryCurrentInput += currentInput;
                            if (battery.IsCharging)
                            {
                                BatteryCurrentOutput -= currentInput;
                            }
                            else
                            {
                                BatteryCurrentOutput -= currentInput;
                            }
                        }
                        BatteryMaxPower      += maxOutput;
                        BatteryCurrentOutput += currentOutput;
                    }
                }

                try
                {
                    using (FakeShipController.CubeGrid?.Pin())
                    {
                        if (FakeShipController.CubeGrid == null || FakeShipController.CubeGrid.MarkedForClose || FakeShipController.GridResourceDistributor == null || FakeShipController.GridResourceDistributor != PowerDistributor)
                        {
                            try
                            {
                                if (Weapons.Count > 0)
                                {
                                    var cube = Weapons[Weapons.Count - 1].MyCube;
                                    if (cube != null)
                                    {
                                        using (cube.Pin())
                                        {
                                            if (cube.MarkedForClose || cube.CubeGrid.MarkedForClose && cube.SlimBlock != null)
                                            {
                                                Log.Line($"powerDist cube is not ready");
                                                return;
                                            }
                                            FakeShipController.SlimBlock = cube.SlimBlock;
                                            PowerDistributor             = FakeShipController.GridResourceDistributor;
                                            if (PowerDistributor == null)
                                            {
                                                return;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        Log.Line($"powerDist cube is null");
                                        return;
                                    }
                                }
                                else
                                {
                                    return;
                                }
                            }
                            catch (Exception ex) { Log.Line($"Exception in UpdateGridPower: {ex} - probable null!"); }
                        }

                        if (PowerDistributor == null)
                        {
                            Log.Line($"powerDist is null - check 1");
                            return;
                        }

                        try
                        {
                            GridMaxPower     = PowerDistributor.MaxAvailableResourceByType(GId);
                            GridCurrentPower = PowerDistributor.TotalRequiredInputByType(GId);
                        }
                        catch (Exception ex) { Log.Line($"Exception in UpdateGridPower: {ex} - impossible null!"); }
                    }
                }
                catch (Exception ex) { Log.Line($"Exception in UpdateGridPower: {ex} - unlikely null!"); }


                GridAvailablePower = GridMaxPower - GridCurrentPower;

                GridCurrentPower   += BatteryCurrentInput;
                GridAvailablePower -= BatteryCurrentInput;
                UpdatePowerSources  = false;

                RequestedPowerChanged = Math.Abs(LastRequestedPower - RequestedWeaponsDraw) > 0.001 && LastRequestedPower > 0;
                AvailablePowerChanged = Math.Abs(GridMaxPower - LastAvailablePower) > 0.001 && LastAvailablePower > 0;

                RequestIncrease = LastRequestedPower < RequestedWeaponsDraw;
                PowerIncrease   = LastAvailablePower < GridMaxPower;

                LastAvailablePower = GridMaxPower;
                LastRequestedPower = RequestedWeaponsDraw;

                HadPower = HasPower;
                HasPower = GridMaxPower > 0;
                if (!WasPowered && HasPower)
                {
                    WasPowered = true;
                }

                if (HasPower)
                {
                    return;
                }
                if (HadPower)
                {
                    WeaponShootOff();
                }
            }
            catch (Exception ex) { Log.Line($"Exception in UpdateGridPower: {ex} - SessionNull{Session == null} - FakeShipControllerNull{FakeShipController == null} - PowerDistributorNull{PowerDistributor == null} - MyGridNull{MyGrid == null}"); }
        }