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.
12 GLYPHS = (ACTION_GLYPHS.NOTHING,)
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):
70 def perform_action(self, board, location):
74 class LoseHealthOrMSB(LocationAction):
75 TEXT = "Lose {HEALTH} or {MSB}."
76 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
78 def perform_action(self, board, location):
79 if not self.check_and_clear_MSB(board.player):
83 class SetBits(LocationAction):
84 TEXT = "Set %(location_bits)s."
85 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
87 def perform_action(self, board, location):
88 board.player.bits.set_bits(location.bitwise_operand)
91 class ToggleBits(LocationAction):
92 TEXT = "Toggle %(location_bits)s."
93 GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
95 def perform_action(self, board, location):
96 board.player.bits.toggle_bits(location.bitwise_operand)
99 class LoseHealthOrMSBAndSetBits(LocationAction):
100 TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
101 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
102 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
104 def perform_action(self, board, location):
105 if not self.check_and_clear_MSB(board.player):
107 board.player.bits.set_bits(location.bitwise_operand)
110 class AcquireWinToken(LocationAction):
111 TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
112 GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
114 def perform_action(self, board, location):
115 board.acquire_win_token()
116 board.player.bits.clear_bits(set([
117 BITS.RED, BITS.GREEN, BITS.BLUE,
121 class GainHealth(LocationAction):
122 TEXT = "Gain {HEALTH}."
123 GLYPHS = (ACTION_GLYPHS.HEAL,)
125 def perform_action(self, board, location):
129 class GainHealthAndClearBitsOrMSB(LocationAction):
130 TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
131 GLYPHS = (ACTION_GLYPHS.HEAL,)
132 MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
134 def perform_action(self, board, location):
136 if not self.check_and_clear_MSB(board.player):
137 board.player.bits.clear_bits(location.bitwise_operand)
140 class ShiftLocations(LocationAction):
141 TEXT = "Shift current %(rowcol)s %(direction)s."
142 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
144 def perform_action(self, board, location):
145 sound.play_sound('grind.ogg')
146 board.shift_locations(self.data['direction'])
149 class RotateLocations(LocationAction):
150 TEXT = "Rotate adjacent locations %(rot_direction_name)s."
151 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
153 def perform_action(self, board, location):
154 sound.play_sound('grind.ogg')
155 board.rotate_locations(self.data['rot_direction'])
158 class AllowChessMove(LocationAction):
159 TEXT = "Move like a %(chesspiece_name)s for one turn."
160 GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
162 def perform_action(self, board, location):
163 if self.data['chesspiece'] in CHESS_PIECES:
164 chesspiece = CHESS_PIECES[self.data['chesspiece']]
165 board.allow_chess_move(chesspiece)