Make action.GLYPHS a function, action.get_glyphs.
[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, parse_bits
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_glyphs(self):
20         return self.GLYPHS
21
22     def get_text(self, location=None):
23         substitutions = self.data.copy()
24
25         if 'shift' in self.data:
26             substitutions['shift'] = self.data['shift']
27             substitutions['shift_glyph'] = ('{SHIFT_%s}'
28                                             % self.data['direction'].upper())
29         elif 'direction' in self.data:
30             substitutions['rowcol'] = {
31                 'NORTH': 'column',
32                 'SOUTH': 'column',
33                 'EAST': 'row',
34                 'WEST': 'row',
35             }[self.data['direction']]
36             substitutions['direction'] = '{%s}' % (substitutions['direction'],)
37
38         if 'chesspiece' in self.data:
39             substitutions['chesspiece_name'] = move_glyph(
40                 self.data['chesspiece'])
41
42         if 'rot_direction' in self.data:
43             substitutions['rot_direction_name'] = '{%s}' % (
44                 substitutions['rot_direction'],)
45
46         if location is None:
47             substitutions['location_bits'] = 'bits specified by this tile'
48         else:
49             substitutions['location_bits'] = bit_glyphs(
50                 location.bitwise_operand)
51
52         text = self.TEXT
53         if self.data.get('message', None) is not None:
54             text = self.data['message']
55
56         return text % substitutions
57
58     def check_available(self, player):
59         return player.bits.check_bits(self.required_bits)
60
61     def perform_action(self, board, location):
62         raise NotImplementedError(
63             "%s does not implement perform_action()." % (type(self).__name__,))
64
65     def check_and_clear_MSB(self, player):
66         if player.bits.check_bit(BITS.MSB):
67             player.bits.clear_bit(BITS.MSB)
68             return True
69         else:
70             return False
71
72     def export(self):
73         return {'required_bits': list(self.required_bits),
74                 'data': self.data,
75                 'action_class': self.__class__.__name__}
76
77
78 class DoNothing(LocationAction):
79     TEXT = "No effect."
80     GLYPHS = (ACTION_GLYPHS.NOTHING,)
81
82     def perform_action(self, board, location):
83         pass
84
85
86 class LoseHealthOrMSB(LocationAction):
87     TEXT = "Lose {HEALTH} or {MSB}."
88     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
89
90     def perform_action(self, board, location):
91         if not self.check_and_clear_MSB(board.player):
92             sound.play_sound('awwww.ogg')
93             board.lose_health()
94
95
96 class SetBits(LocationAction):
97     TEXT = "Set %(location_bits)s."
98     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
99
100     def perform_action(self, board, location):
101         board.player.bits.set_bits(location.bitwise_operand)
102
103
104 class ClearBits(LocationAction):
105     TEXT = "Clear %(location_bits)s."
106     GLYPHS = (ACTION_GLYPHS.CLEAR_BITS,)
107
108     def perform_action(self, board, location):
109         board.player.bits.clear_bits(location.bitwise_operand)
110
111
112 class ToggleBits(LocationAction):
113     TEXT = "Toggle %(location_bits)s."
114     GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
115
116     def perform_action(self, board, location):
117         board.player.bits.toggle_bits(location.bitwise_operand)
118
119
120 class GenericBits(LocationAction):
121     GLYPHS = (ACTION_GLYPHS.SET_BITS, ACTION_GLYPHS.CLEAR_BITS)
122
123     def __init__(self, *args, **kw):
124         super(GenericBits, self).__init__(*args, **kw)
125         self.set_bits = parse_bits(self.data.get('set', []))
126         self.clear_bits = parse_bits(self.data.get('clear', []))
127         self.toggle_bits = parse_bits(self.data.get('toggle', []))
128
129     def perform_action(self, board, location):
130         bits = board.player.bits
131         bits.set_bits(self.set_bits)
132         bits.toggle_bits(self.toggle_bits)
133         bits.clear_bits(self.clear_bits)
134
135     def get_text(self, location=None):
136         if 'message' in self.data:
137             return super(GenericBits, self).get_text()
138         parts = []
139         for template, bits in [
140                 ('Set %s.', self.set_bits), ('Clear %s.', self.clear_bits),
141                 ('Toggle %s', self.toggle_bits)]:
142             if bits:
143                 parts.append(template % (bit_glyphs(bits)))
144         return " ".join(parts)
145
146
147 class ShiftBits(LocationAction):
148     TEXT = "Barrel-shift player bits %(shift_glyph)s %(shift)s."
149     GLYPHS = (ACTION_GLYPHS.SHIFT_LEFT,)
150
151     def perform_action(self, board, location):
152         shift = self.data['shift']
153         if self.data['direction'] == 'left':
154             board.player.bits.shift_bits_left(shift)
155         else:
156             board.player.bits.shift_bits_right(shift)
157
158
159 class LoseHealthOrMSBAndSetBits(LocationAction):
160     TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
161     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
162     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
163
164     def perform_action(self, board, location):
165         if not self.check_and_clear_MSB(board.player):
166             sound.play_sound('awwww.ogg')
167             board.lose_health()
168         board.player.bits.set_bits(location.bitwise_operand)
169
170
171 class AcquireWinToken(LocationAction):
172     TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
173     GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
174
175     def perform_action(self, board, location):
176         sound.play_sound('yipee.ogg')
177         board.acquire_win_token()
178         board.player.bits.clear_bits(set([
179             BITS.RED, BITS.GREEN, BITS.BLUE,
180         ]))
181         if self.data.get('once', False):
182             location.actions.remove(self)
183
184
185 class GainHealth(LocationAction):
186     TEXT = "Gain {HEALTH}."
187     GLYPHS = (ACTION_GLYPHS.HEAL,)
188
189     def perform_action(self, board, location):
190         sound.play_sound('aha.ogg')
191         board.gain_health()
192
193
194 class GainHealthAndClearBitsOrMSB(LocationAction):
195     TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
196     GLYPHS = (ACTION_GLYPHS.HEAL,)
197     MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
198
199     def perform_action(self, board, location):
200         sound.play_sound('aha.ogg')
201         board.gain_health()
202         if not self.check_and_clear_MSB(board.player):
203             board.player.bits.clear_bits(location.bitwise_operand)
204
205
206 class ShiftLocations(LocationAction):
207     TEXT = "Shift current %(rowcol)s %(direction)s."
208     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
209
210     def perform_action(self, board, location):
211         sound.play_sound('grind.ogg')
212         board.shift_locations(self.data['direction'])
213
214
215 class RotateLocations(LocationAction):
216     TEXT = "Rotate adjacent tiles %(rot_direction_name)s."
217     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
218
219     def perform_action(self, board, location):
220         sound.play_sound('grind.ogg')
221         board.rotate_locations(self.data['rot_direction'])
222
223
224 class AllowChessMove(LocationAction):
225     TEXT = "Move like a %(chesspiece_name)s for one turn."
226     GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
227
228     def perform_action(self, board, location):
229         if self.data['chesspiece'] in CHESS_PIECES:
230             chesspiece = CHESS_PIECES[self.data['chesspiece']]
231             board.allow_chess_move(chesspiece)
232
233
234 class AllowChessMoveIfMSB(LocationAction):
235     TEXT = (
236         "Clear {MSB} and move like a %(chesspiece_name)s for one turn if it "
237         "was set.")
238     MSB_GLYPH = ACTION_GLYPHS.MOVEMENT
239
240     def perform_action(self, board, location):
241         if self.data['chesspiece'] in CHESS_PIECES:
242             if self.check_and_clear_MSB(board.player):
243                 chesspiece = CHESS_PIECES[self.data['chesspiece']]
244                 board.allow_chess_move(chesspiece)
245
246
247 class GainMSB(LocationAction):
248     TEXT = "Set {MSB}."
249     GLYPHS = (ACTION_GLYPHS.MSB,)
250
251     def perform_action(self, board, location):
252         board.player.bits.set_bit(BITS.MSB)