Esempio n. 1
0
        public static List <IMySlimBlock> GetBlocksContactingVoxel(IMyCubeGrid grid, MyPlanet planet, out byte voxelType)
        {
            Vector3D[] corners = grid.WorldAABB.GetCorners();
            if (corners.All(c => Vector3D.DistanceSquared(c, planet.GetClosestSurfacePointGlobal(c)) > 2500))
            {
                voxelType = 0;
                return(new List <IMySlimBlock>());
            }

            Vector3D planetPos = planet.PositionComp.GetPosition();

            var result = new MyConcurrentHashSet <IMySlimBlock>();
            var blocks = new List <IMySlimBlock>();

            grid.GetBlocks(blocks);
            byte id = 0;

            MyAPIGateway.Parallel.ForEach(blocks, block =>
            {
                try
                {
                    var b = block.GetPosition();
                    Vector3D closestSurfacePoint = planet.GetClosestSurfacePointGlobal(ref b);

                    if (Vector3D.DistanceSquared(planetPos, closestSurfacePoint) > Vector3D.DistanceSquared(planetPos, b))
                    {
                        var hits = new List <IHitInfo>();
                        MyAPIGateway.Physics.CastRay(b, closestSurfacePoint, hits);
                        foreach (IHitInfo hit in hits)
                        {
                            if (hit.HitEntity is IMyVoxelBase)
                            {
                                closestSurfacePoint = hit.Position;
                            }
                        }
                    }

                    double cd = Vector3D.DistanceSquared(b, closestSurfacePoint);
                    if (cd > 200)
                    {
                        return;
                    }

                    if (cd > 6.25)
                    {
                        BoundingBoxD box;
                        block.GetWorldBoundingBox(out box, true);
                        Vector3D[] bc = box.GetCorners();
                        if (bc.All(c => Vector3D.DistanceSquared(c, planet.GetClosestSurfacePointGlobal(c)) > 6.25))
                        {
                            return;
                        }
                    }

                    MyVoxelMaterialDefinition mat = planet.GetMaterialAt_R(ref closestSurfacePoint);
                    if (Config.VOXEL_IDS.ContainsKey(mat.Index))
                    {
                        result.Add(block);
                    }
                    id = mat.Index;
                }
                catch
                {
                    //meh!
                }
            });
            voxelType = id;
            return(result.ToList());

            /*
             * var chunks = new HashSet<Vector3I[]>();
             *
             * int chunkSize = grid.GridSizeEnum == MyCubeSize.Large ? 3 : 5;
             * for (int x = grid.Min.X; x < grid.Max.X + chunkSize; x += chunkSize)
             * {
             *  for (int y = grid.Min.Y; y < grid.Max.Y + chunkSize; y += chunkSize)
             *  {
             *      for (int z = grid.Min.Z; z < grid.Max.Z + chunkSize; z += chunkSize)
             *      {
             *          chunks.Add(new[]
             *                     {
             *                         new Vector3I(x, y, z),
             *                         new Vector3I(x + chunkSize, y, z),
             *                         new Vector3I(x, y + chunkSize, z),
             *                         new Vector3I(x, y, z + chunkSize),
             *                         new Vector3I(x + chunkSize, y + chunkSize, z),
             *                         new Vector3I(x + chunkSize, y, z + chunkSize),
             *                         new Vector3I(x, y + chunkSize, z + chunkSize),
             *                         new Vector3I(x + chunkSize, y + chunkSize, z + chunkSize),
             *                     });
             *      }
             *  }
             * }
             *
             * MyAPIGateway.Parallel.ForEach(chunks, chunk =>
             *                                    {
             *                                        var success = false;
             *                                        foreach (Vector3I pos in chunk)
             *                                        {
             *                                            Vector3D d = grid.GridIntegerToWorld(pos);
             *                                            Vector3D s = planet.GetClosestSurfacePointGlobal(ref d);
             *                                            if (Vector3D.DistanceSquared(d, s) > 6.25)
             *                                                continue;
             *
             *                                            if (!Config.VOXEL_IDS.ContainsKey(planet.GetMaterialAt_R(ref s).Index))
             *                                                continue;
             *                                            success = true;
             *                                            break;
             *                                        }
             *
             *                                        if (!success)
             *                                            return;
             *
             *                                        foreach (Vector3I pos in chunk)
             *                                        {
             *                                            IMySlimBlock block = grid.GetCubeBlock(pos);
             *                                            if (block != null)
             *                                                result.Add(block);
             *                                        }
             *                                    });
             *
             * return result.ToList();
             */
        }