Beispiel #1
0
    //---------------------------------------------------------------------------------------

    public void MakeAStarLogic2(E03_TileType[,] maze)
    {
        int mapSize = maze.GetLength(0);
        int goalY   = mapSize - 2;
        int goalX   = mapSize - 2;

        bool[,] closed = new bool[mapSize, mapSize];
        int[,] open    = new int[mapSize, mapSize];
        for (int y = 0; y < mapSize; y++)
        {
            for (int x = 0; x < mapSize; x++)
            {
                open[y, x] = int.MaxValue;
            }
        }

        E03_Pos[,] from  = new E03_Pos[mapSize, mapSize];
        from[PosY, PosX] = new E03_Pos(PosY, PosX);

        D05_PriorityQueue <E03_PQNode> pq = new D05_PriorityQueue <E03_PQNode>();

        open[PosY, PosX] = 10 * (Mathf.Abs(goalY - PosY) + Mathf.Abs(goalX - PosX));
        pq.Push(new E03_PQNode()
        {
            F = open[PosY, PosX], G = 0, X = PosX, Y = PosY
        });

        while (pq.Count > 0)
        {
            E03_PQNode node = pq.Pop();
            int        nowY = node.Y;
            int        nowX = node.X;

            if (closed[nowY, nowX])
            {
                continue;
            }
            if (nowY == goalY && nowX == goalX)
            {
                break;
            }

            for (int i = 0; i < forwardX2.Length; i++)
            {
                int nextY = nowY + forwardY2[i];
                int nextX = nowX + forwardX2[i];

                if (nextX < 0 || nextY < 0 || nextX >= mapSize || nextY >= mapSize)
                {
                    continue;
                }
                if (maze[nextY, nextX] == E03_TileType.Wall)
                {
                    continue;
                }
                if (closed[nextY, nextX])
                {
                    continue;
                }

                int g = node.G + moveCost2[i];
                int h = 10 * (Mathf.Abs(goalY - nextY) + Mathf.Abs(goalX - nextX));

                if (open[nextY, nextX] < g + h)
                {
                    continue;
                }

                open[nextY, nextX] = g + h;
                pq.Push(new E03_PQNode()
                {
                    F = g + h, G = g, X = nextX, Y = nextY
                });
                from[nextY, nextX] = new E03_Pos(nowY, nowX);
            }
        }

        AStarRoad = MakeRoad(from, mapSize);
    }
Beispiel #2
0
    //---------------------------------------------------------------------------------------

    public void MakeAStarLogic(E03_TileType[,] maze)
    {
        int mapSize = maze.GetLength(0);
        int goalY   = mapSize - 2;
        int goalX   = mapSize - 2;
        int startY  = PosY;
        int startX  = PosX;

        bool[,] closed       = new bool[mapSize, mapSize];
        int[,] open          = new int[mapSize, mapSize];
        E03_Pos[,] from      = new E03_Pos[mapSize, mapSize];
        from[startY, startX] = new E03_Pos(startY, startX);

        for (int y = 0; y < open.GetLength(0); y++)
        {
            for (int x = 0; x < open.GetLength(1); x++)
            {
                open[y, x] = int.MaxValue;
            }
        }

        D05_PriorityQueue <E03_PQNode> pq = new D05_PriorityQueue <E03_PQNode>();

        open[startY, startY] = Mathf.Abs(goalY - startY) + Math.Abs(goalX - startX) + 0; // 시작점이므로 현재까지 온 거리는 0이다. 사실 더해주지 않아도 무방하다.
        pq.Push(new E03_PQNode()
        {
            F = Mathf.Abs(goalY - startY) + Math.Abs(goalX - startX), G = 0, X = startX, Y = startY
        });

        while (pq.Count > 0)
        {
            E03_PQNode node = pq.Pop();

            if (closed[node.Y, node.X])
            {
                continue;
            }

            closed[node.Y, node.X] = true;
            int nowY = node.Y;
            int nowX = node.X;

            if (nowY == goalY && nowX == goalX)
            {
                break;
            }

            for (int i = 0; i < forwardX.Length; i++)
            {
                int nextY = nowY + forwardY[i];
                int nextX = nowX + forwardX[i];

                if (nextX < 0 || nextY < 0 || nextX >= mapSize || nextY >= mapSize)
                {
                    continue;
                }
                if (closed[nextY, nextX])
                {
                    continue;
                }
                if (maze[nextY, nextX] == E03_TileType.Wall)
                {
                    continue;
                }

                int g        = node.G + moveCost[i];
                int h        = Mathf.Abs(goalY - nowY) + Math.Abs(goalX - nowX);
                int distance = g + h;

                if (open[nextY, nextX] < distance)
                {
                    continue;
                }

                pq.Push(new E03_PQNode()
                {
                    F = distance, G = g, X = nextX, Y = nextY
                });
                open[nextY, nextX] = distance;
                from[nextY, nextX] = new E03_Pos(nowY, nowX);
            }
        }

        AStarRoad = MakeRoad(from, mapSize);
    }