Advertisement
Guest User

Untitled

a guest
May 2nd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1.  
  2.     public float2[,] FlowField(MapCell goal, Squad squad = null)
  3.     {
  4. #if UNITY_EDITOR
  5.         UnityEngine.Profiling.Profiler.BeginSample("FlowField");
  6. #endif
  7.         // reset costs
  8.         for (int x = 0; x < width; x++)
  9.         {
  10.             for (int y = 0; y < height; y++)
  11.             {
  12.                 MapCell cell = grid.grid_cells[x, y];
  13.                 cell.cost = 0;
  14.                 result[x, y] = 0;
  15.             }
  16.         }
  17.         // result directions
  18.         // open cells
  19.         List<MapCell> open = new List<MapCell>();
  20.         // start dijkstra
  21.         open.Add(goal);
  22.         goal.cost = 0;
  23.         // if there is a squad, only process dijkstra until we reach each squad pawn
  24.         List<Pawn> to_process = null;
  25.         List<int2> coords_to_process = new List<int2>();
  26.         if(squad != null)
  27.         {
  28.             to_process = new List<Pawn>();
  29.             for(int i = 0; i < squad.pawns.Count; i++)
  30.             {
  31.                 to_process.Add(squad.pawns[i]);
  32.             }
  33.         }
  34.        
  35.         while(open.Count > 0)
  36.         {
  37.             MapCell cell = open[0];
  38.             coords_to_process.Add(cell.coords);
  39.             open.RemoveAt(0);
  40.             var neighbours = grid.GetNeighbours(cell);
  41.             for (var i = 0; i < neighbours.Count; i++)
  42.             {
  43.                 MapCell n = neighbours[i];
  44.                 if(n == null)
  45.                 {
  46.                     continue;
  47.                 }
  48.                 if (!open.Contains(n) && n.cost == 0)
  49.                 {
  50.                     n.cost = cell.cost + n.weight;
  51.                     open.Add(n);
  52.                 }
  53.             }
  54.             // early break in case we've reached every pawn, no point in going further
  55.             if(to_process != null)
  56.             {
  57.                 for (int j = to_process.Count - 1; j >= 0; j--)
  58.                 {
  59.                     if (cell.pawns.Contains(to_process[j]))
  60.                     {
  61.                         to_process.RemoveAt(j);
  62.                     }
  63.                 }
  64.                 if (to_process.Count == 0)
  65.                 {
  66.                     break;
  67.                 }
  68.             }
  69.         }
  70.  
  71.         // after dijkstra create flow field
  72.         for (int j = 0; j < coords_to_process.Count; j++)
  73.         {
  74.             int2 coords = coords_to_process[j];
  75.             MapCell cell = grid.grid_cells[coords.x, coords.y];
  76.             var neighbours = grid.GetNeighbours(cell);
  77.  
  78.             //Go through all neighbours and find the one with the lowest distance
  79.             MapCell min = null;
  80.             float minDist = 0;
  81.             for (var i = 0; i < neighbours.Count; i++)
  82.             {
  83.                 MapCell n = neighbours[i];
  84.                 var dist = n.cost - cell.cost;
  85.  
  86.                 if (dist < minDist)
  87.                 {
  88.                     min = n;
  89.                     minDist = dist;
  90.                 }
  91.             }
  92.  
  93.             //If we found a valid neighbour, point in its direction
  94.             if (min != null)
  95.             {
  96.                 result[coords.x, coords.y] = math.normalize(min.pos - cell.pos);
  97.             }
  98.         }
  99. #if UNITY_EDITOR
  100.         UnityEngine.Profiling.Profiler.EndSample();
  101. #endif
  102.         return result;
  103.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement