Example #1
0
        internal void UpdateGridPower()
        {
            try
            {
                if (Weapons.Count == 0)
                {
                    Log.Line($"no valid weapon in powerDist");
                    return;
                }

                GridCurrentPower = 0;
                GridMaxPower     = 0;
                for (int i = -1, j = 0; i < Weapons.Count; i++, j++)
                {
                    var powerBlock = j == 0 ? PowerBlock : Weapons[i].MyCube;
                    if (powerBlock == null || j == 0 && PowerDirty)
                    {
                        continue;
                    }

                    using (powerBlock.Pin()) {
                        using (powerBlock.CubeGrid.Pin()) {
                            try {
                                if (powerBlock.MarkedForClose || powerBlock.SlimBlock == null || powerBlock.CubeGrid.MarkedForClose)
                                {
                                    continue;
                                }

                                try {
                                    if (PowerBlock != powerBlock || PowerDistributor?.SourcesEnabled == MyMultipleEnabledEnum.NoObjects)
                                    {
                                        PowerBlock = powerBlock;
                                        FakeShipController.SlimBlock = powerBlock.SlimBlock;
                                        PowerDistributor             = FakeShipController.GridResourceDistributor;
                                        PowerDirty = false;
                                    }
                                }
                                catch (Exception ex) { Log.Line($"Exception in UpdateGridPower: {ex} - Changed PowerBlock!"); }

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

                                try {
                                    GridMaxPower     = PowerDistributor.MaxAvailableResourceByType(GId);
                                    GridCurrentPower = PowerDistributor.TotalRequiredInputByType(GId);
                                    break;
                                }
                                catch (Exception ex) { Log.Line($"Exception in UpdateGridPower: {ex} - impossible null!"); }
                            }
                            catch (Exception ex) { Log.Line($"Exception in UpdateGridPower: {ex} - main null catch"); }
                        }
                    }
                    Log.Line($"no valid power blocks");
                    return;
                }

                if (Session.Tick60)
                {
                    BatteryMaxPower      = 0;
                    BatteryCurrentOutput = 0;
                    BatteryCurrentInput  = 0;

                    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;
                    }
                }

                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;

                LastPowerUpdateTick = Session.Tick;

                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}"); }
        }
Example #2
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}"); }
        }