Snippet GetTrueRandomPlayer

black.sheep

Active Member
Reaction score
24
Simple snippet for getting a random player (seeing as the blizz one is utter fail) that is filled by a human player.
Don't bother crediting, change at your own free will.
JASS:
function GetTrueRandomPlayerCheck takes nothing returns boolean
    return ( GetPlayerController(GetFilterPlayer()) != MAP_CONTROL_COMPUTER and GetPlayerController(GetFilterPlayer()) != MAP_CONTROL_NONE )
endfunction

function GetTrueRandomPlayer takes nothing returns player
   local force players
   local player p
   local integer i
   set players = GetPlayersMatching(Condition(function GetTrueRandomPlayerCheck))
    loop
       set i =GetRandomInt(0,11)
       set p =ConvertedPlayer(i)
         exitwhen IsPlayerInForce(p,players) == true
    endloop
    call DestroyForce(players)
    set players = null
    return p
endfunction
 
Reaction score
91
I was wondering how your map didn't crash when you tested it...

Here's a better one, though it might not be the best:
JASS:

function GetTrueRandomPlayerCheck takes nothing returns boolean
    return GetPlayerController(GetFilterPlayer()) == MAP_CONTROL_USER
endfunction

function GetTrueRandomPlayer takes nothing returns player
    local force players = CreateForce()
    local player p
    local integer i
    call ForceEnumPlayers(players, Condition(function GetTrueRandomPlayerCheck))
    loop
        set i = GetRandomInt(0, 11)
        set p = Player(i)
        exitwhen IsPlayerInForce(p, players)
    endloop
    call DestroyForce(players)
    set players = null
    return p
endfunction
 

black.sheep

Active Member
Reaction score
24
Heh, well I didn't expect it to fail so hard.
I did remake this with GUI, same basic principle, run loop until random integer = player number of player in group, it works fine, now i just gotta remake it in jass.
 

the_ideal

user title
Reaction score
61
Two questions:
What is bad about random player-- are there patterns?

Why not just use Player Index - [random number from 1-12]?
 

cleeezzz

The Undead Ranger.
Reaction score
268
>Why not just use Player Index - [random number from 1-12]?

1 reason, its not flexible, lets say you didn't want player 6 to be selected.

>What is bad about random player-- are there patterns?

im guessing because, the function itself is a BJ, and also because, it calls another BJ.
 

black.sheep

Active Member
Reaction score
24
Acturally random player is rather horrible, in my map, it seemed to like getting red 100% of time there was a person in it.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Here's a better verison I think.

JASS:
function PickRandomPlayer takes integer start, integer end returns player
    local integer r = GetRandomInt(start,end)
    return Player(r)
endfunction

The whole point of his was to only get human players. Getting a random player from All Players wasn't the problem.

However, the original function does have a problem: it has the potential to hit the OP Limit. Although it's improbably that the random player does not match the conditions that many times, it can still cause pauses/lag spikes in-game if it takes a while to come to a valid player.

Here's a more consistent method, that allows you to choose your own filter:

JASS:
function GetRandomPlayerMatching takes boolexpr matching returns player
    local force players = CreateForce()
    local player this
    local player array these
    local integer i = 0
    call ForceEnumPlayers(players,matching)
    loop
        set this = FirstOfGroup(players)
        exitwhen this = null
        set these<i> = this
        set i = i + 1
        call ForceRemovePlayer(players,this)
    endloop
    call DestroyForce(players)
    set players = null
    return these[GetRandomInt(0,i-1)]
endfunction</i>
 

Jesus4Lyf

Good Idea™
Reaction score
397
I still think all of these are garbage until forces are removed. Have a global array, enumerate it with valid player numbers when the function is called, then return Player(Array[RandomInt(1,max)]). Do the enumeration by looping from 0 to 11. NO FORCES!

>that is filled by a human player.
Not based on a filter or from a force. Just a random human player (and I'd also say make sure they're still in the game).

Anyway, the author has seen what was being posted here and still hasn't updated and improved the snippet. It is poor quality as it is O(n) complexity unnecessarily. Worse yet the original still leaks. I personally vote for graveyarding this thread.
 

black.sheep

Active Member
Reaction score
24
Anyway, the author has seen what was being posted here and still hasn't updated and improved the snippet. It is poor quality as it is O(n) complexity unnecessarily. Worse yet the original still leaks. I personally vote for graveyarding this thread.
Heh, agreed. Although it does have other snippets that could work. Oh, and I tried that global integer arrary before I made this snippet, but it seemed to crash for me. Oh well, i probally made some mistake, somewhere.
 

Jesus4Lyf

Good Idea™
Reaction score
397
Well, before this knocks off, here's to a good GetRandomPlayingPlayer function (actually untested, but should work perfectly :)):

JASS:
globals
    player array RandomPlayerArray // Just for temp storage stuff
endglobals
function GetTrueRandomPlayer takes nothing returns player
    local integer max=0
    local integer n=11
    local player p
    loop
        set p=Player(n)
        if GetPlayerSlotState(p)==PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p)==MAP_CONTROL_USER then
            set max=max+1
            set RandomPlayerArray[max]=p
        endif
        exitwhen n==0
        set n=n-1
    endloop
    return RandomPlayerArray[GetRandomInt(1,max)]
endfunction

No forces, gg. ;)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top