迪克斯特拉';十六进制网格上的s算法不效率,C#,Unity3D

迪克斯特拉';十六进制网格上的s算法不效率,C#,Unity3D,c#,unity3d,path-finding,C#,Unity3d,Path Finding,我正在尝试使用3D六角网格地图创建一个基于回合的策略游戏,我已经实现了dijkstra的算法,但它并没有100%有效地运行,我也不知道为什么。我还试图实现一个*但不得不停止,因为我无法正确地实现它,所以对此的任何帮助都将不胜感激 我的单位将其游戏对象和目标的矢量3传递给generate path函数,图形列表中的每个节点都会填充其x、y、z及其所有邻居 低效率是这样的,当移动时;当在奇数Z平面上时,在a-X方向上,或当在偶数Z平面上时,在a+X方向上,将执行额外的步骤,如屏幕截图所示。另一个低效

我正在尝试使用3D六角网格地图创建一个基于回合的策略游戏,我已经实现了dijkstra的算法,但它并没有100%有效地运行,我也不知道为什么。我还试图实现一个*但不得不停止,因为我无法正确地实现它,所以对此的任何帮助都将不胜感激

我的单位将其游戏对象和目标的矢量3传递给generate path函数,图形列表中的每个节点都会填充其x、y、z及其所有邻居

低效率是这样的,当移动时;当在奇数Z平面上时,在a-X方向上,或当在偶数Z平面上时,在a+X方向上,将执行额外的步骤,如屏幕截图所示。另一个低效之处是,当在Z平面上移动时,通常会采取额外的步骤,因为代码似乎更喜欢在接近Z平面之前尽可能长时间保持其X值不变。这会导致单位在开始Z移动时距离目标1瓦片,而不是一开始负移动1 X时距离目标1瓦片

我将添加路径生成代码、邻居生成代码和节点类代码,以及效率低下发生的屏幕截图,因为我知道我的解释最多也不清楚。邻居代码确保最高的相邻磁贴是存储为邻居的磁贴(它还必须搜索类型,因为我有多种磁贴类型)

提前非常感谢您,感谢任何能够提供帮助或洞察问题所在的人

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
using System.Linq;

public class UnitMasterScript : MonoBehaviour {

//  private int Number_of_neighbours = 6;
private int Number_of_neighbours = 6;
private GameObject[] Neighbours;
Node[,,] graph;

public bool MapMakerDone = false;

void Update()
{
    if (MapMakerDone == true)
    {
        // Wait for MapMaker to change MapMakerDone to true then allow rest of script to run

        GameObject Map = GameObject.Find("MapMaker");
        int WidthVal = Map.GetComponent<MapMakerFromFile>().WidthVal;
        int HeightVal = Map.GetComponent<MapMakerFromFile>().HeightVal;
        int DepthVal = Map.GetComponent<MapMakerFromFile>().DepthVal;

        // Graph of node generation code
        // Code to find which hex is to each side of this hex
        // Need to find hex to left, right, ul, ur, ll, lr
        // Need to find hex at the top of the stack adjacent
        // 0:L 1:R 2:UL 3:UR 4:LL 5:LR
        graph = new Node[WidthVal, HeightVal, DepthVal];

        for (int x = 0; x < WidthVal; x++)
        {
            for (int y = 0; y < HeightVal; y++)
            {
                for (int z = 0; z < DepthVal; z++)
                {
                    graph[x, y, z] = new Node();

                    graph[x, y, z].x = x;
                    graph[x, y, z].y = y;
                    graph[x, y, z].z = z;
                }
            }
        }

        for (int x = 0; x < WidthVal; x++)
        {
            for (int y = 0; y < HeightVal; y++)
            {
                for (int z = 0; z < DepthVal; z++)
                {

                    // Set up the x and y for the neighbour as the source so it can be used
                    int neighbourX = x;
                    int neighbourY = 0; // must always start from 0 to ensure a downstep isn't missed
                    int neighbourZ = z;
                    int neighbourType = 0;
                    int correct_type = 0;

                    GameObject Hex_Present_checker = null;
                    // First needs to check if there even is a tile at this coordinate location
                    for (neighbourType = 0; neighbourType < 5; neighbourType++)
                    {
                        Hex_Present_checker = GameObject.Find("Hex_" + x + "_" + y + "_" + z + "_" + neighbourType);
                        if (Hex_Present_checker != null)
                        {
                            correct_type = neighbourType;
                        }
                        Hex_Present_checker = null;
                    }

                    if (correct_type != 0)
                    {
                        neighbourType = correct_type;
                        // int Number_of_neighbours = 6;
                        // GameObject[] Neighbours;
                        Neighbours = new GameObject[Number_of_neighbours];

                        // For each value of each tile in neighbours find what the tile coordinates are in XYZ 
                        for (int q = 0; q < Number_of_neighbours; q++)
                        {
                            // Finds X and Z values of the neighbours
                            if (q < 2)
                            {
                                if (q == 0) { neighbourX = x + 1; }
                                if (q == 1) { neighbourX = x - 1; }
                            }
                            if (z % 2 == 1)
                            {
                                if (q == 2) { neighbourX = x; neighbourZ = z + 1; }
                                if (q == 3) { neighbourX = x + 1; neighbourZ = z + 1; }
                                if (q == 4) { neighbourX = x; neighbourZ = z - 1; }
                                if (q == 5) { neighbourX = x + 1; neighbourZ = z - 1; }
                            }
                            else
                            {
                                if (q == 2) { neighbourX = x - 1; neighbourZ = z + 1; }
                                if (q == 3) { neighbourX = x; neighbourZ = z + 1; }
                                if (q == 4) { neighbourX = x - 1; neighbourZ = z - 1; }
                                if (q == 5) { neighbourX = x; neighbourZ = z - 1; }
                            }
                            // Checks for the highest tile for the XZ coordinate and gets its Y value
                            GameObject potential_highest_ring;
                            int highest_Y = 0;
                            int correct_neighbour_type = 0;
                            for (neighbourY = 0; neighbourY < HeightVal; neighbourY++)
                            {
                                for (neighbourType = 0; neighbourType < 5; neighbourType++)
                                {
                                    potential_highest_ring = null;
                                    potential_highest_ring = GameObject.Find("Hex_" + neighbourX + "_" + neighbourY + "_" + neighbourZ + "_" + neighbourType);
                                    if (potential_highest_ring != null)
                                    {
                                        highest_Y = neighbourY;
                                        correct_neighbour_type = neighbourType;
                                    }
                                }
                            }
                            // Need to check if there is a neighbour at the given coordinates
                            // Debug.Log("Hex_" + neighbourX + "_" + highest_Y + "_" + neighbourZ + "_" + neighbourType);
                            Neighbours[q] = GameObject.Find("Hex_" + neighbourX + "_" + highest_Y + "_" + neighbourZ + "_" + correct_neighbour_type);
                            // While there is a neighbour in the neighbours array
                            // add it's coordinates to the graph node as a part of its neighbours sublist
                            if (Neighbours[q] != null)
                            {
                                graph[x, y, z].neighbours.Add(graph[neighbourX, highest_Y, neighbourZ]);
                            }
                        }
                    }
                }
            }
        }
        MapMakerDone = false;
    }
}

// List<Node> currentPath = null;


public List<Node> GeneratePathTo(GameObject SelectedUnit, Vector3 targetVec)
{
    // Dijkstra's Algorithm Implementation
    Dictionary<Node, float> dist = new Dictionary<Node, float>();
    Dictionary<Node, Node> prev = new Dictionary<Node, Node>();

    List<Node> unvisited = new List<Node>();


    Node source = graph[
        SelectedUnit.GetComponent<UnitBasicScript>().HexX,
        SelectedUnit.GetComponent<UnitBasicScript>().HexY,
        SelectedUnit.GetComponent<UnitBasicScript>().HexZ];


    // TargetNode float to int conversion
    int targetVecXInt = (int)targetVec.x;
    int targetVecYInt = (int)targetVec.y;
    int targetVecZInt = (int)targetVec.z;
    Node targetNode = graph[
        targetVecXInt,
        targetVecYInt,
        targetVecZInt];

    // Debug.Log(targetVecXInt + "_" + targetVecYInt + "_" + targetVecZInt);


    dist[source] = 0;
    prev[source] = null;


    // Initialise everything to have infinity distance since no other 
information available
    // Some nodes might not eb erachable therefore infinity is reasonable
    foreach (Node v in graph)
    {
        if (v != source)
        {
            dist[v] = Mathf.Infinity;
            prev[v] = null;
        }

        unvisited.Add(v);
    }

    while (unvisited.Count > 0)
    {
        // u is unvisited node with shortest distance
        Node u = null;
        foreach (Node possibleU in unvisited)
        {
            if (u == null || dist[possibleU] < dist[u])
            {
                u = possibleU;
            }
        }

        unvisited.Remove(u);


        if (u == targetNode)
        {
            break;
        }


        foreach (Node v in u.neighbours)
        {
            float alt = dist[u] + u.Distanceto(v);
            if (alt < dist[v])
            {
                dist[v] = alt;
                prev[v] = u;
            }
        }
    }

    if (prev[targetNode] == null)
    {
        // No route to target
        return null;
    }

    List<Node> currentPath = new List<Node>();

    Node curr = targetNode;

    while (prev[curr] != null)
    {
        currentPath.Add(curr);
        curr = prev[curr];
    }

    currentPath.Reverse();
    return currentPath;
} // End of generate path function

public class Node
{

    public int x = 0;
    public int y = 0;
    public int z = 0;

    public List<Node> neighbours;

    public Node()
    {
        neighbours = new List<Node>();
    }

    public float Distanceto(Node n)
    {
        if (n == null)
        {
            Debug.Log("Error");
        }

        return Vector2.Distance(
            new Vector3(x, y, z),
            new Vector3(n.x, n.y, n.z));
    }
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEditor;
使用System.IO;
使用制度;
使用System.Linq;
公共类UnitMasterScript:MonoBehavior{
//邻域的私有整数=6;
邻域的私有整数=6;
私有游戏对象[]邻居;
节点[,]图;
public bool MapMakerDone=false;
无效更新()
{
如果(MapMakerDone==true)
{
//等待MapMaker将MapMakerDone更改为true,然后允许脚本的其余部分运行
GameObject Map=GameObject.Find(“MapMaker”);
int WidthVal=Map.GetComponent().WidthVal;
int HeightVal=Map.GetComponent().HeightVal;
int DepthVal=Map.GetComponent().DepthVal;
//节点生成代码图
//代码以查找此十六进制的每一侧对应的十六进制
//需要找到左、右、ul、ur、ll、lr的十六进制
//需要在相邻堆栈的顶部找到十六进制
//0:L1:R2:UL 3:UR4:LL 5:LR
图形=新节点[WidthVal、HeightVal、DepthVal];
对于(int x=0;x