Пример #1
0
    //dijkstra method that allow finding the shortest path depending on the cost of the blocks*******************************************************
    Stack <Vector3> Dijkstra(Vector3 start, Vector3 end, VoxelChunk vc)
    {
        Stack <Vector3>               waypoints     = new Stack <Vector3> ();
        Dictionary <Vector3, int>     distance      = new Dictionary <Vector3, int> ();
        Dictionary <Vector3, Vector3> visitedParent = new Dictionary <Vector3, Vector3> ();
        bool            found   = false;
        Stack <Vector3> q       = new Stack <Vector3> ();
        List <Vector3>  l       = new List <Vector3>();
        int             newDist = 0;
        int             length  = 0;
        int             tempHolder;

        //Vector3 tempDKHolder;
        start += new Vector3(0, -1, 0);
        end   += new Vector3(0, -1, 0);
        Vector3 current = start;

        foreach (Vector3 n in vc.GetTerrainList())
        {
            distance[n] = int.MaxValue;

            l.Add(n);
            //Debug.Log (n);
        }
        distance [start] = 0;
        while (l.Count > 0 && !found)
        {
            //set tempHolder to inf*
            tempHolder = int.MaxValue;

            //loop through each of the items in the list to find the smallest value
            foreach (Vector3 v in l)
            {
                //if the item is in the distance dictionary
                if (distance.ContainsKey(v))
                {
                    //the tempholder will be always greater than the first value because its inf
                    //but after it will equal to the first value
                    //which will campare to each value of the list to find the smallest value again
                    if (tempHolder > distance[v])
                    {
                        tempHolder = distance[v];
                        //once it found the smaller value, set the current equal to that vector
                        current = v;
                    }
                }
            }
            //once the smallest value confirmed, remove the item from the list
            l.Remove(current);



            if (current != end)
            {
                //our adjacent nodes are x+1, x-1, z+1 and z-1
                List <Vector3> neighbourList = new List <Vector3>();
                neighbourList.Add(current + new Vector3(1, 0, 0));             //x+1
                neighbourList.Add(current + new Vector3(-1, 0, 0));            //x-1
                neighbourList.Add(current + new Vector3(0, 0, 1));             //z+1
                neighbourList.Add(current + new Vector3(0, 0, -1));            //z-1

                foreach (Vector3 n in neighbourList)
                {
                    if (l.Contains(n))
                    {
                        //check if n is within range
                        if ((n.x >= 0 && n.x < vc.GetChunkSize()) &&
                            n.z >= 0 && n.z < vc.GetChunkSize())
                        {
                            //check if we can traverse over this
                            if (vc.IsTraversableForDijkstra(n))
                            {
                                if (vc.isStone(n))
                                {
                                    length = 1;
                                }
                                else
                                {
                                    length = 3;
                                }

                                newDist = distance[current] + length;

                                if (newDist < distance[n] || !distance.ContainsKey(n))
                                {
//									//check if node is alread processed
//									if(!visitedParent.ContainsKey(n))
//									{
                                    distance[n]      = newDist;
                                    visitedParent[n] = current;

//									}
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                found = true;
            }
        }
        if (found)
        {
            while (current != start)
            {
                //put the current location into the waypoint
                waypoints.Push(current + offset + new Vector3(0, 1, 0));
                //find the parent of the current and replace the current location
                current = visitedParent[current];
            }
            waypoints.Push(start + offset + new Vector3(0, 1, 0));
        }
        //Debug.Log (waypoints.Count);
        return(waypoints);
    }