예제 #1
0
        /// <summary>
        /// 2016-2-22
        /// Returns all the neighbors of the provided coordinates. Optional last parameter indicates the
        /// neighbor filter:
        ///     0 -- return all neighbors
        ///     1 -- return orthogonal neighbors only
        ///     2 -- diagonal neighbors only
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static List <T> GetNeighborsOf <T>(this T[,,] source, int x, int y, int z, int filter = 0)
        {
            if (!source.ContainsCoords(x, y, z))
            {
                return(null);
            }

            List <T> neighbors = new List <T>();

            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    for (int k = -1; k <= 1; k++)
                    {
                        int indexX = x + i;
                        int indexY = y + j;
                        int indexZ = z + k;

                        //  ignore coordinates that aren't on the map
                        if (!source.ContainsCoords(indexX, indexY, indexZ))
                        {
                            continue;
                        }

                        //  ignore the center coordinate
                        if (i == 0 && j == 0 && k == 0)
                        {
                            continue;
                        }

                        switch (filter)
                        {
                        case 1:
                            //  skip diagonals
                            if ((i == -1 || i == 1) && j != 0 && k != 0)
                            {
                                continue;
                            }
                            if ((j == -1 || j == 1) && i != 0 && k != 0)
                            {
                                continue;
                            }
                            if ((k == -1 || k == 1) && i != 0 && j != 0)
                            {
                                continue;
                            }
                            break;

                        case 2:
                            //  skip orthogonals
                            if ((i == -1 || i == 1) && j == 0 && k == 0)
                            {
                                continue;
                            }
                            if ((j == -1 || j == 1) && i == 0 && k == 0)
                            {
                                continue;
                            }
                            if ((k == -1 || k == 1) && i == 0 && j == 0)
                            {
                                continue;
                            }
                            break;

                        case 0:
                        default:
                            //  keep everything
                            break;
                        }
                        neighbors.Add(source[indexX, indexY, indexZ]);
                    }
                }
            }
            return(neighbors);
        }