Function That Detects Players In District

03/20/2024 00:42 albino albatross#1
To anyone interested, I wrote this function which detects the presence of other players in the district. You can use it to stop the bot from continuing while other players are in the district.

Code:
; ; Checks if there are other players present in the vicinity of the player and logs their names.
Func AreOtherPlayersPresent()
    Local $myLoginNumber = DllStructGetData(GetAgentByID(-2), 'LoginNumber') ; Your character's login number
    Local $playerNames = "" ; Initialize an empty string to store player names
    Local $playerFound = False ; Flag to indicate if any player was found

    For $i = 1 To GetMaxAgents() ; Iterates through all possible agent IDs
        Local $agent = GetAgentByID($i)
        If IsDllStruct($agent) Then ; Checks if $agent is a valid structure
            Local $loginNumber = DllStructGetData($agent, 'LoginNumber')
            If $loginNumber <> 0 And $loginNumber <> $myLoginNumber Then ; Checks if the agent is a player and not you
                Local $playerName = GetPlayerName($agent)
                ; Adjust the regex to allow spaces in player names.
                If $playerName <> "" And StringRegExp($playerName, "^[a-zA-Z0-9 ]+$") Then ; Checks if the name is valid
                    $playerFound = True
                    $playerNames &= $playerName & [MENTION=3576271]CRLF[/MENTION] ; Append the player name to the list
                EndIf
            EndIf
        EndIf
    Next

    If $playerFound Then
        Logger("Players detected in the district: " & $playerNames) ; Log all detected player names
        Return True ; Returns true as players were found
    Else
        Logger("No Other Players Found") ; Log when no players are found
        Return False ; Returns false as no players were found
    EndIf
EndFunc
Example usage (put this in your main loop):

Code:
    ; Wait until the district is empty of other players.
    While AreOtherPlayersPresent()
        Logger("Other players detected. Waiting 5s")
        Sleep(10000) ; Wait for 10 seconds before checking again.
    WEnd
05/01/2024 12:54 ATbs#2
Hello! Thanks for the contri.

How did you start? Im good at changing stuff but not from 0. Like there's so much and I don't think is hard at all somehow feel like I have to hit the right spot with the info I look for as I eventually want to write my own.
05/04/2024 02:21 albino albatross#3
Well I started by going through the gwa2 library to check out what kinds of functions and other things were available to use. Then I modified a bunch of pre-existing scripts to include utilities I desired. At one point I decided it would be useful to be able to detect players in the district, this seemed like a decent solution. I've noticed every once in a while it'll detect someone when there's nobody there, but it happens very rarely, and I'm still not sure of the cause.
05/04/2024 05:59 ATbs#4
Fair enough, thanks!
And to change districts? If players are there
05/22/2024 19:07 Scythe X#5
I am utilizing this in my Vaettir bot, and i just wanted to post a verbal thank you. I placed Credits for you in my readme file. I will be using this for my current map-running bot project. If you want this removed at any time just let me know please. Thank you again!
05/25/2024 04:53 albino albatross#6
Thank you Scythe X for the kind words. Please go ahead and use it, that's why I posted it!

At ATbs, for changing districts and the like, you'll have to add that functionality yourself for now. Personally, I make my script run outside and wait if it detects someone in the district.
06/01/2024 03:29 ATbs#7
Yup, made it work with everything i wanted!