User:Ath/WebAPI Examples

From Team Fortress Wiki
< User:Ath
Revision as of 04:10, 8 November 2010 by Ath (talk | contribs) (PHP: Add PHP non-discriminatory equip check - useful for simple backpack checking)
Jump to: navigation, search

GetPlayerItems Inventory Token

Extracting Item Position and Equipped Classes

C99/C++

#define CLASS_SCOUT     0
#define CLASS_SNIPER    1
#define CLASS_SOLDIER   2
#define CLASS_DEMOMAN   3
#define CLASS_MEDIC     4
#define CLASS_HEAVY     5
#define CLASS_PYRO      6
#define CLASS_SPY       7
#define CLASS_ENGINEER  8

uint32 ExtractBackpackPosition( uint32 unInventoryToken )
{
  return unInventoryToken & 0xFFFF;
}

bool IsEquippedForClass( uint32 unInventoryToken, uint32 unClass )
{
  return 0 != unInventoryToken & ( 1 << ( unClass + 16 ) );
}

PHP

function is_equipped($inventory_token)
{
  return ($inventory_token & 0x0FFF0000) ? true : false; 
}
function is_equipped_for_class($class, $inventory_token)
{
  return ($inventory_token & 0x80000000) && ($inventory_token & (0x00010000 << $class)); 
}
function extract_backpack_position($inventory_token)
{
  return $inventory_token & 0x0000FFFF;
}

Lua

function extract_backpack_position(inventory_token)
  return inventory_token % 2^16
end

function is_equipped_for_class(inventory_token, class)
  return inventory_token % 2^(class+17) >= 2^(class+16)
end