Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. protected override List<GraphNode> GetNodesInRegion (Bounds b, GraphUpdateShape shape) {
  2.     var rect = GetRectFromBounds(b);
  3.  
  4.     if (nodes == null || !rect.IsValid() || nodes.Length != width*depth*layerCount) {
  5.         return Pathfinding.Util.ListPool<GraphNode>.Claim();
  6.     }
  7.  
  8.     // Get a buffer we can use
  9.     var inArea = Pathfinding.Util.ListPool<GraphNode>.Claim(rect.Width*rect.Height*layerCount);
  10.  
  11.     // Loop through all nodes in the rectangle
  12.     for (int l = 0; l < layerCount; l++) {
  13.         var lwd = l * width * depth;
  14.         for (int x = rect.xmin; x <= rect.xmax; x++) {
  15.             for (int z = rect.ymin; z <= rect.ymax; z++) {
  16.                 int index = lwd + z*width + x;
  17.  
  18.                 GraphNode node = nodes[index];
  19.  
  20.                 // If it is contained in the bounds (and optionally the shape)
  21.                 // then add it to the buffer
  22.                 if (node != null && b.Contains((Vector3)node.position) && (shape == null || shape.Contains((Vector3)node.position))) {
  23.                     inArea.Add(node);
  24.                 }
  25.             }
  26.         }
  27.     }
  28.  
  29.     return inArea;
  30. }
  31.  
  32. public override List<GraphNode> GetNodesInRegion (IntRect rect) {
  33.     // Get a buffer we can use
  34.     var inArea = Pathfinding.Util.ListPool<GraphNode>.Claim();
  35.  
  36.     // Rect which covers the whole grid
  37.     var gridRect = new IntRect(0, 0, width-1, depth-1);
  38.  
  39.     // Clamp the rect to the grid
  40.     rect = IntRect.Intersection(rect, gridRect);
  41.  
  42.     if (nodes == null || !rect.IsValid() || nodes.Length != width*depth*layerCount) return inArea;
  43.  
  44.     for (int l = 0; l < layerCount; l++) {
  45.         var lwd = l * Width * Depth;
  46.         for (int z = rect.ymin; z <= rect.ymax; z++) {
  47.             var offset = lwd + z*Width;
  48.             for (int x = rect.xmin; x <= rect.xmax; x++) {
  49.                 var node = nodes[offset + x];
  50.                 if (node != null) {
  51.                     inArea.Add(node);
  52.                 }
  53.             }
  54.         }
  55.     }
  56.  
  57.     return inArea;
  58. }
  59.  
  60. /** Get all nodes in a rectangle.
  61.  * \param rect Region in which to return nodes. It will be clamped to the grid.
  62.  * \param buffer Buffer in which the nodes will be stored. Should be at least as large as the number of nodes that can exist in that region.
  63.  * \returns The number of nodes written to the buffer.
  64.  */
  65. public override int GetNodesInRegion (IntRect rect, GridNodeBase[] buffer) {
  66.     // Clamp the rect to the grid
  67.     // Rect which covers the whole grid
  68.     var gridRect = new IntRect(0, 0, width-1, depth-1);
  69.  
  70.     rect = IntRect.Intersection(rect, gridRect);
  71.  
  72.     if (nodes == null || !rect.IsValid() || nodes.Length != width*depth*layerCount) return 0;
  73.  
  74.     int counter = 0;
  75.     try {
  76.         for (int l = 0; l < layerCount; l++) {
  77.             var lwd = l * Width * Depth;
  78.             for (int z = rect.ymin; z <= rect.ymax; z++) {
  79.                 var offset = lwd + z*Width;
  80.                 for (int x = rect.xmin; x <= rect.xmax; x++) {
  81.                     var node = nodes[offset + x];
  82.                     if (node != null) {
  83.                         buffer[counter] = node;
  84.                         counter++;
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.     } catch (System.IndexOutOfRangeException) {
  90.         // Catch the exception which 'buffer[counter] = node' would throw if the buffer was too small
  91.         throw new System.ArgumentException("Buffer is too small");
  92.     }
  93.  
  94.     return counter;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement