better action export
[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         return {'required_bits': list(self.required_bits),
30                 'data': self.data,
31                 'action_class': self.__class__.__name__}
32
33
34 class DoNothing(LocationAction):
35     TEXT = "No effect."
36
37     def perform_action(self, board, location):
38         pass
39
40
41 class LoseHealthOrMSB(LocationAction):
42     TEXT = "Lose health. If MSB is set, it will be cleared instead."
43
44     def perform_action(self, board, location):
45         if not self.check_and_clear_MSB(board.player):
46             board.lose_health()
47
48
49 class SetBits(LocationAction):
50     TEXT = "Set bits specified by this location."
51
52     def perform_action(self, board, location):
53         board.player.bits.set_bits(location.bitwise_operand)
54
55
56 class ToggleBits(LocationAction):
57     TEXT = "Toggle bits specified by this location."
58
59     def perform_action(self, board, location):
60         board.player.bits.toggle_bits(location.bitwise_operand)