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['direction'] = self.data['direction']
25 elif 'direction' in self.data:
26 substitutions['rowcol'] = {
31 }[self.data['direction']]
32 substitutions['direction'] = '{%s}' % (substitutions['direction'],)
34 if 'chesspiece' in self.data:
35 substitutions['chesspiece_name'] = move_glyph(
36 self.data['chesspiece'])
38 if 'rot_direction' in self.data:
39 substitutions['rot_direction_name'] = '{%s}' % (
40 substitutions['rot_direction'],)
43 substitutions['location_bits'] = 'bits specified by this location'
45 substitutions['location_bits'] = bit_glyphs(
46 location.bitwise_operand)
48 return self.TEXT % substitutions
50 def check_available(self, player):
51 return player.bits.check_bits(self.required_bits)
53 def perform_action(self, board, location):
54 raise NotImplementedError(
55 "%s does not implement perform_action()." % (type(self).__name__,))
57 def check_and_clear_MSB(self, player):
58 if player.bits.check_bit(BITS.MSB):
59 player.bits.clear_bit(BITS.MSB)
65 return {'required_bits': list(self.required_bits),
67 'action_class': self.__class__.__name__}
70 class DoNothing(LocationAction):
72 GLYPHS = (ACTION_GLYPHS.NOTHING,)
74 def perform_action(self, board, location):
78 class LoseHealthOrMSB(LocationAction):
79 TEXT = "Lose {HEALTH} or {MSB}."
80 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
82 def perform_action(self, board, location):
83 if not self.check_and_clear_MSB(board.player):
87 class SetBits(LocationAction):
88 TEXT = "Set %(location_bits)s."
89 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
91 def perform_action(self, board, location):
92 board.player.bits.set_bits(location.bitwise_operand)
95 class ToggleBits(LocationAction):
96 TEXT = "Toggle %(location_bits)s."
97 GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
99 def perform_action(self, board, location):
100 board.player.bits.toggle_bits(location.bitwise_operand)
103 class ShiftBits(LocationAction):
104 TEXT = "Barrel-shift player %(shift)s bits %(direction)s."
105 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
107 def perform_action(self, board, location):
108 shift = self.data['shift']
109 if self.data['direction'] == 'left':
110 board.player.bits.shift_bits_left(shift)
112 board.player.bits.shift_bits_right(shift)
115 class LoseHealthOrMSBAndSetBits(LocationAction):
116 TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
117 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
118 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
120 def perform_action(self, board, location):
121 if not self.check_and_clear_MSB(board.player):
123 board.player.bits.set_bits(location.bitwise_operand)
126 class AcquireWinToken(LocationAction):
127 TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
128 GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
130 def perform_action(self, board, location):
131 board.acquire_win_token()
132 board.player.bits.clear_bits(set([
133 BITS.RED, BITS.GREEN, BITS.BLUE,
137 class GainHealth(LocationAction):
138 TEXT = "Gain {HEALTH}."
139 GLYPHS = (ACTION_GLYPHS.HEAL,)
141 def perform_action(self, board, location):
145 class GainHealthAndClearBitsOrMSB(LocationAction):
146 TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
147 GLYPHS = (ACTION_GLYPHS.HEAL,)
148 MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
150 def perform_action(self, board, location):
152 if not self.check_and_clear_MSB(board.player):
153 board.player.bits.clear_bits(location.bitwise_operand)
156 class ShiftLocations(LocationAction):
157 TEXT = "Shift current %(rowcol)s %(direction)s."
158 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
160 def perform_action(self, board, location):
161 sound.play_sound('grind.ogg')
162 board.shift_locations(self.data['direction'])
165 class RotateLocations(LocationAction):
166 TEXT = "Rotate adjacent locations %(rot_direction_name)s."
167 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
169 def perform_action(self, board, location):
170 sound.play_sound('grind.ogg')
171 board.rotate_locations(self.data['rot_direction'])
174 class AllowChessMove(LocationAction):
175 TEXT = "Move like a %(chesspiece_name)s for one turn."
176 GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
178 def perform_action(self, board, location):
179 if self.data['chesspiece'] in CHESS_PIECES:
180 chesspiece = CHESS_PIECES[self.data['chesspiece']]
181 board.allow_chess_move(chesspiece)