Summary :

Finds closest NPC depending on what id you search for and then right click attack it.


Code:
type
TA = Array[0..100] of integer;

procedure interactWithMonster(Tile : TPoint; option : String);
var
  punt : TPoint;
  playerPos : TPoint;
begin
    playerPos := R_getTileGlobal;
    punt := R_TileToMS(Point(Tile.x, Tile.y));
    if (punt.x <= 0) then
      Exit;
    mouse( punt.x,  punt.y, 3, 3, false);
    if (ChooseOptionMulti([option])) then
    begin
      wait(RandomRange(2000, 3000));
    end else
    wait(Randomrange(100, 500));
end;


function sortFunction(A : TIntegerArray) : Integer;
var
  j, Index, k, temp : integer;
begin
  for j := 1 to high(A) do
  begin
    Index := j;
    for k := (j + 1) to high(A) do
      if A[k] < A[Index] then
        Index := k;
      Temp := A[Index];
      A[Index] := A[j];
      A[j] := Temp
  end;
  Result := A[0];
end;

procedure R_FindClosestNpc(monsters : TNPCArray; npcId : Integer);
var
  i : integer;
  npcSpots : TA;
  npcResult : integer;
begin
  for i := 0 to high(monsters) do
  begin
    if (monsters[i].NpcID = npcId) then
      npcSpots[i] := R_DistanceFromTile(monsters[i].tile);
  end;
  npcResult := sortFunction(npcSpots);
  for i := 0 to high(monsters) do
  begin
    if (R_DistanceFromTile(monsters[i].tile) = npcResult) then
    begin
      interactWithMonster(monsters[i].tile, 'Attack');
    end;
  end;
end;

USAGE

Code:
R_FindClosestNpc(R_GetAllNpcs, 3185);