Advertisement
Guest User

AstarPath.cs ScanLoop

a guest
May 12th, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.11 KB | None | 0 0
  1. //public void ScanLoop (OnScanStatus statusCallback) {
  2.     public IEnumerable<Progress> ScanLoop() {
  3.         if (graphs == null) {
  4.             yield break;
  5.         }
  6.  
  7.         isScanning = true;
  8.         euclideanEmbedding.dirty = false;
  9.  
  10.         VerifyIntegrity ();
  11.  
  12.         BlockUntilPathQueueBlocked ();
  13.  
  14.         if (!Application.isPlaying) {
  15.             GraphModifier.FindAllModifiers ();
  16.             RelevantGraphSurface.FindAllGraphSurfaces ();
  17.         }
  18.  
  19.         RelevantGraphSurface.UpdateAllPositions ();
  20.  
  21.         astarData.UpdateShortcuts ();
  22.  
  23.         //if (statusCallback != null) statusCallback (new Progress (0.05F,"Pre processing graphs"));
  24.         yield return new Progress(0.05F, "Pre processing graphs");
  25.  
  26.         if (OnPreScan != null) {
  27.             OnPreScan (this);
  28.         }
  29.  
  30.         GraphModifier.TriggerEvent (GraphModifier.EventType.PreScan);
  31.  
  32.         System.DateTime startTime = System.DateTime.UtcNow;
  33.  
  34.         // Destroy previous nodes
  35.         for (int i=0;i<graphs.Length;i++) {
  36.             if (graphs[i] != null) {
  37.                 graphs[i].GetNodes (delegate (GraphNode node) {
  38.                     node.Destroy ();
  39.                     return true;
  40.                 });
  41.             }
  42.         }
  43.  
  44.         for (int i=0;i<graphs.Length;i++) {
  45.  
  46.             NavGraph graph = graphs[i];
  47.  
  48.             if (graph == null) {
  49.                 //if (statusCallback != null) statusCallback (new Progress (AstarMath.MapTo (0.05F,0.7F,(float)(i+0.5F)/(graphs.Length+1)),"Skipping graph "+(i+1)+" of "+graphs.Length+" because it is null"));
  50.                 yield return new Progress(AstarMath.MapTo(0.05F, 0.7F, (float)(i + 0.5F) / (graphs.Length + 1)), "Skipping graph " + (i + 1) + " of " + graphs.Length + " because it is null");
  51.                 continue;
  52.             }
  53.  
  54.             if (OnGraphPreScan != null) {
  55.                 //if (statusCallback != null) statusCallback (new Progress (AstarMath.MapToRange (0.1F,0.7F,(float)(i)/(graphs.Length)),"Scanning graph "+(i+1)+" of "+graphs.Length+" - Pre processing"));
  56.                 yield return new Progress(Mathf.Lerp(0.1F, 0.7F, (float)(i) / (graphs.Length)), "Scanning graph " + (i + 1) + " of " + graphs.Length + " - Pre processing");
  57.                 OnGraphPreScan (graph);
  58.             }
  59.  
  60.             //float minp = AstarMath.MapToRange (0.1F,0.7F,(float)(i)/(graphs.Length));
  61.             //float maxp = AstarMath.MapToRange (0.1F,0.7F,(float)(i+0.95F)/(graphs.Length));
  62.  
  63.             //if (statusCallback != null) statusCallback (new Progress (minp,"Scanning graph "+(i+1)+" of "+graphs.Length));
  64.  
  65.             // Just used for progress information
  66.             // This graph will advance the progress bar from minp to maxp
  67.             float minp = Mathf.Lerp(0.1F, 0.7F, (float)(i) / (graphs.Length));
  68.             float maxp = Mathf.Lerp(0.1F, 0.7F, (float)(i + 0.95F) / (graphs.Length));
  69.  
  70.             var progressDescriptionPrefix = "Scanning graph " + (i + 1) + " of " + graphs.Length;
  71.  
  72.             yield return new Progress(minp, progressDescriptionPrefix);
  73.  
  74.  
  75.             //OnScanStatus info = null;
  76.             //if (statusCallback != null) {
  77.             //  info = delegate (Progress p) {
  78.             //      p.progress = AstarMath.MapToRange (minp, maxp, p.progress);
  79.             //      statusCallback (p);
  80.             //  };
  81.             //}
  82.             foreach (var p in graph.ScanInternal())
  83.             {
  84.                 yield return new Progress(AstarMath.MapToRange(minp, maxp, p.progress), progressDescriptionPrefix + " - " + p.description);
  85.             }
  86.  
  87.             //graph.ScanInternal (info);
  88.  
  89.             // Assign the graph index to every node in the graph
  90.             graph.GetNodes (delegate (GraphNode node) {
  91.                 node.GraphIndex = (uint)i;
  92.                 return true;
  93.             });
  94.  
  95.             if (OnGraphPostScan != null) {
  96.                 //if (statusCallback != null) statusCallback (new Progress (AstarMath.MapToRange (0.1F,0.7F,(float)(i+0.95F)/(graphs.Length)),"Scanning graph "+(i+1)+" of "+graphs.Length+" - Post processing"));
  97.                 yield return new Progress(AstarMath.MapToRange(0.1F, 0.7F, (float)(i + 0.95F) / (graphs.Length)), progressDescriptionPrefix + " - Post processing");
  98.                 OnGraphPostScan (graph);
  99.             }
  100.         }
  101.  
  102.         //if (statusCallback != null) statusCallback (new Progress (0.8F,"Post processing graphs"));
  103.         yield return new Progress(0.8F, "Post processing graphs");
  104.  
  105.         if (OnPostScan != null) {
  106.             OnPostScan (this);
  107.         }
  108.         GraphModifier.TriggerEvent (GraphModifier.EventType.PostScan);
  109.  
  110.         try {
  111.             FlushWorkItems(false, true);
  112.         } catch (System.Exception e) {
  113.             Debug.LogException (e);
  114.         }
  115.  
  116.         isScanning = false;
  117.  
  118.         //if (statusCallback != null) statusCallback (new Progress (0.90F,"Computing areas"));
  119.         yield return new Progress(0.90F, "Computing areas");
  120.  
  121.         FloodFill ();
  122.  
  123.         VerifyIntegrity ();
  124.  
  125.         //if (statusCallback != null) statusCallback (new Progress (0.95F,"Late post processing"));
  126.         yield return new Progress(0.95F, "Late post processing");
  127.  
  128.         // Signal that we have stopped scanning here
  129.         // Note that no yields can happen after this point
  130.         // since then other parts of the system can start to interfere
  131.         isScanning = false;
  132.  
  133.         if (OnLatePostScan != null) {
  134.             OnLatePostScan (this);
  135.         }
  136.         GraphModifier.TriggerEvent (GraphModifier.EventType.LatePostScan);
  137.  
  138.         euclideanEmbedding.dirty = true;
  139.         euclideanEmbedding.RecalculatePivots ();
  140.  
  141.         //Perform any blocking actions and unblock (probably, some tasks might take a few frames)
  142.         PerformBlockingActions(true);
  143.  
  144.         lastScanTime = (float)(System.DateTime.UtcNow-startTime).TotalSeconds;
  145.  
  146.         System.GC.Collect ();
  147.  
  148.         AstarLog ("Scanning - Process took "+(lastScanTime*1000).ToString ("0")+" ms to complete");
  149.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement