Esempio n. 1
0
        //compiles the path
        public List<Coord> GetPath(Coord startPos, Coord endPos, ref Map m) {
            //round down coords to lock to tiles
            int startX = (int)(startPos.X);
            int startY = (int)(startPos.Y);
            int endX = (int)(endPos.X);
            int endY = (int)(endPos.Y);
            //create nodemap
            Node[,] nodeMap = new Node[m.ObjectMap.GetLength(0), m.ObjectMap.GetLength(1)];
            for (int x = 0; x < nodeMap.GetLength(0); x++) {
                for (int y = 0; y < nodeMap.GetLength(1); y++) {
                    if (m.ObjectMap[x, y] != null) {
                        nodeMap[x, y] = new Node(new Coord(x, y), m.ObjectMap[x, y].collision, new Coord(endX, endY), null);
                    } else {
                        nodeMap[x, y] = new Node(new Coord(x, y), false, new Coord(endX, endY), null);
                    }
                }
            }

            bool success = Search(nodeMap[startX, startY], new Coord(startX, startY), new Coord(endX, endY), ref nodeMap);
            if (success) {
                List<Coord> path = new List<Coord>();
                Node n = nodeMap[endX, endY];
                while (n.parent != null) {
                    path.Add(new Coord(n.location.X + (random.Next(-3, 3) * .1), n.location.Y + (random.Next(-3, 3) * .1)));
                    n = n.parent;
                }
                path.Reverse();
                return path;
            } else {
                //return empty list if search is not successful
                return new List<Coord>();
            }
        }
Esempio n. 2
0
        //Checks all adjacent tiles and returns list of valid tiles sorted by estimated path length
        public List<Node> checkAdjacent(Node curNode, Coord end, ref Node[,] nodeMap) {
            int X = (int)curNode.location.X;
            int Y = (int)curNode.location.Y;

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

            //check for valid adjacent tiles and add them to list
            for (int i = X - 1; i < X + 2; i++) {
                for (int j = Y - 1; j < Y + 2; j++) {
                    //stay within boundaries of map and check if the tile is walkable
                    if (i > 0 && j > 0 && j < nodeMap.GetLength(1) && i < nodeMap.GetLength(0) && nodeMap[i,j].collidable == false) {
                        Node n = nodeMap[i, j];
                        if (n.state == Node.NodeState.Closed) {
                            //do nothing if the node is closed
                            continue;
                        }
                        // Already-open nodes are added to the list only if you find a shorter way to get there
                        if (n.state == Node.NodeState.Open) {
                            //if the currently stored path to that node is longer, override with shorter path
                            n.UpdateDist(n.location, end);
                            if (n.GetNewPathDist(curNode) < n.pathLength) {
                                n.parent = curNode;
                                n.UpdateDist(n.location, end);
                                validNodes.Add(n);
                            }
                        } 
                        //untested node case
                        else {
                            n.parent = curNode;
                            n.state = Node.NodeState.Open;
                            n.UpdateDist(n.location, end);
                            validNodes.Add(n);
                        }
                        //update node on map
                        nodeMap[i,j] = n;
                    }
                }
            }
            //sort the nodes by the least distance first
            validNodes = validNodes.OrderBy(o => o.estLength).ToList();
            return validNodes;
        }