SAIL - NearestUnitToArea function

Free modding tutorials and source codes.

Moderators: zoNE, Antitheus

Post Reply
User avatar

Topic author
zoNE
The Great Uniter & Site Administrator
The Great Uniter & Site Administrator
Posts: 1436
Joined: Fri Feb 17, 2006 3:44 pm
Location: Poland
Contact:

SAIL - NearestUnitToArea function

#1

Post by zoNE » Sun Jun 14, 2015 10:10 pm

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
PC MB: MSI Z97-G43 | CPU: Intel Xeon E3-1231v3 3.4GHz + Raijintek EreBoss | RAM: HyperX 2x8GB 1866MHz DDR3 CL9 | GPU: MSI GeForce GTX 970 100ME 4GB GDDR5 (256bit) | SSD: Crucial BX100 250GB | HDD: 4TB | LCD: EIZO FlexScan 22" S2243W Black | AUDIO: Asus Xonar D2 + KODA DRA-660 + KODA AV-701 + SW-550 v2 + Sennheiser HD 595 | OS: Windows 10 Pro
Laptop DELL Precision M6500 CPU: i5-520M RAM: 8GB DDR3 SSD: 128GB HDD: 500GB GPU: ATI FirePro M7740 1GB DDR5 OS: Windows 7 Ultimate 64-bit

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests