示例#1
0
 static public PathWeights WakeUp()
 {
     if (_instance == null)
     {
         _instance = new PathWeights();
     }
     return(_instance);
 }
示例#2
0
    public void GetPath(PathNode start, PathNode end, PathFollowerTypes type)
    {
        var openSet = new List <PathNode>();

        openSet.Add(start);
        var closedSet = new List <PathNode>();
        var cameFrom  = new Dictionary <PathNode, PathNode>();

        var globalScore = new Dictionary <PathNode, float>();

        globalScore.Add(start, 0);
        var fScore = new Dictionary <PathNode, float>();

        fScore.Add(start, globalScore[start] + Heuristics.ManhattanDistance(start.GridPosition, end.GridPosition));

        var iWeights = PathWeights.GetWeights(type);

        float[] weights = new float[iWeights.Count];
        for (int w = 0; w < weights.Length; w++)
        {
            weights[w] = (float)iWeights[(PathType)w] * gridNodeSize;
        }

        while (openSet.Count > 0)
        {
            float    lowestF = -1;
            PathNode current = null;
            foreach (PathNode node in openSet)
            {
                var score = fScore[node];
                if (score < lowestF || lowestF == -1)
                {
                    lowestF = score;
                    current = node;
                }
            }
            if (current == end)
            {
                Debug.Log("Path found! With a cost of " + fScore[current]);
                RecreatePath(current, cameFrom);
                start.SetColor(Color.green);
                end.SetColor(Color.red);
                return;
            }

            openSet.Remove(current);
            closedSet.Add(current);
            var   connections    = current.GetConnections();
            float tentativeScore = 0;
            foreach (PathNode node in connections)
            {
                if (node == null || !node.Walkable(type))
                {
                    continue;
                }
                tentativeScore = globalScore[current] + weights[(int)node.Type];                 //Heuristics.ManhattanDistance(current.GridPosition, node.GridPosition);
                if (closedSet.Contains(node) && tentativeScore >= globalScore[node])
                {
                    continue;
                }
                else
                {
                    if (cameFrom.ContainsKey(node))
                    {
                        cameFrom[node] = current;
                    }
                    else
                    {
                        cameFrom.Add(node, current);
                    }
                    globalScore[node] = tentativeScore;
                    fScore[node]      = tentativeScore + Heuristics.ManhattanDistance(node.GridPosition, end.GridPosition);
                    if (!openSet.Contains(node))
                    {
                        openSet.Add(node);
                    }
                }
            }
        }
        Debug.Log("Failure");
    }
示例#3
0
 private void Awake()
 {
     PathWeights.WakeUp();
 }
示例#4
0
    public void GetPath(PathNode start, PathNode end, PathFollowerTypes type, Action <PathData> callback)
    {
        Debug.Log("Start = " + start.name + " End = " + end.name);
        var endPos    = end.GridPosition;
        var closedSet = new List <PathNode>();
        var openSet   = new List <PathNode>();

        openSet.Add(start);
        var          navigatedSet = new List <PathNode>();
        var          pathWeight   = new List <int>();
        List <float> score        = new List <float>();

        float estimatedScore  = 0;
        var   openConnections = new PathNode[0];
        var   iWeights        = PathWeights.GetWeights(type);

        float[] weights = new float[iWeights.Count];
        for (int i = 0; i < weights.Length; i++)
        {
            weights[i] = iWeights[(PathType)i] * gridNodeSize;
        }
        bool endIsFound = false;

        for (int i = 0; i < openSet.Count; i++)
        {
            if (openSet[i] == end)
            {
                endIsFound = true;
                break;
            }
            Debug.Log("Checking connections of: " + openSet[i].name, openSet[i].gameObject);
            openConnections = GetConnections(openSet[i]);
            int    index                   = 0;
            float  distance                = 100000;
            bool   foundNewNode            = false;
            bool[] newConnections          = new bool[4];
            bool   hasConnectionsAvailable = false;
            for (int c = 0; c < 4; c++)
            {
                if (navigatedSet.Contains(openConnections[c]))
                {
                    continue;
                }
                newConnections[c]       = true;
                hasConnectionsAvailable = true;
            }
            if (!hasConnectionsAvailable)
            {
                continue;
            }
            for (int c = 0; c < 4; c++)
            {
                if (openConnections[c] == null || !newConnections[c])
                {
                    continue;
                }
                if (openConnections[c] == end)
                {
                    distance     = 0;
                    index        = c;
                    foundNewNode = true;
                    break;
                }

                //Debug.Log((openConnections[c] == null) +" "+c, openSet[i].gameObject);
                estimatedScore = Heuristics.ManhattanDistance(openConnections[c].GridPosition, endPos) + weights[(int)openConnections[c].Type];
                if (estimatedScore < distance)
                {
                    distance     = estimatedScore;
                    index        = c;
                    foundNewNode = true;
                }
            }
            if (!foundNewNode || !openConnections[index].Walkable(type))
            {
                //openSet.RemoveAt(i);
                i = -1;
                openSet.Clear();
                openSet.Add(start);
                if (openSet.Count == 0)
                {
                    Debug.Log("No path found");
                    return;
                }
            }
            else
            {
                Debug.Log("Next = " + openConnections[index].name);
                openSet.Add(openConnections[index]);
                navigatedSet.Add(openConnections[index]);
            }
        }
        if (!endIsFound)
        {
            Debug.Log("Impossible to create a path!");
        }
        foreach (PathNode p in openSet)
        {
            p.SetColor(Color.cyan);
        }
    }
示例#5
0
        public void CalcShortestPath(WeightedGraph graph, string startVertex, string endVertex)
        {
            verticesDict.Clear();
            predecessorDict.Clear();
            foreach (string vertix in graph.Vertices)
            {
                if (!vertix.Equals(startVertex))
                {
                    verticesDict.Add(vertix, int.MaxValue);
                }
            }
            verticesDict.Add(startVertex, 0);
            predecessorDict.Add(startVertex, null);

            List <string> queue = new List <string>();

            queue.AddRange(graph.Vertices);

            while (queue.Count != 0)
            {
                string u       = null;
                int    minimum = int.MaxValue;
                foreach (string v in queue)
                {
                    if (verticesDict[v] < minimum)
                    {
                        minimum = verticesDict[v];
                        u       = v;
                    }
                }
                if (u != null)
                {
                    queue.Remove(u);

                    foreach (string v in graph.adjacentVertices[u])
                    {
                        if ((graph.Connected(u, v)) && (verticesDict[u] + graph.GetWeight(u, v) < verticesDict[v]))
                        {
                            verticesDict[v]    = verticesDict[u] + graph.GetWeight(u, v);
                            predecessorDict[v] = u;
                        }
                    }
                }
                else
                {
                    break;
                }
            }


            path.Clear();
            pathWeights.Clear();

            string v2 = endVertex;

            if (predecessorDict.ContainsKey(v2))
            {
                while (predecessorDict[v2] != null)
                {
                    path.Add(predecessorDict[v2]);
                    v2 = predecessorDict[v2];
                }
                path.Reverse();
                for (int i = 0; i < path.Count; i++)
                {
                    string v3 = path[i];
                    int    w  = i != path.Count - 1 ? graph.GetWeight(v3, path[i + 1]) : graph.GetWeight(v3, endVertex);
                    pathWeights.Add(w);
                }
            }
            else
            {
                path.Add(null);
                PathWeights.Add(int.MaxValue);
            }
        }