» Indeks > Modding Archive > SAIL - SortByDistance (sortowanie wg odległości)
SAIL - SortByDistance (sortowanie wg odległości)
Serpent pisze: ↑wt paź 16, 2018 8: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
Kod:
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:
Kod:
Export Function SortByDistanceUnit(unit, list, asc); begin result := SortByDistanceXY(GetX(unit), GetY(unit), list, asc); End;
Author: Serpent
Topic: https://forum.original-war.net/viewtopic.php?f=42&t=5866
» Indeks > Modding Archive > SAIL - SortByDistance (sortowanie wg odległości)



