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 tile'
46 substitutions['location_bits'] = bit_glyphs(
47 location.bitwise_operand)
50 if self.data.get('message', None) is not None:
51 text = self.data['message']
53 return text % substitutions
55 def check_available(self, player):
56 return player.bits.check_bits(self.required_bits)
58 def perform_action(self, board, location):
59 raise NotImplementedError(
60 "%s does not implement perform_action()." % (type(self).__name__,))
62 def check_and_clear_MSB(self, player):
63 if player.bits.check_bit(BITS.MSB):
64 player.bits.clear_bit(BITS.MSB)
70 return {'required_bits': list(self.required_bits),
72 'action_class': self.__class__.__name__}
75 class DoNothing(LocationAction):
77 GLYPHS = (ACTION_GLYPHS.NOTHING,)
79 def perform_action(self, board, location):
83 class LoseHealthOrMSB(LocationAction):
84 TEXT = "Lose {HEALTH} or {MSB}."
85 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
87 def perform_action(self, board, location):
88 if not self.check_and_clear_MSB(board.player):
89 sound.play_sound('awwww.ogg')
93 class SetBits(LocationAction):
94 TEXT = "Set %(location_bits)s."
95 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
97 def perform_action(self, board, location):
98 board.player.bits.set_bits(location.bitwise_operand)
101 class ClearBits(LocationAction):
102 TEXT = "Clear %(location_bits)s."
103 GLYPHS = (ACTION_GLYPHS.CLEAR_BITS,)
105 def perform_action(self, board, location):
106 board.player.bits.clear_bits(location.bitwise_operand)
109 class ToggleBits(LocationAction):
110 TEXT = "Toggle %(location_bits)s."
111 GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
113 def perform_action(self, board, location):
114 board.player.bits.toggle_bits(location.bitwise_operand)
117 class ShiftBits(LocationAction):
118 TEXT = "Barrel-shift player bits %(shift_glyph)s %(shift)s."
119 GLYPHS = (ACTION_GLYPHS.SHIFT_LEFT,)
121 def perform_action(self, board, location):
122 shift = self.data['shift']
123 if self.data['direction'] == 'left':
124 board.player.bits.shift_bits_left(shift)
126 board.player.bits.shift_bits_right(shift)
129 class LoseHealthOrMSBAndSetBits(LocationAction):
130 TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
131 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
132 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
134 def perform_action(self, board, location):
135 if not self.check_and_clear_MSB(board.player):
136 sound.play_sound('awwww.ogg')
138 board.player.bits.set_bits(location.bitwise_operand)
141 class AcquireWinToken(LocationAction):
142 TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
143 GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
145 def perform_action(self, board, location):
146 sound.play_sound('yipee.ogg')
147 board.acquire_win_token()
148 board.player.bits.clear_bits(set([
149 BITS.RED, BITS.GREEN, BITS.BLUE,
153 class GainHealth(LocationAction):
154 TEXT = "Gain {HEALTH}."
155 GLYPHS = (ACTION_GLYPHS.HEAL,)
157 def perform_action(self, board, location):
158 sound.play_sound('aha.ogg')
162 class GainHealthAndClearBitsOrMSB(LocationAction):
163 TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
164 GLYPHS = (ACTION_GLYPHS.HEAL,)
165 MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
167 def perform_action(self, board, location):
168 sound.play_sound('aha.ogg')
170 if not self.check_and_clear_MSB(board.player):
171 board.player.bits.clear_bits(location.bitwise_operand)
174 class ShiftLocations(LocationAction):
175 TEXT = "Shift current %(rowcol)s %(direction)s."
176 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
178 def perform_action(self, board, location):
179 sound.play_sound('grind.ogg')
180 board.shift_locations(self.data['direction'])
183 class RotateLocations(LocationAction):
184 TEXT = "Rotate adjacent tiles %(rot_direction_name)s."
185 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
187 def perform_action(self, board, location):
188 sound.play_sound('grind.ogg')
189 board.rotate_locations(self.data['rot_direction'])
192 class AllowChessMove(LocationAction):
193 TEXT = "Move like a %(chesspiece_name)s for one turn."
194 GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
196 def perform_action(self, board, location):
197 if self.data['chesspiece'] in CHESS_PIECES:
198 chesspiece = CHESS_PIECES[self.data['chesspiece']]
199 board.allow_chess_move(chesspiece)
202 class AllowChessMoveIfMSB(LocationAction):
204 "Clear {MSB} and move like a %(chesspiece_name)s for one turn if it "
206 MSB_GLYPH = ACTION_GLYPHS.MOVEMENT
208 def perform_action(self, board, location):
209 if self.data['chesspiece'] in CHESS_PIECES:
210 if self.check_and_clear_MSB(board.player):
211 chesspiece = CHESS_PIECES[self.data['chesspiece']]
212 board.allow_chess_move(chesspiece)
215 class GainMSB(LocationAction):
217 GLYPHS = (ACTION_GLYPHS.MSB,)
219 def perform_action(self, board, location):
220 board.player.bits.set_bit(BITS.MSB)