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):
88 class SetBits(LocationAction):
89 TEXT = "Set %(location_bits)s."
90 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
92 def perform_action(self, board, location):
93 board.player.bits.set_bits(location.bitwise_operand)
96 class ToggleBits(LocationAction):
97 TEXT = "Toggle %(location_bits)s."
98 GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
100 def perform_action(self, board, location):
101 board.player.bits.toggle_bits(location.bitwise_operand)
104 class ShiftBits(LocationAction):
105 TEXT = "Barrel-shift player bits %(shift_glyph)s %(shift)s."
106 GLYPHS = (ACTION_GLYPHS.SHIFT_LEFT,)
108 def perform_action(self, board, location):
109 shift = self.data['shift']
110 if self.data['direction'] == 'left':
111 board.player.bits.shift_bits_left(shift)
113 board.player.bits.shift_bits_right(shift)
116 class LoseHealthOrMSBAndSetBits(LocationAction):
117 TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
118 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
119 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
121 def perform_action(self, board, location):
122 if not self.check_and_clear_MSB(board.player):
124 board.player.bits.set_bits(location.bitwise_operand)
127 class AcquireWinToken(LocationAction):
128 TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
129 GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
131 def perform_action(self, board, location):
132 board.acquire_win_token()
133 board.player.bits.clear_bits(set([
134 BITS.RED, BITS.GREEN, BITS.BLUE,
138 class GainHealth(LocationAction):
139 TEXT = "Gain {HEALTH}."
140 GLYPHS = (ACTION_GLYPHS.HEAL,)
142 def perform_action(self, board, location):
146 class GainHealthAndClearBitsOrMSB(LocationAction):
147 TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
148 GLYPHS = (ACTION_GLYPHS.HEAL,)
149 MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
151 def perform_action(self, board, location):
153 if not self.check_and_clear_MSB(board.player):
154 board.player.bits.clear_bits(location.bitwise_operand)
157 class ShiftLocations(LocationAction):
158 TEXT = "Shift current %(rowcol)s %(direction)s."
159 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
161 def perform_action(self, board, location):
162 sound.play_sound('grind.ogg')
163 board.shift_locations(self.data['direction'])
166 class RotateLocations(LocationAction):
167 TEXT = "Rotate adjacent locations %(rot_direction_name)s."
168 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
170 def perform_action(self, board, location):
171 sound.play_sound('grind.ogg')
172 board.rotate_locations(self.data['rot_direction'])
175 class AllowChessMove(LocationAction):
176 TEXT = "Move like a %(chesspiece_name)s for one turn."
177 GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
179 def perform_action(self, board, location):
180 if self.data['chesspiece'] in CHESS_PIECES:
181 chesspiece = CHESS_PIECES[self.data['chesspiece']]
182 board.allow_chess_move(chesspiece)