예제 #1
0
        public virtual void Remove(object target)
        {
            TargetList.Remove(target);

            using (Lock.AcquireExclusiveUsing())
                PotentialTargetList.Remove(target);
        }
        private bool AddPotentialBlock(IMySlimBlock block, bool remote = false, NaniteAreaBeacon beacon = null)
        {
            if (PotentialTargetList.Contains(block))
            {
                return(false);
            }

            if (!remote && block.FatBlock != null && block.FatBlock is IMyTerminalBlock && block.FatBlock.OwnerId != 0 &&
                !MyRelationsBetweenPlayerAndBlockExtensions.IsFriendly(block.FatBlock.GetUserRelationToOwner(m_constructionBlock.ConstructionBlock.OwnerId)))
            {
                return(false);
            }

            if (!block.IsFullIntegrity || block.HasDeformation)
            {
                if (beacon != null)
                {
                    if (!m_areaTargetBlocks.ContainsKey(block))
                    {
                        m_areaTargetBlocks.Add(block, beacon);
                    }
                    else
                    {
                        m_areaTargetBlocks[block] = beacon;
                    }
                }

                PotentialTargetList.Add(block);
                return(true);
            }

            return(false);
        }
        private void RemoveGridTarget(IMyCubeGrid grid)
        {
            foreach (var item in m_validBeaconedGrids)
            {
                if (item.MainGrid == grid)
                {
                    using (Lock.AcquireExclusiveUsing())
                    {
                        foreach (var block in item.RemoveList)
                        {
                            PotentialTargetList.Remove(block);
                            TargetList.Remove(block);
                        }
                    }

                    m_validBeaconedGrids.Remove(item);
                    break;
                }
            }

            if (m_areaTargetBlocks.ContainsKey(grid))
            {
                m_areaTargetBlocks.Remove(grid);
            }
        }
        public override void CompleteTarget(object obj)
        {
            if (m_progressSoundEmitter.IsPlaying)
            {
                m_progressSoundEmitter.StopSound(true);
            }

            var player = obj as IMyPlayer;

            if (player == null)
            {
                return;
            }

            if (Sync.IsServer)
            {
                m_constructionBlock.SendCompleteTarget(player);
            }

            m_constructionBlock.ParticleManager.CompleteTarget(obj);

            foreach (IMyPlayer item in TargetList.Where(x => (IMyPlayer)x == player))
            {
                Logging.Instance.WriteLine(string.Format("COMPLETING Medical Target: {0} - {1} (Player={2},Position={3})", m_constructionBlock.ConstructionBlock.EntityId, item.GetType().Name, item.DisplayName, item.GetPosition()));
            }

            TargetList.RemoveAll(x => ((IMyPlayer)x).IdentityId == player.IdentityId);
            PotentialTargetList.RemoveAll(x => ((IMyPlayer)x).IdentityId == player.IdentityId);

            m_targetTracker.Remove(player);
        }
예제 #5
0
        public override void ParallelUpdate(List <IMyCubeGrid> gridList, List <BlockTarget> blocks)
        {
            m_entities.Clear();
            try
            {
                MyAPIGateway.Entities.GetEntities(m_entities, x => x is IMyFloatingObject || x is MyInventoryBagEntity || x is IMyCharacter);
            }
            catch
            {
                Logging.Instance.WriteLine("NaniteFloatingTargets.ParallelUpdate: Error getting entities, skipping.");
                return;
            }

            foreach (var item in m_entities.ToList())
            {
                if (item == null)
                {
                    continue;
                }

                if (item is IMyCharacter)
                {
                    var charBuilder = (MyObjectBuilder_Character)item.GetObjectBuilder();
                    if (charBuilder.LootingCounter <= 0f)
                    {
                        continue;
                    }
                }

                if (IsInRange(item.GetPosition(), m_maxDistance) && TransferFromTarget(item, false))
                {
                    PotentialTargetList.Add(item);
                }
            }
        }
        private bool AddPotentialBlock(IMySlimBlock block, bool remote = false, NaniteAreaBeacon beacon = null)
        {
            if (TargetList.Contains(block))
            {
                return(false);
            }

            if (!remote && block.FatBlock != null && block.FatBlock is IMyTerminalBlock && block.FatBlock.OwnerId != 0)
            {
                IMyTerminalBlock terminal = (IMyTerminalBlock)block.FatBlock;
                MyRelationsBetweenPlayerAndBlock relation = terminal.GetUserRelationToOwner(m_constructionBlock.ConstructionBlock.OwnerId);

                if (relation == MyRelationsBetweenPlayerAndBlock.Neutral ||
                    relation == MyRelationsBetweenPlayerAndBlock.Enemies)
                {
                    return(false);
                }
            }
            else if (remote)
            {
                //if (block.CubeGrid.BigOwners.Count < 1)
                //    return false;

                foreach (var item in block.CubeGrid.BigOwners)
                {
                    MyRelationsBetweenPlayerAndBlock relation = m_constructionBlock.ConstructionBlock.GetUserRelationToOwner(item);

                    if (relation == MyRelationsBetweenPlayerAndBlock.Neutral ||
                        relation == MyRelationsBetweenPlayerAndBlock.Enemies)
                    {
                        return(false);
                    }
                }
            }

            if (!block.IsFullIntegrity || block.HasDeformation)
            {
                using (Lock.AcquireExclusiveUsing())
                {
                    if (beacon != null)
                    {
                        if (!m_areaTargetBlocks.ContainsKey(block))
                        {
                            m_areaTargetBlocks.Add(block, beacon);
                        }
                        else
                        {
                            m_areaTargetBlocks[block] = beacon;
                        }
                    }

                    PotentialTargetList.Add(block);
                    return(true);
                }
            }

            return(false);
        }
        public override void ParallelUpdate(List <IMyCubeGrid> gridList, List <IMySlimBlock> blocks)
        {
            using (Lock.AcquireExclusiveUsing())
            {
                PotentialTargetList.Clear();
            }

            var remoteList = new HashSet <IMySlimBlock>();

            if (!IsEnabled())
            {
                return;
            }

            foreach (var block in blocks)
            {
                AddPotentialBlock(block);
            }

            foreach (var beaconBlock in NaniteConstructionManager.BeaconList.Where(x => x is NaniteBeaconConstruct && Vector3D.DistanceSquared(m_constructionBlock.ConstructionBlock.GetPosition(), x.BeaconBlock.GetPosition()) < m_maxDistance * m_maxDistance))
            {
                IMyCubeBlock item = (IMyCubeBlock)beaconBlock.BeaconBlock;

                if (!((IMyFunctionalBlock)item).Enabled || !((IMyFunctionalBlock)item).IsFunctional)
                {
                    continue;
                }

                MyRelationsBetweenPlayerAndBlock relation = item.GetUserRelationToOwner(m_constructionBlock.ConstructionBlock.OwnerId);
                if (!(relation == MyRelationsBetweenPlayerAndBlock.Owner || relation == MyRelationsBetweenPlayerAndBlock.FactionShare || (MyAPIGateway.Session.CreativeMode && relation == MyRelationsBetweenPlayerAndBlock.NoOwnership)))
                {
                    continue;
                }

                List <IMyCubeGrid>  beaconGridList = GridHelper.GetGridGroup((IMyCubeGrid)item.CubeGrid);
                List <IMySlimBlock> beaconBlocks   = new List <IMySlimBlock>();
                foreach (var grid in beaconGridList)
                {
                    grid.GetBlocks(beaconBlocks);
                }

                foreach (var block in beaconBlocks)
                {
                    if (AddPotentialBlock(block, true))
                    {
                        remoteList.Add(block);
                    }
                }
            }

            CheckAreaBeacons();

            using (m_remoteLock.AcquireExclusiveUsing())
            {
                m_remoteTargets = remoteList;
            }
        }
예제 #8
0
        public override void ParallelUpdate(List <IMyCubeGrid> gridList, List <BlockTarget> gridBlocks)
        {
            List <IMyPlayer> players = new List <IMyPlayer>();

            try
            {
                MyAPIGateway.Players.GetPlayers(players);
            }
            catch
            {
                Logging.Instance.WriteLine("NaniteLifeSupportTargets.ParallelUpdate: Error getting players, skipping");
                return;
            }

            foreach (IMyPlayer item in players)
            {
                var functional = m_constructionBlock.ConstructionBlock as IMyFunctionalBlock;
                MyRelationsBetweenPlayerAndBlock relations = functional.GetUserRelationToOwner(item.IdentityId);
                if (relations != MyRelationsBetweenPlayerAndBlock.Owner && relations != MyRelationsBetweenPlayerAndBlock.FactionShare)
                {
                    continue;
                }

                if (!DoesTargetNeedLifeSupport(item))
                {
                    continue;
                }

                if (PotentialTargetList.Contains(item) || TargetList.Contains(item))
                {
                    continue;
                }

                if (IsInRange(item.GetPosition(), m_maxDistance))
                {
                    PotentialTargetList.Add(item);
                }
            }

            List <IMyGasTank> removalList = new List <IMyGasTank>();

            foreach (IMyGasTank tank in connectedGasTanks)
            {
                if (!GridHelper.IsValidGasConnection(m_constructionBlock.ConstructionCubeBlock, tank))
                {
                    removalList.Add(tank);
                }
            }

            foreach (IMyGasTank tank in removalList)
            {
                connectedGasTanks.Remove(tank);
            }

            CheckTanks(ref m_hasOxygen, ref m_hasHydrogen);
        }
예제 #9
0
 private void ProcessProjector(IMyProjector projector)
 {
     foreach (IMySlimBlock block in ProjectorIntegration.IterBuildableBlocks(projector))
     {
         if (!PotentialTargetList.Contains(block))
         {
             PotentialTargetList.Add(block);
         }
     }
 }
        private bool UpdateProjection(MyCubeBlock projector, MyCubeGrid projectedGrid, MyObjectBuilder_ProjectorBase projectorBuilder)
        {
            // god f*****g damnit object builders
            MyCubeGrid cubeGrid = projector.CubeGrid;
            MyObjectBuilder_CubeGrid gridBuilder = (MyObjectBuilder_CubeGrid)projectedGrid.GetObjectBuilder();
            bool found = false;

            foreach (MyObjectBuilder_CubeBlock blockBuilder in gridBuilder.CubeBlocks)
            {
                Vector3  worldPosition = projectedGrid.GridIntegerToWorld(blockBuilder.Min);
                Vector3I realPosition  = cubeGrid.WorldToGridInteger(worldPosition);
                var      realBlock     = (IMySlimBlock)cubeGrid.GetCubeBlock(realPosition);

                MyCubeBlockDefinition blockDefinition;
                MyDefinitionManager.Static.TryGetCubeBlockDefinition(blockBuilder.GetId(), out blockDefinition);
                if (realBlock != null) // && blockDefinition.Id == new MyDefinitionId(realBlock.GetType()))
                {
                    //Logging.Instance.WriteLine(string.Format("Found overlap - {0} {1}", blockBuilder.GetId(), realBlock.GetObjectBuilder().GetId()));
                }
                else
                {
                    //Logging.Instance.WriteLine(string.Format("No block at position: {0}", blockBuilder.GetId()));
                    if (CanBuildBlock(blockBuilder, projectedGrid, projector, cubeGrid, projectorBuilder))
                    {
                        //Logging.Instance.WriteLine(string.Format("No block at position: {0}", blockBuilder.GetId()));
                        var slimBlock = (IMySlimBlock)projectedGrid.GetCubeBlock(blockBuilder.Min);
                        if (slimBlock != null && slimBlock.CubeGrid.GetPosition() != Vector3D.Zero)
                        {
                            //Logging.Instance.WriteLine(string.Format("Adding block: {0}", blockBuilder.GetId()));
                            PotentialTargetList.Add(slimBlock);
                            found = true;
                        }
                    }
                    else
                    {
                        using (m_lock.AcquireExclusiveUsing())
                        {
                            foreach (var item in blockDefinition.Components)
                            {
                                if (!ComponentsRequired.ContainsKey(item.Definition.Id.SubtypeName))
                                {
                                    ComponentsRequired.Add(item.Definition.Id.SubtypeName, item.Count);
                                }
                                else
                                {
                                    ComponentsRequired[item.Definition.Id.SubtypeName] += item.Count;
                                }
                            }
                        }
                    }
                }
            }

            return(found);
        }
예제 #11
0
        private void ProcessProjector(IMyProjector projector)
        {
            MyCubeGrid grid = (MyCubeGrid)projector.ProjectedGrid;

            foreach (IMySlimBlock block in grid.GetBlocks())
            {
                if (projector.CanBuild(block, false) == BuildCheckResult.OK && !PotentialTargetList.Contains(block))
                {
                    PotentialTargetList.Add(block);
                }
            }
        }
        private void CheckAreaBeacons(List <IMyCubeGrid> NaniteGridGroup)
        {
            foreach (var beaconBlock in NaniteConstructionManager.BeaconList.Where(x => x.Value is NaniteAreaBeacon).ToList())
            {
                IMyCubeBlock cubeBlock = (IMyCubeBlock)beaconBlock.Value.BeaconBlock;

                if (!IsAreaBeaconValid(cubeBlock))
                {
                    continue;
                }

                var item = beaconBlock.Value as NaniteAreaBeacon;
                if (!item.Settings.AllowDeconstruction)
                {
                    continue;
                }

                HashSet <IMyEntity> entities = new HashSet <IMyEntity>();
                MyAPIGateway.Entities.GetEntities(entities);
                foreach (var entity in entities)
                {
                    var grid = entity as IMyCubeGrid;
                    if (grid != null && grid.Physics != null && grid.Physics.AngularVelocity.Length() == 0f &&
                        grid.Physics.LinearVelocity.Length() == 0f && m_validBeaconedGrids.FirstOrDefault(x => x.GridsProcessed.Contains(grid)) == null &&
                        !MyAPIGateway.GridGroups.GetGroup(grid, GridLinkTypeEnum.Physical).Contains(cubeBlock.CubeGrid) &&
                        (grid.GetPosition() - cubeBlock.GetPosition()).LengthSquared() < m_maxDistance * m_maxDistance && item.IsInsideBox(grid.WorldAABB, false))
                    {
                        NaniteDeconstructionGrid deconstruct = new NaniteDeconstructionGrid(grid);
                        m_validBeaconedGrids.Add(deconstruct);
                        CreateGridStack(NaniteGridGroup, deconstruct, (MyCubeGrid)grid, null);

                        if (!m_areaTargetBlocks.ContainsKey(grid))
                        {
                            m_areaTargetBlocks.Add(grid, item);
                        }
                        else
                        {
                            m_areaTargetBlocks[grid] = item;
                        }

                        foreach (var block in deconstruct.RemoveList)
                        {
                            if (!PotentialTargetList.Contains(block))
                            {
                                PotentialTargetList.Add(block);
                            }
                        }
                    }
                }
            }
        }
        public void CancelTarget(IMySlimBlock obj)
        {
            Logging.Instance.WriteLine(string.Format("CANCELLING Deconstruction Target: {0} - {1} (EntityID={2},Position={3})", m_constructionBlock.ConstructionBlock.EntityId, obj.GetType().Name, obj.FatBlock != null ? obj.FatBlock.EntityId : 0, obj.Position));
            if (Sync.IsServer)
            {
                m_constructionBlock.SendCancelTarget(obj, TargetTypes.Deconstruction);
            }

            m_constructionBlock.ParticleManager.CancelTarget(obj);
            m_constructionBlock.ToolManager.Remove(obj);
            Remove(obj);

            using (Lock.AcquireExclusiveUsing())
                PotentialTargetList.Add(obj);
        }
        private void ScanProjection(List <IMySlimBlock> blocks)
        {
            using (m_lock.AcquireExclusiveUsing())
            {
                TargetList.Clear();
                PotentialTargetList.Clear();
            }

            HashSet <IMyEntity> entities = new HashSet <IMyEntity>();

            MyAPIGateway.Entities.GetEntities(entities, x => x is IMyCubeGrid && x.Physics == null);
            foreach (var item in blocks)
            {
                CheckBlockProjection(item);
            }

            foreach (var beaconBlock in NaniteConstructionManager.BeaconList.Where(x => x is NaniteBeaconProjection && Vector3D.DistanceSquared(m_constructionBlock.ConstructionBlock.GetPosition(), x.BeaconBlock.GetPosition()) < m_maxDistance * m_maxDistance))
            {
                IMyCubeBlock item = (IMyCubeBlock)beaconBlock.BeaconBlock;

                if (!((IMyFunctionalBlock)item).Enabled || !((IMyFunctionalBlock)item).IsFunctional)
                {
                    continue;
                }

                MyRelationsBetweenPlayerAndBlock relation = item.GetUserRelationToOwner(m_constructionBlock.ConstructionBlock.OwnerId);
                if (!(relation == MyRelationsBetweenPlayerAndBlock.Owner || relation == MyRelationsBetweenPlayerAndBlock.FactionShare || (MyAPIGateway.Session.CreativeMode && relation == MyRelationsBetweenPlayerAndBlock.NoOwnership)))
                {
                    continue;
                }

                List <IMyCubeGrid>  beaconGridList = GridHelper.GetGridGroup((IMyCubeGrid)item.CubeGrid);
                List <IMySlimBlock> beaconBlocks   = new List <IMySlimBlock>();
                foreach (var grid in beaconGridList)
                {
                    grid.GetBlocks(beaconBlocks);
                }

                foreach (var block in beaconBlocks)
                {
                    CheckBlockProjection(block);
                }
            }

            CheckAreaBeacons();
        }
예제 #15
0
        public override void ParallelUpdate(List <IMyCubeGrid> gridList, List <IMySlimBlock> blocks)
        {
            using (Lock.AcquireExclusiveUsing())
            {
                PotentialTargetList.Clear();
            }

            if (!IsEnabled())
            {
                return;
            }

            foreach (var block in blocks)
            {
                AddPotentialBlock(block);
            }
            CheckBeacons();
            CheckAreaBeacons();
        }
        public void CompleteTarget(long entityId)
        {
            m_constructionBlock.ParticleManager.CompleteTarget(entityId);
            foreach (var item in m_targetTracker.ToList())
            {
                if (item.Key.EntityId == entityId)
                {
                    m_targetTracker.Remove(item.Key);
                }
            }

            foreach (IMyEntity item in TargetList.Where(x => ((IMyEntity)x).EntityId == entityId))
            {
                Logging.Instance.WriteLine(string.Format("COMPLETING Floating Object Target: {0} - {1} (EntityID={2},Position={3})", m_constructionBlock.ConstructionBlock.EntityId, item.GetType().Name, item.EntityId, item.GetPosition()));
            }

            TargetList.RemoveAll(x => ((IMyEntity)x).EntityId == entityId);
            PotentialTargetList.RemoveAll(x => ((IMyEntity)x).EntityId == entityId);
        }
        public override void ParallelUpdate(List <IMyCubeGrid> gridList, List <IMySlimBlock> blocks)
        {
            using (m_lock.AcquireExclusiveUsing())
            {
                PotentialTargetList.Clear();
            }

            m_entities.Clear();
            try
            {
                MyAPIGateway.Entities.GetEntities(m_entities, x => x is IMyFloatingObject || x is MyInventoryBagEntity || x is IMyCharacter);
            }
            catch
            {
                Logging.Instance.WriteLine(string.Format("Error getting entities, skipping"));
                return;
            }

            if (!IsEnabled())
            {
                return;
            }

            foreach (var item in m_entities)
            {
                if (item is IMyCharacter)
                {
                    var charBuilder = (MyObjectBuilder_Character)item.GetObjectBuilder();
                    if (charBuilder.LootingCounter <= 0f)
                    {
                        continue;
                    }
                }

                if (Vector3D.DistanceSquared(m_constructionBlock.ConstructionBlock.GetPosition(), item.GetPosition()) < m_maxDistance * m_maxDistance &&
                    TransferFromTarget(item, false))
                {
                    using (m_lock.AcquireExclusiveUsing())
                        PotentialTargetList.Add(item);
                }
            }
        }
        public override void FindTargets(ref Dictionary <string, int> available)
        {
            m_lastInvalidTargetReason = "";

            if (!IsEnabled())
            {
                return;
            }

            if (TargetList.Count >= GetMaximumTargets())
            {
                if (PotentialTargetList.Count > 0)
                {
                    m_lastInvalidTargetReason = "Maximum targets reached.  Add more upgrades!";
                }

                return;
            }

            using (Lock.AcquireExclusiveUsing())
            {
                foreach (IMySlimBlock item in PotentialTargetList.ToList())
                {
                    if (m_constructionBlock.IsUserDefinedLimitReached())
                    {
                        m_lastInvalidTargetReason = "User defined maximum nanite limit reached";
                        return;
                    }

                    if (TargetList.Contains(item))
                    {
                        continue;
                    }

                    if (!NaniteConstructionPower.HasRequiredPowerForNewTarget((IMyFunctionalBlock)m_constructionBlock.ConstructionBlock, this))
                    {
                        m_lastInvalidTargetReason = "Insufficient power for another target.";
                        break;
                    }

                    if (item.CubeGrid.Closed || item.IsDestroyed || item.IsFullyDismounted || (item.FatBlock != null && item.FatBlock.Closed))
                    {
                        m_lastInvalidTargetReason = "Potential target is destroyed";
                        continue;
                    }

                    var  blockList = NaniteConstructionManager.GetConstructionBlocks((IMyCubeGrid)m_constructionBlock.ConstructionBlock.CubeGrid);
                    bool found     = false;
                    foreach (var block in blockList)
                    {
                        if (block.Targets.First(x => x is NaniteDeconstructionTargets).TargetList.Contains(item as IMySlimBlock))
                        {
                            found = true;
                            break;
                        }
                    }

                    if (found)
                    {
                        m_lastInvalidTargetReason = "Another factory has this block as a target";
                        continue;
                    }

                    /*
                     * var blocks = NaniteConstructionManager.NaniteBlocks.Select(x => x.Value).Where(y => y.ConstructionBlock.CubeGrid == m_constructionBlock.ConstructionBlock.CubeGrid && y.ConstructionBlock != m_constructionBlock.ConstructionBlock);
                     * Logging.Instance.WriteLine(string.Format("Count: {0}", blocks.Count()));
                     * var found = blocks.FirstOrDefault(x => x.Targets.First(y => y is NaniteDeconstructionTargets).TargetList.Contains(item)) != null;
                     * if (found)
                     * {
                     *  Logging.Instance.WriteLine("Found");
                     *  continue;
                     * }
                     */

                    PotentialTargetList.Remove(item);
                    TargetList.Add(item);

                    var def = item.BlockDefinition as MyCubeBlockDefinition;
                    Logging.Instance.WriteLine(string.Format("ADDING Deconstruction Target: conid={0} subtypeid={1} entityID={2} position={3}", m_constructionBlock.ConstructionBlock.EntityId, def.Id.SubtypeId, item.FatBlock != null ? item.FatBlock.EntityId : 0, item.Position));

                    if (TargetList.Count >= GetMaximumTargets())
                    {
                        break;
                    }
                }
            }
        }
        public override void ParallelUpdate(List <IMyCubeGrid> gridList, List <IMySlimBlock> gridBlocks)
        {
            try
            {
                if (!IsEnabled())
                {
                    PotentialTargetList.Clear();
                    return;
                }

                // Add
                foreach (var beaconBlock in NaniteConstructionManager.BeaconList.Where(x => x is NaniteBeaconDeconstruct && Vector3D.DistanceSquared(m_constructionBlock.ConstructionBlock.GetPosition(), x.BeaconBlock.GetPosition()) < m_maxDistance * m_maxDistance))
                {
                    IMyCubeBlock item = (IMyCubeBlock)beaconBlock.BeaconBlock;

                    if (!((IMyFunctionalBlock)item).Enabled || !((IMyFunctionalBlock)item).IsFunctional)
                    {
                        continue;
                    }

                    if (gridList.Contains(item.CubeGrid))
                    {
                        continue;
                    }

                    MyRelationsBetweenPlayerAndBlock relation = item.GetUserRelationToOwner(m_constructionBlock.ConstructionBlock.OwnerId);
                    if (!(relation == MyRelationsBetweenPlayerAndBlock.Owner || relation == MyRelationsBetweenPlayerAndBlock.FactionShare || (MyAPIGateway.Session.CreativeMode && relation == MyRelationsBetweenPlayerAndBlock.NoOwnership)))
                    {
                        continue;
                    }

                    if (m_validBeaconedGrids.FirstOrDefault(x => x.GridsProcessed.Contains(item.CubeGrid)) != null)
                    {
                        continue;
                    }

                    NaniteDeconstructionGrid deconstruct = new NaniteDeconstructionGrid(item.CubeGrid);
                    m_validBeaconedGrids.Add(deconstruct);
                    CreateGridStack(deconstruct, (MyCubeGrid)item.CubeGrid, (MyCubeBlock)item);

                    using (Lock.AcquireExclusiveUsing())
                    {
                        foreach (var slimBlock in deconstruct.RemoveList)
                        {
                            if (!PotentialTargetList.Contains(slimBlock))
                            {
                                PotentialTargetList.Add(slimBlock);
                            }
                        }
                    }

                    deconstruct.RemoveList.Clear();
                }

                CheckAreaBeacons();

                if (PotentialTargetList.Count > 0)
                {
                    using (Lock.AcquireExclusiveUsing())
                    {
                        foreach (IMySlimBlock item in PotentialTargetList.ToList())
                        {
                            if (item.CubeGrid.Closed || item.IsDestroyed || item.IsFullyDismounted || (item.FatBlock != null && item.FatBlock.Closed))
                            {
                                PotentialTargetList.Remove(item);
                            }

                            if (EntityHelper.GetDistanceBetweenBlockAndSlimblock((IMyCubeBlock)m_constructionBlock.ConstructionBlock, item) > m_maxDistance)
                            {
                                PotentialTargetList.Remove(item);
                            }
                        }

                        //m_potentialTargetList = m_potentialTargetList.OrderBy(x => GetBlockConnections((IMySlimBlock)(x))).ToList();
                    }
                }
                else if (TargetList.Count == 0 && PotentialTargetList.Count == 0)
                {
                    m_validBeaconedGrids.Clear();
                }
            }
            catch (Exception ex)
            {
                Logging.Instance.WriteLine(string.Format("Parallel Erorr: {0}", ex.ToString()));
            }
        }
        public override void FindTargets(ref Dictionary <string, int> available)
        {
            ComponentsRequired.Clear();

            if (!IsEnabled())
            {
                return;
            }

            if (TargetList.Count >= GetMaximumTargets())
            {
                if (PotentialTargetList.Count > 0)
                {
                    m_lastInvalidTargetReason = "Maximum targets reached.  Add more upgrades!";
                }

                return;
            }

            NaniteConstructionInventory inventoryManager = m_constructionBlock.InventoryManager;
            Vector3D sourcePosition          = m_constructionBlock.ConstructionBlock.GetPosition();
            Dictionary <string, int> missing = new Dictionary <string, int>();

            using (m_lock.AcquireExclusiveUsing())
            {
                foreach (var item in PotentialTargetList.OrderBy(x => Vector3D.Distance(sourcePosition, EntityHelper.GetBlockPosition((IMySlimBlock)x))))
                {
                    if (m_constructionBlock.IsUserDefinedLimitReached())
                    {
                        m_lastInvalidTargetReason = "User defined maximum nanite limit reached";
                        return;
                    }

                    if (TargetList.Contains(item))
                    {
                        continue;
                    }

                    missing = inventoryManager.GetProjectionComponents((IMySlimBlock)item);
                    bool haveComponents = inventoryManager.CheckComponentsAvailable(ref missing, ref available);
                    if (haveComponents && NaniteConstructionPower.HasRequiredPowerForNewTarget((IMyFunctionalBlock)m_constructionBlock.ConstructionBlock, this))
                    {
                        if (((IMySlimBlock)item).CubeGrid.GetPosition() == Vector3D.Zero)
                        {
                            m_lastInvalidTargetReason = "Target blocks grid is in an invalid position (Vector3D.Zero, this shouldn't happen!)";
                            continue;
                        }

                        var  blockList = NaniteConstructionManager.GetConstructionBlocks((IMyCubeGrid)m_constructionBlock.ConstructionBlock.CubeGrid);
                        bool found     = false;
                        foreach (var block in blockList)
                        {
                            if (block.GetTarget <NaniteProjectionTargets>().TargetList.Contains(item))
                            {
                                found = true;
                                break;
                            }
                        }

                        if (found)
                        {
                            m_lastInvalidTargetReason = "Another factory has this block as a target";
                            continue;
                        }

                        TargetList.Add(item);
                        IMySlimBlock slimBlock = (IMySlimBlock)item;
                        var          def       = slimBlock.BlockDefinition as MyCubeBlockDefinition;
                        Logging.Instance.WriteLine(string.Format("ADDING Projection Target: conid={0} subtypeid={1} entityID={2} position={3}", m_constructionBlock.ConstructionBlock.EntityId, def.Id.SubtypeId, slimBlock.FatBlock != null ? slimBlock.FatBlock.EntityId : 0, slimBlock.Position));
                        if (TargetList.Count >= GetMaximumTargets())
                        {
                            break;
                        }
                    }
                    else if (!haveComponents)
                    {
                        m_lastInvalidTargetReason = "Missing components to start projected block";
                    }
                    else if (!NaniteConstructionPower.HasRequiredPowerForNewTarget((IMyFunctionalBlock)m_constructionBlock.ConstructionBlock, this))
                    {
                        m_lastInvalidTargetReason = "Insufficient power for another target.";
                    }
                }
            }
        }
        public override void FindTargets(ref Dictionary <string, int> available, List <NaniteConstructionBlock> blockList)
        {
            InvalidTargetReason("");

            var maxTargets = GetMaximumTargets();

            if (TargetList.Count >= maxTargets)
            {
                if (PotentialTargetList.Count > 0)
                {
                    InvalidTargetReason("Maximum targets reached. Add more upgrades!");
                }

                return;
            }

            int    TargetListCount         = TargetList.Count;
            string LastInvalidTargetReason = "";

            foreach (IMySlimBlock item in PotentialTargetList.ToList())
            {
                if (item == null || TargetList.Contains(item))
                {
                    continue;
                }

                if (!m_constructionBlock.HasRequiredPowerForNewTarget(this))
                {
                    LastInvalidTargetReason = "Insufficient power for another target.";
                    break;
                }

                if (item.CubeGrid.Closed || item.IsDestroyed || item.IsFullyDismounted || (item.FatBlock != null && item.FatBlock.Closed))
                {
                    LastInvalidTargetReason = "Potential target is destroyed";
                    continue;
                }

                bool found = false;
                foreach (var block in blockList.ToList())
                {
                    if (block != null && block.Targets.First(x => x is NaniteDeconstructionTargets).TargetList.Contains(item as IMySlimBlock))
                    {
                        found = true;
                        LastInvalidTargetReason = "Another factory has this block as a target";
                        break;
                    }
                }

                if (found)
                {
                    continue;
                }

                AddTarget(item);

                var def = item.BlockDefinition as MyCubeBlockDefinition;
                Logging.Instance.WriteLine(string.Format("[Deconstruction] Adding Deconstruction Target: conid={0} subtypeid={1} entityID={2} position={3}",
                                                         m_constructionBlock.ConstructionBlock.EntityId, def.Id.SubtypeId, item.FatBlock != null ? item.FatBlock.EntityId : 0, item.Position), 1);

                if (++TargetListCount >= maxTargets)
                {
                    break;
                }
            }

            if (LastInvalidTargetReason != "")
            {
                InvalidTargetReason(LastInvalidTargetReason);
            }
        }
예제 #22
0
        public override void FindTargets(ref Dictionary <string, int> available)
        {
            if (!IsEnabled())
            {
                return;
            }

            if (TargetList.Count >= GetMaximumTargets())
            {
                if (PotentialTargetList.Count > 0)
                {
                    m_lastInvalidTargetReason = "Maximum targets reached.  Add more upgrades!";
                }

                return;
            }

            using (Lock.AcquireExclusiveUsing())
            {
                for (int r = PotentialTargetList.Count - 1; r >= 0; r--)
                {
                    if (m_constructionBlock.IsUserDefinedLimitReached())
                    {
                        m_lastInvalidTargetReason = "User defined maximum nanite limit reached";
                        return;
                    }

                    var item = (IMyPlayer)PotentialTargetList[r];
                    if (TargetList.Contains(item))
                    {
                        continue;
                    }

                    if (item.Controller == null || item.Controller.ControlledEntity == null || item.Controller.ControlledEntity.Entity == null)
                    {
                        PotentialTargetList.RemoveAt(r);
                        continue;
                    }

                    var  blockList = NaniteConstructionManager.GetConstructionBlocks((IMyCubeGrid)m_constructionBlock.ConstructionBlock.CubeGrid);
                    bool found     = false;
                    foreach (var block in blockList)
                    {
                        if (block.Targets.First(x => x is NaniteMedicalTargets).TargetList.Contains(item))
                        {
                            found = true;
                            break;
                        }
                    }

                    if (found)
                    {
                        m_lastInvalidTargetReason = "Another factory has this block as a target";
                        continue;
                    }

                    if (Vector3D.DistanceSquared(m_constructionBlock.ConstructionBlock.GetPosition(), item.GetPosition()) < m_maxDistance * m_maxDistance &&
                        NaniteConstructionPower.HasRequiredPowerForNewTarget((IMyFunctionalBlock)m_constructionBlock.ConstructionBlock, this))
                    {
                        TargetList.Add(item);
                        Logging.Instance.WriteLine(string.Format("ADDING Medical Target: conid={0} type={1} playerName={2} position={3}", m_constructionBlock.ConstructionBlock.EntityId, item.GetType().Name, item.DisplayName, item.GetPosition()));
                        if (TargetList.Count >= GetMaximumTargets())
                        {
                            break;
                        }
                    }
                }
            }
        }
예제 #23
0
        public override void ParallelUpdate(List <IMyCubeGrid> gridList, List <IMySlimBlock> gridBlocks)
        {
            using (Lock.AcquireExclusiveUsing())
                PotentialTargetList.Clear();

            List <IMyPlayer> players = new List <IMyPlayer>();

            try
            {
                MyAPIGateway.Players.GetPlayers(players);
            }
            catch
            {
                Logging.Instance.WriteLine(string.Format("Error getting players, skipping"));
                return;
            }

            if (!IsEnabled())
            {
                return;
            }

            foreach (var item in players)
            {
                var functional = m_constructionBlock.ConstructionBlock as IMyFunctionalBlock;
                MyRelationsBetweenPlayerAndBlock relations = functional.GetUserRelationToOwner(item.IdentityId);
                if (relations != MyRelationsBetweenPlayerAndBlock.Owner && relations != MyRelationsBetweenPlayerAndBlock.FactionShare)
                {
                    continue;
                }

                if (item.Controller == null || item.Controller.ControlledEntity == null || item.Controller.ControlledEntity.Entity == null)
                {
                    continue;
                }

                bool damaged = false;
                foreach (var component in item.Controller.ControlledEntity.Entity.Components)
                {
                    var stat = component as MyCharacterStatComponent;
                    if (stat != null)
                    {
                        if (stat.Health.Value < stat.Health.MaxValue)
                        {
                            damaged = true;
                        }

                        break;
                    }
                }

                if (!damaged)
                {
                    continue;
                }

                if (Vector3D.DistanceSquared(m_constructionBlock.ConstructionBlock.GetPosition(), item.GetPosition()) < m_maxDistance * m_maxDistance)
                {
                    using (m_lock.AcquireExclusiveUsing())
                        PotentialTargetList.Add(item);
                }
            }
        }
예제 #24
0
        public override void ParallelUpdate(List <IMyCubeGrid> gridList, List <IMySlimBlock> gridBlocks)
        {
            using (Lock.AcquireExclusiveUsing())
                PotentialTargetList.Clear();

            DateTime      start        = DateTime.Now;
            List <object> finalAddList = new List <object>();
            int           listCount    = 0;

            foreach (var miningBlock in NaniteConstructionManager.MiningList.Where(x => x.IsWorking && Vector3D.DistanceSquared(m_constructionBlock.ConstructionBlock.GetPosition(), x.MiningBlock.GetPosition()) < m_maxDistance * m_maxDistance).OrderBy(x => rnd.Next(100)))
            {
                IMyCubeBlock item = (IMyCubeBlock)miningBlock.MiningBlock;
                MyRelationsBetweenPlayerAndBlock relation = item.GetUserRelationToOwner(m_constructionBlock.ConstructionBlock.OwnerId);
                if (!(relation == MyRelationsBetweenPlayerAndBlock.Owner || relation == MyRelationsBetweenPlayerAndBlock.FactionShare || (MyAPIGateway.Session.CreativeMode && relation == MyRelationsBetweenPlayerAndBlock.NoOwnership)))
                {
                    continue;
                }

                if (!((IMyFunctionalBlock)item).Enabled)
                {
                    continue;
                }

                if (miningBlock.OreList == null || miningBlock.OreList.Count < 1)
                {
                    continue;
                }

                int sum = miningBlock.OreList.Sum(x => x.Value.Count);
                Dictionary <MyVoxelMaterialDefinition, List <NaniteMiningItem> > lookup = null;
                using (miningBlock.Lock.AcquireExclusiveUsing())
                {
                    lookup = miningBlock.OreList.ToDictionary(x => x.Key, x => x.Value);
                }

                List <object> addList = new List <object>();
                int           count   = 0;
                int           pos     = 0;

                while (true)
                {
                    var group = lookup.ElementAt(count % miningBlock.OreList.Count);
                    if (pos < group.Value.Count)
                    {
                        addList.Insert(0, group.Value[pos]);
                    }

                    count++;
                    if (count % miningBlock.OreList.Count == 0)
                    {
                        pos++;
                    }

                    if (count >= 1000)
                    {
                        break;
                    }

                    if (count >= sum)
                    {
                        break;
                    }
                }

                DistributeList(addList, finalAddList, listCount);
                listCount++;

                if (listCount > 5)
                {
                    break;
                }
            }

            var listToAdd = finalAddList.Take(1000).ToList();

            listToAdd.Reverse();
            using (Lock.AcquireExclusiveUsing())
            {
                PotentialTargetList.AddRange(listToAdd);
            }

            //Logging.Instance.WriteLine(string.Format("ParallelUpdate() took {0} ms", (DateTime.Now - start).TotalMilliseconds));
        }
예제 #25
0
 public virtual void Remove(object target)
 {
     TargetList.Remove(target);
     PotentialTargetList.Remove(target);
 }
        private void CheckAreaBeacons()
        {
            foreach (var beaconBlock in NaniteConstructionManager.BeaconList.Where(x => x is NaniteAreaBeacon))
            {
                IMyCubeBlock cubeBlock = (IMyCubeBlock)beaconBlock.BeaconBlock;

                if (!((IMyFunctionalBlock)cubeBlock).Enabled || !((IMyFunctionalBlock)cubeBlock).IsFunctional)
                {
                    continue;
                }

                MyRelationsBetweenPlayerAndBlock relation = cubeBlock.GetUserRelationToOwner(m_constructionBlock.ConstructionBlock.OwnerId);
                if (!(relation == MyRelationsBetweenPlayerAndBlock.Owner || relation == MyRelationsBetweenPlayerAndBlock.FactionShare || (MyAPIGateway.Session.CreativeMode && relation == MyRelationsBetweenPlayerAndBlock.NoOwnership)))
                {
                    continue;
                }

                var item = beaconBlock as NaniteAreaBeacon;
                if (!item.Settings.AllowDeconstruction)
                {
                    continue;
                }

                HashSet <IMyEntity> entities = new HashSet <IMyEntity>();
                MyAPIGateway.Entities.GetEntities(entities);
                foreach (var entity in entities)
                {
                    var grid = entity as IMyCubeGrid;
                    if (grid == null)
                    {
                        continue;
                    }

                    if (grid.Physics == null)
                    {
                        continue;
                    }

                    if (grid.Physics.AngularVelocity.Length() != 0f || grid.Physics.LinearVelocity.Length() != 0f)
                    {
                        continue;
                    }

                    if (m_validBeaconedGrids.FirstOrDefault(x => x.GridsProcessed.Contains(grid)) != null)
                    {
                        continue;
                    }

                    if (GridHelper.GetGridGroup(grid).Contains(cubeBlock.CubeGrid))
                    {
                        continue;
                    }

                    if ((grid.GetPosition() - cubeBlock.GetPosition()).LengthSquared() < m_maxDistance * m_maxDistance)
                    {
                        if (item.IsInsideBox(grid.WorldAABB, false))
                        {
                            NaniteDeconstructionGrid deconstruct = new NaniteDeconstructionGrid(grid);
                            m_validBeaconedGrids.Add(deconstruct);
                            CreateGridStack(deconstruct, (MyCubeGrid)grid, null);
                            if (!m_areaTargetBlocks.ContainsKey(grid))
                            {
                                m_areaTargetBlocks.Add(grid, item);
                            }
                            else
                            {
                                m_areaTargetBlocks[grid] = item;
                            }

                            foreach (var block in deconstruct.RemoveList)
                            {
                                if (!PotentialTargetList.Contains(block))
                                {
                                    PotentialTargetList.Add(block);
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #27
0
        public override void FindTargets(ref Dictionary <string, int> available, List <NaniteConstructionBlock> blockList)
        {
            MyAPIGateway.Utilities.InvokeOnGameThread(() =>
            {
                m_lastInvalidTargetReason = "";
                ComponentsRequired.Clear();
            });

            var maxTargets = GetMaximumTargets();

            if (TargetList.Count >= maxTargets)
            {
                if (PotentialTargetList.Count > 0)
                {
                    InvalidTargetReason("Maximum targets reached. Add more upgrades!");
                }

                return;
            }

            NaniteConstructionInventory inventoryManager = m_constructionBlock.InventoryManager;
            Vector3D sourcePosition          = m_constructionBlock.ConstructionBlock.GetPosition();
            Dictionary <string, int> missing = new Dictionary <string, int>();
            string LastInvalidTargetReason   = "";

            int TargetListCount = TargetList.Count;

            foreach (var item in PotentialTargetList.OrderBy(x => Vector3D.Distance(sourcePosition, EntityHelper.GetBlockPosition((IMySlimBlock)x))).ToList())
            {
                if (item == null || TargetList.Contains(item))
                {
                    continue;
                }

                missing = inventoryManager.GetProjectionComponents((IMySlimBlock)item);
                bool haveComponents = inventoryManager.CheckComponentsAvailable(ref missing, ref available);
                if ((MyAPIGateway.Session.CreativeMode || haveComponents) && m_constructionBlock.HasRequiredPowerForNewTarget(this) &&
                    ((IMySlimBlock)item).CubeGrid.GetPosition() != Vector3D.Zero)
                {
                    bool found = false;
                    foreach (var block in blockList.ToList())
                    {
                        if (block != null && block.GetTarget <NaniteProjectionTargets>().TargetList.Contains(item))
                        {
                            found = true;
                            LastInvalidTargetReason = "Another factory has this block as a target";
                            break;
                        }
                    }

                    if (found)
                    {
                        continue;
                    }

                    AddTarget(item);

                    IMySlimBlock slimBlock = (IMySlimBlock)item;
                    var          def       = slimBlock.BlockDefinition as MyCubeBlockDefinition;
                    Logging.Instance.WriteLine(string.Format("[Projection] Adding Projection Target: conid={0} subtypeid={1} entityID={2} position={3}",
                                                             m_constructionBlock.ConstructionBlock.EntityId, def.Id.SubtypeId, slimBlock.FatBlock != null ? slimBlock.FatBlock.EntityId : 0, slimBlock.Position), 1);

                    if (++TargetListCount >= maxTargets)
                    {
                        break;
                    }
                }
                else if (!haveComponents)
                {
                    LastInvalidTargetReason = "Missing components to start projected block";
                }

                else if (!m_constructionBlock.HasRequiredPowerForNewTarget(this))
                {
                    LastInvalidTargetReason = "Insufficient power for another target.";
                    break;
                }
            }
            if (LastInvalidTargetReason != "")
            {
                InvalidTargetReason(LastInvalidTargetReason);
            }
        }
        public override void ParallelUpdate(List <IMyCubeGrid> NaniteGridGroup, List <BlockTarget> gridBlocks)
        {
            try
            {
                // Add
                foreach (var beaconBlock in NaniteConstructionManager.BeaconList.Where(x => x.Value is NaniteBeaconDeconstruct))
                {
                    IMyCubeBlock item = (IMyCubeBlock)beaconBlock.Value.BeaconBlock;

                    if (item == null ||
                        item.CubeGrid == null ||
                        !((IMyFunctionalBlock)item).Enabled ||
                        !((IMyFunctionalBlock)item).IsFunctional ||
                        NaniteGridGroup.Contains(item.CubeGrid) ||
                        !MyRelationsBetweenPlayerAndBlockExtensions.IsFriendly(item.GetUserRelationToOwner(m_constructionBlock.ConstructionBlock.OwnerId)) ||
                        m_validBeaconedGrids.FirstOrDefault(x => x.GridsProcessed.Contains(item.CubeGrid)) != null ||
                        !IsInRange(item.GetPosition(), m_maxDistance)
                        )
                    {
                        continue;
                    }

                    NaniteDeconstructionGrid deconstruct = new NaniteDeconstructionGrid(item.CubeGrid);

                    CreateGridStack(NaniteGridGroup, deconstruct, (MyCubeGrid)item.CubeGrid, (MyCubeBlock)item);
                    m_validBeaconedGrids.Add(deconstruct);

                    Logging.Instance.WriteLine($"[Deconstruction] Grid {item.CubeGrid.CustomName} queued for deconstruction", 1);

                    foreach (var slimBlock in deconstruct.RemoveList)
                    {
                        if (slimBlock != null)
                        {
                            PotentialTargetList.Add(slimBlock);
                        }
                    }

                    deconstruct.RemoveList.Clear();
                }

                CheckAreaBeacons(NaniteGridGroup);

                if (PotentialTargetList.Count > 0)
                {
                    foreach (IMySlimBlock item in PotentialTargetList.ToList())
                    {
                        if (item == null || item.CubeGrid == null || item.CubeGrid.Closed || item.IsDestroyed || item.IsFullyDismounted ||
                            (item.FatBlock != null && item.FatBlock.Closed) ||
                            EntityHelper.GetDistanceBetweenBlockAndSlimblock((IMyCubeBlock)m_constructionBlock.ConstructionBlock, item) > m_maxDistance)
                        {
                            PotentialTargetList.Remove(item);
                        }
                    }
                }
                else if (TargetList.Count == 0 && PotentialTargetList.Count == 0)
                {
                    m_validBeaconedGrids.Clear();
                }
            }
            catch (Exception e)
            { Logging.Instance.WriteLine($"Exception in NaniteDeconstructionTargets.ParallelUpdate:\n{e}"); }
        }