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