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