SRL Archive - Reflection Help

Finding nearest npc


Lancelot074

2011-07-04 16:38:39

Hey guys, I know how to go about doing this; find all the available npcs and cycle through them to compare which one is closest, but I can't seem to make it work. Any help would be appreciated, thanks.

Smarter Child

2011-07-04 17:23:34

Get all the NPCs loaded into a TNPCArray, then use
Function SortNPCs(NPCs:array of TNPC):array of TNPC;    

You could then use a for..to..do loop with FindNPC to cycle through the NPCs, and find the closest NPC, I think that would work right.

E- Just wondering what is this for? >.<

Lancelot074

2011-07-04 17:45:30

Thanks for the quick reply, after I sort the NPCs, how do I find the closest warrior? I assume it would be something like:

FindNPCs([NpcID], SortedArray)

Would it just find the closest one first?

Smarter Child

2011-07-04 17:49:15

From: Lancelot074
Thanks for the quick reply, after I sort the NPCs, how do I find the closest warrior? I assume it would be something like:

FindNPC(NpcID, SortedArray[0])

because the closest one is stored into the lowest position.


Not exactly sure how the function sorts the NPCs Although, I was assuming you would sort the npcs then for..to..do loop to get FindNPC(ID, TNPC);

Might want to test it ?

By the way, what kind of script is this, because you might be able to this easier.

E- i.e. TileToMM, dist.etc.

Lancelot074

2011-07-04 17:54:30

I modified my post after you read my original one, and the script I am making is an Al-Kharid warrior fighter that banks.

Smarter Child

2011-07-04 17:57:35

okay, umm you could loop the NPCs, then do
Dist := TileToMM(TNPC.tile);
DistanceFrom(Tile(Dist.x, Dist.y));

And then sort the distances, as a failsafe perhaps?

Lancelot074

2011-07-04 17:59:22

Ta := GetNPCs(WarriorID);
if (Length(Ta) > 0) then
begin
SortNPCs(Ta);
for i := 0 to High(Ta) do
begin
if FindNPC(WarriorID, Ta[i]) then
begin

end;
end;
end;

I think I got it, would this work because it would find the first in the array, which is the closest because it was sorted.

Smarter Child

2011-07-04 18:07:02

From: Lancelot074
Ta := GetNPCs(WarriorID);
if (Length(Ta) > 0) then
begin
SortNPCs(Ta);
for i := 0 to High(Ta) do
begin
if FindNPC(WarriorID, Ta[i]) then
begin

end;
end;
end;

I think I got it, would this work because it would find the first in the array, which is the closest because it was sorted.


Yup, that's pretty much what I was going for. :) Test it out though. If it isn't 100% accurate like it should be you can always you the MM as a failsafe ;)

Lancelot074

2011-07-04 18:12:12

Thanks for the help.

cause

2011-07-04 20:27:39

Quick question, how would you use the minimap :)? (Is there a tut for this?)

Smarter Child

2011-07-04 21:28:38

From: cause
Quick question, how would you use the minimap :)? (Is there a tut for this?)


Not really, just use the include. It is pretty self-explanatory :)