internal void Scan()
        {
            using (_scanLock.AcquireExclusiveUsing())
            {
                if (!Scanning && Session.Tick - _lastScan > 100)
                {
                    Scanning          = true;
                    _lastScan         = Session.Tick;
                    GridVolume.Radius = MaxTargetingRange;
                    MyGamePruningStructure.GetAllTopMostEntitiesInSphere(ref GridVolume, _possibleTargets);
                    foreach (var grid in PrevSubGrids)
                    {
                        RemSubGrids.Add(grid);
                    }

                    PrevSubGrids.Clear();
                    for (int i = 0; i < _possibleTargets.Count; i++)
                    {
                        var ent = _possibleTargets[i];
                        using (ent.Pin())
                        {
                            if (ent is MyVoxelBase || ent.Physics == null || ent is MyFloatingObject ||
                                ent.MarkedForClose || !ent.InScene || ent.IsPreview || ent.Physics.IsPhantom)
                            {
                                continue;
                            }

                            var grid = ent as MyCubeGrid;
                            if (grid != null && MyGrid.IsSameConstructAs(grid))
                            {
                                PrevSubGrids.Add(grid);
                                continue;
                            }

                            Sandbox.ModAPI.Ingame.MyDetectedEntityInfo entInfo;
                            if (!CreateEntInfo(ent, MyOwner, out entInfo))
                            {
                                continue;
                            }

                            switch (entInfo.Relationship)
                            {
                            case MyRelationsBetweenPlayerAndBlock.Owner:
                            case MyRelationsBetweenPlayerAndBlock.FactionShare:
                            case MyRelationsBetweenPlayerAndBlock.Friends:
                                continue;
                            }

                            if (grid != null)
                            {
                                FatMap fatMap;
                                if (!Session.GridToFatMap.TryGetValue(grid, out fatMap) || fatMap.Trash)
                                {
                                    continue;
                                }

                                var allFat   = fatMap.MyCubeBocks;
                                var fatCount = allFat.Count;

                                if (fatCount <= 0 || !grid.IsPowered)
                                {
                                    continue;
                                }

                                if (fatCount <= 20) // possible debris
                                {
                                    var valid = false;
                                    for (int j = 0; j < fatCount; j++)
                                    {
                                        var fat = allFat[j];
                                        if (fat is IMyTerminalBlock && fat.IsWorking)
                                        {
                                            valid = true;
                                            break;
                                        }
                                    }
                                    if (!valid)
                                    {
                                        continue;
                                    }
                                }

                                int    partCount;
                                GridAi targetAi;
                                if (Session.GridTargetingAIs.TryGetValue(grid, out targetAi))
                                {
                                    targetAi.TargetAisTmp.Add(this);
                                    TargetAisTmp.Add(targetAi);
                                    partCount = targetAi.Construct.BlockCount;
                                }
                                else
                                {
                                    partCount = fatMap.MostBlocks;
                                }

                                NewEntities.Add(new DetectInfo(Session, ent, entInfo, partCount, fatCount));
                                ValidGrids.Add(ent);
                            }
                            else
                            {
                                NewEntities.Add(new DetectInfo(Session, ent, entInfo, 1, 0));
                            }
                        }
                    }
                    FinalizeTargetDb();
                    SubGridDetect();
                }
                Scanning = false;
            }
        }
示例#2
0
        internal void Scan()
        {
            MyGamePruningStructure.GetAllTopMostEntitiesInSphere(ref ScanVolume, _possibleTargets);
            NearByEntitiesTmp = _possibleTargets.Count;

            foreach (var grid in PrevSubGrids)
            {
                RemSubGrids.Add((MyCubeGrid)grid);
            }

            PrevSubGrids.Clear();
            for (int i = 0; i < NearByEntitiesTmp; i++)
            {
                var ent = _possibleTargets[i];
                using (ent.Pin()) {
                    if (ent is MyVoxelBase || ent.Physics == null || ent is MyFloatingObject || ent.MarkedForClose || !ent.InScene || ent.IsPreview || ent.Physics.IsPhantom)
                    {
                        continue;
                    }

                    var grid = ent as MyCubeGrid;
                    if (grid != null && MyGrid.IsSameConstructAs(grid))
                    {
                        PrevSubGrids.Add(grid);
                        continue;
                    }

                    Sandbox.ModAPI.Ingame.MyDetectedEntityInfo entInfo;
                    if (!CreateEntInfo(ent, AiOwner, out entInfo))
                    {
                        continue;
                    }

                    switch (entInfo.Relationship)
                    {
                    case MyRelationsBetweenPlayerAndBlock.Owner:
                    case MyRelationsBetweenPlayerAndBlock.FactionShare:
                    case MyRelationsBetweenPlayerAndBlock.Friends:
                        continue;
                    }

                    if (grid != null)
                    {
                        GridMap gridMap;
                        if (!Session.GridToInfoMap.TryGetValue(grid, out gridMap) || gridMap.Trash)
                        {
                            continue;
                        }

                        var allFat   = gridMap.MyCubeBocks;
                        var fatCount = allFat.Count;

                        if (fatCount <= 0)
                        {
                            continue;
                        }

                        var loneWarhead = false;
                        if (fatCount <= 20)    // possible debris

                        {
                            var valid = false;
                            for (int j = 0; j < fatCount; j++)
                            {
                                var fat     = allFat[j];
                                var warhead = fat is IMyWarhead;
                                if (warhead || fat is IMyTerminalBlock && fat.IsWorking)
                                {
                                    loneWarhead = warhead && fatCount == 1;
                                    valid       = true;
                                    break;
                                }
                            }
                            if (!valid)
                            {
                                continue;
                            }
                        }
                        int    partCount;
                        GridAi targetAi;
                        if (Session.GridTargetingAIs.TryGetValue(grid, out targetAi))
                        {
                            targetAi.TargetAisTmp.Add(this);
                            TargetAisTmp.Add(targetAi);
                            partCount = targetAi.Construct.BlockCount;
                        }
                        else
                        {
                            partCount = gridMap.MostBlocks;
                        }

                        NewEntities.Add(new DetectInfo(Session, ent, entInfo, partCount, !loneWarhead? fatCount : 2));// bump warhead to 2 fatblocks so its not ignored by targeting
                        ValidGrids.Add(ent);
                    }
                    else
                    {
                        NewEntities.Add(new DetectInfo(Session, ent, entInfo, 1, 0));
                    }
                }
            }
            FinalizeTargetDb();
            SubGridDetect();
        }