示例#1
0
        public Routing(Vector destination, CosmicMap map, CosmicOwnership routingShip)
        {
            directionList = new List <Vector>();
            closedList    = new List <RoutingPoint>();
            openList      = new List <RoutingPoint>();
            this.map      = map;


            ownShip          = routingShip;
            unitLength       = routingShip.Radius * 4;
            ownShip          = routingShip;
            minimumDistance  = routingShip.Radius * 4;
            destinationPoint = new RoutingPoint(destination, null, routingShip, 0);

            Vector rightVector = new Vector(1, 0);

            rightVector.Length = unitLength;
            directionList.Add(rightVector);

            Vector righttopVector = new Vector(1, 1);

            righttopVector.Length = unitLength * (float)Math.Sqrt(2);
            directionList.Add(righttopVector);

            Vector rightdownVector = new Vector(1, -1);

            rightdownVector.Length = unitLength * (float)Math.Sqrt(2);
            directionList.Add(rightdownVector);

            Vector leftVector = new Vector(-1, 0);

            leftVector.Length = unitLength;
            directionList.Add(leftVector);

            Vector lefttopVector = new Vector(-1, 1);

            lefttopVector.Length = unitLength * (float)Math.Sqrt(2);
            directionList.Add(lefttopVector);

            Vector leftdownVector = new Vector(-1, -1);

            leftdownVector.Length = unitLength * (float)Math.Sqrt(2);
            directionList.Add(leftdownVector);

            Vector topVector = new Vector(0, 1);

            topVector.Length = unitLength;
            directionList.Add(topVector);

            Vector downVector = new Vector(0, -1);

            downVector.Length = unitLength;
            directionList.Add(downVector);

            openList.Add(new RoutingPoint(routingShip.Position, null, routingShip, 0));
            openList.Sort();
        }
        public int CompareTo(object obj)
        {
            RoutingPoint otherRoutingPoint = obj as RoutingPoint;

            if (otherRoutingPoint.Cost > Cost)
            {
                return(-1);
            }

            return(1);
        }
        public RoutingPoint(Vector point, RoutingPoint lastPoint, CosmicOwnership ownShip, float cost)
        {
            if (lastPoint != null)
            {
                Cost = cost + lastPoint.Cost;
            }

            Point     = point;
            LastPoint = lastPoint;
            ship      = ownShip;
        }
示例#4
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();
            }
        }