Advertisement
mixster

mixster

Aug 25th, 2009
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.39 KB | None | 0 0
  1. type
  2.   node = record
  3.     parent: Integer;
  4.     x, y: Integer;
  5.     f, g, h: Integer;
  6.     cl, op: Boolean;
  7.   end;
  8.    
  9. procedure AStarGrid(var path: TPointArray; st, en: TPoint; grid: T2DIntArray);
  10. var
  11.   nodes: array of node;
  12.   w, h, i, nh, ti, tf, oh, ch: Integer;
  13.   closed, open, neigh: TIntegerArray;
  14. begin
  15.   h := High(grid) + 1;
  16.   w := High(grid[0]) + 1;
  17.   nh := (w * h);
  18.   SetLength(nodes, nh);
  19.   SetLength(open, nh);
  20.   SetLength(closed, nh);
  21.   nh := nh - 1;
  22.   for i := 0 to nh do
  23.   begin
  24.     with nodes[i] do
  25.     begin
  26.       parent := -1;
  27.       x := i mod w;
  28.       y := Trunc(i / w);
  29.       g := grid[y][x];
  30.       h := Round(10 * Distance(x, 0, en.x, 0) + Distance(0, y, 0, en.y));
  31.       f := g + h;
  32.       cl := False;
  33.       op := False;
  34.     end;
  35.   end;
  36.  
  37.   open[0] := (st.y * w) + st.x;
  38.   nodes[open[0]].op := True;
  39.   oh := 0;
  40.   ch := -1;
  41.   repeat
  42.     ti := 0;
  43.     tf := nodes[open[0]].f;
  44.     for i := 1 to oh do
  45.     begin
  46.       if (nodes[open[i]].f < tf) then
  47.       begin
  48.         ti := i;
  49.         tf := nodes[open[i]].f;
  50.       end;
  51.     end;
  52.     i := ti;
  53.     ti := open[ti];
  54.     nodes[ti].cl := True;
  55.     nodes[ti].op := False;
  56.     Swap(open[i], open[oh]);
  57.     oh := oh - 1;
  58.     ch := ch + 1;
  59.     closed[ch] := ti;
  60.     if ((nodes[ti].x = en.x) and (nodes[ti].y = en.y)) then
  61.       break;
  62.    
  63.     with nodes[ti] do
  64.     begin
  65.       neigh := [(ti - w) - 1, (ti - w), (ti - w) + 1, ti - 1, ti + 1, (ti + w) - 1, (ti + w), (ti + w) + 1];
  66.     end;
  67.  
  68.     for i := 0 to 7 do
  69.     begin
  70.       if (not (InRange(neigh[i], 0, nh))) then
  71.         continue;
  72.       if (nodes[neigh[i]].cl) then
  73.         continue;
  74.       if (nodes[neigh[i]].op) then
  75.       begin
  76.         if (nodes[ti].f < nodes[neigh[i]].g) then
  77.         begin
  78.           nodes[neigh[i]].parent := ti;
  79.           nodes[neigh[i]].g := nodes[ti].g + grid[nodes[neigh[i]].y][nodes[neigh[i]].x];
  80.           nodes[neigh[i]].f := nodes[neigh[i]].g + nodes[neigh[i]].h;
  81.         end;
  82.       end
  83.       else
  84.       begin
  85.         nodes[neigh[i]].parent := ti;
  86.         nodes[neigh[i]].g := nodes[ti].g + grid[nodes[neigh[i]].y][nodes[neigh[i]].x];
  87.         nodes[neigh[i]].f := nodes[neigh[i]].g + nodes[neigh[i]].h;
  88.         nodes[neigh[i]].op := True;
  89.         oh := oh + 1;
  90.         open[oh] := neigh[i];
  91.       end;
  92.     end;
  93.     if (oh = -1) then
  94.       exit;
  95.   until false;
  96.  
  97.   SetLength(path, 0);
  98.   repeat
  99.     SetLength(path, High(path) + 2);
  100.     path[High(path)] := IntToPoint(nodes[ti].x, nodes[ti].y);
  101.     ti := nodes[ti].parent;
  102.   until (nodes[ti].parent = -1);
  103.   SetLength(path, High(path) + 2);
  104.   path[High(path)] := IntToPoint(nodes[ti].x, nodes[ti].y);
  105.   ti := nodes[ti].parent;
  106.   InvertTPA(path);
  107. end;
  108.  
  109. var
  110.   g: array of array of Integer;
  111.   x, y: Integer;
  112.   p: TPointArray;
  113.   s: string;
  114. begin
  115.   {SetLength(g, 10);
  116.   for y := 0 to High(g) do
  117.   begin
  118.     SetLength(g[y], 10);
  119.     s := '';
  120.     for x := 0 to High(g[y]) do
  121.     begin
  122.       //g[y][x] := 1;
  123.       g[y][x] := Random(9) + 1;
  124.       s := s + IntToStr(g[y][x]);
  125.     end;
  126.     Writeln(s);
  127.   end;}
  128.   Writeln('Begin');
  129.   SetLength(g, 20);
  130.    g[0] := [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  131.    g[1] := [0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0];
  132.    g[2] := [0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0];
  133.    g[3] := [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0];
  134.    g[4] := [0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0];
  135.    g[5] := [0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0];
  136.    g[6] := [0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0];
  137.    g[7] := [0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0];
  138.    g[8] := [0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0];
  139.    g[9] := [0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0];
  140.   g[10] := [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0];
  141.   g[11] := [0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0];
  142.   g[12] := [0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0];
  143.   g[13] := [0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0];
  144.   g[14] := [0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0];
  145.   g[15] := [0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0];
  146.   g[16] := [0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0];
  147.   g[17] := [0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0];
  148.   g[18] := [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0];
  149.   g[19] := [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  150.  
  151.   for y := 0 to High(g) do
  152.     for x := 0 to High(g[y]) do
  153.       if (g[y][x] = 0) then
  154.         g[y][x] := 255;
  155.   x := GetSystemTime;
  156.   AStarGrid(p, Point(1, 1), Point(18, 18), g);
  157.   x := GetSystemTime - x;
  158.   Writeln('Took ' + IntToStr(x) + 'ms');
  159.    
  160.   DisplayDebugImgWindow(Length(g[0]) * 10, Length(g) * 10);
  161.   for y := 0 to High(g) do
  162.     for x := 0 to High(g[y]) do
  163.     begin
  164.       GetDebugCanvas.Brush.Color := RGBToColor(g[y][x] * 1, g[y][x] * 1, g[y][x] * 1);
  165.       GetDebugCanvas.Rectangle(x * 10, y * 10, (x + 1) * 10, (y + 1) * 10);
  166.     end;
  167.   GetDebugCanvas.Brush.Color := clRed;
  168.   for y := 0 to High(p) do
  169.   begin
  170.     GetDebugCanvas.Ellipse((p[y].x * 10) + 2, (p[y].y * 10) + 2, (p[y].x * 10) + 8, (p[y].y * 10) + 8);
  171. //    Writeln(IntToStr(p[y].x) + ',' + IntToStr(p[y].y) + ' - ' + IntToStr(g[p[y].y][p[y].x]));
  172.   end;
  173.   Writeln('End');
  174. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement