Swap weapons based on enemies count

For our lovely destroyers! Configured the character to equip a polearm when more than one mob (alive) is found within a radius of X (200 in this example). If there is one or none, the character will use the double-handed sword.

var
  Polearm: TL2Item;
  PolearmID: Integer;
  
  DHSword: TL2Item;
  DHSwordID: Integer;
  
  MobRadius: Integer;

function GetNearestMobsInRadius(dist: integer): integer;  
var 
  i: integer;
begin
  Result := 0;
  for i := 0 to NpcList.Count-1 do begin    
    if NpcList(i).Valid and (User.DistTo(NpcList(i)) < dist) and not NpcList(i).Dead then begin     
      Result := Result + 1;                  
    end;
  end;
end;

begin
  PolearmID := 2369;
  DHSwordID := 265;
  MobRadius := 200;

  while delay(555) do begin
    if (GetNearestMobsInRadius(MobRadius) > 1) and Inventory.User.ByID(PolearmID, Polearm) and not Polearm.Equipped then begin
      Engine.UseItem(Polearm);
    end else begin
      if (GetNearestMobsInRadius(MobRadius) <= 1) and Inventory.User.ByID(DHSwordID, DHSword) and not DHSword.Equipped then begin
        Engine.UseItem(DHSword);
      end;
    end;
  end;
end.

All you need to do is specify the following three lines:

PolearmID := 2369;
DHSwordID := 265;
MobRadius := 200;

Author: Velmsun