Forum in READ ONLY mode! All questions and discussions on Discord official server, invite link: https://discord.gg/VxsGzJ7

More efficient way to detect if theres a player nearby?

Ask for help
Post Reply
nyce
Neophyte
Neophyte
Posts: 17
Joined: 16.06.2016 19:48

More efficient way to detect if theres a player nearby?

Post by nyce »

I need to detect if theres a player nearby me.

This should be done as fast as possible and the range of the search should be as big as possible as well.

Im using this bool function below. It does the job.

However it takes long to run, i think because of multiple Findtypes and TStringList.Create. Since is crucial for me to react fast when a played is detected, theres a better aproach?

Any improvement will help.

Thanks so much

Originary Code

Code: Select all

function playercheck : Boolean;
var  n : integer; l : TStringList;
begin
  FindDistance := 40;
  l := TStringList.Create();
  Result := False;
  FindType($190,ground); //m_human
  GetFindedList(l);
  if (l.count > 0) then
  begin
    Result := true;
    ClientPrint(l[0]);
  end;
  l.free;
  l := TStringList.Create();
  FindType($191,ground); //f_human
  GetFindedList(l);
  if (l.count > 1) then // 1 cause my char is f
  begin
    Result := true;
    ClientPrint(l[0]);
  end;
  l.free;
  l := TStringList.Create();
  FindType($25E,ground); //f_elf
  GetFindedList(l);
  if (l.count > 0) then
  begin
    Result := true;
    ClientPrint(l[0]);
  end;
  l.free;
  l := TStringList.Create();
  FindType($25D,ground); //m_elf
  GetFindedList(l);
  if (l.count > 0) then
  begin
    Result := true;
    ClientPrint(l[0]);
  end;
  l.free;
end;
My improvement try:

Code: Select all

function playercheck : Boolean;
var  n : integer; l : TStringList;
begin
  FindDistance := 40;
  l := TStringList.Create();
  Result := False;
  FindType($190,ground); //m_human
  GetFindedList(l);
  if (l.count > 0) then
  begin
    Result := true;
    ClientPrint(l[0]);
    l.free;
    exit;
  end
  else begin l.free; end;
  l := TStringList.Create();
  FindType($191,ground); //f_human
  GetFindedList(l);
  if (l.count > 1) then // 1 cause my char is f
  begin
    Result := true;
    ClientPrint(l[0]);
    l.free;
    exit;
  end
  else begin l.free; end;
  l := TStringList.Create();
  FindType($25E,ground); //f_elf
  GetFindedList(l);
  if (l.count > 0) then
  begin
    Result := true;
    ClientPrint(l[0]);
    l.free;
    exit;
  end
  else begin l.free; end;
  l := TStringList.Create();
  FindType($25D,ground); //m_elf
  GetFindedList(l);
  if (l.count > 0) then
  begin
    Result := true;
    ClientPrint(l[0]);
    l.free;
    exit;
  end
  else begin l.free; end;
end;
User avatar
Vizit0r
Developer
Developer
Posts: 3958
Joined: 24.03.2005 17:05
Contact:

Re: More efficient way to detect if theres a player nearby?

Post by Vizit0r »

1) Optimization#1

Code: Select all

l := TStringList.Create();
  FindType($191,ground); //f_human
  GetFindedList(l);
  if (l.count > 1) then // 1 cause my char is f
  begin
    Result := true;
    ClientPrint(l[0]);
    l.free;
    exit;
  end
  else begin l.free; end;
uuf.

http://stealth.od.ua/Category:Object_Search

Code: Select all

  FindType($191,ground); //f_human
if fincount > 0 then
  begin
    Result := true;
    ClientPrint('$' + IntToHex(FindItem,8);
    exit;
  end
2) Optimization#2


Use the Force, Luke!
http://stealth.od.ua/Doc:Api/FindTypesArrayEx

Code: Select all

function playercheck : Boolean;
begin
  Result := FindTypesArrayEx([$190, $191, $25E, $25D],[$FFFF],[Ground],false) > 0;
  If Result then
    ClientPrint('$' + IntToHex(FindItem,8);
end;
"Пишите код так, как будто сопровождать его будет склонный к насилию психопат, который знает, где вы живете". (с) Макконнелл, "Совершенный код".
nyce
Neophyte
Neophyte
Posts: 17
Joined: 16.06.2016 19:48

Re: More efficient way to detect if theres a player nearby?

Post by nyce »

Solved! I would like to thank Vizit0r for the tremendous support. Hes a code magician who can transform 40 lines in 4 :)

Code: Select all

function playercheck : Boolean;
begin
  FindDistance := 40;
  Ignore(SelfId);
  Result := FindTypesArrayEx([$190, $191, $25E, $25D],[$FFFF],[Ground],false) > 0;
  IgnoreOff(SelfId);
  FindDistance := 2; //default
end;
Post Reply