Fix bits and add new action.
authorJeremy Thurgood <firxen@gmail.com>
Tue, 13 May 2014 18:37:39 +0000 (20:37 +0200)
committerJeremy Thurgood <firxen@gmail.com>
Tue, 13 May 2014 18:38:38 +0000 (20:38 +0200)
data/location_decks/test.yaml
naja/actions.py
naja/constants.py
naja/player.py

index d26cafde89356beb56cf41fc0c40b2854b332856..53a0c77dcd65e3f62701c7ee675c375bc841f20d 100644 (file)
@@ -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
index 32ac03509c88527cf8d5205d0bfa0a490456c674..efbb16b696027255dad973984b15c2dfc54b9e14 100644 (file)
@@ -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)
index 3f45496558320aac8e3084829879cd5800404b52..6e3973da25b26640ac13b9ffcc14d3403833b476 100644 (file)
@@ -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,
index 83c41242accbc9da98a955dfeecafa40761dd351..f51e83ef257fdcdc7fe03d0062e00ab0a7ba7cec 100644 (file)
@@ -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