internal void WebMonitor()
        {
            try
            {
                MyAPIGateway.Parallel.ForEach(FunctionalShields.Keys, s =>
                {
                    if (s.MarkedForClose || !s.Warming)
                    {
                        return;
                    }
                    var reInforce = s.DsState.State.ReInforce;
                    if (!IsServer)
                    {
                        if (reInforce != s.ReInforcedShield)
                        {
                            foreach (var sub in s.ShieldComp.SubGrids)
                            {
                                EntRefreshQueue.Enqueue(sub.Key);
                            }
                            s.ReInforcedShield = reInforce;
                        }

                        if (EntSlotTick && RefreshCycle == s.MonitorSlot)
                        {
                            var newSubClient = false;

                            var monitorListClient = ListMyEntityPool.Get();

                            MonitorRefreshTasks(s, ref monitorListClient, reInforce, ref newSubClient);

                            monitorListClient.Clear();
                            ListMyEntityPool.Return(monitorListClient);
                        }
                        s.TicksWithNoActivity = 0;
                        s.LastWokenTick       = Tick;
                        s.Asleep = false;
                        return;
                    }

                    var shieldActive = ActiveShields.ContainsKey(s);

                    if (s.LostPings > 59)
                    {
                        if (shieldActive)
                        {
                            if (Enforced.Debug >= 2)
                            {
                                Log.Line("Logic Paused by lost pings");
                            }

                            byte ignore;
                            ActiveShields.TryRemove(s, out ignore);
                            s.WasPaused = true;
                        }
                        s.Asleep = false;
                        return;
                    }
                    if (Enforced.Debug >= 2 && s.LostPings > 0)
                    {
                        Log.Line($"Lost Logic Pings:{s.LostPings}");
                    }

                    if (shieldActive)
                    {
                        s.LostPings++;
                    }

                    if (!shieldActive && s.LostPings > 59)
                    {
                        s.Asleep = true;
                        return;
                    }

                    var newSub = false;

                    var monitorList = ListMyEntityPool.Get();
                    if (EntSlotTick && RefreshCycle == s.MonitorSlot)
                    {
                        MonitorRefreshTasks(s, ref monitorList, reInforce, ref newSub);
                    }

                    if (reInforce)
                    {
                        return;
                    }
                    if (Tick < s.LastWokenTick + 400 || s.Missiles.Count > 0)
                    {
                        s.Asleep = false;
                        return;
                    }

                    if (s.GridIsMobile && s.MyGrid.Physics.IsMoving)
                    {
                        s.LastWokenTick = Tick;
                        s.Asleep        = false;
                        return;
                    }

                    if (!s.PlayerByShield && !s.MoverByShield && !s.NewEntByShield && s.MyGrid.OccupiedBlocks.Count == 0)
                    {
                        if (s.TicksWithNoActivity++ % EntCleanCycle == 0)
                        {
                            s.EntCleanUpTime = true;
                        }
                        if (shieldActive && !s.WasPaused && Tick > 1200)
                        {
                            if (Enforced.Debug >= 2)
                            {
                                Log.Line($"Logic Paused by monitor");
                            }
                            byte ignore;
                            ActiveShields.TryRemove(s, out ignore);
                            s.WasPaused           = true;
                            s.Asleep              = false;
                            s.TicksWithNoActivity = 0;
                            s.LastWokenTick       = Tick;
                        }
                        else
                        {
                            s.Asleep = true;
                        }
                        return;
                    }

                    var intersect = false;
                    if (!(EntSlotTick && RefreshCycle == s.MonitorSlot))
                    {
                        MyGamePruningStructure.GetTopmostEntitiesInBox(ref s.WebBox, monitorList, MyEntityQueryType.Dynamic);
                    }
                    for (int i = 0; i < monitorList.Count; i++)
                    {
                        var ent = monitorList[i];

                        if (ent.Physics == null || ent.Physics.IsPhantom || !(ent is MyCubeGrid || ent is IMyCharacter || ent is IMyMeteor))
                        {
                            continue;
                        }
                        if (ent.Physics.IsMoving)
                        {
                            if (s.WebBox.Intersects(ent.PositionComp.WorldAABB))
                            {
                                intersect = true;
                                break;
                            }
                        }
                    }

                    monitorList.Clear();
                    ListMyEntityPool.Return(monitorList);

                    if (!intersect)
                    {
                        s.Asleep = true;
                        return;
                    }
                    s.TicksWithNoActivity = 0;
                    s.LastWokenTick       = Tick;
                    s.Asleep = false;
                });

                if (Tick % 180 == 0 && Tick > 1199)
                {
                    foreach (var info in _globalEntTmp)
                    {
                        if (Tick - 540 > info.Value)
                        {
                            var ent = info.Key;
                            EntRefreshQueue.Enqueue(ent);
                            uint value;
                            _globalEntTmp.TryRemove(ent, out value);
                        }
                    }
                }
            }
            catch (Exception ex) { Log.Line($"Exception in WebMonitor: {ex}"); }
        }
Beispiel #2
0
        internal void WebMonitor()
        {
            try
            {
                while (Monitor)
                {
                    _autoResetEvent.WaitOne();
                    if (!Monitor)
                    {
                        break;
                    }
                    //if (Enforced.Debug >= 3 && EntSlotTick) Dsutil2.Sw.Restart();
                    _newFrame = false;
                    _workData.DoIt(new List <DefenseShields>(FunctionalShields.Keys), Tick);
                    MinScaler = _workData.MinScaler;
                    MyAPIGateway.Parallel.For(0, _workData.ShieldCnt, x =>
                    {
                        var s    = _workData.ShieldList[x];
                        var tick = _workData.Tick;
                        if (_newFrame || s.MarkedForClose || !s.Warming)
                        {
                            return;
                        }
                        var reInforce = s.DsState.State.ReInforce;
                        if (!IsServer)
                        {
                            lock (s.GetCubesLock)
                            {
                                var cleanDistributor = s.MyGridDistributor != null && s.FuncTask.IsComplete && s.MyGridDistributor.SourcesEnabled != MyMultipleEnabledEnum.NoObjects;
                                if (cleanDistributor)
                                {
                                    s.GridCurrentPower = s.MyGridDistributor.TotalRequiredInputByType(MyResourceDistributorComponent.ElectricityId);
                                    s.GridMaxPower     = s.MyGridDistributor.MaxAvailableResourceByType(MyResourceDistributorComponent.ElectricityId);
                                }

                                if (reInforce != s.ReInforcedShield)
                                {
                                    //if (Enforced.Debug == 4) Log.Line("Client queuing entFresh for reinforced shield");
                                    foreach (var sub in s.ShieldComp.GetSubGrids)
                                    {
                                        _entRefreshQueue.Enqueue(sub);
                                    }
                                    s.ReInforcedShield = reInforce;
                                }
                            }
                            s.TicksWithNoActivity = 0;
                            s.LastWokenTick       = tick;
                            s.Asleep = false;
                            return;
                        }

                        var shieldActive = ActiveShields.ContainsKey(s);
                        if (s.LostPings > 59)
                        {
                            if (shieldActive)
                            {
                                if (Enforced.Debug >= 2)
                                {
                                    Log.Line("Logic Paused by lost pings");
                                }
                                bool value;
                                ActiveShields.TryRemove(s, out value);
                                s.WasPaused = true;
                            }
                            s.Asleep = false;
                            return;
                        }
                        //if (Enforced.Debug >= 2 && s.LostPings > 0) Log.Line($"Lost Logic Pings:{s.LostPings}");
                        if (shieldActive)
                        {
                            s.LostPings++;
                        }

                        lock (s.GetCubesLock)
                        {
                            var cleanDistributor = s.MyGridDistributor != null && s.FuncTask.IsComplete && s.MyGridDistributor.SourcesEnabled != MyMultipleEnabledEnum.NoObjects;
                            if (cleanDistributor)
                            {
                                s.GridCurrentPower = s.MyGridDistributor.TotalRequiredInputByType(MyResourceDistributorComponent.ElectricityId);
                                s.GridMaxPower     = s.MyGridDistributor.MaxAvailableResourceByType(MyResourceDistributorComponent.ElectricityId);
                            }
                        }

                        if (s.Asleep && EmpStore.Count != 0 && Vector3D.DistanceSquared(s.DetectionCenter, EmpWork.EpiCenter) <= SyncDistSqr)
                        {
                            s.TicksWithNoActivity = 0;
                            s.LastWokenTick       = tick;
                            s.Asleep = false;
                            return;
                        }

                        if (!shieldActive && s.LostPings > 59)
                        {
                            s.Asleep = true;
                            return;
                        }

                        List <MyEntity> monitorList = null;
                        var newSub = false;
                        if (!reInforce)
                        {
                            monitorList = new List <MyEntity>();
                        }
                        if (EntSlotTick && RefreshCycle == s.MonitorSlot)
                        {
                            MonitorRefreshTasks(x, ref monitorList, reInforce, ref newSub);
                        }

                        if (reInforce)
                        {
                            return;
                        }

                        if (tick < s.LastWokenTick + 400 || s.ShieldComp.GridIsMoving || s.Missiles.Count > 0)
                        {
                            if (s.ShieldComp.GridIsMoving)
                            {
                                s.LastWokenTick = tick;
                            }
                            s.Asleep = false;
                            return;
                        }
                        if (!s.PlayerByShield && !s.MoverByShield && !s.NewEntByShield)
                        {
                            if (s.TicksWithNoActivity++ % EntCleanCycle == 0)
                            {
                                s.EntCleanUpTime = true;
                            }
                            if (tick > 1200 && !s.WasPaused)
                            {
                                if (Enforced.Debug >= 2)
                                {
                                    Log.Line($"Logic Paused by monitor");
                                }
                                bool value;
                                ActiveShields.TryRemove(s, out value);
                                s.WasPaused           = true;
                                s.Asleep              = false;
                                s.TicksWithNoActivity = 0;
                                s.LastWokenTick       = tick;
                            }
                            else
                            {
                                s.Asleep = true;
                            }
                            return;
                        }

                        var intersect = false;
                        if (!(EntSlotTick && RefreshCycle == s.MonitorSlot))
                        {
                            MyGamePruningStructure.GetTopmostEntitiesInBox(ref s.WebBox, monitorList, MyEntityQueryType.Dynamic);
                        }
                        for (int i = 0; i < monitorList.Count; i++)
                        {
                            var ent = monitorList[i];

                            if (ent.Physics == null || !(ent is MyCubeGrid || ent is IMyCharacter || ent is IMyMeteor))
                            {
                                continue;
                            }
                            if (ent.Physics.IsMoving)
                            {
                                if (s.WebBox.Intersects(ent.PositionComp.WorldAABB))
                                {
                                    intersect = true;
                                    break;
                                }
                            }
                        }

                        if (!intersect)
                        {
                            s.Asleep = true;
                            return;
                        }
                        s.TicksWithNoActivity = 0;
                        s.LastWokenTick       = tick;
                        s.Asleep = false;
                    });

                    if (_workData.Tick % 180 == 0 && _workData.Tick > 1199)
                    {
                        _entRefreshTmpList.Clear();
                        _entRefreshTmpList.AddRange(_globalEntTmp.Where(info => _workData.Tick - 540 > info.Value));
                        foreach (var dict in _entRefreshTmpList)
                        {
                            var ent = dict.Key;
                            _entRefreshQueue.Enqueue(ent);
                            uint value;
                            _globalEntTmp.TryRemove(ent, out value);
                        }
                    }
                    //if (Enforced.Debug >= 3 && EntSlotTick) Dsutil2.StopWatchReport("monitor", -1);
                }
            }
            catch (Exception ex) { Log.Line($"Exception in WebMonitor: {ex}"); }
        }