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 'direction' in self.data:
23 substitutions['rowcol'] = {
28 }[self.data['direction']]
29 substitutions['direction'] = '{%s}' % (substitutions['direction'],)
31 if 'chesspiece' in self.data:
32 substitutions['chesspiece_name'] = move_glyph(
33 self.data['chesspiece'])
35 if 'rot_direction' in self.data:
36 substitutions['rot_direction_name'] = '{%s}' % (
37 substitutions['rot_direction'],)
40 substitutions['location_bits'] = 'bits specified by this location'
42 substitutions['location_bits'] = bit_glyphs(
43 location.bitwise_operand)
45 return self.TEXT % substitutions
47 def check_available(self, player):
48 return player.bits.check_bits(self.required_bits)
50 def perform_action(self, board, location):
51 raise NotImplementedError(
52 "%s does not implement perform_action()." % (type(self).__name__,))
54 def check_and_clear_MSB(self, player):
55 if player.bits.check_bit(BITS.MSB):
56 player.bits.clear_bit(BITS.MSB)
62 return {'required_bits': list(self.required_bits),
64 'action_class': self.__class__.__name__}
67 class DoNothing(LocationAction):
69 GLYPHS = (ACTION_GLYPHS.NOTHING,)
71 def perform_action(self, board, location):
75 class LoseHealthOrMSB(LocationAction):
76 TEXT = "Lose {HEALTH} or {MSB}."
77 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
79 def perform_action(self, board, location):
80 if not self.check_and_clear_MSB(board.player):
84 class SetBits(LocationAction):
85 TEXT = "Set %(location_bits)s."
86 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
88 def perform_action(self, board, location):
89 board.player.bits.set_bits(location.bitwise_operand)
92 class ToggleBits(LocationAction):
93 TEXT = "Toggle %(location_bits)s."
94 GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
96 def perform_action(self, board, location):
97 board.player.bits.toggle_bits(location.bitwise_operand)
100 class LoseHealthOrMSBAndSetBits(LocationAction):
101 TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
102 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
103 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
105 def perform_action(self, board, location):
106 if not self.check_and_clear_MSB(board.player):
108 board.player.bits.set_bits(location.bitwise_operand)
111 class AcquireWinToken(LocationAction):
112 TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
113 GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
115 def perform_action(self, board, location):
116 board.acquire_win_token()
117 board.player.bits.clear_bits(set([
118 BITS.RED, BITS.GREEN, BITS.BLUE,
122 class GainHealth(LocationAction):
123 TEXT = "Gain {HEALTH}."
124 GLYPHS = (ACTION_GLYPHS.HEAL,)
126 def perform_action(self, board, location):
130 class GainHealthAndClearBitsOrMSB(LocationAction):
131 TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
132 GLYPHS = (ACTION_GLYPHS.HEAL,)
133 MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
135 def perform_action(self, board, location):
137 if not self.check_and_clear_MSB(board.player):
138 board.player.bits.clear_bits(location.bitwise_operand)
141 class ShiftLocations(LocationAction):
142 TEXT = "Shift current %(rowcol)s %(direction)s."
143 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
145 def perform_action(self, board, location):
146 sound.play_sound('grind.ogg')
147 board.shift_locations(self.data['direction'])
150 class RotateLocations(LocationAction):
151 TEXT = "Rotate adjacent locations %(rot_direction_name)s."
152 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
154 def perform_action(self, board, location):
155 sound.play_sound('grind.ogg')
156 board.rotate_locations(self.data['rot_direction'])
159 class AllowChessMove(LocationAction):
160 TEXT = "Move like a %(chesspiece_name)s for one turn."
161 GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
163 def perform_action(self, board, location):
164 if self.data['chesspiece'] in CHESS_PIECES:
165 chesspiece = CHESS_PIECES[self.data['chesspiece']]
166 board.allow_chess_move(chesspiece)