示例#1
0
        public List <Node> Compute(int startX, int startY, int endX, int endY, Cell[][][] matrix, int time, bool prob)
        {
            try{
                Priority_Queue.IPriorityQueue <Node> heap2 = new Priority_Queue.HeapPriorityQueue <Node>(600);

                // Initialize our version of the matrix (can we skip this?)
                Node[][] newMatrix = new Node[matrix[0].Length][];
                for (int x = 0; x < matrix [0].Length; x++)
                {
                    newMatrix [x] = new Node[matrix [0] [x].Length];
                    for (int y = 0; y < matrix [0] [x].Length; y++)
                    {
                        newMatrix [x] [y]        = new Node();
                        newMatrix [x] [y].parent = null;
                        newMatrix [x] [y].cell   = matrix [0] [x] [y];
                        newMatrix [x] [y].x      = x;
                        newMatrix [x] [y].y      = y;
                    }
                }
                enemyPathProb(newMatrix);
                // Do the work for the first cell before firing the algorithm
                start         = newMatrix [startX] [startY];
                end           = newMatrix [endX] [endY];
                start.parent  = null;
                start.visited = true;


                foreach (Node n in getAdjacent(start, newMatrix))
                {
                    n.t      = time;
                    n.parent = start;
                    float fVal = f(n);
                    n.Priority = fVal;
                    heap2.Enqueue(n, fVal);
                }
                while (heap2.Count != 0)
                {
                    Node first = heap2.Dequeue();
                    if (first == end)
                    {
                        break;
                    }
                    first.visited = true;
                    double      temprt = 1;
                    List <Node> adjs   = getAdjacent(first, newMatrix);

                    foreach (Node m in adjs)
                    {
                        float currentG = (float)(g(first) + h(first, m, ref temprt));
                        if (m.visited)
                        {
                            if (g(m) > currentG)
                            {
                                m.parent = first;
                                m.t      = first.t + time;
                            }
                        }
                        else
                        {
                            if (!heap2.Contains(m))
                            {
                                m.parent   = first;
                                m.t        = first.t + time;
                                m.Priority = (float)temprt * f(m);
                                heap2.Enqueue(m, m.Priority);
                            }
                            else
                            {
                                float gVal = g(m);
                                if (gVal > currentG)
                                {
                                    m.parent   = first;
                                    m.t        = first.t + time;
                                    m.Priority = (float)temprt * f(m);
                                    heap2.UpdatePriority(m, m.Priority);
                                }
                            }
                        }
                    }
                }
                // Creates the result list
                Node        l      = end;
                List <Node> points = new List <Node> ();
                while (l != null)
                {
                    points.Add(l);
                    l = l.parent;
                }
                points.Reverse();

                // If we didn't find a path
                if (points.Count == 1)
                {
                    points.Clear();
                }
                return(points);
            }catch (System.Exception e) {
                Debug.Log(e.Message);
                Debug.Log(e.StackTrace);
                Debug.Log("ERROR 2");
                return(null);
            }
        }
示例#2
0
        public List <Node> Compute(int startX, int startY, int endX, int endY, Cell[][][] matrix, float playerSpeed)
        {
            this.speed = 1.0d / playerSpeed;
            try {
                Priority_Queue.IPriorityQueue <Node> heap2 = new Priority_Queue.HeapPriorityQueue <Node> (1000000);
                List <Node> closed = new List <Node> ();

                // Initialize our version of the matrix (can we skip this?)
                Node[][][] newMatrix = new Node[matrix.Length][][];
                for (int t = 0; t < matrix.Length; t++)
                {
                    newMatrix [t] = new Node[matrix [t].Length][];
                    for (int x = 0; x < matrix [t].Length; x++)
                    {
                        newMatrix [t] [x] = new Node[matrix [t] [x].Length];
                        for (int y = 0; y < matrix [t] [x].Length; y++)
                        {
                            newMatrix [t] [x] [y]        = new Node();
                            newMatrix [t] [x] [y].parent = null;
                            newMatrix [t] [x] [y].cell   = matrix [t] [x] [y];
                            newMatrix [t] [x] [y].x      = x;
                            newMatrix [t] [x] [y].y      = y;
                            newMatrix [t] [x] [y].t      = t;
                        }
                    }
                }
                // Do the work for the first cell before firing the algorithm
                start          = newMatrix [0] [startX] [startY];
                end            = newMatrix [0] [endX] [endY];
                start.parent   = null;
                start.visited  = true;
                start.accSpeed = speed - Math.Floor(speed);
                foreach (Node n in getAdjacent(start, newMatrix))
                {
                    n.parent = start;
                    float fVal = f(n);
                    n.Priority = fVal;
                    heap2.Enqueue(n, fVal);
                }
                while (heap2.Count != 0)
                {
                    Node first = heap2.Dequeue();
                    if (first.x == end.x && first.y == end.y)
                    {
                        end = newMatrix [first.t] [end.x] [end.y];
                        break;
                    }
                    first.visited = true;
                    foreach (Node m in getAdjacent(first, newMatrix))
                    {
                        float currentG = g(first) + h(m, first);
                        float gVal     = g(m);
                        if (m.visited)
                        {
                            if (gVal > currentG)
                            {
                                m.parent = first;
                                acc(m);
                            }
                        }
                        else
                        {
                            if (!heap2.Contains(m))
                            {
                                m.parent   = first;
                                m.Priority = f(m);
                                heap2.Enqueue(m, m.Priority);
                                acc(m);
                            }
                            else
                            {
                                if (gVal > currentG)
                                {
                                    m.parent   = first;
                                    m.Priority = f(m);
                                    heap2.UpdatePriority(m, m.Priority);
                                    acc(m);
                                }
                            }
                        }
                    }
                }
                // Creates the result list
                Node        e      = end;
                List <Node> points = new List <Node> ();
                while (e != null)
                {
                    points.Add(e);
                    e = e.parent;
                }
                points.Reverse();

                // If we didn't find a path
                if (points.Count == 1)
                {
                    points.Clear();
                }
                return(points);
            } catch (System.Exception e) {
                Debug.Log(e.Message);
                Debug.Log(e.StackTrace);
                Debug.Log("ERROR 2");
                return(null);
            }
        }
示例#3
0
        public List<Node> Compute(int startX, int startY, int endX, int endY, Cell[][][] matrix, float playerSpeed)
        {
            this.speed = 1.0d / playerSpeed;
            try {
                Priority_Queue.IPriorityQueue<Node> heap2 = new Priority_Queue.HeapPriorityQueue<Node> (1000000);
                List<Node> closed = new List<Node> ();

                // Initialize our version of the matrix (can we skip this?)
                Node[][][] newMatrix = new Node[matrix.Length][][];
                for (int t=0; t<matrix.Length; t++) {
                    newMatrix [t] = new Node[matrix [t].Length][];
                    for (int x = 0; x < matrix [t].Length; x++) {
                        newMatrix [t] [x] = new Node[matrix [t] [x].Length];
                        for (int y = 0; y < matrix [t] [x].Length; y++) {
                            newMatrix [t] [x] [y] = new Node ();
                            newMatrix [t] [x] [y].parent = null;
                            newMatrix [t] [x] [y].cell = matrix [t] [x] [y];
                            newMatrix [t] [x] [y].x = x;
                            newMatrix [t] [x] [y].y = y;
                            newMatrix [t] [x] [y].t = t;
                        }
                    }
                }
                // Do the work for the first cell before firing the algorithm
                start = newMatrix [0] [startX] [startY];
                end = newMatrix [0] [endX] [endY];
                start.parent = null;
                start.visited = true;
                start.accSpeed = speed - Math.Floor(speed);
                foreach (Node n in getAdjacent(start, newMatrix)) {
                    n.parent = start;
                    float fVal = f (n);
                    n.Priority = fVal;
                    heap2.Enqueue (n, fVal);
                }
                while (heap2.Count != 0) {
                    Node first = heap2.Dequeue ();
                    if (first.x == end.x && first.y == end.y) {
                        end = newMatrix [first.t] [end.x] [end.y];
                        break;
                    }
                    first.visited = true;
                    foreach (Node m in getAdjacent(first, newMatrix)) {
                        float currentG = g (first) + h (m, first);
                        float gVal = g (m);
                        if (m.visited) {
                            if (gVal > currentG) {
                                m.parent = first;
                                acc(m);
                            }
                        } else {
                            if (!heap2.Contains (m)) {
                                m.parent = first;
                                m.Priority = f (m);
                                heap2.Enqueue (m, m.Priority);
                                acc(m);
                            } else {
                                if (gVal > currentG) {
                                    m.parent = first;
                                    m.Priority = f (m);
                                    heap2.UpdatePriority (m, m.Priority);
                                    acc(m);
                                }
                            }
                        }
                    }
                }
                // Creates the result list
                Node e = end;
                List<Node> points = new List<Node> ();
                while (e != null) {
                    points.Add (e);
                    e = e.parent;
                }
                points.Reverse ();

                // If we didn't find a path
                if (points.Count == 1)
                    points.Clear ();
                return points;
            } catch (System.Exception e) {
                Debug.Log (e.Message);
                Debug.Log (e.StackTrace);
                Debug.Log ("ERROR 2");
                return null;
            }
        }
示例#4
0
        public List<Node> Compute(int startX, int startY, int endX, int endY, Cell[][][] matrix, int time, bool prob)
        {
            try{
            Priority_Queue.IPriorityQueue<Node> heap2 = new Priority_Queue.HeapPriorityQueue<Node>(600);

            // Initialize our version of the matrix (can we skip this?)
            Node[][] newMatrix = new Node[matrix[0].Length][];
            for (int x = 0; x < matrix [0].Length; x++) {
                newMatrix [x] = new Node[matrix [0] [x].Length];
                for (int y = 0; y < matrix [0] [x].Length; y++) {
                    newMatrix [x] [y] = new Node ();
                    newMatrix [x] [y].parent = null;
                    newMatrix [x] [y].cell = matrix [0] [x] [y];
                    newMatrix [x] [y].x = x;
                    newMatrix [x] [y].y = y;
                }
            }
            enemyPathProb(newMatrix);
            // Do the work for the first cell before firing the algorithm
            start = newMatrix [startX] [startY];
            end = newMatrix [endX] [endY];
            start.parent=null;
            start.visited=true;

            foreach (Node n in getAdjacent(start, newMatrix)) {
                n.t=time;
                n.parent = start;
                float fVal = f (n);
                n.Priority = fVal;
                heap2.Enqueue(n,fVal);
            }
            while(heap2.Count != 0){

                Node first = heap2.Dequeue();
                if(first == end)
                    break;
                first.visited=true;
                double temprt = 1;
                List<Node> adjs = getAdjacent(first,newMatrix);

                foreach(Node m in adjs){
                    float currentG = (float)(g (first) + h (first,m, ref temprt));
                    if(m.visited){
                            if(g (m)>currentG){
                                m.parent=first;
                                m.t = first.t+time;
                            }
                        }
                    else{
                        if( !heap2.Contains(m)){
                            m.parent = first;
                            m.t= first.t +time;
                            m.Priority = (float)temprt*f(m);
                            heap2.Enqueue(m, m.Priority);
                        }
                        else
                        {
                            float gVal = g (m);
                            if(gVal>currentG){
                                m.parent= first;
                                m.t = first.t+time;
                                m.Priority= (float)temprt *f (m);
                                heap2.UpdatePriority(m, m.Priority);
                            }
                        }
                    }
                }
            }
                // Creates the result list
                Node l = end;
                List<Node> points = new List<Node> ();
                while (l != null) {
                    points.Add (l);
                    l = l.parent;
                }
                points.Reverse ();

                // If we didn't find a path
                if (points.Count == 1)
                points.Clear ();
                return points;
            }catch(System.Exception e){
                                Debug.Log (e.Message);
                                Debug.Log (e.StackTrace);
                                Debug.Log ("ERROR 2");
                                return null;
                        }
        }