SAIL - NearestUnitToArea function

Modding, patches, etc., for anyone who speaks neither Polish nor Czech.
Odpovědět
Uživatelský avatar
Morphid
Site Administrator
Site Administrator
Czech Republic
Příspěvky: 86
Registrován: sob pro 25, 2010 12:48 am
Bydliště: Czech Republic
Kontaktovat uživatele:

SAIL - NearestUnitToArea function

Příspěvek od Morphid »

Hi, I have prepared a SAIL function to find a unit who is nearest to the area.
Using: NearestUnitToArea(units:plist, area:integer)

Kód: Vybrat vše

export function NearestUnitToArea(list, area);
var i, n;
begin
     n:=[];
     for i:=1 to list do
     begin
          if not (n = 0) then
          begin
               if n > GetDistUnitArea(list[i], area) then
                  n:=GetDistUnitArea(list[i], area);
               end
          else begin
               n:=GetDistUnitArea(list[i], area);
          end;
     end;
     for i:=1 to list do
     begin
          if GetDistUnitArea(list[i], area) = n then
          begin
               result:=list[i];
               break;
          end;
     end;
end;
Naposledy upravil(a) Morphid dne pon pro 27, 2010 11:57 pm, celkem upraveno 1 x.
Radzio
Site Administrator
Site Administrator
Poland
Příspěvky: 2898
Registrován: pát črc 28, 2006 10:58 am
Bydliště: Bialystok, Poland

Re: SAIL - NearesUnitToArea function

Příspěvek od Radzio »

Hi, this is better:

Kód: Vybrat vše

export function NearestUnitToArea(list, area);
var un, n;
begin
  n:= 999999;
  for un in list do
    begin
      if GetDistUnitArea(un, area) < n then
        begin
          n:= GetDistUnitArea(un, area);
          result:= un;
        end;
      if (n = 0) then exit;
    end;
end;
Uživatelský avatar
Morphid
Site Administrator
Site Administrator
Czech Republic
Příspěvky: 86
Registrován: sob pro 25, 2010 12:48 am
Bydliště: Czech Republic
Kontaktovat uživatele:

Re: SAIL - NearesUnitToArea function

Příspěvek od Morphid »

It looks shorter, thanks, Radzio :)
Radzio
Site Administrator
Site Administrator
Poland
Příspěvky: 2898
Registrován: pát črc 28, 2006 10:58 am
Bydliště: Bialystok, Poland

Re: SAIL - NearesUnitToArea function

Příspěvek od Radzio »

Morphid píše:It looks shorter, thanks, Radzio :)
:)
It's not only shorter, it's more CPU friendly.
Odpovědět