Example #1
0
        private static IEnumerable <IntVec> lineBetween(Level level, IntVec start, IntVec end)
        {
            IntVec delta = new IntVec(Math.Abs(end.X - start.X), Math.Abs(end.Y - start.Y));
            int    sx    = (end.X > start.X) ? 1 : -1;
            int    sy    = (end.Y > start.Y) ? 1 : -1;
            int    err   = delta.X - delta.Y;

            IntVec current = new IntVec(start);

            while (!current.Equals(end))
            {
                int e2 = 2 * err;

                if (e2 > -delta.Y)
                {
                    err       -= delta.Y;
                    current.X += sx;
                }
                if (e2 < delta.X)
                {
                    err       += delta.X;
                    current.Y += sy;
                }

                yield return(new IntVec(current));
            }
        }
Example #2
0
        private Tuple <T, IntVec> getTupleByPosition(IntVec position)
        {
            Tuple <T, IntVec> result = null;

            foreach (Tuple <T, IntVec> tup in list)
            {
                if (position.Equals(tup.Item2))
                {
                    result = tup;
                }
            }

            return(result);
        }
Example #3
0
        public void RemoveAtPosition(IntVec position)
        {
            bool found = false;

            for (int i = 0; i < list.Count; i++)
            {
                if (!found)
                {
                    Tuple <T, IntVec> tup = list.ElementAt(i);
                    if (position.Equals(tup.Item2))
                    {
                        list.RemoveAt(i);
                        found = true;
                    }
                }
            }
        }
Example #4
0
        private static void possiblePositionsFromStep(Level level, List <IntVec> positions, int[,] used, IntVec start, IntVec position, int budget, CharacterTargeting targetCharacters, bool straight, bool expand)
        {
            if ((!straight || lineIsFree(level, start, position, targetCharacters != CharacterTargeting.PASS_THROUGH)))
            {
                used[position.X, position.Y] = budget;

                if (!start.Equals(position) && !positions.Contains(position))
                {
                    positions.Add(position);
                }

                if (expand && budget > 0)
                {
                    foreach (Direction dir in Direction.Values)
                    {
                        IntVec target = position + dir;
                        if (used[target.X, target.Y] < budget - 1 || (used[target.X, target.Y] == int.MaxValue && (targetCharacters == CharacterTargeting.PASS_THROUGH || targetCharacters == CharacterTargeting.TARGET_FIRST) && level.CharacterEntities.FindEntity(target) != null))
                        {
                            possiblePositionsFromStep(level, positions, used, start, target, budget - 1, targetCharacters, straight, !(used[target.X, target.Y] == int.MaxValue));
                        }
                    }
                }
            }
        }
Example #5
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());
        }