[Snippet] TeamReforce

Level 9
Joined
Jun 21, 2012
Messages
432
JASS:
library TeamIndex/*
****************************************************************************************
*
*   ************************************************************************************
*
*   */ uses /*
*       */ Table /* hiveworkshop.com/forums/jass-functions-413/snippet-new-table-188084/
*   
*   ************************************************************************************
*
*   TeamIndex
*   _________
*   v1.1.2.3
*   by Thelordmarshall
*   
*   Previously TeamReforce...
*   
*   Useful to get indexes of each team in the map, find out how many players 
*   there are in map, detect when alliances are changed, etc...
*   
*   Important:
*   __________
*
*   In order that it work correctly go to: Scenario/Player Properties/Forces
*   and select the box "Allied" of each force of your map.
*
*   Functions:
*   __________
*       
*       - function GetStartPlayers takes nothing returns integer
*           - get count starting players in map.
*       - function GetCountPlayers takes nothing returns integer
*           - get count active players in map.
*       - function CountMapTeams takes nothing returns integer
*           - returns the number of teams in your map.
*       - function GetPlayerForce takes player whichPlayer returns force
*           - returns the force of the player.
*       - function GetPlayerForceId takes player whichPlayer returns integer
*           - returns the force index of the player.
*       - function GetPlayerIdInForce takes player whichPlayer returns integer
*           - returns player id in force.
*       - function CountPlayersInForce takes integer forceId returns integer
*           - similar to CountPlayersInForceBJ, returns the numbers of players in force.
*       - function RegisterEventAllianceChange takes code c returns nothing
*           - register event when alliance is changed.
***************************************************************************************/

    globals
        private TableArray ht=0
        private timer m=CreateTimer()
        private trigger t=CreateTrigger()
        private trigger e=CreateTrigger()
    endglobals
    
    function GetStartPlayers takes nothing returns integer
        return ht[5].integer[0]
    endfunction
    function GetCountPlayers takes nothing returns integer
        return ht[0].integer[0]
    endfunction
    function CountMapTeams takes nothing returns integer
        return ht[0].integer[1]
    endfunction
    function GetPlayerForce takes player whichPlayer returns force
        return ht[1].force[GetPlayerId(whichPlayer)]
    endfunction
    function GetPlayerForceId takes player whichPlayer returns integer
        return ht[4].integer[GetPlayerId(whichPlayer)]
    endfunction
    function GetPlayerIdInForce takes player whichPlayer returns integer
        return ht[2].integer[GetPlayerId(whichPlayer)]
    endfunction
    function CountPlayersInForce takes integer forceId returns integer
        return ht[3].integer[forceId]
    endfunction
    function RegisterEventAllianceChange takes code c returns nothing
        call TriggerAddCondition(t,Filter(c))
    endfunction
    
    //Core
    private function RefreshData takes nothing returns nothing
        local integer c=-1
        local integer i=5
        local integer h=0
        local integer x=0
        local player p
        local player p2
        loop
            call ht[i].flush()
            exitwhen(0==i)
            set i=i-1
        endloop
        loop
            exitwhen(i==bj_MAX_PLAYERS)
            set p=Player(i)
            if(GetPlayerSlotState(p)==PLAYER_SLOT_STATE_PLAYING)then
                set ht[0].integer[0]=ht[0].integer[0]+1
            endif
            if(GetPlayerController(p)!=MAP_CONTROL_NONE)then
                set ht[5].integer[0]=ht[5].integer[0]+1
                if(not ht[1].has(i))then
                    set ht[0].force[h]=CreateForce()
                    set ht[0].integer[1]=ht[0].integer[1]+1
                    loop
                        exitwhen(x==bj_MAX_PLAYERS)
                        set p2=Player(x)
                        if(GetPlayerSlotState(p2)==PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p2)!=MAP_CONTROL_NONE and IsPlayerAlly(p2,p))then
                            call ForceAddPlayer(ht[0].force[h],p2)
                            set c=c+1
                            set ht[2].integer[x]=c
                            set ht[4].integer[x]=h
                            set ht[1].force[x]=ht[0].force[h]
                            set ht[3].integer[h]=ht[3].integer[h]+1
                            set ht[1].integer[x]=x
                        endif
                        set x=x+1
                    endloop
                    set x=0
                    set h=h+1
                endif
            endif
            set i=i+1
        endloop
        call PauseTimer(m)
        call TriggerEvaluate(t)
    endfunction
    
    //! textmacro Force_hook takes FUNCTION_1,FUNCTION_2,ARGUMENTS,H
    private function $FUNCTION_1$ takes $ARGUMENTS$ returns nothing
        call TimerStart(m,0.,false,function RefreshData)
    endfunction
    $H$hook $FUNCTION_2$ $FUNCTION_1$
    //! endtextmacro
    
    //! runtextmacro Force_hook("Hook_Alliance","SetPlayerAlliance","player p1, player p2, alliancetype s, boolean b","")
    //! runtextmacro Force_hook("Hook_PlayerTeam","SetPlayerTeam","player p, integer t","")
    //! runtextmacro Force_hook("Hook_AllianceBJ","SetPlayerAllianceStateBJ","player p1, player p2, integer a","")
    //! runtextmacro Force_hook("Hook_StateAllyBJ","SetPlayerAllianceStateAllyBJ","player p1, player p2, boolean b","")
    //! runtextmacro Force_hook("Hook_StateVisionBJ","SetPlayerAllianceStateVisionBJ","player p1, player p2, boolean b","")
    //! runtextmacro Force_hook("Hook_StateControlBJ","SetPlayerAllianceStateControlBJ","player p1, player p2, boolean b","")
    //! runtextmacro Force_hook("Hook_StateFullControlBJ","SetPlayerAllianceStateFullControlBJ","player p1, player p2, boolean b","")
    //! runtextmacro Force_hook("EventCondition","","nothing","//")

    private module Init
        static method onInit takes nothing returns nothing
            local code c=function EventCondition
            local integer i=12
            loop
                call TriggerRegisterPlayerEvent(e,Player(i),EVENT_PLAYER_LEAVE)
                exitwhen(i==0)
                set i=i-1
            endloop
            call TriggerAddCondition(e,Filter(c))
            set ht=TableArray[6]
            call TimerStart(m,0.,false,function RefreshData)
            set c=null
        endmethod
    endmodule
    private struct i extends array
        implement Init
    endstruct
endlibrary
 
Last edited:
Level 23
Joined
Apr 16, 2012
Messages
4,041
if Table supports what hashables support, then this should not collide, because each "native" type has its own storage cell for given index pair. So writing force into (0,0) inside hashtable a and then writing integer in the same place in same hashtable will not make the force nonexistant, and you can still load it just fine.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
Any reason for this local code c=function EventCondition ?

JASS:
function f takes nothing returns nothing
endfunction

function q takes nothing returns nothing
    call TriggerAddCondition(null, Filter(f))
endfunction

unless you update your pjass into the newest version, this will not compile, but the following one will

JASS:
function f takes nothing returns nothing
endfunction

function q takes nothing returns nothing
    local code c = function f
    call TriggerAddCondition(null, Filter(c))
endfunction
 
Level 38
Joined
Sep 26, 2009
Messages
8,465
GetPlayerForce should just return bj_FORCE_PLAYER[GetPlayerId(player)]

Also, just let the user assign teams to forces on their own. If you want to auto-track inter-force movement, make a separate resource for that. This system doesn't have a specific-enough role to play. Graveyarding next week.
 
Top