ScriptUO

Scripting Resources & Utilities => Stealth Client => Stealth archive => Topic started by: Th3Ma5hatt3r on March 15, 2014, 11:38:47 AM

Title: Python not returning the right object?
Post by: Th3Ma5hatt3r on March 15, 2014, 11:38:47 AM
I'm using a simple function to try to see if there are any players close by. It should return true at the closest player and it works for the most part. If I run the code below it will find the closest player to me than if I move closer to a different player, but the last found player is still visible. It will return the last found player every time and not the closest player, until the last found player leaves or is to far away. Does anyone have any idea why this might be happening?

Code: [Select]
# This is a list of friendly player types.
Friendly_Player_Types = [0x0190, 0x0191, 0x02EC, 0x02EB, 0x007B]

def ScanForPlayer(ScanDistance):
SetFindDistance(ScanDistance)
FindTypeEx(0xFFFF, 0xFFFF, Ground(), False)
PossiblePlayers = GetFindedList()
#PossiblePlayers.reverse()

for player in PossiblePlayers:
playerType = GetType(player)
playerNotoriety = GetNotoriety(player)
 
print('Found Object - Name: ['+GetName(player)+'] Type: ['+str(playerType)+'] Notoriety: ['+str(playerNotoriety)+'].')
if not(IsNPC(player)):
continue
elif playerType not in Friendly_Player_Types:
continue
elif player == Self():
continue
elif playerNotoriety != 1:
continue
else:
print('Found nearby Player: ['+GetName(player)+'].')
return True
return False

Timer = GetTickCount()
foundPlayer = ScanForPlayer(10)
print('Search Time Took: ['+str(GetTickCount() - Timer)+'] in milliseconds.')
if foundPlayer:
print('Found a player.')
else:
print("Didn't find a player.")

Thanks for taking the time to read my post.
Title: Re: Python not returning the right object?
Post by: dxrom on March 15, 2014, 12:43:22 PM
Shouldn't you be stepping through the array of Friendly_Player_Types in order to find each type? I'm unsure how python works, however it looks to me like if there were a player type 0x0190 and then another player type 0x02EB, the player id of type found 0x0190 would be returned, however the player id of type 0x02EB would not be returned because the FindTypeEx statement doesn't step past the first type (0x0190). Also GetFindedList() calls a list of the previous FindTypeEx, aka whatever has shown up for the call FindTypeEx(0xFFFF, 0xFFFF, Ground(), False), so again I believe you need to step through your array of Friendly_Player_Types in order to populate an accurate list.

I could be completely wrong though, I'm bad at programming :X
Title: Re: Python not returning the right object?
Post by: slyone on March 16, 2014, 07:24:16 PM
I'm using a simple function to try to see if there are any players close by. It should return true at the closest player and it works for the most part. If I run the code below it will find the closest player to me than if I move closer to a different player, but the last found player is still visible. It will return the last found player every time and not the closest player, until the last found player leaves or is to far away. Does anyone have any idea why this might be happening?


I tested out your code a bit and added the following after your "Found object" print statement:

Code: [Select]
print( GetX(player), GetY(player), GetZ(player) )
Adding that line just prints the location of each of the objects found.  For me it looks like the function
Code: [Select]
FindTypeEx doesn't return a list sorted closest to farthest.

Let me know if I'm mis-reading something.

-S
Title: Re: Python not returning the right object?
Post by: Th3Ma5hatt3r on March 16, 2014, 09:13:33 PM
I'm using a simple function to try to see if there are any players close by. It should return true at the closest player and it works for the most part. If I run the code below it will find the closest player to me than if I move closer to a different player, but the last found player is still visible. It will return the last found player every time and not the closest player, until the last found player leaves or is to far away. Does anyone have any idea why this might be happening?


I tested out your code a bit and added the following after your "Found object" print statement:

Code: [Select]
print( GetX(player), GetY(player), GetZ(player) )
Adding that line just prints the location of each of the objects found.  For me it looks like the function
Code: [Select]
FindTypeEx doesn't return a list sorted closest to farthest.

Let me know if I'm mis-reading something.

-S

Yes I am starting to think that the list is not sorted either. Currently doing more work on that function. I will let you know what I figure out.
Title: Re: Python not returning the right object?
Post by: Boydon on March 21, 2014, 01:41:49 AM
You can sort it by yourself using Python sorted (https://wiki.python.org/moin/HowTo/Sorting#Key_Functions):

Code: [Select]
PossiblePlayers = GetFindedList()
SortedPlayers = sorted(PossiblePlayers, key=FindDistance)