SAIL - NearestUnitToArea function

Modding, patches, etc., for anyone who speaks neither Polish nor Czech.
Post Reply
User avatar

Topic author
Morphid
Site Administrator
Site Administrator
Posts: 85
Joined: Sat Dec 25, 2010 12:48 am
Location: Czech Republic
Contact:

SAIL - NearestUnitToArea function

#1

Post by Morphid » Sun Dec 26, 2010 6:32 pm

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

Code: Select all

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;
Last edited by Morphid on Mon Dec 27, 2010 11:57 pm, edited 1 time in total.


Radzio
Site Administrator
Site Administrator
Posts: 2930
Joined: Fri Jul 28, 2006 10:58 am
Location: Bialystok, Poland

Re: SAIL - NearesUnitToArea function

#2

Post by Radzio » Sun Dec 26, 2010 6:47 pm

Hi, this is better:

Code: Select all

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;

User avatar

Topic author
Morphid
Site Administrator
Site Administrator
Posts: 85
Joined: Sat Dec 25, 2010 12:48 am
Location: Czech Republic
Contact:

Re: SAIL - NearesUnitToArea function

#3

Post by Morphid » Sun Dec 26, 2010 6:50 pm

It looks shorter, thanks, Radzio :)


Radzio
Site Administrator
Site Administrator
Posts: 2930
Joined: Fri Jul 28, 2006 10:58 am
Location: Bialystok, Poland

Re: SAIL - NearesUnitToArea function

#4

Post by Radzio » Sun Dec 26, 2010 6:52 pm

Morphid wrote:It looks shorter, thanks, Radzio :)
:)
It's not only shorter, it's more CPU friendly.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest