Bit shifts!
[naja.git] / naja / actions.py
1 from naja.constants import ACTION_GLYPHS, BITS, CHESS_PIECES
2 from naja.sound import sound
3 from naja.utils import bit_glyphs, move_glyph
4
5
6 class LocationAction(object):
7     """
8     An action that may be performed on a location.
9     """
10
11     TEXT = None
12     GLYPHS = tuple()
13     MSB_GLYPH = None
14
15     def __init__(self, required_bits, **data):
16         self.required_bits = required_bits
17         self.data = data
18
19     def get_text(self, location=None):
20         substitutions = self.data.copy()
21
22         if 'shift' in self.data:
23             substitutions['shift'] = self.data['shift']
24             substitutions['direction'] = self.data['direction']
25         elif 'direction' in self.data:
26             substitutions['rowcol'] = {
27                 'NORTH': 'column',
28                 'SOUTH': 'column',
29                 'EAST': 'row',
30                 'WEST': 'row',
31             }[self.data['direction']]
32             substitutions['direction'] = '{%s}' % (substitutions['direction'],)
33
34         if 'chesspiece' in self.data:
35             substitutions['chesspiece_name'] = move_glyph(
36                 self.data['chesspiece'])
37
38         if 'rot_direction' in self.data:
39             substitutions['rot_direction_name'] = '{%s}' % (
40                 substitutions['rot_direction'],)
41
42         if location is None:
43             substitutions['location_bits'] = 'bits specified by this location'
44         else:
45             substitutions['location_bits'] = bit_glyphs(
46                 location.bitwise_operand)
47
48         return self.TEXT % substitutions
49
50     def check_available(self, player):
51         return player.bits.check_bits(self.required_bits)
52
53     def perform_action(self, board, location):
54         raise NotImplementedError(
55             "%s does not implement perform_action()." % (type(self).__name__,))
56
57     def check_and_clear_MSB(self, player):
58         if player.bits.check_bit(BITS.MSB):
59             player.bits.clear_bit(BITS.MSB)
60             return True
61         else:
62             return False
63
64     def export(self):
65         return {'required_bits': list(self.required_bits),
66                 'data': self.data,
67                 'action_class': self.__class__.__name__}
68
69
70 class DoNothing(LocationAction):
71     TEXT = "No effect."
72     GLYPHS = (ACTION_GLYPHS.NOTHING,)
73
74     def perform_action(self, board, location):
75         pass
76
77
78 class LoseHealthOrMSB(LocationAction):
79     TEXT = "Lose {HEALTH} or {MSB}."
80     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
81
82     def perform_action(self, board, location):
83         if not self.check_and_clear_MSB(board.player):
84             board.lose_health()
85
86
87 class SetBits(LocationAction):
88     TEXT = "Set %(location_bits)s."
89     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
90
91     def perform_action(self, board, location):
92         board.player.bits.set_bits(location.bitwise_operand)
93
94
95 class ToggleBits(LocationAction):
96     TEXT = "Toggle %(location_bits)s."
97     GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
98
99     def perform_action(self, board, location):
100         board.player.bits.toggle_bits(location.bitwise_operand)
101
102
103 class ShiftBits(LocationAction):
104     TEXT = "Barrel-shift player %(shift)s bits %(direction)s."
105     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
106
107     def perform_action(self, board, location):
108         shift = self.data['shift']
109         if self.data['direction'] == 'left':
110             board.player.bits.shift_bits_left(shift)
111         else:
112             board.player.bits.shift_bits_right(shift)
113
114
115 class LoseHealthOrMSBAndSetBits(LocationAction):
116     TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
117     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
118     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
119
120     def perform_action(self, board, location):
121         if not self.check_and_clear_MSB(board.player):
122             board.lose_health()
123         board.player.bits.set_bits(location.bitwise_operand)
124
125
126 class AcquireWinToken(LocationAction):
127     TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
128     GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
129
130     def perform_action(self, board, location):
131         board.acquire_win_token()
132         board.player.bits.clear_bits(set([
133             BITS.RED, BITS.GREEN, BITS.BLUE,
134         ]))
135
136
137 class GainHealth(LocationAction):
138     TEXT = "Gain {HEALTH}."
139     GLYPHS = (ACTION_GLYPHS.HEAL,)
140
141     def perform_action(self, board, location):
142         board.gain_health()
143
144
145 class GainHealthAndClearBitsOrMSB(LocationAction):
146     TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
147     GLYPHS = (ACTION_GLYPHS.HEAL,)
148     MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
149
150     def perform_action(self, board, location):
151         board.gain_health()
152         if not self.check_and_clear_MSB(board.player):
153             board.player.bits.clear_bits(location.bitwise_operand)
154
155
156 class ShiftLocations(LocationAction):
157     TEXT = "Shift current %(rowcol)s %(direction)s."
158     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
159
160     def perform_action(self, board, location):
161         sound.play_sound('grind.ogg')
162         board.shift_locations(self.data['direction'])
163
164
165 class RotateLocations(LocationAction):
166     TEXT = "Rotate adjacent locations %(rot_direction_name)s."
167     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
168
169     def perform_action(self, board, location):
170         sound.play_sound('grind.ogg')
171         board.rotate_locations(self.data['rot_direction'])
172
173
174 class AllowChessMove(LocationAction):
175     TEXT = "Move like a %(chesspiece_name)s for one turn."
176     GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
177
178     def perform_action(self, board, location):
179         if self.data['chesspiece'] in CHESS_PIECES:
180             chesspiece = CHESS_PIECES[self.data['chesspiece']]
181             board.allow_chess_move(chesspiece)