Exemplo n.º 1
0
        public static int SearchForOne(int startX, int startY, int range, IsNavigableDelegate IsNavigable, CheckMatchDelegate CheckMatch, RectInt requiredZone)
        {
            outputTiles = Search(startX, startY, range, IsNavigable, CheckMatch, requiredZone, 1);
            if (outputTiles.Count == 0)
            {
                return(-1);
            }

            return(outputTiles[0]);
        }
Exemplo n.º 2
0
        public static List <int> Search(int startX, int startY, int range, IsNavigableDelegate IsNavigable, CheckMatchDelegate CheckMatch, RectInt requiredZone, int maxResultCount = 0)
        {
            mapWidth  = Map.instance.mapVector.x;
            mapHeight = Map.instance.mapVector.y;

            if (visitedTiles == null)
            {
                visitedTiles = new int[mapWidth, mapHeight];
                activeTiles  = new List <int>();
                nextTiles    = new List <int>();
                outputTiles  = new List <int>();
            }

            for (var x = 0; x < mapWidth; x++)
            {
                for (var y = 0; y < mapHeight; y++)
                {
                    visitedTiles[x, y] = -1;
                }
            }
            outputTiles.Clear();
            visitedTiles[startX, startY] = 0;
            activeTiles.Clear();
            nextTiles.Clear();
            nextTiles.Add(Hash(startX, startY));

            var steps = 0;

            while (nextTiles.Count > 0 && (steps < range || range == 0))
            {
                var temp = activeTiles;
                activeTiles = nextTiles;
                nextTiles   = temp;
                nextTiles.Clear();

                steps++;

                foreach (var t in activeTiles)
                {
                    int x, y;
                    Unhash(t, out x, out y);

                    for (var j = 0; j < dirsX.Length; j++)
                    {
                        var x2 = x + dirsX[j];
                        var y2 = y + dirsY[j];

                        if (x2 < 0 || y2 < 0 || x2 >= mapWidth || y2 >= mapHeight)
                        {
                            continue;
                        }

                        if (visitedTiles[x2, y2] != -1 && visitedTiles[x2, y2] <= steps)
                        {
                            continue;
                        }
                        var hash = Hash(x2, y2);
                        if (IsNavigable(x2, y2))
                        {
                            visitedTiles[x2, y2] = steps;
                            nextTiles.Add(hash);
                        }

                        if (x2 < requiredZone.xMin || x2 > requiredZone.xMax)
                        {
                            continue;
                        }
                        if (y2 < requiredZone.yMin || y2 > requiredZone.yMax)
                        {
                            continue;
                        }
                        if (!CheckMatch(x2, y2))
                        {
                            continue;
                        }
                        outputTiles.Add(hash);
                        if (maxResultCount != 0 && outputTiles.Count >= maxResultCount)
                        {
                            return(outputTiles);
                        }
                    }
                }
            }

            return(outputTiles);
        }
Exemplo n.º 3
0
    public static List <int> Search(int startX, int startY, int range, IsNavigableDelegate IsNavigable, CheckMatchDelegate CheckMatch, RectInt requiredZone, int maxResultCount = 0)
    {
        mapWidth  = Farm.instance.mapSize.x;
        mapHeight = Farm.instance.mapSize.y;

        if (visitedTiles == null)
        {
            visitedTiles = new int[mapWidth, mapHeight];
            activeTiles  = new List <int>();
            nextTiles    = new List <int>();
            outputTiles  = new List <int>();
        }

        for (int x = 0; x < mapWidth; x++)
        {
            for (int y = 0; y < mapHeight; y++)
            {
                visitedTiles[x, y] = -1;
            }
        }
        outputTiles.Clear();
        visitedTiles[startX, startY] = 0;
        activeTiles.Clear();
        nextTiles.Clear();
        nextTiles.Add(Hash(startX, startY));

        int steps = 0;

        while (nextTiles.Count > 0 && (steps < range || range == 0))
        {
            List <int> temp = activeTiles;
            activeTiles = nextTiles;
            nextTiles   = temp;
            nextTiles.Clear();

            steps++;

            for (int i = 0; i < activeTiles.Count; i++)
            {
                int x, y;
                Unhash(activeTiles[i], out x, out y);

                for (int j = 0; j < dirsX.Length; j++)
                {
                    int x2 = x + dirsX[j];
                    int y2 = y + dirsY[j];

                    if (x2 < 0 || y2 < 0 || x2 >= mapWidth || y2 >= mapHeight)
                    {
                        continue;
                    }

                    if (visitedTiles[x2, y2] == -1 || visitedTiles[x2, y2] > steps)
                    {
                        int hash = Hash(x2, y2);
                        if (IsNavigable(x2, y2))
                        {
                            visitedTiles[x2, y2] = steps;
                            nextTiles.Add(hash);
                        }
                        if (x2 >= requiredZone.xMin && x2 <= requiredZone.xMax)
                        {
                            if (y2 >= requiredZone.yMin && y2 <= requiredZone.yMax)
                            {
                                if (CheckMatch(x2, y2))
                                {
                                    outputTiles.Add(hash);
                                    if (maxResultCount != 0 && outputTiles.Count >= maxResultCount)
                                    {
                                        return(outputTiles);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        return(outputTiles);
    }