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.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      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