SAIL - NearestUnitToArea function

Free modding tutorials and source codes.

Moderators: zoNE, Antitheus

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 - NearestUnitToArea function

Post by zoNE »

Morphid wrote: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;
Radzio wrote: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;
Author: Morphid & Radzio
Topic: http://forum.original-war.net/viewtopic.php?f=48&t=2230
Post Reply