In one of my classes, I have an integer that stores a set of enum flags like so:
enum AttackFlags
{
Contact = 1, //Move connects with target
Projectile = 2, //Attack is projectile based
Unblockable = 4, //Attack can not be blocked
UncounterableLv1 = 8, //Attack can't be countered except by extreme counter attack skills/status effects
UncounterableLv2 = 16, //Attack can not be countered
Flinches = 32, //Has a chance to stun the enemy, pushing back their next turn
Unreflectable = 64, //Attack penetrates reflect. Only checked for Magic attacks
IgnoreDefenderStatusEffects = 128, //Ignores active status effects on the defender
IgnoreAttackerStatusEffects = 256, //Ignores active status effects on the attacker
IgnoreDefenderAbilities = 512, //Ignore the defenders abilities
IgnoreAttackerAbilities = 1024, //Ignore the attackers abilities
IgnoreArmorRating = 2048, //Ignore the defensive boosts of armor
IgnoreWeaponRating = 4096, //Ignore the attack boost from weapons
HighCritical = 8192, //The move has an increased chance to crit
CausesStatus = 16384, //Can the move cause status effects?
Elemental = 32768, //Is the move elemental based?
Unimplemented = 65536, //has the move been implemented yet?
ModsTimer = 131072, //Does it have an effect on the target or users timer?
Heals = 262144, //Does the move heal?
SecondaryEffects = 524288, //Attack has additional effects besides basic attack
PhysicalAttackFlag = 1048576, //Is the Class Physically based? I.E. blocked by Protect and Shield
MagicAttackFlag = 2097152, //Is the move Magically Based? I.E. is it affected by things like Shell
MultiHit = 4194304, //Does it enxcapsulate more then 1 hit
SingleUse = 8388608, //Attack can only be used once per battle
DoesNotCauseDamage = 16777216
};
class Attack
{
int AtkFlags; //Stores AttackFlags |'d together
}
I’d like to add a method to my Attack class with the following signature
bool HasAttackFlags(int flags);
flags would be numerous AttackFlags |’d together. If I only wanted to check against a single flag, I could just & AtkFlags and flags together, but because I have to check against multiple possible flags this won’t work. How can I properly check for multiple flags? I’d like to avoid passing a vector/set of flags to check against as simply |’ing a set of flags together is simpler then constructing a vector/set
Thanks in advance
EDIT:
To clarify what I mean, I might have the following
Attack atk;
atk.AtkFlags = Contact | Projectile | Heals | MagicAttackFlag;
Then later, I’d want to check the flags on atk like so:
bool res = atk.HasAttackFlags(Contact | Projectile);
res should be true and conversely
bool res = atk.HasAttackFlags(Contact | Unreflectable);
should be false becase AtkFlags does not contain both Contact adn Unreflectable.
I am not sure I am following your question, because you seem to mention the obvious solution. So what is wrong with this as a solution, obviously add whatever additional flags you wish:
EDIT: Oh… I think I just figured it out, do you want to check for the presence of 2 or more flags as a set? In which case you can simply modify the method to: