Talk:WebAPI/GetPlayerItems

From Team Fortress Wiki
< Talk:WebAPI
Revision as of 18:49, 24 November 2010 by Alex2539 (talk | contribs) (PHP on Windows)
Jump to: navigation, search

PHP on Windows

It's not easy to extract the backpack position or perform any other operations on the inventory bitmask, as PHP_INT_MAX on Windows (even 64-bit) is 2147483647 (2^31 - 1) and PHP does not support unsigned integers. Netshroud 06:48, 24 November 2010 (UTC)

Sure it is. No matter what the integer value is, the binary value is the same. 4294967295 as an unsigned 32-bit integer has exactly the same binary representation as -1 as a signed 32-bit integer (32 ones). It doesn't matter that PHP only supports signed integers since you typically will never want to deal with the entire integer value directly anyway. Once you split the inventory value into its two 16-bit components, it is very simple to deal with.
Assuming the entire inventory value (ie: both backpack position and equipped values) is stored in $inventory, then you can split it into $position and $equipped like this:
$position = $inventory & 65535;
$equipped = ($inventory >> 16) & 511; //I only took the first 9 bits since there are only 9 classes. 
Using this will make $position equal to the numerical value of the position in the backpack (ie: between 1 and 200 inclusive) and $equipped will represent the classes that have the item equipped, as demonstrated in the schema. That is, each class is assigned a power of 2 and that number is the sum of all of the classes that are equipping it. For example, if both the Scout and the Demoman have the Ghastly Gibus equipped, then the Gibus would get an $equipped value of 9: the Scout's number is 1 and the Demoman's is 8. To find out if a class has an item equipped, you just need to do something like this:
$equippedPyro = $equipped & 64;
If it is equipped by the Pyro, $equippedPyro will be 64, otherwise it will be 0. Obviously, you're going to want to come up with a function that does this for you instead of having a whole bunch of "equipped" variables, but the point stands. -- Alex2539 - (talk | contribs) -- 18:49, 24 November 2010 (UTC)