Snippet Players

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:
////////////////////////////////////////////
//
//      PLAYERS v1.0
//          by kingking
//
//  This library sorts types of players
//  in map.
//  
//  Functions :
//  Players_IsHuman(whichPlayer) -> boolean
//  Players_IsComputer(whichPlayer) -> boolean
//  Players_IsEmpty(whichPlayer) -> boolean
//  Players_IsPlaying(whichPlayer) -> boolean
//  Players_IsLeft(whichPlayer) -> boolean
//
//  ForHumanPlayers(whichCallback)
//  ForComputerPlayers(whichCallback)
//  ForEmptyPlayers(whichCallback)
//  ForPlayingPlayers(whichCallback)
//  ForLeftPlayers(whichCallback)
//
//  Players[whichPlayerId] -> player
//
//  TriggerRegisterHumanPlayersLeaveEvent(whichTrigger)
//
//  Requires :
//  Jasshelper 0.A.2.7
//  Event
/////////////////////////////////////////
library Players initializer Init requires Event

    globals
        player array Players
        private force HumanPlayers
        private force ComputerPlayers
        private force EmptyPlayers
        private force PlayingPlayers
        private force LeftPlayers
        private Event HumanPlayersLeaveEvent
    endglobals
    
    //! textmacro PLAYERS takes TYPE
    public function Is$TYPE$ takes player whichPlayer returns boolean
        return IsPlayerInForce(whichPlayer,$TYPE$Players)
    endfunction
    function For$TYPE$Players takes code whichCallback returns nothing
        call ForForce($TYPE$Players, whichCallback)
    endfunction
    //! endtextmacro
    
    //! runtextmacro PLAYERS ("Human")
    //! runtextmacro PLAYERS ("Computer")
    //! runtextmacro PLAYERS ("Empty")
    //! runtextmacro PLAYERS ("Playing")
    //! runtextmacro PLAYERS ("Left")
    
    function TriggerRegisterHumanPlayerLeaveEvent takes trigger whichTrigger returns EventReg
        return HumanPlayersLeaveEvent.register(whichTrigger)
    endfunction
     
    private function FireEvent takes nothing returns boolean
        local player p = GetTriggerPlayer()
        call ForceRemovePlayer(HumanPlayers,p)
        call ForceRemovePlayer(PlayingPlayers,p)
        call ForceAddPlayer(EmptyPlayers,p)
        call ForceAddPlayer(LeftPlayers,p)
        call HumanPlayersLeaveEvent.fire()
        set p = null
        return false
    endfunction
    
    private function Init takes nothing returns nothing
        local integer i = 0
        local trigger trig = CreateTrigger()
        set HumanPlayersLeaveEvent = Event.create()
        
        set HumanPlayers = CreateForce()
        set ComputerPlayers = CreateForce()
        set EmptyPlayers = CreateForce()
        set LeftPlayers = CreateForce()
        set PlayingPlayers = CreateForce()        

        loop
        exitwhen i > bj_MAX_PLAYER_SLOTS
            set Players<i> = Player(i)
            if GetPlayerController(Players<i>) == MAP_CONTROL_USER then
                call ForceAddPlayer(HumanPlayers,Players<i>)
            endif
            if GetPlayerController(Players<i>) == MAP_CONTROL_COMPUTER then
                call ForceAddPlayer(ComputerPlayers,Players<i>)
            endif
            if GetPlayerController(Players<i>) == MAP_CONTROL_NONE then
                call ForceAddPlayer(EmptyPlayers,Players<i>)
            endif
            if GetPlayerSlotState(Players<i>) == PLAYER_SLOT_STATE_PLAYING then
                call ForceAddPlayer(PlayingPlayers,Players<i>)
            endif
            if GetPlayerSlotState(Players<i>) == PLAYER_SLOT_STATE_EMPTY then
                call ForceAddPlayer(EmptyPlayers,Players<i>)
            endif
            if IsPlayerInForce(Players<i>,HumanPlayers) and IsPlayerInForce(Players<i>,PlayingPlayers) then
                call TriggerRegisterPlayerEvent(trig,Players<i>,EVENT_PLAYER_LEAVE)
            endif
            set i = i + 1
        endloop
    endfunction
endlibrary
</i></i></i></i></i></i></i></i></i></i></i></i></i></i>
 

Nestharus

o-o
Reaction score
84
This has already been done, and it's been done better, and the done version is graveyarded on this site because moderators say that player tracking is too simple to constitute as a resource.

Host tracking was also done, so don't try to do that ^_- It's rotting in tutorials and resources ;p (not approved or graveyarded)

Although Player Tracking that's been done doesn't track players that are not humans or computers (100% nonplaying), lol.

But a feature was planned last night that would allow for tracking of active and non-active players in the game ^_^ (human players that are in the game, but are actually playing or not playing at the moment).

http://www.thehelper.net/forums/showthread.php?t=135783 (player tracking)
http://www.thehelper.net/forums/showthread.php?t=137438 (host tracking)

But anyways, a few pointers-
JASS:
if GetPlayerController(Players<i>) == MAP_CONTROL_USER then
                call ForceAddPlayer(HumanPlayers,Players<i>)
            endif
            if GetPlayerController(Players<i>) == MAP_CONTROL_COMPUTER then
                call ForceAddPlayer(ComputerPlayers,Players<i>)
            endif
            if GetPlayerController(Players<i>) == MAP_CONTROL_NONE then
                call ForceAddPlayer(EmptyPlayers,Players<i>)
            endif
            if GetPlayerSlotState(Players<i>) == PLAYER_SLOT_STATE_PLAYING then
                call ForceAddPlayer(PlayingPlayers,Players<i>)
            endif
            if GetPlayerSlotState(Players<i>) == PLAYER_SLOT_STATE_EMPTY then
                call ForceAddPlayer(EmptyPlayers,Players<i>)
            endif
</i></i></i></i></i></i></i></i></i></i>


Don't use a set of ifs-
if, else if, else

if player is map controller
else if player is computer
else (obviously neither)

if player slot state is playing
else

Next, don't use player slot state playing. Look under player tracking to see a better method that's faster.

Next, don't use forces. Iterating through players plainly in code is faster than going through a force -.-. Use an indexed array (not a stack or a linked list as there is really no need).

The indexed array should return either player or player id. While you're at it, might as well do a player array. Just look at player tracking for inspiration.

Also, active players and non-active players for game play is a good idea as a feature as it allows people to more easily determine when a game ends and more easily iterate through players. If they want to iterate through all players for something, they can. If they only want to do active players (players that aren't defeated or that are in a match or something), they can. If they only want to do non-active (are they observing?) they can.

A use of active/non-active-
loop
exitwhen activePlayer is not ally to activePlayer2
if i == PlayerCount
endGame


The first active player will always be full. All active players can be cycled through with win and all non-active can be cycled through with loss or w/e ^_^.

Oh yes, and including a boolean as a default (players auto active or auto deactive) would be a good idea ;).

I think you should just leave Player Tracking up to the player tracking utility ;p.
 

Jesus4Lyf

Good Idea™
Reaction score
397
Yea, I still don't think this kind of thing is that useful...

>For$TYPE$Players
I'd like your ForXPlayers functions if it wasn't better/easier to use a loop through integers...

I mean, it's obscure, right? You've covered some random things, but what if people want PlayingHumanPlayers, or PlayingComputerPlayers, or DefeatedComputerPlayersWhoWerePlaying, or...
Just use a loop. :)
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
I mean, it's obscure, right? You've covered some random things, but what if people want PlayingHumanPlayers, or PlayingComputerPlayers, or DefeatedComputerPlayersWhoWerePlaying, or...
Use "And".
JASS:
Players_IsHuman(player) and Players_IsPlaying(player)
 

Nestharus

o-o
Reaction score
84
Yea, I still don't think this kind of thing is that useful...

I use things like this in every map I do and I use things like this in every system that requires initialization for human players or computer players or a combination ;).

I use it all the time, lol ;p
 

Romek

Super Moderator
Reaction score
963
Graveyarded for reasons stated above.

I think everyone uses something like this.
Though I prefer ':' syntax for things like this though.
JASS:
local player p = GetTriggerPlayer()
if p:isPlaying and p:isHuman then
    call BJDebugMsg(p:Name + &quot;&#039;s Number is: &quot; + I2S(p:Id))
endif
 

Nestharus

o-o
Reaction score
84
Actually, as I keep saying, state playing is deprecated as there is a better solution.

Look under my players snippet to find out ^_^, it's in the last or second last post where I discuss the solution ;p. It's even less typing ^_^
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top