1 from naja.constants import BITS, CHESS_PIECES
2 from naja.utils import bit_glyphs, move_glyph
5 class LocationAction(object):
7 An action that may be performed on a location.
13 def __init__(self, required_bits, **data):
14 self.required_bits = required_bits
17 def get_text(self, location=None):
18 substitutions = self.data.copy()
20 if 'direction' in self.data:
21 substitutions['rowcol'] = {
26 }[self.data['direction']]
27 substitutions['direction'] = '{%s}' % (substitutions['direction'],)
29 if 'chesspiece' in self.data:
30 substitutions['chesspiece_name'] = move_glyph(
31 self.data['chesspiece'])
34 substitutions['location_bits'] = 'bits specified by this location'
36 substitutions['location_bits'] = bit_glyphs(
37 location.bitwise_operand)
39 return self.TEXT % substitutions
41 def check_available(self, player):
42 return player.bits.check_bits(self.required_bits)
44 def perform_action(self, board, location):
45 raise NotImplementedError(
46 "%s does not implement perform_action()." % (type(self).__name__,))
48 def check_and_clear_MSB(self, player):
49 if player.bits.check_bit(BITS.MSB):
50 player.bits.clear_bit(BITS.MSB)
56 return {'required_bits': list(self.required_bits),
58 'action_class': self.__class__.__name__}
61 class DoNothing(LocationAction):
64 def perform_action(self, board, location):
68 class LoseHealthOrMSB(LocationAction):
69 TEXT = "Lose {HEALTH} or {MSB}."
72 def perform_action(self, board, location):
73 if not self.check_and_clear_MSB(board.player):
77 class SetBits(LocationAction):
78 TEXT = "Set %(location_bits)s."
80 def perform_action(self, board, location):
81 board.player.bits.set_bits(location.bitwise_operand)
84 class ToggleBits(LocationAction):
85 TEXT = "Toggle %(location_bits)s."
87 def perform_action(self, board, location):
88 board.player.bits.toggle_bits(location.bitwise_operand)
91 class LoseHealthOrMSBAndSetBits(LocationAction):
92 TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
95 def perform_action(self, board, location):
96 if not self.check_and_clear_MSB(board.player):
98 board.player.bits.set_bits(location.bitwise_operand)
101 class AcquireWinToken(LocationAction):
102 TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
104 def perform_action(self, board, location):
105 board.acquire_win_token()
106 board.player.bits.clear_bits(set([
107 BITS.RED, BITS.GREEN, BITS.BLUE,
111 class GainHealthAndClearBitsOrMSB(LocationAction):
112 TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
115 def perform_action(self, board, location):
117 if not self.check_and_clear_MSB(board.player):
118 board.player.bits.clear_bits(location.bitwise_operand)
121 class ShiftLocations(LocationAction):
122 TEXT = "Shift current %(rowcol)s %(direction)s."
124 def perform_action(self, board, location):
125 board.shift_locations(self.data['direction'])
128 class AllowChessMove(LocationAction):
129 TEXT = "Move like a %(chesspiece_name)s for one turn."
131 def perform_action(self, board, location):
132 if self.data['chesspiece'] in CHESS_PIECES:
133 chesspiece = CHESS_PIECES[self.data['chesspiece']]
134 board.allow_chess_move(chesspiece)