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
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
19 def get_text(self, location=None):
20 substitutions = self.data.copy()
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'] = {
32 }[self.data['direction']]
33 substitutions['direction'] = '{%s}' % (substitutions['direction'],)
35 if 'chesspiece' in self.data:
36 substitutions['chesspiece_name'] = move_glyph(
37 self.data['chesspiece'])
39 if 'rot_direction' in self.data:
40 substitutions['rot_direction_name'] = '{%s}' % (
41 substitutions['rot_direction'],)
44 substitutions['location_bits'] = 'bits specified by this location'
46 substitutions['location_bits'] = bit_glyphs(
47 location.bitwise_operand)
49 return self.TEXT % substitutions
51 def check_available(self, player):
52 return player.bits.check_bits(self.required_bits)
54 def perform_action(self, board, location):
55 raise NotImplementedError(
56 "%s does not implement perform_action()." % (type(self).__name__,))
58 def check_and_clear_MSB(self, player):
59 if player.bits.check_bit(BITS.MSB):
60 player.bits.clear_bit(BITS.MSB)
66 return {'required_bits': list(self.required_bits),
68 'action_class': self.__class__.__name__}
71 class DoNothing(LocationAction):
73 GLYPHS = (ACTION_GLYPHS.NOTHING,)
75 def perform_action(self, board, location):
79 class LoseHealthOrMSB(LocationAction):
80 TEXT = "Lose {HEALTH} or {MSB}."
81 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
83 def perform_action(self, board, location):
84 if not self.check_and_clear_MSB(board.player):
85 sound.play_sound('awwww.ogg')
89 class SetBits(LocationAction):
90 TEXT = "Set %(location_bits)s."
91 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
93 def perform_action(self, board, location):
94 board.player.bits.set_bits(location.bitwise_operand)
97 class ToggleBits(LocationAction):
98 TEXT = "Toggle %(location_bits)s."
99 GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
101 def perform_action(self, board, location):
102 board.player.bits.toggle_bits(location.bitwise_operand)
105 class ShiftBits(LocationAction):
106 TEXT = "Barrel-shift player bits %(shift_glyph)s %(shift)s."
107 GLYPHS = (ACTION_GLYPHS.SHIFT_LEFT,)
109 def perform_action(self, board, location):
110 shift = self.data['shift']
111 if self.data['direction'] == 'left':
112 board.player.bits.shift_bits_left(shift)
114 board.player.bits.shift_bits_right(shift)
117 class LoseHealthOrMSBAndSetBits(LocationAction):
118 TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
119 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
120 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
122 def perform_action(self, board, location):
123 if not self.check_and_clear_MSB(board.player):
124 sound.play_sound('awwww.ogg')
126 board.player.bits.set_bits(location.bitwise_operand)
129 class AcquireWinToken(LocationAction):
130 TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
131 GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
133 def perform_action(self, board, location):
134 sound.play_sound('yipee.ogg')
135 board.acquire_win_token()
136 board.player.bits.clear_bits(set([
137 BITS.RED, BITS.GREEN, BITS.BLUE,
141 class GainHealth(LocationAction):
142 TEXT = "Gain {HEALTH}."
143 GLYPHS = (ACTION_GLYPHS.HEAL,)
145 def perform_action(self, board, location):
146 sound.play_sound('aha.ogg')
150 class GainHealthAndClearBitsOrMSB(LocationAction):
151 TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
152 GLYPHS = (ACTION_GLYPHS.HEAL,)
153 MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
155 def perform_action(self, board, location):
156 sound.play_sound('aha.ogg')
158 if not self.check_and_clear_MSB(board.player):
159 board.player.bits.clear_bits(location.bitwise_operand)
162 class ShiftLocations(LocationAction):
163 TEXT = "Shift current %(rowcol)s %(direction)s."
164 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
166 def perform_action(self, board, location):
167 sound.play_sound('grind.ogg')
168 board.shift_locations(self.data['direction'])
171 class RotateLocations(LocationAction):
172 TEXT = "Rotate adjacent locations %(rot_direction_name)s."
173 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
175 def perform_action(self, board, location):
176 sound.play_sound('grind.ogg')
177 board.rotate_locations(self.data['rot_direction'])
180 class AllowChessMove(LocationAction):
181 TEXT = "Move like a %(chesspiece_name)s for one turn."
182 GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
184 def perform_action(self, board, location):
185 if self.data['chesspiece'] in CHESS_PIECES:
186 chesspiece = CHESS_PIECES[self.data['chesspiece']]
187 board.allow_chess_move(chesspiece)