Difference between revisions of "Flag"
(Created page with "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 weapons are a great example of this: {| class="wikitable" |+ !item flag-constant !integer value !2 !binary value |- |IT_SHOTGUN |1 |2^0 |<code>0000 0001</code> |- |IT_SUPER_SHOTGUN |2 |2^1...") |
|||
Line 1: | Line 1: | ||
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. | 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 | 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: | ||
{| class="wikitable" | {| class="wikitable" | ||
|+ | |+ | ||
Line 44: | Line 44: | ||
|<code>0100 0000</code> | |<code>0100 0000</code> | ||
|} | |} | ||
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: | |||
<code>if(player.items & IT_ROCKET_LAUNCHER)</code> | |||
If the logic needs to add the rocket launcher to the player's inventory: | |||
<code>player.items = players.items | IT_ROCKET_LAUNCHER;</code> | |||
If the logic needed to remove the rocket launcher from the player's inventory(provided that they have it: | |||
<code>if(player.items & IT_ROCKET_LAUNCHER)</code> | |||
<code>player.items = players.items - IT_ROCKET_LAUNCHER;</code> | |||
Quake-C makes use of [[Float|floats]] for this. |
Latest revision as of 07:16, 17 February 2022
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.