SAIL - SortByDistance (sortowanie wg. odległości)

Gotowe kody SAIL'a, informacje na temat moddingu, itd.

Moderator: zoNE

Post Reply
User avatar
zoNE
The Great Uniter & Site Administrator
The Great Uniter & Site Administrator
Posts: 2059
Joined: Fri Feb 17, 2006 3:44 pm
Location: Poland
Contact:

SAIL - SortByDistance (sortowanie wg. odległości)

Post by zoNE »

Serpent wrote: Tue Oct 16, 2018 7:52 pm Funkcja sortująca jednostki z listy wg. odległości od wskazanego hexa (x,y).

Zwraca tablicę w postaci:
[ [jednostka, odległość], [jednostka, odległość], ...]

Parametry:
  • x,y - koordynaty
  • list - lista jednostek
  • asc - tryb sortowania: true -> rosnąco, false -> malejąco

Code: Select all

Export Function SortByDistanceXY(x, y, list, asc);
var i, j, tmp;
begin
if not list then
   exit;

result := [];

for i in list do
    begin
    tmp := GetDistUnitXY(i, x, y);

    if not result then
       result := [[i, tmp]]
     else
      begin
       if result[result][2] < tmp then
          result := Insert(result, result+1, [i, tmp])
       else
       for j = 1 to result do
           begin
           if tmp < result[j][2] then
              begin
              result := Insert(result, j, [i, tmp]);
              break;
              end;
           end;
      end;
    end;

if result and not asc then
   begin
   tmp := result;

   for i = tmp downto 1 do
       result := Replace(result, tmp - i + 1, tmp[i]);
   end;
End;
Z tej funkcji wyprowadzić można sortowanie dla pozycji konkretnej jednostki:

Code: Select all

Export Function SortByDistanceUnit(unit, list, asc);
begin
result := SortByDistanceXY(GetX(unit), GetY(unit), list, asc);
End;
Author: Serpent
Topic: https://forum.original-war.net/viewtopi ... =42&t=5866
Post Reply