Move the player.
[naja.git] / naja / actions.py
1 from naja.constants import BITS
2
3
4 class LocationAction(object):
5     """
6     An action that may be performed on a location.
7     """
8
9     TEXT = None
10
11     def __init__(self, required_bits, **data):
12         bits = set()
13         for bit in required_bits:
14             # Convert names to numbers if applicable.
15             bits.add(BITS.get(bit, bit))
16         self.required_bits = frozenset(bits)
17         self.data = data
18
19     def check_available(self, player):
20         return player.bits.check_bits(self.required_bits)
21
22     def perform_action(self, board, location):
23         raise NotImplementedError("TODO")
24
25     def check_and_clear_MSB(self, player):
26         if player.bits.check_bit(BITS.MSB):
27             player.bits.clear_bit(BITS.MSB)
28             return True
29         else:
30             return False
31
32     def export(self):
33         return {'required_bits': list(self.required_bits),
34                 'data': self.data,
35                 'action_class': self.__class__.__name__}
36
37
38 class DoNothing(LocationAction):
39     TEXT = "No effect."
40
41     def perform_action(self, board, location):
42         pass
43
44
45 class LoseHealthOrMSB(LocationAction):
46     TEXT = "Lose health. If MSB is set, it will be cleared instead."
47
48     def perform_action(self, board, location):
49         if not self.check_and_clear_MSB(board.player):
50             board.lose_health()
51
52
53 class SetBits(LocationAction):
54     TEXT = "Set bits specified by this location."
55
56     def perform_action(self, board, location):
57         board.player.bits.set_bits(location.bitwise_operand)
58
59
60 class ToggleBits(LocationAction):
61     TEXT = "Toggle bits specified by this location."
62
63     def perform_action(self, board, location):
64         board.player.bits.toggle_bits(location.bitwise_operand)