Exemplo n.º 1
0
        private IEnumerable <AstarDecoratorValues> DiscoverPossibleStepsArround(AstarDecoratorValues currentPosition, int startX, int startZ, int destinationX, int destinationZ)
        {
            List <AstarDecoratorValues> possibleSteps = new List <AstarDecoratorValues>();

            for (int xOff = -1; xOff <= 1; xOff++)
            {
                for (int zOff = -1; zOff <= 1; zOff++)
                {
                    var lookedX = currentPosition.PossibleStep.X + xOff;
                    var lookedZ = currentPosition.PossibleStep.Z + zOff;

                    if (xOff == 0 && zOff == 0)
                    {
                        continue;
                    }

                    if (NoDiagonals)
                    {
                        if (xOff == -1 && zOff == -1)
                        {
                            continue;
                        }
                        if (xOff == -1 && zOff == 1)
                        {
                            continue;
                        }
                        if (xOff == 1 && zOff == -1)
                        {
                            continue;
                        }
                        if (xOff == 1 && zOff == 1)
                        {
                            continue;
                        }
                    }

                    if (!MapService.IsBlockedPosition(lookedX, lookedZ))
                    {
                        if (LimitCubeType.HasValue && MapService.GetCubeType(lookedX, lookedZ) != LimitCubeType.Value)
                        {
                            continue;
                        }

                        var possibleStep = new MovingStep(lookedX, lookedZ);
                        var aStar        = new AstarDecoratorValues(possibleStep, currentPosition);
                        CalculateCost(startX, startZ, destinationX, destinationZ, aStar);

                        // found destination
                        if (aStar.HCost == 0)
                        {
                            return(new AstarDecoratorValues[] { aStar });
                        }

                        possibleSteps.Add(aStar);
                    }
                }
            }
            return(possibleSteps);
        }
Exemplo n.º 2
0
        public IEnumerable <MovingStep> FromTo(int startX, int startZ, int destinationX, int destinationZ)
        {
            var blockCount = MapService.GetTotalBocks();

            AllPossibilities = new Dictionary <KeyValuePair <int, int>, AstarDecoratorValues>(blockCount);

            var startingPosition = new AstarDecoratorValues(new MovingStep(startX, startZ), null);

            CalculateCost(startX, startZ, destinationX, destinationZ, startingPosition);

            var foundPath = ContinuePathFinding(startingPosition, startX, startZ, destinationX, destinationZ);

            while (foundPath == null)
            {
                foundPath = GetNextPossibility();
                if (foundPath == null)
                {
                    break;
                }
                else if (foundPath.HCost == 0)
                {
                    break;
                }

                foundPath.AlreadyChecked = true;
                var newFound = ContinuePathFinding(foundPath, startX, startZ, destinationX, destinationZ);
                if (newFound != null)
                {
                    foundPath = newFound;
                    break;
                }
                foundPath = null;
            }

            if (foundPath == null)
            {
                return(Enumerable.Empty <MovingStep>());
            }

            var stepsToDestination = new Stack <MovingStep>();
            var currentPath        = foundPath;

            while (currentPath.Parent != null)
            {
                stepsToDestination.Push(currentPath.PossibleStep);
                currentPath = currentPath.Parent;
            }

            return(stepsToDestination.ToList());
        }
Exemplo n.º 3
0
        private AstarDecoratorValues ContinuePathFinding(AstarDecoratorValues currentPosition, int startX, int startZ, int destinationX, int destinationZ)
        {
            IEnumerable <AstarDecoratorValues> astarSteps = DiscoverPossibleStepsArround(currentPosition, startX, startZ, destinationX, destinationZ);

            foreach (var astarStep in astarSteps)
            {
                var similarPossibility = GetPossibilityAt(astarStep.PossibleStep.X, astarStep.PossibleStep.Z);

                KeyValuePair <int, int> key = new KeyValuePair <int, int>(astarStep.PossibleStep.X, astarStep.PossibleStep.Z);

                if (AllPossibilities.ContainsKey(key))
                {
                    var older = AllPossibilities[key];
                    if (older.FCost > astarStep.FCost)
                    {
                        AllPossibilities[key] = astarStep;
                    }
                }
                else
                {
                    AllPossibilities[key] = astarStep;
                }

                if (similarPossibility != null && similarPossibility.AlreadyChecked)
                {
                    astarStep.AlreadyChecked = similarPossibility.AlreadyChecked;
                }
            }

            AstarDecoratorValues nextPossibility = GetNextPossibility();

            if (nextPossibility != null)
            {
                if (nextPossibility.HCost == 0)
                {
                    return(nextPossibility);
                }
            }
            return(null);
        }
Exemplo n.º 4
0
 private void CalculateCost(int startX, int startZ, int destinationX, int destinationZ, AstarDecoratorValues aStarValue)
 {
     aStarValue.GCost = CalculateCostRecursive(aStarValue.PossibleStep.X, aStarValue.PossibleStep.Z, startX, startZ);
     aStarValue.HCost = CalculateCostRecursive(aStarValue.PossibleStep.X, aStarValue.PossibleStep.Z, destinationX, destinationZ);
 }
Exemplo n.º 5
0
 public AstarDecoratorValues(MovingStep possibleStep, AstarDecoratorValues parent)
 {
     PossibleStep = possibleStep;
     Parent       = parent;
 }