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
6 class LocationAction(object):
8 An action that may be performed on a location.
15 def __init__(self, required_bits, **data):
16 self.required_bits = required_bits
22 def get_text(self, location=None):
23 substitutions = self.data.copy()
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'] = {
35 }[self.data['direction']]
36 substitutions['direction'] = '{%s}' % (substitutions['direction'],)
38 if 'chesspiece' in self.data:
39 substitutions['chesspiece_name'] = move_glyph(
40 self.data['chesspiece'])
42 if 'rot_direction' in self.data:
43 substitutions['rot_direction_name'] = '{%s}' % (
44 substitutions['rot_direction'],)
47 substitutions['location_bits'] = 'bits specified by this tile'
49 substitutions['location_bits'] = bit_glyphs(
50 location.bitwise_operand)
53 if self.data.get('message', None) is not None:
54 text = self.data['message']
56 return text % substitutions
58 def check_available(self, player):
59 return player.bits.check_bits(self.required_bits)
61 def perform_action(self, board, location):
62 raise NotImplementedError(
63 "%s does not implement perform_action()." % (type(self).__name__,))
65 def check_and_clear_MSB(self, player):
66 if player.bits.check_bit(BITS.MSB):
67 player.bits.clear_bit(BITS.MSB)
73 return {'required_bits': list(self.required_bits),
75 'action_class': self.__class__.__name__}
78 class DoNothing(LocationAction):
80 GLYPHS = (ACTION_GLYPHS.NOTHING,)
82 def perform_action(self, board, location):
86 class LoseHealthOrMSB(LocationAction):
87 TEXT = "Lose {HEALTH} or {MSB}."
88 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
90 def perform_action(self, board, location):
91 if not self.check_and_clear_MSB(board.player):
92 sound.play_sound('awwww.ogg')
96 class SetBits(LocationAction):
97 TEXT = "Set %(location_bits)s."
98 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
100 def perform_action(self, board, location):
101 board.player.bits.set_bits(location.bitwise_operand)
104 class ClearBits(LocationAction):
105 TEXT = "Clear %(location_bits)s."
106 GLYPHS = (ACTION_GLYPHS.CLEAR_BITS,)
108 def perform_action(self, board, location):
109 board.player.bits.clear_bits(location.bitwise_operand)
112 class ToggleBits(LocationAction):
113 TEXT = "Toggle %(location_bits)s."
114 GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
116 def perform_action(self, board, location):
117 board.player.bits.toggle_bits(location.bitwise_operand)
120 class GenericBits(LocationAction):
121 GLYPHS = (ACTION_GLYPHS.SET_BITS, ACTION_GLYPHS.CLEAR_BITS)
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', []))
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)
135 def get_text(self, location=None):
136 if 'message' in self.data:
137 return super(GenericBits, self).get_text()
139 for template, bits in [
140 ('Set %s.', self.set_bits), ('Clear %s.', self.clear_bits),
141 ('Toggle %s', self.toggle_bits)]:
143 parts.append(template % (bit_glyphs(bits)))
144 return " ".join(parts)
147 class ShiftBits(LocationAction):
148 TEXT = "Barrel-shift player bits %(shift_glyph)s %(shift)s."
149 GLYPHS = (ACTION_GLYPHS.SHIFT_LEFT,)
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)
156 board.player.bits.shift_bits_right(shift)
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
164 def perform_action(self, board, location):
165 if not self.check_and_clear_MSB(board.player):
166 sound.play_sound('awwww.ogg')
168 board.player.bits.set_bits(location.bitwise_operand)
171 class AcquireWinToken(LocationAction):
172 TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
173 GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
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,
181 if self.data.get('once', False):
182 location.actions.remove(self)
185 class GainHealth(LocationAction):
186 TEXT = "Gain {HEALTH}."
187 GLYPHS = (ACTION_GLYPHS.HEAL,)
189 def perform_action(self, board, location):
190 sound.play_sound('aha.ogg')
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
199 def perform_action(self, board, location):
200 sound.play_sound('aha.ogg')
202 if not self.check_and_clear_MSB(board.player):
203 board.player.bits.clear_bits(location.bitwise_operand)
206 class ShiftLocations(LocationAction):
207 TEXT = "Shift current %(rowcol)s %(direction)s."
208 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
210 def perform_action(self, board, location):
211 sound.play_sound('grind.ogg')
212 board.shift_locations(self.data['direction'])
215 class RotateLocations(LocationAction):
216 TEXT = "Rotate adjacent tiles %(rot_direction_name)s."
217 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
219 def perform_action(self, board, location):
220 sound.play_sound('grind.ogg')
221 board.rotate_locations(self.data['rot_direction'])
224 class AllowChessMove(LocationAction):
225 TEXT = "Move like a %(chesspiece_name)s for one turn."
226 GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
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)
234 class AllowChessMoveIfMSB(LocationAction):
236 "Clear {MSB} and move like a %(chesspiece_name)s for one turn if it "
238 MSB_GLYPH = ACTION_GLYPHS.MOVEMENT
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)
247 class GainMSB(LocationAction):
249 GLYPHS = (ACTION_GLYPHS.MSB,)
251 def perform_action(self, board, location):
252 board.player.bits.set_bit(BITS.MSB)