Advertisement
mixster

mixster

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