• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] [Snippet] IsPlaying/Cpu

To help aid myself (and hopefully others) I have created this system to help with indexing players that are in game with this snippet.

It should be able to help with initializing systems that use only playing players and systems that use playing players constantly like an income system or a player multiboard.
Code:
JASS:
library IsPlaying /* v1.0.1.0, created by DeathChef

    Description
        
        Keeps track of playing players (cpu's optional) and keeps them
        indexed into variables to aid with efficient and
        effective coding
        
        
    Settings
        */
        //! textmacro ISPLAYING_SETTINGS
        
            private static constant boolean INCLUDE_CPUS = false
        
        //! endtextmacro
        /*
        
        
    Fields
        
        static integer Count
            - Amount of players
        
        integer Id
            - Actual player id from index
        
        player Player
            - Actual player from index
        
        
        integer Index
            - Index from actual player id
        
        boolean Registered
            - Is playing from actual player id
        
        
        constant static trigger LEAVE_TRIGGER
            - Trigger executed when a player leaves
              Add this trigger with "TriggerAddAction" to a function
              to execute when a player leaves

*/

    struct IsPlaying extends array
        //! runtextmacro ISPLAYING_SETTINGS()
        
        
        static constant trigger LEAVE_TRIGGER = CreateTrigger()
        
        
        readonly static thistype Count = 0
        //Use struct index
        readonly integer Id
        readonly player Player
        
        //Use player id
        readonly integer Index
        readonly boolean Registered
        
        
        private static method PlayerLeaves takes nothing returns nothing
            local thistype i = GetPlayerId(GetTriggerPlayer())
            local thistype thisIndex = i.Index
            
            set i.Index = 0
            set i.Registered = false
            
            if thisIndex == Count then
                set Count.Player = null
            else
                set thisIndex.Id = Count.Id
                set thisIndex.Player = Count.Player
                
                set thistype[Count.Id].Index = thisIndex
            endif
        endmethod
        
        private static method onInit takes nothing returns nothing
            local player p
            local thistype i = 0
            
            loop
                
                set p = Player(i)
                
                static if INCLUDE_CPUS then
                    if GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING then
                        set Count.Id = i
                        set Count.Player = p
                        
                        set i.Index = Count
                        set i.Registered = true
                        
                        set Count = Count + 1
                        
                        call TriggerRegisterPlayerEvent(LEAVE_TRIGGER, p, EVENT_PLAYER_LEAVE)
                    endif
                else
                    if GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p) == MAP_CONTROL_USER then
                        set Count.Id = i
                        set Count.Player = p
                        
                        set i.Index = Count
                        set i.Registered = true
                        
                        set Count = Count + 1
                        
                        call TriggerRegisterPlayerEvent(LEAVE_TRIGGER, p, EVENT_PLAYER_LEAVE)
                    endif
                endif
                
                exitwhen i == 11
                set i = i + 1
                
            endloop
            
            set p = null
            
            call TriggerAddAction(LEAVE_TRIGGER, function thistype.PlayerLeaves)
        endmethod
    
    endstruct
    
endlibrary

JASS:
library IsCpu /* v1.0.1.0, created by DeathChef

    Description
        
        Keeps track of playing players (cpu's optional) and keeps them
        indexed into variables to aid with efficient and
        effective coding
        
        
    Fields
        
        static integer Count
            - Amount of players
        
        integer Id
            - Actual player id from index
        
        player Player
            - Actual player from index
        
        
        integer Index
            - Index from actual player id
        
        boolean Registered
            - Is actual player slot a player
        
        
        constant static trigger LEAVE_TRIGGER
            - Trigger executed when a player leaves
              Add this trigger with "TriggerAddAction" to a function
              to execute when a player leaves

*/

    struct IsCpu extends array
        readonly static thistype Count = 0
        //Use struct index
        readonly integer Id
        readonly player Player
        
        //Use player id
        readonly integer Index
        readonly boolean Registered
        
        
        private static method onInit takes nothing returns nothing
            local player p
            local thistype i = 0
            
            loop
                
                set p = Player(i)
                
                if GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p) == MAP_CONTROL_COMPUTER then
                    set Count.Id = i
                    set Count.Player = p
                    
                    set i.Index = Count
                    set i.Registered = true
                    
                    set Count = Count + 1
                endif
                
                exitwhen i == 11
                set i = i + 1
                
            endloop
            
            set p = null
        endmethod
    
    endstruct
    
endlibrary
 
Last edited:
I'm not sure why GetPlayerController(p) == MAP_CONTROL_USER should not work within normal loops.
You have to convince us first before this statement is valid. I know you said "consider", but still.

Never even try to destroy bj constants, they are just there, so they won't ever leak.

Demo code would be..?
JASS:
local integer i = 1
loop
    exitwhen i > UserCount   
    call TriggerRegisterPlayerUnitEvent(trigger, IndexUser[i], EVENT_PLAYER_UNIT_XXXXX, null)
endloop

What about change to structs and then also use data variables into readonly ones.

Somehoe with jass mentality people think array indexing starts with 0,
but here you start with IndexUser[1], which is maybe more intuitive in gui than in jass.

First when I read CPU I meant you are talking about Central Processing Unit, lol.^^
 
Btw the code doesn't compile.
I'll update that right now.

I'm not sure why GetPlayerController(p) == MAP_CONTROL_USER should not work within normal loops.
You have to convince us first before this statement is valid. I know you said "consider", but still.
Your right I do need to, I just got lazy.
Hopefully what I found out earlier in my coding career hasn't hindered me now as false knowledge. :p


Never even try to destroy bj constants, they are just there, so they won't ever leak.
Will fix.

Demo code would be..?
JASS:
local integer i = 1
loop
    exitwhen i > UserCount   
    call TriggerRegisterPlayerUnitEvent(trigger, IndexUser[i], EVENT_PLAYER_UNIT_XXXXX, null)
endloop
I'll get onto demo code soon.
I sometimes find doing demo code annoying to do.

What about change to structs and then also use data variables into readonly ones.
This code was actually originally coded in a struct so I guess I'll code it back into one.

Somehoe with jass mentality people think array indexing starts with 0,
but here you start with IndexUser[1], which is maybe more intuitive in gui than in jass.
That was a mistake on my part.
I don't think I've read through my own code properly haha.

GetPlayerSlotState does everything your library does.

That detects both cpu's and human players.
This system does them seperate or/and together with indexing.

But yes I will probably replace the one where they do them together with that instead.
 
That detects both cpu's and human players.
You can combine GetPlayerController and GetPlayerSlotState in a condition and be done with it.

Why all that indexing and event crap?


You are basicly replacing a one-liner condition with a convoluted library that creates pointless overhead on the JASS layer instead of just using a simple AND conditional on the native layer.
 
Updated with the use of loop and changed thread description.

You can combine GetPlayerController and GetPlayerSlotState in a condition and be done with it.
I don't know why I didn't do this in the first place.
Guess I resorted to the ForForce native cause the constant bj_FORCE_ALL_PLAYERS contains all playing players by default so I must of missed it somehow that way...

Why all that indexing and event crap?

You are basicly replacing a one-liner condition with a convoluted library that creates pointless overhead on the JASS layer instead of just using a simple AND conditional on the native layer.
My aim isn't really to overall the one liner, just some indexing of actual players ingame. I'm currently using the indexing for initializes atm, although it could be used for things like resource incomes for players or player multiboards.

I think I should change the deindexing to slide all the players down instead of just replacing the player that left with the last one in the index though(considering that this could cause issues that efficiency isn't worth overwriting for).

It just helps with keeping things clean and helps makes a slight edge of efficiency.

___

So I still need to make an example with deindex(when player leaves) where the user needs to create a function that attaches the when player leaves function in their libraries to clean variables and anything else.
I may also need to change the deindexing scheme.
 
PlayerUtils by Alain.Mark just indexes players into their original index.
The only other thing that that snippet does is count the amount of active players.
Just reading the on leave event should be enough proof, as he only coded it to decrease the active player count and not to index the players.
I don't really see a use for this system lol.

Nes's Player manager seems to have everything I need, although is a bit hard to read to be sure.

PlayerUtils by TriggerHappy seems to have what I want and a bit more.
Any idea why it was graveyarded?
 
Level 14
Joined
Dec 12, 2012
Messages
1,007
What is the state of this submission? I'm not really sure about that.

Why there are two librarys (IsPlaying and IsCpu)? If they are very similar, they should be merged, if not they should be posted as two different proposals.

And you stated:

I've tested it and have proof.
Uhm I'll post about it soon I guess.

Where exactly, I can only see the code of the two different libraries... Can you please share this proof?
 
Ok just trying to read over this thread again.

What is the state of this submission? I'm not really sure about that.

Why there are two librarys (IsPlaying and IsCpu)? If they are very similar, they should be merged, if not they should be posted as two different proposals.

https://github.com/nestharus/JASS/blob/master/jass/Systems/PlayerManager/script.j
I'm currently using this in my projects.
If I were to merge them, my system would look more like Nestharus's so this might as well be graveyarded.

I might contact him about uploading it here on Hive again(?) and adding things like teams managment and linear indexing so a random player can be chosen from one of the groups easier.

Where exactly, I can only see the code of the two different libraries... Can you please share this proof?

See post #8 where I last updated my system.
I was mistaken by something dumb.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
I vote for graveyarding, this doesnt do anything I couldn't do with calling 2 natives in loop, which is even shorter(like 6 lines). Hooking the onLeave event is like, 3 lines of code, so now its 9 for all your funcionality.
 
Top