Oh yeah, we're using a AttrDict thing, aren't we?
[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         self.required_bits = frozenset(required_bits)
13         self.data = data
14
15     def check_available(self, player):
16         return player.bits.check_bits(self.required_bits)
17
18     def perform_action(self, board, location):
19         raise NotImplementedError("TODO")
20
21     def check_and_clear_MSB(self, player):
22         if player.bits.check_bit(BITS.MSB):
23             player.bits.clear_bit(BITS.MSB)
24             return True
25         else:
26             return False
27
28     def export(self):
29         raise NotImplementedError("TODO")
30
31
32 class DoNothing(LocationAction):
33     TEXT = "No effect."
34
35     def perform_action(self, board, location):
36         pass
37
38     def export(self):
39         return {'required_bits': list(self.required_bits),
40                 'action_class': 'DoNothing'}
41
42
43 class LoseHeathOrMSB(LocationAction):
44     TEXT = "Lose health. If MSB is set, it will be cleared instead."
45
46     def perform_action(self, board, location):
47         if not self.check_and_clear_MSB(board.player):
48             board.lose_health()
49
50
51 class SetBits(LocationAction):
52     TEXT = "Set bits specified by this location."
53
54     def perform_action(self, board, location):
55         board.player.bits.set_bits(location.bitwise_operand)
56
57
58 class ToggleBits(LocationAction):
59     TEXT = "Toggle bits specified by this location."
60
61     def perform_action(self, board, location):
62         board.player.bits.toggle_bits(location.bitwise_operand)