Exemple #1
0
            private void          ExpandHex(IDirectedPath path, Hexside hexside)
            {
                var here  = path.PathStep.Hex;
                var there = Board[here.Coords.GetNeighbour(hexside)];

                if (there != null && !ClosedSet.Contains(there.Coords))
                {
                    var cost = StepCost(here, hexside, there);
                    if ((cost > 0) &&
                        (path.TotalCost + cost < BestSoFar || !OpenSet.ContainsKey(there.Coords))
                        )
                    {
                        var key     = path.TotalCost + cost + Heuristic(there.Coords);
                        var newPath = path.AddStep(there, HexsideDirection(hexside), cost);

                        TraceFindPathEnqueue(there.Coords, key, 0);

                        IDirectedPath oldPath;
                        if (!OpenSet.TryGetValue(there.Coords, out oldPath))
                        {
                            OpenSet.Add(there.Coords, newPath);
                            Queue.Enqueue(key, newPath);
                        }
                        else if (newPath.TotalCost < oldPath.TotalCost)
                        {
                            OpenSet.Remove(there.Coords);
                            OpenSet.Add(there.Coords, newPath);
                            Queue.Enqueue(key, newPath);
                        }

                        SetBestSoFar(newPath, GetPartnerPath(there.Coords));
                    }
                }
            }
        private void     ExpandHex(IDirectedPath path, Hexside hexside)
        {
            var here  = path.PathStep.Coords;
            var there = here.GetNeighbour(hexside);

            if (!ClosedSet.Contains(there))
            {
                TryStepCost(here, hexside).IfHasValueDo(cost => {
                    if (path.TotalCost + cost < BestSoFar || !OpenSet.ContainsKey(there))
                    {
                        Heuristic(there).IfHasValueDo(heuristic => {
                            var key     = path.TotalCost + cost + heuristic;
                            var newPath = path.AddStep(there, HexsideDirection(hexside), cost);

                            PathfinderExtensions.TraceFindPathEnqueue(there, key, 0);

                            if (!OpenSet.TryGetValue(there, out var oldPath))
                            {
                                OpenSet.Add(there, newPath);
                                Queue.Enqueue(key, newPath);
                            }
                            else if (newPath.TotalCost < oldPath.TotalCost)
                            {
                                OpenSet.Remove(there);
                                OpenSet.Add(there, newPath);
                                Queue.Enqueue(key, newPath);
                            }

                            SetBestSoFar(newPath, PartnerPath(there));
                        });
                    }
                });
            }
        }
Exemple #3
0
 /// <summary>TODO</summary>
 private static IDirectedPath MergePaths(IDirectedPath targetPath, IDirectedPath sourcePath)
 {
     if (sourcePath != null)
     {
         while (sourcePath.PathSoFar != null)
         {
             var hexside = sourcePath.PathStep.HexsideExit;
             var cost    = sourcePath.TotalCost - (sourcePath = sourcePath.PathSoFar).TotalCost;
             targetPath = targetPath.AddStep(sourcePath.PathStep.Hex, hexside, cost);
         }
     }
     return(targetPath);
 }
 /// <summary>Returns the result of stacking <paramref name="mergePath"/> onto <paramref name="targetPath"/></summary>
 public static Maybe <IDirectedPath> MergePaths <THex>(this IDirectedPath targetPath, IDirectedPath mergePath)
     where THex : class, IHex
 {
     if (mergePath != null)
     {
         while (mergePath.PathSoFar != null)
         {
             var hexside = mergePath.PathStep.HexsideExit;
             var cost    = mergePath.TotalCost - (mergePath = mergePath.PathSoFar).TotalCost;
             targetPath = targetPath.AddStep(mergePath.PathStep.Hex, hexside, cost);
         }
     }
     return(targetPath.ToMaybe());
 }
Exemple #5
0
 /// <summary>Returns the result of stacking <paramref name="mergePath"/> onto <paramref name="this"/></summary>
 public static Maybe <IDirectedPath> MergePaths(this IDirectedPath @this, IDirectedPath mergePath)
 {
     if (@this == null || mergePath == null)
     {
         return(null);
     }
     while (mergePath.PathSoFar != null)
     {
         var hexside = mergePath.PathStep.HexsideExit;
         var cost    = mergePath.TotalCost - (mergePath = mergePath.PathSoFar).TotalCost;
         @this = @this.AddStep(mergePath.PathStep.Coords, hexside, cost);
     }
     return(@this.ToMaybe());
 }
        void ExpandNeighbour(IDirectedPath path, NeighbourHex neighbour)
        {
            if (!OpenSet.Contains(neighbour.Hex.Coords))
            {
                var cost = StepCost(neighbour.Hex, neighbour.HexsideExit);
                if (cost > 0)
                {
                    var newPath = path.AddStep(neighbour, cost);
                    var key     = Estimate(Heuristic, VectorGoal, Source.Coords,
                                           neighbour.Hex.Coords, newPath.TotalCost);

                    TraceFindPathEnqueue(neighbour.Hex.Coords, key >> 16, (int)(key & 0xFFFFu));

                    Queue.Enqueue(key, newPath);
                }
            }
        }
Exemple #7
0
 /// <summary>Returns a new instance composed by extending this DirectedPath by one hex.</summary>
 /// <param name="this"></param>
 /// <param name="hex"></param>
 /// <param name="hexsideExit"></param>
 /// <param name="stepCost"></param>
 public static IDirectedPath AddStep(this IDirectedPath @this, IHex hex, Hexside hexsideExit, int stepCost)
 => @this.AddStep(new DirectedPathStepHex(hex, hexsideExit), stepCost);