Ability to move player with location.
[naja.git] / naja / actions.py
index b0371a7ca987235e0b5ffce32168f020eea64ceb..e1ec1e0a40b2cf58bbc741e0fcdd71cbc966602f 100644 (file)
@@ -1,4 +1,6 @@
-from naja.constants import BITS
+from naja.constants import ACTION_GLYPHS, BITS, CHESS_PIECES
+from naja.sound import sound
+from naja.utils import bit_glyphs, move_glyph, parse_bits
 
 
 class LocationAction(object):
@@ -7,16 +9,64 @@ class LocationAction(object):
     """
 
     TEXT = None
+    GLYPHS = tuple()
+    MSB_GLYPH = None
 
     def __init__(self, required_bits, **data):
-        self.required_bits = frozenset(required_bits)
+        self.required_bits = required_bits
         self.data = data
 
+    def sanity_check(self, location):
+        pass
+
+    def get_glyphs(self):
+        return self.GLYPHS
+
+    def get_msb_glyph(self):
+        return self.MSB_GLYPH
+
+    def get_text(self, location=None):
+        substitutions = self.data.copy()
+
+        if 'shift' in self.data:
+            substitutions['shift'] = self.data['shift']
+            substitutions['shift_glyph'] = ('{SHIFT_%s}'
+                                            % self.data['direction'].upper())
+        elif 'direction' in self.data:
+            substitutions['rowcol'] = {
+                'NORTH': 'column',
+                'SOUTH': 'column',
+                'EAST': 'row',
+                'WEST': 'row',
+            }[self.data['direction']]
+            substitutions['direction'] = '{%s}' % (substitutions['direction'],)
+
+        if 'chesspiece' in self.data:
+            substitutions['chesspiece_name'] = move_glyph(
+                self.data['chesspiece'])
+
+        if 'rot_direction' in self.data:
+            substitutions['rot_direction_name'] = '{%s}' % (
+                substitutions['rot_direction'],)
+
+        if location is None:
+            substitutions['location_bits'] = 'bits specified by this tile'
+        else:
+            substitutions['location_bits'] = bit_glyphs(
+                location.bitwise_operand)
+
+        text = self.TEXT
+        if self.data.get('message', None) is not None:
+            text = self.data['message']
+
+        return text % substitutions
+
     def check_available(self, player):
         return player.bits.check_bits(self.required_bits)
 
-    def perform_action(self, board):
-        raise NotImplementedError("TODO")
+    def perform_action(self, board, location):
+        raise NotImplementedError(
+            "%s does not implement perform_action()." % (type(self).__name__,))
 
     def check_and_clear_MSB(self, player):
         if player.bits.check_bit(BITS.MSB):
@@ -25,17 +75,270 @@ class LocationAction(object):
         else:
             return False
 
+    def export(self):
+        return {'required_bits': list(self.required_bits),
+                'data': self.data,
+                'action_class': self.__class__.__name__}
+
+    def take_damage(self, board):
+        sound.play_sound('awwww.ogg')
+        board.lose_health()
+
 
 class DoNothing(LocationAction):
     TEXT = "No effect."
+    GLYPHS = (ACTION_GLYPHS.NOTHING,)
 
-    def perform_action(self, board):
+    def perform_action(self, board, location):
         pass
 
 
-class LoseHeathOrMSB(LocationAction):
-    TEXT = "Lose health. If MSB is set, it will be cleared instead."
+class LoseHealthOrMSB(LocationAction):
+    TEXT = "Lose {HEALTH} or {MSB}."
+    MSB_GLYPH = ACTION_GLYPHS.DAMAGE
 
-    def perform_action(self, board):
+    def perform_action(self, board, location):
         if not self.check_and_clear_MSB(board.player):
+            self.take_damage(board)
+
+
+class SetBits(LocationAction):
+    TEXT = "Set %(location_bits)s."
+    GLYPHS = (ACTION_GLYPHS.SET_BITS,)
+
+    def perform_action(self, board, location):
+        board.player.bits.set_bits(location.bitwise_operand)
+
+
+class ClearBits(LocationAction):
+    TEXT = "Clear %(location_bits)s."
+    GLYPHS = (ACTION_GLYPHS.CLEAR_BITS,)
+
+    def perform_action(self, board, location):
+        board.player.bits.clear_bits(location.bitwise_operand)
+
+
+class ClearBitsAndHealth(LocationAction):
+    TEXT = "Clear %(location_bits)s and lose {HEALTH}."
+    GLYPHS = (ACTION_GLYPHS.CLEAR_BITS, ACTION_GLYPHS.DAMAGE)
+
+    def perform_action(self, board, location):
+        board.player.bits.clear_bits(location.bitwise_operand)
+        self.take_damage(board)
+
+
+class ToggleBits(LocationAction):
+    TEXT = "Toggle %(location_bits)s."
+    GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
+
+    def perform_action(self, board, location):
+        board.player.bits.toggle_bits(location.bitwise_operand)
+
+
+class ToggleBitsAndHarm(LocationAction):
+    TEXT = "Toggle %(location_bits)s and lose {HEALTH}."
+    GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS, ACTION_GLYPHS.DAMAGE)
+
+    def perform_action(self, board, location):
+        board.player.bits.toggle_bits(location.bitwise_operand)
+        self.take_damage(board)
+
+
+class GenericBits(LocationAction):
+    GLYPHS = (ACTION_GLYPHS.SET_BITS, ACTION_GLYPHS.CLEAR_BITS)
+
+    def __init__(self, *args, **kw):
+        super(GenericBits, self).__init__(*args, **kw)
+        self.set_bits = parse_bits(self.data.get('set', []))
+        self.clear_bits = parse_bits(self.data.get('clear', []))
+        self.toggle_bits = parse_bits(self.data.get('toggle', []))
+        self.once = self.data.get('once', False)
+        self.acquire_win = self.data.get('acquire_win', False)
+        self.lose_health = self.data.get('lose_health', False)
+        self.gain_health = self.data.get('gain_health', False)
+
+    def sanity_check(self, location):
+        missing_bits = set()
+        missing_bits.update(self.set_bits - set(location.bitwise_operand))
+        missing_bits.update(self.clear_bits - set(location.bitwise_operand))
+        missing_bits.update(self.toggle_bits - set(location.bitwise_operand))
+        if missing_bits:
+            raise ValueError(
+                "Location %s missing bits %r"
+                % (location.card_name, sorted(list(missing_bits))))
+
+    def perform_action(self, board, location):
+        bits = board.player.bits
+        bits.set_bits(self.set_bits)
+        bits.toggle_bits(self.toggle_bits)
+        bits.clear_bits(self.clear_bits)
+        if self.acquire_win:
+            sound.play_sound('yipee.ogg')
+            board.acquire_win_token()
+        if self.lose_health:
+            sound.play_sound('awwww.ogg')
             board.lose_health()
+        if self.gain_health:
+            sound.play_sound('aha.ogg')
+            board.gain_health()
+        if self.once:
+            location.actions.remove(self)
+
+    def get_glyphs(self):
+        glyphs = []
+        if self.acquire_win:
+            glyphs.append(ACTION_GLYPHS.WINTOKEN)
+        if self.lose_health:
+            glyphs.append(ACTION_GLYPHS.DAMAGE)
+        if self.gain_health:
+            glyphs.append(ACTION_GLYPHS.HEAL)
+        if self.set_bits:
+            glyphs.append(ACTION_GLYPHS.SET_BITS)
+        if self.clear_bits:
+            glyphs.append(ACTION_GLYPHS.CLEAR_BITS)
+        if self.toggle_bits:
+            glyphs.append(ACTION_GLYPHS.TOGGLE_BITS)
+        return tuple(glyphs)
+
+    def get_text(self, location=None):
+        if 'message' in self.data:
+            return super(GenericBits, self).get_text()
+        parts = []
+        if self.acquire_win:
+            parts.append("Gain {WINTOKEN}.")
+        if self.lose_health:
+            parts.append("Lose {HEALTH}.")
+        if self.gain_health:
+            parts.append("Gain {HEALTH}.")
+        for template, bits in [
+                ('Set %s.', self.set_bits), ('Clear %s.', self.clear_bits),
+                ('Toggle %s.', self.toggle_bits)]:
+            if bits:
+                parts.append(template % (bit_glyphs(bits)))
+        if self.once:
+            parts.append('Usable once only.')
+        return " ".join(parts)
+
+
+class ShiftBits(LocationAction):
+    TEXT = "Barrel-shift player bits %(shift_glyph)s %(shift)s."
+    GLYPHS = (ACTION_GLYPHS.SHIFT_LEFT,)
+
+    def perform_action(self, board, location):
+        shift = self.data['shift']
+        if self.data['direction'] == 'left':
+            board.player.bits.shift_bits_left(shift)
+        else:
+            board.player.bits.shift_bits_right(shift)
+
+
+class LoseHealthOrMSBAndSetBits(LocationAction):
+    TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
+    GLYPHS = (ACTION_GLYPHS.SET_BITS,)
+    MSB_GLYPH = ACTION_GLYPHS.DAMAGE
+
+    def perform_action(self, board, location):
+        if not self.check_and_clear_MSB(board.player):
+            sound.play_sound('awwww.ogg')
+            board.lose_health()
+        board.player.bits.set_bits(location.bitwise_operand)
+
+
+class AcquireWinToken(LocationAction):
+    TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
+    GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
+
+    def perform_action(self, board, location):
+        sound.play_sound('yipee.ogg')
+        board.acquire_win_token()
+        board.player.bits.clear_bits(set([
+            BITS.RED, BITS.GREEN, BITS.BLUE,
+        ]))
+
+
+class AcquireWinTokenAndLoseHealth(AcquireWinToken):
+    TEXT = "Gain {WINTOKEN}, lose {HEALTH}, then clear {RED,GREEN,BLUE}."
+    GLYPHS = (ACTION_GLYPHS.WINTOKEN, ACTION_GLYPHS.DAMAGE)
+
+    def perform_action(self, board, location):
+        self.take_damage(board)
+        super(AcquireWinTokenAndLoseHealth, self).perform_action(
+            board, location)
+
+
+class GainHealth(LocationAction):
+    TEXT = "Gain {HEALTH}."
+    GLYPHS = (ACTION_GLYPHS.HEAL,)
+
+    def perform_action(self, board, location):
+        sound.play_sound('aha.ogg')
+        board.gain_health()
+
+
+class GainHealthAndClearBitsOrMSB(LocationAction):
+    TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
+    GLYPHS = (ACTION_GLYPHS.HEAL,)
+    MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
+
+    def perform_action(self, board, location):
+        sound.play_sound('aha.ogg')
+        board.gain_health()
+        if not self.check_and_clear_MSB(board.player):
+            board.player.bits.clear_bits(location.bitwise_operand)
+
+
+class ShiftLocations(LocationAction):
+    TEXT = "Shift current %(rowcol)s %(direction)s."
+    GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
+
+    def perform_action(self, board, location):
+        sound.play_sound('grind.ogg')
+        board.shift_locations(
+            self.data['direction'],
+            self.data.get('skip_current', True))
+        if self.data.get('move_player', False):
+            pos = {
+                'NORTH': (0, -1), 'SOUTH': (0, 1),
+                'EAST': (1, 0), 'WEST': (-1, 0),
+            }.get(self.data['direction'], (0, 0))
+            board.player.force_position(pos, delta=True)
+
+
+class RotateLocations(LocationAction):
+    TEXT = "Rotate adjacent tiles %(rot_direction_name)s."
+    GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
+
+    def perform_action(self, board, location):
+        sound.play_sound('grind.ogg')
+        board.rotate_locations(self.data['rot_direction'])
+
+
+class AllowChessMove(LocationAction):
+    TEXT = "Move like a %(chesspiece_name)s for one turn."
+    GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
+
+    def perform_action(self, board, location):
+        if self.data['chesspiece'] in CHESS_PIECES:
+            chesspiece = CHESS_PIECES[self.data['chesspiece']]
+            board.allow_chess_move(chesspiece)
+
+
+class AllowChessMoveIfMSB(LocationAction):
+    TEXT = (
+        "If {MSB} is set, unset {MSB} and move like a "
+        "%(chesspiece_name)s for one turn. Otherwise do nothing.")
+    MSB_GLYPH = ACTION_GLYPHS.MOVEMENT
+
+    def perform_action(self, board, location):
+        if self.data['chesspiece'] in CHESS_PIECES:
+            if self.check_and_clear_MSB(board.player):
+                chesspiece = CHESS_PIECES[self.data['chesspiece']]
+                board.allow_chess_move(chesspiece)
+
+
+class GainMSB(LocationAction):
+    TEXT = "Set {MSB}."
+    GLYPHS = (ACTION_GLYPHS.MSB,)
+
+    def perform_action(self, board, location):
+        board.player.bits.set_bit(BITS.MSB)