From 40a95ca09c7a7ca0b4cca019e0ca8eb7670054bc Mon Sep 17 00:00:00 2001 From: Jeremy Thurgood Date: Tue, 13 May 2014 20:37:39 +0200 Subject: [PATCH] Fix bits and add new action. --- data/location_decks/test.yaml | 6 +++++- naja/actions.py | 14 +++++++++++++- naja/constants.py | 18 +++++++++--------- naja/player.py | 8 ++++---- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/data/location_decks/test.yaml b/data/location_decks/test.yaml index d26cafd..53a0c77 100644 --- a/data/location_decks/test.yaml +++ b/data/location_decks/test.yaml @@ -3,6 +3,9 @@ description: "Test location deck." # This field is ignored, but it's a useful place to put some action definitions # we can reference later. _standard_actions: + bad_default: &SET-BITS-DEFAULT + action_class: 'LoseHealthOrMSBAndSetBits' + required_bits: [] bad_default: &BAD-DEFAULT action_class: 'LoseHealthOrMSB' required_bits: [] @@ -14,7 +17,8 @@ _standard_actions: required_bits: [YELLOW, MAGENTA] cards: - - actions: [] + - actions: + - *SET-BITS-DEFAULT - actions: - *BAD-DEFAULT - *TOGGLE-BITS-C diff --git a/naja/actions.py b/naja/actions.py index 32ac035..efbb16b 100644 --- a/naja/actions.py +++ b/naja/actions.py @@ -7,6 +7,7 @@ class LocationAction(object): """ TEXT = None + USES_MSB = False def __init__(self, required_bits, **data): bits = set() @@ -43,7 +44,8 @@ class DoNothing(LocationAction): class LoseHealthOrMSB(LocationAction): - TEXT = "Lose health. If MSB is set, it will be cleared instead." + TEXT = "Lose health or MSB." + USES_MSB = True def perform_action(self, board, location): if not self.check_and_clear_MSB(board.player): @@ -62,3 +64,13 @@ class ToggleBits(LocationAction): def perform_action(self, board, location): board.player.bits.toggle_bits(location.bitwise_operand) + + +class LoseHealthOrMSBAndSetBits(LocationAction): + TEXT = "Lose health or MSB, then set bits specified by this location." + USES_MSB = True + + def perform_action(self, board, location): + if not self.check_and_clear_MSB(board.player): + board.lose_health() + board.player.bits.set_bits(location.bitwise_operand) diff --git a/naja/constants.py b/naja/constants.py index 3f45496..6e3973d 100644 --- a/naja/constants.py +++ b/naja/constants.py @@ -26,22 +26,22 @@ DEFAULT_MUSIC_VOLUME = 0.3 # music volume # Player bits BITS = AttrDict({ # Direction bits - 'NORTH': 1, - 'SOUTH': 2, - 'EAST': 4, - 'WEST': 8, + 'NORTH': 0, + 'SOUTH': 1, + 'EAST': 2, + 'WEST': 3, # Condition bits - 'CYAN': 16, - 'MAGENTA': 32, - 'YELLOW': 64, - 'MSB': 128, + 'CYAN': 4, + 'MAGENTA': 5, + 'YELLOW': 6, + 'MSB': 7, }) DIRECTION_BITS = AttrDict((k, v) for k, v in BITS.items() if v < 4) CONDITION_BITS = AttrDict((k, v) for k, v in BITS.items() if v >= 4) # Player defaults PLAYER_DEFAULTS = AttrDict({ - 'INITIAL_BITS': BITS.NORTH | BITS.SOUTH | BITS.EAST | BITS.WEST, + 'INITIAL_BITS': 0x0f, 'INITIAL_POS': (2, 2), 'MAX_HEALTH': 4, 'WINS_REQUIRED': 4, diff --git a/naja/player.py b/naja/player.py index 83c4124..f51e83e 100644 --- a/naja/player.py +++ b/naja/player.py @@ -21,16 +21,16 @@ class PlayerBits(object): # Operate on individual bits def check_bit(self, bit): - return bool(self.bits & bit) + return bool(self.bits & (1 << bit)) def set_bit(self, bit): - self.bits |= bit + self.bits |= (1 << bit) def clear_bit(self, bit): - self.bits &= (0xff ^ bit) + self.bits &= (0xff ^ (1 << bit)) def toggle_bit(self, bit): - self.bits ^= bit + self.bits ^= (1 << bit) # Operate on sets of bits -- 2.34.1