- Joined
- Apr 27, 2011
- Messages
- 272
I've seen a lot of kids spam the "Player" function, in an event where in you can just do this and speed things up a little.
changes:
-now takes computer players into account by default
JASS:
library PlayerUtils initializer PlayerUtils_ini
//===========================================================================
// "PlayerUtils" by Alain.Mark
//
// -Info-
// -The main purpose of this lib is to get rid of using the Player(...) function
// and replace it with a faster array search instead. (also w/ added utilities)
//
// -API-
// -function GetPlayerById takes integer id returns nothing
// -function GetPlayersCount takes nothing returns integer
// -function GetHumanPlayersCount takes nothing returns integer
// -function GetComputerPlayersCount takes nothing returns integer
//
//===========================================================================
//=======================================================================
globals
// The value of "AVAILABLE_SLOTS" is in counting numbers. (starts from 1)
private constant integer AVAILABLE_SLOTS = 12
endglobals
//=======================================================================
globals
private constant trigger LEAVER_SENTINEL = CreateTrigger()
private player array IndexedPlayers
private integer ACTIVE_PLAYERS_COUNT = 0
private integer ACTIVE_HUMAN_PLAYERS_COUNT = 0
private integer ACTIVE_COMPUTER_PLAYERS_COUNT = 0
endglobals
//=======================================================================
function GetPlayerById takes integer id returns player
return IndexedPlayers[id]
endfunction
//=======================================================================
function GetPlayersCount takes nothing returns integer
return ACTIVE_PLAYERS_COUNT
endfunction
//=======================================================================
function GetHumanPlayersCount takes nothing returns integer
return ACTIVE_HUMAN_PLAYERS_COUNT
endfunction
//=======================================================================
function GetComputerPlayersCount takes nothing returns integer
return ACTIVE_COMPUTER_PLAYERS_COUNT
endfunction
//=======================================================================
private function DetectLeavers takes nothing returns nothing
set ACTIVE_PLAYERS_COUNT=ACTIVE_PLAYERS_COUNT-1
set ACTIVE_HUMAN_PLAYERS_COUNT=ACTIVE_HUMAN_PLAYERS_COUNT-1
endfunction
//=======================================================================
private function IndexPlayers takes nothing returns nothing
local player pl
local integer n = 0
local integer nMax = AVAILABLE_SLOTS
local code c = function DetectLeavers
loop
exitwhen n>nMax
set pl=Player(n)
if GetPlayerSlotState(pl)==PLAYER_SLOT_STATE_PLAYING then
if GetPlayerController(pl)==MAP_CONTROL_USER then
set ACTIVE_HUMAN_PLAYERS_COUNT=ACTIVE_HUMAN_PLAYERS_COUNT+1
call TriggerRegisterPlayerEvent(LEAVER_SENTINEL,pl,EVENT_PLAYER_LEAVE)
endif
set IndexedPlayers[n]=pl
set ACTIVE_PLAYERS_COUNT=ACTIVE_PLAYERS_COUNT+1
endif
set n=n+1
endloop
set ACTIVE_COMPUTER_PLAYERS_COUNT=ACTIVE_PLAYERS_COUNT-ACTIVE_HUMAN_PLAYERS_COUNT
call TriggerAddCondition(LEAVER_SENTINEL,Filter(c))
endfunction
//=======================================================================
private function PlayerUtils_ini takes nothing returns nothing
call IndexPlayers()
endfunction
//===========================================================================
endlibrary
changes:
-now takes computer players into account by default
Last edited: