Guest User

Untitled

a guest
Jun 5th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 6.04 KB | None | 0 0
  1. commit 4adb2cde5744189550f87a475c09837938ed90d4
  2. Author: Aron Granberg <aron.granberg@gmail.com>
  3. Date:   Thu Apr 2 13:57:16 2020 +0200
  4.  
  5.     The RaycastModifier now respects which tags are traversable and any ITraversalProvider set on the path.
  6.  
  7. diff --git a/Assets/AstarPathfindingProject/Modifiers/RaycastModifier.cs b/Assets/AstarPathfindingProject/Modifiers/RaycastModifier.cs
  8. index 7e0c5157..23298d54 100644
  9. --- a/Assets/AstarPathfindingProject/Modifiers/RaycastModifier.cs
  10. +++ b/Assets/AstarPathfindingProject/Modifiers/RaycastModifier.cs
  11. @@ -108,12 +108,28 @@ namespace Pathfinding {
  12.         static float[] DPCosts = new float[16];
  13.         static int[] DPParents = new int[16];
  14.  
  15. +       Filter cachedFilter = new Filter();
  16. +
  17. +       class Filter {
  18. +           public Path path;
  19. +           public readonly System.Func<GraphNode, bool> cachedDelegate;
  20. +
  21. +           public Filter() {
  22. +               cachedDelegate = this.CanTraverse;
  23. +           }
  24. +
  25. +           bool CanTraverse(GraphNode node) {
  26. +               return path.CanTraverse(node);
  27. +           }
  28. +       }
  29. +
  30.         public override void Apply (Path p) {
  31.             if (!useRaycasting && !useGraphRaycasting) return;
  32.  
  33.             var points = p.vectorPath;
  34. +           cachedFilter.path = p;
  35.  
  36. -           if (ValidateLine(null, null, p.vectorPath[0], p.vectorPath[p.vectorPath.Count-1])) {
  37. +           if (ValidateLine(null, null, p.vectorPath[0], p.vectorPath[p.vectorPath.Count-1], cachedFilter.cachedDelegate)) {
  38.                 // A very common case is that there is a straight line to the target.
  39.                 var s = p.vectorPath[0];
  40.                 var e = p.vectorPath[p.vectorPath.Count-1];
  41. @@ -130,7 +146,7 @@ namespace Pathfinding {
  42.                         points.Reverse();
  43.                     }
  44.  
  45. -                   points = quality >= Quality.High ? ApplyDP(p, points) : ApplyGreedy(p, points);
  46. +                   points = quality >= Quality.High ? ApplyDP(p, points, cachedFilter.cachedDelegate) : ApplyGreedy(p, points, cachedFilter.cachedDelegate);
  47.                 }
  48.                 if ((iterations % 2) == 0) points.Reverse();
  49.             }
  50. @@ -138,7 +154,7 @@ namespace Pathfinding {
  51.             p.vectorPath = points;
  52.         }
  53.  
  54. -       List<Vector3> ApplyGreedy (Path p, List<Vector3> points) {
  55. +       List<Vector3> ApplyGreedy (Path p, List<Vector3> points, System.Func<GraphNode, bool> filter) {
  56.             bool canBeOriginalNodes = points.Count == p.path.Count;
  57.             int startIndex = 0;
  58.  
  59. @@ -157,7 +173,7 @@ namespace Pathfinding {
  60.                     }
  61.                     Vector3 end = points[endIndex];
  62.                     var endNode = canBeOriginalNodes && end == (Vector3)p.path[endIndex].position ? p.path[endIndex] : null;
  63. -                   if (!ValidateLine(startNode, endNode, start, end)) break;
  64. +                   if (!ValidateLine(startNode, endNode, start, end, filter)) break;
  65.                     mn = mx;
  66.                     mx *= 2;
  67.                 }
  68. @@ -168,7 +184,7 @@ namespace Pathfinding {
  69.                     Vector3 end = points[endIndex];
  70.                     var endNode = canBeOriginalNodes && end == (Vector3)p.path[endIndex].position ? p.path[endIndex] : null;
  71.  
  72. -                   if (ValidateLine(startNode, endNode, start, end)) {
  73. +                   if (ValidateLine(startNode, endNode, start, end, filter)) {
  74.                         mn = mid;
  75.                     } else {
  76.                         mx = mid;
  77. @@ -182,7 +198,7 @@ namespace Pathfinding {
  78.             return points;
  79.         }
  80.  
  81. -       List<Vector3> ApplyDP (Path p, List<Vector3> points) {
  82. +       List<Vector3> ApplyDP (Path p, List<Vector3> points, System.Func<GraphNode, bool> filter) {
  83.             if (DPCosts.Length < points.Count) {
  84.                 DPCosts = new float[points.Count];
  85.                 DPParents = new int[points.Count];
  86. @@ -201,7 +217,7 @@ namespace Pathfinding {
  87.                     float d2 = d + (points[j] - start).magnitude + 0.0001f;
  88.                     if (DPParents[j] == -1 || d2 < DPCosts[j]) {
  89.                         var endIsOriginalNode = canBeOriginalNodes && points[j] == (Vector3)p.path[j].position;
  90. -                       if (j == i+1 || ValidateLine(startIsOriginalNode ? p.path[i] : null, endIsOriginalNode ? p.path[j] : null, start, points[j])) {
  91. +                       if (j == i+1 || ValidateLine(startIsOriginalNode ? p.path[i] : null, endIsOriginalNode ? p.path[j] : null, start, points[j], filter)) {
  92.                             DPCosts[j] = d2;
  93.                             DPParents[j] = i;
  94.                         } else {
  95. @@ -225,7 +241,7 @@ namespace Pathfinding {
  96.         /** Check if a straight path between v1 and v2 is valid.
  97.          * If both \a n1 and \a n2 are supplied it is assumed that the line goes from the center of \a n1 to the center of \a n2 and a more optimized graph linecast may be done.
  98.          */
  99. -       protected bool ValidateLine (GraphNode n1, GraphNode n2, Vector3 v1, Vector3 v2) {
  100. +       protected bool ValidateLine (GraphNode n1, GraphNode n2, Vector3 v1, Vector3 v2, System.Func<GraphNode, bool> filter) {
  101.             if (useRaycasting) {
  102.                 // Use raycasting to check if a straight path between v1 and v2 is valid
  103.                 if (use2DPhysics) {
  104. @@ -276,11 +292,11 @@ namespace Pathfinding {
  105.                         // This method is also more stable when raycasting along a diagonal when the line just touches an obstacle.
  106.                         // The normal linecast method may or may not detect that as a hit depending on floating point errors
  107.                         // however this method never detect it as an obstacle (and that is very good for this component as it improves the simplification).
  108. -                       return !gg.Linecast(n1 as GridNodeBase, n2 as GridNodeBase);
  109. +                       return !gg.Linecast(n1 as GridNodeBase, n2 as GridNodeBase, filter);
  110.                     } else
  111.  #endif
  112.                     if (rayGraph != null) {
  113. -                       return !rayGraph.Linecast(v1, v2, n1);
  114. +                       return !rayGraph.Linecast(v1, v2, out GraphHitInfo _, null, filter);
  115.                     }
  116.                 }
  117.             }
  118. diff --git a/Assets/AstarPathfindingProject/changelog.cs b/Assets/AstarPathfindingProject/changelog.cs
  119. index 277ef2b5..6e78c8e7 100644
  120. --- a/Assets/AstarPathfindingProject/changelog.cs
  121. +++ b/Assets/AstarPathfindingProject/changelog.cs
  122. @@ -4,6 +4,7 @@
  123.  - 4.3.20
  124.     - Added a filter parameter to graph linecast methods. This can be used to mark additional nodes as not being traversable by the linecast.
  125.         See \link Pathfinding.GridGraph.Linecast(Vector3,Vector3,GraphHitInfo,List<GraphNode>,System.Func<GraphNode,bool>) GridGraph.Linecast\endlink.
  126. +   - The \link Pathfinding.RaycastModifier RaycastModifier\endlink now respects which tags are traversable and any ITraversalProvider set on the path.
  127.  
  128.  - 4.3.19
  129.     - Fixed the Optimization tab not working when installing the package using the unity package manager.
Add Comment
Please, Sign In to add comment