- Joined
- Nov 7, 2014
- Messages
- 571
It seems that the
type player extends agent // a single player reference
variables's handle id (which you get from GetHandleId(...)
) get cached when they are first created, which seems to allow one to make a faster GetPlayerId
function (according to my fps benchmark at least) =).
JASS:
library GetPlayerIdFast initializer init
globals
// These need to be the first handle variables that are going to get initialized in the globals block of war3map.j
// otherwise the offset (0x00100008) is going to be wrong!
//
// The offset (0x00100008) would have to be updated if Blizzard makes changes to common.j or blizzard.j
// that involve initialization of handle variables in the globals block
//
constant player player_0 = Player(0)
constant player player_1 = Player(1)
constant player player_2 = Player(2)
constant player player_3 = Player(3)
constant player player_4 = Player(4)
constant player player_5 = Player(5)
constant player player_6 = Player(6)
constant player player_7 = Player(7)
constant player player_8 = Player(8)
constant player player_9 = Player(9)
constant player player_10 = Player(10)
constant player player_11 = Player(11)
constant player player_12 = Player(12)
constant player player_13 = Player(13)
constant player player_14 = Player(14)
constant player player_15 = Player(15)
endglobals
// NOTE:
// GetPlayerIdFast is only faster if it get's inlined (will not get inlined in DEBUG_MODE)
// and if we try to use a variable:
// return GetHandleId(p) - player_0_handle_id
// it will become slower than GetPlayerId, i.e it must use a constant offset (0x00100008)
//
function GetPlayerIdFast takes player p returns integer
return GetHandleId(p) - 0x00100008
endfunction
// NOTE: do NOT try to use hook because it doesn't seem to replace GetPlayerId with the inlined version of GetPlayerIdFast!
//
// hook GetPlayerId GetPlayerIdFast
function GetTriggerPlayerId takes nothing returns integer
return GetHandleId(GetTriggerPlayer()) - 0x00100008
endfunction
private function init takes nothing returns nothing
static if DEBUG_MODE then
if GetHandleId(player_0) - 0x00100008 != 0 then
call BJDebugMsg("|cffFF0000[GetPlayerIdFast] error: incorrect initialization|r")
endif
endif
endfunction
endlibrary