• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] Pick players from PlayerGroup

Status
Not open for further replies.
Wasted almost half a day and didn't find simple solution.:psick:
Map: AoS type, 2 teams TeamA, TeamB, up to 5 players per team (can be 4vs4 or less)
player can be switched to other team - to make teams equal quantity
there are 2 players groups (updated when player left or switched)
there's integer array that represents how many times each players fight on Arena:
udg_ArenaCount[gui playerNumber]

I need a function(s) that allow to:
pick x players from TeamA and add them to player group BattleTeamA (the same with B team)
*and*
those picked players should have lowest values of udg_ArenaCount[playerNumber]

//--------------------------
example: there's 4 players in team A, with following udg_ArenaCount:
udg_ArenaCount[1] = 0
udg_ArenaCount[3] = 2
udg_ArenaCount[4] = 2
udg_ArenaCount[8] = 1

and I need, lets say, 3 players to be picked,
so this should be: player1, player8, and (player3 or player4)
cause player1 has ArenaCount=0 /didnt fight arena yet/
player8 = 1
and one of 2 remaning players: 3 or 4

zibi
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,287
player can be switched to other team - to make teams equal quantity
Which is bad if they joined as a team to play as a team. Why else do you think such functionality is never in AoS games like Heroes of the Storm?

Anyway the solution is pretty simple. You create a search for the "best match" player (simple linear search algorthim), remove them from the player group (possibly copying the group at the start to prevent data loss) and repeat however many times you need players or until the group is empty.

A linear search is performed with a variable to store the current best result and a variable to cache his performance metric. The performance metric is deterministic at that point in time with the player so that if the algorithm was immediately re-run on the player it would return the same performance metric value. For each element in the set (player group) you then compute the performance metric, if it is superior to the currently cached performance metric you then set the current best result to that player and the currently cached performance metric to the calculated value. Once all players are tested then you have your best result in the variable for storing the current best result. The best performance metric cache is technically not needed since one could always re-compute the metric from the best result variable however it does improve efficiency in the case that the performance metric is non-trivial to compute.

In your case the performance metric could be the negative of udg_ArenaCount value for the player. This logically can be viewed as searching for the worst performance in positive udg_ArenaCount.

In theory the algorithm could be improved to return a best performance set rather than a single element by extending the current best to be a list of results rather than a single element. Better performance then displaces worse performance from the list and eventually after all elements are visited the list is returned. However at this stage the implementation would start to look like and be classed as a sorting algorithm (where you take one end of the sorted set and discard the rest).
 
thanks very much for your answer DSG ;]
player can be switched to other team - to make teams equal quantity
Which is bad if they joined as a team to play as a team.
They have dialog to choose player to switch and there's button to close "switch system".

I added 2 global integer vars
udg_ArenaSearch and udg_PickedPlayerId

can you pls take a look at this code and say your opinion
JASS:
function AddToTempGroup1 takes nothing returns nothing
    call ForceAddPlayer(udg_TempPlayerGroup, GetEnumPlayer())
endfunction
//--------------------------------------------------------------------------------------
function ComputeForArena takes nothing returns nothing
    local integer id = GetPlayerId(GetEnumPlayer())+1
    if udg_ArenaSearch > udg_ArenaCount[id] then        
        set udg_ArenaSearch = udg_ArenaCount[id]
        set udg_PickedPlayerId = id //gui
    endif
endfunction
//--------------------------------------------------------------------------------------
function SetXPlayersAlliance takes integer howManyPlayers returns nothing
    local integer a=0
    call ForceClear(udg_TempPlayerGroup)
    call ForForce(udg_PlayerGroupAlliance, function AddToTempGroup1)
    
    loop
    exitwhen a==howManyPlayers
    
        set udg_ArenaSearch = 9999
        call ForForce(udg_TempPlayerGroup, function ComputeForArena)  //search for player
        call ForceAddPlayer(udg_BattleAlliance, Player(udg_PickedPlayerId-1))
        call ForceRemovePlayer(udg_TempPlayerGroup, Player(udg_PickedPlayerId-1))
        
    set a=a+1
    endloop
            
    call ForceClear(udg_TempPlayerGroup)
endfunction
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
I can not stop thinking about post 2 and 6, when you guys say force is it a reference to his avatar or jass variable type.
 
Level 24
Joined
Aug 1, 2013
Messages
4,658
JASS:
function SetXPlayersAlliance takes integer howManyPlayers returns nothing
    local integer a=0
    call ForceClear(udg_TempPlayerGroup)
    call ForForce(udg_PlayerGroupAlliance, function AddToTempGroup1)
    
    loop
    exitwhen a==howManyPlayers
    
        set udg_ArenaSearch = 9999
        call ForForce(udg_TempPlayerGroup, function ComputeForArena)  //search for player
        call ForceAddPlayer(udg_BattleAlliance, Player(udg_PickedPlayerId-1))
        call ForceRemovePlayer(udg_TempPlayerGroup, Player(udg_PickedPlayerId-1))
        
    set a=a+1
    endloop
            
    call ForceClear(udg_TempPlayerGroup)
endfunction
The force is strong in this JASS code.
 
Status
Not open for further replies.
Top