Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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;
            }
        }
Ejemplo n.º 3
0
        public List <Node> Compute(int startX, int startY, int endX, int endY, Cell[][] matrix, bool improve)
        {
            List <Node> opened = new List <Node> ();

            Priority_Queue.IPriorityQueue <Node> heap2 = new Priority_Queue.HeapPriorityQueue <Node> (600);

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

            // Initialize our version of the matrix (can we skip this?)
            Node[][] newMatrix = new Node[matrix.Length][];
            for (int x = 0; x < matrix.Length; x++)
            {
                newMatrix [x] = new Node[matrix [x].Length];
                for (int y = 0; y < matrix[x].Length; y++)
                {
                    newMatrix [x] [y]        = new Node();
                    newMatrix [x] [y].parent = null;
                    newMatrix [x] [y].cell   = matrix [x] [y];
                    newMatrix [x] [y].x      = x;
                    newMatrix [x] [y].y      = y;
                }
            }

            // Do the work for the first cell before firing the algorithm
            start = newMatrix [startX] [startY];
            end   = newMatrix [endX] [endY];

            closed.Add(start);

            foreach (Node c in getAdjacent(start, newMatrix))
            {
                c.parent = start;
                if (improve)
                {
                    heap2.Enqueue(c, f(c));
                }
                else
                {
                    opened.Add(c);
                }
            }

            while ((improve && heap2.Count > 0) || (!improve && opened.Count > 0))
            {
                // Pick the closest to the goal
                Node minF = null;
                if (improve)
                {
                    minF = heap2.Dequeue();
                }
                else
                {
                    for (int i = 0; i < opened.Count; i++)
                    {
                        if (minF == null || f(minF) > f(opened [i]))
                        {
                            minF = opened [i];
                        }
                    }
                    opened.Remove(minF);
                }

                closed.Add(minF);

                // Found it
                if (minF == end)
                {
                    break;
                }

                foreach (Node adj in getAdjacent(minF, newMatrix))
                {
                    float soFar = g(minF) + h(adj, minF);

                    // Create the links between cells (picks the best path)
                    if (closed.Contains(adj))
                    {
                        if (g(adj) > soFar)
                        {
                            adj.parent = minF;
                        }
                    }
                    else
                    {
                        if ((improve && heap2.Contains(adj)) || (!improve && opened.Contains(adj)))
                        {
                            if (g(adj) > soFar)
                            {
                                adj.parent = minF;
                            }
                        }
                        else
                        {
                            adj.parent = minF;
                            if (improve)
                            {
                                heap2.Enqueue(adj, f(adj));
                            }
                            else
                            {
                                opened.Add(adj);
                            }
                        }
                    }
                }
            }

            // Creates the result list
            Node        n      = end;
            List <Node> points = new List <Node> ();

            while (n != null)
            {
                points.Add(n);
                n = n.parent;
            }
            points.Reverse();

            // If we didn't find a path
            if (points.Count == 1)
            {
                points.Clear();
            }
            return(points);
        }
Ejemplo n.º 4
0
        public List<Node> Compute(int startX, int startY, int endX, int endY, Cell[][] matrix, bool improve)
        {
            List<Node> opened = new List<Node> ();
            Priority_Queue.IPriorityQueue<Node> heap2 = new Priority_Queue.HeapPriorityQueue<Node> (600);

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

            // Initialize our version of the matrix (can we skip this?)
            Node[][] newMatrix = new Node[matrix.Length][];
            for (int x = 0; x < matrix.Length; x++) {
                newMatrix [x] = new Node[matrix [x].Length];
                for (int y = 0; y < matrix[x].Length; y++) {
                    newMatrix [x] [y] = new Node ();
                    newMatrix [x] [y].parent = null;
                    newMatrix [x] [y].cell = matrix [x] [y];
                    newMatrix [x] [y].x = x;
                    newMatrix [x] [y].y = y;
                }
            }

            // Do the work for the first cell before firing the algorithm
            start = newMatrix [startX] [startY];
            end = newMatrix [endX] [endY];

            closed.Add (start);

            foreach (Node c in getAdjacent(start, newMatrix)) {
                c.parent = start;
                if (improve)
                    heap2.Enqueue (c, f (c));
                else
                    opened.Add (c);
            }

            while ((improve && heap2.Count > 0) || (!improve && opened.Count > 0)) {

                // Pick the closest to the goal
                Node minF = null;
                if (improve) {
                    minF = heap2.Dequeue ();
                } else {
                    for (int i = 0; i < opened.Count; i++) {
                        if (minF == null || f (minF) > f (opened [i]))
                            minF = opened [i];
                    }
                    opened.Remove (minF);
                }

                closed.Add (minF);

                // Found it
                if (minF == end)
                    break;

                foreach (Node adj in getAdjacent(minF, newMatrix)) {

                    float soFar = g (minF) + h (adj, minF);

                    // Create the links between cells (picks the best path)
                    if (closed.Contains (adj)) {
                        if (g (adj) > soFar) {
                            adj.parent = minF;
                        }
                    } else {
                        if ((improve && heap2.Contains (adj)) || (!improve && opened.Contains (adj))) {
                            if (g (adj) > soFar) {
                                adj.parent = minF;
                            }
                        } else {
                            adj.parent = minF;
                            if (improve)
                                heap2.Enqueue (adj, f (adj));
                            else
                                opened.Add (adj);
                        }
                    }
                }
            }

            // Creates the result list
            Node n = end;
            List<Node> points = new List<Node> ();
            while (n != null) {
                points.Add (n);
                n = n.parent;
            }
            points.Reverse ();

            // If we didn't find a path
            if (points.Count == 1)
                points.Clear ();
            return points;
        }
Ejemplo n.º 5
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);
            }
        }
Ejemplo n.º 6
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;
                        }
        }