Пример #1
0
        /// <summary>
        /// Returns an array of positions that are able to be reached from a certain position given a move budget
        /// </summary>
        /// <param name="level">Level layout to query</param>
        /// <param name="start">Starting position</param>
        /// <param name="budget">How many movements it can make (-1 is infinite)</param>
        /// <param name="straight">Whether the positions should be in direct view</param>
        /// <returns>An unordered array of positions</returns>
        public static IntVec[] getPossiblePositionsFrom(Level level, IntVec start, int budget = -1, CharacterTargeting targetCharacters = CharacterTargeting.TREAT_AS_SOLID, bool straight = false)
        {
            List <IntVec> positions = new List <IntVec>();

            int[,] used = level.getIntSolid(targetCharacters != CharacterTargeting.PASS_THROUGH);
            //used[start.X, start.Y] = int.MaxValue - 1;

            possiblePositionsFromStep(level, positions, used, start, start, budget, targetCharacters, straight, true);

            return(positions.ToArray <IntVec>());
        }
Пример #2
0
        public static SortedSet <__AStarNode> getPathDrawnBetween(Level level, IntVec from, IntVec to, int count)
        {
            int actions = 0;


            int[,] solid = level.getIntSolid();
            SortedSet <__AStarNode> nodes = new SortedSet <__AStarNode>(new __AStarNode.__AStarNodeComparer());

            __AStarNode recentNode = new __AStarNode(from, 0, 10 * calculateHeuristic(from, to));
            __AStarNode bestNode   = recentNode;

            nodes.Add(recentNode);

            while (recentNode != null && !recentNode.position.Equals(to) && !recentNode.expanded && (actions < count))
            {
                recentNode = nodes.Min;

                if (recentNode != null)
                {
                    //recentNode.expanded = true;
                    Direction[] closest = closestDirections(to - recentNode.position);

                    if (recentNode.heuristic.CompareTo(bestNode.heuristic) <= 0)
                    {
                        //Console.Write("test");
                        bestNode = recentNode;
                    }

                    for (int i = 0; i < closest.Length; i++)//foreach (Direction dir in Direction.Values)
                    {
                        Direction dir = closest[i];

                        IntVec newLocation = recentNode.position + dir;

                        if (solid[newLocation.X, newLocation.Y] < -(recentNode.actualCost + 10) && (actions++ < count))
                        {
                            solid[newLocation.X, newLocation.Y] = -(recentNode.actualCost + 10);
                            nodes.Add(new __AStarNode(newLocation, recentNode.actualCost + 10, 10 * calculateHeuristic(newLocation, to), i, recentNode, dir));
                        }
                    }



                    if (actions++ < count)
                    {
                        nodes.Remove(recentNode);
                    }
                }
            }

            return(nodes);
        }
Пример #3
0
 /// <summary>
 /// Returns an ordered array of directions needed for moves between two positions
 /// </summary>
 /// <param name="level">Level layout to query</param>
 /// <param name="from">Starting position</param>
 /// <param name="to">Final position</param>
 /// <returns>The moves needed to make the journey, null if not possible</returns>
 public static Direction[] getPathBetweenOld(Level level, IntVec from, IntVec to)
 {
     bestPath = null;
     return(pathBetweenStep(level.getIntSolid(), from, to, 1, new List <Direction>()));
 }
Пример #4
0
        public static Direction[] getPathBetween(Level level, IntVec from, IntVec to, ref bool isPossible)
        {
            int[,] solid = level.getIntSolid();
            SortedSet <__AStarNode> nodes = new SortedSet <__AStarNode>(new __AStarNode.__AStarNodeComparer());

            __AStarNode recentNode = new __AStarNode(from, 0, 10 * calculateHeuristic(from, to));
            __AStarNode bestNode   = recentNode;

            nodes.Add(recentNode);

            while (recentNode != null && !recentNode.position.Equals(to) && !recentNode.expanded)
            {
                recentNode = nodes.Min;

                if (recentNode != null)
                {
                    //recentNode.expanded = true;
                    Direction[] closest = closestDirections(to - recentNode.position);

                    if (recentNode.heuristic.CompareTo(bestNode.heuristic) <= 0)
                    {
                        //Console.Write("test");
                        bestNode = recentNode;
                    }

                    for (int i = 0; i < closest.Length; i++) //foreach (Direction dir in Direction.Values)
                    {
                        Direction dir = closest[i];

                        IntVec newLocation = recentNode.position + dir;

                        if (solid[newLocation.X, newLocation.Y] < -(recentNode.actualCost + 10) || (solid[newLocation.X, newLocation.Y] == int.MaxValue && newLocation.Equals(to)))
                        {
                            solid[newLocation.X, newLocation.Y] = -(recentNode.actualCost + 10);
                            nodes.Add(new __AStarNode(newLocation, recentNode.actualCost + 10, 10 * calculateHeuristic(newLocation, to), i, recentNode, dir));
                        }
                    }



                    nodes.Remove(recentNode);
                }
            }

            if (bestNode != null)
            {
                isPossible = bestNode.position.Equals(to);
            }

            List <Direction> path = new List <Direction>();

            recentNode = recentNode ?? bestNode;

            while (recentNode != null && !recentNode.position.Equals(from))
            {
                path.Add(recentNode.directionTaken);
                recentNode = recentNode.reachedFrom;
            }

            path.Reverse();

            return(path.ToArray());
        }