Flag

From QuakeQEWiki
Revision as of 07:16, 17 February 2022 by Teamred (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Vanilla Quake-C files make ample use of binary flags for entity properties as disparate as whether the entity is touching the ground to which weapons a player possesses.

Binary flags can be thought of as a bunch of switches that are either on or off pushed up next to each other. The player weapon system is a great example of this:

item flag-constant integer value 2 binary value
IT_SHOTGUN 1 2^0 0000 0001
IT_SUPER_SHOTGUN 2 2^1 0000 0010
IT_NAILGUN 4 2^2 0000 0100
IT_SUPER_NAILGUN 8 2^3 0000 1000
IT_GRENADE_LAUNCHER 16 2^4 0001 0000
IT_ROCKET_LAUNCHER 32 2^5 0010 0000
IT_LIGHTNING 64 2^6 0100 0000

With these constants the game code can pack the weapon inventory into just 7 bits of data.

If the logic needs to check if a player has a rocket launcher:

if(player.items & IT_ROCKET_LAUNCHER)

If the logic needs to add the rocket launcher to the player's inventory:

player.items = players.items | IT_ROCKET_LAUNCHER;

If the logic needed to remove the rocket launcher from the player's inventory(provided that they have it:

if(player.items & IT_ROCKET_LAUNCHER)

player.items = players.items - IT_ROCKET_LAUNCHER;

Quake-C makes use of floats for this.