Page 1 of 1

More efficient way to detect if theres a player nearby?

Posted: 18.06.2016 9:47
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;

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

Posted: 18.06.2016 12:11
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;

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

Posted: 18.06.2016 18:10
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;