Esempio n. 1
0
        public PathFinder(PathFindingParameters parameters)
        {
            this.parameters = parameters;
              InitNodes(parameters.Map);

              this.startNode = this.nodes[parameters.StartLocation.X, parameters.StartLocation.Y];
              this.destinationNode = this.nodes[parameters.EndLocation.X, parameters.EndLocation.Y];

              this.startNode.State = PathNodeState.Possible;
        }
Esempio n. 2
0
        private bool SearchPath(PathNode currentNode)
        {
            currentNode.State = PathNodeState.Included;
              List<PathNode> nextNodes = GetWalkableNodes(currentNode);

              nextNodes.Sort((node1, node2) => node1.CostTotal.CompareTo(node2.CostTotal));

              foreach (PathNode nextNode in nextNodes)
              {
            if (nextNode.Location == this.destinationNode.Location)
            {
              return true;
            }
            else
            {
              if (SearchPath(nextNode))
            return true;
            }
              }

              // no path found
              return false;
        }
Esempio n. 3
0
        private List<PathNode> GetWalkableNodes(PathNode currentNode)
        {
            List<PathNode> walkables = new List<PathNode>();
              IEnumerable<Point> nextLocations;

              if (currentNode.Location.Y == 1)
              {
            nextLocations = new Point[] { new Point(destinationNode.Location.X, destinationNode.Location.Y) };
              }
              else
              {
            nextLocations = GetAdjacentLocations(currentNode.Location);
              }

              foreach (Point location in nextLocations)
              {
            int x = location.X;
            int y = location.Y;

            if (x < 0 || x >= this.width || y < 0 || y >= this.heigth)
              continue;

            PathNode node = this.nodes[x, y];

            // check if tower is not walkable or already in the path
            if (!node.Walkable || node.State == PathNodeState.Included)
              continue;

            // Check if path goes between 2 towers
            if (x != currentNode.Location.X && y != currentNode.Location.Y && y != 0)
            {
              if (!this.nodes[x, currentNode.Location.Y].Walkable || !this.nodes[currentNode.Location.X, y].Walkable)
            continue;
            }

            if (node.State == PathNodeState.Possible)
            {
              float cost = PathNode.GetCost(node.Location, node.ParentNode.Location);
              float tmp = currentNode.CostFromStart + cost;

              // Nodes are only added to the list if their cost is lower going via this route
              if (tmp < node.CostFromStart)
              {
            node.ParentNode = currentNode;
            walkables.Add(node);
              }
            }
            else
            {
              // Untested node -> set parent and consider it for the path
              node.ParentNode = currentNode;
              node.State = PathNodeState.Possible;
              walkables.Add(node);
            }
              }

              return walkables;
        }