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