ff5bdba7dbfd6037afdd5764d4cf30c0ec138c60
[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     def take_damage(self, board):
81         sound.play_sound('awwww.ogg')
82         board.lose_health()
83
84
85 class DoNothing(LocationAction):
86     TEXT = "No effect."
87     GLYPHS = (ACTION_GLYPHS.NOTHING,)
88
89     def perform_action(self, board, location):
90         pass
91
92
93 class LoseHealthOrMSB(LocationAction):
94     TEXT = "Lose {HEALTH} or {MSB}."
95     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
96
97     def perform_action(self, board, location):
98         if not self.check_and_clear_MSB(board.player):
99             self.take_damage(board)
100
101
102 class SetBits(LocationAction):
103     TEXT = "Set %(location_bits)s."
104     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
105
106     def perform_action(self, board, location):
107         board.player.bits.set_bits(location.bitwise_operand)
108
109
110 class ClearBits(LocationAction):
111     TEXT = "Clear %(location_bits)s."
112     GLYPHS = (ACTION_GLYPHS.CLEAR_BITS,)
113
114     def perform_action(self, board, location):
115         board.player.bits.clear_bits(location.bitwise_operand)
116
117
118 class ClearBitsAndHealth(LocationAction):
119     TEXT = "Clear %(location_bits)s and {HEALTH}."
120     GLYPHS = (ACTION_GLYPHS.CLEAR_BITS, ACTION_GLYPHS.DAMAGE)
121
122     def perform_action(self, board, location):
123         board.player.bits.clear_bits(location.bitwise_operand)
124         self.take_damage(board)
125
126
127 class ToggleBits(LocationAction):
128     TEXT = "Toggle %(location_bits)s."
129     GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
130
131     def perform_action(self, board, location):
132         board.player.bits.toggle_bits(location.bitwise_operand)
133
134
135 class ToggleBitsAndHarm(LocationAction):
136     TEXT = "Toggle %(location_bits)s and lose {HEALTH}."
137     GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS, ACTION_GLYPHS.DAMAGE)
138
139     def perform_action(self, board, location):
140         board.player.bits.toggle_bits(location.bitwise_operand)
141         self.take_damage(board)
142
143
144 class GenericBits(LocationAction):
145     GLYPHS = (ACTION_GLYPHS.SET_BITS, ACTION_GLYPHS.CLEAR_BITS)
146
147     def __init__(self, *args, **kw):
148         super(GenericBits, self).__init__(*args, **kw)
149         self.set_bits = parse_bits(self.data.get('set', []))
150         self.clear_bits = parse_bits(self.data.get('clear', []))
151         self.toggle_bits = parse_bits(self.data.get('toggle', []))
152         self.once = self.data.get('once', False)
153         self.acquire_win = self.data.get('acquire_win', False)
154         self.lose_health = self.data.get('lose_health', False)
155
156     def perform_action(self, board, location):
157         bits = board.player.bits
158         bits.set_bits(self.set_bits)
159         bits.toggle_bits(self.toggle_bits)
160         bits.clear_bits(self.clear_bits)
161         if self.acquire_win:
162             sound.play_sound('yipee.ogg')
163             board.acquire_win_token()
164         if self.lose_health:
165             sound.play_sound('awwww.ogg')
166             board.lose_health()
167         if self.once:
168             location.actions.remove(self)
169
170     def get_glyphs(self):
171         glyphs = []
172         if self.acquire_win:
173             glyphs.append(ACTION_GLYPHS.WINTOKEN)
174         if self.lose_health:
175             glyphs.append(ACTION_GLYPHS.DAMAGE)
176         if self.set_bits:
177             glyphs.append(ACTION_GLYPHS.SET_BITS)
178         if self.clear_bits:
179             glyphs.append(ACTION_GLYPHS.CLEAR_BITS)
180         if self.toggle_bits:
181             glyphs.append(ACTION_GLYPHS.TOGGLE_BITS)
182         return tuple(glyphs)
183
184     def get_text(self, location=None):
185         if 'message' in self.data:
186             return super(GenericBits, self).get_text()
187         parts = []
188         if self.acquire_win:
189             parts.append("Gain a {WINTOKEN}.")
190         if self.lose_health:
191             parts.append("Lose {HEALTH}.")
192         for template, bits in [
193                 ('Set %s.', self.set_bits), ('Clear %s.', self.clear_bits),
194                 ('Toggle %s.', self.toggle_bits)]:
195             if bits:
196                 parts.append(template % (bit_glyphs(bits)))
197         if self.once:
198             parts.append('Usable once only.')
199         return " ".join(parts)
200
201
202 class ShiftBits(LocationAction):
203     TEXT = "Barrel-shift player bits %(shift_glyph)s %(shift)s."
204     GLYPHS = (ACTION_GLYPHS.SHIFT_LEFT,)
205
206     def perform_action(self, board, location):
207         shift = self.data['shift']
208         if self.data['direction'] == 'left':
209             board.player.bits.shift_bits_left(shift)
210         else:
211             board.player.bits.shift_bits_right(shift)
212
213
214 class LoseHealthOrMSBAndSetBits(LocationAction):
215     TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
216     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
217     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
218
219     def perform_action(self, board, location):
220         if not self.check_and_clear_MSB(board.player):
221             sound.play_sound('awwww.ogg')
222             board.lose_health()
223         board.player.bits.set_bits(location.bitwise_operand)
224
225
226 class AcquireWinToken(LocationAction):
227     TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
228     GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
229
230     def perform_action(self, board, location):
231         sound.play_sound('yipee.ogg')
232         board.acquire_win_token()
233         board.player.bits.clear_bits(set([
234             BITS.RED, BITS.GREEN, BITS.BLUE,
235         ]))
236
237 class AcquireWinTokenAndLoseHealth(AcquireWinToken):
238     TEXT = "Gain {WINTOKEN}, lose {HEALTH}, then clear {RED,GREEN,BLUE}."
239     GLYPHS = (ACTION_GLYPHS.WINTOKEN, ACTION_GLYPHS.DAMAGE)
240
241     def perform_action(self, board, location):
242         self.take_damage(board)
243         super(AcquireWinTokenAndLoseHealth, self).perform_action(board,
244                                                                 location)
245
246 class GainHealth(LocationAction):
247     TEXT = "Gain {HEALTH}."
248     GLYPHS = (ACTION_GLYPHS.HEAL,)
249
250     def perform_action(self, board, location):
251         sound.play_sound('aha.ogg')
252         board.gain_health()
253
254
255 class GainHealthAndClearBitsOrMSB(LocationAction):
256     TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
257     GLYPHS = (ACTION_GLYPHS.HEAL,)
258     MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
259
260     def perform_action(self, board, location):
261         sound.play_sound('aha.ogg')
262         board.gain_health()
263         if not self.check_and_clear_MSB(board.player):
264             board.player.bits.clear_bits(location.bitwise_operand)
265
266
267 class ShiftLocations(LocationAction):
268     TEXT = "Shift current %(rowcol)s %(direction)s."
269     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
270
271     def perform_action(self, board, location):
272         sound.play_sound('grind.ogg')
273         board.shift_locations(self.data['direction'])
274
275
276 class RotateLocations(LocationAction):
277     TEXT = "Rotate adjacent tiles %(rot_direction_name)s."
278     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
279
280     def perform_action(self, board, location):
281         sound.play_sound('grind.ogg')
282         board.rotate_locations(self.data['rot_direction'])
283
284
285 class AllowChessMove(LocationAction):
286     TEXT = "Move like a %(chesspiece_name)s for one turn."
287     GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
288
289     def perform_action(self, board, location):
290         if self.data['chesspiece'] in CHESS_PIECES:
291             chesspiece = CHESS_PIECES[self.data['chesspiece']]
292             board.allow_chess_move(chesspiece)
293
294
295 class AllowChessMoveIfMSB(LocationAction):
296     TEXT = (
297         "Clear {MSB} and move like a %(chesspiece_name)s for one turn if it "
298         "was set.")
299     MSB_GLYPH = ACTION_GLYPHS.MOVEMENT
300
301     def perform_action(self, board, location):
302         if self.data['chesspiece'] in CHESS_PIECES:
303             if self.check_and_clear_MSB(board.player):
304                 chesspiece = CHESS_PIECES[self.data['chesspiece']]
305                 board.allow_chess_move(chesspiece)
306
307
308 class GainMSB(LocationAction):
309     TEXT = "Set {MSB}."
310     GLYPHS = (ACTION_GLYPHS.MSB,)
311
312     def perform_action(self, board, location):
313         board.player.bits.set_bit(BITS.MSB)