- Joined
- Jun 23, 2007
- Messages
- 4,066
JASS:
library PlayerUtils initializer onInit uses optional GroupUtils
/**************************************************************
*
* v1.1.0 by TriggerHappy
*
* This library provides utilities for dealing with players, as well
* as caches player data for improved efficiency.
*
* Public Variables
*
* force FORCE_ACTIVE_PLAYERS - Player group of everyone who is playing.
*
* Function API
*
* -- These functions are to be used with the "player" type.
*
* function PlayerEx takes integer i returns player // this function is an alternitve to Player()
* function GetPlayer takes integer i returns player // this function is an alternitve to Player()
* function GetPlayerCount takes nothing returns integer // returns how many active players there are
* function GetPlayerNameEx takes player p returns string // should be used instead of GetPlayerName
* function GetPlayerNameColored takes player p returns string // returns the players name with color
* function GetPlayerHex takes player p returns string // returns player color code
* function GetActivePlayer takes integer i returns player // get a player from list of playing players
* function IsPlayerPlaying takes player p returns boolean // returns true if the player is playing
*
* Struct API
*
* -- These provide the most efficiency and should be used whenever possible.
*
* static method get takes integer i returns player
* static method fromLocal takes nothing returns player
* static method fromEnum takes nothing returns Player
* static method do takes code func returns nothing
* static method for takes force f, code func returns nothing
*
* method isPlaying takes nothing returns boolean
* method setColor takes playercolor c, boolean changeUnits returns nothing
*
* static method operator count takes nothing returns integer
* static method operator [] takes integer id returns player
*
* method operator name takes nothing returns string
* method operator name= takes string name returns nothing
* method operator color takes nothing returns playercolor
* method operator color= takes playercolor c returns nothing
* method operator defaultColor takes nothing returns playercolor
* method operator hex takes nothing returns string
* method operator nameColored takes nothing returns string
*
**************************************************************/
globals
constant force FORCE_ACTIVE_PLAYERS = CreateForce()
private player Local
private integer ActivePlayerCount = -1
private string array Name
private player array ActivePlayers
private boolean array IsActive
private string array HexData
private string array Hex
endglobals
static if (not LIBRARY.GroupUtils) then
globals
private constant group ENUM_GROUP = CreateGroup()
endglobals
endif
struct Players
player handle
static playercolor array Color
static method get takes integer i returns player
return thistype(i).handle
endmethod
static method fromLocal takes nothing returns player
return Local
endmethod
static method do takes code func returns nothing
call ForForce(FORCE_ACTIVE_PLAYERS, func)
endmethod
static method for takes force f, code func returns nothing
call ForForce(f, func)
endmethod
method isPlaying takes nothing returns boolean
return IsActive[this]
endmethod
method setColor takes playercolor c, boolean changeUnits returns nothing
local unit u
call SetPlayerColor(this.handle, c)
if (changeUnits) then
call GroupEnumUnitsOfPlayer(ENUM_GROUP, this.handle, null)
loop
set u = FirstOfGroup(ENUM_GROUP)
exitwhen u == null
call SetUnitColor(u, c)
call GroupRemoveUnit(ENUM_GROUP, u)
endloop
endif
endmethod
static method operator count takes nothing returns integer
return ActivePlayerCount + 1
endmethod
static method operator [] takes player p returns thistype
return thistype(GetPlayerId(p))
endmethod
method operator name takes nothing returns string
return Name[this]
endmethod
method operator name= takes string name returns nothing
call SetPlayerName(this.handle, name)
endmethod
method operator color takes nothing returns playercolor
return GetPlayerColor(this.handle)
endmethod
method operator color= takes playercolor c returns nothing
call SetPlayerColor(this.handle, c)
endmethod
method operator defaultColor takes nothing returns playercolor
return Color[this]
endmethod
method operator hex takes nothing returns string
return Hex[this]
endmethod
method operator nameColored takes nothing returns string
return Hex[this] + Name[this] + "|r"
endmethod
endstruct
function PlayerEx takes integer i returns player
return Players(i).handle
endfunction
function GetPlayer takes integer i returns player
return Players(i).handle
endfunction
function LocalPlayer takes nothing returns player
return Local
endfunction
function GetPlayerCount takes nothing returns integer
return ActivePlayerCount + 1
endfunction
function GetPlayerNameEx takes player p returns string
return Name[GetPlayerId(p)]
endfunction
function GetPlayerNameColored takes player p returns string
local integer i = GetPlayerId(p)
return Hex[i] + Name[i] + "|r"
endfunction
function GetPlayerHex takes player p returns string
return Hex[GetPlayerId(p)]
endfunction
function GetActivePlayer takes integer i returns player
return ActivePlayers[i]
endfunction
function IsPlayerPlaying takes player p returns boolean
return IsActive[GetPlayerId(p)]
endfunction
//===========================================================================
private function SetPlayerNameHook takes player p, string name returns nothing
set Name[GetPlayerId(p)] = name
endfunction
private function SetPlayerColorHook takes player p, playercolor c returns nothing
set Hex[GetPlayerId(p)] = HexData[GetHandleId(c)]
endfunction
private function onLeave takes nothing returns boolean
local integer i = 0
local player p = GetTriggerPlayer()
loop
exitwhen i > ActivePlayerCount
if (ActivePlayers[i] == p) then
set ActivePlayers[i] = ActivePlayers[ActivePlayerCount]
set ActivePlayerCount = ActivePlayerCount - 1
set IsActive[GetPlayerId(p)] = false
call ForceRemovePlayer(FORCE_ACTIVE_PLAYERS, ActivePlayers[i])
return false
endif
set i = i + 1
endloop
return false
endfunction
private function onInit takes nothing returns nothing
local trigger t = CreateTrigger()
local integer i = 0
local Players p
set Local = GetLocalPlayer()
set HexData[0] = "|cffff0303"
set HexData[1] = "|cff0042ff"
set HexData[2] = "|cff1ce6b9"
set HexData[3] = "|cff540081"
set HexData[4] = "|cfffffc01"
set HexData[5] = "|cfffe8a0e"
set HexData[6] = "|cff20c000"
set HexData[7] = "|cffe55bb0"
set HexData[8] = "|cff959697"
set HexData[9] = "|cff7ebff1"
set HexData[10] = "|cff106246"
set HexData[11] = "|cff4e2a04"
loop
exitwhen i == bj_MAX_PLAYER_SLOTS
set p = Players(i)
set p.handle = Player(i)
set Players.Color[i] = GetPlayerColor(p.handle)
if (GetPlayerController(p.handle) == MAP_CONTROL_USER and GetPlayerSlotState(p.handle) == PLAYER_SLOT_STATE_PLAYING) then
set ActivePlayerCount = ActivePlayerCount + 1
set ActivePlayers[ActivePlayerCount] = p.handle
set IsActive[i] = true
call TriggerRegisterPlayerEvent(t, p.handle, EVENT_PLAYER_LEAVE)
call ForceAddPlayer(FORCE_ACTIVE_PLAYERS, p.handle)
set Hex[i] = HexData[GetHandleId(Players.Color[i])]
endif
set Name[i] = GetPlayerName(p.handle)
set i = i + 1
endloop
call TriggerAddCondition(t, Filter(function onLeave))
endfunction
hook SetPlayerName SetPlayerNameHook
hook SetPlayerColor SetPlayerColorHook
endlibrary
JASS:
// Here's an example of enumerating through all active players via a force
function Callback takes nothing returns nothing
local Players p = Players[GetFilterPlayer()]
call p.setColor(Players.Color[GetRandomInt(0, 11)], true) // randomize player color
call BJDebugMsg(p.nameColored) // display name (with player color)
endfunction
call Players.do(function Callback)
// Now without a force
local integer i = 0
loop
exitwhen i == GetPlayerCount() // or Players.count
call Players(i).setColor(Players.Color[GetRandomInt(0, 11)], true) // randomize player color
call BJDebugMsg(Players(i).nameColored)
set i = i + 1
endloop
Last edited: