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