Exemple #1
0
 protected void CheckPosition(Vector4 pos, out int n)
 {
     n = 0;
     for (int x = -1; x <= 1; x++)
     {
         for (int y = -1; y <= 1; y++)
         {
             for (int z = -1; z <= 1; z++)
             {
                 for (int w = -1; w <= 1; w++)
                 {
                     if (x == 0 && y == 0 && z == 0 && w == 0)
                     {
                         continue;                                       // this is ourselves
                     }
                     var nPos = new Vector4(pos.X + x, pos.Y + y, pos.Z + z, pos.W + w);
                     if (ActiveCells.Contains(nPos))
                     {
                         n++;
                     }
                 }
             }
         }
     }
 }
            public PocketDimension3d Next()
            {
                var minX = ActiveCells.Min(cell => cell.X);
                var minY = ActiveCells.Min(cell => cell.Y);
                var minZ = ActiveCells.Min(cell => cell.Z);
                var maxX = ActiveCells.Max(cell => cell.X);
                var maxY = ActiveCells.Max(cell => cell.Y);
                var maxZ = ActiveCells.Max(cell => cell.Z);

                var nextGenBuilder = ImmutableHashSet <Cell3d> .Empty.ToBuilder();

                for (int x = minX - 1; x <= maxX + 1; x++)
                {
                    for (int y = minY - 1; y <= maxY + 1; y++)
                    {
                        for (int z = minZ - 1; z <= maxZ + 1; z++)
                        {
                            var cell = new Cell3d(x, y, z);
                            var activeNeighboursCount = cell.GetNeighbours()
                                                        .Where(neighbour => ActiveCells.Contains(neighbour))
                                                        .Count();
                            var has2Neighbors = activeNeighboursCount == 2;
                            var has3Neighbors = activeNeighboursCount == 3;
                            var isActive      = ActiveCells.Contains(cell);

                            if (isActive && (has2Neighbors || has3Neighbors))
                            {
                                // If a cube is active and exactly 2 or 3 of its neighbors are also active,
                                // the cube remains active. Otherwise, the cube becomes inactive.
                                nextGenBuilder.Add(cell);
                            }
                            else if (!isActive && has3Neighbors)
                            {
                                // If a cube is inactive but exactly 3 of its neighbors are active,
                                // the cube becomes active. Otherwise, the cube remains inactive.
                                nextGenBuilder.Add(cell);
                            }
                        }
                    }
                }

                return(new PocketDimension3d(nextGenBuilder.ToImmutable()));
            }