示例#1
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));
        }