Ejemplo n.º 1
0
        public List <Vector> Route()
        {
            List <RoutingPoint> possibleNewPoints;
            RoutingPoint        reachedDestinationWithLeastCost;
            float destinationCost;

            while (true)
            {
                possibleNewPoints = new List <RoutingPoint>();

                foreach (RoutingPoint openPoint in openList)
                {
                    reachedDestinationWithLeastCost = null;
                    destinationCost = float.MaxValue;

                    foreach (Vector direction in directionList)
                    {
                        Vector possibleNewPoint = openPoint.Point + direction;

                        if (CheckClosedList(possibleNewPoint) && map.WayFree(possibleNewPoint, minimumDistance))
                        {
                            if (destinationPoint.Distance(possibleNewPoint) < minimumDistance)
                            {
                                RoutingPoint currentParent = new RoutingPoint(possibleNewPoint, openPoint, ownShip, direction.Length);

                                if (reachedDestinationWithLeastCost == null || destinationCost > currentParent.Cost)
                                {
                                    reachedDestinationWithLeastCost = currentParent;
                                    destinationCost = currentParent.Cost;
                                }
                            }

                            possibleNewPoints.Add(new RoutingPoint(possibleNewPoint, openPoint, ownShip, direction.Length));
                        }
                    }

                    if (reachedDestinationWithLeastCost != null)
                    {
                        destinationRouter = new List <Vector>();

                        destinationRouter.Add(destinationPoint.Point);
                        destinationRouter.Add(reachedDestinationWithLeastCost.Point);

                        reachedDestinationWithLeastCost = reachedDestinationWithLeastCost.GoBack(ref destinationRouter);

                        while (reachedDestinationWithLeastCost != null)
                        {
                            reachedDestinationWithLeastCost = reachedDestinationWithLeastCost.GoBack(ref destinationRouter);
                        }

                        destinationRouter.Reverse();

                        return(destinationRouter);
                    }

                    if (!closedList.Contains(openPoint))
                    {
                        closedList.Add(openPoint);
                    }
                }

                if (possibleNewPoints.Count == 0)
                {
                    throw new Exception();
                }

                foreach (RoutingPoint possibleNewPoint in possibleNewPoints)
                {
                    openList.Add(possibleNewPoint);
                }

                openList.Sort();
            }
        }