public override void FindTargets()
    {
        for (int i = 0; i < EnemyManager.Singleton.enemyList.Count; i++)
        {
            // if (EnemyManager.Singleton.enemyList[i].enemySpeed > 1.25f) {// Only Targets Bikes
            if (!TargetList.Contains(EnemyManager.Singleton.enemyList[i]))
            {
                if ((EnemyManager.Singleton.enemyList[i].transform.position - towerPosition).magnitude <= range)
                {
                    TargetList.Add(EnemyManager.Singleton.enemyList[i]);
                    Fire();
                }
            }
            else
            {
                if ((EnemyManager.Singleton.enemyList[i].transform.position - towerPosition).magnitude > range)
                {
                    EnemyManager.Singleton.enemyList[i].enemySpeed = EnemyManager.Singleton.enemyList[i].enemySpeed / .85f;
                    TargetList.Remove(EnemyManager.Singleton.enemyList[i]);
                }
            }

            // }
        }
        // SortTargetList();
    }
 // Start is called before the first frame update
 public override void FindTargets()
 {
     for (int i = 0; i < EnemyManager.Singleton.enemyList.Count; i++)
     {
         if (EnemyManager.Singleton.enemyList[i].secondName != "Grape")
         {
             if (!TargetList.Contains(EnemyManager.Singleton.enemyList[i]))
             {
                 if ((EnemyManager.Singleton.enemyList[i].transform.position - towerPosition).magnitude <= range)
                 {
                     print(EnemyManager.Singleton.enemyList[i].secondName);
                     TargetList.Add(EnemyManager.Singleton.enemyList[i]);
                 }
             }
             else
             {
                 if ((EnemyManager.Singleton.enemyList[i].transform.position - towerPosition).magnitude > range)
                 {
                     TargetList.Remove(EnemyManager.Singleton.enemyList[i]);
                 }
             }
         }
     }
     SortTargetList();
 }
Ejemplo n.º 3
0
        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;

            foreach (IMyEntity item in m_potentialTargetList.ToList())
            {
                if (item == null || TargetList.Contains(item) || item.Closed)
                {
                    continue;
                }

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

                if (found)
                {
                    continue;
                }

                if (m_constructionBlock.HasRequiredPowerForNewTarget(this))
                {
                    AddTarget(item);

                    Logging.Instance.WriteLine(string.Format("[Floating] Adding Floating Object Target: conid={0} type={1} entityID={2} position={3}",
                                                             m_constructionBlock.ConstructionBlock.EntityId, item.GetType().Name, item.EntityId, item.GetPosition()), 1);

                    if (++TargetListCount >= maxTargets)
                    {
                        break;
                    }
                }
                else
                {
                    InvalidTargetReason("Not enough power for new target!");
                    break;
                }
            }
        }
        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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public Boolean ColorCollision(Vector2 enemysPosition, Vector2 targetPosition)
        {
            // Use 56 x 56 as target collision thus border offset is 4px on all sides.
            Vector2 offsetPosition = targetPosition;

            offsetPosition.X += borderSize;
            offsetPosition.Y += borderSize;

            Int16 enemysPosX = (Int16)enemysPosition.X;
            Int16 enemysPosY = (Int16)enemysPosition.Y;
            Int16 targetPosX = (Int16)targetPosition.X;
            Int16 targetPosY = (Int16)targetPosition.Y;
            Int16 offsetPosX = (Int16)offsetPosition.X;
            Int16 offsetPosY = (Int16)offsetPosition.Y;

            //Int16 lft = Math.Max(enemysPosX, targetPosX);
            //Int16 top = Math.Max(enemysPosY, targetPosY);
            //Int16 rgt = Math.Min((Int16)(enemysPosX + enemysSize), (Int16)(targetPosX + targetSize));
            //Int16 bot = Math.Min((Int16)(enemysPosY + enemysSize), (Int16)(targetPosY + targetSize));

            Int16 lft = Math.Max(enemysPosX, offsetPosX);
            Int16 top = Math.Max(enemysPosY, offsetPosY);
            Int16 rgt = Math.Min((Int16)(enemysPosX + enemysSize), (Int16)(offsetPosX + offsetSize));
            Int16 bot = Math.Min((Int16)(enemysPosY + enemysSize), (Int16)(offsetPosY + offsetSize));


            // Check every point within the intersection bounds.
            for (Int16 y = top; y < bot; y++)
            {
                for (Int16 x = lft; x < rgt; x++)
                {
                    //UInt16 targetIndex = (UInt16)((x - targetPosX) + (y - targetPosY) * targetSize);
                    UInt16 targetIndex = (UInt16)((x - offsetPosX) + (y - offsetPosY) * offsetSize);
                    if (!TargetList.Contains(targetIndex))
                    {
                        continue;
                    }
                    UInt16 enemysIndex = (UInt16)((x - enemysPosX) + (y - enemysPosY) * enemysSize);
                    if (!EnemysList.Contains(enemysIndex))
                    {
                        continue;
                    }

                    // An intersection has been found.
                    return(true);
                }
            }

            // No intersection found.
            return(false);
        }
Ejemplo n.º 7
0
        public void AddToTargetList(Character target)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (target == m_character)
            {
                throw new ArgumentOutOfRangeException(nameof(target));
            }

            if (TargetList.Contains(target) == false)
            {
                TargetList.Add(target);
            }
        }
        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.";
                    }
                }
            }
        }
Ejemplo n.º 9
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;
                        }
                    }
                }
            }
        }
Ejemplo n.º 10
0
    private IEnumerator Detonate()
    {
        if (this.debugLevel > DEBUG_LEVELS.Off)
        {
            string msg = "Detonating...";
            Debug.Log(string.Format("Detonator ({0}): {1}", this.name, msg));
        }

        // Wait for next frame to begin to be sure targets have been propegated
        // This also makes this loop easier to manage.
        yield return(new WaitForFixedUpdate());  // Because of physics...matters?

        // START EVENT
        if (this.OnDetonatingDelegates != null)
        {
            this.OnDetonatingDelegates();
        }

        // Keep track of targets which have already been processed so they
        //      aren't hit twice
        var processedTargetList = new TargetList();

        this.range = Vector3.zero;

        float timer    = 0;
        float progress = 0;  // Normalized mount processed 0-1

        // The timer can exit the loop if used
        while (true)
        {
            // UPDATE EVENT
            if (this.OnDetonatingUpdateDelegates != null)
            {
                this.OnDetonatingUpdateDelegates(progress);
            }

            // Exit?
            if (timer >= this.durration)
            {
                break;
            }
            timer += Time.deltaTime;

            progress = timer / this.durration;

            this.range = this.maxRange * progress;

            // Build a list of targets in range which have NOT been processed yet.
            var newTargets = new TargetList();
            foreach (Target target in this.targets)
            {
                if (!processedTargetList.Contains(target))
                {
                    newTargets.Add(target);
                }
            }

            if (newTargets.Count > 0)
            {
                if (this.debugLevel > DEBUG_LEVELS.Off)
                {
                    string msg = string.Format("Detonation hitting targets: {0}", this.targets);
                    Debug.Log(string.Format("Detonator ({0}): {1}", this.name, msg));
                }

                foreach (Target target in newTargets)
                {
                    target.targetable.OnHit
                    (
                        this.effectsOnTarget,
                        target,
                        this.perimeter.collider
                    );

                    processedTargetList.Add(target);
                }
            }

            yield return(new WaitForFixedUpdate());  // Because of physics...matters?
        }

        // Prevent being run more than once in a frame where it has already
        //   been destroyed.
        if (!this.gameObject.activeInHierarchy)
        {
            yield break;  // Same as return
        }
        TargetPro.InstanceManager.Despawn(this.transform);
    }
        public override void FindTargets(ref Dictionary <string, int> available, List <NaniteConstructionBlock> blockList)
        {
            var maxTargets = GetMaximumTargets();

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

                return;
            }

            string LastInvalidTargetReason = "";

            int targetListCount = m_targetList.Count;

            HashSet <Vector3D>      usedPositions = new HashSet <Vector3D>();
            List <NaniteMiningItem> removeList    = new List <NaniteMiningItem>();

            foreach (NaniteMiningItem item in m_potentialMiningTargets.ToList())
            {
                if (item == null || TargetList.Contains(item))
                {
                    removeList.Add(item);
                    continue;
                }
                if (m_globalPositionList.Contains(item.Position) || usedPositions.Contains(item.Position))
                {
                    LastInvalidTargetReason = "Mining position was already targeted";
                    removeList.Add(item);
                    continue;
                }
                if (!m_constructionBlock.HasRequiredPowerForNewTarget(this))
                {
                    LastInvalidTargetReason = "Insufficient power for another target";
                    break;
                }

                bool found = false;
                foreach (var block in blockList.ToList())
                {
                    if (block != null && block.GetTarget <NaniteMiningTargets>().TargetList.FirstOrDefault(x => ((NaniteMiningItem)x).Position == item.Position) != null)
                    {
                        found = true;
                        LastInvalidTargetReason = "Another factory has this voxel as a target";
                        break;
                    }
                }

                if (found)
                {
                    removeList.Add(item);
                    continue;
                }
                var nearestFactory = GetNearestFactory(TargetName, item.Position);
                if (IsInRange(nearestFactory, item.Position, m_maxDistance))
                {
                    Logging.Instance.WriteLine(string.Format("[Mining] Adding Mining Target: conid={0} pos={1} type={2}",
                                                             m_constructionBlock.ConstructionBlock.EntityId, item.Position, MyDefinitionManager.Static.GetVoxelMaterialDefinition(item.VoxelMaterial).MinedOre), 1);

                    removeList.Add(item);
                    usedPositions.Add(item.Position);

                    MyAPIGateway.Utilities.InvokeOnGameThread(() =>
                    {
                        try
                        {
                            if (m_constructionBlock.IsUserDefinedLimitReached())
                            {
                                InvalidTargetReason("User defined maximum nanite limit reached");
                            }
                            else if (item != null)
                            {
                                removeList.Add(item);
                                TargetList.Add(item);
                            }
                        }
                        catch (Exception e)
                        {
                            Logging.Instance.WriteLine("NaniteMiningTargets.FindTargets.Invocation1: " + e.ToString());
                        }
                    });

                    if (targetListCount++ >= maxTargets)
                    {
                        break;
                    }
                }
                else
                {
                    removeList.Add(item);
                }
            }

            foreach (var item in removeList)
            {
                m_potentialMiningTargets.Remove(item);
            }

            if (LastInvalidTargetReason != "")
            {
                InvalidTargetReason(LastInvalidTargetReason);
            }
        }
Ejemplo n.º 12
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;
            }

            DateTime start = DateTime.Now;

            using (Lock.AcquireExclusiveUsing())
            {
                if (m_constructionBlock.IsUserDefinedLimitReached())
                {
                    m_lastInvalidTargetReason = "User defined maximum nanite limit reached";
                    return;
                }

                //foreach (NaniteMiningItem item in PotentialTargetList)
                for (int r = PotentialTargetList.Count - 1; r >= 0; r--)
                {
                    var item = (NaniteMiningItem)PotentialTargetList[r];
                    if (TargetList.Contains(item))
                    {
                        continue;
                    }

                    if (m_globalPositionList.Contains(item.Position))
                    {
                        m_lastInvalidTargetReason = "Another factory has this voxel as a target";
                        continue;
                    }

                    var  blockList = NaniteConstructionManager.GetConstructionBlocks((IMyCubeGrid)m_constructionBlock.ConstructionBlock.CubeGrid);
                    bool found     = false;
                    foreach (var block in blockList)
                    {
                        // This can be sped up if necessary by indexing items by position
                        if (block.GetTarget <NaniteMiningTargets>().TargetList.FirstOrDefault(x => ((NaniteMiningItem)x).Position == item.Position) != null)
                        {
                            found = true;
                            break;
                        }
                    }

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

                    if (!NaniteMining.CheckVoxelContent(item.VoxelId, item.Position))
                    {
                        continue;
                    }

                    if (Vector3D.DistanceSquared(m_constructionBlock.ConstructionBlock.GetPosition(), item.Position) < m_maxDistance * m_maxDistance &&
                        NaniteConstructionPower.HasRequiredPowerForNewTarget((IMyFunctionalBlock)m_constructionBlock.ConstructionBlock, this))
                    {
                        Logging.Instance.WriteLine(string.Format("ADDING Mining Target: conid={0} pos={1} type={2}", m_constructionBlock.ConstructionBlock.EntityId, item.Position, MyDefinitionManager.Static.GetVoxelMaterialDefinition(item.VoxelMaterial).MinedOre));

                        //PotentialTargetList.Remove(item);
                        TargetList.Add(item);
                        m_globalPositionList.Add(item.Position);
                        if (TargetList.Count >= GetMaximumTargets())
                        {
                            break;
                        }
                    }
                }
            }

            //Logging.Instance.WriteLine(string.Format("FindTargets took {0}ms", (DateTime.Now - start).TotalMilliseconds));
        }
        public override void FindTargets(ref Dictionary <string, int> available)
        {
            if (!IsEnabled())
            {
                return;
            }

            ComponentsRequired.Clear();
            if (m_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 (Lock.AcquireExclusiveUsing())
            {
                var potentialList = m_potentialTargetList.OrderByDescending(x => GetMissingComponentCount(inventoryManager, (IMySlimBlock)x)).ToList();
                for (int r = potentialList.Count - 1; r >= 0; r--)
                {
                    if (m_constructionBlock.IsUserDefinedLimitReached())
                    {
                        m_lastInvalidTargetReason = "User defined maximum nanite limit reached";
                        return;
                    }

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

                    missing.Clear();
                    item.GetMissingComponents(missing);
                    bool foundMissingComponents = true;

                    if (MyAPIGateway.Session.CreativeMode)
                    {
                        foundMissingComponents = true;
                    }
                    else if (missing.Count > 0)
                    {
                        foundMissingComponents = inventoryManager.CheckComponentsAvailable(ref missing, ref available);
                    }

                    if (foundMissingComponents && NaniteConstructionPower.HasRequiredPowerForNewTarget((IMyFunctionalBlock)m_constructionBlock.ConstructionBlock, this))
                    {
                        // Check to see if another block targetting this for construction
                        var  blockList = NaniteConstructionManager.GetConstructionBlocks((IMyCubeGrid)m_constructionBlock.ConstructionBlock.CubeGrid);
                        bool found     = false;
                        foreach (var block in blockList)
                        {
                            if (block.Targets.First(y => y is NaniteConstructionTargets).TargetList.Contains(item as IMySlimBlock))
                            {
                                found = true;
                                break;
                            }
                        }

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

                        m_potentialTargetList.RemoveAt(r);
                        m_targetList.Add(item);
                        var def = item.BlockDefinition as MyCubeBlockDefinition;
                        Logging.Instance.WriteLine(string.Format("ADDING Construction/Repair Target: conid={0} subtype={1} entityID={2} position={3}", m_constructionBlock.ConstructionBlock.EntityId, def.Id.SubtypeId, item.FatBlock != null ? item.FatBlock.EntityId : 0, item.Position));
                        if (m_targetList.Count >= GetMaximumTargets())
                        {
                            break;
                        }
                    }
                    else if (!foundMissingComponents)
                    {
                        foreach (var component in missing)
                        {
                            if (!ComponentsRequired.ContainsKey(component.Key))
                            {
                                ComponentsRequired.Add(component.Key, component.Value);
                            }
                            else
                            {
                                ComponentsRequired[component.Key] += component.Value;
                            }
                        }

                        m_lastInvalidTargetReason = "Missing components";
                    }
                    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)
        {
            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 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);
            }
        }
        public override void FindTargets(ref Dictionary <string, int> available, List <NaniteConstructionBlock> blockList)
        {
            InvalidTargetReason("");

            var maxTargets = GetMaximumTargets();

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

            Dictionary <string, int> missing = new Dictionary <string, int>();
            string LastInvalidTargetReason   = "";

            int targetListCount = m_targetList.Count;

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

                missing.Clear();
                item.GetMissingComponents(missing);
                if (missing == null && !MyAPIGateway.Session.CreativeMode)
                {
                    continue;
                }

                bool foundMissingComponents = true;

                if (missing.Count > 0)
                {
                    foundMissingComponents = m_constructionBlock.InventoryManager.CheckComponentsAvailable(ref missing, ref available);
                }

                if (foundMissingComponents && m_constructionBlock.HasRequiredPowerForNewTarget(this))
                {
                    bool found = false;
                    foreach (var block in blockList.ToList())
                    {
                        if (block != null && block != m_constructionBlock && !m_constructionBlock.Slaves.Contains(block) &&
                            block.Targets.First(y => y is NaniteConstructionTargets).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("[Construction] Adding Construction/Repair Target: conid={0} subtype={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;
                    }
                }
                else if (!foundMissingComponents)
                {
                    LastInvalidTargetReason = "Missing components";
                }

                else if (!m_constructionBlock.HasRequiredPowerForNewTarget(this))
                {
                    LastInvalidTargetReason = "Insufficient power for another target.";
                    break;
                }
            }
            if (LastInvalidTargetReason != "")
            {
                InvalidTargetReason(LastInvalidTargetReason);
            }
        }
Ejemplo n.º 17
0
        private IEnumerator Detonate()
        {
            if (this.debugLevel > DEBUG_LEVELS.Off)
            {
                string msg = "Detonating...";
                Debug.Log(string.Format("Detonator ({0}): {1}", this.name, msg));
            }

            // Wait for next frame to begin to be sure targets have been propegated
            // This also makes this loop easier to manage.
            yield return new WaitForFixedUpdate();  // Because of physics...matters?

            // START EVENT
            if (this.OnDetonatingDelegates != null) this.OnDetonatingDelegates();

            // Keep track of targets which have already been processed so they 
            //      aren't hit twice
            var processedTargetList = new TargetList();

            this.range = Vector3.zero;

            float timer = 0;
            float progress = 0;  // Normalized mount processed 0-1

            // The timer can exit the loop if used 
            while (true)
            {
                // UPDATE EVENT
                if (this.OnDetonatingUpdateDelegates != null)
                    this.OnDetonatingUpdateDelegates(progress);

                // Exit?
                if (timer >= this.durration) break;
                timer += Time.deltaTime;

                progress = timer / this.durration;

                this.range = this.maxRange * progress;

                // Build a list of targets in range which have NOT been processed yet.
                var newTargets = new TargetList();
                foreach (Target target in this.targets)
                    if (!processedTargetList.Contains(target))
                        newTargets.Add(target);

                if (newTargets.Count > 0)
                {
                    if (this.debugLevel > DEBUG_LEVELS.Off)
                    {
                        string msg = string.Format("Detonation hitting targets: {0}", this.targets);
                        Debug.Log(string.Format("Detonator ({0}): {1}", this.name, msg));
                    }

                    foreach (Target target in newTargets)
                    {
                        target.targetable.OnHit
                        (
                            this.effectsOnTarget,
                            target,
                            this.perimeter.collider
                        );

                        processedTargetList.Add(target);
                    }
                }

                yield return new WaitForFixedUpdate();  // Because of physics...matters?
            }

            // Prevent being run more than once in a frame where it has already 
            //   been destroyed.
            if (!this.gameObject.activeInHierarchy)
                yield break;  // Same as return

            PathologicalGames.InstanceManager.Despawn(this.transform);
        }
Ejemplo n.º 18
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);
            }
        }